script.c 581 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327133281332913330133311333213333133341333513336133371333813339133401334113342133431334413345133461334713348133491335013351133521335313354133551335613357133581335913360133611336213363133641336513366133671336813369133701337113372133731337413375133761337713378133791338013381133821338313384133851338613387133881338913390133911339213393133941339513396133971339813399134001340113402134031340413405134061340713408134091341013411134121341313414134151341613417134181341913420134211342213423134241342513426134271342813429134301343113432134331343413435134361343713438134391344013441134421344313444134451344613447134481344913450134511345213453134541345513456134571345813459134601346113462134631346413465134661346713468134691347013471134721347313474134751347613477134781347913480134811348213483134841348513486134871348813489134901349113492134931349413495134961349713498134991350013501135021350313504135051350613507135081350913510135111351213513135141351513516135171351813519135201352113522135231352413525135261352713528135291353013531135321353313534135351353613537135381353913540135411354213543135441354513546135471354813549135501355113552135531355413555135561355713558135591356013561135621356313564135651356613567135681356913570135711357213573135741357513576135771357813579135801358113582135831358413585135861358713588135891359013591135921359313594135951359613597135981359913600136011360213603136041360513606136071360813609136101361113612136131361413615136161361713618136191362013621136221362313624136251362613627136281362913630136311363213633136341363513636136371363813639136401364113642136431364413645136461364713648136491365013651136521365313654136551365613657136581365913660136611366213663136641366513666136671366813669136701367113672136731367413675136761367713678136791368013681136821368313684136851368613687136881368913690136911369213693136941369513696136971369813699137001370113702137031370413705137061370713708137091371013711137121371313714137151371613717137181371913720137211372213723137241372513726137271372813729137301373113732137331373413735137361373713738137391374013741137421374313744137451374613747137481374913750137511375213753137541375513756137571375813759137601376113762137631376413765137661376713768137691377013771137721377313774137751377613777137781377913780137811378213783137841378513786137871378813789137901379113792137931379413795137961379713798137991380013801138021380313804138051380613807138081380913810138111381213813138141381513816138171381813819138201382113822138231382413825138261382713828138291383013831138321383313834138351383613837138381383913840138411384213843138441384513846138471384813849138501385113852138531385413855138561385713858138591386013861138621386313864138651386613867138681386913870138711387213873138741387513876138771387813879138801388113882138831388413885138861388713888138891389013891138921389313894138951389613897138981389913900139011390213903139041390513906139071390813909139101391113912139131391413915139161391713918139191392013921139221392313924139251392613927139281392913930139311393213933139341393513936139371393813939139401394113942139431394413945139461394713948139491395013951139521395313954139551395613957139581395913960139611396213963139641396513966139671396813969139701397113972139731397413975139761397713978139791398013981139821398313984139851398613987139881398913990139911399213993139941399513996139971399813999140001400114002140031400414005140061400714008140091401014011140121401314014140151401614017140181401914020140211402214023140241402514026140271402814029140301403114032140331403414035140361403714038140391404014041140421404314044140451404614047140481404914050140511405214053140541405514056140571405814059140601406114062140631406414065140661406714068140691407014071140721407314074140751407614077140781407914080140811408214083140841408514086140871408814089140901409114092140931409414095140961409714098140991410014101141021410314104141051410614107141081410914110141111411214113141141411514116141171411814119141201412114122141231412414125141261412714128141291413014131141321413314134141351413614137141381413914140141411414214143141441414514146141471414814149141501415114152141531415414155141561415714158141591416014161141621416314164141651416614167141681416914170141711417214173141741417514176141771417814179141801418114182141831418414185141861418714188141891419014191141921419314194141951419614197141981419914200142011420214203142041420514206142071420814209142101421114212142131421414215142161421714218142191422014221142221422314224142251422614227142281422914230142311423214233142341423514236142371423814239142401424114242142431424414245142461424714248142491425014251142521425314254142551425614257142581425914260142611426214263142641426514266142671426814269142701427114272142731427414275142761427714278142791428014281142821428314284142851428614287142881428914290142911429214293142941429514296142971429814299143001430114302143031430414305143061430714308143091431014311143121431314314143151431614317143181431914320143211432214323143241432514326143271432814329143301433114332143331433414335143361433714338143391434014341143421434314344143451434614347143481434914350143511435214353143541435514356143571435814359143601436114362143631436414365143661436714368143691437014371143721437314374143751437614377143781437914380143811438214383143841438514386143871438814389143901439114392143931439414395143961439714398143991440014401144021440314404144051440614407144081440914410144111441214413144141441514416144171441814419144201442114422144231442414425144261442714428144291443014431144321443314434144351443614437144381443914440144411444214443144441444514446144471444814449144501445114452144531445414455144561445714458144591446014461144621446314464144651446614467144681446914470144711447214473144741447514476144771447814479144801448114482144831448414485144861448714488144891449014491144921449314494144951449614497144981449914500145011450214503145041450514506145071450814509145101451114512145131451414515145161451714518145191452014521145221452314524145251452614527145281452914530145311453214533145341453514536145371453814539145401454114542145431454414545145461454714548145491455014551145521455314554145551455614557145581455914560145611456214563145641456514566145671456814569145701457114572145731457414575145761457714578145791458014581145821458314584145851458614587145881458914590145911459214593145941459514596145971459814599146001460114602146031460414605146061460714608146091461014611146121461314614146151461614617146181461914620146211462214623146241462514626146271462814629146301463114632146331463414635146361463714638146391464014641146421464314644146451464614647146481464914650146511465214653146541465514656146571465814659146601466114662146631466414665146661466714668146691467014671146721467314674146751467614677146781467914680146811468214683146841468514686146871468814689146901469114692146931469414695146961469714698146991470014701147021470314704147051470614707147081470914710147111471214713147141471514716147171471814719147201472114722147231472414725147261472714728147291473014731147321473314734147351473614737147381473914740147411474214743147441474514746147471474814749147501475114752147531475414755147561475714758147591476014761147621476314764147651476614767147681476914770147711477214773147741477514776147771477814779147801478114782147831478414785147861478714788147891479014791147921479314794147951479614797147981479914800148011480214803148041480514806148071480814809148101481114812148131481414815148161481714818148191482014821148221482314824148251482614827148281482914830148311483214833148341483514836148371483814839148401484114842148431484414845148461484714848148491485014851148521485314854148551485614857148581485914860148611486214863148641486514866148671486814869148701487114872148731487414875148761487714878148791488014881148821488314884148851488614887148881488914890148911489214893148941489514896148971489814899149001490114902149031490414905149061490714908149091491014911149121491314914149151491614917149181491914920149211492214923149241492514926149271492814929149301493114932149331493414935149361493714938149391494014941149421494314944149451494614947149481494914950149511495214953149541495514956149571495814959149601496114962149631496414965149661496714968149691497014971149721497314974149751497614977149781497914980149811498214983149841498514986149871498814989149901499114992149931499414995149961499714998149991500015001150021500315004150051500615007150081500915010150111501215013150141501515016150171501815019150201502115022150231502415025150261502715028150291503015031150321503315034150351503615037150381503915040150411504215043150441504515046150471504815049150501505115052150531505415055150561505715058150591506015061150621506315064150651506615067150681506915070150711507215073150741507515076150771507815079150801508115082150831508415085150861508715088150891509015091150921509315094150951509615097150981509915100151011510215103151041510515106151071510815109151101511115112151131511415115151161511715118151191512015121151221512315124151251512615127151281512915130151311513215133151341513515136151371513815139151401514115142151431514415145151461514715148151491515015151151521515315154151551515615157151581515915160151611516215163151641516515166151671516815169151701517115172151731517415175151761517715178151791518015181151821518315184151851518615187151881518915190151911519215193151941519515196151971519815199152001520115202152031520415205152061520715208152091521015211152121521315214152151521615217152181521915220152211522215223152241522515226152271522815229152301523115232152331523415235152361523715238152391524015241152421524315244152451524615247152481524915250152511525215253152541525515256152571525815259152601526115262152631526415265152661526715268152691527015271152721527315274152751527615277152781527915280152811528215283152841528515286152871528815289152901529115292152931529415295152961529715298152991530015301153021530315304153051530615307153081530915310153111531215313153141531515316153171531815319153201532115322153231532415325153261532715328153291533015331153321533315334153351533615337153381533915340153411534215343153441534515346153471534815349153501535115352153531535415355153561535715358153591536015361153621536315364153651536615367153681536915370153711537215373153741537515376153771537815379153801538115382153831538415385153861538715388153891539015391153921539315394153951539615397153981539915400154011540215403154041540515406154071540815409154101541115412154131541415415154161541715418154191542015421154221542315424154251542615427154281542915430154311543215433154341543515436154371543815439154401544115442154431544415445154461544715448154491545015451154521545315454154551545615457154581545915460154611546215463154641546515466154671546815469154701547115472154731547415475154761547715478154791548015481154821548315484154851548615487154881548915490154911549215493154941549515496154971549815499155001550115502155031550415505155061550715508155091551015511155121551315514155151551615517155181551915520155211552215523155241552515526155271552815529155301553115532155331553415535155361553715538155391554015541155421554315544155451554615547155481554915550155511555215553155541555515556155571555815559155601556115562155631556415565155661556715568155691557015571155721557315574155751557615577155781557915580155811558215583155841558515586155871558815589155901559115592155931559415595155961559715598155991560015601156021560315604156051560615607156081560915610156111561215613156141561515616156171561815619156201562115622156231562415625156261562715628156291563015631156321563315634156351563615637156381563915640156411564215643156441564515646156471564815649156501565115652156531565415655156561565715658156591566015661156621566315664156651566615667156681566915670156711567215673156741567515676156771567815679156801568115682156831568415685156861568715688156891569015691156921569315694156951569615697156981569915700157011570215703157041570515706157071570815709157101571115712157131571415715157161571715718157191572015721157221572315724157251572615727157281572915730157311573215733157341573515736157371573815739157401574115742157431574415745157461574715748157491575015751157521575315754157551575615757157581575915760157611576215763157641576515766157671576815769157701577115772157731577415775157761577715778157791578015781157821578315784157851578615787157881578915790157911579215793157941579515796157971579815799158001580115802158031580415805158061580715808158091581015811158121581315814158151581615817158181581915820158211582215823158241582515826158271582815829158301583115832158331583415835158361583715838158391584015841158421584315844158451584615847158481584915850158511585215853158541585515856158571585815859158601586115862158631586415865158661586715868158691587015871158721587315874158751587615877158781587915880158811588215883158841588515886158871588815889158901589115892158931589415895158961589715898158991590015901159021590315904159051590615907159081590915910159111591215913159141591515916159171591815919159201592115922159231592415925159261592715928159291593015931159321593315934159351593615937159381593915940159411594215943159441594515946159471594815949159501595115952159531595415955159561595715958159591596015961159621596315964159651596615967159681596915970159711597215973159741597515976159771597815979159801598115982159831598415985159861598715988159891599015991159921599315994159951599615997159981599916000160011600216003160041600516006160071600816009160101601116012160131601416015160161601716018160191602016021160221602316024160251602616027160281602916030160311603216033160341603516036160371603816039160401604116042160431604416045160461604716048160491605016051160521605316054160551605616057160581605916060160611606216063160641606516066160671606816069160701607116072160731607416075160761607716078160791608016081160821608316084160851608616087160881608916090160911609216093160941609516096160971609816099161001610116102161031610416105161061610716108161091611016111161121611316114161151611616117161181611916120161211612216123161241612516126161271612816129161301613116132161331613416135161361613716138161391614016141161421614316144161451614616147161481614916150161511615216153161541615516156161571615816159161601616116162161631616416165161661616716168161691617016171161721617316174161751617616177161781617916180161811618216183161841618516186161871618816189161901619116192161931619416195161961619716198161991620016201162021620316204162051620616207162081620916210162111621216213162141621516216162171621816219162201622116222162231622416225162261622716228162291623016231162321623316234162351623616237162381623916240162411624216243162441624516246162471624816249162501625116252162531625416255162561625716258162591626016261162621626316264162651626616267162681626916270162711627216273162741627516276162771627816279162801628116282162831628416285162861628716288162891629016291162921629316294162951629616297162981629916300163011630216303163041630516306163071630816309163101631116312163131631416315163161631716318163191632016321163221632316324163251632616327163281632916330163311633216333163341633516336163371633816339163401634116342163431634416345163461634716348163491635016351163521635316354163551635616357163581635916360163611636216363163641636516366163671636816369163701637116372163731637416375163761637716378163791638016381163821638316384163851638616387163881638916390163911639216393163941639516396163971639816399164001640116402164031640416405164061640716408164091641016411164121641316414164151641616417164181641916420164211642216423164241642516426164271642816429164301643116432164331643416435164361643716438164391644016441164421644316444164451644616447164481644916450164511645216453164541645516456164571645816459164601646116462164631646416465164661646716468164691647016471164721647316474164751647616477164781647916480164811648216483164841648516486164871648816489164901649116492164931649416495164961649716498164991650016501165021650316504165051650616507165081650916510165111651216513165141651516516165171651816519165201652116522165231652416525165261652716528165291653016531165321653316534165351653616537165381653916540165411654216543165441654516546165471654816549165501655116552165531655416555165561655716558165591656016561165621656316564165651656616567165681656916570165711657216573165741657516576165771657816579165801658116582165831658416585165861658716588165891659016591165921659316594165951659616597165981659916600166011660216603166041660516606166071660816609166101661116612166131661416615166161661716618166191662016621166221662316624166251662616627166281662916630166311663216633166341663516636166371663816639166401664116642166431664416645166461664716648166491665016651166521665316654166551665616657166581665916660166611666216663166641666516666166671666816669166701667116672166731667416675166761667716678166791668016681166821668316684166851668616687166881668916690166911669216693166941669516696166971669816699167001670116702167031670416705167061670716708167091671016711167121671316714167151671616717167181671916720167211672216723167241672516726167271672816729167301673116732167331673416735167361673716738167391674016741167421674316744167451674616747167481674916750167511675216753167541675516756167571675816759167601676116762167631676416765167661676716768167691677016771167721677316774167751677616777167781677916780167811678216783167841678516786167871678816789167901679116792167931679416795167961679716798167991680016801168021680316804168051680616807168081680916810168111681216813168141681516816168171681816819168201682116822168231682416825168261682716828168291683016831168321683316834168351683616837168381683916840168411684216843168441684516846168471684816849168501685116852168531685416855168561685716858168591686016861168621686316864168651686616867168681686916870168711687216873168741687516876168771687816879168801688116882168831688416885168861688716888168891689016891168921689316894168951689616897168981689916900169011690216903169041690516906169071690816909169101691116912169131691416915169161691716918169191692016921169221692316924169251692616927169281692916930169311693216933169341693516936169371693816939169401694116942169431694416945169461694716948169491695016951169521695316954169551695616957169581695916960169611696216963169641696516966169671696816969169701697116972169731697416975169761697716978169791698016981169821698316984169851698616987169881698916990169911699216993169941699516996169971699816999170001700117002170031700417005170061700717008170091701017011170121701317014170151701617017170181701917020170211702217023170241702517026170271702817029170301703117032170331703417035170361703717038170391704017041170421704317044170451704617047170481704917050170511705217053170541705517056170571705817059170601706117062170631706417065170661706717068170691707017071170721707317074170751707617077170781707917080170811708217083170841708517086170871708817089170901709117092170931709417095170961709717098170991710017101171021710317104171051710617107171081710917110171111711217113171141711517116171171711817119171201712117122171231712417125171261712717128171291713017131171321713317134171351713617137171381713917140171411714217143171441714517146171471714817149171501715117152171531715417155171561715717158171591716017161171621716317164171651716617167171681716917170171711717217173171741717517176171771717817179171801718117182171831718417185171861718717188171891719017191171921719317194171951719617197171981719917200172011720217203172041720517206172071720817209172101721117212172131721417215172161721717218172191722017221172221722317224172251722617227172281722917230172311723217233172341723517236172371723817239172401724117242172431724417245172461724717248172491725017251172521725317254172551725617257172581725917260172611726217263172641726517266172671726817269172701727117272172731727417275172761727717278172791728017281172821728317284172851728617287172881728917290172911729217293172941729517296172971729817299173001730117302173031730417305173061730717308173091731017311173121731317314173151731617317173181731917320173211732217323173241732517326173271732817329173301733117332173331733417335173361733717338173391734017341173421734317344173451734617347173481734917350173511735217353173541735517356173571735817359173601736117362173631736417365173661736717368173691737017371173721737317374173751737617377173781737917380173811738217383173841738517386173871738817389173901739117392173931739417395173961739717398173991740017401174021740317404174051740617407174081740917410174111741217413174141741517416174171741817419174201742117422174231742417425174261742717428174291743017431174321743317434174351743617437174381743917440174411744217443174441744517446174471744817449174501745117452174531745417455174561745717458174591746017461174621746317464174651746617467174681746917470174711747217473174741747517476174771747817479174801748117482174831748417485174861748717488174891749017491174921749317494174951749617497174981749917500175011750217503175041750517506175071750817509175101751117512175131751417515175161751717518175191752017521175221752317524175251752617527175281752917530175311753217533175341753517536175371753817539175401754117542175431754417545175461754717548175491755017551175521755317554175551755617557175581755917560175611756217563175641756517566175671756817569175701757117572175731757417575175761757717578175791758017581175821758317584175851758617587175881758917590175911759217593175941759517596175971759817599176001760117602176031760417605176061760717608176091761017611176121761317614176151761617617176181761917620176211762217623176241762517626176271762817629176301763117632176331763417635176361763717638176391764017641176421764317644176451764617647176481764917650176511765217653176541765517656176571765817659176601766117662176631766417665176661766717668176691767017671176721767317674176751767617677176781767917680176811768217683176841768517686176871768817689176901769117692176931769417695176961769717698176991770017701177021770317704177051770617707177081770917710177111771217713177141771517716177171771817719177201772117722177231772417725177261772717728177291773017731177321773317734177351773617737177381773917740177411774217743177441774517746177471774817749177501775117752177531775417755177561775717758177591776017761177621776317764177651776617767177681776917770177711777217773177741777517776177771777817779177801778117782177831778417785177861778717788177891779017791177921779317794177951779617797177981779917800178011780217803178041780517806178071780817809178101781117812178131781417815178161781717818178191782017821178221782317824178251782617827178281782917830178311783217833178341783517836178371783817839178401784117842178431784417845178461784717848178491785017851178521785317854178551785617857178581785917860178611786217863178641786517866178671786817869178701787117872178731787417875178761787717878178791788017881178821788317884178851788617887178881788917890178911789217893178941789517896178971789817899179001790117902179031790417905179061790717908179091791017911179121791317914179151791617917179181791917920179211792217923179241792517926179271792817929179301793117932179331793417935179361793717938179391794017941179421794317944179451794617947179481794917950179511795217953179541795517956179571795817959179601796117962179631796417965179661796717968179691797017971179721797317974179751797617977179781797917980179811798217983179841798517986179871798817989179901799117992179931799417995179961799717998179991800018001180021800318004180051800618007180081800918010180111801218013180141801518016180171801818019180201802118022180231802418025180261802718028180291803018031180321803318034180351803618037180381803918040180411804218043180441804518046180471804818049180501805118052180531805418055180561805718058180591806018061180621806318064180651806618067180681806918070180711807218073180741807518076180771807818079180801808118082180831808418085180861808718088180891809018091180921809318094180951809618097180981809918100181011810218103181041810518106181071810818109181101811118112181131811418115181161811718118181191812018121181221812318124181251812618127181281812918130181311813218133181341813518136181371813818139181401814118142181431814418145181461814718148181491815018151181521815318154181551815618157181581815918160181611816218163181641816518166181671816818169181701817118172181731817418175181761817718178181791818018181181821818318184181851818618187181881818918190181911819218193181941819518196181971819818199182001820118202182031820418205182061820718208182091821018211182121821318214182151821618217182181821918220182211822218223182241822518226182271822818229182301823118232182331823418235182361823718238182391824018241182421824318244182451824618247182481824918250182511825218253182541825518256182571825818259182601826118262182631826418265182661826718268182691827018271182721827318274182751827618277182781827918280182811828218283182841828518286182871828818289182901829118292182931829418295182961829718298182991830018301183021830318304183051830618307183081830918310183111831218313183141831518316183171831818319183201832118322183231832418325183261832718328183291833018331183321833318334183351833618337183381833918340183411834218343183441834518346183471834818349183501835118352183531835418355183561835718358183591836018361183621836318364183651836618367183681836918370183711837218373183741837518376183771837818379183801838118382183831838418385183861838718388183891839018391183921839318394183951839618397183981839918400184011840218403184041840518406184071840818409184101841118412184131841418415184161841718418184191842018421184221842318424184251842618427184281842918430184311843218433184341843518436184371843818439184401844118442184431844418445184461844718448184491845018451184521845318454184551845618457184581845918460184611846218463184641846518466184671846818469184701847118472184731847418475184761847718478184791848018481184821848318484184851848618487184881848918490184911849218493184941849518496184971849818499185001850118502185031850418505185061850718508185091851018511185121851318514185151851618517185181851918520185211852218523185241852518526185271852818529185301853118532185331853418535185361853718538185391854018541185421854318544185451854618547185481854918550185511855218553185541855518556185571855818559185601856118562185631856418565185661856718568185691857018571185721857318574185751857618577185781857918580185811858218583185841858518586185871858818589185901859118592185931859418595185961859718598185991860018601186021860318604186051860618607186081860918610186111861218613186141861518616186171861818619186201862118622186231862418625186261862718628186291863018631186321863318634186351863618637186381863918640186411864218643186441864518646186471864818649186501865118652186531865418655186561865718658186591866018661186621866318664186651866618667186681866918670186711867218673186741867518676186771867818679186801868118682186831868418685186861868718688186891869018691186921869318694186951869618697186981869918700187011870218703187041870518706187071870818709187101871118712187131871418715187161871718718187191872018721187221872318724187251872618727187281872918730187311873218733187341873518736187371873818739187401874118742187431874418745187461874718748187491875018751187521875318754187551875618757187581875918760187611876218763187641876518766187671876818769187701877118772187731877418775187761877718778187791878018781187821878318784187851878618787187881878918790187911879218793187941879518796187971879818799188001880118802188031880418805188061880718808188091881018811188121881318814188151881618817188181881918820188211882218823188241882518826188271882818829188301883118832188331883418835188361883718838188391884018841188421884318844188451884618847188481884918850188511885218853188541885518856188571885818859188601886118862188631886418865188661886718868188691887018871188721887318874188751887618877188781887918880188811888218883188841888518886188871888818889188901889118892188931889418895188961889718898188991890018901189021890318904189051890618907189081890918910189111891218913189141891518916189171891818919189201892118922189231892418925189261892718928189291893018931189321893318934189351893618937189381893918940189411894218943189441894518946189471894818949189501895118952189531895418955189561895718958189591896018961189621896318964189651896618967189681896918970189711897218973189741897518976189771897818979189801898118982189831898418985189861898718988189891899018991189921899318994189951899618997189981899919000190011900219003190041900519006190071900819009190101901119012190131901419015190161901719018190191902019021190221902319024190251902619027190281902919030190311903219033190341903519036190371903819039190401904119042190431904419045190461904719048190491905019051190521905319054190551905619057190581905919060190611906219063190641906519066190671906819069190701907119072190731907419075190761907719078190791908019081190821908319084190851908619087190881908919090190911909219093190941909519096190971909819099191001910119102191031910419105191061910719108191091911019111191121911319114191151911619117191181911919120191211912219123191241912519126191271912819129191301913119132191331913419135191361913719138191391914019141191421914319144191451914619147191481914919150191511915219153191541915519156191571915819159191601916119162191631916419165191661916719168191691917019171191721917319174191751917619177191781917919180191811918219183191841918519186191871918819189191901919119192191931919419195191961919719198191991920019201192021920319204192051920619207192081920919210192111921219213192141921519216192171921819219192201922119222192231922419225192261922719228192291923019231192321923319234192351923619237192381923919240192411924219243192441924519246192471924819249192501925119252192531925419255192561925719258192591926019261192621926319264192651926619267192681926919270192711927219273192741927519276192771927819279192801928119282192831928419285192861928719288192891929019291192921929319294192951929619297192981929919300193011930219303193041930519306193071930819309193101931119312193131931419315193161931719318193191932019321193221932319324193251932619327193281932919330193311933219333193341933519336193371933819339193401934119342193431934419345193461934719348193491935019351193521935319354193551935619357193581935919360193611936219363193641936519366193671936819369193701937119372193731937419375193761937719378193791938019381193821938319384193851938619387193881938919390193911939219393193941939519396193971939819399194001940119402194031940419405194061940719408194091941019411194121941319414194151941619417194181941919420194211942219423194241942519426194271942819429194301943119432194331943419435194361943719438194391944019441194421944319444194451944619447194481944919450194511945219453194541945519456194571945819459194601946119462194631946419465194661946719468194691947019471194721947319474194751947619477194781947919480194811948219483194841948519486194871948819489194901949119492194931949419495194961949719498194991950019501195021950319504195051950619507195081950919510195111951219513195141951519516195171951819519195201952119522195231952419525195261952719528195291953019531195321953319534195351953619537195381953919540195411954219543195441954519546195471954819549195501955119552195531955419555195561955719558195591956019561195621956319564195651956619567195681956919570195711957219573195741957519576195771957819579195801958119582195831958419585195861958719588195891959019591195921959319594195951959619597195981959919600196011960219603196041960519606196071960819609196101961119612196131961419615196161961719618196191962019621196221962319624196251962619627196281962919630196311963219633196341963519636196371963819639196401964119642196431964419645196461964719648196491965019651196521965319654196551965619657196581965919660196611966219663196641966519666196671966819669196701967119672196731967419675196761967719678196791968019681196821968319684196851968619687196881968919690196911969219693196941969519696196971969819699197001970119702197031970419705197061970719708197091971019711197121971319714197151971619717197181971919720197211972219723197241972519726197271972819729197301973119732197331973419735197361973719738197391974019741197421974319744197451974619747197481974919750197511975219753197541975519756197571975819759197601976119762197631976419765197661976719768197691977019771197721977319774197751977619777197781977919780197811978219783197841978519786197871978819789197901979119792197931979419795197961979719798197991980019801198021980319804198051980619807198081980919810198111981219813198141981519816198171981819819198201982119822198231982419825198261982719828198291983019831198321983319834198351983619837198381983919840198411984219843198441984519846198471984819849198501985119852198531985419855198561985719858198591986019861198621986319864198651986619867198681986919870198711987219873198741987519876198771987819879198801988119882198831988419885198861988719888198891989019891198921989319894198951989619897198981989919900199011990219903199041990519906199071990819909199101991119912199131991419915199161991719918199191992019921199221992319924199251992619927199281992919930199311993219933199341993519936199371993819939199401994119942199431994419945199461994719948199491995019951199521995319954199551995619957199581995919960199611996219963199641996519966199671996819969199701997119972199731997419975199761997719978199791998019981199821998319984199851998619987199881998919990199911999219993199941999519996199971999819999200002000120002200032000420005200062000720008200092001020011200122001320014200152001620017200182001920020200212002220023200242002520026200272002820029200302003120032200332003420035200362003720038200392004020041200422004320044200452004620047200482004920050200512005220053200542005520056200572005820059200602006120062200632006420065200662006720068200692007020071200722007320074200752007620077200782007920080200812008220083200842008520086200872008820089200902009120092200932009420095200962009720098200992010020101201022010320104201052010620107201082010920110201112011220113201142011520116201172011820119201202012120122201232012420125201262012720128201292013020131201322013320134201352013620137201382013920140201412014220143201442014520146201472014820149201502015120152201532015420155201562015720158201592016020161201622016320164201652016620167201682016920170201712017220173201742017520176201772017820179201802018120182201832018420185201862018720188201892019020191201922019320194201952019620197201982019920200202012020220203202042020520206202072020820209202102021120212202132021420215202162021720218202192022020221202222022320224202252022620227202282022920230202312023220233202342023520236202372023820239202402024120242202432024420245202462024720248202492025020251202522025320254202552025620257202582025920260202612026220263202642026520266202672026820269202702027120272202732027420275202762027720278202792028020281202822028320284202852028620287202882028920290202912029220293202942029520296202972029820299203002030120302203032030420305203062030720308203092031020311203122031320314203152031620317203182031920320203212032220323203242032520326203272032820329203302033120332203332033420335203362033720338203392034020341203422034320344203452034620347203482034920350203512035220353203542035520356203572035820359203602036120362203632036420365203662036720368203692037020371203722037320374203752037620377203782037920380203812038220383203842038520386203872038820389203902039120392203932039420395203962039720398203992040020401204022040320404204052040620407204082040920410204112041220413204142041520416204172041820419204202042120422204232042420425204262042720428204292043020431204322043320434204352043620437204382043920440204412044220443204442044520446204472044820449204502045120452204532045420455204562045720458204592046020461204622046320464204652046620467204682046920470204712047220473204742047520476204772047820479204802048120482204832048420485204862048720488204892049020491204922049320494204952049620497204982049920500205012050220503205042050520506205072050820509205102051120512205132051420515205162051720518205192052020521205222052320524205252052620527205282052920530205312053220533205342053520536205372053820539205402054120542205432054420545205462054720548205492055020551205522055320554205552055620557205582055920560205612056220563205642056520566205672056820569205702057120572205732057420575205762057720578205792058020581205822058320584205852058620587205882058920590205912059220593205942059520596205972059820599206002060120602206032060420605206062060720608206092061020611206122061320614206152061620617206182061920620206212062220623206242062520626206272062820629206302063120632206332063420635206362063720638206392064020641206422064320644206452064620647206482064920650206512065220653206542065520656206572065820659206602066120662206632066420665206662066720668206692067020671206722067320674206752067620677206782067920680206812068220683206842068520686206872068820689206902069120692206932069420695206962069720698206992070020701207022070320704207052070620707207082070920710207112071220713207142071520716207172071820719207202072120722207232072420725207262072720728207292073020731207322073320734207352073620737207382073920740207412074220743207442074520746207472074820749207502075120752207532075420755207562075720758207592076020761207622076320764207652076620767207682076920770207712077220773207742077520776207772077820779207802078120782207832078420785207862078720788207892079020791207922079320794207952079620797207982079920800208012080220803208042080520806208072080820809208102081120812208132081420815208162081720818208192082020821208222082320824208252082620827208282082920830208312083220833208342083520836208372083820839208402084120842208432084420845208462084720848208492085020851208522085320854208552085620857208582085920860208612086220863208642086520866208672086820869208702087120872208732087420875208762087720878208792088020881208822088320884208852088620887208882088920890208912089220893208942089520896208972089820899209002090120902209032090420905209062090720908209092091020911209122091320914209152091620917209182091920920209212092220923209242092520926209272092820929209302093120932209332093420935209362093720938209392094020941209422094320944209452094620947209482094920950209512095220953209542095520956209572095820959209602096120962209632096420965209662096720968209692097020971209722097320974209752097620977209782097920980209812098220983209842098520986209872098820989209902099120992209932099420995209962099720998209992100021001210022100321004210052100621007210082100921010210112101221013210142101521016210172101821019210202102121022210232102421025210262102721028210292103021031210322103321034210352103621037210382103921040210412104221043210442104521046210472104821049210502105121052210532105421055210562105721058210592106021061210622106321064210652106621067210682106921070210712107221073210742107521076210772107821079210802108121082210832108421085210862108721088210892109021091210922109321094210952109621097210982109921100211012110221103211042110521106211072110821109211102111121112211132111421115211162111721118211192112021121211222112321124211252112621127211282112921130211312113221133211342113521136211372113821139211402114121142211432114421145211462114721148211492115021151211522115321154211552115621157211582115921160211612116221163211642116521166211672116821169211702117121172211732117421175211762117721178211792118021181211822118321184211852118621187211882118921190211912119221193211942119521196211972119821199212002120121202212032120421205212062120721208212092121021211212122121321214212152121621217212182121921220212212122221223212242122521226212272122821229212302123121232212332123421235212362123721238212392124021241212422124321244212452124621247212482124921250212512125221253212542125521256212572125821259212602126121262212632126421265212662126721268212692127021271212722127321274212752127621277212782127921280212812128221283212842128521286212872128821289212902129121292212932129421295212962129721298212992130021301213022130321304213052130621307213082130921310213112131221313213142131521316213172131821319213202132121322213232132421325213262132721328213292133021331213322133321334213352133621337213382133921340213412134221343213442134521346213472134821349213502135121352213532135421355213562135721358213592136021361213622136321364213652136621367213682136921370213712137221373213742137521376213772137821379213802138121382213832138421385213862138721388213892139021391213922139321394213952139621397213982139921400214012140221403214042140521406214072140821409214102141121412214132141421415214162141721418214192142021421214222142321424214252142621427214282142921430214312143221433214342143521436214372143821439214402144121442214432144421445214462144721448214492145021451214522145321454214552145621457214582145921460214612146221463214642146521466214672146821469214702147121472214732147421475214762147721478214792148021481214822148321484214852148621487214882148921490214912149221493214942149521496214972149821499215002150121502215032150421505215062150721508215092151021511215122151321514215152151621517215182151921520215212152221523215242152521526215272152821529215302153121532215332153421535215362153721538215392154021541215422154321544215452154621547215482154921550215512155221553215542155521556215572155821559215602156121562215632156421565215662156721568215692157021571215722157321574215752157621577215782157921580215812158221583215842158521586215872158821589215902159121592215932159421595215962159721598215992160021601216022160321604216052160621607216082160921610216112161221613216142161521616216172161821619216202162121622216232162421625216262162721628216292163021631216322163321634216352163621637216382163921640216412164221643216442164521646216472164821649216502165121652216532165421655216562165721658216592166021661216622166321664216652166621667216682166921670216712167221673216742167521676216772167821679216802168121682216832168421685216862168721688216892169021691216922169321694216952169621697216982169921700217012170221703217042170521706217072170821709217102171121712217132171421715217162171721718217192172021721217222172321724217252172621727217282172921730217312173221733217342173521736217372173821739217402174121742217432174421745217462174721748217492175021751217522175321754217552175621757217582175921760217612176221763217642176521766217672176821769217702177121772217732177421775217762177721778217792178021781217822178321784217852178621787217882178921790217912179221793217942179521796217972179821799218002180121802218032180421805218062180721808218092181021811218122181321814218152181621817218182181921820218212182221823218242182521826218272182821829218302183121832218332183421835218362183721838218392184021841218422184321844218452184621847218482184921850218512185221853218542185521856218572185821859218602186121862218632186421865218662186721868218692187021871218722187321874218752187621877218782187921880218812188221883218842188521886
  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. #ifdef PCRE_SUPPORT
  9. #include "../../3rdparty/pcre/include/pcre.h" // preg_match
  10. #endif
  11. #include "../common/cbasetypes.h"
  12. #include "../common/malloc.h"
  13. #include "../common/md5calc.h"
  14. #include "../common/nullpo.h"
  15. #include "../common/random.h"
  16. #include "../common/showmsg.h"
  17. #include "../common/socket.h"
  18. #include "../common/strlib.h"
  19. #include "../common/timer.h"
  20. #include "../common/utils.h"
  21. #ifdef BETA_THREAD_TEST
  22. #include "../common/atomic.h"
  23. #include "../common/spinlock.h"
  24. #include "../common/thread.h"
  25. #include "../common/mutex.h"
  26. #endif
  27. #include "map.h"
  28. #include "path.h"
  29. #include "clif.h"
  30. #include "chrif.h"
  31. #include "itemdb.h"
  32. #include "pc.h"
  33. #include "storage.h"
  34. #include "pet.h"
  35. #include "mapreg.h"
  36. #include "homunculus.h"
  37. #include "instance.h"
  38. #include "mercenary.h"
  39. #include "intif.h"
  40. #include "chat.h"
  41. #include "battleground.h"
  42. #include "party.h"
  43. #include "mail.h"
  44. #include "script.h"
  45. #include "quest.h"
  46. #include "elemental.h"
  47. #include <stdio.h>
  48. #include <stdlib.h>
  49. #include <string.h>
  50. #include <math.h>
  51. #ifndef WIN32
  52. #endif
  53. #include <setjmp.h>
  54. #include <errno.h>
  55. TBL_PC *script_rid2sd(struct script_state *st);
  56. /**
  57. * Get `sd` from a account id in `loc` param instead of attached rid
  58. * @param st Script
  59. * @param loc Location to look account id in script parameter
  60. * @param sd Variable that will be assigned
  61. * @return True if `sd` is assigned, false otherwise
  62. **/
  63. static bool script_accid2sd_(struct script_state *st, uint8 loc, struct map_session_data **sd, const char *func) {
  64. if (script_hasdata(st, loc)) {
  65. int id_ = script_getnum(st, loc);
  66. if (!(*sd = map_id2sd(id_)))
  67. ShowError("%s: Player with account id '%d' is not found.\n", func, id_);
  68. }
  69. else
  70. *sd = script_rid2sd(st);
  71. return (*sd) ? true : false;
  72. }
  73. /**
  74. * Get `sd` from a char id in `loc` param instead of attached rid
  75. * @param st Script
  76. * @param loc Location to look char id in script parameter
  77. * @param sd Variable that will be assigned
  78. * @return True if `sd` is assigned, false otherwise
  79. **/
  80. static bool script_charid2sd_(struct script_state *st, uint8 loc, struct map_session_data **sd, const char *func) {
  81. if (script_hasdata(st, loc)) {
  82. int id_ = script_getnum(st, loc);
  83. if (!(*sd = map_charid2sd(id_)))
  84. ShowError("%s: Player with char id '%d' is not found.\n", func, id_);
  85. }
  86. else
  87. *sd = script_rid2sd(st);
  88. return (*sd) ? true : false;
  89. }
  90. /**
  91. * Get `sd` from a nick in `loc` param instead of attached rid
  92. * @param st Script
  93. * @param loc Location to look nick in script parameter
  94. * @param sd Variable that will be assigned
  95. * @return True if `sd` is assigned, false otherwise
  96. **/
  97. static bool script_nick2sd_(struct script_state *st, uint8 loc, struct map_session_data **sd, const char *func) {
  98. if (script_hasdata(st, loc)) {
  99. const char *name_ = script_getstr(st, loc);
  100. if (!(*sd = map_nick2sd(name_)))
  101. ShowError("%s: Player with nick '%s' is not found.\n", func, name_);
  102. }
  103. else
  104. *sd = script_rid2sd(st);
  105. return (*sd) ? true : false;
  106. }
  107. #define script_accid2sd(loc,sd) script_accid2sd_(st,(loc),&(sd),__FUNCTION__)
  108. #define script_charid2sd(loc,sd) script_charid2sd_(st,(loc),&(sd),__FUNCTION__)
  109. #define script_nick2sd(loc,sd) script_nick2sd_(st,(loc),&(sd),__FUNCTION__)
  110. /// temporary buffer for passing around compiled bytecode
  111. /// @see add_scriptb, set_label, parse_script
  112. static unsigned char* script_buf = NULL;
  113. static int script_pos = 0, script_size = 0;
  114. static inline int GETVALUE(const unsigned char* buf, int i)
  115. {
  116. return (int)MakeDWord(MakeWord(buf[i], buf[i+1]), MakeWord(buf[i+2], 0));
  117. }
  118. static inline void SETVALUE(unsigned char* buf, int i, int n)
  119. {
  120. buf[i] = GetByte(n, 0);
  121. buf[i+1] = GetByte(n, 1);
  122. buf[i+2] = GetByte(n, 2);
  123. }
  124. // String buffer structures.
  125. // str_data stores string information
  126. static struct str_data_struct {
  127. enum c_op type;
  128. int str;
  129. int backpatch;
  130. int label;
  131. int (*func)(struct script_state *st);
  132. int val;
  133. int next;
  134. } *str_data = NULL;
  135. static int str_data_size = 0; // size of the data
  136. static int str_num = LABEL_START; // next id to be assigned
  137. // str_buf holds the strings themselves
  138. static char *str_buf;
  139. static int str_size = 0; // size of the buffer
  140. static int str_pos = 0; // next position to be assigned
  141. // Using a prime number for SCRIPT_HASH_SIZE should give better distributions
  142. #define SCRIPT_HASH_SIZE 1021
  143. int str_hash[SCRIPT_HASH_SIZE];
  144. // Specifies which string hashing method to use
  145. //#define SCRIPT_HASH_DJB2
  146. //#define SCRIPT_HASH_SDBM
  147. #define SCRIPT_HASH_ELF
  148. static DBMap* scriptlabel_db = NULL; // const char* label_name -> int script_pos
  149. static DBMap* userfunc_db = NULL; // const char* func_name -> struct script_code*
  150. static int parse_options = 0;
  151. DBMap* script_get_label_db(void) { return scriptlabel_db; }
  152. DBMap* script_get_userfunc_db(void) { return userfunc_db; }
  153. // important buildin function references for usage in scripts
  154. static int buildin_set_ref = 0;
  155. static int buildin_callsub_ref = 0;
  156. static int buildin_callfunc_ref = 0;
  157. static int buildin_getelementofarray_ref = 0;
  158. // Caches compiled autoscript item code.
  159. // Note: This is not cleared when reloading itemdb.
  160. static DBMap* autobonus_db = NULL; // char* script -> char* bytecode
  161. struct Script_Config script_config = {
  162. 1, // warn_func_mismatch_argtypes
  163. 1, 65535, 2048, //warn_func_mismatch_paramnum/check_cmdcount/check_gotocount
  164. 0, INT_MAX, // input_min_value/input_max_value
  165. "OnPCDieEvent", //die_event_name
  166. "OnPCKillEvent", //kill_pc_event_name
  167. "OnNPCKillEvent", //kill_mob_event_name
  168. "OnPCLoginEvent", //login_event_name
  169. "OnPCLogoutEvent", //logout_event_name
  170. "OnPCLoadMapEvent", //loadmap_event_name
  171. "OnPCBaseLvUpEvent", //baselvup_event_name
  172. "OnPCJobLvUpEvent", //joblvup_event_name
  173. "OnPCStatCalcEvent", //stat_calc_event_name
  174. "OnTouch_", //ontouch_name (runs on first visible char to enter area, picks another char if the first char leaves)
  175. "OnTouch", //ontouch2_name (run whenever a char walks into the OnTouch area)
  176. };
  177. static jmp_buf error_jump;
  178. static char* error_msg;
  179. static const char* error_pos;
  180. static int error_report; // if the error should produce output
  181. // Used by disp_warning_message
  182. static const char* parser_current_src;
  183. static const char* parser_current_file;
  184. static int parser_current_line;
  185. // for advanced scripting support ( nested if, switch, while, for, do-while, function, etc )
  186. // [Eoe / jA 1080, 1081, 1094, 1164]
  187. enum curly_type {
  188. TYPE_NULL = 0,
  189. TYPE_IF,
  190. TYPE_SWITCH,
  191. TYPE_WHILE,
  192. TYPE_FOR,
  193. TYPE_DO,
  194. TYPE_USERFUNC,
  195. TYPE_ARGLIST // function argument list
  196. };
  197. enum e_arglist
  198. {
  199. ARGLIST_UNDEFINED = 0,
  200. ARGLIST_NO_PAREN = 1,
  201. ARGLIST_PAREN = 2,
  202. };
  203. static struct {
  204. struct {
  205. enum curly_type type;
  206. int index;
  207. int count;
  208. int flag;
  209. struct linkdb_node *case_label;
  210. } curly[256]; // Information right parenthesis
  211. int curly_count; // The number of right brackets
  212. int index; // Number of the syntax used in the script
  213. } syntax;
  214. const char* parse_curly_close(const char* p);
  215. const char* parse_syntax_close(const char* p);
  216. const char* parse_syntax_close_sub(const char* p,int* flag);
  217. const char* parse_syntax(const char* p);
  218. static int parse_syntax_for_flag = 0;
  219. extern short current_equip_item_index; //for New CARDS Scripts. It contains Inventory Index of the EQUIP_SCRIPT caller item. [Lupus]
  220. extern unsigned int current_equip_combo_pos;
  221. int potion_flag=0; //For use on Alchemist improved potions/Potion Pitcher. [Skotlex]
  222. int potion_hp=0, potion_per_hp=0, potion_sp=0, potion_per_sp=0;
  223. int potion_target = 0;
  224. unsigned int *generic_ui_array = NULL;
  225. unsigned int generic_ui_array_size = 0;
  226. c_op get_com(unsigned char *script,int *pos);
  227. int get_num(unsigned char *script,int *pos);
  228. typedef struct script_function {
  229. int (*func)(struct script_state *st);
  230. const char *name;
  231. const char *arg;
  232. } script_function;
  233. extern script_function buildin_func[];
  234. #ifdef BETA_THREAD_TEST
  235. /**
  236. * MySQL Query Slave
  237. **/
  238. static SPIN_LOCK queryThreadLock;
  239. static rAthread queryThread = NULL;
  240. static ramutex queryThreadMutex = NULL;
  241. static racond queryThreadCond = NULL;
  242. static volatile int32 queryThreadTerminate = 0;
  243. struct queryThreadEntry {
  244. bool ok;
  245. bool type; /* main db or log db? */
  246. struct script_state *st;
  247. };
  248. /* Ladies and Gentleman the Manager! */
  249. struct {
  250. struct queryThreadEntry **entry;/* array of structs */
  251. int count;
  252. int timer;/* used to receive processed entries */
  253. } queryThreadData;
  254. #endif
  255. /*==========================================
  256. * (Only those needed) local declaration prototype
  257. *------------------------------------------*/
  258. const char* parse_subexpr(const char* p,int limit);
  259. int run_func(struct script_state *st);
  260. int script_instancegetid(struct script_state *st);
  261. enum {
  262. MF_NOMEMO, //0
  263. MF_NOTELEPORT,
  264. MF_NOSAVE,
  265. MF_NOBRANCH,
  266. MF_NOPENALTY,
  267. MF_NOZENYPENALTY,
  268. MF_PVP,
  269. MF_PVP_NOPARTY,
  270. MF_PVP_NOGUILD,
  271. MF_GVG,
  272. MF_GVG_NOPARTY, //10
  273. MF_NOTRADE,
  274. MF_NOSKILL,
  275. MF_NOWARP,
  276. MF_PARTYLOCK,
  277. MF_NOICEWALL,
  278. MF_SNOW,
  279. MF_FOG,
  280. MF_SAKURA,
  281. MF_LEAVES,
  282. /**
  283. * No longer available, keeping here just in case it's back someday. [Ind]
  284. **/
  285. //MF_RAIN, //20
  286. // 21 free
  287. MF_NOGO = 22,
  288. MF_CLOUDS,
  289. MF_CLOUDS2,
  290. MF_FIREWORKS,
  291. MF_GVG_CASTLE,
  292. MF_GVG_DUNGEON,
  293. MF_NIGHTENABLED,
  294. MF_NOBASEEXP,
  295. MF_NOJOBEXP, //30
  296. MF_NOMOBLOOT,
  297. MF_NOMVPLOOT,
  298. MF_NORETURN,
  299. MF_NOWARPTO,
  300. MF_NIGHTMAREDROP,
  301. MF_RESTRICTED,
  302. MF_NOCOMMAND,
  303. MF_NODROP,
  304. MF_JEXP,
  305. MF_BEXP, //40
  306. MF_NOVENDING,
  307. MF_LOADEVENT,
  308. MF_NOCHAT,
  309. MF_NOEXPPENALTY,
  310. MF_GUILDLOCK,
  311. MF_TOWN,
  312. MF_AUTOTRADE,
  313. MF_ALLOWKS,
  314. MF_MONSTER_NOTELEPORT,
  315. MF_PVP_NOCALCRANK, //50
  316. MF_BATTLEGROUND,
  317. MF_RESET,
  318. MF_CHANNELAUTOJOIN,
  319. MF_NOUSECART,
  320. MF_NOITEMCONSUMPTION,
  321. MF_SUMSTARTMIRACLE,
  322. MF_NOMINEEFFECT,
  323. MF_NOLOCKON,
  324. MF_NOTOMB,
  325. MF_SKILL_DAMAGE //60
  326. };
  327. const char* script_op2name(int op)
  328. {
  329. #define RETURN_OP_NAME(type) case type: return #type
  330. switch( op )
  331. {
  332. RETURN_OP_NAME(C_NOP);
  333. RETURN_OP_NAME(C_POS);
  334. RETURN_OP_NAME(C_INT);
  335. RETURN_OP_NAME(C_PARAM);
  336. RETURN_OP_NAME(C_FUNC);
  337. RETURN_OP_NAME(C_STR);
  338. RETURN_OP_NAME(C_CONSTSTR);
  339. RETURN_OP_NAME(C_ARG);
  340. RETURN_OP_NAME(C_NAME);
  341. RETURN_OP_NAME(C_EOL);
  342. RETURN_OP_NAME(C_RETINFO);
  343. RETURN_OP_NAME(C_USERFUNC);
  344. RETURN_OP_NAME(C_USERFUNC_POS);
  345. // operators
  346. RETURN_OP_NAME(C_OP3);
  347. RETURN_OP_NAME(C_LOR);
  348. RETURN_OP_NAME(C_LAND);
  349. RETURN_OP_NAME(C_LE);
  350. RETURN_OP_NAME(C_LT);
  351. RETURN_OP_NAME(C_GE);
  352. RETURN_OP_NAME(C_GT);
  353. RETURN_OP_NAME(C_EQ);
  354. RETURN_OP_NAME(C_NE);
  355. RETURN_OP_NAME(C_XOR);
  356. RETURN_OP_NAME(C_OR);
  357. RETURN_OP_NAME(C_AND);
  358. RETURN_OP_NAME(C_ADD);
  359. RETURN_OP_NAME(C_SUB);
  360. RETURN_OP_NAME(C_MUL);
  361. RETURN_OP_NAME(C_DIV);
  362. RETURN_OP_NAME(C_MOD);
  363. RETURN_OP_NAME(C_NEG);
  364. RETURN_OP_NAME(C_LNOT);
  365. RETURN_OP_NAME(C_NOT);
  366. RETURN_OP_NAME(C_R_SHIFT);
  367. RETURN_OP_NAME(C_L_SHIFT);
  368. default:
  369. ShowDebug("script_op2name: unexpected op=%d\n", op);
  370. return "???";
  371. }
  372. #undef RETURN_OP_NAME
  373. }
  374. #ifdef DEBUG_DUMP_STACK
  375. static void script_dump_stack(struct script_state* st)
  376. {
  377. int i;
  378. ShowMessage("\tstart = %d\n", st->start);
  379. ShowMessage("\tend = %d\n", st->end);
  380. ShowMessage("\tdefsp = %d\n", st->stack->defsp);
  381. ShowMessage("\tsp = %d\n", st->stack->sp);
  382. for( i = 0; i < st->stack->sp; ++i )
  383. {
  384. struct script_data* data = &st->stack->stack_data[i];
  385. ShowMessage("\t[%d] %s", i, script_op2name(data->type));
  386. switch( data->type )
  387. {
  388. case C_INT:
  389. case C_POS:
  390. ShowMessage(" %d\n", data->u.num);
  391. break;
  392. case C_STR:
  393. case C_CONSTSTR:
  394. ShowMessage(" \"%s\"\n", data->u.str);
  395. break;
  396. case C_NAME:
  397. 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));
  398. break;
  399. case C_RETINFO:
  400. {
  401. struct script_retinfo* ri = data->u.ri;
  402. ShowMessage(" %p {scope.vars=%p, scope.arrays=%p, script=%p, pos=%d, nargs=%d, defsp=%d}\n", ri, ri->scope.vars, ri->scope.arrays, ri->script, ri->pos, ri->nargs, ri->defsp);
  403. }
  404. break;
  405. default:
  406. ShowMessage("\n");
  407. break;
  408. }
  409. }
  410. }
  411. #endif
  412. /// Reports on the console the src of a script error.
  413. static void script_reportsrc(struct script_state *st)
  414. {
  415. struct block_list* bl;
  416. if( st->oid == 0 )
  417. return; //Can't report source.
  418. bl = map_id2bl(st->oid);
  419. if( bl == NULL )
  420. return;
  421. switch( bl->type ) {
  422. case BL_NPC:
  423. if( bl->m >= 0 )
  424. ShowDebug("Source (NPC): %s at %s (%d,%d)\n", ((struct npc_data *)bl)->name, map[bl->m].name, bl->x, bl->y);
  425. else
  426. ShowDebug("Source (NPC): %s (invisible/not on a map)\n", ((struct npc_data *)bl)->name);
  427. break;
  428. default:
  429. if( bl->m >= 0 )
  430. 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);
  431. else
  432. ShowDebug("Source (Non-NPC type %d): name %s (invisible/not on a map)\n", bl->type, status_get_name(bl));
  433. break;
  434. }
  435. }
  436. /// Reports on the console information about the script data.
  437. static void script_reportdata(struct script_data* data)
  438. {
  439. if( data == NULL )
  440. return;
  441. switch( data->type ) {
  442. case C_NOP:// no value
  443. ShowDebug("Data: nothing (nil)\n");
  444. break;
  445. case C_INT:// number
  446. ShowDebug("Data: number value=%"PRId64"\n", data->u.num);
  447. break;
  448. case C_STR:
  449. case C_CONSTSTR:// string
  450. if( data->u.str ) {
  451. ShowDebug("Data: string value=\"%s\"\n", data->u.str);
  452. } else {
  453. ShowDebug("Data: string value=NULL\n");
  454. }
  455. break;
  456. case C_NAME:// reference
  457. if( reference_tovariable(data) ) {// variable
  458. const char* name = reference_getname(data);
  459. ShowDebug("Data: variable name='%s' index=%d\n", name, reference_getindex(data));
  460. } else if( reference_toconstant(data) ) {// constant
  461. ShowDebug("Data: constant name='%s' value=%d\n", reference_getname(data), reference_getconstant(data));
  462. } else if( reference_toparam(data) ) {// param
  463. ShowDebug("Data: param name='%s' type=%d\n", reference_getname(data), reference_getparamtype(data));
  464. } else {// ???
  465. ShowDebug("Data: reference name='%s' type=%s\n", reference_getname(data), script_op2name(data->type));
  466. ShowDebug("Please report this!!! - script->str_data.type=%s\n", script_op2name(str_data[reference_getid(data)].type));
  467. }
  468. break;
  469. case C_POS:// label
  470. ShowDebug("Data: label pos=%"PRId64"\n", data->u.num);
  471. break;
  472. default:
  473. ShowDebug("Data: %s\n", script_op2name(data->type));
  474. break;
  475. }
  476. }
  477. /// Reports on the console information about the current built-in function.
  478. static void script_reportfunc(struct script_state* st)
  479. {
  480. int params, id;
  481. struct script_data* data;
  482. if( !script_hasdata(st,0) )
  483. {// no stack
  484. return;
  485. }
  486. data = script_getdata(st,0);
  487. if( !data_isreference(data) || str_data[reference_getid(data)].type != C_FUNC )
  488. {// script currently not executing a built-in function or corrupt stack
  489. return;
  490. }
  491. id = reference_getid(data);
  492. params = script_lastdata(st)-1;
  493. if( params > 0 )
  494. {
  495. int i;
  496. ShowDebug("Function: %s (%d parameter%s):\n", get_str(id), params, ( params == 1 ) ? "" : "s");
  497. for( i = 2; i <= script_lastdata(st); i++ )
  498. {
  499. script_reportdata(script_getdata(st,i));
  500. }
  501. }
  502. else
  503. {
  504. ShowDebug("Function: %s (no parameters)\n", get_str(id));
  505. }
  506. }
  507. /*==========================================
  508. * Output error message
  509. *------------------------------------------*/
  510. static void disp_error_message2(const char *mes,const char *pos,int report)
  511. {
  512. error_msg = aStrdup(mes);
  513. error_pos = pos;
  514. error_report = report;
  515. longjmp( error_jump, 1 );
  516. }
  517. #define disp_error_message(mes,pos) disp_error_message2(mes,pos,1)
  518. static void disp_warning_message(const char *mes, const char *pos) {
  519. script_warning(parser_current_src,parser_current_file,parser_current_line,mes,pos);
  520. }
  521. /// Checks event parameter validity
  522. static void check_event(struct script_state *st, const char *evt)
  523. {
  524. if( evt && evt[0] && !stristr(evt, "::On") )
  525. {
  526. ShowWarning("NPC event parameter deprecated! Please use 'NPCNAME::OnEVENT' instead of '%s'.\n", evt);
  527. script_reportsrc(st);
  528. }
  529. }
  530. /*==========================================
  531. * Hashes the input string
  532. *------------------------------------------*/
  533. static unsigned int calc_hash(const char* p)
  534. {
  535. unsigned int h;
  536. #if defined(SCRIPT_HASH_DJB2)
  537. h = 5381;
  538. while( *p ) // hash*33 + c
  539. h = ( h << 5 ) + h + ((unsigned char)TOLOWER(*p++));
  540. #elif defined(SCRIPT_HASH_SDBM)
  541. h = 0;
  542. while( *p ) // hash*65599 + c
  543. h = ( h << 6 ) + ( h << 16 ) - h + ((unsigned char)TOLOWER(*p++));
  544. #elif defined(SCRIPT_HASH_ELF) // UNIX ELF hash
  545. h = 0;
  546. while( *p ){
  547. unsigned int g;
  548. h = ( h << 4 ) + ((unsigned char)TOLOWER(*p++));
  549. g = h & 0xF0000000;
  550. if( g )
  551. {
  552. h ^= g >> 24;
  553. h &= ~g;
  554. }
  555. }
  556. #else // athena hash
  557. h = 0;
  558. while( *p )
  559. h = ( h << 1 ) + ( h >> 3 ) + ( h >> 5 ) + ( h >> 8 ) + (unsigned char)TOLOWER(*p++);
  560. #endif
  561. return h % SCRIPT_HASH_SIZE;
  562. }
  563. /*==========================================
  564. * str_data manipulation functions
  565. *------------------------------------------*/
  566. /// Looks up string using the provided id.
  567. const char* get_str(int id)
  568. {
  569. Assert( id >= LABEL_START && id < str_size );
  570. return str_buf+str_data[id].str;
  571. }
  572. /// Returns the uid of the string, or -1.
  573. static int search_str(const char* p)
  574. {
  575. int i;
  576. for( i = str_hash[calc_hash(p)]; i != 0; i = str_data[i].next )
  577. if( strcasecmp(get_str(i),p) == 0 )
  578. return i;
  579. return -1;
  580. }
  581. /// Stores a copy of the string and returns its id.
  582. /// If an identical string is already present, returns its id instead.
  583. int add_str(const char* p)
  584. {
  585. int h;
  586. int len;
  587. h = calc_hash(p);
  588. if( str_hash[h] == 0 )
  589. {// empty bucket, add new node here
  590. str_hash[h] = str_num;
  591. }
  592. else
  593. {// scan for end of list, or occurence of identical string
  594. int i;
  595. for( i = str_hash[h]; ; i = str_data[i].next )
  596. {
  597. if( strcasecmp(get_str(i),p) == 0 )
  598. return i; // string already in list
  599. if( str_data[i].next == 0 )
  600. break; // reached the end
  601. }
  602. // append node to end of list
  603. str_data[i].next = str_num;
  604. }
  605. // grow list if neccessary
  606. if( str_num >= str_data_size )
  607. {
  608. str_data_size += 128;
  609. RECREATE(str_data,struct str_data_struct,str_data_size);
  610. memset(str_data + (str_data_size - 128), '\0', 128);
  611. }
  612. len=(int)strlen(p);
  613. // grow string buffer if neccessary
  614. while( str_pos+len+1 >= str_size )
  615. {
  616. str_size += 256;
  617. RECREATE(str_buf,char,str_size);
  618. memset(str_buf + (str_size - 256), '\0', 256);
  619. }
  620. safestrncpy(str_buf+str_pos, p, len+1);
  621. str_data[str_num].type = C_NOP;
  622. str_data[str_num].str = str_pos;
  623. str_data[str_num].next = 0;
  624. str_data[str_num].func = NULL;
  625. str_data[str_num].backpatch = -1;
  626. str_data[str_num].label = -1;
  627. str_pos += len+1;
  628. return str_num++;
  629. }
  630. /// Appends 1 byte to the script buffer.
  631. static void add_scriptb(int a)
  632. {
  633. if( script_pos+1 >= script_size )
  634. {
  635. script_size += SCRIPT_BLOCK_SIZE;
  636. RECREATE(script_buf,unsigned char,script_size);
  637. }
  638. script_buf[script_pos++] = (uint8)(a);
  639. }
  640. /// Appends a c_op value to the script buffer.
  641. /// The value is variable-length encoded into 8-bit blocks.
  642. /// The encoding scheme is ( 01?????? )* 00??????, LSB first.
  643. /// All blocks but the last hold 7 bits of data, topmost bit is always 1 (carries).
  644. static void add_scriptc(int a)
  645. {
  646. while( a >= 0x40 )
  647. {
  648. add_scriptb((a&0x3f)|0x40);
  649. a = (a - 0x40) >> 6;
  650. }
  651. add_scriptb(a);
  652. }
  653. /// Appends an integer value to the script buffer.
  654. /// The value is variable-length encoded into 8-bit blocks.
  655. /// The encoding scheme is ( 11?????? )* 10??????, LSB first.
  656. /// All blocks but the last hold 7 bits of data, topmost bit is always 1 (carries).
  657. static void add_scripti(int a)
  658. {
  659. while( a >= 0x40 )
  660. {
  661. add_scriptb((a&0x3f)|0xc0);
  662. a = (a - 0x40) >> 6;
  663. }
  664. add_scriptb(a|0x80);
  665. }
  666. /// Appends a str_data object (label/function/variable/integer) to the script buffer.
  667. ///
  668. /// @param l The id of the str_data entry
  669. // Maximum up to 16M
  670. static void add_scriptl(int l)
  671. {
  672. int backpatch = str_data[l].backpatch;
  673. switch(str_data[l].type){
  674. case C_POS:
  675. case C_USERFUNC_POS:
  676. add_scriptc(C_POS);
  677. add_scriptb(str_data[l].label);
  678. add_scriptb(str_data[l].label>>8);
  679. add_scriptb(str_data[l].label>>16);
  680. break;
  681. case C_NOP:
  682. case C_USERFUNC:
  683. // Embedded data backpatch there is a possibility of label
  684. add_scriptc(C_NAME);
  685. str_data[l].backpatch = script_pos;
  686. add_scriptb(backpatch);
  687. add_scriptb(backpatch>>8);
  688. add_scriptb(backpatch>>16);
  689. break;
  690. case C_INT:
  691. add_scripti(abs(str_data[l].val));
  692. if( str_data[l].val < 0 ) //Notice that this is negative, from jA (Rayce)
  693. add_scriptc(C_NEG);
  694. break;
  695. default: // assume C_NAME
  696. add_scriptc(C_NAME);
  697. add_scriptb(l);
  698. add_scriptb(l>>8);
  699. add_scriptb(l>>16);
  700. break;
  701. }
  702. }
  703. /*==========================================
  704. * Resolve the label
  705. *------------------------------------------*/
  706. void set_label(int l,int pos, const char* script_pos_cur)
  707. {
  708. int i;
  709. if(str_data[l].type==C_INT || str_data[l].type==C_PARAM || str_data[l].type==C_FUNC)
  710. { //Prevent overwriting constants values, parameters and built-in functions [Skotlex]
  711. disp_error_message("set_label: invalid label name",script_pos_cur);
  712. return;
  713. }
  714. if(str_data[l].label!=-1){
  715. disp_error_message("set_label: dup label ",script_pos_cur);
  716. return;
  717. }
  718. str_data[l].type=(str_data[l].type == C_USERFUNC ? C_USERFUNC_POS : C_POS);
  719. str_data[l].label=pos;
  720. for(i=str_data[l].backpatch;i>=0 && i!=0x00ffffff;){
  721. int next=GETVALUE(script_buf,i);
  722. script_buf[i-1]=(str_data[l].type == C_USERFUNC ? C_USERFUNC_POS : C_POS);
  723. SETVALUE(script_buf,i,pos);
  724. i=next;
  725. }
  726. }
  727. /// Skips spaces and/or comments.
  728. const char* skip_space(const char* p)
  729. {
  730. if( p == NULL )
  731. return NULL;
  732. for(;;)
  733. {
  734. while( ISSPACE(*p) )
  735. ++p;
  736. if( *p == '/' && p[1] == '/' )
  737. {// line comment
  738. while(*p && *p!='\n')
  739. ++p;
  740. }
  741. else if( *p == '/' && p[1] == '*' )
  742. {// block comment
  743. p += 2;
  744. for(;;)
  745. {
  746. if( *p == '\0' ) {
  747. disp_warning_message("script:script->skip_space: end of file while parsing block comment. expected "CL_BOLD"*/"CL_NORM, p);
  748. return p;
  749. }
  750. if( *p == '*' && p[1] == '/' )
  751. {// end of block comment
  752. p += 2;
  753. break;
  754. }
  755. ++p;
  756. }
  757. }
  758. else
  759. break;
  760. }
  761. return p;
  762. }
  763. /// Skips a word.
  764. /// A word consists of undercores and/or alphanumeric characters,
  765. /// and valid variable prefixes/postfixes.
  766. static const char* skip_word(const char* p)
  767. {
  768. // prefix
  769. switch( *p ) {
  770. case '@':// temporary char variable
  771. ++p; break;
  772. case '#':// account variable
  773. p += ( p[1] == '#' ? 2 : 1 ); break;
  774. case '\'':// instance variable
  775. ++p; break;
  776. case '.':// npc variable
  777. p += ( p[1] == '@' ? 2 : 1 ); break;
  778. case '$':// global variable
  779. p += ( p[1] == '@' ? 2 : 1 ); break;
  780. }
  781. while( ISALNUM(*p) || *p == '_' )
  782. ++p;
  783. // postfix
  784. if( *p == '$' )// string
  785. p++;
  786. return p;
  787. }
  788. /// Adds a word to str_data.
  789. /// @see skip_word
  790. /// @see add_str
  791. static int add_word(const char* p)
  792. {
  793. char* word;
  794. int len;
  795. int i;
  796. // Check for a word
  797. len = skip_word(p) - p;
  798. if( len == 0 )
  799. disp_error_message("script:add_word: invalid word. A word consists of undercores and/or alphanumeric characters, and valid variable prefixes/postfixes.", p);
  800. // Duplicate the word
  801. word = (char*)aMalloc(len+1);
  802. memcpy(word, p, len);
  803. word[len] = 0;
  804. // add the word
  805. i = add_str(word);
  806. aFree(word);
  807. return i;
  808. }
  809. /// Parses a function call.
  810. /// The argument list can have parenthesis or not.
  811. /// The number of arguments is checked.
  812. static
  813. const char* parse_callfunc(const char* p, int require_paren, int is_custom)
  814. {
  815. const char* p2;
  816. const char* arg=NULL;
  817. int func;
  818. func = add_word(p);
  819. if( str_data[func].type == C_FUNC ){
  820. // buildin function
  821. add_scriptl(func);
  822. add_scriptc(C_ARG);
  823. arg = buildin_func[str_data[func].val].arg;
  824. } else if( str_data[func].type == C_USERFUNC || str_data[func].type == C_USERFUNC_POS ){
  825. // script defined function
  826. add_scriptl(buildin_callsub_ref);
  827. add_scriptc(C_ARG);
  828. add_scriptl(func);
  829. arg = buildin_func[str_data[buildin_callsub_ref].val].arg;
  830. if( *arg == 0 )
  831. disp_error_message("parse_callfunc: callsub has no arguments, please review its definition",p);
  832. if( *arg != '*' )
  833. ++arg; // count func as argument
  834. } else {
  835. #ifdef SCRIPT_CALLFUNC_CHECK
  836. const char* name = get_str(func);
  837. if( !is_custom && strdb_get(userfunc_db, name) == NULL ) {
  838. #endif
  839. disp_error_message("parse_line: expect command, missing function name or calling undeclared function",p);
  840. #ifdef SCRIPT_CALLFUNC_CHECK
  841. } else {;
  842. add_scriptl(buildin_callfunc_ref);
  843. add_scriptc(C_ARG);
  844. add_scriptc(C_STR);
  845. while( *name ) add_scriptb(*name ++);
  846. add_scriptb(0);
  847. arg = buildin_func[str_data[buildin_callfunc_ref].val].arg;
  848. if( *arg != '*' ) ++ arg;
  849. }
  850. #endif
  851. }
  852. p = skip_word(p);
  853. p = skip_space(p);
  854. syntax.curly[syntax.curly_count].type = TYPE_ARGLIST;
  855. syntax.curly[syntax.curly_count].count = 0;
  856. if( *p == ';' )
  857. {// <func name> ';'
  858. syntax.curly[syntax.curly_count].flag = ARGLIST_NO_PAREN;
  859. } else if( *p == '(' && *(p2=skip_space(p+1)) == ')' )
  860. {// <func name> '(' ')'
  861. syntax.curly[syntax.curly_count].flag = ARGLIST_PAREN;
  862. p = p2;
  863. /*
  864. } else if( 0 && require_paren && *p != '(' )
  865. {// <func name>
  866. syntax.curly[syntax.curly_count].flag = ARGLIST_NO_PAREN;
  867. */
  868. } else
  869. {// <func name> <arg list>
  870. if( require_paren ){
  871. if( *p != '(' )
  872. disp_error_message("need '('",p);
  873. ++p; // skip '('
  874. syntax.curly[syntax.curly_count].flag = ARGLIST_PAREN;
  875. } else if( *p == '(' ){
  876. syntax.curly[syntax.curly_count].flag = ARGLIST_UNDEFINED;
  877. } else {
  878. syntax.curly[syntax.curly_count].flag = ARGLIST_NO_PAREN;
  879. }
  880. ++syntax.curly_count;
  881. while( *arg ) {
  882. p2=parse_subexpr(p,-1);
  883. if( p == p2 )
  884. break; // not an argument
  885. if( *arg != '*' )
  886. ++arg; // next argument
  887. p=skip_space(p2);
  888. if( *arg == 0 || *p != ',' )
  889. break; // no more arguments
  890. ++p; // skip comma
  891. }
  892. --syntax.curly_count;
  893. }
  894. if( *arg && *arg != '?' && *arg != '*' )
  895. disp_error_message2("parse_callfunc: not enough arguments, expected ','", p, script_config.warn_func_mismatch_paramnum);
  896. if( syntax.curly[syntax.curly_count].type != TYPE_ARGLIST )
  897. disp_error_message("parse_callfunc: DEBUG last curly is not an argument list",p);
  898. if( syntax.curly[syntax.curly_count].flag == ARGLIST_PAREN ){
  899. if( *p != ')' )
  900. disp_error_message("parse_callfunc: expected ')' to close argument list",p);
  901. ++p;
  902. }
  903. add_scriptc(C_FUNC);
  904. return p;
  905. }
  906. /// Processes end of logical script line.
  907. /// @param first When true, only fix up scheduling data is initialized
  908. /// @param p Script position for error reporting in set_label
  909. static void parse_nextline(bool first, const char* p)
  910. {
  911. if( !first )
  912. {
  913. add_scriptc(C_EOL); // mark end of line for stack cleanup
  914. set_label(LABEL_NEXTLINE, script_pos, p); // fix up '-' labels
  915. }
  916. // initialize data for new '-' label fix up scheduling
  917. str_data[LABEL_NEXTLINE].type = C_NOP;
  918. str_data[LABEL_NEXTLINE].backpatch = -1;
  919. str_data[LABEL_NEXTLINE].label = -1;
  920. }
  921. /// Parse a variable assignment using the direct equals operator
  922. /// @param p script position where the function should run from
  923. /// @return NULL if not a variable assignment, the new position otherwise
  924. const char* parse_variable(const char* p) {
  925. int word;
  926. c_op type = C_NOP;
  927. const char *p2 = NULL;
  928. const char *var = p;
  929. // skip the variable where applicable
  930. p = skip_word(p);
  931. p = skip_space(p);
  932. if( p == NULL ) {// end of the line or invalid buffer
  933. return NULL;
  934. }
  935. if( *p == '[' ) {// array variable so process the array as appropriate
  936. int i,j;
  937. for( p2 = p, i = 0, j = 1; p; ++ i ) {
  938. if( *p ++ == ']' && --(j) == 0 ) break;
  939. if( *p == '[' ) ++ j;
  940. }
  941. if( !(p = skip_space(p)) ) {// end of line or invalid characters remaining
  942. disp_error_message("Missing right expression or closing bracket for variable.", p);
  943. }
  944. }
  945. if( type == C_NOP &&
  946. !( ( p[0] == '=' && p[1] != '=' && (type = C_EQ) ) // =
  947. || ( p[0] == '+' && p[1] == '=' && (type = C_ADD) ) // +=
  948. || ( p[0] == '-' && p[1] == '=' && (type = C_SUB) ) // -=
  949. || ( p[0] == '^' && p[1] == '=' && (type = C_XOR) ) // ^=
  950. || ( p[0] == '|' && p[1] == '=' && (type = C_OR ) ) // |=
  951. || ( p[0] == '&' && p[1] == '=' && (type = C_AND) ) // &=
  952. || ( p[0] == '*' && p[1] == '=' && (type = C_MUL) ) // *=
  953. || ( p[0] == '/' && p[1] == '=' && (type = C_DIV) ) // /=
  954. || ( p[0] == '%' && p[1] == '=' && (type = C_MOD) ) // %=
  955. || ( p[0] == '~' && p[1] == '=' && (type = C_NOT) ) // ~=
  956. || ( p[0] == '+' && p[1] == '+' && (type = C_ADD_PP) ) // ++
  957. || ( p[0] == '-' && p[1] == '-' && (type = C_SUB_PP) ) // --
  958. || ( p[0] == '<' && p[1] == '<' && p[2] == '=' && (type = C_L_SHIFT) ) // <<=
  959. || ( p[0] == '>' && p[1] == '>' && p[2] == '=' && (type = C_R_SHIFT) ) // >>=
  960. ) )
  961. {// failed to find a matching operator combination so invalid
  962. return NULL;
  963. }
  964. switch( type ) {
  965. case C_EQ: {// incremental modifier
  966. p = skip_space( &p[1] );
  967. }
  968. break;
  969. case C_L_SHIFT:
  970. case C_R_SHIFT: {// left or right shift modifier
  971. p = skip_space( &p[3] );
  972. }
  973. break;
  974. default: {// normal incremental command
  975. p = skip_space( &p[2] );
  976. }
  977. }
  978. if( p == NULL ) {// end of line or invalid buffer
  979. return NULL;
  980. }
  981. // push the set function onto the stack
  982. add_scriptl(buildin_set_ref);
  983. add_scriptc(C_ARG);
  984. // always append parenthesis to avoid errors
  985. syntax.curly[syntax.curly_count].type = TYPE_ARGLIST;
  986. syntax.curly[syntax.curly_count].count = 0;
  987. syntax.curly[syntax.curly_count].flag = ARGLIST_PAREN;
  988. // increment the total curly count for the position in the script
  989. ++ syntax.curly_count;
  990. // parse the variable currently being modified
  991. word = add_word(var);
  992. if( str_data[word].type == C_FUNC || str_data[word].type == C_USERFUNC || str_data[word].type == C_USERFUNC_POS )
  993. {// cannot assign a variable which exists as a function or label
  994. disp_error_message("Cannot modify a variable which has the same name as a function or label.", p);
  995. }
  996. if( p2 ) {// process the variable index
  997. const char* p3 = NULL;
  998. // push the getelementofarray method into the stack
  999. add_scriptl(buildin_getelementofarray_ref);
  1000. add_scriptc(C_ARG);
  1001. add_scriptl(word);
  1002. // process the sub-expression for this assignment
  1003. p3 = parse_subexpr(p2 + 1, 1);
  1004. p3 = skip_space(p3);
  1005. if( *p3 != ']' ) {// closing parenthesis is required for this script
  1006. disp_error_message("Missing closing ']' parenthesis for the variable assignment.", p3);
  1007. }
  1008. // push the closing function stack operator onto the stack
  1009. add_scriptc(C_FUNC);
  1010. p3 ++;
  1011. } else {// simply push the variable or value onto the stack
  1012. add_scriptl(word);
  1013. }
  1014. if( type != C_EQ )
  1015. add_scriptc(C_REF);
  1016. if( type == C_ADD_PP || type == C_SUB_PP ) {// incremental operator for the method
  1017. add_scripti(1);
  1018. add_scriptc(type == C_ADD_PP ? C_ADD : C_SUB);
  1019. } else {// process the value as an expression
  1020. p = parse_subexpr(p, -1);
  1021. if( type != C_EQ )
  1022. {// push the type of modifier onto the stack
  1023. add_scriptc(type);
  1024. }
  1025. }
  1026. // decrement the curly count for the position within the script
  1027. -- syntax.curly_count;
  1028. // close the script by appending the function operator
  1029. add_scriptc(C_FUNC);
  1030. // push the buffer from the method
  1031. return p;
  1032. }
  1033. /*
  1034. * Checks whether the gives string is a number literal
  1035. *
  1036. * Mainly necessary to differentiate between number literals and NPC name
  1037. * constants, since several of those start with a digit.
  1038. *
  1039. * All this does is to check if the string begins with an optional + or - sign,
  1040. * followed by a hexadecimal or decimal number literal literal and is NOT
  1041. * followed by a underscore or letter.
  1042. *
  1043. * @author : Hercules.ws
  1044. * @param p Pointer to the string to check
  1045. * @return Whether the string is a number literal
  1046. */
  1047. bool is_number(const char *p) {
  1048. const char *np;
  1049. if (!p)
  1050. return false;
  1051. if (*p == '-' || *p == '+')
  1052. p++;
  1053. np = p;
  1054. if (*p == '0' && p[1] == 'x') {
  1055. p+=2;
  1056. np = p;
  1057. // Hexadecimal
  1058. while (ISXDIGIT(*np))
  1059. np++;
  1060. } else {
  1061. // Decimal
  1062. while (ISDIGIT(*np))
  1063. np++;
  1064. }
  1065. if (p != np && *np != '_' && !ISALPHA(*np)) // At least one digit, and next isn't a letter or _
  1066. return true;
  1067. return false;
  1068. }
  1069. /*==========================================
  1070. * Analysis section
  1071. *------------------------------------------*/
  1072. const char* parse_simpleexpr(const char *p)
  1073. {
  1074. long long i;
  1075. p=skip_space(p);
  1076. if(*p==';' || *p==',')
  1077. disp_error_message("parse_simpleexpr: unexpected end of expression",p);
  1078. if(*p=='('){
  1079. if( (i=syntax.curly_count-1) >= 0 && syntax.curly[i].type == TYPE_ARGLIST )
  1080. ++syntax.curly[i].count;
  1081. p=parse_subexpr(p+1,-1);
  1082. p=skip_space(p);
  1083. if( (i=syntax.curly_count-1) >= 0 && syntax.curly[i].type == TYPE_ARGLIST &&
  1084. syntax.curly[i].flag == ARGLIST_UNDEFINED && --syntax.curly[i].count == 0
  1085. ){
  1086. if( *p == ',' ){
  1087. syntax.curly[i].flag = ARGLIST_PAREN;
  1088. return p;
  1089. } else
  1090. syntax.curly[i].flag = ARGLIST_NO_PAREN;
  1091. }
  1092. if( *p != ')' )
  1093. disp_error_message("parse_simpleexpr: unmatched ')'",p);
  1094. ++p;
  1095. } else if(is_number(p)) {
  1096. char *np;
  1097. while(*p == '0' && ISDIGIT(p[1])) p++;
  1098. i=strtoll(p,&np,0);
  1099. if( i < INT_MIN ) {
  1100. i = INT_MIN;
  1101. disp_warning_message("parse_simpleexpr: underflow detected, capping value to INT_MIN",p);
  1102. } else if( i > INT_MAX ) {
  1103. i = INT_MAX;
  1104. disp_warning_message("parse_simpleexpr: overflow detected, capping value to INT_MAX",p);
  1105. }
  1106. add_scripti((int)i);
  1107. p=np;
  1108. } else if(*p=='"'){
  1109. add_scriptc(C_STR);
  1110. p++;
  1111. while( *p && *p != '"' ){
  1112. if( (unsigned char)p[-1] <= 0x7e && *p == '\\' )
  1113. {
  1114. char buf[8];
  1115. size_t len = skip_escaped_c(p) - p;
  1116. size_t n = sv_unescape_c(buf, p, len);
  1117. if( n != 1 )
  1118. ShowDebug("parse_simpleexpr: unexpected length %d after unescape (\"%.*s\" -> %.*s)\n", (int)n, (int)len, p, (int)n, buf);
  1119. p += len;
  1120. add_scriptb(*buf);
  1121. continue;
  1122. }
  1123. else if( *p == '\n' )
  1124. disp_error_message("parse_simpleexpr: unexpected newline @ string",p);
  1125. add_scriptb(*p++);
  1126. }
  1127. if(!*p)
  1128. disp_error_message("parse_simpleexpr: unexpected eof @ string",p);
  1129. add_scriptb(0);
  1130. p++; //'"'
  1131. } else {
  1132. int l;
  1133. const char* pv;
  1134. // label , register , function etc
  1135. if(skip_word(p)==p)
  1136. disp_error_message("parse_simpleexpr: unexpected character",p);
  1137. l=add_word(p);
  1138. if( str_data[l].type == C_FUNC || str_data[l].type == C_USERFUNC || str_data[l].type == C_USERFUNC_POS)
  1139. return parse_callfunc(p,1,0);
  1140. #ifdef SCRIPT_CALLFUNC_CHECK
  1141. else {
  1142. const char* name = get_str(l);
  1143. if( strdb_get(userfunc_db,name) != NULL ) {
  1144. return parse_callfunc(p,1,1);
  1145. }
  1146. }
  1147. #endif
  1148. if( (pv = parse_variable(p)) )
  1149. {// successfully processed a variable assignment
  1150. return pv;
  1151. }
  1152. p=skip_word(p);
  1153. if( *p == '[' ){
  1154. // array(name[i] => getelementofarray(name,i) )
  1155. add_scriptl(buildin_getelementofarray_ref);
  1156. add_scriptc(C_ARG);
  1157. add_scriptl(l);
  1158. p=parse_subexpr(p+1,-1);
  1159. p=skip_space(p);
  1160. if( *p != ']' )
  1161. disp_error_message("parse_simpleexpr: unmatched ']'",p);
  1162. ++p;
  1163. add_scriptc(C_FUNC);
  1164. }else
  1165. add_scriptl(l);
  1166. }
  1167. return p;
  1168. }
  1169. /*==========================================
  1170. * Analysis of the expression
  1171. *------------------------------------------*/
  1172. const char* parse_subexpr(const char* p,int limit)
  1173. {
  1174. int op,opl,len;
  1175. p=skip_space(p);
  1176. if( *p == '-' ){
  1177. const char* tmpp = skip_space(p+1);
  1178. if( *tmpp == ';' || *tmpp == ',' ){
  1179. add_scriptl(LABEL_NEXTLINE);
  1180. p++;
  1181. return p;
  1182. }
  1183. }
  1184. if((op=C_NEG,*p=='-') || (op=C_LNOT,*p=='!') || (op=C_NOT,*p=='~')){
  1185. p=parse_subexpr(p+1,10);
  1186. add_scriptc(op);
  1187. } else
  1188. p=parse_simpleexpr(p);
  1189. p=skip_space(p);
  1190. while((
  1191. (op=C_OP3,opl=0,len=1,*p=='?') ||
  1192. (op=C_ADD,opl=8,len=1,*p=='+') ||
  1193. (op=C_SUB,opl=8,len=1,*p=='-') ||
  1194. (op=C_MUL,opl=9,len=1,*p=='*') ||
  1195. (op=C_DIV,opl=9,len=1,*p=='/') ||
  1196. (op=C_MOD,opl=9,len=1,*p=='%') ||
  1197. (op=C_LAND,opl=2,len=2,*p=='&' && p[1]=='&') ||
  1198. (op=C_AND,opl=6,len=1,*p=='&') ||
  1199. (op=C_LOR,opl=1,len=2,*p=='|' && p[1]=='|') ||
  1200. (op=C_OR,opl=5,len=1,*p=='|') ||
  1201. (op=C_XOR,opl=4,len=1,*p=='^') ||
  1202. (op=C_EQ,opl=3,len=2,*p=='=' && p[1]=='=') ||
  1203. (op=C_NE,opl=3,len=2,*p=='!' && p[1]=='=') ||
  1204. (op=C_R_SHIFT,opl=7,len=2,*p=='>' && p[1]=='>') ||
  1205. (op=C_GE,opl=3,len=2,*p=='>' && p[1]=='=') ||
  1206. (op=C_GT,opl=3,len=1,*p=='>') ||
  1207. (op=C_L_SHIFT,opl=7,len=2,*p=='<' && p[1]=='<') ||
  1208. (op=C_LE,opl=3,len=2,*p=='<' && p[1]=='=') ||
  1209. (op=C_LT,opl=3,len=1,*p=='<')) && opl>limit){
  1210. p+=len;
  1211. if(op == C_OP3) {
  1212. p=parse_subexpr(p,-1);
  1213. p=skip_space(p);
  1214. if( *(p++) != ':')
  1215. disp_error_message("parse_subexpr: expected ':'", p-1);
  1216. p=parse_subexpr(p,-1);
  1217. } else {
  1218. p=parse_subexpr(p,opl);
  1219. }
  1220. add_scriptc(op);
  1221. p=skip_space(p);
  1222. }
  1223. return p; /* return first untreated operator */
  1224. }
  1225. /*==========================================
  1226. * Evaluation of the expression
  1227. *------------------------------------------*/
  1228. const char* parse_expr(const char *p)
  1229. {
  1230. switch(*p){
  1231. case ')': case ';': case ':': case '[': case ']':
  1232. case '}':
  1233. disp_error_message("parse_expr: unexpected character",p);
  1234. }
  1235. p=parse_subexpr(p,-1);
  1236. return p;
  1237. }
  1238. /*==========================================
  1239. * Analysis of the line
  1240. *------------------------------------------*/
  1241. const char* parse_line(const char* p)
  1242. {
  1243. const char* p2;
  1244. p=skip_space(p);
  1245. if(*p==';') {
  1246. //Close decision for if(); for(); while();
  1247. p = parse_syntax_close(p + 1);
  1248. return p;
  1249. }
  1250. if(*p==')' && parse_syntax_for_flag)
  1251. return p+1;
  1252. p = skip_space(p);
  1253. if(p[0] == '{') {
  1254. syntax.curly[syntax.curly_count].type = TYPE_NULL;
  1255. syntax.curly[syntax.curly_count].count = -1;
  1256. syntax.curly[syntax.curly_count].index = -1;
  1257. syntax.curly_count++;
  1258. return p + 1;
  1259. } else if(p[0] == '}') {
  1260. return parse_curly_close(p);
  1261. }
  1262. // Syntax-related processing
  1263. p2 = parse_syntax(p);
  1264. if(p2 != NULL)
  1265. return p2;
  1266. // attempt to process a variable assignment
  1267. p2 = parse_variable(p);
  1268. if( p2 != NULL )
  1269. {// variable assignment processed so leave the method
  1270. return parse_syntax_close(p2 + 1);
  1271. }
  1272. p = parse_callfunc(p,0,0);
  1273. p = skip_space(p);
  1274. if(parse_syntax_for_flag) {
  1275. if( *p != ')' )
  1276. disp_error_message("parse_line: expected ')'",p);
  1277. } else {
  1278. if( *p != ';' )
  1279. disp_error_message("parse_line: expected ';'",p);
  1280. }
  1281. //Binding decision for if(), for(), while()
  1282. p = parse_syntax_close(p+1);
  1283. return p;
  1284. }
  1285. // { ... } Closing process
  1286. const char* parse_curly_close(const char* p)
  1287. {
  1288. if(syntax.curly_count <= 0) {
  1289. disp_error_message("parse_curly_close: unexpected string",p);
  1290. return p + 1;
  1291. } else if(syntax.curly[syntax.curly_count-1].type == TYPE_NULL) {
  1292. syntax.curly_count--;
  1293. //Close decision if, for , while
  1294. p = parse_syntax_close(p + 1);
  1295. return p;
  1296. } else if(syntax.curly[syntax.curly_count-1].type == TYPE_SWITCH) {
  1297. //Closing switch()
  1298. int pos = syntax.curly_count-1;
  1299. char label[256];
  1300. int l;
  1301. // Remove temporary variables
  1302. sprintf(label,"set $@__SW%x_VAL,0;",syntax.curly[pos].index);
  1303. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1304. parse_line(label);
  1305. syntax.curly_count--;
  1306. // Go to the end pointer unconditionally
  1307. sprintf(label,"goto __SW%x_FIN;",syntax.curly[pos].index);
  1308. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1309. parse_line(label);
  1310. syntax.curly_count--;
  1311. // You are here labeled
  1312. sprintf(label,"__SW%x_%x",syntax.curly[pos].index,syntax.curly[pos].count);
  1313. l=add_str(label);
  1314. set_label(l,script_pos, p);
  1315. if(syntax.curly[pos].flag) {
  1316. //Exists default
  1317. sprintf(label,"goto __SW%x_DEF;",syntax.curly[pos].index);
  1318. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1319. parse_line(label);
  1320. syntax.curly_count--;
  1321. }
  1322. // Label end
  1323. sprintf(label,"__SW%x_FIN",syntax.curly[pos].index);
  1324. l=add_str(label);
  1325. set_label(l,script_pos, p);
  1326. linkdb_final(&syntax.curly[pos].case_label); // free the list of case label
  1327. syntax.curly_count--;
  1328. //Closing decision if, for , while
  1329. p = parse_syntax_close(p + 1);
  1330. return p;
  1331. } else {
  1332. disp_error_message("parse_curly_close: unexpected string",p);
  1333. return p + 1;
  1334. }
  1335. }
  1336. // Syntax-related processing
  1337. // break, case, continue, default, do, for, function,
  1338. // if, switch, while ? will handle this internally.
  1339. const char* parse_syntax(const char* p)
  1340. {
  1341. const char *p2 = skip_word(p);
  1342. switch(*p) {
  1343. case 'B':
  1344. case 'b':
  1345. if(p2 - p == 5 && !strncasecmp(p,"break",5)) {
  1346. // break Processing
  1347. char label[256];
  1348. int pos = syntax.curly_count - 1;
  1349. while(pos >= 0) {
  1350. if(syntax.curly[pos].type == TYPE_DO) {
  1351. sprintf(label,"goto __DO%x_FIN;",syntax.curly[pos].index);
  1352. break;
  1353. } else if(syntax.curly[pos].type == TYPE_FOR) {
  1354. sprintf(label,"goto __FR%x_FIN;",syntax.curly[pos].index);
  1355. break;
  1356. } else if(syntax.curly[pos].type == TYPE_WHILE) {
  1357. sprintf(label,"goto __WL%x_FIN;",syntax.curly[pos].index);
  1358. break;
  1359. } else if(syntax.curly[pos].type == TYPE_SWITCH) {
  1360. sprintf(label,"goto __SW%x_FIN;",syntax.curly[pos].index);
  1361. break;
  1362. }
  1363. pos--;
  1364. }
  1365. if(pos < 0) {
  1366. disp_error_message("parse_syntax: unexpected 'break'",p);
  1367. } else {
  1368. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1369. parse_line(label);
  1370. syntax.curly_count--;
  1371. }
  1372. p = skip_space(p2);
  1373. if(*p != ';')
  1374. disp_error_message("parse_syntax: expected ';'",p);
  1375. // Closing decision if, for , while
  1376. p = parse_syntax_close(p + 1);
  1377. return p;
  1378. }
  1379. break;
  1380. case 'c':
  1381. case 'C':
  1382. if(p2 - p == 4 && !strncasecmp(p,"case",4)) {
  1383. //Processing case
  1384. int pos = syntax.curly_count-1;
  1385. if(pos < 0 || syntax.curly[pos].type != TYPE_SWITCH) {
  1386. disp_error_message("parse_syntax: unexpected 'case' ",p);
  1387. return p+1;
  1388. } else {
  1389. char label[256];
  1390. int l,v;
  1391. char *np;
  1392. if(syntax.curly[pos].count != 1) {
  1393. //Jump for FALLTHRU
  1394. sprintf(label,"goto __SW%x_%xJ;",syntax.curly[pos].index,syntax.curly[pos].count);
  1395. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1396. parse_line(label);
  1397. syntax.curly_count--;
  1398. // You are here labeled
  1399. sprintf(label,"__SW%x_%x",syntax.curly[pos].index,syntax.curly[pos].count);
  1400. l=add_str(label);
  1401. set_label(l,script_pos, p);
  1402. }
  1403. //Decision statement switch
  1404. p = skip_space(p2);
  1405. if(p == p2) {
  1406. disp_error_message("parse_syntax: expected a space ' '",p);
  1407. }
  1408. // check whether case label is integer or not
  1409. if(is_number(p)) {
  1410. //Numeric value
  1411. v = (int)strtol(p,&np,0);
  1412. if((*p == '-' || *p == '+') && ISDIGIT(p[1])) // pre-skip because '-' can not skip_word
  1413. p++;
  1414. p = skip_word(p);
  1415. if(np != p)
  1416. disp_error_message("parse_syntax: 'case' label is not an integer",np);
  1417. } else {
  1418. //Check for constants
  1419. p2 = skip_word(p);
  1420. v = (int)(size_t) (p2-p); // length of word at p2
  1421. memcpy(label,p,v);
  1422. label[v]='\0';
  1423. if( !script_get_constant(label, &v) )
  1424. disp_error_message("parse_syntax: 'case' label is not an integer",p);
  1425. p = skip_word(p);
  1426. }
  1427. p = skip_space(p);
  1428. if(*p != ':')
  1429. disp_error_message("parse_syntax: expect ':'",p);
  1430. sprintf(label,"if(%d != $@__SW%x_VAL) goto __SW%x_%x;",
  1431. v,syntax.curly[pos].index,syntax.curly[pos].index,syntax.curly[pos].count+1);
  1432. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1433. // Bad I do not parse twice
  1434. p2 = parse_line(label);
  1435. parse_line(p2);
  1436. syntax.curly_count--;
  1437. if(syntax.curly[pos].count != 1) {
  1438. // Label after the completion of FALLTHRU
  1439. sprintf(label,"__SW%x_%xJ",syntax.curly[pos].index,syntax.curly[pos].count);
  1440. l=add_str(label);
  1441. set_label(l,script_pos,p);
  1442. }
  1443. // check duplication of case label [Rayce]
  1444. if(linkdb_search(&syntax.curly[pos].case_label, (void*)__64BPRTSIZE(v)) != NULL)
  1445. disp_error_message("parse_syntax: dup 'case'",p);
  1446. linkdb_insert(&syntax.curly[pos].case_label, (void*)__64BPRTSIZE(v), (void*)1);
  1447. sprintf(label,"set $@__SW%x_VAL,0;",syntax.curly[pos].index);
  1448. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1449. parse_line(label);
  1450. syntax.curly_count--;
  1451. syntax.curly[pos].count++;
  1452. }
  1453. return p + 1;
  1454. } else if(p2 - p == 8 && !strncasecmp(p,"continue",8)) {
  1455. // Processing continue
  1456. char label[256];
  1457. int pos = syntax.curly_count - 1;
  1458. while(pos >= 0) {
  1459. if(syntax.curly[pos].type == TYPE_DO) {
  1460. sprintf(label,"goto __DO%x_NXT;",syntax.curly[pos].index);
  1461. syntax.curly[pos].flag = 1; //Flag put the link for continue
  1462. break;
  1463. } else if(syntax.curly[pos].type == TYPE_FOR) {
  1464. sprintf(label,"goto __FR%x_NXT;",syntax.curly[pos].index);
  1465. break;
  1466. } else if(syntax.curly[pos].type == TYPE_WHILE) {
  1467. sprintf(label,"goto __WL%x_NXT;",syntax.curly[pos].index);
  1468. break;
  1469. }
  1470. pos--;
  1471. }
  1472. if(pos < 0) {
  1473. disp_error_message("parse_syntax: unexpected 'continue'",p);
  1474. } else {
  1475. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1476. parse_line(label);
  1477. syntax.curly_count--;
  1478. }
  1479. p = skip_space(p2);
  1480. if(*p != ';')
  1481. disp_error_message("parse_syntax: expected ';'",p);
  1482. //Closing decision if, for , while
  1483. p = parse_syntax_close(p + 1);
  1484. return p;
  1485. }
  1486. break;
  1487. case 'd':
  1488. case 'D':
  1489. if(p2 - p == 7 && !strncasecmp(p,"default",7)) {
  1490. // Switch - default processing
  1491. int pos = syntax.curly_count-1;
  1492. if(pos < 0 || syntax.curly[pos].type != TYPE_SWITCH) {
  1493. disp_error_message("parse_syntax: unexpected 'default'",p);
  1494. } else if(syntax.curly[pos].flag) {
  1495. disp_error_message("parse_syntax: dup 'default'",p);
  1496. } else {
  1497. char label[256];
  1498. int l;
  1499. // Put the label location
  1500. p = skip_space(p2);
  1501. if(*p != ':') {
  1502. disp_error_message("parse_syntax: expected ':'",p);
  1503. }
  1504. sprintf(label,"__SW%x_%x",syntax.curly[pos].index,syntax.curly[pos].count);
  1505. l=add_str(label);
  1506. set_label(l,script_pos,p);
  1507. // Skip to the next link w/o condition
  1508. sprintf(label,"goto __SW%x_%x;",syntax.curly[pos].index,syntax.curly[pos].count+1);
  1509. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1510. parse_line(label);
  1511. syntax.curly_count--;
  1512. // The default label
  1513. sprintf(label,"__SW%x_DEF",syntax.curly[pos].index);
  1514. l=add_str(label);
  1515. set_label(l,script_pos,p);
  1516. syntax.curly[syntax.curly_count - 1].flag = 1;
  1517. syntax.curly[pos].count++;
  1518. }
  1519. return p + 1;
  1520. } else if(p2 - p == 2 && !strncasecmp(p,"do",2)) {
  1521. int l;
  1522. char label[256];
  1523. p=skip_space(p2);
  1524. syntax.curly[syntax.curly_count].type = TYPE_DO;
  1525. syntax.curly[syntax.curly_count].count = 1;
  1526. syntax.curly[syntax.curly_count].index = syntax.index++;
  1527. syntax.curly[syntax.curly_count].flag = 0;
  1528. // Label of the (do) form here
  1529. sprintf(label,"__DO%x_BGN",syntax.curly[syntax.curly_count].index);
  1530. l=add_str(label);
  1531. set_label(l,script_pos,p);
  1532. syntax.curly_count++;
  1533. return p;
  1534. }
  1535. break;
  1536. case 'f':
  1537. case 'F':
  1538. if(p2 - p == 3 && !strncasecmp(p,"for",3)) {
  1539. int l;
  1540. char label[256];
  1541. int pos = syntax.curly_count;
  1542. syntax.curly[syntax.curly_count].type = TYPE_FOR;
  1543. syntax.curly[syntax.curly_count].count = 1;
  1544. syntax.curly[syntax.curly_count].index = syntax.index++;
  1545. syntax.curly[syntax.curly_count].flag = 0;
  1546. syntax.curly_count++;
  1547. p=skip_space(p2);
  1548. if(*p != '(')
  1549. disp_error_message("parse_syntax: expected '('",p);
  1550. p++;
  1551. // Execute the initialization statement
  1552. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1553. p=parse_line(p);
  1554. syntax.curly_count--;
  1555. // Form the start of label decision
  1556. sprintf(label,"__FR%x_J",syntax.curly[pos].index);
  1557. l=add_str(label);
  1558. set_label(l,script_pos,p);
  1559. p=skip_space(p);
  1560. if(*p == ';') {
  1561. // For (; Because the pattern of always true ;)
  1562. ;
  1563. } else {
  1564. // Skip to the end point if the condition is false
  1565. sprintf(label,"__FR%x_FIN",syntax.curly[pos].index);
  1566. add_scriptl(add_str("jump_zero"));
  1567. add_scriptc(C_ARG);
  1568. p=parse_expr(p);
  1569. p=skip_space(p);
  1570. add_scriptl(add_str(label));
  1571. add_scriptc(C_FUNC);
  1572. }
  1573. if(*p != ';')
  1574. disp_error_message("parse_syntax: expected ';'",p);
  1575. p++;
  1576. // Skip to the beginning of the loop
  1577. sprintf(label,"goto __FR%x_BGN;",syntax.curly[pos].index);
  1578. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1579. parse_line(label);
  1580. syntax.curly_count--;
  1581. // Labels to form the next loop
  1582. sprintf(label,"__FR%x_NXT",syntax.curly[pos].index);
  1583. l=add_str(label);
  1584. set_label(l,script_pos,p);
  1585. // Process the next time you enter the loop
  1586. // A ')' last for; flag to be treated as'
  1587. parse_syntax_for_flag = 1;
  1588. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1589. p=parse_line(p);
  1590. syntax.curly_count--;
  1591. parse_syntax_for_flag = 0;
  1592. // Skip to the determination process conditions
  1593. sprintf(label,"goto __FR%x_J;",syntax.curly[pos].index);
  1594. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1595. parse_line(label);
  1596. syntax.curly_count--;
  1597. // Loop start labeling
  1598. sprintf(label,"__FR%x_BGN",syntax.curly[pos].index);
  1599. l=add_str(label);
  1600. set_label(l,script_pos,p);
  1601. return p;
  1602. }
  1603. else if( p2 - p == 8 && strncasecmp(p,"function",8) == 0 )
  1604. {// internal script function
  1605. const char *func_name;
  1606. func_name = skip_space(p2);
  1607. p = skip_word(func_name);
  1608. if( p == func_name )
  1609. disp_error_message("parse_syntax:function: function name is missing or invalid", p);
  1610. p2 = skip_space(p);
  1611. if( *p2 == ';' )
  1612. {// function <name> ;
  1613. // function declaration - just register the name
  1614. int l;
  1615. l = add_word(func_name);
  1616. if( str_data[l].type == C_NOP )// register only, if the name was not used by something else
  1617. str_data[l].type = C_USERFUNC;
  1618. else if( str_data[l].type == C_USERFUNC )
  1619. ; // already registered
  1620. else
  1621. disp_error_message("parse_syntax:function: function name is invalid", func_name);
  1622. // Close condition of if, for, while
  1623. p = parse_syntax_close(p2 + 1);
  1624. return p;
  1625. }
  1626. else if(*p2 == '{')
  1627. {// function <name> <line/block of code>
  1628. char label[256];
  1629. int l;
  1630. syntax.curly[syntax.curly_count].type = TYPE_USERFUNC;
  1631. syntax.curly[syntax.curly_count].count = 1;
  1632. syntax.curly[syntax.curly_count].index = syntax.index++;
  1633. syntax.curly[syntax.curly_count].flag = 0;
  1634. ++syntax.curly_count;
  1635. // Jump over the function code
  1636. sprintf(label, "goto __FN%x_FIN;", syntax.curly[syntax.curly_count-1].index);
  1637. syntax.curly[syntax.curly_count].type = TYPE_NULL;
  1638. ++syntax.curly_count;
  1639. parse_line(label);
  1640. --syntax.curly_count;
  1641. // Set the position of the function (label)
  1642. l=add_word(func_name);
  1643. if( str_data[l].type == C_NOP || str_data[l].type == C_USERFUNC )// register only, if the name was not used by something else
  1644. {
  1645. str_data[l].type = C_USERFUNC;
  1646. set_label(l, script_pos, p);
  1647. if( parse_options&SCRIPT_USE_LABEL_DB )
  1648. strdb_iput(scriptlabel_db, get_str(l), script_pos);
  1649. }
  1650. else
  1651. disp_error_message("parse_syntax:function: function name is invalid", func_name);
  1652. return skip_space(p);
  1653. }
  1654. else
  1655. {
  1656. disp_error_message("expect ';' or '{' at function syntax",p);
  1657. }
  1658. }
  1659. break;
  1660. case 'i':
  1661. case 'I':
  1662. if(p2 - p == 2 && !strncasecmp(p,"if",2)) {
  1663. // If process
  1664. char label[256];
  1665. p=skip_space(p2);
  1666. if(*p != '(') { //Prevent if this {} non-c syntax. from Rayce (jA)
  1667. disp_error_message("need '('",p);
  1668. }
  1669. syntax.curly[syntax.curly_count].type = TYPE_IF;
  1670. syntax.curly[syntax.curly_count].count = 1;
  1671. syntax.curly[syntax.curly_count].index = syntax.index++;
  1672. syntax.curly[syntax.curly_count].flag = 0;
  1673. sprintf(label,"__IF%x_%x",syntax.curly[syntax.curly_count].index,syntax.curly[syntax.curly_count].count);
  1674. syntax.curly_count++;
  1675. add_scriptl(add_str("jump_zero"));
  1676. add_scriptc(C_ARG);
  1677. p=parse_expr(p);
  1678. p=skip_space(p);
  1679. add_scriptl(add_str(label));
  1680. add_scriptc(C_FUNC);
  1681. return p;
  1682. }
  1683. break;
  1684. case 's':
  1685. case 'S':
  1686. if(p2 - p == 6 && !strncasecmp(p,"switch",6)) {
  1687. // Processing of switch ()
  1688. char label[256];
  1689. p=skip_space(p2);
  1690. if(*p != '(') {
  1691. disp_error_message("need '('",p);
  1692. }
  1693. syntax.curly[syntax.curly_count].type = TYPE_SWITCH;
  1694. syntax.curly[syntax.curly_count].count = 1;
  1695. syntax.curly[syntax.curly_count].index = syntax.index++;
  1696. syntax.curly[syntax.curly_count].flag = 0;
  1697. sprintf(label,"$@__SW%x_VAL",syntax.curly[syntax.curly_count].index);
  1698. syntax.curly_count++;
  1699. add_scriptl(add_str("set"));
  1700. add_scriptc(C_ARG);
  1701. add_scriptl(add_str(label));
  1702. p=parse_expr(p);
  1703. p=skip_space(p);
  1704. if(*p != '{') {
  1705. disp_error_message("parse_syntax: expected '{'",p);
  1706. }
  1707. add_scriptc(C_FUNC);
  1708. return p + 1;
  1709. }
  1710. break;
  1711. case 'w':
  1712. case 'W':
  1713. if(p2 - p == 5 && !strncasecmp(p,"while",5)) {
  1714. int l;
  1715. char label[256];
  1716. p=skip_space(p2);
  1717. if(*p != '(') {
  1718. disp_error_message("need '('",p);
  1719. }
  1720. syntax.curly[syntax.curly_count].type = TYPE_WHILE;
  1721. syntax.curly[syntax.curly_count].count = 1;
  1722. syntax.curly[syntax.curly_count].index = syntax.index++;
  1723. syntax.curly[syntax.curly_count].flag = 0;
  1724. // Form the start of label decision
  1725. sprintf(label,"__WL%x_NXT",syntax.curly[syntax.curly_count].index);
  1726. l=add_str(label);
  1727. set_label(l,script_pos,p);
  1728. // Skip to the end point if the condition is false
  1729. sprintf(label,"__WL%x_FIN",syntax.curly[syntax.curly_count].index);
  1730. syntax.curly_count++;
  1731. add_scriptl(add_str("jump_zero"));
  1732. add_scriptc(C_ARG);
  1733. p=parse_expr(p);
  1734. p=skip_space(p);
  1735. add_scriptl(add_str(label));
  1736. add_scriptc(C_FUNC);
  1737. return p;
  1738. }
  1739. break;
  1740. }
  1741. return NULL;
  1742. }
  1743. const char* parse_syntax_close(const char *p) {
  1744. // If (...) for (...) hoge (); as to make sure closed closed once again
  1745. int flag;
  1746. do {
  1747. p = parse_syntax_close_sub(p,&flag);
  1748. } while(flag);
  1749. return p;
  1750. }
  1751. // Close judgment if, for, while, of do
  1752. // flag == 1 : closed
  1753. // flag == 0 : not closed
  1754. const char* parse_syntax_close_sub(const char* p,int* flag)
  1755. {
  1756. char label[256];
  1757. int pos = syntax.curly_count - 1;
  1758. int l;
  1759. *flag = 1;
  1760. if(syntax.curly_count <= 0) {
  1761. *flag = 0;
  1762. return p;
  1763. } else if(syntax.curly[pos].type == TYPE_IF) {
  1764. const char *bp = p;
  1765. const char *p2;
  1766. // if-block and else-block end is a new line
  1767. parse_nextline(false, p);
  1768. // Skip to the last location if
  1769. sprintf(label,"goto __IF%x_FIN;",syntax.curly[pos].index);
  1770. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1771. parse_line(label);
  1772. syntax.curly_count--;
  1773. // Put the label of the location
  1774. sprintf(label,"__IF%x_%x",syntax.curly[pos].index,syntax.curly[pos].count);
  1775. l=add_str(label);
  1776. set_label(l,script_pos,p);
  1777. syntax.curly[pos].count++;
  1778. p = skip_space(p);
  1779. p2 = skip_word(p);
  1780. if(!syntax.curly[pos].flag && p2 - p == 4 && !strncasecmp(p,"else",4)) {
  1781. // else or else - if
  1782. p = skip_space(p2);
  1783. p2 = skip_word(p);
  1784. if(p2 - p == 2 && !strncasecmp(p,"if",2)) {
  1785. // else - if
  1786. p=skip_space(p2);
  1787. if(*p != '(') {
  1788. disp_error_message("need '('",p);
  1789. }
  1790. sprintf(label,"__IF%x_%x",syntax.curly[pos].index,syntax.curly[pos].count);
  1791. add_scriptl(add_str("jump_zero"));
  1792. add_scriptc(C_ARG);
  1793. p=parse_expr(p);
  1794. p=skip_space(p);
  1795. add_scriptl(add_str(label));
  1796. add_scriptc(C_FUNC);
  1797. *flag = 0;
  1798. return p;
  1799. } else {
  1800. // else
  1801. if(!syntax.curly[pos].flag) {
  1802. syntax.curly[pos].flag = 1;
  1803. *flag = 0;
  1804. return p;
  1805. }
  1806. }
  1807. }
  1808. // Close if
  1809. syntax.curly_count--;
  1810. // Put the label of the final location
  1811. sprintf(label,"__IF%x_FIN",syntax.curly[pos].index);
  1812. l=add_str(label);
  1813. set_label(l,script_pos,p);
  1814. if(syntax.curly[pos].flag == 1) {
  1815. // Because the position of the pointer is the same if not else for this
  1816. return bp;
  1817. }
  1818. return p;
  1819. } else if(syntax.curly[pos].type == TYPE_DO) {
  1820. int l2;
  1821. char label2[256];
  1822. const char *p2;
  1823. if(syntax.curly[pos].flag) {
  1824. // (Come here continue) to form the label here
  1825. sprintf(label2,"__DO%x_NXT",syntax.curly[pos].index);
  1826. l2=add_str(label2);
  1827. set_label(l2,script_pos,p);
  1828. }
  1829. // Skip to the end point if the condition is false
  1830. p = skip_space(p);
  1831. p2 = skip_word(p);
  1832. if(p2 - p != 5 || strncasecmp(p,"while",5))
  1833. disp_error_message("parse_syntax: expected 'while'",p);
  1834. p = skip_space(p2);
  1835. if(*p != '(') {
  1836. disp_error_message("need '('",p);
  1837. }
  1838. // do-block end is a new line
  1839. parse_nextline(false, p);
  1840. sprintf(label2,"__DO%x_FIN",syntax.curly[pos].index);
  1841. add_scriptl(add_str("jump_zero"));
  1842. add_scriptc(C_ARG);
  1843. p=parse_expr(p);
  1844. p=skip_space(p);
  1845. add_scriptl(add_str(label2));
  1846. add_scriptc(C_FUNC);
  1847. // Skip to the starting point
  1848. sprintf(label2,"goto __DO%x_BGN;",syntax.curly[pos].index);
  1849. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1850. parse_line(label2);
  1851. syntax.curly_count--;
  1852. // Form label of the end point conditions
  1853. sprintf(label2,"__DO%x_FIN",syntax.curly[pos].index);
  1854. l2=add_str(label2);
  1855. set_label(l2,script_pos,p);
  1856. p = skip_space(p);
  1857. if(*p != ';') {
  1858. disp_error_message("parse_syntax: expected ';'",p);
  1859. return p+1;
  1860. }
  1861. p++;
  1862. syntax.curly_count--;
  1863. return p;
  1864. } else if(syntax.curly[pos].type == TYPE_FOR) {
  1865. // for-block end is a new line
  1866. parse_nextline(false, p);
  1867. // Skip to the next loop
  1868. sprintf(label,"goto __FR%x_NXT;",syntax.curly[pos].index);
  1869. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1870. parse_line(label);
  1871. syntax.curly_count--;
  1872. // End for labeling
  1873. sprintf(label,"__FR%x_FIN",syntax.curly[pos].index);
  1874. l=add_str(label);
  1875. set_label(l,script_pos,p);
  1876. syntax.curly_count--;
  1877. return p;
  1878. } else if(syntax.curly[pos].type == TYPE_WHILE) {
  1879. // while-block end is a new line
  1880. parse_nextline(false, p);
  1881. // Skip to the decision while
  1882. sprintf(label,"goto __WL%x_NXT;",syntax.curly[pos].index);
  1883. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1884. parse_line(label);
  1885. syntax.curly_count--;
  1886. // End while labeling
  1887. sprintf(label,"__WL%x_FIN",syntax.curly[pos].index);
  1888. l=add_str(label);
  1889. set_label(l,script_pos,p);
  1890. syntax.curly_count--;
  1891. return p;
  1892. } else if(syntax.curly[syntax.curly_count-1].type == TYPE_USERFUNC) {
  1893. int pos2 = syntax.curly_count-1;
  1894. char label2[256];
  1895. int l2;
  1896. // Back
  1897. sprintf(label2,"return;");
  1898. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1899. parse_line(label2);
  1900. syntax.curly_count--;
  1901. // Put the label of the location
  1902. sprintf(label2,"__FN%x_FIN",syntax.curly[pos2].index);
  1903. l2=add_str(label2);
  1904. set_label(l2,script_pos,p);
  1905. syntax.curly_count--;
  1906. return p;
  1907. } else {
  1908. *flag = 0;
  1909. return p;
  1910. }
  1911. }
  1912. /*==========================================
  1913. * Added built-in functions
  1914. *------------------------------------------*/
  1915. static void add_buildin_func(void)
  1916. {
  1917. int i;
  1918. for( i = 0; buildin_func[i].func; i++ )
  1919. {
  1920. // arg must follow the pattern: (v|s|i|r|l)*\?*\*?
  1921. // 'v' - value (either string or int or reference)
  1922. // 's' - string
  1923. // 'i' - int
  1924. // 'r' - reference (of a variable)
  1925. // 'l' - label
  1926. // '?' - one optional parameter
  1927. // '*' - unknown number of optional parameters
  1928. const char* p = buildin_func[i].arg;
  1929. while( *p == 'v' || *p == 's' || *p == 'i' || *p == 'r' || *p == 'l' ) ++p;
  1930. while( *p == '?' ) ++p;
  1931. if( *p == '*' ) ++p;
  1932. if( *p != 0){
  1933. ShowWarning("add_buildin_func: ignoring function \"%s\" with invalid arg \"%s\".\n", buildin_func[i].name, buildin_func[i].arg);
  1934. } else if( *skip_word(buildin_func[i].name) != 0 ){
  1935. ShowWarning("add_buildin_func: ignoring function with invalid name \"%s\" (must be a word).\n", buildin_func[i].name);
  1936. } else {
  1937. int n = add_str(buildin_func[i].name);
  1938. str_data[n].type = C_FUNC;
  1939. str_data[n].val = i;
  1940. str_data[n].func = buildin_func[i].func;
  1941. if (!strcmp(buildin_func[i].name, "set")) buildin_set_ref = n;
  1942. else if (!strcmp(buildin_func[i].name, "callsub")) buildin_callsub_ref = n;
  1943. else if (!strcmp(buildin_func[i].name, "callfunc")) buildin_callfunc_ref = n;
  1944. else if( !strcmp(buildin_func[i].name, "getelementofarray") ) buildin_getelementofarray_ref = n;
  1945. }
  1946. }
  1947. }
  1948. /// Retrieves the value of a constant.
  1949. bool script_get_constant(const char* name, int* value)
  1950. {
  1951. int n = search_str(name);
  1952. if( n == -1 || str_data[n].type != C_INT )
  1953. {// not found or not a constant
  1954. return false;
  1955. }
  1956. value[0] = str_data[n].val;
  1957. return true;
  1958. }
  1959. /// Creates new constant or parameter with given value.
  1960. void script_set_constant(const char* name, int value, bool isparameter)
  1961. {
  1962. int n = add_str(name);
  1963. if( str_data[n].type == C_NOP )
  1964. {// new
  1965. str_data[n].type = isparameter ? C_PARAM : C_INT;
  1966. str_data[n].val = value;
  1967. }
  1968. else if( str_data[n].type == C_PARAM || str_data[n].type == C_INT )
  1969. {// existing parameter or constant
  1970. 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);
  1971. }
  1972. else
  1973. {// existing name
  1974. ShowError("script_set_constant: Invalid name for %s '%s' (already defined as %s).\n", isparameter ? "parameter" : "constant", name, script_op2name(str_data[n].type));
  1975. }
  1976. }
  1977. /*==========================================
  1978. * Reading constant databases
  1979. * const.txt
  1980. *------------------------------------------*/
  1981. static void read_constdb(void)
  1982. {
  1983. FILE *fp;
  1984. char line[1024],name[1024],val[1024];
  1985. int type;
  1986. int entries=0, skipped=0, linenum=0;
  1987. sprintf(line, "%s/const.txt", db_path);
  1988. fp=fopen(line, "r");
  1989. if(fp==NULL){
  1990. ShowError("can't read %s\n", line);
  1991. return ;
  1992. }
  1993. while(fgets(line, sizeof(line), fp))
  1994. {
  1995. linenum++;
  1996. if( line[0] == '\0' || line[0] == '\n' || line[0] == '\r') //ignore empty line
  1997. continue;
  1998. if(line[0]=='/' && line[1]=='/') //ignore commented line
  1999. continue;
  2000. type=0;
  2001. if(sscanf(line,"%1023[A-Za-z0-9/_],%1023[A-Za-z0-9/_-],%11d",name,val,&type)>=2 ||
  2002. sscanf(line,"%1023[A-Za-z0-9/_] %1023[A-Za-z0-9/_-] %11d",name,val,&type)>=2){
  2003. entries++;
  2004. script_set_constant(name, (int)strtol(val, NULL, 0), (bool)type);
  2005. }
  2006. else {
  2007. skipped++;
  2008. ShowWarning("Skipping line '"CL_WHITE"%d"CL_RESET"', invalide constant definition\n",linenum);
  2009. }
  2010. }
  2011. fclose(fp);
  2012. ShowStatus("Done reading '"CL_WHITE"%d"CL_RESET"' entries in '"CL_WHITE"%s/const.txt"CL_RESET"'.\n", entries, db_path);
  2013. if(skipped){
  2014. ShowWarning("Skipped '"CL_WHITE"%d"CL_RESET"', entries\n",skipped);
  2015. }
  2016. }
  2017. /**
  2018. * Sets source-end constants for NPC scripts to access.
  2019. **/
  2020. void script_hardcoded_constants(void) {
  2021. #include "script_constants.h"
  2022. }
  2023. /*==========================================
  2024. * Display emplacement line of script
  2025. *------------------------------------------*/
  2026. static const char* script_print_line(StringBuf* buf, const char* p, const char* mark, int line)
  2027. {
  2028. int i;
  2029. if( p == NULL || !p[0] ) return NULL;
  2030. if( line < 0 )
  2031. StringBuf_Printf(buf, "*% 5d : ", -line);
  2032. else
  2033. StringBuf_Printf(buf, " % 5d : ", line);
  2034. for(i=0;p[i] && p[i] != '\n';i++){
  2035. if(p + i != mark)
  2036. StringBuf_Printf(buf, "%c", p[i]);
  2037. else
  2038. StringBuf_Printf(buf, "\'%c\'", p[i]);
  2039. }
  2040. StringBuf_AppendStr(buf, "\n");
  2041. return p+i+(p[i] == '\n' ? 1 : 0);
  2042. }
  2043. 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) {
  2044. // Find the line where the error occurred
  2045. int j;
  2046. int line = start_line;
  2047. const char *p;
  2048. const char *linestart[5] = { NULL, NULL, NULL, NULL, NULL };
  2049. for(p=src;p && *p;line++){
  2050. const char *lineend=strchr(p,'\n');
  2051. if(lineend==NULL || error_pos_cur<lineend){
  2052. break;
  2053. }
  2054. for( j = 0; j < 4; j++ ) {
  2055. linestart[j] = linestart[j+1];
  2056. }
  2057. linestart[4] = p;
  2058. p=lineend+1;
  2059. }
  2060. StringBuf_Printf(buf, "script error on %s line %d\n", file, line);
  2061. StringBuf_Printf(buf, " %s\n", error_msg_cur);
  2062. for(j = 0; j < 5; j++ ) {
  2063. script_print_line(buf, linestart[j], NULL, line + j - 5);
  2064. }
  2065. p = script_print_line(buf, p, error_pos_cur, -line);
  2066. for(j = 0; j < 5; j++) {
  2067. p = script_print_line(buf, p, NULL, line + j + 1 );
  2068. }
  2069. }
  2070. void script_error(const char* src, const char* file, int start_line, const char* error_msg_cur, const char* error_pos_cur) {
  2071. StringBuf buf;
  2072. StringBuf_Init(&buf);
  2073. StringBuf_AppendStr(&buf, "\a\n");
  2074. script_errorwarning_sub(&buf, src, file, start_line, error_msg_cur, error_pos_cur);
  2075. ShowError("%s", StringBuf_Value(&buf));
  2076. StringBuf_Destroy(&buf);
  2077. }
  2078. void script_warning(const char* src, const char* file, int start_line, const char* error_msg_cur, const char* error_pos_cur) {
  2079. StringBuf buf;
  2080. StringBuf_Init(&buf);
  2081. script_errorwarning_sub(&buf, src, file, start_line, error_msg_cur, error_pos_cur);
  2082. ShowWarning("%s", StringBuf_Value(&buf));
  2083. StringBuf_Destroy(&buf);
  2084. }
  2085. /*==========================================
  2086. * Analysis of the script
  2087. *------------------------------------------*/
  2088. struct script_code* parse_script(const char *src,const char *file,int line,int options)
  2089. {
  2090. const char *p,*tmpp;
  2091. int i;
  2092. struct script_code* code = NULL;
  2093. static int first=1;
  2094. char end;
  2095. bool unresolved_names = false;
  2096. parser_current_src = src;
  2097. parser_current_file = file;
  2098. parser_current_line = line;
  2099. if( src == NULL )
  2100. return NULL;// empty script
  2101. memset(&syntax,0,sizeof(syntax));
  2102. if(first){
  2103. add_buildin_func();
  2104. read_constdb();
  2105. script_hardcoded_constants();
  2106. first=0;
  2107. }
  2108. script_buf=(unsigned char *)aMalloc(SCRIPT_BLOCK_SIZE*sizeof(unsigned char));
  2109. script_pos=0;
  2110. script_size=SCRIPT_BLOCK_SIZE;
  2111. parse_nextline(true, NULL);
  2112. // who called parse_script is responsible for clearing the database after using it, but just in case... lets clear it here
  2113. if( options&SCRIPT_USE_LABEL_DB )
  2114. db_clear(scriptlabel_db);
  2115. parse_options = options;
  2116. if( setjmp( error_jump ) != 0 ) {
  2117. //Restore program state when script has problems. [from jA]
  2118. int j;
  2119. const int size = ARRAYLENGTH(syntax.curly);
  2120. if( error_report )
  2121. script_error(src,file,line,error_msg,error_pos);
  2122. aFree( error_msg );
  2123. aFree( script_buf );
  2124. script_pos = 0;
  2125. script_size = 0;
  2126. script_buf = NULL;
  2127. for(j=LABEL_START;j<str_num;j++)
  2128. if(str_data[j].type == C_NOP) str_data[j].type = C_NAME;
  2129. for(j=0; j<size; j++)
  2130. linkdb_final(&syntax.curly[j].case_label);
  2131. return NULL;
  2132. }
  2133. parse_syntax_for_flag=0;
  2134. p=src;
  2135. p=skip_space(p);
  2136. if( options&SCRIPT_IGNORE_EXTERNAL_BRACKETS )
  2137. {// does not require brackets around the script
  2138. if( *p == '\0' && !(options&SCRIPT_RETURN_EMPTY_SCRIPT) )
  2139. {// empty script and can return NULL
  2140. aFree( script_buf );
  2141. script_pos = 0;
  2142. script_size = 0;
  2143. script_buf = NULL;
  2144. return NULL;
  2145. }
  2146. end = '\0';
  2147. }
  2148. else
  2149. {// requires brackets around the script
  2150. if( *p != '{' )
  2151. disp_error_message("not found '{'",p);
  2152. p = skip_space(p+1);
  2153. if( *p == '}' && !(options&SCRIPT_RETURN_EMPTY_SCRIPT) )
  2154. {// empty script and can return NULL
  2155. aFree( script_buf );
  2156. script_pos = 0;
  2157. script_size = 0;
  2158. script_buf = NULL;
  2159. return NULL;
  2160. }
  2161. end = '}';
  2162. }
  2163. // clear references of labels, variables and internal functions
  2164. for(i=LABEL_START;i<str_num;i++){
  2165. if(
  2166. str_data[i].type==C_POS || str_data[i].type==C_NAME ||
  2167. str_data[i].type==C_USERFUNC || str_data[i].type == C_USERFUNC_POS
  2168. ){
  2169. str_data[i].type=C_NOP;
  2170. str_data[i].backpatch=-1;
  2171. str_data[i].label=-1;
  2172. }
  2173. }
  2174. while( syntax.curly_count != 0 || *p != end )
  2175. {
  2176. if( *p == '\0' )
  2177. disp_error_message("unexpected end of script",p);
  2178. // Special handling only label
  2179. tmpp=skip_space(skip_word(p));
  2180. if(*tmpp==':' && !(!strncasecmp(p,"default:",8) && p + 7 == tmpp)){
  2181. i=add_word(p);
  2182. set_label(i,script_pos,p);
  2183. if( parse_options&SCRIPT_USE_LABEL_DB )
  2184. strdb_iput(scriptlabel_db, get_str(i), script_pos);
  2185. p=tmpp+1;
  2186. p=skip_space(p);
  2187. continue;
  2188. }
  2189. // All other lumped
  2190. p=parse_line(p);
  2191. p=skip_space(p);
  2192. parse_nextline(false, p);
  2193. }
  2194. add_scriptc(C_NOP);
  2195. // trim code to size
  2196. script_size = script_pos;
  2197. RECREATE(script_buf,unsigned char,script_pos);
  2198. // default unknown references to variables
  2199. for(i=LABEL_START;i<str_num;i++){
  2200. if(str_data[i].type==C_NOP){
  2201. int j;
  2202. str_data[i].type=C_NAME;
  2203. str_data[i].label=i;
  2204. for(j=str_data[i].backpatch;j>=0 && j!=0x00ffffff;){
  2205. int next=GETVALUE(script_buf,j);
  2206. SETVALUE(script_buf,j,i);
  2207. j=next;
  2208. }
  2209. }
  2210. else if( str_data[i].type == C_USERFUNC )
  2211. {// 'function name;' without follow-up code
  2212. ShowError("parse_script: function '%s' declared but not defined.\n", str_buf+str_data[i].str);
  2213. unresolved_names = true;
  2214. }
  2215. }
  2216. if( unresolved_names )
  2217. {
  2218. disp_error_message("parse_script: unresolved function references", p);
  2219. }
  2220. #ifdef DEBUG_DISP
  2221. for(i=0;i<script_pos;i++){
  2222. if((i&15)==0) ShowMessage("%04x : ",i);
  2223. ShowMessage("%02x ",script_buf[i]);
  2224. if((i&15)==15) ShowMessage("\n");
  2225. }
  2226. ShowMessage("\n");
  2227. #endif
  2228. #ifdef DEBUG_DISASM
  2229. {
  2230. int i = 0,j;
  2231. while(i < script_pos) {
  2232. c_op op = get_com(script_buf,&i);
  2233. ShowMessage("%06x %s", i, script_op2name(op));
  2234. j = i;
  2235. switch(op) {
  2236. case C_INT:
  2237. ShowMessage(" %d", get_num(script_buf,&i));
  2238. break;
  2239. case C_POS:
  2240. ShowMessage(" 0x%06x", *(int*)(script_buf+i)&0xffffff);
  2241. i += 3;
  2242. break;
  2243. case C_NAME:
  2244. j = (*(int*)(script_buf+i)&0xffffff);
  2245. ShowMessage(" %s", ( j == 0xffffff ) ? "?? unknown ??" : get_str(j));
  2246. i += 3;
  2247. break;
  2248. case C_STR:
  2249. j = strlen(script_buf + i);
  2250. ShowMessage(" %s", script_buf + i);
  2251. i += j+1;
  2252. break;
  2253. }
  2254. ShowMessage(CL_CLL"\n");
  2255. }
  2256. }
  2257. #endif
  2258. CREATE(code,struct script_code,1);
  2259. code->script_buf = script_buf;
  2260. code->script_size = script_size;
  2261. code->local.vars = NULL;
  2262. code->local.arrays = NULL;
  2263. return code;
  2264. }
  2265. /// Returns the player attached to this script, identified by the rid.
  2266. /// If there is no player attached, the script is terminated.
  2267. TBL_PC *script_rid2sd(struct script_state *st)
  2268. {
  2269. TBL_PC *sd=map_id2sd(st->rid);
  2270. if(!sd){
  2271. ShowError("script_rid2sd: fatal error ! player not attached!\n");
  2272. script_reportfunc(st);
  2273. script_reportsrc(st);
  2274. st->state = END;
  2275. }
  2276. return sd;
  2277. }
  2278. /**
  2279. * Dereferences a variable/constant, replacing it with a copy of the value.
  2280. * @param st Script state
  2281. * @param data Variable/constant
  2282. * @param sd If NULL, will try to use sd from st->rid (for player's variables)
  2283. */
  2284. void get_val_(struct script_state* st, struct script_data* data, struct map_session_data *sd)
  2285. {
  2286. const char* name;
  2287. char prefix;
  2288. char postfix;
  2289. if( !data_isreference(data) )
  2290. return;// not a variable/constant
  2291. name = reference_getname(data);
  2292. prefix = name[0];
  2293. postfix = name[strlen(name) - 1];
  2294. //##TODO use reference_tovariable(data) when it's confirmed that it works [FlavioJS]
  2295. if( !reference_toconstant(data) && not_server_variable(prefix) ) {
  2296. if (sd == NULL)
  2297. sd = script_rid2sd(st);
  2298. if( sd == NULL ) {// needs player attached
  2299. if( postfix == '$' ) {// string variable
  2300. ShowWarning("script:get_val: cannot access player variable '%s', defaulting to \"\"\n", name);
  2301. data->type = C_CONSTSTR;
  2302. data->u.str = "";
  2303. } else {// integer variable
  2304. ShowWarning("script:get_val: cannot access player variable '%s', defaulting to 0\n", name);
  2305. data->type = C_INT;
  2306. data->u.num = 0;
  2307. }
  2308. return;
  2309. }
  2310. }
  2311. if( postfix == '$' ) {// string variable
  2312. switch( prefix ) {
  2313. case '@':
  2314. data->u.str = pc_readregstr(sd, data->u.num);
  2315. break;
  2316. case '$':
  2317. data->u.str = mapreg_readregstr(data->u.num);
  2318. break;
  2319. case '#':
  2320. if( name[1] == '#' )
  2321. data->u.str = pc_readaccountreg2str(sd, data->u.num);// global
  2322. else
  2323. data->u.str = pc_readaccountregstr(sd, data->u.num);// local
  2324. break;
  2325. case '.':
  2326. {
  2327. struct DBMap* n = data->ref ?
  2328. data->ref->vars : name[1] == '@' ?
  2329. st->stack->scope.vars : // instance/scope variable
  2330. st->script->local.vars; // npc variable
  2331. if( n )
  2332. data->u.str = (char*)i64db_get(n,reference_getuid(data));
  2333. else
  2334. data->u.str = NULL;
  2335. }
  2336. break;
  2337. case '\'':
  2338. {
  2339. int instance_id = script_instancegetid(st);
  2340. if( instance_id )
  2341. data->u.str = (char*)i64db_get(instance_data[instance_id].regs.vars,reference_getuid(data));
  2342. else {
  2343. ShowWarning("script:get_val: cannot access instance variable '%s', defaulting to \"\"\n", name);
  2344. data->u.str = NULL;
  2345. }
  2346. }
  2347. break;
  2348. default:
  2349. data->u.str = pc_readglobalreg_str(sd, data->u.num);
  2350. break;
  2351. }
  2352. if( data->u.str == NULL || data->u.str[0] == '\0' ) {// empty string
  2353. data->type = C_CONSTSTR;
  2354. data->u.str = "";
  2355. } else {// duplicate string
  2356. data->type = C_STR;
  2357. data->u.str = aStrdup(data->u.str);
  2358. }
  2359. } else {// integer variable
  2360. data->type = C_INT;
  2361. if( reference_toconstant(data) ) {
  2362. data->u.num = reference_getconstant(data);
  2363. } else if( reference_toparam(data) ) {
  2364. data->u.num = pc_readparam(sd, reference_getparamtype(data));
  2365. } else
  2366. switch( prefix ) {
  2367. case '@':
  2368. data->u.num = pc_readreg(sd, data->u.num);
  2369. break;
  2370. case '$':
  2371. data->u.num = mapreg_readreg(data->u.num);
  2372. break;
  2373. case '#':
  2374. if( name[1] == '#' )
  2375. data->u.num = pc_readaccountreg2(sd, data->u.num);// global
  2376. else
  2377. data->u.num = pc_readaccountreg(sd, data->u.num);// local
  2378. break;
  2379. case '.':
  2380. {
  2381. struct DBMap* n = data->ref ?
  2382. data->ref->vars : name[1] == '@' ?
  2383. st->stack->scope.vars : // instance/scope variable
  2384. st->script->local.vars; // npc variable
  2385. if( n )
  2386. data->u.num = (int)i64db_iget(n,reference_getuid(data));
  2387. else
  2388. data->u.num = 0;
  2389. }
  2390. break;
  2391. case '\'':
  2392. {
  2393. int instance_id = script_instancegetid(st);
  2394. if( instance_id )
  2395. data->u.num = (int)i64db_iget(instance_data[instance_id].regs.vars,reference_getuid(data));
  2396. else {
  2397. ShowWarning("script:get_val: cannot access instance variable '%s', defaulting to 0\n", name);
  2398. data->u.num = 0;
  2399. }
  2400. }
  2401. break;
  2402. default:
  2403. data->u.num = pc_readglobalreg(sd, data->u.num);
  2404. break;
  2405. }
  2406. }
  2407. data->ref = NULL;
  2408. return;
  2409. }
  2410. void get_val(struct script_state* st, struct script_data* data)
  2411. {
  2412. get_val_(st,data,NULL);
  2413. }
  2414. struct script_data* push_val2(struct script_stack* stack, enum c_op type, int64 val, struct reg_db* ref);
  2415. /// Retrieves the value of a reference identified by uid (variable, constant, param)
  2416. /// The value is left in the top of the stack and needs to be removed manually.
  2417. /// @param st[in]: script state.
  2418. /// @param uid[in]: reference identifier.
  2419. /// @param ref[in]: the container to look up the reference into.
  2420. /// @return: the retrieved value of the reference.
  2421. void* get_val2(struct script_state* st, int64 uid, struct reg_db *ref)
  2422. {
  2423. struct script_data* data;
  2424. push_val2(st->stack, C_NAME, uid, ref);
  2425. data = script_getdatatop(st, -1);
  2426. get_val(st, data);
  2427. //! TODO: Support data->u.num as int64 instead cast it to int? (in get_val, it was casted to int).
  2428. return (data->type == C_INT ? (void*)__64BPRTSIZE((int)data->u.num) : (void*)__64BPRTSIZE(data->u.str));
  2429. }
  2430. /**
  2431. * Because, currently, array members with key 0 are indifferenciable from normal variables, we should ensure its actually in
  2432. * Will be gone as soon as undefined var feature is implemented
  2433. **/
  2434. void script_array_ensure_zero(struct script_state *st, struct map_session_data *sd, int64 uid, struct reg_db *ref)
  2435. {
  2436. const char *name = get_str(script_getvarid(uid));
  2437. // is here st can be null pointer and st->rid is wrong?
  2438. struct reg_db *src = script_array_src(st, sd ? sd : st->rid ? map_id2sd(st->rid) : NULL, name, ref);
  2439. bool insert = false;
  2440. if (sd && !st) {
  2441. // when sd comes, st isn't available
  2442. insert = true;
  2443. } else {
  2444. if( is_string_variable(name) ) {
  2445. char* str = (char*)get_val2(st, uid, ref);
  2446. if( str && *str )
  2447. insert = true;
  2448. script_removetop(st, -1, 0);
  2449. } else {
  2450. int32 num = (int32)__64BPRTSIZE(get_val2(st, uid, ref));
  2451. if( num )
  2452. insert = true;
  2453. script_removetop(st, -1, 0);
  2454. }
  2455. }
  2456. if (src && src->arrays) {
  2457. struct script_array *sa = idb_get(src->arrays, script_getvarid(uid));
  2458. if (sa) {
  2459. unsigned int i;
  2460. ARR_FIND(0, sa->size, i, sa->members[i] == 0);
  2461. if( i != sa->size ) {
  2462. if( !insert )
  2463. script_array_remove_member(src,sa,i);
  2464. return;
  2465. }
  2466. script_array_add_member(sa,0);
  2467. } else if (insert) {
  2468. script_array_update(src,reference_uid(script_getvarid(uid), 0),false);
  2469. }
  2470. }
  2471. }
  2472. /**
  2473. * Returns array size by ID
  2474. **/
  2475. unsigned int script_array_size(struct script_state *st, struct map_session_data *sd, const char *name, struct reg_db *ref)
  2476. {
  2477. struct script_array *sa = NULL;
  2478. struct reg_db *src = script_array_src(st, sd, name, ref);
  2479. if (src && src->arrays)
  2480. sa = idb_get(src->arrays, search_str(name));
  2481. return sa ? sa->size : 0;
  2482. }
  2483. /**
  2484. * Returns array's highest key (for that awful getarraysize implementation that doesn't really gets the array size)
  2485. **/
  2486. unsigned int script_array_highest_key(struct script_state *st, struct map_session_data *sd, const char *name, struct reg_db *ref)
  2487. {
  2488. struct script_array *sa = NULL;
  2489. struct reg_db *src = script_array_src(st, sd, name, ref);
  2490. if (src && src->arrays) {
  2491. int key = add_word(name);
  2492. script_array_ensure_zero(st,sd,reference_uid(key, 0), ref);
  2493. if( ( sa = idb_get(src->arrays, key) ) ) {
  2494. unsigned int i, highest_key = 0;
  2495. for(i = 0; i < sa->size; i++) {
  2496. if( sa->members[i] > highest_key )
  2497. highest_key = sa->members[i];
  2498. }
  2499. return sa->size ? highest_key + 1 : 0;
  2500. }
  2501. }
  2502. return SCRIPT_CMD_SUCCESS;
  2503. }
  2504. int script_free_array_db(DBKey key, DBData *data, va_list ap)
  2505. {
  2506. struct script_array *sa = db_data2ptr(data);
  2507. aFree(sa->members);
  2508. ers_free(array_ers, sa);
  2509. return SCRIPT_CMD_SUCCESS;
  2510. }
  2511. /**
  2512. * Clears script_array and removes it from script->array_db
  2513. **/
  2514. void script_array_delete(struct reg_db *src, struct script_array *sa)
  2515. {
  2516. aFree(sa->members);
  2517. idb_remove(src->arrays, sa->id);
  2518. ers_free(array_ers, sa);
  2519. }
  2520. /**
  2521. * Removes a member from a script_array list
  2522. *
  2523. * @param idx the index of the member in script_array struct list, not of the actual array member
  2524. **/
  2525. void script_array_remove_member(struct reg_db *src, struct script_array *sa, unsigned int idx)
  2526. {
  2527. unsigned int i, cursor;
  2528. // it's the only member left, no need to do anything other than delete the array data
  2529. if( sa->size == 1 ) {
  2530. script_array_delete(src,sa);
  2531. return;
  2532. }
  2533. sa->members[idx] = UINT_MAX;
  2534. for(i = 0, cursor = 0; i < sa->size; i++) {
  2535. if( sa->members[i] == UINT_MAX )
  2536. continue;
  2537. if( i != cursor )
  2538. sa->members[cursor] = sa->members[i];
  2539. cursor++;
  2540. }
  2541. sa->size = cursor;
  2542. }
  2543. /**
  2544. * Appends a new array index to the list in script_array
  2545. *
  2546. * @param idx the index of the array member being inserted
  2547. **/
  2548. void script_array_add_member(struct script_array *sa, unsigned int idx)
  2549. {
  2550. RECREATE(sa->members, unsigned int, ++sa->size);
  2551. sa->members[sa->size - 1] = idx;
  2552. }
  2553. /**
  2554. * Obtains the source of the array database for this type and scenario
  2555. * Initializes such database when not yet initialized.
  2556. **/
  2557. struct reg_db *script_array_src(struct script_state *st, struct map_session_data *sd, const char *name, struct reg_db *ref)
  2558. {
  2559. struct reg_db *src = NULL;
  2560. switch( name[0] ) {
  2561. // from player
  2562. default: // char reg
  2563. case '@': // temp char reg
  2564. case '#': // account reg
  2565. src = &sd->regs;
  2566. break;
  2567. case '$': // map reg
  2568. src = &regs;
  2569. break;
  2570. case '.': // npc/script
  2571. if (ref)
  2572. src = ref;
  2573. else
  2574. src = (name[1] == '@') ? &st->stack->scope : &st->script->local;
  2575. break;
  2576. case '\'': // instance
  2577. {
  2578. int instance_id = script_instancegetid(st);
  2579. if( instance_id ) {
  2580. src = &instance_data[instance_id].regs;
  2581. }
  2582. break;
  2583. }
  2584. }
  2585. if( src ) {
  2586. if( !src->arrays )
  2587. src->arrays = idb_alloc(DB_OPT_BASE);
  2588. return src;
  2589. }
  2590. return NULL;
  2591. }
  2592. /**
  2593. * Processes a array member modification, and update data accordingly
  2594. *
  2595. * @param src[in,out] Variable source database. If the array database doesn't exist, it is created.
  2596. * @param num[in] Variable ID
  2597. * @param empty[in] Whether the modified member is empty (needs to be removed)
  2598. **/
  2599. void script_array_update(struct reg_db *src, int64 num, bool empty)
  2600. {
  2601. struct script_array *sa = NULL;
  2602. int id = script_getvarid(num);
  2603. unsigned int index = script_getvaridx(num);
  2604. if (!src->arrays) {
  2605. src->arrays = idb_alloc(DB_OPT_BASE);
  2606. } else {
  2607. sa = idb_get(src->arrays, id);
  2608. }
  2609. if( sa ) {
  2610. unsigned int i;
  2611. // search
  2612. for(i = 0; i < sa->size; i++) {
  2613. if( sa->members[i] == index )
  2614. break;
  2615. }
  2616. // if existent
  2617. if( i != sa->size ) {
  2618. // if empty, we gotta remove it
  2619. if( empty ) {
  2620. script_array_remove_member(src, sa, i);
  2621. }
  2622. } else if( !empty ) { /* new entry */
  2623. script_array_add_member(sa,index);
  2624. // we do nothing if its empty, no point in modifying array data for a new empty member
  2625. }
  2626. } else if ( !empty ) { // we only move to create if not empty
  2627. sa = ers_alloc(array_ers, struct script_array);
  2628. sa->id = id;
  2629. sa->members = NULL;
  2630. sa->size = 0;
  2631. script_array_add_member(sa,index);
  2632. idb_put(src->arrays, id, sa);
  2633. }
  2634. }
  2635. /**
  2636. * Stores the value of a script variable
  2637. *
  2638. * @param st: current script state.
  2639. * @param sd: current character data.
  2640. * @param num: variable identifier.
  2641. * @param name: variable name.
  2642. * @param value: new value.
  2643. * @param ref: variable container, in case of a npc/scope variable reference outside the current scope.
  2644. * @return: 0 failure, 1 success.
  2645. *
  2646. * TODO: return values are screwed up, have been for some time (reaad: years), e.g. some functions return 1 failure and success.
  2647. *------------------------------------------*/
  2648. int set_reg(struct script_state* st, TBL_PC* sd, int64 num, const char* name, const void* value, struct reg_db *ref)
  2649. {
  2650. char prefix = name[0];
  2651. if( is_string_variable(name) ) {// string variable
  2652. const char *str = (const char*)value;
  2653. switch (prefix) {
  2654. case '@':
  2655. pc_setregstr(sd, num, str);
  2656. return 1;
  2657. case '$':
  2658. return mapreg_setregstr(num, str);
  2659. case '#':
  2660. return (name[1] == '#') ?
  2661. pc_setaccountreg2str(sd, num, str) :
  2662. pc_setaccountregstr(sd, num, str);
  2663. case '.':
  2664. {
  2665. struct reg_db *n = (ref) ? ref : (name[1] == '@') ? &st->stack->scope : &st->script->local;
  2666. if( n ) {
  2667. if (str[0]) {
  2668. i64db_put(n->vars, num, aStrdup(str));
  2669. if( script_getvaridx(num) )
  2670. script_array_update(n, num, false);
  2671. } else {
  2672. i64db_remove(n->vars, num);
  2673. if( script_getvaridx(num) )
  2674. script_array_update(n, num, true);
  2675. }
  2676. }
  2677. }
  2678. return 1;
  2679. case '\'':
  2680. {
  2681. int instance_id = script_instancegetid(st);
  2682. if( instance_id ) {
  2683. if( str[0] ) {
  2684. i64db_put(instance_data[instance_id].regs.vars, num, aStrdup(str));
  2685. if( script_getvaridx(num) )
  2686. script_array_update(&instance_data[instance_id].regs, num, false);
  2687. } else {
  2688. i64db_remove(instance_data[instance_id].regs.vars, num);
  2689. if (script_getvaridx(num))
  2690. script_array_update(&instance_data[instance_id].regs, num, true);
  2691. }
  2692. } else {
  2693. ShowError("script_set_reg: cannot write instance variable '%s', NPC not in a instance!\n", name);
  2694. script_reportsrc(st);
  2695. }
  2696. return 1;
  2697. }
  2698. default:
  2699. return pc_setglobalreg_str(sd, num, str);
  2700. }
  2701. } else {// integer variable
  2702. int val = (int)__64BPRTSIZE(value);
  2703. if(str_data[script_getvarid(num)].type == C_PARAM) {
  2704. if( pc_setparam(sd, str_data[script_getvarid(num)].val, val) == 0 ) {
  2705. if( st != NULL ) {
  2706. ShowError("script_set_reg: failed to set param '%s' to %d.\n", name, val);
  2707. script_reportsrc(st);
  2708. st->state = END;
  2709. }
  2710. return 0;
  2711. }
  2712. return 1;
  2713. }
  2714. switch (prefix) {
  2715. case '@':
  2716. pc_setreg(sd, num, val);
  2717. return 1;
  2718. case '$':
  2719. return mapreg_setreg(num, val);
  2720. case '#':
  2721. return (name[1] == '#') ?
  2722. pc_setaccountreg2(sd, num, val) :
  2723. pc_setaccountreg(sd, num, val);
  2724. case '.':
  2725. {
  2726. struct reg_db *n = (ref) ? ref : (name[1] == '@') ? &st->stack->scope : &st->script->local;
  2727. if( n ) {
  2728. if( val != 0 ) {
  2729. i64db_iput(n->vars, num, val);
  2730. if( script_getvaridx(num) )
  2731. script_array_update(n, num, false);
  2732. } else {
  2733. i64db_remove(n->vars, num);
  2734. if( script_getvaridx(num) )
  2735. script_array_update(n, num, true);
  2736. }
  2737. }
  2738. }
  2739. return 1;
  2740. case '\'':
  2741. {
  2742. int instance_id = script_instancegetid(st);
  2743. if( instance_id ) {
  2744. if( val != 0 ) {
  2745. i64db_iput(instance_data[instance_id].regs.vars, num, val);
  2746. if( script_getvaridx(num) )
  2747. script_array_update(&instance_data[instance_id].regs, num, false);
  2748. } else {
  2749. i64db_remove(instance_data[instance_id].regs.vars, num);
  2750. if (script_getvaridx(num))
  2751. script_array_update(&instance_data[instance_id].regs, num, true);
  2752. }
  2753. } else {
  2754. ShowError("script_set_reg: cannot write instance variable '%s', NPC not in a instance!\n", name);
  2755. script_reportsrc(st);
  2756. }
  2757. return 1;
  2758. }
  2759. default:
  2760. return pc_setglobalreg(sd, num, val);
  2761. }
  2762. }
  2763. }
  2764. int set_var(TBL_PC* sd, char* name, void* val)
  2765. {
  2766. return set_reg(NULL, sd, reference_uid(add_str(name),0), name, val, NULL);
  2767. }
  2768. void setd_sub(struct script_state *st, TBL_PC *sd, const char *varname, int elem, void *value, struct reg_db *ref)
  2769. {
  2770. set_reg(st, sd, reference_uid(add_str(varname),elem), varname, value, ref);
  2771. }
  2772. /**
  2773. * Converts the data to a string
  2774. * @param st
  2775. * @param data
  2776. * @param sd
  2777. */
  2778. const char* conv_str_(struct script_state* st, struct script_data* data, struct map_session_data *sd)
  2779. {
  2780. char* p;
  2781. get_val_(st, data, sd);
  2782. if( data_isstring(data) )
  2783. {// nothing to convert
  2784. }
  2785. else if( data_isint(data) )
  2786. {// int -> string
  2787. CREATE(p, char, ITEM_NAME_LENGTH);
  2788. snprintf(p, ITEM_NAME_LENGTH, "%"PRId64"", data->u.num);
  2789. p[ITEM_NAME_LENGTH-1] = '\0';
  2790. data->type = C_STR;
  2791. data->u.str = p;
  2792. }
  2793. else if( data_isreference(data) )
  2794. {// reference -> string
  2795. //##TODO when does this happen (check get_val) [FlavioJS]
  2796. data->type = C_CONSTSTR;
  2797. data->u.str = reference_getname(data);
  2798. }
  2799. else
  2800. {// unsupported data type
  2801. ShowError("script:conv_str: cannot convert to string, defaulting to \"\"\n");
  2802. script_reportdata(data);
  2803. script_reportsrc(st);
  2804. data->type = C_CONSTSTR;
  2805. data->u.str = "";
  2806. }
  2807. return data->u.str;
  2808. }
  2809. const char* conv_str(struct script_state* st, struct script_data* data)
  2810. {
  2811. return conv_str_(st, data, NULL);
  2812. }
  2813. /**
  2814. * Converts the data to an int
  2815. * @param st
  2816. * @param data
  2817. * @param sd
  2818. */
  2819. int conv_num_(struct script_state* st, struct script_data* data, struct map_session_data *sd)
  2820. {
  2821. get_val_(st, data, sd);
  2822. if( data_isint(data) )
  2823. {// nothing to convert
  2824. }
  2825. else if( data_isstring(data) )
  2826. {// string -> int
  2827. // the result does not overflow or underflow, it is capped instead
  2828. // ex: 999999999999 is capped to INT_MAX (2147483647)
  2829. char* p = data->u.str;
  2830. long num;
  2831. errno = 0;
  2832. num = strtol(data->u.str, NULL, 10);// change radix to 0 to support octal numbers "o377" and hex numbers "0xFF"
  2833. if( errno == ERANGE
  2834. #if LONG_MAX > INT_MAX
  2835. || num < INT_MIN || num > INT_MAX
  2836. #endif
  2837. )
  2838. {
  2839. if( num <= INT_MIN )
  2840. {
  2841. num = INT_MIN;
  2842. ShowError("script:conv_num: underflow detected, capping to %ld\n", num);
  2843. }
  2844. else//if( num >= INT_MAX )
  2845. {
  2846. num = INT_MAX;
  2847. ShowError("script:conv_num: overflow detected, capping to %ld\n", num);
  2848. }
  2849. script_reportdata(data);
  2850. script_reportsrc(st);
  2851. }
  2852. if( data->type == C_STR )
  2853. aFree(p);
  2854. data->type = C_INT;
  2855. data->u.num = (int)num;
  2856. }
  2857. #if 0
  2858. // FIXME this function is being used to retrieve the position of labels and
  2859. // probably other stuff [FlavioJS]
  2860. else
  2861. {// unsupported data type
  2862. ShowError("script:conv_num: cannot convert to number, defaulting to 0\n");
  2863. script_reportdata(data);
  2864. script_reportsrc(st);
  2865. data->type = C_INT;
  2866. data->u.num = 0;
  2867. }
  2868. #endif
  2869. return (int)data->u.num;
  2870. }
  2871. int conv_num(struct script_state* st, struct script_data* data)
  2872. {
  2873. return conv_num_(st, data, NULL);
  2874. }
  2875. //
  2876. // Stack operations
  2877. //
  2878. /// Increases the size of the stack
  2879. void stack_expand(struct script_stack* stack)
  2880. {
  2881. stack->sp_max += 64;
  2882. stack->stack_data = (struct script_data*)aRealloc(stack->stack_data,
  2883. stack->sp_max * sizeof(stack->stack_data[0]) );
  2884. memset(stack->stack_data + (stack->sp_max - 64), 0,
  2885. 64 * sizeof(stack->stack_data[0]) );
  2886. }
  2887. /// Pushes a value into the stack
  2888. #define push_val(stack,type,val) push_val2(stack, type, val, NULL)
  2889. /// Pushes a value into the stack (with reference)
  2890. struct script_data* push_val2(struct script_stack* stack, enum c_op type, int64 val, struct reg_db *ref)
  2891. {
  2892. if( stack->sp >= stack->sp_max )
  2893. stack_expand(stack);
  2894. stack->stack_data[stack->sp].type = type;
  2895. stack->stack_data[stack->sp].u.num = val;
  2896. stack->stack_data[stack->sp].ref = ref;
  2897. stack->sp++;
  2898. return &stack->stack_data[stack->sp-1];
  2899. }
  2900. /// Pushes a string into the stack
  2901. struct script_data* push_str(struct script_stack* stack, enum c_op type, char* str)
  2902. {
  2903. if( stack->sp >= stack->sp_max )
  2904. stack_expand(stack);
  2905. stack->stack_data[stack->sp].type = type;
  2906. stack->stack_data[stack->sp].u.str = str;
  2907. stack->stack_data[stack->sp].ref = NULL;
  2908. stack->sp++;
  2909. return &stack->stack_data[stack->sp-1];
  2910. }
  2911. /// Pushes a retinfo into the stack
  2912. struct script_data* push_retinfo(struct script_stack* stack, struct script_retinfo* ri, struct reg_db *ref)
  2913. {
  2914. if( stack->sp >= stack->sp_max )
  2915. stack_expand(stack);
  2916. stack->stack_data[stack->sp].type = C_RETINFO;
  2917. stack->stack_data[stack->sp].u.ri = ri;
  2918. stack->stack_data[stack->sp].ref = ref;
  2919. stack->sp++;
  2920. return &stack->stack_data[stack->sp-1];
  2921. }
  2922. /// Pushes a copy of the target position into the stack
  2923. struct script_data* push_copy(struct script_stack* stack, int pos)
  2924. {
  2925. switch( stack->stack_data[pos].type ) {
  2926. case C_CONSTSTR:
  2927. return push_str(stack, C_CONSTSTR, stack->stack_data[pos].u.str);
  2928. break;
  2929. case C_STR:
  2930. return push_str(stack, C_STR, aStrdup(stack->stack_data[pos].u.str));
  2931. break;
  2932. case C_RETINFO:
  2933. ShowFatalError("script:push_copy: can't create copies of C_RETINFO. Exiting...\n");
  2934. exit(1);
  2935. break;
  2936. default:
  2937. return push_val2(
  2938. stack,stack->stack_data[pos].type,
  2939. stack->stack_data[pos].u.num,
  2940. stack->stack_data[pos].ref
  2941. );
  2942. break;
  2943. }
  2944. }
  2945. /// Removes the values in indexes [start,end] from the stack.
  2946. /// Adjusts all stack pointers.
  2947. void pop_stack(struct script_state* st, int start, int end)
  2948. {
  2949. struct script_stack* stack = st->stack;
  2950. struct script_data* data;
  2951. int i;
  2952. if( start < 0 )
  2953. start = 0;
  2954. if( end > stack->sp )
  2955. end = stack->sp;
  2956. if( start >= end )
  2957. return;// nothing to pop
  2958. // free stack elements
  2959. for( i = start; i < end; i++ )
  2960. {
  2961. data = &stack->stack_data[i];
  2962. if( data->type == C_STR )
  2963. aFree(data->u.str);
  2964. if( data->type == C_RETINFO ) {
  2965. struct script_retinfo* ri = data->u.ri;
  2966. if (ri->scope.vars) {
  2967. script_free_vars(ri->scope.vars);
  2968. ri->scope.vars = NULL;
  2969. }
  2970. if (ri->scope.arrays) {
  2971. ri->scope.arrays->destroy(ri->scope.arrays, script_free_array_db);
  2972. ri->scope.arrays = NULL;
  2973. }
  2974. if( data->ref )
  2975. aFree(data->ref);
  2976. aFree(ri);
  2977. }
  2978. data->type = C_NOP;
  2979. }
  2980. // move the rest of the elements
  2981. if( stack->sp > end )
  2982. {
  2983. memmove(&stack->stack_data[start], &stack->stack_data[end], sizeof(stack->stack_data[0])*(stack->sp - end));
  2984. for( i = start + stack->sp - end; i < stack->sp; ++i )
  2985. stack->stack_data[i].type = C_NOP;
  2986. }
  2987. // adjust stack pointers
  2988. if( st->start > end ) st->start -= end - start;
  2989. else if( st->start > start ) st->start = start;
  2990. if( st->end > end ) st->end -= end - start;
  2991. else if( st->end > start ) st->end = start;
  2992. if( stack->defsp > end ) stack->defsp -= end - start;
  2993. else if( stack->defsp > start ) stack->defsp = start;
  2994. stack->sp -= end - start;
  2995. }
  2996. ///
  2997. ///
  2998. ///
  2999. /*==========================================
  3000. * Release script dependent variable, dependent variable of function
  3001. *------------------------------------------*/
  3002. void script_free_vars(struct DBMap* storage)
  3003. {
  3004. if( storage ) {
  3005. // destroy the storage construct containing the variables
  3006. db_destroy(storage);
  3007. }
  3008. }
  3009. void script_free_code(struct script_code* code)
  3010. {
  3011. nullpo_retv(code);
  3012. if (code->instances)
  3013. script_stop_instances(code);
  3014. script_free_vars(code->local.vars);
  3015. if (code->local.arrays)
  3016. code->local.arrays->destroy(code->local.arrays, script_free_array_db);
  3017. aFree(code->script_buf);
  3018. aFree(code);
  3019. }
  3020. /// Creates a new script state.
  3021. ///
  3022. /// @param script Script code
  3023. /// @param pos Position in the code
  3024. /// @param rid Who is running the script (attached player)
  3025. /// @param oid Where the code is being run (npc 'object')
  3026. /// @return Script state
  3027. struct script_state* script_alloc_state(struct script_code* rootscript, int pos, int rid, int oid)
  3028. {
  3029. struct script_state* st;
  3030. st = ers_alloc(st_ers, struct script_state);
  3031. st->stack = ers_alloc(stack_ers, struct script_stack);
  3032. st->stack->sp = 0;
  3033. st->stack->sp_max = 64;
  3034. CREATE(st->stack->stack_data, struct script_data, st->stack->sp_max);
  3035. st->stack->defsp = st->stack->sp;
  3036. st->stack->scope.vars = i64db_alloc(DB_OPT_RELEASE_DATA);
  3037. st->stack->scope.arrays = NULL;
  3038. st->state = RUN;
  3039. st->script = rootscript;
  3040. st->pos = pos;
  3041. st->rid = rid;
  3042. st->oid = oid;
  3043. st->sleep.timer = INVALID_TIMER;
  3044. st->npc_item_flag = battle_config.item_enabled_npc;
  3045. if( st->script->instances != USHRT_MAX )
  3046. st->script->instances++;
  3047. else {
  3048. struct npc_data *nd = map_id2nd(oid);
  3049. ShowError("Over 65k instances of '%s' script are being run!\n",nd ? nd->name : "unknown");
  3050. }
  3051. if (!st->script->local.vars)
  3052. st->script->local.vars = i64db_alloc(DB_OPT_RELEASE_DATA);
  3053. st->id = next_id++;
  3054. active_scripts++;
  3055. idb_put(st_db, st->id, st);
  3056. return st;
  3057. }
  3058. /// Frees a script state.
  3059. ///
  3060. /// @param st Script state
  3061. void script_free_state(struct script_state* st)
  3062. {
  3063. if (idb_exists(st_db, st->id)) {
  3064. struct map_session_data *sd = st->rid ? map_id2sd(st->rid) : NULL;
  3065. if (st->bk_st) // backup was not restored
  3066. 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);
  3067. if (sd && sd->st == st) { // Current script is aborted.
  3068. if(sd->state.using_fake_npc) {
  3069. clif_clearunit_single(sd->npc_id, CLR_OUTSIGHT, sd->fd);
  3070. sd->state.using_fake_npc = 0;
  3071. }
  3072. sd->st = NULL;
  3073. sd->npc_id = 0;
  3074. }
  3075. if (st->sleep.timer != INVALID_TIMER)
  3076. delete_timer(st->sleep.timer, run_script_timer);
  3077. if (st->stack) {
  3078. script_free_vars(st->stack->scope.vars);
  3079. if (st->stack->scope.arrays)
  3080. st->stack->scope.arrays->destroy(st->stack->scope.arrays, script_free_array_db);
  3081. pop_stack(st, 0, st->stack->sp);
  3082. aFree(st->stack->stack_data);
  3083. ers_free(stack_ers, st->stack);
  3084. st->stack = NULL;
  3085. }
  3086. if (st->script && st->script->instances != USHRT_MAX && --st->script->instances == 0) {
  3087. if (st->script->local.vars && !db_size(st->script->local.vars)) {
  3088. script_free_vars(st->script->local.vars);
  3089. st->script->local.vars = NULL;
  3090. }
  3091. if (st->script->local.arrays && !db_size(st->script->local.arrays)) {
  3092. st->script->local.arrays->destroy(st->script->local.arrays, script_free_array_db);
  3093. st->script->local.arrays = NULL;
  3094. }
  3095. }
  3096. st->pos = -1;
  3097. idb_remove(st_db, st->id);
  3098. ers_free(st_ers, st);
  3099. if (--active_scripts == 0)
  3100. next_id = 0;
  3101. }
  3102. }
  3103. //
  3104. // Main execution unit
  3105. //
  3106. /*==========================================
  3107. * Read command
  3108. *------------------------------------------*/
  3109. c_op get_com(unsigned char *script,int *pos)
  3110. {
  3111. int i = 0, j = 0;
  3112. if(script[*pos]>=0x80){
  3113. return C_INT;
  3114. }
  3115. while(script[*pos]>=0x40){
  3116. i=script[(*pos)++]<<j;
  3117. j+=6;
  3118. }
  3119. return (c_op)(i+(script[(*pos)++]<<j));
  3120. }
  3121. /*==========================================
  3122. * Income figures
  3123. *------------------------------------------*/
  3124. int get_num(unsigned char *script,int *pos)
  3125. {
  3126. int i,j;
  3127. i=0; j=0;
  3128. while(script[*pos]>=0xc0){
  3129. i+=(script[(*pos)++]&0x7f)<<j;
  3130. j+=6;
  3131. }
  3132. return i+((script[(*pos)++]&0x7f)<<j);
  3133. }
  3134. /// Ternary operators
  3135. /// test ? if_true : if_false
  3136. void op_3(struct script_state* st, int op)
  3137. {
  3138. struct script_data* data;
  3139. int flag = 0;
  3140. data = script_getdatatop(st, -3);
  3141. get_val(st, data);
  3142. if( data_isstring(data) )
  3143. flag = data->u.str[0];// "" -> false
  3144. else if( data_isint(data) )
  3145. flag = data->u.num == 0 ? 0 : 1;// 0 -> false
  3146. else
  3147. {
  3148. ShowError("script:op_3: invalid data for the ternary operator test\n");
  3149. script_reportdata(data);
  3150. script_reportsrc(st);
  3151. script_removetop(st, -3, 0);
  3152. script_pushnil(st);
  3153. return;
  3154. }
  3155. if( flag )
  3156. script_pushcopytop(st, -2);
  3157. else
  3158. script_pushcopytop(st, -1);
  3159. script_removetop(st, -4, -1);
  3160. }
  3161. /// Binary string operators
  3162. /// s1 EQ s2 -> i
  3163. /// s1 NE s2 -> i
  3164. /// s1 GT s2 -> i
  3165. /// s1 GE s2 -> i
  3166. /// s1 LT s2 -> i
  3167. /// s1 LE s2 -> i
  3168. /// s1 ADD s2 -> s
  3169. void op_2str(struct script_state* st, int op, const char* s1, const char* s2)
  3170. {
  3171. int a = 0;
  3172. switch(op){
  3173. case C_EQ: a = (strcmp(s1,s2) == 0); break;
  3174. case C_NE: a = (strcmp(s1,s2) != 0); break;
  3175. case C_GT: a = (strcmp(s1,s2) > 0); break;
  3176. case C_GE: a = (strcmp(s1,s2) >= 0); break;
  3177. case C_LT: a = (strcmp(s1,s2) < 0); break;
  3178. case C_LE: a = (strcmp(s1,s2) <= 0); break;
  3179. case C_ADD:
  3180. {
  3181. char* buf = (char *)aMalloc((strlen(s1)+strlen(s2)+1)*sizeof(char));
  3182. strcpy(buf, s1);
  3183. strcat(buf, s2);
  3184. script_pushstr(st, buf);
  3185. return;
  3186. }
  3187. default:
  3188. ShowError("script:op2_str: unexpected string operator %s\n", script_op2name(op));
  3189. script_reportsrc(st);
  3190. script_pushnil(st);
  3191. st->state = END;
  3192. return;
  3193. }
  3194. script_pushint(st,a);
  3195. }
  3196. /// Binary number operators
  3197. /// i OP i -> i
  3198. void op_2num(struct script_state* st, int op, int i1, int i2)
  3199. {
  3200. int ret;
  3201. double ret_double;
  3202. switch( op ) {
  3203. case C_AND: ret = i1 & i2; break;
  3204. case C_OR: ret = i1 | i2; break;
  3205. case C_XOR: ret = i1 ^ i2; break;
  3206. case C_LAND: ret = (i1 && i2); break;
  3207. case C_LOR: ret = (i1 || i2); break;
  3208. case C_EQ: ret = (i1 == i2); break;
  3209. case C_NE: ret = (i1 != i2); break;
  3210. case C_GT: ret = (i1 > i2); break;
  3211. case C_GE: ret = (i1 >= i2); break;
  3212. case C_LT: ret = (i1 < i2); break;
  3213. case C_LE: ret = (i1 <= i2); break;
  3214. case C_R_SHIFT: ret = i1>>i2; break;
  3215. case C_L_SHIFT: ret = i1<<i2; break;
  3216. case C_DIV:
  3217. case C_MOD:
  3218. if( i2 == 0 )
  3219. {
  3220. ShowError("script:op_2num: division by zero detected op=%s i1=%d i2=%d\n", script_op2name(op), i1, i2);
  3221. script_reportsrc(st);
  3222. script_pushnil(st);
  3223. st->state = END;
  3224. return;
  3225. }
  3226. else if( op == C_DIV )
  3227. ret = i1 / i2;
  3228. else//if( op == C_MOD )
  3229. ret = i1 % i2;
  3230. break;
  3231. default:
  3232. switch( op ) {// operators that can overflow/underflow
  3233. case C_ADD: ret = i1 + i2; ret_double = (double)i1 + (double)i2; break;
  3234. case C_SUB: ret = i1 - i2; ret_double = (double)i1 - (double)i2; break;
  3235. case C_MUL: ret = i1 * i2; ret_double = (double)i1 * (double)i2; break;
  3236. default:
  3237. ShowError("script:op_2num: unexpected number operator %s i1=%d i2=%d\n", script_op2name(op), i1, i2);
  3238. script_reportsrc(st);
  3239. script_pushnil(st);
  3240. return;
  3241. }
  3242. if( ret_double < (double)INT_MIN ) {
  3243. ShowWarning("script:op_2num: underflow detected op=%s i1=%d i2=%d\n", script_op2name(op), i1, i2);
  3244. script_reportsrc(st);
  3245. ret = INT_MIN;
  3246. }
  3247. else if( ret_double > (double)INT_MAX ) {
  3248. ShowWarning("script:op_2num: overflow detected op=%s i1=%d i2=%d\n", script_op2name(op), i1, i2);
  3249. script_reportsrc(st);
  3250. ret = INT_MAX;
  3251. }
  3252. }
  3253. script_pushint(st, ret);
  3254. }
  3255. /// Binary operators
  3256. void op_2(struct script_state *st, int op)
  3257. {
  3258. struct script_data* left, leftref;
  3259. struct script_data* right;
  3260. leftref.type = C_NOP;
  3261. left = script_getdatatop(st, -2);
  3262. right = script_getdatatop(st, -1);
  3263. if (st->op2ref) {
  3264. if (data_isreference(left)) {
  3265. leftref = *left;
  3266. }
  3267. st->op2ref = 0;
  3268. }
  3269. get_val(st, left);
  3270. get_val(st, right);
  3271. // automatic conversions
  3272. switch( op )
  3273. {
  3274. case C_ADD:
  3275. if( data_isint(left) && data_isstring(right) )
  3276. {// convert int-string to string-string
  3277. conv_str(st, left);
  3278. }
  3279. else if( data_isstring(left) && data_isint(right) )
  3280. {// convert string-int to string-string
  3281. conv_str(st, right);
  3282. }
  3283. break;
  3284. }
  3285. if( data_isstring(left) && data_isstring(right) )
  3286. {// ss => op_2str
  3287. op_2str(st, op, left->u.str, right->u.str);
  3288. script_removetop(st, leftref.type == C_NOP ? -3 : -2, -1);// pop the two values before the top one
  3289. if (leftref.type != C_NOP)
  3290. {
  3291. if (left->type == C_STR) // don't free C_CONSTSTR
  3292. aFree(left->u.str);
  3293. *left = leftref;
  3294. }
  3295. }
  3296. else if( data_isint(left) && data_isint(right) )
  3297. {// ii => op_2num
  3298. int i1 = (int)left->u.num;
  3299. int i2 = (int)right->u.num;
  3300. script_removetop(st, leftref.type == C_NOP ? -2 : -1, 0);
  3301. op_2num(st, op, i1, i2);
  3302. if (leftref.type != C_NOP)
  3303. *left = leftref;
  3304. }
  3305. else
  3306. {// invalid argument
  3307. ShowError("script:op_2: invalid data for operator %s\n", script_op2name(op));
  3308. script_reportdata(left);
  3309. script_reportdata(right);
  3310. script_reportsrc(st);
  3311. script_removetop(st, -2, 0);
  3312. script_pushnil(st);
  3313. st->state = END;
  3314. }
  3315. }
  3316. /// Unary operators
  3317. /// NEG i -> i
  3318. /// NOT i -> i
  3319. /// LNOT i -> i
  3320. void op_1(struct script_state* st, int op)
  3321. {
  3322. struct script_data* data;
  3323. int i1;
  3324. data = script_getdatatop(st, -1);
  3325. get_val(st, data);
  3326. if( !data_isint(data) )
  3327. {// not a number
  3328. ShowError("script:op_1: argument is not a number (op=%s)\n", script_op2name(op));
  3329. script_reportdata(data);
  3330. script_reportsrc(st);
  3331. script_pushnil(st);
  3332. st->state = END;
  3333. return;
  3334. }
  3335. i1 = (int)data->u.num;
  3336. script_removetop(st, -1, 0);
  3337. switch( op )
  3338. {
  3339. case C_NEG: i1 = -i1; break;
  3340. case C_NOT: i1 = ~i1; break;
  3341. case C_LNOT: i1 = !i1; break;
  3342. default:
  3343. ShowError("script:op_1: unexpected operator %s i1=%d\n", script_op2name(op), i1);
  3344. script_reportsrc(st);
  3345. script_pushnil(st);
  3346. st->state = END;
  3347. return;
  3348. }
  3349. script_pushint(st, i1);
  3350. }
  3351. /// Checks the type of all arguments passed to a built-in function.
  3352. ///
  3353. /// @param st Script state whose stack arguments should be inspected.
  3354. /// @param func Built-in function for which the arguments are intended.
  3355. static void script_check_buildin_argtype(struct script_state* st, int func)
  3356. {
  3357. int idx, invalid = 0;
  3358. for( idx = 2; script_hasdata(st, idx); idx++ )
  3359. {
  3360. struct script_data* data = script_getdata(st, idx);
  3361. script_function* sf = &buildin_func[str_data[func].val];
  3362. char type = sf->arg[idx-2];
  3363. if( type == '?' || type == '*' )
  3364. {// optional argument or unknown number of optional parameters ( no types are after this )
  3365. break;
  3366. }
  3367. else if( type == 0 )
  3368. {// more arguments than necessary ( should not happen, as it is checked before )
  3369. ShowWarning("Found more arguments than necessary. unexpected arg type %s\n",script_op2name(data->type));
  3370. invalid++;
  3371. break;
  3372. }
  3373. else
  3374. {
  3375. const char* name = NULL;
  3376. if( data_isreference(data) )
  3377. {// get name for variables to determine the type they refer to
  3378. name = reference_getname(data);
  3379. }
  3380. switch( type )
  3381. {
  3382. case 'v':
  3383. if( !data_isstring(data) && !data_isint(data) && !data_isreference(data) )
  3384. {// variant
  3385. ShowWarning("Unexpected type for argument %d. Expected string, number or variable.\n", idx-1);
  3386. script_reportdata(data);
  3387. invalid++;
  3388. }
  3389. break;
  3390. case 's':
  3391. if( !data_isstring(data) && !( data_isreference(data) && is_string_variable(name) ) )
  3392. {// string
  3393. ShowWarning("Unexpected type for argument %d. Expected string.\n", idx-1);
  3394. script_reportdata(data);
  3395. invalid++;
  3396. }
  3397. break;
  3398. case 'i':
  3399. if( !data_isint(data) && !( data_isreference(data) && ( reference_toparam(data) || reference_toconstant(data) || !is_string_variable(name) ) ) )
  3400. {// int ( params and constants are always int )
  3401. ShowWarning("Unexpected type for argument %d. Expected number.\n", idx-1);
  3402. script_reportdata(data);
  3403. invalid++;
  3404. }
  3405. break;
  3406. case 'r':
  3407. if( !data_isreference(data) )
  3408. {// variables
  3409. ShowWarning("Unexpected type for argument %d. Expected variable, got %s.\n", idx-1,script_op2name(data->type));
  3410. script_reportdata(data);
  3411. invalid++;
  3412. }
  3413. break;
  3414. case 'l':
  3415. if( !data_islabel(data) && !data_isfunclabel(data) )
  3416. {// label
  3417. ShowWarning("Unexpected type for argument %d. Expected label, got %s\n", idx-1,script_op2name(data->type));
  3418. script_reportdata(data);
  3419. invalid++;
  3420. }
  3421. break;
  3422. }
  3423. }
  3424. }
  3425. if(invalid)
  3426. {
  3427. ShowDebug("Function: %s\n", get_str(func));
  3428. script_reportsrc(st);
  3429. }
  3430. }
  3431. /// Executes a buildin command.
  3432. /// Stack: C_NAME(<command>) C_ARG <arg0> <arg1> ... <argN>
  3433. int run_func(struct script_state *st)
  3434. {
  3435. struct script_data* data;
  3436. int i,start_sp,end_sp,func;
  3437. end_sp = st->stack->sp;// position after the last argument
  3438. for( i = end_sp-1; i > 0 ; --i )
  3439. if( st->stack->stack_data[i].type == C_ARG )
  3440. break;
  3441. if( i == 0 ) {
  3442. ShowError("script:run_func: C_ARG not found. please report this!!!\n");
  3443. st->state = END;
  3444. script_reportsrc(st);
  3445. return 1;
  3446. }
  3447. start_sp = i-1;// C_NAME of the command
  3448. st->start = start_sp;
  3449. st->end = end_sp;
  3450. data = &st->stack->stack_data[st->start];
  3451. if( data->type == C_NAME && str_data[data->u.num].type == C_FUNC ) {
  3452. func = (int)data->u.num;
  3453. st->funcname = reference_getname(data);
  3454. } else {
  3455. ShowError("script:run_func: not a buildin command.\n");
  3456. script_reportdata(data);
  3457. script_reportsrc(st);
  3458. st->state = END;
  3459. return 1;
  3460. }
  3461. if( script_config.warn_func_mismatch_argtypes ) {
  3462. script_check_buildin_argtype(st, func);
  3463. }
  3464. if(str_data[func].func) {
  3465. if (str_data[func].func(st) == SCRIPT_CMD_FAILURE) //Report error
  3466. script_reportsrc(st);
  3467. } else {
  3468. 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));
  3469. script_reportsrc(st);
  3470. st->state = END;
  3471. }
  3472. // Stack's datum are used when re-running functions [Eoe]
  3473. if( st->state == RERUNLINE )
  3474. return 0;
  3475. pop_stack(st, st->start, st->end);
  3476. if( st->state == RETFUNC ) {// return from a user-defined function
  3477. struct script_retinfo* ri;
  3478. int olddefsp = st->stack->defsp;
  3479. int nargs;
  3480. pop_stack(st, st->stack->defsp, st->start);// pop distractions from the stack
  3481. if( st->stack->defsp < 1 || st->stack->stack_data[st->stack->defsp-1].type != C_RETINFO )
  3482. {
  3483. ShowWarning("script:run_func: return without callfunc or callsub!\n");
  3484. script_reportsrc(st);
  3485. st->state = END;
  3486. return 1;
  3487. }
  3488. script_free_vars(st->stack->scope.vars);
  3489. st->stack->scope.arrays->destroy(st->stack->scope.arrays, script_free_array_db);
  3490. ri = st->stack->stack_data[st->stack->defsp-1].u.ri;
  3491. nargs = ri->nargs;
  3492. st->pos = ri->pos;
  3493. st->script = ri->script;
  3494. st->stack->scope.vars = ri->scope.vars;
  3495. st->stack->scope.arrays = ri->scope.arrays;
  3496. st->stack->defsp = ri->defsp;
  3497. memset(ri, 0, sizeof(struct script_retinfo));
  3498. pop_stack(st, olddefsp-nargs-1, olddefsp);// pop arguments and retinfo
  3499. st->state = GOTO;
  3500. }
  3501. return 0;
  3502. }
  3503. /*==========================================
  3504. * script execution
  3505. *------------------------------------------*/
  3506. void run_script(struct script_code *rootscript, int pos, int rid, int oid)
  3507. {
  3508. struct script_state *st;
  3509. if( rootscript == NULL || pos < 0 )
  3510. return;
  3511. // TODO In jAthena, this function can take over the pending script in the player. [FlavioJS]
  3512. // It is unclear how that can be triggered, so it needs the be traced/checked in more detail.
  3513. // NOTE At the time of this change, this function wasn't capable of taking over the script state because st->scriptroot was never set.
  3514. st = script_alloc_state(rootscript, pos, rid, oid);
  3515. run_script_main(st);
  3516. }
  3517. void script_stop_instances(struct script_code *code) {
  3518. DBIterator *iter;
  3519. struct script_state* st;
  3520. if( !active_scripts )
  3521. return; // Don't even bother.
  3522. iter = db_iterator(st_db);
  3523. for( st = dbi_first(iter); dbi_exists(iter); st = dbi_next(iter) ) {
  3524. if( st->script == code )
  3525. script_free_state(st);
  3526. }
  3527. dbi_destroy(iter);
  3528. }
  3529. /*==========================================
  3530. * Timer function for sleep
  3531. *------------------------------------------*/
  3532. int run_script_timer(int tid, unsigned int tick, int id, intptr_t data)
  3533. {
  3534. struct script_state *st = (struct script_state *)data;
  3535. TBL_PC *sd = map_id2sd(st->rid);
  3536. if ((sd && sd->status.char_id != id) || (st->rid && !sd)) { // Character mismatch. Cancel execution.
  3537. st->rid = 0;
  3538. st->state = END;
  3539. }
  3540. st->sleep.timer = INVALID_TIMER;
  3541. if(st->state != RERUNLINE)
  3542. st->sleep.tick = 0;
  3543. run_script_main(st);
  3544. return 0;
  3545. }
  3546. /// Detaches script state from possibly attached character and restores it's previous script if any.
  3547. ///
  3548. /// @param st Script state to detach.
  3549. /// @param dequeue_event Whether to schedule any queued events, when there was no previous script.
  3550. static void script_detach_state(struct script_state* st, bool dequeue_event)
  3551. {
  3552. struct map_session_data* sd;
  3553. if(st->rid && (sd = map_id2sd(st->rid))!=NULL) {
  3554. sd->st = st->bk_st;
  3555. sd->npc_id = st->bk_npcid;
  3556. sd->state.disable_atcommand_on_npc = 0;
  3557. if(st->bk_st) {
  3558. //Remove tag for removal.
  3559. st->bk_st = NULL;
  3560. st->bk_npcid = 0;
  3561. } else if(dequeue_event) {
  3562. #ifdef SECURE_NPCTIMEOUT
  3563. /**
  3564. * We're done with this NPC session, so we cancel the timer (if existent) and move on
  3565. **/
  3566. if( sd->npc_idle_timer != INVALID_TIMER ) {
  3567. delete_timer(sd->npc_idle_timer,npc_rr_secure_timeout_timer);
  3568. sd->npc_idle_timer = INVALID_TIMER;
  3569. }
  3570. #endif
  3571. npc_event_dequeue(sd);
  3572. }
  3573. }
  3574. else if(st->bk_st)
  3575. {// rid was set to 0, before detaching the script state
  3576. 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);
  3577. script_reportsrc(st->bk_st);
  3578. script_free_state(st->bk_st);
  3579. st->bk_st = NULL;
  3580. }
  3581. }
  3582. /// Attaches script state to possibly attached character and backups it's previous script, if any.
  3583. ///
  3584. /// @param st Script state to attach.
  3585. static void script_attach_state(struct script_state* st)
  3586. {
  3587. struct map_session_data* sd;
  3588. if(st->rid && (sd = map_id2sd(st->rid))!=NULL)
  3589. {
  3590. if(st!=sd->st)
  3591. {
  3592. if(st->bk_st)
  3593. {// there is already a backup
  3594. ShowDebug("script_attach_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);
  3595. }
  3596. st->bk_st = sd->st;
  3597. st->bk_npcid = sd->npc_id;
  3598. }
  3599. sd->st = st;
  3600. sd->npc_id = st->oid;
  3601. sd->npc_item_flag = st->npc_item_flag; // load default.
  3602. sd->state.disable_atcommand_on_npc = (!pc_has_permission(sd, PC_PERM_ENABLE_COMMAND));
  3603. #ifdef SECURE_NPCTIMEOUT
  3604. if( sd->npc_idle_timer == INVALID_TIMER )
  3605. sd->npc_idle_timer = add_timer(gettick() + (SECURE_NPCTIMEOUT_INTERVAL*1000),npc_rr_secure_timeout_timer,sd->bl.id,0);
  3606. sd->npc_idle_tick = gettick();
  3607. #endif
  3608. }
  3609. }
  3610. /*==========================================
  3611. * The main part of the script execution
  3612. *------------------------------------------*/
  3613. void run_script_main(struct script_state *st)
  3614. {
  3615. int cmdcount = script_config.check_cmdcount;
  3616. int gotocount = script_config.check_gotocount;
  3617. TBL_PC *sd;
  3618. struct script_stack *stack = st->stack;
  3619. script_attach_state(st);
  3620. if(st->state == RERUNLINE) {
  3621. run_func(st);
  3622. if(st->state == GOTO)
  3623. st->state = RUN;
  3624. } else if(st->state != END)
  3625. st->state = RUN;
  3626. while(st->state == RUN) {
  3627. enum c_op c = get_com(st->script->script_buf,&st->pos);
  3628. switch(c){
  3629. case C_EOL:
  3630. if( stack->defsp > stack->sp )
  3631. ShowError("script:run_script_main: unexpected stack position (defsp=%d sp=%d). please report this!!!\n", stack->defsp, stack->sp);
  3632. else
  3633. pop_stack(st, stack->defsp, stack->sp);// pop unused stack data. (unused return value)
  3634. break;
  3635. case C_INT:
  3636. push_val(stack,C_INT,get_num(st->script->script_buf,&st->pos));
  3637. break;
  3638. case C_POS:
  3639. case C_NAME:
  3640. push_val(stack,c,GETVALUE(st->script->script_buf,st->pos));
  3641. st->pos+=3;
  3642. break;
  3643. case C_ARG:
  3644. push_val(stack,c,0);
  3645. break;
  3646. case C_STR:
  3647. push_str(stack,C_CONSTSTR,(char*)(st->script->script_buf+st->pos));
  3648. while(st->script->script_buf[st->pos++]);
  3649. break;
  3650. case C_FUNC:
  3651. run_func(st);
  3652. if(st->state==GOTO){
  3653. st->state = RUN;
  3654. if( !st->freeloop && gotocount>0 && (--gotocount)<=0 ){
  3655. ShowError("script:run_script_main: infinity loop !\n");
  3656. script_reportsrc(st);
  3657. st->state=END;
  3658. }
  3659. }
  3660. break;
  3661. case C_REF:
  3662. st->op2ref = 1;
  3663. break;
  3664. case C_NEG:
  3665. case C_NOT:
  3666. case C_LNOT:
  3667. op_1(st ,c);
  3668. break;
  3669. case C_ADD:
  3670. case C_SUB:
  3671. case C_MUL:
  3672. case C_DIV:
  3673. case C_MOD:
  3674. case C_EQ:
  3675. case C_NE:
  3676. case C_GT:
  3677. case C_GE:
  3678. case C_LT:
  3679. case C_LE:
  3680. case C_AND:
  3681. case C_OR:
  3682. case C_XOR:
  3683. case C_LAND:
  3684. case C_LOR:
  3685. case C_R_SHIFT:
  3686. case C_L_SHIFT:
  3687. op_2(st, c);
  3688. break;
  3689. case C_OP3:
  3690. op_3(st, c);
  3691. break;
  3692. case C_NOP:
  3693. st->state=END;
  3694. break;
  3695. default:
  3696. ShowError("script:run_script_main:unknown command : %d @ %d\n",c,st->pos);
  3697. st->state=END;
  3698. break;
  3699. }
  3700. if( !st->freeloop && cmdcount>0 && (--cmdcount)<=0 ){
  3701. ShowError("script:run_script_main: infinity loop !\n");
  3702. script_reportsrc(st);
  3703. st->state=END;
  3704. }
  3705. }
  3706. if(st->sleep.tick > 0) {
  3707. //Restore previous script
  3708. script_detach_state(st, false);
  3709. //Delay execution
  3710. sd = map_id2sd(st->rid); // Get sd since script might have attached someone while running. [Inkfish]
  3711. st->sleep.charid = sd?sd->status.char_id:0;
  3712. st->sleep.timer = add_timer(gettick()+st->sleep.tick,
  3713. run_script_timer, st->sleep.charid, (intptr_t)st);
  3714. } else if(st->state != END && st->rid) {
  3715. //Resume later (st is already attached to player).
  3716. if(st->bk_st) {
  3717. ShowWarning("Unable to restore stack! Double continuation!\n");
  3718. //Report BOTH scripts to see if that can help somehow.
  3719. ShowDebug("Previous script (lost):\n");
  3720. script_reportsrc(st->bk_st);
  3721. ShowDebug("Current script:\n");
  3722. script_reportsrc(st);
  3723. script_free_state(st->bk_st);
  3724. st->bk_st = NULL;
  3725. }
  3726. } else {
  3727. //Dispose of script.
  3728. if ((sd = map_id2sd(st->rid))!=NULL)
  3729. { //Restore previous stack and save char.
  3730. if(sd->state.using_fake_npc){
  3731. clif_clearunit_single(sd->npc_id, CLR_OUTSIGHT, sd->fd);
  3732. sd->state.using_fake_npc = 0;
  3733. }
  3734. //Restore previous script if any.
  3735. script_detach_state(st, true);
  3736. if (sd->vars_dirty)
  3737. intif_saveregistry(sd);
  3738. }
  3739. script_free_state(st);
  3740. }
  3741. }
  3742. int script_config_read(char *cfgName)
  3743. {
  3744. int i;
  3745. char line[1024],w1[1024],w2[1024];
  3746. FILE *fp;
  3747. fp=fopen(cfgName,"r");
  3748. if(fp==NULL){
  3749. ShowError("File not found: %s\n", cfgName);
  3750. return 1;
  3751. }
  3752. while(fgets(line, sizeof(line), fp))
  3753. {
  3754. if(line[0] == '/' && line[1] == '/')
  3755. continue;
  3756. i=sscanf(line,"%1023[^:]: %1023[^\r\n]",w1,w2);
  3757. if(i!=2)
  3758. continue;
  3759. if(strcmpi(w1,"warn_func_mismatch_paramnum")==0) {
  3760. script_config.warn_func_mismatch_paramnum = config_switch(w2);
  3761. }
  3762. else if(strcmpi(w1,"check_cmdcount")==0) {
  3763. script_config.check_cmdcount = config_switch(w2);
  3764. }
  3765. else if(strcmpi(w1,"check_gotocount")==0) {
  3766. script_config.check_gotocount = config_switch(w2);
  3767. }
  3768. else if(strcmpi(w1,"input_min_value")==0) {
  3769. script_config.input_min_value = config_switch(w2);
  3770. }
  3771. else if(strcmpi(w1,"input_max_value")==0) {
  3772. script_config.input_max_value = config_switch(w2);
  3773. }
  3774. else if(strcmpi(w1,"warn_func_mismatch_argtypes")==0) {
  3775. script_config.warn_func_mismatch_argtypes = config_switch(w2);
  3776. }
  3777. else if(strcmpi(w1,"import")==0){
  3778. script_config_read(w2);
  3779. }
  3780. else {
  3781. ShowWarning("Unknown setting '%s' in file %s\n", w1, cfgName);
  3782. }
  3783. }
  3784. fclose(fp);
  3785. return 0;
  3786. }
  3787. /**
  3788. * @see DBApply
  3789. */
  3790. static int db_script_free_code_sub(DBKey key, DBData *data, va_list ap)
  3791. {
  3792. struct script_code *code = db_data2ptr(data);
  3793. if (code)
  3794. script_free_code(code);
  3795. return 0;
  3796. }
  3797. void script_run_autobonus(const char *autobonus, struct map_session_data *sd, unsigned int pos)
  3798. {
  3799. struct script_code *script = (struct script_code *)strdb_get(autobonus_db, autobonus);
  3800. if( script )
  3801. {
  3802. int j;
  3803. ARR_FIND( 0, EQI_MAX, j, sd->equip_index[j] >= 0 && sd->status.inventory[sd->equip_index[j]].equip == pos );
  3804. if( j < EQI_MAX ) {
  3805. //Single item autobonus
  3806. current_equip_item_index = sd->equip_index[j];
  3807. current_equip_combo_pos = 0;
  3808. } else {
  3809. //Combo autobonus
  3810. current_equip_item_index = -1;
  3811. current_equip_combo_pos = pos;
  3812. }
  3813. run_script(script,0,sd->bl.id,0);
  3814. }
  3815. }
  3816. void script_add_autobonus(const char *autobonus)
  3817. {
  3818. if( strdb_get(autobonus_db, autobonus) == NULL )
  3819. {
  3820. struct script_code *script = parse_script(autobonus, "autobonus", 0, 0);
  3821. if( script )
  3822. strdb_put(autobonus_db, autobonus, script);
  3823. }
  3824. }
  3825. /// resets a temporary character array variable to given value
  3826. void script_cleararray_pc(struct map_session_data* sd, const char* varname, void* value)
  3827. {
  3828. struct script_array *sa = NULL;
  3829. struct reg_db *src = NULL;
  3830. unsigned int i, *list = NULL, size = 0;
  3831. int key;
  3832. key = add_str(varname);
  3833. if( !(src = script_array_src(NULL,sd,varname,NULL) ) )
  3834. return;
  3835. if( value )
  3836. script_array_ensure_zero(NULL,sd,reference_uid(key,0), NULL);
  3837. if( !(sa = idb_get(src->arrays, key)) ) /* non-existent array, nothing to empty */
  3838. return;
  3839. size = sa->size;
  3840. list = script_array_cpy_list(sa);
  3841. for(i = 0; i < size; i++) {
  3842. set_reg(NULL,sd,reference_uid(key, list[i]),varname,value,NULL);
  3843. }
  3844. }
  3845. /// sets a temporary character array variable element idx to given value
  3846. /// @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.
  3847. void script_setarray_pc(struct map_session_data* sd, const char* varname, uint32 idx, void* value, int* refcache)
  3848. {
  3849. int key;
  3850. if( idx >= SCRIPT_MAX_ARRAYSIZE ) {
  3851. ShowError("script_setarray_pc: Variable '%s' has invalid index '%u' (char_id=%d).\n", varname, idx, sd->status.char_id);
  3852. return;
  3853. }
  3854. key = ( refcache && refcache[0] ) ? refcache[0] : add_str(varname);
  3855. set_reg(NULL,sd,reference_uid(key, idx),varname,value,NULL);
  3856. if( refcache ) {
  3857. // save to avoid repeated script->add_str calls
  3858. refcache[0] = key;
  3859. }
  3860. }
  3861. /**
  3862. * Clears persistent variables from memory
  3863. **/
  3864. int script_reg_destroy(DBKey key, DBData *data, va_list ap)
  3865. {
  3866. struct script_reg_state *src;
  3867. if( data->type != DB_DATA_PTR ) // got no need for those!
  3868. return 0;
  3869. src = db_data2ptr(data);
  3870. if( src->type ) {
  3871. struct script_reg_str *p = (struct script_reg_str *)src;
  3872. if( p->value )
  3873. aFree(p->value);
  3874. ers_free(str_reg_ers,p);
  3875. } else {
  3876. ers_free(num_reg_ers,(struct script_reg_num*)src);
  3877. }
  3878. return 0;
  3879. }
  3880. /**
  3881. * Clears a single persistent variable
  3882. **/
  3883. void script_reg_destroy_single(struct map_session_data *sd, int64 reg, struct script_reg_state *data)
  3884. {
  3885. i64db_remove(sd->regs.vars, reg);
  3886. if( data->type ) {
  3887. struct script_reg_str *p = (struct script_reg_str*)data;
  3888. if( p->value )
  3889. aFree(p->value);
  3890. ers_free(str_reg_ers,p);
  3891. } else {
  3892. ers_free(num_reg_ers,(struct script_reg_num*)data);
  3893. }
  3894. }
  3895. unsigned int *script_array_cpy_list(struct script_array *sa)
  3896. {
  3897. if( sa->size > generic_ui_array_size )
  3898. script_generic_ui_array_expand(sa->size);
  3899. memcpy(generic_ui_array, sa->members, sizeof(unsigned int)*sa->size);
  3900. return generic_ui_array;
  3901. }
  3902. void script_generic_ui_array_expand (unsigned int plus)
  3903. {
  3904. generic_ui_array_size += plus + 100;
  3905. RECREATE(generic_ui_array, unsigned int, generic_ui_array_size);
  3906. }
  3907. int buildin_query_sql_sub(struct script_state *st, Sql *handle);
  3908. #ifdef BETA_THREAD_TEST
  3909. /* used to receive items the queryThread has already processed */
  3910. int queryThread_timer(int tid, unsigned int tick, int id, intptr_t data) {
  3911. int i, cursor = 0;
  3912. bool allOk = true;
  3913. EnterSpinLock(&queryThreadLock);
  3914. for( i = 0; i < queryThreadData.count; i++ ) {
  3915. struct queryThreadEntry *entry = queryThreadData.entry[i];
  3916. if( !entry->ok ) {
  3917. allOk = false;
  3918. continue;
  3919. }
  3920. run_script_main(entry->st);
  3921. entry->st = NULL;/* empty entries */
  3922. aFree(entry);
  3923. queryThreadData.entry[i] = NULL;
  3924. }
  3925. if( allOk ) {
  3926. /* cancel the repeating timer -- it'll re-create itself when necessary, dont need to remain looping */
  3927. delete_timer(queryThreadData.timer, queryThread_timer);
  3928. queryThreadData.timer = INVALID_TIMER;
  3929. }
  3930. /* now lets clear the mess. */
  3931. for( i = 0; i < queryThreadData.count; i++ ) {
  3932. struct queryThreadEntry *entry = queryThreadData.entry[i];
  3933. if( entry == NULL )
  3934. continue;/* entry on hold */
  3935. /* move */
  3936. memmove(&queryThreadData.entry[cursor], &queryThreadData.entry[i], sizeof(struct queryThreadEntry*));
  3937. cursor++;
  3938. }
  3939. queryThreadData.count = cursor;
  3940. LeaveSpinLock(&queryThreadLock);
  3941. return 0;
  3942. }
  3943. void queryThread_add(struct script_state *st, bool type) {
  3944. int idx = 0;
  3945. struct queryThreadEntry* entry = NULL;
  3946. EnterSpinLock(&queryThreadLock);
  3947. if( queryThreadData.count++ != 0 )
  3948. RECREATE(queryThreadData.entry, struct queryThreadEntry* , queryThreadData.count);
  3949. idx = queryThreadData.count-1;
  3950. CREATE(queryThreadData.entry[idx],struct queryThreadEntry,1);
  3951. entry = queryThreadData.entry[idx];
  3952. entry->st = st;
  3953. entry->ok = false;
  3954. entry->type = type;
  3955. if( queryThreadData.timer == INVALID_TIMER ) { /* start the receiver timer */
  3956. queryThreadData.timer = add_timer_interval(gettick() + 100, queryThread_timer, 0, 0, 100);
  3957. }
  3958. LeaveSpinLock(&queryThreadLock);
  3959. /* unlock the queryThread */
  3960. racond_signal(queryThreadCond);
  3961. }
  3962. /* adds a new log to the queue */
  3963. void queryThread_log(char * entry, int length) {
  3964. int idx = logThreadData.count;
  3965. EnterSpinLock(&queryThreadLock);
  3966. if( logThreadData.count++ != 0 )
  3967. RECREATE(logThreadData.entry, char* , logThreadData.count);
  3968. CREATE(logThreadData.entry[idx], char, length + 1 );
  3969. safestrncpy(logThreadData.entry[idx], entry, length + 1 );
  3970. LeaveSpinLock(&queryThreadLock);
  3971. /* unlock the queryThread */
  3972. racond_signal(queryThreadCond);
  3973. }
  3974. /* queryThread_main */
  3975. static void *queryThread_main(void *x) {
  3976. Sql *queryThread_handle = Sql_Malloc();
  3977. int i;
  3978. if ( SQL_ERROR == Sql_Connect(queryThread_handle, map_server_id, map_server_pw, map_server_ip, map_server_port, map_server_db) ){
  3979. ShowError("Couldn't connect with uname='%s',passwd='%s',host='%s',port='%d',database='%s'\n",
  3980. map_server_id, map_server_pw, map_server_ip, map_server_port, map_server_db);
  3981. Sql_ShowDebug(queryThread_handle);
  3982. Sql_Free(queryThread_handle);
  3983. exit(EXIT_FAILURE);
  3984. }
  3985. if( strlen(default_codepage) > 0 )
  3986. if ( SQL_ERROR == Sql_SetEncoding(queryThread_handle, default_codepage) )
  3987. Sql_ShowDebug(queryThread_handle);
  3988. if( log_config.sql_logs ) {
  3989. logmysql_handle = Sql_Malloc();
  3990. if ( SQL_ERROR == Sql_Connect(logmysql_handle, log_db_id, log_db_pw, log_db_ip, log_db_port, log_db_db) ){
  3991. ShowError("Couldn't connect with uname='%s',passwd='%s',host='%s',port='%d',database='%s'\n",
  3992. log_db_id, log_db_pw, log_db_ip, log_db_port, log_db_db);
  3993. Sql_ShowDebug(logmysql_handle);
  3994. Sql_Free(logmysql_handle);
  3995. exit(EXIT_FAILURE);
  3996. }
  3997. if( strlen(default_codepage) > 0 )
  3998. if ( SQL_ERROR == Sql_SetEncoding(logmysql_handle, default_codepage) )
  3999. Sql_ShowDebug(logmysql_handle);
  4000. }
  4001. while( 1 ) {
  4002. if(queryThreadTerminate > 0)
  4003. break;
  4004. EnterSpinLock(&queryThreadLock);
  4005. /* mess with queryThreadData within the lock */
  4006. for( i = 0; i < queryThreadData.count; i++ ) {
  4007. struct queryThreadEntry *entry = queryThreadData.entry[i];
  4008. if( entry->ok )
  4009. continue;
  4010. else if ( !entry->st || !entry->st->stack ) {
  4011. entry->ok = true;/* dispose */
  4012. continue;
  4013. }
  4014. buildin_query_sql_sub(entry->st, entry->type ? logmysql_handle : queryThread_handle);
  4015. entry->ok = true;/* we're done with this */
  4016. }
  4017. /* also check for any logs in need to be sent */
  4018. if( log_config.sql_logs ) {
  4019. for( i = 0; i < logThreadData.count; i++ ) {
  4020. if( SQL_ERROR == Sql_Query(logmysql_handle, logThreadData.entry[i]) )
  4021. Sql_ShowDebug(logmysql_handle);
  4022. aFree(logThreadData.entry[i]);
  4023. }
  4024. logThreadData.count = 0;
  4025. }
  4026. LeaveSpinLock(&queryThreadLock);
  4027. ramutex_lock( queryThreadMutex );
  4028. racond_wait( queryThreadCond, queryThreadMutex, -1 );
  4029. ramutex_unlock( queryThreadMutex );
  4030. }
  4031. Sql_Free(queryThread_handle);
  4032. if( log_config.sql_logs ) {
  4033. Sql_Free(logmysql_handle);
  4034. }
  4035. return NULL;
  4036. }
  4037. #endif
  4038. /*==========================================
  4039. * Destructor
  4040. *------------------------------------------*/
  4041. void do_final_script() {
  4042. int i;
  4043. DBIterator *iter;
  4044. struct script_state *st;
  4045. #ifdef DEBUG_HASH
  4046. if (battle_config.etc_log)
  4047. {
  4048. FILE *fp = fopen("hash_dump.txt","wt");
  4049. if(fp) {
  4050. int count[SCRIPT_HASH_SIZE];
  4051. int count2[SCRIPT_HASH_SIZE]; // number of buckets with a certain number of items
  4052. int n=0;
  4053. int min=INT_MAX,max=0,zero=0;
  4054. double mean=0.0f;
  4055. double median=0.0f;
  4056. ShowNotice("Dumping script str hash information to hash_dump.txt\n");
  4057. memset(count, 0, sizeof(count));
  4058. fprintf(fp,"num : hash : data_name\n");
  4059. fprintf(fp,"---------------------------------------------------------------\n");
  4060. for(i=LABEL_START; i<str_num; i++) {
  4061. unsigned int h = calc_hash(get_str(i));
  4062. fprintf(fp,"%04d : %4u : %s\n",i,h, get_str(i));
  4063. ++count[h];
  4064. }
  4065. fprintf(fp,"--------------------\n\n");
  4066. memset(count2, 0, sizeof(count2));
  4067. for(i=0; i<SCRIPT_HASH_SIZE; i++) {
  4068. fprintf(fp," hash %3d = %d\n",i,count[i]);
  4069. if(min > count[i])
  4070. min = count[i]; // minimun count of collision
  4071. if(max < count[i])
  4072. max = count[i]; // maximun count of collision
  4073. if(count[i] == 0)
  4074. zero++;
  4075. ++count2[count[i]];
  4076. }
  4077. fprintf(fp,"\n--------------------\n items : buckets\n--------------------\n");
  4078. for( i=min; i <= max; ++i ){
  4079. fprintf(fp," %5d : %7d\n",i,count2[i]);
  4080. mean += 1.0f*i*count2[i]/SCRIPT_HASH_SIZE; // Note: this will always result in <nr labels>/<nr buckets>
  4081. }
  4082. for( i=min; i <= max; ++i ){
  4083. n += count2[i];
  4084. if( n*2 >= SCRIPT_HASH_SIZE )
  4085. {
  4086. if( SCRIPT_HASH_SIZE%2 == 0 && SCRIPT_HASH_SIZE/2 == n )
  4087. median = (i+i+1)/2.0f;
  4088. else
  4089. median = i;
  4090. break;
  4091. }
  4092. }
  4093. fprintf(fp,"--------------------\n min = %d, max = %d, zero = %d\n mean = %lf, median = %lf\n",min,max,zero,mean,median);
  4094. fclose(fp);
  4095. }
  4096. }
  4097. #endif
  4098. mapreg_final();
  4099. db_destroy(scriptlabel_db);
  4100. userfunc_db->destroy(userfunc_db, db_script_free_code_sub);
  4101. autobonus_db->destroy(autobonus_db, db_script_free_code_sub);
  4102. ers_destroy(array_ers);
  4103. if (generic_ui_array)
  4104. aFree(generic_ui_array);
  4105. iter = db_iterator(st_db);
  4106. for(st = dbi_first(iter); dbi_exists(iter); st = dbi_next(iter))
  4107. script_free_state(st);
  4108. dbi_destroy(iter);
  4109. if (str_data)
  4110. aFree(str_data);
  4111. if (str_buf)
  4112. aFree(str_buf);
  4113. for( i = 0; i < atcmd_binding_count; i++ ) {
  4114. aFree(atcmd_binding[i]);
  4115. }
  4116. if( atcmd_binding_count != 0 )
  4117. aFree(atcmd_binding);
  4118. ers_destroy(st_ers);
  4119. ers_destroy(stack_ers);
  4120. db_destroy(st_db);
  4121. #ifdef BETA_THREAD_TEST
  4122. /* QueryThread */
  4123. InterlockedIncrement(&queryThreadTerminate);
  4124. racond_signal(queryThreadCond);
  4125. rathread_wait(queryThread, NULL);
  4126. // Destroy cond var and mutex.
  4127. racond_destroy( queryThreadCond );
  4128. ramutex_destroy( queryThreadMutex );
  4129. /* Clear missing vars */
  4130. for( i = 0; i < queryThreadData.count; i++ ) {
  4131. aFree(queryThreadData.entry[i]);
  4132. }
  4133. aFree(queryThreadData.entry);
  4134. for( i = 0; i < logThreadData.count; i++ ) {
  4135. aFree(logThreadData.entry[i]);
  4136. }
  4137. aFree(logThreadData.entry);
  4138. #endif
  4139. }
  4140. /*==========================================
  4141. * Initialization
  4142. *------------------------------------------*/
  4143. void do_init_script(void) {
  4144. st_db = idb_alloc(DB_OPT_BASE);
  4145. userfunc_db = strdb_alloc(DB_OPT_DUP_KEY,0);
  4146. scriptlabel_db = strdb_alloc(DB_OPT_DUP_KEY,50);
  4147. autobonus_db = strdb_alloc(DB_OPT_DUP_KEY,0);
  4148. st_ers = ers_new(sizeof(struct script_state), "script.c::st_ers", ERS_CACHE_OPTIONS);
  4149. stack_ers = ers_new(sizeof(struct script_stack), "script.c::script_stack", ERS_OPT_FLEX_CHUNK);
  4150. array_ers = ers_new(sizeof(struct script_array), "script.c:array_ers", ERS_OPT_CLEAN|ERS_OPT_CLEAR); // TODO: This value is not yet in the enum
  4151. ers_chunk_size(st_ers, 10);
  4152. ers_chunk_size(stack_ers, 10);
  4153. active_scripts = 0;
  4154. next_id = 0;
  4155. mapreg_init();
  4156. #ifdef BETA_THREAD_TEST
  4157. CREATE(queryThreadData.entry, struct queryThreadEntry*, 1);
  4158. queryThreadData.count = 0;
  4159. CREATE(logThreadData.entry, char *, 1);
  4160. logThreadData.count = 0;
  4161. /* QueryThread Start */
  4162. InitializeSpinLock(&queryThreadLock);
  4163. queryThreadData.timer = INVALID_TIMER;
  4164. queryThreadTerminate = 0;
  4165. queryThreadMutex = ramutex_create();
  4166. queryThreadCond = racond_create();
  4167. queryThread = rathread_create(queryThread_main, NULL);
  4168. if(queryThread == NULL){
  4169. ShowFatalError("do_init_script: cannot spawn Query Thread.\n");
  4170. exit(EXIT_FAILURE);
  4171. }
  4172. add_timer_func_list(queryThread_timer, "queryThread_timer");
  4173. #endif
  4174. }
  4175. void script_reload(void) {
  4176. int i;
  4177. DBIterator *iter;
  4178. struct script_state *st;
  4179. #ifdef BETA_THREAD_TEST
  4180. /* we're reloading so any queries undergoing should be...exterminated. */
  4181. EnterSpinLock(&queryThreadLock);
  4182. for( i = 0; i < queryThreadData.count; i++ ) {
  4183. aFree(queryThreadData.entry[i]);
  4184. }
  4185. queryThreadData.count = 0;
  4186. if( queryThreadData.timer != INVALID_TIMER ) {
  4187. delete_timer(queryThreadData.timer, queryThread_timer);
  4188. queryThreadData.timer = INVALID_TIMER;
  4189. }
  4190. LeaveSpinLock(&queryThreadLock);
  4191. #endif
  4192. userfunc_db->clear(userfunc_db, db_script_free_code_sub);
  4193. db_clear(scriptlabel_db);
  4194. // @commands (script based)
  4195. // Clear bindings
  4196. for( i = 0; i < atcmd_binding_count; i++ ) {
  4197. aFree(atcmd_binding[i]);
  4198. }
  4199. if( atcmd_binding_count != 0 )
  4200. aFree(atcmd_binding);
  4201. atcmd_binding_count = 0;
  4202. iter = db_iterator(st_db);
  4203. for(st = dbi_first(iter); dbi_exists(iter); st = dbi_next(iter))
  4204. script_free_state(st);
  4205. dbi_destroy(iter);
  4206. db_clear(st_db);
  4207. mapreg_reload();
  4208. }
  4209. //-----------------------------------------------------------------------------
  4210. // buildin functions
  4211. //
  4212. #define BUILDIN_DEF(x,args) { buildin_ ## x , #x , args }
  4213. #define BUILDIN_DEF2(x,x2,args) { buildin_ ## x , x2 , args }
  4214. #define BUILDIN_FUNC(x) int buildin_ ## x (struct script_state* st)
  4215. /////////////////////////////////////////////////////////////////////
  4216. // NPC interaction
  4217. //
  4218. /// Appends a message to the npc dialog.
  4219. /// If a dialog doesn't exist yet, one is created.
  4220. ///
  4221. /// mes "<message>";
  4222. BUILDIN_FUNC(mes)
  4223. {
  4224. TBL_PC* sd = script_rid2sd(st);
  4225. if( sd == NULL )
  4226. return SCRIPT_CMD_SUCCESS;
  4227. if( !script_hasdata(st, 3) )
  4228. {// only a single line detected in the script
  4229. clif_scriptmes(sd, st->oid, script_getstr(st, 2));
  4230. }
  4231. else
  4232. {// parse multiple lines as they exist
  4233. int i;
  4234. for( i = 2; script_hasdata(st, i); i++ )
  4235. {
  4236. // send the message to the client
  4237. clif_scriptmes(sd, st->oid, script_getstr(st, i));
  4238. }
  4239. }
  4240. st->mes_active = 1; // Invoking character has a NPC dialog box open.
  4241. return SCRIPT_CMD_SUCCESS;
  4242. }
  4243. /// Displays the button 'next' in the npc dialog.
  4244. /// The dialog text is cleared and the script continues when the button is pressed.
  4245. ///
  4246. /// next;
  4247. BUILDIN_FUNC(next)
  4248. {
  4249. TBL_PC* sd;
  4250. sd = script_rid2sd(st);
  4251. if( sd == NULL )
  4252. return SCRIPT_CMD_SUCCESS;
  4253. #ifdef SECURE_NPCTIMEOUT
  4254. sd->npc_idle_type = NPCT_WAIT;
  4255. #endif
  4256. st->state = STOP;
  4257. clif_scriptnext(sd, st->oid);
  4258. return SCRIPT_CMD_SUCCESS;
  4259. }
  4260. /// Ends the script and displays the button 'close' on the npc dialog.
  4261. /// The dialog is closed when the button is pressed.
  4262. ///
  4263. /// close;
  4264. BUILDIN_FUNC(close)
  4265. {
  4266. TBL_PC* sd;
  4267. sd = script_rid2sd(st);
  4268. if( sd == NULL )
  4269. return SCRIPT_CMD_SUCCESS;
  4270. if( !st->mes_active ) {
  4271. TBL_NPC* nd = map_id2nd(st->oid);
  4272. st->state = END; // Keep backwards compatibility.
  4273. ShowWarning("Incorrect use of 'close' command! (source:%s / path:%s)\n",nd?nd->name:"Unknown",nd?nd->path:"Unknown");
  4274. } else {
  4275. st->state = CLOSE;
  4276. st->mes_active = 0;
  4277. }
  4278. clif_scriptclose(sd, st->oid);
  4279. return SCRIPT_CMD_SUCCESS;
  4280. }
  4281. /// Displays the button 'close' on the npc dialog.
  4282. /// The dialog is closed and the script continues when the button is pressed.
  4283. ///
  4284. /// close2;
  4285. BUILDIN_FUNC(close2)
  4286. {
  4287. TBL_PC* sd;
  4288. sd = script_rid2sd(st);
  4289. if( sd == NULL )
  4290. return SCRIPT_CMD_SUCCESS;
  4291. st->state = STOP;
  4292. if( st->mes_active )
  4293. st->mes_active = 0;
  4294. clif_scriptclose(sd, st->oid);
  4295. return SCRIPT_CMD_SUCCESS;
  4296. }
  4297. /// Counts the number of valid and total number of options in 'str'
  4298. /// If max_count > 0 the counting stops when that valid option is reached
  4299. /// total is incremented for each option (NULL is supported)
  4300. static int menu_countoptions(const char* str, int max_count, int* total)
  4301. {
  4302. int count = 0;
  4303. int bogus_total;
  4304. if( total == NULL )
  4305. total = &bogus_total;
  4306. ++(*total);
  4307. // initial empty options
  4308. while( *str == ':' )
  4309. {
  4310. ++str;
  4311. ++(*total);
  4312. }
  4313. // count menu options
  4314. while( *str != '\0' )
  4315. {
  4316. ++count;
  4317. --max_count;
  4318. if( max_count == 0 )
  4319. break;
  4320. while( *str != ':' && *str != '\0' )
  4321. ++str;
  4322. while( *str == ':' )
  4323. {
  4324. ++str;
  4325. ++(*total);
  4326. }
  4327. }
  4328. return count;
  4329. }
  4330. /// Displays a menu with options and goes to the target label.
  4331. /// The script is stopped if cancel is pressed.
  4332. /// Options with no text are not displayed in the client.
  4333. ///
  4334. /// Options can be grouped together, separated by the character ':' in the text:
  4335. /// ex: menu "A:B:C",L_target;
  4336. /// All these options go to the specified target label.
  4337. ///
  4338. /// The index of the selected option is put in the variable @menu.
  4339. /// Indexes start with 1 and are consistent with grouped and empty options.
  4340. /// ex: menu "A::B",-,"",L_Impossible,"C",-;
  4341. /// // displays "A", "B" and "C", corresponding to indexes 1, 3 and 5
  4342. ///
  4343. /// NOTE: the client closes the npc dialog when cancel is pressed
  4344. ///
  4345. /// menu "<option_text>",<target_label>{,"<option_text>",<target_label>,...};
  4346. BUILDIN_FUNC(menu)
  4347. {
  4348. int i;
  4349. const char* text;
  4350. TBL_PC* sd;
  4351. sd = script_rid2sd(st);
  4352. if( sd == NULL )
  4353. return SCRIPT_CMD_SUCCESS;
  4354. #ifdef SECURE_NPCTIMEOUT
  4355. sd->npc_idle_type = NPCT_MENU;
  4356. #endif
  4357. // TODO detect multiple scripts waiting for input at the same time, and what to do when that happens
  4358. if( sd->state.menu_or_input == 0 )
  4359. {
  4360. struct StringBuf buf;
  4361. if( script_lastdata(st) % 2 == 0 )
  4362. {// argument count is not even (1st argument is at index 2)
  4363. ShowError("buildin_menu: Illegal number of arguments (%d).\n", (script_lastdata(st) - 1));
  4364. st->state = END;
  4365. return SCRIPT_CMD_FAILURE;
  4366. }
  4367. StringBuf_Init(&buf);
  4368. sd->npc_menu = 0;
  4369. for( i = 2; i < script_lastdata(st); i += 2 )
  4370. {
  4371. struct script_data* data;
  4372. // menu options
  4373. text = script_getstr(st, i);
  4374. // target label
  4375. data = script_getdata(st, i+1);
  4376. if( !data_islabel(data) )
  4377. {// not a label
  4378. StringBuf_Destroy(&buf);
  4379. ShowError("buildin_menu: Argument #%d (from 1) is not a label or label not found.\n", i);
  4380. script_reportdata(data);
  4381. st->state = END;
  4382. return SCRIPT_CMD_FAILURE;
  4383. }
  4384. // append option(s)
  4385. if( text[0] == '\0' )
  4386. continue;// empty string, ignore
  4387. if( sd->npc_menu > 0 )
  4388. StringBuf_AppendStr(&buf, ":");
  4389. StringBuf_AppendStr(&buf, text);
  4390. sd->npc_menu += menu_countoptions(text, 0, NULL);
  4391. }
  4392. st->state = RERUNLINE;
  4393. sd->state.menu_or_input = 1;
  4394. /**
  4395. * menus beyond this length crash the client (see bugreport:6402)
  4396. **/
  4397. if( StringBuf_Length(&buf) >= 2047 ) {
  4398. struct npc_data * nd = map_id2nd(st->oid);
  4399. char* menu;
  4400. CREATE(menu, char, 2048);
  4401. safestrncpy(menu, StringBuf_Value(&buf), 2047);
  4402. ShowWarning("buildin_menu: NPC Menu too long! (source:%s / length:%d)\n",nd?nd->name:"Unknown",StringBuf_Length(&buf));
  4403. clif_scriptmenu(sd, st->oid, menu);
  4404. aFree(menu);
  4405. } else
  4406. clif_scriptmenu(sd, st->oid, StringBuf_Value(&buf));
  4407. StringBuf_Destroy(&buf);
  4408. if( sd->npc_menu >= 0xff )
  4409. {// 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
  4410. ShowWarning("buildin_menu: Too many options specified (current=%d, max=254).\n", sd->npc_menu);
  4411. script_reportsrc(st);
  4412. }
  4413. }
  4414. else if( sd->npc_menu == 0xff )
  4415. {// Cancel was pressed
  4416. sd->state.menu_or_input = 0;
  4417. st->state = END;
  4418. }
  4419. else
  4420. {// goto target label
  4421. int menu = 0;
  4422. sd->state.menu_or_input = 0;
  4423. if( sd->npc_menu <= 0 )
  4424. {
  4425. ShowDebug("buildin_menu: Unexpected selection (%d)\n", sd->npc_menu);
  4426. st->state = END;
  4427. return SCRIPT_CMD_FAILURE;
  4428. }
  4429. // get target label
  4430. for( i = 2; i < script_lastdata(st); i += 2 )
  4431. {
  4432. text = script_getstr(st, i);
  4433. sd->npc_menu -= menu_countoptions(text, sd->npc_menu, &menu);
  4434. if( sd->npc_menu <= 0 )
  4435. break;// entry found
  4436. }
  4437. if( sd->npc_menu > 0 )
  4438. {// Invalid selection
  4439. ShowDebug("buildin_menu: Selection is out of range (%d pairs are missing?) - please report this\n", sd->npc_menu);
  4440. st->state = END;
  4441. return SCRIPT_CMD_FAILURE;
  4442. }
  4443. if( !data_islabel(script_getdata(st, i + 1)) )
  4444. {// TODO remove this temporary crash-prevention code (fallback for multiple scripts requesting user input)
  4445. ShowError("buildin_menu: Unexpected data in label argument\n");
  4446. script_reportdata(script_getdata(st, i + 1));
  4447. st->state = END;
  4448. return SCRIPT_CMD_FAILURE;
  4449. }
  4450. pc_setreg(sd, add_str("@menu"), menu);
  4451. st->pos = script_getnum(st, i + 1);
  4452. st->state = GOTO;
  4453. }
  4454. return SCRIPT_CMD_SUCCESS;
  4455. }
  4456. /// Displays a menu with options and returns the selected option.
  4457. /// Behaves like 'menu' without the target labels.
  4458. ///
  4459. /// select(<option_text>{,<option_text>,...}) -> <selected_option>
  4460. ///
  4461. /// @see menu
  4462. BUILDIN_FUNC(select)
  4463. {
  4464. int i;
  4465. const char* text;
  4466. TBL_PC* sd;
  4467. sd = script_rid2sd(st);
  4468. if( sd == NULL )
  4469. return SCRIPT_CMD_SUCCESS;
  4470. #ifdef SECURE_NPCTIMEOUT
  4471. sd->npc_idle_type = NPCT_MENU;
  4472. #endif
  4473. if( sd->state.menu_or_input == 0 ) {
  4474. struct StringBuf buf;
  4475. StringBuf_Init(&buf);
  4476. sd->npc_menu = 0;
  4477. for( i = 2; i <= script_lastdata(st); ++i ) {
  4478. text = script_getstr(st, i);
  4479. if( sd->npc_menu > 0 )
  4480. StringBuf_AppendStr(&buf, ":");
  4481. StringBuf_AppendStr(&buf, text);
  4482. sd->npc_menu += menu_countoptions(text, 0, NULL);
  4483. }
  4484. st->state = RERUNLINE;
  4485. sd->state.menu_or_input = 1;
  4486. /**
  4487. * menus beyond this length crash the client (see bugreport:6402)
  4488. **/
  4489. if( StringBuf_Length(&buf) >= 2047 ) {
  4490. struct npc_data * nd = map_id2nd(st->oid);
  4491. char* menu;
  4492. CREATE(menu, char, 2048);
  4493. safestrncpy(menu, StringBuf_Value(&buf), 2047);
  4494. ShowWarning("buildin_select: NPC Menu too long! (source:%s / length:%d)\n",nd?nd->name:"Unknown",StringBuf_Length(&buf));
  4495. clif_scriptmenu(sd, st->oid, menu);
  4496. aFree(menu);
  4497. } else
  4498. clif_scriptmenu(sd, st->oid, StringBuf_Value(&buf));
  4499. StringBuf_Destroy(&buf);
  4500. if( sd->npc_menu >= 0xff ) {
  4501. ShowWarning("buildin_select: Too many options specified (current=%d, max=254).\n", sd->npc_menu);
  4502. script_reportsrc(st);
  4503. }
  4504. } else if( sd->npc_menu == 0xff ) {// Cancel was pressed
  4505. sd->state.menu_or_input = 0;
  4506. st->state = END;
  4507. } else {// return selected option
  4508. int menu = 0;
  4509. sd->state.menu_or_input = 0;
  4510. for( i = 2; i <= script_lastdata(st); ++i ) {
  4511. text = script_getstr(st, i);
  4512. sd->npc_menu -= menu_countoptions(text, sd->npc_menu, &menu);
  4513. if( sd->npc_menu <= 0 )
  4514. break;// entry found
  4515. }
  4516. pc_setreg(sd, add_str("@menu"), menu);
  4517. script_pushint(st, menu);
  4518. st->state = RUN;
  4519. }
  4520. return SCRIPT_CMD_SUCCESS;
  4521. }
  4522. /// Displays a menu with options and returns the selected option.
  4523. /// Behaves like 'menu' without the target labels, except when cancel is
  4524. /// pressed.
  4525. /// When cancel is pressed, the script continues and 255 is returned.
  4526. ///
  4527. /// prompt(<option_text>{,<option_text>,...}) -> <selected_option>
  4528. ///
  4529. /// @see menu
  4530. BUILDIN_FUNC(prompt)
  4531. {
  4532. int i;
  4533. const char *text;
  4534. TBL_PC* sd;
  4535. sd = script_rid2sd(st);
  4536. if( sd == NULL )
  4537. return SCRIPT_CMD_SUCCESS;
  4538. #ifdef SECURE_NPCTIMEOUT
  4539. sd->npc_idle_type = NPCT_MENU;
  4540. #endif
  4541. if( sd->state.menu_or_input == 0 )
  4542. {
  4543. struct StringBuf buf;
  4544. StringBuf_Init(&buf);
  4545. sd->npc_menu = 0;
  4546. for( i = 2; i <= script_lastdata(st); ++i )
  4547. {
  4548. text = script_getstr(st, i);
  4549. if( sd->npc_menu > 0 )
  4550. StringBuf_AppendStr(&buf, ":");
  4551. StringBuf_AppendStr(&buf, text);
  4552. sd->npc_menu += menu_countoptions(text, 0, NULL);
  4553. }
  4554. st->state = RERUNLINE;
  4555. sd->state.menu_or_input = 1;
  4556. /**
  4557. * menus beyond this length crash the client (see bugreport:6402)
  4558. **/
  4559. if( StringBuf_Length(&buf) >= 2047 ) {
  4560. struct npc_data * nd = map_id2nd(st->oid);
  4561. char* menu;
  4562. CREATE(menu, char, 2048);
  4563. safestrncpy(menu, StringBuf_Value(&buf), 2047);
  4564. ShowWarning("buildin_prompt: NPC Menu too long! (source:%s / length:%d)\n",nd?nd->name:"Unknown",StringBuf_Length(&buf));
  4565. clif_scriptmenu(sd, st->oid, menu);
  4566. aFree(menu);
  4567. } else
  4568. clif_scriptmenu(sd, st->oid, StringBuf_Value(&buf));
  4569. StringBuf_Destroy(&buf);
  4570. if( sd->npc_menu >= 0xff )
  4571. {
  4572. ShowWarning("buildin_prompt: Too many options specified (current=%d, max=254).\n", sd->npc_menu);
  4573. script_reportsrc(st);
  4574. }
  4575. }
  4576. else if( sd->npc_menu == 0xff )
  4577. {// Cancel was pressed
  4578. sd->state.menu_or_input = 0;
  4579. pc_setreg(sd, add_str("@menu"), 0xff);
  4580. script_pushint(st, 0xff);
  4581. st->state = RUN;
  4582. }
  4583. else
  4584. {// return selected option
  4585. int menu = 0;
  4586. sd->state.menu_or_input = 0;
  4587. for( i = 2; i <= script_lastdata(st); ++i )
  4588. {
  4589. text = script_getstr(st, i);
  4590. sd->npc_menu -= menu_countoptions(text, sd->npc_menu, &menu);
  4591. if( sd->npc_menu <= 0 )
  4592. break;// entry found
  4593. }
  4594. pc_setreg(sd, add_str("@menu"), menu);
  4595. script_pushint(st, menu);
  4596. st->state = RUN;
  4597. }
  4598. return SCRIPT_CMD_SUCCESS;
  4599. }
  4600. /////////////////////////////////////////////////////////////////////
  4601. // ...
  4602. //
  4603. /// Jumps to the target script label.
  4604. ///
  4605. /// goto <label>;
  4606. BUILDIN_FUNC(goto)
  4607. {
  4608. if( !data_islabel(script_getdata(st,2)) )
  4609. {
  4610. ShowError("buildin_goto: Not a label\n");
  4611. script_reportdata(script_getdata(st,2));
  4612. st->state = END;
  4613. return SCRIPT_CMD_FAILURE;
  4614. }
  4615. st->pos = script_getnum(st,2);
  4616. st->state = GOTO;
  4617. return SCRIPT_CMD_SUCCESS;
  4618. }
  4619. /*==========================================
  4620. * user-defined function call
  4621. *------------------------------------------*/
  4622. BUILDIN_FUNC(callfunc)
  4623. {
  4624. int i, j;
  4625. struct script_retinfo* ri;
  4626. struct script_code* scr;
  4627. const char* str = script_getstr(st,2);
  4628. struct reg_db *ref = NULL;
  4629. scr = (struct script_code*)strdb_get(userfunc_db, str);
  4630. if(!scr) {
  4631. ShowError("buildin_callfunc: Function not found! [%s]\n", str);
  4632. st->state = END;
  4633. return SCRIPT_CMD_FAILURE;
  4634. }
  4635. ref = (struct reg_db *)aCalloc(sizeof(struct reg_db), 2);
  4636. ref[0].vars = st->stack->scope.vars;
  4637. if (!st->stack->scope.arrays)
  4638. st->stack->scope.arrays = idb_alloc(DB_OPT_BASE); // TODO: Can this happen? when?
  4639. ref[0].arrays = st->stack->scope.arrays;
  4640. ref[1].vars = st->script->local.vars;
  4641. if (!st->script->local.arrays)
  4642. st->script->local.arrays = idb_alloc(DB_OPT_BASE); // TODO: Can this happen? when?
  4643. ref[1].arrays = st->script->local.arrays;
  4644. for(i = st->start+3, j = 0; i < st->end; i++, j++) {
  4645. struct script_data* data = push_copy(st->stack,i);
  4646. if (data_isreference(data) && !data->ref) {
  4647. const char* name = reference_getname(data);
  4648. if (name[0] == '.')
  4649. data->ref = (name[1] == '@' ? &ref[0] : &ref[1]);
  4650. }
  4651. }
  4652. CREATE(ri, struct script_retinfo, 1);
  4653. ri->script = st->script; // script code
  4654. ri->scope.vars = st->stack->scope.vars; // scope variables
  4655. ri->scope.arrays = st->stack->scope.arrays; // scope arrays
  4656. ri->pos = st->pos; // script location
  4657. ri->nargs = j; // argument count
  4658. ri->defsp = st->stack->defsp; // default stack pointer
  4659. push_retinfo(st->stack, ri, ref);
  4660. st->pos = 0;
  4661. st->script = scr;
  4662. st->stack->defsp = st->stack->sp;
  4663. st->state = GOTO;
  4664. st->stack->scope.vars = i64db_alloc(DB_OPT_RELEASE_DATA);
  4665. st->stack->scope.arrays = idb_alloc(DB_OPT_BASE);
  4666. if (!st->script->local.vars)
  4667. st->script->local.vars = i64db_alloc(DB_OPT_RELEASE_DATA);
  4668. return SCRIPT_CMD_SUCCESS;
  4669. }
  4670. /*==========================================
  4671. * subroutine call
  4672. *------------------------------------------*/
  4673. BUILDIN_FUNC(callsub)
  4674. {
  4675. int i,j;
  4676. struct script_retinfo* ri;
  4677. int pos = script_getnum(st,2);
  4678. struct reg_db *ref = NULL;
  4679. if( !data_islabel(script_getdata(st,2)) && !data_isfunclabel(script_getdata(st,2)) ) {
  4680. ShowError("buildin_callsub: Argument is not a label\n");
  4681. script_reportdata(script_getdata(st,2));
  4682. st->state = END;
  4683. return SCRIPT_CMD_FAILURE;
  4684. }
  4685. ref = (struct reg_db *)aCalloc(sizeof(struct reg_db), 1);
  4686. ref[0].vars = st->stack->scope.vars;
  4687. if (!st->stack->scope.arrays)
  4688. st->stack->scope.arrays = idb_alloc(DB_OPT_BASE); // TODO: Can this happen? when?
  4689. ref[0].arrays = st->stack->scope.arrays;
  4690. for(i = st->start+3, j = 0; i < st->end; i++, j++) {
  4691. struct script_data* data = push_copy(st->stack,i);
  4692. if (data_isreference(data) && !data->ref) {
  4693. const char* name = reference_getname(data);
  4694. if (name[0] == '.' && name[1] == '@')
  4695. data->ref = &ref[0];
  4696. }
  4697. }
  4698. CREATE(ri, struct script_retinfo, 1);
  4699. ri->script = st->script; // script code
  4700. ri->scope.vars = st->stack->scope.vars; // scope variables
  4701. ri->scope.arrays = st->stack->scope.arrays; // scope arrays
  4702. ri->pos = st->pos; // script location
  4703. ri->nargs = j; // argument count
  4704. ri->defsp = st->stack->defsp; // default stack pointer
  4705. push_retinfo(st->stack, ri, ref);
  4706. st->pos = pos;
  4707. st->stack->defsp = st->stack->sp;
  4708. st->state = GOTO;
  4709. st->stack->scope.vars = i64db_alloc(DB_OPT_RELEASE_DATA);
  4710. st->stack->scope.arrays = idb_alloc(DB_OPT_BASE);
  4711. return SCRIPT_CMD_SUCCESS;
  4712. }
  4713. /// Retrieves an argument provided to callfunc/callsub.
  4714. /// If the argument doesn't exist
  4715. ///
  4716. /// getarg(<index>{,<default_value>}) -> <value>
  4717. BUILDIN_FUNC(getarg)
  4718. {
  4719. struct script_retinfo* ri;
  4720. int idx;
  4721. if( st->stack->defsp < 1 || st->stack->stack_data[st->stack->defsp - 1].type != C_RETINFO )
  4722. {
  4723. ShowError("buildin_getarg: No callfunc or callsub!\n");
  4724. st->state = END;
  4725. return SCRIPT_CMD_FAILURE;
  4726. }
  4727. ri = st->stack->stack_data[st->stack->defsp - 1].u.ri;
  4728. idx = script_getnum(st,2);
  4729. if( idx >= 0 && idx < ri->nargs )
  4730. push_copy(st->stack, st->stack->defsp - 1 - ri->nargs + idx);
  4731. else if( script_hasdata(st,3) )
  4732. script_pushcopy(st, 3);
  4733. else
  4734. {
  4735. ShowError("buildin_getarg: Index (idx=%d) out of range (nargs=%d) and no default value found\n", idx, ri->nargs);
  4736. st->state = END;
  4737. return SCRIPT_CMD_FAILURE;
  4738. }
  4739. return SCRIPT_CMD_SUCCESS;
  4740. }
  4741. /// Returns from the current function, optionaly returning a value from the functions.
  4742. /// Don't use outside script functions.
  4743. ///
  4744. /// return;
  4745. /// return <value>;
  4746. BUILDIN_FUNC(return)
  4747. {
  4748. if( script_hasdata(st,2) )
  4749. {// return value
  4750. struct script_data* data;
  4751. script_pushcopy(st, 2);
  4752. data = script_getdatatop(st, -1);
  4753. if( data_isreference(data) ) {
  4754. const char* name = reference_getname(data);
  4755. if( name[0] == '.' && name[1] == '@' ) { // scope variable
  4756. if( !data->ref || data->ref->vars == st->stack->scope.vars )
  4757. get_val(st, data); // current scope, convert to value
  4758. if( data->ref && data->ref->vars == st->stack->stack_data[st->stack->defsp-1].u.ri->scope.vars )
  4759. data->ref = NULL; // Reference to the parent scope, remove reference pointer
  4760. } else if( name[0] == '.' && !data->ref ) { // script variable, link to current script
  4761. data->ref = (struct reg_db *)aCalloc(sizeof(struct reg_db), 1);
  4762. data->ref->vars = st->script->local.vars;
  4763. if (!st->script->local.arrays)
  4764. st->script->local.arrays = idb_alloc(DB_OPT_BASE);
  4765. data->ref->arrays = st->script->local.arrays;
  4766. }
  4767. }
  4768. }
  4769. else
  4770. {// no return value
  4771. script_pushnil(st);
  4772. }
  4773. st->state = RETFUNC;
  4774. return SCRIPT_CMD_SUCCESS;
  4775. }
  4776. /// Returns a random number from 0 to <range>-1.
  4777. /// Or returns a random number from <min> to <max>.
  4778. /// If <min> is greater than <max>, their numbers are switched.
  4779. /// rand(<range>) -> <int>
  4780. /// rand(<min>,<max>) -> <int>
  4781. BUILDIN_FUNC(rand)
  4782. {
  4783. int range;
  4784. int min;
  4785. if( script_hasdata(st,3) )
  4786. {// min,max
  4787. int max = script_getnum(st,3);
  4788. min = script_getnum(st,2);
  4789. if( max < min )
  4790. swap(min, max);
  4791. range = max - min + 1;
  4792. }
  4793. else
  4794. {// range
  4795. min = 0;
  4796. range = script_getnum(st,2);
  4797. }
  4798. if( range <= 1 )
  4799. script_pushint(st, min);
  4800. else
  4801. script_pushint(st, rnd()%range + min);
  4802. return SCRIPT_CMD_SUCCESS;
  4803. }
  4804. /*==========================================
  4805. * Warp sd to str,x,y or Random or SavePoint/Save
  4806. *------------------------------------------*/
  4807. BUILDIN_FUNC(warp)
  4808. {
  4809. int ret;
  4810. int x,y;
  4811. const char* str;
  4812. TBL_PC* sd;
  4813. sd = script_rid2sd(st);
  4814. if( sd == NULL )
  4815. return SCRIPT_CMD_SUCCESS;
  4816. str = script_getstr(st,2);
  4817. x = script_getnum(st,3);
  4818. y = script_getnum(st,4);
  4819. if(strcmp(str,"Random")==0)
  4820. ret = pc_randomwarp(sd,CLR_TELEPORT);
  4821. else if(strcmp(str,"SavePoint")==0 || strcmp(str,"Save")==0)
  4822. ret = pc_setpos(sd,sd->status.save_point.map,sd->status.save_point.x,sd->status.save_point.y,CLR_TELEPORT);
  4823. else
  4824. ret = pc_setpos(sd,mapindex_name2id(str),x,y,CLR_OUTSIGHT);
  4825. if( ret ) {
  4826. ShowError("buildin_warp: moving player '%s' to \"%s\",%d,%d failed.\n", sd->status.name, str, x, y);
  4827. return SCRIPT_CMD_FAILURE;
  4828. }
  4829. return SCRIPT_CMD_SUCCESS;
  4830. }
  4831. /*==========================================
  4832. * Warp a specified area
  4833. *------------------------------------------*/
  4834. static int buildin_areawarp_sub(struct block_list *bl,va_list ap)
  4835. {
  4836. int x2,y2,x3,y3;
  4837. unsigned int index;
  4838. index = va_arg(ap,unsigned int);
  4839. x2 = va_arg(ap,int);
  4840. y2 = va_arg(ap,int);
  4841. x3 = va_arg(ap,int);
  4842. y3 = va_arg(ap,int);
  4843. if(index == 0)
  4844. pc_randomwarp((TBL_PC *)bl,CLR_TELEPORT);
  4845. else if(x3 && y3) {
  4846. int max, tx, ty, j = 0;
  4847. // choose a suitable max number of attempts
  4848. if( (max = (y3-y2+1)*(x3-x2+1)*3) > 1000 )
  4849. max = 1000;
  4850. // find a suitable map cell
  4851. do {
  4852. tx = rnd()%(x3-x2+1)+x2;
  4853. ty = rnd()%(y3-y2+1)+y2;
  4854. j++;
  4855. } while( map_getcell(index,tx,ty,CELL_CHKNOPASS) && j < max );
  4856. pc_setpos((TBL_PC *)bl,index,tx,ty,CLR_OUTSIGHT);
  4857. }
  4858. else
  4859. pc_setpos((TBL_PC *)bl,index,x2,y2,CLR_OUTSIGHT);
  4860. return 0;
  4861. }
  4862. BUILDIN_FUNC(areawarp)
  4863. {
  4864. int16 m, x0,y0,x1,y1, x2,y2,x3=0,y3=0;
  4865. unsigned int index;
  4866. const char *str;
  4867. const char *mapname;
  4868. mapname = script_getstr(st,2);
  4869. x0 = script_getnum(st,3);
  4870. y0 = script_getnum(st,4);
  4871. x1 = script_getnum(st,5);
  4872. y1 = script_getnum(st,6);
  4873. str = script_getstr(st,7);
  4874. x2 = script_getnum(st,8);
  4875. y2 = script_getnum(st,9);
  4876. if( script_hasdata(st,10) && script_hasdata(st,11) ) { // Warp area to area
  4877. if( (x3 = script_getnum(st,10)) < 0 || (y3 = script_getnum(st,11)) < 0 ){
  4878. x3 = 0;
  4879. y3 = 0;
  4880. } else if( x3 && y3 ) {
  4881. // normalize x3/y3 coordinates
  4882. if( x3 < x2 ) swap(x3,x2);
  4883. if( y3 < y2 ) swap(y3,y2);
  4884. }
  4885. }
  4886. if( (m = map_mapname2mapid(mapname)) < 0 )
  4887. return SCRIPT_CMD_FAILURE;
  4888. if( strcmp(str,"Random") == 0 )
  4889. index = 0;
  4890. else if( !(index=mapindex_name2id(str)) )
  4891. return SCRIPT_CMD_FAILURE;
  4892. map_foreachinarea(buildin_areawarp_sub, m,x0,y0,x1,y1, BL_PC, index,x2,y2,x3,y3);
  4893. return SCRIPT_CMD_SUCCESS;
  4894. }
  4895. /*==========================================
  4896. * areapercentheal <map>,<x1>,<y1>,<x2>,<y2>,<hp>,<sp>
  4897. *------------------------------------------*/
  4898. static int buildin_areapercentheal_sub(struct block_list *bl,va_list ap)
  4899. {
  4900. int hp, sp;
  4901. hp = va_arg(ap, int);
  4902. sp = va_arg(ap, int);
  4903. pc_percentheal((TBL_PC *)bl,hp,sp);
  4904. return 0;
  4905. }
  4906. BUILDIN_FUNC(areapercentheal)
  4907. {
  4908. int hp,sp,m;
  4909. const char *mapname;
  4910. int x0,y0,x1,y1;
  4911. mapname=script_getstr(st,2);
  4912. x0=script_getnum(st,3);
  4913. y0=script_getnum(st,4);
  4914. x1=script_getnum(st,5);
  4915. y1=script_getnum(st,6);
  4916. hp=script_getnum(st,7);
  4917. sp=script_getnum(st,8);
  4918. if( (m=map_mapname2mapid(mapname))< 0)
  4919. return SCRIPT_CMD_FAILURE;
  4920. map_foreachinarea(buildin_areapercentheal_sub,m,x0,y0,x1,y1,BL_PC,hp,sp);
  4921. return SCRIPT_CMD_SUCCESS;
  4922. }
  4923. /*==========================================
  4924. * warpchar [LuzZza]
  4925. * Useful for warp one player from
  4926. * another player npc-session.
  4927. * Using: warpchar "mapname",x,y,Char_ID;
  4928. *------------------------------------------*/
  4929. BUILDIN_FUNC(warpchar)
  4930. {
  4931. int x,y,a;
  4932. const char *str;
  4933. TBL_PC *sd;
  4934. str=script_getstr(st,2);
  4935. x=script_getnum(st,3);
  4936. y=script_getnum(st,4);
  4937. a=script_getnum(st,5);
  4938. sd = map_charid2sd(a);
  4939. if( sd == NULL )
  4940. return SCRIPT_CMD_SUCCESS;
  4941. if(strcmp(str, "Random") == 0)
  4942. pc_randomwarp(sd, CLR_TELEPORT);
  4943. else
  4944. if(strcmp(str, "SavePoint") == 0)
  4945. pc_setpos(sd, sd->status.save_point.map,sd->status.save_point.x, sd->status.save_point.y, CLR_TELEPORT);
  4946. else
  4947. pc_setpos(sd, mapindex_name2id(str), x, y, CLR_TELEPORT);
  4948. return SCRIPT_CMD_SUCCESS;
  4949. }
  4950. /*==========================================
  4951. * Warpparty - [Fredzilla] [Paradox924X]
  4952. * Syntax: warpparty "to_mapname",x,y,Party_ID,{"from_mapname"};
  4953. * If 'from_mapname' is specified, only the party members on that map will be warped
  4954. *------------------------------------------*/
  4955. BUILDIN_FUNC(warpparty)
  4956. {
  4957. TBL_PC *sd = NULL;
  4958. TBL_PC *pl_sd;
  4959. struct party_data* p;
  4960. int type;
  4961. int mapindex;
  4962. int i;
  4963. const char* str = script_getstr(st,2);
  4964. int x = script_getnum(st,3);
  4965. int y = script_getnum(st,4);
  4966. int p_id = script_getnum(st,5);
  4967. const char* str2 = NULL;
  4968. if ( script_hasdata(st,6) )
  4969. str2 = script_getstr(st,6);
  4970. p = party_search(p_id);
  4971. if(!p)
  4972. return SCRIPT_CMD_SUCCESS;
  4973. type = ( strcmp(str,"Random")==0 ) ? 0
  4974. : ( strcmp(str,"SavePointAll")==0 ) ? 1
  4975. : ( strcmp(str,"SavePoint")==0 ) ? 2
  4976. : ( strcmp(str,"Leader")==0 ) ? 3
  4977. : 4;
  4978. switch (type)
  4979. {
  4980. case 3:
  4981. for(i = 0; i < MAX_PARTY && !p->party.member[i].leader; i++);
  4982. if (i == MAX_PARTY || !p->data[i].sd) //Leader not found / not online
  4983. return SCRIPT_CMD_FAILURE;
  4984. pl_sd = p->data[i].sd;
  4985. mapindex = pl_sd->mapindex;
  4986. x = pl_sd->bl.x;
  4987. y = pl_sd->bl.y;
  4988. break;
  4989. case 4:
  4990. mapindex = mapindex_name2id(str);
  4991. break;
  4992. case 2:
  4993. //"SavePoint" uses save point of the currently attached player
  4994. if (( sd = script_rid2sd(st) ) == NULL )
  4995. return SCRIPT_CMD_SUCCESS;
  4996. default:
  4997. mapindex = 0;
  4998. break;
  4999. }
  5000. for (i = 0; i < MAX_PARTY; i++)
  5001. {
  5002. if( !(pl_sd = p->data[i].sd) || pl_sd->status.party_id != p_id )
  5003. continue;
  5004. if( str2 && strcmp(str2, map[pl_sd->bl.m].name) != 0 )
  5005. continue;
  5006. if( pc_isdead(pl_sd) )
  5007. continue;
  5008. switch( type )
  5009. {
  5010. case 0: // Random
  5011. if(!map[pl_sd->bl.m].flag.nowarp)
  5012. pc_randomwarp(pl_sd,CLR_TELEPORT);
  5013. break;
  5014. case 1: // SavePointAll
  5015. if(!map[pl_sd->bl.m].flag.noreturn)
  5016. pc_setpos(pl_sd,pl_sd->status.save_point.map,pl_sd->status.save_point.x,pl_sd->status.save_point.y,CLR_TELEPORT);
  5017. break;
  5018. case 2: // SavePoint
  5019. if(!map[pl_sd->bl.m].flag.noreturn)
  5020. pc_setpos(pl_sd,sd->status.save_point.map,sd->status.save_point.x,sd->status.save_point.y,CLR_TELEPORT);
  5021. break;
  5022. case 3: // Leader
  5023. case 4: // m,x,y
  5024. if(!map[pl_sd->bl.m].flag.noreturn && !map[pl_sd->bl.m].flag.nowarp)
  5025. pc_setpos(pl_sd,mapindex,x,y,CLR_TELEPORT);
  5026. break;
  5027. }
  5028. }
  5029. return SCRIPT_CMD_SUCCESS;
  5030. }
  5031. /*==========================================
  5032. * Warpguild - [Fredzilla]
  5033. * Syntax: warpguild "mapname",x,y,Guild_ID;
  5034. *------------------------------------------*/
  5035. BUILDIN_FUNC(warpguild)
  5036. {
  5037. TBL_PC *sd = NULL;
  5038. TBL_PC *pl_sd;
  5039. struct guild* g;
  5040. struct s_mapiterator* iter;
  5041. int type;
  5042. const char* str = script_getstr(st,2);
  5043. int x = script_getnum(st,3);
  5044. int y = script_getnum(st,4);
  5045. int gid = script_getnum(st,5);
  5046. g = guild_search(gid);
  5047. if( g == NULL )
  5048. return SCRIPT_CMD_SUCCESS;
  5049. type = ( strcmp(str,"Random")==0 ) ? 0
  5050. : ( strcmp(str,"SavePointAll")==0 ) ? 1
  5051. : ( strcmp(str,"SavePoint")==0 ) ? 2
  5052. : 3;
  5053. if( type == 2 && ( sd = script_rid2sd(st) ) == NULL )
  5054. {// "SavePoint" uses save point of the currently attached player
  5055. return SCRIPT_CMD_SUCCESS;
  5056. }
  5057. iter = mapit_getallusers();
  5058. for( pl_sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); pl_sd = (TBL_PC*)mapit_next(iter) )
  5059. {
  5060. if( pl_sd->status.guild_id != gid )
  5061. continue;
  5062. switch( type )
  5063. {
  5064. case 0: // Random
  5065. if(!map[pl_sd->bl.m].flag.nowarp)
  5066. pc_randomwarp(pl_sd,CLR_TELEPORT);
  5067. break;
  5068. case 1: // SavePointAll
  5069. if(!map[pl_sd->bl.m].flag.noreturn)
  5070. pc_setpos(pl_sd,pl_sd->status.save_point.map,pl_sd->status.save_point.x,pl_sd->status.save_point.y,CLR_TELEPORT);
  5071. break;
  5072. case 2: // SavePoint
  5073. if(!map[pl_sd->bl.m].flag.noreturn)
  5074. pc_setpos(pl_sd,sd->status.save_point.map,sd->status.save_point.x,sd->status.save_point.y,CLR_TELEPORT);
  5075. break;
  5076. case 3: // m,x,y
  5077. if(!map[pl_sd->bl.m].flag.noreturn && !map[pl_sd->bl.m].flag.nowarp)
  5078. pc_setpos(pl_sd,mapindex_name2id(str),x,y,CLR_TELEPORT);
  5079. break;
  5080. }
  5081. }
  5082. mapit_free(iter);
  5083. return SCRIPT_CMD_SUCCESS;
  5084. }
  5085. /*==========================================
  5086. * Force Heal a player (hp and sp)
  5087. *------------------------------------------*/
  5088. BUILDIN_FUNC(heal)
  5089. {
  5090. TBL_PC *sd;
  5091. int hp,sp;
  5092. if (!script_charid2sd(4,sd))
  5093. return SCRIPT_CMD_SUCCESS;
  5094. hp=script_getnum(st,2);
  5095. sp=script_getnum(st,3);
  5096. status_heal(&sd->bl, hp, sp, 1);
  5097. return SCRIPT_CMD_SUCCESS;
  5098. }
  5099. /*==========================================
  5100. * Heal a player by item (get vit bonus etc)
  5101. *------------------------------------------*/
  5102. BUILDIN_FUNC(itemheal)
  5103. {
  5104. TBL_PC *sd;
  5105. int hp,sp;
  5106. hp=script_getnum(st,2);
  5107. sp=script_getnum(st,3);
  5108. if(potion_flag==1) {
  5109. potion_hp = hp;
  5110. potion_sp = sp;
  5111. return SCRIPT_CMD_SUCCESS;
  5112. }
  5113. if (!script_charid2sd(4,sd))
  5114. return SCRIPT_CMD_SUCCESS;
  5115. pc_itemheal(sd,sd->itemid,hp,sp);
  5116. return SCRIPT_CMD_SUCCESS;
  5117. }
  5118. /*==========================================
  5119. *
  5120. *------------------------------------------*/
  5121. BUILDIN_FUNC(percentheal)
  5122. {
  5123. int hp,sp;
  5124. TBL_PC* sd;
  5125. hp=script_getnum(st,2);
  5126. sp=script_getnum(st,3);
  5127. if(potion_flag==1) {
  5128. potion_per_hp = hp;
  5129. potion_per_sp = sp;
  5130. return SCRIPT_CMD_SUCCESS;
  5131. }
  5132. if (!script_charid2sd(4,sd))
  5133. return SCRIPT_CMD_SUCCESS;
  5134. #ifdef RENEWAL
  5135. if( sd->sc.data[SC_EXTREMITYFIST2] )
  5136. sp = 0;
  5137. #endif
  5138. if (sd->sc.data[SC_NORECOVER_STATE]) {
  5139. hp = 0;
  5140. sp = 0;
  5141. }
  5142. if (sd->sc.data[SC_BITESCAR])
  5143. hp = 0;
  5144. pc_percentheal(sd,hp,sp);
  5145. return SCRIPT_CMD_SUCCESS;
  5146. }
  5147. /*==========================================
  5148. *
  5149. *------------------------------------------*/
  5150. BUILDIN_FUNC(jobchange)
  5151. {
  5152. int job, upper=-1;
  5153. job=script_getnum(st,2);
  5154. if( script_hasdata(st,3) )
  5155. upper=script_getnum(st,3);
  5156. if (pcdb_checkid(job))
  5157. {
  5158. TBL_PC* sd;
  5159. if (!script_charid2sd(4,sd))
  5160. return SCRIPT_CMD_SUCCESS;
  5161. pc_jobchange(sd, job, upper);
  5162. }
  5163. return SCRIPT_CMD_SUCCESS;
  5164. }
  5165. /*==========================================
  5166. *
  5167. *------------------------------------------*/
  5168. BUILDIN_FUNC(jobname)
  5169. {
  5170. int class_=script_getnum(st,2);
  5171. script_pushconststr(st, (char*)job_name(class_));
  5172. return SCRIPT_CMD_SUCCESS;
  5173. }
  5174. /// Get input from the player.
  5175. /// For numeric inputs the value is capped to the range [min,max]. Returns 1 if
  5176. /// the value was higher than 'max', -1 if lower than 'min' and 0 otherwise.
  5177. /// For string inputs it returns 1 if the string was longer than 'max', -1 is
  5178. /// shorter than 'min' and 0 otherwise.
  5179. ///
  5180. /// input(<var>{,<min>{,<max>}}) -> <int>
  5181. BUILDIN_FUNC(input)
  5182. {
  5183. TBL_PC* sd;
  5184. struct script_data* data;
  5185. int64 uid;
  5186. const char* name;
  5187. int min;
  5188. int max;
  5189. sd = script_rid2sd(st);
  5190. if( sd == NULL )
  5191. return SCRIPT_CMD_SUCCESS;
  5192. data = script_getdata(st,2);
  5193. if( !data_isreference(data) ){
  5194. ShowError("script:input: not a variable\n");
  5195. script_reportdata(data);
  5196. st->state = END;
  5197. return SCRIPT_CMD_FAILURE;
  5198. }
  5199. uid = reference_getuid(data);
  5200. name = reference_getname(data);
  5201. min = (script_hasdata(st,3) ? script_getnum(st,3) : script_config.input_min_value);
  5202. max = (script_hasdata(st,4) ? script_getnum(st,4) : script_config.input_max_value);
  5203. #ifdef SECURE_NPCTIMEOUT
  5204. sd->npc_idle_type = NPCT_WAIT;
  5205. #endif
  5206. if( !sd->state.menu_or_input )
  5207. { // first invocation, display npc input box
  5208. sd->state.menu_or_input = 1;
  5209. st->state = RERUNLINE;
  5210. if( is_string_variable(name) )
  5211. clif_scriptinputstr(sd,st->oid);
  5212. else
  5213. clif_scriptinput(sd,st->oid);
  5214. }
  5215. else
  5216. { // take received text/value and store it in the designated variable
  5217. sd->state.menu_or_input = 0;
  5218. if( is_string_variable(name) )
  5219. {
  5220. int len = (int)strlen(sd->npc_str);
  5221. set_reg(st, sd, uid, name, (void*)sd->npc_str, script_getref(st,2));
  5222. script_pushint(st, (len > max ? 1 : len < min ? -1 : 0));
  5223. }
  5224. else
  5225. {
  5226. int amount = sd->npc_amount;
  5227. set_reg(st, sd, uid, name, (void*)__64BPRTSIZE(cap_value(amount,min,max)), script_getref(st,2));
  5228. script_pushint(st, (amount > max ? 1 : amount < min ? -1 : 0));
  5229. }
  5230. st->state = RUN;
  5231. }
  5232. return SCRIPT_CMD_SUCCESS;
  5233. }
  5234. // declare the copyarray method here for future reference
  5235. BUILDIN_FUNC(copyarray);
  5236. /// Sets the value of a variable.
  5237. /// The value is converted to the type of the variable.
  5238. ///
  5239. /// set(<variable>,<value>{,<charid>})
  5240. BUILDIN_FUNC(set)
  5241. {
  5242. TBL_PC* sd = NULL;
  5243. struct script_data* data;
  5244. //struct script_data* datavalue;
  5245. int64 num;
  5246. const char* name;
  5247. char prefix;
  5248. data = script_getdata(st,2);
  5249. //datavalue = script_getdata(st,3);
  5250. if( !data_isreference(data) )
  5251. {
  5252. ShowError("script:set: not a variable\n");
  5253. script_reportdata(script_getdata(st,2));
  5254. st->state = END;
  5255. return SCRIPT_CMD_FAILURE;
  5256. }
  5257. num = reference_getuid(data);
  5258. name = reference_getname(data);
  5259. prefix = *name;
  5260. if (not_server_variable(prefix) && !script_charid2sd(4,sd)) {
  5261. ShowError("buildin_set: No player attached for player variable '%s'\n", name);
  5262. return SCRIPT_CMD_FAILURE;
  5263. }
  5264. #if 0
  5265. if( data_isreference(datavalue) )
  5266. {// the value being referenced is a variable
  5267. const char* namevalue = reference_getname(datavalue);
  5268. if( !not_array_variable(*namevalue) )
  5269. {// array variable being copied into another array variable
  5270. if( sd == NULL && not_server_variable(*namevalue) && !(sd = script_rid2sd(st)) )
  5271. {// player must be attached in order to copy a player variable
  5272. ShowError("script:set: no player attached for player variable '%s'\n", namevalue);
  5273. return SCRIPT_CMD_SUCCESS;
  5274. }
  5275. if( is_string_variable(namevalue) != is_string_variable(name) )
  5276. {// non-matching array value types
  5277. ShowWarning("script:set: two array variables do not match in type.\n");
  5278. return SCRIPT_CMD_SUCCESS;
  5279. }
  5280. // push the maximum number of array values to the stack
  5281. push_val(st->stack, C_INT, SCRIPT_MAX_ARRAYSIZE);
  5282. // call the copy array method directly
  5283. return buildin_copyarray(st);
  5284. }
  5285. }
  5286. #endif
  5287. if( is_string_variable(name) )
  5288. set_reg(st,sd,num,name,(void*)script_getstr(st,3),script_getref(st,2));
  5289. else
  5290. set_reg(st,sd,num,name,(void*)__64BPRTSIZE(script_getnum(st,3)),script_getref(st,2));
  5291. // return a copy of the variable reference
  5292. script_pushcopy(st,2);
  5293. return SCRIPT_CMD_SUCCESS;
  5294. }
  5295. /////////////////////////////////////////////////////////////////////
  5296. /// Array variables
  5297. ///
  5298. /// Sets values of an array, from the starting index.
  5299. /// ex: setarray arr[1],1,2,3;
  5300. ///
  5301. /// setarray <array variable>,<value1>{,<value2>...};
  5302. BUILDIN_FUNC(setarray)
  5303. {
  5304. struct script_data* data;
  5305. const char* name;
  5306. uint32 start;
  5307. uint32 end;
  5308. int32 id;
  5309. int32 i;
  5310. TBL_PC* sd = NULL;
  5311. data = script_getdata(st, 2);
  5312. if( !data_isreference(data) )
  5313. {
  5314. ShowError("script:setarray: not a variable\n");
  5315. script_reportdata(data);
  5316. st->state = END;
  5317. return SCRIPT_CMD_FAILURE;// not a variable
  5318. }
  5319. id = reference_getid(data);
  5320. start = reference_getindex(data);
  5321. name = reference_getname(data);
  5322. if( not_server_variable(*name) )
  5323. {
  5324. sd = script_rid2sd(st);
  5325. if( sd == NULL )
  5326. return SCRIPT_CMD_SUCCESS;// no player attached
  5327. }
  5328. end = start + script_lastdata(st) - 2;
  5329. if( end > SCRIPT_MAX_ARRAYSIZE )
  5330. end = SCRIPT_MAX_ARRAYSIZE;
  5331. if( is_string_variable(name) )
  5332. {// string array
  5333. for( i = 3; start < end; ++start, ++i )
  5334. set_reg(st, sd, reference_uid(id, start), name, (void*)script_getstr(st,i), reference_getref(data));
  5335. }
  5336. else
  5337. {// int array
  5338. for( i = 3; start < end; ++start, ++i )
  5339. set_reg(st, sd, reference_uid(id, start), name, (void*)__64BPRTSIZE(script_getnum(st,i)), reference_getref(data));
  5340. }
  5341. return SCRIPT_CMD_SUCCESS;
  5342. }
  5343. /// Sets count values of an array, from the starting index.
  5344. /// ex: cleararray arr[0],0,1;
  5345. ///
  5346. /// cleararray <array variable>,<value>,<count>;
  5347. BUILDIN_FUNC(cleararray)
  5348. {
  5349. struct script_data* data;
  5350. const char* name;
  5351. uint32 start;
  5352. uint32 end;
  5353. int32 id;
  5354. void* v;
  5355. TBL_PC* sd = NULL;
  5356. data = script_getdata(st, 2);
  5357. if( !data_isreference(data) )
  5358. {
  5359. ShowError("script:cleararray: not a variable\n");
  5360. script_reportdata(data);
  5361. st->state = END;
  5362. return SCRIPT_CMD_FAILURE;// not a variable
  5363. }
  5364. id = reference_getid(data);
  5365. start = reference_getindex(data);
  5366. name = reference_getname(data);
  5367. if( not_server_variable(*name) )
  5368. {
  5369. sd = script_rid2sd(st);
  5370. if( sd == NULL )
  5371. return SCRIPT_CMD_SUCCESS;// no player attached
  5372. }
  5373. if( is_string_variable(name) )
  5374. v = (void*)script_getstr(st, 3);
  5375. else
  5376. v = (void*)__64BPRTSIZE(script_getnum(st, 3));
  5377. end = start + script_getnum(st, 4);
  5378. if( end > SCRIPT_MAX_ARRAYSIZE )
  5379. end = SCRIPT_MAX_ARRAYSIZE;
  5380. for( ; start < end; ++start )
  5381. set_reg(st, sd, reference_uid(id, start), name, v, script_getref(st,2));
  5382. return SCRIPT_CMD_SUCCESS;
  5383. }
  5384. /// Copies data from one array to another.
  5385. /// ex: copyarray arr[0],arr[2],2;
  5386. ///
  5387. /// copyarray <destination array variable>,<source array variable>,<count>;
  5388. BUILDIN_FUNC(copyarray)
  5389. {
  5390. struct script_data* data1;
  5391. struct script_data* data2;
  5392. const char* name1;
  5393. const char* name2;
  5394. int32 idx1;
  5395. int32 idx2;
  5396. int32 id1;
  5397. int32 id2;
  5398. void* v;
  5399. int32 i;
  5400. uint32 count;
  5401. TBL_PC* sd = NULL;
  5402. data1 = script_getdata(st, 2);
  5403. data2 = script_getdata(st, 3);
  5404. if( !data_isreference(data1) || !data_isreference(data2) )
  5405. {
  5406. ShowError("script:copyarray: not a variable\n");
  5407. script_reportdata(data1);
  5408. script_reportdata(data2);
  5409. st->state = END;
  5410. return SCRIPT_CMD_FAILURE;// not a variable
  5411. }
  5412. id1 = reference_getid(data1);
  5413. id2 = reference_getid(data2);
  5414. idx1 = reference_getindex(data1);
  5415. idx2 = reference_getindex(data2);
  5416. name1 = reference_getname(data1);
  5417. name2 = reference_getname(data2);
  5418. if( is_string_variable(name1) != is_string_variable(name2) )
  5419. {
  5420. ShowError("script:copyarray: type mismatch\n");
  5421. script_reportdata(data1);
  5422. script_reportdata(data2);
  5423. st->state = END;
  5424. return SCRIPT_CMD_FAILURE;// data type mismatch
  5425. }
  5426. if( not_server_variable(*name1) || not_server_variable(*name2) )
  5427. {
  5428. sd = script_rid2sd(st);
  5429. if( sd == NULL )
  5430. return SCRIPT_CMD_SUCCESS;// no player attached
  5431. }
  5432. count = script_getnum(st, 4);
  5433. if( count > SCRIPT_MAX_ARRAYSIZE - idx1 )
  5434. count = SCRIPT_MAX_ARRAYSIZE - idx1;
  5435. if( count <= 0 || (id1 == id2 && idx1 == idx2) )
  5436. return SCRIPT_CMD_SUCCESS;// nothing to copy
  5437. if( id1 == id2 && idx1 > idx2 )
  5438. {// destination might be overlapping the source - copy in reverse order
  5439. for( i = count - 1; i >= 0; --i )
  5440. {
  5441. v = get_val2(st, reference_uid(id2, idx2 + i), reference_getref(data2));
  5442. set_reg(st, sd, reference_uid(id1, idx1 + i), name1, v, reference_getref(data1));
  5443. script_removetop(st, -1, 0);
  5444. }
  5445. }
  5446. else
  5447. {// normal copy
  5448. for( i = 0; i < count; ++i )
  5449. {
  5450. if( idx2 + i < SCRIPT_MAX_ARRAYSIZE )
  5451. {
  5452. v = get_val2(st, reference_uid(id2, idx2 + i), reference_getref(data2));
  5453. set_reg(st, sd, reference_uid(id1, idx1 + i), name1, v, reference_getref(data1));
  5454. script_removetop(st, -1, 0);
  5455. }
  5456. else// out of range - assume ""/0
  5457. set_reg(st, sd, reference_uid(id1, idx1 + i), name1, (is_string_variable(name1)?(void*)"":(void*)0), reference_getref(data1));
  5458. }
  5459. }
  5460. return SCRIPT_CMD_SUCCESS;
  5461. }
  5462. /// Returns the size of the array.
  5463. /// Assumes that everything before the starting index exists.
  5464. /// ex: getarraysize(arr[3])
  5465. ///
  5466. /// getarraysize(<array variable>) -> <int>
  5467. BUILDIN_FUNC(getarraysize)
  5468. {
  5469. struct script_data* data;
  5470. data = script_getdata(st, 2);
  5471. if( !data_isreference(data) )
  5472. {
  5473. ShowError("script:getarraysize: not a variable\n");
  5474. script_reportdata(data);
  5475. script_pushnil(st);
  5476. st->state = END;
  5477. return SCRIPT_CMD_FAILURE;// not a variable
  5478. }
  5479. script_pushint(st, script_array_highest_key(st, st->rid ? script_rid2sd(st) : NULL, reference_getname(data), reference_getref(data)));
  5480. return SCRIPT_CMD_SUCCESS;
  5481. }
  5482. int script_array_index_cmp(const void *a, const void *b) {
  5483. return ( *(unsigned int*)a - *(unsigned int*)b );
  5484. }
  5485. /// Deletes count or all the elements in an array, from the starting index.
  5486. /// ex: deletearray arr[4],2;
  5487. ///
  5488. /// deletearray <array variable>;
  5489. /// deletearray <array variable>,<count>;
  5490. BUILDIN_FUNC(deletearray)
  5491. {
  5492. struct script_data* data;
  5493. const char* name;
  5494. unsigned int start, end, i;
  5495. int id;
  5496. TBL_PC *sd = NULL;
  5497. struct script_array *sa = NULL;
  5498. struct reg_db *src = NULL;
  5499. void *value;
  5500. data = script_getdata(st, 2);
  5501. if( !data_isreference(data) ) {
  5502. ShowError("script:deletearray: not a variable\n");
  5503. script_reportdata(data);
  5504. st->state = END;
  5505. return SCRIPT_CMD_FAILURE;// not a variable
  5506. }
  5507. id = reference_getid(data);
  5508. start = reference_getindex(data);
  5509. name = reference_getname(data);
  5510. if( not_server_variable(*name) ) {
  5511. sd = script_rid2sd(st);
  5512. if( sd == NULL )
  5513. return SCRIPT_CMD_SUCCESS;// no player attached
  5514. }
  5515. if (!(src = script_array_src(st, sd, name, reference_getref(data)))) {
  5516. ShowError("script:deletearray: not a array\n");
  5517. script_reportdata(data);
  5518. st->state = END;
  5519. return SCRIPT_CMD_FAILURE;// not a variable
  5520. }
  5521. script_array_ensure_zero(st, NULL, data->u.num, reference_getref(data));
  5522. if ( !(sa = idb_get(src->arrays, id)) ) { // non-existent array, nothing to empty
  5523. return SCRIPT_CMD_SUCCESS;// not a variable
  5524. }
  5525. end = script_array_highest_key(st, sd, name, reference_getref(data));
  5526. if( start >= end )
  5527. return SCRIPT_CMD_SUCCESS;// nothing to free
  5528. if( is_string_variable(name) )
  5529. value = (void *)"";
  5530. else
  5531. value = (void *)0;
  5532. if( script_hasdata(st,3) ) {
  5533. unsigned int count = script_getnum(st, 3);
  5534. if( count > end - start )
  5535. count = end - start;
  5536. if( count <= 0 )
  5537. return SCRIPT_CMD_SUCCESS;// nothing to free
  5538. if( end - start < sa->size ) {
  5539. // Better to iterate directly on the array, no speed-up from using sa
  5540. for( ; start + count < end; ++start ) {
  5541. // Compact and overwrite
  5542. void* v = get_val2(st, reference_uid(id, start + count), reference_getref(data));
  5543. set_reg(st, sd, reference_uid(id, start), name, v, reference_getref(data));
  5544. script_removetop(st, -1, 0);
  5545. }
  5546. for( ; start < end; start++ ) {
  5547. // Clean up any leftovers that weren't overwritten
  5548. set_reg(st, sd, reference_uid(id, start), name, value, reference_getref(data));
  5549. }
  5550. } else {
  5551. // using sa to speed up
  5552. unsigned int *list = NULL, size = 0;
  5553. list = script_array_cpy_list(sa);
  5554. size = sa->size;
  5555. qsort(list, size, sizeof(unsigned int), script_array_index_cmp);
  5556. ARR_FIND(0, size, i, list[i] >= start);
  5557. for( ; i < size && list[i] < start + count; i++ ) {
  5558. // Clear any entries between start and start+count, if they exist
  5559. set_reg(st, sd, reference_uid(id, list[i]), name, value, reference_getref(data));
  5560. }
  5561. for( ; i < size && list[i] < end; i++ ) {
  5562. // Move back count positions any entries between start+count to fill the gaps
  5563. void* v = get_val2(st, reference_uid(id, list[i]), reference_getref(data));
  5564. set_reg(st, sd, reference_uid(id, list[i]-count), name, v, reference_getref(data));
  5565. script_removetop(st, -1, 0);
  5566. // Clear their originals
  5567. set_reg(st, sd, reference_uid(id, list[i]), name, value, reference_getref(data));
  5568. }
  5569. }
  5570. } else {
  5571. unsigned int *list = NULL, size = 0;
  5572. list = script_array_cpy_list(sa);
  5573. size = sa->size;
  5574. for(i = 0; i < size; i++) {
  5575. if( list[i] >= start ) // Less expensive than sorting it, most likely
  5576. set_reg(st, sd, reference_uid(id, list[i]), name, value, reference_getref(data));
  5577. }
  5578. }
  5579. return SCRIPT_CMD_SUCCESS;
  5580. }
  5581. /// Returns a reference to the target index of the array variable.
  5582. /// Equivalent to var[index].
  5583. ///
  5584. /// getelementofarray(<array variable>,<index>) -> <variable reference>
  5585. BUILDIN_FUNC(getelementofarray)
  5586. {
  5587. struct script_data* data;
  5588. int32 id;
  5589. int64 i;
  5590. data = script_getdata(st, 2);
  5591. if( !data_isreference(data) )
  5592. {
  5593. ShowError("script:getelementofarray: not a variable\n");
  5594. script_reportdata(data);
  5595. script_pushnil(st);
  5596. st->state = END;
  5597. return SCRIPT_CMD_SUCCESS;// not a variable
  5598. }
  5599. id = reference_getid(data);
  5600. i = script_getnum(st, 3);
  5601. if (i < 0 || i >= SCRIPT_MAX_ARRAYSIZE) {
  5602. ShowWarning("script:getelementofarray: index out of range (%"PRId64")\n", i);
  5603. script_reportdata(data);
  5604. script_pushnil(st);
  5605. st->state = END;
  5606. return SCRIPT_CMD_FAILURE;// out of range
  5607. }
  5608. push_val2(st->stack, C_NAME, reference_uid(id, i), reference_getref(data));
  5609. return SCRIPT_CMD_SUCCESS;
  5610. }
  5611. /////////////////////////////////////////////////////////////////////
  5612. /// ...
  5613. ///
  5614. /*==========================================
  5615. *
  5616. *------------------------------------------*/
  5617. BUILDIN_FUNC(setlook)
  5618. {
  5619. int type,val;
  5620. TBL_PC* sd;
  5621. type = script_getnum(st,2);
  5622. val = script_getnum(st,3);
  5623. if (!script_charid2sd(4,sd))
  5624. return SCRIPT_CMD_SUCCESS;
  5625. pc_changelook(sd,type,val);
  5626. return SCRIPT_CMD_SUCCESS;
  5627. }
  5628. BUILDIN_FUNC(changelook)
  5629. { // As setlook but only client side
  5630. int type,val;
  5631. TBL_PC* sd;
  5632. type = script_getnum(st,2);
  5633. val = script_getnum(st,3);
  5634. if (!script_charid2sd(4,sd))
  5635. return SCRIPT_CMD_SUCCESS;
  5636. clif_changelook(&sd->bl,type,val);
  5637. return SCRIPT_CMD_SUCCESS;
  5638. }
  5639. /*==========================================
  5640. *
  5641. *------------------------------------------*/
  5642. BUILDIN_FUNC(cutin)
  5643. {
  5644. TBL_PC* sd;
  5645. sd = script_rid2sd(st);
  5646. if( sd == NULL )
  5647. return SCRIPT_CMD_SUCCESS;
  5648. clif_cutin(sd,script_getstr(st,2),script_getnum(st,3));
  5649. return SCRIPT_CMD_SUCCESS;
  5650. }
  5651. /*==========================================
  5652. *
  5653. *------------------------------------------*/
  5654. BUILDIN_FUNC(viewpoint)
  5655. {
  5656. int type,x,y,id,color;
  5657. TBL_PC* sd;
  5658. type=script_getnum(st,2);
  5659. x=script_getnum(st,3);
  5660. y=script_getnum(st,4);
  5661. id=script_getnum(st,5);
  5662. color=script_getnum(st,6);
  5663. sd = script_rid2sd(st);
  5664. if( sd == NULL )
  5665. return SCRIPT_CMD_SUCCESS;
  5666. clif_viewpoint(sd,st->oid,type,x,y,id,color);
  5667. return SCRIPT_CMD_SUCCESS;
  5668. }
  5669. /// Returns number of items in inventory/cart/storage
  5670. /// countitem <nameID>{,<accountID>});
  5671. /// countitem2 <nameID>,<Identified>,<Refine>,<Attribute>,<Card0>,<Card1>,<Card2>,<Card3>{,<accountID>}) [Lupus]
  5672. /// cartcountitem <nameID>{,<accountID>});
  5673. /// cartcountitem2 <nameID>,<Identified>,<Refine>,<Attribute>,<Card0>,<Card1>,<Card2>,<Card3>{,<accountID>})
  5674. /// storagecountitem <nameID>{,<accountID>});
  5675. /// storagecountitem2 <nameID>,<Identified>,<Refine>,<Attribute>,<Card0>,<Card1>,<Card2>,<Card3>{,<accountID>})
  5676. BUILDIN_FUNC(countitem)
  5677. {
  5678. int i = 0, aid = 3;
  5679. struct item_data* id = NULL;
  5680. struct script_data* data;
  5681. char *command = (char *)script_getfuncname(st);
  5682. uint8 loc = 0;
  5683. uint16 size, count = 0;
  5684. struct item *items;
  5685. TBL_PC *sd = NULL;
  5686. if( command[strlen(command)-1] == '2' ) {
  5687. i = 1;
  5688. aid = 10;
  5689. }
  5690. if( script_hasdata(st,aid) ) {
  5691. if( !(sd = map_id2sd( (aid = script_getnum(st, aid)) )) ) {
  5692. ShowError("buildin_%s: player not found (AID=%d).\n", command, aid);
  5693. st->state = END;
  5694. return SCRIPT_CMD_FAILURE;
  5695. }
  5696. }
  5697. else {
  5698. if( !(sd = script_rid2sd(st)) )
  5699. return SCRIPT_CMD_SUCCESS;
  5700. }
  5701. if( !strncmp(command, "cart", 4) ) {
  5702. loc = 1;
  5703. size = MAX_CART;
  5704. items = sd->status.cart;
  5705. }
  5706. else if( !strncmp(command, "storage", 7) ) {
  5707. loc = 2;
  5708. size = MAX_STORAGE;
  5709. items = sd->status.storage.items;
  5710. }
  5711. //TODO: 3 - Guild Storage
  5712. else {
  5713. size = MAX_INVENTORY;
  5714. items = sd->status.inventory;
  5715. }
  5716. if( loc == 1 && !pc_iscarton(sd) ) {
  5717. ShowError("buildin_%s: Player doesn't have cart (CID:%d).\n", command, sd->status.char_id);
  5718. script_pushint(st,-1);
  5719. return SCRIPT_CMD_SUCCESS;
  5720. }
  5721. data = script_getdata(st, 2);
  5722. get_val(st, data); // Convert into value in case of a variable
  5723. if( data_isstring(data) ) // item name
  5724. id = itemdb_searchname(conv_str(st, data));
  5725. else // item id
  5726. id = itemdb_exists(conv_num(st, data));
  5727. if( id == NULL ) {
  5728. ShowError("buildin_%s: Invalid item '%s'.\n", command, script_getstr(st,2)); // returns string, regardless of what it was
  5729. script_pushint(st,0);
  5730. return SCRIPT_CMD_FAILURE;
  5731. }
  5732. if( !i ) { // For count/cart/storagecountitem function
  5733. unsigned short nameid = id->nameid;
  5734. for( i = 0; i < size; i++ )
  5735. if( &items[i] && items[i].nameid == nameid )
  5736. count += items[i].amount;
  5737. }
  5738. else { // For count/cart/storagecountitem2 function
  5739. unsigned short nameid;
  5740. int iden, ref, attr, c1, c2, c3, c4;
  5741. nameid = id->nameid;
  5742. iden = script_getnum(st,3);
  5743. ref = script_getnum(st,4);
  5744. attr = script_getnum(st,5);
  5745. c1 = script_getnum(st,6);
  5746. c2 = script_getnum(st,7);
  5747. c3 = script_getnum(st,8);
  5748. c4 = script_getnum(st,9);
  5749. for( i = 0; i < size; i++ )
  5750. if( &items[i] && items[i].nameid > 0 && items[i].nameid == nameid &&
  5751. items[i].amount > 0 && items[i].identify == iden &&
  5752. items[i].refine == ref && items[i].attribute == attr &&
  5753. items[i].card[0] == c1 && items[i].card[1] == c2 &&
  5754. items[i].card[2] == c3 && items[i].card[3] == c4 )
  5755. {
  5756. count += items[i].amount;
  5757. }
  5758. }
  5759. script_pushint(st, count);
  5760. return SCRIPT_CMD_SUCCESS;
  5761. }
  5762. /*==========================================
  5763. * Check if item with this amount can fit in inventory
  5764. * Checking : weight, stack amount >(MAX_AMOUNT), slots amount >(MAX_INVENTORY)
  5765. * Return
  5766. * 0 : fail
  5767. * 1 : success (npc side only)
  5768. *------------------------------------------*/
  5769. BUILDIN_FUNC(checkweight)
  5770. {
  5771. int slots = 0;
  5772. unsigned short amount2 = 0;
  5773. unsigned int weight = 0, i, nbargs;
  5774. struct item_data* id = NULL;
  5775. struct map_session_data* sd;
  5776. if( ( sd = script_rid2sd(st) ) == NULL )
  5777. return SCRIPT_CMD_SUCCESS;
  5778. nbargs = script_lastdata(st)+1;
  5779. if(nbargs%2) {
  5780. ShowError("buildin_checkweight: Invalid nb of args should be a multiple of 2.\n");
  5781. script_pushint(st,0);
  5782. return SCRIPT_CMD_FAILURE;
  5783. }
  5784. slots = pc_inventoryblank(sd); //nb of empty slot
  5785. for (i = 2; i < nbargs; i += 2) {
  5786. unsigned short nameid, amount;
  5787. struct script_data* data = script_getdata(st,i);
  5788. get_val(st, data); // Convert into value in case of a variable
  5789. if( data_isstring(data) ) // item name
  5790. id = itemdb_searchname(conv_str(st, data));
  5791. else // item id
  5792. id = itemdb_exists(conv_num(st, data));
  5793. if( id == NULL ) {
  5794. ShowError("buildin_checkweight: Invalid item '%s'.\n", script_getstr(st,i)); // returns string, regardless of what it was
  5795. script_pushint(st,0);
  5796. return SCRIPT_CMD_FAILURE;
  5797. }
  5798. nameid = id->nameid;
  5799. amount = script_getnum(st,i+1);
  5800. if( amount < 1 ) {
  5801. ShowError("buildin_checkweight: Invalid amount '%d'.\n", amount);
  5802. script_pushint(st,0);
  5803. return SCRIPT_CMD_FAILURE;
  5804. }
  5805. weight += itemdb_weight(nameid)*amount; //total weight for all chk
  5806. if( weight + sd->weight > sd->max_weight )
  5807. {// too heavy
  5808. script_pushint(st,0);
  5809. return SCRIPT_CMD_SUCCESS;
  5810. }
  5811. switch( pc_checkadditem(sd, nameid, amount) ) {
  5812. case CHKADDITEM_EXIST:
  5813. // item is already in inventory, but there is still space for the requested amount
  5814. break;
  5815. case CHKADDITEM_NEW:
  5816. if( itemdb_isstackable(nameid) ) {
  5817. // stackable
  5818. amount2++;
  5819. if( slots < amount2 ) {
  5820. script_pushint(st,0);
  5821. return SCRIPT_CMD_SUCCESS;
  5822. }
  5823. } else {
  5824. // non-stackable
  5825. amount2 += amount;
  5826. if( slots < amount2) {
  5827. script_pushint(st,0);
  5828. return SCRIPT_CMD_SUCCESS;
  5829. }
  5830. }
  5831. break;
  5832. case CHKADDITEM_OVERAMOUNT:
  5833. script_pushint(st,0);
  5834. return SCRIPT_CMD_SUCCESS;
  5835. }
  5836. }
  5837. script_pushint(st,1);
  5838. return SCRIPT_CMD_SUCCESS;
  5839. }
  5840. BUILDIN_FUNC(checkweight2)
  5841. {
  5842. //variable sub checkweight
  5843. int i = 0, slots = 0, weight = 0;
  5844. short fail = 0;
  5845. unsigned short amount2 = 0;
  5846. //variable for array parsing
  5847. struct script_data* data_it;
  5848. struct script_data* data_nb;
  5849. const char* name_it;
  5850. const char* name_nb;
  5851. int32 id_it, id_nb;
  5852. int32 idx_it, idx_nb;
  5853. int nb_it, nb_nb; //array size
  5854. TBL_PC *sd = script_rid2sd(st);
  5855. nullpo_retr(1,sd);
  5856. data_it = script_getdata(st, 2);
  5857. data_nb = script_getdata(st, 3);
  5858. if( !data_isreference(data_it) || !data_isreference(data_nb)) {
  5859. ShowError("buildin_checkweight2: parameter not a variable\n");
  5860. script_pushint(st,0);
  5861. return SCRIPT_CMD_FAILURE;// not a variable
  5862. }
  5863. id_it = reference_getid(data_it);
  5864. id_nb = reference_getid(data_nb);
  5865. idx_it = reference_getindex(data_it);
  5866. idx_nb = reference_getindex(data_nb);
  5867. name_it = reference_getname(data_it);
  5868. name_nb = reference_getname(data_nb);
  5869. if(is_string_variable(name_it) || is_string_variable(name_nb)) {
  5870. ShowError("buildin_checkweight2: illegal type, need int\n");
  5871. script_pushint(st,0);
  5872. return SCRIPT_CMD_FAILURE;// not supported
  5873. }
  5874. nb_it = script_array_highest_key(st, sd, reference_getname(data_it), reference_getref(data_it));
  5875. nb_nb = script_array_highest_key(st, sd, reference_getname(data_nb), reference_getref(data_nb));
  5876. if(nb_it != nb_nb) {
  5877. ShowError("buildin_checkweight2: Size mistmatch: nb_it=%d, nb_nb=%d\n",nb_it,nb_nb);
  5878. fail = 1;
  5879. }
  5880. slots = pc_inventoryblank(sd);
  5881. for(i=0; i<nb_it; i++) {
  5882. unsigned short amount, nameid = (int32)__64BPRTSIZE(get_val2(st,reference_uid(id_it,idx_it+i),reference_getref(data_it)));
  5883. script_removetop(st, -1, 0);
  5884. amount = (int32)__64BPRTSIZE(get_val2(st,reference_uid(id_nb,idx_nb+i),reference_getref(data_nb)));
  5885. script_removetop(st, -1, 0);
  5886. if(fail)
  5887. continue; //cpntonie to depop rest
  5888. if(itemdb_exists(nameid) == NULL ) {
  5889. ShowError("buildin_checkweight2: Invalid item '%d'.\n", nameid);
  5890. fail=1;
  5891. continue;
  5892. }
  5893. if(amount < 0 ) {
  5894. ShowError("buildin_checkweight2: Invalid amount '%d'.\n", amount);
  5895. fail = 1;
  5896. continue;
  5897. }
  5898. weight += itemdb_weight(nameid)*amount;
  5899. if( weight + sd->weight > sd->max_weight ) {
  5900. fail = 1;
  5901. continue;
  5902. }
  5903. switch( pc_checkadditem(sd, nameid, amount) ) {
  5904. case CHKADDITEM_EXIST:
  5905. // item is already in inventory, but there is still space for the requested amount
  5906. break;
  5907. case CHKADDITEM_NEW:
  5908. if( itemdb_isstackable(nameid) ) {// stackable
  5909. amount2++;
  5910. if( slots < amount2 )
  5911. fail = 1;
  5912. } else {// non-stackable
  5913. amount2 += amount;
  5914. if( slots < amount2 ) {
  5915. fail = 1;
  5916. }
  5917. }
  5918. break;
  5919. case CHKADDITEM_OVERAMOUNT:
  5920. fail = 1;
  5921. } //end switch
  5922. } //end loop DO NOT break it prematurly we need to depop all stack
  5923. fail ? script_pushint(st,0) : script_pushint(st,1);
  5924. return SCRIPT_CMD_SUCCESS;
  5925. }
  5926. /*==========================================
  5927. * getitem <item id>,<amount>{,<account ID>};
  5928. * getitem "<item name>",<amount>{,<account ID>};
  5929. *
  5930. * getitembound <item id>,<amount>,<type>{,<account ID>};
  5931. * getitembound "<item id>",<amount>,<type>{,<account ID>};
  5932. * Type:
  5933. * 0 - No bound
  5934. * 1 - Account Bound
  5935. * 2 - Guild Bound
  5936. * 3 - Party Bound
  5937. * 4 - Character Bound
  5938. *------------------------------------------*/
  5939. BUILDIN_FUNC(getitem)
  5940. {
  5941. int get_count, i;
  5942. unsigned short nameid, amount;
  5943. struct item it;
  5944. TBL_PC *sd;
  5945. struct script_data *data;
  5946. unsigned char flag = 0;
  5947. const char* command = script_getfuncname(st);
  5948. struct item_data *id = NULL;
  5949. data = script_getdata(st,2);
  5950. get_val(st,data);
  5951. if( data_isstring(data) ) {// "<item name>"
  5952. const char *name = conv_str(st,data);
  5953. id = itemdb_searchname(name);
  5954. if( id == NULL ){
  5955. ShowError("buildin_getitem: Nonexistant item %s requested.\n", name);
  5956. return SCRIPT_CMD_FAILURE; //No item created.
  5957. }
  5958. nameid = id->nameid;
  5959. } else if( data_isint(data) ) {// <item id>
  5960. nameid = conv_num(st,data);
  5961. if( !(id = itemdb_exists(nameid)) ){
  5962. ShowError("buildin_getitem: Nonexistant item %d requested.\n", nameid);
  5963. return SCRIPT_CMD_FAILURE; //No item created.
  5964. }
  5965. } else {
  5966. ShowError("buildin_getitem: invalid data type for argument #1 (%d).", data->type);
  5967. return SCRIPT_CMD_FAILURE;
  5968. }
  5969. // <amount>
  5970. if( (amount = script_getnum(st,3)) <= 0)
  5971. return SCRIPT_CMD_SUCCESS; //return if amount <=0, skip the useles iteration
  5972. memset(&it,0,sizeof(it));
  5973. it.nameid = nameid;
  5974. it.identify = 1;
  5975. it.bound = BOUND_NONE;
  5976. if( !strcmp(command,"getitembound") ) {
  5977. char bound = script_getnum(st,4);
  5978. if( bound < BOUND_NONE || bound >= BOUND_MAX ) {
  5979. ShowError("script_getitembound: Not a correct bound type! Type=%d\n",bound);
  5980. return SCRIPT_CMD_FAILURE;
  5981. }
  5982. if( script_hasdata(st,5) )
  5983. sd = map_id2sd(script_getnum(st,5));
  5984. else
  5985. sd = script_rid2sd(st); // Attached player
  5986. it.bound = bound;
  5987. } else if( script_hasdata(st,4) )
  5988. sd = map_id2sd(script_getnum(st,4)); // <Account ID>
  5989. else
  5990. sd = script_rid2sd(st); // Attached player
  5991. if( sd == NULL ) // no target
  5992. return SCRIPT_CMD_SUCCESS;
  5993. //Check if it's stackable.
  5994. if (!itemdb_isstackable2(id))
  5995. get_count = 1;
  5996. else
  5997. get_count = amount;
  5998. for (i = 0; i < amount; i += get_count)
  5999. {
  6000. // if not pet egg
  6001. if (!pet_create_egg(sd, nameid))
  6002. {
  6003. if ((flag = pc_additem(sd, &it, get_count, LOG_TYPE_SCRIPT)))
  6004. {
  6005. clif_additem(sd, 0, 0, flag);
  6006. if( pc_candrop(sd,&it) )
  6007. map_addflooritem(&it,get_count,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0,0);
  6008. }
  6009. }
  6010. }
  6011. return SCRIPT_CMD_SUCCESS;
  6012. }
  6013. /*==========================================
  6014. * getitem2 <item id>,<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>{,<account ID>};
  6015. * getitem2 "<item name>",<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>{,<account ID>};
  6016. *
  6017. * getitembound2 <item id>,<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>,<bound type>{,<account ID>};
  6018. * getitembound2 "<item name>",<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>,<bound type>{,<account ID>};
  6019. * Type:
  6020. * 0 - No bound
  6021. * 1 - Account Bound
  6022. * 2 - Guild Bound
  6023. * 3 - Party Bound
  6024. * 4 - Character Bound
  6025. *------------------------------------------*/
  6026. BUILDIN_FUNC(getitem2)
  6027. {
  6028. unsigned short nameid, amount;
  6029. int iden, ref, attr;
  6030. unsigned short c1, c2, c3, c4;
  6031. char bound = BOUND_NONE;
  6032. struct item_data *item_data = NULL;
  6033. struct item item_tmp;
  6034. TBL_PC *sd;
  6035. struct script_data *data;
  6036. const char* command = script_getfuncname(st);
  6037. if( !strcmp(command,"getitembound2") ) {
  6038. bound = script_getnum(st,11);
  6039. if( bound < BOUND_NONE || bound >= BOUND_MAX ) {
  6040. ShowError("script_getitembound2: Not a correct bound type! Type=%d\n",bound);
  6041. return SCRIPT_CMD_FAILURE;
  6042. }
  6043. if( script_hasdata(st,12) )
  6044. sd = map_id2sd(script_getnum(st,12));
  6045. else
  6046. sd = script_rid2sd(st); // Attached player
  6047. } else if( script_hasdata(st,11) )
  6048. sd = map_id2sd(script_getnum(st,11)); // <Account ID>
  6049. else
  6050. sd = script_rid2sd(st); // Attached player
  6051. if( sd == NULL ) // no target
  6052. return SCRIPT_CMD_SUCCESS;
  6053. data = script_getdata(st,2);
  6054. get_val(st,data);
  6055. if( data_isstring(data) ) {
  6056. const char *name = conv_str(st,data);
  6057. if( (item_data = itemdb_searchname(name)) == NULL ){
  6058. ShowError("buildin_getitem2: Nonexistant item %s requested (by conv_str).\n", name);
  6059. return SCRIPT_CMD_FAILURE; //No item created.
  6060. }
  6061. nameid = item_data->nameid;
  6062. } else {
  6063. nameid = conv_num(st,data);
  6064. if( (item_data = itemdb_exists(nameid)) == NULL ){
  6065. ShowError("buildin_getitem2: Nonexistant item %d requested (by conv_num).\n", nameid);
  6066. return SCRIPT_CMD_FAILURE; //No item created.
  6067. }
  6068. }
  6069. amount = script_getnum(st,3);
  6070. iden = script_getnum(st,4);
  6071. ref = script_getnum(st,5);
  6072. attr = script_getnum(st,6);
  6073. c1 = (unsigned short)script_getnum(st,7);
  6074. c2 = (unsigned short)script_getnum(st,8);
  6075. c3 = (unsigned short)script_getnum(st,9);
  6076. c4 = (unsigned short)script_getnum(st,10);
  6077. if( item_data ) {
  6078. int get_count = 0, i;
  6079. memset(&item_tmp,0,sizeof(item_tmp));
  6080. if( item_data->type == IT_WEAPON || item_data->type == IT_ARMOR || item_data->type == IT_SHADOWGEAR ) {
  6081. if(ref > MAX_REFINE)
  6082. ref = MAX_REFINE;
  6083. }
  6084. else if( item_data->type == IT_PETEGG ) {
  6085. iden = 1;
  6086. ref = 0;
  6087. }
  6088. else {
  6089. iden = 1;
  6090. ref = attr = 0;
  6091. }
  6092. item_tmp.nameid = nameid;
  6093. item_tmp.identify = iden;
  6094. item_tmp.refine = ref;
  6095. item_tmp.attribute = attr;
  6096. item_tmp.card[0] = c1;
  6097. item_tmp.card[1] = c2;
  6098. item_tmp.card[2] = c3;
  6099. item_tmp.card[3] = c4;
  6100. item_tmp.bound = bound;
  6101. //Check if it's stackable.
  6102. if (!itemdb_isstackable2(item_data))
  6103. get_count = 1;
  6104. else
  6105. get_count = amount;
  6106. for (i = 0; i < amount; i += get_count)
  6107. {
  6108. // if not pet egg
  6109. if (!pet_create_egg(sd, nameid))
  6110. {
  6111. unsigned char flag = 0;
  6112. if ((flag = pc_additem(sd, &item_tmp, get_count, LOG_TYPE_SCRIPT)))
  6113. {
  6114. clif_additem(sd, 0, 0, flag);
  6115. if( pc_candrop(sd,&item_tmp) )
  6116. map_addflooritem(&item_tmp,get_count,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0,0);
  6117. }
  6118. }
  6119. }
  6120. }
  6121. return SCRIPT_CMD_SUCCESS;
  6122. }
  6123. /** Gives rental item to player
  6124. * rentitem <item id>,<seconds>{,<account_id>}
  6125. * rentitem "<item name>",<seconds>{,<account_id>}
  6126. */
  6127. BUILDIN_FUNC(rentitem) {
  6128. struct map_session_data *sd;
  6129. struct script_data *data;
  6130. struct item it;
  6131. int seconds;
  6132. unsigned short nameid = 0;
  6133. unsigned char flag = 0;
  6134. data = script_getdata(st,2);
  6135. get_val(st,data);
  6136. if (!script_accid2sd(4,sd))
  6137. return SCRIPT_CMD_FAILURE;
  6138. if( data_isstring(data) )
  6139. {
  6140. const char *name = conv_str(st,data);
  6141. struct item_data *itd = itemdb_searchname(name);
  6142. if( itd == NULL )
  6143. {
  6144. ShowError("buildin_rentitem: Nonexistant item %s requested.\n", name);
  6145. return SCRIPT_CMD_FAILURE;
  6146. }
  6147. nameid = itd->nameid;
  6148. }
  6149. else if( data_isint(data) )
  6150. {
  6151. nameid = conv_num(st,data);
  6152. if( nameid == 0 || !itemdb_exists(nameid) )
  6153. {
  6154. ShowError("buildin_rentitem: Nonexistant item %hu requested.\n", nameid);
  6155. return SCRIPT_CMD_FAILURE;
  6156. }
  6157. }
  6158. else
  6159. {
  6160. ShowError("buildin_rentitem: invalid data type for argument #1 (%d).\n", data->type);
  6161. return SCRIPT_CMD_FAILURE;
  6162. }
  6163. seconds = script_getnum(st,3);
  6164. memset(&it, 0, sizeof(it));
  6165. it.nameid = nameid;
  6166. it.identify = 1;
  6167. it.expire_time = (unsigned int)(time(NULL) + seconds);
  6168. it.bound = BOUND_NONE;
  6169. if( (flag = pc_additem(sd, &it, 1, LOG_TYPE_SCRIPT)) )
  6170. {
  6171. clif_additem(sd, 0, 0, flag);
  6172. return SCRIPT_CMD_FAILURE;
  6173. }
  6174. return SCRIPT_CMD_SUCCESS;
  6175. }
  6176. /** Gives rental item to player with advanced option
  6177. * rentitem2 <item id>,<time>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>{,<account_id>};
  6178. * rentitem2 "<item name>",<time>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>{,<account_id>};
  6179. */
  6180. BUILDIN_FUNC(rentitem2) {
  6181. struct map_session_data *sd;
  6182. struct script_data *data;
  6183. struct item it;
  6184. struct item_data *id;
  6185. int seconds;
  6186. unsigned short nameid = 0;
  6187. unsigned char flag = 0;
  6188. int iden,ref,attr,c1,c2,c3,c4;
  6189. data = script_getdata(st,2);
  6190. get_val(st,data);
  6191. if (!script_accid2sd(11,sd))
  6192. return SCRIPT_CMD_FAILURE;
  6193. if( data_isstring(data) ) {
  6194. const char *name = conv_str(st,data);
  6195. id = itemdb_searchname(name);
  6196. if( id == NULL ) {
  6197. ShowError("buildin_rentitem2: Nonexistant item %s requested.\n", name);
  6198. return SCRIPT_CMD_FAILURE;
  6199. }
  6200. nameid = id->nameid;
  6201. }
  6202. else if( data_isint(data) ) {
  6203. nameid = conv_num(st,data);
  6204. if( !(id = itemdb_search(nameid))) {
  6205. ShowError("buildin_rentitem2: Nonexistant item %hu requested.\n", nameid);
  6206. return SCRIPT_CMD_FAILURE;
  6207. }
  6208. }
  6209. else {
  6210. ShowError("buildin_rentitem2: invalid data type for argument #1 (%d).\n", data->type);
  6211. return SCRIPT_CMD_FAILURE;
  6212. }
  6213. seconds = script_getnum(st,3);
  6214. iden = script_getnum(st,4);
  6215. ref = script_getnum(st,5);
  6216. attr = script_getnum(st,6);
  6217. if (id->type==IT_WEAPON || id->type==IT_ARMOR || id->type==IT_SHADOWGEAR) {
  6218. if(ref > MAX_REFINE) ref = MAX_REFINE;
  6219. }
  6220. else if (id->type==IT_PETEGG) {
  6221. iden = 1;
  6222. ref = 0;
  6223. }
  6224. else {
  6225. iden = 1;
  6226. ref = attr = 0;
  6227. }
  6228. c1 = (short)script_getnum(st,7);
  6229. c2 = (short)script_getnum(st,8);
  6230. c3 = (short)script_getnum(st,9);
  6231. c4 = (short)script_getnum(st,10);
  6232. memset(&it, 0, sizeof(it));
  6233. it.nameid = nameid;
  6234. it.identify = iden;
  6235. it.refine = ref;
  6236. it.attribute = attr;
  6237. it.card[0] = (short)c1;
  6238. it.card[1] = (short)c2;
  6239. it.card[2] = (short)c3;
  6240. it.card[3] = (short)c4;
  6241. it.expire_time = (unsigned int)(time(NULL) + seconds);
  6242. if( (flag = pc_additem(sd, &it, 1, LOG_TYPE_SCRIPT)) ) {
  6243. clif_additem(sd, 0, 0, flag);
  6244. return SCRIPT_CMD_FAILURE;
  6245. }
  6246. return SCRIPT_CMD_SUCCESS;
  6247. }
  6248. /*==========================================
  6249. * gets an item with someone's name inscribed [Skotlex]
  6250. * getinscribeditem item_num, character_name
  6251. * Returned Qty is always 1, only works on equip-able
  6252. * equipment
  6253. *------------------------------------------*/
  6254. BUILDIN_FUNC(getnameditem)
  6255. {
  6256. unsigned short nameid;
  6257. struct item item_tmp;
  6258. TBL_PC *sd, *tsd;
  6259. struct script_data *data;
  6260. sd = script_rid2sd(st);
  6261. if (sd == NULL)
  6262. { //Player not attached!
  6263. script_pushint(st,0);
  6264. return SCRIPT_CMD_SUCCESS;
  6265. }
  6266. data=script_getdata(st,2);
  6267. get_val(st,data);
  6268. if( data_isstring(data) ){
  6269. const char *name=conv_str(st,data);
  6270. struct item_data *item_data = itemdb_searchname(name);
  6271. if( item_data == NULL)
  6272. { //Failed
  6273. script_pushint(st,0);
  6274. return SCRIPT_CMD_SUCCESS;
  6275. }
  6276. nameid = item_data->nameid;
  6277. }else
  6278. nameid = conv_num(st,data);
  6279. if(!itemdb_exists(nameid)/* || itemdb_isstackable(nameid)*/)
  6280. { //Even though named stackable items "could" be risky, they are required for certain quests.
  6281. script_pushint(st,0);
  6282. return SCRIPT_CMD_SUCCESS;
  6283. }
  6284. data=script_getdata(st,3);
  6285. get_val(st,data);
  6286. if( data_isstring(data) ) //Char Name
  6287. tsd=map_nick2sd(conv_str(st,data));
  6288. else //Char Id was given
  6289. tsd=map_charid2sd(conv_num(st,data));
  6290. if( tsd == NULL )
  6291. { //Failed
  6292. script_pushint(st,0);
  6293. return SCRIPT_CMD_SUCCESS;
  6294. }
  6295. memset(&item_tmp,0,sizeof(item_tmp));
  6296. item_tmp.nameid=nameid;
  6297. item_tmp.amount=1;
  6298. item_tmp.identify=1;
  6299. item_tmp.card[0]=CARD0_CREATE; //we don't use 255! because for example SIGNED WEAPON shouldn't get TOP10 BS Fame bonus [Lupus]
  6300. item_tmp.card[2]=tsd->status.char_id;
  6301. item_tmp.card[3]=tsd->status.char_id >> 16;
  6302. if(pc_additem(sd,&item_tmp,1,LOG_TYPE_SCRIPT)) {
  6303. script_pushint(st,0);
  6304. return SCRIPT_CMD_SUCCESS; //Failed to add item, we will not drop if they don't fit
  6305. }
  6306. script_pushint(st,1);
  6307. return SCRIPT_CMD_SUCCESS;
  6308. }
  6309. /*==========================================
  6310. * gets a random item ID from an item group [Skotlex]
  6311. * groupranditem <group_num>{,<sub_group>};
  6312. *------------------------------------------*/
  6313. BUILDIN_FUNC(grouprandomitem) {
  6314. struct s_item_group_entry *entry = NULL;
  6315. int sub_group = 1;
  6316. FETCH(3, sub_group);
  6317. entry = itemdb_get_randgroupitem(script_getnum(st,2),sub_group);
  6318. if (!entry) {
  6319. ShowError("buildin_grouprandomitem: Invalid item group with group_id '%d', sub_group '%d'.\n", script_getnum(st,2), sub_group);
  6320. script_pushint(st,UNKNOWN_ITEM_ID);
  6321. return SCRIPT_CMD_FAILURE;
  6322. }
  6323. script_pushint(st,entry->nameid);
  6324. return SCRIPT_CMD_SUCCESS;
  6325. }
  6326. /**
  6327. * makeitem <item id>,<amount>,"<map name>",<X>,<Y>;
  6328. * makeitem "<item name>",<amount>,"<map name>",<X>,<Y>;
  6329. */
  6330. BUILDIN_FUNC(makeitem) {
  6331. uint16 nameid, amount, flag = 0, x, y;
  6332. const char *mapname;
  6333. int m;
  6334. struct item item_tmp;
  6335. struct script_data *data;
  6336. data = script_getdata(st,2);
  6337. get_val(st,data);
  6338. if( data_isstring(data) ){
  6339. const char *name = conv_str(st,data);
  6340. struct item_data *item_data = itemdb_searchname(name);
  6341. if( item_data )
  6342. nameid = item_data->nameid;
  6343. else
  6344. nameid = UNKNOWN_ITEM_ID;
  6345. }
  6346. else
  6347. nameid = conv_num(st,data);
  6348. amount = script_getnum(st,3);
  6349. mapname = script_getstr(st,4);
  6350. x = script_getnum(st,5);
  6351. y = script_getnum(st,6);
  6352. if(strcmp(mapname,"this")==0) {
  6353. TBL_PC *sd;
  6354. sd = script_rid2sd(st);
  6355. if (!sd)
  6356. return SCRIPT_CMD_SUCCESS; //Failed...
  6357. m = sd->bl.m;
  6358. } else
  6359. m = map_mapname2mapid(mapname);
  6360. if(nameid<0) {
  6361. nameid = -nameid;
  6362. flag = 1;
  6363. }
  6364. if(nameid > 0) {
  6365. memset(&item_tmp,0,sizeof(item_tmp));
  6366. item_tmp.nameid = nameid;
  6367. if(!flag)
  6368. item_tmp.identify = 1;
  6369. else
  6370. item_tmp.identify = itemdb_isidentified(nameid);
  6371. map_addflooritem(&item_tmp,amount,m,x,y,0,0,0,4,0);
  6372. }
  6373. return SCRIPT_CMD_SUCCESS;
  6374. }
  6375. /**
  6376. * makeitem2 <item id>,<amount>,"<map name>",<X>,<Y>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>;
  6377. * makeitem2 "<item name>",<amount>,"<map name>",<X>,<Y>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>;
  6378. */
  6379. BUILDIN_FUNC(makeitem2) {
  6380. uint16 nameid, amount, x, y;
  6381. const char *mapname;
  6382. int m;
  6383. struct item item_tmp;
  6384. struct script_data *data;
  6385. struct item_data *id;
  6386. data = script_getdata(st,2);
  6387. get_val(st,data);
  6388. if( data_isstring(data) ){
  6389. const char *name = conv_str(st,data);
  6390. struct item_data *item_data = itemdb_searchname(name);
  6391. if( item_data )
  6392. nameid = item_data->nameid;
  6393. else
  6394. nameid = UNKNOWN_ITEM_ID;
  6395. }
  6396. else
  6397. nameid = conv_num(st,data);
  6398. amount = script_getnum(st,3);
  6399. mapname = script_getstr(st,4);
  6400. x = script_getnum(st,5);
  6401. y = script_getnum(st,6);
  6402. if (strcmp(mapname,"this")==0) {
  6403. TBL_PC *sd;
  6404. sd = script_rid2sd(st);
  6405. if (!sd)
  6406. return SCRIPT_CMD_SUCCESS; //Failed...
  6407. m = sd->bl.m;
  6408. }
  6409. else
  6410. m = map_mapname2mapid(mapname);
  6411. if ((id = itemdb_search(nameid))) {
  6412. char iden, ref, attr;
  6413. memset(&item_tmp,0,sizeof(item_tmp));
  6414. item_tmp.nameid = nameid;
  6415. iden = (char)script_getnum(st,7);
  6416. ref = (char)script_getnum(st,8);
  6417. attr = (char)script_getnum(st,9);
  6418. if (id->type==IT_WEAPON || id->type==IT_ARMOR || id->type==IT_SHADOWGEAR) {
  6419. if(ref > MAX_REFINE) ref = MAX_REFINE;
  6420. }
  6421. else if (id->type==IT_PETEGG) {
  6422. iden = 1;
  6423. ref = 0;
  6424. }
  6425. else {
  6426. iden = 1;
  6427. ref = attr = 0;
  6428. }
  6429. item_tmp.identify = iden;
  6430. item_tmp.refine = ref;
  6431. item_tmp.attribute = attr;
  6432. item_tmp.card[0] = script_getnum(st,10);
  6433. item_tmp.card[1] = script_getnum(st,11);
  6434. item_tmp.card[2] = script_getnum(st,12);
  6435. item_tmp.card[3] = script_getnum(st,13);
  6436. map_addflooritem(&item_tmp,amount,m,x,y,0,0,0,4,0);
  6437. }
  6438. else
  6439. return SCRIPT_CMD_FAILURE;
  6440. return SCRIPT_CMD_SUCCESS;
  6441. }
  6442. /// Counts / deletes the current item given by idx.
  6443. /// Used by buildin_delitem_search
  6444. /// Relies on all input data being already fully valid.
  6445. static void buildin_delitem_delete(struct map_session_data* sd, int idx, int* amount, uint8 loc, bool delete_items)
  6446. {
  6447. int delamount;
  6448. struct item *itm = NULL;
  6449. switch(loc) {
  6450. case 1: // cart
  6451. itm = &sd->status.cart[idx];
  6452. break;
  6453. case 2: // storage
  6454. itm = &sd->status.storage.items[idx];
  6455. break;
  6456. default: //inventory
  6457. itm = &sd->status.inventory[idx];
  6458. break;
  6459. }
  6460. delamount = ( amount[0] < itm->amount ) ? amount[0] : itm->amount;
  6461. if( delete_items )
  6462. {
  6463. if( itemdb_type(itm->nameid) == IT_PETEGG && itm->card[0] == CARD0_PET )
  6464. {// delete associated pet
  6465. intif_delete_petdata(MakeDWord(itm->card[1], itm->card[2]));
  6466. }
  6467. switch(loc) {
  6468. case 1:
  6469. pc_cart_delitem(sd,idx,delamount,0,LOG_TYPE_SCRIPT);
  6470. break;
  6471. case 2:
  6472. storage_delitem(sd,idx,delamount);
  6473. log_pick_pc(sd,LOG_TYPE_SCRIPT,-delamount,itm);
  6474. break;
  6475. default:
  6476. pc_delitem(sd, idx, delamount, 0, 0, LOG_TYPE_SCRIPT);
  6477. break;
  6478. }
  6479. }
  6480. amount[0]-= delamount;
  6481. }
  6482. /// Searches for item(s) and checks, if there is enough of them.
  6483. /// Used by delitem and delitem2
  6484. /// Relies on all input data being already fully valid.
  6485. /// @param exact_match will also match item attributes and cards, not just name id
  6486. /// @return true when all items could be deleted, false when there were not enough items to delete
  6487. static bool buildin_delitem_search(struct map_session_data* sd, struct item* it, bool exact_match, uint8 loc)
  6488. {
  6489. bool delete_items = false;
  6490. int i, amount, size;
  6491. struct item *items;
  6492. // prefer always non-equipped items
  6493. it->equip = 0;
  6494. // when searching for nameid only, prefer additionally
  6495. if( !exact_match )
  6496. {
  6497. // non-refined items
  6498. it->refine = 0;
  6499. // card-less items
  6500. memset(it->card, 0, sizeof(it->card));
  6501. }
  6502. switch(loc) {
  6503. case 1: // cart
  6504. size = MAX_CART;
  6505. items = sd->status.cart;
  6506. break;
  6507. case 2: // storage
  6508. size = MAX_STORAGE;
  6509. items = sd->status.storage.items;
  6510. break;
  6511. default: //inventory
  6512. size = MAX_INVENTORY;
  6513. items = sd->status.inventory;
  6514. break;
  6515. }
  6516. for(;;)
  6517. {
  6518. unsigned short important = 0;
  6519. amount = it->amount;
  6520. // 1st pass -- less important items / exact match
  6521. for( i = 0; amount && i < size; i++ )
  6522. {
  6523. struct item *itm = NULL;
  6524. if( !&items[i] || !(itm = &items[i])->nameid || itm->nameid != it->nameid )
  6525. {// wrong/invalid item
  6526. continue;
  6527. }
  6528. if( itm->equip != it->equip || itm->refine != it->refine )
  6529. {// not matching attributes
  6530. important++;
  6531. continue;
  6532. }
  6533. if( exact_match )
  6534. {
  6535. if( itm->identify != it->identify || itm->attribute != it->attribute || memcmp(itm->card, it->card, sizeof(itm->card)) )
  6536. {// not matching exact attributes
  6537. continue;
  6538. }
  6539. }
  6540. else
  6541. {
  6542. if( itemdb_type(itm->nameid) == IT_PETEGG )
  6543. {
  6544. if( itm->card[0] == CARD0_PET && CheckForCharServer() )
  6545. {// pet which cannot be deleted
  6546. continue;
  6547. }
  6548. }
  6549. else if( memcmp(itm->card, it->card, sizeof(itm->card)) )
  6550. {// named/carded item
  6551. important++;
  6552. continue;
  6553. }
  6554. }
  6555. // count / delete item
  6556. buildin_delitem_delete(sd, i, &amount, loc, delete_items);
  6557. }
  6558. // 2nd pass -- any matching item
  6559. if( amount == 0 || important == 0 )
  6560. {// either everything was already consumed or no items were skipped
  6561. ;
  6562. }
  6563. else for( i = 0; amount && i < size; i++ )
  6564. {
  6565. struct item *itm = NULL;
  6566. if( !&items[i] || !(itm = &items[i])->nameid || itm->nameid != it->nameid )
  6567. {// wrong/invalid item
  6568. continue;
  6569. }
  6570. if( itemdb_type(itm->nameid) == IT_PETEGG && itm->card[0] == CARD0_PET && CheckForCharServer() )
  6571. {// pet which cannot be deleted
  6572. continue;
  6573. }
  6574. if( exact_match )
  6575. {
  6576. if( itm->refine != it->refine || itm->identify != it->identify || itm->attribute != it->attribute || memcmp(itm->card, it->card, sizeof(itm->card)) )
  6577. {// not matching attributes
  6578. continue;
  6579. }
  6580. }
  6581. // count / delete item
  6582. buildin_delitem_delete(sd, i, &amount, loc, delete_items);
  6583. }
  6584. if( amount )
  6585. {// not enough items
  6586. return false;
  6587. }
  6588. else if( delete_items )
  6589. {// we are done with the work
  6590. return true;
  6591. }
  6592. else
  6593. {// get rid of the items now
  6594. delete_items = true;
  6595. }
  6596. }
  6597. }
  6598. /// Deletes items from the target/attached player.
  6599. /// Prioritizes ordinary items.
  6600. ///
  6601. /// delitem <item id>,<amount>{,<account id>}
  6602. /// delitem "<item name>",<amount>{,<account id>}
  6603. /// cartdelitem <item id>,<amount>{,<account id>}
  6604. /// cartdelitem "<item name>",<amount>{,<account id>}
  6605. /// storagedelitem <item id>,<amount>{,<account id>}
  6606. /// storagedelitem "<item name>",<amount>{,<account id>}
  6607. BUILDIN_FUNC(delitem)
  6608. {
  6609. TBL_PC *sd;
  6610. struct item it;
  6611. struct script_data *data;
  6612. uint8 loc = 0;
  6613. char* command = (char*)script_getfuncname(st);
  6614. if(!strncmp(command, "cart", 4))
  6615. loc = 1;
  6616. else if(!strncmp(command, "storage", 7))
  6617. loc = 2;
  6618. //TODO: 3 - Guild Storage
  6619. if( script_hasdata(st,4) )
  6620. {
  6621. uint32 account_id = script_getnum(st,4);
  6622. sd = map_id2sd(account_id); // <account id>
  6623. if( sd == NULL )
  6624. {
  6625. ShowError("buildin_%s: player not found (AID=%d).\n", command, account_id);
  6626. st->state = END;
  6627. return SCRIPT_CMD_FAILURE;
  6628. }
  6629. }
  6630. else
  6631. {
  6632. sd = script_rid2sd(st);// attached player
  6633. if( sd == NULL )
  6634. return SCRIPT_CMD_SUCCESS;
  6635. }
  6636. if (loc == 1 && !pc_iscarton(sd)) {
  6637. ShowError("buildin_cartdelitem: player doesn't have cart (CID=%d).\n", sd->status.char_id);
  6638. return SCRIPT_CMD_FAILURE;
  6639. }
  6640. data = script_getdata(st,2);
  6641. get_val(st,data);
  6642. if( data_isstring(data) )
  6643. {
  6644. const char* item_name = conv_str(st,data);
  6645. struct item_data* id = itemdb_searchname(item_name);
  6646. if( id == NULL )
  6647. {
  6648. ShowError("buildin_%s: unknown item \"%s\".\n", command, item_name);
  6649. st->state = END;
  6650. return SCRIPT_CMD_FAILURE;
  6651. }
  6652. it.nameid = id->nameid;// "<item name>"
  6653. }
  6654. else
  6655. {
  6656. it.nameid = conv_num(st,data);// <item id>
  6657. if( !itemdb_exists( it.nameid ) )
  6658. {
  6659. ShowError("buildin_%s: unknown item \"%hu\".\n", command, it.nameid);
  6660. st->state = END;
  6661. return SCRIPT_CMD_FAILURE;
  6662. }
  6663. }
  6664. it.amount=script_getnum(st,3);
  6665. if( it.amount <= 0 )
  6666. return SCRIPT_CMD_SUCCESS;// nothing to do
  6667. if( buildin_delitem_search(sd, &it, false, loc) )
  6668. {// success
  6669. return SCRIPT_CMD_SUCCESS;
  6670. }
  6671. ShowError("buildin_%s: failed to delete %d items (AID=%d item_id=%hu).\n", command, it.amount, sd->status.account_id, it.nameid);
  6672. st->state = END;
  6673. st->mes_active = 0;
  6674. clif_scriptclose(sd, st->oid);
  6675. return SCRIPT_CMD_FAILURE;
  6676. }
  6677. /// Deletes items from the target/attached player.
  6678. ///
  6679. /// delitem2 <item id>,<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>{,<account ID>}
  6680. /// delitem2 "<Item name>",<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>{,<account ID>}
  6681. /// cartdelitem2 <item id>,<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>{,<account ID>}
  6682. /// cartdelitem2 "<Item name>",<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>{,<account ID>}
  6683. /// storagedelitem2 <item id>,<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>{,<account ID>}
  6684. /// storagedelitem2 "<Item name>",<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>{,<account ID>}
  6685. BUILDIN_FUNC(delitem2)
  6686. {
  6687. TBL_PC *sd;
  6688. struct item it;
  6689. struct script_data *data;
  6690. uint8 loc = 0;
  6691. char* command = (char*)script_getfuncname(st);
  6692. if(!strncmp(command, "cart", 4))
  6693. loc = 1;
  6694. else if(!strncmp(command, "storage", 7))
  6695. loc = 2;
  6696. //TODO: 3 - Guild Storage
  6697. if( script_hasdata(st,11) )
  6698. {
  6699. uint32 account_id = script_getnum(st,11);
  6700. sd = map_id2sd(account_id); // <account id>
  6701. if( sd == NULL )
  6702. {
  6703. ShowError("buildin_%s: player not found (AID=%d).\n", command, account_id);
  6704. st->state = END;
  6705. return SCRIPT_CMD_FAILURE;
  6706. }
  6707. }
  6708. else
  6709. {
  6710. sd = script_rid2sd(st);// attached player
  6711. if( sd == NULL )
  6712. return SCRIPT_CMD_SUCCESS;
  6713. }
  6714. if (loc == 1 && !pc_iscarton(sd)) {
  6715. ShowError("buildin_cartdelitem: player doesn't have cart (CID=%d).\n", sd->status.char_id);
  6716. script_pushint(st,-1);
  6717. return SCRIPT_CMD_FAILURE;
  6718. }
  6719. data = script_getdata(st,2);
  6720. get_val(st,data);
  6721. if( data_isstring(data) )
  6722. {
  6723. const char* item_name = conv_str(st,data);
  6724. struct item_data* id = itemdb_searchname(item_name);
  6725. if( id == NULL )
  6726. {
  6727. ShowError("buildin_%s: unknown item \"%s\".\n", command, item_name);
  6728. st->state = END;
  6729. return SCRIPT_CMD_FAILURE;
  6730. }
  6731. it.nameid = id->nameid;// "<item name>"
  6732. }
  6733. else
  6734. {
  6735. it.nameid = conv_num(st,data);// <item id>
  6736. if( !itemdb_exists( it.nameid ) )
  6737. {
  6738. ShowError("buildin_%s: unknown item \"%hu\".\n", command, it.nameid);
  6739. st->state = END;
  6740. return SCRIPT_CMD_FAILURE;
  6741. }
  6742. }
  6743. it.amount=script_getnum(st,3);
  6744. it.identify=script_getnum(st,4);
  6745. it.refine=script_getnum(st,5);
  6746. it.attribute=script_getnum(st,6);
  6747. it.card[0]=(short)script_getnum(st,7);
  6748. it.card[1]=(short)script_getnum(st,8);
  6749. it.card[2]=(short)script_getnum(st,9);
  6750. it.card[3]=(short)script_getnum(st,10);
  6751. if( it.amount <= 0 )
  6752. return SCRIPT_CMD_SUCCESS;// nothing to do
  6753. if( buildin_delitem_search(sd, &it, true, loc) )
  6754. {// success
  6755. return SCRIPT_CMD_SUCCESS;
  6756. }
  6757. ShowError("buildin_%s: failed to delete %d items (AID=%d item_id=%hu).\n", command, it.amount, sd->status.account_id, it.nameid);
  6758. st->state = END;
  6759. st->mes_active = 0;
  6760. clif_scriptclose(sd, st->oid);
  6761. return SCRIPT_CMD_FAILURE;
  6762. }
  6763. /*==========================================
  6764. * Enables/Disables use of items while in an NPC [Skotlex]
  6765. *------------------------------------------*/
  6766. BUILDIN_FUNC(enableitemuse)
  6767. {
  6768. TBL_PC *sd;
  6769. sd=script_rid2sd(st);
  6770. if (sd)
  6771. st->npc_item_flag = sd->npc_item_flag = 1;
  6772. return SCRIPT_CMD_SUCCESS;
  6773. }
  6774. BUILDIN_FUNC(disableitemuse)
  6775. {
  6776. TBL_PC *sd;
  6777. sd=script_rid2sd(st);
  6778. if (sd)
  6779. st->npc_item_flag = sd->npc_item_flag = 0;
  6780. return SCRIPT_CMD_SUCCESS;
  6781. }
  6782. /*==========================================
  6783. * Returns a character's specified stat.
  6784. * Check pc_readparam for available options.
  6785. * readparam <param>{,"<nick>"}
  6786. *------------------------------------------*/
  6787. BUILDIN_FUNC(readparam)
  6788. {
  6789. int type;
  6790. struct script_data *data = script_getdata(st, 2);
  6791. TBL_PC *sd;
  6792. if (!script_nick2sd(3,sd)) {
  6793. script_pushint(st,-1);
  6794. return SCRIPT_CMD_FAILURE;
  6795. }
  6796. if (data->type == C_NAME) { // If using constant name, just get the val
  6797. get_val_(st, data, sd);
  6798. script_pushint(st, (int)data->u.num);
  6799. return SCRIPT_CMD_SUCCESS;
  6800. }
  6801. type = script_getnum(st, 2);
  6802. script_pushint(st,pc_readparam(sd,type));
  6803. return SCRIPT_CMD_SUCCESS;
  6804. }
  6805. /*==========================================
  6806. * Return charid identification
  6807. * return by @num :
  6808. * 0 : char_id
  6809. * 1 : party_id
  6810. * 2 : guild_id
  6811. * 3 : account_id
  6812. * 4 : bg_id
  6813. *------------------------------------------*/
  6814. BUILDIN_FUNC(getcharid)
  6815. {
  6816. int num;
  6817. TBL_PC *sd;
  6818. num = script_getnum(st,2);
  6819. if( script_hasdata(st,3) )
  6820. sd=map_nick2sd(script_getstr(st,3));
  6821. else
  6822. sd=script_rid2sd(st);
  6823. if(sd==NULL){
  6824. script_pushint(st,0); //return 0, according docs
  6825. return SCRIPT_CMD_SUCCESS;
  6826. }
  6827. switch( num ) {
  6828. case 0: script_pushint(st,sd->status.char_id); break;
  6829. case 1: script_pushint(st,sd->status.party_id); break;
  6830. case 2: script_pushint(st,sd->status.guild_id); break;
  6831. case 3: script_pushint(st,sd->status.account_id); break;
  6832. case 4: script_pushint(st,sd->bg_id); break;
  6833. default:
  6834. ShowError("buildin_getcharid: invalid parameter (%d).\n", num);
  6835. script_pushint(st,0);
  6836. break;
  6837. }
  6838. return SCRIPT_CMD_SUCCESS;
  6839. }
  6840. /*==========================================
  6841. * returns the GID of an NPC
  6842. *------------------------------------------*/
  6843. BUILDIN_FUNC(getnpcid)
  6844. {
  6845. int num = script_getnum(st,2);
  6846. struct npc_data* nd = NULL;
  6847. if( script_hasdata(st,3) )
  6848. {// unique npc name
  6849. if( ( nd = npc_name2id(script_getstr(st,3)) ) == NULL )
  6850. {
  6851. ShowError("buildin_getnpcid: No such NPC '%s'.\n", script_getstr(st,3));
  6852. script_pushint(st,0);
  6853. return SCRIPT_CMD_FAILURE;
  6854. }
  6855. }
  6856. switch (num) {
  6857. case 0:
  6858. script_pushint(st,nd ? nd->bl.id : st->oid);
  6859. break;
  6860. default:
  6861. ShowError("buildin_getnpcid: invalid parameter (%d).\n", num);
  6862. script_pushint(st,0);
  6863. return SCRIPT_CMD_FAILURE;
  6864. }
  6865. return SCRIPT_CMD_SUCCESS;
  6866. }
  6867. /*==========================================
  6868. * Return the name of the party_id
  6869. * null if not found
  6870. *------------------------------------------*/
  6871. BUILDIN_FUNC(getpartyname)
  6872. {
  6873. int party_id;
  6874. struct party_data* p;
  6875. party_id = script_getnum(st,2);
  6876. if( ( p = party_search(party_id) ) != NULL )
  6877. {
  6878. script_pushstrcopy(st,p->party.name);
  6879. }
  6880. else
  6881. {
  6882. script_pushconststr(st,"null");
  6883. }
  6884. return SCRIPT_CMD_SUCCESS;
  6885. }
  6886. /*==========================================
  6887. * Get the information of the members of a party by type
  6888. * @party_id, @type
  6889. * return by @type :
  6890. * - : nom des membres
  6891. * 1 : char_id des membres
  6892. * 2 : account_id des membres
  6893. *------------------------------------------*/
  6894. BUILDIN_FUNC(getpartymember)
  6895. {
  6896. struct party_data *p;
  6897. uint8 j = 0;
  6898. p = party_search(script_getnum(st,2));
  6899. if (p != NULL) {
  6900. uint8 i, type = 0;
  6901. struct script_data *data = NULL;
  6902. char *varname = NULL;
  6903. if (script_hasdata(st,3))
  6904. type = script_getnum(st,3);
  6905. if (script_hasdata(st,4)) {
  6906. data = script_getdata(st, 4);
  6907. if (!data_isreference(data)) {
  6908. ShowError("buildin_getpartymember: Error in argument! Please give a variable to store values in.\n");
  6909. return SCRIPT_CMD_FAILURE;
  6910. }
  6911. varname = reference_getname(data);
  6912. if (type <= 0 && varname[strlen(varname)-1] != '$') {
  6913. ShowError("buildin_getpartymember: The array %s is not string type.\n", varname);
  6914. return SCRIPT_CMD_FAILURE;
  6915. }
  6916. }
  6917. for (i = 0; i < MAX_PARTY; i++) {
  6918. if (p->party.member[i].account_id) {
  6919. switch (type) {
  6920. case 2:
  6921. if (data)
  6922. setd_sub(st, NULL, varname, j, (void *)__64BPRTSIZE(p->party.member[i].account_id), data->ref);
  6923. else
  6924. mapreg_setreg(reference_uid(add_str("$@partymemberaid"), j),p->party.member[i].account_id);
  6925. break;
  6926. case 1:
  6927. if (data)
  6928. setd_sub(st, NULL, varname, j, (void *)__64BPRTSIZE(p->party.member[i].char_id), data->ref);
  6929. else
  6930. mapreg_setreg(reference_uid(add_str("$@partymembercid"), j),p->party.member[i].char_id);
  6931. break;
  6932. default:
  6933. if (data)
  6934. setd_sub(st, NULL, varname, j, (void *)__64BPRTSIZE(p->party.member[i].name), data->ref);
  6935. else
  6936. mapreg_setregstr(reference_uid(add_str("$@partymembername$"), j),p->party.member[i].name);
  6937. break;
  6938. }
  6939. j++;
  6940. }
  6941. }
  6942. }
  6943. mapreg_setreg(add_str("$@partymembercount"),j);
  6944. script_pushint(st, j);
  6945. return SCRIPT_CMD_SUCCESS;
  6946. }
  6947. /*==========================================
  6948. * Retrieves party leader. if flag is specified,
  6949. * return some of the leader data. Otherwise, return name.
  6950. *------------------------------------------*/
  6951. BUILDIN_FUNC(getpartyleader)
  6952. {
  6953. int party_id, type = 0, i=0;
  6954. struct party_data *p;
  6955. party_id=script_getnum(st,2);
  6956. if( script_hasdata(st,3) )
  6957. type=script_getnum(st,3);
  6958. p=party_search(party_id);
  6959. if (p) //Search leader
  6960. for(i = 0; i < MAX_PARTY && !p->party.member[i].leader; i++);
  6961. if (!p || i == MAX_PARTY) { //leader not found
  6962. if (type)
  6963. script_pushint(st,-1);
  6964. else
  6965. script_pushconststr(st,"null");
  6966. return SCRIPT_CMD_SUCCESS;
  6967. }
  6968. switch (type) {
  6969. case 1: script_pushint(st,p->party.member[i].account_id); break;
  6970. case 2: script_pushint(st,p->party.member[i].char_id); break;
  6971. case 3: script_pushint(st,p->party.member[i].class_); break;
  6972. case 4: script_pushstrcopy(st,mapindex_id2name(p->party.member[i].map)); break;
  6973. case 5: script_pushint(st,p->party.member[i].lv); break;
  6974. default: script_pushstrcopy(st,p->party.member[i].name); break;
  6975. }
  6976. return SCRIPT_CMD_SUCCESS;
  6977. }
  6978. /*==========================================
  6979. * Return the name of the @guild_id
  6980. * null if not found
  6981. *------------------------------------------*/
  6982. BUILDIN_FUNC(getguildname)
  6983. {
  6984. int guild_id;
  6985. struct guild* g;
  6986. guild_id = script_getnum(st,2);
  6987. if( ( g = guild_search(guild_id) ) != NULL )
  6988. script_pushstrcopy(st,g->name);
  6989. else
  6990. script_pushconststr(st,"null");
  6991. return SCRIPT_CMD_SUCCESS;
  6992. }
  6993. /*==========================================
  6994. * Return the name of the guild master of @guild_id
  6995. * null if not found
  6996. *------------------------------------------*/
  6997. BUILDIN_FUNC(getguildmaster)
  6998. {
  6999. int guild_id;
  7000. struct guild* g;
  7001. guild_id = script_getnum(st,2);
  7002. if( ( g = guild_search(guild_id) ) != NULL )
  7003. script_pushstrcopy(st,g->member[0].name);
  7004. else
  7005. script_pushconststr(st,"null");
  7006. return SCRIPT_CMD_SUCCESS;
  7007. }
  7008. BUILDIN_FUNC(getguildmasterid)
  7009. {
  7010. int guild_id;
  7011. struct guild* g;
  7012. guild_id = script_getnum(st,2);
  7013. if( ( g = guild_search(guild_id) ) != NULL )
  7014. script_pushint(st,g->member[0].char_id);
  7015. else
  7016. script_pushint(st,0);
  7017. return SCRIPT_CMD_SUCCESS;
  7018. }
  7019. /*==========================================
  7020. * Get char string information by type :
  7021. * Return by @type :
  7022. * 0 : char_name
  7023. * 1 : party_name or ""
  7024. * 2 : guild_name or ""
  7025. * 3 : map_name
  7026. * - : ""
  7027. * strcharinfo(<type>{,<char_id>})
  7028. *------------------------------------------*/
  7029. BUILDIN_FUNC(strcharinfo)
  7030. {
  7031. TBL_PC *sd;
  7032. int num;
  7033. struct guild* g;
  7034. struct party_data* p;
  7035. if (!script_charid2sd(3,sd)) {
  7036. script_pushconststr(st,"");
  7037. return SCRIPT_CMD_FAILURE;
  7038. }
  7039. num=script_getnum(st,2);
  7040. switch(num){
  7041. case 0:
  7042. script_pushstrcopy(st,sd->status.name);
  7043. break;
  7044. case 1:
  7045. if( ( p = party_search(sd->status.party_id) ) != NULL ) {
  7046. script_pushstrcopy(st,p->party.name);
  7047. } else {
  7048. script_pushconststr(st,"");
  7049. }
  7050. break;
  7051. case 2:
  7052. if( ( g = sd->guild ) != NULL ) {
  7053. script_pushstrcopy(st,g->name);
  7054. } else {
  7055. script_pushconststr(st,"");
  7056. }
  7057. break;
  7058. case 3:
  7059. script_pushconststr(st,map[sd->bl.m].name);
  7060. break;
  7061. default:
  7062. ShowWarning("buildin_strcharinfo: unknown parameter.\n");
  7063. script_pushconststr(st,"");
  7064. break;
  7065. }
  7066. return SCRIPT_CMD_SUCCESS;
  7067. }
  7068. /*==========================================
  7069. * Get npc string information by type
  7070. * return by @type:
  7071. * 0 : name
  7072. * 1 : str#
  7073. * 2 : #str
  7074. * 3 : ::str
  7075. * 4 : map name
  7076. *------------------------------------------*/
  7077. BUILDIN_FUNC(strnpcinfo)
  7078. {
  7079. TBL_NPC* nd;
  7080. int num;
  7081. char *buf,*name=NULL;
  7082. nd = map_id2nd(st->oid);
  7083. if (!nd) {
  7084. script_pushconststr(st, "");
  7085. return SCRIPT_CMD_SUCCESS;
  7086. }
  7087. num = script_getnum(st,2);
  7088. switch(num){
  7089. case 0: // display name
  7090. name = aStrdup(nd->name);
  7091. break;
  7092. case 1: // visible part of display name
  7093. if((buf = strchr(nd->name,'#')) != NULL)
  7094. {
  7095. name = aStrdup(nd->name);
  7096. name[buf - nd->name] = 0;
  7097. } else // Return the name, there is no '#' present
  7098. name = aStrdup(nd->name);
  7099. break;
  7100. case 2: // # fragment
  7101. if((buf = strchr(nd->name,'#')) != NULL)
  7102. name = aStrdup(buf+1);
  7103. break;
  7104. case 3: // unique name
  7105. name = aStrdup(nd->exname);
  7106. break;
  7107. case 4: // map name
  7108. name = aStrdup(map[nd->bl.m].name);
  7109. break;
  7110. }
  7111. if(name)
  7112. script_pushstr(st, name);
  7113. else
  7114. script_pushconststr(st, "");
  7115. return SCRIPT_CMD_SUCCESS;
  7116. }
  7117. /// aegis->athena slot position conversion table
  7118. /// Index is used by EQI_ in db/const.txt
  7119. static unsigned int equip[] = {
  7120. EQP_HEAD_TOP,
  7121. EQP_ARMOR,
  7122. EQP_HAND_L,
  7123. EQP_HAND_R,
  7124. EQP_GARMENT,
  7125. EQP_SHOES,
  7126. EQP_ACC_L,
  7127. EQP_ACC_R,
  7128. EQP_HEAD_MID,
  7129. EQP_HEAD_LOW,
  7130. EQP_COSTUME_HEAD_LOW,
  7131. EQP_COSTUME_HEAD_MID,
  7132. EQP_COSTUME_HEAD_TOP,
  7133. EQP_COSTUME_GARMENT,
  7134. EQP_AMMO,
  7135. EQP_SHADOW_ARMOR,
  7136. EQP_SHADOW_WEAPON,
  7137. EQP_SHADOW_SHIELD,
  7138. EQP_SHADOW_SHOES,
  7139. EQP_SHADOW_ACC_R,
  7140. EQP_SHADOW_ACC_L
  7141. };
  7142. /**
  7143. * getequipid(<equipment slot>{,<char_id>})
  7144. **/
  7145. BUILDIN_FUNC(getequipid)
  7146. {
  7147. int i, num;
  7148. TBL_PC* sd;
  7149. struct item_data* item;
  7150. if (!script_charid2sd(3, sd)) {
  7151. script_pushint(st,-1);
  7152. return SCRIPT_CMD_FAILURE;
  7153. }
  7154. num = script_getnum(st,2) - 1;
  7155. if( num < 0 || num >= ARRAYLENGTH(equip) )
  7156. {
  7157. script_pushint(st,-1);
  7158. return SCRIPT_CMD_FAILURE;
  7159. }
  7160. // get inventory position of item
  7161. i = pc_checkequip(sd,equip[num]);
  7162. if( i < 0 )
  7163. {
  7164. script_pushint(st,-1);
  7165. return SCRIPT_CMD_SUCCESS;
  7166. }
  7167. item = sd->inventory_data[i];
  7168. if( item != 0 )
  7169. script_pushint(st,item->nameid);
  7170. else
  7171. script_pushint(st,0);
  7172. return SCRIPT_CMD_SUCCESS;
  7173. }
  7174. /*==========================================
  7175. * getequipuniqueid(<equipment slot>{,<char_id>})
  7176. *------------------------------------------*/
  7177. BUILDIN_FUNC(getequipuniqueid)
  7178. {
  7179. int i, num;
  7180. TBL_PC* sd;
  7181. struct item* item;
  7182. if (!script_charid2sd(3, sd)) {
  7183. script_pushconststr(st, "");
  7184. return SCRIPT_CMD_FAILURE;
  7185. }
  7186. num = script_getnum(st,2) - 1;
  7187. if (num < 0 || num >= ARRAYLENGTH(equip)) {
  7188. script_pushconststr(st, "");
  7189. return SCRIPT_CMD_FAILURE;
  7190. }
  7191. // get inventory position of item
  7192. i = pc_checkequip(sd,equip[num]);
  7193. if (i < 0) {
  7194. script_pushconststr(st, "");
  7195. return SCRIPT_CMD_FAILURE;
  7196. }
  7197. item = &sd->status.inventory[i];
  7198. if (item != 0) {
  7199. int maxlen = 256;
  7200. char *buf = (char *)aMalloc(maxlen*sizeof(char));
  7201. memset(buf, 0, maxlen);
  7202. snprintf(buf, maxlen-1, "%llu", (unsigned long long)item->unique_id);
  7203. script_pushstr(st, buf);
  7204. } else
  7205. script_pushconststr(st, "");
  7206. return SCRIPT_CMD_SUCCESS;
  7207. }
  7208. /*==========================================
  7209. * Get the equipement name at pos
  7210. * return item jname or ""
  7211. * getequipname(<equipment slot>{,<char_id>})
  7212. *------------------------------------------*/
  7213. BUILDIN_FUNC(getequipname)
  7214. {
  7215. int i, num;
  7216. TBL_PC* sd;
  7217. struct item_data* item;
  7218. if (!script_charid2sd(3, sd)) {
  7219. script_pushconststr(st, "");
  7220. return SCRIPT_CMD_FAILURE;
  7221. }
  7222. num = script_getnum(st,2) - 1;
  7223. if( num < 0 || num >= ARRAYLENGTH(equip) )
  7224. {
  7225. script_pushconststr(st,"");
  7226. return SCRIPT_CMD_SUCCESS;
  7227. }
  7228. // get inventory position of item
  7229. i = pc_checkequip(sd,equip[num]);
  7230. if( i < 0 )
  7231. {
  7232. script_pushconststr(st,"");
  7233. return SCRIPT_CMD_SUCCESS;
  7234. }
  7235. item = sd->inventory_data[i];
  7236. if( item != 0 )
  7237. script_pushstrcopy(st,item->jname);
  7238. else
  7239. script_pushconststr(st,"");
  7240. return SCRIPT_CMD_SUCCESS;
  7241. }
  7242. /*==========================================
  7243. * getbrokenid [Valaris]
  7244. * getbrokenid(<number>{,<char_id>})
  7245. *------------------------------------------*/
  7246. BUILDIN_FUNC(getbrokenid)
  7247. {
  7248. int i,num,id=0,brokencounter=0;
  7249. TBL_PC *sd;
  7250. if (!script_charid2sd(3, sd)) {
  7251. script_pushint(st,0);
  7252. return SCRIPT_CMD_FAILURE;
  7253. }
  7254. num=script_getnum(st,2);
  7255. for(i=0; i<MAX_INVENTORY; i++) {
  7256. if(sd->status.inventory[i].attribute){
  7257. brokencounter++;
  7258. if(num==brokencounter){
  7259. id=sd->status.inventory[i].nameid;
  7260. break;
  7261. }
  7262. }
  7263. }
  7264. script_pushint(st,id);
  7265. return SCRIPT_CMD_SUCCESS;
  7266. }
  7267. /*==========================================
  7268. * repair [Valaris]
  7269. * repair <num>{,<char_id>};
  7270. *------------------------------------------*/
  7271. BUILDIN_FUNC(repair)
  7272. {
  7273. int i,num;
  7274. int repaircounter=0;
  7275. TBL_PC *sd;
  7276. if (!script_charid2sd(3,sd))
  7277. return SCRIPT_CMD_FAILURE;
  7278. num=script_getnum(st,2);
  7279. for(i=0; i<MAX_INVENTORY; i++) {
  7280. if(sd->status.inventory[i].attribute){
  7281. repaircounter++;
  7282. if(num==repaircounter){
  7283. sd->status.inventory[i].attribute=0;
  7284. clif_equiplist(sd);
  7285. clif_produceeffect(sd, 0, sd->status.inventory[i].nameid);
  7286. clif_misceffect(&sd->bl, 3);
  7287. break;
  7288. }
  7289. }
  7290. }
  7291. return SCRIPT_CMD_SUCCESS;
  7292. }
  7293. /*==========================================
  7294. * repairall {<char_id>}
  7295. *------------------------------------------*/
  7296. BUILDIN_FUNC(repairall)
  7297. {
  7298. int i, repaircounter = 0;
  7299. TBL_PC *sd;
  7300. if (!script_charid2sd(2,sd))
  7301. return SCRIPT_CMD_FAILURE;
  7302. for(i = 0; i < MAX_INVENTORY; i++)
  7303. {
  7304. if(sd->status.inventory[i].nameid && sd->status.inventory[i].attribute)
  7305. {
  7306. sd->status.inventory[i].attribute = 0;
  7307. clif_produceeffect(sd,0,sd->status.inventory[i].nameid);
  7308. repaircounter++;
  7309. }
  7310. }
  7311. if(repaircounter)
  7312. {
  7313. clif_misceffect(&sd->bl, 3);
  7314. clif_equiplist(sd);
  7315. }
  7316. return SCRIPT_CMD_SUCCESS;
  7317. }
  7318. /*==========================================
  7319. * Chk if player have something equiped at pos
  7320. * getequipisequiped <pos>{,<char_id>}
  7321. *------------------------------------------*/
  7322. BUILDIN_FUNC(getequipisequiped)
  7323. {
  7324. int i = -1,num;
  7325. TBL_PC *sd;
  7326. num = script_getnum(st,2);
  7327. if (!script_charid2sd(3,sd))
  7328. return SCRIPT_CMD_FAILURE;
  7329. if (num > 0 && num <= ARRAYLENGTH(equip))
  7330. i=pc_checkequip(sd,equip[num-1]);
  7331. if(i >= 0)
  7332. script_pushint(st,1);
  7333. else
  7334. script_pushint(st,0);
  7335. return SCRIPT_CMD_SUCCESS;
  7336. }
  7337. /*==========================================
  7338. * Chk if the player have something equiped at pos
  7339. * if so chk if this item ain't marked not refinable or rental
  7340. * return (npc)
  7341. * 1 : true
  7342. * 0 : false
  7343. * getequipisenableref(<equipment slot>{,<char_id>})
  7344. *------------------------------------------*/
  7345. BUILDIN_FUNC(getequipisenableref)
  7346. {
  7347. int i = -1,num;
  7348. TBL_PC *sd;
  7349. num = script_getnum(st,2);
  7350. if (!script_charid2sd(3, sd)) {
  7351. script_pushint(st,0);
  7352. return SCRIPT_CMD_FAILURE;
  7353. }
  7354. if( num > 0 && num <= ARRAYLENGTH(equip) )
  7355. i = pc_checkequip(sd,equip[num-1]);
  7356. if( i >= 0 && sd->inventory_data[i] && !sd->inventory_data[i]->flag.no_refine && !sd->status.inventory[i].expire_time )
  7357. script_pushint(st,1);
  7358. else
  7359. script_pushint(st,0);
  7360. return SCRIPT_CMD_SUCCESS;
  7361. }
  7362. /*==========================================
  7363. * Get the item refined value at pos
  7364. * return (npc)
  7365. * x : refine amount
  7366. * 0 : false (not refined)
  7367. * getequiprefinerycnt(<equipment slot>{,<char_id>})
  7368. *------------------------------------------*/
  7369. BUILDIN_FUNC(getequiprefinerycnt)
  7370. {
  7371. int i = -1,num;
  7372. TBL_PC *sd;
  7373. num = script_getnum(st,2);
  7374. if (!script_charid2sd(3, sd)) {
  7375. script_pushint(st,0);
  7376. return SCRIPT_CMD_FAILURE;
  7377. }
  7378. if (num > 0 && num <= ARRAYLENGTH(equip))
  7379. i=pc_checkequip(sd,equip[num-1]);
  7380. if(i >= 0)
  7381. script_pushint(st,sd->status.inventory[i].refine);
  7382. else
  7383. script_pushint(st,0);
  7384. return SCRIPT_CMD_SUCCESS;
  7385. }
  7386. /*==========================================
  7387. * Get the weapon level value at pos
  7388. * (pos should normally only be EQI_HAND_L or EQI_HAND_R)
  7389. * return (npc)
  7390. * x : weapon level
  7391. * 0 : false
  7392. * getequipweaponlv(<equipment slot>{,<char_id>})
  7393. *------------------------------------------*/
  7394. BUILDIN_FUNC(getequipweaponlv)
  7395. {
  7396. int i = -1, num;
  7397. TBL_PC *sd;
  7398. num = script_getnum(st, 2);
  7399. if (!script_charid2sd(3, sd)) {
  7400. script_pushint(st, 0);
  7401. return SCRIPT_CMD_FAILURE;
  7402. }
  7403. if (num == -1)
  7404. i = current_equip_item_index;
  7405. else if (num > 0 && num <= ARRAYLENGTH(equip))
  7406. i = pc_checkequip(sd, equip[num - 1]);
  7407. if (i >= 0 && sd->inventory_data[i])
  7408. script_pushint(st, sd->inventory_data[i]->wlv);
  7409. else
  7410. script_pushint(st, 0);
  7411. return SCRIPT_CMD_SUCCESS;
  7412. }
  7413. /*==========================================
  7414. * Get the item refine chance (from refine.txt) for item at pos
  7415. * return (npc)
  7416. * x : refine chance
  7417. * 0 : false (max refine level or unequip..)
  7418. * getequippercentrefinery(<equipment slot>{,<char_id>})
  7419. *------------------------------------------*/
  7420. BUILDIN_FUNC(getequippercentrefinery)
  7421. {
  7422. int i = -1,num;
  7423. TBL_PC *sd;
  7424. num = script_getnum(st,2);
  7425. if (!script_charid2sd(3, sd)) {
  7426. script_pushint(st,0);
  7427. return SCRIPT_CMD_FAILURE;
  7428. }
  7429. if (num > 0 && num <= ARRAYLENGTH(equip))
  7430. i = pc_checkequip(sd,equip[num-1]);
  7431. if(i >= 0 && sd->status.inventory[i].nameid && sd->status.inventory[i].refine < MAX_REFINE)
  7432. script_pushint(st,status_get_refine_chance((enum refine_type)itemdb_wlv(sd->status.inventory[i].nameid), (int)sd->status.inventory[i].refine));
  7433. else
  7434. script_pushint(st,0);
  7435. return SCRIPT_CMD_SUCCESS;
  7436. }
  7437. /*==========================================
  7438. * Refine +1 item at pos and log and display refine
  7439. * successrefitem <equipment slot>{,<count>{,<char_id>}}
  7440. *------------------------------------------*/
  7441. BUILDIN_FUNC(successrefitem) {
  7442. short i = -1, up = 1;
  7443. int pos;
  7444. TBL_PC *sd;
  7445. pos = script_getnum(st,2);
  7446. if (!script_charid2sd(4, sd)) {
  7447. script_pushint(st, -1);
  7448. return SCRIPT_CMD_FAILURE;
  7449. }
  7450. if (script_hasdata(st, 3))
  7451. up = script_getnum(st, 3);
  7452. if (pos > 0 && pos <= ARRAYLENGTH(equip))
  7453. i = pc_checkequip(sd,equip[pos-1]);
  7454. if (i >= 0) {
  7455. unsigned int ep = sd->status.inventory[i].equip;
  7456. //Logs items, got from (N)PC scripts [Lupus]
  7457. log_pick_pc(sd, LOG_TYPE_SCRIPT, -1, &sd->status.inventory[i]);
  7458. if (sd->status.inventory[i].refine >= MAX_REFINE) {
  7459. script_pushint(st, MAX_REFINE);
  7460. return SCRIPT_CMD_SUCCESS;
  7461. }
  7462. sd->status.inventory[i].refine += up;
  7463. sd->status.inventory[i].refine = cap_value( sd->status.inventory[i].refine, 0, MAX_REFINE);
  7464. pc_unequipitem(sd,i,2); // status calc will happen in pc_equipitem() below
  7465. clif_refine(sd->fd,0,i,sd->status.inventory[i].refine);
  7466. clif_delitem(sd,i,1,3);
  7467. //Logs items, got from (N)PC scripts [Lupus]
  7468. log_pick_pc(sd, LOG_TYPE_SCRIPT, 1, &sd->status.inventory[i]);
  7469. clif_additem(sd,i,1,0);
  7470. pc_equipitem(sd,i,ep);
  7471. clif_misceffect(&sd->bl,3);
  7472. if (sd->status.inventory[i].refine == MAX_REFINE &&
  7473. sd->status.inventory[i].card[0] == CARD0_FORGE &&
  7474. sd->status.char_id == (int)MakeDWord(sd->status.inventory[i].card[2],sd->status.inventory[i].card[3]))
  7475. { // Fame point system [DracoRPG]
  7476. switch (sd->inventory_data[i]->wlv){
  7477. case 1:
  7478. pc_addfame(sd, battle_config.fame_refine_lv1); // Success to refine to +10 a lv1 weapon you forged = +1 fame point
  7479. break;
  7480. case 2:
  7481. pc_addfame(sd, battle_config.fame_refine_lv2); // Success to refine to +10 a lv2 weapon you forged = +25 fame point
  7482. break;
  7483. case 3:
  7484. pc_addfame(sd, battle_config.fame_refine_lv3); // Success to refine to +10 a lv3 weapon you forged = +1000 fame point
  7485. break;
  7486. }
  7487. }
  7488. script_pushint(st, sd->status.inventory[i].refine);
  7489. return SCRIPT_CMD_SUCCESS;
  7490. }
  7491. ShowError("buildin_successrefitem: No item equipped at pos %d (CID=%d/AID=%d).\n", pos, sd->status.char_id, sd->status.account_id);
  7492. script_pushint(st, -1);
  7493. return SCRIPT_CMD_FAILURE;
  7494. }
  7495. /*==========================================
  7496. * Show a failed Refine +1 attempt
  7497. * failedrefitem <equipment slot>{,<char_id>}
  7498. *------------------------------------------*/
  7499. BUILDIN_FUNC(failedrefitem) {
  7500. short i = -1;
  7501. int pos;
  7502. TBL_PC *sd;
  7503. pos = script_getnum(st,2);
  7504. if (!script_charid2sd(3, sd)) {
  7505. script_pushint(st, 0);
  7506. return SCRIPT_CMD_FAILURE;
  7507. }
  7508. if (pos > 0 && pos <= ARRAYLENGTH(equip))
  7509. i = pc_checkequip(sd,equip[pos-1]);
  7510. if (i >= 0) {
  7511. sd->status.inventory[i].refine = 0;
  7512. pc_unequipitem(sd,i,3); //recalculate bonus
  7513. clif_refine(sd->fd,1,i,sd->status.inventory[i].refine); //notify client of failure
  7514. pc_delitem(sd,i,1,0,2,LOG_TYPE_SCRIPT);
  7515. clif_misceffect(&sd->bl,2); // display failure effect
  7516. script_pushint(st, 1);
  7517. return SCRIPT_CMD_SUCCESS;
  7518. }
  7519. ShowError("buildin_failedrefitem: No item equipped at pos %d (CID=%d/AID=%d).\n", pos, sd->status.char_id, sd->status.account_id);
  7520. script_pushint(st, 0);
  7521. return SCRIPT_CMD_FAILURE;
  7522. }
  7523. /*==========================================
  7524. * Downgrades an Equipment Part by -1 . [Masao]
  7525. * downrefitem <equipment slot>{,<count>{,<char_id>}}
  7526. *------------------------------------------*/
  7527. BUILDIN_FUNC(downrefitem) {
  7528. short i = -1, down = 1;
  7529. int pos;
  7530. TBL_PC *sd;
  7531. if (!script_charid2sd(4, sd)) {
  7532. script_pushint(st, -1);
  7533. return SCRIPT_CMD_FAILURE;
  7534. }
  7535. pos = script_getnum(st,2);
  7536. if (script_hasdata(st, 3))
  7537. down = script_getnum(st, 3);
  7538. if (pos > 0 && pos <= ARRAYLENGTH(equip))
  7539. i = pc_checkequip(sd,equip[pos-1]);
  7540. if (i >= 0) {
  7541. unsigned int ep = sd->status.inventory[i].equip;
  7542. //Logs items, got from (N)PC scripts [Lupus]
  7543. log_pick_pc(sd, LOG_TYPE_SCRIPT, -1, &sd->status.inventory[i]);
  7544. pc_unequipitem(sd,i,2); // status calc will happen in pc_equipitem() below
  7545. sd->status.inventory[i].refine -= down;
  7546. sd->status.inventory[i].refine = cap_value( sd->status.inventory[i].refine, 0, MAX_REFINE);
  7547. clif_refine(sd->fd,2,i,sd->status.inventory[i].refine);
  7548. clif_delitem(sd,i,1,3);
  7549. //Logs items, got from (N)PC scripts [Lupus]
  7550. log_pick_pc(sd, LOG_TYPE_SCRIPT, 1, &sd->status.inventory[i]);
  7551. clif_additem(sd,i,1,0);
  7552. pc_equipitem(sd,i,ep);
  7553. clif_misceffect(&sd->bl,2);
  7554. script_pushint(st, sd->status.inventory[i].refine);
  7555. return SCRIPT_CMD_SUCCESS;
  7556. }
  7557. ShowError("buildin_downrefitem: No item equipped at pos %d (CID=%d/AID=%d).\n", pos, sd->status.char_id, sd->status.account_id);
  7558. script_pushint(st, -1);
  7559. return SCRIPT_CMD_FAILURE;
  7560. }
  7561. /**
  7562. * Delete the item equipped at pos.
  7563. * delequip <equipment slot>{,<char_id>};
  7564. **/
  7565. BUILDIN_FUNC(delequip) {
  7566. short i = -1;
  7567. int pos;
  7568. int8 ret;
  7569. TBL_PC *sd;
  7570. pos = script_getnum(st,2);
  7571. if (!script_charid2sd(3,sd)) {
  7572. st->state = END;
  7573. return SCRIPT_CMD_FAILURE;
  7574. }
  7575. if (pos > 0 && pos <= ARRAYLENGTH(equip))
  7576. i = pc_checkequip(sd,equip[pos-1]);
  7577. if (i >= 0) {
  7578. pc_unequipitem(sd,i,3); //recalculate bonus
  7579. ret = !(pc_delitem(sd,i,1,0,2,LOG_TYPE_SCRIPT));
  7580. }
  7581. else {
  7582. ShowError("buildin_delequip: No item equipped at pos %d (CID=%d/AID=%d).\n", pos, sd->status.char_id, sd->status.account_id);
  7583. st->state = END;
  7584. return SCRIPT_CMD_FAILURE;
  7585. }
  7586. script_pushint(st,ret);
  7587. return SCRIPT_CMD_SUCCESS;
  7588. }
  7589. /**
  7590. * Break the item equipped at pos.
  7591. * breakequip <equipment slot>{,<char_id>};
  7592. **/
  7593. BUILDIN_FUNC(breakequip) {
  7594. short i = -1;
  7595. int pos;
  7596. TBL_PC *sd;
  7597. pos = script_getnum(st,2);
  7598. if (!script_charid2sd(3,sd))
  7599. return SCRIPT_CMD_FAILURE;
  7600. if (pos > 0 && pos <= ARRAYLENGTH(equip))
  7601. i = pc_checkequip(sd,equip[pos-1]);
  7602. if (i >= 0) {
  7603. sd->status.inventory[i].attribute = 1;
  7604. pc_unequipitem(sd,i,3);
  7605. clif_equiplist(sd);
  7606. script_pushint(st,1);
  7607. return SCRIPT_CMD_SUCCESS;
  7608. }
  7609. ShowError("buildin_breakequip: No item equipped at pos %d (CID=%d/AID=%d).\n", pos, sd->status.char_id, sd->status.account_id);
  7610. script_pushint(st,0);
  7611. return SCRIPT_CMD_FAILURE;
  7612. }
  7613. /**
  7614. * statusup <stat>{,<char_id>};
  7615. **/
  7616. BUILDIN_FUNC(statusup)
  7617. {
  7618. int type;
  7619. TBL_PC *sd;
  7620. type = script_getnum(st,2);
  7621. if (!script_charid2sd(3, sd))
  7622. return SCRIPT_CMD_FAILURE;
  7623. pc_statusup(sd, type, 1);
  7624. return SCRIPT_CMD_SUCCESS;
  7625. }
  7626. /**
  7627. * statusup2 <stat>,<amount>{,<char_id>};
  7628. **/
  7629. BUILDIN_FUNC(statusup2)
  7630. {
  7631. int type,val;
  7632. TBL_PC *sd;
  7633. type=script_getnum(st,2);
  7634. val=script_getnum(st,3);
  7635. if (!script_charid2sd(4, sd))
  7636. return SCRIPT_CMD_FAILURE;
  7637. pc_statusup2(sd,type,val);
  7638. return SCRIPT_CMD_SUCCESS;
  7639. }
  7640. /// See 'doc/item_bonus.txt'
  7641. ///
  7642. /// bonus <bonus type>,<val1>;
  7643. /// bonus2 <bonus type>,<val1>,<val2>;
  7644. /// bonus3 <bonus type>,<val1>,<val2>,<val3>;
  7645. /// bonus4 <bonus type>,<val1>,<val2>,<val3>,<val4>;
  7646. /// bonus5 <bonus type>,<val1>,<val2>,<val3>,<val4>,<val5>;
  7647. BUILDIN_FUNC(bonus)
  7648. {
  7649. int type;
  7650. int val1;
  7651. int val2 = 0;
  7652. int val3 = 0;
  7653. int val4 = 0;
  7654. int val5 = 0;
  7655. TBL_PC* sd;
  7656. struct script_data *data;
  7657. sd = script_rid2sd(st);
  7658. if( sd == NULL )
  7659. return SCRIPT_CMD_SUCCESS; // no player attached
  7660. type = script_getnum(st,2);
  7661. switch( type ) {
  7662. case SP_AUTOSPELL:
  7663. case SP_AUTOSPELL_WHENHIT:
  7664. case SP_AUTOSPELL_ONSKILL:
  7665. case SP_SKILL_ATK:
  7666. case SP_SKILL_HEAL:
  7667. case SP_SKILL_HEAL2:
  7668. case SP_ADD_SKILL_BLOW:
  7669. case SP_CASTRATE:
  7670. case SP_ADDEFF_ONSKILL:
  7671. case SP_SKILL_USE_SP_RATE:
  7672. case SP_SKILL_COOLDOWN:
  7673. case SP_SKILL_FIXEDCAST:
  7674. case SP_SKILL_VARIABLECAST:
  7675. case SP_VARCASTRATE:
  7676. case SP_FIXCASTRATE:
  7677. case SP_SKILL_USE_SP:
  7678. case SP_SUB_SKILL:
  7679. // these bonuses support skill names
  7680. data = script_getdata(st, 3);
  7681. get_val(st, data); // Convert into value in case of a variable
  7682. val1 = ( data_isstring(data) ? skill_name2id(script_getstr(st,3)) : script_getnum(st,3) );
  7683. break;
  7684. default:
  7685. val1 = script_getnum(st,3);
  7686. break;
  7687. }
  7688. switch( script_lastdata(st)-2 ) {
  7689. case 1:
  7690. pc_bonus(sd, type, val1);
  7691. break;
  7692. case 2:
  7693. val2 = script_getnum(st,4);
  7694. pc_bonus2(sd, type, val1, val2);
  7695. break;
  7696. case 3:
  7697. val2 = script_getnum(st,4);
  7698. val3 = script_getnum(st,5);
  7699. pc_bonus3(sd, type, val1, val2, val3);
  7700. break;
  7701. case 4:
  7702. data = script_getdata(st, 4);
  7703. get_val(st, data); // Convert into value in case of a variable
  7704. if( type == SP_AUTOSPELL_ONSKILL && data_isstring(data) )
  7705. val2 = skill_name2id(script_getstr(st,4)); // 2nd value can be skill name
  7706. else
  7707. val2 = script_getnum(st,4);
  7708. val3 = script_getnum(st,5);
  7709. val4 = script_getnum(st,6);
  7710. pc_bonus4(sd, type, val1, val2, val3, val4);
  7711. break;
  7712. case 5:
  7713. data = script_getdata(st, 4);
  7714. get_val(st, data); // Convert into value in case of a variable
  7715. if( type == SP_AUTOSPELL_ONSKILL && data_isstring(data) )
  7716. val2 = skill_name2id(script_getstr(st,4)); // 2nd value can be skill name
  7717. else
  7718. val2 = script_getnum(st,4);
  7719. val3 = script_getnum(st,5);
  7720. val4 = script_getnum(st,6);
  7721. val5 = script_getnum(st,7);
  7722. pc_bonus5(sd, type, val1, val2, val3, val4, val5);
  7723. break;
  7724. default:
  7725. ShowDebug("buildin_bonus: unexpected number of arguments (%d)\n", (script_lastdata(st) - 1));
  7726. break;
  7727. }
  7728. return SCRIPT_CMD_SUCCESS;
  7729. }
  7730. BUILDIN_FUNC(autobonus)
  7731. {
  7732. unsigned int dur, pos;
  7733. short rate;
  7734. short atk_type = 0;
  7735. TBL_PC* sd;
  7736. const char *bonus_script, *other_script = NULL;
  7737. sd = script_rid2sd(st);
  7738. if( sd == NULL )
  7739. return SCRIPT_CMD_SUCCESS; // no player attached
  7740. if (current_equip_combo_pos)
  7741. pos = current_equip_combo_pos;
  7742. else
  7743. pos = sd->status.inventory[current_equip_item_index].equip;
  7744. if((sd->state.autobonus&pos) == pos)
  7745. return SCRIPT_CMD_SUCCESS;
  7746. rate = script_getnum(st,3);
  7747. dur = script_getnum(st,4);
  7748. bonus_script = script_getstr(st,2);
  7749. if( !rate || !dur || !pos || !bonus_script )
  7750. return SCRIPT_CMD_SUCCESS;
  7751. if( script_hasdata(st,5) )
  7752. atk_type = script_getnum(st,5);
  7753. if( script_hasdata(st,6) )
  7754. other_script = script_getstr(st,6);
  7755. if( pc_addautobonus(sd->autobonus,ARRAYLENGTH(sd->autobonus),
  7756. bonus_script,rate,dur,atk_type,other_script,pos,false) )
  7757. {
  7758. script_add_autobonus(bonus_script);
  7759. if( other_script )
  7760. script_add_autobonus(other_script);
  7761. }
  7762. return SCRIPT_CMD_SUCCESS;
  7763. }
  7764. BUILDIN_FUNC(autobonus2)
  7765. {
  7766. unsigned int dur, pos;
  7767. short rate;
  7768. short atk_type = 0;
  7769. TBL_PC* sd;
  7770. const char *bonus_script, *other_script = NULL;
  7771. sd = script_rid2sd(st);
  7772. if( sd == NULL )
  7773. return SCRIPT_CMD_SUCCESS; // no player attached
  7774. if (current_equip_combo_pos)
  7775. pos = current_equip_combo_pos;
  7776. else
  7777. pos = sd->status.inventory[current_equip_item_index].equip;
  7778. if((sd->state.autobonus&pos) == pos)
  7779. return SCRIPT_CMD_SUCCESS;
  7780. rate = script_getnum(st,3);
  7781. dur = script_getnum(st,4);
  7782. bonus_script = script_getstr(st,2);
  7783. if( !rate || !dur || !pos || !bonus_script )
  7784. return SCRIPT_CMD_SUCCESS;
  7785. if( script_hasdata(st,5) )
  7786. atk_type = script_getnum(st,5);
  7787. if( script_hasdata(st,6) )
  7788. other_script = script_getstr(st,6);
  7789. if( pc_addautobonus(sd->autobonus2,ARRAYLENGTH(sd->autobonus2),
  7790. bonus_script,rate,dur,atk_type,other_script,pos,false) )
  7791. {
  7792. script_add_autobonus(bonus_script);
  7793. if( other_script )
  7794. script_add_autobonus(other_script);
  7795. }
  7796. return SCRIPT_CMD_SUCCESS;
  7797. }
  7798. BUILDIN_FUNC(autobonus3)
  7799. {
  7800. unsigned int dur, pos;
  7801. short rate,atk_type;
  7802. TBL_PC* sd;
  7803. const char *bonus_script, *other_script = NULL;
  7804. struct script_data *data;
  7805. sd = script_rid2sd(st);
  7806. if( sd == NULL )
  7807. return SCRIPT_CMD_SUCCESS; // no player attached
  7808. if (current_equip_combo_pos)
  7809. pos = current_equip_combo_pos;
  7810. else
  7811. pos = sd->status.inventory[current_equip_item_index].equip;
  7812. if((sd->state.autobonus&pos) == pos)
  7813. return SCRIPT_CMD_SUCCESS;
  7814. rate = script_getnum(st,3);
  7815. dur = script_getnum(st,4);
  7816. data = script_getdata(st, 5);
  7817. get_val(st, data); // Convert into value in case of a variable
  7818. atk_type = ( data_isstring(data) ? skill_name2id(script_getstr(st,5)) : script_getnum(st,5) );
  7819. bonus_script = script_getstr(st,2);
  7820. if( !rate || !dur || !pos || !atk_type || !bonus_script )
  7821. return SCRIPT_CMD_SUCCESS;
  7822. if( script_hasdata(st,6) )
  7823. other_script = script_getstr(st,6);
  7824. if( pc_addautobonus(sd->autobonus3,ARRAYLENGTH(sd->autobonus3),
  7825. bonus_script,rate,dur,atk_type,other_script,pos,true) )
  7826. {
  7827. script_add_autobonus(bonus_script);
  7828. if( other_script )
  7829. script_add_autobonus(other_script);
  7830. }
  7831. return SCRIPT_CMD_SUCCESS;
  7832. }
  7833. /// Changes the level of a player skill.
  7834. /// <flag> defaults to 1
  7835. /// <flag>=0 : set the level of the skill
  7836. /// <flag>=1 : set the temporary level of the skill
  7837. /// <flag>=2 : add to the level of the skill
  7838. ///
  7839. /// skill <skill id>,<level>,<flag>
  7840. /// skill <skill id>,<level>
  7841. /// skill "<skill name>",<level>,<flag>
  7842. /// skill "<skill name>",<level>
  7843. BUILDIN_FUNC(skill)
  7844. {
  7845. int id;
  7846. int level;
  7847. int flag = ADDSKILL_TEMP;
  7848. TBL_PC* sd;
  7849. struct script_data *data;
  7850. const char* command = script_getfuncname(st);
  7851. sd = script_rid2sd(st);
  7852. if( sd == NULL )
  7853. return SCRIPT_CMD_SUCCESS;// no player attached, report source
  7854. if (strcmpi(command, "addtoskill") == 0)
  7855. flag = ADDSKILL_TEMP_ADDLEVEL;
  7856. data = script_getdata(st, 2);
  7857. get_val(st, data); // Convert into value in case of a variable
  7858. id = ( data_isstring(data) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  7859. level = script_getnum(st,3);
  7860. if( script_hasdata(st,4) )
  7861. flag = script_getnum(st,4);
  7862. pc_skill(sd, id, level, (enum e_addskill_type)flag);
  7863. return SCRIPT_CMD_SUCCESS;
  7864. }
  7865. /// Increases the level of a guild skill.
  7866. ///
  7867. /// guildskill <skill id>,<amount>;
  7868. /// guildskill "<skill name>",<amount>;
  7869. BUILDIN_FUNC(guildskill)
  7870. {
  7871. int id;
  7872. int level;
  7873. TBL_PC* sd;
  7874. int i;
  7875. struct script_data *data;
  7876. sd = script_rid2sd(st);
  7877. if( sd == NULL )
  7878. return SCRIPT_CMD_SUCCESS;// no player attached, report source
  7879. data = script_getdata(st, 2);
  7880. get_val(st, data); // Convert into value in case of a variable
  7881. id = ( data_isstring(data) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  7882. level = script_getnum(st,3);
  7883. for( i=0; i < level; i++ )
  7884. guild_skillup(sd, id);
  7885. return SCRIPT_CMD_SUCCESS;
  7886. }
  7887. /// Returns the level of the player skill.
  7888. ///
  7889. /// getskilllv(<skill id>) -> <level>
  7890. /// getskilllv("<skill name>") -> <level>
  7891. BUILDIN_FUNC(getskilllv)
  7892. {
  7893. int id;
  7894. TBL_PC* sd;
  7895. struct script_data *data;
  7896. sd = script_rid2sd(st);
  7897. if( sd == NULL )
  7898. return SCRIPT_CMD_SUCCESS;// no player attached, report source
  7899. data = script_getdata(st, 2);
  7900. get_val(st, data); // Convert into value in case of a variable
  7901. id = ( data_isstring(data) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  7902. script_pushint(st, pc_checkskill(sd,id));
  7903. return SCRIPT_CMD_SUCCESS;
  7904. }
  7905. /// Returns the level of the guild skill.
  7906. ///
  7907. /// getgdskilllv(<guild id>,<skill id>) -> <level>
  7908. /// getgdskilllv(<guild id>,"<skill name>") -> <level>
  7909. BUILDIN_FUNC(getgdskilllv)
  7910. {
  7911. int guild_id;
  7912. uint16 skill_id;
  7913. struct guild* g;
  7914. struct script_data *data;
  7915. guild_id = script_getnum(st,2);
  7916. data = script_getdata(st, 3);
  7917. get_val(st, data); // Convert into value in case of a variable
  7918. skill_id = ( data_isstring(data) ? skill_name2id(script_getstr(st,3)) : script_getnum(st,3) );
  7919. g = guild_search(guild_id);
  7920. if( g == NULL )
  7921. script_pushint(st, -1);
  7922. else
  7923. script_pushint(st, guild_checkskill(g,skill_id));
  7924. return SCRIPT_CMD_SUCCESS;
  7925. }
  7926. /// Returns the 'basic_skill_check' setting.
  7927. /// This config determines if the server checks the skill level of NV_BASIC
  7928. /// before allowing the basic actions.
  7929. ///
  7930. /// basicskillcheck() -> <bool>
  7931. BUILDIN_FUNC(basicskillcheck)
  7932. {
  7933. script_pushint(st, battle_config.basic_skill_check);
  7934. return SCRIPT_CMD_SUCCESS;
  7935. }
  7936. /// Returns the GM level of the player.
  7937. ///
  7938. /// getgmlevel({<char_id>}) -> <level>
  7939. BUILDIN_FUNC(getgmlevel)
  7940. {
  7941. TBL_PC* sd;
  7942. if (!script_charid2sd(2,sd))
  7943. return SCRIPT_CMD_FAILURE;
  7944. script_pushint(st, pc_get_group_level(sd));
  7945. return SCRIPT_CMD_SUCCESS;
  7946. }
  7947. /// Returns the group ID of the player.
  7948. ///
  7949. /// getgroupid({<char_id>}) -> <int>
  7950. BUILDIN_FUNC(getgroupid)
  7951. {
  7952. TBL_PC* sd;
  7953. if (!script_charid2sd(2,sd))
  7954. return SCRIPT_CMD_FAILURE;
  7955. script_pushint(st, pc_get_group_id(sd));
  7956. return SCRIPT_CMD_SUCCESS;
  7957. }
  7958. /// Terminates the execution of this script instance.
  7959. ///
  7960. /// end
  7961. BUILDIN_FUNC(end)
  7962. {
  7963. TBL_PC* sd;
  7964. sd = map_id2sd(st->rid);
  7965. st->state = END;
  7966. if (st->stack->defsp >= 1 && st->stack->stack_data[st->stack->defsp-1].type == C_RETINFO) {
  7967. int i;
  7968. for(i = 0; i < st->stack->sp; i++) {
  7969. if (st->stack->stack_data[i].type == C_RETINFO) { // Grab the first, aka the original
  7970. struct script_retinfo *ri = st->stack->stack_data[i].u.ri;
  7971. st->script = ri->script;
  7972. break;
  7973. }
  7974. }
  7975. }
  7976. if( st->mes_active )
  7977. st->mes_active = 0;
  7978. if (sd){
  7979. if (sd->state.callshop == 0)
  7980. clif_scriptclose(sd, st->oid); // If a menu/select/prompt is active, close it.
  7981. else
  7982. sd->state.callshop = 0;
  7983. }
  7984. return SCRIPT_CMD_SUCCESS;
  7985. }
  7986. /// Checks if the player has that effect state (option).
  7987. ///
  7988. /// checkoption(<option>{,<char_id>}) -> <bool>
  7989. BUILDIN_FUNC(checkoption)
  7990. {
  7991. int option;
  7992. TBL_PC* sd;
  7993. if (!script_charid2sd(3,sd))
  7994. return SCRIPT_CMD_FAILURE;
  7995. option = script_getnum(st,2);
  7996. if( sd->sc.option&option )
  7997. script_pushint(st, 1);
  7998. else
  7999. script_pushint(st, 0);
  8000. return SCRIPT_CMD_SUCCESS;
  8001. }
  8002. /// Checks if the player is in that body state (opt1).
  8003. ///
  8004. /// checkoption1(<opt1>{,<char_id>}) -> <bool>
  8005. BUILDIN_FUNC(checkoption1)
  8006. {
  8007. int opt1;
  8008. TBL_PC* sd;
  8009. if (!script_charid2sd(3,sd))
  8010. return SCRIPT_CMD_FAILURE;
  8011. opt1 = script_getnum(st,2);
  8012. if( sd->sc.opt1 == opt1 )
  8013. script_pushint(st, 1);
  8014. else
  8015. script_pushint(st, 0);
  8016. return SCRIPT_CMD_SUCCESS;
  8017. }
  8018. /// Checks if the player has that health state (opt2).
  8019. ///
  8020. /// checkoption2(<opt2>{,<char_id>}) -> <bool>
  8021. BUILDIN_FUNC(checkoption2)
  8022. {
  8023. int opt2;
  8024. TBL_PC* sd;
  8025. if (!script_charid2sd(3,sd))
  8026. return SCRIPT_CMD_FAILURE;
  8027. opt2 = script_getnum(st,2);
  8028. if( sd->sc.opt2&opt2 )
  8029. script_pushint(st, 1);
  8030. else
  8031. script_pushint(st, 0);
  8032. return SCRIPT_CMD_SUCCESS;
  8033. }
  8034. /// Changes the effect state (option) of the player.
  8035. /// <flag> defaults to 1
  8036. /// <flag>=0 : removes the option
  8037. /// <flag>=other : adds the option
  8038. ///
  8039. /// setoption <option>{,<flag>{,<char_id>}};
  8040. BUILDIN_FUNC(setoption)
  8041. {
  8042. int option;
  8043. int flag = 1;
  8044. TBL_PC* sd;
  8045. if (!script_charid2sd(4,sd))
  8046. return SCRIPT_CMD_FAILURE;
  8047. option = script_getnum(st,2);
  8048. if( script_hasdata(st,3) )
  8049. flag = script_getnum(st,3);
  8050. else if( !option ){// Request to remove everything.
  8051. flag = 0;
  8052. option = OPTION_FALCON|OPTION_RIDING;
  8053. #ifndef NEW_CARTS
  8054. option |= OPTION_CART;
  8055. #endif
  8056. }
  8057. if( flag ){// Add option
  8058. if( option&OPTION_WEDDING && !battle_config.wedding_modifydisplay )
  8059. option &= ~OPTION_WEDDING;// Do not show the wedding sprites
  8060. pc_setoption(sd, sd->sc.option|option);
  8061. } else// Remove option
  8062. pc_setoption(sd, sd->sc.option&~option);
  8063. return SCRIPT_CMD_SUCCESS;
  8064. }
  8065. /// Returns if the player has a cart.
  8066. ///
  8067. /// checkcart({char_id}) -> <bool>
  8068. ///
  8069. /// @author Valaris
  8070. BUILDIN_FUNC(checkcart)
  8071. {
  8072. TBL_PC* sd;
  8073. if (!script_charid2sd(2,sd))
  8074. return SCRIPT_CMD_FAILURE;
  8075. if( pc_iscarton(sd) )
  8076. script_pushint(st, 1);
  8077. else
  8078. script_pushint(st, 0);
  8079. return SCRIPT_CMD_SUCCESS;
  8080. }
  8081. /// Sets the cart of the player.
  8082. /// <type> defaults to 1
  8083. /// <type>=0 : removes the cart
  8084. /// <type>=1 : Normal cart
  8085. /// <type>=2 : Wooden cart
  8086. /// <type>=3 : Covered cart with flowers and ferns
  8087. /// <type>=4 : Wooden cart with a Panda doll on the back
  8088. /// <type>=5 : Normal cart with bigger wheels, a roof and a banner on the back
  8089. ///
  8090. /// setcart {<type>{,<char_id>}};
  8091. BUILDIN_FUNC(setcart)
  8092. {
  8093. int type = 1;
  8094. TBL_PC* sd;
  8095. if (!script_charid2sd(3,sd))
  8096. return SCRIPT_CMD_FAILURE;
  8097. if( script_hasdata(st,2) )
  8098. type = script_getnum(st,2);
  8099. pc_setcart(sd, type);
  8100. return SCRIPT_CMD_SUCCESS;
  8101. }
  8102. /// Returns if the player has a falcon.
  8103. ///
  8104. /// checkfalcon({char_id}) -> <bool>
  8105. ///
  8106. /// @author Valaris
  8107. BUILDIN_FUNC(checkfalcon)
  8108. {
  8109. TBL_PC* sd;
  8110. if (!script_charid2sd(2,sd))
  8111. return SCRIPT_CMD_FAILURE;
  8112. if( pc_isfalcon(sd) )
  8113. script_pushint(st, 1);
  8114. else
  8115. script_pushint(st, 0);
  8116. return SCRIPT_CMD_SUCCESS;
  8117. }
  8118. /// Sets if the player has a falcon or not.
  8119. /// <flag> defaults to 1
  8120. ///
  8121. /// setfalcon {<flag>{,<char_id>}};
  8122. BUILDIN_FUNC(setfalcon)
  8123. {
  8124. int flag = 1;
  8125. TBL_PC* sd;
  8126. if (!script_charid2sd(3,sd))
  8127. return SCRIPT_CMD_FAILURE;
  8128. if( script_hasdata(st,2) )
  8129. flag = script_getnum(st,2);
  8130. pc_setfalcon(sd, flag);
  8131. return SCRIPT_CMD_SUCCESS;
  8132. }
  8133. /// Returns if the player is riding.
  8134. ///
  8135. /// checkriding({char_id}) -> <bool>
  8136. ///
  8137. /// @author Valaris
  8138. BUILDIN_FUNC(checkriding)
  8139. {
  8140. TBL_PC* sd;
  8141. if (!script_charid2sd(2,sd))
  8142. return SCRIPT_CMD_FAILURE;
  8143. if( pc_isriding(sd) )
  8144. script_pushint(st, 1);
  8145. else
  8146. script_pushint(st, 0);
  8147. return SCRIPT_CMD_SUCCESS;
  8148. }
  8149. /// Sets if the player is riding.
  8150. /// <flag> defaults to 1
  8151. ///
  8152. /// setriding {<flag>{,<char_id>}};
  8153. BUILDIN_FUNC(setriding)
  8154. {
  8155. int flag = 1;
  8156. TBL_PC* sd;
  8157. if (!script_charid2sd(3,sd))
  8158. return SCRIPT_CMD_FAILURE;
  8159. if( script_hasdata(st,2) )
  8160. flag = script_getnum(st,2);
  8161. pc_setriding(sd, flag);
  8162. return SCRIPT_CMD_SUCCESS;
  8163. }
  8164. /// Returns if the player has a warg.
  8165. ///
  8166. /// checkwug({char_id}) -> <bool>
  8167. ///
  8168. BUILDIN_FUNC(checkwug)
  8169. {
  8170. TBL_PC* sd;
  8171. if (!script_charid2sd(2,sd))
  8172. return SCRIPT_CMD_FAILURE;
  8173. if( pc_iswug(sd) || pc_isridingwug(sd) )
  8174. script_pushint(st, 1);
  8175. else
  8176. script_pushint(st, 0);
  8177. return SCRIPT_CMD_SUCCESS;
  8178. }
  8179. /// Returns if the player is wearing MADO Gear.
  8180. ///
  8181. /// checkmadogear({<char_id>}) -> <bool>
  8182. ///
  8183. BUILDIN_FUNC(checkmadogear)
  8184. {
  8185. TBL_PC* sd;
  8186. if (!script_charid2sd(2,sd))
  8187. return SCRIPT_CMD_FAILURE;
  8188. if( pc_ismadogear(sd) )
  8189. script_pushint(st, 1);
  8190. else
  8191. script_pushint(st, 0);
  8192. return SCRIPT_CMD_SUCCESS;
  8193. }
  8194. /// Sets if the player is riding MADO Gear.
  8195. /// <flag> defaults to 1
  8196. ///
  8197. /// setmadogear {<flag>{,<char_id>}};
  8198. BUILDIN_FUNC(setmadogear)
  8199. {
  8200. int flag = 1;
  8201. TBL_PC* sd;
  8202. if (!script_charid2sd(3,sd))
  8203. return SCRIPT_CMD_FAILURE;
  8204. if( script_hasdata(st,2) )
  8205. flag = script_getnum(st,2);
  8206. pc_setmadogear(sd, flag);
  8207. return SCRIPT_CMD_SUCCESS;
  8208. }
  8209. /// Sets the save point of the player.
  8210. ///
  8211. /// save "<map name>",<x>,<y>{,<char_id>}
  8212. /// savepoint "<map name>",<x>,<y>{,<char_id>}
  8213. BUILDIN_FUNC(savepoint)
  8214. {
  8215. int x;
  8216. int y;
  8217. short map_idx;
  8218. const char* str;
  8219. TBL_PC* sd;
  8220. if (!script_charid2sd(5,sd))
  8221. return SCRIPT_CMD_FAILURE;// no player attached, report source
  8222. str = script_getstr(st, 2);
  8223. x = script_getnum(st,3);
  8224. y = script_getnum(st,4);
  8225. map_idx = mapindex_name2id(str);
  8226. if( map_idx )
  8227. pc_setsavepoint(sd, map_idx, x, y);
  8228. return SCRIPT_CMD_SUCCESS;
  8229. }
  8230. /*==========================================
  8231. * GetTimeTick(0: System Tick, 1: Time Second Tick)
  8232. *------------------------------------------*/
  8233. BUILDIN_FUNC(gettimetick) /* Asgard Version */
  8234. {
  8235. int type;
  8236. time_t timer;
  8237. struct tm *t;
  8238. type=script_getnum(st,2);
  8239. switch(type){
  8240. case 2:
  8241. //type 2:(Get the number of seconds elapsed since 00:00 hours, Jan 1, 1970 UTC
  8242. // from the system clock.)
  8243. script_pushint(st,(int)time(NULL));
  8244. break;
  8245. case 1:
  8246. //type 1:(Second Ticks: 0-86399, 00:00:00-23:59:59)
  8247. time(&timer);
  8248. t=localtime(&timer);
  8249. script_pushint(st,((t->tm_hour)*3600+(t->tm_min)*60+t->tm_sec));
  8250. break;
  8251. case 0:
  8252. default:
  8253. //type 0:(System Ticks)
  8254. script_pushint(st,gettick());
  8255. break;
  8256. }
  8257. return SCRIPT_CMD_SUCCESS;
  8258. }
  8259. /*==========================================
  8260. * GetTime(Type);
  8261. * 1: Sec 2: Min 3: Hour
  8262. * 4: WeekDay 5: MonthDay 6: Month
  8263. * 7: Year
  8264. *------------------------------------------*/
  8265. BUILDIN_FUNC(gettime) /* Asgard Version */
  8266. {
  8267. int type;
  8268. time_t timer;
  8269. struct tm *t;
  8270. type=script_getnum(st,2);
  8271. time(&timer);
  8272. t=localtime(&timer);
  8273. switch(type){
  8274. case 1://Sec(0~59)
  8275. script_pushint(st,t->tm_sec);
  8276. break;
  8277. case 2://Min(0~59)
  8278. script_pushint(st,t->tm_min);
  8279. break;
  8280. case 3://Hour(0~23)
  8281. script_pushint(st,t->tm_hour);
  8282. break;
  8283. case 4://WeekDay(0~6)
  8284. script_pushint(st,t->tm_wday);
  8285. break;
  8286. case 5://MonthDay(01~31)
  8287. script_pushint(st,t->tm_mday);
  8288. break;
  8289. case 6://Month(01~12)
  8290. script_pushint(st,t->tm_mon+1);
  8291. break;
  8292. case 7://Year(20xx)
  8293. script_pushint(st,t->tm_year+1900);
  8294. break;
  8295. case 8://Year Day(01~366)
  8296. script_pushint(st,t->tm_yday+1);
  8297. break;
  8298. default://(format error)
  8299. script_pushint(st,-1);
  8300. break;
  8301. }
  8302. return SCRIPT_CMD_SUCCESS;
  8303. }
  8304. /*==========================================
  8305. * GetTimeStr("TimeFMT", Length);
  8306. *------------------------------------------*/
  8307. BUILDIN_FUNC(gettimestr)
  8308. {
  8309. char *tmpstr;
  8310. const char *fmtstr;
  8311. int maxlen;
  8312. time_t now = time(NULL);
  8313. fmtstr=script_getstr(st,2);
  8314. maxlen=script_getnum(st,3);
  8315. tmpstr=(char *)aMalloc((maxlen+1)*sizeof(char));
  8316. strftime(tmpstr,maxlen,fmtstr,localtime(&now));
  8317. tmpstr[maxlen]='\0';
  8318. script_pushstr(st,tmpstr);
  8319. return SCRIPT_CMD_SUCCESS;
  8320. }
  8321. /*==========================================
  8322. * Open player storage
  8323. *------------------------------------------*/
  8324. BUILDIN_FUNC(openstorage)
  8325. {
  8326. TBL_PC* sd;
  8327. sd = script_rid2sd(st);
  8328. if( sd == NULL )
  8329. return SCRIPT_CMD_SUCCESS;
  8330. storage_storageopen(sd);
  8331. return SCRIPT_CMD_SUCCESS;
  8332. }
  8333. BUILDIN_FUNC(guildopenstorage)
  8334. {
  8335. TBL_PC* sd;
  8336. int ret;
  8337. sd = script_rid2sd(st);
  8338. if( sd == NULL )
  8339. return SCRIPT_CMD_SUCCESS;
  8340. ret = gstorage_storageopen(sd);
  8341. script_pushint(st,ret);
  8342. return SCRIPT_CMD_SUCCESS;
  8343. }
  8344. /*==========================================
  8345. * Make player use a skill trought item usage
  8346. *------------------------------------------*/
  8347. /// itemskill <skill id>,<level>
  8348. /// itemskill "<skill name>",<level>
  8349. BUILDIN_FUNC(itemskill)
  8350. {
  8351. int id;
  8352. int lv;
  8353. TBL_PC* sd;
  8354. struct script_data *data;
  8355. sd = script_rid2sd(st);
  8356. if( sd == NULL || sd->ud.skilltimer != INVALID_TIMER )
  8357. return SCRIPT_CMD_SUCCESS;
  8358. data = script_getdata(st, 2);
  8359. get_val(st, data); // Convert into value in case of a variable
  8360. id = ( data_isstring(data) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  8361. lv = script_getnum(st,3);
  8362. sd->skillitem=id;
  8363. sd->skillitemlv=lv;
  8364. clif_item_skill(sd,id,lv);
  8365. return SCRIPT_CMD_SUCCESS;
  8366. }
  8367. /*==========================================
  8368. * Attempt to create an item
  8369. *------------------------------------------*/
  8370. BUILDIN_FUNC(produce)
  8371. {
  8372. int trigger;
  8373. TBL_PC* sd;
  8374. sd = script_rid2sd(st);
  8375. if( sd == NULL )
  8376. return SCRIPT_CMD_SUCCESS;
  8377. trigger=script_getnum(st,2);
  8378. clif_skill_produce_mix_list(sd, -1, trigger);
  8379. return SCRIPT_CMD_SUCCESS;
  8380. }
  8381. /*==========================================
  8382. *
  8383. *------------------------------------------*/
  8384. BUILDIN_FUNC(cooking)
  8385. {
  8386. int trigger;
  8387. TBL_PC* sd;
  8388. sd = script_rid2sd(st);
  8389. if( sd == NULL )
  8390. return SCRIPT_CMD_SUCCESS;
  8391. trigger=script_getnum(st,2);
  8392. clif_cooking_list(sd, trigger, AM_PHARMACY, 1, 1);
  8393. return SCRIPT_CMD_SUCCESS;
  8394. }
  8395. /*==========================================
  8396. * Create a pet
  8397. *------------------------------------------*/
  8398. BUILDIN_FUNC(makepet)
  8399. {
  8400. TBL_PC* sd;
  8401. int id,pet_id;
  8402. id=script_getnum(st,2);
  8403. sd = script_rid2sd(st);
  8404. if( sd == NULL )
  8405. return SCRIPT_CMD_SUCCESS;
  8406. pet_id = search_petDB_index(id, PET_CLASS);
  8407. if (pet_id < 0)
  8408. pet_id = search_petDB_index(id, PET_EGG);
  8409. if (pet_id >= 0 && sd) {
  8410. sd->catch_target_class = pet_db[pet_id].class_;
  8411. intif_create_pet(
  8412. sd->status.account_id, sd->status.char_id,
  8413. (short)pet_db[pet_id].class_, (short)mob_db(pet_db[pet_id].class_)->lv,
  8414. (short)pet_db[pet_id].EggID, 0, (short)pet_db[pet_id].intimate,
  8415. 100, 0, 1, pet_db[pet_id].jname);
  8416. }
  8417. return SCRIPT_CMD_SUCCESS;
  8418. }
  8419. /**
  8420. * Give player exp base,job * quest_exp_rate/100
  8421. * getexp <base xp>,<job xp>{,<char_id>};
  8422. **/
  8423. BUILDIN_FUNC(getexp)
  8424. {
  8425. TBL_PC* sd;
  8426. int base=0,job=0;
  8427. double bonus;
  8428. if (!script_charid2sd(4,sd))
  8429. return SCRIPT_CMD_FAILURE;
  8430. base=script_getnum(st,2);
  8431. job =script_getnum(st,3);
  8432. if(base<0 || job<0)
  8433. return SCRIPT_CMD_SUCCESS;
  8434. // bonus for npc-given exp
  8435. bonus = battle_config.quest_exp_rate / 100.;
  8436. if (base)
  8437. base = (int) cap_value(base * bonus, 0, INT_MAX);
  8438. if (job)
  8439. job = (int) cap_value(job * bonus, 0, INT_MAX);
  8440. pc_gainexp(sd, NULL, base, job, 1);
  8441. return SCRIPT_CMD_SUCCESS;
  8442. }
  8443. /*==========================================
  8444. * Gain guild exp [Celest]
  8445. *------------------------------------------*/
  8446. BUILDIN_FUNC(guildgetexp)
  8447. {
  8448. TBL_PC* sd;
  8449. int exp;
  8450. sd = script_rid2sd(st);
  8451. if( sd == NULL )
  8452. return SCRIPT_CMD_SUCCESS;
  8453. exp = script_getnum(st,2);
  8454. if(exp < 0)
  8455. return SCRIPT_CMD_SUCCESS;
  8456. if(sd && sd->status.guild_id > 0)
  8457. guild_getexp (sd, exp);
  8458. return SCRIPT_CMD_SUCCESS;
  8459. }
  8460. /*==========================================
  8461. * Changes the guild master of a guild [Skotlex]
  8462. *------------------------------------------*/
  8463. BUILDIN_FUNC(guildchangegm)
  8464. {
  8465. TBL_PC *sd;
  8466. int guild_id;
  8467. const char *name;
  8468. guild_id = script_getnum(st,2);
  8469. name = script_getstr(st,3);
  8470. sd=map_nick2sd(name);
  8471. if (!sd)
  8472. script_pushint(st,0);
  8473. else
  8474. script_pushint(st,guild_gm_change(guild_id, sd));
  8475. return SCRIPT_CMD_SUCCESS;
  8476. }
  8477. /*==========================================
  8478. * Spawn a monster:
  8479. * *monster "<map name>",<x>,<y>,"<name to show>",<mob id>,<amount>{,"<event label>",<size>,<ai>};
  8480. *------------------------------------------*/
  8481. BUILDIN_FUNC(monster)
  8482. {
  8483. const char* mapn = script_getstr(st,2);
  8484. int x = script_getnum(st,3);
  8485. int y = script_getnum(st,4);
  8486. const char* str = script_getstr(st,5);
  8487. int class_ = script_getnum(st,6);
  8488. int amount = script_getnum(st,7);
  8489. const char* event = "";
  8490. unsigned int size = SZ_SMALL;
  8491. unsigned int ai = AI_NONE;
  8492. struct map_session_data* sd;
  8493. int16 m;
  8494. int i;
  8495. if (script_hasdata(st, 8)) {
  8496. event = script_getstr(st, 8);
  8497. check_event(st, event);
  8498. }
  8499. if (script_hasdata(st, 9)) {
  8500. size = script_getnum(st, 9);
  8501. if (size > 3) {
  8502. ShowWarning("buildin_monster: Attempted to spawn non-existing size %d for monster class %d\n", size, class_);
  8503. return SCRIPT_CMD_FAILURE;
  8504. }
  8505. }
  8506. if (script_hasdata(st, 10)) {
  8507. ai = script_getnum(st, 10);
  8508. if (ai >= AI_MAX) {
  8509. ShowWarning("buildin_monster: Attempted to spawn non-existing ai %d for monster class %d\n", ai, class_);
  8510. return SCRIPT_CMD_FAILURE;
  8511. }
  8512. }
  8513. if (class_ >= 0 && !mobdb_checkid(class_)) {
  8514. ShowWarning("buildin_monster: Attempted to spawn non-existing monster class %d\n", class_);
  8515. return SCRIPT_CMD_FAILURE;
  8516. }
  8517. sd = map_id2sd(st->rid);
  8518. if (sd && strcmp(mapn, "this") == 0)
  8519. m = sd->bl.m;
  8520. else
  8521. m = map_mapname2mapid(mapn);
  8522. for(i = 0; i < amount; i++) { //not optimised
  8523. int mobid = mob_once_spawn(sd, m, x, y, str, class_, 1, event, size, ai);
  8524. if (mobid)
  8525. mapreg_setreg(reference_uid(add_str("$@mobid"), i), mobid);
  8526. }
  8527. return SCRIPT_CMD_SUCCESS;
  8528. }
  8529. /*==========================================
  8530. * Request List of Monster Drops
  8531. *------------------------------------------*/
  8532. BUILDIN_FUNC(getmobdrops)
  8533. {
  8534. int class_ = script_getnum(st,2);
  8535. int i, j = 0;
  8536. struct mob_db *mob;
  8537. if( !mobdb_checkid(class_) )
  8538. {
  8539. script_pushint(st, 0);
  8540. return SCRIPT_CMD_SUCCESS;
  8541. }
  8542. mob = mob_db(class_);
  8543. for( i = 0; i < MAX_MOB_DROP; i++ )
  8544. {
  8545. if( mob->dropitem[i].nameid < 1 )
  8546. continue;
  8547. if( itemdb_exists(mob->dropitem[i].nameid) == NULL )
  8548. continue;
  8549. mapreg_setreg(reference_uid(add_str("$@MobDrop_item"), j), mob->dropitem[i].nameid);
  8550. mapreg_setreg(reference_uid(add_str("$@MobDrop_rate"), j), mob->dropitem[i].p);
  8551. j++;
  8552. }
  8553. mapreg_setreg(add_str("$@MobDrop_count"), j);
  8554. script_pushint(st, 1);
  8555. return SCRIPT_CMD_SUCCESS;
  8556. }
  8557. /*==========================================
  8558. * Spawn a monster in a random location
  8559. * in x0,x1,y0,y1 area.
  8560. *------------------------------------------*/
  8561. BUILDIN_FUNC(areamonster)
  8562. {
  8563. const char* mapn = script_getstr(st,2);
  8564. int x0 = script_getnum(st,3);
  8565. int y0 = script_getnum(st,4);
  8566. int x1 = script_getnum(st,5);
  8567. int y1 = script_getnum(st,6);
  8568. const char* str = script_getstr(st,7);
  8569. int class_ = script_getnum(st,8);
  8570. int amount = script_getnum(st,9);
  8571. const char* event = "";
  8572. unsigned int size = SZ_SMALL;
  8573. unsigned int ai = AI_NONE;
  8574. struct map_session_data* sd;
  8575. int16 m;
  8576. int i;
  8577. if (script_hasdata(st,10)) {
  8578. event = script_getstr(st, 10);
  8579. check_event(st, event);
  8580. }
  8581. if (script_hasdata(st, 11)) {
  8582. size = script_getnum(st, 11);
  8583. if (size > 3) {
  8584. ShowWarning("buildin_monster: Attempted to spawn non-existing size %d for monster class %d\n", size, class_);
  8585. return SCRIPT_CMD_FAILURE;
  8586. }
  8587. }
  8588. if (script_hasdata(st, 12)) {
  8589. ai = script_getnum(st, 12);
  8590. if (ai >= AI_MAX) {
  8591. ShowWarning("buildin_monster: Attempted to spawn non-existing ai %d for monster class %d\n", ai, class_);
  8592. return SCRIPT_CMD_FAILURE;
  8593. }
  8594. }
  8595. sd = map_id2sd(st->rid);
  8596. if (sd && strcmp(mapn, "this") == 0)
  8597. m = sd->bl.m;
  8598. else
  8599. m = map_mapname2mapid(mapn);
  8600. for(i = 0; i < amount; i++) { //not optimised
  8601. int mobid = mob_once_spawn_area(sd, m, x0, y0, x1, y1, str, class_, 1, event, size, ai);
  8602. if (mobid)
  8603. mapreg_setreg(reference_uid(add_str("$@mobid"), i), mobid);
  8604. }
  8605. return SCRIPT_CMD_SUCCESS;
  8606. }
  8607. /*==========================================
  8608. * KillMonster subcheck, verify if mob to kill ain't got an even to handle, could be force kill by allflag
  8609. *------------------------------------------*/
  8610. static int buildin_killmonster_sub_strip(struct block_list *bl,va_list ap)
  8611. { //same fix but with killmonster instead - stripping events from mobs.
  8612. TBL_MOB* md = (TBL_MOB*)bl;
  8613. char *event=va_arg(ap,char *);
  8614. int allflag=va_arg(ap,int);
  8615. md->state.npc_killmonster = 1;
  8616. if(!allflag){
  8617. if(strcmp(event,md->npc_event)==0)
  8618. status_kill(bl);
  8619. }else{
  8620. if(!md->spawn)
  8621. status_kill(bl);
  8622. }
  8623. md->state.npc_killmonster = 0;
  8624. return SCRIPT_CMD_SUCCESS;
  8625. }
  8626. static int buildin_killmonster_sub(struct block_list *bl,va_list ap)
  8627. {
  8628. TBL_MOB* md = (TBL_MOB*)bl;
  8629. char *event=va_arg(ap,char *);
  8630. int allflag=va_arg(ap,int);
  8631. if(!allflag){
  8632. if(strcmp(event,md->npc_event)==0)
  8633. status_kill(bl);
  8634. }else{
  8635. if(!md->spawn)
  8636. status_kill(bl);
  8637. }
  8638. return SCRIPT_CMD_SUCCESS;
  8639. }
  8640. BUILDIN_FUNC(killmonster)
  8641. {
  8642. const char *mapname,*event;
  8643. int16 m,allflag=0;
  8644. mapname=script_getstr(st,2);
  8645. event=script_getstr(st,3);
  8646. if(strcmp(event,"All")==0)
  8647. allflag = 1;
  8648. else
  8649. check_event(st, event);
  8650. if( (m=map_mapname2mapid(mapname))<0 )
  8651. return SCRIPT_CMD_SUCCESS;
  8652. if( script_hasdata(st,4) ) {
  8653. if ( script_getnum(st,4) == 1 ) {
  8654. map_foreachinmap(buildin_killmonster_sub, m, BL_MOB, event ,allflag);
  8655. return SCRIPT_CMD_SUCCESS;
  8656. }
  8657. }
  8658. map_freeblock_lock();
  8659. map_foreachinmap(buildin_killmonster_sub_strip, m, BL_MOB, event ,allflag);
  8660. map_freeblock_unlock();
  8661. return SCRIPT_CMD_SUCCESS;
  8662. }
  8663. static int buildin_killmonsterall_sub_strip(struct block_list *bl,va_list ap)
  8664. { //Strips the event from the mob if it's killed the old method.
  8665. struct mob_data *md;
  8666. md = BL_CAST(BL_MOB, bl);
  8667. if (md->npc_event[0])
  8668. md->npc_event[0] = 0;
  8669. status_kill(bl);
  8670. return 0;
  8671. }
  8672. static int buildin_killmonsterall_sub(struct block_list *bl,va_list ap)
  8673. {
  8674. status_kill(bl);
  8675. return 0;
  8676. }
  8677. BUILDIN_FUNC(killmonsterall)
  8678. {
  8679. const char *mapname;
  8680. int16 m;
  8681. mapname=script_getstr(st,2);
  8682. if( (m = map_mapname2mapid(mapname))<0 )
  8683. return SCRIPT_CMD_SUCCESS;
  8684. if( script_hasdata(st,3) ) {
  8685. if ( script_getnum(st,3) == 1 ) {
  8686. map_foreachinmap(buildin_killmonsterall_sub,m,BL_MOB);
  8687. return SCRIPT_CMD_SUCCESS;
  8688. }
  8689. }
  8690. map_foreachinmap(buildin_killmonsterall_sub_strip,m,BL_MOB);
  8691. return SCRIPT_CMD_SUCCESS;
  8692. }
  8693. /*==========================================
  8694. * Creates a clone of a player.
  8695. * clone map, x, y, event, char_id, master_id, mode, flag, duration
  8696. *------------------------------------------*/
  8697. BUILDIN_FUNC(clone)
  8698. {
  8699. TBL_PC *sd, *msd=NULL;
  8700. uint32 char_id;
  8701. int master_id=0,x,y, flag = 0, m;
  8702. enum e_mode mode = 0;
  8703. unsigned int duration = 0;
  8704. const char *mapname,*event;
  8705. mapname=script_getstr(st,2);
  8706. x=script_getnum(st,3);
  8707. y=script_getnum(st,4);
  8708. event=script_getstr(st,5);
  8709. char_id=script_getnum(st,6);
  8710. if( script_hasdata(st,7) )
  8711. master_id=script_getnum(st,7);
  8712. if( script_hasdata(st,8) )
  8713. mode=script_getnum(st,8);
  8714. if( script_hasdata(st,9) )
  8715. flag=script_getnum(st,9);
  8716. if( script_hasdata(st,10) )
  8717. duration=script_getnum(st,10);
  8718. check_event(st, event);
  8719. m = map_mapname2mapid(mapname);
  8720. if (m < 0)
  8721. return SCRIPT_CMD_SUCCESS;
  8722. sd = map_charid2sd(char_id);
  8723. if (master_id) {
  8724. msd = map_charid2sd(master_id);
  8725. if (msd)
  8726. master_id = msd->bl.id;
  8727. else
  8728. master_id = 0;
  8729. }
  8730. if (sd) //Return ID of newly crafted clone.
  8731. script_pushint(st,mob_clone_spawn(sd, m, x, y, event, master_id, mode, flag, 1000*duration));
  8732. else //Failed to create clone.
  8733. script_pushint(st,0);
  8734. return SCRIPT_CMD_SUCCESS;
  8735. }
  8736. /*==========================================
  8737. *------------------------------------------*/
  8738. BUILDIN_FUNC(doevent)
  8739. {
  8740. const char* event = script_getstr(st,2);
  8741. struct map_session_data* sd;
  8742. if( ( sd = script_rid2sd(st) ) == NULL )
  8743. {
  8744. return SCRIPT_CMD_SUCCESS;
  8745. }
  8746. check_event(st, event);
  8747. npc_event(sd, event, 0);
  8748. return SCRIPT_CMD_SUCCESS;
  8749. }
  8750. /*==========================================
  8751. *------------------------------------------*/
  8752. BUILDIN_FUNC(donpcevent)
  8753. {
  8754. const char* event = script_getstr(st,2);
  8755. check_event(st, event);
  8756. if( !npc_event_do(event) ) {
  8757. struct npc_data * nd = map_id2nd(st->oid);
  8758. ShowDebug("NPCEvent '%s' not found! (source: %s)\n",event,nd?nd->name:"Unknown");
  8759. script_pushint(st, 0);
  8760. } else
  8761. script_pushint(st, 1);
  8762. return SCRIPT_CMD_SUCCESS;
  8763. }
  8764. /// for Aegis compatibility
  8765. /// basically a specialized 'donpcevent', with the event specified as two arguments instead of one
  8766. BUILDIN_FUNC(cmdothernpc) // Added by RoVeRT
  8767. {
  8768. const char* npc = script_getstr(st,2);
  8769. const char* command = script_getstr(st,3);
  8770. char event[EVENT_NAME_LENGTH];
  8771. snprintf(event, sizeof(event), "%s::OnCommand%s", npc, command);
  8772. check_event(st, event);
  8773. npc_event_do(event);
  8774. return SCRIPT_CMD_SUCCESS;
  8775. }
  8776. /*==========================================
  8777. *------------------------------------------*/
  8778. BUILDIN_FUNC(addtimer)
  8779. {
  8780. int tick = script_getnum(st,2);
  8781. const char* event = script_getstr(st, 3);
  8782. TBL_PC* sd;
  8783. check_event(st, event);
  8784. sd = script_rid2sd(st);
  8785. if( sd == NULL )
  8786. return SCRIPT_CMD_SUCCESS;
  8787. if (!pc_addeventtimer(sd,tick,event)) {
  8788. ShowWarning("buildin_addtimer: Event timer is full, can't add new event timer. (cid:%d timer:%s)\n",sd->status.char_id,event);
  8789. return SCRIPT_CMD_FAILURE;
  8790. }
  8791. return SCRIPT_CMD_SUCCESS;
  8792. }
  8793. /*==========================================
  8794. *------------------------------------------*/
  8795. BUILDIN_FUNC(deltimer)
  8796. {
  8797. const char *event;
  8798. TBL_PC* sd;
  8799. event=script_getstr(st, 2);
  8800. sd = script_rid2sd(st);
  8801. if( sd == NULL )
  8802. return SCRIPT_CMD_SUCCESS;
  8803. check_event(st, event);
  8804. pc_deleventtimer(sd,event);
  8805. return SCRIPT_CMD_SUCCESS;
  8806. }
  8807. /*==========================================
  8808. *------------------------------------------*/
  8809. BUILDIN_FUNC(addtimercount)
  8810. {
  8811. const char *event;
  8812. int tick;
  8813. TBL_PC* sd;
  8814. tick=script_getnum(st,2);
  8815. event=script_getstr(st,3);
  8816. sd = script_rid2sd(st);
  8817. if( sd == NULL )
  8818. return SCRIPT_CMD_SUCCESS;
  8819. check_event(st, event);
  8820. pc_addeventtimercount(sd,event,tick);
  8821. return SCRIPT_CMD_SUCCESS;
  8822. }
  8823. /*==========================================
  8824. *------------------------------------------*/
  8825. BUILDIN_FUNC(initnpctimer)
  8826. {
  8827. struct npc_data *nd;
  8828. int flag = 0;
  8829. if( script_hasdata(st,3) )
  8830. { //Two arguments: NPC name and attach flag.
  8831. nd = npc_name2id(script_getstr(st, 2));
  8832. flag = script_getnum(st,3);
  8833. }
  8834. else if( script_hasdata(st,2) )
  8835. { //Check if argument is numeric (flag) or string (npc name)
  8836. struct script_data *data;
  8837. data = script_getdata(st,2);
  8838. get_val(st,data);
  8839. if( data_isstring(data) ) //NPC name
  8840. nd = npc_name2id(conv_str(st, data));
  8841. else if( data_isint(data) ) //Flag
  8842. {
  8843. nd = (struct npc_data *)map_id2bl(st->oid);
  8844. flag = conv_num(st,data);
  8845. }
  8846. else
  8847. {
  8848. ShowError("initnpctimer: invalid argument type #1 (needs be int or string)).\n");
  8849. return SCRIPT_CMD_FAILURE;
  8850. }
  8851. }
  8852. else
  8853. nd = (struct npc_data *)map_id2bl(st->oid);
  8854. if( !nd )
  8855. return SCRIPT_CMD_SUCCESS;
  8856. if( flag ) //Attach
  8857. {
  8858. TBL_PC* sd = script_rid2sd(st);
  8859. if( sd == NULL )
  8860. return SCRIPT_CMD_SUCCESS;
  8861. nd->u.scr.rid = sd->bl.id;
  8862. }
  8863. nd->u.scr.timertick = 0;
  8864. npc_settimerevent_tick(nd,0);
  8865. npc_timerevent_start(nd, st->rid);
  8866. return SCRIPT_CMD_SUCCESS;
  8867. }
  8868. /*==========================================
  8869. *------------------------------------------*/
  8870. BUILDIN_FUNC(startnpctimer)
  8871. {
  8872. struct npc_data *nd;
  8873. int flag = 0;
  8874. if( script_hasdata(st,3) )
  8875. { //Two arguments: NPC name and attach flag.
  8876. nd = npc_name2id(script_getstr(st, 2));
  8877. flag = script_getnum(st,3);
  8878. }
  8879. else if( script_hasdata(st,2) )
  8880. { //Check if argument is numeric (flag) or string (npc name)
  8881. struct script_data *data;
  8882. data = script_getdata(st,2);
  8883. get_val(st,data);
  8884. if( data_isstring(data) ) //NPC name
  8885. nd = npc_name2id(conv_str(st, data));
  8886. else if( data_isint(data) ) //Flag
  8887. {
  8888. nd = (struct npc_data *)map_id2bl(st->oid);
  8889. flag = conv_num(st,data);
  8890. }
  8891. else
  8892. {
  8893. ShowError("initnpctimer: invalid argument type #1 (needs be int or string)).\n");
  8894. return SCRIPT_CMD_FAILURE;
  8895. }
  8896. }
  8897. else
  8898. nd=(struct npc_data *)map_id2bl(st->oid);
  8899. if( !nd )
  8900. return SCRIPT_CMD_SUCCESS;
  8901. if( flag ) //Attach
  8902. {
  8903. TBL_PC* sd = script_rid2sd(st);
  8904. if( sd == NULL )
  8905. return SCRIPT_CMD_SUCCESS;
  8906. nd->u.scr.rid = sd->bl.id;
  8907. }
  8908. npc_timerevent_start(nd, st->rid);
  8909. return SCRIPT_CMD_SUCCESS;
  8910. }
  8911. /*==========================================
  8912. *------------------------------------------*/
  8913. BUILDIN_FUNC(stopnpctimer)
  8914. {
  8915. struct npc_data *nd;
  8916. int flag = 0;
  8917. if( script_hasdata(st,3) )
  8918. { //Two arguments: NPC name and attach flag.
  8919. nd = npc_name2id(script_getstr(st, 2));
  8920. flag = script_getnum(st,3);
  8921. }
  8922. else if( script_hasdata(st,2) )
  8923. { //Check if argument is numeric (flag) or string (npc name)
  8924. struct script_data *data;
  8925. data = script_getdata(st,2);
  8926. get_val(st,data);
  8927. if( data_isstring(data) ) //NPC name
  8928. nd = npc_name2id(conv_str(st, data));
  8929. else if( data_isint(data) ) //Flag
  8930. {
  8931. nd = (struct npc_data *)map_id2bl(st->oid);
  8932. flag = conv_num(st,data);
  8933. }
  8934. else
  8935. {
  8936. ShowError("initnpctimer: invalid argument type #1 (needs be int or string)).\n");
  8937. return SCRIPT_CMD_FAILURE;
  8938. }
  8939. }
  8940. else
  8941. nd=(struct npc_data *)map_id2bl(st->oid);
  8942. if( !nd )
  8943. return SCRIPT_CMD_SUCCESS;
  8944. if( flag ) //Detach
  8945. nd->u.scr.rid = 0;
  8946. npc_timerevent_stop(nd);
  8947. return SCRIPT_CMD_SUCCESS;
  8948. }
  8949. /*==========================================
  8950. *------------------------------------------*/
  8951. BUILDIN_FUNC(getnpctimer)
  8952. {
  8953. struct npc_data *nd;
  8954. TBL_PC *sd;
  8955. int type = script_getnum(st,2);
  8956. int val = 0;
  8957. if( script_hasdata(st,3) )
  8958. nd = npc_name2id(script_getstr(st,3));
  8959. else
  8960. nd = (struct npc_data *)map_id2bl(st->oid);
  8961. if( !nd || nd->bl.type != BL_NPC )
  8962. {
  8963. script_pushint(st,0);
  8964. ShowError("getnpctimer: Invalid NPC.\n");
  8965. return SCRIPT_CMD_FAILURE;
  8966. }
  8967. switch( type )
  8968. {
  8969. case 0: val = npc_gettimerevent_tick(nd); break;
  8970. case 1:
  8971. if( nd->u.scr.rid )
  8972. {
  8973. sd = map_id2sd(nd->u.scr.rid);
  8974. if( !sd )
  8975. {
  8976. ShowError("buildin_getnpctimer: Attached player not found!\n");
  8977. break;
  8978. }
  8979. val = (sd->npc_timer_id != INVALID_TIMER);
  8980. }
  8981. else
  8982. val = (nd->u.scr.timerid != INVALID_TIMER);
  8983. break;
  8984. case 2: val = nd->u.scr.timeramount; break;
  8985. }
  8986. script_pushint(st,val);
  8987. return SCRIPT_CMD_SUCCESS;
  8988. }
  8989. /*==========================================
  8990. *------------------------------------------*/
  8991. BUILDIN_FUNC(setnpctimer)
  8992. {
  8993. int tick;
  8994. struct npc_data *nd;
  8995. tick = script_getnum(st,2);
  8996. if( script_hasdata(st,3) )
  8997. nd = npc_name2id(script_getstr(st,3));
  8998. else
  8999. nd = (struct npc_data *)map_id2bl(st->oid);
  9000. if( !nd || nd->bl.type != BL_NPC )
  9001. {
  9002. script_pushint(st,1);
  9003. ShowError("setnpctimer: Invalid NPC.\n");
  9004. return SCRIPT_CMD_FAILURE;
  9005. }
  9006. npc_settimerevent_tick(nd,tick);
  9007. script_pushint(st,0);
  9008. return SCRIPT_CMD_SUCCESS;
  9009. }
  9010. /*==========================================
  9011. * attaches the player rid to the timer [Celest]
  9012. *------------------------------------------*/
  9013. BUILDIN_FUNC(attachnpctimer)
  9014. {
  9015. TBL_PC *sd;
  9016. struct npc_data *nd = (struct npc_data *)map_id2bl(st->oid);
  9017. if( !nd || nd->bl.type != BL_NPC )
  9018. {
  9019. script_pushint(st,1);
  9020. ShowError("setnpctimer: Invalid NPC.\n");
  9021. return SCRIPT_CMD_FAILURE;
  9022. }
  9023. if( script_hasdata(st,2) )
  9024. sd = map_nick2sd(script_getstr(st,2));
  9025. else
  9026. sd = script_rid2sd(st);
  9027. if( !sd )
  9028. {
  9029. script_pushint(st,1);
  9030. ShowWarning("attachnpctimer: Invalid player.\n");
  9031. return SCRIPT_CMD_FAILURE;
  9032. }
  9033. nd->u.scr.rid = sd->bl.id;
  9034. script_pushint(st,0);
  9035. return SCRIPT_CMD_SUCCESS;
  9036. }
  9037. /*==========================================
  9038. * detaches a player rid from the timer [Celest]
  9039. *------------------------------------------*/
  9040. BUILDIN_FUNC(detachnpctimer)
  9041. {
  9042. struct npc_data *nd;
  9043. if( script_hasdata(st,2) )
  9044. nd = npc_name2id(script_getstr(st,2));
  9045. else
  9046. nd = (struct npc_data *)map_id2bl(st->oid);
  9047. if( !nd || nd->bl.type != BL_NPC )
  9048. {
  9049. script_pushint(st,1);
  9050. ShowError("detachnpctimer: Invalid NPC.\n");
  9051. return SCRIPT_CMD_FAILURE;
  9052. }
  9053. nd->u.scr.rid = 0;
  9054. script_pushint(st,0);
  9055. return SCRIPT_CMD_SUCCESS;
  9056. }
  9057. /*==========================================
  9058. * To avoid "player not attached" script errors, this function is provided,
  9059. * it checks if there is a player attached to the current script. [Skotlex]
  9060. * If no, returns 0, if yes, returns the account_id of the attached player.
  9061. *------------------------------------------*/
  9062. BUILDIN_FUNC(playerattached)
  9063. {
  9064. if(st->rid == 0 || map_id2sd(st->rid) == NULL)
  9065. script_pushint(st,0);
  9066. else
  9067. script_pushint(st,st->rid);
  9068. return SCRIPT_CMD_SUCCESS;
  9069. }
  9070. /*==========================================
  9071. *------------------------------------------*/
  9072. BUILDIN_FUNC(announce)
  9073. {
  9074. const char *mes = script_getstr(st,2);
  9075. int flag = script_getnum(st,3);
  9076. const char *fontColor = script_hasdata(st,4) ? script_getstr(st,4) : NULL;
  9077. int fontType = script_hasdata(st,5) ? script_getnum(st,5) : FW_NORMAL; // default fontType
  9078. int fontSize = script_hasdata(st,6) ? script_getnum(st,6) : 12; // default fontSize
  9079. int fontAlign = script_hasdata(st,7) ? script_getnum(st,7) : 0; // default fontAlign
  9080. int fontY = script_hasdata(st,8) ? script_getnum(st,8) : 0; // default fontY
  9081. if (flag&(BC_TARGET_MASK|BC_SOURCE_MASK)) // Broadcast source or broadcast region defined
  9082. {
  9083. send_target target;
  9084. 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
  9085. if (bl == NULL)
  9086. return SCRIPT_CMD_SUCCESS;
  9087. switch (flag&BC_TARGET_MASK) {
  9088. case BC_MAP: target = ALL_SAMEMAP; break;
  9089. case BC_AREA: target = AREA; break;
  9090. case BC_SELF: target = SELF; break;
  9091. default: target = ALL_CLIENT; break; // BC_ALL
  9092. }
  9093. if (fontColor)
  9094. clif_broadcast2(bl, mes, (int)strlen(mes)+1, strtol(fontColor, (char **)NULL, 0), fontType, fontSize, fontAlign, fontY, target);
  9095. else
  9096. clif_broadcast(bl, mes, (int)strlen(mes)+1, flag&BC_COLOR_MASK, target);
  9097. }
  9098. else
  9099. {
  9100. if (fontColor)
  9101. intif_broadcast2(mes, (int)strlen(mes)+1, strtol(fontColor, (char **)NULL, 0), fontType, fontSize, fontAlign, fontY);
  9102. else
  9103. intif_broadcast(mes, (int)strlen(mes)+1, flag&BC_COLOR_MASK);
  9104. }
  9105. return SCRIPT_CMD_SUCCESS;
  9106. }
  9107. /*==========================================
  9108. *------------------------------------------*/
  9109. static int buildin_announce_sub(struct block_list *bl, va_list ap)
  9110. {
  9111. char *mes = va_arg(ap, char *);
  9112. int len = va_arg(ap, int);
  9113. int type = va_arg(ap, int);
  9114. char *fontColor = va_arg(ap, char *);
  9115. short fontType = (short)va_arg(ap, int);
  9116. short fontSize = (short)va_arg(ap, int);
  9117. short fontAlign = (short)va_arg(ap, int);
  9118. short fontY = (short)va_arg(ap, int);
  9119. if (fontColor)
  9120. clif_broadcast2(bl, mes, len, strtol(fontColor, (char **)NULL, 0), fontType, fontSize, fontAlign, fontY, SELF);
  9121. else
  9122. clif_broadcast(bl, mes, len, type, SELF);
  9123. return SCRIPT_CMD_SUCCESS;
  9124. }
  9125. BUILDIN_FUNC(mapannounce)
  9126. {
  9127. const char *mapname = script_getstr(st,2);
  9128. const char *mes = script_getstr(st,3);
  9129. int flag = script_getnum(st,4);
  9130. const char *fontColor = script_hasdata(st,5) ? script_getstr(st,5) : NULL;
  9131. int fontType = script_hasdata(st,6) ? script_getnum(st,6) : FW_NORMAL; // default fontType
  9132. int fontSize = script_hasdata(st,7) ? script_getnum(st,7) : 12; // default fontSize
  9133. int fontAlign = script_hasdata(st,8) ? script_getnum(st,8) : 0; // default fontAlign
  9134. int fontY = script_hasdata(st,9) ? script_getnum(st,9) : 0; // default fontY
  9135. int16 m;
  9136. if ((m = map_mapname2mapid(mapname)) < 0)
  9137. return SCRIPT_CMD_SUCCESS;
  9138. map_foreachinmap(buildin_announce_sub, m, BL_PC,
  9139. mes, strlen(mes)+1, flag&BC_COLOR_MASK, fontColor, fontType, fontSize, fontAlign, fontY);
  9140. return SCRIPT_CMD_SUCCESS;
  9141. }
  9142. /*==========================================
  9143. *------------------------------------------*/
  9144. BUILDIN_FUNC(areaannounce)
  9145. {
  9146. const char *mapname = script_getstr(st,2);
  9147. int x0 = script_getnum(st,3);
  9148. int y0 = script_getnum(st,4);
  9149. int x1 = script_getnum(st,5);
  9150. int y1 = script_getnum(st,6);
  9151. const char *mes = script_getstr(st,7);
  9152. int flag = script_getnum(st,8);
  9153. const char *fontColor = script_hasdata(st,9) ? script_getstr(st,9) : NULL;
  9154. int fontType = script_hasdata(st,10) ? script_getnum(st,10) : FW_NORMAL; // default fontType
  9155. int fontSize = script_hasdata(st,11) ? script_getnum(st,11) : 12; // default fontSize
  9156. int fontAlign = script_hasdata(st,12) ? script_getnum(st,12) : 0; // default fontAlign
  9157. int fontY = script_hasdata(st,13) ? script_getnum(st,13) : 0; // default fontY
  9158. int16 m;
  9159. if ((m = map_mapname2mapid(mapname)) < 0)
  9160. return SCRIPT_CMD_SUCCESS;
  9161. map_foreachinarea(buildin_announce_sub, m, x0, y0, x1, y1, BL_PC,
  9162. mes, strlen(mes)+1, flag&BC_COLOR_MASK, fontColor, fontType, fontSize, fontAlign, fontY);
  9163. return SCRIPT_CMD_SUCCESS;
  9164. }
  9165. /*==========================================
  9166. *------------------------------------------*/
  9167. BUILDIN_FUNC(getusers)
  9168. {
  9169. int flag, val = 0;
  9170. struct map_session_data* sd;
  9171. struct block_list* bl = NULL;
  9172. flag = script_getnum(st,2);
  9173. switch(flag&0x07)
  9174. {
  9175. case 0:
  9176. if(flag&0x8)
  9177. {// npc
  9178. bl = map_id2bl(st->oid);
  9179. }
  9180. else if((sd = script_rid2sd(st))!=NULL)
  9181. {// pc
  9182. bl = &sd->bl;
  9183. }
  9184. if(bl)
  9185. {
  9186. val = map[bl->m].users;
  9187. }
  9188. break;
  9189. case 1:
  9190. val = map_getusers();
  9191. break;
  9192. default:
  9193. ShowWarning("buildin_getusers: Unknown type %d.\n", flag);
  9194. script_pushint(st,0);
  9195. return SCRIPT_CMD_FAILURE;
  9196. }
  9197. script_pushint(st,val);
  9198. return SCRIPT_CMD_SUCCESS;
  9199. }
  9200. /*==========================================
  9201. * getmapguildusers("mapname",guild ID) Returns the number guild members present on a map [Reddozen]
  9202. *------------------------------------------*/
  9203. BUILDIN_FUNC(getmapguildusers)
  9204. {
  9205. const char *str;
  9206. int16 m;
  9207. int gid;
  9208. int c=0;
  9209. struct guild *g = NULL;
  9210. str=script_getstr(st,2);
  9211. gid=script_getnum(st,3);
  9212. if ((m = map_mapname2mapid(str)) < 0) { // map id on this server (m == -1 if not in actual map-server)
  9213. script_pushint(st,-1);
  9214. return SCRIPT_CMD_SUCCESS;
  9215. }
  9216. g = guild_search(gid);
  9217. if (g){
  9218. unsigned short i;
  9219. for(i = 0; i < g->max_member; i++)
  9220. {
  9221. if (g->member[i].sd && g->member[i].sd->bl.m == m)
  9222. c++;
  9223. }
  9224. }
  9225. script_pushint(st,c);
  9226. return SCRIPT_CMD_SUCCESS;
  9227. }
  9228. /*==========================================
  9229. *------------------------------------------*/
  9230. BUILDIN_FUNC(getmapusers)
  9231. {
  9232. const char *str;
  9233. int16 m;
  9234. str=script_getstr(st,2);
  9235. if( (m=map_mapname2mapid(str))< 0){
  9236. script_pushint(st,-1);
  9237. return SCRIPT_CMD_SUCCESS;
  9238. }
  9239. script_pushint(st,map[m].users);
  9240. return SCRIPT_CMD_SUCCESS;
  9241. }
  9242. /*==========================================
  9243. *------------------------------------------*/
  9244. static int buildin_getareausers_sub(struct block_list *bl,va_list ap)
  9245. {
  9246. int *users=va_arg(ap,int *);
  9247. (*users)++;
  9248. return SCRIPT_CMD_SUCCESS;
  9249. }
  9250. BUILDIN_FUNC(getareausers)
  9251. {
  9252. const char *str;
  9253. int16 m,x0,y0,x1,y1,users=0; //doubt we can have more then 32k users on
  9254. str=script_getstr(st,2);
  9255. x0=script_getnum(st,3);
  9256. y0=script_getnum(st,4);
  9257. x1=script_getnum(st,5);
  9258. y1=script_getnum(st,6);
  9259. if( (m=map_mapname2mapid(str))< 0){
  9260. script_pushint(st,-1);
  9261. return SCRIPT_CMD_SUCCESS;
  9262. }
  9263. map_foreachinarea(buildin_getareausers_sub,
  9264. m,x0,y0,x1,y1,BL_PC,&users);
  9265. script_pushint(st,users);
  9266. return SCRIPT_CMD_SUCCESS;
  9267. }
  9268. /*==========================================
  9269. *------------------------------------------*/
  9270. static int buildin_getareadropitem_sub(struct block_list *bl,va_list ap)
  9271. {
  9272. unsigned short nameid = va_arg(ap, unsigned short);
  9273. unsigned short *amount = va_arg(ap, unsigned short *);
  9274. struct flooritem_data *drop=(struct flooritem_data *)bl;
  9275. if(drop->item.nameid==nameid)
  9276. (*amount)+=drop->item.amount;
  9277. return SCRIPT_CMD_SUCCESS;
  9278. }
  9279. BUILDIN_FUNC(getareadropitem)
  9280. {
  9281. const char *str;
  9282. int16 m,x0,y0,x1,y1;
  9283. unsigned short nameid, amount = 0;
  9284. struct script_data *data;
  9285. str=script_getstr(st,2);
  9286. x0=script_getnum(st,3);
  9287. y0=script_getnum(st,4);
  9288. x1=script_getnum(st,5);
  9289. y1=script_getnum(st,6);
  9290. data=script_getdata(st,7);
  9291. get_val(st,data);
  9292. if( data_isstring(data) ){
  9293. const char *name=conv_str(st,data);
  9294. struct item_data *item_data = itemdb_searchname(name);
  9295. nameid=UNKNOWN_ITEM_ID;
  9296. if( item_data )
  9297. nameid=item_data->nameid;
  9298. }else
  9299. nameid=conv_num(st,data);
  9300. if( (m=map_mapname2mapid(str))< 0){
  9301. script_pushint(st,-1);
  9302. return SCRIPT_CMD_SUCCESS;
  9303. }
  9304. map_foreachinarea(buildin_getareadropitem_sub,
  9305. m,x0,y0,x1,y1,BL_ITEM,nameid,&amount);
  9306. script_pushint(st,amount);
  9307. return SCRIPT_CMD_SUCCESS;
  9308. }
  9309. /*==========================================
  9310. *------------------------------------------*/
  9311. BUILDIN_FUNC(enablenpc)
  9312. {
  9313. const char *str;
  9314. str=script_getstr(st,2);
  9315. npc_enable(str,1);
  9316. return SCRIPT_CMD_SUCCESS;
  9317. }
  9318. /*==========================================
  9319. *------------------------------------------*/
  9320. BUILDIN_FUNC(disablenpc)
  9321. {
  9322. const char *str;
  9323. str=script_getstr(st,2);
  9324. npc_enable(str,0);
  9325. return SCRIPT_CMD_SUCCESS;
  9326. }
  9327. /*==========================================
  9328. *------------------------------------------*/
  9329. BUILDIN_FUNC(hideoffnpc)
  9330. {
  9331. const char *str;
  9332. str=script_getstr(st,2);
  9333. npc_enable(str,2);
  9334. return SCRIPT_CMD_SUCCESS;
  9335. }
  9336. /*==========================================
  9337. *------------------------------------------*/
  9338. BUILDIN_FUNC(hideonnpc)
  9339. {
  9340. const char *str;
  9341. str=script_getstr(st,2);
  9342. npc_enable(str,4);
  9343. return SCRIPT_CMD_SUCCESS;
  9344. }
  9345. /* Starts a status effect on the target unit or on the attached player.
  9346. *
  9347. * sc_start <effect_id>,<duration>,<val1>{,<rate>,<flag>,{<unit_id>}};
  9348. * sc_start2 <effect_id>,<duration>,<val1>,<val2>{,<rate,<flag>,{<unit_id>}};
  9349. * sc_start4 <effect_id>,<duration>,<val1>,<val2>,<val3>,<val4>{,<rate,<flag>,{<unit_id>}};
  9350. * <flag>: enum e_status_change_start_flags
  9351. */
  9352. BUILDIN_FUNC(sc_start)
  9353. {
  9354. TBL_NPC * nd = map_id2nd(st->oid);
  9355. struct block_list* bl;
  9356. enum sc_type type;
  9357. int tick, val1, val2, val3, val4=0, rate, flag;
  9358. char start_type;
  9359. const char* command = script_getfuncname(st);
  9360. if(strstr(command, "4"))
  9361. start_type = 4;
  9362. else if(strstr(command, "2"))
  9363. start_type = 2;
  9364. else
  9365. start_type = 1;
  9366. type = (sc_type)script_getnum(st,2);
  9367. tick = script_getnum(st,3);
  9368. val1 = script_getnum(st,4);
  9369. //If from NPC we make default flag 1 to be unavoidable
  9370. if(nd && nd->bl.id == fake_nd->bl.id)
  9371. flag = script_hasdata(st,5+start_type)?script_getnum(st,5+start_type):SCSTART_NOTICKDEF;
  9372. else
  9373. flag = script_hasdata(st,5+start_type)?script_getnum(st,5+start_type):SCSTART_NOAVOID;
  9374. rate = script_hasdata(st,4+start_type)?min(script_getnum(st,4+start_type),10000):10000;
  9375. if(script_hasdata(st,(6+start_type)))
  9376. bl = map_id2bl(script_getnum(st,(6+start_type)));
  9377. else
  9378. bl = map_id2bl(st->rid);
  9379. if(tick == 0 && val1 > 0 && type > SC_NONE && type < SC_MAX && status_sc2skill(type) != 0)
  9380. {// When there isn't a duration specified, try to get it from the skill_db
  9381. tick = skill_get_time(status_sc2skill(type), val1);
  9382. }
  9383. if(potion_flag == 1 && potion_target) { //skill.c set the flags before running the script, this is a potion-pitched effect.
  9384. bl = map_id2bl(potion_target);
  9385. tick /= 2;// Thrown potions only last half.
  9386. val4 = 1;// Mark that this was a thrown sc_effect
  9387. }
  9388. if(!bl)
  9389. return SCRIPT_CMD_SUCCESS;
  9390. switch(start_type) {
  9391. case 1:
  9392. status_change_start(bl, bl, type, rate, val1, 0, 0, val4, tick, flag);
  9393. break;
  9394. case 2:
  9395. val2 = script_getnum(st,5);
  9396. status_change_start(bl, bl, type, rate, val1, val2, 0, val4, tick, flag);
  9397. break;
  9398. case 4:
  9399. val2 = script_getnum(st,5);
  9400. val3 = script_getnum(st,6);
  9401. val4 = script_getnum(st,7);
  9402. status_change_start(bl, bl, type, rate, val1, val2, val3, val4, tick, flag);
  9403. break;
  9404. }
  9405. return SCRIPT_CMD_SUCCESS;
  9406. }
  9407. /// Ends one or all status effects on the target unit or on the attached player.
  9408. ///
  9409. /// sc_end <effect_id>{,<unit_id>};
  9410. BUILDIN_FUNC(sc_end)
  9411. {
  9412. struct block_list* bl;
  9413. int type;
  9414. type = script_getnum(st, 2);
  9415. if (script_hasdata(st, 3))
  9416. bl = map_id2bl(script_getnum(st, 3));
  9417. else
  9418. bl = map_id2bl(st->rid);
  9419. if (potion_flag == 1 && potion_target) //##TODO how does this work [FlavioJS]
  9420. bl = map_id2bl(potion_target);
  9421. if (!bl)
  9422. return SCRIPT_CMD_SUCCESS;
  9423. if (type >= 0 && type < SC_MAX) {
  9424. struct status_change *sc = status_get_sc(bl);
  9425. struct status_change_entry *sce = sc ? sc->data[type] : NULL;
  9426. if (!sce)
  9427. return SCRIPT_CMD_SUCCESS;
  9428. switch (type) {
  9429. case SC_WEIGHT50:
  9430. case SC_WEIGHT90:
  9431. case SC_NOCHAT:
  9432. case SC_PUSH_CART:
  9433. case SC_ALL_RIDING:
  9434. case SC_STYLE_CHANGE:
  9435. case SC_MONSTER_TRANSFORM:
  9436. case SC_MTF_ASPD:
  9437. case SC_MTF_RANGEATK:
  9438. case SC_MTF_MATK:
  9439. case SC_MTF_MLEATKED:
  9440. case SC_MTF_CRIDAMAGE:
  9441. case SC_MTF_ASPD2:
  9442. case SC_MTF_RANGEATK2:
  9443. case SC_MTF_MATK2:
  9444. case SC_MTF_MHP:
  9445. case SC_MTF_MSP:
  9446. case SC_MTF_PUMPKIN:
  9447. case SC_MTF_HITFLEE:
  9448. case SC_ATTHASTE_CASH:
  9449. case SC_REUSE_LIMIT_A: case SC_REUSE_LIMIT_B: case SC_REUSE_LIMIT_C:
  9450. case SC_REUSE_LIMIT_D: case SC_REUSE_LIMIT_E: case SC_REUSE_LIMIT_F:
  9451. case SC_REUSE_LIMIT_G: case SC_REUSE_LIMIT_H: case SC_REUSE_LIMIT_MTF:
  9452. case SC_REUSE_LIMIT_ASPD_POTION: case SC_REUSE_MILLENNIUMSHIELD: case SC_REUSE_CRUSHSTRIKE:
  9453. case SC_REUSE_STORMBLAST: case SC_ALL_RIDING_REUSE_LIMIT: case SC_REUSE_REFRESH:
  9454. case SC_REUSE_LIMIT_ECL: case SC_REUSE_LIMIT_RECALL:
  9455. return SCRIPT_CMD_SUCCESS;
  9456. default:
  9457. break;
  9458. }
  9459. //This should help status_change_end force disabling the SC in case it has no limit.
  9460. sce->val1 = sce->val2 = sce->val3 = sce->val4 = 0;
  9461. status_change_end(bl, (sc_type)type, INVALID_TIMER);
  9462. } else
  9463. status_change_clear(bl, 3); // remove all effects
  9464. return SCRIPT_CMD_SUCCESS;
  9465. }
  9466. /*==========================================
  9467. * @FIXME atm will return reduced tick, 0 immune, 1 no tick
  9468. *------------------------------------------*/
  9469. BUILDIN_FUNC(getscrate)
  9470. {
  9471. struct block_list *bl;
  9472. int type,rate;
  9473. type=script_getnum(st,2);
  9474. rate=script_getnum(st,3);
  9475. if( script_hasdata(st,4) ) //get for the bl assigned
  9476. bl = map_id2bl(script_getnum(st,4));
  9477. else
  9478. bl = map_id2bl(st->rid);
  9479. if (bl)
  9480. rate = status_get_sc_def(NULL,bl, (sc_type)type, 10000, 10000, SCSTART_NONE);
  9481. script_pushint(st,rate);
  9482. return SCRIPT_CMD_SUCCESS;
  9483. }
  9484. /**
  9485. * getstatus(<effect type>{,<type>{,<char_id>}});
  9486. **/
  9487. BUILDIN_FUNC(getstatus)
  9488. {
  9489. int id, type;
  9490. struct map_session_data* sd;
  9491. if (!script_charid2sd(4,sd))
  9492. return SCRIPT_CMD_FAILURE;
  9493. id = script_getnum(st, 2);
  9494. type = script_hasdata(st, 3) ? script_getnum(st, 3) : 0;
  9495. if( id <= SC_NONE || id >= SC_MAX )
  9496. {// invalid status type given
  9497. ShowWarning("script.c:getstatus: Invalid status type given (%d).\n", id);
  9498. return SCRIPT_CMD_SUCCESS;
  9499. }
  9500. if( sd->sc.count == 0 || !sd->sc.data[id] )
  9501. {// no status is active
  9502. script_pushint(st, 0);
  9503. return SCRIPT_CMD_SUCCESS;
  9504. }
  9505. switch( type )
  9506. {
  9507. case 1: script_pushint(st, sd->sc.data[id]->val1); break;
  9508. case 2: script_pushint(st, sd->sc.data[id]->val2); break;
  9509. case 3: script_pushint(st, sd->sc.data[id]->val3); break;
  9510. case 4: script_pushint(st, sd->sc.data[id]->val4); break;
  9511. case 5:
  9512. {
  9513. struct TimerData* timer = (struct TimerData*)get_timer(sd->sc.data[id]->timer);
  9514. if( timer )
  9515. {// return the amount of time remaining
  9516. script_pushint(st, timer->tick - gettick());
  9517. } else {
  9518. script_pushint(st, -1);
  9519. }
  9520. }
  9521. break;
  9522. default: script_pushint(st, 1); break;
  9523. }
  9524. return SCRIPT_CMD_SUCCESS;
  9525. }
  9526. /*==========================================
  9527. *
  9528. *------------------------------------------*/
  9529. BUILDIN_FUNC(debugmes)
  9530. {
  9531. const char *str;
  9532. str=script_getstr(st,2);
  9533. ShowDebug("script debug : %d %d : %s\n",st->rid,st->oid,str);
  9534. return SCRIPT_CMD_SUCCESS;
  9535. }
  9536. /*==========================================
  9537. *------------------------------------------*/
  9538. BUILDIN_FUNC(catchpet)
  9539. {
  9540. int pet_id;
  9541. TBL_PC *sd;
  9542. pet_id= script_getnum(st,2);
  9543. sd=script_rid2sd(st);
  9544. if( sd == NULL )
  9545. return SCRIPT_CMD_SUCCESS;
  9546. pet_catch_process1(sd,pet_id);
  9547. return SCRIPT_CMD_SUCCESS;
  9548. }
  9549. /*==========================================
  9550. * [orn]
  9551. *------------------------------------------*/
  9552. BUILDIN_FUNC(homunculus_evolution)
  9553. {
  9554. TBL_PC *sd;
  9555. sd=script_rid2sd(st);
  9556. if( sd == NULL )
  9557. return SCRIPT_CMD_SUCCESS;
  9558. if(hom_is_active(sd->hd))
  9559. {
  9560. if (sd->hd->homunculus.intimacy >= battle_config.homunculus_evo_intimacy_need)
  9561. hom_evolution(sd->hd);
  9562. else
  9563. clif_emotion(&sd->hd->bl, E_SWT);
  9564. }
  9565. return SCRIPT_CMD_SUCCESS;
  9566. }
  9567. /*==========================================
  9568. * Checks for vaporized morph state
  9569. * and deletes ITEMID_STRANGE_EMBRYO.
  9570. *------------------------------------------*/
  9571. BUILDIN_FUNC(homunculus_mutate)
  9572. {
  9573. int homun_id;
  9574. TBL_PC *sd;
  9575. sd = script_rid2sd(st);
  9576. if( sd == NULL || sd->hd == NULL )
  9577. return SCRIPT_CMD_SUCCESS;
  9578. if(script_hasdata(st,2))
  9579. homun_id = script_getnum(st,2);
  9580. else
  9581. homun_id = 6048 + (rnd() % 4);
  9582. if( sd->hd->homunculus.vaporize == HOM_ST_MORPH ) {
  9583. int m_class = hom_class2mapid(sd->hd->homunculus.class_);
  9584. int m_id = hom_class2mapid(homun_id);
  9585. short i = pc_search_inventory(sd, ITEMID_STRANGE_EMBRYO);
  9586. if ( m_class != -1 && m_id != -1 && m_class&HOM_EVO && m_id&HOM_S && sd->hd->homunculus.level >= 99 && i >= 0 ) {
  9587. sd->hd->homunculus.vaporize = HOM_ST_REST; // Remove morph state.
  9588. hom_call(sd); // Respawn homunculus.
  9589. hom_mutate(sd->hd, homun_id);
  9590. pc_delitem(sd, i, 1, 0, 0, LOG_TYPE_SCRIPT);
  9591. script_pushint(st, 1);
  9592. return SCRIPT_CMD_SUCCESS;
  9593. } else
  9594. clif_emotion(&sd->bl, E_SWT);
  9595. } else
  9596. clif_emotion(&sd->bl, E_SWT);
  9597. script_pushint(st, 0);
  9598. return SCRIPT_CMD_SUCCESS;
  9599. }
  9600. /*==========================================
  9601. * Puts homunculus into morph state
  9602. * and gives ITEMID_STRANGE_EMBRYO.
  9603. *------------------------------------------*/
  9604. BUILDIN_FUNC(morphembryo)
  9605. {
  9606. struct item item_tmp;
  9607. TBL_PC *sd;
  9608. sd = script_rid2sd(st);
  9609. if( sd == NULL || sd->hd == NULL )
  9610. return SCRIPT_CMD_SUCCESS;
  9611. if( hom_is_active(sd->hd) ) {
  9612. int m_class = hom_class2mapid(sd->hd->homunculus.class_);
  9613. if ( m_class != -1 && m_class&HOM_EVO && sd->hd->homunculus.level >= 99 ) {
  9614. char i;
  9615. memset(&item_tmp, 0, sizeof(item_tmp));
  9616. item_tmp.nameid = ITEMID_STRANGE_EMBRYO;
  9617. item_tmp.identify = 1;
  9618. if( (i = pc_additem(sd, &item_tmp, 1, LOG_TYPE_SCRIPT)) ) {
  9619. clif_additem(sd, 0, 0, i);
  9620. clif_emotion(&sd->bl, E_SWT); // Fail to avoid item drop exploit.
  9621. } else {
  9622. hom_vaporize(sd, HOM_ST_MORPH);
  9623. script_pushint(st, 1);
  9624. return SCRIPT_CMD_SUCCESS;
  9625. }
  9626. } else
  9627. clif_emotion(&sd->hd->bl, E_SWT);
  9628. } else
  9629. clif_emotion(&sd->bl, E_SWT);
  9630. script_pushint(st, 0);
  9631. return SCRIPT_CMD_SUCCESS;
  9632. }
  9633. // [Zephyrus]
  9634. BUILDIN_FUNC(homunculus_shuffle)
  9635. {
  9636. TBL_PC *sd;
  9637. sd=script_rid2sd(st);
  9638. if( sd == NULL )
  9639. return SCRIPT_CMD_SUCCESS;
  9640. if(hom_is_active(sd->hd))
  9641. hom_shuffle(sd->hd);
  9642. return SCRIPT_CMD_SUCCESS;
  9643. }
  9644. /*==========================================
  9645. * Check for homunculus state.
  9646. * Return: -1 = No homunculus
  9647. * 0 = Homunculus is active
  9648. * 1 = Homunculus is vaporized (rest)
  9649. * 2 = Homunculus is in morph state
  9650. *------------------------------------------*/
  9651. BUILDIN_FUNC(checkhomcall)
  9652. {
  9653. TBL_PC *sd = script_rid2sd(st);
  9654. TBL_HOM *hd;
  9655. if( sd == NULL )
  9656. return SCRIPT_CMD_SUCCESS;
  9657. hd = sd->hd;
  9658. if( !hd )
  9659. script_pushint(st, -1);
  9660. else
  9661. script_pushint(st, hd->homunculus.vaporize);
  9662. return SCRIPT_CMD_SUCCESS;
  9663. }
  9664. //These two functions bring the eA MAPID_* class functionality to scripts.
  9665. BUILDIN_FUNC(eaclass)
  9666. {
  9667. int class_;
  9668. if( script_hasdata(st,2) )
  9669. class_ = script_getnum(st,2);
  9670. else {
  9671. TBL_PC *sd;
  9672. if (!script_charid2sd(3,sd)) {
  9673. script_pushint(st,-1);
  9674. return SCRIPT_CMD_SUCCESS;
  9675. }
  9676. class_ = sd->status.class_;
  9677. }
  9678. script_pushint(st,pc_jobid2mapid(class_));
  9679. return SCRIPT_CMD_SUCCESS;
  9680. }
  9681. BUILDIN_FUNC(roclass)
  9682. {
  9683. int class_ =script_getnum(st,2);
  9684. int sex;
  9685. if( script_hasdata(st,3) )
  9686. sex = script_getnum(st,3);
  9687. else {
  9688. TBL_PC *sd;
  9689. if (st->rid && (sd=script_rid2sd(st)))
  9690. sex = sd->status.sex;
  9691. else
  9692. sex = 1; //Just use male when not found.
  9693. }
  9694. script_pushint(st,pc_mapid2jobid(class_, sex));
  9695. return SCRIPT_CMD_SUCCESS;
  9696. }
  9697. /*==========================================
  9698. * Tells client to open a hatching window, used for pet incubator
  9699. *------------------------------------------*/
  9700. BUILDIN_FUNC(birthpet)
  9701. {
  9702. TBL_PC *sd;
  9703. sd=script_rid2sd(st);
  9704. if( sd == NULL )
  9705. return SCRIPT_CMD_SUCCESS;
  9706. if( sd->status.pet_id )
  9707. {// do not send egg list, when you already have a pet
  9708. return SCRIPT_CMD_SUCCESS;
  9709. }
  9710. clif_sendegg(sd);
  9711. return SCRIPT_CMD_SUCCESS;
  9712. }
  9713. /**
  9714. * resetlvl <action type>{,<char_id>};
  9715. * @param action_type:
  9716. * 1 : make like after rebirth
  9717. * 2 : blvl,jlvl=1, skillpoint=0
  9718. * 3 : don't reset skill, blvl=1
  9719. * 4 : jlvl=0
  9720. * @author AppleGirl
  9721. **/
  9722. BUILDIN_FUNC(resetlvl)
  9723. {
  9724. TBL_PC *sd;
  9725. int type=script_getnum(st,2);
  9726. if (!script_charid2sd(3,sd))
  9727. return SCRIPT_CMD_FAILURE;
  9728. pc_resetlvl(sd,type);
  9729. return SCRIPT_CMD_SUCCESS;
  9730. }
  9731. /**
  9732. * Reset a player status point
  9733. * resetstatus({<char_id>});
  9734. **/
  9735. BUILDIN_FUNC(resetstatus)
  9736. {
  9737. TBL_PC *sd;
  9738. if (!script_charid2sd(2,sd))
  9739. return SCRIPT_CMD_FAILURE;
  9740. pc_resetstate(sd);
  9741. return SCRIPT_CMD_SUCCESS;
  9742. }
  9743. /**
  9744. * Reset player's skill
  9745. * resetskill({<char_id>});
  9746. **/
  9747. BUILDIN_FUNC(resetskill)
  9748. {
  9749. TBL_PC *sd;
  9750. if (!script_charid2sd(2,sd))
  9751. return SCRIPT_CMD_FAILURE;
  9752. pc_resetskill(sd,1);
  9753. return SCRIPT_CMD_SUCCESS;
  9754. }
  9755. /**
  9756. * Counts total amount of skill points.
  9757. * skillpointcount({<char_id>})
  9758. **/
  9759. BUILDIN_FUNC(skillpointcount)
  9760. {
  9761. TBL_PC *sd;
  9762. if (!script_charid2sd(2,sd))
  9763. return SCRIPT_CMD_FAILURE;
  9764. script_pushint(st,sd->status.skill_point + pc_resetskill(sd,2));
  9765. return SCRIPT_CMD_SUCCESS;
  9766. }
  9767. /*==========================================
  9768. *
  9769. *------------------------------------------*/
  9770. BUILDIN_FUNC(changebase)
  9771. {
  9772. TBL_PC *sd=NULL;
  9773. int vclass;
  9774. if( script_hasdata(st,3) )
  9775. sd=map_id2sd(script_getnum(st,3));
  9776. else
  9777. sd=script_rid2sd(st);
  9778. if(sd == NULL)
  9779. return SCRIPT_CMD_SUCCESS;
  9780. vclass = script_getnum(st,2);
  9781. if(vclass == JOB_WEDDING)
  9782. {
  9783. if (!battle_config.wedding_modifydisplay || //Do not show the wedding sprites
  9784. sd->class_&JOBL_BABY //Baby classes screw up when showing wedding sprites. [Skotlex] They don't seem to anymore.
  9785. )
  9786. return SCRIPT_CMD_SUCCESS;
  9787. }
  9788. if(!sd->disguise && vclass != sd->vd.class_) {
  9789. status_set_viewdata(&sd->bl, vclass);
  9790. //Updated client view. Base, Weapon and Cloth Colors.
  9791. clif_changelook(&sd->bl,LOOK_BASE,sd->vd.class_);
  9792. clif_changelook(&sd->bl,LOOK_WEAPON,sd->status.weapon);
  9793. if (sd->vd.cloth_color)
  9794. clif_changelook(&sd->bl,LOOK_CLOTHES_COLOR,sd->vd.cloth_color);
  9795. if (sd->vd.body_style)
  9796. clif_changelook(&sd->bl,LOOK_BODY2,sd->vd.body_style);
  9797. clif_skillinfoblock(sd);
  9798. }
  9799. return SCRIPT_CMD_SUCCESS;
  9800. }
  9801. /**
  9802. * Change account sex and unequip all item and request for a changesex to char-serv
  9803. * changesex({<char_id>});
  9804. */
  9805. BUILDIN_FUNC(changesex)
  9806. {
  9807. int i;
  9808. TBL_PC *sd = NULL;
  9809. if (!script_charid2sd(2,sd))
  9810. return SCRIPT_CMD_FAILURE;
  9811. pc_resetskill(sd,4);
  9812. // to avoid any problem with equipment and invalid sex, equipment is unequiped.
  9813. for(i = 0; i < EQI_MAX; i++) {
  9814. if (sd->equip_index[i] >= 0)
  9815. pc_unequipitem(sd, sd->equip_index[i], 3);
  9816. }
  9817. chrif_changesex(sd, true);
  9818. return SCRIPT_CMD_SUCCESS;
  9819. }
  9820. /**
  9821. * Change character's sex and unequip all item and request for a changesex to char-serv
  9822. * changecharsex({<char_id>});
  9823. */
  9824. BUILDIN_FUNC(changecharsex)
  9825. {
  9826. #if PACKETVER >= 20141016
  9827. int i;
  9828. TBL_PC *sd = NULL;
  9829. if (!script_charid2sd(2,sd))
  9830. return SCRIPT_CMD_FAILURE;
  9831. pc_resetskill(sd,4);
  9832. // to avoid any problem with equipment and invalid sex, equipment is unequiped.
  9833. for (i = 0; i < EQI_MAX; i++) {
  9834. if (sd->equip_index[i] >= 0)
  9835. pc_unequipitem(sd, sd->equip_index[i], 3);
  9836. }
  9837. chrif_changesex(sd, false);
  9838. return SCRIPT_CMD_SUCCESS;
  9839. #else
  9840. return SCRIPT_CMD_FAILURE;
  9841. #endif
  9842. }
  9843. /*==========================================
  9844. * Works like 'announce' but outputs in the common chat window
  9845. *------------------------------------------*/
  9846. BUILDIN_FUNC(globalmes)
  9847. {
  9848. struct block_list *bl = map_id2bl(st->oid);
  9849. struct npc_data *nd = (struct npc_data *)bl;
  9850. const char *name=NULL,*mes;
  9851. mes=script_getstr(st,2);
  9852. if(mes==NULL)
  9853. return SCRIPT_CMD_SUCCESS;
  9854. if(script_hasdata(st,3)){ // npc name to display
  9855. name=script_getstr(st,3);
  9856. } else {
  9857. name=nd->name; //use current npc name
  9858. }
  9859. npc_globalmessage(name,mes); // broadcast to all players connected
  9860. return SCRIPT_CMD_SUCCESS;
  9861. }
  9862. /////////////////////////////////////////////////////////////////////
  9863. // NPC waiting room (chat room)
  9864. //
  9865. /// Creates a waiting room (chat room) for this npc.
  9866. ///
  9867. /// waitingroom "<title>",<limit>{,"<event>"{,<trigger>{,<zeny>{,<minlvl>{,<maxlvl>}}}}};
  9868. BUILDIN_FUNC(waitingroom)
  9869. {
  9870. struct npc_data* nd;
  9871. const char* title = script_getstr(st, 2);
  9872. int limit = script_getnum(st, 3);
  9873. const char* ev = script_hasdata(st,4) ? script_getstr(st,4) : "";
  9874. int trigger = script_hasdata(st,5) ? script_getnum(st,5) : limit;
  9875. int zeny = script_hasdata(st,6) ? script_getnum(st,6) : 0;
  9876. int minLvl = script_hasdata(st,7) ? script_getnum(st,7) : 1;
  9877. int maxLvl = script_hasdata(st,8) ? script_getnum(st,8) : MAX_LEVEL;
  9878. nd = (struct npc_data *)map_id2bl(st->oid);
  9879. if( nd != NULL )
  9880. chat_createnpcchat(nd, title, limit, 1, trigger, ev, zeny, minLvl, maxLvl);
  9881. return SCRIPT_CMD_SUCCESS;
  9882. }
  9883. /// Removes the waiting room of the current or target npc.
  9884. ///
  9885. /// delwaitingroom "<npc_name>";
  9886. /// delwaitingroom;
  9887. BUILDIN_FUNC(delwaitingroom)
  9888. {
  9889. struct npc_data* nd;
  9890. if( script_hasdata(st,2) )
  9891. nd = npc_name2id(script_getstr(st, 2));
  9892. else
  9893. nd = (struct npc_data *)map_id2bl(st->oid);
  9894. if( nd != NULL )
  9895. chat_deletenpcchat(nd);
  9896. return SCRIPT_CMD_SUCCESS;
  9897. }
  9898. /// Kick the specified player from the waiting room of the target npc.
  9899. ///
  9900. /// waitingroomkick "<npc_name>", <kickusername>;
  9901. BUILDIN_FUNC(waitingroomkick)
  9902. {
  9903. struct npc_data* nd;
  9904. struct chat_data* cd;
  9905. const char* kickusername;
  9906. nd = npc_name2id(script_getstr(st,2));
  9907. kickusername = script_getstr(st,3);
  9908. if( nd != NULL && (cd=(struct chat_data *)map_id2bl(nd->chat_id)) != NULL )
  9909. chat_npckickchat(cd, kickusername);
  9910. return SCRIPT_CMD_SUCCESS;
  9911. }
  9912. /// Get Users in waiting room and stores gids in .@waitingroom_users[]
  9913. /// Num users stored in .@waitingroom_usercount
  9914. ///
  9915. /// getwaitingroomusers "<npc_name>";
  9916. BUILDIN_FUNC(getwaitingroomusers)
  9917. {
  9918. struct npc_data* nd;
  9919. struct chat_data* cd;
  9920. int i, j=0;
  9921. if( script_hasdata(st,2) )
  9922. nd = npc_name2id(script_getstr(st, 2));
  9923. else
  9924. nd = (struct npc_data *)map_id2bl(st->oid);
  9925. if( nd != NULL && (cd=(struct chat_data *)map_id2bl(nd->chat_id)) != NULL ) {
  9926. for(i = 0; i < cd->users; ++i) {
  9927. setd_sub(st, NULL, ".@waitingroom_users", j, (void *)__64BPRTSIZE(cd->usersd[i]->status.account_id), NULL);
  9928. j++;
  9929. }
  9930. setd_sub(st, NULL, ".@waitingroom_usercount", 0, (void *)__64BPRTSIZE(j), NULL);
  9931. }
  9932. return SCRIPT_CMD_SUCCESS;
  9933. }
  9934. /// Kicks all the players from the waiting room of the current or target npc.
  9935. ///
  9936. /// kickwaitingroomall "<npc_name>";
  9937. /// kickwaitingroomall;
  9938. BUILDIN_FUNC(waitingroomkickall)
  9939. {
  9940. struct npc_data* nd;
  9941. struct chat_data* cd;
  9942. if( script_hasdata(st,2) )
  9943. nd = npc_name2id(script_getstr(st,2));
  9944. else
  9945. nd = (struct npc_data *)map_id2bl(st->oid);
  9946. if( nd != NULL && (cd=(struct chat_data *)map_id2bl(nd->chat_id)) != NULL )
  9947. chat_npckickall(cd);
  9948. return SCRIPT_CMD_SUCCESS;
  9949. }
  9950. /// Enables the waiting room event of the current or target npc.
  9951. ///
  9952. /// enablewaitingroomevent "<npc_name>";
  9953. /// enablewaitingroomevent;
  9954. BUILDIN_FUNC(enablewaitingroomevent)
  9955. {
  9956. struct npc_data* nd;
  9957. struct chat_data* cd;
  9958. if( script_hasdata(st,2) )
  9959. nd = npc_name2id(script_getstr(st, 2));
  9960. else
  9961. nd = (struct npc_data *)map_id2bl(st->oid);
  9962. if( nd != NULL && (cd=(struct chat_data *)map_id2bl(nd->chat_id)) != NULL )
  9963. chat_enableevent(cd);
  9964. return SCRIPT_CMD_SUCCESS;
  9965. }
  9966. /// Disables the waiting room event of the current or target npc.
  9967. ///
  9968. /// disablewaitingroomevent "<npc_name>";
  9969. /// disablewaitingroomevent;
  9970. BUILDIN_FUNC(disablewaitingroomevent)
  9971. {
  9972. struct npc_data *nd;
  9973. struct chat_data *cd;
  9974. if( script_hasdata(st,2) )
  9975. nd = npc_name2id(script_getstr(st, 2));
  9976. else
  9977. nd = (struct npc_data *)map_id2bl(st->oid);
  9978. if( nd != NULL && (cd=(struct chat_data *)map_id2bl(nd->chat_id)) != NULL )
  9979. chat_disableevent(cd);
  9980. return SCRIPT_CMD_SUCCESS;
  9981. }
  9982. /// Returns info on the waiting room of the current or target npc.
  9983. /// Returns -1 if the type unknown
  9984. /// <type>=0 : current number of users
  9985. /// <type>=1 : maximum number of users allowed
  9986. /// <type>=2 : the number of users that trigger the event
  9987. /// <type>=3 : if the trigger is disabled
  9988. /// <type>=4 : the title of the waiting room
  9989. /// <type>=5 : the password of the waiting room
  9990. /// <type>=16 : the name of the waiting room event
  9991. /// <type>=32 : if the waiting room is full
  9992. /// <type>=33 : if there are enough users to trigger the event
  9993. ///
  9994. /// getwaitingroomstate(<type>,"<npc_name>") -> <info>
  9995. /// getwaitingroomstate(<type>) -> <info>
  9996. BUILDIN_FUNC(getwaitingroomstate)
  9997. {
  9998. struct npc_data *nd;
  9999. struct chat_data *cd;
  10000. int type;
  10001. type = script_getnum(st,2);
  10002. if( script_hasdata(st,3) )
  10003. nd = npc_name2id(script_getstr(st, 3));
  10004. else
  10005. nd = (struct npc_data *)map_id2bl(st->oid);
  10006. if( nd == NULL || (cd=(struct chat_data *)map_id2bl(nd->chat_id)) == NULL )
  10007. {
  10008. script_pushint(st, -1);
  10009. return SCRIPT_CMD_SUCCESS;
  10010. }
  10011. switch(type)
  10012. {
  10013. case 0: script_pushint(st, cd->users); break;
  10014. case 1: script_pushint(st, cd->limit); break;
  10015. case 2: script_pushint(st, cd->trigger&0x7f); break;
  10016. case 3: script_pushint(st, ((cd->trigger&0x80)!=0)); break;
  10017. case 4: script_pushstrcopy(st, cd->title); break;
  10018. case 5: script_pushstrcopy(st, cd->pass); break;
  10019. case 16: script_pushstrcopy(st, cd->npc_event);break;
  10020. case 32: script_pushint(st, (cd->users >= cd->limit)); break;
  10021. case 33: script_pushint(st, (cd->users >= cd->trigger)); break;
  10022. default: script_pushint(st, -1); break;
  10023. }
  10024. return SCRIPT_CMD_SUCCESS;
  10025. }
  10026. /// Warps the trigger or target amount of players to the target map and position.
  10027. /// Players are automatically removed from the waiting room.
  10028. /// Those waiting the longest will get warped first.
  10029. /// The target map can be "Random" for a random position in the current map,
  10030. /// and "SavePoint" for the savepoint map+position.
  10031. /// The map flag noteleport of the current map is only considered when teleporting to the savepoint.
  10032. ///
  10033. /// The id's of the teleported players are put into the array $@warpwaitingpc[]
  10034. /// The total number of teleported players is put into $@warpwaitingpcnum
  10035. ///
  10036. /// warpwaitingpc "<map name>",<x>,<y>,<number of players>;
  10037. /// warpwaitingpc "<map name>",<x>,<y>;
  10038. BUILDIN_FUNC(warpwaitingpc)
  10039. {
  10040. int x;
  10041. int y;
  10042. int i;
  10043. int n;
  10044. const char* map_name;
  10045. struct npc_data* nd;
  10046. struct chat_data* cd;
  10047. nd = (struct npc_data *)map_id2bl(st->oid);
  10048. if( nd == NULL || (cd=(struct chat_data *)map_id2bl(nd->chat_id)) == NULL )
  10049. return SCRIPT_CMD_SUCCESS;
  10050. map_name = script_getstr(st,2);
  10051. x = script_getnum(st,3);
  10052. y = script_getnum(st,4);
  10053. n = cd->trigger&0x7f;
  10054. if( script_hasdata(st,5) )
  10055. n = script_getnum(st,5);
  10056. for( i = 0; i < n && cd->users > 0; i++ )
  10057. {
  10058. TBL_PC* sd = cd->usersd[0];
  10059. if( strcmp(map_name,"SavePoint") == 0 && map[sd->bl.m].flag.noteleport )
  10060. {// can't teleport on this map
  10061. break;
  10062. }
  10063. if( cd->zeny )
  10064. {// fee set
  10065. if( (uint32)sd->status.zeny < cd->zeny )
  10066. {// no zeny to cover set fee
  10067. break;
  10068. }
  10069. pc_payzeny(sd, cd->zeny, LOG_TYPE_NPC, NULL);
  10070. }
  10071. mapreg_setreg(reference_uid(add_str("$@warpwaitingpc"), i), sd->bl.id);
  10072. if( strcmp(map_name,"Random") == 0 )
  10073. pc_randomwarp(sd,CLR_TELEPORT);
  10074. else if( strcmp(map_name,"SavePoint") == 0 )
  10075. pc_setpos(sd, sd->status.save_point.map, sd->status.save_point.x, sd->status.save_point.y, CLR_TELEPORT);
  10076. else
  10077. pc_setpos(sd, mapindex_name2id(map_name), x, y, CLR_OUTSIGHT);
  10078. }
  10079. mapreg_setreg(add_str("$@warpwaitingpcnum"), i);
  10080. return SCRIPT_CMD_SUCCESS;
  10081. }
  10082. /////////////////////////////////////////////////////////////////////
  10083. // ...
  10084. //
  10085. /// Detaches a character from a script.
  10086. ///
  10087. /// @param st Script state to detach the character from.
  10088. static void script_detach_rid(struct script_state* st)
  10089. {
  10090. if(st->rid)
  10091. {
  10092. script_detach_state(st, false);
  10093. st->rid = 0;
  10094. }
  10095. }
  10096. /*=========================================================================
  10097. * Attaches a set of RIDs to the current script. [digitalhamster]
  10098. * addrid(<type>{,<flag>{,<parameters>}});
  10099. * <type>:
  10100. * 0 : All players in the server.
  10101. * 1 : All players in the map of the invoking player, or the invoking NPC if no player is attached.
  10102. * 2 : Party members of a specified party ID.
  10103. * [ Parameters: <party id> ]
  10104. * 3 : Guild members of a specified guild ID.
  10105. * [ Parameters: <guild id> ]
  10106. * 4 : All players in a specified area of the map of the invoking player (or NPC).
  10107. * [ Parameters: <x0>,<y0>,<x1>,<y1> ]
  10108. * 5 : All players in the map.
  10109. * [ Parameters: "<map name>" ]
  10110. * Account ID: The specified account ID.
  10111. * <flag>:
  10112. * 0 : Players are always attached. (default)
  10113. * 1 : Players currently running another script will not be attached.
  10114. *-------------------------------------------------------------------------*/
  10115. static int buildin_addrid_sub(struct block_list *bl,va_list ap)
  10116. {
  10117. int forceflag;
  10118. struct map_session_data *sd = (TBL_PC *)bl;
  10119. struct script_state* st;
  10120. st = va_arg(ap,struct script_state*);
  10121. forceflag = va_arg(ap,int);
  10122. if(!forceflag || !sd->st)
  10123. if(sd->status.account_id != st->rid)
  10124. run_script(st->script,st->pos,sd->status.account_id,st->oid);
  10125. return 0;
  10126. }
  10127. BUILDIN_FUNC(addrid)
  10128. {
  10129. struct s_mapiterator* iter;
  10130. struct block_list *bl;
  10131. TBL_PC *sd;
  10132. if(st->rid < 1) {
  10133. st->state = END;
  10134. bl = map_id2bl(st->oid);
  10135. } else
  10136. bl = map_id2bl(st->rid); //if run without rid it'd error,also oid if npc, else rid for map
  10137. iter = mapit_getallusers();
  10138. switch(script_getnum(st,2)) {
  10139. case 0:
  10140. for( sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); sd = (TBL_PC*)mapit_next(iter)) {
  10141. if(!script_getnum(st,3) || !sd->st)
  10142. if(sd->status.account_id != st->rid) //attached player already runs.
  10143. run_script(st->script,st->pos,sd->status.account_id,st->oid);
  10144. }
  10145. break;
  10146. case 1:
  10147. for( sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); sd = (TBL_PC*)mapit_next(iter)) {
  10148. if(!script_getnum(st,3) || !sd->st)
  10149. if((sd->bl.m == bl->m) && (sd->status.account_id != st->rid))
  10150. run_script(st->script,st->pos,sd->status.account_id,st->oid);
  10151. }
  10152. break;
  10153. case 2:
  10154. if(script_getnum(st,4) == 0) {
  10155. script_pushint(st,0);
  10156. return SCRIPT_CMD_SUCCESS;
  10157. }
  10158. for( sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); sd = (TBL_PC*)mapit_next(iter)) {
  10159. if(!script_getnum(st,3) || !sd->st)
  10160. if((sd->status.account_id != st->rid) && (sd->status.party_id == script_getnum(st,4))) //attached player already runs.
  10161. run_script(st->script,st->pos,sd->status.account_id,st->oid);
  10162. }
  10163. break;
  10164. case 3:
  10165. if(script_getnum(st,4) == 0) {
  10166. script_pushint(st,0);
  10167. return SCRIPT_CMD_SUCCESS;
  10168. }
  10169. for( sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); sd = (TBL_PC*)mapit_next(iter)) {
  10170. if(!script_getnum(st,3) || !sd->st)
  10171. if((sd->status.account_id != st->rid) && (sd->status.guild_id == script_getnum(st,4))) //attached player already runs.
  10172. run_script(st->script,st->pos,sd->status.account_id,st->oid);
  10173. }
  10174. break;
  10175. case 4:
  10176. map_foreachinarea(buildin_addrid_sub,
  10177. bl->m,script_getnum(st,4),script_getnum(st,5),script_getnum(st,6),script_getnum(st,7),BL_PC,
  10178. st,script_getnum(st,3));//4-x0 , 5-y0 , 6-x1, 7-y1
  10179. break;
  10180. case 5:
  10181. if (script_getstr(st, 4) == NULL) {
  10182. script_pushint(st, 0);
  10183. return SCRIPT_CMD_FAILURE;
  10184. }
  10185. if (map_mapname2mapid(script_getstr(st, 4)) < 0) {
  10186. script_pushint(st, 0);
  10187. return SCRIPT_CMD_FAILURE;
  10188. }
  10189. map_foreachinmap(buildin_addrid_sub, map_mapname2mapid(script_getstr(st, 4)), BL_PC, st, script_getnum(st, 3));
  10190. break;
  10191. default:
  10192. if((map_id2sd(script_getnum(st,2))) == NULL) { // Player not found.
  10193. script_pushint(st,0);
  10194. return SCRIPT_CMD_SUCCESS;
  10195. }
  10196. if(!script_getnum(st,3) || !map_id2sd(script_getnum(st,2))->st) {
  10197. run_script(st->script,st->pos,script_getnum(st,2),st->oid);
  10198. script_pushint(st,1);
  10199. }
  10200. return SCRIPT_CMD_SUCCESS;
  10201. }
  10202. mapit_free(iter);
  10203. script_pushint(st,1);
  10204. return SCRIPT_CMD_SUCCESS;
  10205. }
  10206. /*==========================================
  10207. * Attach sd char id to script and detach current one if any
  10208. *------------------------------------------*/
  10209. BUILDIN_FUNC(attachrid)
  10210. {
  10211. int rid = script_getnum(st,2);
  10212. if (map_id2sd(rid) != NULL) {
  10213. script_detach_rid(st);
  10214. st->rid = rid;
  10215. script_attach_state(st);
  10216. script_pushint(st,1);
  10217. } else
  10218. script_pushint(st,0);
  10219. return SCRIPT_CMD_SUCCESS;
  10220. }
  10221. /*==========================================
  10222. * Detach script to rid
  10223. *------------------------------------------*/
  10224. BUILDIN_FUNC(detachrid)
  10225. {
  10226. script_detach_rid(st);
  10227. return SCRIPT_CMD_SUCCESS;
  10228. }
  10229. /*==========================================
  10230. * Chk if account connected, (and charid from account if specified)
  10231. *------------------------------------------*/
  10232. BUILDIN_FUNC(isloggedin)
  10233. {
  10234. TBL_PC* sd = map_id2sd(script_getnum(st,2));
  10235. if (script_hasdata(st,3) && sd &&
  10236. sd->status.char_id != script_getnum(st,3))
  10237. sd = NULL;
  10238. push_val(st->stack,C_INT,sd!=NULL);
  10239. return SCRIPT_CMD_SUCCESS;
  10240. }
  10241. /*==========================================
  10242. *
  10243. *------------------------------------------*/
  10244. BUILDIN_FUNC(setmapflagnosave)
  10245. {
  10246. int16 m,x,y;
  10247. unsigned short mapindex;
  10248. const char *str,*str2;
  10249. str=script_getstr(st,2);
  10250. str2=script_getstr(st,3);
  10251. x=script_getnum(st,4);
  10252. y=script_getnum(st,5);
  10253. m = map_mapname2mapid(str);
  10254. mapindex = mapindex_name2id(str2);
  10255. if(m >= 0 && mapindex) {
  10256. map[m].flag.nosave=1;
  10257. map[m].save.map=mapindex;
  10258. map[m].save.x=x;
  10259. map[m].save.y=y;
  10260. }
  10261. return SCRIPT_CMD_SUCCESS;
  10262. }
  10263. BUILDIN_FUNC(getmapflag)
  10264. {
  10265. int16 m,i;
  10266. const char *str;
  10267. str=script_getstr(st,2);
  10268. i=script_getnum(st,3);
  10269. m = map_mapname2mapid(str);
  10270. if(m >= 0) {
  10271. switch(i) {
  10272. case MF_NOMEMO: script_pushint(st,map[m].flag.nomemo); break;
  10273. case MF_NOTELEPORT: script_pushint(st,map[m].flag.noteleport); break;
  10274. case MF_NOSAVE: script_pushint(st,map[m].flag.nosave); break;
  10275. case MF_NOBRANCH: script_pushint(st,map[m].flag.nobranch); break;
  10276. case MF_NOPENALTY: script_pushint(st,map[m].flag.noexppenalty); break;
  10277. case MF_NOZENYPENALTY: script_pushint(st,map[m].flag.nozenypenalty); break;
  10278. case MF_PVP: script_pushint(st,map[m].flag.pvp); break;
  10279. case MF_PVP_NOPARTY: script_pushint(st,map[m].flag.pvp_noparty); break;
  10280. case MF_PVP_NOGUILD: script_pushint(st,map[m].flag.pvp_noguild); break;
  10281. case MF_GVG: script_pushint(st,map[m].flag.gvg); break;
  10282. case MF_GVG_NOPARTY: script_pushint(st,map[m].flag.gvg_noparty); break;
  10283. case MF_NOTRADE: script_pushint(st,map[m].flag.notrade); break;
  10284. case MF_NOSKILL: script_pushint(st,map[m].flag.noskill); break;
  10285. case MF_NOWARP: script_pushint(st,map[m].flag.nowarp); break;
  10286. case MF_PARTYLOCK: script_pushint(st,map[m].flag.partylock); break;
  10287. case MF_NOICEWALL: script_pushint(st,map[m].flag.noicewall); break;
  10288. case MF_SNOW: script_pushint(st,map[m].flag.snow); break;
  10289. case MF_FOG: script_pushint(st,map[m].flag.fog); break;
  10290. case MF_SAKURA: script_pushint(st,map[m].flag.sakura); break;
  10291. case MF_LEAVES: script_pushint(st,map[m].flag.leaves); break;
  10292. case MF_NOGO: script_pushint(st,map[m].flag.nogo); break;
  10293. case MF_CLOUDS: script_pushint(st,map[m].flag.clouds); break;
  10294. case MF_CLOUDS2: script_pushint(st,map[m].flag.clouds2); break;
  10295. case MF_FIREWORKS: script_pushint(st,map[m].flag.fireworks); break;
  10296. case MF_GVG_CASTLE: script_pushint(st,map[m].flag.gvg_castle); break;
  10297. case MF_GVG_DUNGEON: script_pushint(st,map[m].flag.gvg_dungeon); break;
  10298. case MF_NIGHTENABLED: script_pushint(st,map[m].flag.nightenabled); break;
  10299. case MF_NOBASEEXP: script_pushint(st,map[m].flag.nobaseexp); break;
  10300. case MF_NOJOBEXP: script_pushint(st,map[m].flag.nojobexp); break;
  10301. case MF_NOMOBLOOT: script_pushint(st,map[m].flag.nomobloot); break;
  10302. case MF_NOMVPLOOT: script_pushint(st,map[m].flag.nomvploot); break;
  10303. case MF_NORETURN: script_pushint(st,map[m].flag.noreturn); break;
  10304. case MF_NOWARPTO: script_pushint(st,map[m].flag.nowarpto); break;
  10305. case MF_NIGHTMAREDROP: script_pushint(st,map[m].flag.pvp_nightmaredrop); break;
  10306. case MF_RESTRICTED: script_pushint(st,map[m].flag.restricted); break;
  10307. case MF_NOCOMMAND: script_pushint(st,map[m].nocommand); break;
  10308. case MF_NODROP: script_pushint(st,map[m].flag.nodrop); break;
  10309. case MF_JEXP: script_pushint(st,map[m].adjust.jexp); break;
  10310. case MF_BEXP: script_pushint(st,map[m].adjust.bexp); break;
  10311. case MF_NOVENDING: script_pushint(st,map[m].flag.novending); break;
  10312. case MF_LOADEVENT: script_pushint(st,map[m].flag.loadevent); break;
  10313. case MF_NOCHAT: script_pushint(st,map[m].flag.nochat); break;
  10314. case MF_NOEXPPENALTY: script_pushint(st,map[m].flag.noexppenalty ); break;
  10315. case MF_GUILDLOCK: script_pushint(st,map[m].flag.guildlock); break;
  10316. case MF_TOWN: script_pushint(st,map[m].flag.town); break;
  10317. case MF_AUTOTRADE: script_pushint(st,map[m].flag.autotrade); break;
  10318. case MF_ALLOWKS: script_pushint(st,map[m].flag.allowks); break;
  10319. case MF_MONSTER_NOTELEPORT: script_pushint(st,map[m].flag.monster_noteleport); break;
  10320. case MF_PVP_NOCALCRANK: script_pushint(st,map[m].flag.pvp_nocalcrank); break;
  10321. case MF_BATTLEGROUND: script_pushint(st,map[m].flag.battleground); break;
  10322. case MF_RESET: script_pushint(st,map[m].flag.reset); break;
  10323. case MF_CHANNELAUTOJOIN: script_pushint(st,map[m].flag.chmautojoin); break;
  10324. case MF_NOUSECART: script_pushint(st,map[m].flag.nousecart); break;
  10325. case MF_NOITEMCONSUMPTION: script_pushint(st,map[m].flag.noitemconsumption); break;
  10326. case MF_SUMSTARTMIRACLE: script_pushint(st,map[m].flag.nosumstarmiracle); break;
  10327. case MF_NOMINEEFFECT: script_pushint(st,map[m].flag.nomineeffect); break;
  10328. case MF_NOLOCKON: script_pushint(st,map[m].flag.nolockon); break;
  10329. case MF_NOTOMB: script_pushint(st,map[m].flag.notomb); break;
  10330. #ifdef ADJUST_SKILL_DAMAGE
  10331. case MF_SKILL_DAMAGE:
  10332. {
  10333. int ret_val=0, type=0;
  10334. FETCH(4,type);
  10335. switch (type) {
  10336. case 1: ret_val = map[m].adjust.damage.pc; break;
  10337. case 2: ret_val = map[m].adjust.damage.mob; break;
  10338. case 3: ret_val = map[m].adjust.damage.boss; break;
  10339. case 4: ret_val = map[m].adjust.damage.other; break;
  10340. case 5: ret_val = map[m].adjust.damage.caster; break;
  10341. default: ret_val = map[m].flag.skill_damage; break;
  10342. }
  10343. script_pushint(st,ret_val); break;
  10344. } break;
  10345. #endif
  10346. }
  10347. }
  10348. return SCRIPT_CMD_SUCCESS;
  10349. }
  10350. /* pvp timer handling */
  10351. static int script_mapflag_pvp_sub(struct block_list *bl,va_list ap) {
  10352. TBL_PC* sd = (TBL_PC*)bl;
  10353. if (sd->pvp_timer == INVALID_TIMER) {
  10354. sd->pvp_timer = add_timer(gettick() + 200, pc_calc_pvprank_timer, sd->bl.id, 0);
  10355. sd->pvp_rank = 0;
  10356. sd->pvp_lastusers = 0;
  10357. sd->pvp_point = 5;
  10358. sd->pvp_won = 0;
  10359. sd->pvp_lost = 0;
  10360. }
  10361. clif_map_property(sd, MAPPROPERTY_FREEPVPZONE);
  10362. clif_maptypeproperty2(&sd->bl,SELF);
  10363. return 0;
  10364. }
  10365. BUILDIN_FUNC(setmapflag)
  10366. {
  10367. int16 m,i;
  10368. const char *str;
  10369. int val=0;
  10370. str=script_getstr(st,2);
  10371. i=script_getnum(st,3);
  10372. FETCH(4,val);
  10373. m = map_mapname2mapid(str);
  10374. if(m >= 0) {
  10375. switch(i) {
  10376. case MF_NOMEMO: map[m].flag.nomemo = 1; break;
  10377. case MF_NOTELEPORT: map[m].flag.noteleport = 1; break;
  10378. case MF_NOSAVE: map[m].flag.nosave = 1; break;
  10379. case MF_NOBRANCH: map[m].flag.nobranch = 1; break;
  10380. case MF_NOPENALTY: map[m].flag.noexppenalty = 1; map[m].flag.nozenypenalty = 1; break;
  10381. case MF_NOZENYPENALTY: map[m].flag.nozenypenalty = 1; break;
  10382. case MF_PVP:
  10383. map[m].flag.pvp = 1;
  10384. if( !battle_config.pk_mode ) {
  10385. map_foreachinmap(script_mapflag_pvp_sub,m,BL_PC);
  10386. }
  10387. break;
  10388. case MF_PVP_NOPARTY: map[m].flag.pvp_noparty = 1; break;
  10389. case MF_PVP_NOGUILD: map[m].flag.pvp_noguild = 1; break;
  10390. case MF_GVG: {
  10391. struct block_list bl;
  10392. map[m].flag.gvg = 1;
  10393. clif_map_property_mapall(m, MAPPROPERTY_AGITZONE);
  10394. bl.type = BL_NUL;
  10395. bl.m = m;
  10396. clif_maptypeproperty2(&bl,ALL_SAMEMAP);
  10397. }
  10398. break;
  10399. case MF_GVG_NOPARTY: map[m].flag.gvg_noparty = 1; break;
  10400. case MF_NOTRADE: map[m].flag.notrade = 1; break;
  10401. case MF_NOSKILL: map[m].flag.noskill = 1; break;
  10402. case MF_NOWARP: map[m].flag.nowarp = 1; break;
  10403. case MF_PARTYLOCK: map[m].flag.partylock = 1; break;
  10404. case MF_NOICEWALL: map[m].flag.noicewall = 1; break;
  10405. case MF_SNOW: map[m].flag.snow = 1; break;
  10406. case MF_FOG: map[m].flag.fog = 1; break;
  10407. case MF_SAKURA: map[m].flag.sakura = 1; break;
  10408. case MF_LEAVES: map[m].flag.leaves = 1; break;
  10409. case MF_NOGO: map[m].flag.nogo = 1; break;
  10410. case MF_CLOUDS: map[m].flag.clouds = 1; break;
  10411. case MF_CLOUDS2: map[m].flag.clouds2 = 1; break;
  10412. case MF_FIREWORKS: map[m].flag.fireworks = 1; break;
  10413. case MF_GVG_CASTLE: map[m].flag.gvg_castle = 1; break;
  10414. case MF_GVG_DUNGEON: map[m].flag.gvg_dungeon = 1; break;
  10415. case MF_NIGHTENABLED: map[m].flag.nightenabled = 1; break;
  10416. case MF_NOBASEEXP: map[m].flag.nobaseexp = 1; break;
  10417. case MF_NOJOBEXP: map[m].flag.nojobexp = 1; break;
  10418. case MF_NOMOBLOOT: map[m].flag.nomobloot = 1; break;
  10419. case MF_NOMVPLOOT: map[m].flag.nomvploot = 1; break;
  10420. case MF_NORETURN: map[m].flag.noreturn = 1; break;
  10421. case MF_NOWARPTO: map[m].flag.nowarpto = 1; break;
  10422. case MF_NIGHTMAREDROP: map[m].flag.pvp_nightmaredrop = 1; break;
  10423. case MF_RESTRICTED:
  10424. map[m].zone |= 1<<(val+1);
  10425. map[m].flag.restricted=1;
  10426. break;
  10427. case MF_NOCOMMAND: map[m].nocommand = (val <= 0) ? 100 : val; break;
  10428. case MF_NODROP: map[m].flag.nodrop = 1; break;
  10429. case MF_JEXP: map[m].adjust.jexp = (val <= 0) ? 100 : val; break;
  10430. case MF_BEXP: map[m].adjust.bexp = (val <= 0) ? 100 : val; break;
  10431. case MF_NOVENDING: map[m].flag.novending = 1; break;
  10432. case MF_LOADEVENT: map[m].flag.loadevent = 1; break;
  10433. case MF_NOCHAT: map[m].flag.nochat = 1; break;
  10434. case MF_NOEXPPENALTY: map[m].flag.noexppenalty = 1; break;
  10435. case MF_GUILDLOCK: map[m].flag.guildlock = 1; break;
  10436. case MF_TOWN: map[m].flag.town = 1; break;
  10437. case MF_AUTOTRADE: map[m].flag.autotrade = 1; break;
  10438. case MF_ALLOWKS: map[m].flag.allowks = 1; break;
  10439. case MF_MONSTER_NOTELEPORT: map[m].flag.monster_noteleport = 1; break;
  10440. case MF_PVP_NOCALCRANK: map[m].flag.pvp_nocalcrank = 1; break;
  10441. case MF_BATTLEGROUND: map[m].flag.battleground = (val <= 0 || val > 2) ? 1 : val; break;
  10442. case MF_RESET: map[m].flag.reset = 1; break;
  10443. case MF_CHANNELAUTOJOIN: map[m].flag.chmautojoin = 1 ; break;
  10444. case MF_NOUSECART: map[m].flag.nousecart = 1 ; break;
  10445. case MF_NOITEMCONSUMPTION: map[m].flag.noitemconsumption = 1 ; break;
  10446. case MF_SUMSTARTMIRACLE: map[m].flag.nosumstarmiracle = 1 ; break;
  10447. case MF_NOMINEEFFECT: map[m].flag.nomineeffect = 1 ; break;
  10448. case MF_NOLOCKON: map[m].flag.nolockon = 1 ; break;
  10449. case MF_NOTOMB: map[m].flag.notomb = 1; break;
  10450. #ifdef ADJUST_SKILL_DAMAGE
  10451. case MF_SKILL_DAMAGE:
  10452. {
  10453. int type=0;
  10454. FETCH(5,type);
  10455. switch (type) {
  10456. case 1: map[m].adjust.damage.pc = val; break;
  10457. case 2: map[m].adjust.damage.mob = val; break;
  10458. case 3: map[m].adjust.damage.boss = val; break;
  10459. case 4: map[m].adjust.damage.other = val; break;
  10460. case 5: map[m].adjust.damage.caster = val; break;
  10461. }
  10462. map[m].flag.skill_damage = 1;
  10463. } break;
  10464. #endif
  10465. }
  10466. }
  10467. return SCRIPT_CMD_SUCCESS;
  10468. }
  10469. BUILDIN_FUNC(removemapflag)
  10470. {
  10471. int16 m,i;
  10472. const char *str;
  10473. int val=0;
  10474. str=script_getstr(st,2);
  10475. i=script_getnum(st,3);
  10476. FETCH(4,val);
  10477. m = map_mapname2mapid(str);
  10478. if(m >= 0) {
  10479. switch(i) {
  10480. case MF_NOMEMO: map[m].flag.nomemo = 0; break;
  10481. case MF_NOTELEPORT: map[m].flag.noteleport = 0; break;
  10482. case MF_NOSAVE: map[m].flag.nosave = 0; break;
  10483. case MF_NOBRANCH: map[m].flag.nobranch = 0; break;
  10484. case MF_NOPENALTY: map[m].flag.noexppenalty = 0; map[m].flag.nozenypenalty = 0; break;
  10485. case MF_NOZENYPENALTY: map[m].flag.nozenypenalty = 0; break;
  10486. case MF_PVP: {
  10487. struct block_list bl;
  10488. bl.type = BL_NUL;
  10489. bl.m = m;
  10490. map[m].flag.pvp = 0;
  10491. clif_map_property_mapall(m, MAPPROPERTY_NOTHING);
  10492. clif_maptypeproperty2(&bl,ALL_SAMEMAP);
  10493. }
  10494. break;
  10495. case MF_PVP_NOPARTY: map[m].flag.pvp_noparty = 0; break;
  10496. case MF_PVP_NOGUILD: map[m].flag.pvp_noguild = 0; break;
  10497. case MF_GVG: {
  10498. struct block_list bl;
  10499. bl.type = BL_NUL;
  10500. bl.m = m;
  10501. map[m].flag.gvg = 0;
  10502. clif_map_property_mapall(m, MAPPROPERTY_NOTHING);
  10503. clif_maptypeproperty2(&bl,ALL_SAMEMAP);
  10504. }
  10505. break;
  10506. case MF_GVG_NOPARTY: map[m].flag.gvg_noparty = 0; break;
  10507. case MF_NOTRADE: map[m].flag.notrade = 0; break;
  10508. case MF_NOSKILL: map[m].flag.noskill = 0; break;
  10509. case MF_NOWARP: map[m].flag.nowarp = 0; break;
  10510. case MF_PARTYLOCK: map[m].flag.partylock = 0; break;
  10511. case MF_NOICEWALL: map[m].flag.noicewall = 0; break;
  10512. case MF_SNOW: map[m].flag.snow = 0; break;
  10513. case MF_FOG: map[m].flag.fog = 0; break;
  10514. case MF_SAKURA: map[m].flag.sakura = 0; break;
  10515. case MF_LEAVES: map[m].flag.leaves = 0; break;
  10516. case MF_NOGO: map[m].flag.nogo = 0; break;
  10517. case MF_CLOUDS: map[m].flag.clouds = 0; break;
  10518. case MF_CLOUDS2: map[m].flag.clouds2 = 0; break;
  10519. case MF_FIREWORKS: map[m].flag.fireworks = 0; break;
  10520. case MF_GVG_CASTLE: map[m].flag.gvg_castle = 0; break;
  10521. case MF_GVG_DUNGEON: map[m].flag.gvg_dungeon = 0; break;
  10522. case MF_NIGHTENABLED: map[m].flag.nightenabled = 0; break;
  10523. case MF_NOBASEEXP: map[m].flag.nobaseexp = 0; break;
  10524. case MF_NOJOBEXP: map[m].flag.nojobexp = 0; break;
  10525. case MF_NOMOBLOOT: map[m].flag.nomobloot = 0; break;
  10526. case MF_NOMVPLOOT: map[m].flag.nomvploot = 0; break;
  10527. case MF_NORETURN: map[m].flag.noreturn = 0; break;
  10528. case MF_NOWARPTO: map[m].flag.nowarpto = 0; break;
  10529. case MF_NIGHTMAREDROP: map[m].flag.pvp_nightmaredrop = 0; break;
  10530. case MF_RESTRICTED:
  10531. map[m].zone ^= 1<<(val+1);
  10532. if (map[m].zone == 0){
  10533. map[m].flag.restricted=0;
  10534. }
  10535. break;
  10536. case MF_NOCOMMAND: map[m].nocommand = 0; break;
  10537. case MF_NODROP: map[m].flag.nodrop = 0; break;
  10538. case MF_JEXP: map[m].adjust.jexp = 0; break;
  10539. case MF_BEXP: map[m].adjust.bexp = 0; break;
  10540. case MF_NOVENDING: map[m].flag.novending = 0; break;
  10541. case MF_LOADEVENT: map[m].flag.loadevent = 0; break;
  10542. case MF_NOCHAT: map[m].flag.nochat = 0; break;
  10543. case MF_NOEXPPENALTY: map[m].flag.noexppenalty = 0; break;
  10544. case MF_GUILDLOCK: map[m].flag.guildlock = 0; break;
  10545. case MF_TOWN: map[m].flag.town = 0; break;
  10546. case MF_AUTOTRADE: map[m].flag.autotrade = 0; break;
  10547. case MF_ALLOWKS: map[m].flag.allowks = 0; break;
  10548. case MF_MONSTER_NOTELEPORT: map[m].flag.monster_noteleport = 0; break;
  10549. case MF_PVP_NOCALCRANK: map[m].flag.pvp_nocalcrank = 0; break;
  10550. case MF_BATTLEGROUND: map[m].flag.battleground = 0; break;
  10551. case MF_RESET: map[m].flag.reset = 0; break;
  10552. case MF_CHANNELAUTOJOIN: map[m].flag.chmautojoin = 0 ; break;
  10553. case MF_NOUSECART: map[m].flag.nousecart = 0 ; break;
  10554. case MF_NOITEMCONSUMPTION: map[m].flag.noitemconsumption = 0 ; break;
  10555. case MF_SUMSTARTMIRACLE: map[m].flag.nosumstarmiracle = 0 ; break;
  10556. case MF_NOMINEEFFECT: map[m].flag.nomineeffect = 0 ; break;
  10557. case MF_NOLOCKON: map[m].flag.nolockon = 0 ; break;
  10558. case MF_NOTOMB: map[m].flag.notomb = 0; break;
  10559. #ifdef ADJUST_SKILL_DAMAGE
  10560. case MF_SKILL_DAMAGE:
  10561. {
  10562. map[m].flag.skill_damage = 0;
  10563. memset(&map[m].adjust.damage, 0, sizeof(map[m].adjust.damage));
  10564. if (map[m].skill_damage.count)
  10565. map_skill_damage_free(&map[m]);
  10566. } break;
  10567. #endif
  10568. }
  10569. }
  10570. return SCRIPT_CMD_SUCCESS;
  10571. }
  10572. BUILDIN_FUNC(pvpon)
  10573. {
  10574. int16 m;
  10575. const char *str;
  10576. TBL_PC* sd = NULL;
  10577. struct s_mapiterator* iter;
  10578. struct block_list bl;
  10579. str = script_getstr(st,2);
  10580. m = map_mapname2mapid(str);
  10581. if( m < 0 || map[m].flag.pvp )
  10582. return SCRIPT_CMD_SUCCESS; // nothing to do
  10583. map[m].flag.pvp = 1;
  10584. clif_map_property_mapall(m, MAPPROPERTY_FREEPVPZONE);
  10585. bl.type = BL_NUL;
  10586. bl.m = m;
  10587. clif_maptypeproperty2(&bl,ALL_SAMEMAP);
  10588. if(battle_config.pk_mode) // disable ranking functions if pk_mode is on [Valaris]
  10589. return SCRIPT_CMD_SUCCESS;
  10590. iter = mapit_getallusers();
  10591. for( sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); sd = (TBL_PC*)mapit_next(iter) )
  10592. {
  10593. if( sd->bl.m != m || sd->pvp_timer != INVALID_TIMER )
  10594. continue; // not applicable
  10595. sd->pvp_timer = add_timer(gettick()+200,pc_calc_pvprank_timer,sd->bl.id,0);
  10596. sd->pvp_rank = 0;
  10597. sd->pvp_lastusers = 0;
  10598. sd->pvp_point = 5;
  10599. sd->pvp_won = 0;
  10600. sd->pvp_lost = 0;
  10601. }
  10602. mapit_free(iter);
  10603. return SCRIPT_CMD_SUCCESS;
  10604. }
  10605. static int buildin_pvpoff_sub(struct block_list *bl,va_list ap)
  10606. {
  10607. TBL_PC* sd = (TBL_PC*)bl;
  10608. clif_pvpset(sd, 0, 0, 2);
  10609. if (sd->pvp_timer != INVALID_TIMER) {
  10610. delete_timer(sd->pvp_timer, pc_calc_pvprank_timer);
  10611. sd->pvp_timer = INVALID_TIMER;
  10612. }
  10613. return 0;
  10614. }
  10615. BUILDIN_FUNC(pvpoff)
  10616. {
  10617. int16 m;
  10618. const char *str;
  10619. struct block_list bl;
  10620. str=script_getstr(st,2);
  10621. m = map_mapname2mapid(str);
  10622. if(m < 0 || !map[m].flag.pvp)
  10623. return SCRIPT_CMD_SUCCESS; //fixed Lupus
  10624. map[m].flag.pvp = 0;
  10625. clif_map_property_mapall(m, MAPPROPERTY_NOTHING);
  10626. bl.type = BL_NUL;
  10627. bl.m = m;
  10628. clif_maptypeproperty2(&bl,ALL_SAMEMAP);
  10629. if(battle_config.pk_mode) // disable ranking options if pk_mode is on [Valaris]
  10630. return SCRIPT_CMD_SUCCESS;
  10631. map_foreachinmap(buildin_pvpoff_sub, m, BL_PC);
  10632. return SCRIPT_CMD_SUCCESS;
  10633. }
  10634. BUILDIN_FUNC(gvgon)
  10635. {
  10636. int16 m;
  10637. const char *str;
  10638. struct block_list bl;
  10639. str=script_getstr(st,2);
  10640. m = map_mapname2mapid(str);
  10641. if(m >= 0 && !map[m].flag.gvg) {
  10642. map[m].flag.gvg = 1;
  10643. clif_map_property_mapall(m, MAPPROPERTY_AGITZONE);
  10644. bl.type = BL_NUL;
  10645. bl.m = m;
  10646. clif_maptypeproperty2(&bl,ALL_SAMEMAP);
  10647. }
  10648. return SCRIPT_CMD_SUCCESS;
  10649. }
  10650. BUILDIN_FUNC(gvgoff)
  10651. {
  10652. int16 m;
  10653. const char *str;
  10654. str=script_getstr(st,2);
  10655. m = map_mapname2mapid(str);
  10656. if(m >= 0 && map[m].flag.gvg) {
  10657. struct block_list bl;
  10658. map[m].flag.gvg = 0;
  10659. clif_map_property_mapall(m, MAPPROPERTY_NOTHING);
  10660. bl.type = BL_NUL;
  10661. bl.m = m;
  10662. clif_maptypeproperty2(&bl,ALL_SAMEMAP);
  10663. }
  10664. return SCRIPT_CMD_SUCCESS;
  10665. }
  10666. /*==========================================
  10667. * Shows an emoticon on top of the player/npc
  10668. * emotion emotion#, <target: 0 - NPC, 1 - PC>, <NPC/PC name>
  10669. *------------------------------------------*/
  10670. //Optional second parameter added by [Skotlex]
  10671. BUILDIN_FUNC(emotion)
  10672. {
  10673. int type;
  10674. int player=0;
  10675. type=script_getnum(st,2);
  10676. if(type < 0 || type > 100)
  10677. return SCRIPT_CMD_SUCCESS;
  10678. if( script_hasdata(st,3) )
  10679. player=script_getnum(st,3);
  10680. if (player) {
  10681. TBL_PC *sd = NULL;
  10682. if( script_hasdata(st,4) )
  10683. sd = map_nick2sd(script_getstr(st,4));
  10684. else
  10685. sd = script_rid2sd(st);
  10686. if (sd)
  10687. clif_emotion(&sd->bl,type);
  10688. } else
  10689. if( script_hasdata(st,4) )
  10690. {
  10691. TBL_NPC *nd = npc_name2id(script_getstr(st,4));
  10692. if(nd)
  10693. clif_emotion(&nd->bl,type);
  10694. }
  10695. else
  10696. clif_emotion(map_id2bl(st->oid),type);
  10697. return SCRIPT_CMD_SUCCESS;
  10698. }
  10699. static int buildin_maprespawnguildid_sub_pc(struct map_session_data* sd, va_list ap)
  10700. {
  10701. int16 m=va_arg(ap,int);
  10702. int g_id=va_arg(ap,int);
  10703. int flag=va_arg(ap,int);
  10704. if(!sd || sd->bl.m != m)
  10705. return 0;
  10706. if(
  10707. (sd->status.guild_id == g_id && flag&1) || //Warp out owners
  10708. (sd->status.guild_id != g_id && flag&2) || //Warp out outsiders
  10709. (sd->status.guild_id == 0 && flag&2) // Warp out players not in guild
  10710. )
  10711. pc_setpos(sd,sd->status.save_point.map,sd->status.save_point.x,sd->status.save_point.y,CLR_TELEPORT);
  10712. return 1;
  10713. }
  10714. static int buildin_maprespawnguildid_sub_mob(struct block_list *bl,va_list ap)
  10715. {
  10716. struct mob_data *md=(struct mob_data *)bl;
  10717. if(!md->guardian_data && md->mob_id != MOBID_EMPERIUM)
  10718. status_kill(bl);
  10719. return 0;
  10720. }
  10721. /*
  10722. * Function to kick guild members out of a map and to their save points.
  10723. * m : mapid
  10724. * g_id : owner guild id
  10725. * flag & 1 : Warp out owners
  10726. * flag & 2 : Warp out outsiders
  10727. * flag & 4 : reserved for mobs
  10728. * */
  10729. BUILDIN_FUNC(maprespawnguildid)
  10730. {
  10731. const char *mapname=script_getstr(st,2);
  10732. int g_id=script_getnum(st,3);
  10733. int flag=script_getnum(st,4);
  10734. int16 m=map_mapname2mapid(mapname);
  10735. if(m == -1)
  10736. return SCRIPT_CMD_SUCCESS;
  10737. //Catch ALL players (in case some are 'between maps' on execution time)
  10738. map_foreachpc(buildin_maprespawnguildid_sub_pc,m,g_id,flag);
  10739. if (flag&4) //Remove script mobs.
  10740. map_foreachinmap(buildin_maprespawnguildid_sub_mob,m,BL_MOB);
  10741. return SCRIPT_CMD_SUCCESS;
  10742. }
  10743. BUILDIN_FUNC(agitstart)
  10744. {
  10745. if(agit_flag==1) return SCRIPT_CMD_SUCCESS; // Agit already Start.
  10746. agit_flag=1;
  10747. guild_agit_start();
  10748. return SCRIPT_CMD_SUCCESS;
  10749. }
  10750. BUILDIN_FUNC(agitend)
  10751. {
  10752. if(agit_flag==0) return SCRIPT_CMD_SUCCESS; // Agit already End.
  10753. agit_flag=0;
  10754. guild_agit_end();
  10755. return SCRIPT_CMD_SUCCESS;
  10756. }
  10757. BUILDIN_FUNC(agitstart2)
  10758. {
  10759. if(agit2_flag==1) return SCRIPT_CMD_SUCCESS; // Agit2 already Start.
  10760. agit2_flag=1;
  10761. guild_agit2_start();
  10762. return SCRIPT_CMD_SUCCESS;
  10763. }
  10764. BUILDIN_FUNC(agitend2)
  10765. {
  10766. if(agit2_flag==0) return SCRIPT_CMD_SUCCESS; // Agit2 already End.
  10767. agit2_flag=0;
  10768. guild_agit2_end();
  10769. return SCRIPT_CMD_SUCCESS;
  10770. }
  10771. /*==========================================
  10772. * Returns whether woe is on or off. // choice script
  10773. *------------------------------------------*/
  10774. BUILDIN_FUNC(agitcheck)
  10775. {
  10776. script_pushint(st,agit_flag);
  10777. return SCRIPT_CMD_SUCCESS;
  10778. }
  10779. /*==========================================
  10780. * Returns whether woese is on or off. // choice script
  10781. *------------------------------------------*/
  10782. BUILDIN_FUNC(agitcheck2)
  10783. {
  10784. script_pushint(st,agit2_flag);
  10785. return SCRIPT_CMD_SUCCESS;
  10786. }
  10787. /// Sets the guild_id of this npc.
  10788. ///
  10789. /// flagemblem <guild_id>;
  10790. BUILDIN_FUNC(flagemblem)
  10791. {
  10792. TBL_NPC* nd;
  10793. int g_id = script_getnum(st,2);
  10794. if(g_id < 0)
  10795. return SCRIPT_CMD_SUCCESS;
  10796. nd = (TBL_NPC*)map_id2nd(st->oid);
  10797. if( nd == NULL ) {
  10798. ShowError("script:flagemblem: npc %d not found\n", st->oid);
  10799. } else if( nd->subtype != NPCTYPE_SCRIPT ) {
  10800. ShowError("script:flagemblem: unexpected subtype %d for npc %d '%s'\n", nd->subtype, st->oid, nd->exname);
  10801. } else {
  10802. bool changed = ( nd->u.scr.guild_id != g_id )?true:false;
  10803. nd->u.scr.guild_id = g_id;
  10804. clif_guild_emblem_area(&nd->bl);
  10805. /* guild flag caching */
  10806. if( g_id ) /* adding a id */
  10807. guild_flag_add(nd);
  10808. else if( changed ) /* removing a flag */
  10809. guild_flag_remove(nd);
  10810. }
  10811. return SCRIPT_CMD_SUCCESS;
  10812. }
  10813. BUILDIN_FUNC(getcastlename)
  10814. {
  10815. const char* mapname = mapindex_getmapname(script_getstr(st,2),NULL);
  10816. struct guild_castle* gc = guild_mapname2gc(mapname);
  10817. const char* name = (gc) ? gc->castle_name : "";
  10818. script_pushstrcopy(st,name);
  10819. return SCRIPT_CMD_SUCCESS;
  10820. }
  10821. BUILDIN_FUNC(getcastledata)
  10822. {
  10823. const char *mapname = mapindex_getmapname(script_getstr(st,2),NULL);
  10824. int index = script_getnum(st,3);
  10825. struct guild_castle *gc = guild_mapname2gc(mapname);
  10826. if (gc == NULL) {
  10827. script_pushint(st,0);
  10828. ShowWarning("buildin_setcastledata: guild castle for map '%s' not found\n", mapname);
  10829. return SCRIPT_CMD_FAILURE;
  10830. }
  10831. switch (index) {
  10832. case 1:
  10833. script_pushint(st,gc->guild_id); break;
  10834. case 2:
  10835. script_pushint(st,gc->economy); break;
  10836. case 3:
  10837. script_pushint(st,gc->defense); break;
  10838. case 4:
  10839. script_pushint(st,gc->triggerE); break;
  10840. case 5:
  10841. script_pushint(st,gc->triggerD); break;
  10842. case 6:
  10843. script_pushint(st,gc->nextTime); break;
  10844. case 7:
  10845. script_pushint(st,gc->payTime); break;
  10846. case 8:
  10847. script_pushint(st,gc->createTime); break;
  10848. case 9:
  10849. script_pushint(st,gc->visibleC); break;
  10850. default:
  10851. if (index > 9 && index <= 9+MAX_GUARDIANS) {
  10852. script_pushint(st,gc->guardian[index-10].visible);
  10853. break;
  10854. }
  10855. script_pushint(st,0);
  10856. ShowWarning("buildin_setcastledata: index = '%d' is out of allowed range\n", index);
  10857. return SCRIPT_CMD_FAILURE;
  10858. }
  10859. return SCRIPT_CMD_SUCCESS;
  10860. }
  10861. BUILDIN_FUNC(setcastledata)
  10862. {
  10863. const char *mapname = mapindex_getmapname(script_getstr(st,2),NULL);
  10864. int index = script_getnum(st,3);
  10865. int value = script_getnum(st,4);
  10866. struct guild_castle *gc = guild_mapname2gc(mapname);
  10867. if (gc == NULL) {
  10868. ShowWarning("buildin_setcastledata: guild castle for map '%s' not found\n", mapname);
  10869. return SCRIPT_CMD_FAILURE;
  10870. }
  10871. if (index <= 0 || index > 9+MAX_GUARDIANS) {
  10872. ShowWarning("buildin_setcastledata: index = '%d' is out of allowed range\n", index);
  10873. return SCRIPT_CMD_FAILURE;
  10874. }
  10875. guild_castledatasave(gc->castle_id, index, value);
  10876. return SCRIPT_CMD_SUCCESS;
  10877. }
  10878. /* =====================================================================
  10879. * ---------------------------------------------------------------------*/
  10880. BUILDIN_FUNC(requestguildinfo)
  10881. {
  10882. int guild_id=script_getnum(st,2);
  10883. const char *event=NULL;
  10884. if( script_hasdata(st,3) ){
  10885. event=script_getstr(st,3);
  10886. check_event(st, event);
  10887. }
  10888. if(guild_id>0)
  10889. guild_npc_request_info(guild_id,event);
  10890. return SCRIPT_CMD_SUCCESS;
  10891. }
  10892. /// Returns the number of cards that have been compounded onto the specified equipped item.
  10893. /// getequipcardcnt(<equipment slot>);
  10894. BUILDIN_FUNC(getequipcardcnt)
  10895. {
  10896. int i=-1,j,num;
  10897. TBL_PC *sd;
  10898. int count;
  10899. num=script_getnum(st,2);
  10900. sd=script_rid2sd(st);
  10901. if (num > 0 && num <= ARRAYLENGTH(equip))
  10902. i=pc_checkequip(sd,equip[num-1]);
  10903. if (i < 0 || !sd->inventory_data[i]) {
  10904. script_pushint(st,0);
  10905. return SCRIPT_CMD_SUCCESS;
  10906. }
  10907. if(itemdb_isspecial(sd->status.inventory[i].card[0]))
  10908. {
  10909. script_pushint(st,0);
  10910. return SCRIPT_CMD_SUCCESS;
  10911. }
  10912. count = 0;
  10913. for( j = 0; j < sd->inventory_data[i]->slot; j++ )
  10914. if( sd->status.inventory[i].card[j] && itemdb_type(sd->status.inventory[i].card[j]) == IT_CARD )
  10915. count++;
  10916. script_pushint(st,count);
  10917. return SCRIPT_CMD_SUCCESS;
  10918. }
  10919. /// Removes all cards from the item found in the specified equipment slot of the invoking character,
  10920. /// and give them to the character. If any cards were removed in this manner, it will also show a success effect.
  10921. /// successremovecards <slot>;
  10922. BUILDIN_FUNC(successremovecards) {
  10923. int i=-1,c,cardflag=0;
  10924. TBL_PC* sd = script_rid2sd(st);
  10925. int num = script_getnum(st,2);
  10926. if (num > 0 && num <= ARRAYLENGTH(equip))
  10927. i=pc_checkequip(sd,equip[num-1]);
  10928. if (i < 0 || !sd->inventory_data[i]) {
  10929. return SCRIPT_CMD_SUCCESS;
  10930. }
  10931. if(itemdb_isspecial(sd->status.inventory[i].card[0]))
  10932. return SCRIPT_CMD_SUCCESS;
  10933. for( c = sd->inventory_data[i]->slot - 1; c >= 0; --c ) {
  10934. if( sd->status.inventory[i].card[c] && itemdb_type(sd->status.inventory[i].card[c]) == IT_CARD ) {// extract this card from the item
  10935. unsigned char flag = 0;
  10936. struct item item_tmp;
  10937. memset(&item_tmp,0,sizeof(item_tmp));
  10938. cardflag = 1;
  10939. item_tmp.nameid = sd->status.inventory[i].card[c];
  10940. item_tmp.identify = 1;
  10941. if((flag=pc_additem(sd,&item_tmp,1,LOG_TYPE_SCRIPT))){ // get back the cart in inventory
  10942. clif_additem(sd,0,0,flag);
  10943. map_addflooritem(&item_tmp,1,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0,0);
  10944. }
  10945. }
  10946. }
  10947. if(cardflag == 1) {//if card was remove remplace item with no card
  10948. unsigned char flag = 0, j;
  10949. struct item item_tmp;
  10950. memset(&item_tmp,0,sizeof(item_tmp));
  10951. item_tmp.nameid = sd->status.inventory[i].nameid;
  10952. item_tmp.identify = 1;
  10953. item_tmp.refine = sd->status.inventory[i].refine;
  10954. item_tmp.attribute = sd->status.inventory[i].attribute;
  10955. item_tmp.expire_time = sd->status.inventory[i].expire_time;
  10956. item_tmp.bound = sd->status.inventory[i].bound;
  10957. for (j = sd->inventory_data[i]->slot; j < MAX_SLOTS; j++)
  10958. item_tmp.card[j]=sd->status.inventory[i].card[j];
  10959. pc_delitem(sd,i,1,0,3,LOG_TYPE_SCRIPT);
  10960. if((flag=pc_additem(sd,&item_tmp,1,LOG_TYPE_SCRIPT))){ //chk if can be spawn in inventory otherwise put on floor
  10961. clif_additem(sd,0,0,flag);
  10962. map_addflooritem(&item_tmp,1,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0,0);
  10963. }
  10964. clif_misceffect(&sd->bl,3);
  10965. }
  10966. return SCRIPT_CMD_SUCCESS;
  10967. }
  10968. /// Removes all cards from the item found in the specified equipment slot of the invoking character.
  10969. /// failedremovecards <slot>, <type>;
  10970. /// <type>=0 : will destroy both the item and the cards.
  10971. /// <type>=1 : will keep the item, but destroy the cards.
  10972. /// <type>=2 : will keep the cards, but destroy the item.
  10973. /// <type>=? : will just display the failure effect.
  10974. BUILDIN_FUNC(failedremovecards) {
  10975. int i=-1,c,cardflag=0;
  10976. TBL_PC* sd = script_rid2sd(st);
  10977. int num = script_getnum(st,2);
  10978. int typefail = script_getnum(st,3);
  10979. if (num > 0 && num <= ARRAYLENGTH(equip))
  10980. i=pc_checkequip(sd,equip[num-1]);
  10981. if (i < 0 || !sd->inventory_data[i])
  10982. return SCRIPT_CMD_SUCCESS;
  10983. if(itemdb_isspecial(sd->status.inventory[i].card[0]))
  10984. return SCRIPT_CMD_SUCCESS;
  10985. for( c = sd->inventory_data[i]->slot - 1; c >= 0; --c ) {
  10986. if( sd->status.inventory[i].card[c] && itemdb_type(sd->status.inventory[i].card[c]) == IT_CARD ) {
  10987. cardflag = 1;
  10988. if(typefail == 2) {// add cards to inventory, clear
  10989. unsigned char flag = 0;
  10990. struct item item_tmp;
  10991. memset(&item_tmp,0,sizeof(item_tmp));
  10992. item_tmp.nameid = sd->status.inventory[i].card[c];
  10993. item_tmp.identify = 1;
  10994. if((flag=pc_additem(sd,&item_tmp,1,LOG_TYPE_SCRIPT))){
  10995. clif_additem(sd,0,0,flag);
  10996. map_addflooritem(&item_tmp,1,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0,0);
  10997. }
  10998. }
  10999. }
  11000. }
  11001. if(cardflag == 1) {
  11002. if(typefail == 0 || typefail == 2){ // destroy the item
  11003. pc_delitem(sd,i,1,0,2,LOG_TYPE_SCRIPT);
  11004. }
  11005. if(typefail == 1){ // destroy the card
  11006. unsigned char flag = 0, j;
  11007. struct item item_tmp;
  11008. memset(&item_tmp,0,sizeof(item_tmp));
  11009. item_tmp.nameid = sd->status.inventory[i].nameid;
  11010. item_tmp.identify = 1;
  11011. item_tmp.refine = sd->status.inventory[i].refine;
  11012. item_tmp.attribute = sd->status.inventory[i].attribute;
  11013. item_tmp.expire_time = sd->status.inventory[i].expire_time;
  11014. item_tmp.bound = sd->status.inventory[i].bound;
  11015. for (j = sd->inventory_data[i]->slot; j < MAX_SLOTS; j++)
  11016. item_tmp.card[j]=sd->status.inventory[i].card[j];
  11017. pc_delitem(sd,i,1,0,2,LOG_TYPE_SCRIPT);
  11018. if((flag=pc_additem(sd,&item_tmp,1,LOG_TYPE_SCRIPT))){
  11019. clif_additem(sd,0,0,flag);
  11020. map_addflooritem(&item_tmp,1,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0,0);
  11021. }
  11022. }
  11023. clif_misceffect(&sd->bl,2);
  11024. }
  11025. return SCRIPT_CMD_SUCCESS;
  11026. }
  11027. /* ================================================================
  11028. * mapwarp "<from map>","<to map>",<x>,<y>,<type>,<ID for Type>;
  11029. * type: 0=everyone, 1=guild, 2=party; [Reddozen]
  11030. * improved by [Lance]
  11031. * ================================================================*/
  11032. BUILDIN_FUNC(mapwarp) // Added by RoVeRT
  11033. {
  11034. int x,y,m,check_val=0,check_ID=0,i=0;
  11035. struct guild *g = NULL;
  11036. struct party_data *p = NULL;
  11037. const char *str;
  11038. const char *mapname;
  11039. unsigned int index;
  11040. mapname=script_getstr(st,2);
  11041. str=script_getstr(st,3);
  11042. x=script_getnum(st,4);
  11043. y=script_getnum(st,5);
  11044. if(script_hasdata(st,7)){
  11045. check_val=script_getnum(st,6);
  11046. check_ID=script_getnum(st,7);
  11047. }
  11048. if((m=map_mapname2mapid(mapname))< 0)
  11049. return SCRIPT_CMD_SUCCESS;
  11050. if(!(index=mapindex_name2id(str)))
  11051. return SCRIPT_CMD_SUCCESS;
  11052. switch(check_val){
  11053. case 1:
  11054. g = guild_search(check_ID);
  11055. if (g){
  11056. for( i=0; i < g->max_member; i++)
  11057. {
  11058. if(g->member[i].sd && g->member[i].sd->bl.m==m){
  11059. pc_setpos(g->member[i].sd,index,x,y,CLR_TELEPORT);
  11060. }
  11061. }
  11062. }
  11063. break;
  11064. case 2:
  11065. p = party_search(check_ID);
  11066. if(p){
  11067. for(i=0;i<MAX_PARTY; i++){
  11068. if(p->data[i].sd && p->data[i].sd->bl.m == m){
  11069. pc_setpos(p->data[i].sd,index,x,y,CLR_TELEPORT);
  11070. }
  11071. }
  11072. }
  11073. break;
  11074. default:
  11075. map_foreachinmap(buildin_areawarp_sub,m,BL_PC,index,x,y,0,0);
  11076. break;
  11077. }
  11078. return SCRIPT_CMD_SUCCESS;
  11079. }
  11080. static int buildin_mobcount_sub(struct block_list *bl,va_list ap) // Added by RoVeRT
  11081. {
  11082. char *event=va_arg(ap,char *);
  11083. struct mob_data *md = ((struct mob_data *)bl);
  11084. if( md->status.hp > 0 && (!event || strcmp(event,md->npc_event) == 0) )
  11085. return 1;
  11086. return SCRIPT_CMD_SUCCESS;
  11087. }
  11088. BUILDIN_FUNC(mobcount) // Added by RoVeRT
  11089. {
  11090. const char *mapname,*event;
  11091. int16 m;
  11092. mapname=script_getstr(st,2);
  11093. event=script_getstr(st,3);
  11094. if( strcmp(event, "all") == 0 )
  11095. event = NULL;
  11096. else
  11097. check_event(st, event);
  11098. if( strcmp(mapname, "this") == 0 ) {
  11099. struct map_session_data *sd = script_rid2sd(st);
  11100. if( sd )
  11101. m = sd->bl.m;
  11102. else {
  11103. script_pushint(st,-1);
  11104. return SCRIPT_CMD_SUCCESS;
  11105. }
  11106. }
  11107. else if( (m = map_mapname2mapid(mapname)) < 0 ) {
  11108. script_pushint(st,-1);
  11109. return SCRIPT_CMD_SUCCESS;
  11110. }
  11111. script_pushint(st,map_foreachinmap(buildin_mobcount_sub, m, BL_MOB, event));
  11112. return SCRIPT_CMD_SUCCESS;
  11113. }
  11114. BUILDIN_FUNC(marriage)
  11115. {
  11116. const char *partner=script_getstr(st,2);
  11117. TBL_PC *sd=script_rid2sd(st);
  11118. TBL_PC *p_sd=map_nick2sd(partner);
  11119. if(!sd || !p_sd || !pc_marriage(sd,p_sd)){
  11120. script_pushint(st,0);
  11121. return SCRIPT_CMD_SUCCESS;
  11122. }
  11123. script_pushint(st,1);
  11124. return SCRIPT_CMD_SUCCESS;
  11125. }
  11126. BUILDIN_FUNC(wedding_effect)
  11127. {
  11128. TBL_PC *sd=script_rid2sd(st);
  11129. struct block_list *bl;
  11130. if(sd==NULL) {
  11131. bl=map_id2bl(st->oid);
  11132. } else
  11133. bl=&sd->bl;
  11134. clif_wedding_effect(bl);
  11135. return SCRIPT_CMD_SUCCESS;
  11136. }
  11137. /**
  11138. * divorce({<char_id>})
  11139. **/
  11140. BUILDIN_FUNC(divorce)
  11141. {
  11142. TBL_PC *sd;
  11143. if (!script_charid2sd(2,sd) || !pc_divorce(sd)) {
  11144. script_pushint(st,0);
  11145. return SCRIPT_CMD_FAILURE;
  11146. }
  11147. script_pushint(st,1);
  11148. return SCRIPT_CMD_SUCCESS;
  11149. }
  11150. /**
  11151. * ispartneron({<char_id>})
  11152. **/
  11153. BUILDIN_FUNC(ispartneron)
  11154. {
  11155. TBL_PC *sd;
  11156. if (!script_charid2sd(2,sd) || !pc_ismarried(sd) ||
  11157. map_charid2sd(sd->status.partner_id) == NULL)
  11158. {
  11159. script_pushint(st,0);
  11160. return SCRIPT_CMD_FAILURE;
  11161. }
  11162. script_pushint(st,1);
  11163. return SCRIPT_CMD_SUCCESS;
  11164. }
  11165. /**
  11166. * getpartnerid({<char_id>})
  11167. **/
  11168. BUILDIN_FUNC(getpartnerid)
  11169. {
  11170. TBL_PC *sd;
  11171. if (!script_charid2sd(2,sd)) {
  11172. script_pushint(st,0);
  11173. return SCRIPT_CMD_FAILURE;
  11174. }
  11175. script_pushint(st,sd->status.partner_id);
  11176. return SCRIPT_CMD_SUCCESS;
  11177. }
  11178. /**
  11179. * getchildid({<char_id>})
  11180. **/
  11181. BUILDIN_FUNC(getchildid)
  11182. {
  11183. TBL_PC *sd;
  11184. if (!script_charid2sd(2,sd)) {
  11185. script_pushint(st,0);
  11186. return SCRIPT_CMD_FAILURE;
  11187. }
  11188. script_pushint(st,sd->status.child);
  11189. return SCRIPT_CMD_SUCCESS;
  11190. }
  11191. /**
  11192. * getmotherid({<char_id>})
  11193. **/
  11194. BUILDIN_FUNC(getmotherid)
  11195. {
  11196. TBL_PC *sd;
  11197. if (!script_charid2sd(2,sd)) {
  11198. script_pushint(st,0);
  11199. return SCRIPT_CMD_FAILURE;
  11200. }
  11201. script_pushint(st,sd->status.mother);
  11202. return SCRIPT_CMD_SUCCESS;
  11203. }
  11204. /**
  11205. * getfatherid({<char_id>})
  11206. **/
  11207. BUILDIN_FUNC(getfatherid)
  11208. {
  11209. TBL_PC *sd;
  11210. if (!script_charid2sd(2,sd)) {
  11211. script_pushint(st,0);
  11212. return SCRIPT_CMD_FAILURE;
  11213. }
  11214. script_pushint(st,sd->status.father);
  11215. return SCRIPT_CMD_SUCCESS;
  11216. }
  11217. BUILDIN_FUNC(warppartner)
  11218. {
  11219. int x,y;
  11220. unsigned short mapindex;
  11221. const char *str;
  11222. TBL_PC *sd=script_rid2sd(st);
  11223. TBL_PC *p_sd=NULL;
  11224. if(sd==NULL || !pc_ismarried(sd) ||
  11225. (p_sd=map_charid2sd(sd->status.partner_id)) == NULL) {
  11226. script_pushint(st,0);
  11227. return SCRIPT_CMD_SUCCESS;
  11228. }
  11229. str=script_getstr(st,2);
  11230. x=script_getnum(st,3);
  11231. y=script_getnum(st,4);
  11232. mapindex = mapindex_name2id(str);
  11233. if (mapindex) {
  11234. pc_setpos(p_sd,mapindex,x,y,CLR_OUTSIGHT);
  11235. script_pushint(st,1);
  11236. } else
  11237. script_pushint(st,0);
  11238. return SCRIPT_CMD_SUCCESS;
  11239. }
  11240. /*================================================
  11241. * Script for Displaying MOB Information [Valaris]
  11242. *------------------------------------------------*/
  11243. BUILDIN_FUNC(strmobinfo)
  11244. {
  11245. int num=script_getnum(st,2);
  11246. int class_=script_getnum(st,3);
  11247. if(!mobdb_checkid(class_))
  11248. {
  11249. if (num < 3) //requested a string
  11250. script_pushconststr(st,"");
  11251. else
  11252. script_pushint(st,0);
  11253. return SCRIPT_CMD_SUCCESS;
  11254. }
  11255. switch (num) {
  11256. case 1: script_pushstrcopy(st,mob_db(class_)->name); break;
  11257. case 2: script_pushstrcopy(st,mob_db(class_)->jname); break;
  11258. case 3: script_pushint(st,mob_db(class_)->lv); break;
  11259. case 4: script_pushint(st,mob_db(class_)->status.max_hp); break;
  11260. case 5: script_pushint(st,mob_db(class_)->status.max_sp); break;
  11261. case 6: script_pushint(st,mob_db(class_)->base_exp); break;
  11262. case 7: script_pushint(st,mob_db(class_)->job_exp); break;
  11263. default:
  11264. script_pushint(st,0);
  11265. break;
  11266. }
  11267. return SCRIPT_CMD_SUCCESS;
  11268. }
  11269. /*==========================================
  11270. * Summon guardians [Valaris]
  11271. * guardian("<map name>",<x>,<y>,"<name to show>",<mob id>{,"<event label>"}{,<guardian index>}) -> <id>
  11272. *------------------------------------------*/
  11273. BUILDIN_FUNC(guardian)
  11274. {
  11275. int class_=0,x=0,y=0,guardian=0;
  11276. const char *str,*mapname,*evt="";
  11277. bool has_index = false;
  11278. mapname =script_getstr(st,2);
  11279. x =script_getnum(st,3);
  11280. y =script_getnum(st,4);
  11281. str =script_getstr(st,5);
  11282. class_=script_getnum(st,6);
  11283. if( script_hasdata(st,8) )
  11284. {// "<event label>",<guardian index>
  11285. evt=script_getstr(st,7);
  11286. guardian=script_getnum(st,8);
  11287. has_index = true;
  11288. } else if( script_hasdata(st,7) ){
  11289. struct script_data *data = script_getdata(st,7);
  11290. get_val(st,data);
  11291. if( data_isstring(data) )
  11292. {// "<event label>"
  11293. evt=script_getstr(st,7);
  11294. } else if( data_isint(data) )
  11295. {// <guardian index>
  11296. guardian=script_getnum(st,7);
  11297. has_index = true;
  11298. } else {
  11299. ShowError("script:guardian: invalid data type for argument #6 (from 1)\n");
  11300. script_reportdata(data);
  11301. return SCRIPT_CMD_FAILURE;
  11302. }
  11303. }
  11304. check_event(st, evt);
  11305. script_pushint(st, mob_spawn_guardian(mapname,x,y,str,class_,evt,guardian,has_index));
  11306. return SCRIPT_CMD_SUCCESS;
  11307. }
  11308. /*==========================================
  11309. * Invisible Walls [Zephyrus]
  11310. *------------------------------------------*/
  11311. BUILDIN_FUNC(setwall)
  11312. {
  11313. const char *mapname, *name;
  11314. int x, y, m, size, dir;
  11315. bool shootable;
  11316. mapname = script_getstr(st,2);
  11317. x = script_getnum(st,3);
  11318. y = script_getnum(st,4);
  11319. size = script_getnum(st,5);
  11320. dir = script_getnum(st,6);
  11321. shootable = script_getnum(st,7);
  11322. name = script_getstr(st,8);
  11323. if( (m = map_mapname2mapid(mapname)) < 0 )
  11324. return SCRIPT_CMD_SUCCESS; // Invalid Map
  11325. map_iwall_set(m, x, y, size, dir, shootable, name);
  11326. return SCRIPT_CMD_SUCCESS;
  11327. }
  11328. BUILDIN_FUNC(delwall)
  11329. {
  11330. const char *name = script_getstr(st,2);
  11331. map_iwall_remove(name);
  11332. return SCRIPT_CMD_SUCCESS;
  11333. }
  11334. /// Retrieves various information about the specified guardian.
  11335. ///
  11336. /// guardianinfo("<map_name>", <index>, <type>) -> <value>
  11337. /// type: 0 - whether it is deployed or not
  11338. /// 1 - maximum hp
  11339. /// 2 - current hp
  11340. ///
  11341. BUILDIN_FUNC(guardianinfo)
  11342. {
  11343. const char* mapname = mapindex_getmapname(script_getstr(st,2),NULL);
  11344. int id = script_getnum(st,3);
  11345. int type = script_getnum(st,4);
  11346. struct guild_castle* gc = guild_mapname2gc(mapname);
  11347. struct mob_data* gd;
  11348. if( gc == NULL || id < 0 || id >= MAX_GUARDIANS )
  11349. {
  11350. script_pushint(st,-1);
  11351. return SCRIPT_CMD_SUCCESS;
  11352. }
  11353. if( type == 0 )
  11354. script_pushint(st, gc->guardian[id].visible);
  11355. else
  11356. if( !gc->guardian[id].visible )
  11357. script_pushint(st,-1);
  11358. else
  11359. if( (gd = map_id2md(gc->guardian[id].id)) == NULL )
  11360. script_pushint(st,-1);
  11361. else
  11362. {
  11363. if ( type == 1 ) script_pushint(st,gd->status.max_hp);
  11364. else if( type == 2 ) script_pushint(st,gd->status.hp);
  11365. else
  11366. script_pushint(st,-1);
  11367. }
  11368. return SCRIPT_CMD_SUCCESS;
  11369. }
  11370. /*==========================================
  11371. * Get the item name by item_id or null
  11372. *------------------------------------------*/
  11373. BUILDIN_FUNC(getitemname)
  11374. {
  11375. unsigned short item_id = 0;
  11376. struct item_data *i_data;
  11377. char *item_name;
  11378. struct script_data *data;
  11379. data=script_getdata(st,2);
  11380. get_val(st,data);
  11381. if( data_isstring(data) ){
  11382. const char *name=conv_str(st,data);
  11383. struct item_data *item_data = itemdb_searchname(name);
  11384. if( item_data )
  11385. item_id=item_data->nameid;
  11386. }else
  11387. item_id=conv_num(st,data);
  11388. i_data = itemdb_exists(item_id);
  11389. if (i_data == NULL)
  11390. {
  11391. script_pushconststr(st,"null");
  11392. return SCRIPT_CMD_SUCCESS;
  11393. }
  11394. item_name=(char *)aMalloc(ITEM_NAME_LENGTH*sizeof(char));
  11395. memcpy(item_name, i_data->jname, ITEM_NAME_LENGTH);
  11396. script_pushstr(st,item_name);
  11397. return SCRIPT_CMD_SUCCESS;
  11398. }
  11399. /*==========================================
  11400. * Returns number of slots an item has. [Skotlex]
  11401. *------------------------------------------*/
  11402. BUILDIN_FUNC(getitemslots)
  11403. {
  11404. unsigned short item_id;
  11405. struct item_data *i_data;
  11406. item_id=script_getnum(st,2);
  11407. i_data = itemdb_exists(item_id);
  11408. if (i_data)
  11409. script_pushint(st,i_data->slot);
  11410. else
  11411. script_pushint(st,-1);
  11412. return SCRIPT_CMD_SUCCESS;
  11413. }
  11414. // TODO: add matk here if needed/once we get rid of RENEWAL
  11415. /*==========================================
  11416. * Returns some values of an item [Lupus]
  11417. * Price, Weight, etc...
  11418. getiteminfo(itemID,n), where n
  11419. 0 value_buy;
  11420. 1 value_sell;
  11421. 2 type;
  11422. 3 maxchance = Max drop chance of this item e.g. 1 = 0.01% , etc..
  11423. if = 0, then monsters don't drop it at all (rare or a quest item)
  11424. if = -1, then this item is sold in NPC shops only
  11425. 4 sex;
  11426. 5 equip;
  11427. 6 weight;
  11428. 7 atk;
  11429. 8 def;
  11430. 9 range;
  11431. 10 slot;
  11432. 11 look;
  11433. 12 elv;
  11434. 13 wlv;
  11435. 14 view id
  11436. 15 eLvmax
  11437. 16 matk (renewal)
  11438. *------------------------------------------*/
  11439. BUILDIN_FUNC(getiteminfo)
  11440. {
  11441. unsigned short item_id,n;
  11442. struct item_data *i_data;
  11443. item_id = script_getnum(st,2);
  11444. n = script_getnum(st,3);
  11445. i_data = itemdb_exists(item_id);
  11446. if (i_data && n <= 16) {
  11447. int *item_arr = (int*)&i_data->value_buy;
  11448. #ifndef RENEWAL
  11449. if (n == 16)
  11450. script_pushint(st,0);
  11451. else
  11452. #endif
  11453. script_pushint(st,item_arr[n]);
  11454. } else
  11455. script_pushint(st,-1);
  11456. return SCRIPT_CMD_SUCCESS;
  11457. }
  11458. /*==========================================
  11459. * Set some values of an item [Lupus]
  11460. * Price, Weight, etc...
  11461. setiteminfo(itemID,n,Value), where n
  11462. 0 value_buy;
  11463. 1 value_sell;
  11464. 2 type;
  11465. 3 maxchance = Max drop chance of this item e.g. 1 = 0.01% , etc..
  11466. if = 0, then monsters don't drop it at all (rare or a quest item)
  11467. if = -1, then this item is sold in NPC shops only
  11468. 4 sex;
  11469. 5 equip;
  11470. 6 weight;
  11471. 7 atk;
  11472. 8 def;
  11473. 9 range;
  11474. 10 slot;
  11475. 11 look;
  11476. 12 elv;
  11477. 13 wlv;
  11478. 14 view id
  11479. * Returns Value or -1 if the wrong field's been set
  11480. *------------------------------------------*/
  11481. BUILDIN_FUNC(setiteminfo)
  11482. {
  11483. unsigned short item_id;
  11484. int n,value;
  11485. struct item_data *i_data;
  11486. item_id = script_getnum(st,2);
  11487. n = script_getnum(st,3);
  11488. value = script_getnum(st,4);
  11489. i_data = itemdb_exists(item_id);
  11490. if (i_data && n>=0 && n<=14) {
  11491. int *item_arr = (int*)&i_data->value_buy;
  11492. item_arr[n] = value;
  11493. script_pushint(st,value);
  11494. } else
  11495. script_pushint(st,-1);
  11496. return SCRIPT_CMD_SUCCESS;
  11497. }
  11498. /*==========================================
  11499. * Returns value from equipped item slot n [Lupus]
  11500. getequipcardid(num,slot)
  11501. where
  11502. num = eqip position slot
  11503. slot = 0,1,2,3 (Card Slot N)
  11504. This func returns CARD ID, 255,254,-255 (for card 0, if the item is produced)
  11505. it's useful when you want to check item cards or if it's signed
  11506. Useful for such quests as "Sign this refined item with players name" etc
  11507. Hat[0] +4 -> Player's Hat[0] +4
  11508. *------------------------------------------*/
  11509. BUILDIN_FUNC(getequipcardid)
  11510. {
  11511. int i=-1,num,slot;
  11512. TBL_PC *sd;
  11513. num=script_getnum(st,2);
  11514. slot=script_getnum(st,3);
  11515. sd=script_rid2sd(st);
  11516. if (num > 0 && num <= ARRAYLENGTH(equip))
  11517. i=pc_checkequip(sd,equip[num-1]);
  11518. if(i >= 0 && slot>=0 && slot<4)
  11519. script_pushint(st,sd->status.inventory[i].card[slot]);
  11520. else
  11521. script_pushint(st,0);
  11522. return SCRIPT_CMD_SUCCESS;
  11523. }
  11524. /*==========================================
  11525. * petskillbonus [Valaris] //Rewritten by [Skotlex]
  11526. *------------------------------------------*/
  11527. BUILDIN_FUNC(petskillbonus)
  11528. {
  11529. struct pet_data *pd;
  11530. TBL_PC *sd = script_rid2sd(st);
  11531. if(sd == NULL || sd->pd == NULL)
  11532. return SCRIPT_CMD_FAILURE;
  11533. pd = sd->pd;
  11534. if (pd->bonus)
  11535. { //Clear previous bonus
  11536. if (pd->bonus->timer != INVALID_TIMER)
  11537. delete_timer(pd->bonus->timer, pet_skill_bonus_timer);
  11538. } else //init
  11539. pd->bonus = (struct pet_bonus *) aMalloc(sizeof(struct pet_bonus));
  11540. pd->bonus->type = script_getnum(st,2);
  11541. pd->bonus->val = script_getnum(st,3);
  11542. pd->bonus->duration = script_getnum(st,4);
  11543. pd->bonus->delay = script_getnum(st,5);
  11544. if (pd->state.skillbonus == 1)
  11545. pd->state.skillbonus = 0; // waiting state
  11546. // wait for timer to start
  11547. if (battle_config.pet_equip_required && pd->pet.equip == 0)
  11548. pd->bonus->timer = INVALID_TIMER;
  11549. else
  11550. pd->bonus->timer = add_timer(gettick()+pd->bonus->delay*1000, pet_skill_bonus_timer, sd->bl.id, 0);
  11551. return SCRIPT_CMD_SUCCESS;
  11552. }
  11553. /*==========================================
  11554. * pet looting [Valaris] //Rewritten by [Skotlex]
  11555. *------------------------------------------*/
  11556. BUILDIN_FUNC(petloot)
  11557. {
  11558. int max;
  11559. struct pet_data *pd;
  11560. TBL_PC *sd=script_rid2sd(st);
  11561. if(sd==NULL || sd->pd==NULL)
  11562. return SCRIPT_CMD_SUCCESS;
  11563. max=script_getnum(st,2);
  11564. if(max < 1)
  11565. max = 1; //Let'em loot at least 1 item.
  11566. else if (max > MAX_PETLOOT_SIZE)
  11567. max = MAX_PETLOOT_SIZE;
  11568. pd = sd->pd;
  11569. if (pd->loot != NULL)
  11570. { //Release whatever was there already and reallocate memory
  11571. pet_lootitem_drop(pd, pd->master);
  11572. aFree(pd->loot->item);
  11573. }
  11574. else
  11575. pd->loot = (struct pet_loot *)aMalloc(sizeof(struct pet_loot));
  11576. pd->loot->item = (struct item *)aCalloc(max,sizeof(struct item));
  11577. pd->loot->max=max;
  11578. pd->loot->count = 0;
  11579. pd->loot->weight = 0;
  11580. return SCRIPT_CMD_SUCCESS;
  11581. }
  11582. /*==========================================
  11583. * Set arrays with info of all sd inventory :
  11584. * @inventorylist_id, @inventorylist_amount, @inventorylist_equip,
  11585. * @inventorylist_refine, @inventorylist_identify, @inventorylist_attribute,
  11586. * @inventorylist_card(0..3), @inventorylist_expire
  11587. * @inventorylist_count = scalar
  11588. *------------------------------------------*/
  11589. BUILDIN_FUNC(getinventorylist)
  11590. {
  11591. TBL_PC *sd;
  11592. char card_var[NAME_LENGTH];
  11593. int i,j=0,k;
  11594. if (!script_charid2sd(2,sd))
  11595. return SCRIPT_CMD_FAILURE;
  11596. for(i=0;i<MAX_INVENTORY;i++){
  11597. if(sd->status.inventory[i].nameid > 0 && sd->status.inventory[i].amount > 0){
  11598. pc_setreg(sd,reference_uid(add_str("@inventorylist_id"), j),sd->status.inventory[i].nameid);
  11599. pc_setreg(sd,reference_uid(add_str("@inventorylist_amount"), j),sd->status.inventory[i].amount);
  11600. pc_setreg(sd,reference_uid(add_str("@inventorylist_equip"), j),sd->status.inventory[i].equip);
  11601. pc_setreg(sd,reference_uid(add_str("@inventorylist_refine"), j),sd->status.inventory[i].refine);
  11602. pc_setreg(sd,reference_uid(add_str("@inventorylist_identify"), j),sd->status.inventory[i].identify);
  11603. pc_setreg(sd,reference_uid(add_str("@inventorylist_attribute"), j),sd->status.inventory[i].attribute);
  11604. for (k = 0; k < MAX_SLOTS; k++)
  11605. {
  11606. sprintf(card_var, "@inventorylist_card%d",k+1);
  11607. pc_setreg(sd,reference_uid(add_str(card_var), j),sd->status.inventory[i].card[k]);
  11608. }
  11609. pc_setreg(sd,reference_uid(add_str("@inventorylist_expire"), j),sd->status.inventory[i].expire_time);
  11610. pc_setreg(sd,reference_uid(add_str("@inventorylist_bound"), j),sd->status.inventory[i].bound);
  11611. j++;
  11612. }
  11613. }
  11614. pc_setreg(sd,add_str("@inventorylist_count"),j);
  11615. return SCRIPT_CMD_SUCCESS;
  11616. }
  11617. /**
  11618. * getskilllist ({<char_id>});
  11619. **/
  11620. BUILDIN_FUNC(getskilllist)
  11621. {
  11622. TBL_PC *sd;
  11623. int i,j=0;
  11624. if (!script_charid2sd(2,sd))
  11625. return SCRIPT_CMD_FAILURE;
  11626. for(i=0;i<MAX_SKILL;i++){
  11627. if(sd->status.skill[i].id > 0 && sd->status.skill[i].lv > 0){
  11628. pc_setreg(sd,reference_uid(add_str("@skilllist_id"), j),sd->status.skill[i].id);
  11629. pc_setreg(sd,reference_uid(add_str("@skilllist_lv"), j),sd->status.skill[i].lv);
  11630. pc_setreg(sd,reference_uid(add_str("@skilllist_flag"), j),sd->status.skill[i].flag);
  11631. j++;
  11632. }
  11633. }
  11634. pc_setreg(sd,add_str("@skilllist_count"),j);
  11635. return SCRIPT_CMD_SUCCESS;
  11636. }
  11637. /**
  11638. * clearitem ({<char_id>});
  11639. **/
  11640. BUILDIN_FUNC(clearitem)
  11641. {
  11642. TBL_PC *sd;
  11643. int i;
  11644. if (!script_charid2sd(2,sd))
  11645. return SCRIPT_CMD_FAILURE;
  11646. for (i=0; i<MAX_INVENTORY; i++) {
  11647. if (sd->status.inventory[i].amount) {
  11648. pc_delitem(sd, i, sd->status.inventory[i].amount, 0, 0, LOG_TYPE_SCRIPT);
  11649. }
  11650. }
  11651. return SCRIPT_CMD_SUCCESS;
  11652. }
  11653. /**
  11654. * Disguise Player (returns Mob/NPC ID if success, 0 on fail)
  11655. * disguise <Monster ID>{,<char_id>};
  11656. **/
  11657. BUILDIN_FUNC(disguise)
  11658. {
  11659. int id;
  11660. TBL_PC* sd;
  11661. if (!script_charid2sd(3,sd))
  11662. return SCRIPT_CMD_FAILURE;
  11663. id = script_getnum(st,2);
  11664. if (mobdb_checkid(id) || npcdb_checkid(id)) {
  11665. pc_disguise(sd, id);
  11666. script_pushint(st,id);
  11667. } else
  11668. script_pushint(st,0);
  11669. return SCRIPT_CMD_SUCCESS;
  11670. }
  11671. /**
  11672. * Undisguise Player (returns 1 if success, 0 on fail)
  11673. * undisguise {<char_id>};
  11674. **/
  11675. BUILDIN_FUNC(undisguise)
  11676. {
  11677. TBL_PC* sd;
  11678. if (!script_charid2sd(2,sd))
  11679. return SCRIPT_CMD_FAILURE;
  11680. if (sd->disguise) {
  11681. pc_disguise(sd, 0);
  11682. script_pushint(st,0);
  11683. } else {
  11684. script_pushint(st,1);
  11685. }
  11686. return SCRIPT_CMD_SUCCESS;
  11687. }
  11688. /*==========================================
  11689. * Transform a bl to another _class,
  11690. * @type unused
  11691. *------------------------------------------*/
  11692. BUILDIN_FUNC(classchange)
  11693. {
  11694. int _class,type;
  11695. struct block_list *bl=map_id2bl(st->oid);
  11696. if (bl==NULL)
  11697. return SCRIPT_CMD_FAILURE;
  11698. _class=script_getnum(st,2);
  11699. type=script_getnum(st,3);
  11700. clif_class_change(bl,_class,type);
  11701. return SCRIPT_CMD_SUCCESS;
  11702. }
  11703. /*==========================================
  11704. * Display an effect
  11705. *------------------------------------------*/
  11706. BUILDIN_FUNC(misceffect)
  11707. {
  11708. int type;
  11709. type=script_getnum(st,2);
  11710. if(st->oid && st->oid != fake_nd->bl.id) {
  11711. struct block_list *bl = map_id2bl(st->oid);
  11712. if (bl)
  11713. clif_specialeffect(bl,type,AREA);
  11714. } else{
  11715. TBL_PC *sd=script_rid2sd(st);
  11716. if(sd)
  11717. clif_specialeffect(&sd->bl,type,AREA);
  11718. }
  11719. return SCRIPT_CMD_SUCCESS;
  11720. }
  11721. /*==========================================
  11722. * Play a BGM on a single client [Rikter/Yommy]
  11723. *------------------------------------------*/
  11724. BUILDIN_FUNC(playBGM)
  11725. {
  11726. struct map_session_data* sd;
  11727. if( ( sd = script_rid2sd(st) ) != NULL ) {
  11728. clif_playBGM(sd, script_getstr(st,2));
  11729. }
  11730. return SCRIPT_CMD_SUCCESS;
  11731. }
  11732. static int playBGM_sub(struct block_list* bl,va_list ap)
  11733. {
  11734. const char* name = va_arg(ap,const char*);
  11735. clif_playBGM(BL_CAST(BL_PC, bl), name);
  11736. return 0;
  11737. }
  11738. static int playBGM_foreachpc_sub(struct map_session_data* sd, va_list args)
  11739. {
  11740. const char* name = va_arg(args, const char*);
  11741. clif_playBGM(sd, name);
  11742. return 0;
  11743. }
  11744. /*==========================================
  11745. * Play a BGM on multiple client [Rikter/Yommy]
  11746. *------------------------------------------*/
  11747. BUILDIN_FUNC(playBGMall)
  11748. {
  11749. const char* name;
  11750. name = script_getstr(st,2);
  11751. if( script_hasdata(st,7) ) {// specified part of map
  11752. const char* mapname = script_getstr(st,3);
  11753. int x0 = script_getnum(st,4);
  11754. int y0 = script_getnum(st,5);
  11755. int x1 = script_getnum(st,6);
  11756. int y1 = script_getnum(st,7);
  11757. map_foreachinarea(playBGM_sub, map_mapname2mapid(mapname), x0, y0, x1, y1, BL_PC, name);
  11758. }
  11759. else if( script_hasdata(st,3) ) {// entire map
  11760. const char* mapname = script_getstr(st,3);
  11761. map_foreachinmap(playBGM_sub, map_mapname2mapid(mapname), BL_PC, name);
  11762. }
  11763. else {// entire server
  11764. map_foreachpc(&playBGM_foreachpc_sub, name);
  11765. }
  11766. return SCRIPT_CMD_SUCCESS;
  11767. }
  11768. /*==========================================
  11769. * Play a .wav sound for sd
  11770. *------------------------------------------*/
  11771. BUILDIN_FUNC(soundeffect)
  11772. {
  11773. TBL_PC* sd = script_rid2sd(st);
  11774. const char* name = script_getstr(st,2);
  11775. int type = script_getnum(st,3);
  11776. if(sd) {
  11777. clif_soundeffect(sd,&sd->bl,name,type);
  11778. }
  11779. return SCRIPT_CMD_SUCCESS;
  11780. }
  11781. int soundeffect_sub(struct block_list* bl,va_list ap)
  11782. {
  11783. char* name = va_arg(ap,char*);
  11784. int type = va_arg(ap,int);
  11785. clif_soundeffect((TBL_PC *)bl, bl, name, type);
  11786. return 0;
  11787. }
  11788. /*==========================================
  11789. * Play a sound effect (.wav) on multiple clients
  11790. * soundeffectall "<filepath>",<type>{,"<map name>"}{,<x0>,<y0>,<x1>,<y1>};
  11791. *------------------------------------------*/
  11792. BUILDIN_FUNC(soundeffectall)
  11793. {
  11794. struct block_list* bl;
  11795. const char* name;
  11796. int type;
  11797. bl = (st->rid) ? &(script_rid2sd(st)->bl) : map_id2bl(st->oid);
  11798. if (!bl)
  11799. return SCRIPT_CMD_SUCCESS;
  11800. name = script_getstr(st,2);
  11801. type = script_getnum(st,3);
  11802. //FIXME: enumerating map squares (map_foreach) is slower than enumerating the list of online players (map_foreachpc?) [ultramage]
  11803. if(!script_hasdata(st,4))
  11804. { // area around
  11805. clif_soundeffectall(bl, name, type, AREA);
  11806. }
  11807. else
  11808. if(!script_hasdata(st,5))
  11809. { // entire map
  11810. const char* mapname = script_getstr(st,4);
  11811. map_foreachinmap(soundeffect_sub, map_mapname2mapid(mapname), BL_PC, name, type);
  11812. }
  11813. else
  11814. if(script_hasdata(st,8))
  11815. { // specified part of map
  11816. const char* mapname = script_getstr(st,4);
  11817. int x0 = script_getnum(st,5);
  11818. int y0 = script_getnum(st,6);
  11819. int x1 = script_getnum(st,7);
  11820. int y1 = script_getnum(st,8);
  11821. map_foreachinarea(soundeffect_sub, map_mapname2mapid(mapname), x0, y0, x1, y1, BL_PC, name, type);
  11822. }
  11823. else
  11824. {
  11825. ShowError("buildin_soundeffectall: insufficient arguments for specific area broadcast.\n");
  11826. }
  11827. return SCRIPT_CMD_SUCCESS;
  11828. }
  11829. /*==========================================
  11830. * pet status recovery [Valaris] / Rewritten by [Skotlex]
  11831. *------------------------------------------*/
  11832. BUILDIN_FUNC(petrecovery)
  11833. {
  11834. struct pet_data *pd;
  11835. TBL_PC *sd=script_rid2sd(st);
  11836. int sc;
  11837. if(sd == NULL || sd->pd == NULL)
  11838. return SCRIPT_CMD_FAILURE;
  11839. sc = script_getnum(st,2);
  11840. if (sc <= SC_NONE || sc >= SC_MAX) {
  11841. ShowError("buildin_petrecovery: Invalid SC type: %d\n", sc);
  11842. return SCRIPT_CMD_FAILURE;
  11843. }
  11844. pd = sd->pd;
  11845. if (pd->recovery)
  11846. { //Halt previous bonus
  11847. if (pd->recovery->timer != INVALID_TIMER)
  11848. delete_timer(pd->recovery->timer, pet_recovery_timer);
  11849. } else //Init
  11850. pd->recovery = (struct pet_recovery *)aMalloc(sizeof(struct pet_recovery));
  11851. pd->recovery->type = (sc_type)sc;
  11852. pd->recovery->delay = script_getnum(st,3);
  11853. pd->recovery->timer = INVALID_TIMER;
  11854. return SCRIPT_CMD_SUCCESS;
  11855. }
  11856. /*==========================================
  11857. * pet healing [Valaris] //Rewritten by [Skotlex]
  11858. *------------------------------------------*/
  11859. BUILDIN_FUNC(petheal)
  11860. {
  11861. struct pet_data *pd;
  11862. TBL_PC *sd=script_rid2sd(st);
  11863. if(sd==NULL || sd->pd==NULL)
  11864. return SCRIPT_CMD_SUCCESS;
  11865. pd=sd->pd;
  11866. if (pd->s_skill)
  11867. { //Clear previous skill
  11868. if (pd->s_skill->timer != INVALID_TIMER)
  11869. {
  11870. if (pd->s_skill->id)
  11871. delete_timer(pd->s_skill->timer, pet_skill_support_timer);
  11872. else
  11873. delete_timer(pd->s_skill->timer, pet_heal_timer);
  11874. }
  11875. } else //init memory
  11876. pd->s_skill = (struct pet_skill_support *) aMalloc(sizeof(struct pet_skill_support));
  11877. pd->s_skill->id=0; //This id identifies that it IS petheal rather than pet_skillsupport
  11878. //Use the lv as the amount to heal
  11879. pd->s_skill->lv=script_getnum(st,2);
  11880. pd->s_skill->delay=script_getnum(st,3);
  11881. pd->s_skill->hp=script_getnum(st,4);
  11882. pd->s_skill->sp=script_getnum(st,5);
  11883. //Use delay as initial offset to avoid skill/heal exploits
  11884. if (battle_config.pet_equip_required && pd->pet.equip == 0)
  11885. pd->s_skill->timer = INVALID_TIMER;
  11886. else
  11887. pd->s_skill->timer = add_timer(gettick()+pd->s_skill->delay*1000,pet_heal_timer,sd->bl.id,0);
  11888. return SCRIPT_CMD_SUCCESS;
  11889. }
  11890. /*==========================================
  11891. * pet attack skills [Valaris] //Rewritten by [Skotlex]
  11892. *------------------------------------------*/
  11893. /// petskillattack <skill id>,<level>,<rate>,<bonusrate>
  11894. /// petskillattack "<skill name>",<level>,<rate>,<bonusrate>
  11895. BUILDIN_FUNC(petskillattack)
  11896. {
  11897. struct pet_data *pd;
  11898. struct script_data *data;
  11899. TBL_PC *sd = script_rid2sd(st);
  11900. int id = 0;
  11901. if(sd == NULL || sd->pd == NULL)
  11902. return SCRIPT_CMD_FAILURE;
  11903. data = script_getdata(st, 2);
  11904. get_val(st, data);
  11905. id = (data_isstring(data) ? skill_name2id(script_getstr(st,2)) : skill_get_index(script_getnum(st,2)));
  11906. if (!id) {
  11907. ShowError("buildin_petskillattack: Invalid skill defined!\n");
  11908. return SCRIPT_CMD_FAILURE;
  11909. }
  11910. pd = sd->pd;
  11911. if (pd->a_skill == NULL)
  11912. pd->a_skill = (struct pet_skill_attack *)aMalloc(sizeof(struct pet_skill_attack));
  11913. pd->a_skill->id = id;
  11914. pd->a_skill->damage = 0;
  11915. pd->a_skill->lv = (unsigned short)min(script_getnum(st,3), skill_get_max(pd->a_skill->id));
  11916. pd->a_skill->div_ = 0;
  11917. pd->a_skill->rate = script_getnum(st,4);
  11918. pd->a_skill->bonusrate = script_getnum(st,5);
  11919. return SCRIPT_CMD_SUCCESS;
  11920. }
  11921. /*==========================================
  11922. * pet attack skills [Valaris]
  11923. *------------------------------------------*/
  11924. /// petskillattack2 <skill id>,<damage>,<div>,<rate>,<bonusrate>
  11925. /// petskillattack2 "<skill name>",<damage>,<div>,<rate>,<bonusrate>
  11926. BUILDIN_FUNC(petskillattack2)
  11927. {
  11928. struct pet_data *pd;
  11929. struct script_data *data;
  11930. TBL_PC *sd = script_rid2sd(st);
  11931. int id = 0;
  11932. if(sd == NULL || sd->pd == NULL)
  11933. return SCRIPT_CMD_FAILURE;
  11934. data = script_getdata(st, 2);
  11935. get_val(st, data);
  11936. id = (data_isstring(data) ? skill_name2id(script_getstr(st,2)) : skill_get_index(script_getnum(st,2)));
  11937. if (!id) {
  11938. ShowError("buildin_petskillattack2: Invalid skill defined!\n");
  11939. return SCRIPT_CMD_FAILURE;
  11940. }
  11941. pd = sd->pd;
  11942. if (pd->a_skill == NULL)
  11943. pd->a_skill = (struct pet_skill_attack *)aMalloc(sizeof(struct pet_skill_attack));
  11944. pd->a_skill->id = id;
  11945. pd->a_skill->damage = script_getnum(st,3); // Fixed damage
  11946. pd->a_skill->lv = (unsigned short)skill_get_max(pd->a_skill->id); // Adjust to max skill level
  11947. pd->a_skill->div_ = script_getnum(st,4);
  11948. pd->a_skill->rate = script_getnum(st,5);
  11949. pd->a_skill->bonusrate = script_getnum(st,6);
  11950. return SCRIPT_CMD_SUCCESS;
  11951. }
  11952. /*==========================================
  11953. * pet support skills [Skotlex]
  11954. *------------------------------------------*/
  11955. /// petskillsupport <skill id>,<level>,<delay>,<hp>,<sp>
  11956. /// petskillsupport "<skill name>",<level>,<delay>,<hp>,<sp>
  11957. BUILDIN_FUNC(petskillsupport)
  11958. {
  11959. struct pet_data *pd;
  11960. struct script_data *data;
  11961. TBL_PC *sd = script_rid2sd(st);
  11962. int id = 0;
  11963. if(sd == NULL || sd->pd == NULL)
  11964. return SCRIPT_CMD_FAILURE;
  11965. data = script_getdata(st, 2);
  11966. get_val(st, data);
  11967. id = (data_isstring(data) ? skill_name2id(script_getstr(st,2)) : skill_get_index(script_getnum(st,2)));
  11968. if (!id) {
  11969. ShowError("buildin_petskillsupport: Invalid skill defined!\n");
  11970. return SCRIPT_CMD_FAILURE;
  11971. }
  11972. pd = sd->pd;
  11973. if (pd->s_skill)
  11974. { //Clear previous skill
  11975. if (pd->s_skill->timer != INVALID_TIMER)
  11976. {
  11977. if (pd->s_skill->id)
  11978. delete_timer(pd->s_skill->timer, pet_skill_support_timer);
  11979. else
  11980. delete_timer(pd->s_skill->timer, pet_heal_timer);
  11981. }
  11982. } else //init memory
  11983. pd->s_skill = (struct pet_skill_support *) aMalloc(sizeof(struct pet_skill_support));
  11984. pd->s_skill->id = id;
  11985. pd->s_skill->lv = script_getnum(st,3);
  11986. pd->s_skill->delay = script_getnum(st,4);
  11987. pd->s_skill->hp = script_getnum(st,5);
  11988. pd->s_skill->sp = script_getnum(st,6);
  11989. //Use delay as initial offset to avoid skill/heal exploits
  11990. if (battle_config.pet_equip_required && pd->pet.equip == 0)
  11991. pd->s_skill->timer = INVALID_TIMER;
  11992. else
  11993. pd->s_skill->timer = add_timer(gettick()+pd->s_skill->delay*1000,pet_skill_support_timer,sd->bl.id,0);
  11994. return SCRIPT_CMD_SUCCESS;
  11995. }
  11996. /*==========================================
  11997. * Scripted skill effects [Celest]
  11998. *------------------------------------------*/
  11999. /// skilleffect <skill id>,<level>
  12000. /// skilleffect "<skill name>",<level>
  12001. BUILDIN_FUNC(skilleffect)
  12002. {
  12003. TBL_PC *sd;
  12004. uint16 skill_id, skill_lv;
  12005. struct script_data *data = script_getdata(st, 2);
  12006. get_val(st, data); // Convert into value in case of a variable
  12007. skill_id = ( data_isstring(data) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  12008. skill_lv = script_getnum(st,3);
  12009. sd = script_rid2sd(st);
  12010. /* Ensure we're standing because the following packet causes the client to virtually set the char to stand,
  12011. * which leaves the server thinking it still is sitting. */
  12012. if( pc_issit(sd) && pc_setstand(sd, false) ) {
  12013. skill_sit(sd, 0);
  12014. clif_standing(&sd->bl);
  12015. }
  12016. clif_skill_nodamage(&sd->bl,&sd->bl,skill_id,skill_lv,1);
  12017. return SCRIPT_CMD_SUCCESS;
  12018. }
  12019. /*==========================================
  12020. * NPC skill effects [Valaris]
  12021. *------------------------------------------*/
  12022. /// npcskilleffect <skill id>,<level>,<x>,<y>
  12023. /// npcskilleffect "<skill name>",<level>,<x>,<y>
  12024. BUILDIN_FUNC(npcskilleffect)
  12025. {
  12026. struct block_list *bl= map_id2bl(st->oid);
  12027. uint16 skill_id, skill_lv;
  12028. int x, y;
  12029. struct script_data *data = script_getdata(st, 2);
  12030. get_val(st, data); // Convert into value in case of a variable
  12031. skill_id=( data_isstring(data) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  12032. skill_lv=script_getnum(st,3);
  12033. x=script_getnum(st,4);
  12034. y=script_getnum(st,5);
  12035. if (bl)
  12036. clif_skill_poseffect(bl,skill_id,skill_lv,x,y,gettick());
  12037. return SCRIPT_CMD_SUCCESS;
  12038. }
  12039. /*==========================================
  12040. * Special effects [Valaris]
  12041. *------------------------------------------*/
  12042. BUILDIN_FUNC(specialeffect)
  12043. {
  12044. struct block_list *bl=map_id2bl(st->oid);
  12045. int type = script_getnum(st,2);
  12046. enum send_target target = script_hasdata(st,3) ? (send_target)script_getnum(st,3) : AREA;
  12047. if(bl==NULL)
  12048. return SCRIPT_CMD_SUCCESS;
  12049. if( script_hasdata(st,4) )
  12050. {
  12051. TBL_NPC *nd = npc_name2id(script_getstr(st,4));
  12052. if(nd)
  12053. clif_specialeffect(&nd->bl, type, target);
  12054. }
  12055. else
  12056. {
  12057. if (target == SELF) {
  12058. TBL_PC *sd=script_rid2sd(st);
  12059. if (sd)
  12060. clif_specialeffect_single(bl,type,sd->fd);
  12061. } else {
  12062. clif_specialeffect(bl, type, target);
  12063. }
  12064. }
  12065. return SCRIPT_CMD_SUCCESS;
  12066. }
  12067. BUILDIN_FUNC(specialeffect2)
  12068. {
  12069. TBL_PC *sd=script_rid2sd(st);
  12070. int type = script_getnum(st,2);
  12071. enum send_target target = script_hasdata(st,3) ? (send_target)script_getnum(st,3) : AREA;
  12072. if( script_hasdata(st,4) )
  12073. sd = map_nick2sd(script_getstr(st,4));
  12074. if (sd)
  12075. clif_specialeffect(&sd->bl, type, target);
  12076. return SCRIPT_CMD_SUCCESS;
  12077. }
  12078. /**
  12079. * nude({<char_id>});
  12080. * @author [Valaris]
  12081. **/
  12082. BUILDIN_FUNC(nude)
  12083. {
  12084. TBL_PC *sd;
  12085. int i, calcflag = 0;
  12086. if (!script_charid2sd(2,sd))
  12087. return SCRIPT_CMD_FAILURE;
  12088. for( i = 0 ; i < EQI_MAX; i++ ) {
  12089. if( sd->equip_index[ i ] >= 0 ) {
  12090. if( !calcflag )
  12091. calcflag = 1;
  12092. pc_unequipitem( sd , sd->equip_index[ i ] , 2);
  12093. }
  12094. }
  12095. if( calcflag )
  12096. status_calc_pc(sd,SCO_NONE);
  12097. return SCRIPT_CMD_SUCCESS;
  12098. }
  12099. int atcommand_sub(struct script_state* st,int type) {
  12100. TBL_PC *sd, dummy_sd;
  12101. int fd;
  12102. const char *cmd;
  12103. cmd = script_getstr(st,2);
  12104. if (st->rid) {
  12105. sd = script_rid2sd(st);
  12106. fd = sd->fd;
  12107. } else { //Use a dummy character.
  12108. sd = &dummy_sd;
  12109. fd = 0;
  12110. memset(&dummy_sd, 0, sizeof(TBL_PC));
  12111. if (st->oid) {
  12112. struct block_list* bl = map_id2bl(st->oid);
  12113. memcpy(&dummy_sd.bl, bl, sizeof(struct block_list));
  12114. if (bl->type == BL_NPC)
  12115. safestrncpy(dummy_sd.status.name, ((TBL_NPC*)bl)->name, NAME_LENGTH);
  12116. sd->mapindex = (bl->m > 0) ? bl->m : mapindex_name2id(map_default.mapname);
  12117. }
  12118. // Init Group ID, Level, & permissions
  12119. sd->group_id = sd->group_level = 99;
  12120. sd->permissions |= PC_PERM_ALLPERMISSION;
  12121. }
  12122. if (!is_atcommand(fd, sd, cmd, type)) {
  12123. ShowWarning("buildin_atcommand: failed to execute command '%s'\n", cmd);
  12124. script_reportsrc(st);
  12125. return SCRIPT_CMD_FAILURE;
  12126. }
  12127. return SCRIPT_CMD_SUCCESS;
  12128. }
  12129. /*==========================================
  12130. * gmcommand [MouseJstr]
  12131. *------------------------------------------*/
  12132. BUILDIN_FUNC(atcommand) {
  12133. return atcommand_sub(st,0);
  12134. }
  12135. /** Displays a message for the player only (like system messages like "you got an apple" )
  12136. * dispbottom("<message>"{,<color>{,<char_id>}})
  12137. * @param message
  12138. * @param color Hex color default (Green)
  12139. */
  12140. BUILDIN_FUNC(dispbottom)
  12141. {
  12142. TBL_PC *sd;
  12143. int color = 0;
  12144. const char *message;
  12145. if (!script_charid2sd(4,sd))
  12146. return SCRIPT_CMD_FAILURE;
  12147. message = script_getstr(st,2);
  12148. if (script_hasdata(st,3))
  12149. color = script_getnum(st,3); // <color>
  12150. if(sd) {
  12151. if (script_hasdata(st,3))
  12152. clif_messagecolor2(sd, color, message); // [Napster]
  12153. else
  12154. clif_disp_onlyself(sd, message, (int)strlen(message));
  12155. }
  12156. return SCRIPT_CMD_SUCCESS;
  12157. }
  12158. /*===================================
  12159. * Heal portion of recovery command
  12160. *-----------------------------------*/
  12161. int recovery_sub(struct map_session_data* sd, int revive)
  12162. {
  12163. if(revive&(1|4) && pc_isdead(sd)) {
  12164. status_revive(&sd->bl, 100, 100);
  12165. clif_displaymessage(sd->fd,msg_txt(sd,16)); // You've been revived!
  12166. clif_specialeffect(&sd->bl, 77, AREA);
  12167. } else if(revive&(1|2) && !pc_isdead(sd)) {
  12168. status_percent_heal(&sd->bl, 100, 100);
  12169. clif_displaymessage(sd->fd,msg_txt(sd,680)); // You have been recovered!
  12170. }
  12171. return SCRIPT_CMD_SUCCESS;
  12172. }
  12173. /*=========================================================================
  12174. * Fully Recover a Character's HP/SP - [Capuche] & [Akinari]
  12175. * recovery <type>{,<option>,<revive_flag>{,<map name>}};
  12176. * <type> determines <option>:
  12177. * 0 : char_id
  12178. * 1 : party_id
  12179. * 2 : guild_id
  12180. * 3 : map_name
  12181. * 4 : all characters
  12182. * <revive_flag>:
  12183. * 1 : Revive and heal all players (default)
  12184. * 2 : Heal living players only
  12185. * 4 : Revive dead players only
  12186. * <map name>:
  12187. * for types 1-2 : map_name (null = all maps)
  12188. *-------------------------------------------------------------------------*/
  12189. BUILDIN_FUNC(recovery)
  12190. {
  12191. TBL_PC *sd;
  12192. int map_idx = 0, type = 0, revive = 1;
  12193. type = script_getnum(st,2);
  12194. if(script_hasdata(st,4))
  12195. revive = script_getnum(st,4);
  12196. switch(type) {
  12197. case 0:
  12198. if(script_hasdata(st,3))
  12199. sd=map_charid2sd(script_getnum(st,3));
  12200. else if((sd = script_rid2sd(st)) == NULL)
  12201. return SCRIPT_CMD_SUCCESS; //If we don't have sd by now, bail out
  12202. recovery_sub(sd, revive);
  12203. break;
  12204. case 1:
  12205. {
  12206. struct party_data* p;
  12207. //When no party given, we use invoker party
  12208. int p_id = 0, i;
  12209. if(script_hasdata(st,5)) {//Bad maps shouldn't cause issues
  12210. map_idx = map_mapname2mapid(script_getstr(st,5));
  12211. if(map_idx < 1) { //But we'll check anyways
  12212. ShowDebug("recovery: bad map name given (%s)\n", script_getstr(st,5));
  12213. return SCRIPT_CMD_FAILURE;
  12214. }
  12215. }
  12216. if(script_hasdata(st,3))
  12217. p_id = script_getnum(st,3);
  12218. else if((sd = script_rid2sd(st)))
  12219. p_id = sd->status.party_id;
  12220. p = party_search(p_id);
  12221. if(p == NULL)
  12222. return SCRIPT_CMD_SUCCESS;
  12223. for (i = 0; i < MAX_PARTY; i++) {
  12224. struct map_session_data* pl_sd;
  12225. if((!(pl_sd = p->data[i].sd) || pl_sd->status.party_id != p_id)
  12226. || (map_idx && pl_sd->bl.m != map_idx))
  12227. continue;
  12228. recovery_sub(pl_sd, revive);
  12229. }
  12230. break;
  12231. }
  12232. case 2:
  12233. {
  12234. struct guild* g;
  12235. //When no guild given, we use invoker guild
  12236. int g_id = 0, i;
  12237. if(script_hasdata(st,5)) {//Bad maps shouldn't cause issues
  12238. map_idx = map_mapname2mapid(script_getstr(st,5));
  12239. if(map_idx < 1) { //But we'll check anyways
  12240. ShowDebug("recovery: bad map name given (%s)\n", script_getstr(st,5));
  12241. return SCRIPT_CMD_FAILURE;
  12242. }
  12243. }
  12244. if(script_hasdata(st,3))
  12245. g_id = script_getnum(st,3);
  12246. else if((sd = script_rid2sd(st)))
  12247. g_id = sd->status.guild_id;
  12248. g = guild_search(g_id);
  12249. if(g == NULL)
  12250. return SCRIPT_CMD_SUCCESS;
  12251. for (i = 0; i < MAX_GUILD; i++) {
  12252. struct map_session_data* pl_sd;
  12253. if((!(pl_sd = g->member[i].sd) || pl_sd->status.guild_id != g_id)
  12254. || (map_idx && pl_sd->bl.m != map_idx))
  12255. continue;
  12256. recovery_sub(pl_sd, revive);
  12257. }
  12258. break;
  12259. }
  12260. case 3:
  12261. if(script_hasdata(st,3))
  12262. map_idx = map_mapname2mapid(script_getstr(st,3));
  12263. else if((sd = script_rid2sd(st)))
  12264. map_idx = sd->bl.m;
  12265. if(map_idx < 1)
  12266. return SCRIPT_CMD_FAILURE; //No sd and no map given - return
  12267. case 4:
  12268. {
  12269. struct s_mapiterator *iter;
  12270. struct script_data *data = script_getdata(st, 3);
  12271. get_val(st, data); // Convert into value in case of a variable
  12272. if(script_hasdata(st,3) && !data_isstring(data))
  12273. revive = script_getnum(st,3); // recovery 4,<revive_flag>;
  12274. iter = mapit_getallusers();
  12275. for (sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); sd = (TBL_PC*)mapit_next(iter)) {
  12276. if(type == 3 && sd->bl.m != map_idx)
  12277. continue;
  12278. recovery_sub(sd, revive);
  12279. }
  12280. mapit_free(iter);
  12281. break;
  12282. }
  12283. default:
  12284. ShowWarning("script: buildin_recovery: Invalid type %d\n", type);
  12285. script_pushint(st,-1);
  12286. return SCRIPT_CMD_FAILURE;
  12287. }
  12288. script_pushint(st,1); //Successfully executed without errors
  12289. return SCRIPT_CMD_SUCCESS;
  12290. }
  12291. /*==========================================
  12292. * Get your pet info: getpetinfo <type>{,<char_id>}
  12293. * n -> 0:pet_id 1:pet_class 2:pet_name
  12294. * 3:friendly 4:hungry, 5: rename flag.6:level,
  12295. * 7:game id
  12296. *------------------------------------------*/
  12297. BUILDIN_FUNC(getpetinfo)
  12298. {
  12299. TBL_PC *sd=script_rid2sd(st);
  12300. TBL_PET *pd;
  12301. int type = script_getnum(st,2);
  12302. if (!script_charid2sd(3, sd) || !(pd = sd->pd)) {
  12303. if (type == 2)
  12304. script_pushconststr(st,"null");
  12305. else
  12306. script_pushint(st,0);
  12307. return SCRIPT_CMD_SUCCESS;
  12308. }
  12309. switch(type){
  12310. case PETINFO_ID: script_pushint(st,pd->pet.pet_id); break;
  12311. case PETINFO_CLASS: script_pushint(st,pd->pet.class_); break;
  12312. case PETINFO_NAME: script_pushstrcopy(st,pd->pet.name); break;
  12313. case PETINFO_INTIMATE: script_pushint(st,pd->pet.intimate); break;
  12314. case PETINFO_HUNGRY: script_pushint(st,pd->pet.hungry); break;
  12315. case PETINFO_RENAMED: script_pushint(st,pd->pet.rename_flag); break;
  12316. case PETINFO_LEVEL: script_pushint(st,(int)pd->pet.level); break;
  12317. case PETINFO_BLOCKID: script_pushint(st,pd->bl.id); break;
  12318. default:
  12319. script_pushint(st,0);
  12320. break;
  12321. }
  12322. return SCRIPT_CMD_SUCCESS;
  12323. }
  12324. /*==========================================
  12325. * Get your homunculus info: gethominfo <type>{,<char_id>};
  12326. * type -> 0:hom_id 1:class 2:name
  12327. * 3:friendly 4:hungry, 5: rename flag.
  12328. * 6: level, 7: game id
  12329. *------------------------------------------*/
  12330. BUILDIN_FUNC(gethominfo)
  12331. {
  12332. TBL_PC *sd=script_rid2sd(st);
  12333. TBL_HOM *hd;
  12334. int type=script_getnum(st,2);
  12335. if (!script_charid2sd(3, sd) || !(hd = sd->hd)) {
  12336. if (type == 2)
  12337. script_pushconststr(st,"null");
  12338. else
  12339. script_pushint(st,0);
  12340. return SCRIPT_CMD_SUCCESS;
  12341. }
  12342. switch(type){
  12343. case 0: script_pushint(st,hd->homunculus.hom_id); break;
  12344. case 1: script_pushint(st,hd->homunculus.class_); break;
  12345. case 2: script_pushstrcopy(st,hd->homunculus.name); break;
  12346. case 3: script_pushint(st,hd->homunculus.intimacy); break;
  12347. case 4: script_pushint(st,hd->homunculus.hunger); break;
  12348. case 5: script_pushint(st,hd->homunculus.rename_flag); break;
  12349. case 6: script_pushint(st,hd->homunculus.level); break;
  12350. case 7: script_pushint(st,hd->bl.id); break;
  12351. default:
  12352. script_pushint(st,0);
  12353. break;
  12354. }
  12355. return SCRIPT_CMD_SUCCESS;
  12356. }
  12357. /// Retrieves information about character's mercenary
  12358. /// getmercinfo <type>[,<char id>];
  12359. BUILDIN_FUNC(getmercinfo)
  12360. {
  12361. int type;
  12362. struct map_session_data* sd;
  12363. struct mercenary_data* md;
  12364. type = script_getnum(st,2);
  12365. if( script_hasdata(st,3) )
  12366. {
  12367. int char_id = script_getnum(st,3);
  12368. if( ( sd = map_charid2sd(char_id) ) == NULL )
  12369. {
  12370. ShowError("buildin_getmercinfo: No such character (char_id=%d).\n", char_id);
  12371. script_pushnil(st);
  12372. return SCRIPT_CMD_FAILURE;
  12373. }
  12374. }
  12375. else
  12376. {
  12377. if( ( sd = script_rid2sd(st) ) == NULL )
  12378. {
  12379. script_pushnil(st);
  12380. return SCRIPT_CMD_SUCCESS;
  12381. }
  12382. }
  12383. md = ( sd->status.mer_id && sd->md ) ? sd->md : NULL;
  12384. switch( type )
  12385. {
  12386. case 0: script_pushint(st,md ? md->mercenary.mercenary_id : 0); break;
  12387. case 1: script_pushint(st,md ? md->mercenary.class_ : 0); break;
  12388. case 2:
  12389. if( md )
  12390. script_pushstrcopy(st,md->db->name);
  12391. else
  12392. script_pushconststr(st,"");
  12393. break;
  12394. case 3: script_pushint(st,md ? mercenary_get_faith(md) : 0); break;
  12395. case 4: script_pushint(st,md ? mercenary_get_calls(md) : 0); break;
  12396. case 5: script_pushint(st,md ? md->mercenary.kill_count : 0); break;
  12397. case 6: script_pushint(st,md ? mercenary_get_lifetime(md) : 0); break;
  12398. case 7: script_pushint(st,md ? md->db->lv : 0); break;
  12399. case 8: script_pushint(st,md ? md->bl.id : 0); break;
  12400. default:
  12401. ShowError("buildin_getmercinfo: Invalid type %d (char_id=%d).\n", type, sd->status.char_id);
  12402. script_pushnil(st);
  12403. return SCRIPT_CMD_FAILURE;
  12404. }
  12405. return SCRIPT_CMD_SUCCESS;
  12406. }
  12407. /*==========================================
  12408. * Shows wether your inventory(and equips) contain
  12409. selected card or not.
  12410. checkequipedcard(4001);
  12411. *------------------------------------------*/
  12412. BUILDIN_FUNC(checkequipedcard)
  12413. {
  12414. TBL_PC *sd=script_rid2sd(st);
  12415. if(sd){
  12416. int n,i,c=0;
  12417. c=script_getnum(st,2);
  12418. for(i=0;i<MAX_INVENTORY;i++){
  12419. if(sd->status.inventory[i].nameid > 0 && sd->status.inventory[i].amount && sd->inventory_data[i]){
  12420. if (itemdb_isspecial(sd->status.inventory[i].card[0]))
  12421. continue;
  12422. for(n=0;n<sd->inventory_data[i]->slot;n++){
  12423. if(sd->status.inventory[i].card[n]==c){
  12424. script_pushint(st,1);
  12425. return SCRIPT_CMD_SUCCESS;
  12426. }
  12427. }
  12428. }
  12429. }
  12430. }
  12431. script_pushint(st,0);
  12432. return SCRIPT_CMD_SUCCESS;
  12433. }
  12434. BUILDIN_FUNC(jump_zero)
  12435. {
  12436. int sel;
  12437. sel=script_getnum(st,2);
  12438. if(!sel) {
  12439. int pos;
  12440. if( !data_islabel(script_getdata(st,3)) ){
  12441. ShowError("script: jump_zero: not label !\n");
  12442. st->state=END;
  12443. return SCRIPT_CMD_FAILURE;
  12444. }
  12445. pos=script_getnum(st,3);
  12446. st->pos=pos;
  12447. st->state=GOTO;
  12448. }
  12449. return SCRIPT_CMD_SUCCESS;
  12450. }
  12451. /*==========================================
  12452. * movenpc [MouseJstr]
  12453. *------------------------------------------*/
  12454. BUILDIN_FUNC(movenpc)
  12455. {
  12456. TBL_NPC *nd = NULL;
  12457. const char *npc;
  12458. int x,y;
  12459. npc = script_getstr(st,2);
  12460. x = script_getnum(st,3);
  12461. y = script_getnum(st,4);
  12462. if ((nd = npc_name2id(npc)) == NULL){
  12463. ShowError("script: movenpc: NPC with ID '%s' was not found!\n", npc );
  12464. return -1;
  12465. }
  12466. if (script_hasdata(st,5))
  12467. nd->ud.dir = script_getnum(st,5) % 8;
  12468. npc_movenpc(nd, x, y);
  12469. return SCRIPT_CMD_SUCCESS;
  12470. }
  12471. /*==========================================
  12472. * message [MouseJstr]
  12473. *------------------------------------------*/
  12474. BUILDIN_FUNC(message)
  12475. {
  12476. const char *msg,*player;
  12477. TBL_PC *pl_sd = NULL;
  12478. player = script_getstr(st,2);
  12479. msg = script_getstr(st,3);
  12480. if((pl_sd=map_nick2sd((char *) player)) == NULL)
  12481. return SCRIPT_CMD_SUCCESS;
  12482. clif_displaymessage(pl_sd->fd, msg);
  12483. return SCRIPT_CMD_SUCCESS;
  12484. }
  12485. /*==========================================
  12486. * npctalk (sends message to surrounding area)
  12487. *------------------------------------------*/
  12488. BUILDIN_FUNC(npctalk)
  12489. {
  12490. struct npc_data* nd = (struct npc_data *)map_id2bl(st->oid);
  12491. const char* str = script_getstr(st,2);
  12492. if (nd) {
  12493. char message[256];
  12494. safesnprintf(message, sizeof(message), "%s", str);
  12495. clif_disp_overhead(&nd->bl, message);
  12496. }
  12497. return SCRIPT_CMD_SUCCESS;
  12498. }
  12499. // change npc walkspeed [Valaris]
  12500. BUILDIN_FUNC(npcspeed)
  12501. {
  12502. struct npc_data* nd;
  12503. int speed;
  12504. speed = script_getnum(st,2);
  12505. nd =(struct npc_data *)map_id2bl(st->oid);
  12506. if( nd ) {
  12507. nd->speed = speed;
  12508. nd->ud.state.speed_changed = 1;
  12509. }
  12510. return SCRIPT_CMD_SUCCESS;
  12511. }
  12512. // make an npc walk to a position [Valaris]
  12513. BUILDIN_FUNC(npcwalkto)
  12514. {
  12515. struct npc_data *nd=(struct npc_data *)map_id2bl(st->oid);
  12516. int x=0,y=0;
  12517. x=script_getnum(st,2);
  12518. y=script_getnum(st,3);
  12519. if(nd) {
  12520. if (!nd->status.hp)
  12521. status_calc_npc(nd, SCO_FIRST);
  12522. else
  12523. status_calc_npc(nd, SCO_NONE);
  12524. unit_walktoxy(&nd->bl,x,y,0);
  12525. }
  12526. return SCRIPT_CMD_SUCCESS;
  12527. }
  12528. // stop an npc's movement [Valaris]
  12529. BUILDIN_FUNC(npcstop)
  12530. {
  12531. struct npc_data *nd=(struct npc_data *)map_id2bl(st->oid);
  12532. if(nd) {
  12533. unit_stop_walking(&nd->bl,1|4);
  12534. }
  12535. return SCRIPT_CMD_SUCCESS;
  12536. }
  12537. /**
  12538. * getlook(<type>{,<char_id>})
  12539. **/
  12540. BUILDIN_FUNC(getlook)
  12541. {
  12542. int type,val;
  12543. TBL_PC *sd;
  12544. if (!script_charid2sd(3,sd)) {
  12545. script_pushint(st,-1);
  12546. return SCRIPT_CMD_FAILURE;
  12547. }
  12548. type=script_getnum(st,2);
  12549. val=-1;
  12550. switch(type) {
  12551. // TODO: implement LOOK_BASE as stated in script doc
  12552. case LOOK_HAIR: val=sd->status.hair; break; //1
  12553. case LOOK_WEAPON: val=sd->status.weapon; break; //2
  12554. case LOOK_HEAD_BOTTOM: val=sd->status.head_bottom; break; //3
  12555. case LOOK_HEAD_TOP: val=sd->status.head_top; break; //4
  12556. case LOOK_HEAD_MID: val=sd->status.head_mid; break; //5
  12557. case LOOK_HAIR_COLOR: val=sd->status.hair_color; break; //6
  12558. case LOOK_CLOTHES_COLOR: val=sd->status.clothes_color; break; //7
  12559. case LOOK_SHIELD: val=sd->status.shield; break; //8
  12560. case LOOK_SHOES: break; //9
  12561. case LOOK_ROBE: val=sd->status.robe; break; //12
  12562. case LOOK_BODY2: val=sd->status.body; break; //13
  12563. }
  12564. script_pushint(st,val);
  12565. return SCRIPT_CMD_SUCCESS;
  12566. }
  12567. /**
  12568. * getsavepoint(<information type>{,<char_id>})
  12569. * @param type 0- map name, 1- x, 2- y
  12570. **/
  12571. BUILDIN_FUNC(getsavepoint)
  12572. {
  12573. TBL_PC* sd;
  12574. int type;
  12575. if (!script_charid2sd(3,sd)) {
  12576. script_pushint(st,0);
  12577. return SCRIPT_CMD_SUCCESS;
  12578. }
  12579. type = script_getnum(st,2);
  12580. switch(type) {
  12581. case 0: script_pushstrcopy(st,mapindex_id2name(sd->status.save_point.map)); break;
  12582. case 1: script_pushint(st,sd->status.save_point.x); break;
  12583. case 2: script_pushint(st,sd->status.save_point.y); break;
  12584. default:
  12585. script_pushint(st,0);
  12586. break;
  12587. }
  12588. return SCRIPT_CMD_SUCCESS;
  12589. }
  12590. /**
  12591. * Get position for BL objects.
  12592. * getmapxy(<map name>,<x>,<y>,<type>{,<char name>});
  12593. * @param mapname: String variable for output map name
  12594. * @param x: Integer variable for output coord X
  12595. * @param y: Integer variable for output coord Y
  12596. * @param type: Type of object
  12597. * UNITTYPE_PC - Character coord
  12598. * UNITTYPE_NPC - NPC coord
  12599. * UNITTYPE_PET - Pet coord
  12600. * UNITTYPE_HOM - Homun coord
  12601. * UNITTYPE_MER - Mercenary coord
  12602. * UNITTYPE_ELEM - Elemental coord
  12603. * @param charname: Name object. If empty or "this" use the current object
  12604. * @return 0 - success; -1 - some error, MapName$,MapX,MapY contains unknown value.
  12605. */
  12606. BUILDIN_FUNC(getmapxy)
  12607. {
  12608. struct block_list *bl = NULL;
  12609. TBL_PC *sd=NULL;
  12610. int64 num;
  12611. const char *name;
  12612. char prefix;
  12613. int x,y,type;
  12614. char mapname[MAP_NAME_LENGTH];
  12615. if( !data_isreference(script_getdata(st,2)) ) {
  12616. ShowWarning("script: buildin_getmapxy: mapname value is not a variable.\n");
  12617. script_pushint(st,-1);
  12618. return SCRIPT_CMD_FAILURE;
  12619. }
  12620. if( !data_isreference(script_getdata(st,3)) ) {
  12621. ShowWarning("script: buildin_getmapxy: mapx value is not a variable.\n");
  12622. script_pushint(st,-1);
  12623. return SCRIPT_CMD_FAILURE;
  12624. }
  12625. if( !data_isreference(script_getdata(st,4)) ) {
  12626. ShowWarning("script: buildin_getmapxy: mapy value is not a variable.\n");
  12627. script_pushint(st,-1);
  12628. return SCRIPT_CMD_FAILURE;
  12629. }
  12630. if( !is_string_variable(reference_getname(script_getdata(st, 2))) ) {
  12631. ShowWarning("script: buildin_getmapxy: %s is not a string variable.\n",reference_getname(script_getdata(st, 2)));
  12632. script_pushint(st,-1);
  12633. return SCRIPT_CMD_FAILURE;
  12634. }
  12635. if( is_string_variable(reference_getname(script_getdata(st, 3))) ) {
  12636. ShowWarning("script: buildin_getmapxy: %s is a string variable, should be an INT.\n",reference_getname(script_getdata(st, 3)));
  12637. script_pushint(st,-1);
  12638. return SCRIPT_CMD_FAILURE;
  12639. }
  12640. if( is_string_variable(reference_getname(script_getdata(st, 4))) ) {
  12641. ShowWarning("script: buildin_getmapxy: %s is a string variable, should be an INT.\n",reference_getname(script_getdata(st, 4)));
  12642. script_pushint(st,-1);
  12643. return SCRIPT_CMD_FAILURE;
  12644. }
  12645. // Possible needly check function parameters on C_STR,C_INT,C_INT
  12646. type=script_getnum(st,5);
  12647. switch (type) {
  12648. case UNITTYPE_PC: //Get Character Position
  12649. if( script_hasdata(st,6) )
  12650. sd=map_nick2sd(script_getstr(st,6));
  12651. else
  12652. sd=script_rid2sd(st);
  12653. if (sd)
  12654. bl = &sd->bl;
  12655. break;
  12656. case UNITTYPE_NPC: //Get NPC Position
  12657. if( script_hasdata(st,6) )
  12658. {
  12659. struct npc_data *nd;
  12660. nd=npc_name2id(script_getstr(st,6));
  12661. if (nd)
  12662. bl = &nd->bl;
  12663. } else //In case the origin is not an npc?
  12664. bl=map_id2bl(st->oid);
  12665. break;
  12666. case UNITTYPE_PET: //Get Pet Position
  12667. if(script_hasdata(st,6))
  12668. sd=map_nick2sd(script_getstr(st,6));
  12669. else
  12670. sd=script_rid2sd(st);
  12671. if (sd && sd->pd)
  12672. bl = &sd->pd->bl;
  12673. break;
  12674. case UNITTYPE_HOM: //Get Homun Position
  12675. if(script_hasdata(st,6))
  12676. sd=map_nick2sd(script_getstr(st,6));
  12677. else
  12678. sd=script_rid2sd(st);
  12679. if (sd && sd->hd)
  12680. bl = &sd->hd->bl;
  12681. break;
  12682. case UNITTYPE_MER: //Get Mercenary Position
  12683. if(script_hasdata(st,6))
  12684. sd=map_nick2sd(script_getstr(st,6));
  12685. else
  12686. sd=script_rid2sd(st);
  12687. if (sd && sd->md)
  12688. bl = &sd->md->bl;
  12689. break;
  12690. case UNITTYPE_ELEM: //Get Elemental Position
  12691. if(script_hasdata(st,6))
  12692. sd=map_nick2sd(script_getstr(st,6));
  12693. else
  12694. sd=script_rid2sd(st);
  12695. if (sd && sd->ed)
  12696. bl = &sd->ed->bl;
  12697. break;
  12698. default:
  12699. ShowWarning("script: buildin_getmapxy: Invalid type %d.\n", type);
  12700. script_pushint(st,-1);
  12701. return SCRIPT_CMD_FAILURE;
  12702. }
  12703. if (!bl) { //No object found.
  12704. script_pushint(st,-1);
  12705. return SCRIPT_CMD_SUCCESS;
  12706. }
  12707. x= bl->x;
  12708. y= bl->y;
  12709. safestrncpy(mapname, map[bl->m].name, MAP_NAME_LENGTH);
  12710. //Set MapName$
  12711. num=st->stack->stack_data[st->start+2].u.num;
  12712. name=get_str(script_getvarid(num));
  12713. prefix=*name;
  12714. if(not_server_variable(prefix))
  12715. sd=script_rid2sd(st);
  12716. else
  12717. sd=NULL;
  12718. set_reg(st,sd,num,name,(void*)mapname,script_getref(st,2));
  12719. //Set MapX
  12720. num=st->stack->stack_data[st->start+3].u.num;
  12721. name=get_str(script_getvarid(num));
  12722. prefix=*name;
  12723. if(not_server_variable(prefix))
  12724. sd=script_rid2sd(st);
  12725. else
  12726. sd=NULL;
  12727. set_reg(st,sd,num,name,(void*)__64BPRTSIZE(x),script_getref(st,3));
  12728. //Set MapY
  12729. num=st->stack->stack_data[st->start+4].u.num;
  12730. name=get_str(script_getvarid(num));
  12731. prefix=*name;
  12732. if(not_server_variable(prefix))
  12733. sd=script_rid2sd(st);
  12734. else
  12735. sd=NULL;
  12736. set_reg(st,sd,num,name,(void*)__64BPRTSIZE(y),script_getref(st,4));
  12737. //Return Success value
  12738. script_pushint(st,0);
  12739. return SCRIPT_CMD_SUCCESS;
  12740. }
  12741. /// Returns the map name of given map ID.
  12742. ///
  12743. /// mapid2name <map ID>;
  12744. BUILDIN_FUNC(mapid2name)
  12745. {
  12746. uint16 m = script_getnum(st, 2);
  12747. if (m < 0 || m >= MAX_MAP_PER_SERVER) {
  12748. script_pushstr(st, "");
  12749. return SCRIPT_CMD_FAILURE;
  12750. }
  12751. script_pushstrcopy(st, map_mapid2mapname(m));
  12752. return SCRIPT_CMD_SUCCESS;
  12753. }
  12754. /*==========================================
  12755. * Allows player to write NPC logs (i.e. Bank NPC, etc) [Lupus]
  12756. *------------------------------------------*/
  12757. BUILDIN_FUNC(logmes)
  12758. {
  12759. const char *str;
  12760. TBL_PC* sd;
  12761. sd = script_rid2sd(st);
  12762. if( sd == NULL )
  12763. return SCRIPT_CMD_FAILURE;
  12764. str = script_getstr(st,2);
  12765. log_npc(sd,str);
  12766. return SCRIPT_CMD_SUCCESS;
  12767. }
  12768. BUILDIN_FUNC(summon)
  12769. {
  12770. int _class, timeout=0;
  12771. const char *str,*event="";
  12772. TBL_PC *sd;
  12773. struct mob_data *md;
  12774. int tick = gettick();
  12775. sd=script_rid2sd(st);
  12776. if (!sd)
  12777. return SCRIPT_CMD_SUCCESS;
  12778. str =script_getstr(st,2);
  12779. _class=script_getnum(st,3);
  12780. if( script_hasdata(st,4) )
  12781. timeout=script_getnum(st,4);
  12782. if( script_hasdata(st,5) ){
  12783. event=script_getstr(st,5);
  12784. check_event(st, event);
  12785. }
  12786. clif_skill_poseffect(&sd->bl,AM_CALLHOMUN,1,sd->bl.x,sd->bl.y,tick);
  12787. md = mob_once_spawn_sub(&sd->bl, sd->bl.m, sd->bl.x, sd->bl.y, str, _class, event, SZ_SMALL, AI_NONE);
  12788. if (md) {
  12789. md->master_id=sd->bl.id;
  12790. md->special_state.ai = AI_ATTACK;
  12791. if( md->deletetimer != INVALID_TIMER )
  12792. delete_timer(md->deletetimer, mob_timer_delete);
  12793. md->deletetimer = add_timer(tick+(timeout>0?timeout:60000),mob_timer_delete,md->bl.id,0);
  12794. mob_spawn (md); //Now it is ready for spawning.
  12795. clif_specialeffect(&md->bl,344,AREA);
  12796. sc_start4(NULL,&md->bl, SC_MODECHANGE, 100, 1, 0, MD_AGGRESSIVE, 0, 60000);
  12797. }
  12798. script_pushint(st, md->bl.id);
  12799. return SCRIPT_CMD_SUCCESS;
  12800. }
  12801. /*==========================================
  12802. * Checks whether it is daytime/nighttime
  12803. *------------------------------------------*/
  12804. BUILDIN_FUNC(isnight)
  12805. {
  12806. script_pushint(st,(night_flag == 1));
  12807. return SCRIPT_CMD_SUCCESS;
  12808. }
  12809. BUILDIN_FUNC(isday)
  12810. {
  12811. script_pushint(st,(night_flag == 0));
  12812. return SCRIPT_CMD_SUCCESS;
  12813. }
  12814. /*================================================
  12815. * Check how many items/cards in the list are
  12816. * equipped - used for 2/15's cards patch [celest]
  12817. *------------------------------------------------*/
  12818. BUILDIN_FUNC(isequippedcnt)
  12819. {
  12820. TBL_PC *sd;
  12821. int i, id = 1;
  12822. int ret = 0;
  12823. sd = script_rid2sd(st);
  12824. if (!sd) { //If the player is not attached it is a script error anyway... but better prevent the map server from crashing...
  12825. script_pushint(st,0);
  12826. return SCRIPT_CMD_SUCCESS;
  12827. }
  12828. for (i=0; id!=0; i++) {
  12829. short j;
  12830. FETCH (i+2, id) else id = 0;
  12831. if (id <= 0)
  12832. continue;
  12833. for (j=0; j<EQI_MAX; j++) {
  12834. short index = sd->equip_index[j];
  12835. if(index < 0)
  12836. continue;
  12837. if (pc_is_same_equip_index((enum equip_index)j, sd->equip_index, index))
  12838. continue;
  12839. if(!sd->inventory_data[index])
  12840. continue;
  12841. if (itemdb_type(id) != IT_CARD) { //No card. Count amount in inventory.
  12842. if (sd->inventory_data[index]->nameid == id)
  12843. ret+= sd->status.inventory[index].amount;
  12844. } else { //Count cards.
  12845. short k;
  12846. if (itemdb_isspecial(sd->status.inventory[index].card[0]))
  12847. continue; //No cards
  12848. for(k=0; k<sd->inventory_data[index]->slot; k++) {
  12849. if (sd->status.inventory[index].card[k] == id)
  12850. ret++; //[Lupus]
  12851. }
  12852. }
  12853. }
  12854. }
  12855. script_pushint(st,ret);
  12856. return SCRIPT_CMD_SUCCESS;
  12857. }
  12858. /*================================================
  12859. * Check whether another card has been
  12860. * equipped - used for 2/15's cards patch [celest]
  12861. * -- Items checked cannot be reused in another
  12862. * card set to prevent exploits
  12863. *------------------------------------------------*/
  12864. BUILDIN_FUNC(isequipped)
  12865. {
  12866. TBL_PC *sd;
  12867. int i, id = 1;
  12868. int ret = -1;
  12869. //Original hash to reverse it when full check fails.
  12870. unsigned int setitem_hash = 0, setitem_hash2 = 0;
  12871. sd = script_rid2sd(st);
  12872. if (!sd) { //If the player is not attached it is a script error anyway... but better prevent the map server from crashing...
  12873. script_pushint(st,0);
  12874. return SCRIPT_CMD_SUCCESS;
  12875. }
  12876. setitem_hash = sd->bonus.setitem_hash;
  12877. setitem_hash2 = sd->bonus.setitem_hash2;
  12878. for (i=0; id!=0; i++) {
  12879. int flag = 0;
  12880. short j;
  12881. FETCH (i+2, id) else id = 0;
  12882. if (id <= 0)
  12883. continue;
  12884. for (j=0; j<EQI_MAX; j++) {
  12885. short index = sd->equip_index[j];
  12886. if(index < 0)
  12887. continue;
  12888. if (pc_is_same_equip_index((enum equip_index)i, sd->equip_index, index))
  12889. continue;
  12890. if(!sd->inventory_data[index])
  12891. continue;
  12892. if (itemdb_type(id) != IT_CARD) {
  12893. if (sd->inventory_data[index]->nameid != id)
  12894. continue;
  12895. flag = 1;
  12896. break;
  12897. } else { //Cards
  12898. short k;
  12899. if (sd->inventory_data[index]->slot == 0 ||
  12900. itemdb_isspecial(sd->status.inventory[index].card[0]))
  12901. continue;
  12902. for (k = 0; k < sd->inventory_data[index]->slot; k++)
  12903. { //New hash system which should support up to 4 slots on any equipment. [Skotlex]
  12904. unsigned int hash = 0;
  12905. if (sd->status.inventory[index].card[k] != id)
  12906. continue;
  12907. hash = 1<<((j<5?j:j-5)*4 + k);
  12908. // check if card is already used by another set
  12909. if ( ( j < 5 ? sd->bonus.setitem_hash : sd->bonus.setitem_hash2 ) & hash)
  12910. continue;
  12911. // We have found a match
  12912. flag = 1;
  12913. // Set hash so this card cannot be used by another
  12914. if (j<5)
  12915. sd->bonus.setitem_hash |= hash;
  12916. else
  12917. sd->bonus.setitem_hash2 |= hash;
  12918. break;
  12919. }
  12920. }
  12921. if (flag) break; //Card found
  12922. }
  12923. if (ret == -1)
  12924. ret = flag;
  12925. else
  12926. ret &= flag;
  12927. if (!ret) break;
  12928. }
  12929. if (!ret) {//When check fails, restore original hash values. [Skotlex]
  12930. sd->bonus.setitem_hash = setitem_hash;
  12931. sd->bonus.setitem_hash2 = setitem_hash2;
  12932. }
  12933. script_pushint(st,ret);
  12934. return SCRIPT_CMD_SUCCESS;
  12935. }
  12936. /*================================================
  12937. * Check how many given inserted cards in the CURRENT
  12938. * weapon - used for 2/15's cards patch [Lupus]
  12939. *------------------------------------------------*/
  12940. BUILDIN_FUNC(cardscnt)
  12941. {
  12942. TBL_PC *sd;
  12943. int i, k, id = 1;
  12944. int ret = 0;
  12945. int index;
  12946. sd = script_rid2sd(st);
  12947. for (i=0; id!=0; i++) {
  12948. FETCH (i+2, id) else id = 0;
  12949. if (id <= 0)
  12950. continue;
  12951. index = current_equip_item_index; //we get CURRENT WEAPON inventory index from status.c [Lupus]
  12952. if(index < 0) continue;
  12953. if(!sd->inventory_data[index])
  12954. continue;
  12955. if(itemdb_type(id) != IT_CARD) {
  12956. if (sd->inventory_data[index]->nameid == id)
  12957. ret+= sd->status.inventory[index].amount;
  12958. } else {
  12959. if (itemdb_isspecial(sd->status.inventory[index].card[0]))
  12960. continue;
  12961. for(k=0; k<sd->inventory_data[index]->slot; k++) {
  12962. if (sd->status.inventory[index].card[k] == id)
  12963. ret++;
  12964. }
  12965. }
  12966. }
  12967. script_pushint(st,ret);
  12968. // script_pushint(st,current_equip_item_index);
  12969. return SCRIPT_CMD_SUCCESS;
  12970. }
  12971. /*=======================================================
  12972. * Returns the refined number of the current item, or an
  12973. * item with inventory index specified
  12974. *-------------------------------------------------------*/
  12975. BUILDIN_FUNC(getrefine)
  12976. {
  12977. TBL_PC *sd;
  12978. if ((sd = script_rid2sd(st))!= NULL)
  12979. script_pushint(st,sd->status.inventory[current_equip_item_index].refine);
  12980. else
  12981. script_pushint(st,0);
  12982. return SCRIPT_CMD_SUCCESS;
  12983. }
  12984. /*=======================================================
  12985. * Day/Night controls
  12986. *-------------------------------------------------------*/
  12987. BUILDIN_FUNC(night)
  12988. {
  12989. if (night_flag != 1) map_night_timer(night_timer_tid, 0, 0, 1);
  12990. return SCRIPT_CMD_SUCCESS;
  12991. }
  12992. BUILDIN_FUNC(day)
  12993. {
  12994. if (night_flag != 0) map_day_timer(day_timer_tid, 0, 0, 1);
  12995. return SCRIPT_CMD_SUCCESS;
  12996. }
  12997. /**
  12998. * unequip <equipment slot>{,<char_id>};
  12999. * @author [Spectre]
  13000. **/
  13001. BUILDIN_FUNC(unequip) {
  13002. int pos;
  13003. TBL_PC *sd;
  13004. if (!script_charid2sd(3,sd))
  13005. return SCRIPT_CMD_FAILURE;
  13006. pos = script_getnum(st,2);
  13007. if (pos >= 1 && pos <= ARRAYLENGTH(equip)) {
  13008. short i = pc_checkequip(sd,equip[pos-1]);
  13009. if (i >= 0) {
  13010. pc_unequipitem(sd,i,1|2);
  13011. script_pushint(st, 1);
  13012. return SCRIPT_CMD_SUCCESS;
  13013. }
  13014. }
  13015. ShowError("buildin_unequip: No item equipped at pos %d (CID=%d/AID=%d).\n", pos, sd->status.char_id, sd->status.account_id);
  13016. script_pushint(st, 0);
  13017. return SCRIPT_CMD_FAILURE;
  13018. }
  13019. /**
  13020. * equip <item id>{,<char_id>};
  13021. **/
  13022. BUILDIN_FUNC(equip) {
  13023. unsigned short nameid = 0;
  13024. TBL_PC *sd;
  13025. struct item_data *item_data;
  13026. if (!script_charid2sd(3,sd))
  13027. return SCRIPT_CMD_FAILURE;
  13028. nameid = script_getnum(st,2);
  13029. if ((item_data = itemdb_exists(nameid))) {
  13030. int i;
  13031. ARR_FIND( 0, MAX_INVENTORY, i, sd->status.inventory[i].nameid == nameid );
  13032. if (i < MAX_INVENTORY) {
  13033. pc_equipitem(sd,i,item_data->equip);
  13034. script_pushint(st,1);
  13035. return SCRIPT_CMD_SUCCESS;
  13036. }
  13037. }
  13038. ShowError("buildin_equip: Item %hu cannot be equipped\n",nameid);
  13039. script_pushint(st,0);
  13040. return SCRIPT_CMD_FAILURE;
  13041. }
  13042. BUILDIN_FUNC(autoequip)
  13043. {
  13044. unsigned short nameid;
  13045. int flag;
  13046. struct item_data *item_data;
  13047. nameid=script_getnum(st,2);
  13048. flag=script_getnum(st,3);
  13049. if( ( item_data = itemdb_exists(nameid) ) == NULL )
  13050. {
  13051. ShowError("buildin_autoequip: Invalid item '%hu'.\n", nameid);
  13052. return SCRIPT_CMD_FAILURE;
  13053. }
  13054. if( !itemdb_isequip2(item_data) )
  13055. {
  13056. ShowError("buildin_autoequip: Item '%hu' cannot be equipped.\n", nameid);
  13057. return SCRIPT_CMD_FAILURE;
  13058. }
  13059. item_data->flag.autoequip = flag>0?1:0;
  13060. return SCRIPT_CMD_SUCCESS;
  13061. }
  13062. BUILDIN_FUNC(setbattleflag)
  13063. {
  13064. const char *flag, *value;
  13065. flag = script_getstr(st,2);
  13066. value = script_getstr(st,3); // HACK: Retrieve number as string (auto-converted) for battle_set_value
  13067. if (battle_set_value(flag, value) == 0)
  13068. ShowWarning("buildin_setbattleflag: unknown battle_config flag '%s'\n",flag);
  13069. else
  13070. ShowInfo("buildin_setbattleflag: battle_config flag '%s' is now set to '%s'.\n",flag,value);
  13071. return SCRIPT_CMD_SUCCESS;
  13072. }
  13073. BUILDIN_FUNC(getbattleflag)
  13074. {
  13075. const char *flag;
  13076. flag = script_getstr(st,2);
  13077. script_pushint(st,battle_get_value(flag));
  13078. return SCRIPT_CMD_SUCCESS;
  13079. }
  13080. //=======================================================
  13081. // strlen [Valaris]
  13082. //-------------------------------------------------------
  13083. BUILDIN_FUNC(getstrlen)
  13084. {
  13085. const char *str = script_getstr(st,2);
  13086. int len = (str) ? (int)strlen(str) : 0;
  13087. script_pushint(st,len);
  13088. return SCRIPT_CMD_SUCCESS;
  13089. }
  13090. //=======================================================
  13091. // isalpha [Valaris]
  13092. //-------------------------------------------------------
  13093. BUILDIN_FUNC(charisalpha)
  13094. {
  13095. const char *str=script_getstr(st,2);
  13096. int pos=script_getnum(st,3);
  13097. int val = ( str && pos >= 0 && (unsigned int)pos < strlen(str) ) ? ISALPHA( str[pos] ) != 0 : 0;
  13098. script_pushint(st,val);
  13099. return SCRIPT_CMD_SUCCESS;
  13100. }
  13101. //=======================================================
  13102. // charisupper <str>, <index>
  13103. //-------------------------------------------------------
  13104. BUILDIN_FUNC(charisupper)
  13105. {
  13106. const char *str = script_getstr(st,2);
  13107. int pos = script_getnum(st,3);
  13108. int val = ( str && pos >= 0 && (unsigned int)pos < strlen(str) ) ? ISUPPER( str[pos] ) : 0;
  13109. script_pushint(st,val);
  13110. return SCRIPT_CMD_SUCCESS;
  13111. }
  13112. //=======================================================
  13113. // charislower <str>, <index>
  13114. //-------------------------------------------------------
  13115. BUILDIN_FUNC(charislower)
  13116. {
  13117. const char *str = script_getstr(st,2);
  13118. int pos = script_getnum(st,3);
  13119. int val = ( str && pos >= 0 && (unsigned int)pos < strlen(str) ) ? ISLOWER( str[pos] ) : 0;
  13120. script_pushint(st,val);
  13121. return SCRIPT_CMD_SUCCESS;
  13122. }
  13123. //=======================================================
  13124. // charat <str>, <index>
  13125. //-------------------------------------------------------
  13126. BUILDIN_FUNC(charat) {
  13127. const char *str = script_getstr(st,2);
  13128. int pos = script_getnum(st,3);
  13129. if( pos >= 0 && (unsigned int)pos < strlen(str) ) {
  13130. char output[2];
  13131. output[0] = str[pos];
  13132. output[1] = '\0';
  13133. script_pushstrcopy(st, output);
  13134. } else
  13135. script_pushconststr(st, "");
  13136. return SCRIPT_CMD_SUCCESS;
  13137. }
  13138. //=======================================================
  13139. // setchar <string>, <char>, <index>
  13140. //-------------------------------------------------------
  13141. BUILDIN_FUNC(setchar)
  13142. {
  13143. const char *str = script_getstr(st,2);
  13144. const char *c = script_getstr(st,3);
  13145. int index = script_getnum(st,4);
  13146. char *output = aStrdup(str);
  13147. if(index >= 0 && index < strlen(output))
  13148. output[index] = *c;
  13149. script_pushstr(st, output);
  13150. return SCRIPT_CMD_SUCCESS;
  13151. }
  13152. //=======================================================
  13153. // insertchar <string>, <char>, <index>
  13154. //-------------------------------------------------------
  13155. BUILDIN_FUNC(insertchar)
  13156. {
  13157. const char *str = script_getstr(st,2);
  13158. const char *c = script_getstr(st,3);
  13159. int index = script_getnum(st,4);
  13160. char *output;
  13161. size_t len = strlen(str);
  13162. if(index < 0)
  13163. index = 0;
  13164. else if(index > len)
  13165. index = len;
  13166. output = (char*)aMalloc(len + 2);
  13167. memcpy(output, str, index);
  13168. output[index] = c[0];
  13169. memcpy(&output[index+1], &str[index], len - index);
  13170. output[len+1] = '\0';
  13171. script_pushstr(st, output);
  13172. return SCRIPT_CMD_SUCCESS;
  13173. }
  13174. //=======================================================
  13175. // delchar <string>, <index>
  13176. //-------------------------------------------------------
  13177. BUILDIN_FUNC(delchar)
  13178. {
  13179. const char *str = script_getstr(st,2);
  13180. int index = script_getnum(st,3);
  13181. char *output;
  13182. size_t len = strlen(str);
  13183. if(index < 0 || index > len) {
  13184. //return original
  13185. output = aStrdup(str);
  13186. script_pushstr(st, output);
  13187. return SCRIPT_CMD_SUCCESS;
  13188. }
  13189. output = (char*)aMalloc(len);
  13190. memcpy(output, str, index);
  13191. memcpy(&output[index], &str[index+1], len - index);
  13192. script_pushstr(st, output);
  13193. return SCRIPT_CMD_SUCCESS;
  13194. }
  13195. //=======================================================
  13196. // strtoupper <str>
  13197. //-------------------------------------------------------
  13198. BUILDIN_FUNC(strtoupper)
  13199. {
  13200. const char *str = script_getstr(st,2);
  13201. char *output = aStrdup(str);
  13202. char *cursor = output;
  13203. while (*cursor != '\0') {
  13204. *cursor = TOUPPER(*cursor);
  13205. cursor++;
  13206. }
  13207. script_pushstr(st, output);
  13208. return SCRIPT_CMD_SUCCESS;
  13209. }
  13210. //=======================================================
  13211. // strtolower <str>
  13212. //-------------------------------------------------------
  13213. BUILDIN_FUNC(strtolower)
  13214. {
  13215. const char *str = script_getstr(st,2);
  13216. char *output = aStrdup(str);
  13217. char *cursor = output;
  13218. while (*cursor != '\0') {
  13219. *cursor = TOLOWER(*cursor);
  13220. cursor++;
  13221. }
  13222. script_pushstr(st, output);
  13223. return SCRIPT_CMD_SUCCESS;
  13224. }
  13225. //=======================================================
  13226. // substr <str>, <start>, <end>
  13227. //-------------------------------------------------------
  13228. BUILDIN_FUNC(substr)
  13229. {
  13230. const char *str = script_getstr(st,2);
  13231. char *output;
  13232. int start = script_getnum(st,3);
  13233. int end = script_getnum(st,4);
  13234. int len = 0;
  13235. if(start >= 0 && end < strlen(str) && start <= end) {
  13236. len = end - start + 1;
  13237. output = (char*)aMalloc(len + 1);
  13238. memcpy(output, &str[start], len);
  13239. } else
  13240. output = (char*)aMalloc(1);
  13241. output[len] = '\0';
  13242. script_pushstr(st, output);
  13243. return SCRIPT_CMD_SUCCESS;
  13244. }
  13245. //=======================================================
  13246. // explode <dest_string_array>, <str>, <delimiter>
  13247. // Note: delimiter is limited to 1 char
  13248. //-------------------------------------------------------
  13249. BUILDIN_FUNC(explode)
  13250. {
  13251. struct script_data* data = script_getdata(st, 2);
  13252. const char *str = script_getstr(st,3);
  13253. const char delimiter = script_getstr(st, 4)[0];
  13254. int32 id;
  13255. size_t len = strlen(str);
  13256. int i = 0, j = 0;
  13257. int start;
  13258. char *temp;
  13259. const char* name;
  13260. TBL_PC* sd = NULL;
  13261. temp = (char*)aMalloc(len + 1);
  13262. if( !data_isreference(data) ) {
  13263. ShowError("script:explode: not a variable\n");
  13264. script_reportdata(data);
  13265. st->state = END;
  13266. return SCRIPT_CMD_FAILURE;// not a variable
  13267. }
  13268. id = reference_getid(data);
  13269. start = reference_getindex(data);
  13270. name = reference_getname(data);
  13271. if( !is_string_variable(name) ) {
  13272. ShowError("script:explode: not string array\n");
  13273. script_reportdata(data);
  13274. st->state = END;
  13275. return SCRIPT_CMD_FAILURE;// data type mismatch
  13276. }
  13277. if( not_server_variable(*name) ) {
  13278. sd = script_rid2sd(st);
  13279. if( sd == NULL )
  13280. return SCRIPT_CMD_SUCCESS;// no player attached
  13281. }
  13282. while(str[i] != '\0') {
  13283. if(str[i] == delimiter && start < SCRIPT_MAX_ARRAYSIZE-1) { //break at delimiter but ignore after reaching last array index
  13284. temp[j] = '\0';
  13285. set_reg(st, sd, reference_uid(id, start++), name, (void*)temp, reference_getref(data));
  13286. j = 0;
  13287. ++i;
  13288. } else {
  13289. temp[j++] = str[i++];
  13290. }
  13291. }
  13292. //set last string
  13293. temp[j] = '\0';
  13294. set_reg(st, sd, reference_uid(id, start), name, (void*)temp, reference_getref(data));
  13295. aFree(temp);
  13296. return SCRIPT_CMD_SUCCESS;
  13297. }
  13298. //=======================================================
  13299. // implode <string_array>
  13300. // implode <string_array>, <glue>
  13301. //-------------------------------------------------------
  13302. BUILDIN_FUNC(implode)
  13303. {
  13304. struct script_data* data = script_getdata(st, 2);
  13305. const char *name;
  13306. uint32 glue_len = 0, array_size, id;
  13307. char *output;
  13308. TBL_PC* sd = NULL;
  13309. if( !data_isreference(data) ) {
  13310. ShowError("script:implode: not a variable\n");
  13311. script_reportdata(data);
  13312. st->state = END;
  13313. return SCRIPT_CMD_FAILURE;// not a variable
  13314. }
  13315. id = reference_getid(data);
  13316. name = reference_getname(data);
  13317. if( !is_string_variable(name) ) {
  13318. ShowError("script:implode: not string array\n");
  13319. script_reportdata(data);
  13320. st->state = END;
  13321. return SCRIPT_CMD_FAILURE;// data type mismatch
  13322. }
  13323. if( not_server_variable(*name) ) {
  13324. sd = script_rid2sd(st);
  13325. if( sd == NULL )
  13326. return SCRIPT_CMD_SUCCESS;// no player attached
  13327. }
  13328. //count chars
  13329. array_size = script_array_highest_key(st, sd, name, reference_getref(data)) - 1;
  13330. if(array_size == -1) { //empty array check (AmsTaff)
  13331. ShowWarning("script:implode: array length = 0\n");
  13332. output = (char*)aMalloc(sizeof(char)*5);
  13333. sprintf(output,"%s","NULL");
  13334. } else {
  13335. const char *glue = NULL, *temp;
  13336. size_t len = 0;
  13337. int i, k = 0;
  13338. for(i = 0; i <= array_size; ++i) {
  13339. temp = (char*) get_val2(st, reference_uid(id, i), reference_getref(data));
  13340. len += strlen(temp);
  13341. script_removetop(st, -1, 0);
  13342. }
  13343. //allocate mem
  13344. if( script_hasdata(st,3) ) {
  13345. glue = script_getstr(st,3);
  13346. glue_len = strlen(glue);
  13347. len += glue_len * (array_size);
  13348. }
  13349. output = (char*)aMalloc(len + 1);
  13350. //build output
  13351. for(i = 0; i < array_size; ++i) {
  13352. temp = (char*) get_val2(st, reference_uid(id, i), reference_getref(data));
  13353. len = strlen(temp);
  13354. memcpy(&output[k], temp, len);
  13355. k += len;
  13356. if(glue_len != 0) {
  13357. memcpy(&output[k], glue, glue_len);
  13358. k += glue_len;
  13359. }
  13360. script_removetop(st, -1, 0);
  13361. }
  13362. temp = (char*) get_val2(st, reference_uid(id, array_size), reference_getref(data));
  13363. len = strlen(temp);
  13364. memcpy(&output[k], temp, len);
  13365. k += len;
  13366. script_removetop(st, -1, 0);
  13367. output[k] = '\0';
  13368. }
  13369. script_pushstr(st, output);
  13370. return SCRIPT_CMD_SUCCESS;
  13371. }
  13372. //=======================================================
  13373. // sprintf(<format>, ...);
  13374. // Implements C sprintf, except format %n. The resulting string is
  13375. // returned, instead of being saved in variable by reference.
  13376. //-------------------------------------------------------
  13377. BUILDIN_FUNC(sprintf)
  13378. {
  13379. unsigned int len, argc = 0, arg = 0, buf2_len = 0;
  13380. const char* format;
  13381. char* p;
  13382. char* q;
  13383. char* buf = NULL;
  13384. char* buf2 = NULL;
  13385. struct script_data* data;
  13386. StringBuf final_buf;
  13387. // Fetch init data
  13388. format = script_getstr(st, 2);
  13389. argc = script_lastdata(st)-2;
  13390. len = strlen(format);
  13391. // Skip parsing, where no parsing is required.
  13392. if(len == 0) {
  13393. script_pushconststr(st,"");
  13394. return SCRIPT_CMD_SUCCESS;
  13395. }
  13396. // Pessimistic alloc
  13397. CREATE(buf, char, len+1);
  13398. // Need not be parsed, just solve stuff like %%.
  13399. if(argc == 0) {
  13400. memcpy(buf,format,len+1);
  13401. script_pushstrcopy(st, buf);
  13402. aFree(buf);
  13403. return SCRIPT_CMD_SUCCESS;
  13404. }
  13405. safestrncpy(buf, format, len+1);
  13406. // Issue sprintf for each parameter
  13407. StringBuf_Init(&final_buf);
  13408. q = buf;
  13409. while((p = strchr(q, '%')) != NULL) {
  13410. if(p != q) {
  13411. len = p - q + 1;
  13412. if(buf2_len < len) {
  13413. RECREATE(buf2, char, len);
  13414. buf2_len = len;
  13415. }
  13416. safestrncpy(buf2, q, len);
  13417. StringBuf_AppendStr(&final_buf, buf2);
  13418. q = p;
  13419. }
  13420. p = q + 1;
  13421. if(*p == '%') { // %%
  13422. StringBuf_AppendStr(&final_buf, "%");
  13423. q+=2;
  13424. continue;
  13425. }
  13426. if(*p == 'n') { // %n
  13427. ShowWarning("buildin_sprintf: Format %%n not supported! Skipping...\n");
  13428. script_reportsrc(st);
  13429. q+=2;
  13430. continue;
  13431. }
  13432. if(arg >= argc) {
  13433. ShowError("buildin_sprintf: Not enough arguments passed!\n");
  13434. if(buf) aFree(buf);
  13435. if(buf2) aFree(buf2);
  13436. StringBuf_Destroy(&final_buf);
  13437. script_pushconststr(st,"");
  13438. return SCRIPT_CMD_FAILURE;
  13439. }
  13440. if((p = strchr(q+1, '%')) == NULL)
  13441. p = strchr(q, 0); // EOS
  13442. len = p - q + 1;
  13443. if(buf2_len < len) {
  13444. RECREATE(buf2, char, len);
  13445. buf2_len = len;
  13446. }
  13447. safestrncpy(buf2, q, len);
  13448. q = p;
  13449. // Note: This assumes the passed value being the correct
  13450. // type to the current format specifier. If not, the server
  13451. // probably crashes or returns anything else, than expected,
  13452. // but it would behave in normal code the same way so it's
  13453. // the scripter's responsibility.
  13454. data = script_getdata(st, arg+3);
  13455. if(data_isstring(data)) // String
  13456. StringBuf_Printf(&final_buf, buf2, script_getstr(st, arg+3));
  13457. else if(data_isint(data)) // Number
  13458. StringBuf_Printf(&final_buf, buf2, script_getnum(st, arg+3));
  13459. else if(data_isreference(data)) { // Variable
  13460. char* name = reference_getname(data);
  13461. if(name[strlen(name)-1]=='$') // var Str
  13462. StringBuf_Printf(&final_buf, buf2, script_getstr(st, arg+3));
  13463. else // var Int
  13464. StringBuf_Printf(&final_buf, buf2, script_getnum(st, arg+3));
  13465. } else { // Unsupported type
  13466. ShowError("buildin_sprintf: Unknown argument type!\n");
  13467. if(buf) aFree(buf);
  13468. if(buf2) aFree(buf2);
  13469. StringBuf_Destroy(&final_buf);
  13470. script_pushconststr(st,"");
  13471. return SCRIPT_CMD_FAILURE;
  13472. }
  13473. arg++;
  13474. }
  13475. // Append anything left
  13476. if(*q)
  13477. StringBuf_AppendStr(&final_buf, q);
  13478. // Passed more, than needed
  13479. if(arg < argc) {
  13480. ShowWarning("buildin_sprintf: Unused arguments passed.\n");
  13481. script_reportsrc(st);
  13482. }
  13483. script_pushstrcopy(st, StringBuf_Value(&final_buf));
  13484. if(buf) aFree(buf);
  13485. if(buf2) aFree(buf2);
  13486. StringBuf_Destroy(&final_buf);
  13487. return SCRIPT_CMD_SUCCESS;
  13488. }
  13489. //=======================================================
  13490. // sscanf(<str>, <format>, ...);
  13491. // Implements C sscanf.
  13492. //-------------------------------------------------------
  13493. BUILDIN_FUNC(sscanf){
  13494. unsigned int argc, arg = 0, len;
  13495. struct script_data* data;
  13496. struct map_session_data* sd = NULL;
  13497. const char* str;
  13498. const char* format;
  13499. const char* p;
  13500. const char* q;
  13501. char* buf = NULL;
  13502. char* buf_p;
  13503. char* ref_str = NULL;
  13504. int ref_int;
  13505. // Get data
  13506. str = script_getstr(st, 2);
  13507. format = script_getstr(st, 3);
  13508. argc = script_lastdata(st)-3;
  13509. len = strlen(format);
  13510. if (len != 0 && strlen(str) == 0) {
  13511. // If the source string is empty but the format string is not, we return -1
  13512. // according to the C specs. (if the format string is also empty, we shall
  13513. // continue and return 0: 0 conversions took place out of the 0 attempted.)
  13514. script_pushint(st, -1);
  13515. return SCRIPT_CMD_SUCCESS;
  13516. }
  13517. CREATE(buf, char, len*2+1);
  13518. // Issue sscanf for each parameter
  13519. *buf = 0;
  13520. q = format;
  13521. while((p = strchr(q, '%'))){
  13522. if(p!=q){
  13523. strncat(buf, q, (size_t)(p-q));
  13524. q = p;
  13525. }
  13526. p = q+1;
  13527. if(*p=='*' || *p=='%'){ // Skip
  13528. strncat(buf, q, 2);
  13529. q+=2;
  13530. continue;
  13531. }
  13532. if(arg>=argc){
  13533. ShowError("buildin_sscanf: Not enough arguments passed!\n");
  13534. script_pushint(st, -1);
  13535. if(buf) aFree(buf);
  13536. if(ref_str) aFree(ref_str);
  13537. return SCRIPT_CMD_FAILURE;
  13538. }
  13539. if((p = strchr(q+1, '%'))==NULL){
  13540. p = strchr(q, 0); // EOS
  13541. }
  13542. len = p-q;
  13543. strncat(buf, q, len);
  13544. q = p;
  13545. // Validate output
  13546. data = script_getdata(st, arg+4);
  13547. if(!data_isreference(data) || !reference_tovariable(data)){
  13548. ShowError("buildin_sscanf: Target argument is not a variable!\n");
  13549. script_pushint(st, -1);
  13550. if(buf) aFree(buf);
  13551. if(ref_str) aFree(ref_str);
  13552. return SCRIPT_CMD_FAILURE;
  13553. }
  13554. buf_p = reference_getname(data);
  13555. if(not_server_variable(*buf_p) && (sd = script_rid2sd(st))==NULL){
  13556. script_pushint(st, -1);
  13557. if(buf) aFree(buf);
  13558. if(ref_str) aFree(ref_str);
  13559. return SCRIPT_CMD_SUCCESS;
  13560. }
  13561. // Save value if any
  13562. if(buf_p[strlen(buf_p)-1]=='$'){ // String
  13563. if(ref_str==NULL){
  13564. CREATE(ref_str, char, strlen(str)+1);
  13565. }
  13566. if(sscanf(str, buf, ref_str)==0){
  13567. break;
  13568. }
  13569. set_reg(st, sd, reference_uid( reference_getid(data), reference_getindex(data) ), buf_p, (void *)(ref_str), reference_getref(data));
  13570. } else { // Number
  13571. if(sscanf(str, buf, &ref_int)==0){
  13572. break;
  13573. }
  13574. set_reg(st, sd, reference_uid( reference_getid(data), reference_getindex(data) ), buf_p, (void *)__64BPRTSIZE(ref_int), reference_getref(data));
  13575. }
  13576. arg++;
  13577. // Disable used format (%... -> %*...)
  13578. buf_p = strchr(buf, 0);
  13579. memmove(buf_p-len+2, buf_p-len+1, len);
  13580. *(buf_p-len+1) = '*';
  13581. }
  13582. script_pushint(st, arg);
  13583. if(buf) aFree(buf);
  13584. if(ref_str) aFree(ref_str);
  13585. return SCRIPT_CMD_SUCCESS;
  13586. }
  13587. //=======================================================
  13588. // strpos(<haystack>, <needle>)
  13589. // strpos(<haystack>, <needle>, <offset>)
  13590. //
  13591. // Implements PHP style strpos. Adapted from code from
  13592. // http://www.daniweb.com/code/snippet313.html, Dave Sinkula
  13593. //-------------------------------------------------------
  13594. BUILDIN_FUNC(strpos) {
  13595. const char *haystack = script_getstr(st,2);
  13596. const char *needle = script_getstr(st,3);
  13597. int i;
  13598. size_t len;
  13599. if( script_hasdata(st,4) )
  13600. i = script_getnum(st,4);
  13601. else
  13602. i = 0;
  13603. if (needle[0] == '\0') {
  13604. script_pushint(st, -1);
  13605. return SCRIPT_CMD_SUCCESS;
  13606. }
  13607. len = strlen(haystack);
  13608. for ( ; i < len; ++i ) {
  13609. if ( haystack[i] == *needle ) {
  13610. // matched starting char -- loop through remaining chars
  13611. const char *h, *n;
  13612. for ( h = &haystack[i], n = needle; *h && *n; ++h, ++n ) {
  13613. if ( *h != *n ) {
  13614. break;
  13615. }
  13616. }
  13617. if ( !*n ) { // matched all of 'needle' to null termination
  13618. script_pushint(st, i);
  13619. return SCRIPT_CMD_SUCCESS;
  13620. }
  13621. }
  13622. }
  13623. script_pushint(st, -1);
  13624. return SCRIPT_CMD_SUCCESS;
  13625. }
  13626. //===============================================================
  13627. // replacestr <input>, <search>, <replace>{, <usecase>{, <count>}}
  13628. //
  13629. // Note: Finds all instances of <search> in <input> and replaces
  13630. // with <replace>. If specified will only replace as many
  13631. // instances as specified in <count>. By default will be case
  13632. // sensitive.
  13633. //---------------------------------------------------------------
  13634. BUILDIN_FUNC(replacestr)
  13635. {
  13636. const char *input = script_getstr(st, 2);
  13637. const char *find = script_getstr(st, 3);
  13638. const char *replace = script_getstr(st, 4);
  13639. size_t inputlen = strlen(input);
  13640. size_t findlen = strlen(find);
  13641. struct StringBuf output;
  13642. bool usecase = true;
  13643. int count = 0;
  13644. int numFinds = 0;
  13645. int i = 0, f = 0;
  13646. if(findlen == 0) {
  13647. ShowError("script:replacestr: Invalid search length.\n");
  13648. st->state = END;
  13649. return SCRIPT_CMD_FAILURE;
  13650. }
  13651. if(script_hasdata(st, 5)) {
  13652. struct script_data *data = script_getdata(st, 5);
  13653. get_val(st, data); // Convert into value in case of a variable
  13654. if( !data_isstring(data) )
  13655. usecase = script_getnum(st, 5) != 0;
  13656. else {
  13657. ShowError("script:replacestr: Invalid usecase value. Expected int got string\n");
  13658. st->state = END;
  13659. return SCRIPT_CMD_FAILURE;
  13660. }
  13661. }
  13662. if(script_hasdata(st, 6)) {
  13663. count = script_getnum(st, 6);
  13664. if(count == 0) {
  13665. ShowError("script:replacestr: Invalid count value. Expected int got string\n");
  13666. st->state = END;
  13667. return SCRIPT_CMD_FAILURE;
  13668. }
  13669. }
  13670. StringBuf_Init(&output);
  13671. for(; i < inputlen; i++) {
  13672. if(count && count == numFinds) { //found enough, stop looking
  13673. break;
  13674. }
  13675. for(f = 0; f <= findlen; f++) {
  13676. if(f == findlen) { //complete match
  13677. numFinds++;
  13678. StringBuf_AppendStr(&output, replace);
  13679. i += findlen - 1;
  13680. break;
  13681. } else {
  13682. if(usecase) {
  13683. if((i + f) > inputlen || input[i + f] != find[f]) {
  13684. StringBuf_Printf(&output, "%c", input[i]);
  13685. break;
  13686. }
  13687. } else {
  13688. if(((i + f) > inputlen || input[i + f] != find[f]) && TOUPPER(input[i+f]) != TOUPPER(find[f])) {
  13689. StringBuf_Printf(&output, "%c", input[i]);
  13690. break;
  13691. }
  13692. }
  13693. }
  13694. }
  13695. }
  13696. //append excess after enough found
  13697. if(i < inputlen)
  13698. StringBuf_AppendStr(&output, &(input[i]));
  13699. script_pushstrcopy(st, StringBuf_Value(&output));
  13700. StringBuf_Destroy(&output);
  13701. return SCRIPT_CMD_SUCCESS;
  13702. }
  13703. //========================================================
  13704. // countstr <input>, <search>{, <usecase>}
  13705. //
  13706. // Note: Counts the number of times <search> occurs in
  13707. // <input>. By default will be case sensitive.
  13708. //--------------------------------------------------------
  13709. BUILDIN_FUNC(countstr)
  13710. {
  13711. const char *input = script_getstr(st, 2);
  13712. const char *find = script_getstr(st, 3);
  13713. size_t inputlen = strlen(input);
  13714. size_t findlen = strlen(find);
  13715. bool usecase = true;
  13716. int numFinds = 0;
  13717. int i = 0, f = 0;
  13718. if(findlen == 0) {
  13719. ShowError("script:countstr: Invalid search length.\n");
  13720. st->state = END;
  13721. return SCRIPT_CMD_FAILURE;
  13722. }
  13723. if(script_hasdata(st, 4)) {
  13724. struct script_data *data;
  13725. data = script_getdata(st, 4);
  13726. get_val(st, data); // Convert into value in case of a variable
  13727. if( !data_isstring(data) )
  13728. usecase = script_getnum(st, 4) != 0;
  13729. else {
  13730. ShowError("script:countstr: Invalid usecase value. Expected int got string\n");
  13731. st->state = END;
  13732. return SCRIPT_CMD_FAILURE;
  13733. }
  13734. }
  13735. for(; i < inputlen; i++) {
  13736. for(f = 0; f <= findlen; f++) {
  13737. if(f == findlen) { //complete match
  13738. numFinds++;
  13739. i += findlen - 1;
  13740. break;
  13741. } else {
  13742. if(usecase) {
  13743. if((i + f) > inputlen || input[i + f] != find[f]) {
  13744. break;
  13745. }
  13746. } else {
  13747. if(((i + f) > inputlen || input[i + f] != find[f]) && TOUPPER(input[i+f]) != TOUPPER(find[f])) {
  13748. break;
  13749. }
  13750. }
  13751. }
  13752. }
  13753. }
  13754. script_pushint(st, numFinds);
  13755. return SCRIPT_CMD_SUCCESS;
  13756. }
  13757. /// Changes the display name and/or display class of the npc.
  13758. /// Returns 0 is successful, 1 if the npc does not exist.
  13759. ///
  13760. /// setnpcdisplay("<npc name>", "<new display name>", <new class id>, <new size>) -> <int>
  13761. /// setnpcdisplay("<npc name>", "<new display name>", <new class id>) -> <int>
  13762. /// setnpcdisplay("<npc name>", "<new display name>") -> <int>
  13763. /// setnpcdisplay("<npc name>", <new class id>) -> <int>
  13764. BUILDIN_FUNC(setnpcdisplay)
  13765. {
  13766. const char* name;
  13767. const char* newname = NULL;
  13768. int class_ = -1, size = -1;
  13769. struct script_data* data;
  13770. struct npc_data* nd;
  13771. name = script_getstr(st,2);
  13772. data = script_getdata(st,3);
  13773. if( script_hasdata(st,4) )
  13774. class_ = script_getnum(st,4);
  13775. if( script_hasdata(st,5) )
  13776. size = script_getnum(st,5);
  13777. get_val(st, data);
  13778. if( data_isstring(data) )
  13779. newname = conv_str(st,data);
  13780. else if( data_isint(data) )
  13781. class_ = conv_num(st,data);
  13782. else
  13783. {
  13784. ShowError("script:setnpcdisplay: expected string or number\n");
  13785. script_reportdata(data);
  13786. return SCRIPT_CMD_FAILURE;
  13787. }
  13788. nd = npc_name2id(name);
  13789. if( nd == NULL )
  13790. {// not found
  13791. script_pushint(st,1);
  13792. return SCRIPT_CMD_SUCCESS;
  13793. }
  13794. // update npc
  13795. if( newname )
  13796. npc_setdisplayname(nd, newname);
  13797. if( size != -1 && size != (int)nd->size )
  13798. nd->size = size;
  13799. else
  13800. size = -1;
  13801. if( class_ != -1 && nd->class_ != class_ )
  13802. npc_setclass(nd, class_);
  13803. else if( size != -1 )
  13804. { // Required to update the visual size
  13805. clif_clearunit_area(&nd->bl, CLR_OUTSIGHT);
  13806. clif_spawn(&nd->bl);
  13807. }
  13808. script_pushint(st,0);
  13809. return SCRIPT_CMD_SUCCESS;
  13810. }
  13811. BUILDIN_FUNC(atoi)
  13812. {
  13813. const char *value;
  13814. value = script_getstr(st,2);
  13815. script_pushint(st,atoi(value));
  13816. return SCRIPT_CMD_SUCCESS;
  13817. }
  13818. BUILDIN_FUNC(axtoi)
  13819. {
  13820. const char *hex = script_getstr(st,2);
  13821. long value = strtol(hex, NULL, 16);
  13822. #if LONG_MAX > INT_MAX || LONG_MIN < INT_MIN
  13823. value = cap_value(value, INT_MIN, INT_MAX);
  13824. #endif
  13825. script_pushint(st, (int)value);
  13826. return SCRIPT_CMD_SUCCESS;
  13827. }
  13828. BUILDIN_FUNC(strtol)
  13829. {
  13830. const char *string = script_getstr(st, 2);
  13831. int base = script_getnum(st, 3);
  13832. long value = strtol(string, NULL, base);
  13833. #if LONG_MAX > INT_MAX || LONG_MIN < INT_MIN
  13834. value = cap_value(value, INT_MIN, INT_MAX);
  13835. #endif
  13836. script_pushint(st, (int)value);
  13837. return SCRIPT_CMD_SUCCESS;
  13838. }
  13839. // case-insensitive substring search [lordalfa]
  13840. BUILDIN_FUNC(compare)
  13841. {
  13842. const char *message;
  13843. const char *cmpstring;
  13844. message = script_getstr(st,2);
  13845. cmpstring = script_getstr(st,3);
  13846. script_pushint(st,(stristr(message,cmpstring) != NULL));
  13847. return SCRIPT_CMD_SUCCESS;
  13848. }
  13849. BUILDIN_FUNC(strcmp)
  13850. {
  13851. const char *str1;
  13852. const char *str2;
  13853. str1 = script_getstr(st,2);
  13854. str2 = script_getstr(st,3);
  13855. script_pushint(st,strcmp(str1, str2));
  13856. return SCRIPT_CMD_SUCCESS;
  13857. }
  13858. // [zBuffer] List of mathematics commands --->
  13859. BUILDIN_FUNC(sqrt)
  13860. {
  13861. double i, a;
  13862. i = script_getnum(st,2);
  13863. a = sqrt(i);
  13864. script_pushint(st,(int)a);
  13865. return SCRIPT_CMD_SUCCESS;
  13866. }
  13867. BUILDIN_FUNC(pow)
  13868. {
  13869. double i, a, b;
  13870. a = script_getnum(st,2);
  13871. b = script_getnum(st,3);
  13872. i = pow(a,b);
  13873. script_pushint(st,(int)i);
  13874. return SCRIPT_CMD_SUCCESS;
  13875. }
  13876. BUILDIN_FUNC(distance)
  13877. {
  13878. int x0, y0, x1, y1;
  13879. x0 = script_getnum(st,2);
  13880. y0 = script_getnum(st,3);
  13881. x1 = script_getnum(st,4);
  13882. y1 = script_getnum(st,5);
  13883. script_pushint(st,distance_xy(x0,y0,x1,y1));
  13884. return SCRIPT_CMD_SUCCESS;
  13885. }
  13886. // <--- [zBuffer] List of mathematics commands
  13887. BUILDIN_FUNC(md5)
  13888. {
  13889. const char *tmpstr;
  13890. char *md5str;
  13891. tmpstr = script_getstr(st,2);
  13892. md5str = (char *)aMalloc((32+1)*sizeof(char));
  13893. MD5_String(tmpstr, md5str);
  13894. script_pushstr(st, md5str);
  13895. return SCRIPT_CMD_SUCCESS;
  13896. }
  13897. // [zBuffer] List of dynamic var commands --->
  13898. /**
  13899. * setd "<variable name>",<value>{,<char_id>};
  13900. **/
  13901. BUILDIN_FUNC(setd)
  13902. {
  13903. TBL_PC *sd = NULL;
  13904. char varname[100];
  13905. const char *buffer;
  13906. int elem;
  13907. buffer = script_getstr(st, 2);
  13908. if(sscanf(buffer, "%99[^[][%11d]", varname, &elem) < 2)
  13909. elem = 0;
  13910. if( not_server_variable(*varname) ) {
  13911. if (!script_charid2sd(4,sd))
  13912. return SCRIPT_CMD_FAILURE;
  13913. }
  13914. if( is_string_variable(varname) ) {
  13915. setd_sub(st, sd, varname, elem, (void *)script_getstr(st, 3), NULL);
  13916. } else {
  13917. setd_sub(st, sd, varname, elem, (void *)__64BPRTSIZE(script_getnum(st, 3)), NULL);
  13918. }
  13919. return SCRIPT_CMD_SUCCESS;
  13920. }
  13921. int buildin_query_sql_sub(struct script_state* st, Sql* handle)
  13922. {
  13923. int i, j;
  13924. TBL_PC* sd = NULL;
  13925. const char* query;
  13926. struct script_data* data;
  13927. const char* name;
  13928. unsigned int max_rows = SCRIPT_MAX_ARRAYSIZE; // maximum number of rows
  13929. int num_vars;
  13930. int num_cols;
  13931. // check target variables
  13932. for( i = 3; script_hasdata(st,i); ++i ) {
  13933. data = script_getdata(st, i);
  13934. if( data_isreference(data) ) { // it's a variable
  13935. name = reference_getname(data);
  13936. if( not_server_variable(*name) && sd == NULL ) { // requires a player
  13937. sd = script_rid2sd(st);
  13938. if( sd == NULL ) { // no player attached
  13939. script_reportdata(data);
  13940. st->state = END;
  13941. return SCRIPT_CMD_FAILURE;
  13942. }
  13943. }
  13944. } else {
  13945. ShowError("script:query_sql: not a variable\n");
  13946. script_reportdata(data);
  13947. st->state = END;
  13948. return SCRIPT_CMD_FAILURE;
  13949. }
  13950. }
  13951. num_vars = i - 3;
  13952. // Execute the query
  13953. query = script_getstr(st,2);
  13954. if( SQL_ERROR == Sql_QueryStr(handle, query) ) {
  13955. Sql_ShowDebug(handle);
  13956. script_pushint(st, -1);
  13957. return SCRIPT_CMD_FAILURE;
  13958. }
  13959. if( Sql_NumRows(handle) == 0 ) { // No data received
  13960. Sql_FreeResult(handle);
  13961. script_pushint(st, 0);
  13962. return SCRIPT_CMD_SUCCESS;
  13963. }
  13964. // Count the number of columns to store
  13965. num_cols = Sql_NumColumns(handle);
  13966. if( num_vars < num_cols ) {
  13967. ShowWarning("script:query_sql: Too many columns, discarding last %u columns.\n", (unsigned int)(num_cols-num_vars));
  13968. script_reportsrc(st);
  13969. } else if( num_vars > num_cols ) {
  13970. ShowWarning("script:query_sql: Too many variables (%u extra).\n", (unsigned int)(num_vars-num_cols));
  13971. script_reportsrc(st);
  13972. }
  13973. // Store data
  13974. for( i = 0; i < max_rows && SQL_SUCCESS == Sql_NextRow(handle); ++i ) {
  13975. for( j = 0; j < num_vars; ++j ) {
  13976. char* str = NULL;
  13977. if( j < num_cols )
  13978. Sql_GetData(handle, j, &str, NULL);
  13979. data = script_getdata(st, j+3);
  13980. name = reference_getname(data);
  13981. if( is_string_variable(name) )
  13982. setd_sub(st, sd, name, i, (void *)(str?str:""), reference_getref(data));
  13983. else
  13984. setd_sub(st, sd, name, i, (void *)__64BPRTSIZE((str?atoi(str):0)), reference_getref(data));
  13985. }
  13986. }
  13987. if( i == max_rows && max_rows < Sql_NumRows(handle) ) {
  13988. ShowWarning("script:query_sql: Only %d/%u rows have been stored.\n", max_rows, (unsigned int)Sql_NumRows(handle));
  13989. script_reportsrc(st);
  13990. }
  13991. // Free data
  13992. Sql_FreeResult(handle);
  13993. script_pushint(st, i);
  13994. return SCRIPT_CMD_SUCCESS;
  13995. }
  13996. BUILDIN_FUNC(query_sql) {
  13997. #ifdef BETA_THREAD_TEST
  13998. if( st->state != RERUNLINE ) {
  13999. queryThread_add(st,false);
  14000. st->state = RERUNLINE;/* will continue when the query is finished running. */
  14001. } else
  14002. st->state = RUN;
  14003. return SCRIPT_CMD_SUCCESS;
  14004. #else
  14005. return buildin_query_sql_sub(st, qsmysql_handle);
  14006. #endif
  14007. }
  14008. BUILDIN_FUNC(query_logsql) {
  14009. if( !log_config.sql_logs ) {// logmysql_handle == NULL
  14010. ShowWarning("buildin_query_logsql: SQL logs are disabled, query '%s' will not be executed.\n", script_getstr(st,2));
  14011. script_pushint(st,-1);
  14012. return SCRIPT_CMD_FAILURE;
  14013. }
  14014. #ifdef BETA_THREAD_TEST
  14015. if( st->state != RERUNLINE ) {
  14016. queryThread_add(st,true);
  14017. st->state = RERUNLINE;/* will continue when the query is finished running. */
  14018. } else
  14019. st->state = RUN;
  14020. return SCRIPT_CMD_SUCCESS;
  14021. #else
  14022. return buildin_query_sql_sub(st, logmysql_handle);
  14023. #endif
  14024. }
  14025. //Allows escaping of a given string.
  14026. BUILDIN_FUNC(escape_sql)
  14027. {
  14028. const char *str;
  14029. char *esc_str;
  14030. size_t len;
  14031. str = script_getstr(st,2);
  14032. len = strlen(str);
  14033. esc_str = (char*)aMalloc(len*2+1);
  14034. Sql_EscapeStringLen(mmysql_handle, esc_str, str, len);
  14035. script_pushstr(st, esc_str);
  14036. return SCRIPT_CMD_SUCCESS;
  14037. }
  14038. BUILDIN_FUNC(getd)
  14039. {
  14040. char varname[100];
  14041. const char *buffer;
  14042. int elem;
  14043. buffer = script_getstr(st, 2);
  14044. if(sscanf(buffer, "%99[^[][%11d]", varname, &elem) < 2)
  14045. elem = 0;
  14046. // Push the 'pointer' so it's more flexible [Lance]
  14047. push_val(st->stack, C_NAME, reference_uid(add_str(varname), elem));
  14048. return SCRIPT_CMD_SUCCESS;
  14049. }
  14050. BUILDIN_FUNC(callshop)
  14051. {
  14052. TBL_PC *sd = NULL;
  14053. struct npc_data *nd;
  14054. const char *shopname;
  14055. int flag = 0;
  14056. sd = script_rid2sd(st);
  14057. if (!sd) {
  14058. script_pushint(st,0);
  14059. return SCRIPT_CMD_SUCCESS;
  14060. }
  14061. shopname = script_getstr(st, 2);
  14062. if (script_hasdata(st,3))
  14063. flag = script_getnum(st,3);
  14064. nd = npc_name2id(shopname);
  14065. if( !nd || nd->bl.type != BL_NPC || (nd->subtype != NPCTYPE_SHOP && nd->subtype != NPCTYPE_CASHSHOP && nd->subtype != NPCTYPE_ITEMSHOP && nd->subtype != NPCTYPE_POINTSHOP && nd->subtype != NPCTYPE_MARKETSHOP) ) {
  14066. ShowError("buildin_callshop: Shop [%s] not found (or NPC is not shop type)\n", shopname);
  14067. script_pushint(st,0);
  14068. return SCRIPT_CMD_FAILURE;
  14069. }
  14070. if (nd->subtype == NPCTYPE_SHOP) {
  14071. // flag the user as using a valid script call for opening the shop (for floating NPCs)
  14072. sd->state.callshop = 1;
  14073. switch (flag) {
  14074. case 1: npc_buysellsel(sd,nd->bl.id,0); break; //Buy window
  14075. case 2: npc_buysellsel(sd,nd->bl.id,1); break; //Sell window
  14076. default: clif_npcbuysell(sd,nd->bl.id); break; //Show menu
  14077. }
  14078. }
  14079. #if PACKETVER >= 20131223
  14080. else if (nd->subtype == NPCTYPE_MARKETSHOP) {
  14081. unsigned short i;
  14082. for (i = 0; i < nd->u.shop.count; i++) {
  14083. if (nd->u.shop.shop_item[i].qty)
  14084. break;
  14085. }
  14086. if (i == nd->u.shop.count) {
  14087. clif_colormes(sd->fd, color_table[COLOR_RED], msg_txt(sd, 534));
  14088. return SCRIPT_CMD_FAILURE;
  14089. }
  14090. sd->npc_shopid = nd->bl.id;
  14091. clif_npc_market_open(sd, nd);
  14092. script_pushint(st,1);
  14093. return SCRIPT_CMD_SUCCESS;
  14094. }
  14095. #endif
  14096. else
  14097. clif_cashshop_show(sd, nd);
  14098. sd->npc_shopid = nd->bl.id;
  14099. script_pushint(st,1);
  14100. return SCRIPT_CMD_SUCCESS;
  14101. }
  14102. BUILDIN_FUNC(npcshopitem)
  14103. {
  14104. const char* npcname = script_getstr(st, 2);
  14105. struct npc_data* nd = npc_name2id(npcname);
  14106. int n, i;
  14107. int amount;
  14108. uint16 offs = 2;
  14109. if( !nd || ( nd->subtype != NPCTYPE_SHOP && nd->subtype != NPCTYPE_CASHSHOP && nd->subtype != NPCTYPE_ITEMSHOP && nd->subtype != NPCTYPE_POINTSHOP && nd->subtype != NPCTYPE_MARKETSHOP ) ) { // Not found.
  14110. script_pushint(st,0);
  14111. return SCRIPT_CMD_SUCCESS;
  14112. }
  14113. #if PACKETVER >= 20131223
  14114. if (nd->subtype == NPCTYPE_MARKETSHOP) {
  14115. offs = 3;
  14116. npc_market_delfromsql_(nd->exname, 0, true);
  14117. }
  14118. #endif
  14119. // get the count of new entries
  14120. amount = (script_lastdata(st)-2)/offs;
  14121. // generate new shop item list
  14122. RECREATE(nd->u.shop.shop_item, struct npc_item_list, amount);
  14123. for (n = 0, i = 3; n < amount; n++, i+=offs) {
  14124. nd->u.shop.shop_item[n].nameid = script_getnum(st,i);
  14125. nd->u.shop.shop_item[n].value = script_getnum(st,i+1);
  14126. #if PACKETVER >= 20131223
  14127. if (nd->subtype == NPCTYPE_MARKETSHOP) {
  14128. nd->u.shop.shop_item[n].qty = script_getnum(st,i+2);
  14129. nd->u.shop.shop_item[n].flag = 1;
  14130. npc_market_tosql(nd->exname, &nd->u.shop.shop_item[n]);
  14131. }
  14132. #endif
  14133. }
  14134. nd->u.shop.count = n;
  14135. script_pushint(st,1);
  14136. return SCRIPT_CMD_SUCCESS;
  14137. }
  14138. BUILDIN_FUNC(npcshopadditem)
  14139. {
  14140. const char* npcname = script_getstr(st,2);
  14141. struct npc_data* nd = npc_name2id(npcname);
  14142. int n, i;
  14143. uint16 offs = 2, amount;
  14144. if (!nd || ( nd->subtype != NPCTYPE_SHOP && nd->subtype != NPCTYPE_CASHSHOP && nd->subtype != NPCTYPE_ITEMSHOP && nd->subtype != NPCTYPE_POINTSHOP && nd->subtype != NPCTYPE_MARKETSHOP)) { // Not found.
  14145. script_pushint(st,0);
  14146. return SCRIPT_CMD_SUCCESS;
  14147. }
  14148. if (nd->subtype == NPCTYPE_MARKETSHOP)
  14149. offs = 3;
  14150. // get the count of new entries
  14151. amount = (script_lastdata(st)-2)/offs;
  14152. #if PACKETVER >= 20131223
  14153. if (nd->subtype == NPCTYPE_MARKETSHOP) {
  14154. for (n = 0, i = 3; n < amount; n++, i += offs) {
  14155. uint16 nameid = script_getnum(st,i), j;
  14156. // Check existing entries
  14157. ARR_FIND(0, nd->u.shop.count, j, nd->u.shop.shop_item[j].nameid == nameid);
  14158. if (j == nd->u.shop.count) {
  14159. RECREATE(nd->u.shop.shop_item, struct npc_item_list, nd->u.shop.count+1);
  14160. j = nd->u.shop.count;
  14161. nd->u.shop.shop_item[j].flag = 1;
  14162. nd->u.shop.count++;
  14163. }
  14164. nd->u.shop.shop_item[j].nameid = nameid;
  14165. nd->u.shop.shop_item[j].value = script_getnum(st,i+1);
  14166. nd->u.shop.shop_item[j].qty = script_getnum(st,i+2);
  14167. npc_market_tosql(nd->exname, &nd->u.shop.shop_item[j]);
  14168. }
  14169. script_pushint(st,1);
  14170. return SCRIPT_CMD_SUCCESS;
  14171. }
  14172. #endif
  14173. // append new items to existing shop item list
  14174. RECREATE(nd->u.shop.shop_item, struct npc_item_list, nd->u.shop.count+amount);
  14175. for (n = nd->u.shop.count, i = 3; n < nd->u.shop.count+amount; n++, i+=offs)
  14176. {
  14177. nd->u.shop.shop_item[n].nameid = script_getnum(st,i);
  14178. nd->u.shop.shop_item[n].value = script_getnum(st,i+1);
  14179. }
  14180. nd->u.shop.count = n;
  14181. script_pushint(st,1);
  14182. return SCRIPT_CMD_SUCCESS;
  14183. }
  14184. BUILDIN_FUNC(npcshopdelitem)
  14185. {
  14186. const char* npcname = script_getstr(st,2);
  14187. struct npc_data* nd = npc_name2id(npcname);
  14188. int n, i, size;
  14189. unsigned short amount;
  14190. if (!nd || ( nd->subtype != NPCTYPE_SHOP && nd->subtype != NPCTYPE_CASHSHOP && nd->subtype != NPCTYPE_ITEMSHOP && nd->subtype != NPCTYPE_POINTSHOP && nd->subtype != NPCTYPE_MARKETSHOP)) { // Not found.
  14191. script_pushint(st,0);
  14192. return SCRIPT_CMD_SUCCESS;
  14193. }
  14194. amount = script_lastdata(st)-2;
  14195. size = nd->u.shop.count;
  14196. // remove specified items from the shop item list
  14197. for( i = 3; i < 3 + amount; i++ ) {
  14198. unsigned short nameid = script_getnum(st,i);
  14199. ARR_FIND( 0, size, n, nd->u.shop.shop_item[n].nameid == nameid );
  14200. if( n < size ) {
  14201. if (n+1 != size)
  14202. memmove(&nd->u.shop.shop_item[n], &nd->u.shop.shop_item[n+1], sizeof(nd->u.shop.shop_item[0])*(size-n));
  14203. #if PACKETVER >= 20131223
  14204. if (nd->subtype == NPCTYPE_MARKETSHOP)
  14205. npc_market_delfromsql_(nd->exname, nameid, false);
  14206. #endif
  14207. size--;
  14208. }
  14209. }
  14210. RECREATE(nd->u.shop.shop_item, struct npc_item_list, size);
  14211. nd->u.shop.count = size;
  14212. script_pushint(st,1);
  14213. return SCRIPT_CMD_SUCCESS;
  14214. }
  14215. /**
  14216. * Sets a script to attach to a shop npc.
  14217. * npcshopattach "<npc_name>";
  14218. **/
  14219. BUILDIN_FUNC(npcshopattach)
  14220. {
  14221. const char* npcname = script_getstr(st,2);
  14222. struct npc_data* nd = npc_name2id(npcname);
  14223. int flag = 1;
  14224. if( script_hasdata(st,3) )
  14225. flag = script_getnum(st,3);
  14226. if (!nd || ( nd->subtype != NPCTYPE_SHOP && nd->subtype != NPCTYPE_CASHSHOP && nd->subtype != NPCTYPE_ITEMSHOP && nd->subtype != NPCTYPE_POINTSHOP && nd->subtype != NPCTYPE_MARKETSHOP)) { // Not found.
  14227. script_pushint(st,0);
  14228. return SCRIPT_CMD_SUCCESS;
  14229. }
  14230. if (flag)
  14231. nd->master_nd = ((struct npc_data *)map_id2bl(st->oid));
  14232. else
  14233. nd->master_nd = NULL;
  14234. script_pushint(st,1);
  14235. return SCRIPT_CMD_SUCCESS;
  14236. }
  14237. /*==========================================
  14238. * Returns some values of an item [Lupus]
  14239. * Price, Weight, etc...
  14240. setitemscript(itemID,"{new item bonus script}",[n]);
  14241. Where n:
  14242. 0 - script
  14243. 1 - Equip script
  14244. 2 - Unequip script
  14245. *------------------------------------------*/
  14246. BUILDIN_FUNC(setitemscript)
  14247. {
  14248. unsigned short item_id;
  14249. int n = 0;
  14250. const char *script;
  14251. struct item_data *i_data;
  14252. struct script_code **dstscript;
  14253. item_id = script_getnum(st,2);
  14254. script = script_getstr(st,3);
  14255. if( script_hasdata(st,4) )
  14256. n=script_getnum(st,4);
  14257. i_data = itemdb_exists(item_id);
  14258. if (!i_data || script==NULL || ( script[0] && script[0]!='{' )) {
  14259. script_pushint(st,0);
  14260. return SCRIPT_CMD_SUCCESS;
  14261. }
  14262. switch (n) {
  14263. case 2:
  14264. dstscript = &i_data->unequip_script;
  14265. break;
  14266. case 1:
  14267. dstscript = &i_data->equip_script;
  14268. break;
  14269. default:
  14270. dstscript = &i_data->script;
  14271. break;
  14272. }
  14273. if(*dstscript)
  14274. script_free_code(*dstscript);
  14275. *dstscript = script[0] ? parse_script(script, "script_setitemscript", 0, 0) : NULL;
  14276. script_pushint(st,1);
  14277. return SCRIPT_CMD_SUCCESS;
  14278. }
  14279. /*=======================================================
  14280. * Add or Update a mob drop temporarily [Akinari]
  14281. * Original Idea By: [Lupus]
  14282. *
  14283. * addmonsterdrop <mob_id or name>,<item_id>,<rate>;
  14284. *
  14285. * If given an item the mob already drops, the rate
  14286. * is updated to the new rate. Rate cannot exceed 10000
  14287. * Returns 1 if succeeded (added/updated a mob drop)
  14288. *-------------------------------------------------------*/
  14289. BUILDIN_FUNC(addmonsterdrop)
  14290. {
  14291. struct mob_db *mob;
  14292. struct script_data *data;
  14293. unsigned short item_id;
  14294. int rate;
  14295. data = script_getdata(st, 2);
  14296. get_val(st, data); // Convert into value in case of a variable
  14297. if(data_isstring(data))
  14298. mob = mob_db(mobdb_searchname(script_getstr(st,2)));
  14299. else
  14300. mob = mob_db(script_getnum(st,2));
  14301. item_id=script_getnum(st,3);
  14302. rate=script_getnum(st,4);
  14303. if(!itemdb_exists(item_id)){
  14304. ShowError("addmonsterdrop: Nonexistant item %hu requested.\n", item_id );
  14305. return SCRIPT_CMD_FAILURE;
  14306. }
  14307. if(mob) { //We got a valid monster, check for available drop slot
  14308. unsigned char i, c = 0;
  14309. for(i = 0; i < MAX_MOB_DROP; i++) {
  14310. if(mob->dropitem[i].nameid) {
  14311. if(mob->dropitem[i].nameid == item_id) { //If it equals item_id we update that drop
  14312. c = i;
  14313. break;
  14314. }
  14315. continue;
  14316. }
  14317. if(!c) //Accept first available slot only
  14318. c = i;
  14319. }
  14320. if(c) { //Fill in the slot with the item and rate
  14321. mob->dropitem[c].nameid = item_id;
  14322. mob->dropitem[c].p = (rate > 10000)?10000:rate;
  14323. script_pushint(st,1);
  14324. } else //No place to put the new drop
  14325. script_pushint(st,0);
  14326. } else {
  14327. ShowWarning("addmonsterdrop: bad mob id given %d\n",script_getnum(st,2));
  14328. return SCRIPT_CMD_FAILURE;
  14329. }
  14330. return SCRIPT_CMD_SUCCESS;
  14331. }
  14332. /*=======================================================
  14333. * Delete a mob drop temporarily [Akinari]
  14334. * Original Idea By: [Lupus]
  14335. *
  14336. * delmonsterdrop <mob_id or name>,<item_id>;
  14337. *
  14338. * Returns 1 if succeeded (deleted a mob drop)
  14339. *-------------------------------------------------------*/
  14340. BUILDIN_FUNC(delmonsterdrop)
  14341. {
  14342. struct mob_db *mob;
  14343. struct script_data *data;
  14344. unsigned short item_id;
  14345. data = script_getdata(st, 2);
  14346. get_val(st, data); // Convert into value in case of a variable
  14347. if(data_isstring(data))
  14348. mob = mob_db(mobdb_searchname(script_getstr(st,2)));
  14349. else
  14350. mob = mob_db(script_getnum(st,2));
  14351. item_id=script_getnum(st,3);
  14352. if(!itemdb_exists(item_id)){
  14353. ShowError("delmonsterdrop: Nonexistant item %hu requested.\n", item_id );
  14354. return SCRIPT_CMD_FAILURE;
  14355. }
  14356. if(mob) { //We got a valid monster, check for item drop on monster
  14357. unsigned char i;
  14358. for(i = 0; i < MAX_MOB_DROP; i++) {
  14359. if(mob->dropitem[i].nameid == item_id) {
  14360. mob->dropitem[i].nameid = 0;
  14361. mob->dropitem[i].p = 0;
  14362. script_pushint(st,1);
  14363. return SCRIPT_CMD_SUCCESS;
  14364. }
  14365. }
  14366. //No drop on that monster
  14367. script_pushint(st,0);
  14368. } else {
  14369. ShowWarning("delmonsterdrop: bad mob id given %d\n",script_getnum(st,2));
  14370. return SCRIPT_CMD_FAILURE;
  14371. }
  14372. return SCRIPT_CMD_SUCCESS;
  14373. }
  14374. /*==========================================
  14375. * Returns some values of a monster [Lupus]
  14376. * Name, Level, race, size, etc...
  14377. getmonsterinfo(monsterID,queryIndex);
  14378. *------------------------------------------*/
  14379. BUILDIN_FUNC(getmonsterinfo)
  14380. {
  14381. struct mob_db *mob;
  14382. int mob_id;
  14383. mob_id = script_getnum(st,2);
  14384. if (!mobdb_checkid(mob_id)) {
  14385. //ShowError("buildin_getmonsterinfo: Wrong Monster ID: %i\n", mob_id);
  14386. if ( script_getnum(st,3) == MOB_NAME ) // requested the name
  14387. script_pushconststr(st,"null");
  14388. else
  14389. script_pushint(st,-1);
  14390. return SCRIPT_CMD_SUCCESS;
  14391. }
  14392. mob = mob_db(mob_id);
  14393. switch ( script_getnum(st,3) ) {
  14394. case MOB_NAME: script_pushstrcopy(st,mob->jname); break;
  14395. case MOB_LV: script_pushint(st,mob->lv); break;
  14396. case MOB_MAXHP: script_pushint(st,mob->status.max_hp); break;
  14397. case MOB_BASEEXP: script_pushint(st,mob->base_exp); break;
  14398. case MOB_JOBEXP: script_pushint(st,mob->job_exp); break;
  14399. case MOB_ATK1: script_pushint(st,mob->status.rhw.atk); break;
  14400. case MOB_ATK2: script_pushint(st,mob->status.rhw.atk2); break;
  14401. case MOB_DEF: script_pushint(st,mob->status.def); break;
  14402. case MOB_MDEF: script_pushint(st,mob->status.mdef); break;
  14403. case MOB_STR: script_pushint(st,mob->status.str); break;
  14404. case MOB_AGI: script_pushint(st,mob->status.agi); break;
  14405. case MOB_VIT: script_pushint(st,mob->status.vit); break;
  14406. case MOB_INT: script_pushint(st,mob->status.int_); break;
  14407. case MOB_DEX: script_pushint(st,mob->status.dex); break;
  14408. case MOB_LUK: script_pushint(st,mob->status.luk); break;
  14409. case MOB_RANGE: script_pushint(st,mob->status.rhw.range); break;
  14410. case MOB_RANGE2: script_pushint(st,mob->range2); break;
  14411. case MOB_RANGE3: script_pushint(st,mob->range3); break;
  14412. case MOB_SIZE: script_pushint(st,mob->status.size); break;
  14413. case MOB_RACE: script_pushint(st,mob->status.race); break;
  14414. case MOB_ELEMENT: script_pushint(st,mob->status.def_ele); break;
  14415. case MOB_MODE: script_pushint(st,mob->status.mode); break;
  14416. case MOB_MVPEXP: script_pushint(st,mob->mexp); break;
  14417. default: script_pushint(st,-1); //wrong Index
  14418. }
  14419. return SCRIPT_CMD_SUCCESS;
  14420. }
  14421. /**
  14422. * Check player's vending/buyingstore/autotrade state
  14423. * checkvending({"<Player Name>"})
  14424. * @author [Nab4]
  14425. */
  14426. BUILDIN_FUNC(checkvending) {
  14427. TBL_PC *sd = NULL;
  14428. if (script_hasdata(st,2)) {
  14429. if (!(sd = map_nick2sd(script_getstr(st,2)))) {
  14430. ShowError("buildin_checkvending: Player '%s' is not online!\n", script_getstr(st,2));
  14431. return SCRIPT_CMD_FAILURE;
  14432. }
  14433. }
  14434. else
  14435. sd = script_rid2sd(st);
  14436. if (!sd) {
  14437. script_pushint(st,0);
  14438. return SCRIPT_CMD_SUCCESS;
  14439. }
  14440. else {
  14441. int8 ret = 0;
  14442. if (sd->state.vending)
  14443. ret = 1;
  14444. else if (sd->state.buyingstore)
  14445. ret = 4;
  14446. if (sd->state.autotrade)
  14447. ret |= 2;
  14448. script_pushint(st, ret);
  14449. }
  14450. return SCRIPT_CMD_SUCCESS;
  14451. }
  14452. BUILDIN_FUNC(checkchatting) // check chatting [Marka]
  14453. {
  14454. TBL_PC *sd = NULL;
  14455. if(script_hasdata(st,2))
  14456. sd = map_nick2sd(script_getstr(st,2));
  14457. else
  14458. sd = script_rid2sd(st);
  14459. if(sd)
  14460. script_pushint(st,(sd->chatID != 0));
  14461. else
  14462. script_pushint(st,0);
  14463. return SCRIPT_CMD_SUCCESS;
  14464. }
  14465. BUILDIN_FUNC(checkidle)
  14466. {
  14467. TBL_PC *sd = NULL;
  14468. if (script_hasdata(st, 2))
  14469. sd = map_nick2sd(script_getstr(st, 2));
  14470. else
  14471. sd = script_rid2sd(st);
  14472. if (sd)
  14473. script_pushint(st, DIFF_TICK(last_tick, sd->idletime));
  14474. else
  14475. script_pushint(st, 0);
  14476. return SCRIPT_CMD_SUCCESS;
  14477. }
  14478. BUILDIN_FUNC(searchitem)
  14479. {
  14480. struct script_data* data = script_getdata(st, 2);
  14481. const char *itemname = script_getstr(st,3);
  14482. struct item_data *items[MAX_SEARCH];
  14483. int count;
  14484. char* name;
  14485. int32 start;
  14486. int32 id;
  14487. int32 i;
  14488. TBL_PC* sd = NULL;
  14489. if ((items[0] = itemdb_exists(atoi(itemname))))
  14490. count = 1;
  14491. else {
  14492. count = itemdb_searchname_array(items, ARRAYLENGTH(items), itemname);
  14493. if (count > MAX_SEARCH) count = MAX_SEARCH;
  14494. }
  14495. if (!count) {
  14496. script_pushint(st, 0);
  14497. return SCRIPT_CMD_SUCCESS;
  14498. }
  14499. if( !data_isreference(data) )
  14500. {
  14501. ShowError("script:searchitem: not a variable\n");
  14502. script_reportdata(data);
  14503. st->state = END;
  14504. return SCRIPT_CMD_FAILURE;// not a variable
  14505. }
  14506. id = reference_getid(data);
  14507. start = reference_getindex(data);
  14508. name = reference_getname(data);
  14509. if( not_server_variable(*name) )
  14510. {
  14511. sd = script_rid2sd(st);
  14512. if( sd == NULL )
  14513. return SCRIPT_CMD_SUCCESS;// no player attached
  14514. }
  14515. if( is_string_variable(name) )
  14516. {// string array
  14517. ShowError("script:searchitem: not an integer array reference\n");
  14518. script_reportdata(data);
  14519. st->state = END;
  14520. return SCRIPT_CMD_FAILURE;// not supported
  14521. }
  14522. for( i = 0; i < count; ++start, ++i )
  14523. {// Set array
  14524. void* v = (void*)__64BPRTSIZE((int)items[i]->nameid);
  14525. set_reg(st, sd, reference_uid(id, start), name, v, reference_getref(data));
  14526. }
  14527. script_pushint(st, count);
  14528. return SCRIPT_CMD_SUCCESS;
  14529. }
  14530. // [zBuffer] List of player cont commands --->
  14531. BUILDIN_FUNC(rid2name)
  14532. {
  14533. struct block_list *bl = NULL;
  14534. int rid = script_getnum(st,2);
  14535. if((bl = map_id2bl(rid)))
  14536. {
  14537. switch(bl->type) {
  14538. case BL_MOB: script_pushstrcopy(st,((TBL_MOB*)bl)->name); break;
  14539. case BL_PC: script_pushstrcopy(st,((TBL_PC*)bl)->status.name); break;
  14540. case BL_NPC: script_pushstrcopy(st,((TBL_NPC*)bl)->exname); break;
  14541. case BL_PET: script_pushstrcopy(st,((TBL_PET*)bl)->pet.name); break;
  14542. case BL_HOM: script_pushstrcopy(st,((TBL_HOM*)bl)->homunculus.name); break;
  14543. case BL_MER: script_pushstrcopy(st,((TBL_MER*)bl)->db->name); break;
  14544. default:
  14545. ShowError("buildin_rid2name: BL type unknown.\n");
  14546. script_pushconststr(st,"");
  14547. break;
  14548. }
  14549. } else {
  14550. ShowError("buildin_rid2name: invalid RID\n");
  14551. script_pushconststr(st,"(null)");
  14552. }
  14553. return SCRIPT_CMD_SUCCESS;
  14554. }
  14555. BUILDIN_FUNC(pcblockmove)
  14556. {
  14557. struct block_list *bl = NULL;
  14558. if (script_getnum(st, 2))
  14559. bl = map_id2bl(script_getnum(st,2));
  14560. else
  14561. bl = map_id2bl(st->rid);
  14562. if (bl) {
  14563. struct unit_data *ud = unit_bl2ud(bl);
  14564. if (ud)
  14565. ud->state.blockedmove = script_getnum(st,3) > 0;
  14566. }
  14567. return SCRIPT_CMD_SUCCESS;
  14568. }
  14569. BUILDIN_FUNC(pcfollow)
  14570. {
  14571. int id, targetid;
  14572. TBL_PC *sd = NULL;
  14573. id = script_getnum(st,2);
  14574. targetid = script_getnum(st,3);
  14575. if(id)
  14576. sd = map_id2sd(id);
  14577. else
  14578. sd = script_rid2sd(st);
  14579. if(sd)
  14580. pc_follow(sd, targetid);
  14581. return SCRIPT_CMD_SUCCESS;
  14582. }
  14583. BUILDIN_FUNC(pcstopfollow)
  14584. {
  14585. int id;
  14586. TBL_PC *sd = NULL;
  14587. id = script_getnum(st,2);
  14588. if(id)
  14589. sd = map_id2sd(id);
  14590. else
  14591. sd = script_rid2sd(st);
  14592. if(sd)
  14593. pc_stop_following(sd);
  14594. return SCRIPT_CMD_SUCCESS;
  14595. }
  14596. // <--- [zBuffer] List of player cont commands
  14597. // [zBuffer] List of unit control commands --->
  14598. /// Gets the type of the given Game ID.
  14599. ///
  14600. /// getunittype <unit id>;
  14601. BUILDIN_FUNC(getunittype)
  14602. {
  14603. struct block_list* bl;
  14604. uint8 value = 0;
  14605. bl = map_id2bl(script_getnum(st, 2));
  14606. if (!bl) {
  14607. ShowWarning("buildin_getunittype: Error in finding object with given game ID %d!\n", script_getnum(st, 2));
  14608. script_pushint(st, -1);
  14609. return SCRIPT_CMD_FAILURE;
  14610. }
  14611. switch (bl->type) {
  14612. case BL_PC: value = UNITTYPE_PC; break;
  14613. case BL_NPC: value = UNITTYPE_NPC; break;
  14614. case BL_PET: value = UNITTYPE_PET; break;
  14615. case BL_MOB: value = UNITTYPE_MOB; break;
  14616. case BL_HOM: value = UNITTYPE_HOM; break;
  14617. case BL_MER: value = UNITTYPE_MER; break;
  14618. case BL_ELEM: value = UNITTYPE_ELEM; break;
  14619. default: value = -1; break;
  14620. }
  14621. script_pushint(st, value);
  14622. return SCRIPT_CMD_SUCCESS;
  14623. }
  14624. /// Gets specific live information of a bl.
  14625. ///
  14626. /// getunitdata <unit id>,<arrayname>;
  14627. BUILDIN_FUNC(getunitdata)
  14628. {
  14629. TBL_PC *sd = st->rid ? map_id2sd(st->rid) : NULL;
  14630. struct block_list* bl;
  14631. TBL_MOB* md = NULL;
  14632. TBL_HOM* hd = NULL;
  14633. TBL_MER* mc = NULL;
  14634. TBL_PET* pd = NULL;
  14635. TBL_ELEM* ed = NULL;
  14636. TBL_NPC* nd = NULL;
  14637. char* name;
  14638. struct script_data *data = script_getdata(st, 3);
  14639. if (!data_isreference(data)) {
  14640. ShowWarning("buildin_getunitdata: Error in argument! Please give a variable to store values in.\n");
  14641. return SCRIPT_CMD_FAILURE;
  14642. }
  14643. bl = map_id2bl(script_getnum(st, 2));
  14644. if (!bl) {
  14645. ShowWarning("buildin_getunitdata: Error in finding object with given game ID %d!\n", script_getnum(st, 2));
  14646. return SCRIPT_CMD_FAILURE;
  14647. }
  14648. switch (bl->type) {
  14649. case BL_MOB: md = map_id2md(bl->id); break;
  14650. case BL_HOM: hd = map_id2hd(bl->id); break;
  14651. case BL_PET: pd = map_id2pd(bl->id); break;
  14652. case BL_MER: mc = map_id2mc(bl->id); break;
  14653. case BL_ELEM: ed = map_id2ed(bl->id); break;
  14654. case BL_NPC: nd = map_id2nd(bl->id); break;
  14655. }
  14656. name = reference_getname(data);
  14657. #define getunitdata_sub(idx__,var__) setd_sub(st,sd,name,(idx__),(void *)__64BPRTSIZE((int)(var__)),data->ref)
  14658. switch(bl->type) {
  14659. case BL_MOB:
  14660. if (!md) {
  14661. ShowWarning("buildin_getunitdata: Error in finding object BL_MOB!\n");
  14662. return SCRIPT_CMD_FAILURE;
  14663. }
  14664. getunitdata_sub(UMOB_SIZE, md->status.size);
  14665. getunitdata_sub(UMOB_LEVEL, md->level);
  14666. getunitdata_sub(UMOB_HP, md->status.hp);
  14667. getunitdata_sub(UMOB_MAXHP, md->status.max_hp);
  14668. getunitdata_sub(UMOB_MASTERAID, md->master_id);
  14669. getunitdata_sub(UMOB_MAPID, md->bl.m);
  14670. getunitdata_sub(UMOB_X, md->bl.x);
  14671. getunitdata_sub(UMOB_Y, md->bl.y);
  14672. getunitdata_sub(UMOB_SPEED, md->status.speed);
  14673. getunitdata_sub(UMOB_MODE, md->status.mode);
  14674. getunitdata_sub(UMOB_AI, md->special_state.ai);
  14675. getunitdata_sub(UMOB_SCOPTION, md->sc.option);
  14676. getunitdata_sub(UMOB_SEX, md->vd->sex);
  14677. getunitdata_sub(UMOB_CLASS, md->vd->class_);
  14678. getunitdata_sub(UMOB_HAIRSTYLE, md->vd->hair_style);
  14679. getunitdata_sub(UMOB_HAIRCOLOR, md->vd->hair_color);
  14680. getunitdata_sub(UMOB_HEADBOTTOM, md->vd->head_bottom);
  14681. getunitdata_sub(UMOB_HEADMIDDLE, md->vd->head_mid);
  14682. getunitdata_sub(UMOB_HEADTOP, md->vd->head_top);
  14683. getunitdata_sub(UMOB_CLOTHCOLOR, md->vd->cloth_color);
  14684. getunitdata_sub(UMOB_SHIELD, md->vd->shield);
  14685. getunitdata_sub(UMOB_WEAPON, md->vd->weapon);
  14686. getunitdata_sub(UMOB_LOOKDIR, md->ud.dir);
  14687. getunitdata_sub(UMOB_CANMOVETICK, md->ud.canmove_tick);
  14688. getunitdata_sub(UMOB_STR, md->status.str);
  14689. getunitdata_sub(UMOB_AGI, md->status.agi);
  14690. getunitdata_sub(UMOB_VIT, md->status.vit);
  14691. getunitdata_sub(UMOB_INT, md->status.int_);
  14692. getunitdata_sub(UMOB_DEX, md->status.dex);
  14693. getunitdata_sub(UMOB_LUK, md->status.luk);
  14694. getunitdata_sub(UMOB_SLAVECPYMSTRMD, md->state.copy_master_mode);
  14695. getunitdata_sub(UMOB_DMGIMMUNE, md->ud.immune_attack);
  14696. getunitdata_sub(UMOB_ATKRANGE, md->status.rhw.range);
  14697. getunitdata_sub(UMOB_ATKMIN, md->status.rhw.atk);
  14698. getunitdata_sub(UMOB_ATKMAX, md->status.rhw.atk2);
  14699. getunitdata_sub(UMOB_MATKMIN, md->status.matk_min);
  14700. getunitdata_sub(UMOB_MATKMAX, md->status.matk_max);
  14701. getunitdata_sub(UMOB_DEF, md->status.def);
  14702. getunitdata_sub(UMOB_MDEF, md->status.mdef);
  14703. getunitdata_sub(UMOB_HIT, md->status.hit);
  14704. getunitdata_sub(UMOB_FLEE, md->status.flee);
  14705. getunitdata_sub(UMOB_PDODGE, md->status.flee2);
  14706. getunitdata_sub(UMOB_CRIT, md->status.cri);
  14707. getunitdata_sub(UMOB_RACE, md->status.race);
  14708. getunitdata_sub(UMOB_ELETYPE, md->status.def_ele);
  14709. getunitdata_sub(UMOB_ELELEVEL, md->status.ele_lv);
  14710. getunitdata_sub(UMOB_AMOTION, md->status.amotion);
  14711. getunitdata_sub(UMOB_ADELAY, md->status.adelay);
  14712. getunitdata_sub(UMOB_DMOTION, md->status.dmotion);
  14713. break;
  14714. case BL_HOM:
  14715. if (!hd) {
  14716. ShowWarning("buildin_getunitdata: Error in finding object BL_HOM!\n");
  14717. return SCRIPT_CMD_FAILURE;
  14718. }
  14719. getunitdata_sub(UHOM_SIZE, hd->base_status.size);
  14720. getunitdata_sub(UHOM_LEVEL, hd->homunculus.level);
  14721. getunitdata_sub(UHOM_HP, hd->homunculus.hp);
  14722. getunitdata_sub(UHOM_MAXHP, hd->homunculus.max_hp);
  14723. getunitdata_sub(UHOM_SP, hd->homunculus.sp);
  14724. getunitdata_sub(UHOM_MAXSP, hd->homunculus.max_sp);
  14725. getunitdata_sub(UHOM_MASTERCID, hd->homunculus.char_id);
  14726. getunitdata_sub(UHOM_MAPID, hd->bl.m);
  14727. getunitdata_sub(UHOM_X, hd->bl.x);
  14728. getunitdata_sub(UHOM_Y, hd->bl.y);
  14729. getunitdata_sub(UHOM_HUNGER, hd->homunculus.hunger);
  14730. getunitdata_sub(UHOM_INTIMACY, hd->homunculus.intimacy);
  14731. getunitdata_sub(UHOM_SPEED, hd->base_status.speed);
  14732. getunitdata_sub(UHOM_LOOKDIR, hd->ud.dir);
  14733. getunitdata_sub(UHOM_CANMOVETICK, hd->ud.canmove_tick);
  14734. getunitdata_sub(UHOM_STR, hd->base_status.str);
  14735. getunitdata_sub(UHOM_AGI, hd->base_status.agi);
  14736. getunitdata_sub(UHOM_VIT, hd->base_status.vit);
  14737. getunitdata_sub(UHOM_INT, hd->base_status.int_);
  14738. getunitdata_sub(UHOM_DEX, hd->base_status.dex);
  14739. getunitdata_sub(UHOM_LUK, hd->base_status.luk);
  14740. getunitdata_sub(UHOM_DMGIMMUNE, hd->ud.immune_attack);
  14741. getunitdata_sub(UHOM_ATKRANGE, hd->battle_status.rhw.range);
  14742. getunitdata_sub(UHOM_ATKMIN, hd->base_status.rhw.atk);
  14743. getunitdata_sub(UHOM_ATKMAX, hd->base_status.rhw.atk2);
  14744. getunitdata_sub(UHOM_MATKMIN, hd->base_status.matk_min);
  14745. getunitdata_sub(UHOM_MATKMAX, hd->base_status.matk_max);
  14746. getunitdata_sub(UHOM_DEF, hd->battle_status.def);
  14747. getunitdata_sub(UHOM_MDEF, hd->battle_status.mdef);
  14748. getunitdata_sub(UHOM_HIT, hd->battle_status.hit);
  14749. getunitdata_sub(UHOM_FLEE, hd->battle_status.flee);
  14750. getunitdata_sub(UHOM_PDODGE, hd->battle_status.flee2);
  14751. getunitdata_sub(UHOM_CRIT, hd->battle_status.cri);
  14752. getunitdata_sub(UHOM_RACE, hd->battle_status.race);
  14753. getunitdata_sub(UHOM_ELETYPE, hd->battle_status.def_ele);
  14754. getunitdata_sub(UHOM_ELELEVEL, hd->battle_status.ele_lv);
  14755. getunitdata_sub(UHOM_AMOTION, hd->battle_status.amotion);
  14756. getunitdata_sub(UHOM_ADELAY, hd->battle_status.adelay);
  14757. getunitdata_sub(UHOM_DMOTION, hd->battle_status.dmotion);
  14758. break;
  14759. case BL_PET:
  14760. if (!pd) {
  14761. ShowWarning("buildin_getunitdata: Error in finding object BL_PET!\n");
  14762. return SCRIPT_CMD_FAILURE;
  14763. }
  14764. getunitdata_sub(UPET_SIZE, pd->status.size);
  14765. getunitdata_sub(UPET_LEVEL, pd->pet.level);
  14766. getunitdata_sub(UPET_HP, pd->status.hp);
  14767. getunitdata_sub(UPET_MAXHP, pd->status.max_hp);
  14768. getunitdata_sub(UPET_MASTERAID, pd->pet.account_id);
  14769. getunitdata_sub(UPET_MAPID, pd->bl.m);
  14770. getunitdata_sub(UPET_X, pd->bl.x);
  14771. getunitdata_sub(UPET_Y, pd->bl.y);
  14772. getunitdata_sub(UPET_HUNGER, pd->pet.hungry);
  14773. getunitdata_sub(UPET_INTIMACY, pd->pet.intimate);
  14774. getunitdata_sub(UPET_SPEED, pd->status.speed);
  14775. getunitdata_sub(UPET_LOOKDIR, pd->ud.dir);
  14776. getunitdata_sub(UPET_CANMOVETICK, pd->ud.canmove_tick);
  14777. getunitdata_sub(UPET_STR, pd->status.str);
  14778. getunitdata_sub(UPET_AGI, pd->status.agi);
  14779. getunitdata_sub(UPET_VIT, pd->status.vit);
  14780. getunitdata_sub(UPET_INT, pd->status.int_);
  14781. getunitdata_sub(UPET_DEX, pd->status.dex);
  14782. getunitdata_sub(UPET_LUK, pd->status.luk);
  14783. getunitdata_sub(UPET_DMGIMMUNE, pd->ud.immune_attack);
  14784. getunitdata_sub(UPET_ATKRANGE, pd->status.rhw.range);
  14785. getunitdata_sub(UPET_ATKMIN, pd->status.rhw.atk);
  14786. getunitdata_sub(UPET_ATKMAX, pd->status.rhw.atk2);
  14787. getunitdata_sub(UPET_MATKMIN, pd->status.matk_min);
  14788. getunitdata_sub(UPET_MATKMAX, pd->status.matk_max);
  14789. getunitdata_sub(UPET_DEF, pd->status.def);
  14790. getunitdata_sub(UPET_MDEF, pd->status.mdef);
  14791. getunitdata_sub(UPET_HIT, pd->status.hit);
  14792. getunitdata_sub(UPET_FLEE, pd->status.flee);
  14793. getunitdata_sub(UPET_PDODGE, pd->status.flee2);
  14794. getunitdata_sub(UPET_CRIT, pd->status.cri);
  14795. getunitdata_sub(UPET_RACE, pd->status.race);
  14796. getunitdata_sub(UPET_ELETYPE, pd->status.def_ele);
  14797. getunitdata_sub(UPET_ELELEVEL, pd->status.ele_lv);
  14798. getunitdata_sub(UPET_AMOTION, pd->status.amotion);
  14799. getunitdata_sub(UPET_ADELAY, pd->status.adelay);
  14800. getunitdata_sub(UPET_DMOTION, pd->status.dmotion);
  14801. break;
  14802. case BL_MER:
  14803. if (!mc) {
  14804. ShowWarning("buildin_getunitdata: Error in finding object BL_MER!\n");
  14805. return SCRIPT_CMD_FAILURE;
  14806. }
  14807. getunitdata_sub(UMER_SIZE, mc->base_status.size);
  14808. getunitdata_sub(UMER_HP, mc->base_status.hp);
  14809. getunitdata_sub(UMER_MAXHP, mc->base_status.max_hp);
  14810. getunitdata_sub(UMER_MASTERCID, mc->mercenary.char_id);
  14811. getunitdata_sub(UMER_MAPID, mc->bl.m);
  14812. getunitdata_sub(UMER_X, mc->bl.x);
  14813. getunitdata_sub(UMER_Y, mc->bl.y);
  14814. getunitdata_sub(UMER_KILLCOUNT, mc->mercenary.kill_count);
  14815. getunitdata_sub(UMER_LIFETIME, mc->mercenary.life_time);
  14816. getunitdata_sub(UMER_SPEED, mc->base_status.speed);
  14817. getunitdata_sub(UMER_LOOKDIR, mc->ud.dir);
  14818. getunitdata_sub(UMER_CANMOVETICK, mc->ud.canmove_tick);
  14819. getunitdata_sub(UMER_STR, mc->base_status.str);
  14820. getunitdata_sub(UMER_AGI, mc->base_status.agi);
  14821. getunitdata_sub(UMER_VIT, mc->base_status.vit);
  14822. getunitdata_sub(UMER_INT, mc->base_status.int_);
  14823. getunitdata_sub(UMER_DEX, mc->base_status.dex);
  14824. getunitdata_sub(UMER_LUK, mc->base_status.luk);
  14825. getunitdata_sub(UMER_DMGIMMUNE, mc->ud.immune_attack);
  14826. getunitdata_sub(UMER_ATKRANGE, mc->base_status.rhw.range);
  14827. getunitdata_sub(UMER_ATKMIN, mc->base_status.rhw.atk);
  14828. getunitdata_sub(UMER_ATKMAX, mc->base_status.rhw.atk2);
  14829. getunitdata_sub(UMER_MATKMIN, mc->base_status.matk_min);
  14830. getunitdata_sub(UMER_MATKMAX, mc->base_status.matk_max);
  14831. getunitdata_sub(UMER_DEF, mc->base_status.def);
  14832. getunitdata_sub(UMER_MDEF, mc->base_status.mdef);
  14833. getunitdata_sub(UMER_HIT, mc->base_status.hit);
  14834. getunitdata_sub(UMER_FLEE, mc->base_status.flee);
  14835. getunitdata_sub(UMER_PDODGE, mc->base_status.flee2);
  14836. getunitdata_sub(UMER_CRIT, mc->base_status.cri);
  14837. getunitdata_sub(UMER_RACE, mc->base_status.race);
  14838. getunitdata_sub(UMER_ELETYPE, mc->base_status.def_ele);
  14839. getunitdata_sub(UMER_ELELEVEL, mc->base_status.ele_lv);
  14840. getunitdata_sub(UMER_AMOTION, mc->base_status.amotion);
  14841. getunitdata_sub(UMER_ADELAY, mc->base_status.adelay);
  14842. getunitdata_sub(UMER_DMOTION, mc->base_status.dmotion);
  14843. break;
  14844. case BL_ELEM:
  14845. if (!ed) {
  14846. ShowWarning("buildin_getunitdata: Error in finding object BL_ELEM!\n");
  14847. return SCRIPT_CMD_FAILURE;
  14848. }
  14849. getunitdata_sub(UELE_SIZE, ed->base_status.size);
  14850. getunitdata_sub(UELE_HP, ed->elemental.hp);
  14851. getunitdata_sub(UELE_MAXHP, ed->elemental.max_hp);
  14852. getunitdata_sub(UELE_SP, ed->elemental.sp);
  14853. getunitdata_sub(UELE_MAXSP, ed->elemental.max_sp);
  14854. getunitdata_sub(UELE_MASTERCID, ed->elemental.char_id);
  14855. getunitdata_sub(UELE_MAPID, ed->bl.m);
  14856. getunitdata_sub(UELE_X, ed->bl.x);
  14857. getunitdata_sub(UELE_Y, ed->bl.y);
  14858. getunitdata_sub(UELE_LIFETIME, ed->elemental.life_time);
  14859. getunitdata_sub(UELE_MODE, ed->elemental.mode);
  14860. getunitdata_sub(UELE_SP, ed->base_status.speed);
  14861. getunitdata_sub(UELE_LOOKDIR, ed->ud.dir);
  14862. getunitdata_sub(UELE_CANMOVETICK, ed->ud.canmove_tick);
  14863. getunitdata_sub(UELE_STR, ed->base_status.str);
  14864. getunitdata_sub(UELE_AGI, ed->base_status.agi);
  14865. getunitdata_sub(UELE_VIT, ed->base_status.vit);
  14866. getunitdata_sub(UELE_INT, ed->base_status.int_);
  14867. getunitdata_sub(UELE_DEX, ed->base_status.dex);
  14868. getunitdata_sub(UELE_LUK, ed->base_status.luk);
  14869. getunitdata_sub(UELE_DMGIMMUNE, ed->ud.immune_attack);
  14870. getunitdata_sub(UELE_ATKRANGE, ed->base_status.rhw.range);
  14871. getunitdata_sub(UELE_ATKMIN, ed->base_status.rhw.atk);
  14872. getunitdata_sub(UELE_ATKMAX, ed->base_status.rhw.atk2);
  14873. getunitdata_sub(UELE_MATKMIN, ed->base_status.matk_min);
  14874. getunitdata_sub(UELE_MATKMAX, ed->base_status.matk_max);
  14875. getunitdata_sub(UELE_DEF, ed->base_status.def);
  14876. getunitdata_sub(UELE_MDEF, ed->base_status.mdef);
  14877. getunitdata_sub(UELE_HIT, ed->base_status.hit);
  14878. getunitdata_sub(UELE_FLEE, ed->base_status.flee);
  14879. getunitdata_sub(UELE_PDODGE, ed->base_status.flee2);
  14880. getunitdata_sub(UELE_CRIT, ed->base_status.cri);
  14881. getunitdata_sub(UELE_RACE, ed->base_status.race);
  14882. getunitdata_sub(UELE_ELETYPE, ed->base_status.def_ele);
  14883. getunitdata_sub(UELE_ELELEVEL, ed->base_status.ele_lv);
  14884. getunitdata_sub(UELE_AMOTION, ed->base_status.amotion);
  14885. getunitdata_sub(UELE_ADELAY, ed->base_status.adelay);
  14886. getunitdata_sub(UELE_DMOTION, ed->base_status.dmotion);
  14887. break;
  14888. case BL_NPC:
  14889. if (!nd) {
  14890. ShowWarning("buildin_getunitdata: Error in finding object BL_NPC!\n");
  14891. return SCRIPT_CMD_FAILURE;
  14892. }
  14893. getunitdata_sub(UNPC_DISPLAY, nd->class_);
  14894. getunitdata_sub(UNPC_LEVEL, nd->level);
  14895. getunitdata_sub(UNPC_HP, nd->status.hp);
  14896. getunitdata_sub(UNPC_MAXHP, nd->status.max_hp);
  14897. getunitdata_sub(UNPC_MAPID, nd->bl.m);
  14898. getunitdata_sub(UNPC_X, nd->bl.x);
  14899. getunitdata_sub(UNPC_Y, nd->bl.y);
  14900. getunitdata_sub(UNPC_LOOKDIR, nd->ud.dir);
  14901. getunitdata_sub(UNPC_STR, nd->status.str);
  14902. getunitdata_sub(UNPC_AGI, nd->status.agi);
  14903. getunitdata_sub(UNPC_VIT, nd->status.vit);
  14904. getunitdata_sub(UNPC_INT, nd->status.int_);
  14905. getunitdata_sub(UNPC_DEX, nd->status.dex);
  14906. getunitdata_sub(UNPC_LUK, nd->status.luk);
  14907. getunitdata_sub(UNPC_PLUSALLSTAT, nd->stat_point);
  14908. getunitdata_sub(UNPC_DMGIMMUNE, nd->ud.immune_attack);
  14909. getunitdata_sub(UNPC_ATKRANGE, nd->status.rhw.range);
  14910. getunitdata_sub(UNPC_ATKMIN, nd->status.rhw.atk);
  14911. getunitdata_sub(UNPC_ATKMAX, nd->status.rhw.atk2);
  14912. getunitdata_sub(UNPC_MATKMIN, nd->status.matk_min);
  14913. getunitdata_sub(UNPC_MATKMAX, nd->status.matk_max);
  14914. getunitdata_sub(UNPC_DEF, nd->status.def);
  14915. getunitdata_sub(UNPC_MDEF, nd->status.mdef);
  14916. getunitdata_sub(UNPC_HIT, nd->status.hit);
  14917. getunitdata_sub(UNPC_FLEE, nd->status.flee);
  14918. getunitdata_sub(UNPC_PDODGE, nd->status.flee2);
  14919. getunitdata_sub(UNPC_CRIT, nd->status.cri);
  14920. getunitdata_sub(UNPC_RACE, nd->status.race);
  14921. getunitdata_sub(UNPC_ELETYPE, nd->status.def_ele);
  14922. getunitdata_sub(UNPC_ELELEVEL, nd->status.ele_lv);
  14923. getunitdata_sub(UNPC_AMOTION, nd->status.amotion);
  14924. getunitdata_sub(UNPC_ADELAY, nd->status.adelay);
  14925. getunitdata_sub(UNPC_DMOTION, nd->status.dmotion);
  14926. break;
  14927. default:
  14928. ShowWarning("buildin_getunitdata: Unknown object type!\n");
  14929. return SCRIPT_CMD_FAILURE;
  14930. }
  14931. #undef getunitdata_sub
  14932. return SCRIPT_CMD_SUCCESS;
  14933. }
  14934. /// Changes the live data of a bl.
  14935. ///
  14936. /// setunitdata <unit id>,<type>,<value>;
  14937. BUILDIN_FUNC(setunitdata)
  14938. {
  14939. struct block_list* bl = NULL;
  14940. struct script_data* data;
  14941. const char *mapname = NULL;
  14942. TBL_MOB* md = NULL;
  14943. TBL_HOM* hd = NULL;
  14944. TBL_MER* mc = NULL;
  14945. TBL_PET* pd = NULL;
  14946. TBL_ELEM* ed = NULL;
  14947. TBL_NPC* nd = NULL;
  14948. int type, value = 0;
  14949. bl = map_id2bl(script_getnum(st, 2));
  14950. if (!bl) {
  14951. ShowWarning("buildin_setunitdata: Error in finding object with given game ID %d!\n", script_getnum(st, 2));
  14952. return SCRIPT_CMD_FAILURE;
  14953. }
  14954. switch (bl->type) {
  14955. case BL_MOB: md = map_id2md(bl->id); break;
  14956. case BL_HOM: hd = map_id2hd(bl->id); break;
  14957. case BL_PET: pd = map_id2pd(bl->id); break;
  14958. case BL_MER: mc = map_id2mc(bl->id); break;
  14959. case BL_ELEM: ed = map_id2ed(bl->id); break;
  14960. case BL_NPC:
  14961. nd = map_id2nd(bl->id);
  14962. if (!nd->status.hp)
  14963. status_calc_npc(nd, SCO_FIRST);
  14964. else
  14965. status_calc_npc(nd, SCO_NONE);
  14966. break;
  14967. default:
  14968. ShowError("buildin_setunitdata: Invalid object!\n");
  14969. return SCRIPT_CMD_FAILURE;
  14970. }
  14971. type = script_getnum(st, 3);
  14972. data = script_getdata(st, 4);
  14973. get_val(st, data);
  14974. if (type == 5 && data_isstring(data))
  14975. mapname = conv_str(st, data);
  14976. else if (data_isint(data))
  14977. value = conv_num(st, data);
  14978. else {
  14979. ShowError("buildin_setunitdata: Invalid data type for argument #3 (%d).\n", data->type);
  14980. return SCRIPT_CMD_FAILURE;
  14981. }
  14982. switch (bl->type) {
  14983. case BL_MOB:
  14984. if (!md) {
  14985. ShowWarning("buildin_setunitdata: Error in finding object BL_MOB!\n");
  14986. return SCRIPT_CMD_FAILURE;
  14987. }
  14988. switch (type) {
  14989. case UMOB_SIZE: md->status.size = (unsigned char)value; break;
  14990. case UMOB_LEVEL: md->level = (unsigned short)value; break;
  14991. case UMOB_HP: status_set_hp(bl, (unsigned int)value, 0); clif_charnameack(0, &md->bl); break;
  14992. case UMOB_MAXHP: status_set_maxhp(bl, (unsigned int)value, 0); clif_charnameack(0, &md->bl); break;
  14993. case UMOB_MASTERAID: md->master_id = value; break;
  14994. case UMOB_MAPID: if (mapname) value = map_mapname2mapid(mapname); unit_warp(bl, (short)value, 0, 0, CLR_TELEPORT); break;
  14995. case UMOB_X: if (!unit_walktoxy(bl, (short)value, md->bl.y, 2)) unit_movepos(bl, (short)value, md->bl.y, 0, 0); break;
  14996. case UMOB_Y: if (!unit_walktoxy(bl, md->bl.x, (short)value, 2)) unit_movepos(bl, md->bl.x, (short)value, 0, 0); break;
  14997. case UMOB_SPEED: md->status.speed = (unsigned short)value; status_calc_misc(bl, &md->status, md->level); break;
  14998. case UMOB_MODE: md->status.mode = (enum e_mode)value; break;
  14999. case UMOB_AI: md->special_state.ai = (enum mob_ai)value; break;
  15000. case UMOB_SCOPTION: md->sc.option = (unsigned short)value; break;
  15001. case UMOB_SEX: md->vd->sex = (char)value; break;
  15002. case UMOB_CLASS: status_set_viewdata(bl, (unsigned short)value); break;
  15003. case UMOB_HAIRSTYLE: clif_changelook(bl, LOOK_HAIR, (unsigned short)value); break;
  15004. case UMOB_HAIRCOLOR: clif_changelook(bl, LOOK_HAIR_COLOR, (unsigned short)value); break;
  15005. case UMOB_HEADBOTTOM: clif_changelook(bl, LOOK_HEAD_BOTTOM, (unsigned short)value); break;
  15006. case UMOB_HEADMIDDLE: clif_changelook(bl, LOOK_HEAD_MID, (unsigned short)value); break;
  15007. case UMOB_HEADTOP: clif_changelook(bl, LOOK_HEAD_TOP, (unsigned short)value); break;
  15008. case UMOB_CLOTHCOLOR: clif_changelook(bl, LOOK_CLOTHES_COLOR, (unsigned short)value); break;
  15009. case UMOB_SHIELD: clif_changelook(bl, LOOK_SHIELD, (unsigned short)value); break;
  15010. case UMOB_WEAPON: clif_changelook(bl, LOOK_WEAPON, (unsigned short)value); break;
  15011. case UMOB_LOOKDIR: unit_setdir(bl, (uint8)value); break;
  15012. case UMOB_CANMOVETICK: md->ud.canmove_tick = value > 0 ? (unsigned int)value : 0; break;
  15013. case UMOB_STR: md->status.str = (unsigned short)value; status_calc_misc(bl, &md->status, md->level); break;
  15014. case UMOB_AGI: md->status.agi = (unsigned short)value; status_calc_misc(bl, &md->status, md->level); break;
  15015. case UMOB_VIT: md->status.vit = (unsigned short)value; status_calc_misc(bl, &md->status, md->level); break;
  15016. case UMOB_INT: md->status.int_ = (unsigned short)value; status_calc_misc(bl, &md->status, md->level); break;
  15017. case UMOB_DEX: md->status.dex = (unsigned short)value; status_calc_misc(bl, &md->status, md->level); break;
  15018. case UMOB_LUK: md->status.luk = (unsigned short)value; status_calc_misc(bl, &md->status, md->level); break;
  15019. case UMOB_SLAVECPYMSTRMD:
  15020. if (value > 0) {
  15021. TBL_MOB *md2 = NULL;
  15022. if (!md->master_id || !(md2 = map_id2md(md->master_id))) {
  15023. ShowWarning("buildin_setunitdata: Trying to set UMOB_SLAVECPYMSTRMD on mob without master!\n");
  15024. break;
  15025. }
  15026. md->status.mode = md2->status.mode;
  15027. md->state.copy_master_mode = 1;
  15028. } else
  15029. md->state.copy_master_mode = 0;
  15030. break;
  15031. case UMOB_DMGIMMUNE: md->ud.immune_attack = (bool)value > 0 ? 1 : 0; break;
  15032. case UMOB_ATKRANGE: md->status.rhw.range = (unsigned short)value; break;
  15033. case UMOB_ATKMIN: md->status.rhw.atk = (unsigned short)value; break;
  15034. case UMOB_ATKMAX: md->status.rhw.atk2 = (unsigned short)value; break;
  15035. case UMOB_MATKMIN: md->status.matk_min = (unsigned short)value; break;
  15036. case UMOB_MATKMAX: md->status.matk_max = (unsigned short)value; break;
  15037. case UMOB_DEF: md->status.def = (defType)value; break;
  15038. case UMOB_MDEF: md->status.mdef = (defType)value; break;
  15039. case UMOB_HIT: md->status.hit = (short)value; break;
  15040. case UMOB_FLEE: md->status.flee = (short)value; break;
  15041. case UMOB_PDODGE: md->status.flee2 = (short)value; break;
  15042. case UMOB_CRIT: md->status.cri = (short)value; break;
  15043. case UMOB_RACE: md->status.race = (unsigned char)value; break;
  15044. case UMOB_ELETYPE: md->status.def_ele = (unsigned char)value; break;
  15045. case UMOB_ELELEVEL: md->status.ele_lv = (unsigned char)value; break;
  15046. case UMOB_AMOTION: md->status.amotion = (short)value; break;
  15047. case UMOB_ADELAY: md->status.adelay = (short)value; break;
  15048. case UMOB_DMOTION: md->status.dmotion = (short)value; break;
  15049. default:
  15050. ShowError("buildin_setunitdata: Unknown data identifier %d for BL_MOB.\n", type);
  15051. return SCRIPT_CMD_FAILURE;
  15052. }
  15053. break;
  15054. case BL_HOM:
  15055. if (!hd) {
  15056. ShowWarning("buildin_setunitdata: Error in finding object BL_HOM!\n");
  15057. return SCRIPT_CMD_FAILURE;
  15058. }
  15059. switch (type) {
  15060. case UHOM_SIZE: hd->base_status.size = (unsigned char)value; break;
  15061. case UHOM_LEVEL: hd->homunculus.level = (unsigned short)value; break;
  15062. case UHOM_HP: status_set_hp(bl, (unsigned int)value, 0); break;
  15063. case UHOM_MAXHP: status_set_maxhp(bl, (unsigned int)value, 0); break;
  15064. case UHOM_SP: status_set_sp(bl, (unsigned int)value, 0); break;
  15065. case UHOM_MAXSP: status_set_maxsp(bl, (unsigned int)value, 0); break;
  15066. case UHOM_MASTERCID: hd->homunculus.char_id = (uint32)value; break;
  15067. case UHOM_MAPID: if (mapname) value = map_mapname2mapid(mapname); unit_warp(bl, (short)value, 0, 0, CLR_TELEPORT); break;
  15068. case UHOM_X: if (!unit_walktoxy(bl, (short)value, hd->bl.y, 2)) unit_movepos(bl, (short)value, hd->bl.y, 0, 0); break;
  15069. case UHOM_Y: if (!unit_walktoxy(bl, hd->bl.x, (short)value, 2)) unit_movepos(bl, hd->bl.x, (short)value, 0, 0); break;
  15070. case UHOM_HUNGER: hd->homunculus.hunger = (short)value; clif_send_homdata(map_charid2sd(hd->homunculus.char_id), SP_HUNGRY, hd->homunculus.hunger); break;
  15071. case UHOM_INTIMACY: hom_increase_intimacy(hd, (unsigned int)value); clif_send_homdata(map_charid2sd(hd->homunculus.char_id), SP_INTIMATE, hd->homunculus.intimacy / 100); break;
  15072. case UHOM_SPEED: hd->base_status.speed = (unsigned short)value; status_calc_misc(bl, &hd->base_status, hd->homunculus.level); break;
  15073. case UHOM_LOOKDIR: unit_setdir(bl, (uint8)value); break;
  15074. case UHOM_CANMOVETICK: hd->ud.canmove_tick = value > 0 ? (unsigned int)value : 0; break;
  15075. case UHOM_STR: hd->base_status.str = (unsigned short)value; status_calc_misc(bl, &hd->base_status, hd->homunculus.level); break;
  15076. case UHOM_AGI: hd->base_status.agi = (unsigned short)value; status_calc_misc(bl, &hd->base_status, hd->homunculus.level); break;
  15077. case UHOM_VIT: hd->base_status.vit = (unsigned short)value; status_calc_misc(bl, &hd->base_status, hd->homunculus.level); break;
  15078. case UHOM_INT: hd->base_status.int_ = (unsigned short)value; status_calc_misc(bl, &hd->base_status, hd->homunculus.level); break;
  15079. case UHOM_DEX: hd->base_status.dex = (unsigned short)value; status_calc_misc(bl, &hd->base_status, hd->homunculus.level); break;
  15080. case UHOM_LUK: hd->base_status.luk = (unsigned short)value; status_calc_misc(bl, &hd->base_status, hd->homunculus.level); break;
  15081. case UHOM_DMGIMMUNE: hd->ud.immune_attack = (bool)value > 0 ? 1 : 0; break;
  15082. case UHOM_ATKRANGE: hd->base_status.rhw.range = (unsigned short)value; break;
  15083. case UHOM_ATKMIN: hd->base_status.rhw.atk = (unsigned short)value; break;
  15084. case UHOM_ATKMAX: hd->base_status.rhw.atk2 = (unsigned short)value; break;
  15085. case UHOM_MATKMIN: hd->base_status.matk_min = (unsigned short)value; break;
  15086. case UHOM_MATKMAX: hd->base_status.matk_max = (unsigned short)value; break;
  15087. case UHOM_DEF: hd->base_status.def = (defType)value; break;
  15088. case UHOM_MDEF: hd->base_status.mdef = (defType)value; break;
  15089. case UHOM_HIT: hd->base_status.hit = (short)value; break;
  15090. case UHOM_FLEE: hd->base_status.flee = (short)value; break;
  15091. case UHOM_PDODGE: hd->base_status.flee2 = (short)value; break;
  15092. case UHOM_CRIT: hd->base_status.cri = (short)value; break;
  15093. case UHOM_RACE: hd->base_status.race = (unsigned char)value; break;
  15094. case UHOM_ELETYPE: hd->base_status.def_ele = (unsigned char)value; break;
  15095. case UHOM_ELELEVEL: hd->base_status.ele_lv = (unsigned char)value; break;
  15096. case UHOM_AMOTION: hd->base_status.amotion = (short)value; break;
  15097. case UHOM_ADELAY: hd->base_status.adelay = (short)value; break;
  15098. case UHOM_DMOTION: hd->base_status.dmotion = (short)value; break;
  15099. default:
  15100. ShowError("buildin_setunitdata: Unknown data identifier %d for BL_HOM.\n", type);
  15101. return SCRIPT_CMD_FAILURE;
  15102. }
  15103. break;
  15104. case BL_PET:
  15105. if (!pd) {
  15106. ShowWarning("buildin_setunitdata: Error in finding object BL_PET!\n");
  15107. return SCRIPT_CMD_FAILURE;
  15108. }
  15109. switch (type) {
  15110. case UPET_SIZE: pd->status.size = (unsigned char)value; break;
  15111. case UPET_LEVEL: pd->pet.level = (unsigned short)value; break;
  15112. case UPET_HP: status_set_hp(bl, (unsigned int)value, 0); break;
  15113. case UPET_MAXHP: status_set_maxhp(bl, (unsigned int)value, 0); break;
  15114. case UPET_MASTERAID: pd->pet.account_id = (unsigned int)value; break;
  15115. case UPET_MAPID: if (mapname) value = map_mapname2mapid(mapname); unit_warp(bl, (short)value, 0, 0, CLR_TELEPORT); break;
  15116. case UPET_X: if (!unit_walktoxy(bl, (short)value, pd->bl.y, 2)) unit_movepos(bl, (short)value, md->bl.y, 0, 0); break;
  15117. case UPET_Y: if (!unit_walktoxy(bl, pd->bl.x, (short)value, 2)) unit_movepos(bl, pd->bl.x, (short)value, 0, 0); break;
  15118. case UPET_HUNGER: pd->pet.hungry = (short)value; clif_send_petdata(map_id2sd(pd->pet.account_id), pd, 2, pd->pet.hungry); break;
  15119. case UPET_INTIMACY: pet_set_intimate(pd, (unsigned int)value); clif_send_petdata(map_id2sd(pd->pet.account_id), pd, 1, pd->pet.intimate); break;
  15120. case UPET_SPEED: pd->status.speed = (unsigned short)value; status_calc_misc(bl, &pd->status, pd->pet.level); break;
  15121. case UPET_LOOKDIR: unit_setdir(bl, (uint8)value); break;
  15122. case UPET_CANMOVETICK: pd->ud.canmove_tick = value > 0 ? (unsigned int)value : 0; break;
  15123. case UPET_STR: pd->status.str = (unsigned short)value; status_calc_misc(bl, &pd->status, pd->pet.level); break;
  15124. case UPET_AGI: pd->status.agi = (unsigned short)value; status_calc_misc(bl, &pd->status, pd->pet.level); break;
  15125. case UPET_VIT: pd->status.vit = (unsigned short)value; status_calc_misc(bl, &pd->status, pd->pet.level); break;
  15126. case UPET_INT: pd->status.int_ = (unsigned short)value; status_calc_misc(bl, &pd->status, pd->pet.level); break;
  15127. case UPET_DEX: pd->status.dex = (unsigned short)value; status_calc_misc(bl, &pd->status, pd->pet.level); break;
  15128. case UPET_LUK: pd->status.luk = (unsigned short)value; status_calc_misc(bl, &pd->status, pd->pet.level); break;
  15129. case UPET_DMGIMMUNE: pd->ud.immune_attack = (bool)value > 0 ? 1 : 0; break;
  15130. case UPET_ATKRANGE: pd->status.rhw.range = (unsigned short)value; break;
  15131. case UPET_ATKMIN: pd->status.rhw.atk = (unsigned short)value; break;
  15132. case UPET_ATKMAX: pd->status.rhw.atk2 = (unsigned short)value; break;
  15133. case UPET_MATKMIN: pd->status.matk_min = (unsigned short)value; break;
  15134. case UPET_MATKMAX: pd->status.matk_max = (unsigned short)value; break;
  15135. case UPET_DEF: pd->status.def = (defType)value; break;
  15136. case UPET_MDEF: pd->status.mdef = (defType)value; break;
  15137. case UPET_HIT: pd->status.hit = (short)value; break;
  15138. case UPET_FLEE: pd->status.flee = (short)value; break;
  15139. case UPET_PDODGE: pd->status.flee2 = (short)value; break;
  15140. case UPET_CRIT: pd->status.cri = (short)value; break;
  15141. case UPET_RACE: pd->status.race = (unsigned char)value; break;
  15142. case UPET_ELETYPE: pd->status.def_ele = (unsigned char)value; break;
  15143. case UPET_ELELEVEL: pd->status.ele_lv = (unsigned char)value; break;
  15144. case UPET_AMOTION: pd->status.amotion = (short)value; break;
  15145. case UPET_ADELAY: pd->status.adelay = (short)value; break;
  15146. case UPET_DMOTION: pd->status.dmotion = (short)value; break;
  15147. default:
  15148. ShowError("buildin_setunitdata: Unknown data identifier %d for BL_PET.\n", type);
  15149. return SCRIPT_CMD_FAILURE;
  15150. }
  15151. break;
  15152. case BL_MER:
  15153. if (!mc) {
  15154. ShowWarning("buildin_setunitdata: Error in finding object BL_MER!\n");
  15155. return SCRIPT_CMD_FAILURE;
  15156. }
  15157. switch (type) {
  15158. case UMER_SIZE: mc->base_status.size = (unsigned char)value; break;
  15159. case UMER_HP: status_set_hp(bl, (unsigned int)value, 0); break;
  15160. case UMER_MAXHP: status_set_maxhp(bl, (unsigned int)value, 0); break;
  15161. case UMER_MASTERCID: mc->mercenary.char_id = (uint32)value; break;
  15162. case UMER_MAPID: if (mapname) value = map_mapname2mapid(mapname); unit_warp(bl, (short)value, 0, 0, CLR_TELEPORT); break;
  15163. case UMER_X: if (!unit_walktoxy(bl, (short)value, mc->bl.y, 2)) unit_movepos(bl, (short)value, mc->bl.y, 0, 0); break;
  15164. case UMER_Y: if (!unit_walktoxy(bl, mc->bl.x, (short)value, 2)) unit_movepos(bl, mc->bl.x, (short)value, 0, 0); break;
  15165. case UMER_KILLCOUNT: mc->mercenary.kill_count = (unsigned int)value; break;
  15166. case UMER_LIFETIME: mc->mercenary.life_time = (unsigned int)value; break;
  15167. case UMER_SPEED: mc->base_status.speed = (unsigned short)value; status_calc_misc(bl, &mc->base_status, mc->db->lv); break;
  15168. case UMER_LOOKDIR: unit_setdir(bl, (uint8)value); break;
  15169. case UMER_CANMOVETICK: mc->ud.canmove_tick = value > 0 ? (unsigned int)value : 0; break;
  15170. case UMER_STR: mc->base_status.str = (unsigned short)value; status_calc_misc(bl, &mc->base_status, mc->db->lv); break;
  15171. case UMER_AGI: mc->base_status.agi = (unsigned short)value; status_calc_misc(bl, &mc->base_status, mc->db->lv); break;
  15172. case UMER_VIT: mc->base_status.vit = (unsigned short)value; status_calc_misc(bl, &mc->base_status, mc->db->lv); break;
  15173. case UMER_INT: mc->base_status.int_ = (unsigned short)value; status_calc_misc(bl, &mc->base_status, mc->db->lv); break;
  15174. case UMER_DEX: mc->base_status.dex = (unsigned short)value; status_calc_misc(bl, &mc->base_status, mc->db->lv); break;
  15175. case UMER_LUK: mc->base_status.luk = (unsigned short)value; status_calc_misc(bl, &mc->base_status, mc->db->lv); break;
  15176. case UMER_DMGIMMUNE: mc->ud.immune_attack = (bool)value > 0 ? 1 : 0; break;
  15177. case UMER_ATKRANGE: mc->base_status.rhw.range = (unsigned short)value; break;
  15178. case UMER_ATKMIN: mc->base_status.rhw.atk = (unsigned short)value; break;
  15179. case UMER_ATKMAX: mc->base_status.rhw.atk2 = (unsigned short)value; break;
  15180. case UMER_MATKMIN: mc->base_status.matk_min = (unsigned short)value; break;
  15181. case UMER_MATKMAX: mc->base_status.matk_max = (unsigned short)value; break;
  15182. case UMER_DEF: mc->base_status.def = (defType)value; break;
  15183. case UMER_MDEF: mc->base_status.mdef = (defType)value; break;
  15184. case UMER_HIT: mc->base_status.hit = (short)value; break;
  15185. case UMER_FLEE: mc->base_status.flee = (short)value; break;
  15186. case UMER_PDODGE: mc->base_status.flee2 = (short)value; break;
  15187. case UMER_CRIT: mc->base_status.cri = (short)value; break;
  15188. case UMER_RACE: mc->base_status.race = (unsigned char)value; break;
  15189. case UMER_ELETYPE: mc->base_status.def_ele = (unsigned char)value; break;
  15190. case UMER_ELELEVEL: mc->base_status.ele_lv = (unsigned char)value; break;
  15191. case UMER_AMOTION: mc->base_status.amotion = (short)value; break;
  15192. case UMER_ADELAY: mc->base_status.adelay = (short)value; break;
  15193. case UMER_DMOTION: mc->base_status.dmotion = (short)value; break;
  15194. default:
  15195. ShowError("buildin_setunitdata: Unknown data identifier %d for BL_MER.\n", type);
  15196. return SCRIPT_CMD_FAILURE;
  15197. }
  15198. break;
  15199. case BL_ELEM:
  15200. if (!ed) {
  15201. ShowWarning("buildin_setunitdata: Error in finding object BL_ELEM!\n");
  15202. return SCRIPT_CMD_FAILURE;
  15203. }
  15204. switch (type) {
  15205. case UELE_SIZE: ed->base_status.size = (unsigned char)value; break;
  15206. case UELE_HP: status_set_hp(bl, (unsigned int)value, 0); break;
  15207. case UELE_MAXHP: status_set_maxhp(bl, (unsigned int)value, 0); break;
  15208. case UELE_SP: status_set_sp(bl, (unsigned int)value, 0); break;
  15209. case UELE_MAXSP: status_set_maxsp(bl, (unsigned int)value, 0); break;
  15210. case UELE_MASTERCID: ed->elemental.char_id = (uint32)value; break;
  15211. case UELE_MAPID: if (mapname) value = map_mapname2mapid(mapname); unit_warp(bl, (short)value, 0, 0, CLR_TELEPORT); break;
  15212. case UELE_X: if (!unit_walktoxy(bl, (short)value, ed->bl.y, 2)) unit_movepos(bl, (short)value, ed->bl.y, 0, 0); break;
  15213. case UELE_Y: if (!unit_walktoxy(bl, ed->bl.x, (short)value, 2)) unit_movepos(bl, ed->bl.x, (short)value, 0, 0); break;
  15214. case UELE_LIFETIME: ed->elemental.life_time = (unsigned int)value; break;
  15215. case UELE_MODE: ed->elemental.mode = (enum e_mode)value; break;
  15216. case UELE_SPEED: ed->base_status.speed = (unsigned short)value; status_calc_misc(bl, &ed->base_status, ed->db->lv); break;
  15217. case UELE_LOOKDIR: unit_setdir(bl, (uint8)value); break;
  15218. case UELE_CANMOVETICK: ed->ud.canmove_tick = value > 0 ? (unsigned int)value : 0; break;
  15219. case UELE_STR: ed->base_status.str = (unsigned short)value; status_calc_misc(bl, &ed->base_status, ed->db->lv); break;
  15220. case UELE_AGI: ed->base_status.agi = (unsigned short)value; status_calc_misc(bl, &ed->base_status, ed->db->lv); break;
  15221. case UELE_VIT: ed->base_status.vit = (unsigned short)value; status_calc_misc(bl, &ed->base_status, ed->db->lv); break;
  15222. case UELE_INT: ed->base_status.int_ = (unsigned short)value; status_calc_misc(bl, &ed->base_status, ed->db->lv); break;
  15223. case UELE_DEX: ed->base_status.dex = (unsigned short)value; status_calc_misc(bl, &ed->base_status, ed->db->lv); break;
  15224. case UELE_LUK: ed->base_status.luk = (unsigned short)value; status_calc_misc(bl, &ed->base_status, ed->db->lv); break;
  15225. case UELE_DMGIMMUNE: ed->ud.immune_attack = (bool)value > 0 ? 1 : 0; break;
  15226. case UELE_ATKRANGE: ed->base_status.rhw.range = (unsigned short)value; break;
  15227. case UELE_ATKMIN: ed->base_status.rhw.atk = (unsigned short)value; break;
  15228. case UELE_ATKMAX: ed->base_status.rhw.atk2 = (unsigned short)value; break;
  15229. case UELE_MATKMIN: ed->base_status.matk_min = (unsigned short)value; break;
  15230. case UELE_MATKMAX: ed->base_status.matk_max = (unsigned short)value; break;
  15231. case UELE_DEF: ed->base_status.def = (defType)value; break;
  15232. case UELE_MDEF: ed->base_status.mdef = (defType)value; break;
  15233. case UELE_HIT: ed->base_status.hit = (short)value; break;
  15234. case UELE_FLEE: ed->base_status.flee = (short)value; break;
  15235. case UELE_PDODGE: ed->base_status.flee2 = (short)value; break;
  15236. case UELE_CRIT: ed->base_status.cri = (short)value; break;
  15237. case UELE_RACE: ed->base_status.race = (unsigned char)value; break;
  15238. case UELE_ELETYPE: ed->base_status.def_ele = (unsigned char)value; break;
  15239. case UELE_ELELEVEL: ed->base_status.ele_lv = (unsigned char)value; break;
  15240. case UELE_AMOTION: ed->base_status.amotion = (short)value; break;
  15241. case UELE_ADELAY: ed->base_status.adelay = (short)value; break;
  15242. case UELE_DMOTION: ed->base_status.dmotion = (short)value; break;
  15243. default:
  15244. ShowError("buildin_setunitdata: Unknown data identifier %d for BL_ELEM.\n", type);
  15245. return SCRIPT_CMD_FAILURE;
  15246. }
  15247. break;
  15248. case BL_NPC:
  15249. if (!nd) {
  15250. ShowWarning("buildin_setunitdata: Error in finding object BL_NPC!\n");
  15251. return SCRIPT_CMD_FAILURE;
  15252. }
  15253. switch (type) {
  15254. case UNPC_DISPLAY: status_set_viewdata(bl, (unsigned short)value); break;
  15255. case UNPC_LEVEL: nd->level = (unsigned int)value; break;
  15256. case UNPC_HP: status_set_hp(bl, (unsigned int)value, 0); break;
  15257. case UNPC_MAXHP: status_set_maxhp(bl, (unsigned int)value, 0); break;
  15258. case UNPC_MAPID: if (mapname) value = map_mapname2mapid(mapname); unit_warp(bl, (short)value, 0, 0, CLR_TELEPORT); break;
  15259. case UNPC_X: if (!unit_walktoxy(bl, (short)value, nd->bl.y, 2)) unit_movepos(bl, (short)value, nd->bl.x, 0, 0); break;
  15260. case UNPC_Y: if (!unit_walktoxy(bl, nd->bl.x, (short)value, 2)) unit_movepos(bl, nd->bl.x, (short)value, 0, 0); break;
  15261. case UNPC_LOOKDIR: unit_setdir(bl, (uint8)value); break;
  15262. case UNPC_STR: nd->params.str = (unsigned short)value; status_calc_misc(bl, &nd->status, nd->level); break;
  15263. case UNPC_AGI: nd->params.agi = (unsigned short)value; status_calc_misc(bl, &nd->status, nd->level); break;
  15264. case UNPC_VIT: nd->params.vit = (unsigned short)value; status_calc_misc(bl, &nd->status, nd->level); break;
  15265. case UNPC_INT: nd->params.int_ = (unsigned short)value; status_calc_misc(bl, &nd->status, nd->level); break;
  15266. case UNPC_DEX: nd->params.dex = (unsigned short)value; status_calc_misc(bl, &nd->status, nd->level); break;
  15267. case UNPC_LUK: nd->params.luk = (unsigned short)value; status_calc_misc(bl, &nd->status, nd->level); break;
  15268. case UNPC_PLUSALLSTAT: nd->stat_point = (unsigned int)value; break;
  15269. case UNPC_ATKRANGE: nd->status.rhw.range = (unsigned short)value; break;
  15270. case UNPC_ATKMIN: nd->status.rhw.atk = (unsigned short)value; break;
  15271. case UNPC_ATKMAX: nd->status.rhw.atk2 = (unsigned short)value; break;
  15272. case UNPC_MATKMIN: nd->status.matk_min = (unsigned short)value; break;
  15273. case UNPC_MATKMAX: nd->status.matk_max = (unsigned short)value; break;
  15274. case UNPC_DEF: nd->status.def = (defType)value; break;
  15275. case UNPC_MDEF: nd->status.mdef = (defType)value; break;
  15276. case UNPC_HIT: nd->status.hit = (short)value; break;
  15277. case UNPC_FLEE: nd->status.flee = (short)value; break;
  15278. case UNPC_PDODGE: nd->status.flee2 = (short)value; break;
  15279. case UNPC_CRIT: nd->status.cri = (short)value; break;
  15280. case UNPC_RACE: nd->status.race = (unsigned char)value; break;
  15281. case UNPC_ELETYPE: nd->status.def_ele = (unsigned char)value; break;
  15282. case UNPC_ELELEVEL: nd->status.ele_lv = (unsigned char)value; break;
  15283. case UNPC_AMOTION: nd->status.amotion = (short)value; break;
  15284. case UNPC_ADELAY: nd->status.adelay = (short)value; break;
  15285. case UNPC_DMOTION: nd->status.dmotion = (short)value; break;
  15286. default:
  15287. ShowError("buildin_setunitdata: Unknown data identifier %d for BL_NPC.\n", type);
  15288. return SCRIPT_CMD_FAILURE;
  15289. }
  15290. break;
  15291. default:
  15292. ShowWarning("buildin_setunitdata: Unknown object type!\n");
  15293. return SCRIPT_CMD_FAILURE;
  15294. }
  15295. // Client information updates
  15296. switch (bl->type) {
  15297. case BL_HOM:
  15298. clif_send_homdata(hd->master, SP_ACK, 0);
  15299. break;
  15300. case BL_PET:
  15301. clif_send_petstatus(pd->master);
  15302. break;
  15303. case BL_MER:
  15304. clif_mercenary_info(map_charid2sd(mc->mercenary.char_id));
  15305. clif_mercenary_skillblock(map_charid2sd(mc->mercenary.char_id));
  15306. break;
  15307. case BL_ELEM:
  15308. clif_elemental_info(ed->master);
  15309. break;
  15310. }
  15311. return SCRIPT_CMD_SUCCESS;
  15312. }
  15313. /// Gets the name of a bl.
  15314. /// Supported types are [MOB|HOM|PET|NPC].
  15315. /// MER and ELEM don't support custom names.
  15316. ///
  15317. /// getunitname <unit id>;
  15318. BUILDIN_FUNC(getunitname)
  15319. {
  15320. struct block_list* bl = NULL;
  15321. bl = map_id2bl(script_getnum(st, 2));
  15322. if (!bl) {
  15323. ShowWarning("buildin_getunitname: Error in finding object with given game ID %d!\n", script_getnum(st, 2));
  15324. return SCRIPT_CMD_FAILURE;
  15325. }
  15326. script_pushstrcopy(st, status_get_name(bl));
  15327. return SCRIPT_CMD_SUCCESS;
  15328. }
  15329. /// Changes the name of a bl.
  15330. /// Supported types are [MOB|HOM|PET].
  15331. /// For NPC see 'setnpcdisplay', MER and ELEM don't support custom names.
  15332. ///
  15333. /// setunitname <unit id>,<name>;
  15334. BUILDIN_FUNC(setunitname)
  15335. {
  15336. struct block_list* bl = NULL;
  15337. TBL_MOB* md = NULL;
  15338. TBL_HOM* hd = NULL;
  15339. TBL_PET* pd = NULL;
  15340. bl = map_id2bl(script_getnum(st, 2));
  15341. if (!bl) {
  15342. ShowWarning("buildin_setunitname: Error in finding object with given game ID %d!\n", script_getnum(st, 2));
  15343. return SCRIPT_CMD_FAILURE;
  15344. }
  15345. switch (bl->type) {
  15346. case BL_MOB: md = map_id2md(bl->id); break;
  15347. case BL_HOM: hd = map_id2hd(bl->id); break;
  15348. case BL_PET: pd = map_id2pd(bl->id); break;
  15349. }
  15350. switch (bl->type) {
  15351. case BL_MOB:
  15352. if (!md) {
  15353. ShowWarning("buildin_setunitname: Error in finding object BL_MOB!\n");
  15354. return SCRIPT_CMD_FAILURE;
  15355. }
  15356. safestrncpy(md->name, script_getstr(st, 3), NAME_LENGTH);
  15357. break;
  15358. case BL_HOM:
  15359. if (!hd) {
  15360. ShowWarning("buildin_setunitname: Error in finding object BL_HOM!\n");
  15361. return SCRIPT_CMD_FAILURE;
  15362. }
  15363. safestrncpy(hd->homunculus.name, script_getstr(st, 3), NAME_LENGTH);
  15364. break;
  15365. case BL_PET:
  15366. if (!pd) {
  15367. ShowWarning("buildin_setunitname: Error in finding object BL_PET!\n");
  15368. return SCRIPT_CMD_FAILURE;
  15369. }
  15370. safestrncpy(pd->pet.name, script_getstr(st, 3), NAME_LENGTH);
  15371. break;
  15372. default:
  15373. ShowWarning("buildin_setunitname: Unknown object type!\n");
  15374. return SCRIPT_CMD_FAILURE;
  15375. }
  15376. clif_charnameack(0, bl); // Send update to client.
  15377. return SCRIPT_CMD_SUCCESS;
  15378. }
  15379. /// Makes the unit walk to target position or map.
  15380. /// Returns if it was successful.
  15381. ///
  15382. /// unitwalk(<unit_id>,<x>,<y>{,<event_label>}) -> <bool>
  15383. /// unitwalkto(<unit_id>,<target_id>{,<event_label>}) -> <bool>
  15384. BUILDIN_FUNC(unitwalk)
  15385. {
  15386. struct block_list* bl;
  15387. struct unit_data *ud = NULL;
  15388. const char *cmd = script_getfuncname(st), *done_label = "";
  15389. uint8 off = 5;
  15390. bl = map_id2bl(script_getnum(st,2));
  15391. if (!bl) {
  15392. ShowError("buildin_unitwalk: Invalid unit with ID '%d'.\n", script_getnum(st,2));
  15393. script_pushint(st, 0);
  15394. return SCRIPT_CMD_FAILURE;
  15395. }
  15396. ud = unit_bl2ud(bl);
  15397. if (!strcmp(cmd,"unitwalk")) {
  15398. int x = script_getnum(st,3);
  15399. int y = script_getnum(st,4);
  15400. if (script_pushint(st, unit_can_reach_pos(bl,x,y,0)))
  15401. add_timer(gettick()+50, unit_delay_walktoxy_timer, bl->id, (x<<16)|(y&0xFFFF)); // Need timer to avoid mismatches
  15402. } else {
  15403. struct block_list* tbl = map_id2bl(script_getnum(st,3));
  15404. if (!tbl) {
  15405. ShowError("buildin_unitwalk: Bad target destination.\n");
  15406. script_pushint(st, 0);
  15407. return SCRIPT_CMD_FAILURE;
  15408. } else if (script_pushint(st, unit_can_reach_bl(bl, tbl, distance_bl(bl, tbl)+1, 0, NULL, NULL)))
  15409. add_timer(gettick()+50, unit_delay_walktobl_timer, bl->id, tbl->id); // Need timer to avoid mismatches
  15410. off = 4;
  15411. }
  15412. if (ud && script_hasdata(st, off)) {
  15413. done_label = script_getstr(st, off);
  15414. check_event(st, done_label);
  15415. safestrncpy(ud->walk_done_event, done_label, sizeof(ud->walk_done_event));
  15416. }
  15417. return SCRIPT_CMD_SUCCESS;
  15418. }
  15419. /// Kills the unit.
  15420. ///
  15421. /// unitkill <unit_id>;
  15422. BUILDIN_FUNC(unitkill)
  15423. {
  15424. struct block_list* bl = map_id2bl(script_getnum(st,2));
  15425. if (bl != NULL)
  15426. status_kill(bl);
  15427. return SCRIPT_CMD_SUCCESS;
  15428. }
  15429. /// Warps the unit to the target position in the target map.
  15430. /// Returns if it was successful.
  15431. ///
  15432. /// unitwarp(<unit_id>,"<map name>",<x>,<y>) -> <bool>
  15433. BUILDIN_FUNC(unitwarp)
  15434. {
  15435. int unit_id;
  15436. int map_idx;
  15437. short x;
  15438. short y;
  15439. struct block_list* bl;
  15440. const char *mapname;
  15441. unit_id = script_getnum(st,2);
  15442. mapname = script_getstr(st, 3);
  15443. x = (short)script_getnum(st,4);
  15444. y = (short)script_getnum(st,5);
  15445. if (!unit_id) //Warp the script's runner
  15446. bl = map_id2bl(st->rid);
  15447. else
  15448. bl = map_id2bl(unit_id);
  15449. if (!strcmp(mapname,"this"))
  15450. map_idx = bl?bl->m:-1;
  15451. else
  15452. map_idx = map_mapname2mapid(mapname);
  15453. if (map_idx >= 0 && bl != NULL)
  15454. script_pushint(st, unit_warp(bl,map_idx,x,y,CLR_OUTSIGHT));
  15455. else
  15456. script_pushint(st, 0);
  15457. return SCRIPT_CMD_SUCCESS;
  15458. }
  15459. /// Makes the unit attack the target.
  15460. /// If the unit is a player and <action type> is not 0, it does a continuous
  15461. /// attack instead of a single attack.
  15462. /// Returns if the request was successful.
  15463. ///
  15464. /// unitattack(<unit_id>,"<target name>"{,<action type>}) -> <bool>
  15465. /// unitattack(<unit_id>,<target_id>{,<action type>}) -> <bool>
  15466. BUILDIN_FUNC(unitattack)
  15467. {
  15468. struct block_list* unit_bl;
  15469. struct block_list* target_bl = NULL;
  15470. struct script_data* data;
  15471. int actiontype = 0;
  15472. unit_bl = map_id2bl(script_getnum(st,2));
  15473. if (!unit_bl) {
  15474. script_pushint(st, 0);
  15475. return SCRIPT_CMD_FAILURE;
  15476. }
  15477. data = script_getdata(st, 3);
  15478. get_val(st, data);
  15479. if (data_isstring(data)) {
  15480. TBL_PC* sd = map_nick2sd(conv_str(st, data));
  15481. if( sd != NULL )
  15482. target_bl = &sd->bl;
  15483. } else
  15484. target_bl = map_id2bl(conv_num(st, data));
  15485. if (!target_bl) {
  15486. script_pushint(st, 0);
  15487. return SCRIPT_CMD_FAILURE;
  15488. }
  15489. if (script_hasdata(st,4))
  15490. actiontype = script_getnum(st,4);
  15491. switch(unit_bl->type) {
  15492. case BL_PC:
  15493. clif_parse_ActionRequest_sub(((TBL_PC *)unit_bl), actiontype > 0 ? 0x07 : 0x00, target_bl->id, gettick());
  15494. script_pushint(st, 1);
  15495. return SCRIPT_CMD_SUCCESS;
  15496. case BL_MOB:
  15497. ((TBL_MOB *)unit_bl)->target_id = target_bl->id;
  15498. break;
  15499. case BL_PET:
  15500. ((TBL_PET *)unit_bl)->target_id = target_bl->id;
  15501. break;
  15502. default:
  15503. ShowError("buildin_unitattack: Unsupported source unit type %d.\n", unit_bl->type);
  15504. script_pushint(st, 0);
  15505. return SCRIPT_CMD_FAILURE;
  15506. }
  15507. script_pushint(st, unit_walktobl(unit_bl, target_bl, 65025, 2));
  15508. return SCRIPT_CMD_SUCCESS;
  15509. }
  15510. /// Makes the unit stop attacking.
  15511. ///
  15512. /// unitstopattack <unit_id>;
  15513. BUILDIN_FUNC(unitstopattack)
  15514. {
  15515. int unit_id;
  15516. struct block_list* bl;
  15517. unit_id = script_getnum(st,2);
  15518. bl = map_id2bl(unit_id);
  15519. if (bl != NULL) {
  15520. unit_stop_attack(bl);
  15521. if (bl->type == BL_MOB)
  15522. ((TBL_MOB*)bl)->target_id = 0;
  15523. }
  15524. return SCRIPT_CMD_SUCCESS;
  15525. }
  15526. /// Makes the unit stop walking.
  15527. ///
  15528. /// unitstopwalk <unit_id>;
  15529. BUILDIN_FUNC(unitstopwalk)
  15530. {
  15531. int unit_id;
  15532. struct block_list* bl;
  15533. unit_id = script_getnum(st,2);
  15534. bl = map_id2bl(unit_id);
  15535. if (bl != NULL)
  15536. unit_stop_walking(bl, 0);
  15537. return SCRIPT_CMD_SUCCESS;
  15538. }
  15539. /// Makes the unit say the given message.
  15540. ///
  15541. /// unittalk <unit_id>,"<message>";
  15542. BUILDIN_FUNC(unittalk)
  15543. {
  15544. int unit_id;
  15545. const char* message;
  15546. struct block_list* bl;
  15547. unit_id = script_getnum(st,2);
  15548. message = script_getstr(st, 3);
  15549. bl = map_id2bl(unit_id);
  15550. if (bl != NULL) {
  15551. struct StringBuf sbuf;
  15552. StringBuf_Init(&sbuf);
  15553. StringBuf_Printf(&sbuf, "%s", message);
  15554. clif_disp_overhead(bl, StringBuf_Value(&sbuf));
  15555. StringBuf_Destroy(&sbuf);
  15556. }
  15557. return SCRIPT_CMD_SUCCESS;
  15558. }
  15559. /// Makes the unit do an emotion.
  15560. ///
  15561. /// unitemote <unit_id>,<emotion>;
  15562. ///
  15563. /// @see e_* in db/const.txt
  15564. BUILDIN_FUNC(unitemote)
  15565. {
  15566. int unit_id;
  15567. int emotion;
  15568. struct block_list* bl;
  15569. unit_id = script_getnum(st,2);
  15570. emotion = script_getnum(st,3);
  15571. bl = map_id2bl(unit_id);
  15572. if (bl != NULL)
  15573. clif_emotion(bl, emotion);
  15574. return SCRIPT_CMD_SUCCESS;
  15575. }
  15576. /// Makes the unit cast the skill on the target or self if no target is specified.
  15577. ///
  15578. /// unitskilluseid <unit_id>,<skill_id>,<skill_lv>{,<target_id>,<casttime>};
  15579. /// unitskilluseid <unit_id>,"<skill name>",<skill_lv>{,<target_id>,<casttime>};
  15580. BUILDIN_FUNC(unitskilluseid)
  15581. {
  15582. int unit_id, target_id, casttime;
  15583. uint16 skill_id, skill_lv;
  15584. struct block_list* bl;
  15585. struct script_data *data;
  15586. unit_id = script_getnum(st,2);
  15587. data = script_getdata(st, 3);
  15588. get_val(st, data); // Convert into value in case of a variable
  15589. skill_id = ( data_isstring(data) ? skill_name2id(script_getstr(st,3)) : script_getnum(st,3) );
  15590. skill_lv = script_getnum(st,4);
  15591. target_id = ( script_hasdata(st,5) ? script_getnum(st,5) : unit_id );
  15592. casttime = ( script_hasdata(st,6) ? script_getnum(st,6) : 0 );
  15593. bl = map_id2bl(unit_id);
  15594. if (bl != NULL) {
  15595. if (bl->type == BL_NPC) {
  15596. if (!((TBL_NPC*)bl)->status.hp)
  15597. status_calc_npc(((TBL_NPC*)bl), SCO_FIRST);
  15598. else
  15599. status_calc_npc(((TBL_NPC*)bl), SCO_NONE);
  15600. }
  15601. unit_skilluse_id2(bl, target_id, skill_id, skill_lv, (casttime * 1000) + skill_castfix(bl, skill_id, skill_lv), skill_get_castcancel(skill_id));
  15602. }
  15603. return SCRIPT_CMD_SUCCESS;
  15604. }
  15605. /// Makes the unit cast the skill on the target position.
  15606. ///
  15607. /// unitskillusepos <unit_id>,<skill_id>,<skill_lv>,<target_x>,<target_y>{,<casttime>};
  15608. /// unitskillusepos <unit_id>,"<skill name>",<skill_lv>,<target_x>,<target_y>{,<casttime>};
  15609. BUILDIN_FUNC(unitskillusepos)
  15610. {
  15611. int unit_id, skill_x, skill_y, casttime;
  15612. uint16 skill_id, skill_lv;
  15613. struct block_list* bl;
  15614. struct script_data *data;
  15615. unit_id = script_getnum(st,2);
  15616. data = script_getdata(st, 3);
  15617. get_val(st, data); // Convert into value in case of a variable
  15618. skill_id = ( data_isstring(data) ? skill_name2id(script_getstr(st,3)) : script_getnum(st,3) );
  15619. skill_lv = script_getnum(st,4);
  15620. skill_x = script_getnum(st,5);
  15621. skill_y = script_getnum(st,6);
  15622. casttime = ( script_hasdata(st,7) ? script_getnum(st,7) : 0 );
  15623. bl = map_id2bl(unit_id);
  15624. if (bl != NULL) {
  15625. if (bl->type == BL_NPC) {
  15626. if (!((TBL_NPC*)bl)->status.hp)
  15627. status_calc_npc(((TBL_NPC*)bl), SCO_FIRST);
  15628. else
  15629. status_calc_npc(((TBL_NPC*)bl), SCO_NONE);
  15630. }
  15631. 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));
  15632. }
  15633. return SCRIPT_CMD_SUCCESS;
  15634. }
  15635. // <--- [zBuffer] List of unit control commands
  15636. /// Pauses the execution of the script, detaching the player
  15637. ///
  15638. /// sleep <mili seconds>;
  15639. BUILDIN_FUNC(sleep)
  15640. {
  15641. int ticks;
  15642. ticks = script_getnum(st,2);
  15643. // detach the player
  15644. script_detach_rid(st);
  15645. if( ticks <= 0 )
  15646. {// do nothing
  15647. }
  15648. else if( st->sleep.tick == 0 )
  15649. {// sleep for the target amount of time
  15650. st->state = RERUNLINE;
  15651. st->sleep.tick = ticks;
  15652. }
  15653. else
  15654. {// sleep time is over
  15655. st->state = RUN;
  15656. st->sleep.tick = 0;
  15657. }
  15658. return SCRIPT_CMD_SUCCESS;
  15659. }
  15660. /// Pauses the execution of the script, keeping the player attached
  15661. /// Returns if a player is still attached
  15662. ///
  15663. /// sleep2(<mili secconds>) -> <bool>
  15664. BUILDIN_FUNC(sleep2)
  15665. {
  15666. int ticks;
  15667. ticks = script_getnum(st,2);
  15668. if( ticks <= 0 )
  15669. {// do nothing
  15670. script_pushint(st, (map_id2sd(st->rid)!=NULL));
  15671. }
  15672. else if( !st->sleep.tick )
  15673. {// sleep for the target amount of time
  15674. st->state = RERUNLINE;
  15675. st->sleep.tick = ticks;
  15676. }
  15677. else
  15678. {// sleep time is over
  15679. st->state = RUN;
  15680. st->sleep.tick = 0;
  15681. script_pushint(st, (map_id2sd(st->rid)!=NULL));
  15682. }
  15683. return SCRIPT_CMD_SUCCESS;
  15684. }
  15685. /// Awakes all the sleep timers of the target npc
  15686. ///
  15687. /// awake "<npc name>";
  15688. BUILDIN_FUNC(awake)
  15689. {
  15690. DBIterator *iter;
  15691. struct script_state *tst;
  15692. struct npc_data* nd;
  15693. if ((nd = npc_name2id(script_getstr(st, 2))) == NULL) {
  15694. ShowError("buildin_awake: NPC \"%s\" not found\n", script_getstr(st, 2));
  15695. return SCRIPT_CMD_FAILURE;
  15696. }
  15697. iter = db_iterator(st_db);
  15698. for (tst = dbi_first(iter); dbi_exists(iter); tst = dbi_next(iter)) {
  15699. if (tst->oid == nd->bl.id) {
  15700. TBL_PC* sd = map_id2sd(tst->rid);
  15701. if (tst->sleep.timer == INVALID_TIMER) { // already awake ???
  15702. continue;
  15703. }
  15704. if ((sd && sd->status.char_id != tst->sleep.charid) || (tst->rid && !sd)) {
  15705. // char not online anymore / another char of the same account is online - Cancel execution
  15706. tst->state = END;
  15707. tst->rid = 0;
  15708. }
  15709. delete_timer(tst->sleep.timer, run_script_timer);
  15710. tst->sleep.timer = INVALID_TIMER;
  15711. if (tst->state != RERUNLINE)
  15712. tst->sleep.tick = 0;
  15713. run_script_main(tst);
  15714. }
  15715. }
  15716. dbi_destroy(iter);
  15717. return SCRIPT_CMD_SUCCESS;
  15718. }
  15719. /// Returns a reference to a variable of the target NPC.
  15720. /// Returns 0 if an error occurs.
  15721. ///
  15722. /// getvariableofnpc(<variable>, "<npc name>") -> <reference>
  15723. BUILDIN_FUNC(getvariableofnpc)
  15724. {
  15725. struct script_data* data;
  15726. const char* name;
  15727. struct npc_data* nd;
  15728. data = script_getdata(st,2);
  15729. if( !data_isreference(data) )
  15730. {// Not a reference (aka varaible name)
  15731. ShowError("buildin_getvariableofnpc: not a variable\n");
  15732. script_reportdata(data);
  15733. script_pushnil(st);
  15734. st->state = END;
  15735. return SCRIPT_CMD_FAILURE;
  15736. }
  15737. name = reference_getname(data);
  15738. if( *name != '.' || name[1] == '@' )
  15739. {// not a npc variable
  15740. ShowError("buildin_getvariableofnpc: invalid scope (not npc variable)\n");
  15741. script_reportdata(data);
  15742. script_pushnil(st);
  15743. st->state = END;
  15744. return SCRIPT_CMD_FAILURE;
  15745. }
  15746. nd = npc_name2id(script_getstr(st,3));
  15747. if( nd == NULL || nd->subtype != NPCTYPE_SCRIPT || nd->u.scr.script == NULL )
  15748. {// NPC not found or has no script
  15749. ShowError("buildin_getvariableofnpc: can't find npc %s\n", script_getstr(st,3));
  15750. script_pushnil(st);
  15751. st->state = END;
  15752. return SCRIPT_CMD_FAILURE;
  15753. }
  15754. if (!nd->u.scr.script->local.vars)
  15755. nd->u.scr.script->local.vars = i64db_alloc(DB_OPT_RELEASE_DATA);
  15756. push_val2(st->stack, C_NAME, reference_getuid(data), &nd->u.scr.script->local);
  15757. return SCRIPT_CMD_SUCCESS;
  15758. }
  15759. /// Opens a warp portal.
  15760. /// Has no "portal opening" effect/sound, it opens the portal immediately.
  15761. ///
  15762. /// warpportal <source x>,<source y>,"<target map>",<target x>,<target y>;
  15763. ///
  15764. /// @author blackhole89
  15765. BUILDIN_FUNC(warpportal)
  15766. {
  15767. int spx;
  15768. int spy;
  15769. unsigned short mapindex;
  15770. int tpx;
  15771. int tpy;
  15772. struct skill_unit_group* group;
  15773. struct block_list* bl;
  15774. bl = map_id2bl(st->oid);
  15775. if( bl == NULL ) {
  15776. ShowError("buildin_warpportal: NPC is needed\n");
  15777. return SCRIPT_CMD_FAILURE;
  15778. }
  15779. spx = script_getnum(st,2);
  15780. spy = script_getnum(st,3);
  15781. mapindex = mapindex_name2id(script_getstr(st, 4));
  15782. tpx = script_getnum(st,5);
  15783. tpy = script_getnum(st,6);
  15784. if( mapindex == 0 ) {
  15785. ShowError("buildin_warpportal: Target map not found %s.\n", script_getstr(st, 4));
  15786. return SCRIPT_CMD_FAILURE;
  15787. }
  15788. group = skill_unitsetting(bl, AL_WARP, 4, spx, spy, 0);
  15789. if( group == NULL )
  15790. return SCRIPT_CMD_FAILURE;// failed
  15791. group->val1 = (group->val1<<16)|(short)0;
  15792. group->val2 = (tpx<<16) | tpy;
  15793. group->val3 = mapindex;
  15794. return SCRIPT_CMD_SUCCESS;
  15795. }
  15796. /**
  15797. * openmail({<char_id>});
  15798. **/
  15799. BUILDIN_FUNC(openmail)
  15800. {
  15801. TBL_PC* sd;
  15802. if (!script_charid2sd(2,sd))
  15803. return SCRIPT_CMD_FAILURE;
  15804. mail_openmail(sd);
  15805. return SCRIPT_CMD_SUCCESS;
  15806. }
  15807. /**
  15808. * openauction({<char_id>});
  15809. **/
  15810. BUILDIN_FUNC(openauction)
  15811. {
  15812. TBL_PC* sd;
  15813. if (!script_charid2sd(2,sd))
  15814. return SCRIPT_CMD_FAILURE;
  15815. if( !battle_config.feature_auction ) {
  15816. clif_colormes(sd->fd, color_table[COLOR_RED], msg_txt(sd, 517));
  15817. return SCRIPT_CMD_SUCCESS;
  15818. }
  15819. clif_Auction_openwindow(sd);
  15820. return SCRIPT_CMD_SUCCESS;
  15821. }
  15822. /// Retrieves the value of the specified flag of the specified cell.
  15823. ///
  15824. /// checkcell("<map name>",<x>,<y>,<type>) -> <bool>
  15825. ///
  15826. /// @see cell_chk* constants in const.txt for the types
  15827. BUILDIN_FUNC(checkcell)
  15828. {
  15829. int16 m = map_mapname2mapid(script_getstr(st,2));
  15830. int16 x = script_getnum(st,3);
  15831. int16 y = script_getnum(st,4);
  15832. cell_chk type = (cell_chk)script_getnum(st,5);
  15833. script_pushint(st, map_getcell(m, x, y, type));
  15834. return SCRIPT_CMD_SUCCESS;
  15835. }
  15836. /// Modifies flags of cells in the specified area.
  15837. ///
  15838. /// setcell "<map name>",<x1>,<y1>,<x2>,<y2>,<type>,<flag>;
  15839. ///
  15840. /// @see cell_* constants in const.txt for the types
  15841. BUILDIN_FUNC(setcell)
  15842. {
  15843. int16 m = map_mapname2mapid(script_getstr(st,2));
  15844. int16 x1 = script_getnum(st,3);
  15845. int16 y1 = script_getnum(st,4);
  15846. int16 x2 = script_getnum(st,5);
  15847. int16 y2 = script_getnum(st,6);
  15848. cell_t type = (cell_t)script_getnum(st,7);
  15849. bool flag = (bool)script_getnum(st,8);
  15850. int x,y;
  15851. if( x1 > x2 ) swap(x1,x2);
  15852. if( y1 > y2 ) swap(y1,y2);
  15853. for( y = y1; y <= y2; ++y )
  15854. for( x = x1; x <= x2; ++x )
  15855. map_setcell(m, x, y, type, flag);
  15856. return SCRIPT_CMD_SUCCESS;
  15857. }
  15858. /*==========================================
  15859. * Mercenary Commands
  15860. *------------------------------------------*/
  15861. BUILDIN_FUNC(mercenary_create)
  15862. {
  15863. struct map_session_data *sd;
  15864. int class_, contract_time;
  15865. if( (sd = script_rid2sd(st)) == NULL || sd->md || sd->status.mer_id != 0 )
  15866. return SCRIPT_CMD_SUCCESS;
  15867. class_ = script_getnum(st,2);
  15868. if( !mercenary_class(class_) )
  15869. return SCRIPT_CMD_SUCCESS;
  15870. contract_time = script_getnum(st,3);
  15871. mercenary_create(sd, class_, contract_time);
  15872. return SCRIPT_CMD_SUCCESS;
  15873. }
  15874. BUILDIN_FUNC(mercenary_heal)
  15875. {
  15876. struct map_session_data *sd = script_rid2sd(st);
  15877. int hp, sp;
  15878. if( sd == NULL || sd->md == NULL )
  15879. return SCRIPT_CMD_SUCCESS;
  15880. hp = script_getnum(st,2);
  15881. sp = script_getnum(st,3);
  15882. status_heal(&sd->md->bl, hp, sp, 0);
  15883. return SCRIPT_CMD_SUCCESS;
  15884. }
  15885. BUILDIN_FUNC(mercenary_sc_start)
  15886. {
  15887. struct map_session_data *sd = script_rid2sd(st);
  15888. enum sc_type type;
  15889. int tick, val1;
  15890. if( sd == NULL || sd->md == NULL )
  15891. return SCRIPT_CMD_SUCCESS;
  15892. type = (sc_type)script_getnum(st,2);
  15893. tick = script_getnum(st,3);
  15894. val1 = script_getnum(st,4);
  15895. status_change_start(NULL, &sd->md->bl, type, 10000, val1, 0, 0, 0, tick, SCSTART_NOTICKDEF);
  15896. return SCRIPT_CMD_SUCCESS;
  15897. }
  15898. BUILDIN_FUNC(mercenary_get_calls)
  15899. {
  15900. struct map_session_data *sd = script_rid2sd(st);
  15901. int guild;
  15902. if( sd == NULL )
  15903. return SCRIPT_CMD_SUCCESS;
  15904. guild = script_getnum(st,2);
  15905. switch( guild )
  15906. {
  15907. case ARCH_MERC_GUILD:
  15908. script_pushint(st,sd->status.arch_calls);
  15909. break;
  15910. case SPEAR_MERC_GUILD:
  15911. script_pushint(st,sd->status.spear_calls);
  15912. break;
  15913. case SWORD_MERC_GUILD:
  15914. script_pushint(st,sd->status.sword_calls);
  15915. break;
  15916. default:
  15917. script_pushint(st,0);
  15918. break;
  15919. }
  15920. return SCRIPT_CMD_SUCCESS;
  15921. }
  15922. BUILDIN_FUNC(mercenary_set_calls)
  15923. {
  15924. struct map_session_data *sd = script_rid2sd(st);
  15925. int guild, value, *calls;
  15926. if( sd == NULL )
  15927. return SCRIPT_CMD_SUCCESS;
  15928. guild = script_getnum(st,2);
  15929. value = script_getnum(st,3);
  15930. switch( guild )
  15931. {
  15932. case ARCH_MERC_GUILD:
  15933. calls = &sd->status.arch_calls;
  15934. break;
  15935. case SPEAR_MERC_GUILD:
  15936. calls = &sd->status.spear_calls;
  15937. break;
  15938. case SWORD_MERC_GUILD:
  15939. calls = &sd->status.sword_calls;
  15940. break;
  15941. default:
  15942. ShowError("buildin_mercenary_set_calls: Invalid guild '%d'.\n", guild);
  15943. return SCRIPT_CMD_SUCCESS; // Invalid Guild
  15944. }
  15945. *calls += value;
  15946. *calls = cap_value(*calls, 0, INT_MAX);
  15947. return SCRIPT_CMD_SUCCESS;
  15948. }
  15949. BUILDIN_FUNC(mercenary_get_faith)
  15950. {
  15951. struct map_session_data *sd = script_rid2sd(st);
  15952. int guild;
  15953. if( sd == NULL )
  15954. return SCRIPT_CMD_SUCCESS;
  15955. guild = script_getnum(st,2);
  15956. switch( guild )
  15957. {
  15958. case ARCH_MERC_GUILD:
  15959. script_pushint(st,sd->status.arch_faith);
  15960. break;
  15961. case SPEAR_MERC_GUILD:
  15962. script_pushint(st,sd->status.spear_faith);
  15963. break;
  15964. case SWORD_MERC_GUILD:
  15965. script_pushint(st,sd->status.sword_faith);
  15966. break;
  15967. default:
  15968. script_pushint(st,0);
  15969. break;
  15970. }
  15971. return SCRIPT_CMD_SUCCESS;
  15972. }
  15973. BUILDIN_FUNC(mercenary_set_faith)
  15974. {
  15975. struct map_session_data *sd = script_rid2sd(st);
  15976. int guild, value, *calls;
  15977. if( sd == NULL )
  15978. return SCRIPT_CMD_SUCCESS;
  15979. guild = script_getnum(st,2);
  15980. value = script_getnum(st,3);
  15981. switch( guild )
  15982. {
  15983. case ARCH_MERC_GUILD:
  15984. calls = &sd->status.arch_faith;
  15985. break;
  15986. case SPEAR_MERC_GUILD:
  15987. calls = &sd->status.spear_faith;
  15988. break;
  15989. case SWORD_MERC_GUILD:
  15990. calls = &sd->status.sword_faith;
  15991. break;
  15992. default:
  15993. ShowError("buildin_mercenary_set_faith: Invalid guild '%d'.\n", guild);
  15994. return SCRIPT_CMD_SUCCESS; // Invalid Guild
  15995. }
  15996. *calls += value;
  15997. *calls = cap_value(*calls, 0, INT_MAX);
  15998. if( mercenary_get_guild(sd->md) == guild )
  15999. clif_mercenary_updatestatus(sd,SP_MERCFAITH);
  16000. return SCRIPT_CMD_SUCCESS;
  16001. }
  16002. /*------------------------------------------
  16003. * Book Reading
  16004. *------------------------------------------*/
  16005. BUILDIN_FUNC(readbook)
  16006. {
  16007. struct map_session_data *sd;
  16008. int book_id, page;
  16009. if( (sd = script_rid2sd(st)) == NULL )
  16010. return SCRIPT_CMD_SUCCESS;
  16011. book_id = script_getnum(st,2);
  16012. page = script_getnum(st,3);
  16013. clif_readbook(sd->fd, book_id, page);
  16014. return SCRIPT_CMD_SUCCESS;
  16015. }
  16016. /******************
  16017. Questlog script commands
  16018. *******************/
  16019. /**
  16020. * Add job criteria to questinfo
  16021. * @param qi Quest Info
  16022. * @param job
  16023. * @author [Cydh]
  16024. **/
  16025. static void buildin_questinfo_setjob(struct questinfo *qi, int job) {
  16026. RECREATE(qi->jobid, unsigned short, qi->jobid_count+1);
  16027. qi->jobid[qi->jobid_count++] = job;
  16028. }
  16029. /**
  16030. * questinfo <Quest ID>,<Icon>{,<Map Mark Color>{,<Job Class>}};
  16031. **/
  16032. BUILDIN_FUNC(questinfo)
  16033. {
  16034. TBL_NPC* nd = map_id2nd(st->oid);
  16035. int quest_id, icon;
  16036. struct questinfo qi, *q2;
  16037. if( nd == NULL || nd->bl.m == -1 ) {
  16038. ShowError("buildin_questinfo: No NPC attached.\n");
  16039. return SCRIPT_CMD_FAILURE;
  16040. }
  16041. quest_id = script_getnum(st, 2);
  16042. icon = script_getnum(st, 3);
  16043. #if PACKETVER >= 20120410
  16044. switch(icon){
  16045. case QTYPE_QUEST:
  16046. case QTYPE_QUEST2:
  16047. case QTYPE_JOB:
  16048. case QTYPE_JOB2:
  16049. case QTYPE_EVENT:
  16050. case QTYPE_EVENT2:
  16051. case QTYPE_WARG:
  16052. case QTYPE_WARG2:
  16053. // Leave everything as it is
  16054. break;
  16055. case QTYPE_NONE:
  16056. default:
  16057. // Default to nothing if icon id is invalid.
  16058. icon = QTYPE_NONE;
  16059. break;
  16060. }
  16061. #else
  16062. if(icon < QTYPE_QUEST || icon > 7) // TODO: check why 7 and not QTYPE_WARG, might be related to icon + 1 below
  16063. icon = QTYPE_QUEST;
  16064. else
  16065. icon = icon + 1;
  16066. #endif
  16067. qi.quest_id = quest_id;
  16068. qi.icon = (unsigned char)icon;
  16069. qi.nd = nd;
  16070. if( script_hasdata(st, 4) ) {
  16071. int color = script_getnum(st, 4);
  16072. if( color < 0 || color > 3 ) {
  16073. ShowWarning("buildin_questinfo: invalid color '%d', changing to 0\n",color);
  16074. script_reportfunc(st);
  16075. color = 0;
  16076. }
  16077. qi.color = (unsigned char)color;
  16078. }
  16079. qi.min_level = 1;
  16080. qi.max_level = MAX_LEVEL;
  16081. q2 = map_add_questinfo(nd->bl.m, &qi);
  16082. q2->req = NULL;
  16083. q2->req_count = 0;
  16084. q2->jobid = NULL;
  16085. q2->jobid_count = 0;
  16086. if(script_hasdata(st, 5)) {
  16087. int job = script_getnum(st, 5);
  16088. if (!pcdb_checkid(job))
  16089. ShowError("buildin_questinfo: Nonexistant Job Class.\n");
  16090. else {
  16091. buildin_questinfo_setjob(q2, job);
  16092. }
  16093. }
  16094. return SCRIPT_CMD_SUCCESS;
  16095. }
  16096. /**
  16097. * setquest <ID>{,<char_id>};
  16098. **/
  16099. BUILDIN_FUNC(setquest)
  16100. {
  16101. struct map_session_data *sd;
  16102. int quest_id;
  16103. quest_id = script_getnum(st, 2);
  16104. if (!script_charid2sd(3,sd))
  16105. return SCRIPT_CMD_FAILURE;
  16106. if( quest_add(sd, quest_id) == -1 ){
  16107. script_reportsrc(st);
  16108. script_reportfunc(st);
  16109. }
  16110. //20120410 or 20090218 ? no reason that shouldn't work for 2009
  16111. pc_show_questinfo(sd);
  16112. return SCRIPT_CMD_SUCCESS;
  16113. }
  16114. /**
  16115. * erasequest <ID>{,<char_id>};
  16116. **/
  16117. BUILDIN_FUNC(erasequest)
  16118. {
  16119. struct map_session_data *sd;
  16120. if (!script_charid2sd(3,sd))
  16121. return SCRIPT_CMD_FAILURE;
  16122. if( quest_delete(sd, script_getnum(st, 2)) == -1 ){
  16123. script_reportsrc(st);
  16124. script_reportfunc(st);
  16125. }
  16126. return SCRIPT_CMD_SUCCESS;
  16127. }
  16128. /**
  16129. * completequest <ID>{,<char_id>};
  16130. **/
  16131. BUILDIN_FUNC(completequest)
  16132. {
  16133. struct map_session_data *sd;
  16134. if (!script_charid2sd(3,sd))
  16135. return SCRIPT_CMD_FAILURE;
  16136. quest_update_status(sd, script_getnum(st, 2), Q_COMPLETE);
  16137. //20120410 or 20090218
  16138. pc_show_questinfo(sd);
  16139. return SCRIPT_CMD_SUCCESS;
  16140. }
  16141. /**
  16142. * changequest <ID>,<ID2>{,<char_id>};
  16143. **/
  16144. BUILDIN_FUNC(changequest)
  16145. {
  16146. struct map_session_data *sd;
  16147. if (!script_charid2sd(4,sd))
  16148. return SCRIPT_CMD_FAILURE;
  16149. if( quest_change(sd, script_getnum(st, 2),script_getnum(st, 3)) == -1 ){
  16150. script_reportsrc(st);
  16151. script_reportfunc(st);
  16152. }
  16153. //20120410 or 20090218
  16154. pc_show_questinfo(sd);
  16155. return SCRIPT_CMD_SUCCESS;
  16156. }
  16157. /**
  16158. * checkquest(<ID>{,PLAYTIME|HUNTING{,<char_id>}})
  16159. **/
  16160. BUILDIN_FUNC(checkquest)
  16161. {
  16162. struct map_session_data *sd;
  16163. enum quest_check_type type = HAVEQUEST;
  16164. if( script_hasdata(st, 3) )
  16165. type = (enum quest_check_type)script_getnum(st, 3);
  16166. if (!script_charid2sd(4,sd))
  16167. return SCRIPT_CMD_FAILURE;
  16168. script_pushint(st, quest_check(sd, script_getnum(st, 2), type));
  16169. return SCRIPT_CMD_SUCCESS;
  16170. }
  16171. /**
  16172. * isbegin_quest(<ID>{,<char_id>})
  16173. **/
  16174. BUILDIN_FUNC(isbegin_quest)
  16175. {
  16176. struct map_session_data *sd;
  16177. int i;
  16178. if (!script_charid2sd(3,sd))
  16179. return SCRIPT_CMD_FAILURE;
  16180. i = quest_check(sd, script_getnum(st, 2), (enum quest_check_type) HAVEQUEST);
  16181. script_pushint(st, i + (i < 1));
  16182. return SCRIPT_CMD_SUCCESS;
  16183. }
  16184. /**
  16185. * showevent <icon>{,<mark color>{,<char_id>}}
  16186. **/
  16187. BUILDIN_FUNC(showevent)
  16188. {
  16189. TBL_PC *sd;
  16190. struct npc_data *nd = map_id2nd(st->oid);
  16191. int icon, color = 0;
  16192. if (!script_charid2sd(4,sd))
  16193. return SCRIPT_CMD_FAILURE;
  16194. if( sd == NULL || nd == NULL )
  16195. return SCRIPT_CMD_SUCCESS;
  16196. icon = script_getnum(st, 2);
  16197. if( script_hasdata(st, 3) ) {
  16198. color = script_getnum(st, 3);
  16199. if( color < 0 || color > 3 ) {
  16200. ShowWarning("buildin_showevent: invalid color '%d', changing to 0\n",color);
  16201. script_reportfunc(st);
  16202. color = 0;
  16203. }
  16204. }
  16205. #if PACKETVER >= 20120410
  16206. if(icon < 0 || (icon > 8 && icon != 9999) || icon == 7)
  16207. icon = 9999; // Default to nothing if icon id is invalid.
  16208. #else
  16209. if(icon < 0 || icon > 7)
  16210. icon = 0;
  16211. else
  16212. icon = icon + 1;
  16213. #endif
  16214. clif_quest_show_event(sd, &nd->bl, icon, color);
  16215. return SCRIPT_CMD_SUCCESS;
  16216. }
  16217. /*==========================================
  16218. * BattleGround System
  16219. *------------------------------------------*/
  16220. BUILDIN_FUNC(waitingroom2bg)
  16221. {
  16222. struct npc_data *nd;
  16223. struct chat_data *cd;
  16224. const char *map_name, *ev = "", *dev = "";
  16225. int x, y, mapindex = 0, bg_id;
  16226. unsigned char i,c=0;
  16227. if( script_hasdata(st,7) )
  16228. nd = npc_name2id(script_getstr(st,7));
  16229. else
  16230. nd = (struct npc_data *)map_id2bl(st->oid);
  16231. if( nd == NULL || (cd = (struct chat_data *)map_id2bl(nd->chat_id)) == NULL )
  16232. {
  16233. script_pushint(st,0);
  16234. return SCRIPT_CMD_SUCCESS;
  16235. }
  16236. map_name = script_getstr(st,2);
  16237. if( strcmp(map_name,"-") != 0 )
  16238. {
  16239. mapindex = mapindex_name2id(map_name);
  16240. if( mapindex == 0 )
  16241. { // Invalid Map
  16242. script_pushint(st,0);
  16243. return SCRIPT_CMD_SUCCESS;
  16244. }
  16245. }
  16246. x = script_getnum(st,3);
  16247. y = script_getnum(st,4);
  16248. ev = script_getstr(st,5); // Logout Event
  16249. dev = script_getstr(st,6); // Die Event
  16250. if( (bg_id = bg_create(mapindex, x, y, ev, dev)) == 0 )
  16251. { // Creation failed
  16252. script_pushint(st,0);
  16253. return SCRIPT_CMD_SUCCESS;
  16254. }
  16255. for (i = 0; i < cd->users; i++) { // Only add those who are in the chat room
  16256. struct map_session_data *sd;
  16257. if( (sd = cd->usersd[i]) != NULL && bg_team_join(bg_id, sd) ){
  16258. mapreg_setreg(reference_uid(add_str("$@arenamembers"), c), sd->bl.id);
  16259. ++c;
  16260. }
  16261. }
  16262. mapreg_setreg(add_str("$@arenamembersnum"), c);
  16263. script_pushint(st,bg_id);
  16264. return SCRIPT_CMD_SUCCESS;
  16265. }
  16266. BUILDIN_FUNC(waitingroom2bg_single)
  16267. {
  16268. const char* map_name;
  16269. struct npc_data *nd;
  16270. struct chat_data *cd;
  16271. struct map_session_data *sd;
  16272. int x, y, mapindex, bg_id;
  16273. bg_id = script_getnum(st,2);
  16274. map_name = script_getstr(st,3);
  16275. if( (mapindex = mapindex_name2id(map_name)) == 0 )
  16276. return SCRIPT_CMD_SUCCESS; // Invalid Map
  16277. x = script_getnum(st,4);
  16278. y = script_getnum(st,5);
  16279. nd = npc_name2id(script_getstr(st,6));
  16280. if( nd == NULL || (cd = (struct chat_data *)map_id2bl(nd->chat_id)) == NULL || cd->users <= 0 )
  16281. return SCRIPT_CMD_SUCCESS;
  16282. if( (sd = cd->usersd[0]) == NULL )
  16283. return SCRIPT_CMD_SUCCESS;
  16284. if( bg_team_join(bg_id, sd) )
  16285. {
  16286. pc_setpos(sd, mapindex, x, y, CLR_TELEPORT);
  16287. script_pushint(st,1);
  16288. }
  16289. else
  16290. script_pushint(st,0);
  16291. return SCRIPT_CMD_SUCCESS;
  16292. }
  16293. BUILDIN_FUNC(bg_team_setxy)
  16294. {
  16295. struct battleground_data *bg;
  16296. int bg_id;
  16297. bg_id = script_getnum(st,2);
  16298. if( (bg = bg_team_search(bg_id)) == NULL )
  16299. return SCRIPT_CMD_SUCCESS;
  16300. bg->x = script_getnum(st,3);
  16301. bg->y = script_getnum(st,4);
  16302. return SCRIPT_CMD_SUCCESS;
  16303. }
  16304. BUILDIN_FUNC(bg_warp)
  16305. {
  16306. int x, y, mapindex, bg_id;
  16307. const char* map_name;
  16308. bg_id = script_getnum(st,2);
  16309. map_name = script_getstr(st,3);
  16310. if( (mapindex = mapindex_name2id(map_name)) == 0 )
  16311. return SCRIPT_CMD_SUCCESS; // Invalid Map
  16312. x = script_getnum(st,4);
  16313. y = script_getnum(st,5);
  16314. bg_team_warp(bg_id, mapindex, x, y);
  16315. return SCRIPT_CMD_SUCCESS;
  16316. }
  16317. BUILDIN_FUNC(bg_monster)
  16318. {
  16319. int class_ = 0, x = 0, y = 0, bg_id = 0;
  16320. const char *str,*mapname, *evt="";
  16321. bg_id = script_getnum(st,2);
  16322. mapname = script_getstr(st,3);
  16323. x = script_getnum(st,4);
  16324. y = script_getnum(st,5);
  16325. str = script_getstr(st,6);
  16326. class_ = script_getnum(st,7);
  16327. if( script_hasdata(st,8) ) evt = script_getstr(st,8);
  16328. check_event(st, evt);
  16329. script_pushint(st, mob_spawn_bg(mapname,x,y,str,class_,evt,bg_id));
  16330. return SCRIPT_CMD_SUCCESS;
  16331. }
  16332. BUILDIN_FUNC(bg_monster_set_team)
  16333. {
  16334. struct mob_data *md;
  16335. struct block_list *mbl;
  16336. int id = script_getnum(st,2),
  16337. bg_id = script_getnum(st,3);
  16338. if( id == 0 || (mbl = map_id2bl(id)) == NULL || mbl->type != BL_MOB )
  16339. return SCRIPT_CMD_SUCCESS;
  16340. md = (TBL_MOB *)mbl;
  16341. md->bg_id = bg_id;
  16342. mob_stop_attack(md);
  16343. mob_stop_walking(md, 0);
  16344. md->target_id = md->attacked_id = 0;
  16345. clif_charnameack(0, &md->bl);
  16346. return SCRIPT_CMD_SUCCESS;
  16347. }
  16348. BUILDIN_FUNC(bg_leave)
  16349. {
  16350. struct map_session_data *sd = NULL;
  16351. if( !script_charid2sd(2,sd) || !sd->bg_id )
  16352. return SCRIPT_CMD_SUCCESS;
  16353. bg_team_leave(sd,0);
  16354. return SCRIPT_CMD_SUCCESS;
  16355. }
  16356. BUILDIN_FUNC(bg_destroy)
  16357. {
  16358. int bg_id = script_getnum(st,2);
  16359. bg_team_delete(bg_id);
  16360. return SCRIPT_CMD_SUCCESS;
  16361. }
  16362. BUILDIN_FUNC(bg_getareausers)
  16363. {
  16364. const char *str;
  16365. int16 m, x0, y0, x1, y1;
  16366. int bg_id;
  16367. int i = 0, c = 0;
  16368. struct battleground_data *bg = NULL;
  16369. bg_id = script_getnum(st,2);
  16370. str = script_getstr(st,3);
  16371. if( (bg = bg_team_search(bg_id)) == NULL || (m = map_mapname2mapid(str)) < 0 )
  16372. {
  16373. script_pushint(st,0);
  16374. return SCRIPT_CMD_SUCCESS;
  16375. }
  16376. x0 = script_getnum(st,4);
  16377. y0 = script_getnum(st,5);
  16378. x1 = script_getnum(st,6);
  16379. y1 = script_getnum(st,7);
  16380. for( i = 0; i < MAX_BG_MEMBERS; i++ )
  16381. {
  16382. struct map_session_data *sd;
  16383. if( (sd = bg->members[i].sd) == NULL )
  16384. continue;
  16385. if( sd->bl.m != m || sd->bl.x < x0 || sd->bl.y < y0 || sd->bl.x > x1 || sd->bl.y > y1 )
  16386. continue;
  16387. c++;
  16388. }
  16389. script_pushint(st,c);
  16390. return SCRIPT_CMD_SUCCESS;
  16391. }
  16392. BUILDIN_FUNC(bg_updatescore)
  16393. {
  16394. const char *str;
  16395. int16 m;
  16396. str = script_getstr(st,2);
  16397. if( (m = map_mapname2mapid(str)) < 0 )
  16398. return SCRIPT_CMD_SUCCESS;
  16399. map[m].bgscore_lion = script_getnum(st,3);
  16400. map[m].bgscore_eagle = script_getnum(st,4);
  16401. clif_bg_updatescore(m);
  16402. return SCRIPT_CMD_SUCCESS;
  16403. }
  16404. BUILDIN_FUNC(bg_get_data)
  16405. {
  16406. struct battleground_data *bg;
  16407. int bg_id = script_getnum(st,2),
  16408. type = script_getnum(st,3);
  16409. if( (bg = bg_team_search(bg_id)) == NULL )
  16410. {
  16411. script_pushint(st,0);
  16412. return SCRIPT_CMD_SUCCESS;
  16413. }
  16414. switch( type )
  16415. {
  16416. case 0: script_pushint(st, bg->count); break;
  16417. default:
  16418. ShowError("script:bg_get_data: unknown data identifier %d\n", type);
  16419. break;
  16420. }
  16421. return SCRIPT_CMD_SUCCESS;
  16422. }
  16423. /*==========================================
  16424. * Instancing System
  16425. *------------------------------------------*/
  16426. //Returns an Instance ID
  16427. //Checks NPC first, then if player is attached we check party
  16428. int script_instancegetid(struct script_state* st)
  16429. {
  16430. short instance_id = 0;
  16431. struct npc_data *nd;
  16432. if( (nd = map_id2nd(st->oid)) && nd->instance_id > 0 )
  16433. instance_id = nd->instance_id;
  16434. else {
  16435. struct map_session_data *sd;
  16436. struct party_data *p;
  16437. if( (sd = script_rid2sd(st)) != NULL && sd->status.party_id && (p = party_search(sd->status.party_id)) != NULL && p->instance_id )
  16438. instance_id = p->instance_id;
  16439. }
  16440. return instance_id;
  16441. }
  16442. /*==========================================
  16443. * Creates the instance
  16444. * Returns Instance ID if created successfully
  16445. *------------------------------------------*/
  16446. BUILDIN_FUNC(instance_create)
  16447. {
  16448. struct map_session_data *sd;
  16449. if((sd = script_rid2sd(st)) == NULL)
  16450. return -1;
  16451. script_pushint(st,instance_create(sd->status.party_id, script_getstr(st, 2)));
  16452. return SCRIPT_CMD_SUCCESS;
  16453. }
  16454. /*==========================================
  16455. * Destroys an instance (unofficial)
  16456. * Officially instances are only destroyed by timeout
  16457. *
  16458. * instance_destroy {<instance_id>};
  16459. *------------------------------------------*/
  16460. BUILDIN_FUNC(instance_destroy)
  16461. {
  16462. short instance_id;
  16463. if( script_hasdata(st,2) )
  16464. instance_id = script_getnum(st,2);
  16465. else
  16466. instance_id = script_instancegetid(st);
  16467. if( instance_id <= 0 || instance_id >= MAX_MAP_PER_SERVER ) {
  16468. ShowError("script:instance_destroy: Trying to destroy invalid instance %d.\n", instance_id);
  16469. return SCRIPT_CMD_SUCCESS;
  16470. }
  16471. instance_destroy(instance_id);
  16472. return SCRIPT_CMD_SUCCESS;
  16473. }
  16474. /*==========================================
  16475. * Warps player to instance
  16476. * Results:
  16477. * 0: Success
  16478. * 1: Character not in party
  16479. * 2: Party doesn't have instance
  16480. * 3: Other errors (instance not in DB, instance doesn't match with party, etc.)
  16481. *------------------------------------------*/
  16482. BUILDIN_FUNC(instance_enter)
  16483. {
  16484. struct map_session_data *sd = NULL;
  16485. int x = script_hasdata(st,3) ? script_getnum(st, 3) : -1;
  16486. int y = script_hasdata(st,4) ? script_getnum(st, 4) : -1;
  16487. if (!script_charid2sd(5,sd))
  16488. return SCRIPT_CMD_FAILURE;
  16489. if (x != -1 && y != -1)
  16490. script_pushint(st,instance_enter_position(sd,script_getstr(st, 2),x,y));
  16491. else
  16492. script_pushint(st,instance_enter(sd,script_getstr(st, 2)));
  16493. return SCRIPT_CMD_SUCCESS;
  16494. }
  16495. /*==========================================
  16496. * Returns the name of a duplicated NPC
  16497. *
  16498. * instance_npcname <npc_name>{,<instance_id>};
  16499. * <npc_name> is the full name of an NPC.
  16500. *------------------------------------------*/
  16501. BUILDIN_FUNC(instance_npcname)
  16502. {
  16503. const char *str;
  16504. short instance_id = 0;
  16505. struct npc_data *nd;
  16506. str = script_getstr(st,2);
  16507. if( script_hasdata(st,3) )
  16508. instance_id = script_getnum(st,3);
  16509. else
  16510. instance_id = script_instancegetid(st);
  16511. if( instance_id && (nd = npc_name2id(str)) != NULL ) {
  16512. static char npcname[NAME_LENGTH];
  16513. snprintf(npcname, sizeof(npcname), "dup_%d_%d", instance_id, nd->bl.id);
  16514. script_pushconststr(st,npcname);
  16515. } else {
  16516. ShowError("script:instance_npcname: invalid instance NPC (instance_id: %d, NPC name: \"%s\".)\n", instance_id, str);
  16517. st->state = END;
  16518. return SCRIPT_CMD_FAILURE;
  16519. }
  16520. return SCRIPT_CMD_SUCCESS;
  16521. }
  16522. /*==========================================
  16523. * Returns the name of a duplicated map
  16524. *
  16525. * instance_mapname <map_name>{,<instance_id>};
  16526. *------------------------------------------*/
  16527. BUILDIN_FUNC(instance_mapname)
  16528. {
  16529. const char *str;
  16530. int16 m;
  16531. short instance_id = 0;
  16532. str = script_getstr(st,2);
  16533. if( script_hasdata(st,3) )
  16534. instance_id = script_getnum(st,3);
  16535. else
  16536. instance_id = script_instancegetid(st);
  16537. // Check that instance mapname is a valid map
  16538. if(!instance_id || (m = instance_mapname2mapid(str,instance_id)) < 0)
  16539. script_pushconststr(st, "");
  16540. else
  16541. script_pushconststr(st, map[m].name);
  16542. return SCRIPT_CMD_SUCCESS;
  16543. }
  16544. /*==========================================
  16545. * Returns an Instance ID
  16546. *------------------------------------------*/
  16547. BUILDIN_FUNC(instance_id)
  16548. {
  16549. short instance_id;
  16550. instance_id = script_instancegetid(st);
  16551. if(!instance_id) {
  16552. //ShowError("script:instance_id: No instance attached to NPC or player");
  16553. script_pushint(st, 0);
  16554. return SCRIPT_CMD_SUCCESS;
  16555. }
  16556. script_pushint(st, instance_id);
  16557. return SCRIPT_CMD_SUCCESS;
  16558. }
  16559. /*==========================================
  16560. * Warps all players inside an instance
  16561. *
  16562. * instance_warpall <map_name>,<x>,<y>{,<instance_id>};
  16563. *------------------------------------------*/
  16564. BUILDIN_FUNC(instance_warpall)
  16565. {
  16566. struct party_data *p;
  16567. int16 m, i;
  16568. short instance_id;
  16569. const char *mapn;
  16570. int x, y;
  16571. mapn = script_getstr(st,2);
  16572. x = script_getnum(st,3);
  16573. y = script_getnum(st,4);
  16574. if( script_hasdata(st,5) )
  16575. instance_id = script_getnum(st,5);
  16576. else
  16577. instance_id = script_instancegetid(st);
  16578. if( !instance_id || (m = map_mapname2mapid(mapn)) < 0 || (m = instance_mapname2mapid(map[m].name,instance_id)) < 0)
  16579. return SCRIPT_CMD_SUCCESS;
  16580. if( !(p = party_search(instance_data[instance_id].party_id)) )
  16581. return SCRIPT_CMD_SUCCESS;
  16582. for( i = 0; i < MAX_PARTY; i++ ) {
  16583. struct map_session_data *pl_sd;
  16584. 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);
  16585. }
  16586. return SCRIPT_CMD_SUCCESS;
  16587. }
  16588. /*==========================================
  16589. * Broadcasts to all maps inside an instance
  16590. *
  16591. * instance_announce <instance id>,"<text>",<flag>{,<fontColor>{,<fontType>{,<fontSize>{,<fontAlign>{,<fontY>}}}}};
  16592. * Using -1 for <instance id> will auto-detect the id.
  16593. *------------------------------------------*/
  16594. BUILDIN_FUNC(instance_announce) {
  16595. int instance_id = script_getnum(st,2);
  16596. const char *mes = script_getstr(st,3);
  16597. int flag = script_getnum(st,4);
  16598. const char *fontColor = script_hasdata(st,5) ? script_getstr(st,5) : NULL;
  16599. int fontType = script_hasdata(st,6) ? script_getnum(st,6) : FW_NORMAL; // default fontType
  16600. int fontSize = script_hasdata(st,7) ? script_getnum(st,7) : 12; // default fontSize
  16601. int fontAlign = script_hasdata(st,8) ? script_getnum(st,8) : 0; // default fontAlign
  16602. int fontY = script_hasdata(st,9) ? script_getnum(st,9) : 0; // default fontY
  16603. int i;
  16604. if( instance_id == -1 ) {
  16605. instance_id = script_instancegetid(st);
  16606. }
  16607. if( !instance_id && &instance_data[instance_id] != NULL) {
  16608. ShowError("buildin_instance_announce: Intance is not found.\n");
  16609. return SCRIPT_CMD_FAILURE;
  16610. }
  16611. for( i = 0; i < instance_data[instance_id].cnt_map; i++ )
  16612. map_foreachinmap(buildin_announce_sub, instance_data[instance_id].map[i].m, BL_PC,
  16613. mes, strlen(mes)+1, flag&BC_COLOR_MASK, fontColor, fontType, fontSize, fontAlign, fontY);
  16614. return SCRIPT_CMD_SUCCESS;
  16615. }
  16616. /*==========================================
  16617. * instance_check_party [malufett]
  16618. * Values:
  16619. * party_id : Party ID of the invoking character. [Required Parameter]
  16620. * amount : Amount of needed Partymembers for the Instance. [Optional Parameter]
  16621. * min : Minimum Level needed to join the Instance. [Optional Parameter]
  16622. * max : Maxium Level allowed to join the Instance. [Optional Parameter]
  16623. * Example: instance_check_party (getcharid(1){,amount}{,min}{,max});
  16624. * Example 2: instance_check_party (getcharid(1),1,1,99);
  16625. *------------------------------------------*/
  16626. BUILDIN_FUNC(instance_check_party)
  16627. {
  16628. int amount, min, max, i, party_id, c = 0;
  16629. struct party_data *p = NULL;
  16630. amount = script_hasdata(st,3) ? script_getnum(st,3) : 1; // Amount of needed Partymembers for the Instance.
  16631. min = script_hasdata(st,4) ? script_getnum(st,4) : 1; // Minimum Level needed to join the Instance.
  16632. max = script_hasdata(st,5) ? script_getnum(st,5) : MAX_LEVEL; // Maxium Level allowed to join the Instance.
  16633. if( min < 1 || min > MAX_LEVEL) {
  16634. ShowError("buildin_instance_check_party: Invalid min level, %d\n", min);
  16635. return SCRIPT_CMD_FAILURE;
  16636. } else if( max < 1 || max > MAX_LEVEL) {
  16637. ShowError("buildin_instance_check_party: Invalid max level, %d\n", max);
  16638. return SCRIPT_CMD_FAILURE;
  16639. }
  16640. if( script_hasdata(st,2) )
  16641. party_id = script_getnum(st,2);
  16642. else return SCRIPT_CMD_SUCCESS;
  16643. if( !(p = party_search(party_id)) ) {
  16644. script_pushint(st, 0); // Returns false if party does not exist.
  16645. return SCRIPT_CMD_SUCCESS;
  16646. }
  16647. for( i = 0; i < MAX_PARTY; i++ ) {
  16648. struct map_session_data *pl_sd;
  16649. if( (pl_sd = p->data[i].sd) )
  16650. if(map_id2bl(pl_sd->bl.id)) {
  16651. if(pl_sd->status.base_level < min) {
  16652. script_pushint(st, 0);
  16653. return SCRIPT_CMD_SUCCESS;
  16654. } else if(pl_sd->status.base_level > max) {
  16655. script_pushint(st, 0);
  16656. return SCRIPT_CMD_SUCCESS;
  16657. }
  16658. c++;
  16659. }
  16660. }
  16661. if(c < amount)
  16662. script_pushint(st, 0); // Not enough Members in the Party to join Instance.
  16663. else
  16664. script_pushint(st, 1);
  16665. return SCRIPT_CMD_SUCCESS;
  16666. }
  16667. /*==========================================
  16668. * Custom Fonts
  16669. *------------------------------------------*/
  16670. BUILDIN_FUNC(setfont)
  16671. {
  16672. struct map_session_data *sd = script_rid2sd(st);
  16673. int font = script_getnum(st,2);
  16674. if( sd == NULL )
  16675. return SCRIPT_CMD_SUCCESS;
  16676. if( sd->status.font != font )
  16677. sd->status.font = font;
  16678. else
  16679. sd->status.font = 0;
  16680. clif_font(sd);
  16681. return SCRIPT_CMD_SUCCESS;
  16682. }
  16683. static int buildin_mobuseskill_sub(struct block_list *bl,va_list ap)
  16684. {
  16685. TBL_MOB* md = (TBL_MOB*)bl;
  16686. struct block_list *tbl;
  16687. int mobid = va_arg(ap,int);
  16688. uint16 skill_id = va_arg(ap,int);
  16689. uint16 skill_lv = va_arg(ap,int);
  16690. int casttime = va_arg(ap,int);
  16691. int cancel = va_arg(ap,int);
  16692. int emotion = va_arg(ap,int);
  16693. int target = va_arg(ap,int);
  16694. if( md->mob_id != mobid )
  16695. return 0;
  16696. // 0:self, 1:target, 2:master, default:random
  16697. switch( target ) {
  16698. case 0: tbl = map_id2bl(md->bl.id); break;
  16699. case 1: tbl = map_id2bl(md->target_id); break;
  16700. case 2: tbl = map_id2bl(md->master_id); break;
  16701. default:tbl = battle_getenemy(&md->bl, DEFAULT_ENEMY_TYPE(md), skill_get_range2(&md->bl, skill_id, skill_lv, true)); break;
  16702. }
  16703. if( !tbl )
  16704. return 0;
  16705. if( md->ud.skilltimer != INVALID_TIMER ) // Cancel the casting skill.
  16706. unit_skillcastcancel(bl,0);
  16707. if( skill_get_casttype(skill_id) == CAST_GROUND )
  16708. unit_skilluse_pos2(&md->bl, tbl->x, tbl->y, skill_id, skill_lv, casttime, cancel);
  16709. else
  16710. unit_skilluse_id2(&md->bl, tbl->id, skill_id, skill_lv, casttime, cancel);
  16711. clif_emotion(&md->bl, emotion);
  16712. return 1;
  16713. }
  16714. /*==========================================
  16715. * areamobuseskill "Map Name",<x>,<y>,<range>,<Mob ID>,"Skill Name"/<Skill ID>,<Skill Lv>,<Cast Time>,<Cancelable>,<Emotion>,<Target Type>;
  16716. *------------------------------------------*/
  16717. BUILDIN_FUNC(areamobuseskill)
  16718. {
  16719. struct block_list center;
  16720. struct script_data *data;
  16721. int16 m;
  16722. int range,mobid,skill_id,skill_lv,casttime,emotion,target,cancel;
  16723. if( (m = map_mapname2mapid(script_getstr(st,2))) < 0 ) {
  16724. ShowError("areamobuseskill: invalid map name.\n");
  16725. return SCRIPT_CMD_SUCCESS;
  16726. }
  16727. center.m = m;
  16728. center.x = script_getnum(st,3);
  16729. center.y = script_getnum(st,4);
  16730. range = script_getnum(st,5);
  16731. mobid = script_getnum(st,6);
  16732. data = script_getdata(st, 7);
  16733. get_val(st, data); // Convert into value in case of a variable
  16734. skill_id = ( data_isstring(data) ? skill_name2id(script_getstr(st,7)) : script_getnum(st,7) );
  16735. if( (skill_lv = script_getnum(st,8)) > battle_config.mob_max_skilllvl )
  16736. skill_lv = battle_config.mob_max_skilllvl;
  16737. casttime = script_getnum(st,9);
  16738. cancel = script_getnum(st,10);
  16739. emotion = script_getnum(st,11);
  16740. target = script_getnum(st,12);
  16741. map_foreachinrange(buildin_mobuseskill_sub, &center, range, BL_MOB, mobid, skill_id, skill_lv, casttime, cancel, emotion, target);
  16742. return SCRIPT_CMD_SUCCESS;
  16743. }
  16744. BUILDIN_FUNC(progressbar)
  16745. {
  16746. struct map_session_data * sd = script_rid2sd(st);
  16747. const char * color;
  16748. unsigned int second;
  16749. if( !st || !sd )
  16750. return SCRIPT_CMD_SUCCESS;
  16751. st->state = STOP;
  16752. color = script_getstr(st,2);
  16753. second = script_getnum(st,3);
  16754. sd->progressbar.npc_id = st->oid;
  16755. sd->progressbar.timeout = gettick() + second*1000;
  16756. clif_progressbar(sd, strtol(color, (char **)NULL, 0), second);
  16757. return SCRIPT_CMD_SUCCESS;
  16758. }
  16759. BUILDIN_FUNC(pushpc)
  16760. {
  16761. uint8 dir;
  16762. int cells, dx, dy;
  16763. struct map_session_data* sd;
  16764. if((sd = script_rid2sd(st))==NULL)
  16765. {
  16766. return SCRIPT_CMD_SUCCESS;
  16767. }
  16768. dir = script_getnum(st,2);
  16769. cells = script_getnum(st,3);
  16770. if(dir>7)
  16771. {
  16772. ShowWarning("buildin_pushpc: Invalid direction %d specified.\n", dir);
  16773. script_reportsrc(st);
  16774. dir%= 8; // trim spin-over
  16775. }
  16776. if(!cells)
  16777. {// zero distance
  16778. return SCRIPT_CMD_SUCCESS;
  16779. }
  16780. else if(cells<0)
  16781. {// pushing backwards
  16782. dir = (dir+4)%8; // turn around
  16783. cells = -cells;
  16784. }
  16785. dx = dirx[dir];
  16786. dy = diry[dir];
  16787. unit_blown(&sd->bl, dx, dy, cells, 0);
  16788. return SCRIPT_CMD_SUCCESS;
  16789. }
  16790. /// Invokes buying store preparation window
  16791. /// buyingstore <slots>;
  16792. BUILDIN_FUNC(buyingstore)
  16793. {
  16794. struct map_session_data* sd;
  16795. if( ( sd = script_rid2sd(st) ) == NULL )
  16796. {
  16797. return SCRIPT_CMD_SUCCESS;
  16798. }
  16799. if( npc_isnear(&sd->bl) ) {
  16800. char output[150];
  16801. sprintf(output, msg_txt(sd,662), battle_config.min_npc_vendchat_distance);
  16802. clif_displaymessage(sd->fd, output);
  16803. return SCRIPT_CMD_SUCCESS;
  16804. }
  16805. buyingstore_setup(sd, script_getnum(st,2));
  16806. return SCRIPT_CMD_SUCCESS;
  16807. }
  16808. /// Invokes search store info window
  16809. /// searchstores <uses>,<effect>;
  16810. BUILDIN_FUNC(searchstores)
  16811. {
  16812. unsigned short effect;
  16813. unsigned int uses;
  16814. struct map_session_data* sd;
  16815. if( ( sd = script_rid2sd(st) ) == NULL )
  16816. {
  16817. return SCRIPT_CMD_SUCCESS;
  16818. }
  16819. uses = script_getnum(st,2);
  16820. effect = script_getnum(st,3);
  16821. if( !uses )
  16822. {
  16823. ShowError("buildin_searchstores: Amount of uses cannot be zero.\n");
  16824. return SCRIPT_CMD_FAILURE;
  16825. }
  16826. if( effect > 1 )
  16827. {
  16828. ShowError("buildin_searchstores: Invalid effect id %hu, specified.\n", effect);
  16829. return SCRIPT_CMD_FAILURE;
  16830. }
  16831. searchstore_open(sd, uses, effect);
  16832. return SCRIPT_CMD_SUCCESS;
  16833. }
  16834. /// Displays a number as large digital clock.
  16835. /// showdigit <value>[,<type>];
  16836. BUILDIN_FUNC(showdigit)
  16837. {
  16838. unsigned int type = 0;
  16839. int value;
  16840. struct map_session_data* sd;
  16841. if( ( sd = script_rid2sd(st) ) == NULL )
  16842. {
  16843. return SCRIPT_CMD_SUCCESS;
  16844. }
  16845. value = script_getnum(st,2);
  16846. if( script_hasdata(st,3) )
  16847. {
  16848. type = script_getnum(st,3);
  16849. if( type > 3 )
  16850. {
  16851. ShowError("buildin_showdigit: Invalid type %u.\n", type);
  16852. return SCRIPT_CMD_FAILURE;
  16853. }
  16854. }
  16855. clif_showdigit(sd, (unsigned char)type, value);
  16856. return SCRIPT_CMD_SUCCESS;
  16857. }
  16858. /**
  16859. * Rune Knight
  16860. * makerune <% success bonus>{,<char_id>};
  16861. **/
  16862. BUILDIN_FUNC(makerune) {
  16863. TBL_PC* sd;
  16864. if (!script_charid2sd(3,sd))
  16865. return SCRIPT_CMD_FAILURE;
  16866. clif_skill_produce_mix_list(sd,RK_RUNEMASTERY,24);
  16867. sd->itemid = script_getnum(st,2);
  16868. return SCRIPT_CMD_SUCCESS;
  16869. }
  16870. /**
  16871. * checkdragon({<char_id>}) returns 1 if mounting a dragon or 0 otherwise.
  16872. **/
  16873. BUILDIN_FUNC(checkdragon) {
  16874. TBL_PC* sd;
  16875. if (!script_charid2sd(2,sd))
  16876. return SCRIPT_CMD_FAILURE;
  16877. if( pc_isridingdragon(sd) )
  16878. script_pushint(st,1);
  16879. else
  16880. script_pushint(st,0);
  16881. return SCRIPT_CMD_SUCCESS;
  16882. }
  16883. /**
  16884. * setdragon({optional Color{,<char_id>}}) returns 1 on success or 0 otherwise
  16885. * - Toggles the dragon on a RK if he can mount;
  16886. * @param Color - when not provided uses the green dragon;
  16887. * - 1 : Green Dragon
  16888. * - 2 : Brown Dragon
  16889. * - 3 : Gray Dragon
  16890. * - 4 : Blue Dragon
  16891. * - 5 : Red Dragon
  16892. **/
  16893. BUILDIN_FUNC(setdragon) {
  16894. TBL_PC* sd;
  16895. int color = script_hasdata(st,2) ? script_getnum(st,2) : 0;
  16896. if (!script_charid2sd(3,sd))
  16897. return SCRIPT_CMD_FAILURE;
  16898. if( !pc_checkskill(sd,RK_DRAGONTRAINING) || (sd->class_&MAPID_THIRDMASK) != MAPID_RUNE_KNIGHT )
  16899. script_pushint(st,0);//Doesn't have the skill or it's not a Rune Knight
  16900. else if ( pc_isridingdragon(sd) ) {//Is mounted; release
  16901. pc_setoption(sd, sd->sc.option&~OPTION_DRAGON);
  16902. script_pushint(st,1);
  16903. } else {//Not mounted; Mount now.
  16904. unsigned int option = OPTION_DRAGON1;
  16905. if( color ) {
  16906. option = ( color == 1 ? OPTION_DRAGON1 :
  16907. color == 2 ? OPTION_DRAGON2 :
  16908. color == 3 ? OPTION_DRAGON3 :
  16909. color == 4 ? OPTION_DRAGON4 :
  16910. color == 5 ? OPTION_DRAGON5 : 0);
  16911. if( !option ) {
  16912. ShowWarning("script_setdragon: Unknown Color %d used; changing to green (1)\n",color);
  16913. option = OPTION_DRAGON1;
  16914. }
  16915. }
  16916. pc_setoption(sd, sd->sc.option|option);
  16917. script_pushint(st,1);
  16918. }
  16919. return SCRIPT_CMD_SUCCESS;
  16920. }
  16921. /**
  16922. * ismounting({<char_id>}) returns 1 if mounting a new mount or 0 otherwise
  16923. **/
  16924. BUILDIN_FUNC(ismounting) {
  16925. TBL_PC* sd;
  16926. if (!script_charid2sd(2,sd))
  16927. return SCRIPT_CMD_FAILURE;
  16928. if( &sd->sc && sd->sc.data[SC_ALL_RIDING] )
  16929. script_pushint(st,1);
  16930. else
  16931. script_pushint(st,0);
  16932. return SCRIPT_CMD_SUCCESS;
  16933. }
  16934. /**
  16935. * setmounting({<char_id>}) returns 1 on success or 0 otherwise
  16936. * - Toggles new mounts on a player when he can mount
  16937. * - Will fail if the player is mounting a non-new mount, e.g. dragon, peco, wug, etc.
  16938. * - Will unmount the player is he is already mounting
  16939. **/
  16940. BUILDIN_FUNC(setmounting) {
  16941. TBL_PC* sd;
  16942. if (!script_charid2sd(2,sd))
  16943. return SCRIPT_CMD_FAILURE;
  16944. if( &sd->sc && sd->sc.option&(OPTION_WUGRIDER|OPTION_RIDING|OPTION_DRAGON|OPTION_MADOGEAR) ) {
  16945. clif_msg(sd, NEED_REINS_OF_MOUNT);
  16946. script_pushint(st,0); //can't mount with one of these
  16947. } else {
  16948. if( &sd->sc && sd->sc.data[SC_ALL_RIDING] )
  16949. status_change_end(&sd->bl, SC_ALL_RIDING, INVALID_TIMER); //release mount
  16950. else
  16951. sc_start(NULL, &sd->bl, SC_ALL_RIDING, 10000, 1, INVALID_TIMER); //mount
  16952. script_pushint(st,1);//in both cases, return 1.
  16953. }
  16954. return SCRIPT_CMD_SUCCESS;
  16955. }
  16956. /**
  16957. * Retrieves quantity of arguments provided to callfunc/callsub.
  16958. * getargcount() -> amount of arguments received in a function
  16959. **/
  16960. BUILDIN_FUNC(getargcount) {
  16961. struct script_retinfo* ri;
  16962. if( st->stack->defsp < 1 || st->stack->stack_data[st->stack->defsp - 1].type != C_RETINFO ) {
  16963. ShowError("script:getargcount: used out of function or callsub label!\n");
  16964. st->state = END;
  16965. return SCRIPT_CMD_FAILURE;
  16966. }
  16967. ri = st->stack->stack_data[st->stack->defsp - 1].u.ri;
  16968. script_pushint(st, ri->nargs);
  16969. return SCRIPT_CMD_SUCCESS;
  16970. }
  16971. /**
  16972. * getcharip(<account ID>/<character ID>/<character name>)
  16973. **/
  16974. BUILDIN_FUNC(getcharip)
  16975. {
  16976. struct map_session_data* sd = NULL;
  16977. /* check if a character name is specified */
  16978. if( script_hasdata(st, 2) )
  16979. {
  16980. struct script_data *data;
  16981. data = script_getdata(st, 2);
  16982. get_val(st, data); // Convert into value in case of a variable
  16983. if (data_isstring(data))
  16984. sd = map_nick2sd(script_getstr(st, 2));
  16985. else if (data_isint(data) || script_getnum(st, 2))
  16986. {
  16987. int id = 0;
  16988. id = script_getnum(st, 2);
  16989. sd = (map_id2sd(id) ? map_id2sd(id) : map_charid2sd(id));
  16990. }
  16991. }
  16992. else
  16993. sd = script_rid2sd(st);
  16994. /* check for sd and IP */
  16995. if (!sd || !session[sd->fd]->client_addr)
  16996. {
  16997. script_pushconststr(st, "");
  16998. return SCRIPT_CMD_SUCCESS;
  16999. }
  17000. /* return the client ip_addr converted for output */
  17001. if (sd && sd->fd && session[sd->fd])
  17002. {
  17003. /* initiliaze */
  17004. const char *ip_addr = NULL;
  17005. uint32 ip;
  17006. /* set ip, ip_addr and convert to ip and push str */
  17007. ip = session[sd->fd]->client_addr;
  17008. ip_addr = ip2str(ip, NULL);
  17009. script_pushstrcopy(st, ip_addr);
  17010. }
  17011. return SCRIPT_CMD_SUCCESS;
  17012. }
  17013. /**
  17014. * is_function(<function name>) -> 1 if function exists, 0 otherwise
  17015. **/
  17016. BUILDIN_FUNC(is_function) {
  17017. const char* str = script_getstr(st,2);
  17018. if( strdb_exists(userfunc_db, str) )
  17019. script_pushint(st,1);
  17020. else
  17021. script_pushint(st,0);
  17022. return SCRIPT_CMD_SUCCESS;
  17023. }
  17024. /**
  17025. * get_revision() -> retrieves the current svn revision (if available)
  17026. **/
  17027. BUILDIN_FUNC(get_revision) {
  17028. const char *svn = get_svn_revision();
  17029. if ( svn[0] != UNKNOWN_VERSION )
  17030. script_pushint(st,atoi(svn));
  17031. else
  17032. script_pushint(st,-1); //unknown
  17033. return SCRIPT_CMD_SUCCESS;
  17034. }
  17035. /* get_hash() -> retrieves the current git hash (if available)*/
  17036. BUILDIN_FUNC(get_githash) {
  17037. const char* git = get_git_hash();
  17038. char buf[CHAT_SIZE_MAX];
  17039. safestrncpy(buf,git,strlen(git)+1);
  17040. if ( git[0] != UNKNOWN_VERSION )
  17041. script_pushstr(st,buf);
  17042. else
  17043. script_pushstr(st,"Unknown"); //unknown
  17044. return SCRIPT_CMD_SUCCESS;
  17045. }
  17046. /**
  17047. * freeloop(<toggle>) -> toggles this script instance's looping-check ability
  17048. **/
  17049. BUILDIN_FUNC(freeloop) {
  17050. if( script_hasdata(st,2) ) {
  17051. if( script_getnum(st,2) )
  17052. st->freeloop = 1;
  17053. else
  17054. st->freeloop = 0;
  17055. }
  17056. script_pushint(st, st->freeloop);
  17057. return SCRIPT_CMD_SUCCESS;
  17058. }
  17059. /**
  17060. * @commands (script based)
  17061. **/
  17062. BUILDIN_FUNC(bindatcmd) {
  17063. const char* atcmd;
  17064. const char* eventName;
  17065. int i, level = 0, level2 = 0;
  17066. bool create = false;
  17067. atcmd = script_getstr(st,2);
  17068. eventName = script_getstr(st,3);
  17069. if( *atcmd == atcommand_symbol || *atcmd == charcommand_symbol )
  17070. atcmd++;
  17071. if( script_hasdata(st,4) ) level = script_getnum(st,4);
  17072. if( script_hasdata(st,5) ) level2 = script_getnum(st,5);
  17073. if( atcmd_binding_count == 0 ) {
  17074. CREATE(atcmd_binding,struct atcmd_binding_data*,1);
  17075. create = true;
  17076. } else {
  17077. ARR_FIND(0, atcmd_binding_count, i, strcmp(atcmd_binding[i]->command,atcmd) == 0);
  17078. if( i < atcmd_binding_count ) {/* update existent entry */
  17079. safestrncpy(atcmd_binding[i]->npc_event, eventName, 50);
  17080. atcmd_binding[i]->level = level;
  17081. atcmd_binding[i]->level2 = level2;
  17082. } else
  17083. create = true;
  17084. }
  17085. if( create ) {
  17086. i = atcmd_binding_count;
  17087. if( atcmd_binding_count++ != 0 )
  17088. RECREATE(atcmd_binding,struct atcmd_binding_data*,atcmd_binding_count);
  17089. CREATE(atcmd_binding[i],struct atcmd_binding_data,1);
  17090. safestrncpy(atcmd_binding[i]->command, atcmd, 50);
  17091. safestrncpy(atcmd_binding[i]->npc_event, eventName, 50);
  17092. atcmd_binding[i]->level = level;
  17093. atcmd_binding[i]->level2 = level2;
  17094. }
  17095. return SCRIPT_CMD_SUCCESS;
  17096. }
  17097. BUILDIN_FUNC(unbindatcmd) {
  17098. const char* atcmd;
  17099. int i = 0;
  17100. atcmd = script_getstr(st, 2);
  17101. if( *atcmd == atcommand_symbol || *atcmd == charcommand_symbol )
  17102. atcmd++;
  17103. if( atcmd_binding_count == 0 ) {
  17104. script_pushint(st, 0);
  17105. return SCRIPT_CMD_SUCCESS;
  17106. }
  17107. ARR_FIND(0, atcmd_binding_count, i, strcmp(atcmd_binding[i]->command, atcmd) == 0);
  17108. if( i < atcmd_binding_count ) {
  17109. int cursor = 0;
  17110. aFree(atcmd_binding[i]);
  17111. atcmd_binding[i] = NULL;
  17112. /* compact the list now that we freed a slot somewhere */
  17113. for( i = 0, cursor = 0; i < atcmd_binding_count; i++ ) {
  17114. if( atcmd_binding[i] == NULL )
  17115. continue;
  17116. if( cursor != i ) {
  17117. memmove(&atcmd_binding[cursor], &atcmd_binding[i], sizeof(struct atcmd_binding_data*));
  17118. }
  17119. cursor++;
  17120. }
  17121. if( (atcmd_binding_count = cursor) == 0 )
  17122. aFree(atcmd_binding);
  17123. script_pushint(st, 1);
  17124. } else
  17125. script_pushint(st, 0);/* not found */
  17126. return SCRIPT_CMD_SUCCESS;
  17127. }
  17128. BUILDIN_FUNC(useatcmd) {
  17129. return atcommand_sub(st,3);
  17130. }
  17131. BUILDIN_FUNC(checkre)
  17132. {
  17133. int num;
  17134. num=script_getnum(st,2);
  17135. switch(num){
  17136. case 0:
  17137. #ifdef RENEWAL
  17138. script_pushint(st, 1);
  17139. #else
  17140. script_pushint(st, 0);
  17141. #endif
  17142. break;
  17143. case 1:
  17144. #ifdef RENEWAL_CAST
  17145. script_pushint(st, 1);
  17146. #else
  17147. script_pushint(st, 0);
  17148. #endif
  17149. break;
  17150. case 2:
  17151. #ifdef RENEWAL_DROP
  17152. script_pushint(st, 1);
  17153. #else
  17154. script_pushint(st, 0);
  17155. #endif
  17156. break;
  17157. case 3:
  17158. #ifdef RENEWAL_EXP
  17159. script_pushint(st, 1);
  17160. #else
  17161. script_pushint(st, 0);
  17162. #endif
  17163. break;
  17164. case 4:
  17165. #ifdef RENEWAL_LVDMG
  17166. script_pushint(st, 1);
  17167. #else
  17168. script_pushint(st, 0);
  17169. #endif
  17170. break;
  17171. case 5:
  17172. #ifdef RENEWAL_ASPD
  17173. script_pushint(st, 1);
  17174. #else
  17175. script_pushint(st, 0);
  17176. #endif
  17177. break;
  17178. default:
  17179. ShowWarning("buildin_checkre: unknown parameter.\n");
  17180. break;
  17181. }
  17182. return SCRIPT_CMD_SUCCESS;
  17183. }
  17184. /* getrandgroupitem <group_id>{,<quantity>{,<sub_group>}} */
  17185. BUILDIN_FUNC(getrandgroupitem) {
  17186. TBL_PC* sd;
  17187. int i, get_count = 0;
  17188. uint16 group = script_getnum(st,2), qty = 0;
  17189. uint8 sub_group = 1;
  17190. struct item item_tmp;
  17191. struct s_item_group_entry *entry = NULL;
  17192. if (!(sd = script_rid2sd(st)))
  17193. return SCRIPT_CMD_SUCCESS;
  17194. if (!group) {
  17195. ShowError("buildin_getrandgroupitem: Invalid group id (%d)!\n",script_getnum(st,2));
  17196. return SCRIPT_CMD_FAILURE;
  17197. }
  17198. FETCH(3, qty);
  17199. FETCH(4, sub_group);
  17200. entry = itemdb_get_randgroupitem(group,sub_group);
  17201. if (!entry)
  17202. return SCRIPT_CMD_FAILURE; //ensure valid itemid
  17203. memset(&item_tmp,0,sizeof(item_tmp));
  17204. item_tmp.nameid = entry->nameid;
  17205. item_tmp.identify = itemdb_isidentified(entry->nameid);
  17206. if (!qty)
  17207. qty = entry->amount;
  17208. //Check if it's stackable.
  17209. if (!itemdb_isstackable(entry->nameid)) {
  17210. item_tmp.amount = 1;
  17211. get_count = qty;
  17212. }
  17213. else {
  17214. item_tmp.amount = qty;
  17215. get_count = 1;
  17216. }
  17217. for (i = 0; i < get_count; i++) {
  17218. // if not pet egg
  17219. if (!pet_create_egg(sd, entry->nameid)) {
  17220. unsigned char flag = 0;
  17221. if ((flag = pc_additem(sd,&item_tmp,item_tmp.amount,LOG_TYPE_SCRIPT))) {
  17222. clif_additem(sd,0,0,flag);
  17223. if (pc_candrop(sd,&item_tmp))
  17224. map_addflooritem(&item_tmp,item_tmp.amount,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0,0);
  17225. }
  17226. }
  17227. }
  17228. return SCRIPT_CMD_SUCCESS;
  17229. }
  17230. /* getgroupitem <group_id>{,<char_id>};
  17231. * Gives item(s) to the attached player based on item group contents
  17232. */
  17233. BUILDIN_FUNC(getgroupitem) {
  17234. TBL_PC *sd;
  17235. int group_id = script_getnum(st,2);
  17236. if (!script_charid2sd(3,sd))
  17237. return SCRIPT_CMD_SUCCESS;
  17238. if (itemdb_pc_get_itemgroup(group_id,sd)) {
  17239. ShowError("buildin_getgroupitem: Invalid group id '%d' specified.\n",group_id);
  17240. return SCRIPT_CMD_FAILURE;
  17241. }
  17242. return SCRIPT_CMD_SUCCESS;
  17243. }
  17244. /* cleanmap <map_name>;
  17245. * cleanarea <map_name>, <x0>, <y0>, <x1>, <y1>; */
  17246. static int atcommand_cleanfloor_sub(struct block_list *bl, va_list ap)
  17247. {
  17248. nullpo_ret(bl);
  17249. map_clearflooritem(bl);
  17250. return SCRIPT_CMD_SUCCESS;
  17251. }
  17252. BUILDIN_FUNC(cleanmap)
  17253. {
  17254. const char *mapname;
  17255. int16 m;
  17256. mapname = script_getstr(st, 2);
  17257. m = map_mapname2mapid(mapname);
  17258. if (!m)
  17259. return SCRIPT_CMD_FAILURE;
  17260. if ((script_lastdata(st) - 2) < 4) {
  17261. map_foreachinmap(atcommand_cleanfloor_sub, m, BL_ITEM);
  17262. } else {
  17263. int16 x0 = script_getnum(st, 3);
  17264. int16 y0 = script_getnum(st, 4);
  17265. int16 x1 = script_getnum(st, 5);
  17266. int16 y1 = script_getnum(st, 6);
  17267. if (x0 > 0 && y0 > 0 && x1 > 0 && y1 > 0) {
  17268. map_foreachinarea(atcommand_cleanfloor_sub, m, x0, y0, x1, y1, BL_ITEM);
  17269. } else {
  17270. ShowError("cleanarea: invalid coordinate defined!\n");
  17271. return SCRIPT_CMD_FAILURE;
  17272. }
  17273. }
  17274. return SCRIPT_CMD_SUCCESS;
  17275. }
  17276. /* Cast a skill on the attached player.
  17277. * npcskill <skill id>, <skill lvl>, <stat point>, <NPC level>;
  17278. * npcskill "<skill name>", <skill lvl>, <stat point>, <NPC level>; */
  17279. BUILDIN_FUNC(npcskill)
  17280. {
  17281. uint16 skill_id;
  17282. unsigned short skill_level;
  17283. unsigned int stat_point;
  17284. unsigned int npc_level;
  17285. struct npc_data *nd;
  17286. struct map_session_data *sd;
  17287. struct script_data *data;
  17288. data = script_getdata(st, 2);
  17289. get_val(st, data); // Convert into value in case of a variable
  17290. skill_id = data_isstring(data) ? skill_name2id(script_getstr(st, 2)) : script_getnum(st, 2);
  17291. skill_level = script_getnum(st, 3);
  17292. stat_point = script_getnum(st, 4);
  17293. npc_level = script_getnum(st, 5);
  17294. sd = script_rid2sd(st);
  17295. nd = (struct npc_data *)map_id2bl(sd->npc_id);
  17296. if (stat_point > battle_config.max_third_parameter) {
  17297. ShowError("npcskill: stat point exceeded maximum of %d.\n",battle_config.max_third_parameter );
  17298. return SCRIPT_CMD_FAILURE;
  17299. }
  17300. if (npc_level > MAX_LEVEL) {
  17301. ShowError("npcskill: level exceeded maximum of %d.\n", MAX_LEVEL);
  17302. return SCRIPT_CMD_FAILURE;
  17303. }
  17304. if (sd == NULL || nd == NULL) { //ain't possible, but I don't trust people.
  17305. return SCRIPT_CMD_FAILURE;
  17306. }
  17307. nd->level = npc_level;
  17308. nd->stat_point = stat_point;
  17309. if (!nd->status.hp)
  17310. status_calc_npc(nd, SCO_FIRST);
  17311. else
  17312. status_calc_npc(nd, SCO_NONE);
  17313. if (skill_get_inf(skill_id)&INF_GROUND_SKILL)
  17314. unit_skilluse_pos(&nd->bl, sd->bl.x, sd->bl.y, skill_id, skill_level);
  17315. else
  17316. unit_skilluse_id(&nd->bl, sd->bl.id, skill_id, skill_level);
  17317. return SCRIPT_CMD_SUCCESS;
  17318. }
  17319. /* Consumes an item.
  17320. * consumeitem <item id>{,<char_id>};
  17321. * consumeitem "<item name>"{,<char_id>};
  17322. * @param item: Item ID or name
  17323. */
  17324. BUILDIN_FUNC(consumeitem)
  17325. {
  17326. TBL_PC *sd;
  17327. struct script_data *data;
  17328. struct item_data *item_data;
  17329. if (!script_charid2sd(3, sd))
  17330. return SCRIPT_CMD_FAILURE;
  17331. data = script_getdata( st, 2 );
  17332. get_val( st, data );
  17333. if( data_isstring( data ) ){
  17334. const char *name = conv_str( st, data );
  17335. if( ( item_data = itemdb_searchname( name ) ) == NULL ){
  17336. ShowError( "buildin_consumeitem: Nonexistant item %s requested.\n", name );
  17337. return SCRIPT_CMD_FAILURE;
  17338. }
  17339. }else if( data_isint( data ) ){
  17340. unsigned short nameid = conv_num( st, data );
  17341. if( ( item_data = itemdb_exists( nameid ) ) == NULL ){
  17342. ShowError("buildin_consumeitem: Nonexistant item %hu requested.\n", nameid );
  17343. return SCRIPT_CMD_FAILURE;
  17344. }
  17345. }else{
  17346. ShowError("buildin_consumeitem: invalid data type for argument #1 (%d).\n", data->type );
  17347. return SCRIPT_CMD_FAILURE;
  17348. }
  17349. run_script( item_data->script, 0, sd->bl.id, 0 );
  17350. return SCRIPT_CMD_SUCCESS;
  17351. }
  17352. /** Makes a player sit/stand.
  17353. * sit {"<character name>"};
  17354. * stand {"<character name>"};
  17355. * Note: Use readparam(Sitting) which returns 1 or 0 (sitting or standing).
  17356. * @param name: Player name that will be invoked
  17357. */
  17358. BUILDIN_FUNC(sit)
  17359. {
  17360. TBL_PC *sd;
  17361. if( script_hasdata(st, 2) )
  17362. sd = map_nick2sd(script_getstr(st, 2));
  17363. else
  17364. sd = script_rid2sd(st);
  17365. if( sd == NULL)
  17366. return SCRIPT_CMD_FAILURE;
  17367. if( !pc_issit(sd) ) {
  17368. pc_setsit(sd);
  17369. skill_sit(sd, 1);
  17370. clif_sitting(&sd->bl);
  17371. }
  17372. return SCRIPT_CMD_SUCCESS;
  17373. }
  17374. /** Makes player to stand
  17375. * @param name: Player name that will be set
  17376. */
  17377. BUILDIN_FUNC(stand)
  17378. {
  17379. TBL_PC *sd;
  17380. if( script_hasdata(st, 2) )
  17381. sd = map_nick2sd(script_getstr(st, 2));
  17382. else
  17383. sd = script_rid2sd(st);
  17384. if( sd == NULL)
  17385. return SCRIPT_CMD_FAILURE;
  17386. if( pc_issit(sd) && pc_setstand(sd, false)) {
  17387. skill_sit(sd, 0);
  17388. clif_standing(&sd->bl);
  17389. }
  17390. return SCRIPT_CMD_SUCCESS;
  17391. }
  17392. /** Creates an array of bounded item IDs
  17393. * countbound {<type>{,<char_id>}};
  17394. * @param type: 0 - All bound items; 1 - Account Bound; 2 - Guild Bound; 3 - Party Bound
  17395. * @return amt: Amount of items found
  17396. */
  17397. BUILDIN_FUNC(countbound)
  17398. {
  17399. int i, type, j = 0, k = 0;
  17400. TBL_PC *sd;
  17401. if (!script_charid2sd(3,sd))
  17402. return SCRIPT_CMD_FAILURE;
  17403. type = script_getnum(st,2);
  17404. for( i = 0; i < MAX_INVENTORY; i ++ ) {
  17405. if( sd->status.inventory[i].nameid > 0 && (
  17406. (!type && sd->status.inventory[i].bound) || (type && sd->status.inventory[i].bound == type)
  17407. ))
  17408. {
  17409. pc_setreg(sd,reference_uid(add_str("@bound_items"), k),sd->status.inventory[i].nameid);
  17410. k++;
  17411. j += sd->status.inventory[i].amount;
  17412. }
  17413. }
  17414. script_pushint(st,j);
  17415. return SCRIPT_CMD_SUCCESS;
  17416. }
  17417. /** Creates new party
  17418. * party_create "<party name>"{,<char id>{,<item share>{,<item share type>}}};
  17419. * @param party_name: String of party name that will be created
  17420. * @param char_id: Chara that will be leader of this new party. If no char_id specified, the invoker will be party leader
  17421. * @param item_share: 0-Each Take. 1-Party Share
  17422. * @param item_share_type: 0-Each Take. 1-Even Share
  17423. * @return val: Result value
  17424. * -3 - party name is exist
  17425. * -2 - player is in party already
  17426. * -1 - player is not found
  17427. * 0 - unknown error
  17428. * 1 - success, will return party id $@party_create_id
  17429. */
  17430. BUILDIN_FUNC(party_create)
  17431. {
  17432. char party_name[NAME_LENGTH];
  17433. int item1 = 0, item2 = 0;
  17434. TBL_PC *sd;
  17435. if( (!script_hasdata(st,3) && !(sd = script_rid2sd(st))) || (script_hasdata(st,3) && !(sd = map_charid2sd(script_getnum(st,3)))) ) {
  17436. script_pushint(st,-1);
  17437. return SCRIPT_CMD_FAILURE;
  17438. }
  17439. if( sd->status.party_id ) {
  17440. script_pushint(st,-2);
  17441. return SCRIPT_CMD_FAILURE;
  17442. }
  17443. safestrncpy(party_name,script_getstr(st,2),NAME_LENGTH);
  17444. trim(party_name);
  17445. if( party_searchname(party_name) ) {
  17446. script_pushint(st,-3);
  17447. return SCRIPT_CMD_FAILURE;
  17448. }
  17449. if( script_getnum(st,4) )
  17450. item1 = 1;
  17451. if( script_getnum(st,5) )
  17452. item2 = 1;
  17453. party_create_byscript = 1;
  17454. script_pushint(st,party_create(sd,party_name,item1,item2));
  17455. return SCRIPT_CMD_SUCCESS;
  17456. }
  17457. /** Adds player to specified party
  17458. * party_addmember <party id>,<char id>;
  17459. * @param party_id: The party that will be entered by player
  17460. * @param char_id: Char id of player that will be joined to the party
  17461. * @return val: Result value
  17462. * -4 - party is full
  17463. * -3 - party is not found
  17464. * -2 - player is in party already
  17465. * -1 - player is not found
  17466. * 0 - unknown error
  17467. * 1 - success
  17468. */
  17469. BUILDIN_FUNC(party_addmember)
  17470. {
  17471. int party_id = script_getnum(st,2);
  17472. TBL_PC *sd;
  17473. struct party_data *party;
  17474. if( !(sd = map_charid2sd(script_getnum(st,3))) ) {
  17475. script_pushint(st,-1);
  17476. return SCRIPT_CMD_FAILURE;
  17477. }
  17478. if( sd->status.party_id ) {
  17479. script_pushint(st,-2);
  17480. return SCRIPT_CMD_FAILURE;
  17481. }
  17482. if( !(party = party_search(party_id)) ) {
  17483. script_pushint(st,-3);
  17484. return SCRIPT_CMD_FAILURE;
  17485. }
  17486. if( party->party.count >= MAX_PARTY ) {
  17487. script_pushint(st,-4);
  17488. return SCRIPT_CMD_FAILURE;
  17489. }
  17490. sd->party_invite = party_id;
  17491. script_pushint(st,party_add_member(party_id,sd));
  17492. return SCRIPT_CMD_SUCCESS;
  17493. }
  17494. /** Removes player from his/her party. If party_id and char_id is empty remove the invoker from his/her party
  17495. * party_delmember {<char id>,<party_id>};
  17496. * @param: char_id
  17497. * @param: party_id
  17498. * @return val: Result value
  17499. * -3 - player is not in party
  17500. * -2 - party is not found
  17501. * -1 - player is not found
  17502. * 0 - unknown error
  17503. * 1 - success
  17504. */
  17505. BUILDIN_FUNC(party_delmember)
  17506. {
  17507. TBL_PC *sd = NULL;
  17508. if( !script_hasdata(st,2) && !script_hasdata(st,3) && !(sd = script_rid2sd(st)) ) {
  17509. script_pushint(st,-1);
  17510. return SCRIPT_CMD_FAILURE;
  17511. }
  17512. if( sd || (script_getnum(st,2) && (sd = map_charid2sd(script_getnum(st,2)))) )
  17513. script_pushint(st,party_removemember2(sd,0,0));
  17514. else
  17515. script_pushint(st,party_removemember2(NULL,script_getnum(st,2),script_getnum(st,3)));
  17516. return SCRIPT_CMD_SUCCESS;
  17517. }
  17518. /** Changes party leader of specified party (even the leader is offline)
  17519. * party_changeleader <party id>,<char id>;
  17520. * @param party_id: ID of party
  17521. * @param char_id: Char ID of new leader
  17522. * @return val: Result value
  17523. * -4 - player is party leader already
  17524. * -3 - player is not in this party
  17525. * -2 - player is not found
  17526. * -1 - party is not found
  17527. * 0 - unknown error
  17528. * 1 - success */
  17529. BUILDIN_FUNC(party_changeleader)
  17530. {
  17531. int i, party_id = script_getnum(st,2);
  17532. TBL_PC *sd = NULL;
  17533. TBL_PC *tsd = NULL;
  17534. struct party_data *party = NULL;
  17535. if( !(party = party_search(party_id)) ) {
  17536. script_pushint(st,-1);
  17537. return SCRIPT_CMD_FAILURE;
  17538. }
  17539. if( !(tsd = map_charid2sd(script_getnum(st,3))) ) {
  17540. script_pushint(st,-2);
  17541. return SCRIPT_CMD_FAILURE;
  17542. }
  17543. if( tsd->status.party_id != party_id ) {
  17544. script_pushint(st,-3);
  17545. return SCRIPT_CMD_FAILURE;
  17546. }
  17547. ARR_FIND(0,MAX_PARTY,i,party->party.member[i].leader);
  17548. if( i >= MAX_PARTY ) { //this is should impossible!
  17549. script_pushint(st,0);
  17550. return SCRIPT_CMD_FAILURE;
  17551. }
  17552. if( party->data[i].sd == tsd ) {
  17553. script_pushint(st,-4);
  17554. return SCRIPT_CMD_FAILURE;
  17555. }
  17556. script_pushint(st,party_changeleader(sd,tsd,party));
  17557. return SCRIPT_CMD_SUCCESS;
  17558. }
  17559. /** Changes party option
  17560. * party_changeoption <party id>,<option>,<flag>;
  17561. * @param party_id: ID of party that will be changed
  17562. * @param option: Type of option
  17563. * @return val: -1 - party is not found, 0 - invalid option, 1 - success
  17564. */
  17565. BUILDIN_FUNC(party_changeoption)
  17566. {
  17567. struct party_data *party;
  17568. if( !(party = party_search(script_getnum(st,2))) ) {
  17569. script_pushint(st,-1);
  17570. return SCRIPT_CMD_FAILURE;
  17571. }
  17572. script_pushint(st,party_setoption(party,script_getnum(st,3),script_getnum(st,4)));
  17573. return SCRIPT_CMD_SUCCESS;
  17574. }
  17575. /** Destroys party with party id.
  17576. * party_destroy <party id>;
  17577. * @param party_id: ID of party that will be destroyed
  17578. */
  17579. BUILDIN_FUNC(party_destroy)
  17580. {
  17581. int i;
  17582. struct party_data *party;
  17583. if( !(party = party_search(script_getnum(st,2))) ) {
  17584. script_pushint(st,0);
  17585. return SCRIPT_CMD_FAILURE;
  17586. }
  17587. ARR_FIND(0,MAX_PARTY,i,party->party.member[i].leader);
  17588. if( i >= MAX_PARTY || !party->data[i].sd ) { //leader not online
  17589. int j;
  17590. for( j = 0; j < MAX_PARTY; j++ ) {
  17591. TBL_PC *sd = party->data[j].sd;
  17592. if(sd)
  17593. party_member_withdraw(party->party.party_id,sd->status.account_id,sd->status.char_id);
  17594. else if( party->party.member[j].char_id )
  17595. intif_party_leave(party->party.party_id,party->party.member[j].account_id,party->party.member[j].char_id);
  17596. }
  17597. party_broken(party->party.party_id);
  17598. script_pushint(st,1);
  17599. }
  17600. else //leader leave = party broken
  17601. script_pushint(st,party_leave(party->data[i].sd));
  17602. return SCRIPT_CMD_SUCCESS;
  17603. }
  17604. /** Checks if a player's client version meets a required version or date.
  17605. * @param type: 0 - check by version number; 1 - check by date
  17606. * @param data: Input
  17607. */
  17608. BUILDIN_FUNC(is_clientver) {
  17609. TBL_PC *sd = NULL;
  17610. int type = script_getnum(st,2);
  17611. int data = script_getnum(st,3);
  17612. int ret = 0;
  17613. if (script_hasdata(st,4))
  17614. sd = map_charid2sd(script_getnum(st,4));
  17615. else
  17616. sd = script_rid2sd(st);
  17617. if (sd == NULL) {
  17618. script_pushint(st,0);
  17619. return SCRIPT_CMD_FAILURE;
  17620. }
  17621. switch(type){
  17622. case 0:
  17623. ret = (sd->packet_ver >= data)?1:0;
  17624. break;
  17625. case 1:
  17626. ret = (sd->packet_ver >= date2version(data))?1:0;
  17627. break;
  17628. }
  17629. script_pushint(st,ret);
  17630. return SCRIPT_CMD_SUCCESS;
  17631. }
  17632. /** Returns various information about a player's VIP status. Need to enable VIP system
  17633. * vip_status <type>,{"<character name>"};
  17634. * @param type: Info type, 1: VIP status, 2: Expired date, 3: Remaining time
  17635. * @param name: Character name (Optional)
  17636. */
  17637. BUILDIN_FUNC(vip_status) {
  17638. #ifdef VIP_ENABLE
  17639. TBL_PC *sd;
  17640. char vip_str[26];
  17641. time_t now = time(NULL);
  17642. int type = script_getnum(st, 2);
  17643. if (script_hasdata(st, 3))
  17644. sd = map_nick2sd(script_getstr(st, 3));
  17645. else
  17646. sd = script_rid2sd(st);
  17647. if (sd == NULL)
  17648. return SCRIPT_CMD_FAILURE;
  17649. switch(type) {
  17650. case 1: // Get VIP status.
  17651. script_pushint(st, pc_isvip(sd));
  17652. break;
  17653. case 2: // Get VIP expire date.
  17654. if (pc_isvip(sd)) {
  17655. time_t viptime = sd->vip.time;
  17656. strftime(vip_str, 24, "%Y-%m-%d %H:%M", localtime(&viptime));
  17657. vip_str[25] = '\0';
  17658. script_pushstrcopy(st, vip_str);
  17659. } else
  17660. script_pushint(st, 0);
  17661. break;
  17662. case 3: // Get remaining time.
  17663. if (pc_isvip(sd)) {
  17664. time_t viptime_remain = sd->vip.time - now;
  17665. int year=0,month=0,day=0,hour=0,min=0,sec=0;
  17666. split_time((int)viptime_remain,&year,&month,&day,&hour,&min,&sec);
  17667. safesnprintf(vip_str,sizeof(vip_str),"%d-%d-%d %d:%d",year,month,day,hour,min);
  17668. script_pushstrcopy(st, vip_str);
  17669. } else
  17670. script_pushint(st, 0);
  17671. break;
  17672. }
  17673. #else
  17674. script_pushint(st, 0);
  17675. #endif
  17676. return SCRIPT_CMD_SUCCESS;
  17677. }
  17678. /** Adds or removes VIP time in minutes. Need to enable VIP system
  17679. * vip_time <time in mn>,{"<character name>"};
  17680. * @param time: VIP duration in minutes. If time < 0 remove time, else add time.
  17681. * @param name: Character name (optional)
  17682. */
  17683. BUILDIN_FUNC(vip_time) {
  17684. #ifdef VIP_ENABLE //would be a pain for scripting npc otherwise
  17685. TBL_PC *sd;
  17686. int viptime = script_getnum(st, 2) * 60; // Convert since it's given in minutes.
  17687. if (script_hasdata(st, 3))
  17688. sd = map_nick2sd(script_getstr(st, 3));
  17689. else
  17690. sd = script_rid2sd(st);
  17691. if (sd == NULL)
  17692. return SCRIPT_CMD_FAILURE;
  17693. chrif_req_login_operation(sd->status.account_id, sd->status.name, CHRIF_OP_LOGIN_VIP, viptime, 7, 0);
  17694. #endif
  17695. return SCRIPT_CMD_SUCCESS;
  17696. }
  17697. /** Turns a player into a monster and grants SC attribute effect. [malufett/Hercules]
  17698. * montransform <monster name/ID>, <duration>, <sc type>, <val1>, <val2>, <val3>, <val4>;
  17699. * @param monster: Monster ID or name
  17700. * @param duration: Transform duration in millisecond (ms)
  17701. * @param sc_type: Type of SC that will be affected during the transformation
  17702. * @param val1: Value for SC
  17703. * @param val2: Value for SC
  17704. * @param val3: Value for SC
  17705. * @param val4: Value for SC
  17706. */
  17707. BUILDIN_FUNC(montransform) {
  17708. TBL_PC *sd;
  17709. enum sc_type type;
  17710. int tick, mob_id, val1, val2, val3, val4;
  17711. struct script_data *data;
  17712. val1 = val2 = val3 = val4 = 0;
  17713. if( (sd = script_rid2sd(st)) == NULL )
  17714. return SCRIPT_CMD_FAILURE;
  17715. data = script_getdata(st, 2);
  17716. get_val(st, data); // Convert into value in case of a variable
  17717. if( data_isstring(data) )
  17718. mob_id = mobdb_searchname(script_getstr(st, 2));
  17719. else
  17720. mob_id = mobdb_checkid(script_getnum(st, 2));
  17721. tick = script_getnum(st, 3);
  17722. if (script_hasdata(st, 4))
  17723. type = (sc_type)script_getnum(st, 4);
  17724. else
  17725. type = SC_NONE;
  17726. if (mob_id == 0) {
  17727. if( data_isstring(data) )
  17728. ShowWarning("buildin_montransform: Attempted to use non-existing monster '%s'.\n", script_getstr(st, 2));
  17729. else
  17730. ShowWarning("buildin_montransform: Attempted to use non-existing monster of ID '%d'.\n", script_getnum(st, 2));
  17731. return SCRIPT_CMD_FAILURE;
  17732. }
  17733. if (mob_id == MOBID_EMPERIUM) {
  17734. ShowWarning("buildin_montransform: Monster 'Emperium' cannot be used.\n");
  17735. return SCRIPT_CMD_FAILURE;
  17736. }
  17737. if (!(type >= SC_NONE && type < SC_MAX)) {
  17738. ShowWarning("buildin_montransform: Unsupported status change id %d\n", type);
  17739. return SCRIPT_CMD_FAILURE;
  17740. }
  17741. if (script_hasdata(st, 5))
  17742. val1 = script_getnum(st, 5);
  17743. if (script_hasdata(st, 6))
  17744. val2 = script_getnum(st, 6);
  17745. if (script_hasdata(st, 7))
  17746. val3 = script_getnum(st, 7);
  17747. if (script_hasdata(st, 8))
  17748. val4 = script_getnum(st, 8);
  17749. if (tick != 0) {
  17750. if (battle_config.mon_trans_disable_in_gvg && map_flag_gvg2(sd->bl.m)) {
  17751. clif_displaymessage(sd->fd, msg_txt(sd,731)); // Transforming into monster is not allowed in Guild Wars.
  17752. return SCRIPT_CMD_FAILURE;
  17753. }
  17754. if (sd->disguise){
  17755. clif_displaymessage(sd->fd, msg_txt(sd,729)); // Cannot transform into monster while in disguise.
  17756. return SCRIPT_CMD_FAILURE;
  17757. }
  17758. status_change_end(&sd->bl, SC_MONSTER_TRANSFORM, INVALID_TIMER); // Clear previous
  17759. sc_start2(NULL, &sd->bl, SC_MONSTER_TRANSFORM, 100, mob_id, type, tick);
  17760. if (type != SC_NONE)
  17761. sc_start4(NULL, &sd->bl, type, 100, val1, val2, val3, val4, tick);
  17762. }
  17763. return SCRIPT_CMD_SUCCESS;
  17764. }
  17765. /**
  17766. * Attach script to player for certain duration
  17767. * bonus_script "<script code>",<duration>{,<flag>{,<type>{,<status_icon>{,<char_id>}}}};
  17768. * @param "script code"
  17769. * @param duration
  17770. * @param flag
  17771. * @param icon
  17772. * @param char_id
  17773. * @author [Cydh]
  17774. **/
  17775. BUILDIN_FUNC(bonus_script) {
  17776. uint16 flag = 0;
  17777. int16 icon = SI_BLANK;
  17778. uint32 dur;
  17779. uint8 type = 0;
  17780. TBL_PC* sd;
  17781. const char *script_str = NULL;
  17782. struct s_bonus_script_entry *entry = NULL;
  17783. if (script_hasdata(st,7)) {
  17784. if (!(sd = map_charid2sd(script_getnum(st,7)))) {
  17785. ShowError("buildin_bonus_script: Player CID=%d is not found.\n", script_getnum(st,7));
  17786. return SCRIPT_CMD_FAILURE;
  17787. }
  17788. }
  17789. else
  17790. sd = script_rid2sd(st);
  17791. if (sd == NULL)
  17792. return SCRIPT_CMD_FAILURE;
  17793. script_str = script_getstr(st,2);
  17794. dur = 1000 * abs(script_getnum(st,3));
  17795. FETCH(4, flag);
  17796. FETCH(5, type);
  17797. FETCH(6, icon);
  17798. // No Script string, No Duration!
  17799. if (script_str[0] == '\0' || !dur) {
  17800. ShowError("buildin_bonus_script: Invalid! Script: \"%s\". Duration: %d\n", script_str, dur);
  17801. return SCRIPT_CMD_FAILURE;
  17802. }
  17803. if (strlen(script_str) >= MAX_BONUS_SCRIPT_LENGTH) {
  17804. ShowError("buildin_bonus_script: Script string to long: \"%s\".\n", script_str);
  17805. return SCRIPT_CMD_FAILURE;
  17806. }
  17807. if (icon <= SI_BLANK || icon >= SI_MAX)
  17808. icon = SI_BLANK;
  17809. if ((entry = pc_bonus_script_add(sd, script_str, dur, (enum si_type)icon, flag, type))) {
  17810. linkdb_insert(&sd->bonus_script.head, (void *)((intptr_t)entry), entry);
  17811. status_calc_pc(sd,SCO_NONE);
  17812. }
  17813. return SCRIPT_CMD_SUCCESS;
  17814. }
  17815. /**
  17816. * Removes all bonus script from player
  17817. * bonus_script_clear {<flag>,{<char_id>}};
  17818. * @param flag 0 - Except permanent bonus, 1 - With permanent bonus
  17819. * @param char_id Clear script from this character
  17820. * @author [Cydh]
  17821. */
  17822. BUILDIN_FUNC(bonus_script_clear) {
  17823. TBL_PC* sd;
  17824. bool flag = false;
  17825. if (script_hasdata(st,2))
  17826. flag = script_getnum(st,2);
  17827. if (script_hasdata(st,3))
  17828. sd = map_charid2sd(script_getnum(st,3));
  17829. else
  17830. sd = script_rid2sd(st);
  17831. if (sd == NULL)
  17832. return SCRIPT_CMD_FAILURE;
  17833. pc_bonus_script_clear(sd,(flag ? BSF_PERMANENT : BSF_REM_ALL));
  17834. return SCRIPT_CMD_SUCCESS;
  17835. }
  17836. /** Allows player to use atcommand while talking with NPC
  17837. * enable_command;
  17838. * @author [Cydh], [Kichi]
  17839. */
  17840. BUILDIN_FUNC(enable_command) {
  17841. TBL_PC* sd = script_rid2sd(st);
  17842. if (!sd)
  17843. return SCRIPT_CMD_FAILURE;
  17844. sd->state.disable_atcommand_on_npc = 0;
  17845. return SCRIPT_CMD_SUCCESS;
  17846. }
  17847. /** Prevents player to use atcommand while talking with NPC
  17848. * disable_command;
  17849. * @author [Cydh], [Kichi]
  17850. */
  17851. BUILDIN_FUNC(disable_command) {
  17852. TBL_PC* sd = script_rid2sd(st);
  17853. if (!sd)
  17854. return SCRIPT_CMD_FAILURE;
  17855. sd->state.disable_atcommand_on_npc = 1;
  17856. return SCRIPT_CMD_SUCCESS;
  17857. }
  17858. /** Get the information of the members of a guild by type.
  17859. * getguildmember <guild_id>{,<type>};
  17860. * @param guild_id: ID of guild
  17861. * @param type: Type of option (optional)
  17862. */
  17863. BUILDIN_FUNC(getguildmember)
  17864. {
  17865. struct guild *g = NULL;
  17866. uint8 j = 0;
  17867. g = guild_search(script_getnum(st,2));
  17868. if (g) {
  17869. uint8 i, type = 0;
  17870. struct script_data *data = NULL;
  17871. char *varname = NULL;
  17872. if (script_hasdata(st,3))
  17873. type = script_getnum(st,3);
  17874. if (script_hasdata(st,4)) {
  17875. data = script_getdata(st, 4);
  17876. if (!data_isreference(data)) {
  17877. ShowError("buildin_getguildmember: Error in argument! Please give a variable to store values in.\n");
  17878. return SCRIPT_CMD_FAILURE;
  17879. }
  17880. varname = reference_getname(data);
  17881. if (type <= 0 && varname[strlen(varname)-1] != '$') {
  17882. ShowError("buildin_getguildmember: The array %s is not string type.\n", varname);
  17883. return SCRIPT_CMD_FAILURE;
  17884. }
  17885. }
  17886. for (i = 0; i < MAX_GUILD; i++) {
  17887. if (g->member[i].account_id) {
  17888. switch (type) {
  17889. case 2:
  17890. if (data)
  17891. setd_sub(st, NULL, varname, j, (void *)__64BPRTSIZE(g->member[i].account_id), data->ref);
  17892. else
  17893. mapreg_setreg(reference_uid(add_str("$@guildmemberaid"), j),g->member[i].account_id);
  17894. break;
  17895. case 1:
  17896. if (data)
  17897. setd_sub(st, NULL, varname, j, (void *)__64BPRTSIZE(g->member[i].char_id), data->ref);
  17898. else
  17899. mapreg_setreg(reference_uid(add_str("$@guildmembercid"), j), g->member[i].char_id);
  17900. break;
  17901. default:
  17902. if (data)
  17903. setd_sub(st, NULL, varname, j, (void *)__64BPRTSIZE(g->member[i].name), data->ref);
  17904. else
  17905. mapreg_setregstr(reference_uid(add_str("$@guildmembername$"), j), g->member[i].name);
  17906. break;
  17907. }
  17908. j++;
  17909. }
  17910. }
  17911. }
  17912. mapreg_setreg(add_str("$@guildmembercount"), j);
  17913. script_pushint(st, j);
  17914. return SCRIPT_CMD_SUCCESS;
  17915. }
  17916. /** Adds spirit ball to player for 'duration' in milisecond
  17917. * addspiritball <count>,<duration>{,<char_id>};
  17918. * @param count How many spirit ball will be added
  17919. * @param duration How long spiritball is active until it disappears
  17920. * @param char_id Target player (Optional)
  17921. * @author [Cydh]
  17922. */
  17923. BUILDIN_FUNC(addspiritball) {
  17924. uint8 i, count = script_getnum(st,2);
  17925. uint16 duration = script_getnum(st,3);
  17926. struct map_session_data *sd = NULL;
  17927. if (count == 0)
  17928. return SCRIPT_CMD_SUCCESS;
  17929. if (script_hasdata(st,4)) {
  17930. if (!script_isstring(st,4))
  17931. sd = map_charid2sd(script_getnum(st,4));
  17932. else
  17933. sd = map_nick2sd(script_getstr(st,4));
  17934. }
  17935. else
  17936. sd = script_rid2sd(st);
  17937. if (!sd)
  17938. return SCRIPT_CMD_FAILURE;
  17939. for (i = 0; i < count; i++)
  17940. pc_addspiritball(sd,duration,10);
  17941. return SCRIPT_CMD_SUCCESS;
  17942. }
  17943. /** Deletes the spirit ball(s) from player
  17944. * delspiritball <count>{,<char_id>};
  17945. * @param count How many spirit ball will be deleted
  17946. * @param char_id Target player (Optional)
  17947. * @author [Cydh]
  17948. */
  17949. BUILDIN_FUNC(delspiritball) {
  17950. uint8 count = script_getnum(st,2);
  17951. struct map_session_data *sd = NULL;
  17952. if (count == 0)
  17953. count = 1;
  17954. if (script_hasdata(st,3)) {
  17955. if (!script_isstring(st,3))
  17956. sd = map_charid2sd(script_getnum(st,3));
  17957. else
  17958. sd = map_nick2sd(script_getstr(st,3));
  17959. }
  17960. else
  17961. sd = script_rid2sd(st);
  17962. if (!sd)
  17963. return SCRIPT_CMD_FAILURE;
  17964. pc_delspiritball(sd,count,0);
  17965. return SCRIPT_CMD_SUCCESS;
  17966. }
  17967. /** Counts the spirit ball that player has
  17968. * countspiritball {<char_id>};
  17969. * @param char_id Target player (Optional)
  17970. * @author [Cydh]
  17971. */
  17972. BUILDIN_FUNC(countspiritball) {
  17973. struct map_session_data *sd;
  17974. if (script_hasdata(st,2)) {
  17975. if (!script_isstring(st,2))
  17976. sd = map_charid2sd(script_getnum(st,2));
  17977. else
  17978. sd = map_nick2sd(script_getstr(st,2));
  17979. }
  17980. else
  17981. sd = script_rid2sd(st);
  17982. if (!sd)
  17983. return SCRIPT_CMD_FAILURE;
  17984. script_pushint(st,sd->spiritball);
  17985. return SCRIPT_CMD_SUCCESS;
  17986. }
  17987. /** Merges separated stackable items because of guid
  17988. * mergeitem {<char_id>};
  17989. * @param char_id Char ID (Optional)
  17990. * @author [Cydh]
  17991. */
  17992. BUILDIN_FUNC(mergeitem) {
  17993. struct map_session_data *sd;
  17994. if (!script_charid2sd(2, sd))
  17995. return SCRIPT_CMD_FAILURE;
  17996. clif_merge_item_open(sd);
  17997. return SCRIPT_CMD_SUCCESS;
  17998. }
  17999. /** Merges separated stackable items because of guid (Unofficial)
  18000. * mergeitem2 {<item_id>,{<char_id>}};
  18001. * mergeitem2 {"<item name>",{<char_id>}};
  18002. * @param item Item ID/Name for merging specific item (Optional)
  18003. * @author [Cydh]
  18004. */
  18005. BUILDIN_FUNC(mergeitem2) {
  18006. struct map_session_data *sd;
  18007. struct item *items = NULL;
  18008. uint16 i, count = 0, nameid = 0;
  18009. if (!script_charid2sd(3, sd))
  18010. return SCRIPT_CMD_FAILURE;
  18011. if (script_hasdata(st, 2)) {
  18012. struct script_data *data = script_getdata(st, 2);
  18013. get_val(st, data);
  18014. if (data_isstring(data)) {// "<item name>"
  18015. const char *name = conv_str(st,data);
  18016. struct item_data *id;
  18017. if (!(id = itemdb_searchname(name))) {
  18018. ShowError("buildin_mergeitem2: Nonexistant item %s requested.\n", name);
  18019. script_pushint(st, count);
  18020. return SCRIPT_CMD_FAILURE;
  18021. }
  18022. nameid = id->nameid;
  18023. }
  18024. else if (data_isint(data)) {// <item id>
  18025. nameid = conv_num(st,data);
  18026. if (!itemdb_exists(nameid)) {
  18027. ShowError("buildin_mergeitem: Nonexistant item %d requested.\n", nameid);
  18028. script_pushint(st, count);
  18029. return SCRIPT_CMD_FAILURE;
  18030. }
  18031. }
  18032. }
  18033. for (i = 0; i < MAX_INVENTORY; i++) {
  18034. struct item *it = &sd->status.inventory[i];
  18035. if (!it || !it->unique_id || it->expire_time || !itemdb_isstackable(it->nameid))
  18036. continue;
  18037. if ((!nameid || (nameid == it->nameid))) {
  18038. uint8 k;
  18039. if (!count) {
  18040. CREATE(items, struct item, 1);
  18041. memcpy(&items[count++], it, sizeof(struct item));
  18042. pc_delitem(sd, i, it->amount, 0, 0, LOG_TYPE_NPC);
  18043. continue;
  18044. }
  18045. for (k = 0; k < count; k++) {
  18046. // Find Match
  18047. if (&items[k] && items[k].nameid == it->nameid && items[k].bound == it->bound && memcmp(items[k].card, it->card, sizeof(it->card)) == 0) {
  18048. items[k].amount += it->amount;
  18049. pc_delitem(sd, i, it->amount, 0, 0, LOG_TYPE_NPC);
  18050. break;
  18051. }
  18052. }
  18053. if (k >= count) {
  18054. // New entry
  18055. RECREATE(items, struct item, count+1);
  18056. memcpy(&items[count++], it, sizeof(struct item));
  18057. pc_delitem(sd, i, it->amount, 0, 0, LOG_TYPE_NPC);
  18058. }
  18059. }
  18060. }
  18061. if (!items) // Nothing todo here
  18062. return SCRIPT_CMD_SUCCESS;
  18063. // Retrieve the items
  18064. for (i = 0; i < count; i++) {
  18065. uint8 flag = 0;
  18066. if (!&items[i])
  18067. continue;
  18068. items[i].id = 0;
  18069. items[i].unique_id = 0;
  18070. if ((flag = pc_additem(sd, &items[i], items[i].amount, LOG_TYPE_NPC)))
  18071. clif_additem(sd, i, items[i].amount, flag);
  18072. }
  18073. aFree(items);
  18074. script_pushint(st, count);
  18075. return SCRIPT_CMD_SUCCESS;
  18076. }
  18077. /**
  18078. * Update an entry from NPC shop.
  18079. * npcshopupdate "<name>",<item_id>,<price>{,<stock>}
  18080. **/
  18081. BUILDIN_FUNC(npcshopupdate) {
  18082. const char* npcname = script_getstr(st, 2);
  18083. struct npc_data* nd = npc_name2id(npcname);
  18084. uint16 nameid = script_getnum(st, 3);
  18085. int price = script_getnum(st, 4);
  18086. #if PACKETVER >= 20131223
  18087. uint16 stock = script_hasdata(st,5) ? script_getnum(st,5) : 0;
  18088. #endif
  18089. int i;
  18090. if( !nd || ( nd->subtype != NPCTYPE_SHOP && nd->subtype != NPCTYPE_CASHSHOP && nd->subtype != NPCTYPE_ITEMSHOP && nd->subtype != NPCTYPE_POINTSHOP && nd->subtype != NPCTYPE_MARKETSHOP ) ) { // Not found.
  18091. script_pushint(st,0);
  18092. return SCRIPT_CMD_FAILURE;
  18093. }
  18094. if (!nd->u.shop.count) {
  18095. ShowError("buildin_npcshopupdate: Attempt to update empty shop from '%s'.\n", nd->exname);
  18096. script_pushint(st,0);
  18097. return SCRIPT_CMD_FAILURE;
  18098. }
  18099. for (i = 0; i < nd->u.shop.count; i++) {
  18100. if (nd->u.shop.shop_item[i].nameid == nameid) {
  18101. if (price != 0)
  18102. nd->u.shop.shop_item[i].value = price;
  18103. #if PACKETVER >= 20131223
  18104. if (nd->subtype == NPCTYPE_MARKETSHOP) {
  18105. nd->u.shop.shop_item[i].qty = stock;
  18106. npc_market_tosql(nd->exname, &nd->u.shop.shop_item[i]);
  18107. }
  18108. #endif
  18109. }
  18110. }
  18111. script_pushint(st,1);
  18112. return SCRIPT_CMD_SUCCESS;
  18113. }
  18114. /**
  18115. * Get rid from running script.
  18116. * getattachedrid();
  18117. **/
  18118. BUILDIN_FUNC(getattachedrid) {
  18119. script_pushint(st, st->rid);
  18120. return SCRIPT_CMD_SUCCESS;
  18121. }
  18122. /**
  18123. * Get variable from a Player
  18124. * getvar <variable>,<char_id>;
  18125. */
  18126. BUILDIN_FUNC(getvar) {
  18127. int char_id = script_getnum(st, 3);
  18128. struct map_session_data *sd = map_charid2sd(char_id);
  18129. struct script_data *data = NULL;
  18130. const char *name = NULL;
  18131. if (!sd) {
  18132. ShowError("buildin_getvar: No player found with char id '%d'.\n", char_id);
  18133. return SCRIPT_CMD_FAILURE;
  18134. }
  18135. data = script_getdata(st, 2);
  18136. if (!data_isreference(data)) {
  18137. ShowError("buildin_getvar: Not a variable\n");
  18138. script_reportdata(data);
  18139. script_pushnil(st);
  18140. st->state = END;
  18141. return SCRIPT_CMD_FAILURE;
  18142. }
  18143. name = reference_getname(data);
  18144. if (name[0] == '.' || name[0] == '$' || name[0] == '\'') { // Not a PC variable
  18145. ShowError("buildin_getvar: Invalid scope (not PC variable)\n");
  18146. script_reportdata(data);
  18147. script_pushnil(st);
  18148. st->state = END;
  18149. return SCRIPT_CMD_FAILURE;
  18150. }
  18151. get_val_(st, data, sd);
  18152. if (data_isint(data))
  18153. script_pushint(st, conv_num_(st, data, sd));
  18154. else
  18155. script_pushstrcopy(st, conv_str_(st, data, sd));
  18156. push_val2(st->stack, C_NAME, reference_getuid(data), reference_getref(data));
  18157. return SCRIPT_CMD_SUCCESS;
  18158. }
  18159. /**
  18160. * Display script message
  18161. * showscript "<message>"{,<GID>};
  18162. **/
  18163. BUILDIN_FUNC(showscript) {
  18164. struct block_list *bl = NULL;
  18165. const char *msg = script_getstr(st,2);
  18166. int id = 0;
  18167. if (script_hasdata(st,3)) {
  18168. id = script_getnum(st,3);
  18169. bl = map_id2bl(id);
  18170. }
  18171. else {
  18172. bl = st->rid ? map_id2bl(st->rid) : map_id2bl(st->oid);
  18173. }
  18174. if (!bl) {
  18175. ShowError("buildin_showscript: Script not attached. (id=%d, rid=%d, oid=%d)\n", id, st->rid, st->oid);
  18176. script_pushint(st,0);
  18177. return SCRIPT_CMD_FAILURE;
  18178. }
  18179. clif_showscript(bl, msg);
  18180. script_pushint(st,1);
  18181. return SCRIPT_CMD_SUCCESS;
  18182. }
  18183. /**
  18184. * Ignore the SECURE_NPCTIMEOUT function.
  18185. * ignoretimeout <flag>{,<char_id>};
  18186. */
  18187. BUILDIN_FUNC(ignoretimeout)
  18188. {
  18189. #ifdef SECURE_NPCTIMEOUT
  18190. struct map_session_data *sd = NULL;
  18191. if (script_hasdata(st,3)) {
  18192. if (!script_isstring(st,3))
  18193. sd = map_charid2sd(script_getnum(st,3));
  18194. else
  18195. sd = map_nick2sd(script_getstr(st,3));
  18196. } else
  18197. sd = script_rid2sd(st);
  18198. if (!sd)
  18199. return SCRIPT_CMD_FAILURE;
  18200. sd->state.ignoretimeout = script_getnum(st,2) > 0;
  18201. #endif
  18202. return SCRIPT_CMD_SUCCESS;
  18203. }
  18204. /**
  18205. * geteleminfo <type>{,<char_id>};
  18206. **/
  18207. BUILDIN_FUNC(geteleminfo) {
  18208. TBL_ELEM *ed = NULL;
  18209. TBL_PC *sd = NULL;
  18210. int type = script_getnum(st,2);
  18211. if (!script_charid2sd(3, sd)) {
  18212. script_pushint(st, 0);
  18213. return SCRIPT_CMD_SUCCESS;
  18214. }
  18215. if (!(ed = sd->ed)) {
  18216. //ShowDebug("buildin_geteleminfo: Player doesn't have Elemental.\n");
  18217. script_pushint(st, 0);
  18218. return SCRIPT_CMD_SUCCESS;
  18219. }
  18220. switch (type) {
  18221. case 0: script_pushint(st, ed->elemental.elemental_id); break;
  18222. case 1: script_pushint(st, ed->bl.id); break;
  18223. default:
  18224. ShowError("buildin_geteleminfo: Invalid type '%d'.\n", type);
  18225. script_pushint(st, 0);
  18226. return SCRIPT_CMD_FAILURE;
  18227. }
  18228. return SCRIPT_CMD_SUCCESS;
  18229. }
  18230. /**
  18231. * Set the quest info of quest_id only showed on player in level range.
  18232. * setquestinfo_level <quest_id>,<min_level>,<max_level>
  18233. * @author [Cydh]
  18234. **/
  18235. BUILDIN_FUNC(setquestinfo_level) {
  18236. TBL_NPC* nd = map_id2nd(st->oid);
  18237. int quest_id = script_getnum(st, 2);
  18238. struct questinfo *qi = map_has_questinfo(nd->bl.m, nd, quest_id);
  18239. if (!qi) {
  18240. ShowError("buildin_setquestinfo_level: Quest with ID '%d' is not defined yet.\n", quest_id);
  18241. return SCRIPT_CMD_FAILURE;
  18242. }
  18243. qi->min_level = script_getnum(st, 3);
  18244. qi->max_level = script_getnum(st, 4);
  18245. if (!qi->max_level)
  18246. qi->max_level = MAX_LEVEL;
  18247. return SCRIPT_CMD_SUCCESS;
  18248. }
  18249. /**
  18250. * Set the quest info of quest_id only showed for player that has quest criteria
  18251. * setquestinfo_req <quest_id>,<quest_req_id>,<state>{,<quest_req_id>,<state>,...};
  18252. * @author [Cydh]
  18253. **/
  18254. BUILDIN_FUNC(setquestinfo_req) {
  18255. TBL_NPC* nd = map_id2nd(st->oid);
  18256. int quest_id = script_getnum(st, 2);
  18257. struct questinfo *qi = map_has_questinfo(nd->bl.m, nd, quest_id);
  18258. uint8 i = 0;
  18259. uint8 num = script_lastdata(st);
  18260. if (!qi) {
  18261. ShowError("buildin_setquestinfo_req: Quest with ID '%d' is not defined yet.\n", quest_id);
  18262. return SCRIPT_CMD_FAILURE;
  18263. }
  18264. if (quest_search(quest_id) == &quest_dummy) {
  18265. ShowError("buildin_setquestinfo_req: Quest with ID '%d' is not found in Quest DB.\n", quest_id);
  18266. return SCRIPT_CMD_FAILURE;
  18267. }
  18268. if (num%2) {
  18269. ShowError("buildin_setquestinfo_req: Odd number of parameters(%d) - pairs of requirements are expected.\n", num-2);
  18270. return SCRIPT_CMD_FAILURE;
  18271. }
  18272. for (i = 3; i <= num; i += 2) {
  18273. RECREATE(qi->req, struct questinfo_req, qi->req_count+1);
  18274. qi->req[qi->req_count].quest_id = script_getnum(st, i);
  18275. qi->req[qi->req_count].state = (script_getnum(st, i+1) >= 2) ? 2 : (script_getnum(st, i+1) <= 0) ? 0 : 1;
  18276. qi->req_count++;
  18277. }
  18278. return SCRIPT_CMD_SUCCESS;
  18279. }
  18280. /**
  18281. * Set the quest info of quest_id only showed for player that has specified Job
  18282. * setquestinfo_job <quest_id>,<job>{,<job>...};
  18283. * @author [Cydh]
  18284. **/
  18285. BUILDIN_FUNC(setquestinfo_job) {
  18286. TBL_NPC* nd = map_id2nd(st->oid);
  18287. int quest_id = script_getnum(st, 2);
  18288. struct questinfo *qi = map_has_questinfo(nd->bl.m, nd, quest_id);
  18289. int job_id = 0;
  18290. uint8 i = 0;
  18291. uint8 num = script_lastdata(st)+1;
  18292. if (!qi) {
  18293. ShowError("buildin_setquestinfo_job: Quest with ID '%d' is not defined yet.\n", quest_id);
  18294. return SCRIPT_CMD_FAILURE;
  18295. }
  18296. for (i = 3; i < num; i++) {
  18297. job_id = script_getnum(st, i);
  18298. if (!pcdb_checkid(job_id)) {
  18299. ShowError("buildin_setquestinfo_job: Invalid job id '%d' in Quest with ID %d.\n", job_id, quest_id);
  18300. continue;
  18301. }
  18302. buildin_questinfo_setjob(qi, job_id);
  18303. }
  18304. return SCRIPT_CMD_SUCCESS;
  18305. }
  18306. /**
  18307. * opendressroom(<flag>{,<char_id>});
  18308. */
  18309. BUILDIN_FUNC(opendressroom)
  18310. {
  18311. #if PACKETVER >= 20150513
  18312. int flag = 1;
  18313. TBL_PC* sd;
  18314. if( script_hasdata(st,2) )
  18315. flag = script_getnum(st,2);
  18316. if (!script_charid2sd(3, sd))
  18317. return SCRIPT_CMD_FAILURE;
  18318. clif_dressing_room(sd, flag);
  18319. return SCRIPT_CMD_SUCCESS;
  18320. #else
  18321. return SCRIPT_CMD_FAILURE;
  18322. #endif
  18323. }
  18324. /**
  18325. * navigateto("<map>"{,<x>,<y>,<flag>,<hide_window>,<monster_id>,<char_id>});
  18326. */
  18327. BUILDIN_FUNC(navigateto){
  18328. #if PACKETVER >= 20111010
  18329. TBL_PC* sd;
  18330. const char *mapname;
  18331. uint16 x = 0, y = 0, monster_id = 0;
  18332. uint8 flag = NAV_KAFRA_AND_AIRSHIP;
  18333. bool hideWindow = true;
  18334. mapname = script_getstr(st,2);
  18335. if( script_hasdata(st,3) )
  18336. x = script_getnum(st,3);
  18337. if( script_hasdata(st,4) )
  18338. y = script_getnum(st,4);
  18339. if( script_hasdata(st,5) )
  18340. flag = (uint8)script_getnum(st,5);
  18341. if( script_hasdata(st,6) )
  18342. hideWindow = script_getnum(st,6) ? true : false;
  18343. if( script_hasdata(st,7) )
  18344. monster_id = script_getnum(st,7);
  18345. if (!script_charid2sd(8, sd))
  18346. return SCRIPT_CMD_FAILURE;
  18347. clif_navigateTo(sd,mapname,x,y,flag,hideWindow,monster_id);
  18348. return SCRIPT_CMD_SUCCESS;
  18349. #else
  18350. return SCRIPT_CMD_FAILURE;
  18351. #endif
  18352. }
  18353. /**
  18354. * adopt("<parent_name>","<baby_name>");
  18355. * adopt(<parent_id>,<baby_id>);
  18356. * https://rathena.org/board/topic/104014-suggestion-add-adopt-or-etc/
  18357. */
  18358. BUILDIN_FUNC(adopt)
  18359. {
  18360. TBL_PC *sd, *b_sd;
  18361. struct script_data *data;
  18362. enum adopt_responses response;
  18363. data = script_getdata(st, 2);
  18364. get_val(st, data);
  18365. if (data_isstring(data)) {
  18366. const char *name = conv_str(st, data);
  18367. sd = map_nick2sd(name);
  18368. if (sd == NULL) {
  18369. ShowError("buildin_adopt: Non-existant parent character %s requested.\n", name);
  18370. return SCRIPT_CMD_FAILURE;
  18371. }
  18372. } else if (data_isint(data)) {
  18373. uint32 char_id = conv_num(st, data);
  18374. sd = map_charid2sd(char_id);
  18375. if (sd == NULL) {
  18376. ShowError("buildin_adopt: Non-existant parent character %d requested.\n", char_id);
  18377. return SCRIPT_CMD_FAILURE;
  18378. }
  18379. } else {
  18380. ShowError("buildin_adopt: Invalid data type for argument #1 (%d).", data->type);
  18381. return SCRIPT_CMD_FAILURE;
  18382. }
  18383. data = script_getdata(st, 3);
  18384. get_val(st, data);
  18385. if (data_isstring(data)) {
  18386. const char *name = conv_str(st, data);
  18387. b_sd = map_nick2sd(name);
  18388. if (b_sd == NULL) {
  18389. ShowError("buildin_adopt: Non-existant baby character %s requested.\n", name);
  18390. return SCRIPT_CMD_FAILURE;
  18391. }
  18392. } else if (data_isint(data)) {
  18393. uint32 char_id = conv_num(st, data);
  18394. b_sd = map_charid2sd(char_id);
  18395. if (b_sd == NULL) {
  18396. ShowError("buildin_adopt: Non-existant baby character %d requested.\n", char_id);
  18397. return SCRIPT_CMD_FAILURE;
  18398. }
  18399. } else {
  18400. ShowError("buildin_adopt: Invalid data type for argument #2 (%d).", data->type);
  18401. return SCRIPT_CMD_FAILURE;
  18402. }
  18403. response = pc_try_adopt(sd, map_charid2sd(sd->status.partner_id), b_sd);
  18404. if (response == ADOPT_ALLOWED) {
  18405. TBL_PC *p_sd = map_charid2sd(sd->status.partner_id);
  18406. b_sd->adopt_invite = sd->status.account_id;
  18407. clif_Adopt_request(b_sd, sd, p_sd->status.account_id);
  18408. script_pushint(st, ADOPT_ALLOWED);
  18409. return SCRIPT_CMD_SUCCESS;
  18410. }
  18411. script_pushint(st, response);
  18412. return SCRIPT_CMD_FAILURE;
  18413. }
  18414. /**
  18415. * Returns the minimum or maximum of all the given parameters for integer variables.
  18416. *
  18417. * min( <value or array>{,value or array 2,...} );
  18418. * minimum( <value or array>{,value or array 2,...} );
  18419. * max( <value or array>{,value or array 2,...} );
  18420. * maximum( <value or array>{,value or array 2,...} );
  18421. */
  18422. BUILDIN_FUNC(minmax){
  18423. char *functionname;
  18424. unsigned int i;
  18425. int value;
  18426. // Function pointer for our comparison function (either min or max at the moment)
  18427. int32 (*func)(int32, int32);
  18428. // Get the real function name
  18429. functionname = script_getfuncname(st);
  18430. // Our data should start at offset 2
  18431. i = 2;
  18432. if( !script_hasdata( st, i ) ){
  18433. ShowError( "buildin_%s: no arguments given!\n", functionname );
  18434. st->state = END;
  18435. return SCRIPT_CMD_FAILURE;
  18436. }
  18437. if( strnicmp( functionname, "min", strlen( "min" ) ) == 0 ){
  18438. value = INT_MAX;
  18439. func = i32min;
  18440. }else if( strnicmp( functionname, "max", strlen( "max" ) ) == 0 ){
  18441. value = INT_MIN;
  18442. func = i32max;
  18443. }else{
  18444. ShowError( "buildin_%s: Unknown call case for min/max!\n", functionname );
  18445. st->state = END;
  18446. return SCRIPT_CMD_FAILURE;
  18447. }
  18448. // As long as we have data on our script stack
  18449. while( script_hasdata(st,i) ){
  18450. struct script_data *data;
  18451. // Get the next piece of data from the script stack
  18452. data = script_getdata( st, i );
  18453. // Is the current parameter a single integer?
  18454. if( data_isint( data ) ){
  18455. value = func( value, script_getnum( st, i ) );
  18456. // Is the current parameter an array variable?
  18457. }else if( data_isreference( data ) ){
  18458. const char *name;
  18459. struct map_session_data* sd;
  18460. unsigned int start, end;
  18461. // Get the name of the variable
  18462. name = reference_getname(data);
  18463. // Check if it's a string variable
  18464. if( is_string_variable( name ) ){
  18465. ShowError( "buildin_%s: illegal type, need integer!\n", functionname );
  18466. script_reportdata( data );
  18467. st->state = END;
  18468. return SCRIPT_CMD_FAILURE;
  18469. }
  18470. // Get the session data, if a player is attached
  18471. sd = st->rid ? map_id2sd(st->rid) : NULL;
  18472. // Try to find the array's source pointer
  18473. if( !script_array_src( st, sd, name, reference_getref( data ) ) ){
  18474. ShowError( "buildin_%s: not a array!\n", functionname );
  18475. script_reportdata( data );
  18476. st->state = END;
  18477. return SCRIPT_CMD_FAILURE;
  18478. }
  18479. // Get the start and end indices of the array
  18480. start = reference_getindex( data );
  18481. end = script_array_highest_key( st, sd, name, reference_getref( data ) );
  18482. // Skip empty arrays
  18483. if( start < end ){
  18484. int id;
  18485. // For getting the values we need the id of the array
  18486. id = reference_getid( data );
  18487. // Loop through each value stored in the array
  18488. for( ; start < end; start++ ){
  18489. value = func( value, (int32)__64BPRTSIZE( get_val2( st, reference_uid( id, start ), reference_getref( data ) ) ) );
  18490. script_removetop( st, -1, 0 );
  18491. }
  18492. }
  18493. }else{
  18494. ShowError( "buildin_%s: not a supported data type!\n", functionname );
  18495. script_reportdata( data );
  18496. st->state = END;
  18497. return SCRIPT_CMD_FAILURE;
  18498. }
  18499. // Continue with the next stack entry
  18500. i++;
  18501. }
  18502. script_pushint( st, value );
  18503. return SCRIPT_CMD_SUCCESS;
  18504. }
  18505. /**
  18506. * Safety Base/Job EXP addition than using `set BaseExp,n;` or `set JobExp,n;`
  18507. * Unlike `getexp` that affected by some adjustments.
  18508. * getexp2 <base_exp>,<job_exp>{,<char_id>};
  18509. * @author [Cydh]
  18510. **/
  18511. BUILDIN_FUNC(getexp2) {
  18512. TBL_PC *sd = NULL;
  18513. int base_exp = script_getnum(st, 2);
  18514. int job_exp = script_getnum(st, 3);
  18515. if (!script_charid2sd(4, sd))
  18516. return SCRIPT_CMD_FAILURE;
  18517. if (base_exp == 0 && job_exp == 0)
  18518. return SCRIPT_CMD_SUCCESS;
  18519. if (base_exp > 0)
  18520. pc_gainexp(sd, NULL, base_exp, 0, 2);
  18521. else if (base_exp < 0)
  18522. pc_lostexp(sd, base_exp * -1, 0);
  18523. if (job_exp > 0)
  18524. pc_gainexp(sd, NULL, 0, job_exp, 2);
  18525. else if (job_exp < 0)
  18526. pc_lostexp(sd, 0, job_exp * -1);
  18527. return SCRIPT_CMD_SUCCESS;
  18528. }
  18529. #include "../custom/script.inc"
  18530. // declarations that were supposed to be exported from npc_chat.c
  18531. #ifdef PCRE_SUPPORT
  18532. BUILDIN_FUNC(defpattern);
  18533. BUILDIN_FUNC(activatepset);
  18534. BUILDIN_FUNC(deactivatepset);
  18535. BUILDIN_FUNC(deletepset);
  18536. #endif
  18537. /** Regular expression matching
  18538. * preg_match(<pattern>,<string>{,<offset>})
  18539. */
  18540. BUILDIN_FUNC(preg_match) {
  18541. #ifdef PCRE_SUPPORT
  18542. pcre *re;
  18543. pcre_extra *pcreExtra;
  18544. const char *error;
  18545. int erroffset, r, offset = 0;
  18546. int subStrVec[30];
  18547. const char* pattern = script_getstr(st,2);
  18548. const char* subject = script_getstr(st,3);
  18549. if (script_hasdata(st,4))
  18550. offset = script_getnum(st,4);
  18551. re = pcre_compile(pattern, 0, &error, &erroffset, NULL);
  18552. pcreExtra = pcre_study(re, 0, &error);
  18553. r = pcre_exec(re, pcreExtra, subject, (int)strlen(subject), offset, 0, subStrVec, 30);
  18554. pcre_free(re);
  18555. if (pcreExtra != NULL)
  18556. pcre_free(pcreExtra);
  18557. if (r < 0)
  18558. script_pushint(st,0);
  18559. else
  18560. script_pushint(st,(r > 0) ? r : 30 / 3);
  18561. return SCRIPT_CMD_SUCCESS;
  18562. #else
  18563. ShowDebug("script:preg_match: cannot run without PCRE library enabled.\n");
  18564. script_pushint(st,0);
  18565. return SCRIPT_CMD_SUCCESS;
  18566. #endif
  18567. }
  18568. /// script command definitions
  18569. /// for an explanation on args, see add_buildin_func
  18570. struct script_function buildin_func[] = {
  18571. // NPC interaction
  18572. BUILDIN_DEF(mes,"s*"),
  18573. BUILDIN_DEF(next,""),
  18574. BUILDIN_DEF(close,""),
  18575. BUILDIN_DEF(close2,""),
  18576. BUILDIN_DEF(menu,"sl*"),
  18577. BUILDIN_DEF(select,"s*"), //for future jA script compatibility
  18578. BUILDIN_DEF(prompt,"s*"),
  18579. //
  18580. BUILDIN_DEF(goto,"l"),
  18581. BUILDIN_DEF(callsub,"l*"),
  18582. BUILDIN_DEF(callfunc,"s*"),
  18583. BUILDIN_DEF(return,"?"),
  18584. BUILDIN_DEF(getarg,"i?"),
  18585. BUILDIN_DEF(jobchange,"i??"),
  18586. BUILDIN_DEF(jobname,"i"),
  18587. BUILDIN_DEF(input,"r??"),
  18588. BUILDIN_DEF(warp,"sii"),
  18589. BUILDIN_DEF(areawarp,"siiiisii??"),
  18590. BUILDIN_DEF(warpchar,"siii"), // [LuzZza]
  18591. BUILDIN_DEF(warpparty,"siii?"), // [Fredzilla] [Paradox924X]
  18592. BUILDIN_DEF(warpguild,"siii"), // [Fredzilla]
  18593. BUILDIN_DEF(setlook,"ii?"),
  18594. BUILDIN_DEF(changelook,"ii?"), // Simulates but don't Store it
  18595. BUILDIN_DEF(set,"rv?"),
  18596. BUILDIN_DEF(setarray,"rv*"),
  18597. BUILDIN_DEF(cleararray,"rvi"),
  18598. BUILDIN_DEF(copyarray,"rri"),
  18599. BUILDIN_DEF(getarraysize,"r"),
  18600. BUILDIN_DEF(deletearray,"r?"),
  18601. BUILDIN_DEF(getelementofarray,"ri"),
  18602. BUILDIN_DEF(getitem,"vi?"),
  18603. BUILDIN_DEF(rentitem,"vi?"),
  18604. BUILDIN_DEF(rentitem2,"viiiiiiii?"),
  18605. BUILDIN_DEF(getitem2,"viiiiiiii?"),
  18606. BUILDIN_DEF(getnameditem,"vv"),
  18607. BUILDIN_DEF2(grouprandomitem,"groupranditem","i?"),
  18608. BUILDIN_DEF(makeitem,"visii"),
  18609. BUILDIN_DEF(makeitem2,"visiiiiiiiii"),
  18610. BUILDIN_DEF(delitem,"vi?"),
  18611. BUILDIN_DEF2(delitem,"storagedelitem","vi?"),
  18612. BUILDIN_DEF2(delitem,"cartdelitem","vi?"),
  18613. BUILDIN_DEF(delitem2,"viiiiiiii?"),
  18614. BUILDIN_DEF2(delitem2,"storagedelitem2","viiiiiiii?"),
  18615. BUILDIN_DEF2(delitem2,"cartdelitem2","viiiiiiii?"),
  18616. BUILDIN_DEF2(enableitemuse,"enable_items",""),
  18617. BUILDIN_DEF2(disableitemuse,"disable_items",""),
  18618. BUILDIN_DEF(cutin,"si"),
  18619. BUILDIN_DEF(viewpoint,"iiiii"),
  18620. BUILDIN_DEF(heal,"ii?"),
  18621. BUILDIN_DEF(itemheal,"ii?"),
  18622. BUILDIN_DEF(percentheal,"ii?"),
  18623. BUILDIN_DEF(rand,"i?"),
  18624. BUILDIN_DEF(countitem,"v?"),
  18625. BUILDIN_DEF2(countitem,"storagecountitem","v?"),
  18626. BUILDIN_DEF2(countitem,"cartcountitem","v?"),
  18627. BUILDIN_DEF2(countitem,"countitem2","viiiiiii?"),
  18628. BUILDIN_DEF2(countitem,"storagecountitem2","viiiiiii?"),
  18629. BUILDIN_DEF2(countitem,"cartcountitem2","viiiiiii?"),
  18630. BUILDIN_DEF(checkweight,"vi*"),
  18631. BUILDIN_DEF(checkweight2,"rr"),
  18632. BUILDIN_DEF(readparam,"i?"),
  18633. BUILDIN_DEF(getcharid,"i?"),
  18634. BUILDIN_DEF(getnpcid,"i?"),
  18635. BUILDIN_DEF(getpartyname,"i"),
  18636. BUILDIN_DEF(getpartymember,"i??"),
  18637. BUILDIN_DEF(getpartyleader,"i?"),
  18638. BUILDIN_DEF(getguildname,"i"),
  18639. BUILDIN_DEF(getguildmaster,"i"),
  18640. BUILDIN_DEF(getguildmasterid,"i"),
  18641. BUILDIN_DEF(strcharinfo,"i?"),
  18642. BUILDIN_DEF(strnpcinfo,"i"),
  18643. BUILDIN_DEF(getequipid,"i?"),
  18644. BUILDIN_DEF(getequipuniqueid,"i?"),
  18645. BUILDIN_DEF(getequipname,"i?"),
  18646. BUILDIN_DEF(getbrokenid,"i?"), // [Valaris]
  18647. BUILDIN_DEF(repair,"i?"), // [Valaris]
  18648. BUILDIN_DEF(repairall,"?"),
  18649. BUILDIN_DEF(getequipisequiped,"i?"),
  18650. BUILDIN_DEF(getequipisenableref,"i?"),
  18651. BUILDIN_DEF(getequiprefinerycnt,"i?"),
  18652. BUILDIN_DEF(getequipweaponlv,"i?"),
  18653. BUILDIN_DEF(getequippercentrefinery,"i?"),
  18654. BUILDIN_DEF(successrefitem,"i??"),
  18655. BUILDIN_DEF(failedrefitem,"i?"),
  18656. BUILDIN_DEF(downrefitem,"i??"),
  18657. BUILDIN_DEF(statusup,"i?"),
  18658. BUILDIN_DEF(statusup2,"ii?"),
  18659. BUILDIN_DEF(bonus,"iv"),
  18660. BUILDIN_DEF2(bonus,"bonus2","ivi"),
  18661. BUILDIN_DEF2(bonus,"bonus3","ivii"),
  18662. BUILDIN_DEF2(bonus,"bonus4","ivvii"),
  18663. BUILDIN_DEF2(bonus,"bonus5","ivviii"),
  18664. BUILDIN_DEF(autobonus,"sii??"),
  18665. BUILDIN_DEF(autobonus2,"sii??"),
  18666. BUILDIN_DEF(autobonus3,"siiv?"),
  18667. BUILDIN_DEF(skill,"vi?"),
  18668. BUILDIN_DEF2(skill,"addtoskill","vi?"), // [Valaris]
  18669. BUILDIN_DEF(guildskill,"vi"),
  18670. BUILDIN_DEF(getskilllv,"v"),
  18671. BUILDIN_DEF(getgdskilllv,"iv"),
  18672. BUILDIN_DEF(basicskillcheck,""),
  18673. BUILDIN_DEF(getgmlevel,"?"),
  18674. BUILDIN_DEF(getgroupid,"?"),
  18675. BUILDIN_DEF(end,""),
  18676. BUILDIN_DEF(checkoption,"i?"),
  18677. BUILDIN_DEF(setoption,"i??"),
  18678. BUILDIN_DEF(setcart,"??"),
  18679. BUILDIN_DEF(checkcart,"?"),
  18680. BUILDIN_DEF(setfalcon,"??"),
  18681. BUILDIN_DEF(checkfalcon,"?"),
  18682. BUILDIN_DEF(setriding,"??"),
  18683. BUILDIN_DEF(checkriding,"?"),
  18684. BUILDIN_DEF(checkwug,"?"),
  18685. BUILDIN_DEF(checkmadogear,"?"),
  18686. BUILDIN_DEF(setmadogear,"??"),
  18687. BUILDIN_DEF2(savepoint,"save","sii?"),
  18688. BUILDIN_DEF(savepoint,"sii?"),
  18689. BUILDIN_DEF(gettimetick,"i"),
  18690. BUILDIN_DEF(gettime,"i"),
  18691. BUILDIN_DEF(gettimestr,"si"),
  18692. BUILDIN_DEF(openstorage,""),
  18693. BUILDIN_DEF(guildopenstorage,""),
  18694. BUILDIN_DEF(itemskill,"vi"),
  18695. BUILDIN_DEF(produce,"i"),
  18696. BUILDIN_DEF(cooking,"i"),
  18697. BUILDIN_DEF(monster,"siisii???"),
  18698. BUILDIN_DEF(getmobdrops,"i"),
  18699. BUILDIN_DEF(areamonster,"siiiisii???"),
  18700. BUILDIN_DEF(killmonster,"ss?"),
  18701. BUILDIN_DEF(killmonsterall,"s?"),
  18702. BUILDIN_DEF(clone,"siisi????"),
  18703. BUILDIN_DEF(doevent,"s"),
  18704. BUILDIN_DEF(donpcevent,"s"),
  18705. BUILDIN_DEF(cmdothernpc,"ss"),
  18706. BUILDIN_DEF(addtimer,"is"),
  18707. BUILDIN_DEF(deltimer,"s"),
  18708. BUILDIN_DEF(addtimercount,"is"),
  18709. BUILDIN_DEF(initnpctimer,"??"),
  18710. BUILDIN_DEF(stopnpctimer,"??"),
  18711. BUILDIN_DEF(startnpctimer,"??"),
  18712. BUILDIN_DEF(setnpctimer,"i?"),
  18713. BUILDIN_DEF(getnpctimer,"i?"),
  18714. BUILDIN_DEF(attachnpctimer,"?"), // attached the player id to the npc timer [Celest]
  18715. BUILDIN_DEF(detachnpctimer,"?"), // detached the player id from the npc timer [Celest]
  18716. BUILDIN_DEF(playerattached,""), // returns id of the current attached player. [Skotlex]
  18717. BUILDIN_DEF(announce,"si?????"),
  18718. BUILDIN_DEF(mapannounce,"ssi?????"),
  18719. BUILDIN_DEF(areaannounce,"siiiisi?????"),
  18720. BUILDIN_DEF(getusers,"i"),
  18721. BUILDIN_DEF(getmapguildusers,"si"),
  18722. BUILDIN_DEF(getmapusers,"s"),
  18723. BUILDIN_DEF(getareausers,"siiii"),
  18724. BUILDIN_DEF(getareadropitem,"siiiiv"),
  18725. BUILDIN_DEF(enablenpc,"s"),
  18726. BUILDIN_DEF(disablenpc,"s"),
  18727. BUILDIN_DEF(hideoffnpc,"s"),
  18728. BUILDIN_DEF(hideonnpc,"s"),
  18729. BUILDIN_DEF(sc_start,"iii???"),
  18730. BUILDIN_DEF2(sc_start,"sc_start2","iiii???"),
  18731. BUILDIN_DEF2(sc_start,"sc_start4","iiiiii???"),
  18732. BUILDIN_DEF(sc_end,"i?"),
  18733. BUILDIN_DEF(getstatus, "i??"),
  18734. BUILDIN_DEF(getscrate,"ii?"),
  18735. BUILDIN_DEF(debugmes,"s"),
  18736. BUILDIN_DEF2(catchpet,"pet","i"),
  18737. BUILDIN_DEF2(birthpet,"bpet",""),
  18738. BUILDIN_DEF(resetlvl,"i?"),
  18739. BUILDIN_DEF(resetstatus,"?"),
  18740. BUILDIN_DEF(resetskill,"?"),
  18741. BUILDIN_DEF(skillpointcount,"?"),
  18742. BUILDIN_DEF(changebase,"i?"),
  18743. BUILDIN_DEF(changesex,"?"),
  18744. BUILDIN_DEF(changecharsex,"?"),
  18745. BUILDIN_DEF(waitingroom,"si?????"),
  18746. BUILDIN_DEF(delwaitingroom,"?"),
  18747. BUILDIN_DEF(waitingroomkick,"ss"),
  18748. BUILDIN_DEF(getwaitingroomusers, "?"),
  18749. BUILDIN_DEF2(waitingroomkickall,"kickwaitingroomall","?"),
  18750. BUILDIN_DEF(enablewaitingroomevent,"?"),
  18751. BUILDIN_DEF(disablewaitingroomevent,"?"),
  18752. BUILDIN_DEF2(enablewaitingroomevent,"enablearena",""), // Added by RoVeRT
  18753. BUILDIN_DEF2(disablewaitingroomevent,"disablearena",""), // Added by RoVeRT
  18754. BUILDIN_DEF(getwaitingroomstate,"i?"),
  18755. BUILDIN_DEF(warpwaitingpc,"sii?"),
  18756. BUILDIN_DEF(attachrid,"i"),
  18757. BUILDIN_DEF(addrid,"i?????"),
  18758. BUILDIN_DEF(detachrid,""),
  18759. BUILDIN_DEF(isloggedin,"i?"),
  18760. BUILDIN_DEF(setmapflagnosave,"ssii"),
  18761. BUILDIN_DEF(getmapflag,"si?"),
  18762. BUILDIN_DEF(setmapflag,"si??"),
  18763. BUILDIN_DEF(removemapflag,"si?"),
  18764. BUILDIN_DEF(pvpon,"s"),
  18765. BUILDIN_DEF(pvpoff,"s"),
  18766. BUILDIN_DEF(gvgon,"s"),
  18767. BUILDIN_DEF(gvgoff,"s"),
  18768. BUILDIN_DEF(emotion,"i??"),
  18769. BUILDIN_DEF(maprespawnguildid,"sii"),
  18770. BUILDIN_DEF(agitstart,""), // <Agit>
  18771. BUILDIN_DEF(agitend,""),
  18772. BUILDIN_DEF(agitcheck,""), // <Agitcheck>
  18773. BUILDIN_DEF(flagemblem,"i"), // Flag Emblem
  18774. BUILDIN_DEF(getcastlename,"s"),
  18775. BUILDIN_DEF(getcastledata,"si"),
  18776. BUILDIN_DEF(setcastledata,"sii"),
  18777. BUILDIN_DEF(requestguildinfo,"i?"),
  18778. BUILDIN_DEF(getequipcardcnt,"i"),
  18779. BUILDIN_DEF(successremovecards,"i"),
  18780. BUILDIN_DEF(failedremovecards,"ii"),
  18781. BUILDIN_DEF(marriage,"s"),
  18782. BUILDIN_DEF2(wedding_effect,"wedding",""),
  18783. BUILDIN_DEF(divorce,"?"),
  18784. BUILDIN_DEF(ispartneron,"?"),
  18785. BUILDIN_DEF(getpartnerid,"?"),
  18786. BUILDIN_DEF(getchildid,"?"),
  18787. BUILDIN_DEF(getmotherid,"?"),
  18788. BUILDIN_DEF(getfatherid,"?"),
  18789. BUILDIN_DEF(warppartner,"sii"),
  18790. BUILDIN_DEF(getitemname,"v"),
  18791. BUILDIN_DEF(getitemslots,"i"),
  18792. BUILDIN_DEF(makepet,"i"),
  18793. BUILDIN_DEF(getexp,"ii?"),
  18794. BUILDIN_DEF(getinventorylist,"?"),
  18795. BUILDIN_DEF(getskilllist,"?"),
  18796. BUILDIN_DEF(clearitem,"?"),
  18797. BUILDIN_DEF(classchange,"ii"),
  18798. BUILDIN_DEF(misceffect,"i"),
  18799. BUILDIN_DEF(playBGM,"s"),
  18800. BUILDIN_DEF(playBGMall,"s?????"),
  18801. BUILDIN_DEF(soundeffect,"si"),
  18802. BUILDIN_DEF(soundeffectall,"si?????"), // SoundEffectAll [Codemaster]
  18803. BUILDIN_DEF(strmobinfo,"ii"), // display mob data [Valaris]
  18804. BUILDIN_DEF(guardian,"siisi??"), // summon guardians
  18805. BUILDIN_DEF(guardianinfo,"sii"), // display guardian data [Valaris]
  18806. BUILDIN_DEF(petskillbonus,"iiii"), // [Valaris]
  18807. BUILDIN_DEF(petrecovery,"ii"), // [Valaris]
  18808. BUILDIN_DEF(petloot,"i"), // [Valaris]
  18809. BUILDIN_DEF(petheal,"iiii"), // [Valaris]
  18810. BUILDIN_DEF(petskillattack,"viii"), // [Skotlex]
  18811. BUILDIN_DEF(petskillattack2,"viiii"), // [Valaris]
  18812. BUILDIN_DEF(petskillsupport,"viiii"), // [Skotlex]
  18813. BUILDIN_DEF(skilleffect,"vi"), // skill effect [Celest]
  18814. BUILDIN_DEF(npcskilleffect,"viii"), // npc skill effect [Valaris]
  18815. BUILDIN_DEF(specialeffect,"i??"), // npc skill effect [Valaris]
  18816. BUILDIN_DEF(specialeffect2,"i??"), // skill effect on players[Valaris]
  18817. BUILDIN_DEF(nude,"?"), // nude command [Valaris]
  18818. BUILDIN_DEF(mapwarp,"ssii??"), // Added by RoVeRT
  18819. BUILDIN_DEF(atcommand,"s"), // [MouseJstr]
  18820. BUILDIN_DEF2(atcommand,"charcommand","s"), // [MouseJstr]
  18821. BUILDIN_DEF(movenpc,"sii?"), // [MouseJstr]
  18822. BUILDIN_DEF(message,"ss"), // [MouseJstr]
  18823. BUILDIN_DEF(npctalk,"s"), // [Valaris]
  18824. BUILDIN_DEF(mobcount,"ss"),
  18825. BUILDIN_DEF(getlook,"i?"),
  18826. BUILDIN_DEF(getsavepoint,"i?"),
  18827. BUILDIN_DEF(npcspeed,"i"), // [Valaris]
  18828. BUILDIN_DEF(npcwalkto,"ii"), // [Valaris]
  18829. BUILDIN_DEF(npcstop,""), // [Valaris]
  18830. BUILDIN_DEF(getmapxy,"rrri?"), //by Lorky [Lupus]
  18831. BUILDIN_DEF(mapid2name,"i"),
  18832. BUILDIN_DEF(checkoption1,"i?"),
  18833. BUILDIN_DEF(checkoption2,"i?"),
  18834. BUILDIN_DEF(guildgetexp,"i"),
  18835. BUILDIN_DEF(guildchangegm,"is"),
  18836. BUILDIN_DEF(logmes,"s"), //this command actls as MES but rints info into LOG file either SQL/TXT [Lupus]
  18837. BUILDIN_DEF(summon,"si??"), // summons a slave monster [Celest]
  18838. BUILDIN_DEF(isnight,""), // check whether it is night time [Celest]
  18839. BUILDIN_DEF(isday,""), // check whether it is day time [Celest]
  18840. BUILDIN_DEF(isequipped,"i*"), // check whether another item/card has been equipped [Celest]
  18841. BUILDIN_DEF(isequippedcnt,"i*"), // check how many items/cards are being equipped [Celest]
  18842. BUILDIN_DEF(cardscnt,"i*"), // check how many items/cards are being equipped in the same arm [Lupus]
  18843. BUILDIN_DEF(getrefine,""), // returns the refined number of the current item, or an item with index specified [celest]
  18844. BUILDIN_DEF(night,""), // sets the server to night time
  18845. BUILDIN_DEF(day,""), // sets the server to day time
  18846. #ifdef PCRE_SUPPORT
  18847. BUILDIN_DEF(defpattern,"iss"), // Define pattern to listen for [MouseJstr]
  18848. BUILDIN_DEF(activatepset,"i"), // Activate a pattern set [MouseJstr]
  18849. BUILDIN_DEF(deactivatepset,"i"), // Deactive a pattern set [MouseJstr]
  18850. BUILDIN_DEF(deletepset,"i"), // Delete a pattern set [MouseJstr]
  18851. #endif
  18852. BUILDIN_DEF(preg_match,"ss?"),
  18853. BUILDIN_DEF(dispbottom,"s??"), //added from jA [Lupus]
  18854. BUILDIN_DEF(recovery,"i???"),
  18855. BUILDIN_DEF(getpetinfo,"i?"),
  18856. BUILDIN_DEF(gethominfo,"i?"),
  18857. BUILDIN_DEF(getmercinfo,"i?"),
  18858. BUILDIN_DEF(checkequipedcard,"i"),
  18859. BUILDIN_DEF(jump_zero,"il"), //for future jA script compatibility
  18860. BUILDIN_DEF(globalmes,"s?"), //end jA addition
  18861. BUILDIN_DEF(unequip,"i?"), // unequip command [Spectre]
  18862. BUILDIN_DEF(getstrlen,"s"), //strlen [Valaris]
  18863. BUILDIN_DEF(charisalpha,"si"), //isalpha [Valaris]
  18864. BUILDIN_DEF(charat,"si"),
  18865. BUILDIN_DEF(setchar,"ssi"),
  18866. BUILDIN_DEF(insertchar,"ssi"),
  18867. BUILDIN_DEF(delchar,"si"),
  18868. BUILDIN_DEF(strtoupper,"s"),
  18869. BUILDIN_DEF(strtolower,"s"),
  18870. BUILDIN_DEF(charisupper, "si"),
  18871. BUILDIN_DEF(charislower, "si"),
  18872. BUILDIN_DEF(substr,"sii"),
  18873. BUILDIN_DEF(explode, "rss"),
  18874. BUILDIN_DEF(implode, "r?"),
  18875. BUILDIN_DEF(sprintf,"s*"), // [Mirei]
  18876. BUILDIN_DEF(sscanf,"ss*"), // [Mirei]
  18877. BUILDIN_DEF(strpos,"ss?"),
  18878. BUILDIN_DEF(replacestr,"sss??"),
  18879. BUILDIN_DEF(countstr,"ss?"),
  18880. BUILDIN_DEF(setnpcdisplay,"sv??"),
  18881. BUILDIN_DEF(compare,"ss"), // Lordalfa - To bring strstr to scripting Engine.
  18882. BUILDIN_DEF(strcmp,"ss"),
  18883. BUILDIN_DEF(getiteminfo,"ii"), //[Lupus] returns Items Buy / sell Price, etc info
  18884. BUILDIN_DEF(setiteminfo,"iii"), //[Lupus] set Items Buy / sell Price, etc info
  18885. BUILDIN_DEF(getequipcardid,"ii"), //[Lupus] returns CARD ID or other info from CARD slot N of equipped item
  18886. // [zBuffer] List of mathematics commands --->
  18887. BUILDIN_DEF(sqrt,"i"),
  18888. BUILDIN_DEF(pow,"ii"),
  18889. BUILDIN_DEF(distance,"iiii"),
  18890. BUILDIN_DEF2(minmax,"min", "*"),
  18891. BUILDIN_DEF2(minmax,"minimum", "*"),
  18892. BUILDIN_DEF2(minmax,"max", "*"),
  18893. BUILDIN_DEF2(minmax,"maximum", "*"),
  18894. // <--- [zBuffer] List of mathematics commands
  18895. BUILDIN_DEF(md5,"s"),
  18896. // [zBuffer] List of dynamic var commands --->
  18897. BUILDIN_DEF(getd,"s"),
  18898. BUILDIN_DEF(setd,"sv?"),
  18899. BUILDIN_DEF(callshop,"s?"), // [Skotlex]
  18900. BUILDIN_DEF(npcshopitem,"sii*"), // [Lance]
  18901. BUILDIN_DEF(npcshopadditem,"sii*"),
  18902. BUILDIN_DEF(npcshopdelitem,"si*"),
  18903. BUILDIN_DEF(npcshopattach,"s?"),
  18904. BUILDIN_DEF(equip,"i?"),
  18905. BUILDIN_DEF(autoequip,"ii"),
  18906. BUILDIN_DEF(setbattleflag,"si"),
  18907. BUILDIN_DEF(getbattleflag,"s"),
  18908. BUILDIN_DEF(setitemscript,"is?"), //Set NEW item bonus script. Lupus
  18909. BUILDIN_DEF(disguise,"i?"), //disguise player. Lupus
  18910. BUILDIN_DEF(undisguise,"?"), //undisguise player. Lupus
  18911. BUILDIN_DEF(getmonsterinfo,"ii"), //Lupus
  18912. BUILDIN_DEF(addmonsterdrop,"vii"), //Akinari [Lupus]
  18913. BUILDIN_DEF(delmonsterdrop,"vi"), //Akinari [Lupus]
  18914. BUILDIN_DEF(axtoi,"s"),
  18915. BUILDIN_DEF(query_sql,"s*"),
  18916. BUILDIN_DEF(query_logsql,"s*"),
  18917. BUILDIN_DEF(escape_sql,"v"),
  18918. BUILDIN_DEF(atoi,"s"),
  18919. BUILDIN_DEF(strtol,"si"),
  18920. // [zBuffer] List of player cont commands --->
  18921. BUILDIN_DEF(rid2name,"i"),
  18922. BUILDIN_DEF(pcfollow,"ii"),
  18923. BUILDIN_DEF(pcstopfollow,"i"),
  18924. BUILDIN_DEF(pcblockmove,"ii"),
  18925. // <--- [zBuffer] List of player cont commands
  18926. // [zBuffer] List of unit control commands --->
  18927. BUILDIN_DEF(getunittype,"i"),
  18928. BUILDIN_DEF(getunitname,"i"),
  18929. BUILDIN_DEF(setunitname,"is"),
  18930. BUILDIN_DEF(getunitdata,"i*"),
  18931. BUILDIN_DEF(setunitdata,"iii"),
  18932. BUILDIN_DEF(unitwalk,"iii?"),
  18933. BUILDIN_DEF2(unitwalk,"unitwalkto","ii?"),
  18934. BUILDIN_DEF(unitkill,"i"),
  18935. BUILDIN_DEF(unitwarp,"isii"),
  18936. BUILDIN_DEF(unitattack,"iv?"),
  18937. BUILDIN_DEF(unitstopattack,"i"),
  18938. BUILDIN_DEF(unitstopwalk,"i"),
  18939. BUILDIN_DEF2(pcblockmove,"unitblockmove","ii"),
  18940. BUILDIN_DEF(unittalk,"is"),
  18941. BUILDIN_DEF(unitemote,"ii"),
  18942. BUILDIN_DEF(unitskilluseid,"ivi??"), // originally by Qamera [Celest]
  18943. BUILDIN_DEF(unitskillusepos,"iviii?"), // [Celest]
  18944. // <--- [zBuffer] List of unit control commands
  18945. BUILDIN_DEF(sleep,"i"),
  18946. BUILDIN_DEF(sleep2,"i"),
  18947. BUILDIN_DEF(awake,"s"),
  18948. BUILDIN_DEF(getvariableofnpc,"rs"),
  18949. BUILDIN_DEF(warpportal,"iisii"),
  18950. BUILDIN_DEF2(homunculus_evolution,"homevolution",""), //[orn]
  18951. BUILDIN_DEF2(homunculus_mutate,"hommutate","?"),
  18952. BUILDIN_DEF(morphembryo,""),
  18953. BUILDIN_DEF2(homunculus_shuffle,"homshuffle",""), //[Zephyrus]
  18954. BUILDIN_DEF(checkhomcall,""),
  18955. BUILDIN_DEF(eaclass,"??"), //[Skotlex]
  18956. BUILDIN_DEF(roclass,"i?"), //[Skotlex]
  18957. BUILDIN_DEF(checkvending,"?"),
  18958. BUILDIN_DEF(checkchatting,"?"),
  18959. BUILDIN_DEF(checkidle,"?"),
  18960. BUILDIN_DEF(openmail,"?"),
  18961. BUILDIN_DEF(openauction,"?"),
  18962. BUILDIN_DEF(checkcell,"siii"),
  18963. BUILDIN_DEF(setcell,"siiiiii"),
  18964. BUILDIN_DEF(setwall,"siiiiis"),
  18965. BUILDIN_DEF(delwall,"s"),
  18966. BUILDIN_DEF(searchitem,"rs"),
  18967. BUILDIN_DEF(mercenary_create,"ii"),
  18968. BUILDIN_DEF(mercenary_heal,"ii"),
  18969. BUILDIN_DEF(mercenary_sc_start,"iii"),
  18970. BUILDIN_DEF(mercenary_get_calls,"i"),
  18971. BUILDIN_DEF(mercenary_get_faith,"i"),
  18972. BUILDIN_DEF(mercenary_set_calls,"ii"),
  18973. BUILDIN_DEF(mercenary_set_faith,"ii"),
  18974. BUILDIN_DEF(readbook,"ii"),
  18975. BUILDIN_DEF(setfont,"i"),
  18976. BUILDIN_DEF(areamobuseskill,"siiiiviiiii"),
  18977. BUILDIN_DEF(progressbar,"si"),
  18978. BUILDIN_DEF(pushpc,"ii"),
  18979. BUILDIN_DEF(buyingstore,"i"),
  18980. BUILDIN_DEF(searchstores,"ii"),
  18981. BUILDIN_DEF(showdigit,"i?"),
  18982. // WoE SE
  18983. BUILDIN_DEF(agitstart2,""),
  18984. BUILDIN_DEF(agitend2,""),
  18985. BUILDIN_DEF(agitcheck2,""),
  18986. // BattleGround
  18987. BUILDIN_DEF(waitingroom2bg,"siiss?"),
  18988. BUILDIN_DEF(waitingroom2bg_single,"isiis"),
  18989. BUILDIN_DEF(bg_team_setxy,"iii"),
  18990. BUILDIN_DEF(bg_warp,"isii"),
  18991. BUILDIN_DEF(bg_monster,"isiisi?"),
  18992. BUILDIN_DEF(bg_monster_set_team,"ii"),
  18993. BUILDIN_DEF(bg_leave,"?"),
  18994. BUILDIN_DEF(bg_destroy,"i"),
  18995. BUILDIN_DEF(areapercentheal,"siiiiii"),
  18996. BUILDIN_DEF(bg_get_data,"ii"),
  18997. BUILDIN_DEF(bg_getareausers,"isiiii"),
  18998. BUILDIN_DEF(bg_updatescore,"sii"),
  18999. // Instancing
  19000. BUILDIN_DEF(instance_create,"s"),
  19001. BUILDIN_DEF(instance_destroy,"?"),
  19002. BUILDIN_DEF(instance_id,""),
  19003. BUILDIN_DEF(instance_enter,"s???"),
  19004. BUILDIN_DEF(instance_npcname,"s?"),
  19005. BUILDIN_DEF(instance_mapname,"s?"),
  19006. BUILDIN_DEF(instance_warpall,"sii?"),
  19007. BUILDIN_DEF(instance_announce,"isi?????"),
  19008. BUILDIN_DEF(instance_check_party,"i???"),
  19009. /**
  19010. * 3rd-related
  19011. **/
  19012. BUILDIN_DEF(makerune,"i?"),
  19013. BUILDIN_DEF(checkdragon,"?"),//[Ind]
  19014. BUILDIN_DEF(setdragon,"??"),//[Ind]
  19015. BUILDIN_DEF(ismounting,"?"),//[Ind]
  19016. BUILDIN_DEF(setmounting,"?"),//[Ind]
  19017. BUILDIN_DEF(checkre,"i"),
  19018. /**
  19019. * rAthena and beyond!
  19020. **/
  19021. BUILDIN_DEF(getargcount,""),
  19022. BUILDIN_DEF(getcharip,"?"),
  19023. BUILDIN_DEF(is_function,"s"),
  19024. BUILDIN_DEF(get_revision,""),
  19025. BUILDIN_DEF(get_githash,""),
  19026. BUILDIN_DEF(freeloop,"?"),
  19027. BUILDIN_DEF(getrandgroupitem,"i??"),
  19028. BUILDIN_DEF(cleanmap,"s"),
  19029. BUILDIN_DEF2(cleanmap,"cleanarea","siiii"),
  19030. BUILDIN_DEF(npcskill,"viii"),
  19031. BUILDIN_DEF(consumeitem,"v?"),
  19032. BUILDIN_DEF(delequip,"i?"),
  19033. BUILDIN_DEF(breakequip,"i?"),
  19034. BUILDIN_DEF(sit,"?"),
  19035. BUILDIN_DEF(stand,"?"),
  19036. //@commands (script based)
  19037. BUILDIN_DEF(bindatcmd, "ss??"),
  19038. BUILDIN_DEF(unbindatcmd, "s"),
  19039. BUILDIN_DEF(useatcmd, "s"),
  19040. //Quest Log System [Inkfish]
  19041. BUILDIN_DEF(questinfo, "ii??"),
  19042. BUILDIN_DEF(setquest, "i?"),
  19043. BUILDIN_DEF(erasequest, "i?"),
  19044. BUILDIN_DEF(completequest, "i?"),
  19045. BUILDIN_DEF(checkquest, "i??"),
  19046. BUILDIN_DEF(isbegin_quest,"i?"),
  19047. BUILDIN_DEF(changequest, "ii?"),
  19048. BUILDIN_DEF(showevent, "i??"),
  19049. //Bound items [Xantara] & [Akinari]
  19050. BUILDIN_DEF2(getitem,"getitembound","vii?"),
  19051. BUILDIN_DEF2(getitem2,"getitembound2","viiiiiiiii?"),
  19052. BUILDIN_DEF(countbound, "??"),
  19053. // Party related
  19054. BUILDIN_DEF(party_create,"s???"),
  19055. BUILDIN_DEF(party_addmember,"ii"),
  19056. BUILDIN_DEF(party_delmember,"??"),
  19057. BUILDIN_DEF(party_changeleader,"ii"),
  19058. BUILDIN_DEF(party_changeoption,"iii"),
  19059. BUILDIN_DEF(party_destroy,"i"),
  19060. BUILDIN_DEF(is_clientver,"ii?"),
  19061. BUILDIN_DEF2(montransform, "transform", "vi?????"), // Monster Transform [malufett/Hercules]
  19062. BUILDIN_DEF(vip_status,"i?"),
  19063. BUILDIN_DEF(vip_time,"i?"),
  19064. BUILDIN_DEF(bonus_script,"si????"),
  19065. BUILDIN_DEF(bonus_script_clear,"??"),
  19066. BUILDIN_DEF(getgroupitem,"i?"),
  19067. BUILDIN_DEF(enable_command,""),
  19068. BUILDIN_DEF(disable_command,""),
  19069. BUILDIN_DEF(getguildmember,"i??"),
  19070. BUILDIN_DEF(addspiritball,"ii?"),
  19071. BUILDIN_DEF(delspiritball,"i?"),
  19072. BUILDIN_DEF(countspiritball,"?"),
  19073. BUILDIN_DEF(mergeitem,"?"),
  19074. BUILDIN_DEF(mergeitem2,"??"),
  19075. BUILDIN_DEF(npcshopupdate,"sii?"),
  19076. BUILDIN_DEF(getattachedrid,""),
  19077. BUILDIN_DEF(getvar,"vi"),
  19078. BUILDIN_DEF(showscript,"s?"),
  19079. BUILDIN_DEF(ignoretimeout,"i?"),
  19080. BUILDIN_DEF(geteleminfo,"i?"),
  19081. BUILDIN_DEF(setquestinfo_level,"iii"),
  19082. BUILDIN_DEF(setquestinfo_req,"iii*"),
  19083. BUILDIN_DEF(setquestinfo_job,"ii*"),
  19084. BUILDIN_DEF(opendressroom,"i?"),
  19085. BUILDIN_DEF(navigateto,"s???????"),
  19086. BUILDIN_DEF(adopt,"vv"),
  19087. BUILDIN_DEF(getexp2,"ii?"),
  19088. #include "../custom/script_def.inc"
  19089. {NULL,NULL,NULL},
  19090. };