script.c 581 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353133541335513356133571335813359133601336113362133631336413365133661336713368133691337013371133721337313374133751337613377133781337913380133811338213383133841338513386133871338813389133901339113392133931339413395133961339713398133991340013401134021340313404134051340613407134081340913410134111341213413134141341513416134171341813419134201342113422134231342413425134261342713428134291343013431134321343313434134351343613437134381343913440134411344213443134441344513446134471344813449134501345113452134531345413455134561345713458134591346013461134621346313464134651346613467134681346913470134711347213473134741347513476134771347813479134801348113482134831348413485134861348713488134891349013491134921349313494134951349613497134981349913500135011350213503135041350513506135071350813509135101351113512135131351413515135161351713518135191352013521135221352313524135251352613527135281352913530135311353213533135341353513536135371353813539135401354113542135431354413545135461354713548135491355013551135521355313554135551355613557135581355913560135611356213563135641356513566135671356813569135701357113572135731357413575135761357713578135791358013581135821358313584135851358613587135881358913590135911359213593135941359513596135971359813599136001360113602136031360413605136061360713608136091361013611136121361313614136151361613617136181361913620136211362213623136241362513626136271362813629136301363113632136331363413635136361363713638136391364013641136421364313644136451364613647136481364913650136511365213653136541365513656136571365813659136601366113662136631366413665136661366713668136691367013671136721367313674136751367613677136781367913680136811368213683136841368513686136871368813689136901369113692136931369413695136961369713698136991370013701137021370313704137051370613707137081370913710137111371213713137141371513716137171371813719137201372113722137231372413725137261372713728137291373013731137321373313734137351373613737137381373913740137411374213743137441374513746137471374813749137501375113752137531375413755137561375713758137591376013761137621376313764137651376613767137681376913770137711377213773137741377513776137771377813779137801378113782137831378413785137861378713788137891379013791137921379313794137951379613797137981379913800138011380213803138041380513806138071380813809138101381113812138131381413815138161381713818138191382013821138221382313824138251382613827138281382913830138311383213833138341383513836138371383813839138401384113842138431384413845138461384713848138491385013851138521385313854138551385613857138581385913860138611386213863138641386513866138671386813869138701387113872138731387413875138761387713878138791388013881138821388313884138851388613887138881388913890138911389213893138941389513896138971389813899139001390113902139031390413905139061390713908139091391013911139121391313914139151391613917139181391913920139211392213923139241392513926139271392813929139301393113932139331393413935139361393713938139391394013941139421394313944139451394613947139481394913950139511395213953139541395513956139571395813959139601396113962139631396413965139661396713968139691397013971139721397313974139751397613977139781397913980139811398213983139841398513986139871398813989139901399113992139931399413995139961399713998139991400014001140021400314004140051400614007140081400914010140111401214013140141401514016140171401814019140201402114022140231402414025140261402714028140291403014031140321403314034140351403614037140381403914040140411404214043140441404514046140471404814049140501405114052140531405414055140561405714058140591406014061140621406314064140651406614067140681406914070140711407214073140741407514076140771407814079140801408114082140831408414085140861408714088140891409014091140921409314094140951409614097140981409914100141011410214103141041410514106141071410814109141101411114112141131411414115141161411714118141191412014121141221412314124141251412614127141281412914130141311413214133141341413514136141371413814139141401414114142141431414414145141461414714148141491415014151141521415314154141551415614157141581415914160141611416214163141641416514166141671416814169141701417114172141731417414175141761417714178141791418014181141821418314184141851418614187141881418914190141911419214193141941419514196141971419814199142001420114202142031420414205142061420714208142091421014211142121421314214142151421614217142181421914220142211422214223142241422514226142271422814229142301423114232142331423414235142361423714238142391424014241142421424314244142451424614247142481424914250142511425214253142541425514256142571425814259142601426114262142631426414265142661426714268142691427014271142721427314274142751427614277142781427914280142811428214283142841428514286142871428814289142901429114292142931429414295142961429714298142991430014301143021430314304143051430614307143081430914310143111431214313143141431514316143171431814319143201432114322143231432414325143261432714328143291433014331143321433314334143351433614337143381433914340143411434214343143441434514346143471434814349143501435114352143531435414355143561435714358143591436014361143621436314364143651436614367143681436914370143711437214373143741437514376143771437814379143801438114382143831438414385143861438714388143891439014391143921439314394143951439614397143981439914400144011440214403144041440514406144071440814409144101441114412144131441414415144161441714418144191442014421144221442314424144251442614427144281442914430144311443214433144341443514436144371443814439144401444114442144431444414445144461444714448144491445014451144521445314454144551445614457144581445914460144611446214463144641446514466144671446814469144701447114472144731447414475144761447714478144791448014481144821448314484144851448614487144881448914490144911449214493144941449514496144971449814499145001450114502145031450414505145061450714508145091451014511145121451314514145151451614517145181451914520145211452214523145241452514526145271452814529145301453114532145331453414535145361453714538145391454014541145421454314544145451454614547145481454914550145511455214553145541455514556145571455814559145601456114562145631456414565145661456714568145691457014571145721457314574145751457614577145781457914580145811458214583145841458514586145871458814589145901459114592145931459414595145961459714598145991460014601146021460314604146051460614607146081460914610146111461214613146141461514616146171461814619146201462114622146231462414625146261462714628146291463014631146321463314634146351463614637146381463914640146411464214643146441464514646146471464814649146501465114652146531465414655146561465714658146591466014661146621466314664146651466614667146681466914670146711467214673146741467514676146771467814679146801468114682146831468414685146861468714688146891469014691146921469314694146951469614697146981469914700147011470214703147041470514706147071470814709147101471114712147131471414715147161471714718147191472014721147221472314724147251472614727147281472914730147311473214733147341473514736147371473814739147401474114742147431474414745147461474714748147491475014751147521475314754147551475614757147581475914760147611476214763147641476514766147671476814769147701477114772147731477414775147761477714778147791478014781147821478314784147851478614787147881478914790147911479214793147941479514796147971479814799148001480114802148031480414805148061480714808148091481014811148121481314814148151481614817148181481914820148211482214823148241482514826148271482814829148301483114832148331483414835148361483714838148391484014841148421484314844148451484614847148481484914850148511485214853148541485514856148571485814859148601486114862148631486414865148661486714868148691487014871148721487314874148751487614877148781487914880148811488214883148841488514886148871488814889148901489114892148931489414895148961489714898148991490014901149021490314904149051490614907149081490914910149111491214913149141491514916149171491814919149201492114922149231492414925149261492714928149291493014931149321493314934149351493614937149381493914940149411494214943149441494514946149471494814949149501495114952149531495414955149561495714958149591496014961149621496314964149651496614967149681496914970149711497214973149741497514976149771497814979149801498114982149831498414985149861498714988149891499014991149921499314994149951499614997149981499915000150011500215003150041500515006150071500815009150101501115012150131501415015150161501715018150191502015021150221502315024150251502615027150281502915030150311503215033150341503515036150371503815039150401504115042150431504415045150461504715048150491505015051150521505315054150551505615057150581505915060150611506215063150641506515066150671506815069150701507115072150731507415075150761507715078150791508015081150821508315084150851508615087150881508915090150911509215093150941509515096150971509815099151001510115102151031510415105151061510715108151091511015111151121511315114151151511615117151181511915120151211512215123151241512515126151271512815129151301513115132151331513415135151361513715138151391514015141151421514315144151451514615147151481514915150151511515215153151541515515156151571515815159151601516115162151631516415165151661516715168151691517015171151721517315174151751517615177151781517915180151811518215183151841518515186151871518815189151901519115192151931519415195151961519715198151991520015201152021520315204152051520615207152081520915210152111521215213152141521515216152171521815219152201522115222152231522415225152261522715228152291523015231152321523315234152351523615237152381523915240152411524215243152441524515246152471524815249152501525115252152531525415255152561525715258152591526015261152621526315264152651526615267152681526915270152711527215273152741527515276152771527815279152801528115282152831528415285152861528715288152891529015291152921529315294152951529615297152981529915300153011530215303153041530515306153071530815309153101531115312153131531415315153161531715318153191532015321153221532315324153251532615327153281532915330153311533215333153341533515336153371533815339153401534115342153431534415345153461534715348153491535015351153521535315354153551535615357153581535915360153611536215363153641536515366153671536815369153701537115372153731537415375153761537715378153791538015381153821538315384153851538615387153881538915390153911539215393153941539515396153971539815399154001540115402154031540415405154061540715408154091541015411154121541315414154151541615417154181541915420154211542215423154241542515426154271542815429154301543115432154331543415435154361543715438154391544015441154421544315444154451544615447154481544915450154511545215453154541545515456154571545815459154601546115462154631546415465154661546715468154691547015471154721547315474154751547615477154781547915480154811548215483154841548515486154871548815489154901549115492154931549415495154961549715498154991550015501155021550315504155051550615507155081550915510155111551215513155141551515516155171551815519155201552115522155231552415525155261552715528155291553015531155321553315534155351553615537155381553915540155411554215543155441554515546155471554815549155501555115552155531555415555155561555715558155591556015561155621556315564155651556615567155681556915570155711557215573155741557515576155771557815579155801558115582155831558415585155861558715588155891559015591155921559315594155951559615597155981559915600156011560215603156041560515606156071560815609156101561115612156131561415615156161561715618156191562015621156221562315624156251562615627156281562915630156311563215633156341563515636156371563815639156401564115642156431564415645156461564715648156491565015651156521565315654156551565615657156581565915660156611566215663156641566515666156671566815669156701567115672156731567415675156761567715678156791568015681156821568315684156851568615687156881568915690156911569215693156941569515696156971569815699157001570115702157031570415705157061570715708157091571015711157121571315714157151571615717157181571915720157211572215723157241572515726157271572815729157301573115732157331573415735157361573715738157391574015741157421574315744157451574615747157481574915750157511575215753157541575515756157571575815759157601576115762157631576415765157661576715768157691577015771157721577315774157751577615777157781577915780157811578215783157841578515786157871578815789157901579115792157931579415795157961579715798157991580015801158021580315804158051580615807158081580915810158111581215813158141581515816158171581815819158201582115822158231582415825158261582715828158291583015831158321583315834158351583615837158381583915840158411584215843158441584515846158471584815849158501585115852158531585415855158561585715858158591586015861158621586315864158651586615867158681586915870158711587215873158741587515876158771587815879158801588115882158831588415885158861588715888158891589015891158921589315894158951589615897158981589915900159011590215903159041590515906159071590815909159101591115912159131591415915159161591715918159191592015921159221592315924159251592615927159281592915930159311593215933159341593515936159371593815939159401594115942159431594415945159461594715948159491595015951159521595315954159551595615957159581595915960159611596215963159641596515966159671596815969159701597115972159731597415975159761597715978159791598015981159821598315984159851598615987159881598915990159911599215993159941599515996159971599815999160001600116002160031600416005160061600716008160091601016011160121601316014160151601616017160181601916020160211602216023160241602516026160271602816029160301603116032160331603416035160361603716038160391604016041160421604316044160451604616047160481604916050160511605216053160541605516056160571605816059160601606116062160631606416065160661606716068160691607016071160721607316074160751607616077160781607916080160811608216083160841608516086160871608816089160901609116092160931609416095160961609716098160991610016101161021610316104161051610616107161081610916110161111611216113161141611516116161171611816119161201612116122161231612416125161261612716128161291613016131161321613316134161351613616137161381613916140161411614216143161441614516146161471614816149161501615116152161531615416155161561615716158161591616016161161621616316164161651616616167161681616916170161711617216173161741617516176161771617816179161801618116182161831618416185161861618716188161891619016191161921619316194161951619616197161981619916200162011620216203162041620516206162071620816209162101621116212162131621416215162161621716218162191622016221162221622316224162251622616227162281622916230162311623216233162341623516236162371623816239162401624116242162431624416245162461624716248162491625016251162521625316254162551625616257162581625916260162611626216263162641626516266162671626816269162701627116272162731627416275162761627716278162791628016281162821628316284162851628616287162881628916290162911629216293162941629516296162971629816299163001630116302163031630416305163061630716308163091631016311163121631316314163151631616317163181631916320163211632216323163241632516326163271632816329163301633116332163331633416335163361633716338163391634016341163421634316344163451634616347163481634916350163511635216353163541635516356163571635816359163601636116362163631636416365163661636716368163691637016371163721637316374163751637616377163781637916380163811638216383163841638516386163871638816389163901639116392163931639416395163961639716398163991640016401164021640316404164051640616407164081640916410164111641216413164141641516416164171641816419164201642116422164231642416425164261642716428164291643016431164321643316434164351643616437164381643916440164411644216443164441644516446164471644816449164501645116452164531645416455164561645716458164591646016461164621646316464164651646616467164681646916470164711647216473164741647516476164771647816479164801648116482164831648416485164861648716488164891649016491164921649316494164951649616497164981649916500165011650216503165041650516506165071650816509165101651116512165131651416515165161651716518165191652016521165221652316524165251652616527165281652916530165311653216533165341653516536165371653816539165401654116542165431654416545165461654716548165491655016551165521655316554165551655616557165581655916560165611656216563165641656516566165671656816569165701657116572165731657416575165761657716578165791658016581165821658316584165851658616587165881658916590165911659216593165941659516596165971659816599166001660116602166031660416605166061660716608166091661016611166121661316614166151661616617166181661916620166211662216623166241662516626166271662816629166301663116632166331663416635166361663716638166391664016641166421664316644166451664616647166481664916650166511665216653166541665516656166571665816659166601666116662166631666416665166661666716668166691667016671166721667316674166751667616677166781667916680166811668216683166841668516686166871668816689166901669116692166931669416695166961669716698166991670016701167021670316704167051670616707167081670916710167111671216713167141671516716167171671816719167201672116722167231672416725167261672716728167291673016731167321673316734167351673616737167381673916740167411674216743167441674516746167471674816749167501675116752167531675416755167561675716758167591676016761167621676316764167651676616767167681676916770167711677216773167741677516776167771677816779167801678116782167831678416785167861678716788167891679016791167921679316794167951679616797167981679916800168011680216803168041680516806168071680816809168101681116812168131681416815168161681716818168191682016821168221682316824168251682616827168281682916830168311683216833168341683516836168371683816839168401684116842168431684416845168461684716848168491685016851168521685316854168551685616857168581685916860168611686216863168641686516866168671686816869168701687116872168731687416875168761687716878168791688016881168821688316884168851688616887168881688916890168911689216893168941689516896168971689816899169001690116902169031690416905169061690716908169091691016911169121691316914169151691616917169181691916920169211692216923169241692516926169271692816929169301693116932169331693416935169361693716938169391694016941169421694316944169451694616947169481694916950169511695216953169541695516956169571695816959169601696116962169631696416965169661696716968169691697016971169721697316974169751697616977169781697916980169811698216983169841698516986169871698816989169901699116992169931699416995169961699716998169991700017001170021700317004170051700617007170081700917010170111701217013170141701517016170171701817019170201702117022170231702417025170261702717028170291703017031170321703317034170351703617037170381703917040170411704217043170441704517046170471704817049170501705117052170531705417055170561705717058170591706017061170621706317064170651706617067170681706917070170711707217073170741707517076170771707817079170801708117082170831708417085170861708717088170891709017091170921709317094170951709617097170981709917100171011710217103171041710517106171071710817109171101711117112171131711417115171161711717118171191712017121171221712317124171251712617127171281712917130171311713217133171341713517136171371713817139171401714117142171431714417145171461714717148171491715017151171521715317154171551715617157171581715917160171611716217163171641716517166171671716817169171701717117172171731717417175171761717717178171791718017181171821718317184171851718617187171881718917190171911719217193171941719517196171971719817199172001720117202172031720417205172061720717208172091721017211172121721317214172151721617217172181721917220172211722217223172241722517226172271722817229172301723117232172331723417235172361723717238172391724017241172421724317244172451724617247172481724917250172511725217253172541725517256172571725817259172601726117262172631726417265172661726717268172691727017271172721727317274172751727617277172781727917280172811728217283172841728517286172871728817289172901729117292172931729417295172961729717298172991730017301173021730317304173051730617307173081730917310173111731217313173141731517316173171731817319173201732117322173231732417325173261732717328173291733017331173321733317334173351733617337173381733917340173411734217343173441734517346173471734817349173501735117352173531735417355173561735717358173591736017361173621736317364173651736617367173681736917370173711737217373173741737517376173771737817379173801738117382173831738417385173861738717388173891739017391173921739317394173951739617397173981739917400174011740217403174041740517406174071740817409174101741117412174131741417415174161741717418174191742017421174221742317424174251742617427174281742917430174311743217433174341743517436174371743817439174401744117442174431744417445174461744717448174491745017451174521745317454174551745617457174581745917460174611746217463174641746517466174671746817469174701747117472174731747417475174761747717478174791748017481174821748317484174851748617487174881748917490174911749217493174941749517496174971749817499175001750117502175031750417505175061750717508175091751017511175121751317514175151751617517175181751917520175211752217523175241752517526175271752817529175301753117532175331753417535175361753717538175391754017541175421754317544175451754617547175481754917550175511755217553175541755517556175571755817559175601756117562175631756417565175661756717568175691757017571175721757317574175751757617577175781757917580175811758217583175841758517586175871758817589175901759117592175931759417595175961759717598175991760017601176021760317604176051760617607176081760917610176111761217613176141761517616176171761817619176201762117622176231762417625176261762717628176291763017631176321763317634176351763617637176381763917640176411764217643176441764517646176471764817649176501765117652176531765417655176561765717658176591766017661176621766317664176651766617667176681766917670176711767217673176741767517676176771767817679176801768117682176831768417685176861768717688176891769017691176921769317694176951769617697176981769917700177011770217703177041770517706177071770817709177101771117712177131771417715177161771717718177191772017721177221772317724177251772617727177281772917730177311773217733177341773517736177371773817739177401774117742177431774417745177461774717748177491775017751177521775317754177551775617757177581775917760177611776217763177641776517766177671776817769177701777117772177731777417775177761777717778177791778017781177821778317784177851778617787177881778917790177911779217793177941779517796177971779817799178001780117802178031780417805178061780717808178091781017811178121781317814178151781617817178181781917820178211782217823178241782517826178271782817829178301783117832178331783417835178361783717838178391784017841178421784317844178451784617847178481784917850178511785217853178541785517856178571785817859178601786117862178631786417865178661786717868178691787017871178721787317874178751787617877178781787917880178811788217883178841788517886178871788817889178901789117892178931789417895178961789717898178991790017901179021790317904179051790617907179081790917910179111791217913179141791517916179171791817919179201792117922179231792417925179261792717928179291793017931179321793317934179351793617937179381793917940179411794217943179441794517946179471794817949179501795117952179531795417955179561795717958179591796017961179621796317964179651796617967179681796917970179711797217973179741797517976179771797817979179801798117982179831798417985179861798717988179891799017991179921799317994179951799617997179981799918000180011800218003180041800518006180071800818009180101801118012180131801418015180161801718018180191802018021180221802318024180251802618027180281802918030180311803218033180341803518036180371803818039180401804118042180431804418045180461804718048180491805018051180521805318054180551805618057180581805918060180611806218063180641806518066180671806818069180701807118072180731807418075180761807718078180791808018081180821808318084180851808618087180881808918090180911809218093180941809518096180971809818099181001810118102181031810418105181061810718108181091811018111181121811318114181151811618117181181811918120181211812218123181241812518126181271812818129181301813118132181331813418135181361813718138181391814018141181421814318144181451814618147181481814918150181511815218153181541815518156181571815818159181601816118162181631816418165181661816718168181691817018171181721817318174181751817618177181781817918180181811818218183181841818518186181871818818189181901819118192181931819418195181961819718198181991820018201182021820318204182051820618207182081820918210182111821218213182141821518216182171821818219182201822118222182231822418225182261822718228182291823018231182321823318234182351823618237182381823918240182411824218243182441824518246182471824818249182501825118252182531825418255182561825718258182591826018261182621826318264182651826618267182681826918270182711827218273182741827518276182771827818279182801828118282182831828418285182861828718288182891829018291182921829318294182951829618297182981829918300183011830218303183041830518306183071830818309183101831118312183131831418315183161831718318183191832018321183221832318324183251832618327183281832918330183311833218333183341833518336183371833818339183401834118342183431834418345183461834718348183491835018351183521835318354183551835618357183581835918360183611836218363183641836518366183671836818369183701837118372183731837418375183761837718378183791838018381183821838318384183851838618387183881838918390183911839218393183941839518396183971839818399184001840118402184031840418405184061840718408184091841018411184121841318414184151841618417184181841918420184211842218423184241842518426184271842818429184301843118432184331843418435184361843718438184391844018441184421844318444184451844618447184481844918450184511845218453184541845518456184571845818459184601846118462184631846418465184661846718468184691847018471184721847318474184751847618477184781847918480184811848218483184841848518486184871848818489184901849118492184931849418495184961849718498184991850018501185021850318504185051850618507185081850918510185111851218513185141851518516185171851818519185201852118522185231852418525185261852718528185291853018531185321853318534185351853618537185381853918540185411854218543185441854518546185471854818549185501855118552185531855418555185561855718558185591856018561185621856318564185651856618567185681856918570185711857218573185741857518576185771857818579185801858118582185831858418585185861858718588185891859018591185921859318594185951859618597185981859918600186011860218603186041860518606186071860818609186101861118612186131861418615186161861718618186191862018621186221862318624186251862618627186281862918630186311863218633186341863518636186371863818639186401864118642186431864418645186461864718648186491865018651186521865318654186551865618657186581865918660186611866218663186641866518666186671866818669186701867118672186731867418675186761867718678186791868018681186821868318684186851868618687186881868918690186911869218693186941869518696186971869818699187001870118702187031870418705187061870718708187091871018711187121871318714187151871618717187181871918720187211872218723187241872518726187271872818729187301873118732187331873418735187361873718738187391874018741187421874318744187451874618747187481874918750187511875218753187541875518756187571875818759187601876118762187631876418765187661876718768187691877018771187721877318774187751877618777187781877918780187811878218783187841878518786187871878818789187901879118792187931879418795187961879718798187991880018801188021880318804188051880618807188081880918810188111881218813188141881518816188171881818819188201882118822188231882418825188261882718828188291883018831188321883318834188351883618837188381883918840188411884218843188441884518846188471884818849188501885118852188531885418855188561885718858188591886018861188621886318864188651886618867188681886918870188711887218873188741887518876188771887818879188801888118882188831888418885188861888718888188891889018891188921889318894188951889618897188981889918900189011890218903189041890518906189071890818909189101891118912189131891418915189161891718918189191892018921189221892318924189251892618927189281892918930189311893218933189341893518936189371893818939189401894118942189431894418945189461894718948189491895018951189521895318954189551895618957189581895918960189611896218963189641896518966189671896818969189701897118972189731897418975189761897718978189791898018981189821898318984189851898618987189881898918990189911899218993189941899518996189971899818999190001900119002190031900419005190061900719008190091901019011190121901319014190151901619017190181901919020190211902219023190241902519026190271902819029190301903119032190331903419035190361903719038190391904019041190421904319044190451904619047190481904919050190511905219053190541905519056190571905819059190601906119062190631906419065190661906719068190691907019071190721907319074190751907619077190781907919080190811908219083190841908519086190871908819089190901909119092190931909419095190961909719098190991910019101191021910319104191051910619107191081910919110191111911219113191141911519116191171911819119191201912119122191231912419125191261912719128191291913019131191321913319134191351913619137191381913919140191411914219143191441914519146191471914819149191501915119152191531915419155191561915719158191591916019161191621916319164191651916619167191681916919170191711917219173191741917519176191771917819179191801918119182191831918419185191861918719188191891919019191191921919319194191951919619197191981919919200192011920219203192041920519206192071920819209192101921119212192131921419215192161921719218192191922019221192221922319224192251922619227192281922919230192311923219233192341923519236192371923819239192401924119242192431924419245192461924719248192491925019251192521925319254192551925619257192581925919260192611926219263192641926519266192671926819269192701927119272192731927419275192761927719278192791928019281192821928319284192851928619287192881928919290192911929219293192941929519296192971929819299193001930119302193031930419305193061930719308193091931019311193121931319314193151931619317193181931919320193211932219323193241932519326193271932819329193301933119332193331933419335193361933719338193391934019341193421934319344193451934619347193481934919350193511935219353193541935519356193571935819359193601936119362193631936419365193661936719368193691937019371193721937319374193751937619377193781937919380193811938219383193841938519386193871938819389193901939119392193931939419395193961939719398193991940019401194021940319404194051940619407194081940919410194111941219413194141941519416194171941819419194201942119422194231942419425194261942719428194291943019431194321943319434194351943619437194381943919440194411944219443194441944519446194471944819449194501945119452194531945419455194561945719458194591946019461194621946319464194651946619467194681946919470194711947219473194741947519476194771947819479194801948119482194831948419485194861948719488194891949019491194921949319494194951949619497194981949919500195011950219503195041950519506195071950819509195101951119512195131951419515195161951719518195191952019521195221952319524195251952619527195281952919530195311953219533195341953519536195371953819539195401954119542195431954419545195461954719548195491955019551195521955319554195551955619557195581955919560195611956219563195641956519566195671956819569195701957119572195731957419575195761957719578195791958019581195821958319584195851958619587195881958919590195911959219593195941959519596195971959819599196001960119602196031960419605196061960719608196091961019611196121961319614196151961619617196181961919620196211962219623196241962519626196271962819629196301963119632196331963419635196361963719638196391964019641196421964319644196451964619647196481964919650196511965219653196541965519656196571965819659196601966119662196631966419665196661966719668196691967019671196721967319674196751967619677196781967919680196811968219683196841968519686196871968819689196901969119692196931969419695196961969719698196991970019701197021970319704197051970619707197081970919710197111971219713197141971519716197171971819719197201972119722197231972419725197261972719728197291973019731197321973319734197351973619737197381973919740197411974219743197441974519746197471974819749197501975119752197531975419755197561975719758197591976019761197621976319764197651976619767197681976919770197711977219773197741977519776197771977819779197801978119782197831978419785197861978719788197891979019791197921979319794197951979619797197981979919800198011980219803198041980519806198071980819809198101981119812198131981419815198161981719818198191982019821198221982319824198251982619827198281982919830198311983219833198341983519836198371983819839198401984119842198431984419845198461984719848198491985019851198521985319854198551985619857198581985919860198611986219863198641986519866198671986819869198701987119872198731987419875198761987719878198791988019881198821988319884198851988619887198881988919890198911989219893198941989519896198971989819899199001990119902199031990419905199061990719908199091991019911199121991319914199151991619917199181991919920199211992219923199241992519926199271992819929199301993119932199331993419935199361993719938199391994019941199421994319944199451994619947199481994919950199511995219953199541995519956199571995819959199601996119962199631996419965199661996719968199691997019971199721997319974199751997619977199781997919980199811998219983199841998519986199871998819989199901999119992199931999419995199961999719998199992000020001200022000320004200052000620007200082000920010200112001220013200142001520016200172001820019200202002120022200232002420025200262002720028200292003020031200322003320034200352003620037200382003920040200412004220043200442004520046200472004820049200502005120052200532005420055200562005720058200592006020061200622006320064200652006620067200682006920070200712007220073200742007520076200772007820079200802008120082200832008420085200862008720088200892009020091200922009320094200952009620097200982009920100201012010220103201042010520106201072010820109201102011120112201132011420115201162011720118201192012020121201222012320124201252012620127201282012920130201312013220133201342013520136201372013820139201402014120142201432014420145201462014720148201492015020151201522015320154201552015620157201582015920160201612016220163201642016520166201672016820169201702017120172201732017420175201762017720178201792018020181201822018320184201852018620187201882018920190201912019220193201942019520196201972019820199202002020120202202032020420205202062020720208202092021020211202122021320214202152021620217202182021920220202212022220223202242022520226202272022820229202302023120232202332023420235202362023720238202392024020241202422024320244202452024620247202482024920250202512025220253202542025520256202572025820259202602026120262202632026420265202662026720268202692027020271202722027320274202752027620277202782027920280202812028220283202842028520286202872028820289202902029120292202932029420295202962029720298202992030020301203022030320304203052030620307203082030920310203112031220313203142031520316203172031820319203202032120322203232032420325203262032720328203292033020331203322033320334203352033620337203382033920340203412034220343203442034520346203472034820349203502035120352203532035420355203562035720358203592036020361203622036320364203652036620367203682036920370203712037220373203742037520376203772037820379203802038120382203832038420385203862038720388203892039020391203922039320394203952039620397203982039920400204012040220403204042040520406204072040820409204102041120412204132041420415204162041720418204192042020421204222042320424204252042620427204282042920430204312043220433204342043520436204372043820439204402044120442204432044420445204462044720448204492045020451204522045320454204552045620457204582045920460204612046220463204642046520466204672046820469204702047120472204732047420475204762047720478204792048020481204822048320484204852048620487204882048920490204912049220493204942049520496204972049820499205002050120502205032050420505205062050720508205092051020511205122051320514205152051620517205182051920520205212052220523205242052520526205272052820529205302053120532205332053420535205362053720538205392054020541205422054320544205452054620547205482054920550205512055220553205542055520556205572055820559205602056120562205632056420565205662056720568205692057020571205722057320574205752057620577205782057920580205812058220583205842058520586205872058820589205902059120592205932059420595205962059720598205992060020601206022060320604206052060620607206082060920610206112061220613206142061520616206172061820619206202062120622206232062420625206262062720628206292063020631206322063320634206352063620637206382063920640206412064220643206442064520646206472064820649206502065120652206532065420655206562065720658206592066020661206622066320664206652066620667206682066920670206712067220673206742067520676206772067820679206802068120682206832068420685206862068720688206892069020691206922069320694206952069620697206982069920700207012070220703207042070520706207072070820709207102071120712207132071420715207162071720718207192072020721207222072320724207252072620727207282072920730207312073220733207342073520736207372073820739207402074120742207432074420745207462074720748207492075020751207522075320754207552075620757207582075920760207612076220763207642076520766207672076820769207702077120772207732077420775207762077720778207792078020781207822078320784207852078620787207882078920790207912079220793207942079520796207972079820799208002080120802208032080420805208062080720808208092081020811208122081320814208152081620817208182081920820208212082220823208242082520826208272082820829208302083120832208332083420835208362083720838208392084020841208422084320844208452084620847208482084920850208512085220853208542085520856208572085820859208602086120862208632086420865208662086720868208692087020871208722087320874208752087620877208782087920880208812088220883208842088520886208872088820889208902089120892208932089420895208962089720898208992090020901209022090320904209052090620907209082090920910209112091220913209142091520916209172091820919209202092120922209232092420925209262092720928209292093020931209322093320934209352093620937209382093920940209412094220943209442094520946209472094820949209502095120952209532095420955209562095720958209592096020961209622096320964209652096620967209682096920970209712097220973209742097520976209772097820979209802098120982209832098420985209862098720988209892099020991209922099320994209952099620997209982099921000210012100221003210042100521006210072100821009210102101121012210132101421015210162101721018210192102021021210222102321024210252102621027210282102921030210312103221033210342103521036210372103821039210402104121042210432104421045210462104721048210492105021051210522105321054210552105621057210582105921060210612106221063210642106521066210672106821069210702107121072210732107421075210762107721078210792108021081210822108321084210852108621087210882108921090210912109221093210942109521096210972109821099211002110121102211032110421105211062110721108211092111021111211122111321114211152111621117211182111921120211212112221123211242112521126211272112821129211302113121132211332113421135211362113721138211392114021141211422114321144211452114621147211482114921150211512115221153211542115521156211572115821159211602116121162211632116421165211662116721168211692117021171211722117321174211752117621177211782117921180211812118221183211842118521186211872118821189211902119121192211932119421195211962119721198211992120021201212022120321204212052120621207212082120921210212112121221213212142121521216212172121821219212202122121222212232122421225212262122721228212292123021231212322123321234212352123621237212382123921240212412124221243212442124521246212472124821249212502125121252212532125421255212562125721258212592126021261212622126321264212652126621267212682126921270212712127221273212742127521276212772127821279212802128121282212832128421285212862128721288212892129021291212922129321294212952129621297212982129921300213012130221303213042130521306213072130821309213102131121312213132131421315213162131721318213192132021321213222132321324213252132621327213282132921330213312133221333213342133521336213372133821339213402134121342213432134421345213462134721348213492135021351213522135321354213552135621357213582135921360213612136221363213642136521366213672136821369213702137121372213732137421375213762137721378213792138021381213822138321384213852138621387213882138921390213912139221393213942139521396213972139821399214002140121402214032140421405214062140721408214092141021411214122141321414214152141621417214182141921420214212142221423214242142521426214272142821429214302143121432214332143421435214362143721438214392144021441214422144321444214452144621447214482144921450214512145221453214542145521456214572145821459214602146121462214632146421465214662146721468214692147021471214722147321474214752147621477214782147921480214812148221483214842148521486214872148821489214902149121492214932149421495214962149721498214992150021501215022150321504215052150621507215082150921510215112151221513215142151521516215172151821519215202152121522215232152421525215262152721528215292153021531215322153321534215352153621537215382153921540215412154221543215442154521546215472154821549215502155121552215532155421555215562155721558215592156021561215622156321564215652156621567215682156921570215712157221573215742157521576215772157821579215802158121582215832158421585215862158721588215892159021591215922159321594215952159621597215982159921600216012160221603216042160521606216072160821609216102161121612216132161421615216162161721618216192162021621216222162321624216252162621627216282162921630216312163221633216342163521636216372163821639216402164121642216432164421645216462164721648216492165021651216522165321654216552165621657216582165921660216612166221663216642166521666216672166821669216702167121672216732167421675216762167721678216792168021681216822168321684216852168621687216882168921690216912169221693216942169521696216972169821699217002170121702217032170421705217062170721708217092171021711217122171321714217152171621717217182171921720217212172221723217242172521726217272172821729217302173121732217332173421735217362173721738217392174021741217422174321744217452174621747217482174921750217512175221753217542175521756217572175821759217602176121762217632176421765217662176721768217692177021771217722177321774217752177621777217782177921780217812178221783217842178521786217872178821789217902179121792217932179421795217962179721798217992180021801218022180321804218052180621807218082180921810218112181221813218142181521816218172181821819218202182121822218232182421825218262182721828218292183021831218322183321834218352183621837218382183921840218412184221843218442184521846
  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 "quest.h"
  45. #include "elemental.h"
  46. #include <math.h>
  47. #ifndef WIN32
  48. #endif
  49. #include <setjmp.h>
  50. #include <errno.h>
  51. TBL_PC *script_rid2sd(struct script_state *st);
  52. /**
  53. * Get `sd` from a account id in `loc` param instead of attached rid
  54. * @param st Script
  55. * @param loc Location to look account id in script parameter
  56. * @param sd Variable that will be assigned
  57. * @return True if `sd` is assigned, false otherwise
  58. **/
  59. static bool script_accid2sd_(struct script_state *st, uint8 loc, struct map_session_data **sd, const char *func) {
  60. if (script_hasdata(st, loc)) {
  61. int id_ = script_getnum(st, loc);
  62. if (!(*sd = map_id2sd(id_)))
  63. ShowError("%s: Player with account id '%d' is not found.\n", func, id_);
  64. }
  65. else
  66. *sd = script_rid2sd(st);
  67. return (*sd) ? true : false;
  68. }
  69. /**
  70. * Get `sd` from a char id in `loc` param instead of attached rid
  71. * @param st Script
  72. * @param loc Location to look char id in script parameter
  73. * @param sd Variable that will be assigned
  74. * @return True if `sd` is assigned, false otherwise
  75. **/
  76. static bool script_charid2sd_(struct script_state *st, uint8 loc, struct map_session_data **sd, const char *func) {
  77. if (script_hasdata(st, loc)) {
  78. int id_ = script_getnum(st, loc);
  79. if (!(*sd = map_charid2sd(id_)))
  80. ShowError("%s: Player with char id '%d' is not found.\n", func, id_);
  81. }
  82. else
  83. *sd = script_rid2sd(st);
  84. return (*sd) ? true : false;
  85. }
  86. /**
  87. * Get `sd` from a nick in `loc` param instead of attached rid
  88. * @param st Script
  89. * @param loc Location to look nick in script parameter
  90. * @param sd Variable that will be assigned
  91. * @return True if `sd` is assigned, false otherwise
  92. **/
  93. static bool script_nick2sd_(struct script_state *st, uint8 loc, struct map_session_data **sd, const char *func) {
  94. if (script_hasdata(st, loc)) {
  95. const char *name_ = script_getstr(st, loc);
  96. if (!(*sd = map_nick2sd(name_)))
  97. ShowError("%s: Player with nick '%s' is not found.\n", func, name_);
  98. }
  99. else
  100. *sd = script_rid2sd(st);
  101. return (*sd) ? true : false;
  102. }
  103. #define script_accid2sd(loc,sd) script_accid2sd_(st,(loc),&(sd),__FUNCTION__)
  104. #define script_charid2sd(loc,sd) script_charid2sd_(st,(loc),&(sd),__FUNCTION__)
  105. #define script_nick2sd(loc,sd) script_nick2sd_(st,(loc),&(sd),__FUNCTION__)
  106. /// temporary buffer for passing around compiled bytecode
  107. /// @see add_scriptb, set_label, parse_script
  108. static unsigned char* script_buf = NULL;
  109. static int script_pos = 0, script_size = 0;
  110. static inline int GETVALUE(const unsigned char* buf, int i)
  111. {
  112. return (int)MakeDWord(MakeWord(buf[i], buf[i+1]), MakeWord(buf[i+2], 0));
  113. }
  114. static inline void SETVALUE(unsigned char* buf, int i, int n)
  115. {
  116. buf[i] = GetByte(n, 0);
  117. buf[i+1] = GetByte(n, 1);
  118. buf[i+2] = GetByte(n, 2);
  119. }
  120. // String buffer structures.
  121. // str_data stores string information
  122. static struct str_data_struct {
  123. enum c_op type;
  124. int str;
  125. int backpatch;
  126. int label;
  127. int (*func)(struct script_state *st);
  128. int val;
  129. int next;
  130. } *str_data = NULL;
  131. static int str_data_size = 0; // size of the data
  132. static int str_num = LABEL_START; // next id to be assigned
  133. // str_buf holds the strings themselves
  134. static char *str_buf;
  135. static int str_size = 0; // size of the buffer
  136. static int str_pos = 0; // next position to be assigned
  137. // Using a prime number for SCRIPT_HASH_SIZE should give better distributions
  138. #define SCRIPT_HASH_SIZE 1021
  139. int str_hash[SCRIPT_HASH_SIZE];
  140. // Specifies which string hashing method to use
  141. //#define SCRIPT_HASH_DJB2
  142. //#define SCRIPT_HASH_SDBM
  143. #define SCRIPT_HASH_ELF
  144. static DBMap* scriptlabel_db = NULL; // const char* label_name -> int script_pos
  145. static DBMap* userfunc_db = NULL; // const char* func_name -> struct script_code*
  146. static int parse_options = 0;
  147. DBMap* script_get_label_db(void) { return scriptlabel_db; }
  148. DBMap* script_get_userfunc_db(void) { return userfunc_db; }
  149. // important buildin function references for usage in scripts
  150. static int buildin_set_ref = 0;
  151. static int buildin_callsub_ref = 0;
  152. static int buildin_callfunc_ref = 0;
  153. static int buildin_getelementofarray_ref = 0;
  154. // Caches compiled autoscript item code.
  155. // Note: This is not cleared when reloading itemdb.
  156. static DBMap* autobonus_db = NULL; // char* script -> char* bytecode
  157. struct Script_Config script_config = {
  158. 1, // warn_func_mismatch_argtypes
  159. 1, 65535, 2048, //warn_func_mismatch_paramnum/check_cmdcount/check_gotocount
  160. 0, INT_MAX, // input_min_value/input_max_value
  161. "OnPCDieEvent", //die_event_name
  162. "OnPCKillEvent", //kill_pc_event_name
  163. "OnNPCKillEvent", //kill_mob_event_name
  164. "OnPCLoginEvent", //login_event_name
  165. "OnPCLogoutEvent", //logout_event_name
  166. "OnPCLoadMapEvent", //loadmap_event_name
  167. "OnPCBaseLvUpEvent", //baselvup_event_name
  168. "OnPCJobLvUpEvent", //joblvup_event_name
  169. "OnPCStatCalcEvent", //stat_calc_event_name
  170. "OnTouch_", //ontouch_name (runs on first visible char to enter area, picks another char if the first char leaves)
  171. "OnTouch", //ontouch2_name (run whenever a char walks into the OnTouch area)
  172. };
  173. static jmp_buf error_jump;
  174. static char* error_msg;
  175. static const char* error_pos;
  176. static int error_report; // if the error should produce output
  177. // Used by disp_warning_message
  178. static const char* parser_current_src;
  179. static const char* parser_current_file;
  180. static int parser_current_line;
  181. // for advanced scripting support ( nested if, switch, while, for, do-while, function, etc )
  182. // [Eoe / jA 1080, 1081, 1094, 1164]
  183. enum curly_type {
  184. TYPE_NULL = 0,
  185. TYPE_IF,
  186. TYPE_SWITCH,
  187. TYPE_WHILE,
  188. TYPE_FOR,
  189. TYPE_DO,
  190. TYPE_USERFUNC,
  191. TYPE_ARGLIST // function argument list
  192. };
  193. enum e_arglist
  194. {
  195. ARGLIST_UNDEFINED = 0,
  196. ARGLIST_NO_PAREN = 1,
  197. ARGLIST_PAREN = 2,
  198. };
  199. static struct {
  200. struct {
  201. enum curly_type type;
  202. int index;
  203. int count;
  204. int flag;
  205. struct linkdb_node *case_label;
  206. } curly[256]; // Information right parenthesis
  207. int curly_count; // The number of right brackets
  208. int index; // Number of the syntax used in the script
  209. } syntax;
  210. const char* parse_curly_close(const char* p);
  211. const char* parse_syntax_close(const char* p);
  212. const char* parse_syntax_close_sub(const char* p,int* flag);
  213. const char* parse_syntax(const char* p);
  214. static int parse_syntax_for_flag = 0;
  215. extern short current_equip_item_index; //for New CARDS Scripts. It contains Inventory Index of the EQUIP_SCRIPT caller item. [Lupus]
  216. extern unsigned int current_equip_combo_pos;
  217. int potion_flag=0; //For use on Alchemist improved potions/Potion Pitcher. [Skotlex]
  218. int potion_hp=0, potion_per_hp=0, potion_sp=0, potion_per_sp=0;
  219. int potion_target = 0;
  220. unsigned int *generic_ui_array = NULL;
  221. unsigned int generic_ui_array_size = 0;
  222. c_op get_com(unsigned char *script,int *pos);
  223. int get_num(unsigned char *script,int *pos);
  224. typedef struct script_function {
  225. int (*func)(struct script_state *st);
  226. const char *name;
  227. const char *arg;
  228. } script_function;
  229. extern script_function buildin_func[];
  230. #ifdef BETA_THREAD_TEST
  231. /**
  232. * MySQL Query Slave
  233. **/
  234. static SPIN_LOCK queryThreadLock;
  235. static rAthread queryThread = NULL;
  236. static ramutex queryThreadMutex = NULL;
  237. static racond queryThreadCond = NULL;
  238. static volatile int32 queryThreadTerminate = 0;
  239. struct queryThreadEntry {
  240. bool ok;
  241. bool type; /* main db or log db? */
  242. struct script_state *st;
  243. };
  244. /* Ladies and Gentleman the Manager! */
  245. struct {
  246. struct queryThreadEntry **entry;/* array of structs */
  247. int count;
  248. int timer;/* used to receive processed entries */
  249. } queryThreadData;
  250. #endif
  251. /*==========================================
  252. * (Only those needed) local declaration prototype
  253. *------------------------------------------*/
  254. const char* parse_subexpr(const char* p,int limit);
  255. int run_func(struct script_state *st);
  256. int script_instancegetid(struct script_state *st);
  257. enum {
  258. MF_NOMEMO, //0
  259. MF_NOTELEPORT,
  260. MF_NOSAVE,
  261. MF_NOBRANCH,
  262. MF_NOPENALTY,
  263. MF_NOZENYPENALTY,
  264. MF_PVP,
  265. MF_PVP_NOPARTY,
  266. MF_PVP_NOGUILD,
  267. MF_GVG,
  268. MF_GVG_NOPARTY, //10
  269. MF_NOTRADE,
  270. MF_NOSKILL,
  271. MF_NOWARP,
  272. MF_PARTYLOCK,
  273. MF_NOICEWALL,
  274. MF_SNOW,
  275. MF_FOG,
  276. MF_SAKURA,
  277. MF_LEAVES,
  278. /**
  279. * No longer available, keeping here just in case it's back someday. [Ind]
  280. **/
  281. //MF_RAIN, //20
  282. // 21 free
  283. MF_NOGO = 22,
  284. MF_CLOUDS,
  285. MF_CLOUDS2,
  286. MF_FIREWORKS,
  287. MF_GVG_CASTLE,
  288. MF_GVG_DUNGEON,
  289. MF_NIGHTENABLED,
  290. MF_NOBASEEXP,
  291. MF_NOJOBEXP, //30
  292. MF_NOMOBLOOT,
  293. MF_NOMVPLOOT,
  294. MF_NORETURN,
  295. MF_NOWARPTO,
  296. MF_NIGHTMAREDROP,
  297. MF_RESTRICTED,
  298. MF_NOCOMMAND,
  299. MF_NODROP,
  300. MF_JEXP,
  301. MF_BEXP, //40
  302. MF_NOVENDING,
  303. MF_LOADEVENT,
  304. MF_NOCHAT,
  305. MF_NOEXPPENALTY,
  306. MF_GUILDLOCK,
  307. MF_TOWN,
  308. MF_AUTOTRADE,
  309. MF_ALLOWKS,
  310. MF_MONSTER_NOTELEPORT,
  311. MF_PVP_NOCALCRANK, //50
  312. MF_BATTLEGROUND,
  313. MF_RESET,
  314. MF_CHANNELAUTOJOIN,
  315. MF_NOUSECART,
  316. MF_NOITEMCONSUMPTION,
  317. MF_SUMSTARTMIRACLE,
  318. MF_NOMINEEFFECT,
  319. MF_NOLOCKON,
  320. MF_NOTOMB,
  321. MF_SKILL_DAMAGE //60
  322. };
  323. const char* script_op2name(int op)
  324. {
  325. #define RETURN_OP_NAME(type) case type: return #type
  326. switch( op )
  327. {
  328. RETURN_OP_NAME(C_NOP);
  329. RETURN_OP_NAME(C_POS);
  330. RETURN_OP_NAME(C_INT);
  331. RETURN_OP_NAME(C_PARAM);
  332. RETURN_OP_NAME(C_FUNC);
  333. RETURN_OP_NAME(C_STR);
  334. RETURN_OP_NAME(C_CONSTSTR);
  335. RETURN_OP_NAME(C_ARG);
  336. RETURN_OP_NAME(C_NAME);
  337. RETURN_OP_NAME(C_EOL);
  338. RETURN_OP_NAME(C_RETINFO);
  339. RETURN_OP_NAME(C_USERFUNC);
  340. RETURN_OP_NAME(C_USERFUNC_POS);
  341. // operators
  342. RETURN_OP_NAME(C_OP3);
  343. RETURN_OP_NAME(C_LOR);
  344. RETURN_OP_NAME(C_LAND);
  345. RETURN_OP_NAME(C_LE);
  346. RETURN_OP_NAME(C_LT);
  347. RETURN_OP_NAME(C_GE);
  348. RETURN_OP_NAME(C_GT);
  349. RETURN_OP_NAME(C_EQ);
  350. RETURN_OP_NAME(C_NE);
  351. RETURN_OP_NAME(C_XOR);
  352. RETURN_OP_NAME(C_OR);
  353. RETURN_OP_NAME(C_AND);
  354. RETURN_OP_NAME(C_ADD);
  355. RETURN_OP_NAME(C_SUB);
  356. RETURN_OP_NAME(C_MUL);
  357. RETURN_OP_NAME(C_DIV);
  358. RETURN_OP_NAME(C_MOD);
  359. RETURN_OP_NAME(C_NEG);
  360. RETURN_OP_NAME(C_LNOT);
  361. RETURN_OP_NAME(C_NOT);
  362. RETURN_OP_NAME(C_R_SHIFT);
  363. RETURN_OP_NAME(C_L_SHIFT);
  364. default:
  365. ShowDebug("script_op2name: unexpected op=%d\n", op);
  366. return "???";
  367. }
  368. #undef RETURN_OP_NAME
  369. }
  370. #ifdef DEBUG_DUMP_STACK
  371. static void script_dump_stack(struct script_state* st)
  372. {
  373. int i;
  374. ShowMessage("\tstart = %d\n", st->start);
  375. ShowMessage("\tend = %d\n", st->end);
  376. ShowMessage("\tdefsp = %d\n", st->stack->defsp);
  377. ShowMessage("\tsp = %d\n", st->stack->sp);
  378. for( i = 0; i < st->stack->sp; ++i )
  379. {
  380. struct script_data* data = &st->stack->stack_data[i];
  381. ShowMessage("\t[%d] %s", i, script_op2name(data->type));
  382. switch( data->type )
  383. {
  384. case C_INT:
  385. case C_POS:
  386. ShowMessage(" %d\n", data->u.num);
  387. break;
  388. case C_STR:
  389. case C_CONSTSTR:
  390. ShowMessage(" \"%s\"\n", data->u.str);
  391. break;
  392. case C_NAME:
  393. 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));
  394. break;
  395. case C_RETINFO:
  396. {
  397. struct script_retinfo* ri = data->u.ri;
  398. 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);
  399. }
  400. break;
  401. default:
  402. ShowMessage("\n");
  403. break;
  404. }
  405. }
  406. }
  407. #endif
  408. /// Reports on the console the src of a script error.
  409. static void script_reportsrc(struct script_state *st)
  410. {
  411. struct block_list* bl;
  412. if( st->oid == 0 )
  413. return; //Can't report source.
  414. bl = map_id2bl(st->oid);
  415. if( bl == NULL )
  416. return;
  417. switch( bl->type ) {
  418. case BL_NPC:
  419. if( bl->m >= 0 )
  420. ShowDebug("Source (NPC): %s at %s (%d,%d)\n", ((struct npc_data *)bl)->name, map[bl->m].name, bl->x, bl->y);
  421. else
  422. ShowDebug("Source (NPC): %s (invisible/not on a map)\n", ((struct npc_data *)bl)->name);
  423. break;
  424. default:
  425. if( bl->m >= 0 )
  426. 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);
  427. else
  428. ShowDebug("Source (Non-NPC type %d): name %s (invisible/not on a map)\n", bl->type, status_get_name(bl));
  429. break;
  430. }
  431. }
  432. /// Reports on the console information about the script data.
  433. static void script_reportdata(struct script_data* data)
  434. {
  435. if( data == NULL )
  436. return;
  437. switch( data->type ) {
  438. case C_NOP:// no value
  439. ShowDebug("Data: nothing (nil)\n");
  440. break;
  441. case C_INT:// number
  442. ShowDebug("Data: number value=%"PRId64"\n", data->u.num);
  443. break;
  444. case C_STR:
  445. case C_CONSTSTR:// string
  446. if( data->u.str ) {
  447. ShowDebug("Data: string value=\"%s\"\n", data->u.str);
  448. } else {
  449. ShowDebug("Data: string value=NULL\n");
  450. }
  451. break;
  452. case C_NAME:// reference
  453. if( reference_tovariable(data) ) {// variable
  454. const char* name = reference_getname(data);
  455. ShowDebug("Data: variable name='%s' index=%d\n", name, reference_getindex(data));
  456. } else if( reference_toconstant(data) ) {// constant
  457. ShowDebug("Data: constant name='%s' value=%d\n", reference_getname(data), reference_getconstant(data));
  458. } else if( reference_toparam(data) ) {// param
  459. ShowDebug("Data: param name='%s' type=%d\n", reference_getname(data), reference_getparamtype(data));
  460. } else {// ???
  461. ShowDebug("Data: reference name='%s' type=%s\n", reference_getname(data), script_op2name(data->type));
  462. ShowDebug("Please report this!!! - script->str_data.type=%s\n", script_op2name(str_data[reference_getid(data)].type));
  463. }
  464. break;
  465. case C_POS:// label
  466. ShowDebug("Data: label pos=%"PRId64"\n", data->u.num);
  467. break;
  468. default:
  469. ShowDebug("Data: %s\n", script_op2name(data->type));
  470. break;
  471. }
  472. }
  473. /// Reports on the console information about the current built-in function.
  474. static void script_reportfunc(struct script_state* st)
  475. {
  476. int params, id;
  477. struct script_data* data;
  478. if( !script_hasdata(st,0) )
  479. {// no stack
  480. return;
  481. }
  482. data = script_getdata(st,0);
  483. if( !data_isreference(data) || str_data[reference_getid(data)].type != C_FUNC )
  484. {// script currently not executing a built-in function or corrupt stack
  485. return;
  486. }
  487. id = reference_getid(data);
  488. params = script_lastdata(st)-1;
  489. if( params > 0 )
  490. {
  491. int i;
  492. ShowDebug("Function: %s (%d parameter%s):\n", get_str(id), params, ( params == 1 ) ? "" : "s");
  493. for( i = 2; i <= script_lastdata(st); i++ )
  494. {
  495. script_reportdata(script_getdata(st,i));
  496. }
  497. }
  498. else
  499. {
  500. ShowDebug("Function: %s (no parameters)\n", get_str(id));
  501. }
  502. }
  503. /*==========================================
  504. * Output error message
  505. *------------------------------------------*/
  506. static void disp_error_message2(const char *mes,const char *pos,int report)
  507. {
  508. error_msg = aStrdup(mes);
  509. error_pos = pos;
  510. error_report = report;
  511. longjmp( error_jump, 1 );
  512. }
  513. #define disp_error_message(mes,pos) disp_error_message2(mes,pos,1)
  514. static void disp_warning_message(const char *mes, const char *pos) {
  515. script_warning(parser_current_src,parser_current_file,parser_current_line,mes,pos);
  516. }
  517. /// Checks event parameter validity
  518. static void check_event(struct script_state *st, const char *evt)
  519. {
  520. if( evt && evt[0] && !stristr(evt, "::On") )
  521. {
  522. ShowWarning("NPC event parameter deprecated! Please use 'NPCNAME::OnEVENT' instead of '%s'.\n", evt);
  523. script_reportsrc(st);
  524. }
  525. }
  526. /*==========================================
  527. * Hashes the input string
  528. *------------------------------------------*/
  529. static unsigned int calc_hash(const char* p)
  530. {
  531. unsigned int h;
  532. #if defined(SCRIPT_HASH_DJB2)
  533. h = 5381;
  534. while( *p ) // hash*33 + c
  535. h = ( h << 5 ) + h + ((unsigned char)TOLOWER(*p++));
  536. #elif defined(SCRIPT_HASH_SDBM)
  537. h = 0;
  538. while( *p ) // hash*65599 + c
  539. h = ( h << 6 ) + ( h << 16 ) - h + ((unsigned char)TOLOWER(*p++));
  540. #elif defined(SCRIPT_HASH_ELF) // UNIX ELF hash
  541. h = 0;
  542. while( *p ){
  543. unsigned int g;
  544. h = ( h << 4 ) + ((unsigned char)TOLOWER(*p++));
  545. g = h & 0xF0000000;
  546. if( g )
  547. {
  548. h ^= g >> 24;
  549. h &= ~g;
  550. }
  551. }
  552. #else // athena hash
  553. h = 0;
  554. while( *p )
  555. h = ( h << 1 ) + ( h >> 3 ) + ( h >> 5 ) + ( h >> 8 ) + (unsigned char)TOLOWER(*p++);
  556. #endif
  557. return h % SCRIPT_HASH_SIZE;
  558. }
  559. /*==========================================
  560. * str_data manipulation functions
  561. *------------------------------------------*/
  562. /// Looks up string using the provided id.
  563. const char* get_str(int id)
  564. {
  565. Assert( id >= LABEL_START && id < str_size );
  566. return str_buf+str_data[id].str;
  567. }
  568. /// Returns the uid of the string, or -1.
  569. static int search_str(const char* p)
  570. {
  571. int i;
  572. for( i = str_hash[calc_hash(p)]; i != 0; i = str_data[i].next )
  573. if( strcasecmp(get_str(i),p) == 0 )
  574. return i;
  575. return -1;
  576. }
  577. /// Stores a copy of the string and returns its id.
  578. /// If an identical string is already present, returns its id instead.
  579. int add_str(const char* p)
  580. {
  581. int h;
  582. int len;
  583. h = calc_hash(p);
  584. if( str_hash[h] == 0 )
  585. {// empty bucket, add new node here
  586. str_hash[h] = str_num;
  587. }
  588. else
  589. {// scan for end of list, or occurence of identical string
  590. int i;
  591. for( i = str_hash[h]; ; i = str_data[i].next )
  592. {
  593. if( strcasecmp(get_str(i),p) == 0 )
  594. return i; // string already in list
  595. if( str_data[i].next == 0 )
  596. break; // reached the end
  597. }
  598. // append node to end of list
  599. str_data[i].next = str_num;
  600. }
  601. // grow list if neccessary
  602. if( str_num >= str_data_size )
  603. {
  604. str_data_size += 128;
  605. RECREATE(str_data,struct str_data_struct,str_data_size);
  606. memset(str_data + (str_data_size - 128), '\0', 128);
  607. }
  608. len=(int)strlen(p);
  609. // grow string buffer if neccessary
  610. while( str_pos+len+1 >= str_size )
  611. {
  612. str_size += 256;
  613. RECREATE(str_buf,char,str_size);
  614. memset(str_buf + (str_size - 256), '\0', 256);
  615. }
  616. safestrncpy(str_buf+str_pos, p, len+1);
  617. str_data[str_num].type = C_NOP;
  618. str_data[str_num].str = str_pos;
  619. str_data[str_num].next = 0;
  620. str_data[str_num].func = NULL;
  621. str_data[str_num].backpatch = -1;
  622. str_data[str_num].label = -1;
  623. str_pos += len+1;
  624. return str_num++;
  625. }
  626. /// Appends 1 byte to the script buffer.
  627. static void add_scriptb(int a)
  628. {
  629. if( script_pos+1 >= script_size )
  630. {
  631. script_size += SCRIPT_BLOCK_SIZE;
  632. RECREATE(script_buf,unsigned char,script_size);
  633. }
  634. script_buf[script_pos++] = (uint8)(a);
  635. }
  636. /// Appends a c_op value to the script buffer.
  637. /// The value is variable-length encoded into 8-bit blocks.
  638. /// The encoding scheme is ( 01?????? )* 00??????, LSB first.
  639. /// All blocks but the last hold 7 bits of data, topmost bit is always 1 (carries).
  640. static void add_scriptc(int a)
  641. {
  642. while( a >= 0x40 )
  643. {
  644. add_scriptb((a&0x3f)|0x40);
  645. a = (a - 0x40) >> 6;
  646. }
  647. add_scriptb(a);
  648. }
  649. /// Appends an integer value to the script buffer.
  650. /// The value is variable-length encoded into 8-bit blocks.
  651. /// The encoding scheme is ( 11?????? )* 10??????, LSB first.
  652. /// All blocks but the last hold 7 bits of data, topmost bit is always 1 (carries).
  653. static void add_scripti(int a)
  654. {
  655. while( a >= 0x40 )
  656. {
  657. add_scriptb((a&0x3f)|0xc0);
  658. a = (a - 0x40) >> 6;
  659. }
  660. add_scriptb(a|0x80);
  661. }
  662. /// Appends a str_data object (label/function/variable/integer) to the script buffer.
  663. ///
  664. /// @param l The id of the str_data entry
  665. // Maximum up to 16M
  666. static void add_scriptl(int l)
  667. {
  668. int backpatch = str_data[l].backpatch;
  669. switch(str_data[l].type){
  670. case C_POS:
  671. case C_USERFUNC_POS:
  672. add_scriptc(C_POS);
  673. add_scriptb(str_data[l].label);
  674. add_scriptb(str_data[l].label>>8);
  675. add_scriptb(str_data[l].label>>16);
  676. break;
  677. case C_NOP:
  678. case C_USERFUNC:
  679. // Embedded data backpatch there is a possibility of label
  680. add_scriptc(C_NAME);
  681. str_data[l].backpatch = script_pos;
  682. add_scriptb(backpatch);
  683. add_scriptb(backpatch>>8);
  684. add_scriptb(backpatch>>16);
  685. break;
  686. case C_INT:
  687. add_scripti(abs(str_data[l].val));
  688. if( str_data[l].val < 0 ) //Notice that this is negative, from jA (Rayce)
  689. add_scriptc(C_NEG);
  690. break;
  691. default: // assume C_NAME
  692. add_scriptc(C_NAME);
  693. add_scriptb(l);
  694. add_scriptb(l>>8);
  695. add_scriptb(l>>16);
  696. break;
  697. }
  698. }
  699. /*==========================================
  700. * Resolve the label
  701. *------------------------------------------*/
  702. void set_label(int l,int pos, const char* script_pos_cur)
  703. {
  704. int i;
  705. if(str_data[l].type==C_INT || str_data[l].type==C_PARAM || str_data[l].type==C_FUNC)
  706. { //Prevent overwriting constants values, parameters and built-in functions [Skotlex]
  707. disp_error_message("set_label: invalid label name",script_pos_cur);
  708. return;
  709. }
  710. if(str_data[l].label!=-1){
  711. disp_error_message("set_label: dup label ",script_pos_cur);
  712. return;
  713. }
  714. str_data[l].type=(str_data[l].type == C_USERFUNC ? C_USERFUNC_POS : C_POS);
  715. str_data[l].label=pos;
  716. for(i=str_data[l].backpatch;i>=0 && i!=0x00ffffff;){
  717. int next=GETVALUE(script_buf,i);
  718. script_buf[i-1]=(str_data[l].type == C_USERFUNC ? C_USERFUNC_POS : C_POS);
  719. SETVALUE(script_buf,i,pos);
  720. i=next;
  721. }
  722. }
  723. /// Skips spaces and/or comments.
  724. const char* skip_space(const char* p)
  725. {
  726. if( p == NULL )
  727. return NULL;
  728. for(;;)
  729. {
  730. while( ISSPACE(*p) )
  731. ++p;
  732. if( *p == '/' && p[1] == '/' )
  733. {// line comment
  734. while(*p && *p!='\n')
  735. ++p;
  736. }
  737. else if( *p == '/' && p[1] == '*' )
  738. {// block comment
  739. p += 2;
  740. for(;;)
  741. {
  742. if( *p == '\0' ) {
  743. disp_warning_message("script:script->skip_space: end of file while parsing block comment. expected "CL_BOLD"*/"CL_NORM, p);
  744. return p;
  745. }
  746. if( *p == '*' && p[1] == '/' )
  747. {// end of block comment
  748. p += 2;
  749. break;
  750. }
  751. ++p;
  752. }
  753. }
  754. else
  755. break;
  756. }
  757. return p;
  758. }
  759. /// Skips a word.
  760. /// A word consists of undercores and/or alphanumeric characters,
  761. /// and valid variable prefixes/postfixes.
  762. static const char* skip_word(const char* p)
  763. {
  764. // prefix
  765. switch( *p ) {
  766. case '@':// temporary char variable
  767. ++p; break;
  768. case '#':// account variable
  769. p += ( p[1] == '#' ? 2 : 1 ); break;
  770. case '\'':// instance variable
  771. ++p; break;
  772. case '.':// npc variable
  773. p += ( p[1] == '@' ? 2 : 1 ); break;
  774. case '$':// global variable
  775. p += ( p[1] == '@' ? 2 : 1 ); break;
  776. }
  777. while( ISALNUM(*p) || *p == '_' )
  778. ++p;
  779. // postfix
  780. if( *p == '$' )// string
  781. p++;
  782. return p;
  783. }
  784. /// Adds a word to str_data.
  785. /// @see skip_word
  786. /// @see add_str
  787. static int add_word(const char* p)
  788. {
  789. char* word;
  790. int len;
  791. int i;
  792. // Check for a word
  793. len = skip_word(p) - p;
  794. if( len == 0 )
  795. disp_error_message("script:add_word: invalid word. A word consists of undercores and/or alphanumeric characters, and valid variable prefixes/postfixes.", p);
  796. // Duplicate the word
  797. word = (char*)aMalloc(len+1);
  798. memcpy(word, p, len);
  799. word[len] = 0;
  800. // add the word
  801. i = add_str(word);
  802. aFree(word);
  803. return i;
  804. }
  805. /// Parses a function call.
  806. /// The argument list can have parenthesis or not.
  807. /// The number of arguments is checked.
  808. static
  809. const char* parse_callfunc(const char* p, int require_paren, int is_custom)
  810. {
  811. const char* p2;
  812. const char* arg=NULL;
  813. int func;
  814. func = add_word(p);
  815. if( str_data[func].type == C_FUNC ){
  816. // buildin function
  817. add_scriptl(func);
  818. add_scriptc(C_ARG);
  819. arg = buildin_func[str_data[func].val].arg;
  820. } else if( str_data[func].type == C_USERFUNC || str_data[func].type == C_USERFUNC_POS ){
  821. // script defined function
  822. add_scriptl(buildin_callsub_ref);
  823. add_scriptc(C_ARG);
  824. add_scriptl(func);
  825. arg = buildin_func[str_data[buildin_callsub_ref].val].arg;
  826. if( *arg == 0 )
  827. disp_error_message("parse_callfunc: callsub has no arguments, please review its definition",p);
  828. if( *arg != '*' )
  829. ++arg; // count func as argument
  830. } else {
  831. #ifdef SCRIPT_CALLFUNC_CHECK
  832. const char* name = get_str(func);
  833. if( !is_custom && strdb_get(userfunc_db, name) == NULL ) {
  834. #endif
  835. disp_error_message("parse_line: expect command, missing function name or calling undeclared function",p);
  836. #ifdef SCRIPT_CALLFUNC_CHECK
  837. } else {;
  838. add_scriptl(buildin_callfunc_ref);
  839. add_scriptc(C_ARG);
  840. add_scriptc(C_STR);
  841. while( *name ) add_scriptb(*name ++);
  842. add_scriptb(0);
  843. arg = buildin_func[str_data[buildin_callfunc_ref].val].arg;
  844. if( *arg != '*' ) ++ arg;
  845. }
  846. #endif
  847. }
  848. p = skip_word(p);
  849. p = skip_space(p);
  850. syntax.curly[syntax.curly_count].type = TYPE_ARGLIST;
  851. syntax.curly[syntax.curly_count].count = 0;
  852. if( *p == ';' )
  853. {// <func name> ';'
  854. syntax.curly[syntax.curly_count].flag = ARGLIST_NO_PAREN;
  855. } else if( *p == '(' && *(p2=skip_space(p+1)) == ')' )
  856. {// <func name> '(' ')'
  857. syntax.curly[syntax.curly_count].flag = ARGLIST_PAREN;
  858. p = p2;
  859. /*
  860. } else if( 0 && require_paren && *p != '(' )
  861. {// <func name>
  862. syntax.curly[syntax.curly_count].flag = ARGLIST_NO_PAREN;
  863. */
  864. } else
  865. {// <func name> <arg list>
  866. if( require_paren ){
  867. if( *p != '(' )
  868. disp_error_message("need '('",p);
  869. ++p; // skip '('
  870. syntax.curly[syntax.curly_count].flag = ARGLIST_PAREN;
  871. } else if( *p == '(' ){
  872. syntax.curly[syntax.curly_count].flag = ARGLIST_UNDEFINED;
  873. } else {
  874. syntax.curly[syntax.curly_count].flag = ARGLIST_NO_PAREN;
  875. }
  876. ++syntax.curly_count;
  877. while( *arg ) {
  878. p2=parse_subexpr(p,-1);
  879. if( p == p2 )
  880. break; // not an argument
  881. if( *arg != '*' )
  882. ++arg; // next argument
  883. p=skip_space(p2);
  884. if( *arg == 0 || *p != ',' )
  885. break; // no more arguments
  886. ++p; // skip comma
  887. }
  888. --syntax.curly_count;
  889. }
  890. if( *arg && *arg != '?' && *arg != '*' )
  891. disp_error_message2("parse_callfunc: not enough arguments, expected ','", p, script_config.warn_func_mismatch_paramnum);
  892. if( syntax.curly[syntax.curly_count].type != TYPE_ARGLIST )
  893. disp_error_message("parse_callfunc: DEBUG last curly is not an argument list",p);
  894. if( syntax.curly[syntax.curly_count].flag == ARGLIST_PAREN ){
  895. if( *p != ')' )
  896. disp_error_message("parse_callfunc: expected ')' to close argument list",p);
  897. ++p;
  898. }
  899. add_scriptc(C_FUNC);
  900. return p;
  901. }
  902. /// Processes end of logical script line.
  903. /// @param first When true, only fix up scheduling data is initialized
  904. /// @param p Script position for error reporting in set_label
  905. static void parse_nextline(bool first, const char* p)
  906. {
  907. if( !first )
  908. {
  909. add_scriptc(C_EOL); // mark end of line for stack cleanup
  910. set_label(LABEL_NEXTLINE, script_pos, p); // fix up '-' labels
  911. }
  912. // initialize data for new '-' label fix up scheduling
  913. str_data[LABEL_NEXTLINE].type = C_NOP;
  914. str_data[LABEL_NEXTLINE].backpatch = -1;
  915. str_data[LABEL_NEXTLINE].label = -1;
  916. }
  917. /// Parse a variable assignment using the direct equals operator
  918. /// @param p script position where the function should run from
  919. /// @return NULL if not a variable assignment, the new position otherwise
  920. const char* parse_variable(const char* p) {
  921. int word;
  922. c_op type = C_NOP;
  923. const char *p2 = NULL;
  924. const char *var = p;
  925. // skip the variable where applicable
  926. p = skip_word(p);
  927. p = skip_space(p);
  928. if( p == NULL ) {// end of the line or invalid buffer
  929. return NULL;
  930. }
  931. if( *p == '[' ) {// array variable so process the array as appropriate
  932. int i,j;
  933. for( p2 = p, i = 0, j = 1; p; ++ i ) {
  934. if( *p ++ == ']' && --(j) == 0 ) break;
  935. if( *p == '[' ) ++ j;
  936. }
  937. if( !(p = skip_space(p)) ) {// end of line or invalid characters remaining
  938. disp_error_message("Missing right expression or closing bracket for variable.", p);
  939. }
  940. }
  941. if( type == C_NOP &&
  942. !( ( p[0] == '=' && p[1] != '=' && (type = C_EQ) ) // =
  943. || ( p[0] == '+' && p[1] == '=' && (type = C_ADD) ) // +=
  944. || ( p[0] == '-' && p[1] == '=' && (type = C_SUB) ) // -=
  945. || ( p[0] == '^' && p[1] == '=' && (type = C_XOR) ) // ^=
  946. || ( p[0] == '|' && p[1] == '=' && (type = C_OR ) ) // |=
  947. || ( p[0] == '&' && p[1] == '=' && (type = C_AND) ) // &=
  948. || ( p[0] == '*' && p[1] == '=' && (type = C_MUL) ) // *=
  949. || ( p[0] == '/' && p[1] == '=' && (type = C_DIV) ) // /=
  950. || ( p[0] == '%' && p[1] == '=' && (type = C_MOD) ) // %=
  951. || ( p[0] == '~' && p[1] == '=' && (type = C_NOT) ) // ~=
  952. || ( p[0] == '+' && p[1] == '+' && (type = C_ADD_PP) ) // ++
  953. || ( p[0] == '-' && p[1] == '-' && (type = C_SUB_PP) ) // --
  954. || ( p[0] == '<' && p[1] == '<' && p[2] == '=' && (type = C_L_SHIFT) ) // <<=
  955. || ( p[0] == '>' && p[1] == '>' && p[2] == '=' && (type = C_R_SHIFT) ) // >>=
  956. ) )
  957. {// failed to find a matching operator combination so invalid
  958. return NULL;
  959. }
  960. switch( type ) {
  961. case C_EQ: {// incremental modifier
  962. p = skip_space( &p[1] );
  963. }
  964. break;
  965. case C_L_SHIFT:
  966. case C_R_SHIFT: {// left or right shift modifier
  967. p = skip_space( &p[3] );
  968. }
  969. break;
  970. default: {// normal incremental command
  971. p = skip_space( &p[2] );
  972. }
  973. }
  974. if( p == NULL ) {// end of line or invalid buffer
  975. return NULL;
  976. }
  977. // push the set function onto the stack
  978. add_scriptl(buildin_set_ref);
  979. add_scriptc(C_ARG);
  980. // always append parenthesis to avoid errors
  981. syntax.curly[syntax.curly_count].type = TYPE_ARGLIST;
  982. syntax.curly[syntax.curly_count].count = 0;
  983. syntax.curly[syntax.curly_count].flag = ARGLIST_PAREN;
  984. // increment the total curly count for the position in the script
  985. ++ syntax.curly_count;
  986. // parse the variable currently being modified
  987. word = add_word(var);
  988. if( str_data[word].type == C_FUNC || str_data[word].type == C_USERFUNC || str_data[word].type == C_USERFUNC_POS )
  989. {// cannot assign a variable which exists as a function or label
  990. disp_error_message("Cannot modify a variable which has the same name as a function or label.", p);
  991. }
  992. if( p2 ) {// process the variable index
  993. const char* p3 = NULL;
  994. // push the getelementofarray method into the stack
  995. add_scriptl(buildin_getelementofarray_ref);
  996. add_scriptc(C_ARG);
  997. add_scriptl(word);
  998. // process the sub-expression for this assignment
  999. p3 = parse_subexpr(p2 + 1, 1);
  1000. p3 = skip_space(p3);
  1001. if( *p3 != ']' ) {// closing parenthesis is required for this script
  1002. disp_error_message("Missing closing ']' parenthesis for the variable assignment.", p3);
  1003. }
  1004. // push the closing function stack operator onto the stack
  1005. add_scriptc(C_FUNC);
  1006. p3 ++;
  1007. } else {// simply push the variable or value onto the stack
  1008. add_scriptl(word);
  1009. }
  1010. if( type != C_EQ )
  1011. add_scriptc(C_REF);
  1012. if( type == C_ADD_PP || type == C_SUB_PP ) {// incremental operator for the method
  1013. add_scripti(1);
  1014. add_scriptc(type == C_ADD_PP ? C_ADD : C_SUB);
  1015. } else {// process the value as an expression
  1016. p = parse_subexpr(p, -1);
  1017. if( type != C_EQ )
  1018. {// push the type of modifier onto the stack
  1019. add_scriptc(type);
  1020. }
  1021. }
  1022. // decrement the curly count for the position within the script
  1023. -- syntax.curly_count;
  1024. // close the script by appending the function operator
  1025. add_scriptc(C_FUNC);
  1026. // push the buffer from the method
  1027. return p;
  1028. }
  1029. /*
  1030. * Checks whether the gives string is a number literal
  1031. *
  1032. * Mainly necessary to differentiate between number literals and NPC name
  1033. * constants, since several of those start with a digit.
  1034. *
  1035. * All this does is to check if the string begins with an optional + or - sign,
  1036. * followed by a hexadecimal or decimal number literal literal and is NOT
  1037. * followed by a underscore or letter.
  1038. *
  1039. * @author : Hercules.ws
  1040. * @param p Pointer to the string to check
  1041. * @return Whether the string is a number literal
  1042. */
  1043. bool is_number(const char *p) {
  1044. const char *np;
  1045. if (!p)
  1046. return false;
  1047. if (*p == '-' || *p == '+')
  1048. p++;
  1049. np = p;
  1050. if (*p == '0' && p[1] == 'x') {
  1051. p+=2;
  1052. np = p;
  1053. // Hexadecimal
  1054. while (ISXDIGIT(*np))
  1055. np++;
  1056. } else {
  1057. // Decimal
  1058. while (ISDIGIT(*np))
  1059. np++;
  1060. }
  1061. if (p != np && *np != '_' && !ISALPHA(*np)) // At least one digit, and next isn't a letter or _
  1062. return true;
  1063. return false;
  1064. }
  1065. /*==========================================
  1066. * Analysis section
  1067. *------------------------------------------*/
  1068. const char* parse_simpleexpr(const char *p)
  1069. {
  1070. long long i;
  1071. p=skip_space(p);
  1072. if(*p==';' || *p==',')
  1073. disp_error_message("parse_simpleexpr: unexpected end of expression",p);
  1074. if(*p=='('){
  1075. if( (i=syntax.curly_count-1) >= 0 && syntax.curly[i].type == TYPE_ARGLIST )
  1076. ++syntax.curly[i].count;
  1077. p=parse_subexpr(p+1,-1);
  1078. p=skip_space(p);
  1079. if( (i=syntax.curly_count-1) >= 0 && syntax.curly[i].type == TYPE_ARGLIST &&
  1080. syntax.curly[i].flag == ARGLIST_UNDEFINED && --syntax.curly[i].count == 0
  1081. ){
  1082. if( *p == ',' ){
  1083. syntax.curly[i].flag = ARGLIST_PAREN;
  1084. return p;
  1085. } else
  1086. syntax.curly[i].flag = ARGLIST_NO_PAREN;
  1087. }
  1088. if( *p != ')' )
  1089. disp_error_message("parse_simpleexpr: unmatched ')'",p);
  1090. ++p;
  1091. } else if(is_number(p)) {
  1092. char *np;
  1093. while(*p == '0' && ISDIGIT(p[1])) p++;
  1094. i=strtoll(p,&np,0);
  1095. if( i < INT_MIN ) {
  1096. i = INT_MIN;
  1097. disp_warning_message("parse_simpleexpr: underflow detected, capping value to INT_MIN",p);
  1098. } else if( i > INT_MAX ) {
  1099. i = INT_MAX;
  1100. disp_warning_message("parse_simpleexpr: overflow detected, capping value to INT_MAX",p);
  1101. }
  1102. add_scripti((int)i);
  1103. p=np;
  1104. } else if(*p=='"'){
  1105. add_scriptc(C_STR);
  1106. p++;
  1107. while( *p && *p != '"' ){
  1108. if( (unsigned char)p[-1] <= 0x7e && *p == '\\' )
  1109. {
  1110. char buf[8];
  1111. size_t len = skip_escaped_c(p) - p;
  1112. size_t n = sv_unescape_c(buf, p, len);
  1113. if( n != 1 )
  1114. ShowDebug("parse_simpleexpr: unexpected length %d after unescape (\"%.*s\" -> %.*s)\n", (int)n, (int)len, p, (int)n, buf);
  1115. p += len;
  1116. add_scriptb(*buf);
  1117. continue;
  1118. }
  1119. else if( *p == '\n' )
  1120. disp_error_message("parse_simpleexpr: unexpected newline @ string",p);
  1121. add_scriptb(*p++);
  1122. }
  1123. if(!*p)
  1124. disp_error_message("parse_simpleexpr: unexpected eof @ string",p);
  1125. add_scriptb(0);
  1126. p++; //'"'
  1127. } else {
  1128. int l;
  1129. const char* pv;
  1130. // label , register , function etc
  1131. if(skip_word(p)==p)
  1132. disp_error_message("parse_simpleexpr: unexpected character",p);
  1133. l=add_word(p);
  1134. if( str_data[l].type == C_FUNC || str_data[l].type == C_USERFUNC || str_data[l].type == C_USERFUNC_POS)
  1135. return parse_callfunc(p,1,0);
  1136. #ifdef SCRIPT_CALLFUNC_CHECK
  1137. else {
  1138. const char* name = get_str(l);
  1139. if( strdb_get(userfunc_db,name) != NULL ) {
  1140. return parse_callfunc(p,1,1);
  1141. }
  1142. }
  1143. #endif
  1144. if( (pv = parse_variable(p)) )
  1145. {// successfully processed a variable assignment
  1146. return pv;
  1147. }
  1148. p=skip_word(p);
  1149. if( *p == '[' ){
  1150. // array(name[i] => getelementofarray(name,i) )
  1151. add_scriptl(buildin_getelementofarray_ref);
  1152. add_scriptc(C_ARG);
  1153. add_scriptl(l);
  1154. p=parse_subexpr(p+1,-1);
  1155. p=skip_space(p);
  1156. if( *p != ']' )
  1157. disp_error_message("parse_simpleexpr: unmatched ']'",p);
  1158. ++p;
  1159. add_scriptc(C_FUNC);
  1160. }else
  1161. add_scriptl(l);
  1162. }
  1163. return p;
  1164. }
  1165. /*==========================================
  1166. * Analysis of the expression
  1167. *------------------------------------------*/
  1168. const char* parse_subexpr(const char* p,int limit)
  1169. {
  1170. int op,opl,len;
  1171. p=skip_space(p);
  1172. if( *p == '-' ){
  1173. const char* tmpp = skip_space(p+1);
  1174. if( *tmpp == ';' || *tmpp == ',' ){
  1175. add_scriptl(LABEL_NEXTLINE);
  1176. p++;
  1177. return p;
  1178. }
  1179. }
  1180. if((op=C_NEG,*p=='-') || (op=C_LNOT,*p=='!') || (op=C_NOT,*p=='~')){
  1181. p=parse_subexpr(p+1,10);
  1182. add_scriptc(op);
  1183. } else
  1184. p=parse_simpleexpr(p);
  1185. p=skip_space(p);
  1186. while((
  1187. (op=C_OP3,opl=0,len=1,*p=='?') ||
  1188. (op=C_ADD,opl=8,len=1,*p=='+') ||
  1189. (op=C_SUB,opl=8,len=1,*p=='-') ||
  1190. (op=C_MUL,opl=9,len=1,*p=='*') ||
  1191. (op=C_DIV,opl=9,len=1,*p=='/') ||
  1192. (op=C_MOD,opl=9,len=1,*p=='%') ||
  1193. (op=C_LAND,opl=2,len=2,*p=='&' && p[1]=='&') ||
  1194. (op=C_AND,opl=6,len=1,*p=='&') ||
  1195. (op=C_LOR,opl=1,len=2,*p=='|' && p[1]=='|') ||
  1196. (op=C_OR,opl=5,len=1,*p=='|') ||
  1197. (op=C_XOR,opl=4,len=1,*p=='^') ||
  1198. (op=C_EQ,opl=3,len=2,*p=='=' && p[1]=='=') ||
  1199. (op=C_NE,opl=3,len=2,*p=='!' && p[1]=='=') ||
  1200. (op=C_R_SHIFT,opl=7,len=2,*p=='>' && p[1]=='>') ||
  1201. (op=C_GE,opl=3,len=2,*p=='>' && p[1]=='=') ||
  1202. (op=C_GT,opl=3,len=1,*p=='>') ||
  1203. (op=C_L_SHIFT,opl=7,len=2,*p=='<' && p[1]=='<') ||
  1204. (op=C_LE,opl=3,len=2,*p=='<' && p[1]=='=') ||
  1205. (op=C_LT,opl=3,len=1,*p=='<')) && opl>limit){
  1206. p+=len;
  1207. if(op == C_OP3) {
  1208. p=parse_subexpr(p,-1);
  1209. p=skip_space(p);
  1210. if( *(p++) != ':')
  1211. disp_error_message("parse_subexpr: expected ':'", p-1);
  1212. p=parse_subexpr(p,-1);
  1213. } else {
  1214. p=parse_subexpr(p,opl);
  1215. }
  1216. add_scriptc(op);
  1217. p=skip_space(p);
  1218. }
  1219. return p; /* return first untreated operator */
  1220. }
  1221. /*==========================================
  1222. * Evaluation of the expression
  1223. *------------------------------------------*/
  1224. const char* parse_expr(const char *p)
  1225. {
  1226. switch(*p){
  1227. case ')': case ';': case ':': case '[': case ']':
  1228. case '}':
  1229. disp_error_message("parse_expr: unexpected character",p);
  1230. }
  1231. p=parse_subexpr(p,-1);
  1232. return p;
  1233. }
  1234. /*==========================================
  1235. * Analysis of the line
  1236. *------------------------------------------*/
  1237. const char* parse_line(const char* p)
  1238. {
  1239. const char* p2;
  1240. p=skip_space(p);
  1241. if(*p==';') {
  1242. //Close decision for if(); for(); while();
  1243. p = parse_syntax_close(p + 1);
  1244. return p;
  1245. }
  1246. if(*p==')' && parse_syntax_for_flag)
  1247. return p+1;
  1248. p = skip_space(p);
  1249. if(p[0] == '{') {
  1250. syntax.curly[syntax.curly_count].type = TYPE_NULL;
  1251. syntax.curly[syntax.curly_count].count = -1;
  1252. syntax.curly[syntax.curly_count].index = -1;
  1253. syntax.curly_count++;
  1254. return p + 1;
  1255. } else if(p[0] == '}') {
  1256. return parse_curly_close(p);
  1257. }
  1258. // Syntax-related processing
  1259. p2 = parse_syntax(p);
  1260. if(p2 != NULL)
  1261. return p2;
  1262. // attempt to process a variable assignment
  1263. p2 = parse_variable(p);
  1264. if( p2 != NULL )
  1265. {// variable assignment processed so leave the method
  1266. return parse_syntax_close(p2 + 1);
  1267. }
  1268. p = parse_callfunc(p,0,0);
  1269. p = skip_space(p);
  1270. if(parse_syntax_for_flag) {
  1271. if( *p != ')' )
  1272. disp_error_message("parse_line: expected ')'",p);
  1273. } else {
  1274. if( *p != ';' )
  1275. disp_error_message("parse_line: expected ';'",p);
  1276. }
  1277. //Binding decision for if(), for(), while()
  1278. p = parse_syntax_close(p+1);
  1279. return p;
  1280. }
  1281. // { ... } Closing process
  1282. const char* parse_curly_close(const char* p)
  1283. {
  1284. if(syntax.curly_count <= 0) {
  1285. disp_error_message("parse_curly_close: unexpected string",p);
  1286. return p + 1;
  1287. } else if(syntax.curly[syntax.curly_count-1].type == TYPE_NULL) {
  1288. syntax.curly_count--;
  1289. //Close decision if, for , while
  1290. p = parse_syntax_close(p + 1);
  1291. return p;
  1292. } else if(syntax.curly[syntax.curly_count-1].type == TYPE_SWITCH) {
  1293. //Closing switch()
  1294. int pos = syntax.curly_count-1;
  1295. char label[256];
  1296. int l;
  1297. // Remove temporary variables
  1298. sprintf(label,"set $@__SW%x_VAL,0;",syntax.curly[pos].index);
  1299. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1300. parse_line(label);
  1301. syntax.curly_count--;
  1302. // Go to the end pointer unconditionally
  1303. sprintf(label,"goto __SW%x_FIN;",syntax.curly[pos].index);
  1304. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1305. parse_line(label);
  1306. syntax.curly_count--;
  1307. // You are here labeled
  1308. sprintf(label,"__SW%x_%x",syntax.curly[pos].index,syntax.curly[pos].count);
  1309. l=add_str(label);
  1310. set_label(l,script_pos, p);
  1311. if(syntax.curly[pos].flag) {
  1312. //Exists default
  1313. sprintf(label,"goto __SW%x_DEF;",syntax.curly[pos].index);
  1314. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1315. parse_line(label);
  1316. syntax.curly_count--;
  1317. }
  1318. // Label end
  1319. sprintf(label,"__SW%x_FIN",syntax.curly[pos].index);
  1320. l=add_str(label);
  1321. set_label(l,script_pos, p);
  1322. linkdb_final(&syntax.curly[pos].case_label); // free the list of case label
  1323. syntax.curly_count--;
  1324. //Closing decision if, for , while
  1325. p = parse_syntax_close(p + 1);
  1326. return p;
  1327. } else {
  1328. disp_error_message("parse_curly_close: unexpected string",p);
  1329. return p + 1;
  1330. }
  1331. }
  1332. // Syntax-related processing
  1333. // break, case, continue, default, do, for, function,
  1334. // if, switch, while ? will handle this internally.
  1335. const char* parse_syntax(const char* p)
  1336. {
  1337. const char *p2 = skip_word(p);
  1338. switch(*p) {
  1339. case 'B':
  1340. case 'b':
  1341. if(p2 - p == 5 && !strncasecmp(p,"break",5)) {
  1342. // break Processing
  1343. char label[256];
  1344. int pos = syntax.curly_count - 1;
  1345. while(pos >= 0) {
  1346. if(syntax.curly[pos].type == TYPE_DO) {
  1347. sprintf(label,"goto __DO%x_FIN;",syntax.curly[pos].index);
  1348. break;
  1349. } else if(syntax.curly[pos].type == TYPE_FOR) {
  1350. sprintf(label,"goto __FR%x_FIN;",syntax.curly[pos].index);
  1351. break;
  1352. } else if(syntax.curly[pos].type == TYPE_WHILE) {
  1353. sprintf(label,"goto __WL%x_FIN;",syntax.curly[pos].index);
  1354. break;
  1355. } else if(syntax.curly[pos].type == TYPE_SWITCH) {
  1356. sprintf(label,"goto __SW%x_FIN;",syntax.curly[pos].index);
  1357. break;
  1358. }
  1359. pos--;
  1360. }
  1361. if(pos < 0) {
  1362. disp_error_message("parse_syntax: unexpected 'break'",p);
  1363. } else {
  1364. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1365. parse_line(label);
  1366. syntax.curly_count--;
  1367. }
  1368. p = skip_space(p2);
  1369. if(*p != ';')
  1370. disp_error_message("parse_syntax: expected ';'",p);
  1371. // Closing decision if, for , while
  1372. p = parse_syntax_close(p + 1);
  1373. return p;
  1374. }
  1375. break;
  1376. case 'c':
  1377. case 'C':
  1378. if(p2 - p == 4 && !strncasecmp(p,"case",4)) {
  1379. //Processing case
  1380. int pos = syntax.curly_count-1;
  1381. if(pos < 0 || syntax.curly[pos].type != TYPE_SWITCH) {
  1382. disp_error_message("parse_syntax: unexpected 'case' ",p);
  1383. return p+1;
  1384. } else {
  1385. char label[256];
  1386. int l,v;
  1387. char *np;
  1388. if(syntax.curly[pos].count != 1) {
  1389. //Jump for FALLTHRU
  1390. sprintf(label,"goto __SW%x_%xJ;",syntax.curly[pos].index,syntax.curly[pos].count);
  1391. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1392. parse_line(label);
  1393. syntax.curly_count--;
  1394. // You are here labeled
  1395. sprintf(label,"__SW%x_%x",syntax.curly[pos].index,syntax.curly[pos].count);
  1396. l=add_str(label);
  1397. set_label(l,script_pos, p);
  1398. }
  1399. //Decision statement switch
  1400. p = skip_space(p2);
  1401. if(p == p2) {
  1402. disp_error_message("parse_syntax: expected a space ' '",p);
  1403. }
  1404. // check whether case label is integer or not
  1405. if(is_number(p)) {
  1406. //Numeric value
  1407. v = (int)strtol(p,&np,0);
  1408. if((*p == '-' || *p == '+') && ISDIGIT(p[1])) // pre-skip because '-' can not skip_word
  1409. p++;
  1410. p = skip_word(p);
  1411. if(np != p)
  1412. disp_error_message("parse_syntax: 'case' label is not an integer",np);
  1413. } else {
  1414. //Check for constants
  1415. p2 = skip_word(p);
  1416. v = (int)(size_t) (p2-p); // length of word at p2
  1417. memcpy(label,p,v);
  1418. label[v]='\0';
  1419. if( !script_get_constant(label, &v) )
  1420. disp_error_message("parse_syntax: 'case' label is not an integer",p);
  1421. p = skip_word(p);
  1422. }
  1423. p = skip_space(p);
  1424. if(*p != ':')
  1425. disp_error_message("parse_syntax: expect ':'",p);
  1426. sprintf(label,"if(%d != $@__SW%x_VAL) goto __SW%x_%x;",
  1427. v,syntax.curly[pos].index,syntax.curly[pos].index,syntax.curly[pos].count+1);
  1428. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1429. // Bad I do not parse twice
  1430. p2 = parse_line(label);
  1431. parse_line(p2);
  1432. syntax.curly_count--;
  1433. if(syntax.curly[pos].count != 1) {
  1434. // Label after the completion of FALLTHRU
  1435. sprintf(label,"__SW%x_%xJ",syntax.curly[pos].index,syntax.curly[pos].count);
  1436. l=add_str(label);
  1437. set_label(l,script_pos,p);
  1438. }
  1439. // check duplication of case label [Rayce]
  1440. if(linkdb_search(&syntax.curly[pos].case_label, (void*)__64BPRTSIZE(v)) != NULL)
  1441. disp_error_message("parse_syntax: dup 'case'",p);
  1442. linkdb_insert(&syntax.curly[pos].case_label, (void*)__64BPRTSIZE(v), (void*)1);
  1443. sprintf(label,"set $@__SW%x_VAL,0;",syntax.curly[pos].index);
  1444. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1445. parse_line(label);
  1446. syntax.curly_count--;
  1447. syntax.curly[pos].count++;
  1448. }
  1449. return p + 1;
  1450. } else if(p2 - p == 8 && !strncasecmp(p,"continue",8)) {
  1451. // Processing continue
  1452. char label[256];
  1453. int pos = syntax.curly_count - 1;
  1454. while(pos >= 0) {
  1455. if(syntax.curly[pos].type == TYPE_DO) {
  1456. sprintf(label,"goto __DO%x_NXT;",syntax.curly[pos].index);
  1457. syntax.curly[pos].flag = 1; //Flag put the link for continue
  1458. break;
  1459. } else if(syntax.curly[pos].type == TYPE_FOR) {
  1460. sprintf(label,"goto __FR%x_NXT;",syntax.curly[pos].index);
  1461. break;
  1462. } else if(syntax.curly[pos].type == TYPE_WHILE) {
  1463. sprintf(label,"goto __WL%x_NXT;",syntax.curly[pos].index);
  1464. break;
  1465. }
  1466. pos--;
  1467. }
  1468. if(pos < 0) {
  1469. disp_error_message("parse_syntax: unexpected 'continue'",p);
  1470. } else {
  1471. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1472. parse_line(label);
  1473. syntax.curly_count--;
  1474. }
  1475. p = skip_space(p2);
  1476. if(*p != ';')
  1477. disp_error_message("parse_syntax: expected ';'",p);
  1478. //Closing decision if, for , while
  1479. p = parse_syntax_close(p + 1);
  1480. return p;
  1481. }
  1482. break;
  1483. case 'd':
  1484. case 'D':
  1485. if(p2 - p == 7 && !strncasecmp(p,"default",7)) {
  1486. // Switch - default processing
  1487. int pos = syntax.curly_count-1;
  1488. if(pos < 0 || syntax.curly[pos].type != TYPE_SWITCH) {
  1489. disp_error_message("parse_syntax: unexpected 'default'",p);
  1490. } else if(syntax.curly[pos].flag) {
  1491. disp_error_message("parse_syntax: dup 'default'",p);
  1492. } else {
  1493. char label[256];
  1494. int l;
  1495. // Put the label location
  1496. p = skip_space(p2);
  1497. if(*p != ':') {
  1498. disp_error_message("parse_syntax: expected ':'",p);
  1499. }
  1500. sprintf(label,"__SW%x_%x",syntax.curly[pos].index,syntax.curly[pos].count);
  1501. l=add_str(label);
  1502. set_label(l,script_pos,p);
  1503. // Skip to the next link w/o condition
  1504. sprintf(label,"goto __SW%x_%x;",syntax.curly[pos].index,syntax.curly[pos].count+1);
  1505. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1506. parse_line(label);
  1507. syntax.curly_count--;
  1508. // The default label
  1509. sprintf(label,"__SW%x_DEF",syntax.curly[pos].index);
  1510. l=add_str(label);
  1511. set_label(l,script_pos,p);
  1512. syntax.curly[syntax.curly_count - 1].flag = 1;
  1513. syntax.curly[pos].count++;
  1514. }
  1515. return p + 1;
  1516. } else if(p2 - p == 2 && !strncasecmp(p,"do",2)) {
  1517. int l;
  1518. char label[256];
  1519. p=skip_space(p2);
  1520. syntax.curly[syntax.curly_count].type = TYPE_DO;
  1521. syntax.curly[syntax.curly_count].count = 1;
  1522. syntax.curly[syntax.curly_count].index = syntax.index++;
  1523. syntax.curly[syntax.curly_count].flag = 0;
  1524. // Label of the (do) form here
  1525. sprintf(label,"__DO%x_BGN",syntax.curly[syntax.curly_count].index);
  1526. l=add_str(label);
  1527. set_label(l,script_pos,p);
  1528. syntax.curly_count++;
  1529. return p;
  1530. }
  1531. break;
  1532. case 'f':
  1533. case 'F':
  1534. if(p2 - p == 3 && !strncasecmp(p,"for",3)) {
  1535. int l;
  1536. char label[256];
  1537. int pos = syntax.curly_count;
  1538. syntax.curly[syntax.curly_count].type = TYPE_FOR;
  1539. syntax.curly[syntax.curly_count].count = 1;
  1540. syntax.curly[syntax.curly_count].index = syntax.index++;
  1541. syntax.curly[syntax.curly_count].flag = 0;
  1542. syntax.curly_count++;
  1543. p=skip_space(p2);
  1544. if(*p != '(')
  1545. disp_error_message("parse_syntax: expected '('",p);
  1546. p++;
  1547. // Execute the initialization statement
  1548. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1549. p=parse_line(p);
  1550. syntax.curly_count--;
  1551. // Form the start of label decision
  1552. sprintf(label,"__FR%x_J",syntax.curly[pos].index);
  1553. l=add_str(label);
  1554. set_label(l,script_pos,p);
  1555. p=skip_space(p);
  1556. if(*p == ';') {
  1557. // For (; Because the pattern of always true ;)
  1558. ;
  1559. } else {
  1560. // Skip to the end point if the condition is false
  1561. sprintf(label,"__FR%x_FIN",syntax.curly[pos].index);
  1562. add_scriptl(add_str("jump_zero"));
  1563. add_scriptc(C_ARG);
  1564. p=parse_expr(p);
  1565. p=skip_space(p);
  1566. add_scriptl(add_str(label));
  1567. add_scriptc(C_FUNC);
  1568. }
  1569. if(*p != ';')
  1570. disp_error_message("parse_syntax: expected ';'",p);
  1571. p++;
  1572. // Skip to the beginning of the loop
  1573. sprintf(label,"goto __FR%x_BGN;",syntax.curly[pos].index);
  1574. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1575. parse_line(label);
  1576. syntax.curly_count--;
  1577. // Labels to form the next loop
  1578. sprintf(label,"__FR%x_NXT",syntax.curly[pos].index);
  1579. l=add_str(label);
  1580. set_label(l,script_pos,p);
  1581. // Process the next time you enter the loop
  1582. // A ')' last for; flag to be treated as'
  1583. parse_syntax_for_flag = 1;
  1584. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1585. p=parse_line(p);
  1586. syntax.curly_count--;
  1587. parse_syntax_for_flag = 0;
  1588. // Skip to the determination process conditions
  1589. sprintf(label,"goto __FR%x_J;",syntax.curly[pos].index);
  1590. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1591. parse_line(label);
  1592. syntax.curly_count--;
  1593. // Loop start labeling
  1594. sprintf(label,"__FR%x_BGN",syntax.curly[pos].index);
  1595. l=add_str(label);
  1596. set_label(l,script_pos,p);
  1597. return p;
  1598. }
  1599. else if( p2 - p == 8 && strncasecmp(p,"function",8) == 0 )
  1600. {// internal script function
  1601. const char *func_name;
  1602. func_name = skip_space(p2);
  1603. p = skip_word(func_name);
  1604. if( p == func_name )
  1605. disp_error_message("parse_syntax:function: function name is missing or invalid", p);
  1606. p2 = skip_space(p);
  1607. if( *p2 == ';' )
  1608. {// function <name> ;
  1609. // function declaration - just register the name
  1610. int l;
  1611. l = add_word(func_name);
  1612. if( str_data[l].type == C_NOP )// register only, if the name was not used by something else
  1613. str_data[l].type = C_USERFUNC;
  1614. else if( str_data[l].type == C_USERFUNC )
  1615. ; // already registered
  1616. else
  1617. disp_error_message("parse_syntax:function: function name is invalid", func_name);
  1618. // Close condition of if, for, while
  1619. p = parse_syntax_close(p2 + 1);
  1620. return p;
  1621. }
  1622. else if(*p2 == '{')
  1623. {// function <name> <line/block of code>
  1624. char label[256];
  1625. int l;
  1626. syntax.curly[syntax.curly_count].type = TYPE_USERFUNC;
  1627. syntax.curly[syntax.curly_count].count = 1;
  1628. syntax.curly[syntax.curly_count].index = syntax.index++;
  1629. syntax.curly[syntax.curly_count].flag = 0;
  1630. ++syntax.curly_count;
  1631. // Jump over the function code
  1632. sprintf(label, "goto __FN%x_FIN;", syntax.curly[syntax.curly_count-1].index);
  1633. syntax.curly[syntax.curly_count].type = TYPE_NULL;
  1634. ++syntax.curly_count;
  1635. parse_line(label);
  1636. --syntax.curly_count;
  1637. // Set the position of the function (label)
  1638. l=add_word(func_name);
  1639. if( str_data[l].type == C_NOP || str_data[l].type == C_USERFUNC )// register only, if the name was not used by something else
  1640. {
  1641. str_data[l].type = C_USERFUNC;
  1642. set_label(l, script_pos, p);
  1643. if( parse_options&SCRIPT_USE_LABEL_DB )
  1644. strdb_iput(scriptlabel_db, get_str(l), script_pos);
  1645. }
  1646. else
  1647. disp_error_message("parse_syntax:function: function name is invalid", func_name);
  1648. return skip_space(p);
  1649. }
  1650. else
  1651. {
  1652. disp_error_message("expect ';' or '{' at function syntax",p);
  1653. }
  1654. }
  1655. break;
  1656. case 'i':
  1657. case 'I':
  1658. if(p2 - p == 2 && !strncasecmp(p,"if",2)) {
  1659. // If process
  1660. char label[256];
  1661. p=skip_space(p2);
  1662. if(*p != '(') { //Prevent if this {} non-c syntax. from Rayce (jA)
  1663. disp_error_message("need '('",p);
  1664. }
  1665. syntax.curly[syntax.curly_count].type = TYPE_IF;
  1666. syntax.curly[syntax.curly_count].count = 1;
  1667. syntax.curly[syntax.curly_count].index = syntax.index++;
  1668. syntax.curly[syntax.curly_count].flag = 0;
  1669. sprintf(label,"__IF%x_%x",syntax.curly[syntax.curly_count].index,syntax.curly[syntax.curly_count].count);
  1670. syntax.curly_count++;
  1671. add_scriptl(add_str("jump_zero"));
  1672. add_scriptc(C_ARG);
  1673. p=parse_expr(p);
  1674. p=skip_space(p);
  1675. add_scriptl(add_str(label));
  1676. add_scriptc(C_FUNC);
  1677. return p;
  1678. }
  1679. break;
  1680. case 's':
  1681. case 'S':
  1682. if(p2 - p == 6 && !strncasecmp(p,"switch",6)) {
  1683. // Processing of switch ()
  1684. char label[256];
  1685. p=skip_space(p2);
  1686. if(*p != '(') {
  1687. disp_error_message("need '('",p);
  1688. }
  1689. syntax.curly[syntax.curly_count].type = TYPE_SWITCH;
  1690. syntax.curly[syntax.curly_count].count = 1;
  1691. syntax.curly[syntax.curly_count].index = syntax.index++;
  1692. syntax.curly[syntax.curly_count].flag = 0;
  1693. sprintf(label,"$@__SW%x_VAL",syntax.curly[syntax.curly_count].index);
  1694. syntax.curly_count++;
  1695. add_scriptl(add_str("set"));
  1696. add_scriptc(C_ARG);
  1697. add_scriptl(add_str(label));
  1698. p=parse_expr(p);
  1699. p=skip_space(p);
  1700. if(*p != '{') {
  1701. disp_error_message("parse_syntax: expected '{'",p);
  1702. }
  1703. add_scriptc(C_FUNC);
  1704. return p + 1;
  1705. }
  1706. break;
  1707. case 'w':
  1708. case 'W':
  1709. if(p2 - p == 5 && !strncasecmp(p,"while",5)) {
  1710. int l;
  1711. char label[256];
  1712. p=skip_space(p2);
  1713. if(*p != '(') {
  1714. disp_error_message("need '('",p);
  1715. }
  1716. syntax.curly[syntax.curly_count].type = TYPE_WHILE;
  1717. syntax.curly[syntax.curly_count].count = 1;
  1718. syntax.curly[syntax.curly_count].index = syntax.index++;
  1719. syntax.curly[syntax.curly_count].flag = 0;
  1720. // Form the start of label decision
  1721. sprintf(label,"__WL%x_NXT",syntax.curly[syntax.curly_count].index);
  1722. l=add_str(label);
  1723. set_label(l,script_pos,p);
  1724. // Skip to the end point if the condition is false
  1725. sprintf(label,"__WL%x_FIN",syntax.curly[syntax.curly_count].index);
  1726. syntax.curly_count++;
  1727. add_scriptl(add_str("jump_zero"));
  1728. add_scriptc(C_ARG);
  1729. p=parse_expr(p);
  1730. p=skip_space(p);
  1731. add_scriptl(add_str(label));
  1732. add_scriptc(C_FUNC);
  1733. return p;
  1734. }
  1735. break;
  1736. }
  1737. return NULL;
  1738. }
  1739. const char* parse_syntax_close(const char *p) {
  1740. // If (...) for (...) hoge (); as to make sure closed closed once again
  1741. int flag;
  1742. do {
  1743. p = parse_syntax_close_sub(p,&flag);
  1744. } while(flag);
  1745. return p;
  1746. }
  1747. // Close judgment if, for, while, of do
  1748. // flag == 1 : closed
  1749. // flag == 0 : not closed
  1750. const char* parse_syntax_close_sub(const char* p,int* flag)
  1751. {
  1752. char label[256];
  1753. int pos = syntax.curly_count - 1;
  1754. int l;
  1755. *flag = 1;
  1756. if(syntax.curly_count <= 0) {
  1757. *flag = 0;
  1758. return p;
  1759. } else if(syntax.curly[pos].type == TYPE_IF) {
  1760. const char *bp = p;
  1761. const char *p2;
  1762. // if-block and else-block end is a new line
  1763. parse_nextline(false, p);
  1764. // Skip to the last location if
  1765. sprintf(label,"goto __IF%x_FIN;",syntax.curly[pos].index);
  1766. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1767. parse_line(label);
  1768. syntax.curly_count--;
  1769. // Put the label of the location
  1770. sprintf(label,"__IF%x_%x",syntax.curly[pos].index,syntax.curly[pos].count);
  1771. l=add_str(label);
  1772. set_label(l,script_pos,p);
  1773. syntax.curly[pos].count++;
  1774. p = skip_space(p);
  1775. p2 = skip_word(p);
  1776. if(!syntax.curly[pos].flag && p2 - p == 4 && !strncasecmp(p,"else",4)) {
  1777. // else or else - if
  1778. p = skip_space(p2);
  1779. p2 = skip_word(p);
  1780. if(p2 - p == 2 && !strncasecmp(p,"if",2)) {
  1781. // else - if
  1782. p=skip_space(p2);
  1783. if(*p != '(') {
  1784. disp_error_message("need '('",p);
  1785. }
  1786. sprintf(label,"__IF%x_%x",syntax.curly[pos].index,syntax.curly[pos].count);
  1787. add_scriptl(add_str("jump_zero"));
  1788. add_scriptc(C_ARG);
  1789. p=parse_expr(p);
  1790. p=skip_space(p);
  1791. add_scriptl(add_str(label));
  1792. add_scriptc(C_FUNC);
  1793. *flag = 0;
  1794. return p;
  1795. } else {
  1796. // else
  1797. if(!syntax.curly[pos].flag) {
  1798. syntax.curly[pos].flag = 1;
  1799. *flag = 0;
  1800. return p;
  1801. }
  1802. }
  1803. }
  1804. // Close if
  1805. syntax.curly_count--;
  1806. // Put the label of the final location
  1807. sprintf(label,"__IF%x_FIN",syntax.curly[pos].index);
  1808. l=add_str(label);
  1809. set_label(l,script_pos,p);
  1810. if(syntax.curly[pos].flag == 1) {
  1811. // Because the position of the pointer is the same if not else for this
  1812. return bp;
  1813. }
  1814. return p;
  1815. } else if(syntax.curly[pos].type == TYPE_DO) {
  1816. int l2;
  1817. char label2[256];
  1818. const char *p2;
  1819. if(syntax.curly[pos].flag) {
  1820. // (Come here continue) to form the label here
  1821. sprintf(label2,"__DO%x_NXT",syntax.curly[pos].index);
  1822. l2=add_str(label2);
  1823. set_label(l2,script_pos,p);
  1824. }
  1825. // Skip to the end point if the condition is false
  1826. p = skip_space(p);
  1827. p2 = skip_word(p);
  1828. if(p2 - p != 5 || strncasecmp(p,"while",5))
  1829. disp_error_message("parse_syntax: expected 'while'",p);
  1830. p = skip_space(p2);
  1831. if(*p != '(') {
  1832. disp_error_message("need '('",p);
  1833. }
  1834. // do-block end is a new line
  1835. parse_nextline(false, p);
  1836. sprintf(label2,"__DO%x_FIN",syntax.curly[pos].index);
  1837. add_scriptl(add_str("jump_zero"));
  1838. add_scriptc(C_ARG);
  1839. p=parse_expr(p);
  1840. p=skip_space(p);
  1841. add_scriptl(add_str(label2));
  1842. add_scriptc(C_FUNC);
  1843. // Skip to the starting point
  1844. sprintf(label2,"goto __DO%x_BGN;",syntax.curly[pos].index);
  1845. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1846. parse_line(label2);
  1847. syntax.curly_count--;
  1848. // Form label of the end point conditions
  1849. sprintf(label2,"__DO%x_FIN",syntax.curly[pos].index);
  1850. l2=add_str(label2);
  1851. set_label(l2,script_pos,p);
  1852. p = skip_space(p);
  1853. if(*p != ';') {
  1854. disp_error_message("parse_syntax: expected ';'",p);
  1855. return p+1;
  1856. }
  1857. p++;
  1858. syntax.curly_count--;
  1859. return p;
  1860. } else if(syntax.curly[pos].type == TYPE_FOR) {
  1861. // for-block end is a new line
  1862. parse_nextline(false, p);
  1863. // Skip to the next loop
  1864. sprintf(label,"goto __FR%x_NXT;",syntax.curly[pos].index);
  1865. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1866. parse_line(label);
  1867. syntax.curly_count--;
  1868. // End for labeling
  1869. sprintf(label,"__FR%x_FIN",syntax.curly[pos].index);
  1870. l=add_str(label);
  1871. set_label(l,script_pos,p);
  1872. syntax.curly_count--;
  1873. return p;
  1874. } else if(syntax.curly[pos].type == TYPE_WHILE) {
  1875. // while-block end is a new line
  1876. parse_nextline(false, p);
  1877. // Skip to the decision while
  1878. sprintf(label,"goto __WL%x_NXT;",syntax.curly[pos].index);
  1879. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1880. parse_line(label);
  1881. syntax.curly_count--;
  1882. // End while labeling
  1883. sprintf(label,"__WL%x_FIN",syntax.curly[pos].index);
  1884. l=add_str(label);
  1885. set_label(l,script_pos,p);
  1886. syntax.curly_count--;
  1887. return p;
  1888. } else if(syntax.curly[syntax.curly_count-1].type == TYPE_USERFUNC) {
  1889. int pos2 = syntax.curly_count-1;
  1890. char label2[256];
  1891. int l2;
  1892. // Back
  1893. sprintf(label2,"return;");
  1894. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1895. parse_line(label2);
  1896. syntax.curly_count--;
  1897. // Put the label of the location
  1898. sprintf(label2,"__FN%x_FIN",syntax.curly[pos2].index);
  1899. l2=add_str(label2);
  1900. set_label(l2,script_pos,p);
  1901. syntax.curly_count--;
  1902. return p;
  1903. } else {
  1904. *flag = 0;
  1905. return p;
  1906. }
  1907. }
  1908. /*==========================================
  1909. * Added built-in functions
  1910. *------------------------------------------*/
  1911. static void add_buildin_func(void)
  1912. {
  1913. int i;
  1914. for( i = 0; buildin_func[i].func; i++ )
  1915. {
  1916. // arg must follow the pattern: (v|s|i|r|l)*\?*\*?
  1917. // 'v' - value (either string or int or reference)
  1918. // 's' - string
  1919. // 'i' - int
  1920. // 'r' - reference (of a variable)
  1921. // 'l' - label
  1922. // '?' - one optional parameter
  1923. // '*' - unknown number of optional parameters
  1924. const char* p = buildin_func[i].arg;
  1925. while( *p == 'v' || *p == 's' || *p == 'i' || *p == 'r' || *p == 'l' ) ++p;
  1926. while( *p == '?' ) ++p;
  1927. if( *p == '*' ) ++p;
  1928. if( *p != 0){
  1929. ShowWarning("add_buildin_func: ignoring function \"%s\" with invalid arg \"%s\".\n", buildin_func[i].name, buildin_func[i].arg);
  1930. } else if( *skip_word(buildin_func[i].name) != 0 ){
  1931. ShowWarning("add_buildin_func: ignoring function with invalid name \"%s\" (must be a word).\n", buildin_func[i].name);
  1932. } else {
  1933. int n = add_str(buildin_func[i].name);
  1934. str_data[n].type = C_FUNC;
  1935. str_data[n].val = i;
  1936. str_data[n].func = buildin_func[i].func;
  1937. if (!strcmp(buildin_func[i].name, "set")) buildin_set_ref = n;
  1938. else if (!strcmp(buildin_func[i].name, "callsub")) buildin_callsub_ref = n;
  1939. else if (!strcmp(buildin_func[i].name, "callfunc")) buildin_callfunc_ref = n;
  1940. else if( !strcmp(buildin_func[i].name, "getelementofarray") ) buildin_getelementofarray_ref = n;
  1941. }
  1942. }
  1943. }
  1944. /// Retrieves the value of a constant.
  1945. bool script_get_constant(const char* name, int* value)
  1946. {
  1947. int n = search_str(name);
  1948. if( n == -1 || str_data[n].type != C_INT )
  1949. {// not found or not a constant
  1950. return false;
  1951. }
  1952. value[0] = str_data[n].val;
  1953. return true;
  1954. }
  1955. /// Creates new constant or parameter with given value.
  1956. void script_set_constant(const char* name, int value, bool isparameter)
  1957. {
  1958. int n = add_str(name);
  1959. if( str_data[n].type == C_NOP )
  1960. {// new
  1961. str_data[n].type = isparameter ? C_PARAM : C_INT;
  1962. str_data[n].val = value;
  1963. }
  1964. else if( str_data[n].type == C_PARAM || str_data[n].type == C_INT )
  1965. {// existing parameter or constant
  1966. 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);
  1967. }
  1968. else
  1969. {// existing name
  1970. ShowError("script_set_constant: Invalid name for %s '%s' (already defined as %s).\n", isparameter ? "parameter" : "constant", name, script_op2name(str_data[n].type));
  1971. }
  1972. }
  1973. /*==========================================
  1974. * Reading constant databases
  1975. * const.txt
  1976. *------------------------------------------*/
  1977. static void read_constdb(void)
  1978. {
  1979. FILE *fp;
  1980. char line[1024],name[1024],val[1024];
  1981. int type;
  1982. int entries=0, skipped=0, linenum=0;
  1983. sprintf(line, "%s/const.txt", db_path);
  1984. fp=fopen(line, "r");
  1985. if(fp==NULL){
  1986. ShowError("can't read %s\n", line);
  1987. return ;
  1988. }
  1989. while(fgets(line, sizeof(line), fp))
  1990. {
  1991. linenum++;
  1992. if( line[0] == '\0' || line[0] == '\n' || line[0] == '\r') //ignore empty line
  1993. continue;
  1994. if(line[0]=='/' && line[1]=='/') //ignore commented line
  1995. continue;
  1996. type=0;
  1997. if(sscanf(line,"%1023[A-Za-z0-9/_],%1023[A-Za-z0-9/_-],%11d",name,val,&type)>=2 ||
  1998. sscanf(line,"%1023[A-Za-z0-9/_] %1023[A-Za-z0-9/_-] %11d",name,val,&type)>=2){
  1999. entries++;
  2000. script_set_constant(name, (int)strtol(val, NULL, 0), (bool)type);
  2001. }
  2002. else {
  2003. skipped++;
  2004. ShowWarning("Skipping line '"CL_WHITE"%d"CL_RESET"', invalide constant definition\n",linenum);
  2005. }
  2006. }
  2007. fclose(fp);
  2008. ShowStatus("Done reading '"CL_WHITE"%d"CL_RESET"' entries in '"CL_WHITE"%s/const.txt"CL_RESET"'.\n", entries, db_path);
  2009. if(skipped){
  2010. ShowWarning("Skipped '"CL_WHITE"%d"CL_RESET"', entries\n",skipped);
  2011. }
  2012. }
  2013. /**
  2014. * Sets source-end constants for NPC scripts to access.
  2015. **/
  2016. void script_hardcoded_constants(void) {
  2017. #include "script_constants.h"
  2018. }
  2019. /*==========================================
  2020. * Display emplacement line of script
  2021. *------------------------------------------*/
  2022. static const char* script_print_line(StringBuf* buf, const char* p, const char* mark, int line)
  2023. {
  2024. int i;
  2025. if( p == NULL || !p[0] ) return NULL;
  2026. if( line < 0 )
  2027. StringBuf_Printf(buf, "*% 5d : ", -line);
  2028. else
  2029. StringBuf_Printf(buf, " % 5d : ", line);
  2030. for(i=0;p[i] && p[i] != '\n';i++){
  2031. if(p + i != mark)
  2032. StringBuf_Printf(buf, "%c", p[i]);
  2033. else
  2034. StringBuf_Printf(buf, "\'%c\'", p[i]);
  2035. }
  2036. StringBuf_AppendStr(buf, "\n");
  2037. return p+i+(p[i] == '\n' ? 1 : 0);
  2038. }
  2039. 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) {
  2040. // Find the line where the error occurred
  2041. int j;
  2042. int line = start_line;
  2043. const char *p;
  2044. const char *linestart[5] = { NULL, NULL, NULL, NULL, NULL };
  2045. for(p=src;p && *p;line++){
  2046. const char *lineend=strchr(p,'\n');
  2047. if(lineend==NULL || error_pos_cur<lineend){
  2048. break;
  2049. }
  2050. for( j = 0; j < 4; j++ ) {
  2051. linestart[j] = linestart[j+1];
  2052. }
  2053. linestart[4] = p;
  2054. p=lineend+1;
  2055. }
  2056. StringBuf_Printf(buf, "script error on %s line %d\n", file, line);
  2057. StringBuf_Printf(buf, " %s\n", error_msg_cur);
  2058. for(j = 0; j < 5; j++ ) {
  2059. script_print_line(buf, linestart[j], NULL, line + j - 5);
  2060. }
  2061. p = script_print_line(buf, p, error_pos_cur, -line);
  2062. for(j = 0; j < 5; j++) {
  2063. p = script_print_line(buf, p, NULL, line + j + 1 );
  2064. }
  2065. }
  2066. void script_error(const char* src, const char* file, int start_line, const char* error_msg_cur, const char* error_pos_cur) {
  2067. StringBuf buf;
  2068. StringBuf_Init(&buf);
  2069. StringBuf_AppendStr(&buf, "\a\n");
  2070. script_errorwarning_sub(&buf, src, file, start_line, error_msg_cur, error_pos_cur);
  2071. ShowError("%s", StringBuf_Value(&buf));
  2072. StringBuf_Destroy(&buf);
  2073. }
  2074. void script_warning(const char* src, const char* file, int start_line, const char* error_msg_cur, const char* error_pos_cur) {
  2075. StringBuf buf;
  2076. StringBuf_Init(&buf);
  2077. script_errorwarning_sub(&buf, src, file, start_line, error_msg_cur, error_pos_cur);
  2078. ShowWarning("%s", StringBuf_Value(&buf));
  2079. StringBuf_Destroy(&buf);
  2080. }
  2081. /*==========================================
  2082. * Analysis of the script
  2083. *------------------------------------------*/
  2084. struct script_code* parse_script(const char *src,const char *file,int line,int options)
  2085. {
  2086. const char *p,*tmpp;
  2087. int i;
  2088. struct script_code* code = NULL;
  2089. static int first=1;
  2090. char end;
  2091. bool unresolved_names = false;
  2092. parser_current_src = src;
  2093. parser_current_file = file;
  2094. parser_current_line = line;
  2095. if( src == NULL )
  2096. return NULL;// empty script
  2097. memset(&syntax,0,sizeof(syntax));
  2098. if(first){
  2099. add_buildin_func();
  2100. read_constdb();
  2101. script_hardcoded_constants();
  2102. first=0;
  2103. }
  2104. script_buf=(unsigned char *)aMalloc(SCRIPT_BLOCK_SIZE*sizeof(unsigned char));
  2105. script_pos=0;
  2106. script_size=SCRIPT_BLOCK_SIZE;
  2107. parse_nextline(true, NULL);
  2108. // who called parse_script is responsible for clearing the database after using it, but just in case... lets clear it here
  2109. if( options&SCRIPT_USE_LABEL_DB )
  2110. db_clear(scriptlabel_db);
  2111. parse_options = options;
  2112. if( setjmp( error_jump ) != 0 ) {
  2113. //Restore program state when script has problems. [from jA]
  2114. int j;
  2115. const int size = ARRAYLENGTH(syntax.curly);
  2116. if( error_report )
  2117. script_error(src,file,line,error_msg,error_pos);
  2118. aFree( error_msg );
  2119. aFree( script_buf );
  2120. script_pos = 0;
  2121. script_size = 0;
  2122. script_buf = NULL;
  2123. for(j=LABEL_START;j<str_num;j++)
  2124. if(str_data[j].type == C_NOP) str_data[j].type = C_NAME;
  2125. for(j=0; j<size; j++)
  2126. linkdb_final(&syntax.curly[j].case_label);
  2127. return NULL;
  2128. }
  2129. parse_syntax_for_flag=0;
  2130. p=src;
  2131. p=skip_space(p);
  2132. if( options&SCRIPT_IGNORE_EXTERNAL_BRACKETS )
  2133. {// does not require brackets around the script
  2134. if( *p == '\0' && !(options&SCRIPT_RETURN_EMPTY_SCRIPT) )
  2135. {// empty script and can return NULL
  2136. aFree( script_buf );
  2137. script_pos = 0;
  2138. script_size = 0;
  2139. script_buf = NULL;
  2140. return NULL;
  2141. }
  2142. end = '\0';
  2143. }
  2144. else
  2145. {// requires brackets around the script
  2146. if( *p != '{' )
  2147. disp_error_message("not found '{'",p);
  2148. p = skip_space(p+1);
  2149. if( *p == '}' && !(options&SCRIPT_RETURN_EMPTY_SCRIPT) )
  2150. {// empty script and can return NULL
  2151. aFree( script_buf );
  2152. script_pos = 0;
  2153. script_size = 0;
  2154. script_buf = NULL;
  2155. return NULL;
  2156. }
  2157. end = '}';
  2158. }
  2159. // clear references of labels, variables and internal functions
  2160. for(i=LABEL_START;i<str_num;i++){
  2161. if(
  2162. str_data[i].type==C_POS || str_data[i].type==C_NAME ||
  2163. str_data[i].type==C_USERFUNC || str_data[i].type == C_USERFUNC_POS
  2164. ){
  2165. str_data[i].type=C_NOP;
  2166. str_data[i].backpatch=-1;
  2167. str_data[i].label=-1;
  2168. }
  2169. }
  2170. while( syntax.curly_count != 0 || *p != end )
  2171. {
  2172. if( *p == '\0' )
  2173. disp_error_message("unexpected end of script",p);
  2174. // Special handling only label
  2175. tmpp=skip_space(skip_word(p));
  2176. if(*tmpp==':' && !(!strncasecmp(p,"default:",8) && p + 7 == tmpp)){
  2177. i=add_word(p);
  2178. set_label(i,script_pos,p);
  2179. if( parse_options&SCRIPT_USE_LABEL_DB )
  2180. strdb_iput(scriptlabel_db, get_str(i), script_pos);
  2181. p=tmpp+1;
  2182. p=skip_space(p);
  2183. continue;
  2184. }
  2185. // All other lumped
  2186. p=parse_line(p);
  2187. p=skip_space(p);
  2188. parse_nextline(false, p);
  2189. }
  2190. add_scriptc(C_NOP);
  2191. // trim code to size
  2192. script_size = script_pos;
  2193. RECREATE(script_buf,unsigned char,script_pos);
  2194. // default unknown references to variables
  2195. for(i=LABEL_START;i<str_num;i++){
  2196. if(str_data[i].type==C_NOP){
  2197. int j;
  2198. str_data[i].type=C_NAME;
  2199. str_data[i].label=i;
  2200. for(j=str_data[i].backpatch;j>=0 && j!=0x00ffffff;){
  2201. int next=GETVALUE(script_buf,j);
  2202. SETVALUE(script_buf,j,i);
  2203. j=next;
  2204. }
  2205. }
  2206. else if( str_data[i].type == C_USERFUNC )
  2207. {// 'function name;' without follow-up code
  2208. ShowError("parse_script: function '%s' declared but not defined.\n", str_buf+str_data[i].str);
  2209. unresolved_names = true;
  2210. }
  2211. }
  2212. if( unresolved_names )
  2213. {
  2214. disp_error_message("parse_script: unresolved function references", p);
  2215. }
  2216. #ifdef DEBUG_DISP
  2217. for(i=0;i<script_pos;i++){
  2218. if((i&15)==0) ShowMessage("%04x : ",i);
  2219. ShowMessage("%02x ",script_buf[i]);
  2220. if((i&15)==15) ShowMessage("\n");
  2221. }
  2222. ShowMessage("\n");
  2223. #endif
  2224. #ifdef DEBUG_DISASM
  2225. {
  2226. int i = 0,j;
  2227. while(i < script_pos) {
  2228. c_op op = get_com(script_buf,&i);
  2229. ShowMessage("%06x %s", i, script_op2name(op));
  2230. j = i;
  2231. switch(op) {
  2232. case C_INT:
  2233. ShowMessage(" %d", get_num(script_buf,&i));
  2234. break;
  2235. case C_POS:
  2236. ShowMessage(" 0x%06x", *(int*)(script_buf+i)&0xffffff);
  2237. i += 3;
  2238. break;
  2239. case C_NAME:
  2240. j = (*(int*)(script_buf+i)&0xffffff);
  2241. ShowMessage(" %s", ( j == 0xffffff ) ? "?? unknown ??" : get_str(j));
  2242. i += 3;
  2243. break;
  2244. case C_STR:
  2245. j = strlen(script_buf + i);
  2246. ShowMessage(" %s", script_buf + i);
  2247. i += j+1;
  2248. break;
  2249. }
  2250. ShowMessage(CL_CLL"\n");
  2251. }
  2252. }
  2253. #endif
  2254. CREATE(code,struct script_code,1);
  2255. code->script_buf = script_buf;
  2256. code->script_size = script_size;
  2257. code->local.vars = NULL;
  2258. code->local.arrays = NULL;
  2259. return code;
  2260. }
  2261. /// Returns the player attached to this script, identified by the rid.
  2262. /// If there is no player attached, the script is terminated.
  2263. TBL_PC *script_rid2sd(struct script_state *st)
  2264. {
  2265. TBL_PC *sd=map_id2sd(st->rid);
  2266. if(!sd){
  2267. ShowError("script_rid2sd: fatal error ! player not attached!\n");
  2268. script_reportfunc(st);
  2269. script_reportsrc(st);
  2270. st->state = END;
  2271. }
  2272. return sd;
  2273. }
  2274. /**
  2275. * Dereferences a variable/constant, replacing it with a copy of the value.
  2276. * @param st Script state
  2277. * @param data Variable/constant
  2278. * @param sd If NULL, will try to use sd from st->rid (for player's variables)
  2279. */
  2280. void get_val_(struct script_state* st, struct script_data* data, struct map_session_data *sd)
  2281. {
  2282. const char* name;
  2283. char prefix;
  2284. char postfix;
  2285. if( !data_isreference(data) )
  2286. return;// not a variable/constant
  2287. name = reference_getname(data);
  2288. prefix = name[0];
  2289. postfix = name[strlen(name) - 1];
  2290. //##TODO use reference_tovariable(data) when it's confirmed that it works [FlavioJS]
  2291. if( !reference_toconstant(data) && not_server_variable(prefix) ) {
  2292. if (sd == NULL)
  2293. sd = script_rid2sd(st);
  2294. if( sd == NULL ) {// needs player attached
  2295. if( postfix == '$' ) {// string variable
  2296. ShowWarning("script:get_val: cannot access player variable '%s', defaulting to \"\"\n", name);
  2297. data->type = C_CONSTSTR;
  2298. data->u.str = "";
  2299. } else {// integer variable
  2300. ShowWarning("script:get_val: cannot access player variable '%s', defaulting to 0\n", name);
  2301. data->type = C_INT;
  2302. data->u.num = 0;
  2303. }
  2304. return;
  2305. }
  2306. }
  2307. if( postfix == '$' ) {// string variable
  2308. switch( prefix ) {
  2309. case '@':
  2310. data->u.str = pc_readregstr(sd, data->u.num);
  2311. break;
  2312. case '$':
  2313. data->u.str = mapreg_readregstr(data->u.num);
  2314. break;
  2315. case '#':
  2316. if( name[1] == '#' )
  2317. data->u.str = pc_readaccountreg2str(sd, data->u.num);// global
  2318. else
  2319. data->u.str = pc_readaccountregstr(sd, data->u.num);// local
  2320. break;
  2321. case '.':
  2322. {
  2323. struct DBMap* n = data->ref ?
  2324. data->ref->vars : name[1] == '@' ?
  2325. st->stack->scope.vars : // instance/scope variable
  2326. st->script->local.vars; // npc variable
  2327. if( n )
  2328. data->u.str = (char*)i64db_get(n,reference_getuid(data));
  2329. else
  2330. data->u.str = NULL;
  2331. }
  2332. break;
  2333. case '\'':
  2334. {
  2335. int instance_id = script_instancegetid(st);
  2336. if( instance_id )
  2337. data->u.str = (char*)i64db_get(instance_data[instance_id].regs.vars,reference_getuid(data));
  2338. else {
  2339. ShowWarning("script:get_val: cannot access instance variable '%s', defaulting to \"\"\n", name);
  2340. data->u.str = NULL;
  2341. }
  2342. }
  2343. break;
  2344. default:
  2345. data->u.str = pc_readglobalreg_str(sd, data->u.num);
  2346. break;
  2347. }
  2348. if( data->u.str == NULL || data->u.str[0] == '\0' ) {// empty string
  2349. data->type = C_CONSTSTR;
  2350. data->u.str = "";
  2351. } else {// duplicate string
  2352. data->type = C_STR;
  2353. data->u.str = aStrdup(data->u.str);
  2354. }
  2355. } else {// integer variable
  2356. data->type = C_INT;
  2357. if( reference_toconstant(data) ) {
  2358. data->u.num = reference_getconstant(data);
  2359. } else if( reference_toparam(data) ) {
  2360. data->u.num = pc_readparam(sd, reference_getparamtype(data));
  2361. } else
  2362. switch( prefix ) {
  2363. case '@':
  2364. data->u.num = pc_readreg(sd, data->u.num);
  2365. break;
  2366. case '$':
  2367. data->u.num = mapreg_readreg(data->u.num);
  2368. break;
  2369. case '#':
  2370. if( name[1] == '#' )
  2371. data->u.num = pc_readaccountreg2(sd, data->u.num);// global
  2372. else
  2373. data->u.num = pc_readaccountreg(sd, data->u.num);// local
  2374. break;
  2375. case '.':
  2376. {
  2377. struct DBMap* n = data->ref ?
  2378. data->ref->vars : name[1] == '@' ?
  2379. st->stack->scope.vars : // instance/scope variable
  2380. st->script->local.vars; // npc variable
  2381. if( n )
  2382. data->u.num = (int)i64db_iget(n,reference_getuid(data));
  2383. else
  2384. data->u.num = 0;
  2385. }
  2386. break;
  2387. case '\'':
  2388. {
  2389. int instance_id = script_instancegetid(st);
  2390. if( instance_id )
  2391. data->u.num = (int)i64db_iget(instance_data[instance_id].regs.vars,reference_getuid(data));
  2392. else {
  2393. ShowWarning("script:get_val: cannot access instance variable '%s', defaulting to 0\n", name);
  2394. data->u.num = 0;
  2395. }
  2396. }
  2397. break;
  2398. default:
  2399. data->u.num = pc_readglobalreg(sd, data->u.num);
  2400. break;
  2401. }
  2402. }
  2403. data->ref = NULL;
  2404. return;
  2405. }
  2406. void get_val(struct script_state* st, struct script_data* data)
  2407. {
  2408. get_val_(st,data,NULL);
  2409. }
  2410. struct script_data* push_val2(struct script_stack* stack, enum c_op type, int64 val, struct reg_db* ref);
  2411. /// Retrieves the value of a reference identified by uid (variable, constant, param)
  2412. /// The value is left in the top of the stack and needs to be removed manually.
  2413. /// @param st[in]: script state.
  2414. /// @param uid[in]: reference identifier.
  2415. /// @param ref[in]: the container to look up the reference into.
  2416. /// @return: the retrieved value of the reference.
  2417. void* get_val2(struct script_state* st, int64 uid, struct reg_db *ref)
  2418. {
  2419. struct script_data* data;
  2420. push_val2(st->stack, C_NAME, uid, ref);
  2421. data = script_getdatatop(st, -1);
  2422. get_val(st, data);
  2423. //! TODO: Support data->u.num as int64 instead cast it to int? (in get_val, it was casted to int).
  2424. return (data->type == C_INT ? (void*)__64BPRTSIZE((int)data->u.num) : (void*)__64BPRTSIZE(data->u.str));
  2425. }
  2426. /**
  2427. * Because, currently, array members with key 0 are indifferenciable from normal variables, we should ensure its actually in
  2428. * Will be gone as soon as undefined var feature is implemented
  2429. **/
  2430. void script_array_ensure_zero(struct script_state *st, struct map_session_data *sd, int64 uid, struct reg_db *ref)
  2431. {
  2432. const char *name = get_str(script_getvarid(uid));
  2433. // is here st can be null pointer and st->rid is wrong?
  2434. struct reg_db *src = script_array_src(st, sd ? sd : st->rid ? map_id2sd(st->rid) : NULL, name, ref);
  2435. bool insert = false;
  2436. if (sd && !st) {
  2437. // when sd comes, st isn't available
  2438. insert = true;
  2439. } else {
  2440. if( is_string_variable(name) ) {
  2441. char* str = (char*)get_val2(st, uid, ref);
  2442. if( str && *str )
  2443. insert = true;
  2444. script_removetop(st, -1, 0);
  2445. } else {
  2446. int32 num = (int32)__64BPRTSIZE(get_val2(st, uid, ref));
  2447. if( num )
  2448. insert = true;
  2449. script_removetop(st, -1, 0);
  2450. }
  2451. }
  2452. if (src && src->arrays) {
  2453. struct script_array *sa = idb_get(src->arrays, script_getvarid(uid));
  2454. if (sa) {
  2455. unsigned int i;
  2456. ARR_FIND(0, sa->size, i, sa->members[i] == 0);
  2457. if( i != sa->size ) {
  2458. if( !insert )
  2459. script_array_remove_member(src,sa,i);
  2460. return;
  2461. }
  2462. script_array_add_member(sa,0);
  2463. } else if (insert) {
  2464. script_array_update(src,reference_uid(script_getvarid(uid), 0),false);
  2465. }
  2466. }
  2467. }
  2468. /**
  2469. * Returns array size by ID
  2470. **/
  2471. unsigned int script_array_size(struct script_state *st, struct map_session_data *sd, const char *name, struct reg_db *ref)
  2472. {
  2473. struct script_array *sa = NULL;
  2474. struct reg_db *src = script_array_src(st, sd, name, ref);
  2475. if (src && src->arrays)
  2476. sa = idb_get(src->arrays, search_str(name));
  2477. return sa ? sa->size : 0;
  2478. }
  2479. /**
  2480. * Returns array's highest key (for that awful getarraysize implementation that doesn't really gets the array size)
  2481. **/
  2482. unsigned int script_array_highest_key(struct script_state *st, struct map_session_data *sd, const char *name, struct reg_db *ref)
  2483. {
  2484. struct script_array *sa = NULL;
  2485. struct reg_db *src = script_array_src(st, sd, name, ref);
  2486. if (src && src->arrays) {
  2487. int key = add_word(name);
  2488. script_array_ensure_zero(st,sd,reference_uid(key, 0), ref);
  2489. if( ( sa = idb_get(src->arrays, key) ) ) {
  2490. unsigned int i, highest_key = 0;
  2491. for(i = 0; i < sa->size; i++) {
  2492. if( sa->members[i] > highest_key )
  2493. highest_key = sa->members[i];
  2494. }
  2495. return sa->size ? highest_key + 1 : 0;
  2496. }
  2497. }
  2498. return SCRIPT_CMD_SUCCESS;
  2499. }
  2500. int script_free_array_db(DBKey key, DBData *data, va_list ap)
  2501. {
  2502. struct script_array *sa = db_data2ptr(data);
  2503. aFree(sa->members);
  2504. ers_free(array_ers, sa);
  2505. return SCRIPT_CMD_SUCCESS;
  2506. }
  2507. /**
  2508. * Clears script_array and removes it from script->array_db
  2509. **/
  2510. void script_array_delete(struct reg_db *src, struct script_array *sa)
  2511. {
  2512. aFree(sa->members);
  2513. idb_remove(src->arrays, sa->id);
  2514. ers_free(array_ers, sa);
  2515. }
  2516. /**
  2517. * Removes a member from a script_array list
  2518. *
  2519. * @param idx the index of the member in script_array struct list, not of the actual array member
  2520. **/
  2521. void script_array_remove_member(struct reg_db *src, struct script_array *sa, unsigned int idx)
  2522. {
  2523. unsigned int i, cursor;
  2524. // it's the only member left, no need to do anything other than delete the array data
  2525. if( sa->size == 1 ) {
  2526. script_array_delete(src,sa);
  2527. return;
  2528. }
  2529. sa->members[idx] = UINT_MAX;
  2530. for(i = 0, cursor = 0; i < sa->size; i++) {
  2531. if( sa->members[i] == UINT_MAX )
  2532. continue;
  2533. if( i != cursor )
  2534. sa->members[cursor] = sa->members[i];
  2535. cursor++;
  2536. }
  2537. sa->size = cursor;
  2538. }
  2539. /**
  2540. * Appends a new array index to the list in script_array
  2541. *
  2542. * @param idx the index of the array member being inserted
  2543. **/
  2544. void script_array_add_member(struct script_array *sa, unsigned int idx)
  2545. {
  2546. RECREATE(sa->members, unsigned int, ++sa->size);
  2547. sa->members[sa->size - 1] = idx;
  2548. }
  2549. /**
  2550. * Obtains the source of the array database for this type and scenario
  2551. * Initializes such database when not yet initialized.
  2552. **/
  2553. struct reg_db *script_array_src(struct script_state *st, struct map_session_data *sd, const char *name, struct reg_db *ref)
  2554. {
  2555. struct reg_db *src = NULL;
  2556. switch( name[0] ) {
  2557. // from player
  2558. default: // char reg
  2559. case '@': // temp char reg
  2560. case '#': // account reg
  2561. src = &sd->regs;
  2562. break;
  2563. case '$': // map reg
  2564. src = &regs;
  2565. break;
  2566. case '.': // npc/script
  2567. if (ref)
  2568. src = ref;
  2569. else
  2570. src = (name[1] == '@') ? &st->stack->scope : &st->script->local;
  2571. break;
  2572. case '\'': // instance
  2573. {
  2574. int instance_id = script_instancegetid(st);
  2575. if( instance_id ) {
  2576. src = &instance_data[instance_id].regs;
  2577. }
  2578. break;
  2579. }
  2580. }
  2581. if( src ) {
  2582. if( !src->arrays )
  2583. src->arrays = idb_alloc(DB_OPT_BASE);
  2584. return src;
  2585. }
  2586. return NULL;
  2587. }
  2588. /**
  2589. * Processes a array member modification, and update data accordingly
  2590. *
  2591. * @param src[in,out] Variable source database. If the array database doesn't exist, it is created.
  2592. * @param num[in] Variable ID
  2593. * @param empty[in] Whether the modified member is empty (needs to be removed)
  2594. **/
  2595. void script_array_update(struct reg_db *src, int64 num, bool empty)
  2596. {
  2597. struct script_array *sa = NULL;
  2598. int id = script_getvarid(num);
  2599. unsigned int index = script_getvaridx(num);
  2600. if (!src->arrays) {
  2601. src->arrays = idb_alloc(DB_OPT_BASE);
  2602. } else {
  2603. sa = idb_get(src->arrays, id);
  2604. }
  2605. if( sa ) {
  2606. unsigned int i;
  2607. // search
  2608. for(i = 0; i < sa->size; i++) {
  2609. if( sa->members[i] == index )
  2610. break;
  2611. }
  2612. // if existent
  2613. if( i != sa->size ) {
  2614. // if empty, we gotta remove it
  2615. if( empty ) {
  2616. script_array_remove_member(src, sa, i);
  2617. }
  2618. } else if( !empty ) { /* new entry */
  2619. script_array_add_member(sa,index);
  2620. // we do nothing if its empty, no point in modifying array data for a new empty member
  2621. }
  2622. } else if ( !empty ) { // we only move to create if not empty
  2623. sa = ers_alloc(array_ers, struct script_array);
  2624. sa->id = id;
  2625. sa->members = NULL;
  2626. sa->size = 0;
  2627. script_array_add_member(sa,index);
  2628. idb_put(src->arrays, id, sa);
  2629. }
  2630. }
  2631. /**
  2632. * Stores the value of a script variable
  2633. *
  2634. * @param st: current script state.
  2635. * @param sd: current character data.
  2636. * @param num: variable identifier.
  2637. * @param name: variable name.
  2638. * @param value: new value.
  2639. * @param ref: variable container, in case of a npc/scope variable reference outside the current scope.
  2640. * @return: 0 failure, 1 success.
  2641. *
  2642. * TODO: return values are screwed up, have been for some time (reaad: years), e.g. some functions return 1 failure and success.
  2643. *------------------------------------------*/
  2644. int set_reg(struct script_state* st, TBL_PC* sd, int64 num, const char* name, const void* value, struct reg_db *ref)
  2645. {
  2646. char prefix = name[0];
  2647. if( is_string_variable(name) ) {// string variable
  2648. const char *str = (const char*)value;
  2649. switch (prefix) {
  2650. case '@':
  2651. pc_setregstr(sd, num, str);
  2652. return 1;
  2653. case '$':
  2654. return mapreg_setregstr(num, str);
  2655. case '#':
  2656. return (name[1] == '#') ?
  2657. pc_setaccountreg2str(sd, num, str) :
  2658. pc_setaccountregstr(sd, num, str);
  2659. case '.':
  2660. {
  2661. struct reg_db *n = (ref) ? ref : (name[1] == '@') ? &st->stack->scope : &st->script->local;
  2662. if( n ) {
  2663. if (str[0]) {
  2664. i64db_put(n->vars, num, aStrdup(str));
  2665. if( script_getvaridx(num) )
  2666. script_array_update(n, num, false);
  2667. } else {
  2668. i64db_remove(n->vars, num);
  2669. if( script_getvaridx(num) )
  2670. script_array_update(n, num, true);
  2671. }
  2672. }
  2673. }
  2674. return 1;
  2675. case '\'':
  2676. {
  2677. int instance_id = script_instancegetid(st);
  2678. if( instance_id ) {
  2679. if( str[0] ) {
  2680. i64db_put(instance_data[instance_id].regs.vars, num, aStrdup(str));
  2681. if( script_getvaridx(num) )
  2682. script_array_update(&instance_data[instance_id].regs, num, false);
  2683. } else {
  2684. i64db_remove(instance_data[instance_id].regs.vars, num);
  2685. if (script_getvaridx(num))
  2686. script_array_update(&instance_data[instance_id].regs, num, true);
  2687. }
  2688. } else {
  2689. ShowError("script_set_reg: cannot write instance variable '%s', NPC not in a instance!\n", name);
  2690. script_reportsrc(st);
  2691. }
  2692. return 1;
  2693. }
  2694. default:
  2695. return pc_setglobalreg_str(sd, num, str);
  2696. }
  2697. } else {// integer variable
  2698. int val = (int)__64BPRTSIZE(value);
  2699. if(str_data[script_getvarid(num)].type == C_PARAM) {
  2700. if( pc_setparam(sd, str_data[script_getvarid(num)].val, val) == 0 ) {
  2701. if( st != NULL ) {
  2702. ShowError("script_set_reg: failed to set param '%s' to %d.\n", name, val);
  2703. script_reportsrc(st);
  2704. st->state = END;
  2705. }
  2706. return 0;
  2707. }
  2708. return 1;
  2709. }
  2710. switch (prefix) {
  2711. case '@':
  2712. pc_setreg(sd, num, val);
  2713. return 1;
  2714. case '$':
  2715. return mapreg_setreg(num, val);
  2716. case '#':
  2717. return (name[1] == '#') ?
  2718. pc_setaccountreg2(sd, num, val) :
  2719. pc_setaccountreg(sd, num, val);
  2720. case '.':
  2721. {
  2722. struct reg_db *n = (ref) ? ref : (name[1] == '@') ? &st->stack->scope : &st->script->local;
  2723. if( n ) {
  2724. if( val != 0 ) {
  2725. i64db_iput(n->vars, num, val);
  2726. if( script_getvaridx(num) )
  2727. script_array_update(n, num, false);
  2728. } else {
  2729. i64db_remove(n->vars, num);
  2730. if( script_getvaridx(num) )
  2731. script_array_update(n, num, true);
  2732. }
  2733. }
  2734. }
  2735. return 1;
  2736. case '\'':
  2737. {
  2738. int instance_id = script_instancegetid(st);
  2739. if( instance_id ) {
  2740. if( val != 0 ) {
  2741. i64db_iput(instance_data[instance_id].regs.vars, num, val);
  2742. if( script_getvaridx(num) )
  2743. script_array_update(&instance_data[instance_id].regs, num, false);
  2744. } else {
  2745. i64db_remove(instance_data[instance_id].regs.vars, num);
  2746. if (script_getvaridx(num))
  2747. script_array_update(&instance_data[instance_id].regs, num, true);
  2748. }
  2749. } else {
  2750. ShowError("script_set_reg: cannot write instance variable '%s', NPC not in a instance!\n", name);
  2751. script_reportsrc(st);
  2752. }
  2753. return 1;
  2754. }
  2755. default:
  2756. return pc_setglobalreg(sd, num, val);
  2757. }
  2758. }
  2759. }
  2760. int set_var(TBL_PC* sd, char* name, void* val)
  2761. {
  2762. return set_reg(NULL, sd, reference_uid(add_str(name),0), name, val, NULL);
  2763. }
  2764. void setd_sub(struct script_state *st, TBL_PC *sd, const char *varname, int elem, void *value, struct reg_db *ref)
  2765. {
  2766. set_reg(st, sd, reference_uid(add_str(varname),elem), varname, value, ref);
  2767. }
  2768. /**
  2769. * Converts the data to a string
  2770. * @param st
  2771. * @param data
  2772. * @param sd
  2773. */
  2774. const char* conv_str_(struct script_state* st, struct script_data* data, struct map_session_data *sd)
  2775. {
  2776. char* p;
  2777. get_val_(st, data, sd);
  2778. if( data_isstring(data) )
  2779. {// nothing to convert
  2780. }
  2781. else if( data_isint(data) )
  2782. {// int -> string
  2783. CREATE(p, char, ITEM_NAME_LENGTH);
  2784. snprintf(p, ITEM_NAME_LENGTH, "%"PRId64"", data->u.num);
  2785. p[ITEM_NAME_LENGTH-1] = '\0';
  2786. data->type = C_STR;
  2787. data->u.str = p;
  2788. }
  2789. else if( data_isreference(data) )
  2790. {// reference -> string
  2791. //##TODO when does this happen (check get_val) [FlavioJS]
  2792. data->type = C_CONSTSTR;
  2793. data->u.str = reference_getname(data);
  2794. }
  2795. else
  2796. {// unsupported data type
  2797. ShowError("script:conv_str: cannot convert to string, defaulting to \"\"\n");
  2798. script_reportdata(data);
  2799. script_reportsrc(st);
  2800. data->type = C_CONSTSTR;
  2801. data->u.str = "";
  2802. }
  2803. return data->u.str;
  2804. }
  2805. const char* conv_str(struct script_state* st, struct script_data* data)
  2806. {
  2807. return conv_str_(st, data, NULL);
  2808. }
  2809. /**
  2810. * Converts the data to an int
  2811. * @param st
  2812. * @param data
  2813. * @param sd
  2814. */
  2815. int conv_num_(struct script_state* st, struct script_data* data, struct map_session_data *sd)
  2816. {
  2817. get_val_(st, data, sd);
  2818. if( data_isint(data) )
  2819. {// nothing to convert
  2820. }
  2821. else if( data_isstring(data) )
  2822. {// string -> int
  2823. // the result does not overflow or underflow, it is capped instead
  2824. // ex: 999999999999 is capped to INT_MAX (2147483647)
  2825. char* p = data->u.str;
  2826. long num;
  2827. errno = 0;
  2828. num = strtol(data->u.str, NULL, 10);// change radix to 0 to support octal numbers "o377" and hex numbers "0xFF"
  2829. if( errno == ERANGE
  2830. #if LONG_MAX > INT_MAX
  2831. || num < INT_MIN || num > INT_MAX
  2832. #endif
  2833. )
  2834. {
  2835. if( num <= INT_MIN )
  2836. {
  2837. num = INT_MIN;
  2838. ShowError("script:conv_num: underflow detected, capping to %ld\n", num);
  2839. }
  2840. else//if( num >= INT_MAX )
  2841. {
  2842. num = INT_MAX;
  2843. ShowError("script:conv_num: overflow detected, capping to %ld\n", num);
  2844. }
  2845. script_reportdata(data);
  2846. script_reportsrc(st);
  2847. }
  2848. if( data->type == C_STR )
  2849. aFree(p);
  2850. data->type = C_INT;
  2851. data->u.num = (int)num;
  2852. }
  2853. #if 0
  2854. // FIXME this function is being used to retrieve the position of labels and
  2855. // probably other stuff [FlavioJS]
  2856. else
  2857. {// unsupported data type
  2858. ShowError("script:conv_num: cannot convert to number, defaulting to 0\n");
  2859. script_reportdata(data);
  2860. script_reportsrc(st);
  2861. data->type = C_INT;
  2862. data->u.num = 0;
  2863. }
  2864. #endif
  2865. return (int)data->u.num;
  2866. }
  2867. int conv_num(struct script_state* st, struct script_data* data)
  2868. {
  2869. return conv_num_(st, data, NULL);
  2870. }
  2871. //
  2872. // Stack operations
  2873. //
  2874. /// Increases the size of the stack
  2875. void stack_expand(struct script_stack* stack)
  2876. {
  2877. stack->sp_max += 64;
  2878. stack->stack_data = (struct script_data*)aRealloc(stack->stack_data,
  2879. stack->sp_max * sizeof(stack->stack_data[0]) );
  2880. memset(stack->stack_data + (stack->sp_max - 64), 0,
  2881. 64 * sizeof(stack->stack_data[0]) );
  2882. }
  2883. /// Pushes a value into the stack
  2884. #define push_val(stack,type,val) push_val2(stack, type, val, NULL)
  2885. /// Pushes a value into the stack (with reference)
  2886. struct script_data* push_val2(struct script_stack* stack, enum c_op type, int64 val, struct reg_db *ref)
  2887. {
  2888. if( stack->sp >= stack->sp_max )
  2889. stack_expand(stack);
  2890. stack->stack_data[stack->sp].type = type;
  2891. stack->stack_data[stack->sp].u.num = val;
  2892. stack->stack_data[stack->sp].ref = ref;
  2893. stack->sp++;
  2894. return &stack->stack_data[stack->sp-1];
  2895. }
  2896. /// Pushes a string into the stack
  2897. struct script_data* push_str(struct script_stack* stack, enum c_op type, char* str)
  2898. {
  2899. if( stack->sp >= stack->sp_max )
  2900. stack_expand(stack);
  2901. stack->stack_data[stack->sp].type = type;
  2902. stack->stack_data[stack->sp].u.str = str;
  2903. stack->stack_data[stack->sp].ref = NULL;
  2904. stack->sp++;
  2905. return &stack->stack_data[stack->sp-1];
  2906. }
  2907. /// Pushes a retinfo into the stack
  2908. struct script_data* push_retinfo(struct script_stack* stack, struct script_retinfo* ri, struct reg_db *ref)
  2909. {
  2910. if( stack->sp >= stack->sp_max )
  2911. stack_expand(stack);
  2912. stack->stack_data[stack->sp].type = C_RETINFO;
  2913. stack->stack_data[stack->sp].u.ri = ri;
  2914. stack->stack_data[stack->sp].ref = ref;
  2915. stack->sp++;
  2916. return &stack->stack_data[stack->sp-1];
  2917. }
  2918. /// Pushes a copy of the target position into the stack
  2919. struct script_data* push_copy(struct script_stack* stack, int pos)
  2920. {
  2921. switch( stack->stack_data[pos].type ) {
  2922. case C_CONSTSTR:
  2923. return push_str(stack, C_CONSTSTR, stack->stack_data[pos].u.str);
  2924. break;
  2925. case C_STR:
  2926. return push_str(stack, C_STR, aStrdup(stack->stack_data[pos].u.str));
  2927. break;
  2928. case C_RETINFO:
  2929. ShowFatalError("script:push_copy: can't create copies of C_RETINFO. Exiting...\n");
  2930. exit(1);
  2931. break;
  2932. default:
  2933. return push_val2(
  2934. stack,stack->stack_data[pos].type,
  2935. stack->stack_data[pos].u.num,
  2936. stack->stack_data[pos].ref
  2937. );
  2938. break;
  2939. }
  2940. }
  2941. /// Removes the values in indexes [start,end] from the stack.
  2942. /// Adjusts all stack pointers.
  2943. void pop_stack(struct script_state* st, int start, int end)
  2944. {
  2945. struct script_stack* stack = st->stack;
  2946. struct script_data* data;
  2947. int i;
  2948. if( start < 0 )
  2949. start = 0;
  2950. if( end > stack->sp )
  2951. end = stack->sp;
  2952. if( start >= end )
  2953. return;// nothing to pop
  2954. // free stack elements
  2955. for( i = start; i < end; i++ )
  2956. {
  2957. data = &stack->stack_data[i];
  2958. if( data->type == C_STR )
  2959. aFree(data->u.str);
  2960. if( data->type == C_RETINFO ) {
  2961. struct script_retinfo* ri = data->u.ri;
  2962. if (ri->scope.vars) {
  2963. script_free_vars(ri->scope.vars);
  2964. ri->scope.vars = NULL;
  2965. }
  2966. if (ri->scope.arrays) {
  2967. ri->scope.arrays->destroy(ri->scope.arrays, script_free_array_db);
  2968. ri->scope.arrays = NULL;
  2969. }
  2970. if( data->ref )
  2971. aFree(data->ref);
  2972. aFree(ri);
  2973. }
  2974. data->type = C_NOP;
  2975. }
  2976. // move the rest of the elements
  2977. if( stack->sp > end )
  2978. {
  2979. memmove(&stack->stack_data[start], &stack->stack_data[end], sizeof(stack->stack_data[0])*(stack->sp - end));
  2980. for( i = start + stack->sp - end; i < stack->sp; ++i )
  2981. stack->stack_data[i].type = C_NOP;
  2982. }
  2983. // adjust stack pointers
  2984. if( st->start > end ) st->start -= end - start;
  2985. else if( st->start > start ) st->start = start;
  2986. if( st->end > end ) st->end -= end - start;
  2987. else if( st->end > start ) st->end = start;
  2988. if( stack->defsp > end ) stack->defsp -= end - start;
  2989. else if( stack->defsp > start ) stack->defsp = start;
  2990. stack->sp -= end - start;
  2991. }
  2992. ///
  2993. ///
  2994. ///
  2995. /*==========================================
  2996. * Release script dependent variable, dependent variable of function
  2997. *------------------------------------------*/
  2998. void script_free_vars(struct DBMap* storage)
  2999. {
  3000. if( storage ) {
  3001. // destroy the storage construct containing the variables
  3002. db_destroy(storage);
  3003. }
  3004. }
  3005. void script_free_code(struct script_code* code)
  3006. {
  3007. nullpo_retv(code);
  3008. if (code->instances)
  3009. script_stop_instances(code);
  3010. script_free_vars(code->local.vars);
  3011. if (code->local.arrays)
  3012. code->local.arrays->destroy(code->local.arrays, script_free_array_db);
  3013. aFree(code->script_buf);
  3014. aFree(code);
  3015. }
  3016. /// Creates a new script state.
  3017. ///
  3018. /// @param script Script code
  3019. /// @param pos Position in the code
  3020. /// @param rid Who is running the script (attached player)
  3021. /// @param oid Where the code is being run (npc 'object')
  3022. /// @return Script state
  3023. struct script_state* script_alloc_state(struct script_code* rootscript, int pos, int rid, int oid)
  3024. {
  3025. struct script_state* st;
  3026. st = ers_alloc(st_ers, struct script_state);
  3027. st->stack = ers_alloc(stack_ers, struct script_stack);
  3028. st->stack->sp = 0;
  3029. st->stack->sp_max = 64;
  3030. CREATE(st->stack->stack_data, struct script_data, st->stack->sp_max);
  3031. st->stack->defsp = st->stack->sp;
  3032. st->stack->scope.vars = i64db_alloc(DB_OPT_RELEASE_DATA);
  3033. st->stack->scope.arrays = NULL;
  3034. st->state = RUN;
  3035. st->script = rootscript;
  3036. st->pos = pos;
  3037. st->rid = rid;
  3038. st->oid = oid;
  3039. st->sleep.timer = INVALID_TIMER;
  3040. st->npc_item_flag = battle_config.item_enabled_npc;
  3041. if( st->script->instances != USHRT_MAX )
  3042. st->script->instances++;
  3043. else {
  3044. struct npc_data *nd = map_id2nd(oid);
  3045. ShowError("Over 65k instances of '%s' script are being run!\n",nd ? nd->name : "unknown");
  3046. }
  3047. if (!st->script->local.vars)
  3048. st->script->local.vars = i64db_alloc(DB_OPT_RELEASE_DATA);
  3049. st->id = next_id++;
  3050. active_scripts++;
  3051. idb_put(st_db, st->id, st);
  3052. return st;
  3053. }
  3054. /// Frees a script state.
  3055. ///
  3056. /// @param st Script state
  3057. void script_free_state(struct script_state* st)
  3058. {
  3059. if (idb_exists(st_db, st->id)) {
  3060. struct map_session_data *sd = st->rid ? map_id2sd(st->rid) : NULL;
  3061. if (st->bk_st) // backup was not restored
  3062. 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);
  3063. if (sd && sd->st == st) { // Current script is aborted.
  3064. if(sd->state.using_fake_npc) {
  3065. clif_clearunit_single(sd->npc_id, CLR_OUTSIGHT, sd->fd);
  3066. sd->state.using_fake_npc = 0;
  3067. }
  3068. sd->st = NULL;
  3069. sd->npc_id = 0;
  3070. }
  3071. if (st->sleep.timer != INVALID_TIMER)
  3072. delete_timer(st->sleep.timer, run_script_timer);
  3073. if (st->stack) {
  3074. script_free_vars(st->stack->scope.vars);
  3075. if (st->stack->scope.arrays)
  3076. st->stack->scope.arrays->destroy(st->stack->scope.arrays, script_free_array_db);
  3077. pop_stack(st, 0, st->stack->sp);
  3078. aFree(st->stack->stack_data);
  3079. ers_free(stack_ers, st->stack);
  3080. st->stack = NULL;
  3081. }
  3082. if (st->script && st->script->instances != USHRT_MAX && --st->script->instances == 0) {
  3083. if (st->script->local.vars && !db_size(st->script->local.vars)) {
  3084. script_free_vars(st->script->local.vars);
  3085. st->script->local.vars = NULL;
  3086. }
  3087. if (st->script->local.arrays && !db_size(st->script->local.arrays)) {
  3088. st->script->local.arrays->destroy(st->script->local.arrays, script_free_array_db);
  3089. st->script->local.arrays = NULL;
  3090. }
  3091. }
  3092. st->pos = -1;
  3093. idb_remove(st_db, st->id);
  3094. ers_free(st_ers, st);
  3095. if (--active_scripts == 0)
  3096. next_id = 0;
  3097. }
  3098. }
  3099. //
  3100. // Main execution unit
  3101. //
  3102. /*==========================================
  3103. * Read command
  3104. *------------------------------------------*/
  3105. c_op get_com(unsigned char *script,int *pos)
  3106. {
  3107. int i = 0, j = 0;
  3108. if(script[*pos]>=0x80){
  3109. return C_INT;
  3110. }
  3111. while(script[*pos]>=0x40){
  3112. i=script[(*pos)++]<<j;
  3113. j+=6;
  3114. }
  3115. return (c_op)(i+(script[(*pos)++]<<j));
  3116. }
  3117. /*==========================================
  3118. * Income figures
  3119. *------------------------------------------*/
  3120. int get_num(unsigned char *script,int *pos)
  3121. {
  3122. int i,j;
  3123. i=0; j=0;
  3124. while(script[*pos]>=0xc0){
  3125. i+=(script[(*pos)++]&0x7f)<<j;
  3126. j+=6;
  3127. }
  3128. return i+((script[(*pos)++]&0x7f)<<j);
  3129. }
  3130. /// Ternary operators
  3131. /// test ? if_true : if_false
  3132. void op_3(struct script_state* st, int op)
  3133. {
  3134. struct script_data* data;
  3135. int flag = 0;
  3136. data = script_getdatatop(st, -3);
  3137. get_val(st, data);
  3138. if( data_isstring(data) )
  3139. flag = data->u.str[0];// "" -> false
  3140. else if( data_isint(data) )
  3141. flag = data->u.num == 0 ? 0 : 1;// 0 -> false
  3142. else
  3143. {
  3144. ShowError("script:op_3: invalid data for the ternary operator test\n");
  3145. script_reportdata(data);
  3146. script_reportsrc(st);
  3147. script_removetop(st, -3, 0);
  3148. script_pushnil(st);
  3149. return;
  3150. }
  3151. if( flag )
  3152. script_pushcopytop(st, -2);
  3153. else
  3154. script_pushcopytop(st, -1);
  3155. script_removetop(st, -4, -1);
  3156. }
  3157. /// Binary string operators
  3158. /// s1 EQ s2 -> i
  3159. /// s1 NE s2 -> i
  3160. /// s1 GT s2 -> i
  3161. /// s1 GE s2 -> i
  3162. /// s1 LT s2 -> i
  3163. /// s1 LE s2 -> i
  3164. /// s1 ADD s2 -> s
  3165. void op_2str(struct script_state* st, int op, const char* s1, const char* s2)
  3166. {
  3167. int a = 0;
  3168. switch(op){
  3169. case C_EQ: a = (strcmp(s1,s2) == 0); break;
  3170. case C_NE: a = (strcmp(s1,s2) != 0); break;
  3171. case C_GT: a = (strcmp(s1,s2) > 0); break;
  3172. case C_GE: a = (strcmp(s1,s2) >= 0); break;
  3173. case C_LT: a = (strcmp(s1,s2) < 0); break;
  3174. case C_LE: a = (strcmp(s1,s2) <= 0); break;
  3175. case C_ADD:
  3176. {
  3177. char* buf = (char *)aMalloc((strlen(s1)+strlen(s2)+1)*sizeof(char));
  3178. strcpy(buf, s1);
  3179. strcat(buf, s2);
  3180. script_pushstr(st, buf);
  3181. return;
  3182. }
  3183. default:
  3184. ShowError("script:op2_str: unexpected string operator %s\n", script_op2name(op));
  3185. script_reportsrc(st);
  3186. script_pushnil(st);
  3187. st->state = END;
  3188. return;
  3189. }
  3190. script_pushint(st,a);
  3191. }
  3192. /// Binary number operators
  3193. /// i OP i -> i
  3194. void op_2num(struct script_state* st, int op, int i1, int i2)
  3195. {
  3196. int ret;
  3197. double ret_double;
  3198. switch( op ) {
  3199. case C_AND: ret = i1 & i2; break;
  3200. case C_OR: ret = i1 | i2; break;
  3201. case C_XOR: ret = i1 ^ i2; break;
  3202. case C_LAND: ret = (i1 && i2); break;
  3203. case C_LOR: ret = (i1 || i2); break;
  3204. case C_EQ: ret = (i1 == i2); break;
  3205. case C_NE: ret = (i1 != i2); break;
  3206. case C_GT: ret = (i1 > i2); break;
  3207. case C_GE: ret = (i1 >= i2); break;
  3208. case C_LT: ret = (i1 < i2); break;
  3209. case C_LE: ret = (i1 <= i2); break;
  3210. case C_R_SHIFT: ret = i1>>i2; break;
  3211. case C_L_SHIFT: ret = i1<<i2; break;
  3212. case C_DIV:
  3213. case C_MOD:
  3214. if( i2 == 0 )
  3215. {
  3216. ShowError("script:op_2num: division by zero detected op=%s i1=%d i2=%d\n", script_op2name(op), i1, i2);
  3217. script_reportsrc(st);
  3218. script_pushnil(st);
  3219. st->state = END;
  3220. return;
  3221. }
  3222. else if( op == C_DIV )
  3223. ret = i1 / i2;
  3224. else//if( op == C_MOD )
  3225. ret = i1 % i2;
  3226. break;
  3227. default:
  3228. switch( op ) {// operators that can overflow/underflow
  3229. case C_ADD: ret = i1 + i2; ret_double = (double)i1 + (double)i2; break;
  3230. case C_SUB: ret = i1 - i2; ret_double = (double)i1 - (double)i2; break;
  3231. case C_MUL: ret = i1 * i2; ret_double = (double)i1 * (double)i2; break;
  3232. default:
  3233. ShowError("script:op_2num: unexpected number operator %s i1=%d i2=%d\n", script_op2name(op), i1, i2);
  3234. script_reportsrc(st);
  3235. script_pushnil(st);
  3236. return;
  3237. }
  3238. if( ret_double < (double)INT_MIN ) {
  3239. ShowWarning("script:op_2num: underflow detected op=%s i1=%d i2=%d\n", script_op2name(op), i1, i2);
  3240. script_reportsrc(st);
  3241. ret = INT_MIN;
  3242. }
  3243. else if( ret_double > (double)INT_MAX ) {
  3244. ShowWarning("script:op_2num: overflow detected op=%s i1=%d i2=%d\n", script_op2name(op), i1, i2);
  3245. script_reportsrc(st);
  3246. ret = INT_MAX;
  3247. }
  3248. }
  3249. script_pushint(st, ret);
  3250. }
  3251. /// Binary operators
  3252. void op_2(struct script_state *st, int op)
  3253. {
  3254. struct script_data* left, leftref;
  3255. struct script_data* right;
  3256. leftref.type = C_NOP;
  3257. left = script_getdatatop(st, -2);
  3258. right = script_getdatatop(st, -1);
  3259. if (st->op2ref) {
  3260. if (data_isreference(left)) {
  3261. leftref = *left;
  3262. }
  3263. st->op2ref = 0;
  3264. }
  3265. get_val(st, left);
  3266. get_val(st, right);
  3267. // automatic conversions
  3268. switch( op )
  3269. {
  3270. case C_ADD:
  3271. if( data_isint(left) && data_isstring(right) )
  3272. {// convert int-string to string-string
  3273. conv_str(st, left);
  3274. }
  3275. else if( data_isstring(left) && data_isint(right) )
  3276. {// convert string-int to string-string
  3277. conv_str(st, right);
  3278. }
  3279. break;
  3280. }
  3281. if( data_isstring(left) && data_isstring(right) )
  3282. {// ss => op_2str
  3283. op_2str(st, op, left->u.str, right->u.str);
  3284. script_removetop(st, leftref.type == C_NOP ? -3 : -2, -1);// pop the two values before the top one
  3285. if (leftref.type != C_NOP)
  3286. {
  3287. if (left->type == C_STR) // don't free C_CONSTSTR
  3288. aFree(left->u.str);
  3289. *left = leftref;
  3290. }
  3291. }
  3292. else if( data_isint(left) && data_isint(right) )
  3293. {// ii => op_2num
  3294. int i1 = (int)left->u.num;
  3295. int i2 = (int)right->u.num;
  3296. script_removetop(st, leftref.type == C_NOP ? -2 : -1, 0);
  3297. op_2num(st, op, i1, i2);
  3298. if (leftref.type != C_NOP)
  3299. *left = leftref;
  3300. }
  3301. else
  3302. {// invalid argument
  3303. ShowError("script:op_2: invalid data for operator %s\n", script_op2name(op));
  3304. script_reportdata(left);
  3305. script_reportdata(right);
  3306. script_reportsrc(st);
  3307. script_removetop(st, -2, 0);
  3308. script_pushnil(st);
  3309. st->state = END;
  3310. }
  3311. }
  3312. /// Unary operators
  3313. /// NEG i -> i
  3314. /// NOT i -> i
  3315. /// LNOT i -> i
  3316. void op_1(struct script_state* st, int op)
  3317. {
  3318. struct script_data* data;
  3319. int i1;
  3320. data = script_getdatatop(st, -1);
  3321. get_val(st, data);
  3322. if( !data_isint(data) )
  3323. {// not a number
  3324. ShowError("script:op_1: argument is not a number (op=%s)\n", script_op2name(op));
  3325. script_reportdata(data);
  3326. script_reportsrc(st);
  3327. script_pushnil(st);
  3328. st->state = END;
  3329. return;
  3330. }
  3331. i1 = (int)data->u.num;
  3332. script_removetop(st, -1, 0);
  3333. switch( op )
  3334. {
  3335. case C_NEG: i1 = -i1; break;
  3336. case C_NOT: i1 = ~i1; break;
  3337. case C_LNOT: i1 = !i1; break;
  3338. default:
  3339. ShowError("script:op_1: unexpected operator %s i1=%d\n", script_op2name(op), i1);
  3340. script_reportsrc(st);
  3341. script_pushnil(st);
  3342. st->state = END;
  3343. return;
  3344. }
  3345. script_pushint(st, i1);
  3346. }
  3347. /// Checks the type of all arguments passed to a built-in function.
  3348. ///
  3349. /// @param st Script state whose stack arguments should be inspected.
  3350. /// @param func Built-in function for which the arguments are intended.
  3351. static void script_check_buildin_argtype(struct script_state* st, int func)
  3352. {
  3353. int idx, invalid = 0;
  3354. for( idx = 2; script_hasdata(st, idx); idx++ )
  3355. {
  3356. struct script_data* data = script_getdata(st, idx);
  3357. script_function* sf = &buildin_func[str_data[func].val];
  3358. char type = sf->arg[idx-2];
  3359. if( type == '?' || type == '*' )
  3360. {// optional argument or unknown number of optional parameters ( no types are after this )
  3361. break;
  3362. }
  3363. else if( type == 0 )
  3364. {// more arguments than necessary ( should not happen, as it is checked before )
  3365. ShowWarning("Found more arguments than necessary. unexpected arg type %s\n",script_op2name(data->type));
  3366. invalid++;
  3367. break;
  3368. }
  3369. else
  3370. {
  3371. const char* name = NULL;
  3372. if( data_isreference(data) )
  3373. {// get name for variables to determine the type they refer to
  3374. name = reference_getname(data);
  3375. }
  3376. switch( type )
  3377. {
  3378. case 'v':
  3379. if( !data_isstring(data) && !data_isint(data) && !data_isreference(data) )
  3380. {// variant
  3381. ShowWarning("Unexpected type for argument %d. Expected string, number or variable.\n", idx-1);
  3382. script_reportdata(data);
  3383. invalid++;
  3384. }
  3385. break;
  3386. case 's':
  3387. if( !data_isstring(data) && !( data_isreference(data) && is_string_variable(name) ) )
  3388. {// string
  3389. ShowWarning("Unexpected type for argument %d. Expected string.\n", idx-1);
  3390. script_reportdata(data);
  3391. invalid++;
  3392. }
  3393. break;
  3394. case 'i':
  3395. if( !data_isint(data) && !( data_isreference(data) && ( reference_toparam(data) || reference_toconstant(data) || !is_string_variable(name) ) ) )
  3396. {// int ( params and constants are always int )
  3397. ShowWarning("Unexpected type for argument %d. Expected number.\n", idx-1);
  3398. script_reportdata(data);
  3399. invalid++;
  3400. }
  3401. break;
  3402. case 'r':
  3403. if( !data_isreference(data) )
  3404. {// variables
  3405. ShowWarning("Unexpected type for argument %d. Expected variable, got %s.\n", idx-1,script_op2name(data->type));
  3406. script_reportdata(data);
  3407. invalid++;
  3408. }
  3409. break;
  3410. case 'l':
  3411. if( !data_islabel(data) && !data_isfunclabel(data) )
  3412. {// label
  3413. ShowWarning("Unexpected type for argument %d. Expected label, got %s\n", idx-1,script_op2name(data->type));
  3414. script_reportdata(data);
  3415. invalid++;
  3416. }
  3417. break;
  3418. }
  3419. }
  3420. }
  3421. if(invalid)
  3422. {
  3423. ShowDebug("Function: %s\n", get_str(func));
  3424. script_reportsrc(st);
  3425. }
  3426. }
  3427. /// Executes a buildin command.
  3428. /// Stack: C_NAME(<command>) C_ARG <arg0> <arg1> ... <argN>
  3429. int run_func(struct script_state *st)
  3430. {
  3431. struct script_data* data;
  3432. int i,start_sp,end_sp,func;
  3433. end_sp = st->stack->sp;// position after the last argument
  3434. for( i = end_sp-1; i > 0 ; --i )
  3435. if( st->stack->stack_data[i].type == C_ARG )
  3436. break;
  3437. if( i == 0 ) {
  3438. ShowError("script:run_func: C_ARG not found. please report this!!!\n");
  3439. st->state = END;
  3440. script_reportsrc(st);
  3441. return 1;
  3442. }
  3443. start_sp = i-1;// C_NAME of the command
  3444. st->start = start_sp;
  3445. st->end = end_sp;
  3446. data = &st->stack->stack_data[st->start];
  3447. if( data->type == C_NAME && str_data[data->u.num].type == C_FUNC ) {
  3448. func = (int)data->u.num;
  3449. st->funcname = reference_getname(data);
  3450. } else {
  3451. ShowError("script:run_func: not a buildin command.\n");
  3452. script_reportdata(data);
  3453. script_reportsrc(st);
  3454. st->state = END;
  3455. return 1;
  3456. }
  3457. if( script_config.warn_func_mismatch_argtypes ) {
  3458. script_check_buildin_argtype(st, func);
  3459. }
  3460. if(str_data[func].func) {
  3461. if (str_data[func].func(st) == SCRIPT_CMD_FAILURE) //Report error
  3462. script_reportsrc(st);
  3463. } else {
  3464. 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));
  3465. script_reportsrc(st);
  3466. st->state = END;
  3467. }
  3468. // Stack's datum are used when re-running functions [Eoe]
  3469. if( st->state == RERUNLINE )
  3470. return 0;
  3471. pop_stack(st, st->start, st->end);
  3472. if( st->state == RETFUNC ) {// return from a user-defined function
  3473. struct script_retinfo* ri;
  3474. int olddefsp = st->stack->defsp;
  3475. int nargs;
  3476. pop_stack(st, st->stack->defsp, st->start);// pop distractions from the stack
  3477. if( st->stack->defsp < 1 || st->stack->stack_data[st->stack->defsp-1].type != C_RETINFO )
  3478. {
  3479. ShowWarning("script:run_func: return without callfunc or callsub!\n");
  3480. script_reportsrc(st);
  3481. st->state = END;
  3482. return 1;
  3483. }
  3484. script_free_vars(st->stack->scope.vars);
  3485. st->stack->scope.arrays->destroy(st->stack->scope.arrays, script_free_array_db);
  3486. ri = st->stack->stack_data[st->stack->defsp-1].u.ri;
  3487. nargs = ri->nargs;
  3488. st->pos = ri->pos;
  3489. st->script = ri->script;
  3490. st->stack->scope.vars = ri->scope.vars;
  3491. st->stack->scope.arrays = ri->scope.arrays;
  3492. st->stack->defsp = ri->defsp;
  3493. memset(ri, 0, sizeof(struct script_retinfo));
  3494. pop_stack(st, olddefsp-nargs-1, olddefsp);// pop arguments and retinfo
  3495. st->state = GOTO;
  3496. }
  3497. return 0;
  3498. }
  3499. /*==========================================
  3500. * script execution
  3501. *------------------------------------------*/
  3502. void run_script(struct script_code *rootscript, int pos, int rid, int oid)
  3503. {
  3504. struct script_state *st;
  3505. if( rootscript == NULL || pos < 0 )
  3506. return;
  3507. // TODO In jAthena, this function can take over the pending script in the player. [FlavioJS]
  3508. // It is unclear how that can be triggered, so it needs the be traced/checked in more detail.
  3509. // NOTE At the time of this change, this function wasn't capable of taking over the script state because st->scriptroot was never set.
  3510. st = script_alloc_state(rootscript, pos, rid, oid);
  3511. run_script_main(st);
  3512. }
  3513. void script_stop_instances(struct script_code *code) {
  3514. DBIterator *iter;
  3515. struct script_state* st;
  3516. if( !active_scripts )
  3517. return; // Don't even bother.
  3518. iter = db_iterator(st_db);
  3519. for( st = dbi_first(iter); dbi_exists(iter); st = dbi_next(iter) ) {
  3520. if( st->script == code )
  3521. script_free_state(st);
  3522. }
  3523. dbi_destroy(iter);
  3524. }
  3525. /*==========================================
  3526. * Timer function for sleep
  3527. *------------------------------------------*/
  3528. int run_script_timer(int tid, unsigned int tick, int id, intptr_t data)
  3529. {
  3530. struct script_state *st = (struct script_state *)data;
  3531. TBL_PC *sd = map_id2sd(st->rid);
  3532. if ((sd && sd->status.char_id != id) || (st->rid && !sd)) { // Character mismatch. Cancel execution.
  3533. st->rid = 0;
  3534. st->state = END;
  3535. }
  3536. st->sleep.timer = INVALID_TIMER;
  3537. if(st->state != RERUNLINE)
  3538. st->sleep.tick = 0;
  3539. run_script_main(st);
  3540. return 0;
  3541. }
  3542. /// Detaches script state from possibly attached character and restores it's previous script if any.
  3543. ///
  3544. /// @param st Script state to detach.
  3545. /// @param dequeue_event Whether to schedule any queued events, when there was no previous script.
  3546. static void script_detach_state(struct script_state* st, bool dequeue_event)
  3547. {
  3548. struct map_session_data* sd;
  3549. if(st->rid && (sd = map_id2sd(st->rid))!=NULL) {
  3550. sd->st = st->bk_st;
  3551. sd->npc_id = st->bk_npcid;
  3552. sd->state.disable_atcommand_on_npc = 0;
  3553. if(st->bk_st) {
  3554. //Remove tag for removal.
  3555. st->bk_st = NULL;
  3556. st->bk_npcid = 0;
  3557. } else if(dequeue_event) {
  3558. #ifdef SECURE_NPCTIMEOUT
  3559. /**
  3560. * We're done with this NPC session, so we cancel the timer (if existent) and move on
  3561. **/
  3562. if( sd->npc_idle_timer != INVALID_TIMER ) {
  3563. delete_timer(sd->npc_idle_timer,npc_rr_secure_timeout_timer);
  3564. sd->npc_idle_timer = INVALID_TIMER;
  3565. }
  3566. #endif
  3567. npc_event_dequeue(sd);
  3568. }
  3569. }
  3570. else if(st->bk_st)
  3571. {// rid was set to 0, before detaching the script state
  3572. 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);
  3573. script_reportsrc(st->bk_st);
  3574. script_free_state(st->bk_st);
  3575. st->bk_st = NULL;
  3576. }
  3577. }
  3578. /// Attaches script state to possibly attached character and backups it's previous script, if any.
  3579. ///
  3580. /// @param st Script state to attach.
  3581. static void script_attach_state(struct script_state* st)
  3582. {
  3583. struct map_session_data* sd;
  3584. if(st->rid && (sd = map_id2sd(st->rid))!=NULL)
  3585. {
  3586. if(st!=sd->st)
  3587. {
  3588. if(st->bk_st)
  3589. {// there is already a backup
  3590. 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);
  3591. }
  3592. st->bk_st = sd->st;
  3593. st->bk_npcid = sd->npc_id;
  3594. }
  3595. sd->st = st;
  3596. sd->npc_id = st->oid;
  3597. sd->npc_item_flag = st->npc_item_flag; // load default.
  3598. sd->state.disable_atcommand_on_npc = (!pc_has_permission(sd, PC_PERM_ENABLE_COMMAND));
  3599. #ifdef SECURE_NPCTIMEOUT
  3600. if( sd->npc_idle_timer == INVALID_TIMER )
  3601. sd->npc_idle_timer = add_timer(gettick() + (SECURE_NPCTIMEOUT_INTERVAL*1000),npc_rr_secure_timeout_timer,sd->bl.id,0);
  3602. sd->npc_idle_tick = gettick();
  3603. #endif
  3604. }
  3605. }
  3606. /*==========================================
  3607. * The main part of the script execution
  3608. *------------------------------------------*/
  3609. void run_script_main(struct script_state *st)
  3610. {
  3611. int cmdcount = script_config.check_cmdcount;
  3612. int gotocount = script_config.check_gotocount;
  3613. TBL_PC *sd;
  3614. struct script_stack *stack = st->stack;
  3615. script_attach_state(st);
  3616. if(st->state == RERUNLINE) {
  3617. run_func(st);
  3618. if(st->state == GOTO)
  3619. st->state = RUN;
  3620. } else if(st->state != END)
  3621. st->state = RUN;
  3622. while(st->state == RUN) {
  3623. enum c_op c = get_com(st->script->script_buf,&st->pos);
  3624. switch(c){
  3625. case C_EOL:
  3626. if( stack->defsp > stack->sp )
  3627. ShowError("script:run_script_main: unexpected stack position (defsp=%d sp=%d). please report this!!!\n", stack->defsp, stack->sp);
  3628. else
  3629. pop_stack(st, stack->defsp, stack->sp);// pop unused stack data. (unused return value)
  3630. break;
  3631. case C_INT:
  3632. push_val(stack,C_INT,get_num(st->script->script_buf,&st->pos));
  3633. break;
  3634. case C_POS:
  3635. case C_NAME:
  3636. push_val(stack,c,GETVALUE(st->script->script_buf,st->pos));
  3637. st->pos+=3;
  3638. break;
  3639. case C_ARG:
  3640. push_val(stack,c,0);
  3641. break;
  3642. case C_STR:
  3643. push_str(stack,C_CONSTSTR,(char*)(st->script->script_buf+st->pos));
  3644. while(st->script->script_buf[st->pos++]);
  3645. break;
  3646. case C_FUNC:
  3647. run_func(st);
  3648. if(st->state==GOTO){
  3649. st->state = RUN;
  3650. if( !st->freeloop && gotocount>0 && (--gotocount)<=0 ){
  3651. ShowError("script:run_script_main: infinity loop !\n");
  3652. script_reportsrc(st);
  3653. st->state=END;
  3654. }
  3655. }
  3656. break;
  3657. case C_REF:
  3658. st->op2ref = 1;
  3659. break;
  3660. case C_NEG:
  3661. case C_NOT:
  3662. case C_LNOT:
  3663. op_1(st ,c);
  3664. break;
  3665. case C_ADD:
  3666. case C_SUB:
  3667. case C_MUL:
  3668. case C_DIV:
  3669. case C_MOD:
  3670. case C_EQ:
  3671. case C_NE:
  3672. case C_GT:
  3673. case C_GE:
  3674. case C_LT:
  3675. case C_LE:
  3676. case C_AND:
  3677. case C_OR:
  3678. case C_XOR:
  3679. case C_LAND:
  3680. case C_LOR:
  3681. case C_R_SHIFT:
  3682. case C_L_SHIFT:
  3683. op_2(st, c);
  3684. break;
  3685. case C_OP3:
  3686. op_3(st, c);
  3687. break;
  3688. case C_NOP:
  3689. st->state=END;
  3690. break;
  3691. default:
  3692. ShowError("script:run_script_main:unknown command : %d @ %d\n",c,st->pos);
  3693. st->state=END;
  3694. break;
  3695. }
  3696. if( !st->freeloop && cmdcount>0 && (--cmdcount)<=0 ){
  3697. ShowError("script:run_script_main: infinity loop !\n");
  3698. script_reportsrc(st);
  3699. st->state=END;
  3700. }
  3701. }
  3702. if(st->sleep.tick > 0) {
  3703. //Restore previous script
  3704. script_detach_state(st, false);
  3705. //Delay execution
  3706. sd = map_id2sd(st->rid); // Get sd since script might have attached someone while running. [Inkfish]
  3707. st->sleep.charid = sd?sd->status.char_id:0;
  3708. st->sleep.timer = add_timer(gettick()+st->sleep.tick,
  3709. run_script_timer, st->sleep.charid, (intptr_t)st);
  3710. } else if(st->state != END && st->rid) {
  3711. //Resume later (st is already attached to player).
  3712. if(st->bk_st) {
  3713. ShowWarning("Unable to restore stack! Double continuation!\n");
  3714. //Report BOTH scripts to see if that can help somehow.
  3715. ShowDebug("Previous script (lost):\n");
  3716. script_reportsrc(st->bk_st);
  3717. ShowDebug("Current script:\n");
  3718. script_reportsrc(st);
  3719. script_free_state(st->bk_st);
  3720. st->bk_st = NULL;
  3721. }
  3722. } else {
  3723. //Dispose of script.
  3724. if ((sd = map_id2sd(st->rid))!=NULL)
  3725. { //Restore previous stack and save char.
  3726. if(sd->state.using_fake_npc){
  3727. clif_clearunit_single(sd->npc_id, CLR_OUTSIGHT, sd->fd);
  3728. sd->state.using_fake_npc = 0;
  3729. }
  3730. //Restore previous script if any.
  3731. script_detach_state(st, true);
  3732. if (sd->vars_dirty)
  3733. intif_saveregistry(sd);
  3734. }
  3735. script_free_state(st);
  3736. }
  3737. }
  3738. int script_config_read(char *cfgName)
  3739. {
  3740. int i;
  3741. char line[1024],w1[1024],w2[1024];
  3742. FILE *fp;
  3743. fp=fopen(cfgName,"r");
  3744. if(fp==NULL){
  3745. ShowError("File not found: %s\n", cfgName);
  3746. return 1;
  3747. }
  3748. while(fgets(line, sizeof(line), fp))
  3749. {
  3750. if(line[0] == '/' && line[1] == '/')
  3751. continue;
  3752. i=sscanf(line,"%1023[^:]: %1023[^\r\n]",w1,w2);
  3753. if(i!=2)
  3754. continue;
  3755. if(strcmpi(w1,"warn_func_mismatch_paramnum")==0) {
  3756. script_config.warn_func_mismatch_paramnum = config_switch(w2);
  3757. }
  3758. else if(strcmpi(w1,"check_cmdcount")==0) {
  3759. script_config.check_cmdcount = config_switch(w2);
  3760. }
  3761. else if(strcmpi(w1,"check_gotocount")==0) {
  3762. script_config.check_gotocount = config_switch(w2);
  3763. }
  3764. else if(strcmpi(w1,"input_min_value")==0) {
  3765. script_config.input_min_value = config_switch(w2);
  3766. }
  3767. else if(strcmpi(w1,"input_max_value")==0) {
  3768. script_config.input_max_value = config_switch(w2);
  3769. }
  3770. else if(strcmpi(w1,"warn_func_mismatch_argtypes")==0) {
  3771. script_config.warn_func_mismatch_argtypes = config_switch(w2);
  3772. }
  3773. else if(strcmpi(w1,"import")==0){
  3774. script_config_read(w2);
  3775. }
  3776. else {
  3777. ShowWarning("Unknown setting '%s' in file %s\n", w1, cfgName);
  3778. }
  3779. }
  3780. fclose(fp);
  3781. return 0;
  3782. }
  3783. /**
  3784. * @see DBApply
  3785. */
  3786. static int db_script_free_code_sub(DBKey key, DBData *data, va_list ap)
  3787. {
  3788. struct script_code *code = db_data2ptr(data);
  3789. if (code)
  3790. script_free_code(code);
  3791. return 0;
  3792. }
  3793. void script_run_autobonus(const char *autobonus, struct map_session_data *sd, unsigned int pos)
  3794. {
  3795. struct script_code *script = (struct script_code *)strdb_get(autobonus_db, autobonus);
  3796. if( script )
  3797. {
  3798. int j;
  3799. ARR_FIND( 0, EQI_MAX, j, sd->equip_index[j] >= 0 && sd->inventory.u.items_inventory[sd->equip_index[j]].equip == pos );
  3800. if( j < EQI_MAX ) {
  3801. //Single item autobonus
  3802. current_equip_item_index = sd->equip_index[j];
  3803. current_equip_combo_pos = 0;
  3804. } else {
  3805. //Combo autobonus
  3806. current_equip_item_index = -1;
  3807. current_equip_combo_pos = pos;
  3808. }
  3809. run_script(script,0,sd->bl.id,0);
  3810. }
  3811. }
  3812. void script_add_autobonus(const char *autobonus)
  3813. {
  3814. if( strdb_get(autobonus_db, autobonus) == NULL )
  3815. {
  3816. struct script_code *script = parse_script(autobonus, "autobonus", 0, 0);
  3817. if( script )
  3818. strdb_put(autobonus_db, autobonus, script);
  3819. }
  3820. }
  3821. /// resets a temporary character array variable to given value
  3822. void script_cleararray_pc(struct map_session_data* sd, const char* varname, void* value)
  3823. {
  3824. struct script_array *sa = NULL;
  3825. struct reg_db *src = NULL;
  3826. unsigned int i, *list = NULL, size = 0;
  3827. int key;
  3828. key = add_str(varname);
  3829. if( !(src = script_array_src(NULL,sd,varname,NULL) ) )
  3830. return;
  3831. if( value )
  3832. script_array_ensure_zero(NULL,sd,reference_uid(key,0), NULL);
  3833. if( !(sa = idb_get(src->arrays, key)) ) /* non-existent array, nothing to empty */
  3834. return;
  3835. size = sa->size;
  3836. list = script_array_cpy_list(sa);
  3837. for(i = 0; i < size; i++) {
  3838. set_reg(NULL,sd,reference_uid(key, list[i]),varname,value,NULL);
  3839. }
  3840. }
  3841. /// sets a temporary character array variable element idx to given value
  3842. /// @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.
  3843. void script_setarray_pc(struct map_session_data* sd, const char* varname, uint32 idx, void* value, int* refcache)
  3844. {
  3845. int key;
  3846. if( idx >= SCRIPT_MAX_ARRAYSIZE ) {
  3847. ShowError("script_setarray_pc: Variable '%s' has invalid index '%u' (char_id=%d).\n", varname, idx, sd->status.char_id);
  3848. return;
  3849. }
  3850. key = ( refcache && refcache[0] ) ? refcache[0] : add_str(varname);
  3851. set_reg(NULL,sd,reference_uid(key, idx),varname,value,NULL);
  3852. if( refcache ) {
  3853. // save to avoid repeated script->add_str calls
  3854. refcache[0] = key;
  3855. }
  3856. }
  3857. /**
  3858. * Clears persistent variables from memory
  3859. **/
  3860. int script_reg_destroy(DBKey key, DBData *data, va_list ap)
  3861. {
  3862. struct script_reg_state *src;
  3863. if( data->type != DB_DATA_PTR ) // got no need for those!
  3864. return 0;
  3865. src = db_data2ptr(data);
  3866. if( src->type ) {
  3867. struct script_reg_str *p = (struct script_reg_str *)src;
  3868. if( p->value )
  3869. aFree(p->value);
  3870. ers_free(str_reg_ers,p);
  3871. } else {
  3872. ers_free(num_reg_ers,(struct script_reg_num*)src);
  3873. }
  3874. return 0;
  3875. }
  3876. /**
  3877. * Clears a single persistent variable
  3878. **/
  3879. void script_reg_destroy_single(struct map_session_data *sd, int64 reg, struct script_reg_state *data)
  3880. {
  3881. i64db_remove(sd->regs.vars, reg);
  3882. if( data->type ) {
  3883. struct script_reg_str *p = (struct script_reg_str*)data;
  3884. if( p->value )
  3885. aFree(p->value);
  3886. ers_free(str_reg_ers,p);
  3887. } else {
  3888. ers_free(num_reg_ers,(struct script_reg_num*)data);
  3889. }
  3890. }
  3891. unsigned int *script_array_cpy_list(struct script_array *sa)
  3892. {
  3893. if( sa->size > generic_ui_array_size )
  3894. script_generic_ui_array_expand(sa->size);
  3895. memcpy(generic_ui_array, sa->members, sizeof(unsigned int)*sa->size);
  3896. return generic_ui_array;
  3897. }
  3898. void script_generic_ui_array_expand (unsigned int plus)
  3899. {
  3900. generic_ui_array_size += plus + 100;
  3901. RECREATE(generic_ui_array, unsigned int, generic_ui_array_size);
  3902. }
  3903. int buildin_query_sql_sub(struct script_state *st, Sql *handle);
  3904. #ifdef BETA_THREAD_TEST
  3905. /* used to receive items the queryThread has already processed */
  3906. int queryThread_timer(int tid, unsigned int tick, int id, intptr_t data) {
  3907. int i, cursor = 0;
  3908. bool allOk = true;
  3909. EnterSpinLock(&queryThreadLock);
  3910. for( i = 0; i < queryThreadData.count; i++ ) {
  3911. struct queryThreadEntry *entry = queryThreadData.entry[i];
  3912. if( !entry->ok ) {
  3913. allOk = false;
  3914. continue;
  3915. }
  3916. run_script_main(entry->st);
  3917. entry->st = NULL;/* empty entries */
  3918. aFree(entry);
  3919. queryThreadData.entry[i] = NULL;
  3920. }
  3921. if( allOk ) {
  3922. /* cancel the repeating timer -- it'll re-create itself when necessary, dont need to remain looping */
  3923. delete_timer(queryThreadData.timer, queryThread_timer);
  3924. queryThreadData.timer = INVALID_TIMER;
  3925. }
  3926. /* now lets clear the mess. */
  3927. for( i = 0; i < queryThreadData.count; i++ ) {
  3928. struct queryThreadEntry *entry = queryThreadData.entry[i];
  3929. if( entry == NULL )
  3930. continue;/* entry on hold */
  3931. /* move */
  3932. memmove(&queryThreadData.entry[cursor], &queryThreadData.entry[i], sizeof(struct queryThreadEntry*));
  3933. cursor++;
  3934. }
  3935. queryThreadData.count = cursor;
  3936. LeaveSpinLock(&queryThreadLock);
  3937. return 0;
  3938. }
  3939. void queryThread_add(struct script_state *st, bool type) {
  3940. int idx = 0;
  3941. struct queryThreadEntry* entry = NULL;
  3942. EnterSpinLock(&queryThreadLock);
  3943. if( queryThreadData.count++ != 0 )
  3944. RECREATE(queryThreadData.entry, struct queryThreadEntry* , queryThreadData.count);
  3945. idx = queryThreadData.count-1;
  3946. CREATE(queryThreadData.entry[idx],struct queryThreadEntry,1);
  3947. entry = queryThreadData.entry[idx];
  3948. entry->st = st;
  3949. entry->ok = false;
  3950. entry->type = type;
  3951. if( queryThreadData.timer == INVALID_TIMER ) { /* start the receiver timer */
  3952. queryThreadData.timer = add_timer_interval(gettick() + 100, queryThread_timer, 0, 0, 100);
  3953. }
  3954. LeaveSpinLock(&queryThreadLock);
  3955. /* unlock the queryThread */
  3956. racond_signal(queryThreadCond);
  3957. }
  3958. /* adds a new log to the queue */
  3959. void queryThread_log(char * entry, int length) {
  3960. int idx = logThreadData.count;
  3961. EnterSpinLock(&queryThreadLock);
  3962. if( logThreadData.count++ != 0 )
  3963. RECREATE(logThreadData.entry, char* , logThreadData.count);
  3964. CREATE(logThreadData.entry[idx], char, length + 1 );
  3965. safestrncpy(logThreadData.entry[idx], entry, length + 1 );
  3966. LeaveSpinLock(&queryThreadLock);
  3967. /* unlock the queryThread */
  3968. racond_signal(queryThreadCond);
  3969. }
  3970. /* queryThread_main */
  3971. static void *queryThread_main(void *x) {
  3972. Sql *queryThread_handle = Sql_Malloc();
  3973. int i;
  3974. if ( SQL_ERROR == Sql_Connect(queryThread_handle, map_server_id, map_server_pw, map_server_ip, map_server_port, map_server_db) ){
  3975. ShowError("Couldn't connect with uname='%s',passwd='%s',host='%s',port='%d',database='%s'\n",
  3976. map_server_id, map_server_pw, map_server_ip, map_server_port, map_server_db);
  3977. Sql_ShowDebug(queryThread_handle);
  3978. Sql_Free(queryThread_handle);
  3979. exit(EXIT_FAILURE);
  3980. }
  3981. if( strlen(default_codepage) > 0 )
  3982. if ( SQL_ERROR == Sql_SetEncoding(queryThread_handle, default_codepage) )
  3983. Sql_ShowDebug(queryThread_handle);
  3984. if( log_config.sql_logs ) {
  3985. logmysql_handle = Sql_Malloc();
  3986. if ( SQL_ERROR == Sql_Connect(logmysql_handle, log_db_id, log_db_pw, log_db_ip, log_db_port, log_db_db) ){
  3987. ShowError("Couldn't connect with uname='%s',passwd='%s',host='%s',port='%d',database='%s'\n",
  3988. log_db_id, log_db_pw, log_db_ip, log_db_port, log_db_db);
  3989. Sql_ShowDebug(logmysql_handle);
  3990. Sql_Free(logmysql_handle);
  3991. exit(EXIT_FAILURE);
  3992. }
  3993. if( strlen(default_codepage) > 0 )
  3994. if ( SQL_ERROR == Sql_SetEncoding(logmysql_handle, default_codepage) )
  3995. Sql_ShowDebug(logmysql_handle);
  3996. }
  3997. while( 1 ) {
  3998. if(queryThreadTerminate > 0)
  3999. break;
  4000. EnterSpinLock(&queryThreadLock);
  4001. /* mess with queryThreadData within the lock */
  4002. for( i = 0; i < queryThreadData.count; i++ ) {
  4003. struct queryThreadEntry *entry = queryThreadData.entry[i];
  4004. if( entry->ok )
  4005. continue;
  4006. else if ( !entry->st || !entry->st->stack ) {
  4007. entry->ok = true;/* dispose */
  4008. continue;
  4009. }
  4010. buildin_query_sql_sub(entry->st, entry->type ? logmysql_handle : queryThread_handle);
  4011. entry->ok = true;/* we're done with this */
  4012. }
  4013. /* also check for any logs in need to be sent */
  4014. if( log_config.sql_logs ) {
  4015. for( i = 0; i < logThreadData.count; i++ ) {
  4016. if( SQL_ERROR == Sql_Query(logmysql_handle, logThreadData.entry[i]) )
  4017. Sql_ShowDebug(logmysql_handle);
  4018. aFree(logThreadData.entry[i]);
  4019. }
  4020. logThreadData.count = 0;
  4021. }
  4022. LeaveSpinLock(&queryThreadLock);
  4023. ramutex_lock( queryThreadMutex );
  4024. racond_wait( queryThreadCond, queryThreadMutex, -1 );
  4025. ramutex_unlock( queryThreadMutex );
  4026. }
  4027. Sql_Free(queryThread_handle);
  4028. if( log_config.sql_logs ) {
  4029. Sql_Free(logmysql_handle);
  4030. }
  4031. return NULL;
  4032. }
  4033. #endif
  4034. /*==========================================
  4035. * Destructor
  4036. *------------------------------------------*/
  4037. void do_final_script() {
  4038. int i;
  4039. DBIterator *iter;
  4040. struct script_state *st;
  4041. #ifdef DEBUG_HASH
  4042. if (battle_config.etc_log)
  4043. {
  4044. FILE *fp = fopen("hash_dump.txt","wt");
  4045. if(fp) {
  4046. int count[SCRIPT_HASH_SIZE];
  4047. int count2[SCRIPT_HASH_SIZE]; // number of buckets with a certain number of items
  4048. int n=0;
  4049. int min=INT_MAX,max=0,zero=0;
  4050. double mean=0.0f;
  4051. double median=0.0f;
  4052. ShowNotice("Dumping script str hash information to hash_dump.txt\n");
  4053. memset(count, 0, sizeof(count));
  4054. fprintf(fp,"num : hash : data_name\n");
  4055. fprintf(fp,"---------------------------------------------------------------\n");
  4056. for(i=LABEL_START; i<str_num; i++) {
  4057. unsigned int h = calc_hash(get_str(i));
  4058. fprintf(fp,"%04d : %4u : %s\n",i,h, get_str(i));
  4059. ++count[h];
  4060. }
  4061. fprintf(fp,"--------------------\n\n");
  4062. memset(count2, 0, sizeof(count2));
  4063. for(i=0; i<SCRIPT_HASH_SIZE; i++) {
  4064. fprintf(fp," hash %3d = %d\n",i,count[i]);
  4065. if(min > count[i])
  4066. min = count[i]; // minimun count of collision
  4067. if(max < count[i])
  4068. max = count[i]; // maximun count of collision
  4069. if(count[i] == 0)
  4070. zero++;
  4071. ++count2[count[i]];
  4072. }
  4073. fprintf(fp,"\n--------------------\n items : buckets\n--------------------\n");
  4074. for( i=min; i <= max; ++i ){
  4075. fprintf(fp," %5d : %7d\n",i,count2[i]);
  4076. mean += 1.0f*i*count2[i]/SCRIPT_HASH_SIZE; // Note: this will always result in <nr labels>/<nr buckets>
  4077. }
  4078. for( i=min; i <= max; ++i ){
  4079. n += count2[i];
  4080. if( n*2 >= SCRIPT_HASH_SIZE )
  4081. {
  4082. if( SCRIPT_HASH_SIZE%2 == 0 && SCRIPT_HASH_SIZE/2 == n )
  4083. median = (i+i+1)/2.0f;
  4084. else
  4085. median = i;
  4086. break;
  4087. }
  4088. }
  4089. fprintf(fp,"--------------------\n min = %d, max = %d, zero = %d\n mean = %lf, median = %lf\n",min,max,zero,mean,median);
  4090. fclose(fp);
  4091. }
  4092. }
  4093. #endif
  4094. mapreg_final();
  4095. db_destroy(scriptlabel_db);
  4096. userfunc_db->destroy(userfunc_db, db_script_free_code_sub);
  4097. autobonus_db->destroy(autobonus_db, db_script_free_code_sub);
  4098. ers_destroy(array_ers);
  4099. if (generic_ui_array)
  4100. aFree(generic_ui_array);
  4101. iter = db_iterator(st_db);
  4102. for(st = dbi_first(iter); dbi_exists(iter); st = dbi_next(iter))
  4103. script_free_state(st);
  4104. dbi_destroy(iter);
  4105. if (str_data)
  4106. aFree(str_data);
  4107. if (str_buf)
  4108. aFree(str_buf);
  4109. for( i = 0; i < atcmd_binding_count; i++ ) {
  4110. aFree(atcmd_binding[i]);
  4111. }
  4112. if( atcmd_binding_count != 0 )
  4113. aFree(atcmd_binding);
  4114. ers_destroy(st_ers);
  4115. ers_destroy(stack_ers);
  4116. db_destroy(st_db);
  4117. #ifdef BETA_THREAD_TEST
  4118. /* QueryThread */
  4119. InterlockedIncrement(&queryThreadTerminate);
  4120. racond_signal(queryThreadCond);
  4121. rathread_wait(queryThread, NULL);
  4122. // Destroy cond var and mutex.
  4123. racond_destroy( queryThreadCond );
  4124. ramutex_destroy( queryThreadMutex );
  4125. /* Clear missing vars */
  4126. for( i = 0; i < queryThreadData.count; i++ ) {
  4127. aFree(queryThreadData.entry[i]);
  4128. }
  4129. aFree(queryThreadData.entry);
  4130. for( i = 0; i < logThreadData.count; i++ ) {
  4131. aFree(logThreadData.entry[i]);
  4132. }
  4133. aFree(logThreadData.entry);
  4134. #endif
  4135. }
  4136. /*==========================================
  4137. * Initialization
  4138. *------------------------------------------*/
  4139. void do_init_script(void) {
  4140. st_db = idb_alloc(DB_OPT_BASE);
  4141. userfunc_db = strdb_alloc(DB_OPT_DUP_KEY,0);
  4142. scriptlabel_db = strdb_alloc(DB_OPT_DUP_KEY,50);
  4143. autobonus_db = strdb_alloc(DB_OPT_DUP_KEY,0);
  4144. st_ers = ers_new(sizeof(struct script_state), "script.c::st_ers", ERS_CACHE_OPTIONS);
  4145. stack_ers = ers_new(sizeof(struct script_stack), "script.c::script_stack", ERS_OPT_FLEX_CHUNK);
  4146. 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
  4147. ers_chunk_size(st_ers, 10);
  4148. ers_chunk_size(stack_ers, 10);
  4149. active_scripts = 0;
  4150. next_id = 0;
  4151. mapreg_init();
  4152. #ifdef BETA_THREAD_TEST
  4153. CREATE(queryThreadData.entry, struct queryThreadEntry*, 1);
  4154. queryThreadData.count = 0;
  4155. CREATE(logThreadData.entry, char *, 1);
  4156. logThreadData.count = 0;
  4157. /* QueryThread Start */
  4158. InitializeSpinLock(&queryThreadLock);
  4159. queryThreadData.timer = INVALID_TIMER;
  4160. queryThreadTerminate = 0;
  4161. queryThreadMutex = ramutex_create();
  4162. queryThreadCond = racond_create();
  4163. queryThread = rathread_create(queryThread_main, NULL);
  4164. if(queryThread == NULL){
  4165. ShowFatalError("do_init_script: cannot spawn Query Thread.\n");
  4166. exit(EXIT_FAILURE);
  4167. }
  4168. add_timer_func_list(queryThread_timer, "queryThread_timer");
  4169. #endif
  4170. }
  4171. void script_reload(void) {
  4172. int i;
  4173. DBIterator *iter;
  4174. struct script_state *st;
  4175. #ifdef BETA_THREAD_TEST
  4176. /* we're reloading so any queries undergoing should be...exterminated. */
  4177. EnterSpinLock(&queryThreadLock);
  4178. for( i = 0; i < queryThreadData.count; i++ ) {
  4179. aFree(queryThreadData.entry[i]);
  4180. }
  4181. queryThreadData.count = 0;
  4182. if( queryThreadData.timer != INVALID_TIMER ) {
  4183. delete_timer(queryThreadData.timer, queryThread_timer);
  4184. queryThreadData.timer = INVALID_TIMER;
  4185. }
  4186. LeaveSpinLock(&queryThreadLock);
  4187. #endif
  4188. userfunc_db->clear(userfunc_db, db_script_free_code_sub);
  4189. db_clear(scriptlabel_db);
  4190. // @commands (script based)
  4191. // Clear bindings
  4192. for( i = 0; i < atcmd_binding_count; i++ ) {
  4193. aFree(atcmd_binding[i]);
  4194. }
  4195. if( atcmd_binding_count != 0 )
  4196. aFree(atcmd_binding);
  4197. atcmd_binding_count = 0;
  4198. iter = db_iterator(st_db);
  4199. for(st = dbi_first(iter); dbi_exists(iter); st = dbi_next(iter))
  4200. script_free_state(st);
  4201. dbi_destroy(iter);
  4202. db_clear(st_db);
  4203. mapreg_reload();
  4204. }
  4205. //-----------------------------------------------------------------------------
  4206. // buildin functions
  4207. //
  4208. #define BUILDIN_DEF(x,args) { buildin_ ## x , #x , args }
  4209. #define BUILDIN_DEF2(x,x2,args) { buildin_ ## x , x2 , args }
  4210. #define BUILDIN_FUNC(x) int buildin_ ## x (struct script_state* st)
  4211. /////////////////////////////////////////////////////////////////////
  4212. // NPC interaction
  4213. //
  4214. /// Appends a message to the npc dialog.
  4215. /// If a dialog doesn't exist yet, one is created.
  4216. ///
  4217. /// mes "<message>";
  4218. BUILDIN_FUNC(mes)
  4219. {
  4220. TBL_PC* sd = script_rid2sd(st);
  4221. if( sd == NULL )
  4222. return SCRIPT_CMD_SUCCESS;
  4223. if( !script_hasdata(st, 3) )
  4224. {// only a single line detected in the script
  4225. clif_scriptmes(sd, st->oid, script_getstr(st, 2));
  4226. }
  4227. else
  4228. {// parse multiple lines as they exist
  4229. int i;
  4230. for( i = 2; script_hasdata(st, i); i++ )
  4231. {
  4232. // send the message to the client
  4233. clif_scriptmes(sd, st->oid, script_getstr(st, i));
  4234. }
  4235. }
  4236. st->mes_active = 1; // Invoking character has a NPC dialog box open.
  4237. return SCRIPT_CMD_SUCCESS;
  4238. }
  4239. /// Displays the button 'next' in the npc dialog.
  4240. /// The dialog text is cleared and the script continues when the button is pressed.
  4241. ///
  4242. /// next;
  4243. BUILDIN_FUNC(next)
  4244. {
  4245. TBL_PC* sd;
  4246. sd = script_rid2sd(st);
  4247. if( sd == NULL )
  4248. return SCRIPT_CMD_SUCCESS;
  4249. #ifdef SECURE_NPCTIMEOUT
  4250. sd->npc_idle_type = NPCT_WAIT;
  4251. #endif
  4252. st->state = STOP;
  4253. clif_scriptnext(sd, st->oid);
  4254. return SCRIPT_CMD_SUCCESS;
  4255. }
  4256. /// Ends the script and displays the button 'close' on the npc dialog.
  4257. /// The dialog is closed when the button is pressed.
  4258. ///
  4259. /// close;
  4260. BUILDIN_FUNC(close)
  4261. {
  4262. TBL_PC* sd;
  4263. sd = script_rid2sd(st);
  4264. if( sd == NULL )
  4265. return SCRIPT_CMD_SUCCESS;
  4266. if( !st->mes_active ) {
  4267. TBL_NPC* nd = map_id2nd(st->oid);
  4268. st->state = END; // Keep backwards compatibility.
  4269. ShowWarning("Incorrect use of 'close' command! (source:%s / path:%s)\n",nd?nd->name:"Unknown",nd?nd->path:"Unknown");
  4270. } else {
  4271. st->state = CLOSE;
  4272. st->mes_active = 0;
  4273. }
  4274. clif_scriptclose(sd, st->oid);
  4275. return SCRIPT_CMD_SUCCESS;
  4276. }
  4277. /// Displays the button 'close' on the npc dialog.
  4278. /// The dialog is closed and the script continues when the button is pressed.
  4279. ///
  4280. /// close2;
  4281. BUILDIN_FUNC(close2)
  4282. {
  4283. TBL_PC* sd;
  4284. sd = script_rid2sd(st);
  4285. if( sd == NULL )
  4286. return SCRIPT_CMD_SUCCESS;
  4287. st->state = STOP;
  4288. if( st->mes_active )
  4289. st->mes_active = 0;
  4290. clif_scriptclose(sd, st->oid);
  4291. return SCRIPT_CMD_SUCCESS;
  4292. }
  4293. /// Counts the number of valid and total number of options in 'str'
  4294. /// If max_count > 0 the counting stops when that valid option is reached
  4295. /// total is incremented for each option (NULL is supported)
  4296. static int menu_countoptions(const char* str, int max_count, int* total)
  4297. {
  4298. int count = 0;
  4299. int bogus_total;
  4300. if( total == NULL )
  4301. total = &bogus_total;
  4302. ++(*total);
  4303. // initial empty options
  4304. while( *str == ':' )
  4305. {
  4306. ++str;
  4307. ++(*total);
  4308. }
  4309. // count menu options
  4310. while( *str != '\0' )
  4311. {
  4312. ++count;
  4313. --max_count;
  4314. if( max_count == 0 )
  4315. break;
  4316. while( *str != ':' && *str != '\0' )
  4317. ++str;
  4318. while( *str == ':' )
  4319. {
  4320. ++str;
  4321. ++(*total);
  4322. }
  4323. }
  4324. return count;
  4325. }
  4326. /// Displays a menu with options and goes to the target label.
  4327. /// The script is stopped if cancel is pressed.
  4328. /// Options with no text are not displayed in the client.
  4329. ///
  4330. /// Options can be grouped together, separated by the character ':' in the text:
  4331. /// ex: menu "A:B:C",L_target;
  4332. /// All these options go to the specified target label.
  4333. ///
  4334. /// The index of the selected option is put in the variable @menu.
  4335. /// Indexes start with 1 and are consistent with grouped and empty options.
  4336. /// ex: menu "A::B",-,"",L_Impossible,"C",-;
  4337. /// // displays "A", "B" and "C", corresponding to indexes 1, 3 and 5
  4338. ///
  4339. /// NOTE: the client closes the npc dialog when cancel is pressed
  4340. ///
  4341. /// menu "<option_text>",<target_label>{,"<option_text>",<target_label>,...};
  4342. BUILDIN_FUNC(menu)
  4343. {
  4344. int i;
  4345. const char* text;
  4346. TBL_PC* sd;
  4347. sd = script_rid2sd(st);
  4348. if( sd == NULL )
  4349. return SCRIPT_CMD_SUCCESS;
  4350. #ifdef SECURE_NPCTIMEOUT
  4351. sd->npc_idle_type = NPCT_MENU;
  4352. #endif
  4353. // TODO detect multiple scripts waiting for input at the same time, and what to do when that happens
  4354. if( sd->state.menu_or_input == 0 )
  4355. {
  4356. struct StringBuf buf;
  4357. if( script_lastdata(st) % 2 == 0 )
  4358. {// argument count is not even (1st argument is at index 2)
  4359. ShowError("buildin_menu: Illegal number of arguments (%d).\n", (script_lastdata(st) - 1));
  4360. st->state = END;
  4361. return SCRIPT_CMD_FAILURE;
  4362. }
  4363. StringBuf_Init(&buf);
  4364. sd->npc_menu = 0;
  4365. for( i = 2; i < script_lastdata(st); i += 2 )
  4366. {
  4367. struct script_data* data;
  4368. // menu options
  4369. text = script_getstr(st, i);
  4370. // target label
  4371. data = script_getdata(st, i+1);
  4372. if( !data_islabel(data) )
  4373. {// not a label
  4374. StringBuf_Destroy(&buf);
  4375. ShowError("buildin_menu: Argument #%d (from 1) is not a label or label not found.\n", i);
  4376. script_reportdata(data);
  4377. st->state = END;
  4378. return SCRIPT_CMD_FAILURE;
  4379. }
  4380. // append option(s)
  4381. if( text[0] == '\0' )
  4382. continue;// empty string, ignore
  4383. if( sd->npc_menu > 0 )
  4384. StringBuf_AppendStr(&buf, ":");
  4385. StringBuf_AppendStr(&buf, text);
  4386. sd->npc_menu += menu_countoptions(text, 0, NULL);
  4387. }
  4388. st->state = RERUNLINE;
  4389. sd->state.menu_or_input = 1;
  4390. /**
  4391. * menus beyond this length crash the client (see bugreport:6402)
  4392. **/
  4393. if( StringBuf_Length(&buf) >= 2047 ) {
  4394. struct npc_data * nd = map_id2nd(st->oid);
  4395. char* menu;
  4396. CREATE(menu, char, 2048);
  4397. safestrncpy(menu, StringBuf_Value(&buf), 2047);
  4398. ShowWarning("buildin_menu: NPC Menu too long! (source:%s / length:%d)\n",nd?nd->name:"Unknown",StringBuf_Length(&buf));
  4399. clif_scriptmenu(sd, st->oid, menu);
  4400. aFree(menu);
  4401. } else
  4402. clif_scriptmenu(sd, st->oid, StringBuf_Value(&buf));
  4403. StringBuf_Destroy(&buf);
  4404. if( sd->npc_menu >= 0xff )
  4405. {// 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
  4406. ShowWarning("buildin_menu: Too many options specified (current=%d, max=254).\n", sd->npc_menu);
  4407. script_reportsrc(st);
  4408. }
  4409. }
  4410. else if( sd->npc_menu == 0xff )
  4411. {// Cancel was pressed
  4412. sd->state.menu_or_input = 0;
  4413. st->state = END;
  4414. }
  4415. else
  4416. {// goto target label
  4417. int menu = 0;
  4418. sd->state.menu_or_input = 0;
  4419. if( sd->npc_menu <= 0 )
  4420. {
  4421. ShowDebug("buildin_menu: Unexpected selection (%d)\n", sd->npc_menu);
  4422. st->state = END;
  4423. return SCRIPT_CMD_FAILURE;
  4424. }
  4425. // get target label
  4426. for( i = 2; i < script_lastdata(st); i += 2 )
  4427. {
  4428. text = script_getstr(st, i);
  4429. sd->npc_menu -= menu_countoptions(text, sd->npc_menu, &menu);
  4430. if( sd->npc_menu <= 0 )
  4431. break;// entry found
  4432. }
  4433. if( sd->npc_menu > 0 )
  4434. {// Invalid selection
  4435. ShowDebug("buildin_menu: Selection is out of range (%d pairs are missing?) - please report this\n", sd->npc_menu);
  4436. st->state = END;
  4437. return SCRIPT_CMD_FAILURE;
  4438. }
  4439. if( !data_islabel(script_getdata(st, i + 1)) )
  4440. {// TODO remove this temporary crash-prevention code (fallback for multiple scripts requesting user input)
  4441. ShowError("buildin_menu: Unexpected data in label argument\n");
  4442. script_reportdata(script_getdata(st, i + 1));
  4443. st->state = END;
  4444. return SCRIPT_CMD_FAILURE;
  4445. }
  4446. pc_setreg(sd, add_str("@menu"), menu);
  4447. st->pos = script_getnum(st, i + 1);
  4448. st->state = GOTO;
  4449. }
  4450. return SCRIPT_CMD_SUCCESS;
  4451. }
  4452. /// Displays a menu with options and returns the selected option.
  4453. /// Behaves like 'menu' without the target labels.
  4454. ///
  4455. /// select(<option_text>{,<option_text>,...}) -> <selected_option>
  4456. ///
  4457. /// @see menu
  4458. BUILDIN_FUNC(select)
  4459. {
  4460. int i;
  4461. const char* text;
  4462. TBL_PC* sd;
  4463. sd = script_rid2sd(st);
  4464. if( sd == NULL )
  4465. return SCRIPT_CMD_SUCCESS;
  4466. #ifdef SECURE_NPCTIMEOUT
  4467. sd->npc_idle_type = NPCT_MENU;
  4468. #endif
  4469. if( sd->state.menu_or_input == 0 ) {
  4470. struct StringBuf buf;
  4471. StringBuf_Init(&buf);
  4472. sd->npc_menu = 0;
  4473. for( i = 2; i <= script_lastdata(st); ++i ) {
  4474. text = script_getstr(st, i);
  4475. if( sd->npc_menu > 0 )
  4476. StringBuf_AppendStr(&buf, ":");
  4477. StringBuf_AppendStr(&buf, text);
  4478. sd->npc_menu += menu_countoptions(text, 0, NULL);
  4479. }
  4480. st->state = RERUNLINE;
  4481. sd->state.menu_or_input = 1;
  4482. /**
  4483. * menus beyond this length crash the client (see bugreport:6402)
  4484. **/
  4485. if( StringBuf_Length(&buf) >= 2047 ) {
  4486. struct npc_data * nd = map_id2nd(st->oid);
  4487. char* menu;
  4488. CREATE(menu, char, 2048);
  4489. safestrncpy(menu, StringBuf_Value(&buf), 2047);
  4490. ShowWarning("buildin_select: NPC Menu too long! (source:%s / length:%d)\n",nd?nd->name:"Unknown",StringBuf_Length(&buf));
  4491. clif_scriptmenu(sd, st->oid, menu);
  4492. aFree(menu);
  4493. } else
  4494. clif_scriptmenu(sd, st->oid, StringBuf_Value(&buf));
  4495. StringBuf_Destroy(&buf);
  4496. if( sd->npc_menu >= 0xff ) {
  4497. ShowWarning("buildin_select: Too many options specified (current=%d, max=254).\n", sd->npc_menu);
  4498. script_reportsrc(st);
  4499. }
  4500. } else if( sd->npc_menu == 0xff ) {// Cancel was pressed
  4501. sd->state.menu_or_input = 0;
  4502. st->state = END;
  4503. } else {// return selected option
  4504. int menu = 0;
  4505. sd->state.menu_or_input = 0;
  4506. for( i = 2; i <= script_lastdata(st); ++i ) {
  4507. text = script_getstr(st, i);
  4508. sd->npc_menu -= menu_countoptions(text, sd->npc_menu, &menu);
  4509. if( sd->npc_menu <= 0 )
  4510. break;// entry found
  4511. }
  4512. pc_setreg(sd, add_str("@menu"), menu);
  4513. script_pushint(st, menu);
  4514. st->state = RUN;
  4515. }
  4516. return SCRIPT_CMD_SUCCESS;
  4517. }
  4518. /// Displays a menu with options and returns the selected option.
  4519. /// Behaves like 'menu' without the target labels, except when cancel is
  4520. /// pressed.
  4521. /// When cancel is pressed, the script continues and 255 is returned.
  4522. ///
  4523. /// prompt(<option_text>{,<option_text>,...}) -> <selected_option>
  4524. ///
  4525. /// @see menu
  4526. BUILDIN_FUNC(prompt)
  4527. {
  4528. int i;
  4529. const char *text;
  4530. TBL_PC* sd;
  4531. sd = script_rid2sd(st);
  4532. if( sd == NULL )
  4533. return SCRIPT_CMD_SUCCESS;
  4534. #ifdef SECURE_NPCTIMEOUT
  4535. sd->npc_idle_type = NPCT_MENU;
  4536. #endif
  4537. if( sd->state.menu_or_input == 0 )
  4538. {
  4539. struct StringBuf buf;
  4540. StringBuf_Init(&buf);
  4541. sd->npc_menu = 0;
  4542. for( i = 2; i <= script_lastdata(st); ++i )
  4543. {
  4544. text = script_getstr(st, i);
  4545. if( sd->npc_menu > 0 )
  4546. StringBuf_AppendStr(&buf, ":");
  4547. StringBuf_AppendStr(&buf, text);
  4548. sd->npc_menu += menu_countoptions(text, 0, NULL);
  4549. }
  4550. st->state = RERUNLINE;
  4551. sd->state.menu_or_input = 1;
  4552. /**
  4553. * menus beyond this length crash the client (see bugreport:6402)
  4554. **/
  4555. if( StringBuf_Length(&buf) >= 2047 ) {
  4556. struct npc_data * nd = map_id2nd(st->oid);
  4557. char* menu;
  4558. CREATE(menu, char, 2048);
  4559. safestrncpy(menu, StringBuf_Value(&buf), 2047);
  4560. ShowWarning("buildin_prompt: NPC Menu too long! (source:%s / length:%d)\n",nd?nd->name:"Unknown",StringBuf_Length(&buf));
  4561. clif_scriptmenu(sd, st->oid, menu);
  4562. aFree(menu);
  4563. } else
  4564. clif_scriptmenu(sd, st->oid, StringBuf_Value(&buf));
  4565. StringBuf_Destroy(&buf);
  4566. if( sd->npc_menu >= 0xff )
  4567. {
  4568. ShowWarning("buildin_prompt: Too many options specified (current=%d, max=254).\n", sd->npc_menu);
  4569. script_reportsrc(st);
  4570. }
  4571. }
  4572. else if( sd->npc_menu == 0xff )
  4573. {// Cancel was pressed
  4574. sd->state.menu_or_input = 0;
  4575. pc_setreg(sd, add_str("@menu"), 0xff);
  4576. script_pushint(st, 0xff);
  4577. st->state = RUN;
  4578. }
  4579. else
  4580. {// return selected option
  4581. int menu = 0;
  4582. sd->state.menu_or_input = 0;
  4583. for( i = 2; i <= script_lastdata(st); ++i )
  4584. {
  4585. text = script_getstr(st, i);
  4586. sd->npc_menu -= menu_countoptions(text, sd->npc_menu, &menu);
  4587. if( sd->npc_menu <= 0 )
  4588. break;// entry found
  4589. }
  4590. pc_setreg(sd, add_str("@menu"), menu);
  4591. script_pushint(st, menu);
  4592. st->state = RUN;
  4593. }
  4594. return SCRIPT_CMD_SUCCESS;
  4595. }
  4596. /////////////////////////////////////////////////////////////////////
  4597. // ...
  4598. //
  4599. /// Jumps to the target script label.
  4600. ///
  4601. /// goto <label>;
  4602. BUILDIN_FUNC(goto)
  4603. {
  4604. if( !data_islabel(script_getdata(st,2)) )
  4605. {
  4606. ShowError("buildin_goto: Not a label\n");
  4607. script_reportdata(script_getdata(st,2));
  4608. st->state = END;
  4609. return SCRIPT_CMD_FAILURE;
  4610. }
  4611. st->pos = script_getnum(st,2);
  4612. st->state = GOTO;
  4613. return SCRIPT_CMD_SUCCESS;
  4614. }
  4615. /*==========================================
  4616. * user-defined function call
  4617. *------------------------------------------*/
  4618. BUILDIN_FUNC(callfunc)
  4619. {
  4620. int i, j;
  4621. struct script_retinfo* ri;
  4622. struct script_code* scr;
  4623. const char* str = script_getstr(st,2);
  4624. struct reg_db *ref = NULL;
  4625. scr = (struct script_code*)strdb_get(userfunc_db, str);
  4626. if(!scr) {
  4627. ShowError("buildin_callfunc: Function not found! [%s]\n", str);
  4628. st->state = END;
  4629. return SCRIPT_CMD_FAILURE;
  4630. }
  4631. ref = (struct reg_db *)aCalloc(sizeof(struct reg_db), 2);
  4632. ref[0].vars = st->stack->scope.vars;
  4633. if (!st->stack->scope.arrays)
  4634. st->stack->scope.arrays = idb_alloc(DB_OPT_BASE); // TODO: Can this happen? when?
  4635. ref[0].arrays = st->stack->scope.arrays;
  4636. ref[1].vars = st->script->local.vars;
  4637. if (!st->script->local.arrays)
  4638. st->script->local.arrays = idb_alloc(DB_OPT_BASE); // TODO: Can this happen? when?
  4639. ref[1].arrays = st->script->local.arrays;
  4640. for(i = st->start+3, j = 0; i < st->end; i++, j++) {
  4641. struct script_data* data = push_copy(st->stack,i);
  4642. if (data_isreference(data) && !data->ref) {
  4643. const char* name = reference_getname(data);
  4644. if (name[0] == '.')
  4645. data->ref = (name[1] == '@' ? &ref[0] : &ref[1]);
  4646. }
  4647. }
  4648. CREATE(ri, struct script_retinfo, 1);
  4649. ri->script = st->script; // script code
  4650. ri->scope.vars = st->stack->scope.vars; // scope variables
  4651. ri->scope.arrays = st->stack->scope.arrays; // scope arrays
  4652. ri->pos = st->pos; // script location
  4653. ri->nargs = j; // argument count
  4654. ri->defsp = st->stack->defsp; // default stack pointer
  4655. push_retinfo(st->stack, ri, ref);
  4656. st->pos = 0;
  4657. st->script = scr;
  4658. st->stack->defsp = st->stack->sp;
  4659. st->state = GOTO;
  4660. st->stack->scope.vars = i64db_alloc(DB_OPT_RELEASE_DATA);
  4661. st->stack->scope.arrays = idb_alloc(DB_OPT_BASE);
  4662. if (!st->script->local.vars)
  4663. st->script->local.vars = i64db_alloc(DB_OPT_RELEASE_DATA);
  4664. return SCRIPT_CMD_SUCCESS;
  4665. }
  4666. /*==========================================
  4667. * subroutine call
  4668. *------------------------------------------*/
  4669. BUILDIN_FUNC(callsub)
  4670. {
  4671. int i,j;
  4672. struct script_retinfo* ri;
  4673. int pos = script_getnum(st,2);
  4674. struct reg_db *ref = NULL;
  4675. if( !data_islabel(script_getdata(st,2)) && !data_isfunclabel(script_getdata(st,2)) ) {
  4676. ShowError("buildin_callsub: Argument is not a label\n");
  4677. script_reportdata(script_getdata(st,2));
  4678. st->state = END;
  4679. return SCRIPT_CMD_FAILURE;
  4680. }
  4681. ref = (struct reg_db *)aCalloc(sizeof(struct reg_db), 1);
  4682. ref[0].vars = st->stack->scope.vars;
  4683. if (!st->stack->scope.arrays)
  4684. st->stack->scope.arrays = idb_alloc(DB_OPT_BASE); // TODO: Can this happen? when?
  4685. ref[0].arrays = st->stack->scope.arrays;
  4686. for(i = st->start+3, j = 0; i < st->end; i++, j++) {
  4687. struct script_data* data = push_copy(st->stack,i);
  4688. if (data_isreference(data) && !data->ref) {
  4689. const char* name = reference_getname(data);
  4690. if (name[0] == '.' && name[1] == '@')
  4691. data->ref = &ref[0];
  4692. }
  4693. }
  4694. CREATE(ri, struct script_retinfo, 1);
  4695. ri->script = st->script; // script code
  4696. ri->scope.vars = st->stack->scope.vars; // scope variables
  4697. ri->scope.arrays = st->stack->scope.arrays; // scope arrays
  4698. ri->pos = st->pos; // script location
  4699. ri->nargs = j; // argument count
  4700. ri->defsp = st->stack->defsp; // default stack pointer
  4701. push_retinfo(st->stack, ri, ref);
  4702. st->pos = pos;
  4703. st->stack->defsp = st->stack->sp;
  4704. st->state = GOTO;
  4705. st->stack->scope.vars = i64db_alloc(DB_OPT_RELEASE_DATA);
  4706. st->stack->scope.arrays = idb_alloc(DB_OPT_BASE);
  4707. return SCRIPT_CMD_SUCCESS;
  4708. }
  4709. /// Retrieves an argument provided to callfunc/callsub.
  4710. /// If the argument doesn't exist
  4711. ///
  4712. /// getarg(<index>{,<default_value>}) -> <value>
  4713. BUILDIN_FUNC(getarg)
  4714. {
  4715. struct script_retinfo* ri;
  4716. int idx;
  4717. if( st->stack->defsp < 1 || st->stack->stack_data[st->stack->defsp - 1].type != C_RETINFO )
  4718. {
  4719. ShowError("buildin_getarg: No callfunc or callsub!\n");
  4720. st->state = END;
  4721. return SCRIPT_CMD_FAILURE;
  4722. }
  4723. ri = st->stack->stack_data[st->stack->defsp - 1].u.ri;
  4724. idx = script_getnum(st,2);
  4725. if( idx >= 0 && idx < ri->nargs )
  4726. push_copy(st->stack, st->stack->defsp - 1 - ri->nargs + idx);
  4727. else if( script_hasdata(st,3) )
  4728. script_pushcopy(st, 3);
  4729. else
  4730. {
  4731. ShowError("buildin_getarg: Index (idx=%d) out of range (nargs=%d) and no default value found\n", idx, ri->nargs);
  4732. st->state = END;
  4733. return SCRIPT_CMD_FAILURE;
  4734. }
  4735. return SCRIPT_CMD_SUCCESS;
  4736. }
  4737. /// Returns from the current function, optionaly returning a value from the functions.
  4738. /// Don't use outside script functions.
  4739. ///
  4740. /// return;
  4741. /// return <value>;
  4742. BUILDIN_FUNC(return)
  4743. {
  4744. if( script_hasdata(st,2) )
  4745. {// return value
  4746. struct script_data* data;
  4747. script_pushcopy(st, 2);
  4748. data = script_getdatatop(st, -1);
  4749. if( data_isreference(data) ) {
  4750. const char* name = reference_getname(data);
  4751. if( name[0] == '.' && name[1] == '@' ) { // scope variable
  4752. if( !data->ref || data->ref->vars == st->stack->scope.vars )
  4753. get_val(st, data); // current scope, convert to value
  4754. if( data->ref && data->ref->vars == st->stack->stack_data[st->stack->defsp-1].u.ri->scope.vars )
  4755. data->ref = NULL; // Reference to the parent scope, remove reference pointer
  4756. } else if( name[0] == '.' && !data->ref ) { // script variable, link to current script
  4757. data->ref = (struct reg_db *)aCalloc(sizeof(struct reg_db), 1);
  4758. data->ref->vars = st->script->local.vars;
  4759. if (!st->script->local.arrays)
  4760. st->script->local.arrays = idb_alloc(DB_OPT_BASE);
  4761. data->ref->arrays = st->script->local.arrays;
  4762. }
  4763. }
  4764. }
  4765. else
  4766. {// no return value
  4767. script_pushnil(st);
  4768. }
  4769. st->state = RETFUNC;
  4770. return SCRIPT_CMD_SUCCESS;
  4771. }
  4772. /// Returns a random number from 0 to <range>-1.
  4773. /// Or returns a random number from <min> to <max>.
  4774. /// If <min> is greater than <max>, their numbers are switched.
  4775. /// rand(<range>) -> <int>
  4776. /// rand(<min>,<max>) -> <int>
  4777. BUILDIN_FUNC(rand)
  4778. {
  4779. int range;
  4780. int min;
  4781. if( script_hasdata(st,3) )
  4782. {// min,max
  4783. int max = script_getnum(st,3);
  4784. min = script_getnum(st,2);
  4785. if( max < min )
  4786. swap(min, max);
  4787. range = max - min + 1;
  4788. }
  4789. else
  4790. {// range
  4791. min = 0;
  4792. range = script_getnum(st,2);
  4793. }
  4794. if( range <= 1 )
  4795. script_pushint(st, min);
  4796. else
  4797. script_pushint(st, rnd()%range + min);
  4798. return SCRIPT_CMD_SUCCESS;
  4799. }
  4800. /*==========================================
  4801. * Warp sd to str,x,y or Random or SavePoint/Save
  4802. *------------------------------------------*/
  4803. BUILDIN_FUNC(warp)
  4804. {
  4805. int ret;
  4806. int x,y;
  4807. const char* str;
  4808. TBL_PC* sd;
  4809. sd = script_rid2sd(st);
  4810. if( sd == NULL )
  4811. return SCRIPT_CMD_SUCCESS;
  4812. str = script_getstr(st,2);
  4813. x = script_getnum(st,3);
  4814. y = script_getnum(st,4);
  4815. if(strcmp(str,"Random")==0)
  4816. ret = pc_randomwarp(sd,CLR_TELEPORT);
  4817. else if(strcmp(str,"SavePoint")==0 || strcmp(str,"Save")==0)
  4818. ret = pc_setpos(sd,sd->status.save_point.map,sd->status.save_point.x,sd->status.save_point.y,CLR_TELEPORT);
  4819. else
  4820. ret = pc_setpos(sd,mapindex_name2id(str),x,y,CLR_OUTSIGHT);
  4821. if( ret ) {
  4822. ShowError("buildin_warp: moving player '%s' to \"%s\",%d,%d failed.\n", sd->status.name, str, x, y);
  4823. return SCRIPT_CMD_FAILURE;
  4824. }
  4825. return SCRIPT_CMD_SUCCESS;
  4826. }
  4827. /*==========================================
  4828. * Warp a specified area
  4829. *------------------------------------------*/
  4830. static int buildin_areawarp_sub(struct block_list *bl,va_list ap)
  4831. {
  4832. int x2,y2,x3,y3;
  4833. unsigned int index;
  4834. index = va_arg(ap,unsigned int);
  4835. x2 = va_arg(ap,int);
  4836. y2 = va_arg(ap,int);
  4837. x3 = va_arg(ap,int);
  4838. y3 = va_arg(ap,int);
  4839. if(index == 0)
  4840. pc_randomwarp((TBL_PC *)bl,CLR_TELEPORT);
  4841. else if(x3 && y3) {
  4842. int max, tx, ty, j = 0;
  4843. // choose a suitable max number of attempts
  4844. if( (max = (y3-y2+1)*(x3-x2+1)*3) > 1000 )
  4845. max = 1000;
  4846. // find a suitable map cell
  4847. do {
  4848. tx = rnd()%(x3-x2+1)+x2;
  4849. ty = rnd()%(y3-y2+1)+y2;
  4850. j++;
  4851. } while( map_getcell(index,tx,ty,CELL_CHKNOPASS) && j < max );
  4852. pc_setpos((TBL_PC *)bl,index,tx,ty,CLR_OUTSIGHT);
  4853. }
  4854. else
  4855. pc_setpos((TBL_PC *)bl,index,x2,y2,CLR_OUTSIGHT);
  4856. return 0;
  4857. }
  4858. BUILDIN_FUNC(areawarp)
  4859. {
  4860. int16 m, x0,y0,x1,y1, x2,y2,x3=0,y3=0;
  4861. unsigned int index;
  4862. const char *str;
  4863. const char *mapname;
  4864. mapname = script_getstr(st,2);
  4865. x0 = script_getnum(st,3);
  4866. y0 = script_getnum(st,4);
  4867. x1 = script_getnum(st,5);
  4868. y1 = script_getnum(st,6);
  4869. str = script_getstr(st,7);
  4870. x2 = script_getnum(st,8);
  4871. y2 = script_getnum(st,9);
  4872. if( script_hasdata(st,10) && script_hasdata(st,11) ) { // Warp area to area
  4873. if( (x3 = script_getnum(st,10)) < 0 || (y3 = script_getnum(st,11)) < 0 ){
  4874. x3 = 0;
  4875. y3 = 0;
  4876. } else if( x3 && y3 ) {
  4877. // normalize x3/y3 coordinates
  4878. if( x3 < x2 ) swap(x3,x2);
  4879. if( y3 < y2 ) swap(y3,y2);
  4880. }
  4881. }
  4882. if( (m = map_mapname2mapid(mapname)) < 0 )
  4883. return SCRIPT_CMD_FAILURE;
  4884. if( strcmp(str,"Random") == 0 )
  4885. index = 0;
  4886. else if( !(index=mapindex_name2id(str)) )
  4887. return SCRIPT_CMD_FAILURE;
  4888. map_foreachinarea(buildin_areawarp_sub, m,x0,y0,x1,y1, BL_PC, index,x2,y2,x3,y3);
  4889. return SCRIPT_CMD_SUCCESS;
  4890. }
  4891. /*==========================================
  4892. * areapercentheal <map>,<x1>,<y1>,<x2>,<y2>,<hp>,<sp>
  4893. *------------------------------------------*/
  4894. static int buildin_areapercentheal_sub(struct block_list *bl,va_list ap)
  4895. {
  4896. int hp, sp;
  4897. hp = va_arg(ap, int);
  4898. sp = va_arg(ap, int);
  4899. pc_percentheal((TBL_PC *)bl,hp,sp);
  4900. return 0;
  4901. }
  4902. BUILDIN_FUNC(areapercentheal)
  4903. {
  4904. int hp,sp,m;
  4905. const char *mapname;
  4906. int x0,y0,x1,y1;
  4907. mapname=script_getstr(st,2);
  4908. x0=script_getnum(st,3);
  4909. y0=script_getnum(st,4);
  4910. x1=script_getnum(st,5);
  4911. y1=script_getnum(st,6);
  4912. hp=script_getnum(st,7);
  4913. sp=script_getnum(st,8);
  4914. if( (m=map_mapname2mapid(mapname))< 0)
  4915. return SCRIPT_CMD_FAILURE;
  4916. map_foreachinarea(buildin_areapercentheal_sub,m,x0,y0,x1,y1,BL_PC,hp,sp);
  4917. return SCRIPT_CMD_SUCCESS;
  4918. }
  4919. /*==========================================
  4920. * warpchar [LuzZza]
  4921. * Useful for warp one player from
  4922. * another player npc-session.
  4923. * Using: warpchar "mapname",x,y,Char_ID;
  4924. *------------------------------------------*/
  4925. BUILDIN_FUNC(warpchar)
  4926. {
  4927. int x,y,a;
  4928. const char *str;
  4929. TBL_PC *sd;
  4930. str=script_getstr(st,2);
  4931. x=script_getnum(st,3);
  4932. y=script_getnum(st,4);
  4933. a=script_getnum(st,5);
  4934. sd = map_charid2sd(a);
  4935. if( sd == NULL )
  4936. return SCRIPT_CMD_SUCCESS;
  4937. if(strcmp(str, "Random") == 0)
  4938. pc_randomwarp(sd, CLR_TELEPORT);
  4939. else
  4940. if(strcmp(str, "SavePoint") == 0)
  4941. pc_setpos(sd, sd->status.save_point.map,sd->status.save_point.x, sd->status.save_point.y, CLR_TELEPORT);
  4942. else
  4943. pc_setpos(sd, mapindex_name2id(str), x, y, CLR_TELEPORT);
  4944. return SCRIPT_CMD_SUCCESS;
  4945. }
  4946. /*==========================================
  4947. * Warpparty - [Fredzilla] [Paradox924X]
  4948. * Syntax: warpparty "to_mapname",x,y,Party_ID,{"from_mapname"};
  4949. * If 'from_mapname' is specified, only the party members on that map will be warped
  4950. *------------------------------------------*/
  4951. BUILDIN_FUNC(warpparty)
  4952. {
  4953. TBL_PC *sd = NULL;
  4954. TBL_PC *pl_sd;
  4955. struct party_data* p;
  4956. int type;
  4957. int mapindex;
  4958. int i;
  4959. const char* str = script_getstr(st,2);
  4960. int x = script_getnum(st,3);
  4961. int y = script_getnum(st,4);
  4962. int p_id = script_getnum(st,5);
  4963. const char* str2 = NULL;
  4964. if ( script_hasdata(st,6) )
  4965. str2 = script_getstr(st,6);
  4966. p = party_search(p_id);
  4967. if(!p)
  4968. return SCRIPT_CMD_SUCCESS;
  4969. type = ( strcmp(str,"Random")==0 ) ? 0
  4970. : ( strcmp(str,"SavePointAll")==0 ) ? 1
  4971. : ( strcmp(str,"SavePoint")==0 ) ? 2
  4972. : ( strcmp(str,"Leader")==0 ) ? 3
  4973. : 4;
  4974. switch (type)
  4975. {
  4976. case 3:
  4977. for(i = 0; i < MAX_PARTY && !p->party.member[i].leader; i++);
  4978. if (i == MAX_PARTY || !p->data[i].sd) //Leader not found / not online
  4979. return SCRIPT_CMD_FAILURE;
  4980. pl_sd = p->data[i].sd;
  4981. mapindex = pl_sd->mapindex;
  4982. x = pl_sd->bl.x;
  4983. y = pl_sd->bl.y;
  4984. break;
  4985. case 4:
  4986. mapindex = mapindex_name2id(str);
  4987. break;
  4988. case 2:
  4989. //"SavePoint" uses save point of the currently attached player
  4990. if (( sd = script_rid2sd(st) ) == NULL )
  4991. return SCRIPT_CMD_SUCCESS;
  4992. default:
  4993. mapindex = 0;
  4994. break;
  4995. }
  4996. for (i = 0; i < MAX_PARTY; i++)
  4997. {
  4998. if( !(pl_sd = p->data[i].sd) || pl_sd->status.party_id != p_id )
  4999. continue;
  5000. if( str2 && strcmp(str2, map[pl_sd->bl.m].name) != 0 )
  5001. continue;
  5002. if( pc_isdead(pl_sd) )
  5003. continue;
  5004. switch( type )
  5005. {
  5006. case 0: // Random
  5007. if(!map[pl_sd->bl.m].flag.nowarp)
  5008. pc_randomwarp(pl_sd,CLR_TELEPORT);
  5009. break;
  5010. case 1: // SavePointAll
  5011. if(!map[pl_sd->bl.m].flag.noreturn)
  5012. pc_setpos(pl_sd,pl_sd->status.save_point.map,pl_sd->status.save_point.x,pl_sd->status.save_point.y,CLR_TELEPORT);
  5013. break;
  5014. case 2: // SavePoint
  5015. if(!map[pl_sd->bl.m].flag.noreturn)
  5016. pc_setpos(pl_sd,sd->status.save_point.map,sd->status.save_point.x,sd->status.save_point.y,CLR_TELEPORT);
  5017. break;
  5018. case 3: // Leader
  5019. case 4: // m,x,y
  5020. if(!map[pl_sd->bl.m].flag.noreturn && !map[pl_sd->bl.m].flag.nowarp)
  5021. pc_setpos(pl_sd,mapindex,x,y,CLR_TELEPORT);
  5022. break;
  5023. }
  5024. }
  5025. return SCRIPT_CMD_SUCCESS;
  5026. }
  5027. /*==========================================
  5028. * Warpguild - [Fredzilla]
  5029. * Syntax: warpguild "mapname",x,y,Guild_ID;
  5030. *------------------------------------------*/
  5031. BUILDIN_FUNC(warpguild)
  5032. {
  5033. TBL_PC *sd = NULL;
  5034. TBL_PC *pl_sd;
  5035. struct guild* g;
  5036. struct s_mapiterator* iter;
  5037. int type;
  5038. const char* str = script_getstr(st,2);
  5039. int x = script_getnum(st,3);
  5040. int y = script_getnum(st,4);
  5041. int gid = script_getnum(st,5);
  5042. g = guild_search(gid);
  5043. if( g == NULL )
  5044. return SCRIPT_CMD_SUCCESS;
  5045. type = ( strcmp(str,"Random")==0 ) ? 0
  5046. : ( strcmp(str,"SavePointAll")==0 ) ? 1
  5047. : ( strcmp(str,"SavePoint")==0 ) ? 2
  5048. : 3;
  5049. if( type == 2 && ( sd = script_rid2sd(st) ) == NULL )
  5050. {// "SavePoint" uses save point of the currently attached player
  5051. return SCRIPT_CMD_SUCCESS;
  5052. }
  5053. iter = mapit_getallusers();
  5054. for( pl_sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); pl_sd = (TBL_PC*)mapit_next(iter) )
  5055. {
  5056. if( pl_sd->status.guild_id != gid )
  5057. continue;
  5058. switch( type )
  5059. {
  5060. case 0: // Random
  5061. if(!map[pl_sd->bl.m].flag.nowarp)
  5062. pc_randomwarp(pl_sd,CLR_TELEPORT);
  5063. break;
  5064. case 1: // SavePointAll
  5065. if(!map[pl_sd->bl.m].flag.noreturn)
  5066. pc_setpos(pl_sd,pl_sd->status.save_point.map,pl_sd->status.save_point.x,pl_sd->status.save_point.y,CLR_TELEPORT);
  5067. break;
  5068. case 2: // SavePoint
  5069. if(!map[pl_sd->bl.m].flag.noreturn)
  5070. pc_setpos(pl_sd,sd->status.save_point.map,sd->status.save_point.x,sd->status.save_point.y,CLR_TELEPORT);
  5071. break;
  5072. case 3: // m,x,y
  5073. if(!map[pl_sd->bl.m].flag.noreturn && !map[pl_sd->bl.m].flag.nowarp)
  5074. pc_setpos(pl_sd,mapindex_name2id(str),x,y,CLR_TELEPORT);
  5075. break;
  5076. }
  5077. }
  5078. mapit_free(iter);
  5079. return SCRIPT_CMD_SUCCESS;
  5080. }
  5081. /*==========================================
  5082. * Force Heal a player (hp and sp)
  5083. *------------------------------------------*/
  5084. BUILDIN_FUNC(heal)
  5085. {
  5086. TBL_PC *sd;
  5087. int hp,sp;
  5088. if (!script_charid2sd(4,sd))
  5089. return SCRIPT_CMD_SUCCESS;
  5090. hp=script_getnum(st,2);
  5091. sp=script_getnum(st,3);
  5092. status_heal(&sd->bl, hp, sp, 1);
  5093. return SCRIPT_CMD_SUCCESS;
  5094. }
  5095. /*==========================================
  5096. * Heal a player by item (get vit bonus etc)
  5097. *------------------------------------------*/
  5098. BUILDIN_FUNC(itemheal)
  5099. {
  5100. TBL_PC *sd;
  5101. int hp,sp;
  5102. hp=script_getnum(st,2);
  5103. sp=script_getnum(st,3);
  5104. if(potion_flag==1) {
  5105. potion_hp = hp;
  5106. potion_sp = sp;
  5107. return SCRIPT_CMD_SUCCESS;
  5108. }
  5109. if (!script_charid2sd(4,sd))
  5110. return SCRIPT_CMD_SUCCESS;
  5111. pc_itemheal(sd,sd->itemid,hp,sp);
  5112. return SCRIPT_CMD_SUCCESS;
  5113. }
  5114. /*==========================================
  5115. *
  5116. *------------------------------------------*/
  5117. BUILDIN_FUNC(percentheal)
  5118. {
  5119. int hp,sp;
  5120. TBL_PC* sd;
  5121. hp=script_getnum(st,2);
  5122. sp=script_getnum(st,3);
  5123. if(potion_flag==1) {
  5124. potion_per_hp = hp;
  5125. potion_per_sp = sp;
  5126. return SCRIPT_CMD_SUCCESS;
  5127. }
  5128. if (!script_charid2sd(4,sd))
  5129. return SCRIPT_CMD_SUCCESS;
  5130. #ifdef RENEWAL
  5131. if( sd->sc.data[SC_EXTREMITYFIST2] )
  5132. sp = 0;
  5133. #endif
  5134. if (sd->sc.data[SC_NORECOVER_STATE]) {
  5135. hp = 0;
  5136. sp = 0;
  5137. }
  5138. if (sd->sc.data[SC_BITESCAR])
  5139. hp = 0;
  5140. pc_percentheal(sd,hp,sp);
  5141. return SCRIPT_CMD_SUCCESS;
  5142. }
  5143. /*==========================================
  5144. *
  5145. *------------------------------------------*/
  5146. BUILDIN_FUNC(jobchange)
  5147. {
  5148. int job, upper=-1;
  5149. job=script_getnum(st,2);
  5150. if( script_hasdata(st,3) )
  5151. upper=script_getnum(st,3);
  5152. if (pcdb_checkid(job))
  5153. {
  5154. TBL_PC* sd;
  5155. if (!script_charid2sd(4,sd))
  5156. return SCRIPT_CMD_SUCCESS;
  5157. pc_jobchange(sd, job, upper);
  5158. }
  5159. return SCRIPT_CMD_SUCCESS;
  5160. }
  5161. /*==========================================
  5162. *
  5163. *------------------------------------------*/
  5164. BUILDIN_FUNC(jobname)
  5165. {
  5166. int class_=script_getnum(st,2);
  5167. script_pushconststr(st, (char*)job_name(class_));
  5168. return SCRIPT_CMD_SUCCESS;
  5169. }
  5170. /// Get input from the player.
  5171. /// For numeric inputs the value is capped to the range [min,max]. Returns 1 if
  5172. /// the value was higher than 'max', -1 if lower than 'min' and 0 otherwise.
  5173. /// For string inputs it returns 1 if the string was longer than 'max', -1 is
  5174. /// shorter than 'min' and 0 otherwise.
  5175. ///
  5176. /// input(<var>{,<min>{,<max>}}) -> <int>
  5177. BUILDIN_FUNC(input)
  5178. {
  5179. TBL_PC* sd;
  5180. struct script_data* data;
  5181. int64 uid;
  5182. const char* name;
  5183. int min;
  5184. int max;
  5185. sd = script_rid2sd(st);
  5186. if( sd == NULL )
  5187. return SCRIPT_CMD_SUCCESS;
  5188. data = script_getdata(st,2);
  5189. if( !data_isreference(data) ){
  5190. ShowError("script:input: not a variable\n");
  5191. script_reportdata(data);
  5192. st->state = END;
  5193. return SCRIPT_CMD_FAILURE;
  5194. }
  5195. uid = reference_getuid(data);
  5196. name = reference_getname(data);
  5197. min = (script_hasdata(st,3) ? script_getnum(st,3) : script_config.input_min_value);
  5198. max = (script_hasdata(st,4) ? script_getnum(st,4) : script_config.input_max_value);
  5199. #ifdef SECURE_NPCTIMEOUT
  5200. sd->npc_idle_type = NPCT_WAIT;
  5201. #endif
  5202. if( !sd->state.menu_or_input )
  5203. { // first invocation, display npc input box
  5204. sd->state.menu_or_input = 1;
  5205. st->state = RERUNLINE;
  5206. if( is_string_variable(name) )
  5207. clif_scriptinputstr(sd,st->oid);
  5208. else
  5209. clif_scriptinput(sd,st->oid);
  5210. }
  5211. else
  5212. { // take received text/value and store it in the designated variable
  5213. sd->state.menu_or_input = 0;
  5214. if( is_string_variable(name) )
  5215. {
  5216. int len = (int)strlen(sd->npc_str);
  5217. set_reg(st, sd, uid, name, (void*)sd->npc_str, script_getref(st,2));
  5218. script_pushint(st, (len > max ? 1 : len < min ? -1 : 0));
  5219. }
  5220. else
  5221. {
  5222. int amount = sd->npc_amount;
  5223. set_reg(st, sd, uid, name, (void*)__64BPRTSIZE(cap_value(amount,min,max)), script_getref(st,2));
  5224. script_pushint(st, (amount > max ? 1 : amount < min ? -1 : 0));
  5225. }
  5226. st->state = RUN;
  5227. }
  5228. return SCRIPT_CMD_SUCCESS;
  5229. }
  5230. // declare the copyarray method here for future reference
  5231. BUILDIN_FUNC(copyarray);
  5232. /// Sets the value of a variable.
  5233. /// The value is converted to the type of the variable.
  5234. ///
  5235. /// set(<variable>,<value>{,<charid>})
  5236. BUILDIN_FUNC(set)
  5237. {
  5238. TBL_PC* sd = NULL;
  5239. struct script_data* data;
  5240. //struct script_data* datavalue;
  5241. int64 num;
  5242. const char* name;
  5243. char prefix;
  5244. data = script_getdata(st,2);
  5245. //datavalue = script_getdata(st,3);
  5246. if( !data_isreference(data) )
  5247. {
  5248. ShowError("script:set: not a variable\n");
  5249. script_reportdata(script_getdata(st,2));
  5250. st->state = END;
  5251. return SCRIPT_CMD_FAILURE;
  5252. }
  5253. num = reference_getuid(data);
  5254. name = reference_getname(data);
  5255. prefix = *name;
  5256. if (not_server_variable(prefix) && !script_charid2sd(4,sd)) {
  5257. ShowError("buildin_set: No player attached for player variable '%s'\n", name);
  5258. return SCRIPT_CMD_FAILURE;
  5259. }
  5260. #if 0
  5261. if( data_isreference(datavalue) )
  5262. {// the value being referenced is a variable
  5263. const char* namevalue = reference_getname(datavalue);
  5264. if( !not_array_variable(*namevalue) )
  5265. {// array variable being copied into another array variable
  5266. if( sd == NULL && not_server_variable(*namevalue) && !(sd = script_rid2sd(st)) )
  5267. {// player must be attached in order to copy a player variable
  5268. ShowError("script:set: no player attached for player variable '%s'\n", namevalue);
  5269. return SCRIPT_CMD_SUCCESS;
  5270. }
  5271. if( is_string_variable(namevalue) != is_string_variable(name) )
  5272. {// non-matching array value types
  5273. ShowWarning("script:set: two array variables do not match in type.\n");
  5274. return SCRIPT_CMD_SUCCESS;
  5275. }
  5276. // push the maximum number of array values to the stack
  5277. push_val(st->stack, C_INT, SCRIPT_MAX_ARRAYSIZE);
  5278. // call the copy array method directly
  5279. return buildin_copyarray(st);
  5280. }
  5281. }
  5282. #endif
  5283. if( is_string_variable(name) )
  5284. set_reg(st,sd,num,name,(void*)script_getstr(st,3),script_getref(st,2));
  5285. else
  5286. set_reg(st,sd,num,name,(void*)__64BPRTSIZE(script_getnum(st,3)),script_getref(st,2));
  5287. // return a copy of the variable reference
  5288. script_pushcopy(st,2);
  5289. return SCRIPT_CMD_SUCCESS;
  5290. }
  5291. /////////////////////////////////////////////////////////////////////
  5292. /// Array variables
  5293. ///
  5294. /// Sets values of an array, from the starting index.
  5295. /// ex: setarray arr[1],1,2,3;
  5296. ///
  5297. /// setarray <array variable>,<value1>{,<value2>...};
  5298. BUILDIN_FUNC(setarray)
  5299. {
  5300. struct script_data* data;
  5301. const char* name;
  5302. uint32 start;
  5303. uint32 end;
  5304. int32 id;
  5305. int32 i;
  5306. TBL_PC* sd = NULL;
  5307. data = script_getdata(st, 2);
  5308. if( !data_isreference(data) )
  5309. {
  5310. ShowError("script:setarray: not a variable\n");
  5311. script_reportdata(data);
  5312. st->state = END;
  5313. return SCRIPT_CMD_FAILURE;// not a variable
  5314. }
  5315. id = reference_getid(data);
  5316. start = reference_getindex(data);
  5317. name = reference_getname(data);
  5318. if( not_server_variable(*name) )
  5319. {
  5320. sd = script_rid2sd(st);
  5321. if( sd == NULL )
  5322. return SCRIPT_CMD_SUCCESS;// no player attached
  5323. }
  5324. end = start + script_lastdata(st) - 2;
  5325. if( end > SCRIPT_MAX_ARRAYSIZE )
  5326. end = SCRIPT_MAX_ARRAYSIZE;
  5327. if( is_string_variable(name) )
  5328. {// string array
  5329. for( i = 3; start < end; ++start, ++i )
  5330. set_reg(st, sd, reference_uid(id, start), name, (void*)script_getstr(st,i), reference_getref(data));
  5331. }
  5332. else
  5333. {// int array
  5334. for( i = 3; start < end; ++start, ++i )
  5335. set_reg(st, sd, reference_uid(id, start), name, (void*)__64BPRTSIZE(script_getnum(st,i)), reference_getref(data));
  5336. }
  5337. return SCRIPT_CMD_SUCCESS;
  5338. }
  5339. /// Sets count values of an array, from the starting index.
  5340. /// ex: cleararray arr[0],0,1;
  5341. ///
  5342. /// cleararray <array variable>,<value>,<count>;
  5343. BUILDIN_FUNC(cleararray)
  5344. {
  5345. struct script_data* data;
  5346. const char* name;
  5347. uint32 start;
  5348. uint32 end;
  5349. int32 id;
  5350. void* v;
  5351. TBL_PC* sd = NULL;
  5352. data = script_getdata(st, 2);
  5353. if( !data_isreference(data) )
  5354. {
  5355. ShowError("script:cleararray: not a variable\n");
  5356. script_reportdata(data);
  5357. st->state = END;
  5358. return SCRIPT_CMD_FAILURE;// not a variable
  5359. }
  5360. id = reference_getid(data);
  5361. start = reference_getindex(data);
  5362. name = reference_getname(data);
  5363. if( not_server_variable(*name) )
  5364. {
  5365. sd = script_rid2sd(st);
  5366. if( sd == NULL )
  5367. return SCRIPT_CMD_SUCCESS;// no player attached
  5368. }
  5369. if( is_string_variable(name) )
  5370. v = (void*)script_getstr(st, 3);
  5371. else
  5372. v = (void*)__64BPRTSIZE(script_getnum(st, 3));
  5373. end = start + script_getnum(st, 4);
  5374. if( end > SCRIPT_MAX_ARRAYSIZE )
  5375. end = SCRIPT_MAX_ARRAYSIZE;
  5376. for( ; start < end; ++start )
  5377. set_reg(st, sd, reference_uid(id, start), name, v, script_getref(st,2));
  5378. return SCRIPT_CMD_SUCCESS;
  5379. }
  5380. /// Copies data from one array to another.
  5381. /// ex: copyarray arr[0],arr[2],2;
  5382. ///
  5383. /// copyarray <destination array variable>,<source array variable>,<count>;
  5384. BUILDIN_FUNC(copyarray)
  5385. {
  5386. struct script_data* data1;
  5387. struct script_data* data2;
  5388. const char* name1;
  5389. const char* name2;
  5390. int32 idx1;
  5391. int32 idx2;
  5392. int32 id1;
  5393. int32 id2;
  5394. void* v;
  5395. int32 i;
  5396. uint32 count;
  5397. TBL_PC* sd = NULL;
  5398. data1 = script_getdata(st, 2);
  5399. data2 = script_getdata(st, 3);
  5400. if( !data_isreference(data1) || !data_isreference(data2) )
  5401. {
  5402. ShowError("script:copyarray: not a variable\n");
  5403. script_reportdata(data1);
  5404. script_reportdata(data2);
  5405. st->state = END;
  5406. return SCRIPT_CMD_FAILURE;// not a variable
  5407. }
  5408. id1 = reference_getid(data1);
  5409. id2 = reference_getid(data2);
  5410. idx1 = reference_getindex(data1);
  5411. idx2 = reference_getindex(data2);
  5412. name1 = reference_getname(data1);
  5413. name2 = reference_getname(data2);
  5414. if( is_string_variable(name1) != is_string_variable(name2) )
  5415. {
  5416. ShowError("script:copyarray: type mismatch\n");
  5417. script_reportdata(data1);
  5418. script_reportdata(data2);
  5419. st->state = END;
  5420. return SCRIPT_CMD_FAILURE;// data type mismatch
  5421. }
  5422. if( not_server_variable(*name1) || not_server_variable(*name2) )
  5423. {
  5424. sd = script_rid2sd(st);
  5425. if( sd == NULL )
  5426. return SCRIPT_CMD_SUCCESS;// no player attached
  5427. }
  5428. count = script_getnum(st, 4);
  5429. if( count > SCRIPT_MAX_ARRAYSIZE - idx1 )
  5430. count = SCRIPT_MAX_ARRAYSIZE - idx1;
  5431. if( count <= 0 || (id1 == id2 && idx1 == idx2) )
  5432. return SCRIPT_CMD_SUCCESS;// nothing to copy
  5433. if( id1 == id2 && idx1 > idx2 )
  5434. {// destination might be overlapping the source - copy in reverse order
  5435. for( i = count - 1; i >= 0; --i )
  5436. {
  5437. v = get_val2(st, reference_uid(id2, idx2 + i), reference_getref(data2));
  5438. set_reg(st, sd, reference_uid(id1, idx1 + i), name1, v, reference_getref(data1));
  5439. script_removetop(st, -1, 0);
  5440. }
  5441. }
  5442. else
  5443. {// normal copy
  5444. for( i = 0; i < count; ++i )
  5445. {
  5446. if( idx2 + i < SCRIPT_MAX_ARRAYSIZE )
  5447. {
  5448. v = get_val2(st, reference_uid(id2, idx2 + i), reference_getref(data2));
  5449. set_reg(st, sd, reference_uid(id1, idx1 + i), name1, v, reference_getref(data1));
  5450. script_removetop(st, -1, 0);
  5451. }
  5452. else// out of range - assume ""/0
  5453. set_reg(st, sd, reference_uid(id1, idx1 + i), name1, (is_string_variable(name1)?(void*)"":(void*)0), reference_getref(data1));
  5454. }
  5455. }
  5456. return SCRIPT_CMD_SUCCESS;
  5457. }
  5458. /// Returns the size of the array.
  5459. /// Assumes that everything before the starting index exists.
  5460. /// ex: getarraysize(arr[3])
  5461. ///
  5462. /// getarraysize(<array variable>) -> <int>
  5463. BUILDIN_FUNC(getarraysize)
  5464. {
  5465. struct script_data* data;
  5466. data = script_getdata(st, 2);
  5467. if( !data_isreference(data) )
  5468. {
  5469. ShowError("script:getarraysize: not a variable\n");
  5470. script_reportdata(data);
  5471. script_pushnil(st);
  5472. st->state = END;
  5473. return SCRIPT_CMD_FAILURE;// not a variable
  5474. }
  5475. script_pushint(st, script_array_highest_key(st, st->rid ? script_rid2sd(st) : NULL, reference_getname(data), reference_getref(data)));
  5476. return SCRIPT_CMD_SUCCESS;
  5477. }
  5478. int script_array_index_cmp(const void *a, const void *b) {
  5479. return ( *(unsigned int*)a - *(unsigned int*)b );
  5480. }
  5481. /// Deletes count or all the elements in an array, from the starting index.
  5482. /// ex: deletearray arr[4],2;
  5483. ///
  5484. /// deletearray <array variable>;
  5485. /// deletearray <array variable>,<count>;
  5486. BUILDIN_FUNC(deletearray)
  5487. {
  5488. struct script_data* data;
  5489. const char* name;
  5490. unsigned int start, end, i;
  5491. int id;
  5492. TBL_PC *sd = NULL;
  5493. struct script_array *sa = NULL;
  5494. struct reg_db *src = NULL;
  5495. void *value;
  5496. data = script_getdata(st, 2);
  5497. if( !data_isreference(data) ) {
  5498. ShowError("script:deletearray: not a variable\n");
  5499. script_reportdata(data);
  5500. st->state = END;
  5501. return SCRIPT_CMD_FAILURE;// not a variable
  5502. }
  5503. id = reference_getid(data);
  5504. start = reference_getindex(data);
  5505. name = reference_getname(data);
  5506. if( not_server_variable(*name) ) {
  5507. sd = script_rid2sd(st);
  5508. if( sd == NULL )
  5509. return SCRIPT_CMD_SUCCESS;// no player attached
  5510. }
  5511. if (!(src = script_array_src(st, sd, name, reference_getref(data)))) {
  5512. ShowError("script:deletearray: not a array\n");
  5513. script_reportdata(data);
  5514. st->state = END;
  5515. return SCRIPT_CMD_FAILURE;// not a variable
  5516. }
  5517. script_array_ensure_zero(st, NULL, data->u.num, reference_getref(data));
  5518. if ( !(sa = idb_get(src->arrays, id)) ) { // non-existent array, nothing to empty
  5519. return SCRIPT_CMD_SUCCESS;// not a variable
  5520. }
  5521. end = script_array_highest_key(st, sd, name, reference_getref(data));
  5522. if( start >= end )
  5523. return SCRIPT_CMD_SUCCESS;// nothing to free
  5524. if( is_string_variable(name) )
  5525. value = (void *)"";
  5526. else
  5527. value = (void *)0;
  5528. if( script_hasdata(st,3) ) {
  5529. unsigned int count = script_getnum(st, 3);
  5530. if( count > end - start )
  5531. count = end - start;
  5532. if( count <= 0 )
  5533. return SCRIPT_CMD_SUCCESS;// nothing to free
  5534. if( end - start < sa->size ) {
  5535. // Better to iterate directly on the array, no speed-up from using sa
  5536. for( ; start + count < end; ++start ) {
  5537. // Compact and overwrite
  5538. void* v = get_val2(st, reference_uid(id, start + count), reference_getref(data));
  5539. set_reg(st, sd, reference_uid(id, start), name, v, reference_getref(data));
  5540. script_removetop(st, -1, 0);
  5541. }
  5542. for( ; start < end; start++ ) {
  5543. // Clean up any leftovers that weren't overwritten
  5544. set_reg(st, sd, reference_uid(id, start), name, value, reference_getref(data));
  5545. }
  5546. } else {
  5547. // using sa to speed up
  5548. unsigned int *list = NULL, size = 0;
  5549. list = script_array_cpy_list(sa);
  5550. size = sa->size;
  5551. qsort(list, size, sizeof(unsigned int), script_array_index_cmp);
  5552. ARR_FIND(0, size, i, list[i] >= start);
  5553. for( ; i < size && list[i] < start + count; i++ ) {
  5554. // Clear any entries between start and start+count, if they exist
  5555. set_reg(st, sd, reference_uid(id, list[i]), name, value, reference_getref(data));
  5556. }
  5557. for( ; i < size && list[i] < end; i++ ) {
  5558. // Move back count positions any entries between start+count to fill the gaps
  5559. void* v = get_val2(st, reference_uid(id, list[i]), reference_getref(data));
  5560. set_reg(st, sd, reference_uid(id, list[i]-count), name, v, reference_getref(data));
  5561. script_removetop(st, -1, 0);
  5562. // Clear their originals
  5563. set_reg(st, sd, reference_uid(id, list[i]), name, value, reference_getref(data));
  5564. }
  5565. }
  5566. } else {
  5567. unsigned int *list = NULL, size = 0;
  5568. list = script_array_cpy_list(sa);
  5569. size = sa->size;
  5570. for(i = 0; i < size; i++) {
  5571. if( list[i] >= start ) // Less expensive than sorting it, most likely
  5572. set_reg(st, sd, reference_uid(id, list[i]), name, value, reference_getref(data));
  5573. }
  5574. }
  5575. return SCRIPT_CMD_SUCCESS;
  5576. }
  5577. /// Returns a reference to the target index of the array variable.
  5578. /// Equivalent to var[index].
  5579. ///
  5580. /// getelementofarray(<array variable>,<index>) -> <variable reference>
  5581. BUILDIN_FUNC(getelementofarray)
  5582. {
  5583. struct script_data* data;
  5584. int32 id;
  5585. int64 i;
  5586. data = script_getdata(st, 2);
  5587. if( !data_isreference(data) )
  5588. {
  5589. ShowError("script:getelementofarray: not a variable\n");
  5590. script_reportdata(data);
  5591. script_pushnil(st);
  5592. st->state = END;
  5593. return SCRIPT_CMD_SUCCESS;// not a variable
  5594. }
  5595. id = reference_getid(data);
  5596. i = script_getnum(st, 3);
  5597. if (i < 0 || i >= SCRIPT_MAX_ARRAYSIZE) {
  5598. ShowWarning("script:getelementofarray: index out of range (%"PRId64")\n", i);
  5599. script_reportdata(data);
  5600. script_pushnil(st);
  5601. st->state = END;
  5602. return SCRIPT_CMD_FAILURE;// out of range
  5603. }
  5604. push_val2(st->stack, C_NAME, reference_uid(id, i), reference_getref(data));
  5605. return SCRIPT_CMD_SUCCESS;
  5606. }
  5607. /////////////////////////////////////////////////////////////////////
  5608. /// ...
  5609. ///
  5610. /*==========================================
  5611. *
  5612. *------------------------------------------*/
  5613. BUILDIN_FUNC(setlook)
  5614. {
  5615. int type,val;
  5616. TBL_PC* sd;
  5617. type = script_getnum(st,2);
  5618. val = script_getnum(st,3);
  5619. if (!script_charid2sd(4,sd))
  5620. return SCRIPT_CMD_SUCCESS;
  5621. pc_changelook(sd,type,val);
  5622. return SCRIPT_CMD_SUCCESS;
  5623. }
  5624. BUILDIN_FUNC(changelook)
  5625. { // As setlook but only client side
  5626. int type,val;
  5627. TBL_PC* sd;
  5628. type = script_getnum(st,2);
  5629. val = script_getnum(st,3);
  5630. if (!script_charid2sd(4,sd))
  5631. return SCRIPT_CMD_SUCCESS;
  5632. clif_changelook(&sd->bl,type,val);
  5633. return SCRIPT_CMD_SUCCESS;
  5634. }
  5635. /*==========================================
  5636. *
  5637. *------------------------------------------*/
  5638. BUILDIN_FUNC(cutin)
  5639. {
  5640. TBL_PC* sd;
  5641. sd = script_rid2sd(st);
  5642. if( sd == NULL )
  5643. return SCRIPT_CMD_SUCCESS;
  5644. clif_cutin(sd,script_getstr(st,2),script_getnum(st,3));
  5645. return SCRIPT_CMD_SUCCESS;
  5646. }
  5647. /*==========================================
  5648. *
  5649. *------------------------------------------*/
  5650. BUILDIN_FUNC(viewpoint)
  5651. {
  5652. int type,x,y,id,color;
  5653. TBL_PC* sd;
  5654. type=script_getnum(st,2);
  5655. x=script_getnum(st,3);
  5656. y=script_getnum(st,4);
  5657. id=script_getnum(st,5);
  5658. color=script_getnum(st,6);
  5659. sd = script_rid2sd(st);
  5660. if( sd == NULL )
  5661. return SCRIPT_CMD_SUCCESS;
  5662. clif_viewpoint(sd,st->oid,type,x,y,id,color);
  5663. return SCRIPT_CMD_SUCCESS;
  5664. }
  5665. /// Returns number of items in inventory/cart/storage
  5666. /// countitem <nameID>{,<accountID>});
  5667. /// countitem2 <nameID>,<Identified>,<Refine>,<Attribute>,<Card0>,<Card1>,<Card2>,<Card3>{,<accountID>}) [Lupus]
  5668. /// cartcountitem <nameID>{,<accountID>});
  5669. /// cartcountitem2 <nameID>,<Identified>,<Refine>,<Attribute>,<Card0>,<Card1>,<Card2>,<Card3>{,<accountID>})
  5670. /// storagecountitem <nameID>{,<accountID>});
  5671. /// storagecountitem2 <nameID>,<Identified>,<Refine>,<Attribute>,<Card0>,<Card1>,<Card2>,<Card3>{,<accountID>})
  5672. /// guildstoragecountitem <nameID>{,<accountID>});
  5673. /// guildstoragecountitem2 <nameID>,<Identified>,<Refine>,<Attribute>,<Card0>,<Card1>,<Card2>,<Card3>{,<accountID>})
  5674. BUILDIN_FUNC(countitem)
  5675. {
  5676. int i = 0, count = 0, aid = 3;
  5677. struct item_data* id = NULL;
  5678. struct script_data* data;
  5679. char *command = (char *)script_getfuncname(st);
  5680. uint8 loc = 0;
  5681. uint16 size;
  5682. struct item *items;
  5683. TBL_PC *sd = NULL;
  5684. struct s_storage *gstor;
  5685. if( command[strlen(command)-1] == '2' ) {
  5686. i = 1;
  5687. aid = 10;
  5688. }
  5689. if( script_hasdata(st,aid) ) {
  5690. if( !(sd = map_id2sd( (aid = script_getnum(st, aid)) )) ) {
  5691. ShowError("buildin_%s: player not found (AID=%d).\n", command, aid);
  5692. st->state = END;
  5693. return SCRIPT_CMD_FAILURE;
  5694. }
  5695. }
  5696. else {
  5697. if( !(sd = script_rid2sd(st)) )
  5698. return SCRIPT_CMD_SUCCESS;
  5699. }
  5700. if( !strncmp(command, "cart", 4) ) {
  5701. loc = 1;
  5702. size = MAX_CART;
  5703. items = sd->cart.u.items_cart;
  5704. }
  5705. else if( !strncmp(command, "storage", 7) ) {
  5706. loc = 2;
  5707. size = MAX_STORAGE;
  5708. items = sd->storage.u.items_storage;
  5709. }
  5710. else if( !strncmp(command, "guildstorage", 12) ) {
  5711. gstor = guild2storage2(sd->status.guild_id);
  5712. if (gstor && !sd->state.storage_flag) {
  5713. loc = 3;
  5714. size = MAX_GUILD_STORAGE;
  5715. items = gstor->u.items_guild;
  5716. } else {
  5717. script_pushint(st, -1);
  5718. return SCRIPT_CMD_SUCCESS;
  5719. }
  5720. }
  5721. else {
  5722. size = MAX_INVENTORY;
  5723. items = sd->inventory.u.items_inventory;
  5724. }
  5725. if( loc == 1 && !pc_iscarton(sd) ) {
  5726. ShowError("buildin_%s: Player doesn't have cart (CID:%d).\n", command, sd->status.char_id);
  5727. script_pushint(st,-1);
  5728. return SCRIPT_CMD_SUCCESS;
  5729. }
  5730. data = script_getdata(st, 2);
  5731. get_val(st, data); // Convert into value in case of a variable
  5732. if( data_isstring(data) ) // item name
  5733. id = itemdb_searchname(conv_str(st, data));
  5734. else // item id
  5735. id = itemdb_exists(conv_num(st, data));
  5736. if( id == NULL ) {
  5737. ShowError("buildin_%s: Invalid item '%s'.\n", command, script_getstr(st,2)); // returns string, regardless of what it was
  5738. script_pushint(st,0);
  5739. return SCRIPT_CMD_FAILURE;
  5740. }
  5741. if (loc == 3)
  5742. gstor->lock = true;
  5743. if( !i ) { // For count/cart/storagecountitem function
  5744. unsigned short nameid = id->nameid;
  5745. for( i = 0; i < size; i++ )
  5746. if( &items[i] && items[i].nameid == nameid )
  5747. count += items[i].amount;
  5748. }
  5749. else { // For count/cart/storagecountitem2 function
  5750. unsigned short nameid;
  5751. int iden, ref, attr, c1, c2, c3, c4;
  5752. nameid = id->nameid;
  5753. iden = script_getnum(st,3);
  5754. ref = script_getnum(st,4);
  5755. attr = script_getnum(st,5);
  5756. c1 = script_getnum(st,6);
  5757. c2 = script_getnum(st,7);
  5758. c3 = script_getnum(st,8);
  5759. c4 = script_getnum(st,9);
  5760. for( i = 0; i < size; i++ )
  5761. if( &items[i] && items[i].nameid > 0 && items[i].nameid == nameid &&
  5762. items[i].amount > 0 && items[i].identify == iden &&
  5763. items[i].refine == ref && items[i].attribute == attr &&
  5764. items[i].card[0] == c1 && items[i].card[1] == c2 &&
  5765. items[i].card[2] == c3 && items[i].card[3] == c4 )
  5766. {
  5767. count += items[i].amount;
  5768. }
  5769. }
  5770. if (loc == 3) {
  5771. storage_guild_storageclose(sd);
  5772. gstor->lock = false;
  5773. }
  5774. script_pushint(st, count);
  5775. return SCRIPT_CMD_SUCCESS;
  5776. }
  5777. /*==========================================
  5778. * Check if item with this amount can fit in inventory
  5779. * Checking : weight, stack amount >(MAX_AMOUNT), slots amount >(MAX_INVENTORY)
  5780. * Return
  5781. * 0 : fail
  5782. * 1 : success (npc side only)
  5783. *------------------------------------------*/
  5784. BUILDIN_FUNC(checkweight)
  5785. {
  5786. int slots, amount2 = 0;
  5787. unsigned int weight = 0, i, nbargs;
  5788. struct item_data* id = NULL;
  5789. struct map_session_data* sd;
  5790. if( ( sd = script_rid2sd(st) ) == NULL )
  5791. return SCRIPT_CMD_SUCCESS;
  5792. nbargs = script_lastdata(st)+1;
  5793. if(nbargs%2) {
  5794. ShowError("buildin_checkweight: Invalid nb of args should be a multiple of 2.\n");
  5795. script_pushint(st,0);
  5796. return SCRIPT_CMD_FAILURE;
  5797. }
  5798. slots = pc_inventoryblank(sd); //nb of empty slot
  5799. for (i = 2; i < nbargs; i += 2) {
  5800. unsigned short nameid, amount;
  5801. struct script_data* data = script_getdata(st,i);
  5802. get_val(st, data); // Convert into value in case of a variable
  5803. if( data_isstring(data) ) // item name
  5804. id = itemdb_searchname(conv_str(st, data));
  5805. else // item id
  5806. id = itemdb_exists(conv_num(st, data));
  5807. if( id == NULL ) {
  5808. ShowError("buildin_checkweight: Invalid item '%s'.\n", script_getstr(st,i)); // returns string, regardless of what it was
  5809. script_pushint(st,0);
  5810. return SCRIPT_CMD_FAILURE;
  5811. }
  5812. nameid = id->nameid;
  5813. amount = script_getnum(st,i+1);
  5814. if( amount < 1 ) {
  5815. ShowError("buildin_checkweight: Invalid amount '%d'.\n", amount);
  5816. script_pushint(st,0);
  5817. return SCRIPT_CMD_FAILURE;
  5818. }
  5819. weight += itemdb_weight(nameid)*amount; //total weight for all chk
  5820. if( weight + sd->weight > sd->max_weight )
  5821. {// too heavy
  5822. script_pushint(st,0);
  5823. return SCRIPT_CMD_SUCCESS;
  5824. }
  5825. switch( pc_checkadditem(sd, nameid, amount) ) {
  5826. case CHKADDITEM_EXIST:
  5827. // item is already in inventory, but there is still space for the requested amount
  5828. break;
  5829. case CHKADDITEM_NEW:
  5830. if( itemdb_isstackable(nameid) ) {
  5831. // stackable
  5832. amount2++;
  5833. if( slots < amount2 ) {
  5834. script_pushint(st,0);
  5835. return SCRIPT_CMD_SUCCESS;
  5836. }
  5837. } else {
  5838. // non-stackable
  5839. amount2 += amount;
  5840. if( slots < amount2) {
  5841. script_pushint(st,0);
  5842. return SCRIPT_CMD_SUCCESS;
  5843. }
  5844. }
  5845. break;
  5846. case CHKADDITEM_OVERAMOUNT:
  5847. script_pushint(st,0);
  5848. return SCRIPT_CMD_SUCCESS;
  5849. }
  5850. }
  5851. script_pushint(st,1);
  5852. return SCRIPT_CMD_SUCCESS;
  5853. }
  5854. BUILDIN_FUNC(checkweight2)
  5855. {
  5856. //variable sub checkweight
  5857. int i = 0, amount2 = 0, slots = 0, weight = 0;
  5858. short fail = 0;
  5859. //variable for array parsing
  5860. struct script_data* data_it;
  5861. struct script_data* data_nb;
  5862. const char* name_it;
  5863. const char* name_nb;
  5864. int32 id_it, id_nb;
  5865. int32 idx_it, idx_nb;
  5866. int nb_it, nb_nb; //array size
  5867. TBL_PC *sd = script_rid2sd(st);
  5868. nullpo_retr(1,sd);
  5869. data_it = script_getdata(st, 2);
  5870. data_nb = script_getdata(st, 3);
  5871. if( !data_isreference(data_it) || !data_isreference(data_nb)) {
  5872. ShowError("buildin_checkweight2: parameter not a variable\n");
  5873. script_pushint(st,0);
  5874. return SCRIPT_CMD_FAILURE;// not a variable
  5875. }
  5876. id_it = reference_getid(data_it);
  5877. id_nb = reference_getid(data_nb);
  5878. idx_it = reference_getindex(data_it);
  5879. idx_nb = reference_getindex(data_nb);
  5880. name_it = reference_getname(data_it);
  5881. name_nb = reference_getname(data_nb);
  5882. if(is_string_variable(name_it) || is_string_variable(name_nb)) {
  5883. ShowError("buildin_checkweight2: illegal type, need int\n");
  5884. script_pushint(st,0);
  5885. return SCRIPT_CMD_FAILURE;// not supported
  5886. }
  5887. nb_it = script_array_highest_key(st, sd, reference_getname(data_it), reference_getref(data_it));
  5888. nb_nb = script_array_highest_key(st, sd, reference_getname(data_nb), reference_getref(data_nb));
  5889. if(nb_it != nb_nb) {
  5890. ShowError("buildin_checkweight2: Size mistmatch: nb_it=%d, nb_nb=%d\n",nb_it,nb_nb);
  5891. fail = 1;
  5892. }
  5893. slots = pc_inventoryblank(sd);
  5894. for(i=0; i<nb_it; i++) {
  5895. unsigned short amount, nameid = (int32)__64BPRTSIZE(get_val2(st,reference_uid(id_it,idx_it+i),reference_getref(data_it)));
  5896. script_removetop(st, -1, 0);
  5897. amount = (int32)__64BPRTSIZE(get_val2(st,reference_uid(id_nb,idx_nb+i),reference_getref(data_nb)));
  5898. script_removetop(st, -1, 0);
  5899. if(fail)
  5900. continue; //cpntonie to depop rest
  5901. if(itemdb_exists(nameid) == NULL ) {
  5902. ShowError("buildin_checkweight2: Invalid item '%d'.\n", nameid);
  5903. fail=1;
  5904. continue;
  5905. }
  5906. if(amount < 0 ) {
  5907. ShowError("buildin_checkweight2: Invalid amount '%d'.\n", amount);
  5908. fail = 1;
  5909. continue;
  5910. }
  5911. weight += itemdb_weight(nameid)*amount;
  5912. if( weight + sd->weight > sd->max_weight ) {
  5913. fail = 1;
  5914. continue;
  5915. }
  5916. switch( pc_checkadditem(sd, nameid, amount) ) {
  5917. case CHKADDITEM_EXIST:
  5918. // item is already in inventory, but there is still space for the requested amount
  5919. break;
  5920. case CHKADDITEM_NEW:
  5921. if( itemdb_isstackable(nameid) ) {// stackable
  5922. amount2++;
  5923. if( slots < amount2 )
  5924. fail = 1;
  5925. } else {// non-stackable
  5926. amount2 += amount;
  5927. if( slots < amount2 ) {
  5928. fail = 1;
  5929. }
  5930. }
  5931. break;
  5932. case CHKADDITEM_OVERAMOUNT:
  5933. fail = 1;
  5934. } //end switch
  5935. } //end loop DO NOT break it prematurly we need to depop all stack
  5936. fail ? script_pushint(st,0) : script_pushint(st,1);
  5937. return SCRIPT_CMD_SUCCESS;
  5938. }
  5939. /*==========================================
  5940. * getitem <item id>,<amount>{,<account ID>};
  5941. * getitem "<item name>",<amount>{,<account ID>};
  5942. *
  5943. * getitembound <item id>,<amount>,<type>{,<account ID>};
  5944. * getitembound "<item id>",<amount>,<type>{,<account ID>};
  5945. * Type:
  5946. * 0 - No bound
  5947. * 1 - Account Bound
  5948. * 2 - Guild Bound
  5949. * 3 - Party Bound
  5950. * 4 - Character Bound
  5951. *------------------------------------------*/
  5952. BUILDIN_FUNC(getitem)
  5953. {
  5954. int amount, get_count, i;
  5955. unsigned short nameid;
  5956. struct item it;
  5957. TBL_PC *sd;
  5958. struct script_data *data;
  5959. unsigned char flag = 0;
  5960. const char* command = script_getfuncname(st);
  5961. struct item_data *id = NULL;
  5962. data = script_getdata(st,2);
  5963. get_val(st,data);
  5964. if( data_isstring(data) ) {// "<item name>"
  5965. const char *name = conv_str(st,data);
  5966. id = itemdb_searchname(name);
  5967. if( id == NULL ){
  5968. ShowError("buildin_getitem: Nonexistant item %s requested.\n", name);
  5969. return SCRIPT_CMD_FAILURE; //No item created.
  5970. }
  5971. nameid = id->nameid;
  5972. } else if( data_isint(data) ) {// <item id>
  5973. nameid = conv_num(st,data);
  5974. if( !(id = itemdb_exists(nameid)) ){
  5975. ShowError("buildin_getitem: Nonexistant item %d requested.\n", nameid);
  5976. return SCRIPT_CMD_FAILURE; //No item created.
  5977. }
  5978. } else {
  5979. ShowError("buildin_getitem: invalid data type for argument #1 (%d).", data->type);
  5980. return SCRIPT_CMD_FAILURE;
  5981. }
  5982. // <amount>
  5983. if( (amount = script_getnum(st,3)) <= 0)
  5984. return SCRIPT_CMD_SUCCESS; //return if amount <=0, skip the useles iteration
  5985. memset(&it,0,sizeof(it));
  5986. it.nameid = nameid;
  5987. it.identify = 1;
  5988. it.bound = BOUND_NONE;
  5989. if( !strcmp(command,"getitembound") ) {
  5990. char bound = script_getnum(st,4);
  5991. if( bound < BOUND_NONE || bound >= BOUND_MAX ) {
  5992. ShowError("script_getitembound: Not a correct bound type! Type=%d\n",bound);
  5993. return SCRIPT_CMD_FAILURE;
  5994. }
  5995. if( script_hasdata(st,5) )
  5996. sd = map_id2sd(script_getnum(st,5));
  5997. else
  5998. sd = script_rid2sd(st); // Attached player
  5999. it.bound = bound;
  6000. } else if( script_hasdata(st,4) )
  6001. sd = map_id2sd(script_getnum(st,4)); // <Account ID>
  6002. else
  6003. sd = script_rid2sd(st); // Attached player
  6004. if( sd == NULL ) // no target
  6005. return SCRIPT_CMD_SUCCESS;
  6006. //Check if it's stackable.
  6007. if (!itemdb_isstackable2(id))
  6008. get_count = 1;
  6009. else
  6010. get_count = amount;
  6011. for (i = 0; i < amount; i += get_count)
  6012. {
  6013. // if not pet egg
  6014. if (!pet_create_egg(sd, nameid))
  6015. {
  6016. if ((flag = pc_additem(sd, &it, get_count, LOG_TYPE_SCRIPT)))
  6017. {
  6018. clif_additem(sd, 0, 0, flag);
  6019. if( pc_candrop(sd,&it) )
  6020. map_addflooritem(&it,get_count,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0,0);
  6021. }
  6022. }
  6023. }
  6024. return SCRIPT_CMD_SUCCESS;
  6025. }
  6026. /*==========================================
  6027. * getitem2 <item id>,<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>{,<account ID>};
  6028. * getitem2 "<item name>",<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>{,<account ID>};
  6029. *
  6030. * getitembound2 <item id>,<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>,<bound type>{,<account ID>};
  6031. * getitembound2 "<item name>",<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>,<bound type>{,<account ID>};
  6032. * Type:
  6033. * 0 - No bound
  6034. * 1 - Account Bound
  6035. * 2 - Guild Bound
  6036. * 3 - Party Bound
  6037. * 4 - Character Bound
  6038. *------------------------------------------*/
  6039. BUILDIN_FUNC(getitem2)
  6040. {
  6041. int amount;
  6042. unsigned short nameid;
  6043. int iden, ref, attr;
  6044. unsigned short c1, c2, c3, c4;
  6045. char bound = BOUND_NONE;
  6046. struct item_data *item_data = NULL;
  6047. struct item item_tmp;
  6048. TBL_PC *sd;
  6049. struct script_data *data;
  6050. const char* command = script_getfuncname(st);
  6051. if( !strcmp(command,"getitembound2") ) {
  6052. bound = script_getnum(st,11);
  6053. if( bound < BOUND_NONE || bound >= BOUND_MAX ) {
  6054. ShowError("script_getitembound2: Not a correct bound type! Type=%d\n",bound);
  6055. return SCRIPT_CMD_FAILURE;
  6056. }
  6057. if( script_hasdata(st,12) )
  6058. sd = map_id2sd(script_getnum(st,12));
  6059. else
  6060. sd = script_rid2sd(st); // Attached player
  6061. } else if( script_hasdata(st,11) )
  6062. sd = map_id2sd(script_getnum(st,11)); // <Account ID>
  6063. else
  6064. sd = script_rid2sd(st); // Attached player
  6065. if( sd == NULL ) // no target
  6066. return SCRIPT_CMD_SUCCESS;
  6067. data = script_getdata(st,2);
  6068. get_val(st,data);
  6069. if( data_isstring(data) ) {
  6070. const char *name = conv_str(st,data);
  6071. if( (item_data = itemdb_searchname(name)) == NULL ){
  6072. ShowError("buildin_getitem2: Nonexistant item %s requested (by conv_str).\n", name);
  6073. return SCRIPT_CMD_FAILURE; //No item created.
  6074. }
  6075. nameid = item_data->nameid;
  6076. } else {
  6077. nameid = conv_num(st,data);
  6078. if( (item_data = itemdb_exists(nameid)) == NULL ){
  6079. ShowError("buildin_getitem2: Nonexistant item %d requested (by conv_num).\n", nameid);
  6080. return SCRIPT_CMD_FAILURE; //No item created.
  6081. }
  6082. }
  6083. amount = script_getnum(st,3);
  6084. iden = script_getnum(st,4);
  6085. ref = script_getnum(st,5);
  6086. attr = script_getnum(st,6);
  6087. c1 = (unsigned short)script_getnum(st,7);
  6088. c2 = (unsigned short)script_getnum(st,8);
  6089. c3 = (unsigned short)script_getnum(st,9);
  6090. c4 = (unsigned short)script_getnum(st,10);
  6091. if( item_data ) {
  6092. int get_count = 0, i;
  6093. memset(&item_tmp,0,sizeof(item_tmp));
  6094. if( item_data->type == IT_WEAPON || item_data->type == IT_ARMOR || item_data->type == IT_SHADOWGEAR ) {
  6095. if(ref > MAX_REFINE)
  6096. ref = MAX_REFINE;
  6097. }
  6098. else if( item_data->type == IT_PETEGG ) {
  6099. iden = 1;
  6100. ref = 0;
  6101. }
  6102. else {
  6103. iden = 1;
  6104. ref = attr = 0;
  6105. }
  6106. item_tmp.nameid = nameid;
  6107. item_tmp.identify = iden;
  6108. item_tmp.refine = ref;
  6109. item_tmp.attribute = attr;
  6110. item_tmp.card[0] = c1;
  6111. item_tmp.card[1] = c2;
  6112. item_tmp.card[2] = c3;
  6113. item_tmp.card[3] = c4;
  6114. item_tmp.bound = bound;
  6115. //Check if it's stackable.
  6116. if (!itemdb_isstackable2(item_data))
  6117. get_count = 1;
  6118. else
  6119. get_count = amount;
  6120. for (i = 0; i < amount; i += get_count)
  6121. {
  6122. // if not pet egg
  6123. if (!pet_create_egg(sd, nameid))
  6124. {
  6125. unsigned char flag = 0;
  6126. if ((flag = pc_additem(sd, &item_tmp, get_count, LOG_TYPE_SCRIPT)))
  6127. {
  6128. clif_additem(sd, 0, 0, flag);
  6129. if( pc_candrop(sd,&item_tmp) )
  6130. map_addflooritem(&item_tmp,get_count,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0,0);
  6131. }
  6132. }
  6133. }
  6134. }
  6135. return SCRIPT_CMD_SUCCESS;
  6136. }
  6137. /** Gives rental item to player
  6138. * rentitem <item id>,<seconds>{,<account_id>}
  6139. * rentitem "<item name>",<seconds>{,<account_id>}
  6140. */
  6141. BUILDIN_FUNC(rentitem) {
  6142. struct map_session_data *sd;
  6143. struct script_data *data;
  6144. struct item it;
  6145. int seconds;
  6146. unsigned short nameid = 0;
  6147. unsigned char flag = 0;
  6148. data = script_getdata(st,2);
  6149. get_val(st,data);
  6150. if (!script_accid2sd(4,sd))
  6151. return SCRIPT_CMD_FAILURE;
  6152. if( data_isstring(data) )
  6153. {
  6154. const char *name = conv_str(st,data);
  6155. struct item_data *itd = itemdb_searchname(name);
  6156. if( itd == NULL )
  6157. {
  6158. ShowError("buildin_rentitem: Nonexistant item %s requested.\n", name);
  6159. return SCRIPT_CMD_FAILURE;
  6160. }
  6161. nameid = itd->nameid;
  6162. }
  6163. else if( data_isint(data) )
  6164. {
  6165. nameid = conv_num(st,data);
  6166. if( nameid == 0 || !itemdb_exists(nameid) )
  6167. {
  6168. ShowError("buildin_rentitem: Nonexistant item %hu requested.\n", nameid);
  6169. return SCRIPT_CMD_FAILURE;
  6170. }
  6171. }
  6172. else
  6173. {
  6174. ShowError("buildin_rentitem: invalid data type for argument #1 (%d).\n", data->type);
  6175. return SCRIPT_CMD_FAILURE;
  6176. }
  6177. seconds = script_getnum(st,3);
  6178. memset(&it, 0, sizeof(it));
  6179. it.nameid = nameid;
  6180. it.identify = 1;
  6181. it.expire_time = (unsigned int)(time(NULL) + seconds);
  6182. it.bound = BOUND_NONE;
  6183. if( (flag = pc_additem(sd, &it, 1, LOG_TYPE_SCRIPT)) )
  6184. {
  6185. clif_additem(sd, 0, 0, flag);
  6186. return SCRIPT_CMD_FAILURE;
  6187. }
  6188. return SCRIPT_CMD_SUCCESS;
  6189. }
  6190. /** Gives rental item to player with advanced option
  6191. * rentitem2 <item id>,<time>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>{,<account_id>};
  6192. * rentitem2 "<item name>",<time>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>{,<account_id>};
  6193. */
  6194. BUILDIN_FUNC(rentitem2) {
  6195. struct map_session_data *sd;
  6196. struct script_data *data;
  6197. struct item it;
  6198. struct item_data *id;
  6199. int seconds;
  6200. unsigned short nameid = 0;
  6201. unsigned char flag = 0;
  6202. int iden,ref,attr,c1,c2,c3,c4;
  6203. data = script_getdata(st,2);
  6204. get_val(st,data);
  6205. if (!script_accid2sd(11,sd))
  6206. return SCRIPT_CMD_FAILURE;
  6207. if( data_isstring(data) ) {
  6208. const char *name = conv_str(st,data);
  6209. id = itemdb_searchname(name);
  6210. if( id == NULL ) {
  6211. ShowError("buildin_rentitem2: Nonexistant item %s requested.\n", name);
  6212. return SCRIPT_CMD_FAILURE;
  6213. }
  6214. nameid = id->nameid;
  6215. }
  6216. else if( data_isint(data) ) {
  6217. nameid = conv_num(st,data);
  6218. if( !(id = itemdb_search(nameid))) {
  6219. ShowError("buildin_rentitem2: Nonexistant item %hu requested.\n", nameid);
  6220. return SCRIPT_CMD_FAILURE;
  6221. }
  6222. }
  6223. else {
  6224. ShowError("buildin_rentitem2: invalid data type for argument #1 (%d).\n", data->type);
  6225. return SCRIPT_CMD_FAILURE;
  6226. }
  6227. seconds = script_getnum(st,3);
  6228. iden = script_getnum(st,4);
  6229. ref = script_getnum(st,5);
  6230. attr = script_getnum(st,6);
  6231. if (id->type==IT_WEAPON || id->type==IT_ARMOR || id->type==IT_SHADOWGEAR) {
  6232. if(ref > MAX_REFINE) ref = MAX_REFINE;
  6233. }
  6234. else if (id->type==IT_PETEGG) {
  6235. iden = 1;
  6236. ref = 0;
  6237. }
  6238. else {
  6239. iden = 1;
  6240. ref = attr = 0;
  6241. }
  6242. c1 = (short)script_getnum(st,7);
  6243. c2 = (short)script_getnum(st,8);
  6244. c3 = (short)script_getnum(st,9);
  6245. c4 = (short)script_getnum(st,10);
  6246. memset(&it, 0, sizeof(it));
  6247. it.nameid = nameid;
  6248. it.identify = iden;
  6249. it.refine = ref;
  6250. it.attribute = attr;
  6251. it.card[0] = (short)c1;
  6252. it.card[1] = (short)c2;
  6253. it.card[2] = (short)c3;
  6254. it.card[3] = (short)c4;
  6255. it.expire_time = (unsigned int)(time(NULL) + seconds);
  6256. if( (flag = pc_additem(sd, &it, 1, LOG_TYPE_SCRIPT)) ) {
  6257. clif_additem(sd, 0, 0, flag);
  6258. return SCRIPT_CMD_FAILURE;
  6259. }
  6260. return SCRIPT_CMD_SUCCESS;
  6261. }
  6262. /*==========================================
  6263. * gets an item with someone's name inscribed [Skotlex]
  6264. * getinscribeditem item_num, character_name
  6265. * Returned Qty is always 1, only works on equip-able
  6266. * equipment
  6267. *------------------------------------------*/
  6268. BUILDIN_FUNC(getnameditem)
  6269. {
  6270. unsigned short nameid;
  6271. struct item item_tmp;
  6272. TBL_PC *sd, *tsd;
  6273. struct script_data *data;
  6274. sd = script_rid2sd(st);
  6275. if (sd == NULL)
  6276. { //Player not attached!
  6277. script_pushint(st,0);
  6278. return SCRIPT_CMD_SUCCESS;
  6279. }
  6280. data=script_getdata(st,2);
  6281. get_val(st,data);
  6282. if( data_isstring(data) ){
  6283. const char *name=conv_str(st,data);
  6284. struct item_data *item_data = itemdb_searchname(name);
  6285. if( item_data == NULL)
  6286. { //Failed
  6287. script_pushint(st,0);
  6288. return SCRIPT_CMD_SUCCESS;
  6289. }
  6290. nameid = item_data->nameid;
  6291. }else
  6292. nameid = conv_num(st,data);
  6293. if(!itemdb_exists(nameid)/* || itemdb_isstackable(nameid)*/)
  6294. { //Even though named stackable items "could" be risky, they are required for certain quests.
  6295. script_pushint(st,0);
  6296. return SCRIPT_CMD_SUCCESS;
  6297. }
  6298. data=script_getdata(st,3);
  6299. get_val(st,data);
  6300. if( data_isstring(data) ) //Char Name
  6301. tsd=map_nick2sd(conv_str(st,data));
  6302. else //Char Id was given
  6303. tsd=map_charid2sd(conv_num(st,data));
  6304. if( tsd == NULL )
  6305. { //Failed
  6306. script_pushint(st,0);
  6307. return SCRIPT_CMD_SUCCESS;
  6308. }
  6309. memset(&item_tmp,0,sizeof(item_tmp));
  6310. item_tmp.nameid=nameid;
  6311. item_tmp.amount=1;
  6312. item_tmp.identify=1;
  6313. item_tmp.card[0]=CARD0_CREATE; //we don't use 255! because for example SIGNED WEAPON shouldn't get TOP10 BS Fame bonus [Lupus]
  6314. item_tmp.card[2]=tsd->status.char_id;
  6315. item_tmp.card[3]=tsd->status.char_id >> 16;
  6316. if(pc_additem(sd,&item_tmp,1,LOG_TYPE_SCRIPT)) {
  6317. script_pushint(st,0);
  6318. return SCRIPT_CMD_SUCCESS; //Failed to add item, we will not drop if they don't fit
  6319. }
  6320. script_pushint(st,1);
  6321. return SCRIPT_CMD_SUCCESS;
  6322. }
  6323. /*==========================================
  6324. * gets a random item ID from an item group [Skotlex]
  6325. * groupranditem <group_num>{,<sub_group>};
  6326. *------------------------------------------*/
  6327. BUILDIN_FUNC(grouprandomitem) {
  6328. struct s_item_group_entry *entry = NULL;
  6329. int sub_group = 1;
  6330. FETCH(3, sub_group);
  6331. entry = itemdb_get_randgroupitem(script_getnum(st,2),sub_group);
  6332. if (!entry) {
  6333. ShowError("buildin_grouprandomitem: Invalid item group with group_id '%d', sub_group '%d'.\n", script_getnum(st,2), sub_group);
  6334. script_pushint(st,UNKNOWN_ITEM_ID);
  6335. return SCRIPT_CMD_FAILURE;
  6336. }
  6337. script_pushint(st,entry->nameid);
  6338. return SCRIPT_CMD_SUCCESS;
  6339. }
  6340. /**
  6341. * makeitem <item id>,<amount>,"<map name>",<X>,<Y>;
  6342. * makeitem "<item name>",<amount>,"<map name>",<X>,<Y>;
  6343. */
  6344. BUILDIN_FUNC(makeitem) {
  6345. int16 nameid;
  6346. uint16 amount, flag = 0, x, y;
  6347. const char *mapname;
  6348. int m;
  6349. struct item item_tmp;
  6350. struct script_data *data;
  6351. data = script_getdata(st,2);
  6352. get_val(st,data);
  6353. if( data_isstring(data) ){
  6354. const char *name = conv_str(st,data);
  6355. struct item_data *item_data = itemdb_searchname(name);
  6356. if( item_data )
  6357. nameid = item_data->nameid;
  6358. else
  6359. nameid = UNKNOWN_ITEM_ID;
  6360. }
  6361. else
  6362. nameid = conv_num(st,data);
  6363. amount = script_getnum(st,3);
  6364. mapname = script_getstr(st,4);
  6365. x = script_getnum(st,5);
  6366. y = script_getnum(st,6);
  6367. if(strcmp(mapname,"this")==0) {
  6368. TBL_PC *sd;
  6369. sd = script_rid2sd(st);
  6370. if (!sd)
  6371. return SCRIPT_CMD_SUCCESS; //Failed...
  6372. m = sd->bl.m;
  6373. } else
  6374. m = map_mapname2mapid(mapname);
  6375. if(nameid<0) {
  6376. nameid = -nameid;
  6377. flag = 1;
  6378. }
  6379. if(nameid > 0) {
  6380. memset(&item_tmp,0,sizeof(item_tmp));
  6381. item_tmp.nameid = nameid;
  6382. if(!flag)
  6383. item_tmp.identify = 1;
  6384. else
  6385. item_tmp.identify = itemdb_isidentified(nameid);
  6386. map_addflooritem(&item_tmp,amount,m,x,y,0,0,0,4,0);
  6387. }
  6388. return SCRIPT_CMD_SUCCESS;
  6389. }
  6390. /**
  6391. * makeitem2 <item id>,<amount>,"<map name>",<X>,<Y>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>;
  6392. * makeitem2 "<item name>",<amount>,"<map name>",<X>,<Y>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>;
  6393. */
  6394. BUILDIN_FUNC(makeitem2) {
  6395. uint16 nameid, amount, x, y;
  6396. const char *mapname;
  6397. int m;
  6398. struct item item_tmp;
  6399. struct script_data *data;
  6400. struct item_data *id;
  6401. data = script_getdata(st,2);
  6402. get_val(st,data);
  6403. if( data_isstring(data) ){
  6404. const char *name = conv_str(st,data);
  6405. struct item_data *item_data = itemdb_searchname(name);
  6406. if( item_data )
  6407. nameid = item_data->nameid;
  6408. else
  6409. nameid = UNKNOWN_ITEM_ID;
  6410. }
  6411. else
  6412. nameid = conv_num(st,data);
  6413. amount = script_getnum(st,3);
  6414. mapname = script_getstr(st,4);
  6415. x = script_getnum(st,5);
  6416. y = script_getnum(st,6);
  6417. if (strcmp(mapname,"this")==0) {
  6418. TBL_PC *sd;
  6419. sd = script_rid2sd(st);
  6420. if (!sd)
  6421. return SCRIPT_CMD_SUCCESS; //Failed...
  6422. m = sd->bl.m;
  6423. }
  6424. else
  6425. m = map_mapname2mapid(mapname);
  6426. if ((id = itemdb_search(nameid))) {
  6427. char iden, ref, attr;
  6428. memset(&item_tmp,0,sizeof(item_tmp));
  6429. item_tmp.nameid = nameid;
  6430. iden = (char)script_getnum(st,7);
  6431. ref = (char)script_getnum(st,8);
  6432. attr = (char)script_getnum(st,9);
  6433. if (id->type==IT_WEAPON || id->type==IT_ARMOR || id->type==IT_SHADOWGEAR) {
  6434. if(ref > MAX_REFINE) ref = MAX_REFINE;
  6435. }
  6436. else if (id->type==IT_PETEGG) {
  6437. iden = 1;
  6438. ref = 0;
  6439. }
  6440. else {
  6441. iden = 1;
  6442. ref = attr = 0;
  6443. }
  6444. item_tmp.identify = iden;
  6445. item_tmp.refine = ref;
  6446. item_tmp.attribute = attr;
  6447. item_tmp.card[0] = script_getnum(st,10);
  6448. item_tmp.card[1] = script_getnum(st,11);
  6449. item_tmp.card[2] = script_getnum(st,12);
  6450. item_tmp.card[3] = script_getnum(st,13);
  6451. map_addflooritem(&item_tmp,amount,m,x,y,0,0,0,4,0);
  6452. }
  6453. else
  6454. return SCRIPT_CMD_FAILURE;
  6455. return SCRIPT_CMD_SUCCESS;
  6456. }
  6457. /// Counts / deletes the current item given by idx.
  6458. /// Used by buildin_delitem_search
  6459. /// Relies on all input data being already fully valid.
  6460. static void buildin_delitem_delete(struct map_session_data* sd, int idx, int* amount, uint8 loc, bool delete_items)
  6461. {
  6462. int delamount;
  6463. struct item *itm = NULL;
  6464. struct s_storage *gstor;
  6465. switch(loc) {
  6466. case 1: // cart
  6467. itm = &sd->cart.u.items_cart[idx];
  6468. break;
  6469. case 2: // storage
  6470. itm = &sd->storage.u.items_storage[idx];
  6471. break;
  6472. case 3: // guild storage
  6473. {
  6474. gstor = guild2storage2(sd->status.guild_id);
  6475. itm = &gstor->u.items_guild[idx];
  6476. }
  6477. break;
  6478. default: //inventory
  6479. itm = &sd->inventory.u.items_inventory[idx];
  6480. break;
  6481. }
  6482. delamount = ( amount[0] < itm->amount ) ? amount[0] : itm->amount;
  6483. if( delete_items )
  6484. {
  6485. if( itemdb_type(itm->nameid) == IT_PETEGG && itm->card[0] == CARD0_PET )
  6486. {// delete associated pet
  6487. intif_delete_petdata(MakeDWord(itm->card[1], itm->card[2]));
  6488. }
  6489. switch(loc) {
  6490. case 1:
  6491. pc_cart_delitem(sd,idx,delamount,0,LOG_TYPE_SCRIPT);
  6492. break;
  6493. case 2:
  6494. storage_delitem(sd,idx,delamount);
  6495. log_pick_pc(sd,LOG_TYPE_SCRIPT,-delamount,itm);
  6496. break;
  6497. case 3:
  6498. gstor->lock = true;
  6499. storage_guild_delitem(sd, gstor, idx, delamount);
  6500. log_pick_pc(sd, LOG_TYPE_SCRIPT, -delamount, itm);
  6501. storage_guild_storageclose(sd);
  6502. gstor->lock = false;
  6503. break;
  6504. default:
  6505. pc_delitem(sd, idx, delamount, 0, 0, LOG_TYPE_SCRIPT);
  6506. break;
  6507. }
  6508. }
  6509. amount[0]-= delamount;
  6510. }
  6511. /// Searches for item(s) and checks, if there is enough of them.
  6512. /// Used by delitem and delitem2
  6513. /// Relies on all input data being already fully valid.
  6514. /// @param exact_match will also match item attributes and cards, not just name id
  6515. /// @return true when all items could be deleted, false when there were not enough items to delete
  6516. static bool buildin_delitem_search(struct map_session_data* sd, struct item* it, bool exact_match, uint8 loc)
  6517. {
  6518. bool delete_items = false;
  6519. int i, amount, size;
  6520. struct item *items;
  6521. // prefer always non-equipped items
  6522. it->equip = 0;
  6523. // when searching for nameid only, prefer additionally
  6524. if( !exact_match )
  6525. {
  6526. // non-refined items
  6527. it->refine = 0;
  6528. // card-less items
  6529. memset(it->card, 0, sizeof(it->card));
  6530. }
  6531. switch(loc) {
  6532. case 1: // cart
  6533. size = MAX_CART;
  6534. items = sd->cart.u.items_cart;
  6535. break;
  6536. case 2: // storage
  6537. size = MAX_STORAGE;
  6538. items = sd->storage.u.items_storage;
  6539. break;
  6540. case 3: // guild storage
  6541. {
  6542. struct s_storage *gstor = guild2storage2(sd->status.guild_id);
  6543. size = MAX_GUILD_STORAGE;
  6544. items = gstor->u.items_guild;
  6545. }
  6546. break;
  6547. default: //inventory
  6548. size = MAX_INVENTORY;
  6549. items = sd->inventory.u.items_inventory;
  6550. break;
  6551. }
  6552. for(;;)
  6553. {
  6554. unsigned short important = 0;
  6555. amount = it->amount;
  6556. // 1st pass -- less important items / exact match
  6557. for( i = 0; amount && i < size; i++ )
  6558. {
  6559. struct item *itm = NULL;
  6560. if( !&items[i] || !(itm = &items[i])->nameid || itm->nameid != it->nameid )
  6561. {// wrong/invalid item
  6562. continue;
  6563. }
  6564. if( itm->equip != it->equip || itm->refine != it->refine )
  6565. {// not matching attributes
  6566. important++;
  6567. continue;
  6568. }
  6569. if( exact_match )
  6570. {
  6571. if( itm->identify != it->identify || itm->attribute != it->attribute || memcmp(itm->card, it->card, sizeof(itm->card)) )
  6572. {// not matching exact attributes
  6573. continue;
  6574. }
  6575. }
  6576. else
  6577. {
  6578. if( itemdb_type(itm->nameid) == IT_PETEGG )
  6579. {
  6580. if( itm->card[0] == CARD0_PET && CheckForCharServer() )
  6581. {// pet which cannot be deleted
  6582. continue;
  6583. }
  6584. }
  6585. else if( memcmp(itm->card, it->card, sizeof(itm->card)) )
  6586. {// named/carded item
  6587. important++;
  6588. continue;
  6589. }
  6590. }
  6591. // count / delete item
  6592. buildin_delitem_delete(sd, i, &amount, loc, delete_items);
  6593. }
  6594. // 2nd pass -- any matching item
  6595. if( amount == 0 || important == 0 )
  6596. {// either everything was already consumed or no items were skipped
  6597. ;
  6598. }
  6599. else for( i = 0; amount && i < size; i++ )
  6600. {
  6601. struct item *itm = NULL;
  6602. if( !&items[i] || !(itm = &items[i])->nameid || itm->nameid != it->nameid )
  6603. {// wrong/invalid item
  6604. continue;
  6605. }
  6606. if( itemdb_type(itm->nameid) == IT_PETEGG && itm->card[0] == CARD0_PET && CheckForCharServer() )
  6607. {// pet which cannot be deleted
  6608. continue;
  6609. }
  6610. if( exact_match )
  6611. {
  6612. if( itm->refine != it->refine || itm->identify != it->identify || itm->attribute != it->attribute || memcmp(itm->card, it->card, sizeof(itm->card)) )
  6613. {// not matching attributes
  6614. continue;
  6615. }
  6616. }
  6617. // count / delete item
  6618. buildin_delitem_delete(sd, i, &amount, loc, delete_items);
  6619. }
  6620. if( amount )
  6621. {// not enough items
  6622. return false;
  6623. }
  6624. else if( delete_items )
  6625. {// we are done with the work
  6626. return true;
  6627. }
  6628. else
  6629. {// get rid of the items now
  6630. delete_items = true;
  6631. }
  6632. }
  6633. }
  6634. /// Deletes items from the target/attached player.
  6635. /// Prioritizes ordinary items.
  6636. ///
  6637. /// delitem <item id>,<amount>{,<account id>}
  6638. /// delitem "<item name>",<amount>{,<account id>}
  6639. /// cartdelitem <item id>,<amount>{,<account id>}
  6640. /// cartdelitem "<item name>",<amount>{,<account id>}
  6641. /// storagedelitem <item id>,<amount>{,<account id>}
  6642. /// storagedelitem "<item name>",<amount>{,<account id>}
  6643. /// guildstoragedelitem <item id>,<amount>{,<account id>}
  6644. /// guildstoragedelitem "<item name>",<amount>{,<account id>}
  6645. BUILDIN_FUNC(delitem)
  6646. {
  6647. TBL_PC *sd;
  6648. struct item it;
  6649. struct script_data *data;
  6650. uint8 loc = 0;
  6651. char* command = (char*)script_getfuncname(st);
  6652. if(!strncmp(command, "cart", 4))
  6653. loc = 1;
  6654. else if(!strncmp(command, "storage", 7))
  6655. loc = 2;
  6656. else if(!strncmp(command, "guildstorage", 12))
  6657. loc = 3;
  6658. if( script_hasdata(st,4) )
  6659. {
  6660. uint32 account_id = script_getnum(st,4);
  6661. sd = map_id2sd(account_id); // <account id>
  6662. if( sd == NULL )
  6663. {
  6664. ShowError("buildin_%s: player not found (AID=%d).\n", command, account_id);
  6665. st->state = END;
  6666. return SCRIPT_CMD_FAILURE;
  6667. }
  6668. }
  6669. else
  6670. {
  6671. sd = script_rid2sd(st);// attached player
  6672. if( sd == NULL )
  6673. return SCRIPT_CMD_SUCCESS;
  6674. }
  6675. if (loc == 1 && !pc_iscarton(sd)) {
  6676. ShowError("buildin_cartdelitem: player doesn't have cart (CID=%d).\n", sd->status.char_id);
  6677. script_pushint(st, -1);
  6678. return SCRIPT_CMD_FAILURE;
  6679. }
  6680. if (loc == 3) {
  6681. struct s_storage *gstor = guild2storage2(sd->status.guild_id);
  6682. if (gstor == NULL || sd->state.storage_flag) {
  6683. script_pushint(st, -1);
  6684. return SCRIPT_CMD_FAILURE;
  6685. }
  6686. }
  6687. data = script_getdata(st,2);
  6688. get_val(st,data);
  6689. if( data_isstring(data) )
  6690. {
  6691. const char* item_name = conv_str(st,data);
  6692. struct item_data* id = itemdb_searchname(item_name);
  6693. if( id == NULL )
  6694. {
  6695. ShowError("buildin_%s: unknown item \"%s\".\n", command, item_name);
  6696. st->state = END;
  6697. return SCRIPT_CMD_FAILURE;
  6698. }
  6699. it.nameid = id->nameid;// "<item name>"
  6700. }
  6701. else
  6702. {
  6703. it.nameid = conv_num(st,data);// <item id>
  6704. if( !itemdb_exists( it.nameid ) )
  6705. {
  6706. ShowError("buildin_%s: unknown item \"%hu\".\n", command, it.nameid);
  6707. st->state = END;
  6708. return SCRIPT_CMD_FAILURE;
  6709. }
  6710. }
  6711. it.amount=script_getnum(st,3);
  6712. if( it.amount <= 0 )
  6713. return SCRIPT_CMD_SUCCESS;// nothing to do
  6714. if( buildin_delitem_search(sd, &it, false, loc) )
  6715. {// success
  6716. return SCRIPT_CMD_SUCCESS;
  6717. }
  6718. ShowError("buildin_%s: failed to delete %d items (AID=%d item_id=%hu).\n", command, it.amount, sd->status.account_id, it.nameid);
  6719. st->state = END;
  6720. st->mes_active = 0;
  6721. clif_scriptclose(sd, st->oid);
  6722. return SCRIPT_CMD_FAILURE;
  6723. }
  6724. /// Deletes items from the target/attached player.
  6725. ///
  6726. /// delitem2 <item id>,<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>{,<account ID>}
  6727. /// delitem2 "<Item name>",<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>{,<account ID>}
  6728. /// cartdelitem2 <item id>,<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>{,<account ID>}
  6729. /// cartdelitem2 "<Item name>",<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>{,<account ID>}
  6730. /// storagedelitem2 <item id>,<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>{,<account ID>}
  6731. /// storagedelitem2 "<Item name>",<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>{,<account ID>}
  6732. /// guildstoragedelitem2 <item id>,<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>{,<account ID>}
  6733. /// guildstoragedelitem2 "<Item name>",<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>{,<account ID>}
  6734. BUILDIN_FUNC(delitem2)
  6735. {
  6736. TBL_PC *sd;
  6737. struct item it;
  6738. struct script_data *data;
  6739. uint8 loc = 0;
  6740. char* command = (char*)script_getfuncname(st);
  6741. if(!strncmp(command, "cart", 4))
  6742. loc = 1;
  6743. else if(!strncmp(command, "storage", 7))
  6744. loc = 2;
  6745. else if(!strncmp(command, "guildstorage", 12))
  6746. loc = 3;
  6747. if( script_hasdata(st,11) )
  6748. {
  6749. uint32 account_id = script_getnum(st,11);
  6750. sd = map_id2sd(account_id); // <account id>
  6751. if( sd == NULL )
  6752. {
  6753. ShowError("buildin_%s: player not found (AID=%d).\n", command, account_id);
  6754. st->state = END;
  6755. return SCRIPT_CMD_FAILURE;
  6756. }
  6757. }
  6758. else
  6759. {
  6760. sd = script_rid2sd(st);// attached player
  6761. if( sd == NULL )
  6762. return SCRIPT_CMD_SUCCESS;
  6763. }
  6764. if (loc == 1 && !pc_iscarton(sd)) {
  6765. ShowError("buildin_cartdelitem: player doesn't have cart (CID=%d).\n", sd->status.char_id);
  6766. script_pushint(st,-1);
  6767. return SCRIPT_CMD_FAILURE;
  6768. }
  6769. if (loc == 3) {
  6770. struct s_storage *gstor = guild2storage2(sd->status.guild_id);
  6771. if (gstor == NULL || sd->state.storage_flag) {
  6772. script_pushint(st, -1);
  6773. return SCRIPT_CMD_FAILURE;
  6774. }
  6775. }
  6776. data = script_getdata(st,2);
  6777. get_val(st,data);
  6778. if( data_isstring(data) )
  6779. {
  6780. const char* item_name = conv_str(st,data);
  6781. struct item_data* id = itemdb_searchname(item_name);
  6782. if( id == NULL )
  6783. {
  6784. ShowError("buildin_%s: unknown item \"%s\".\n", command, item_name);
  6785. st->state = END;
  6786. return SCRIPT_CMD_FAILURE;
  6787. }
  6788. it.nameid = id->nameid;// "<item name>"
  6789. }
  6790. else
  6791. {
  6792. it.nameid = conv_num(st,data);// <item id>
  6793. if( !itemdb_exists( it.nameid ) )
  6794. {
  6795. ShowError("buildin_%s: unknown item \"%hu\".\n", command, it.nameid);
  6796. st->state = END;
  6797. return SCRIPT_CMD_FAILURE;
  6798. }
  6799. }
  6800. it.amount=script_getnum(st,3);
  6801. it.identify=script_getnum(st,4);
  6802. it.refine=script_getnum(st,5);
  6803. it.attribute=script_getnum(st,6);
  6804. it.card[0]=(short)script_getnum(st,7);
  6805. it.card[1]=(short)script_getnum(st,8);
  6806. it.card[2]=(short)script_getnum(st,9);
  6807. it.card[3]=(short)script_getnum(st,10);
  6808. if( it.amount <= 0 )
  6809. return SCRIPT_CMD_SUCCESS;// nothing to do
  6810. if( buildin_delitem_search(sd, &it, true, loc) )
  6811. {// success
  6812. return SCRIPT_CMD_SUCCESS;
  6813. }
  6814. ShowError("buildin_%s: failed to delete %d items (AID=%d item_id=%hu).\n", command, it.amount, sd->status.account_id, it.nameid);
  6815. st->state = END;
  6816. st->mes_active = 0;
  6817. clif_scriptclose(sd, st->oid);
  6818. return SCRIPT_CMD_FAILURE;
  6819. }
  6820. /*==========================================
  6821. * Enables/Disables use of items while in an NPC [Skotlex]
  6822. *------------------------------------------*/
  6823. BUILDIN_FUNC(enableitemuse)
  6824. {
  6825. TBL_PC *sd;
  6826. sd=script_rid2sd(st);
  6827. if (sd)
  6828. st->npc_item_flag = sd->npc_item_flag = 1;
  6829. return SCRIPT_CMD_SUCCESS;
  6830. }
  6831. BUILDIN_FUNC(disableitemuse)
  6832. {
  6833. TBL_PC *sd;
  6834. sd=script_rid2sd(st);
  6835. if (sd)
  6836. st->npc_item_flag = sd->npc_item_flag = 0;
  6837. return SCRIPT_CMD_SUCCESS;
  6838. }
  6839. /*==========================================
  6840. * Returns a character's specified stat.
  6841. * Check pc_readparam for available options.
  6842. * readparam <param>{,"<nick>"}
  6843. *------------------------------------------*/
  6844. BUILDIN_FUNC(readparam)
  6845. {
  6846. int type;
  6847. struct script_data *data = script_getdata(st, 2);
  6848. TBL_PC *sd;
  6849. if (!script_nick2sd(3,sd)) {
  6850. script_pushint(st,-1);
  6851. return SCRIPT_CMD_FAILURE;
  6852. }
  6853. if (data->type == C_NAME) { // If using constant name, just get the val
  6854. get_val_(st, data, sd);
  6855. script_pushint(st, (int)data->u.num);
  6856. return SCRIPT_CMD_SUCCESS;
  6857. }
  6858. type = script_getnum(st, 2);
  6859. script_pushint(st,pc_readparam(sd,type));
  6860. return SCRIPT_CMD_SUCCESS;
  6861. }
  6862. /*==========================================
  6863. * Return charid identification
  6864. * return by @num :
  6865. * 0 : char_id
  6866. * 1 : party_id
  6867. * 2 : guild_id
  6868. * 3 : account_id
  6869. * 4 : bg_id
  6870. *------------------------------------------*/
  6871. BUILDIN_FUNC(getcharid)
  6872. {
  6873. int num;
  6874. TBL_PC *sd;
  6875. num = script_getnum(st,2);
  6876. if( script_hasdata(st,3) )
  6877. sd=map_nick2sd(script_getstr(st,3));
  6878. else
  6879. sd=script_rid2sd(st);
  6880. if(sd==NULL){
  6881. script_pushint(st,0); //return 0, according docs
  6882. return SCRIPT_CMD_SUCCESS;
  6883. }
  6884. switch( num ) {
  6885. case 0: script_pushint(st,sd->status.char_id); break;
  6886. case 1: script_pushint(st,sd->status.party_id); break;
  6887. case 2: script_pushint(st,sd->status.guild_id); break;
  6888. case 3: script_pushint(st,sd->status.account_id); break;
  6889. case 4: script_pushint(st,sd->bg_id); break;
  6890. default:
  6891. ShowError("buildin_getcharid: invalid parameter (%d).\n", num);
  6892. script_pushint(st,0);
  6893. break;
  6894. }
  6895. return SCRIPT_CMD_SUCCESS;
  6896. }
  6897. /*==========================================
  6898. * returns the GID of an NPC
  6899. *------------------------------------------*/
  6900. BUILDIN_FUNC(getnpcid)
  6901. {
  6902. int num = script_getnum(st,2);
  6903. struct npc_data* nd = NULL;
  6904. if( script_hasdata(st,3) )
  6905. {// unique npc name
  6906. if( ( nd = npc_name2id(script_getstr(st,3)) ) == NULL )
  6907. {
  6908. ShowError("buildin_getnpcid: No such NPC '%s'.\n", script_getstr(st,3));
  6909. script_pushint(st,0);
  6910. return SCRIPT_CMD_FAILURE;
  6911. }
  6912. }
  6913. switch (num) {
  6914. case 0:
  6915. script_pushint(st,nd ? nd->bl.id : st->oid);
  6916. break;
  6917. default:
  6918. ShowError("buildin_getnpcid: invalid parameter (%d).\n", num);
  6919. script_pushint(st,0);
  6920. return SCRIPT_CMD_FAILURE;
  6921. }
  6922. return SCRIPT_CMD_SUCCESS;
  6923. }
  6924. /*==========================================
  6925. * Return the name of the party_id
  6926. * null if not found
  6927. *------------------------------------------*/
  6928. BUILDIN_FUNC(getpartyname)
  6929. {
  6930. int party_id;
  6931. struct party_data* p;
  6932. party_id = script_getnum(st,2);
  6933. if( ( p = party_search(party_id) ) != NULL )
  6934. {
  6935. script_pushstrcopy(st,p->party.name);
  6936. }
  6937. else
  6938. {
  6939. script_pushconststr(st,"null");
  6940. }
  6941. return SCRIPT_CMD_SUCCESS;
  6942. }
  6943. /*==========================================
  6944. * Get the information of the members of a party by type
  6945. * @party_id, @type
  6946. * return by @type :
  6947. * - : nom des membres
  6948. * 1 : char_id des membres
  6949. * 2 : account_id des membres
  6950. *------------------------------------------*/
  6951. BUILDIN_FUNC(getpartymember)
  6952. {
  6953. struct party_data *p;
  6954. uint8 j = 0;
  6955. p = party_search(script_getnum(st,2));
  6956. if (p != NULL) {
  6957. uint8 i, type = 0;
  6958. struct script_data *data = NULL;
  6959. char *varname = NULL;
  6960. if (script_hasdata(st,3))
  6961. type = script_getnum(st,3);
  6962. if (script_hasdata(st,4)) {
  6963. data = script_getdata(st, 4);
  6964. if (!data_isreference(data)) {
  6965. ShowError("buildin_getpartymember: Error in argument! Please give a variable to store values in.\n");
  6966. return SCRIPT_CMD_FAILURE;
  6967. }
  6968. varname = reference_getname(data);
  6969. if (type <= 0 && varname[strlen(varname)-1] != '$') {
  6970. ShowError("buildin_getpartymember: The array %s is not string type.\n", varname);
  6971. return SCRIPT_CMD_FAILURE;
  6972. }
  6973. }
  6974. for (i = 0; i < MAX_PARTY; i++) {
  6975. if (p->party.member[i].account_id) {
  6976. switch (type) {
  6977. case 2:
  6978. if (data)
  6979. setd_sub(st, NULL, varname, j, (void *)__64BPRTSIZE(p->party.member[i].account_id), data->ref);
  6980. else
  6981. mapreg_setreg(reference_uid(add_str("$@partymemberaid"), j),p->party.member[i].account_id);
  6982. break;
  6983. case 1:
  6984. if (data)
  6985. setd_sub(st, NULL, varname, j, (void *)__64BPRTSIZE(p->party.member[i].char_id), data->ref);
  6986. else
  6987. mapreg_setreg(reference_uid(add_str("$@partymembercid"), j),p->party.member[i].char_id);
  6988. break;
  6989. default:
  6990. if (data)
  6991. setd_sub(st, NULL, varname, j, (void *)__64BPRTSIZE(p->party.member[i].name), data->ref);
  6992. else
  6993. mapreg_setregstr(reference_uid(add_str("$@partymembername$"), j),p->party.member[i].name);
  6994. break;
  6995. }
  6996. j++;
  6997. }
  6998. }
  6999. }
  7000. mapreg_setreg(add_str("$@partymembercount"),j);
  7001. script_pushint(st, j);
  7002. return SCRIPT_CMD_SUCCESS;
  7003. }
  7004. /*==========================================
  7005. * Retrieves party leader. if flag is specified,
  7006. * return some of the leader data. Otherwise, return name.
  7007. *------------------------------------------*/
  7008. BUILDIN_FUNC(getpartyleader)
  7009. {
  7010. int party_id, type = 0, i=0;
  7011. struct party_data *p;
  7012. party_id=script_getnum(st,2);
  7013. if( script_hasdata(st,3) )
  7014. type=script_getnum(st,3);
  7015. p=party_search(party_id);
  7016. if (p) //Search leader
  7017. for(i = 0; i < MAX_PARTY && !p->party.member[i].leader; i++);
  7018. if (!p || i == MAX_PARTY) { //leader not found
  7019. if (type)
  7020. script_pushint(st,-1);
  7021. else
  7022. script_pushconststr(st,"null");
  7023. return SCRIPT_CMD_SUCCESS;
  7024. }
  7025. switch (type) {
  7026. case 1: script_pushint(st,p->party.member[i].account_id); break;
  7027. case 2: script_pushint(st,p->party.member[i].char_id); break;
  7028. case 3: script_pushint(st,p->party.member[i].class_); break;
  7029. case 4: script_pushstrcopy(st,mapindex_id2name(p->party.member[i].map)); break;
  7030. case 5: script_pushint(st,p->party.member[i].lv); break;
  7031. default: script_pushstrcopy(st,p->party.member[i].name); break;
  7032. }
  7033. return SCRIPT_CMD_SUCCESS;
  7034. }
  7035. /*==========================================
  7036. * Return the name of the @guild_id
  7037. * null if not found
  7038. *------------------------------------------*/
  7039. BUILDIN_FUNC(getguildname)
  7040. {
  7041. int guild_id;
  7042. struct guild* g;
  7043. guild_id = script_getnum(st,2);
  7044. if( ( g = guild_search(guild_id) ) != NULL )
  7045. script_pushstrcopy(st,g->name);
  7046. else
  7047. script_pushconststr(st,"null");
  7048. return SCRIPT_CMD_SUCCESS;
  7049. }
  7050. /*==========================================
  7051. * Return the name of the guild master of @guild_id
  7052. * null if not found
  7053. *------------------------------------------*/
  7054. BUILDIN_FUNC(getguildmaster)
  7055. {
  7056. int guild_id;
  7057. struct guild* g;
  7058. guild_id = script_getnum(st,2);
  7059. if( ( g = guild_search(guild_id) ) != NULL )
  7060. script_pushstrcopy(st,g->member[0].name);
  7061. else
  7062. script_pushconststr(st,"null");
  7063. return SCRIPT_CMD_SUCCESS;
  7064. }
  7065. BUILDIN_FUNC(getguildmasterid)
  7066. {
  7067. int guild_id;
  7068. struct guild* g;
  7069. guild_id = script_getnum(st,2);
  7070. if( ( g = guild_search(guild_id) ) != NULL )
  7071. script_pushint(st,g->member[0].char_id);
  7072. else
  7073. script_pushint(st,0);
  7074. return SCRIPT_CMD_SUCCESS;
  7075. }
  7076. /*==========================================
  7077. * Get char string information by type :
  7078. * Return by @type :
  7079. * 0 : char_name
  7080. * 1 : party_name or ""
  7081. * 2 : guild_name or ""
  7082. * 3 : map_name
  7083. * - : ""
  7084. * strcharinfo(<type>{,<char_id>})
  7085. *------------------------------------------*/
  7086. BUILDIN_FUNC(strcharinfo)
  7087. {
  7088. TBL_PC *sd;
  7089. int num;
  7090. struct guild* g;
  7091. struct party_data* p;
  7092. if (!script_charid2sd(3,sd)) {
  7093. script_pushconststr(st,"");
  7094. return SCRIPT_CMD_FAILURE;
  7095. }
  7096. num=script_getnum(st,2);
  7097. switch(num){
  7098. case 0:
  7099. script_pushstrcopy(st,sd->status.name);
  7100. break;
  7101. case 1:
  7102. if( ( p = party_search(sd->status.party_id) ) != NULL ) {
  7103. script_pushstrcopy(st,p->party.name);
  7104. } else {
  7105. script_pushconststr(st,"");
  7106. }
  7107. break;
  7108. case 2:
  7109. if( ( g = sd->guild ) != NULL ) {
  7110. script_pushstrcopy(st,g->name);
  7111. } else {
  7112. script_pushconststr(st,"");
  7113. }
  7114. break;
  7115. case 3:
  7116. script_pushconststr(st,map[sd->bl.m].name);
  7117. break;
  7118. default:
  7119. ShowWarning("buildin_strcharinfo: unknown parameter.\n");
  7120. script_pushconststr(st,"");
  7121. break;
  7122. }
  7123. return SCRIPT_CMD_SUCCESS;
  7124. }
  7125. /*==========================================
  7126. * Get npc string information by type
  7127. * return by @type:
  7128. * 0 : name
  7129. * 1 : str#
  7130. * 2 : #str
  7131. * 3 : ::str
  7132. * 4 : map name
  7133. *------------------------------------------*/
  7134. BUILDIN_FUNC(strnpcinfo)
  7135. {
  7136. TBL_NPC* nd;
  7137. int num;
  7138. char *buf,*name=NULL;
  7139. nd = map_id2nd(st->oid);
  7140. if (!nd) {
  7141. script_pushconststr(st, "");
  7142. return SCRIPT_CMD_SUCCESS;
  7143. }
  7144. num = script_getnum(st,2);
  7145. switch(num){
  7146. case 0: // display name
  7147. name = aStrdup(nd->name);
  7148. break;
  7149. case 1: // visible part of display name
  7150. if((buf = strchr(nd->name,'#')) != NULL)
  7151. {
  7152. name = aStrdup(nd->name);
  7153. name[buf - nd->name] = 0;
  7154. } else // Return the name, there is no '#' present
  7155. name = aStrdup(nd->name);
  7156. break;
  7157. case 2: // # fragment
  7158. if((buf = strchr(nd->name,'#')) != NULL)
  7159. name = aStrdup(buf+1);
  7160. break;
  7161. case 3: // unique name
  7162. name = aStrdup(nd->exname);
  7163. break;
  7164. case 4: // map name
  7165. name = aStrdup(map[nd->bl.m].name);
  7166. break;
  7167. }
  7168. if(name)
  7169. script_pushstr(st, name);
  7170. else
  7171. script_pushconststr(st, "");
  7172. return SCRIPT_CMD_SUCCESS;
  7173. }
  7174. /// aegis->athena slot position conversion table
  7175. /// Index is used by EQI_ in db/const.txt
  7176. static unsigned int equip[] = {
  7177. EQP_HEAD_TOP,
  7178. EQP_ARMOR,
  7179. EQP_HAND_L,
  7180. EQP_HAND_R,
  7181. EQP_GARMENT,
  7182. EQP_SHOES,
  7183. EQP_ACC_L,
  7184. EQP_ACC_R,
  7185. EQP_HEAD_MID,
  7186. EQP_HEAD_LOW,
  7187. EQP_COSTUME_HEAD_LOW,
  7188. EQP_COSTUME_HEAD_MID,
  7189. EQP_COSTUME_HEAD_TOP,
  7190. EQP_COSTUME_GARMENT,
  7191. EQP_AMMO,
  7192. EQP_SHADOW_ARMOR,
  7193. EQP_SHADOW_WEAPON,
  7194. EQP_SHADOW_SHIELD,
  7195. EQP_SHADOW_SHOES,
  7196. EQP_SHADOW_ACC_R,
  7197. EQP_SHADOW_ACC_L
  7198. };
  7199. /**
  7200. * getequipid(<equipment slot>{,<char_id>})
  7201. **/
  7202. BUILDIN_FUNC(getequipid)
  7203. {
  7204. int i, num;
  7205. TBL_PC* sd;
  7206. struct item_data* item;
  7207. if (!script_charid2sd(3, sd)) {
  7208. script_pushint(st,-1);
  7209. return SCRIPT_CMD_FAILURE;
  7210. }
  7211. num = script_getnum(st,2) - 1;
  7212. if( num < 0 || num >= ARRAYLENGTH(equip) )
  7213. {
  7214. script_pushint(st,-1);
  7215. return SCRIPT_CMD_FAILURE;
  7216. }
  7217. // get inventory position of item
  7218. i = pc_checkequip(sd,equip[num]);
  7219. if( i < 0 )
  7220. {
  7221. script_pushint(st,-1);
  7222. return SCRIPT_CMD_SUCCESS;
  7223. }
  7224. item = sd->inventory_data[i];
  7225. if( item != 0 )
  7226. script_pushint(st,item->nameid);
  7227. else
  7228. script_pushint(st,0);
  7229. return SCRIPT_CMD_SUCCESS;
  7230. }
  7231. /*==========================================
  7232. * getequipuniqueid(<equipment slot>{,<char_id>})
  7233. *------------------------------------------*/
  7234. BUILDIN_FUNC(getequipuniqueid)
  7235. {
  7236. int i, num;
  7237. TBL_PC* sd;
  7238. struct item* item;
  7239. if (!script_charid2sd(3, sd)) {
  7240. script_pushconststr(st, "");
  7241. return SCRIPT_CMD_FAILURE;
  7242. }
  7243. num = script_getnum(st,2) - 1;
  7244. if (num < 0 || num >= ARRAYLENGTH(equip)) {
  7245. script_pushconststr(st, "");
  7246. return SCRIPT_CMD_FAILURE;
  7247. }
  7248. // get inventory position of item
  7249. i = pc_checkequip(sd,equip[num]);
  7250. if (i < 0) {
  7251. script_pushconststr(st, "");
  7252. return SCRIPT_CMD_FAILURE;
  7253. }
  7254. item = &sd->inventory.u.items_inventory[i];
  7255. if (item != 0) {
  7256. int maxlen = 256;
  7257. char *buf = (char *)aMalloc(maxlen*sizeof(char));
  7258. memset(buf, 0, maxlen);
  7259. snprintf(buf, maxlen-1, "%llu", (unsigned long long)item->unique_id);
  7260. script_pushstr(st, buf);
  7261. } else
  7262. script_pushconststr(st, "");
  7263. return SCRIPT_CMD_SUCCESS;
  7264. }
  7265. /*==========================================
  7266. * Get the equipement name at pos
  7267. * return item jname or ""
  7268. * getequipname(<equipment slot>{,<char_id>})
  7269. *------------------------------------------*/
  7270. BUILDIN_FUNC(getequipname)
  7271. {
  7272. int i, num;
  7273. TBL_PC* sd;
  7274. struct item_data* item;
  7275. if (!script_charid2sd(3, sd)) {
  7276. script_pushconststr(st, "");
  7277. return SCRIPT_CMD_FAILURE;
  7278. }
  7279. num = script_getnum(st,2) - 1;
  7280. if( num < 0 || num >= ARRAYLENGTH(equip) )
  7281. {
  7282. script_pushconststr(st,"");
  7283. return SCRIPT_CMD_SUCCESS;
  7284. }
  7285. // get inventory position of item
  7286. i = pc_checkequip(sd,equip[num]);
  7287. if( i < 0 )
  7288. {
  7289. script_pushconststr(st,"");
  7290. return SCRIPT_CMD_SUCCESS;
  7291. }
  7292. item = sd->inventory_data[i];
  7293. if( item != 0 )
  7294. script_pushstrcopy(st,item->jname);
  7295. else
  7296. script_pushconststr(st,"");
  7297. return SCRIPT_CMD_SUCCESS;
  7298. }
  7299. /*==========================================
  7300. * getbrokenid [Valaris]
  7301. * getbrokenid(<number>{,<char_id>})
  7302. *------------------------------------------*/
  7303. BUILDIN_FUNC(getbrokenid)
  7304. {
  7305. int i,num,id = 0,brokencounter = 0;
  7306. TBL_PC *sd;
  7307. if (!script_charid2sd(3, sd)) {
  7308. script_pushint(st,0);
  7309. return SCRIPT_CMD_FAILURE;
  7310. }
  7311. num = script_getnum(st,2);
  7312. for(i = 0; i < MAX_INVENTORY; i++) {
  7313. if(sd->inventory.u.items_inventory[i].attribute) {
  7314. brokencounter++;
  7315. if(num == brokencounter){
  7316. id = sd->inventory.u.items_inventory[i].nameid;
  7317. break;
  7318. }
  7319. }
  7320. }
  7321. script_pushint(st,id);
  7322. return SCRIPT_CMD_SUCCESS;
  7323. }
  7324. /*==========================================
  7325. * repair [Valaris]
  7326. * repair <num>{,<char_id>};
  7327. *------------------------------------------*/
  7328. BUILDIN_FUNC(repair)
  7329. {
  7330. int i,num;
  7331. int repaircounter = 0;
  7332. TBL_PC *sd;
  7333. if (!script_charid2sd(3,sd))
  7334. return SCRIPT_CMD_FAILURE;
  7335. num = script_getnum(st,2);
  7336. for(i = 0; i < MAX_INVENTORY; i++) {
  7337. if(sd->inventory.u.items_inventory[i].attribute) {
  7338. repaircounter++;
  7339. if(num == repaircounter) {
  7340. sd->inventory.u.items_inventory[i].attribute = 0;
  7341. clif_equiplist(sd);
  7342. clif_produceeffect(sd, 0, sd->inventory.u.items_inventory[i].nameid);
  7343. clif_misceffect(&sd->bl, 3);
  7344. break;
  7345. }
  7346. }
  7347. }
  7348. return SCRIPT_CMD_SUCCESS;
  7349. }
  7350. /*==========================================
  7351. * repairall {<char_id>}
  7352. *------------------------------------------*/
  7353. BUILDIN_FUNC(repairall)
  7354. {
  7355. int i, repaircounter = 0;
  7356. TBL_PC *sd;
  7357. if (!script_charid2sd(2,sd))
  7358. return SCRIPT_CMD_FAILURE;
  7359. for(i = 0; i < MAX_INVENTORY; i++)
  7360. {
  7361. if(sd->inventory.u.items_inventory[i].nameid && sd->inventory.u.items_inventory[i].attribute)
  7362. {
  7363. sd->inventory.u.items_inventory[i].attribute = 0;
  7364. clif_produceeffect(sd,0,sd->inventory.u.items_inventory[i].nameid);
  7365. repaircounter++;
  7366. }
  7367. }
  7368. if(repaircounter)
  7369. {
  7370. clif_misceffect(&sd->bl, 3);
  7371. clif_equiplist(sd);
  7372. }
  7373. return SCRIPT_CMD_SUCCESS;
  7374. }
  7375. /*==========================================
  7376. * Chk if player have something equiped at pos
  7377. * getequipisequiped <pos>{,<char_id>}
  7378. *------------------------------------------*/
  7379. BUILDIN_FUNC(getequipisequiped)
  7380. {
  7381. int i = -1,num;
  7382. TBL_PC *sd;
  7383. num = script_getnum(st,2);
  7384. if (!script_charid2sd(3,sd))
  7385. return SCRIPT_CMD_FAILURE;
  7386. if (num > 0 && num <= ARRAYLENGTH(equip))
  7387. i=pc_checkequip(sd,equip[num-1]);
  7388. if(i >= 0)
  7389. script_pushint(st,1);
  7390. else
  7391. script_pushint(st,0);
  7392. return SCRIPT_CMD_SUCCESS;
  7393. }
  7394. /*==========================================
  7395. * Chk if the player have something equiped at pos
  7396. * if so chk if this item ain't marked not refinable or rental
  7397. * return (npc)
  7398. * 1 : true
  7399. * 0 : false
  7400. * getequipisenableref(<equipment slot>{,<char_id>})
  7401. *------------------------------------------*/
  7402. BUILDIN_FUNC(getequipisenableref)
  7403. {
  7404. int i = -1,num;
  7405. TBL_PC *sd;
  7406. num = script_getnum(st,2);
  7407. if (!script_charid2sd(3, sd)) {
  7408. script_pushint(st,0);
  7409. return SCRIPT_CMD_FAILURE;
  7410. }
  7411. if( num > 0 && num <= ARRAYLENGTH(equip) )
  7412. i = pc_checkequip(sd,equip[num-1]);
  7413. if( i >= 0 && sd->inventory_data[i] && !sd->inventory_data[i]->flag.no_refine && !sd->inventory.u.items_inventory[i].expire_time )
  7414. script_pushint(st,1);
  7415. else
  7416. script_pushint(st,0);
  7417. return SCRIPT_CMD_SUCCESS;
  7418. }
  7419. /*==========================================
  7420. * Get the item refined value at pos
  7421. * return (npc)
  7422. * x : refine amount
  7423. * 0 : false (not refined)
  7424. * getequiprefinerycnt(<equipment slot>{,<char_id>})
  7425. *------------------------------------------*/
  7426. BUILDIN_FUNC(getequiprefinerycnt)
  7427. {
  7428. int i = -1,num;
  7429. TBL_PC *sd;
  7430. num = script_getnum(st,2);
  7431. if (!script_charid2sd(3, sd)) {
  7432. script_pushint(st,0);
  7433. return SCRIPT_CMD_FAILURE;
  7434. }
  7435. if (num > 0 && num <= ARRAYLENGTH(equip))
  7436. i=pc_checkequip(sd,equip[num-1]);
  7437. if(i >= 0)
  7438. script_pushint(st,sd->inventory.u.items_inventory[i].refine);
  7439. else
  7440. script_pushint(st,0);
  7441. return SCRIPT_CMD_SUCCESS;
  7442. }
  7443. /*==========================================
  7444. * Get the weapon level value at pos
  7445. * (pos should normally only be EQI_HAND_L or EQI_HAND_R)
  7446. * return (npc)
  7447. * x : weapon level
  7448. * 0 : false
  7449. * getequipweaponlv(<equipment slot>{,<char_id>})
  7450. *------------------------------------------*/
  7451. BUILDIN_FUNC(getequipweaponlv)
  7452. {
  7453. int i = -1,num;
  7454. TBL_PC *sd;
  7455. num = script_getnum(st,2);
  7456. if (!script_charid2sd(3, sd)) {
  7457. script_pushint(st,0);
  7458. return SCRIPT_CMD_FAILURE;
  7459. }
  7460. if (num > 0 && num <= ARRAYLENGTH(equip))
  7461. i=pc_checkequip(sd,equip[num-1]);
  7462. if(i >= 0 && sd->inventory_data[i])
  7463. script_pushint(st,sd->inventory_data[i]->wlv);
  7464. else
  7465. script_pushint(st,0);
  7466. return SCRIPT_CMD_SUCCESS;
  7467. }
  7468. /*==========================================
  7469. * Get the item refine chance (from refine.txt) for item at pos
  7470. * return (npc)
  7471. * x : refine chance
  7472. * 0 : false (max refine level or unequip..)
  7473. * getequippercentrefinery(<equipment slot>{,<char_id>})
  7474. *------------------------------------------*/
  7475. BUILDIN_FUNC(getequippercentrefinery)
  7476. {
  7477. int i = -1,num;
  7478. TBL_PC *sd;
  7479. num = script_getnum(st,2);
  7480. if (!script_charid2sd(3, sd)) {
  7481. script_pushint(st,0);
  7482. return SCRIPT_CMD_FAILURE;
  7483. }
  7484. if (num > 0 && num <= ARRAYLENGTH(equip))
  7485. i = pc_checkequip(sd,equip[num-1]);
  7486. if(i >= 0 && sd->inventory.u.items_inventory[i].nameid && sd->inventory.u.items_inventory[i].refine < MAX_REFINE)
  7487. script_pushint(st,status_get_refine_chance((enum refine_type)itemdb_wlv(sd->inventory.u.items_inventory[i].nameid), (int)sd->inventory.u.items_inventory[i].refine));
  7488. else
  7489. script_pushint(st,0);
  7490. return SCRIPT_CMD_SUCCESS;
  7491. }
  7492. /*==========================================
  7493. * Refine +1 item at pos and log and display refine
  7494. * successrefitem <equipment slot>{,<count>{,<char_id>}}
  7495. *------------------------------------------*/
  7496. BUILDIN_FUNC(successrefitem) {
  7497. short i = -1, up = 1;
  7498. int pos;
  7499. TBL_PC *sd;
  7500. pos = script_getnum(st,2);
  7501. if (!script_charid2sd(4, sd)) {
  7502. script_pushint(st, -1);
  7503. return SCRIPT_CMD_FAILURE;
  7504. }
  7505. if (script_hasdata(st, 3))
  7506. up = script_getnum(st, 3);
  7507. if (pos > 0 && pos <= ARRAYLENGTH(equip))
  7508. i = pc_checkequip(sd,equip[pos-1]);
  7509. if (i >= 0) {
  7510. unsigned int ep = sd->inventory.u.items_inventory[i].equip;
  7511. //Logs items, got from (N)PC scripts [Lupus]
  7512. log_pick_pc(sd, LOG_TYPE_SCRIPT, -1, &sd->inventory.u.items_inventory[i]);
  7513. if (sd->inventory.u.items_inventory[i].refine >= MAX_REFINE) {
  7514. script_pushint(st, MAX_REFINE);
  7515. return SCRIPT_CMD_SUCCESS;
  7516. }
  7517. sd->inventory.u.items_inventory[i].refine += up;
  7518. sd->inventory.u.items_inventory[i].refine = cap_value( sd->inventory.u.items_inventory[i].refine, 0, MAX_REFINE);
  7519. pc_unequipitem(sd,i,2); // status calc will happen in pc_equipitem() below
  7520. clif_refine(sd->fd,0,i,sd->inventory.u.items_inventory[i].refine);
  7521. clif_delitem(sd,i,1,3);
  7522. //Logs items, got from (N)PC scripts [Lupus]
  7523. log_pick_pc(sd, LOG_TYPE_SCRIPT, 1, &sd->inventory.u.items_inventory[i]);
  7524. clif_additem(sd,i,1,0);
  7525. pc_equipitem(sd,i,ep);
  7526. clif_misceffect(&sd->bl,3);
  7527. if (sd->inventory.u.items_inventory[i].refine == MAX_REFINE &&
  7528. sd->inventory.u.items_inventory[i].card[0] == CARD0_FORGE &&
  7529. sd->status.char_id == (int)MakeDWord(sd->inventory.u.items_inventory[i].card[2],sd->inventory.u.items_inventory[i].card[3]))
  7530. { // Fame point system [DracoRPG]
  7531. switch (sd->inventory_data[i]->wlv){
  7532. case 1:
  7533. pc_addfame(sd, battle_config.fame_refine_lv1); // Success to refine to +10 a lv1 weapon you forged = +1 fame point
  7534. break;
  7535. case 2:
  7536. pc_addfame(sd, battle_config.fame_refine_lv2); // Success to refine to +10 a lv2 weapon you forged = +25 fame point
  7537. break;
  7538. case 3:
  7539. pc_addfame(sd, battle_config.fame_refine_lv3); // Success to refine to +10 a lv3 weapon you forged = +1000 fame point
  7540. break;
  7541. }
  7542. }
  7543. script_pushint(st, sd->inventory.u.items_inventory[i].refine);
  7544. return SCRIPT_CMD_SUCCESS;
  7545. }
  7546. ShowError("buildin_successrefitem: No item equipped at pos %d (CID=%d/AID=%d).\n", pos, sd->status.char_id, sd->status.account_id);
  7547. script_pushint(st, -1);
  7548. return SCRIPT_CMD_FAILURE;
  7549. }
  7550. /*==========================================
  7551. * Show a failed Refine +1 attempt
  7552. * failedrefitem <equipment slot>{,<char_id>}
  7553. *------------------------------------------*/
  7554. BUILDIN_FUNC(failedrefitem) {
  7555. short i = -1;
  7556. int pos;
  7557. TBL_PC *sd;
  7558. pos = script_getnum(st,2);
  7559. if (!script_charid2sd(3, sd)) {
  7560. script_pushint(st, 0);
  7561. return SCRIPT_CMD_FAILURE;
  7562. }
  7563. if (pos > 0 && pos <= ARRAYLENGTH(equip))
  7564. i = pc_checkequip(sd,equip[pos-1]);
  7565. if (i >= 0) {
  7566. sd->inventory.u.items_inventory[i].refine = 0;
  7567. pc_unequipitem(sd,i,3); //recalculate bonus
  7568. clif_refine(sd->fd,1,i,sd->inventory.u.items_inventory[i].refine); //notify client of failure
  7569. pc_delitem(sd,i,1,0,2,LOG_TYPE_SCRIPT);
  7570. clif_misceffect(&sd->bl,2); // display failure effect
  7571. script_pushint(st, 1);
  7572. return SCRIPT_CMD_SUCCESS;
  7573. }
  7574. ShowError("buildin_failedrefitem: No item equipped at pos %d (CID=%d/AID=%d).\n", pos, sd->status.char_id, sd->status.account_id);
  7575. script_pushint(st, 0);
  7576. return SCRIPT_CMD_FAILURE;
  7577. }
  7578. /*==========================================
  7579. * Downgrades an Equipment Part by -1 . [Masao]
  7580. * downrefitem <equipment slot>{,<count>{,<char_id>}}
  7581. *------------------------------------------*/
  7582. BUILDIN_FUNC(downrefitem) {
  7583. short i = -1, down = 1;
  7584. int pos;
  7585. TBL_PC *sd;
  7586. if (!script_charid2sd(4, sd)) {
  7587. script_pushint(st, -1);
  7588. return SCRIPT_CMD_FAILURE;
  7589. }
  7590. pos = script_getnum(st,2);
  7591. if (script_hasdata(st, 3))
  7592. down = script_getnum(st, 3);
  7593. if (pos > 0 && pos <= ARRAYLENGTH(equip))
  7594. i = pc_checkequip(sd,equip[pos-1]);
  7595. if (i >= 0) {
  7596. unsigned int ep = sd->inventory.u.items_inventory[i].equip;
  7597. //Logs items, got from (N)PC scripts [Lupus]
  7598. log_pick_pc(sd, LOG_TYPE_SCRIPT, -1, &sd->inventory.u.items_inventory[i]);
  7599. pc_unequipitem(sd,i,2); // status calc will happen in pc_equipitem() below
  7600. sd->inventory.u.items_inventory[i].refine -= down;
  7601. sd->inventory.u.items_inventory[i].refine = cap_value( sd->inventory.u.items_inventory[i].refine, 0, MAX_REFINE);
  7602. clif_refine(sd->fd,2,i,sd->inventory.u.items_inventory[i].refine);
  7603. clif_delitem(sd,i,1,3);
  7604. //Logs items, got from (N)PC scripts [Lupus]
  7605. log_pick_pc(sd, LOG_TYPE_SCRIPT, 1, &sd->inventory.u.items_inventory[i]);
  7606. clif_additem(sd,i,1,0);
  7607. pc_equipitem(sd,i,ep);
  7608. clif_misceffect(&sd->bl,2);
  7609. script_pushint(st, sd->inventory.u.items_inventory[i].refine);
  7610. return SCRIPT_CMD_SUCCESS;
  7611. }
  7612. ShowError("buildin_downrefitem: No item equipped at pos %d (CID=%d/AID=%d).\n", pos, sd->status.char_id, sd->status.account_id);
  7613. script_pushint(st, -1);
  7614. return SCRIPT_CMD_FAILURE;
  7615. }
  7616. /**
  7617. * Delete the item equipped at pos.
  7618. * delequip <equipment slot>{,<char_id>};
  7619. **/
  7620. BUILDIN_FUNC(delequip) {
  7621. short i = -1;
  7622. int pos;
  7623. int8 ret;
  7624. TBL_PC *sd;
  7625. pos = script_getnum(st,2);
  7626. if (!script_charid2sd(3,sd)) {
  7627. st->state = END;
  7628. return SCRIPT_CMD_FAILURE;
  7629. }
  7630. if (pos > 0 && pos <= ARRAYLENGTH(equip))
  7631. i = pc_checkequip(sd,equip[pos-1]);
  7632. if (i >= 0) {
  7633. pc_unequipitem(sd,i,3); //recalculate bonus
  7634. ret = !(pc_delitem(sd,i,1,0,2,LOG_TYPE_SCRIPT));
  7635. }
  7636. else {
  7637. ShowError("buildin_delequip: No item equipped at pos %d (CID=%d/AID=%d).\n", pos, sd->status.char_id, sd->status.account_id);
  7638. st->state = END;
  7639. return SCRIPT_CMD_FAILURE;
  7640. }
  7641. script_pushint(st,ret);
  7642. return SCRIPT_CMD_SUCCESS;
  7643. }
  7644. /**
  7645. * Break the item equipped at pos.
  7646. * breakequip <equipment slot>{,<char_id>};
  7647. **/
  7648. BUILDIN_FUNC(breakequip) {
  7649. short i = -1;
  7650. int pos;
  7651. TBL_PC *sd;
  7652. pos = script_getnum(st,2);
  7653. if (!script_charid2sd(3,sd))
  7654. return SCRIPT_CMD_FAILURE;
  7655. if (pos > 0 && pos <= ARRAYLENGTH(equip))
  7656. i = pc_checkequip(sd,equip[pos-1]);
  7657. if (i >= 0) {
  7658. sd->inventory.u.items_inventory[i].attribute = 1;
  7659. pc_unequipitem(sd,i,3);
  7660. clif_equiplist(sd);
  7661. script_pushint(st,1);
  7662. return SCRIPT_CMD_SUCCESS;
  7663. }
  7664. ShowError("buildin_breakequip: No item equipped at pos %d (CID=%d/AID=%d).\n", pos, sd->status.char_id, sd->status.account_id);
  7665. script_pushint(st,0);
  7666. return SCRIPT_CMD_FAILURE;
  7667. }
  7668. /**
  7669. * statusup <stat>{,<char_id>};
  7670. **/
  7671. BUILDIN_FUNC(statusup)
  7672. {
  7673. int type;
  7674. TBL_PC *sd;
  7675. type = script_getnum(st,2);
  7676. if (!script_charid2sd(3, sd))
  7677. return SCRIPT_CMD_FAILURE;
  7678. pc_statusup(sd, type, 1);
  7679. return SCRIPT_CMD_SUCCESS;
  7680. }
  7681. /**
  7682. * statusup2 <stat>,<amount>{,<char_id>};
  7683. **/
  7684. BUILDIN_FUNC(statusup2)
  7685. {
  7686. int type,val;
  7687. TBL_PC *sd;
  7688. type=script_getnum(st,2);
  7689. val=script_getnum(st,3);
  7690. if (!script_charid2sd(4, sd))
  7691. return SCRIPT_CMD_FAILURE;
  7692. pc_statusup2(sd,type,val);
  7693. return SCRIPT_CMD_SUCCESS;
  7694. }
  7695. /// See 'doc/item_bonus.txt'
  7696. ///
  7697. /// bonus <bonus type>,<val1>;
  7698. /// bonus2 <bonus type>,<val1>,<val2>;
  7699. /// bonus3 <bonus type>,<val1>,<val2>,<val3>;
  7700. /// bonus4 <bonus type>,<val1>,<val2>,<val3>,<val4>;
  7701. /// bonus5 <bonus type>,<val1>,<val2>,<val3>,<val4>,<val5>;
  7702. BUILDIN_FUNC(bonus)
  7703. {
  7704. int type;
  7705. int val1;
  7706. int val2 = 0;
  7707. int val3 = 0;
  7708. int val4 = 0;
  7709. int val5 = 0;
  7710. TBL_PC* sd;
  7711. struct script_data *data;
  7712. sd = script_rid2sd(st);
  7713. if( sd == NULL )
  7714. return SCRIPT_CMD_SUCCESS; // no player attached
  7715. type = script_getnum(st,2);
  7716. switch( type ) {
  7717. case SP_AUTOSPELL:
  7718. case SP_AUTOSPELL_WHENHIT:
  7719. case SP_AUTOSPELL_ONSKILL:
  7720. case SP_SKILL_ATK:
  7721. case SP_SKILL_HEAL:
  7722. case SP_SKILL_HEAL2:
  7723. case SP_ADD_SKILL_BLOW:
  7724. case SP_CASTRATE:
  7725. case SP_ADDEFF_ONSKILL:
  7726. case SP_SKILL_USE_SP_RATE:
  7727. case SP_SKILL_COOLDOWN:
  7728. case SP_SKILL_FIXEDCAST:
  7729. case SP_SKILL_VARIABLECAST:
  7730. case SP_VARCASTRATE:
  7731. case SP_FIXCASTRATE:
  7732. case SP_SKILL_USE_SP:
  7733. case SP_SUB_SKILL:
  7734. // these bonuses support skill names
  7735. data = script_getdata(st, 3);
  7736. get_val(st, data); // Convert into value in case of a variable
  7737. val1 = ( data_isstring(data) ? skill_name2id(script_getstr(st,3)) : script_getnum(st,3) );
  7738. break;
  7739. default:
  7740. val1 = script_getnum(st,3);
  7741. break;
  7742. }
  7743. switch( script_lastdata(st)-2 ) {
  7744. case 1:
  7745. pc_bonus(sd, type, val1);
  7746. break;
  7747. case 2:
  7748. val2 = script_getnum(st,4);
  7749. pc_bonus2(sd, type, val1, val2);
  7750. break;
  7751. case 3:
  7752. val2 = script_getnum(st,4);
  7753. val3 = script_getnum(st,5);
  7754. pc_bonus3(sd, type, val1, val2, val3);
  7755. break;
  7756. case 4:
  7757. data = script_getdata(st, 4);
  7758. get_val(st, data); // Convert into value in case of a variable
  7759. if( type == SP_AUTOSPELL_ONSKILL && data_isstring(data) )
  7760. val2 = skill_name2id(script_getstr(st,4)); // 2nd value can be skill name
  7761. else
  7762. val2 = script_getnum(st,4);
  7763. val3 = script_getnum(st,5);
  7764. val4 = script_getnum(st,6);
  7765. pc_bonus4(sd, type, val1, val2, val3, val4);
  7766. break;
  7767. case 5:
  7768. data = script_getdata(st, 4);
  7769. get_val(st, data); // Convert into value in case of a variable
  7770. if( type == SP_AUTOSPELL_ONSKILL && data_isstring(data) )
  7771. val2 = skill_name2id(script_getstr(st,4)); // 2nd value can be skill name
  7772. else
  7773. val2 = script_getnum(st,4);
  7774. val3 = script_getnum(st,5);
  7775. val4 = script_getnum(st,6);
  7776. val5 = script_getnum(st,7);
  7777. pc_bonus5(sd, type, val1, val2, val3, val4, val5);
  7778. break;
  7779. default:
  7780. ShowDebug("buildin_bonus: unexpected number of arguments (%d)\n", (script_lastdata(st) - 1));
  7781. break;
  7782. }
  7783. return SCRIPT_CMD_SUCCESS;
  7784. }
  7785. BUILDIN_FUNC(autobonus)
  7786. {
  7787. unsigned int dur, pos;
  7788. short rate;
  7789. short atk_type = 0;
  7790. TBL_PC* sd;
  7791. const char *bonus_script, *other_script = NULL;
  7792. sd = script_rid2sd(st);
  7793. if( sd == NULL )
  7794. return SCRIPT_CMD_SUCCESS; // no player attached
  7795. if (current_equip_combo_pos)
  7796. pos = current_equip_combo_pos;
  7797. else
  7798. pos = sd->inventory.u.items_inventory[current_equip_item_index].equip;
  7799. if((sd->state.autobonus&pos) == pos)
  7800. return SCRIPT_CMD_SUCCESS;
  7801. rate = script_getnum(st,3);
  7802. dur = script_getnum(st,4);
  7803. bonus_script = script_getstr(st,2);
  7804. if( !rate || !dur || !pos || !bonus_script )
  7805. return SCRIPT_CMD_SUCCESS;
  7806. if( script_hasdata(st,5) )
  7807. atk_type = script_getnum(st,5);
  7808. if( script_hasdata(st,6) )
  7809. other_script = script_getstr(st,6);
  7810. if( pc_addautobonus(sd->autobonus,ARRAYLENGTH(sd->autobonus),
  7811. bonus_script,rate,dur,atk_type,other_script,pos,false) )
  7812. {
  7813. script_add_autobonus(bonus_script);
  7814. if( other_script )
  7815. script_add_autobonus(other_script);
  7816. }
  7817. return SCRIPT_CMD_SUCCESS;
  7818. }
  7819. BUILDIN_FUNC(autobonus2)
  7820. {
  7821. unsigned int dur, pos;
  7822. short rate;
  7823. short atk_type = 0;
  7824. TBL_PC* sd;
  7825. const char *bonus_script, *other_script = NULL;
  7826. sd = script_rid2sd(st);
  7827. if( sd == NULL )
  7828. return SCRIPT_CMD_SUCCESS; // no player attached
  7829. if (current_equip_combo_pos)
  7830. pos = current_equip_combo_pos;
  7831. else
  7832. pos = sd->inventory.u.items_inventory[current_equip_item_index].equip;
  7833. if((sd->state.autobonus&pos) == pos)
  7834. return SCRIPT_CMD_SUCCESS;
  7835. rate = script_getnum(st,3);
  7836. dur = script_getnum(st,4);
  7837. bonus_script = script_getstr(st,2);
  7838. if( !rate || !dur || !pos || !bonus_script )
  7839. return SCRIPT_CMD_SUCCESS;
  7840. if( script_hasdata(st,5) )
  7841. atk_type = script_getnum(st,5);
  7842. if( script_hasdata(st,6) )
  7843. other_script = script_getstr(st,6);
  7844. if( pc_addautobonus(sd->autobonus2,ARRAYLENGTH(sd->autobonus2),
  7845. bonus_script,rate,dur,atk_type,other_script,pos,false) )
  7846. {
  7847. script_add_autobonus(bonus_script);
  7848. if( other_script )
  7849. script_add_autobonus(other_script);
  7850. }
  7851. return SCRIPT_CMD_SUCCESS;
  7852. }
  7853. BUILDIN_FUNC(autobonus3)
  7854. {
  7855. unsigned int dur, pos;
  7856. short rate,atk_type;
  7857. TBL_PC* sd;
  7858. const char *bonus_script, *other_script = NULL;
  7859. struct script_data *data;
  7860. sd = script_rid2sd(st);
  7861. if( sd == NULL )
  7862. return SCRIPT_CMD_SUCCESS; // no player attached
  7863. if (current_equip_combo_pos)
  7864. pos = current_equip_combo_pos;
  7865. else
  7866. pos = sd->inventory.u.items_inventory[current_equip_item_index].equip;
  7867. if((sd->state.autobonus&pos) == pos)
  7868. return SCRIPT_CMD_SUCCESS;
  7869. rate = script_getnum(st,3);
  7870. dur = script_getnum(st,4);
  7871. data = script_getdata(st, 5);
  7872. get_val(st, data); // Convert into value in case of a variable
  7873. atk_type = ( data_isstring(data) ? skill_name2id(script_getstr(st,5)) : script_getnum(st,5) );
  7874. bonus_script = script_getstr(st,2);
  7875. if( !rate || !dur || !pos || !atk_type || !bonus_script )
  7876. return SCRIPT_CMD_SUCCESS;
  7877. if( script_hasdata(st,6) )
  7878. other_script = script_getstr(st,6);
  7879. if( pc_addautobonus(sd->autobonus3,ARRAYLENGTH(sd->autobonus3),
  7880. bonus_script,rate,dur,atk_type,other_script,pos,true) )
  7881. {
  7882. script_add_autobonus(bonus_script);
  7883. if( other_script )
  7884. script_add_autobonus(other_script);
  7885. }
  7886. return SCRIPT_CMD_SUCCESS;
  7887. }
  7888. /// Changes the level of a player skill.
  7889. /// <flag> defaults to 1
  7890. /// <flag>=0 : set the level of the skill
  7891. /// <flag>=1 : set the temporary level of the skill
  7892. /// <flag>=2 : add to the level of the skill
  7893. ///
  7894. /// skill <skill id>,<level>,<flag>
  7895. /// skill <skill id>,<level>
  7896. /// skill "<skill name>",<level>,<flag>
  7897. /// skill "<skill name>",<level>
  7898. BUILDIN_FUNC(skill)
  7899. {
  7900. int id;
  7901. int level;
  7902. int flag = ADDSKILL_TEMP;
  7903. TBL_PC* sd;
  7904. struct script_data *data;
  7905. const char* command = script_getfuncname(st);
  7906. sd = script_rid2sd(st);
  7907. if( sd == NULL )
  7908. return SCRIPT_CMD_SUCCESS;// no player attached, report source
  7909. if (strcmpi(command, "addtoskill") == 0)
  7910. flag = ADDSKILL_TEMP_ADDLEVEL;
  7911. data = script_getdata(st, 2);
  7912. get_val(st, data); // Convert into value in case of a variable
  7913. id = ( data_isstring(data) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  7914. level = script_getnum(st,3);
  7915. if( script_hasdata(st,4) )
  7916. flag = script_getnum(st,4);
  7917. pc_skill(sd, id, level, (enum e_addskill_type)flag);
  7918. return SCRIPT_CMD_SUCCESS;
  7919. }
  7920. /// Increases the level of a guild skill.
  7921. ///
  7922. /// guildskill <skill id>,<amount>;
  7923. /// guildskill "<skill name>",<amount>;
  7924. BUILDIN_FUNC(guildskill)
  7925. {
  7926. int id;
  7927. int level;
  7928. TBL_PC* sd;
  7929. int i;
  7930. struct script_data *data;
  7931. sd = script_rid2sd(st);
  7932. if( sd == NULL )
  7933. return SCRIPT_CMD_SUCCESS;// no player attached, report source
  7934. data = script_getdata(st, 2);
  7935. get_val(st, data); // Convert into value in case of a variable
  7936. id = ( data_isstring(data) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  7937. level = script_getnum(st,3);
  7938. for( i=0; i < level; i++ )
  7939. guild_skillup(sd, id);
  7940. return SCRIPT_CMD_SUCCESS;
  7941. }
  7942. /// Returns the level of the player skill.
  7943. ///
  7944. /// getskilllv(<skill id>) -> <level>
  7945. /// getskilllv("<skill name>") -> <level>
  7946. BUILDIN_FUNC(getskilllv)
  7947. {
  7948. int id;
  7949. TBL_PC* sd;
  7950. struct script_data *data;
  7951. sd = script_rid2sd(st);
  7952. if( sd == NULL )
  7953. return SCRIPT_CMD_SUCCESS;// no player attached, report source
  7954. data = script_getdata(st, 2);
  7955. get_val(st, data); // Convert into value in case of a variable
  7956. id = ( data_isstring(data) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  7957. script_pushint(st, pc_checkskill(sd,id));
  7958. return SCRIPT_CMD_SUCCESS;
  7959. }
  7960. /// Returns the level of the guild skill.
  7961. ///
  7962. /// getgdskilllv(<guild id>,<skill id>) -> <level>
  7963. /// getgdskilllv(<guild id>,"<skill name>") -> <level>
  7964. BUILDIN_FUNC(getgdskilllv)
  7965. {
  7966. int guild_id;
  7967. uint16 skill_id;
  7968. struct guild* g;
  7969. struct script_data *data;
  7970. guild_id = script_getnum(st,2);
  7971. data = script_getdata(st, 3);
  7972. get_val(st, data); // Convert into value in case of a variable
  7973. skill_id = ( data_isstring(data) ? skill_name2id(script_getstr(st,3)) : script_getnum(st,3) );
  7974. g = guild_search(guild_id);
  7975. if( g == NULL )
  7976. script_pushint(st, -1);
  7977. else
  7978. script_pushint(st, guild_checkskill(g,skill_id));
  7979. return SCRIPT_CMD_SUCCESS;
  7980. }
  7981. /// Returns the 'basic_skill_check' setting.
  7982. /// This config determines if the server checks the skill level of NV_BASIC
  7983. /// before allowing the basic actions.
  7984. ///
  7985. /// basicskillcheck() -> <bool>
  7986. BUILDIN_FUNC(basicskillcheck)
  7987. {
  7988. script_pushint(st, battle_config.basic_skill_check);
  7989. return SCRIPT_CMD_SUCCESS;
  7990. }
  7991. /// Returns the GM level of the player.
  7992. ///
  7993. /// getgmlevel({<char_id>}) -> <level>
  7994. BUILDIN_FUNC(getgmlevel)
  7995. {
  7996. TBL_PC* sd;
  7997. if (!script_charid2sd(2,sd))
  7998. return SCRIPT_CMD_FAILURE;
  7999. script_pushint(st, pc_get_group_level(sd));
  8000. return SCRIPT_CMD_SUCCESS;
  8001. }
  8002. /// Returns the group ID of the player.
  8003. ///
  8004. /// getgroupid({<char_id>}) -> <int>
  8005. BUILDIN_FUNC(getgroupid)
  8006. {
  8007. TBL_PC* sd;
  8008. if (!script_charid2sd(2,sd))
  8009. return SCRIPT_CMD_FAILURE;
  8010. script_pushint(st, pc_get_group_id(sd));
  8011. return SCRIPT_CMD_SUCCESS;
  8012. }
  8013. /// Terminates the execution of this script instance.
  8014. ///
  8015. /// end
  8016. BUILDIN_FUNC(end)
  8017. {
  8018. TBL_PC* sd;
  8019. sd = map_id2sd(st->rid);
  8020. st->state = END;
  8021. if (st->stack->defsp >= 1 && st->stack->stack_data[st->stack->defsp-1].type == C_RETINFO) {
  8022. int i;
  8023. for(i = 0; i < st->stack->sp; i++) {
  8024. if (st->stack->stack_data[i].type == C_RETINFO) { // Grab the first, aka the original
  8025. struct script_retinfo *ri = st->stack->stack_data[i].u.ri;
  8026. st->script = ri->script;
  8027. break;
  8028. }
  8029. }
  8030. }
  8031. if( st->mes_active )
  8032. st->mes_active = 0;
  8033. if (sd){
  8034. if (sd->state.callshop == 0)
  8035. clif_scriptclose(sd, st->oid); // If a menu/select/prompt is active, close it.
  8036. else
  8037. sd->state.callshop = 0;
  8038. }
  8039. return SCRIPT_CMD_SUCCESS;
  8040. }
  8041. /// Checks if the player has that effect state (option).
  8042. ///
  8043. /// checkoption(<option>{,<char_id>}) -> <bool>
  8044. BUILDIN_FUNC(checkoption)
  8045. {
  8046. int option;
  8047. TBL_PC* sd;
  8048. if (!script_charid2sd(3,sd))
  8049. return SCRIPT_CMD_FAILURE;
  8050. option = script_getnum(st,2);
  8051. if( sd->sc.option&option )
  8052. script_pushint(st, 1);
  8053. else
  8054. script_pushint(st, 0);
  8055. return SCRIPT_CMD_SUCCESS;
  8056. }
  8057. /// Checks if the player is in that body state (opt1).
  8058. ///
  8059. /// checkoption1(<opt1>{,<char_id>}) -> <bool>
  8060. BUILDIN_FUNC(checkoption1)
  8061. {
  8062. int opt1;
  8063. TBL_PC* sd;
  8064. if (!script_charid2sd(3,sd))
  8065. return SCRIPT_CMD_FAILURE;
  8066. opt1 = script_getnum(st,2);
  8067. if( sd->sc.opt1 == opt1 )
  8068. script_pushint(st, 1);
  8069. else
  8070. script_pushint(st, 0);
  8071. return SCRIPT_CMD_SUCCESS;
  8072. }
  8073. /// Checks if the player has that health state (opt2).
  8074. ///
  8075. /// checkoption2(<opt2>{,<char_id>}) -> <bool>
  8076. BUILDIN_FUNC(checkoption2)
  8077. {
  8078. int opt2;
  8079. TBL_PC* sd;
  8080. if (!script_charid2sd(3,sd))
  8081. return SCRIPT_CMD_FAILURE;
  8082. opt2 = script_getnum(st,2);
  8083. if( sd->sc.opt2&opt2 )
  8084. script_pushint(st, 1);
  8085. else
  8086. script_pushint(st, 0);
  8087. return SCRIPT_CMD_SUCCESS;
  8088. }
  8089. /// Changes the effect state (option) of the player.
  8090. /// <flag> defaults to 1
  8091. /// <flag>=0 : removes the option
  8092. /// <flag>=other : adds the option
  8093. ///
  8094. /// setoption <option>{,<flag>{,<char_id>}};
  8095. BUILDIN_FUNC(setoption)
  8096. {
  8097. int option;
  8098. int flag = 1;
  8099. TBL_PC* sd;
  8100. if (!script_charid2sd(4,sd))
  8101. return SCRIPT_CMD_FAILURE;
  8102. option = script_getnum(st,2);
  8103. if( script_hasdata(st,3) )
  8104. flag = script_getnum(st,3);
  8105. else if( !option ){// Request to remove everything.
  8106. flag = 0;
  8107. option = OPTION_FALCON|OPTION_RIDING;
  8108. #ifndef NEW_CARTS
  8109. option |= OPTION_CART;
  8110. #endif
  8111. }
  8112. if( flag ){// Add option
  8113. if( option&OPTION_WEDDING && !battle_config.wedding_modifydisplay )
  8114. option &= ~OPTION_WEDDING;// Do not show the wedding sprites
  8115. pc_setoption(sd, sd->sc.option|option);
  8116. } else// Remove option
  8117. pc_setoption(sd, sd->sc.option&~option);
  8118. return SCRIPT_CMD_SUCCESS;
  8119. }
  8120. /// Returns if the player has a cart.
  8121. ///
  8122. /// checkcart({char_id}) -> <bool>
  8123. ///
  8124. /// @author Valaris
  8125. BUILDIN_FUNC(checkcart)
  8126. {
  8127. TBL_PC* sd;
  8128. if (!script_charid2sd(2,sd))
  8129. return SCRIPT_CMD_FAILURE;
  8130. if( pc_iscarton(sd) )
  8131. script_pushint(st, 1);
  8132. else
  8133. script_pushint(st, 0);
  8134. return SCRIPT_CMD_SUCCESS;
  8135. }
  8136. /// Sets the cart of the player.
  8137. /// <type> defaults to 1
  8138. /// <type>=0 : removes the cart
  8139. /// <type>=1 : Normal cart
  8140. /// <type>=2 : Wooden cart
  8141. /// <type>=3 : Covered cart with flowers and ferns
  8142. /// <type>=4 : Wooden cart with a Panda doll on the back
  8143. /// <type>=5 : Normal cart with bigger wheels, a roof and a banner on the back
  8144. ///
  8145. /// setcart {<type>{,<char_id>}};
  8146. BUILDIN_FUNC(setcart)
  8147. {
  8148. int type = 1;
  8149. TBL_PC* sd;
  8150. if (!script_charid2sd(3,sd))
  8151. return SCRIPT_CMD_FAILURE;
  8152. if( script_hasdata(st,2) )
  8153. type = script_getnum(st,2);
  8154. pc_setcart(sd, type);
  8155. return SCRIPT_CMD_SUCCESS;
  8156. }
  8157. /// Returns if the player has a falcon.
  8158. ///
  8159. /// checkfalcon({char_id}) -> <bool>
  8160. ///
  8161. /// @author Valaris
  8162. BUILDIN_FUNC(checkfalcon)
  8163. {
  8164. TBL_PC* sd;
  8165. if (!script_charid2sd(2,sd))
  8166. return SCRIPT_CMD_FAILURE;
  8167. if( pc_isfalcon(sd) )
  8168. script_pushint(st, 1);
  8169. else
  8170. script_pushint(st, 0);
  8171. return SCRIPT_CMD_SUCCESS;
  8172. }
  8173. /// Sets if the player has a falcon or not.
  8174. /// <flag> defaults to 1
  8175. ///
  8176. /// setfalcon {<flag>{,<char_id>}};
  8177. BUILDIN_FUNC(setfalcon)
  8178. {
  8179. int flag = 1;
  8180. TBL_PC* sd;
  8181. if (!script_charid2sd(3,sd))
  8182. return SCRIPT_CMD_FAILURE;
  8183. if( script_hasdata(st,2) )
  8184. flag = script_getnum(st,2);
  8185. pc_setfalcon(sd, flag);
  8186. return SCRIPT_CMD_SUCCESS;
  8187. }
  8188. /// Returns if the player is riding.
  8189. ///
  8190. /// checkriding({char_id}) -> <bool>
  8191. ///
  8192. /// @author Valaris
  8193. BUILDIN_FUNC(checkriding)
  8194. {
  8195. TBL_PC* sd;
  8196. if (!script_charid2sd(2,sd))
  8197. return SCRIPT_CMD_FAILURE;
  8198. if( pc_isriding(sd) )
  8199. script_pushint(st, 1);
  8200. else
  8201. script_pushint(st, 0);
  8202. return SCRIPT_CMD_SUCCESS;
  8203. }
  8204. /// Sets if the player is riding.
  8205. /// <flag> defaults to 1
  8206. ///
  8207. /// setriding {<flag>{,<char_id>}};
  8208. BUILDIN_FUNC(setriding)
  8209. {
  8210. int flag = 1;
  8211. TBL_PC* sd;
  8212. if (!script_charid2sd(3,sd))
  8213. return SCRIPT_CMD_FAILURE;
  8214. if( script_hasdata(st,2) )
  8215. flag = script_getnum(st,2);
  8216. pc_setriding(sd, flag);
  8217. return SCRIPT_CMD_SUCCESS;
  8218. }
  8219. /// Returns if the player has a warg.
  8220. ///
  8221. /// checkwug({char_id}) -> <bool>
  8222. ///
  8223. BUILDIN_FUNC(checkwug)
  8224. {
  8225. TBL_PC* sd;
  8226. if (!script_charid2sd(2,sd))
  8227. return SCRIPT_CMD_FAILURE;
  8228. if( pc_iswug(sd) || pc_isridingwug(sd) )
  8229. script_pushint(st, 1);
  8230. else
  8231. script_pushint(st, 0);
  8232. return SCRIPT_CMD_SUCCESS;
  8233. }
  8234. /// Returns if the player is wearing MADO Gear.
  8235. ///
  8236. /// checkmadogear({<char_id>}) -> <bool>
  8237. ///
  8238. BUILDIN_FUNC(checkmadogear)
  8239. {
  8240. TBL_PC* sd;
  8241. if (!script_charid2sd(2,sd))
  8242. return SCRIPT_CMD_FAILURE;
  8243. if( pc_ismadogear(sd) )
  8244. script_pushint(st, 1);
  8245. else
  8246. script_pushint(st, 0);
  8247. return SCRIPT_CMD_SUCCESS;
  8248. }
  8249. /// Sets if the player is riding MADO Gear.
  8250. /// <flag> defaults to 1
  8251. ///
  8252. /// setmadogear {<flag>{,<char_id>}};
  8253. BUILDIN_FUNC(setmadogear)
  8254. {
  8255. int flag = 1;
  8256. TBL_PC* sd;
  8257. if (!script_charid2sd(3,sd))
  8258. return SCRIPT_CMD_FAILURE;
  8259. if( script_hasdata(st,2) )
  8260. flag = script_getnum(st,2);
  8261. pc_setmadogear(sd, flag);
  8262. return SCRIPT_CMD_SUCCESS;
  8263. }
  8264. /// Sets the save point of the player.
  8265. ///
  8266. /// save "<map name>",<x>,<y>{,<char_id>}
  8267. /// savepoint "<map name>",<x>,<y>{,<char_id>}
  8268. BUILDIN_FUNC(savepoint)
  8269. {
  8270. int x;
  8271. int y;
  8272. short map_idx;
  8273. const char* str;
  8274. TBL_PC* sd;
  8275. if (!script_charid2sd(5,sd))
  8276. return SCRIPT_CMD_FAILURE;// no player attached, report source
  8277. str = script_getstr(st, 2);
  8278. x = script_getnum(st,3);
  8279. y = script_getnum(st,4);
  8280. map_idx = mapindex_name2id(str);
  8281. if( map_idx )
  8282. pc_setsavepoint(sd, map_idx, x, y);
  8283. return SCRIPT_CMD_SUCCESS;
  8284. }
  8285. /*==========================================
  8286. * GetTimeTick(0: System Tick, 1: Time Second Tick)
  8287. *------------------------------------------*/
  8288. BUILDIN_FUNC(gettimetick) /* Asgard Version */
  8289. {
  8290. int type;
  8291. time_t timer;
  8292. struct tm *t;
  8293. type=script_getnum(st,2);
  8294. switch(type){
  8295. case 2:
  8296. //type 2:(Get the number of seconds elapsed since 00:00 hours, Jan 1, 1970 UTC
  8297. // from the system clock.)
  8298. script_pushint(st,(int)time(NULL));
  8299. break;
  8300. case 1:
  8301. //type 1:(Second Ticks: 0-86399, 00:00:00-23:59:59)
  8302. time(&timer);
  8303. t=localtime(&timer);
  8304. script_pushint(st,((t->tm_hour)*3600+(t->tm_min)*60+t->tm_sec));
  8305. break;
  8306. case 0:
  8307. default:
  8308. //type 0:(System Ticks)
  8309. script_pushint(st,gettick());
  8310. break;
  8311. }
  8312. return SCRIPT_CMD_SUCCESS;
  8313. }
  8314. /*==========================================
  8315. * GetTime(Type);
  8316. * 1: Sec 2: Min 3: Hour
  8317. * 4: WeekDay 5: MonthDay 6: Month
  8318. * 7: Year
  8319. *------------------------------------------*/
  8320. BUILDIN_FUNC(gettime) /* Asgard Version */
  8321. {
  8322. int type;
  8323. time_t timer;
  8324. struct tm *t;
  8325. type=script_getnum(st,2);
  8326. time(&timer);
  8327. t=localtime(&timer);
  8328. switch(type){
  8329. case 1://Sec(0~59)
  8330. script_pushint(st,t->tm_sec);
  8331. break;
  8332. case 2://Min(0~59)
  8333. script_pushint(st,t->tm_min);
  8334. break;
  8335. case 3://Hour(0~23)
  8336. script_pushint(st,t->tm_hour);
  8337. break;
  8338. case 4://WeekDay(0~6)
  8339. script_pushint(st,t->tm_wday);
  8340. break;
  8341. case 5://MonthDay(01~31)
  8342. script_pushint(st,t->tm_mday);
  8343. break;
  8344. case 6://Month(01~12)
  8345. script_pushint(st,t->tm_mon+1);
  8346. break;
  8347. case 7://Year(20xx)
  8348. script_pushint(st,t->tm_year+1900);
  8349. break;
  8350. case 8://Year Day(01~366)
  8351. script_pushint(st,t->tm_yday+1);
  8352. break;
  8353. default://(format error)
  8354. script_pushint(st,-1);
  8355. break;
  8356. }
  8357. return SCRIPT_CMD_SUCCESS;
  8358. }
  8359. /*==========================================
  8360. * GetTimeStr("TimeFMT", Length);
  8361. *------------------------------------------*/
  8362. BUILDIN_FUNC(gettimestr)
  8363. {
  8364. char *tmpstr;
  8365. const char *fmtstr;
  8366. int maxlen;
  8367. time_t now = time(NULL);
  8368. fmtstr=script_getstr(st,2);
  8369. maxlen=script_getnum(st,3);
  8370. tmpstr=(char *)aMalloc((maxlen+1)*sizeof(char));
  8371. strftime(tmpstr,maxlen,fmtstr,localtime(&now));
  8372. tmpstr[maxlen]='\0';
  8373. script_pushstr(st,tmpstr);
  8374. return SCRIPT_CMD_SUCCESS;
  8375. }
  8376. /*==========================================
  8377. * Open player storage
  8378. *------------------------------------------*/
  8379. BUILDIN_FUNC(openstorage)
  8380. {
  8381. TBL_PC* sd;
  8382. sd = script_rid2sd(st);
  8383. if( sd == NULL )
  8384. return SCRIPT_CMD_SUCCESS;
  8385. storage_storageopen(sd);
  8386. return SCRIPT_CMD_SUCCESS;
  8387. }
  8388. BUILDIN_FUNC(guildopenstorage)
  8389. {
  8390. TBL_PC* sd;
  8391. int ret;
  8392. sd = script_rid2sd(st);
  8393. if( sd == NULL )
  8394. return SCRIPT_CMD_SUCCESS;
  8395. ret = storage_guild_storageopen(sd);
  8396. script_pushint(st,ret);
  8397. return SCRIPT_CMD_SUCCESS;
  8398. }
  8399. /*==========================================
  8400. * Make player use a skill trought item usage
  8401. *------------------------------------------*/
  8402. /// itemskill <skill id>,<level>
  8403. /// itemskill "<skill name>",<level>
  8404. BUILDIN_FUNC(itemskill)
  8405. {
  8406. int id;
  8407. int lv;
  8408. TBL_PC* sd;
  8409. struct script_data *data;
  8410. sd = script_rid2sd(st);
  8411. if( sd == NULL || sd->ud.skilltimer != INVALID_TIMER )
  8412. return SCRIPT_CMD_SUCCESS;
  8413. data = script_getdata(st, 2);
  8414. get_val(st, data); // Convert into value in case of a variable
  8415. id = ( data_isstring(data) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  8416. lv = script_getnum(st,3);
  8417. sd->skillitem=id;
  8418. sd->skillitemlv=lv;
  8419. clif_item_skill(sd,id,lv);
  8420. return SCRIPT_CMD_SUCCESS;
  8421. }
  8422. /*==========================================
  8423. * Attempt to create an item
  8424. *------------------------------------------*/
  8425. BUILDIN_FUNC(produce)
  8426. {
  8427. int trigger;
  8428. TBL_PC* sd;
  8429. sd = script_rid2sd(st);
  8430. if( sd == NULL )
  8431. return SCRIPT_CMD_SUCCESS;
  8432. trigger=script_getnum(st,2);
  8433. clif_skill_produce_mix_list(sd, -1, trigger);
  8434. return SCRIPT_CMD_SUCCESS;
  8435. }
  8436. /*==========================================
  8437. *
  8438. *------------------------------------------*/
  8439. BUILDIN_FUNC(cooking)
  8440. {
  8441. int trigger;
  8442. TBL_PC* sd;
  8443. sd = script_rid2sd(st);
  8444. if( sd == NULL )
  8445. return SCRIPT_CMD_SUCCESS;
  8446. trigger=script_getnum(st,2);
  8447. clif_cooking_list(sd, trigger, AM_PHARMACY, 1, 1);
  8448. return SCRIPT_CMD_SUCCESS;
  8449. }
  8450. /*==========================================
  8451. * Create a pet
  8452. *------------------------------------------*/
  8453. BUILDIN_FUNC(makepet)
  8454. {
  8455. TBL_PC* sd;
  8456. int id,pet_id;
  8457. id=script_getnum(st,2);
  8458. sd = script_rid2sd(st);
  8459. if( sd == NULL )
  8460. return SCRIPT_CMD_SUCCESS;
  8461. pet_id = search_petDB_index(id, PET_CLASS);
  8462. if (pet_id < 0)
  8463. pet_id = search_petDB_index(id, PET_EGG);
  8464. if (pet_id >= 0 && sd) {
  8465. sd->catch_target_class = pet_db[pet_id].class_;
  8466. intif_create_pet(
  8467. sd->status.account_id, sd->status.char_id,
  8468. (short)pet_db[pet_id].class_, (short)mob_db(pet_db[pet_id].class_)->lv,
  8469. (short)pet_db[pet_id].EggID, 0, (short)pet_db[pet_id].intimate,
  8470. 100, 0, 1, pet_db[pet_id].jname);
  8471. }
  8472. return SCRIPT_CMD_SUCCESS;
  8473. }
  8474. /**
  8475. * Give player exp base,job * quest_exp_rate/100
  8476. * getexp <base xp>,<job xp>{,<char_id>};
  8477. **/
  8478. BUILDIN_FUNC(getexp)
  8479. {
  8480. TBL_PC* sd;
  8481. int base=0,job=0;
  8482. double bonus;
  8483. if (!script_charid2sd(4,sd))
  8484. return SCRIPT_CMD_FAILURE;
  8485. base=script_getnum(st,2);
  8486. job =script_getnum(st,3);
  8487. if(base<0 || job<0)
  8488. return SCRIPT_CMD_SUCCESS;
  8489. // bonus for npc-given exp
  8490. bonus = battle_config.quest_exp_rate / 100.;
  8491. if (base)
  8492. base = (int) cap_value(base * bonus, 0, INT_MAX);
  8493. if (job)
  8494. job = (int) cap_value(job * bonus, 0, INT_MAX);
  8495. pc_gainexp(sd, NULL, base, job, 1);
  8496. return SCRIPT_CMD_SUCCESS;
  8497. }
  8498. /*==========================================
  8499. * Gain guild exp [Celest]
  8500. *------------------------------------------*/
  8501. BUILDIN_FUNC(guildgetexp)
  8502. {
  8503. TBL_PC* sd;
  8504. int exp;
  8505. sd = script_rid2sd(st);
  8506. if( sd == NULL )
  8507. return SCRIPT_CMD_SUCCESS;
  8508. exp = script_getnum(st,2);
  8509. if(exp < 0)
  8510. return SCRIPT_CMD_SUCCESS;
  8511. if(sd && sd->status.guild_id > 0)
  8512. guild_getexp (sd, exp);
  8513. return SCRIPT_CMD_SUCCESS;
  8514. }
  8515. /*==========================================
  8516. * Changes the guild master of a guild [Skotlex]
  8517. *------------------------------------------*/
  8518. BUILDIN_FUNC(guildchangegm)
  8519. {
  8520. TBL_PC *sd;
  8521. int guild_id;
  8522. const char *name;
  8523. guild_id = script_getnum(st,2);
  8524. name = script_getstr(st,3);
  8525. sd=map_nick2sd(name);
  8526. if (!sd)
  8527. script_pushint(st,0);
  8528. else
  8529. script_pushint(st,guild_gm_change(guild_id, sd));
  8530. return SCRIPT_CMD_SUCCESS;
  8531. }
  8532. /*==========================================
  8533. * Spawn a monster:
  8534. * *monster "<map name>",<x>,<y>,"<name to show>",<mob id>,<amount>{,"<event label>",<size>,<ai>};
  8535. *------------------------------------------*/
  8536. BUILDIN_FUNC(monster)
  8537. {
  8538. const char* mapn = script_getstr(st,2);
  8539. int x = script_getnum(st,3);
  8540. int y = script_getnum(st,4);
  8541. const char* str = script_getstr(st,5);
  8542. int class_ = script_getnum(st,6);
  8543. int amount = script_getnum(st,7);
  8544. const char* event = "";
  8545. unsigned int size = SZ_SMALL;
  8546. unsigned int ai = AI_NONE;
  8547. struct map_session_data* sd;
  8548. int16 m;
  8549. int i;
  8550. if (script_hasdata(st, 8)) {
  8551. event = script_getstr(st, 8);
  8552. check_event(st, event);
  8553. }
  8554. if (script_hasdata(st, 9)) {
  8555. size = script_getnum(st, 9);
  8556. if (size > 3) {
  8557. ShowWarning("buildin_monster: Attempted to spawn non-existing size %d for monster class %d\n", size, class_);
  8558. return SCRIPT_CMD_FAILURE;
  8559. }
  8560. }
  8561. if (script_hasdata(st, 10)) {
  8562. ai = script_getnum(st, 10);
  8563. if (ai >= AI_MAX) {
  8564. ShowWarning("buildin_monster: Attempted to spawn non-existing ai %d for monster class %d\n", ai, class_);
  8565. return SCRIPT_CMD_FAILURE;
  8566. }
  8567. }
  8568. if (class_ >= 0 && !mobdb_checkid(class_)) {
  8569. ShowWarning("buildin_monster: Attempted to spawn non-existing monster class %d\n", class_);
  8570. return SCRIPT_CMD_FAILURE;
  8571. }
  8572. sd = map_id2sd(st->rid);
  8573. if (sd && strcmp(mapn, "this") == 0)
  8574. m = sd->bl.m;
  8575. else
  8576. m = map_mapname2mapid(mapn);
  8577. for(i = 0; i < amount; i++) { //not optimised
  8578. int mobid = mob_once_spawn(sd, m, x, y, str, class_, 1, event, size, ai);
  8579. if (mobid)
  8580. mapreg_setreg(reference_uid(add_str("$@mobid"), i), mobid);
  8581. }
  8582. return SCRIPT_CMD_SUCCESS;
  8583. }
  8584. /*==========================================
  8585. * Request List of Monster Drops
  8586. *------------------------------------------*/
  8587. BUILDIN_FUNC(getmobdrops)
  8588. {
  8589. int class_ = script_getnum(st,2);
  8590. int i, j = 0;
  8591. struct mob_db *mob;
  8592. if( !mobdb_checkid(class_) )
  8593. {
  8594. script_pushint(st, 0);
  8595. return SCRIPT_CMD_SUCCESS;
  8596. }
  8597. mob = mob_db(class_);
  8598. for( i = 0; i < MAX_MOB_DROP; i++ )
  8599. {
  8600. if( mob->dropitem[i].nameid < 1 )
  8601. continue;
  8602. if( itemdb_exists(mob->dropitem[i].nameid) == NULL )
  8603. continue;
  8604. mapreg_setreg(reference_uid(add_str("$@MobDrop_item"), j), mob->dropitem[i].nameid);
  8605. mapreg_setreg(reference_uid(add_str("$@MobDrop_rate"), j), mob->dropitem[i].p);
  8606. j++;
  8607. }
  8608. mapreg_setreg(add_str("$@MobDrop_count"), j);
  8609. script_pushint(st, 1);
  8610. return SCRIPT_CMD_SUCCESS;
  8611. }
  8612. /*==========================================
  8613. * Spawn a monster in a random location
  8614. * in x0,x1,y0,y1 area.
  8615. *------------------------------------------*/
  8616. BUILDIN_FUNC(areamonster)
  8617. {
  8618. const char* mapn = script_getstr(st,2);
  8619. int x0 = script_getnum(st,3);
  8620. int y0 = script_getnum(st,4);
  8621. int x1 = script_getnum(st,5);
  8622. int y1 = script_getnum(st,6);
  8623. const char* str = script_getstr(st,7);
  8624. int class_ = script_getnum(st,8);
  8625. int amount = script_getnum(st,9);
  8626. const char* event = "";
  8627. unsigned int size = SZ_SMALL;
  8628. unsigned int ai = AI_NONE;
  8629. struct map_session_data* sd;
  8630. int16 m;
  8631. int i;
  8632. if (script_hasdata(st,10)) {
  8633. event = script_getstr(st, 10);
  8634. check_event(st, event);
  8635. }
  8636. if (script_hasdata(st, 11)) {
  8637. size = script_getnum(st, 11);
  8638. if (size > 3) {
  8639. ShowWarning("buildin_monster: Attempted to spawn non-existing size %d for monster class %d\n", size, class_);
  8640. return SCRIPT_CMD_FAILURE;
  8641. }
  8642. }
  8643. if (script_hasdata(st, 12)) {
  8644. ai = script_getnum(st, 12);
  8645. if (ai >= AI_MAX) {
  8646. ShowWarning("buildin_monster: Attempted to spawn non-existing ai %d for monster class %d\n", ai, class_);
  8647. return SCRIPT_CMD_FAILURE;
  8648. }
  8649. }
  8650. sd = map_id2sd(st->rid);
  8651. if (sd && strcmp(mapn, "this") == 0)
  8652. m = sd->bl.m;
  8653. else
  8654. m = map_mapname2mapid(mapn);
  8655. for(i = 0; i < amount; i++) { //not optimised
  8656. int mobid = mob_once_spawn_area(sd, m, x0, y0, x1, y1, str, class_, 1, event, size, ai);
  8657. if (mobid)
  8658. mapreg_setreg(reference_uid(add_str("$@mobid"), i), mobid);
  8659. }
  8660. return SCRIPT_CMD_SUCCESS;
  8661. }
  8662. /*==========================================
  8663. * KillMonster subcheck, verify if mob to kill ain't got an even to handle, could be force kill by allflag
  8664. *------------------------------------------*/
  8665. static int buildin_killmonster_sub_strip(struct block_list *bl,va_list ap)
  8666. { //same fix but with killmonster instead - stripping events from mobs.
  8667. TBL_MOB* md = (TBL_MOB*)bl;
  8668. char *event=va_arg(ap,char *);
  8669. int allflag=va_arg(ap,int);
  8670. md->state.npc_killmonster = 1;
  8671. if(!allflag){
  8672. if(strcmp(event,md->npc_event)==0)
  8673. status_kill(bl);
  8674. }else{
  8675. if(!md->spawn)
  8676. status_kill(bl);
  8677. }
  8678. md->state.npc_killmonster = 0;
  8679. return SCRIPT_CMD_SUCCESS;
  8680. }
  8681. static int buildin_killmonster_sub(struct block_list *bl,va_list ap)
  8682. {
  8683. TBL_MOB* md = (TBL_MOB*)bl;
  8684. char *event=va_arg(ap,char *);
  8685. int allflag=va_arg(ap,int);
  8686. if(!allflag){
  8687. if(strcmp(event,md->npc_event)==0)
  8688. status_kill(bl);
  8689. }else{
  8690. if(!md->spawn)
  8691. status_kill(bl);
  8692. }
  8693. return SCRIPT_CMD_SUCCESS;
  8694. }
  8695. BUILDIN_FUNC(killmonster)
  8696. {
  8697. const char *mapname,*event;
  8698. int16 m,allflag=0;
  8699. mapname=script_getstr(st,2);
  8700. event=script_getstr(st,3);
  8701. if(strcmp(event,"All")==0)
  8702. allflag = 1;
  8703. else
  8704. check_event(st, event);
  8705. if( (m=map_mapname2mapid(mapname))<0 )
  8706. return SCRIPT_CMD_SUCCESS;
  8707. if( script_hasdata(st,4) ) {
  8708. if ( script_getnum(st,4) == 1 ) {
  8709. map_foreachinmap(buildin_killmonster_sub, m, BL_MOB, event ,allflag);
  8710. return SCRIPT_CMD_SUCCESS;
  8711. }
  8712. }
  8713. map_freeblock_lock();
  8714. map_foreachinmap(buildin_killmonster_sub_strip, m, BL_MOB, event ,allflag);
  8715. map_freeblock_unlock();
  8716. return SCRIPT_CMD_SUCCESS;
  8717. }
  8718. static int buildin_killmonsterall_sub_strip(struct block_list *bl,va_list ap)
  8719. { //Strips the event from the mob if it's killed the old method.
  8720. struct mob_data *md;
  8721. md = BL_CAST(BL_MOB, bl);
  8722. if (md->npc_event[0])
  8723. md->npc_event[0] = 0;
  8724. status_kill(bl);
  8725. return 0;
  8726. }
  8727. static int buildin_killmonsterall_sub(struct block_list *bl,va_list ap)
  8728. {
  8729. status_kill(bl);
  8730. return 0;
  8731. }
  8732. BUILDIN_FUNC(killmonsterall)
  8733. {
  8734. const char *mapname;
  8735. int16 m;
  8736. mapname=script_getstr(st,2);
  8737. if( (m = map_mapname2mapid(mapname))<0 )
  8738. return SCRIPT_CMD_SUCCESS;
  8739. if( script_hasdata(st,3) ) {
  8740. if ( script_getnum(st,3) == 1 ) {
  8741. map_foreachinmap(buildin_killmonsterall_sub,m,BL_MOB);
  8742. return SCRIPT_CMD_SUCCESS;
  8743. }
  8744. }
  8745. map_foreachinmap(buildin_killmonsterall_sub_strip,m,BL_MOB);
  8746. return SCRIPT_CMD_SUCCESS;
  8747. }
  8748. /*==========================================
  8749. * Creates a clone of a player.
  8750. * clone map, x, y, event, char_id, master_id, mode, flag, duration
  8751. *------------------------------------------*/
  8752. BUILDIN_FUNC(clone)
  8753. {
  8754. TBL_PC *sd, *msd=NULL;
  8755. uint32 char_id, master_id = 0, x, y, flag = 0, m;
  8756. enum e_mode mode = 0;
  8757. unsigned int duration = 0;
  8758. const char *mapname,*event;
  8759. mapname=script_getstr(st,2);
  8760. x=script_getnum(st,3);
  8761. y=script_getnum(st,4);
  8762. event=script_getstr(st,5);
  8763. char_id=script_getnum(st,6);
  8764. if( script_hasdata(st,7) )
  8765. master_id=script_getnum(st,7);
  8766. if( script_hasdata(st,8) )
  8767. mode=script_getnum(st,8);
  8768. if( script_hasdata(st,9) )
  8769. flag=script_getnum(st,9);
  8770. if( script_hasdata(st,10) )
  8771. duration=script_getnum(st,10);
  8772. check_event(st, event);
  8773. m = map_mapname2mapid(mapname);
  8774. if (m < 0)
  8775. return SCRIPT_CMD_SUCCESS;
  8776. sd = map_charid2sd(char_id);
  8777. if (master_id) {
  8778. msd = map_charid2sd(master_id);
  8779. if (msd)
  8780. master_id = msd->bl.id;
  8781. else
  8782. master_id = 0;
  8783. }
  8784. if (sd) //Return ID of newly crafted clone.
  8785. script_pushint(st,mob_clone_spawn(sd, m, x, y, event, master_id, mode, flag, 1000*duration));
  8786. else //Failed to create clone.
  8787. script_pushint(st,0);
  8788. return SCRIPT_CMD_SUCCESS;
  8789. }
  8790. /*==========================================
  8791. *------------------------------------------*/
  8792. BUILDIN_FUNC(doevent)
  8793. {
  8794. const char* event = script_getstr(st,2);
  8795. struct map_session_data* sd;
  8796. if( ( sd = script_rid2sd(st) ) == NULL )
  8797. {
  8798. return SCRIPT_CMD_SUCCESS;
  8799. }
  8800. check_event(st, event);
  8801. npc_event(sd, event, 0);
  8802. return SCRIPT_CMD_SUCCESS;
  8803. }
  8804. /*==========================================
  8805. *------------------------------------------*/
  8806. BUILDIN_FUNC(donpcevent)
  8807. {
  8808. const char* event = script_getstr(st,2);
  8809. check_event(st, event);
  8810. if( !npc_event_do(event) ) {
  8811. struct npc_data * nd = map_id2nd(st->oid);
  8812. ShowDebug("NPCEvent '%s' not found! (source: %s)\n",event,nd?nd->name:"Unknown");
  8813. script_pushint(st, 0);
  8814. } else
  8815. script_pushint(st, 1);
  8816. return SCRIPT_CMD_SUCCESS;
  8817. }
  8818. /// for Aegis compatibility
  8819. /// basically a specialized 'donpcevent', with the event specified as two arguments instead of one
  8820. BUILDIN_FUNC(cmdothernpc) // Added by RoVeRT
  8821. {
  8822. const char* npc = script_getstr(st,2);
  8823. const char* command = script_getstr(st,3);
  8824. char event[EVENT_NAME_LENGTH];
  8825. snprintf(event, sizeof(event), "%s::OnCommand%s", npc, command);
  8826. check_event(st, event);
  8827. npc_event_do(event);
  8828. return SCRIPT_CMD_SUCCESS;
  8829. }
  8830. /*==========================================
  8831. *------------------------------------------*/
  8832. BUILDIN_FUNC(addtimer)
  8833. {
  8834. int tick = script_getnum(st,2);
  8835. const char* event = script_getstr(st, 3);
  8836. TBL_PC* sd;
  8837. check_event(st, event);
  8838. sd = script_rid2sd(st);
  8839. if( sd == NULL )
  8840. return SCRIPT_CMD_SUCCESS;
  8841. if (!pc_addeventtimer(sd,tick,event)) {
  8842. ShowWarning("buildin_addtimer: Event timer is full, can't add new event timer. (cid:%d timer:%s)\n",sd->status.char_id,event);
  8843. return SCRIPT_CMD_FAILURE;
  8844. }
  8845. return SCRIPT_CMD_SUCCESS;
  8846. }
  8847. /*==========================================
  8848. *------------------------------------------*/
  8849. BUILDIN_FUNC(deltimer)
  8850. {
  8851. const char *event;
  8852. TBL_PC* sd;
  8853. event=script_getstr(st, 2);
  8854. sd = script_rid2sd(st);
  8855. if( sd == NULL )
  8856. return SCRIPT_CMD_SUCCESS;
  8857. check_event(st, event);
  8858. pc_deleventtimer(sd,event);
  8859. return SCRIPT_CMD_SUCCESS;
  8860. }
  8861. /*==========================================
  8862. *------------------------------------------*/
  8863. BUILDIN_FUNC(addtimercount)
  8864. {
  8865. const char *event;
  8866. int tick;
  8867. TBL_PC* sd;
  8868. tick=script_getnum(st,2);
  8869. event=script_getstr(st,3);
  8870. sd = script_rid2sd(st);
  8871. if( sd == NULL )
  8872. return SCRIPT_CMD_SUCCESS;
  8873. check_event(st, event);
  8874. pc_addeventtimercount(sd,event,tick);
  8875. return SCRIPT_CMD_SUCCESS;
  8876. }
  8877. /*==========================================
  8878. *------------------------------------------*/
  8879. BUILDIN_FUNC(initnpctimer)
  8880. {
  8881. struct npc_data *nd;
  8882. int flag = 0;
  8883. if( script_hasdata(st,3) )
  8884. { //Two arguments: NPC name and attach flag.
  8885. nd = npc_name2id(script_getstr(st, 2));
  8886. flag = script_getnum(st,3);
  8887. }
  8888. else if( script_hasdata(st,2) )
  8889. { //Check if argument is numeric (flag) or string (npc name)
  8890. struct script_data *data;
  8891. data = script_getdata(st,2);
  8892. get_val(st,data);
  8893. if( data_isstring(data) ) //NPC name
  8894. nd = npc_name2id(conv_str(st, data));
  8895. else if( data_isint(data) ) //Flag
  8896. {
  8897. nd = (struct npc_data *)map_id2bl(st->oid);
  8898. flag = conv_num(st,data);
  8899. }
  8900. else
  8901. {
  8902. ShowError("initnpctimer: invalid argument type #1 (needs be int or string)).\n");
  8903. return SCRIPT_CMD_FAILURE;
  8904. }
  8905. }
  8906. else
  8907. nd = (struct npc_data *)map_id2bl(st->oid);
  8908. if( !nd )
  8909. return SCRIPT_CMD_SUCCESS;
  8910. if( flag ) //Attach
  8911. {
  8912. TBL_PC* sd = script_rid2sd(st);
  8913. if( sd == NULL )
  8914. return SCRIPT_CMD_SUCCESS;
  8915. nd->u.scr.rid = sd->bl.id;
  8916. }
  8917. nd->u.scr.timertick = 0;
  8918. npc_settimerevent_tick(nd,0);
  8919. npc_timerevent_start(nd, st->rid);
  8920. return SCRIPT_CMD_SUCCESS;
  8921. }
  8922. /*==========================================
  8923. *------------------------------------------*/
  8924. BUILDIN_FUNC(startnpctimer)
  8925. {
  8926. struct npc_data *nd;
  8927. int flag = 0;
  8928. if( script_hasdata(st,3) )
  8929. { //Two arguments: NPC name and attach flag.
  8930. nd = npc_name2id(script_getstr(st, 2));
  8931. flag = script_getnum(st,3);
  8932. }
  8933. else if( script_hasdata(st,2) )
  8934. { //Check if argument is numeric (flag) or string (npc name)
  8935. struct script_data *data;
  8936. data = script_getdata(st,2);
  8937. get_val(st,data);
  8938. if( data_isstring(data) ) //NPC name
  8939. nd = npc_name2id(conv_str(st, data));
  8940. else if( data_isint(data) ) //Flag
  8941. {
  8942. nd = (struct npc_data *)map_id2bl(st->oid);
  8943. flag = conv_num(st,data);
  8944. }
  8945. else
  8946. {
  8947. ShowError("initnpctimer: invalid argument type #1 (needs be int or string)).\n");
  8948. return SCRIPT_CMD_FAILURE;
  8949. }
  8950. }
  8951. else
  8952. nd=(struct npc_data *)map_id2bl(st->oid);
  8953. if( !nd )
  8954. return SCRIPT_CMD_SUCCESS;
  8955. if( flag ) //Attach
  8956. {
  8957. TBL_PC* sd = script_rid2sd(st);
  8958. if( sd == NULL )
  8959. return SCRIPT_CMD_SUCCESS;
  8960. nd->u.scr.rid = sd->bl.id;
  8961. }
  8962. npc_timerevent_start(nd, st->rid);
  8963. return SCRIPT_CMD_SUCCESS;
  8964. }
  8965. /*==========================================
  8966. *------------------------------------------*/
  8967. BUILDIN_FUNC(stopnpctimer)
  8968. {
  8969. struct npc_data *nd;
  8970. int flag = 0;
  8971. if( script_hasdata(st,3) )
  8972. { //Two arguments: NPC name and attach flag.
  8973. nd = npc_name2id(script_getstr(st, 2));
  8974. flag = script_getnum(st,3);
  8975. }
  8976. else if( script_hasdata(st,2) )
  8977. { //Check if argument is numeric (flag) or string (npc name)
  8978. struct script_data *data;
  8979. data = script_getdata(st,2);
  8980. get_val(st,data);
  8981. if( data_isstring(data) ) //NPC name
  8982. nd = npc_name2id(conv_str(st, data));
  8983. else if( data_isint(data) ) //Flag
  8984. {
  8985. nd = (struct npc_data *)map_id2bl(st->oid);
  8986. flag = conv_num(st,data);
  8987. }
  8988. else
  8989. {
  8990. ShowError("initnpctimer: invalid argument type #1 (needs be int or string)).\n");
  8991. return SCRIPT_CMD_FAILURE;
  8992. }
  8993. }
  8994. else
  8995. nd=(struct npc_data *)map_id2bl(st->oid);
  8996. if( !nd )
  8997. return SCRIPT_CMD_SUCCESS;
  8998. if( flag ) //Detach
  8999. nd->u.scr.rid = 0;
  9000. npc_timerevent_stop(nd);
  9001. return SCRIPT_CMD_SUCCESS;
  9002. }
  9003. /*==========================================
  9004. *------------------------------------------*/
  9005. BUILDIN_FUNC(getnpctimer)
  9006. {
  9007. struct npc_data *nd;
  9008. TBL_PC *sd;
  9009. int type = script_getnum(st,2);
  9010. int val = 0;
  9011. if( script_hasdata(st,3) )
  9012. nd = npc_name2id(script_getstr(st,3));
  9013. else
  9014. nd = (struct npc_data *)map_id2bl(st->oid);
  9015. if( !nd || nd->bl.type != BL_NPC )
  9016. {
  9017. script_pushint(st,0);
  9018. ShowError("getnpctimer: Invalid NPC.\n");
  9019. return SCRIPT_CMD_FAILURE;
  9020. }
  9021. switch( type )
  9022. {
  9023. case 0: val = npc_gettimerevent_tick(nd); break;
  9024. case 1:
  9025. if( nd->u.scr.rid )
  9026. {
  9027. sd = map_id2sd(nd->u.scr.rid);
  9028. if( !sd )
  9029. {
  9030. ShowError("buildin_getnpctimer: Attached player not found!\n");
  9031. break;
  9032. }
  9033. val = (sd->npc_timer_id != INVALID_TIMER);
  9034. }
  9035. else
  9036. val = (nd->u.scr.timerid != INVALID_TIMER);
  9037. break;
  9038. case 2: val = nd->u.scr.timeramount; break;
  9039. }
  9040. script_pushint(st,val);
  9041. return SCRIPT_CMD_SUCCESS;
  9042. }
  9043. /*==========================================
  9044. *------------------------------------------*/
  9045. BUILDIN_FUNC(setnpctimer)
  9046. {
  9047. int tick;
  9048. struct npc_data *nd;
  9049. tick = script_getnum(st,2);
  9050. if( script_hasdata(st,3) )
  9051. nd = npc_name2id(script_getstr(st,3));
  9052. else
  9053. nd = (struct npc_data *)map_id2bl(st->oid);
  9054. if( !nd || nd->bl.type != BL_NPC )
  9055. {
  9056. script_pushint(st,1);
  9057. ShowError("setnpctimer: Invalid NPC.\n");
  9058. return SCRIPT_CMD_FAILURE;
  9059. }
  9060. npc_settimerevent_tick(nd,tick);
  9061. script_pushint(st,0);
  9062. return SCRIPT_CMD_SUCCESS;
  9063. }
  9064. /*==========================================
  9065. * attaches the player rid to the timer [Celest]
  9066. *------------------------------------------*/
  9067. BUILDIN_FUNC(attachnpctimer)
  9068. {
  9069. TBL_PC *sd;
  9070. struct npc_data *nd = (struct npc_data *)map_id2bl(st->oid);
  9071. if( !nd || nd->bl.type != BL_NPC )
  9072. {
  9073. script_pushint(st,1);
  9074. ShowError("setnpctimer: Invalid NPC.\n");
  9075. return SCRIPT_CMD_FAILURE;
  9076. }
  9077. if( script_hasdata(st,2) )
  9078. sd = map_nick2sd(script_getstr(st,2));
  9079. else
  9080. sd = script_rid2sd(st);
  9081. if( !sd )
  9082. {
  9083. script_pushint(st,1);
  9084. ShowWarning("attachnpctimer: Invalid player.\n");
  9085. return SCRIPT_CMD_FAILURE;
  9086. }
  9087. nd->u.scr.rid = sd->bl.id;
  9088. script_pushint(st,0);
  9089. return SCRIPT_CMD_SUCCESS;
  9090. }
  9091. /*==========================================
  9092. * detaches a player rid from the timer [Celest]
  9093. *------------------------------------------*/
  9094. BUILDIN_FUNC(detachnpctimer)
  9095. {
  9096. struct npc_data *nd;
  9097. if( script_hasdata(st,2) )
  9098. nd = npc_name2id(script_getstr(st,2));
  9099. else
  9100. nd = (struct npc_data *)map_id2bl(st->oid);
  9101. if( !nd || nd->bl.type != BL_NPC )
  9102. {
  9103. script_pushint(st,1);
  9104. ShowError("detachnpctimer: Invalid NPC.\n");
  9105. return SCRIPT_CMD_FAILURE;
  9106. }
  9107. nd->u.scr.rid = 0;
  9108. script_pushint(st,0);
  9109. return SCRIPT_CMD_SUCCESS;
  9110. }
  9111. /*==========================================
  9112. * To avoid "player not attached" script errors, this function is provided,
  9113. * it checks if there is a player attached to the current script. [Skotlex]
  9114. * If no, returns 0, if yes, returns the account_id of the attached player.
  9115. *------------------------------------------*/
  9116. BUILDIN_FUNC(playerattached)
  9117. {
  9118. if(st->rid == 0 || map_id2sd(st->rid) == NULL)
  9119. script_pushint(st,0);
  9120. else
  9121. script_pushint(st,st->rid);
  9122. return SCRIPT_CMD_SUCCESS;
  9123. }
  9124. /*==========================================
  9125. *------------------------------------------*/
  9126. BUILDIN_FUNC(announce)
  9127. {
  9128. const char *mes = script_getstr(st,2);
  9129. int flag = script_getnum(st,3);
  9130. const char *fontColor = script_hasdata(st,4) ? script_getstr(st,4) : NULL;
  9131. int fontType = script_hasdata(st,5) ? script_getnum(st,5) : FW_NORMAL; // default fontType
  9132. int fontSize = script_hasdata(st,6) ? script_getnum(st,6) : 12; // default fontSize
  9133. int fontAlign = script_hasdata(st,7) ? script_getnum(st,7) : 0; // default fontAlign
  9134. int fontY = script_hasdata(st,8) ? script_getnum(st,8) : 0; // default fontY
  9135. if (flag&(BC_TARGET_MASK|BC_SOURCE_MASK)) // Broadcast source or broadcast region defined
  9136. {
  9137. send_target target;
  9138. 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
  9139. if (bl == NULL)
  9140. return SCRIPT_CMD_SUCCESS;
  9141. switch (flag&BC_TARGET_MASK) {
  9142. case BC_MAP: target = ALL_SAMEMAP; break;
  9143. case BC_AREA: target = AREA; break;
  9144. case BC_SELF: target = SELF; break;
  9145. default: target = ALL_CLIENT; break; // BC_ALL
  9146. }
  9147. if (fontColor)
  9148. clif_broadcast2(bl, mes, (int)strlen(mes)+1, strtol(fontColor, (char **)NULL, 0), fontType, fontSize, fontAlign, fontY, target);
  9149. else
  9150. clif_broadcast(bl, mes, (int)strlen(mes)+1, flag&BC_COLOR_MASK, target);
  9151. }
  9152. else
  9153. {
  9154. if (fontColor)
  9155. intif_broadcast2(mes, (int)strlen(mes)+1, strtol(fontColor, (char **)NULL, 0), fontType, fontSize, fontAlign, fontY);
  9156. else
  9157. intif_broadcast(mes, (int)strlen(mes)+1, flag&BC_COLOR_MASK);
  9158. }
  9159. return SCRIPT_CMD_SUCCESS;
  9160. }
  9161. /*==========================================
  9162. *------------------------------------------*/
  9163. static int buildin_announce_sub(struct block_list *bl, va_list ap)
  9164. {
  9165. char *mes = va_arg(ap, char *);
  9166. int len = va_arg(ap, int);
  9167. int type = va_arg(ap, int);
  9168. char *fontColor = va_arg(ap, char *);
  9169. short fontType = (short)va_arg(ap, int);
  9170. short fontSize = (short)va_arg(ap, int);
  9171. short fontAlign = (short)va_arg(ap, int);
  9172. short fontY = (short)va_arg(ap, int);
  9173. if (fontColor)
  9174. clif_broadcast2(bl, mes, len, strtol(fontColor, (char **)NULL, 0), fontType, fontSize, fontAlign, fontY, SELF);
  9175. else
  9176. clif_broadcast(bl, mes, len, type, SELF);
  9177. return SCRIPT_CMD_SUCCESS;
  9178. }
  9179. BUILDIN_FUNC(mapannounce)
  9180. {
  9181. const char *mapname = script_getstr(st,2);
  9182. const char *mes = script_getstr(st,3);
  9183. int flag = script_getnum(st,4);
  9184. const char *fontColor = script_hasdata(st,5) ? script_getstr(st,5) : NULL;
  9185. int fontType = script_hasdata(st,6) ? script_getnum(st,6) : FW_NORMAL; // default fontType
  9186. int fontSize = script_hasdata(st,7) ? script_getnum(st,7) : 12; // default fontSize
  9187. int fontAlign = script_hasdata(st,8) ? script_getnum(st,8) : 0; // default fontAlign
  9188. int fontY = script_hasdata(st,9) ? script_getnum(st,9) : 0; // default fontY
  9189. int16 m;
  9190. if ((m = map_mapname2mapid(mapname)) < 0)
  9191. return SCRIPT_CMD_SUCCESS;
  9192. map_foreachinmap(buildin_announce_sub, m, BL_PC,
  9193. mes, strlen(mes)+1, flag&BC_COLOR_MASK, fontColor, fontType, fontSize, fontAlign, fontY);
  9194. return SCRIPT_CMD_SUCCESS;
  9195. }
  9196. /*==========================================
  9197. *------------------------------------------*/
  9198. BUILDIN_FUNC(areaannounce)
  9199. {
  9200. const char *mapname = script_getstr(st,2);
  9201. int x0 = script_getnum(st,3);
  9202. int y0 = script_getnum(st,4);
  9203. int x1 = script_getnum(st,5);
  9204. int y1 = script_getnum(st,6);
  9205. const char *mes = script_getstr(st,7);
  9206. int flag = script_getnum(st,8);
  9207. const char *fontColor = script_hasdata(st,9) ? script_getstr(st,9) : NULL;
  9208. int fontType = script_hasdata(st,10) ? script_getnum(st,10) : FW_NORMAL; // default fontType
  9209. int fontSize = script_hasdata(st,11) ? script_getnum(st,11) : 12; // default fontSize
  9210. int fontAlign = script_hasdata(st,12) ? script_getnum(st,12) : 0; // default fontAlign
  9211. int fontY = script_hasdata(st,13) ? script_getnum(st,13) : 0; // default fontY
  9212. int16 m;
  9213. if ((m = map_mapname2mapid(mapname)) < 0)
  9214. return SCRIPT_CMD_SUCCESS;
  9215. map_foreachinarea(buildin_announce_sub, m, x0, y0, x1, y1, BL_PC,
  9216. mes, strlen(mes)+1, flag&BC_COLOR_MASK, fontColor, fontType, fontSize, fontAlign, fontY);
  9217. return SCRIPT_CMD_SUCCESS;
  9218. }
  9219. /*==========================================
  9220. *------------------------------------------*/
  9221. BUILDIN_FUNC(getusers)
  9222. {
  9223. int flag, val = 0;
  9224. struct map_session_data* sd;
  9225. struct block_list* bl = NULL;
  9226. flag = script_getnum(st,2);
  9227. switch(flag&0x07)
  9228. {
  9229. case 0:
  9230. if(flag&0x8)
  9231. {// npc
  9232. bl = map_id2bl(st->oid);
  9233. }
  9234. else if((sd = script_rid2sd(st))!=NULL)
  9235. {// pc
  9236. bl = &sd->bl;
  9237. }
  9238. if(bl)
  9239. {
  9240. val = map[bl->m].users;
  9241. }
  9242. break;
  9243. case 1:
  9244. val = map_getusers();
  9245. break;
  9246. default:
  9247. ShowWarning("buildin_getusers: Unknown type %d.\n", flag);
  9248. script_pushint(st,0);
  9249. return SCRIPT_CMD_FAILURE;
  9250. }
  9251. script_pushint(st,val);
  9252. return SCRIPT_CMD_SUCCESS;
  9253. }
  9254. /*==========================================
  9255. * getmapguildusers("mapname",guild ID) Returns the number guild members present on a map [Reddozen]
  9256. *------------------------------------------*/
  9257. BUILDIN_FUNC(getmapguildusers)
  9258. {
  9259. const char *str;
  9260. int16 m;
  9261. int gid;
  9262. int c=0;
  9263. struct guild *g = NULL;
  9264. str=script_getstr(st,2);
  9265. gid=script_getnum(st,3);
  9266. if ((m = map_mapname2mapid(str)) < 0) { // map id on this server (m == -1 if not in actual map-server)
  9267. script_pushint(st,-1);
  9268. return SCRIPT_CMD_SUCCESS;
  9269. }
  9270. g = guild_search(gid);
  9271. if (g){
  9272. unsigned short i;
  9273. for(i = 0; i < g->max_member; i++)
  9274. {
  9275. if (g->member[i].sd && g->member[i].sd->bl.m == m)
  9276. c++;
  9277. }
  9278. }
  9279. script_pushint(st,c);
  9280. return SCRIPT_CMD_SUCCESS;
  9281. }
  9282. /*==========================================
  9283. *------------------------------------------*/
  9284. BUILDIN_FUNC(getmapusers)
  9285. {
  9286. const char *str;
  9287. int16 m;
  9288. str=script_getstr(st,2);
  9289. if( (m=map_mapname2mapid(str))< 0){
  9290. script_pushint(st,-1);
  9291. return SCRIPT_CMD_SUCCESS;
  9292. }
  9293. script_pushint(st,map[m].users);
  9294. return SCRIPT_CMD_SUCCESS;
  9295. }
  9296. /*==========================================
  9297. *------------------------------------------*/
  9298. static int buildin_getareausers_sub(struct block_list *bl,va_list ap)
  9299. {
  9300. int *users=va_arg(ap,int *);
  9301. (*users)++;
  9302. return SCRIPT_CMD_SUCCESS;
  9303. }
  9304. BUILDIN_FUNC(getareausers)
  9305. {
  9306. const char *str;
  9307. int16 m,x0,y0,x1,y1,users=0; //doubt we can have more then 32k users on
  9308. str=script_getstr(st,2);
  9309. x0=script_getnum(st,3);
  9310. y0=script_getnum(st,4);
  9311. x1=script_getnum(st,5);
  9312. y1=script_getnum(st,6);
  9313. if( (m=map_mapname2mapid(str))< 0){
  9314. script_pushint(st,-1);
  9315. return SCRIPT_CMD_SUCCESS;
  9316. }
  9317. map_foreachinarea(buildin_getareausers_sub,
  9318. m,x0,y0,x1,y1,BL_PC,&users);
  9319. script_pushint(st,users);
  9320. return SCRIPT_CMD_SUCCESS;
  9321. }
  9322. /*==========================================
  9323. *------------------------------------------*/
  9324. static int buildin_getareadropitem_sub(struct block_list *bl,va_list ap)
  9325. {
  9326. int nameid=va_arg(ap,int);
  9327. int *amount=va_arg(ap,int *);
  9328. struct flooritem_data *drop=(struct flooritem_data *)bl;
  9329. if(drop->item.nameid==nameid)
  9330. (*amount)+=drop->item.amount;
  9331. return SCRIPT_CMD_SUCCESS;
  9332. }
  9333. BUILDIN_FUNC(getareadropitem)
  9334. {
  9335. const char *str;
  9336. int16 m,x0,y0,x1,y1;
  9337. int nameid,amount=0;
  9338. struct script_data *data;
  9339. str=script_getstr(st,2);
  9340. x0=script_getnum(st,3);
  9341. y0=script_getnum(st,4);
  9342. x1=script_getnum(st,5);
  9343. y1=script_getnum(st,6);
  9344. data=script_getdata(st,7);
  9345. get_val(st,data);
  9346. if( data_isstring(data) ){
  9347. const char *name=conv_str(st,data);
  9348. struct item_data *item_data = itemdb_searchname(name);
  9349. nameid=UNKNOWN_ITEM_ID;
  9350. if( item_data )
  9351. nameid=item_data->nameid;
  9352. }else
  9353. nameid=conv_num(st,data);
  9354. if( (m=map_mapname2mapid(str))< 0){
  9355. script_pushint(st,-1);
  9356. return SCRIPT_CMD_SUCCESS;
  9357. }
  9358. map_foreachinarea(buildin_getareadropitem_sub,
  9359. m,x0,y0,x1,y1,BL_ITEM,nameid,&amount);
  9360. script_pushint(st,amount);
  9361. return SCRIPT_CMD_SUCCESS;
  9362. }
  9363. /*==========================================
  9364. *------------------------------------------*/
  9365. BUILDIN_FUNC(enablenpc)
  9366. {
  9367. const char *str;
  9368. str=script_getstr(st,2);
  9369. npc_enable(str,1);
  9370. return SCRIPT_CMD_SUCCESS;
  9371. }
  9372. /*==========================================
  9373. *------------------------------------------*/
  9374. BUILDIN_FUNC(disablenpc)
  9375. {
  9376. const char *str;
  9377. str=script_getstr(st,2);
  9378. npc_enable(str,0);
  9379. return SCRIPT_CMD_SUCCESS;
  9380. }
  9381. /*==========================================
  9382. *------------------------------------------*/
  9383. BUILDIN_FUNC(hideoffnpc)
  9384. {
  9385. const char *str;
  9386. str=script_getstr(st,2);
  9387. npc_enable(str,2);
  9388. return SCRIPT_CMD_SUCCESS;
  9389. }
  9390. /*==========================================
  9391. *------------------------------------------*/
  9392. BUILDIN_FUNC(hideonnpc)
  9393. {
  9394. const char *str;
  9395. str=script_getstr(st,2);
  9396. npc_enable(str,4);
  9397. return SCRIPT_CMD_SUCCESS;
  9398. }
  9399. /* Starts a status effect on the target unit or on the attached player.
  9400. *
  9401. * sc_start <effect_id>,<duration>,<val1>{,<rate>,<flag>,{<unit_id>}};
  9402. * sc_start2 <effect_id>,<duration>,<val1>,<val2>{,<rate,<flag>,{<unit_id>}};
  9403. * sc_start4 <effect_id>,<duration>,<val1>,<val2>,<val3>,<val4>{,<rate,<flag>,{<unit_id>}};
  9404. * <flag>: enum e_status_change_start_flags
  9405. */
  9406. BUILDIN_FUNC(sc_start)
  9407. {
  9408. TBL_NPC * nd = map_id2nd(st->oid);
  9409. struct block_list* bl;
  9410. enum sc_type type;
  9411. int tick, val1, val2, val3, val4=0, rate, flag;
  9412. char start_type;
  9413. const char* command = script_getfuncname(st);
  9414. if(strstr(command, "4"))
  9415. start_type = 4;
  9416. else if(strstr(command, "2"))
  9417. start_type = 2;
  9418. else
  9419. start_type = 1;
  9420. type = (sc_type)script_getnum(st,2);
  9421. tick = script_getnum(st,3);
  9422. val1 = script_getnum(st,4);
  9423. //If from NPC we make default flag 1 to be unavoidable
  9424. if(nd && nd->bl.id == fake_nd->bl.id)
  9425. flag = script_hasdata(st,5+start_type)?script_getnum(st,5+start_type):SCSTART_NOTICKDEF;
  9426. else
  9427. flag = script_hasdata(st,5+start_type)?script_getnum(st,5+start_type):SCSTART_NOAVOID;
  9428. rate = script_hasdata(st,4+start_type)?min(script_getnum(st,4+start_type),10000):10000;
  9429. if(script_hasdata(st,(6+start_type)))
  9430. bl = map_id2bl(script_getnum(st,(6+start_type)));
  9431. else
  9432. bl = map_id2bl(st->rid);
  9433. if(tick == 0 && val1 > 0 && type > SC_NONE && type < SC_MAX && status_sc2skill(type) != 0)
  9434. {// When there isn't a duration specified, try to get it from the skill_db
  9435. tick = skill_get_time(status_sc2skill(type), val1);
  9436. }
  9437. if(potion_flag == 1 && potion_target) { //skill.c set the flags before running the script, this is a potion-pitched effect.
  9438. bl = map_id2bl(potion_target);
  9439. tick /= 2;// Thrown potions only last half.
  9440. val4 = 1;// Mark that this was a thrown sc_effect
  9441. }
  9442. if(!bl)
  9443. return SCRIPT_CMD_SUCCESS;
  9444. switch(start_type) {
  9445. case 1:
  9446. status_change_start(bl, bl, type, rate, val1, 0, 0, val4, tick, flag);
  9447. break;
  9448. case 2:
  9449. val2 = script_getnum(st,5);
  9450. status_change_start(bl, bl, type, rate, val1, val2, 0, val4, tick, flag);
  9451. break;
  9452. case 4:
  9453. val2 = script_getnum(st,5);
  9454. val3 = script_getnum(st,6);
  9455. val4 = script_getnum(st,7);
  9456. status_change_start(bl, bl, type, rate, val1, val2, val3, val4, tick, flag);
  9457. break;
  9458. }
  9459. return SCRIPT_CMD_SUCCESS;
  9460. }
  9461. /// Ends one or all status effects on the target unit or on the attached player.
  9462. ///
  9463. /// sc_end <effect_id>{,<unit_id>};
  9464. BUILDIN_FUNC(sc_end)
  9465. {
  9466. struct block_list* bl;
  9467. int type;
  9468. type = script_getnum(st, 2);
  9469. if (script_hasdata(st, 3))
  9470. bl = map_id2bl(script_getnum(st, 3));
  9471. else
  9472. bl = map_id2bl(st->rid);
  9473. if (potion_flag == 1 && potion_target) //##TODO how does this work [FlavioJS]
  9474. bl = map_id2bl(potion_target);
  9475. if (!bl)
  9476. return SCRIPT_CMD_SUCCESS;
  9477. if (type >= 0 && type < SC_MAX) {
  9478. struct status_change *sc = status_get_sc(bl);
  9479. struct status_change_entry *sce = sc ? sc->data[type] : NULL;
  9480. if (!sce)
  9481. return SCRIPT_CMD_SUCCESS;
  9482. switch (type) {
  9483. case SC_WEIGHT50:
  9484. case SC_WEIGHT90:
  9485. case SC_NOCHAT:
  9486. case SC_PUSH_CART:
  9487. case SC_ALL_RIDING:
  9488. case SC_STYLE_CHANGE:
  9489. case SC_MONSTER_TRANSFORM:
  9490. case SC_MTF_ASPD:
  9491. case SC_MTF_RANGEATK:
  9492. case SC_MTF_MATK:
  9493. case SC_MTF_MLEATKED:
  9494. case SC_MTF_CRIDAMAGE:
  9495. case SC_MTF_ASPD2:
  9496. case SC_MTF_RANGEATK2:
  9497. case SC_MTF_MATK2:
  9498. case SC_MTF_MHP:
  9499. case SC_MTF_MSP:
  9500. case SC_MTF_PUMPKIN:
  9501. case SC_MTF_HITFLEE:
  9502. case SC_ATTHASTE_CASH:
  9503. case SC_REUSE_LIMIT_A: case SC_REUSE_LIMIT_B: case SC_REUSE_LIMIT_C:
  9504. case SC_REUSE_LIMIT_D: case SC_REUSE_LIMIT_E: case SC_REUSE_LIMIT_F:
  9505. case SC_REUSE_LIMIT_G: case SC_REUSE_LIMIT_H: case SC_REUSE_LIMIT_MTF:
  9506. case SC_REUSE_LIMIT_ASPD_POTION: case SC_REUSE_MILLENNIUMSHIELD: case SC_REUSE_CRUSHSTRIKE:
  9507. case SC_REUSE_STORMBLAST: case SC_ALL_RIDING_REUSE_LIMIT: case SC_REUSE_REFRESH:
  9508. case SC_REUSE_LIMIT_ECL: case SC_REUSE_LIMIT_RECALL:
  9509. return SCRIPT_CMD_SUCCESS;
  9510. default:
  9511. break;
  9512. }
  9513. //This should help status_change_end force disabling the SC in case it has no limit.
  9514. sce->val1 = sce->val2 = sce->val3 = sce->val4 = 0;
  9515. status_change_end(bl, (sc_type)type, INVALID_TIMER);
  9516. } else
  9517. status_change_clear(bl, 3); // remove all effects
  9518. return SCRIPT_CMD_SUCCESS;
  9519. }
  9520. /*==========================================
  9521. * @FIXME atm will return reduced tick, 0 immune, 1 no tick
  9522. *------------------------------------------*/
  9523. BUILDIN_FUNC(getscrate)
  9524. {
  9525. struct block_list *bl;
  9526. int type,rate;
  9527. type=script_getnum(st,2);
  9528. rate=script_getnum(st,3);
  9529. if( script_hasdata(st,4) ) //get for the bl assigned
  9530. bl = map_id2bl(script_getnum(st,4));
  9531. else
  9532. bl = map_id2bl(st->rid);
  9533. if (bl)
  9534. rate = status_get_sc_def(NULL,bl, (sc_type)type, 10000, 10000, SCSTART_NONE);
  9535. script_pushint(st,rate);
  9536. return SCRIPT_CMD_SUCCESS;
  9537. }
  9538. /**
  9539. * getstatus(<effect type>{,<type>{,<char_id>}});
  9540. **/
  9541. BUILDIN_FUNC(getstatus)
  9542. {
  9543. int id, type;
  9544. struct map_session_data* sd;
  9545. if (!script_charid2sd(4,sd))
  9546. return SCRIPT_CMD_FAILURE;
  9547. id = script_getnum(st, 2);
  9548. type = script_hasdata(st, 3) ? script_getnum(st, 3) : 0;
  9549. if( id <= SC_NONE || id >= SC_MAX )
  9550. {// invalid status type given
  9551. ShowWarning("script.c:getstatus: Invalid status type given (%d).\n", id);
  9552. return SCRIPT_CMD_SUCCESS;
  9553. }
  9554. if( sd->sc.count == 0 || !sd->sc.data[id] )
  9555. {// no status is active
  9556. script_pushint(st, 0);
  9557. return SCRIPT_CMD_SUCCESS;
  9558. }
  9559. switch( type )
  9560. {
  9561. case 1: script_pushint(st, sd->sc.data[id]->val1); break;
  9562. case 2: script_pushint(st, sd->sc.data[id]->val2); break;
  9563. case 3: script_pushint(st, sd->sc.data[id]->val3); break;
  9564. case 4: script_pushint(st, sd->sc.data[id]->val4); break;
  9565. case 5:
  9566. {
  9567. struct TimerData* timer = (struct TimerData*)get_timer(sd->sc.data[id]->timer);
  9568. if( timer )
  9569. {// return the amount of time remaining
  9570. script_pushint(st, timer->tick - gettick());
  9571. } else {
  9572. script_pushint(st, -1);
  9573. }
  9574. }
  9575. break;
  9576. default: script_pushint(st, 1); break;
  9577. }
  9578. return SCRIPT_CMD_SUCCESS;
  9579. }
  9580. /*==========================================
  9581. *
  9582. *------------------------------------------*/
  9583. BUILDIN_FUNC(debugmes)
  9584. {
  9585. const char *str;
  9586. str=script_getstr(st,2);
  9587. ShowDebug("script debug : %d %d : %s\n",st->rid,st->oid,str);
  9588. return SCRIPT_CMD_SUCCESS;
  9589. }
  9590. /*==========================================
  9591. *------------------------------------------*/
  9592. BUILDIN_FUNC(catchpet)
  9593. {
  9594. int pet_id;
  9595. TBL_PC *sd;
  9596. pet_id= script_getnum(st,2);
  9597. sd=script_rid2sd(st);
  9598. if( sd == NULL )
  9599. return SCRIPT_CMD_SUCCESS;
  9600. pet_catch_process1(sd,pet_id);
  9601. return SCRIPT_CMD_SUCCESS;
  9602. }
  9603. /*==========================================
  9604. * [orn]
  9605. *------------------------------------------*/
  9606. BUILDIN_FUNC(homunculus_evolution)
  9607. {
  9608. TBL_PC *sd;
  9609. sd=script_rid2sd(st);
  9610. if( sd == NULL )
  9611. return SCRIPT_CMD_SUCCESS;
  9612. if(hom_is_active(sd->hd))
  9613. {
  9614. if (sd->hd->homunculus.intimacy >= battle_config.homunculus_evo_intimacy_need)
  9615. hom_evolution(sd->hd);
  9616. else
  9617. clif_emotion(&sd->hd->bl, E_SWT);
  9618. }
  9619. return SCRIPT_CMD_SUCCESS;
  9620. }
  9621. /*==========================================
  9622. * Checks for vaporized morph state
  9623. * and deletes ITEMID_STRANGE_EMBRYO.
  9624. *------------------------------------------*/
  9625. BUILDIN_FUNC(homunculus_mutate)
  9626. {
  9627. int homun_id;
  9628. TBL_PC *sd;
  9629. sd = script_rid2sd(st);
  9630. if( sd == NULL || sd->hd == NULL )
  9631. return SCRIPT_CMD_SUCCESS;
  9632. if(script_hasdata(st,2))
  9633. homun_id = script_getnum(st,2);
  9634. else
  9635. homun_id = 6048 + (rnd() % 4);
  9636. if( sd->hd->homunculus.vaporize == HOM_ST_MORPH ) {
  9637. int m_class = hom_class2mapid(sd->hd->homunculus.class_);
  9638. int m_id = hom_class2mapid(homun_id);
  9639. short i = pc_search_inventory(sd, ITEMID_STRANGE_EMBRYO);
  9640. if ( m_class != -1 && m_id != -1 && m_class&HOM_EVO && m_id&HOM_S && sd->hd->homunculus.level >= 99 && i >= 0 ) {
  9641. sd->hd->homunculus.vaporize = HOM_ST_REST; // Remove morph state.
  9642. hom_call(sd); // Respawn homunculus.
  9643. hom_mutate(sd->hd, homun_id);
  9644. pc_delitem(sd, i, 1, 0, 0, LOG_TYPE_SCRIPT);
  9645. script_pushint(st, 1);
  9646. return SCRIPT_CMD_SUCCESS;
  9647. } else
  9648. clif_emotion(&sd->bl, E_SWT);
  9649. } else
  9650. clif_emotion(&sd->bl, E_SWT);
  9651. script_pushint(st, 0);
  9652. return SCRIPT_CMD_SUCCESS;
  9653. }
  9654. /*==========================================
  9655. * Puts homunculus into morph state
  9656. * and gives ITEMID_STRANGE_EMBRYO.
  9657. *------------------------------------------*/
  9658. BUILDIN_FUNC(morphembryo)
  9659. {
  9660. struct item item_tmp;
  9661. TBL_PC *sd;
  9662. sd = script_rid2sd(st);
  9663. if( sd == NULL || sd->hd == NULL )
  9664. return SCRIPT_CMD_SUCCESS;
  9665. if( hom_is_active(sd->hd) ) {
  9666. int m_class = hom_class2mapid(sd->hd->homunculus.class_);
  9667. if ( m_class != -1 && m_class&HOM_EVO && sd->hd->homunculus.level >= 99 ) {
  9668. char i;
  9669. memset(&item_tmp, 0, sizeof(item_tmp));
  9670. item_tmp.nameid = ITEMID_STRANGE_EMBRYO;
  9671. item_tmp.identify = 1;
  9672. if( (i = pc_additem(sd, &item_tmp, 1, LOG_TYPE_SCRIPT)) ) {
  9673. clif_additem(sd, 0, 0, i);
  9674. clif_emotion(&sd->bl, E_SWT); // Fail to avoid item drop exploit.
  9675. } else {
  9676. hom_vaporize(sd, HOM_ST_MORPH);
  9677. script_pushint(st, 1);
  9678. return SCRIPT_CMD_SUCCESS;
  9679. }
  9680. } else
  9681. clif_emotion(&sd->hd->bl, E_SWT);
  9682. } else
  9683. clif_emotion(&sd->bl, E_SWT);
  9684. script_pushint(st, 0);
  9685. return SCRIPT_CMD_SUCCESS;
  9686. }
  9687. // [Zephyrus]
  9688. BUILDIN_FUNC(homunculus_shuffle)
  9689. {
  9690. TBL_PC *sd;
  9691. sd=script_rid2sd(st);
  9692. if( sd == NULL )
  9693. return SCRIPT_CMD_SUCCESS;
  9694. if(hom_is_active(sd->hd))
  9695. hom_shuffle(sd->hd);
  9696. return SCRIPT_CMD_SUCCESS;
  9697. }
  9698. /*==========================================
  9699. * Check for homunculus state.
  9700. * Return: -1 = No homunculus
  9701. * 0 = Homunculus is active
  9702. * 1 = Homunculus is vaporized (rest)
  9703. * 2 = Homunculus is in morph state
  9704. *------------------------------------------*/
  9705. BUILDIN_FUNC(checkhomcall)
  9706. {
  9707. TBL_PC *sd = script_rid2sd(st);
  9708. TBL_HOM *hd;
  9709. if( sd == NULL )
  9710. return SCRIPT_CMD_SUCCESS;
  9711. hd = sd->hd;
  9712. if( !hd )
  9713. script_pushint(st, -1);
  9714. else
  9715. script_pushint(st, hd->homunculus.vaporize);
  9716. return SCRIPT_CMD_SUCCESS;
  9717. }
  9718. //These two functions bring the eA MAPID_* class functionality to scripts.
  9719. BUILDIN_FUNC(eaclass)
  9720. {
  9721. int class_;
  9722. if( script_hasdata(st,2) )
  9723. class_ = script_getnum(st,2);
  9724. else {
  9725. TBL_PC *sd;
  9726. if (!script_charid2sd(3,sd)) {
  9727. script_pushint(st,-1);
  9728. return SCRIPT_CMD_SUCCESS;
  9729. }
  9730. class_ = sd->status.class_;
  9731. }
  9732. script_pushint(st,pc_jobid2mapid(class_));
  9733. return SCRIPT_CMD_SUCCESS;
  9734. }
  9735. BUILDIN_FUNC(roclass)
  9736. {
  9737. int class_ =script_getnum(st,2);
  9738. int sex;
  9739. if( script_hasdata(st,3) )
  9740. sex = script_getnum(st,3);
  9741. else {
  9742. TBL_PC *sd;
  9743. if (st->rid && (sd=script_rid2sd(st)))
  9744. sex = sd->status.sex;
  9745. else
  9746. sex = 1; //Just use male when not found.
  9747. }
  9748. script_pushint(st,pc_mapid2jobid(class_, sex));
  9749. return SCRIPT_CMD_SUCCESS;
  9750. }
  9751. /*==========================================
  9752. * Tells client to open a hatching window, used for pet incubator
  9753. *------------------------------------------*/
  9754. BUILDIN_FUNC(birthpet)
  9755. {
  9756. TBL_PC *sd;
  9757. sd=script_rid2sd(st);
  9758. if( sd == NULL )
  9759. return SCRIPT_CMD_SUCCESS;
  9760. if( sd->status.pet_id )
  9761. {// do not send egg list, when you already have a pet
  9762. return SCRIPT_CMD_SUCCESS;
  9763. }
  9764. clif_sendegg(sd);
  9765. return SCRIPT_CMD_SUCCESS;
  9766. }
  9767. /**
  9768. * resetlvl <action type>{,<char_id>};
  9769. * @param action_type:
  9770. * 1 : make like after rebirth
  9771. * 2 : blvl,jlvl=1, skillpoint=0
  9772. * 3 : don't reset skill, blvl=1
  9773. * 4 : jlvl=0
  9774. * @author AppleGirl
  9775. **/
  9776. BUILDIN_FUNC(resetlvl)
  9777. {
  9778. TBL_PC *sd;
  9779. int type=script_getnum(st,2);
  9780. if (!script_charid2sd(3,sd))
  9781. return SCRIPT_CMD_FAILURE;
  9782. pc_resetlvl(sd,type);
  9783. return SCRIPT_CMD_SUCCESS;
  9784. }
  9785. /**
  9786. * Reset a player status point
  9787. * resetstatus({<char_id>});
  9788. **/
  9789. BUILDIN_FUNC(resetstatus)
  9790. {
  9791. TBL_PC *sd;
  9792. if (!script_charid2sd(2,sd))
  9793. return SCRIPT_CMD_FAILURE;
  9794. pc_resetstate(sd);
  9795. return SCRIPT_CMD_SUCCESS;
  9796. }
  9797. /**
  9798. * Reset player's skill
  9799. * resetskill({<char_id>});
  9800. **/
  9801. BUILDIN_FUNC(resetskill)
  9802. {
  9803. TBL_PC *sd;
  9804. if (!script_charid2sd(2,sd))
  9805. return SCRIPT_CMD_FAILURE;
  9806. pc_resetskill(sd,1);
  9807. return SCRIPT_CMD_SUCCESS;
  9808. }
  9809. /**
  9810. * Counts total amount of skill points.
  9811. * skillpointcount({<char_id>})
  9812. **/
  9813. BUILDIN_FUNC(skillpointcount)
  9814. {
  9815. TBL_PC *sd;
  9816. if (!script_charid2sd(2,sd))
  9817. return SCRIPT_CMD_FAILURE;
  9818. script_pushint(st,sd->status.skill_point + pc_resetskill(sd,2));
  9819. return SCRIPT_CMD_SUCCESS;
  9820. }
  9821. /*==========================================
  9822. *
  9823. *------------------------------------------*/
  9824. BUILDIN_FUNC(changebase)
  9825. {
  9826. TBL_PC *sd=NULL;
  9827. int vclass;
  9828. if( script_hasdata(st,3) )
  9829. sd=map_id2sd(script_getnum(st,3));
  9830. else
  9831. sd=script_rid2sd(st);
  9832. if(sd == NULL)
  9833. return SCRIPT_CMD_SUCCESS;
  9834. vclass = script_getnum(st,2);
  9835. if(vclass == JOB_WEDDING)
  9836. {
  9837. if (!battle_config.wedding_modifydisplay || //Do not show the wedding sprites
  9838. sd->class_&JOBL_BABY //Baby classes screw up when showing wedding sprites. [Skotlex] They don't seem to anymore.
  9839. )
  9840. return SCRIPT_CMD_SUCCESS;
  9841. }
  9842. if(!sd->disguise && vclass != sd->vd.class_) {
  9843. status_set_viewdata(&sd->bl, vclass);
  9844. //Updated client view. Base, Weapon and Cloth Colors.
  9845. clif_changelook(&sd->bl,LOOK_BASE,sd->vd.class_);
  9846. clif_changelook(&sd->bl,LOOK_WEAPON,sd->status.weapon);
  9847. if (sd->vd.cloth_color)
  9848. clif_changelook(&sd->bl,LOOK_CLOTHES_COLOR,sd->vd.cloth_color);
  9849. if (sd->vd.body_style)
  9850. clif_changelook(&sd->bl,LOOK_BODY2,sd->vd.body_style);
  9851. clif_skillinfoblock(sd);
  9852. }
  9853. return SCRIPT_CMD_SUCCESS;
  9854. }
  9855. /**
  9856. * Change account sex and unequip all item and request for a changesex to char-serv
  9857. * changesex({<char_id>});
  9858. */
  9859. BUILDIN_FUNC(changesex)
  9860. {
  9861. int i;
  9862. TBL_PC *sd = NULL;
  9863. if (!script_charid2sd(2,sd))
  9864. return SCRIPT_CMD_FAILURE;
  9865. pc_resetskill(sd,4);
  9866. // to avoid any problem with equipment and invalid sex, equipment is unequiped.
  9867. for(i = 0; i < EQI_MAX; i++) {
  9868. if (sd->equip_index[i] >= 0)
  9869. pc_unequipitem(sd, sd->equip_index[i], 3);
  9870. }
  9871. chrif_changesex(sd, true);
  9872. return SCRIPT_CMD_SUCCESS;
  9873. }
  9874. /**
  9875. * Change character's sex and unequip all item and request for a changesex to char-serv
  9876. * changecharsex({<char_id>});
  9877. */
  9878. BUILDIN_FUNC(changecharsex)
  9879. {
  9880. #if PACKETVER >= 20141016
  9881. int i;
  9882. TBL_PC *sd = NULL;
  9883. if (!script_charid2sd(2,sd))
  9884. return SCRIPT_CMD_FAILURE;
  9885. pc_resetskill(sd,4);
  9886. // to avoid any problem with equipment and invalid sex, equipment is unequiped.
  9887. for (i = 0; i < EQI_MAX; i++) {
  9888. if (sd->equip_index[i] >= 0)
  9889. pc_unequipitem(sd, sd->equip_index[i], 3);
  9890. }
  9891. chrif_changesex(sd, false);
  9892. return SCRIPT_CMD_SUCCESS;
  9893. #else
  9894. return SCRIPT_CMD_FAILURE;
  9895. #endif
  9896. }
  9897. /*==========================================
  9898. * Works like 'announce' but outputs in the common chat window
  9899. *------------------------------------------*/
  9900. BUILDIN_FUNC(globalmes)
  9901. {
  9902. struct block_list *bl = map_id2bl(st->oid);
  9903. struct npc_data *nd = (struct npc_data *)bl;
  9904. const char *name=NULL,*mes;
  9905. mes=script_getstr(st,2);
  9906. if(mes==NULL)
  9907. return SCRIPT_CMD_SUCCESS;
  9908. if(script_hasdata(st,3)){ // npc name to display
  9909. name=script_getstr(st,3);
  9910. } else {
  9911. name=nd->name; //use current npc name
  9912. }
  9913. npc_globalmessage(name,mes); // broadcast to all players connected
  9914. return SCRIPT_CMD_SUCCESS;
  9915. }
  9916. /////////////////////////////////////////////////////////////////////
  9917. // NPC waiting room (chat room)
  9918. //
  9919. /// Creates a waiting room (chat room) for this npc.
  9920. ///
  9921. /// waitingroom "<title>",<limit>{,"<event>"{,<trigger>{,<zeny>{,<minlvl>{,<maxlvl>}}}}};
  9922. BUILDIN_FUNC(waitingroom)
  9923. {
  9924. struct npc_data* nd;
  9925. const char* title = script_getstr(st, 2);
  9926. int limit = script_getnum(st, 3);
  9927. const char* ev = script_hasdata(st,4) ? script_getstr(st,4) : "";
  9928. int trigger = script_hasdata(st,5) ? script_getnum(st,5) : limit;
  9929. int zeny = script_hasdata(st,6) ? script_getnum(st,6) : 0;
  9930. int minLvl = script_hasdata(st,7) ? script_getnum(st,7) : 1;
  9931. int maxLvl = script_hasdata(st,8) ? script_getnum(st,8) : MAX_LEVEL;
  9932. nd = (struct npc_data *)map_id2bl(st->oid);
  9933. if( nd != NULL )
  9934. chat_createnpcchat(nd, title, limit, 1, trigger, ev, zeny, minLvl, maxLvl);
  9935. return SCRIPT_CMD_SUCCESS;
  9936. }
  9937. /// Removes the waiting room of the current or target npc.
  9938. ///
  9939. /// delwaitingroom "<npc_name>";
  9940. /// delwaitingroom;
  9941. BUILDIN_FUNC(delwaitingroom)
  9942. {
  9943. struct npc_data* nd;
  9944. if( script_hasdata(st,2) )
  9945. nd = npc_name2id(script_getstr(st, 2));
  9946. else
  9947. nd = (struct npc_data *)map_id2bl(st->oid);
  9948. if( nd != NULL )
  9949. chat_deletenpcchat(nd);
  9950. return SCRIPT_CMD_SUCCESS;
  9951. }
  9952. /// Kick the specified player from the waiting room of the target npc.
  9953. ///
  9954. /// waitingroomkick "<npc_name>", <kickusername>;
  9955. BUILDIN_FUNC(waitingroomkick)
  9956. {
  9957. struct npc_data* nd;
  9958. struct chat_data* cd;
  9959. const char* kickusername;
  9960. nd = npc_name2id(script_getstr(st,2));
  9961. kickusername = script_getstr(st,3);
  9962. if( nd != NULL && (cd=(struct chat_data *)map_id2bl(nd->chat_id)) != NULL )
  9963. chat_npckickchat(cd, kickusername);
  9964. return SCRIPT_CMD_SUCCESS;
  9965. }
  9966. /// Get Users in waiting room and stores gids in .@waitingroom_users[]
  9967. /// Num users stored in .@waitingroom_usercount
  9968. ///
  9969. /// getwaitingroomusers "<npc_name>";
  9970. BUILDIN_FUNC(getwaitingroomusers)
  9971. {
  9972. struct npc_data* nd;
  9973. struct chat_data* cd;
  9974. int i, j=0;
  9975. if( script_hasdata(st,2) )
  9976. nd = npc_name2id(script_getstr(st, 2));
  9977. else
  9978. nd = (struct npc_data *)map_id2bl(st->oid);
  9979. if( nd != NULL && (cd=(struct chat_data *)map_id2bl(nd->chat_id)) != NULL ) {
  9980. for(i = 0; i < cd->users; ++i) {
  9981. setd_sub(st, NULL, ".@waitingroom_users", j, (void *)__64BPRTSIZE(cd->usersd[i]->status.account_id), NULL);
  9982. j++;
  9983. }
  9984. setd_sub(st, NULL, ".@waitingroom_usercount", 0, (void *)__64BPRTSIZE(j), NULL);
  9985. }
  9986. return SCRIPT_CMD_SUCCESS;
  9987. }
  9988. /// Kicks all the players from the waiting room of the current or target npc.
  9989. ///
  9990. /// kickwaitingroomall "<npc_name>";
  9991. /// kickwaitingroomall;
  9992. BUILDIN_FUNC(waitingroomkickall)
  9993. {
  9994. struct npc_data* nd;
  9995. struct chat_data* cd;
  9996. if( script_hasdata(st,2) )
  9997. nd = npc_name2id(script_getstr(st,2));
  9998. else
  9999. nd = (struct npc_data *)map_id2bl(st->oid);
  10000. if( nd != NULL && (cd=(struct chat_data *)map_id2bl(nd->chat_id)) != NULL )
  10001. chat_npckickall(cd);
  10002. return SCRIPT_CMD_SUCCESS;
  10003. }
  10004. /// Enables the waiting room event of the current or target npc.
  10005. ///
  10006. /// enablewaitingroomevent "<npc_name>";
  10007. /// enablewaitingroomevent;
  10008. BUILDIN_FUNC(enablewaitingroomevent)
  10009. {
  10010. struct npc_data* nd;
  10011. struct chat_data* cd;
  10012. if( script_hasdata(st,2) )
  10013. nd = npc_name2id(script_getstr(st, 2));
  10014. else
  10015. nd = (struct npc_data *)map_id2bl(st->oid);
  10016. if( nd != NULL && (cd=(struct chat_data *)map_id2bl(nd->chat_id)) != NULL )
  10017. chat_enableevent(cd);
  10018. return SCRIPT_CMD_SUCCESS;
  10019. }
  10020. /// Disables the waiting room event of the current or target npc.
  10021. ///
  10022. /// disablewaitingroomevent "<npc_name>";
  10023. /// disablewaitingroomevent;
  10024. BUILDIN_FUNC(disablewaitingroomevent)
  10025. {
  10026. struct npc_data *nd;
  10027. struct chat_data *cd;
  10028. if( script_hasdata(st,2) )
  10029. nd = npc_name2id(script_getstr(st, 2));
  10030. else
  10031. nd = (struct npc_data *)map_id2bl(st->oid);
  10032. if( nd != NULL && (cd=(struct chat_data *)map_id2bl(nd->chat_id)) != NULL )
  10033. chat_disableevent(cd);
  10034. return SCRIPT_CMD_SUCCESS;
  10035. }
  10036. /// Returns info on the waiting room of the current or target npc.
  10037. /// Returns -1 if the type unknown
  10038. /// <type>=0 : current number of users
  10039. /// <type>=1 : maximum number of users allowed
  10040. /// <type>=2 : the number of users that trigger the event
  10041. /// <type>=3 : if the trigger is disabled
  10042. /// <type>=4 : the title of the waiting room
  10043. /// <type>=5 : the password of the waiting room
  10044. /// <type>=16 : the name of the waiting room event
  10045. /// <type>=32 : if the waiting room is full
  10046. /// <type>=33 : if there are enough users to trigger the event
  10047. ///
  10048. /// getwaitingroomstate(<type>,"<npc_name>") -> <info>
  10049. /// getwaitingroomstate(<type>) -> <info>
  10050. BUILDIN_FUNC(getwaitingroomstate)
  10051. {
  10052. struct npc_data *nd;
  10053. struct chat_data *cd;
  10054. int type;
  10055. type = script_getnum(st,2);
  10056. if( script_hasdata(st,3) )
  10057. nd = npc_name2id(script_getstr(st, 3));
  10058. else
  10059. nd = (struct npc_data *)map_id2bl(st->oid);
  10060. if( nd == NULL || (cd=(struct chat_data *)map_id2bl(nd->chat_id)) == NULL )
  10061. {
  10062. script_pushint(st, -1);
  10063. return SCRIPT_CMD_SUCCESS;
  10064. }
  10065. switch(type)
  10066. {
  10067. case 0: script_pushint(st, cd->users); break;
  10068. case 1: script_pushint(st, cd->limit); break;
  10069. case 2: script_pushint(st, cd->trigger&0x7f); break;
  10070. case 3: script_pushint(st, ((cd->trigger&0x80)!=0)); break;
  10071. case 4: script_pushstrcopy(st, cd->title); break;
  10072. case 5: script_pushstrcopy(st, cd->pass); break;
  10073. case 16: script_pushstrcopy(st, cd->npc_event);break;
  10074. case 32: script_pushint(st, (cd->users >= cd->limit)); break;
  10075. case 33: script_pushint(st, (cd->users >= cd->trigger)); break;
  10076. default: script_pushint(st, -1); break;
  10077. }
  10078. return SCRIPT_CMD_SUCCESS;
  10079. }
  10080. /// Warps the trigger or target amount of players to the target map and position.
  10081. /// Players are automatically removed from the waiting room.
  10082. /// Those waiting the longest will get warped first.
  10083. /// The target map can be "Random" for a random position in the current map,
  10084. /// and "SavePoint" for the savepoint map+position.
  10085. /// The map flag noteleport of the current map is only considered when teleporting to the savepoint.
  10086. ///
  10087. /// The id's of the teleported players are put into the array $@warpwaitingpc[]
  10088. /// The total number of teleported players is put into $@warpwaitingpcnum
  10089. ///
  10090. /// warpwaitingpc "<map name>",<x>,<y>,<number of players>;
  10091. /// warpwaitingpc "<map name>",<x>,<y>;
  10092. BUILDIN_FUNC(warpwaitingpc)
  10093. {
  10094. int x;
  10095. int y;
  10096. int i;
  10097. int n;
  10098. const char* map_name;
  10099. struct npc_data* nd;
  10100. struct chat_data* cd;
  10101. nd = (struct npc_data *)map_id2bl(st->oid);
  10102. if( nd == NULL || (cd=(struct chat_data *)map_id2bl(nd->chat_id)) == NULL )
  10103. return SCRIPT_CMD_SUCCESS;
  10104. map_name = script_getstr(st,2);
  10105. x = script_getnum(st,3);
  10106. y = script_getnum(st,4);
  10107. n = cd->trigger&0x7f;
  10108. if( script_hasdata(st,5) )
  10109. n = script_getnum(st,5);
  10110. for( i = 0; i < n && cd->users > 0; i++ )
  10111. {
  10112. TBL_PC* sd = cd->usersd[0];
  10113. if( strcmp(map_name,"SavePoint") == 0 && map[sd->bl.m].flag.noteleport )
  10114. {// can't teleport on this map
  10115. break;
  10116. }
  10117. if( cd->zeny )
  10118. {// fee set
  10119. if( (uint32)sd->status.zeny < cd->zeny )
  10120. {// no zeny to cover set fee
  10121. break;
  10122. }
  10123. pc_payzeny(sd, cd->zeny, LOG_TYPE_NPC, NULL);
  10124. }
  10125. mapreg_setreg(reference_uid(add_str("$@warpwaitingpc"), i), sd->bl.id);
  10126. if( strcmp(map_name,"Random") == 0 )
  10127. pc_randomwarp(sd,CLR_TELEPORT);
  10128. else if( strcmp(map_name,"SavePoint") == 0 )
  10129. pc_setpos(sd, sd->status.save_point.map, sd->status.save_point.x, sd->status.save_point.y, CLR_TELEPORT);
  10130. else
  10131. pc_setpos(sd, mapindex_name2id(map_name), x, y, CLR_OUTSIGHT);
  10132. }
  10133. mapreg_setreg(add_str("$@warpwaitingpcnum"), i);
  10134. return SCRIPT_CMD_SUCCESS;
  10135. }
  10136. /////////////////////////////////////////////////////////////////////
  10137. // ...
  10138. //
  10139. /// Detaches a character from a script.
  10140. ///
  10141. /// @param st Script state to detach the character from.
  10142. static void script_detach_rid(struct script_state* st)
  10143. {
  10144. if(st->rid)
  10145. {
  10146. script_detach_state(st, false);
  10147. st->rid = 0;
  10148. }
  10149. }
  10150. /*=========================================================================
  10151. * Attaches a set of RIDs to the current script. [digitalhamster]
  10152. * addrid(<type>{,<flag>{,<parameters>}});
  10153. * <type>:
  10154. * 0 : All players in the server.
  10155. * 1 : All players in the map of the invoking player, or the invoking NPC if no player is attached.
  10156. * 2 : Party members of a specified party ID.
  10157. * [ Parameters: <party id> ]
  10158. * 3 : Guild members of a specified guild ID.
  10159. * [ Parameters: <guild id> ]
  10160. * 4 : All players in a specified area of the map of the invoking player (or NPC).
  10161. * [ Parameters: <x0>,<y0>,<x1>,<y1> ]
  10162. * 5 : All players in the map.
  10163. * [ Parameters: "<map name>" ]
  10164. * Account ID: The specified account ID.
  10165. * <flag>:
  10166. * 0 : Players are always attached. (default)
  10167. * 1 : Players currently running another script will not be attached.
  10168. *-------------------------------------------------------------------------*/
  10169. static int buildin_addrid_sub(struct block_list *bl,va_list ap)
  10170. {
  10171. int forceflag;
  10172. struct map_session_data *sd = (TBL_PC *)bl;
  10173. struct script_state* st;
  10174. st = va_arg(ap,struct script_state*);
  10175. forceflag = va_arg(ap,int);
  10176. if(!forceflag || !sd->st)
  10177. if(sd->status.account_id != st->rid)
  10178. run_script(st->script,st->pos,sd->status.account_id,st->oid);
  10179. return 0;
  10180. }
  10181. BUILDIN_FUNC(addrid)
  10182. {
  10183. struct s_mapiterator* iter;
  10184. struct block_list *bl;
  10185. TBL_PC *sd;
  10186. if(st->rid < 1) {
  10187. st->state = END;
  10188. bl = map_id2bl(st->oid);
  10189. } else
  10190. bl = map_id2bl(st->rid); //if run without rid it'd error,also oid if npc, else rid for map
  10191. iter = mapit_getallusers();
  10192. switch(script_getnum(st,2)) {
  10193. case 0:
  10194. for( sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); sd = (TBL_PC*)mapit_next(iter)) {
  10195. if(!script_getnum(st,3) || !sd->st)
  10196. if(sd->status.account_id != st->rid) //attached player already runs.
  10197. run_script(st->script,st->pos,sd->status.account_id,st->oid);
  10198. }
  10199. break;
  10200. case 1:
  10201. for( sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); sd = (TBL_PC*)mapit_next(iter)) {
  10202. if(!script_getnum(st,3) || !sd->st)
  10203. if((sd->bl.m == bl->m) && (sd->status.account_id != st->rid))
  10204. run_script(st->script,st->pos,sd->status.account_id,st->oid);
  10205. }
  10206. break;
  10207. case 2:
  10208. if(script_getnum(st,4) == 0) {
  10209. script_pushint(st,0);
  10210. return SCRIPT_CMD_SUCCESS;
  10211. }
  10212. for( sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); sd = (TBL_PC*)mapit_next(iter)) {
  10213. if(!script_getnum(st,3) || !sd->st)
  10214. if((sd->status.account_id != st->rid) && (sd->status.party_id == script_getnum(st,4))) //attached player already runs.
  10215. run_script(st->script,st->pos,sd->status.account_id,st->oid);
  10216. }
  10217. break;
  10218. case 3:
  10219. if(script_getnum(st,4) == 0) {
  10220. script_pushint(st,0);
  10221. return SCRIPT_CMD_SUCCESS;
  10222. }
  10223. for( sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); sd = (TBL_PC*)mapit_next(iter)) {
  10224. if(!script_getnum(st,3) || !sd->st)
  10225. if((sd->status.account_id != st->rid) && (sd->status.guild_id == script_getnum(st,4))) //attached player already runs.
  10226. run_script(st->script,st->pos,sd->status.account_id,st->oid);
  10227. }
  10228. break;
  10229. case 4:
  10230. map_foreachinarea(buildin_addrid_sub,
  10231. bl->m,script_getnum(st,4),script_getnum(st,5),script_getnum(st,6),script_getnum(st,7),BL_PC,
  10232. st,script_getnum(st,3));//4-x0 , 5-y0 , 6-x1, 7-y1
  10233. break;
  10234. case 5:
  10235. if (script_getstr(st, 4) == NULL) {
  10236. script_pushint(st, 0);
  10237. return SCRIPT_CMD_FAILURE;
  10238. }
  10239. if (map_mapname2mapid(script_getstr(st, 4)) < 0) {
  10240. script_pushint(st, 0);
  10241. return SCRIPT_CMD_FAILURE;
  10242. }
  10243. map_foreachinmap(buildin_addrid_sub, map_mapname2mapid(script_getstr(st, 4)), BL_PC, st, script_getnum(st, 3));
  10244. break;
  10245. default:
  10246. if((map_id2sd(script_getnum(st,2))) == NULL) { // Player not found.
  10247. script_pushint(st,0);
  10248. return SCRIPT_CMD_SUCCESS;
  10249. }
  10250. if(!script_getnum(st,3) || !map_id2sd(script_getnum(st,2))->st) {
  10251. run_script(st->script,st->pos,script_getnum(st,2),st->oid);
  10252. script_pushint(st,1);
  10253. }
  10254. return SCRIPT_CMD_SUCCESS;
  10255. }
  10256. mapit_free(iter);
  10257. script_pushint(st,1);
  10258. return SCRIPT_CMD_SUCCESS;
  10259. }
  10260. /*==========================================
  10261. * Attach sd char id to script and detach current one if any
  10262. *------------------------------------------*/
  10263. BUILDIN_FUNC(attachrid)
  10264. {
  10265. int rid = script_getnum(st,2);
  10266. if (map_id2sd(rid) != NULL) {
  10267. script_detach_rid(st);
  10268. st->rid = rid;
  10269. script_attach_state(st);
  10270. script_pushint(st,1);
  10271. } else
  10272. script_pushint(st,0);
  10273. return SCRIPT_CMD_SUCCESS;
  10274. }
  10275. /*==========================================
  10276. * Detach script to rid
  10277. *------------------------------------------*/
  10278. BUILDIN_FUNC(detachrid)
  10279. {
  10280. script_detach_rid(st);
  10281. return SCRIPT_CMD_SUCCESS;
  10282. }
  10283. /*==========================================
  10284. * Chk if account connected, (and charid from account if specified)
  10285. *------------------------------------------*/
  10286. BUILDIN_FUNC(isloggedin)
  10287. {
  10288. TBL_PC* sd = map_id2sd(script_getnum(st,2));
  10289. if (script_hasdata(st,3) && sd &&
  10290. sd->status.char_id != script_getnum(st,3))
  10291. sd = NULL;
  10292. push_val(st->stack,C_INT,sd!=NULL);
  10293. return SCRIPT_CMD_SUCCESS;
  10294. }
  10295. /*==========================================
  10296. *
  10297. *------------------------------------------*/
  10298. BUILDIN_FUNC(setmapflagnosave)
  10299. {
  10300. int16 m,x,y;
  10301. unsigned short mapindex;
  10302. const char *str,*str2;
  10303. str=script_getstr(st,2);
  10304. str2=script_getstr(st,3);
  10305. x=script_getnum(st,4);
  10306. y=script_getnum(st,5);
  10307. m = map_mapname2mapid(str);
  10308. mapindex = mapindex_name2id(str2);
  10309. if(m >= 0 && mapindex) {
  10310. map[m].flag.nosave=1;
  10311. map[m].save.map=mapindex;
  10312. map[m].save.x=x;
  10313. map[m].save.y=y;
  10314. }
  10315. return SCRIPT_CMD_SUCCESS;
  10316. }
  10317. BUILDIN_FUNC(getmapflag)
  10318. {
  10319. int16 m,i;
  10320. const char *str;
  10321. str=script_getstr(st,2);
  10322. i=script_getnum(st,3);
  10323. m = map_mapname2mapid(str);
  10324. if(m >= 0) {
  10325. switch(i) {
  10326. case MF_NOMEMO: script_pushint(st,map[m].flag.nomemo); break;
  10327. case MF_NOTELEPORT: script_pushint(st,map[m].flag.noteleport); break;
  10328. case MF_NOSAVE: script_pushint(st,map[m].flag.nosave); break;
  10329. case MF_NOBRANCH: script_pushint(st,map[m].flag.nobranch); break;
  10330. case MF_NOPENALTY: script_pushint(st,map[m].flag.noexppenalty); break;
  10331. case MF_NOZENYPENALTY: script_pushint(st,map[m].flag.nozenypenalty); break;
  10332. case MF_PVP: script_pushint(st,map[m].flag.pvp); break;
  10333. case MF_PVP_NOPARTY: script_pushint(st,map[m].flag.pvp_noparty); break;
  10334. case MF_PVP_NOGUILD: script_pushint(st,map[m].flag.pvp_noguild); break;
  10335. case MF_GVG: script_pushint(st,map[m].flag.gvg); break;
  10336. case MF_GVG_NOPARTY: script_pushint(st,map[m].flag.gvg_noparty); break;
  10337. case MF_NOTRADE: script_pushint(st,map[m].flag.notrade); break;
  10338. case MF_NOSKILL: script_pushint(st,map[m].flag.noskill); break;
  10339. case MF_NOWARP: script_pushint(st,map[m].flag.nowarp); break;
  10340. case MF_PARTYLOCK: script_pushint(st,map[m].flag.partylock); break;
  10341. case MF_NOICEWALL: script_pushint(st,map[m].flag.noicewall); break;
  10342. case MF_SNOW: script_pushint(st,map[m].flag.snow); break;
  10343. case MF_FOG: script_pushint(st,map[m].flag.fog); break;
  10344. case MF_SAKURA: script_pushint(st,map[m].flag.sakura); break;
  10345. case MF_LEAVES: script_pushint(st,map[m].flag.leaves); break;
  10346. case MF_NOGO: script_pushint(st,map[m].flag.nogo); break;
  10347. case MF_CLOUDS: script_pushint(st,map[m].flag.clouds); break;
  10348. case MF_CLOUDS2: script_pushint(st,map[m].flag.clouds2); break;
  10349. case MF_FIREWORKS: script_pushint(st,map[m].flag.fireworks); break;
  10350. case MF_GVG_CASTLE: script_pushint(st,map[m].flag.gvg_castle); break;
  10351. case MF_GVG_DUNGEON: script_pushint(st,map[m].flag.gvg_dungeon); break;
  10352. case MF_NIGHTENABLED: script_pushint(st,map[m].flag.nightenabled); break;
  10353. case MF_NOBASEEXP: script_pushint(st,map[m].flag.nobaseexp); break;
  10354. case MF_NOJOBEXP: script_pushint(st,map[m].flag.nojobexp); break;
  10355. case MF_NOMOBLOOT: script_pushint(st,map[m].flag.nomobloot); break;
  10356. case MF_NOMVPLOOT: script_pushint(st,map[m].flag.nomvploot); break;
  10357. case MF_NORETURN: script_pushint(st,map[m].flag.noreturn); break;
  10358. case MF_NOWARPTO: script_pushint(st,map[m].flag.nowarpto); break;
  10359. case MF_NIGHTMAREDROP: script_pushint(st,map[m].flag.pvp_nightmaredrop); break;
  10360. case MF_RESTRICTED: script_pushint(st,map[m].flag.restricted); break;
  10361. case MF_NOCOMMAND: script_pushint(st,map[m].nocommand); break;
  10362. case MF_NODROP: script_pushint(st,map[m].flag.nodrop); break;
  10363. case MF_JEXP: script_pushint(st,map[m].adjust.jexp); break;
  10364. case MF_BEXP: script_pushint(st,map[m].adjust.bexp); break;
  10365. case MF_NOVENDING: script_pushint(st,map[m].flag.novending); break;
  10366. case MF_LOADEVENT: script_pushint(st,map[m].flag.loadevent); break;
  10367. case MF_NOCHAT: script_pushint(st,map[m].flag.nochat); break;
  10368. case MF_NOEXPPENALTY: script_pushint(st,map[m].flag.noexppenalty ); break;
  10369. case MF_GUILDLOCK: script_pushint(st,map[m].flag.guildlock); break;
  10370. case MF_TOWN: script_pushint(st,map[m].flag.town); break;
  10371. case MF_AUTOTRADE: script_pushint(st,map[m].flag.autotrade); break;
  10372. case MF_ALLOWKS: script_pushint(st,map[m].flag.allowks); break;
  10373. case MF_MONSTER_NOTELEPORT: script_pushint(st,map[m].flag.monster_noteleport); break;
  10374. case MF_PVP_NOCALCRANK: script_pushint(st,map[m].flag.pvp_nocalcrank); break;
  10375. case MF_BATTLEGROUND: script_pushint(st,map[m].flag.battleground); break;
  10376. case MF_RESET: script_pushint(st,map[m].flag.reset); break;
  10377. case MF_CHANNELAUTOJOIN: script_pushint(st,map[m].flag.chmautojoin); break;
  10378. case MF_NOUSECART: script_pushint(st,map[m].flag.nousecart); break;
  10379. case MF_NOITEMCONSUMPTION: script_pushint(st,map[m].flag.noitemconsumption); break;
  10380. case MF_SUMSTARTMIRACLE: script_pushint(st,map[m].flag.nosumstarmiracle); break;
  10381. case MF_NOMINEEFFECT: script_pushint(st,map[m].flag.nomineeffect); break;
  10382. case MF_NOLOCKON: script_pushint(st,map[m].flag.nolockon); break;
  10383. case MF_NOTOMB: script_pushint(st,map[m].flag.notomb); break;
  10384. #ifdef ADJUST_SKILL_DAMAGE
  10385. case MF_SKILL_DAMAGE:
  10386. {
  10387. int ret_val=0, type=0;
  10388. FETCH(4,type);
  10389. switch (type) {
  10390. case 1: ret_val = map[m].adjust.damage.pc; break;
  10391. case 2: ret_val = map[m].adjust.damage.mob; break;
  10392. case 3: ret_val = map[m].adjust.damage.boss; break;
  10393. case 4: ret_val = map[m].adjust.damage.other; break;
  10394. case 5: ret_val = map[m].adjust.damage.caster; break;
  10395. default: ret_val = map[m].flag.skill_damage; break;
  10396. }
  10397. script_pushint(st,ret_val); break;
  10398. } break;
  10399. #endif
  10400. }
  10401. }
  10402. return SCRIPT_CMD_SUCCESS;
  10403. }
  10404. /* pvp timer handling */
  10405. static int script_mapflag_pvp_sub(struct block_list *bl,va_list ap) {
  10406. TBL_PC* sd = (TBL_PC*)bl;
  10407. if (sd->pvp_timer == INVALID_TIMER) {
  10408. sd->pvp_timer = add_timer(gettick() + 200, pc_calc_pvprank_timer, sd->bl.id, 0);
  10409. sd->pvp_rank = 0;
  10410. sd->pvp_lastusers = 0;
  10411. sd->pvp_point = 5;
  10412. sd->pvp_won = 0;
  10413. sd->pvp_lost = 0;
  10414. }
  10415. clif_map_property(sd, MAPPROPERTY_FREEPVPZONE);
  10416. clif_maptypeproperty2(&sd->bl,SELF);
  10417. return 0;
  10418. }
  10419. BUILDIN_FUNC(setmapflag)
  10420. {
  10421. int16 m,i;
  10422. const char *str;
  10423. int val=0;
  10424. str=script_getstr(st,2);
  10425. i=script_getnum(st,3);
  10426. FETCH(4,val);
  10427. m = map_mapname2mapid(str);
  10428. if(m >= 0) {
  10429. switch(i) {
  10430. case MF_NOMEMO: map[m].flag.nomemo = 1; break;
  10431. case MF_NOTELEPORT: map[m].flag.noteleport = 1; break;
  10432. case MF_NOSAVE: map[m].flag.nosave = 1; break;
  10433. case MF_NOBRANCH: map[m].flag.nobranch = 1; break;
  10434. case MF_NOPENALTY: map[m].flag.noexppenalty = 1; map[m].flag.nozenypenalty = 1; break;
  10435. case MF_NOZENYPENALTY: map[m].flag.nozenypenalty = 1; break;
  10436. case MF_PVP:
  10437. map[m].flag.pvp = 1;
  10438. if( !battle_config.pk_mode ) {
  10439. map_foreachinmap(script_mapflag_pvp_sub,m,BL_PC);
  10440. }
  10441. break;
  10442. case MF_PVP_NOPARTY: map[m].flag.pvp_noparty = 1; break;
  10443. case MF_PVP_NOGUILD: map[m].flag.pvp_noguild = 1; break;
  10444. case MF_GVG: {
  10445. struct block_list bl;
  10446. map[m].flag.gvg = 1;
  10447. clif_map_property_mapall(m, MAPPROPERTY_AGITZONE);
  10448. bl.type = BL_NUL;
  10449. bl.m = m;
  10450. clif_maptypeproperty2(&bl,ALL_SAMEMAP);
  10451. }
  10452. break;
  10453. case MF_GVG_NOPARTY: map[m].flag.gvg_noparty = 1; break;
  10454. case MF_NOTRADE: map[m].flag.notrade = 1; break;
  10455. case MF_NOSKILL: map[m].flag.noskill = 1; break;
  10456. case MF_NOWARP: map[m].flag.nowarp = 1; break;
  10457. case MF_PARTYLOCK: map[m].flag.partylock = 1; break;
  10458. case MF_NOICEWALL: map[m].flag.noicewall = 1; break;
  10459. case MF_SNOW: map[m].flag.snow = 1; break;
  10460. case MF_FOG: map[m].flag.fog = 1; break;
  10461. case MF_SAKURA: map[m].flag.sakura = 1; break;
  10462. case MF_LEAVES: map[m].flag.leaves = 1; break;
  10463. case MF_NOGO: map[m].flag.nogo = 1; break;
  10464. case MF_CLOUDS: map[m].flag.clouds = 1; break;
  10465. case MF_CLOUDS2: map[m].flag.clouds2 = 1; break;
  10466. case MF_FIREWORKS: map[m].flag.fireworks = 1; break;
  10467. case MF_GVG_CASTLE: map[m].flag.gvg_castle = 1; break;
  10468. case MF_GVG_DUNGEON: map[m].flag.gvg_dungeon = 1; break;
  10469. case MF_NIGHTENABLED: map[m].flag.nightenabled = 1; break;
  10470. case MF_NOBASEEXP: map[m].flag.nobaseexp = 1; break;
  10471. case MF_NOJOBEXP: map[m].flag.nojobexp = 1; break;
  10472. case MF_NOMOBLOOT: map[m].flag.nomobloot = 1; break;
  10473. case MF_NOMVPLOOT: map[m].flag.nomvploot = 1; break;
  10474. case MF_NORETURN: map[m].flag.noreturn = 1; break;
  10475. case MF_NOWARPTO: map[m].flag.nowarpto = 1; break;
  10476. case MF_NIGHTMAREDROP: map[m].flag.pvp_nightmaredrop = 1; break;
  10477. case MF_RESTRICTED:
  10478. map[m].zone |= 1<<(val+1);
  10479. map[m].flag.restricted=1;
  10480. break;
  10481. case MF_NOCOMMAND: map[m].nocommand = (val <= 0) ? 100 : val; break;
  10482. case MF_NODROP: map[m].flag.nodrop = 1; break;
  10483. case MF_JEXP: map[m].adjust.jexp = (val <= 0) ? 100 : val; break;
  10484. case MF_BEXP: map[m].adjust.bexp = (val <= 0) ? 100 : val; break;
  10485. case MF_NOVENDING: map[m].flag.novending = 1; break;
  10486. case MF_LOADEVENT: map[m].flag.loadevent = 1; break;
  10487. case MF_NOCHAT: map[m].flag.nochat = 1; break;
  10488. case MF_NOEXPPENALTY: map[m].flag.noexppenalty = 1; break;
  10489. case MF_GUILDLOCK: map[m].flag.guildlock = 1; break;
  10490. case MF_TOWN: map[m].flag.town = 1; break;
  10491. case MF_AUTOTRADE: map[m].flag.autotrade = 1; break;
  10492. case MF_ALLOWKS: map[m].flag.allowks = 1; break;
  10493. case MF_MONSTER_NOTELEPORT: map[m].flag.monster_noteleport = 1; break;
  10494. case MF_PVP_NOCALCRANK: map[m].flag.pvp_nocalcrank = 1; break;
  10495. case MF_BATTLEGROUND: map[m].flag.battleground = (val <= 0 || val > 2) ? 1 : val; break;
  10496. case MF_RESET: map[m].flag.reset = 1; break;
  10497. case MF_CHANNELAUTOJOIN: map[m].flag.chmautojoin = 1 ; break;
  10498. case MF_NOUSECART: map[m].flag.nousecart = 1 ; break;
  10499. case MF_NOITEMCONSUMPTION: map[m].flag.noitemconsumption = 1 ; break;
  10500. case MF_SUMSTARTMIRACLE: map[m].flag.nosumstarmiracle = 1 ; break;
  10501. case MF_NOMINEEFFECT: map[m].flag.nomineeffect = 1 ; break;
  10502. case MF_NOLOCKON: map[m].flag.nolockon = 1 ; break;
  10503. case MF_NOTOMB: map[m].flag.notomb = 1; break;
  10504. #ifdef ADJUST_SKILL_DAMAGE
  10505. case MF_SKILL_DAMAGE:
  10506. {
  10507. int type=0;
  10508. FETCH(5,type);
  10509. switch (type) {
  10510. case 1: map[m].adjust.damage.pc = val; break;
  10511. case 2: map[m].adjust.damage.mob = val; break;
  10512. case 3: map[m].adjust.damage.boss = val; break;
  10513. case 4: map[m].adjust.damage.other = val; break;
  10514. case 5: map[m].adjust.damage.caster = val; break;
  10515. }
  10516. map[m].flag.skill_damage = 1;
  10517. } break;
  10518. #endif
  10519. }
  10520. }
  10521. return SCRIPT_CMD_SUCCESS;
  10522. }
  10523. BUILDIN_FUNC(removemapflag)
  10524. {
  10525. int16 m,i;
  10526. const char *str;
  10527. int val=0;
  10528. str=script_getstr(st,2);
  10529. i=script_getnum(st,3);
  10530. FETCH(4,val);
  10531. m = map_mapname2mapid(str);
  10532. if(m >= 0) {
  10533. switch(i) {
  10534. case MF_NOMEMO: map[m].flag.nomemo = 0; break;
  10535. case MF_NOTELEPORT: map[m].flag.noteleport = 0; break;
  10536. case MF_NOSAVE: map[m].flag.nosave = 0; break;
  10537. case MF_NOBRANCH: map[m].flag.nobranch = 0; break;
  10538. case MF_NOPENALTY: map[m].flag.noexppenalty = 0; map[m].flag.nozenypenalty = 0; break;
  10539. case MF_NOZENYPENALTY: map[m].flag.nozenypenalty = 0; break;
  10540. case MF_PVP: {
  10541. struct block_list bl;
  10542. bl.type = BL_NUL;
  10543. bl.m = m;
  10544. map[m].flag.pvp = 0;
  10545. clif_map_property_mapall(m, MAPPROPERTY_NOTHING);
  10546. clif_maptypeproperty2(&bl,ALL_SAMEMAP);
  10547. }
  10548. break;
  10549. case MF_PVP_NOPARTY: map[m].flag.pvp_noparty = 0; break;
  10550. case MF_PVP_NOGUILD: map[m].flag.pvp_noguild = 0; break;
  10551. case MF_GVG: {
  10552. struct block_list bl;
  10553. bl.type = BL_NUL;
  10554. bl.m = m;
  10555. map[m].flag.gvg = 0;
  10556. clif_map_property_mapall(m, MAPPROPERTY_NOTHING);
  10557. clif_maptypeproperty2(&bl,ALL_SAMEMAP);
  10558. }
  10559. break;
  10560. case MF_GVG_NOPARTY: map[m].flag.gvg_noparty = 0; break;
  10561. case MF_NOTRADE: map[m].flag.notrade = 0; break;
  10562. case MF_NOSKILL: map[m].flag.noskill = 0; break;
  10563. case MF_NOWARP: map[m].flag.nowarp = 0; break;
  10564. case MF_PARTYLOCK: map[m].flag.partylock = 0; break;
  10565. case MF_NOICEWALL: map[m].flag.noicewall = 0; break;
  10566. case MF_SNOW: map[m].flag.snow = 0; break;
  10567. case MF_FOG: map[m].flag.fog = 0; break;
  10568. case MF_SAKURA: map[m].flag.sakura = 0; break;
  10569. case MF_LEAVES: map[m].flag.leaves = 0; break;
  10570. case MF_NOGO: map[m].flag.nogo = 0; break;
  10571. case MF_CLOUDS: map[m].flag.clouds = 0; break;
  10572. case MF_CLOUDS2: map[m].flag.clouds2 = 0; break;
  10573. case MF_FIREWORKS: map[m].flag.fireworks = 0; break;
  10574. case MF_GVG_CASTLE: map[m].flag.gvg_castle = 0; break;
  10575. case MF_GVG_DUNGEON: map[m].flag.gvg_dungeon = 0; break;
  10576. case MF_NIGHTENABLED: map[m].flag.nightenabled = 0; break;
  10577. case MF_NOBASEEXP: map[m].flag.nobaseexp = 0; break;
  10578. case MF_NOJOBEXP: map[m].flag.nojobexp = 0; break;
  10579. case MF_NOMOBLOOT: map[m].flag.nomobloot = 0; break;
  10580. case MF_NOMVPLOOT: map[m].flag.nomvploot = 0; break;
  10581. case MF_NORETURN: map[m].flag.noreturn = 0; break;
  10582. case MF_NOWARPTO: map[m].flag.nowarpto = 0; break;
  10583. case MF_NIGHTMAREDROP: map[m].flag.pvp_nightmaredrop = 0; break;
  10584. case MF_RESTRICTED:
  10585. map[m].zone ^= 1<<(val+1);
  10586. if (map[m].zone == 0){
  10587. map[m].flag.restricted=0;
  10588. }
  10589. break;
  10590. case MF_NOCOMMAND: map[m].nocommand = 0; break;
  10591. case MF_NODROP: map[m].flag.nodrop = 0; break;
  10592. case MF_JEXP: map[m].adjust.jexp = 0; break;
  10593. case MF_BEXP: map[m].adjust.bexp = 0; break;
  10594. case MF_NOVENDING: map[m].flag.novending = 0; break;
  10595. case MF_LOADEVENT: map[m].flag.loadevent = 0; break;
  10596. case MF_NOCHAT: map[m].flag.nochat = 0; break;
  10597. case MF_NOEXPPENALTY: map[m].flag.noexppenalty = 0; break;
  10598. case MF_GUILDLOCK: map[m].flag.guildlock = 0; break;
  10599. case MF_TOWN: map[m].flag.town = 0; break;
  10600. case MF_AUTOTRADE: map[m].flag.autotrade = 0; break;
  10601. case MF_ALLOWKS: map[m].flag.allowks = 0; break;
  10602. case MF_MONSTER_NOTELEPORT: map[m].flag.monster_noteleport = 0; break;
  10603. case MF_PVP_NOCALCRANK: map[m].flag.pvp_nocalcrank = 0; break;
  10604. case MF_BATTLEGROUND: map[m].flag.battleground = 0; break;
  10605. case MF_RESET: map[m].flag.reset = 0; break;
  10606. case MF_CHANNELAUTOJOIN: map[m].flag.chmautojoin = 0 ; break;
  10607. case MF_NOUSECART: map[m].flag.nousecart = 0 ; break;
  10608. case MF_NOITEMCONSUMPTION: map[m].flag.noitemconsumption = 0 ; break;
  10609. case MF_SUMSTARTMIRACLE: map[m].flag.nosumstarmiracle = 0 ; break;
  10610. case MF_NOMINEEFFECT: map[m].flag.nomineeffect = 0 ; break;
  10611. case MF_NOLOCKON: map[m].flag.nolockon = 0 ; break;
  10612. case MF_NOTOMB: map[m].flag.notomb = 0; break;
  10613. #ifdef ADJUST_SKILL_DAMAGE
  10614. case MF_SKILL_DAMAGE:
  10615. {
  10616. map[m].flag.skill_damage = 0;
  10617. memset(&map[m].adjust.damage, 0, sizeof(map[m].adjust.damage));
  10618. if (map[m].skill_damage.count)
  10619. map_skill_damage_free(&map[m]);
  10620. } break;
  10621. #endif
  10622. }
  10623. }
  10624. return SCRIPT_CMD_SUCCESS;
  10625. }
  10626. BUILDIN_FUNC(pvpon)
  10627. {
  10628. int16 m;
  10629. const char *str;
  10630. TBL_PC* sd = NULL;
  10631. struct s_mapiterator* iter;
  10632. struct block_list bl;
  10633. str = script_getstr(st,2);
  10634. m = map_mapname2mapid(str);
  10635. if( m < 0 || map[m].flag.pvp )
  10636. return SCRIPT_CMD_SUCCESS; // nothing to do
  10637. map[m].flag.pvp = 1;
  10638. clif_map_property_mapall(m, MAPPROPERTY_FREEPVPZONE);
  10639. bl.type = BL_NUL;
  10640. bl.m = m;
  10641. clif_maptypeproperty2(&bl,ALL_SAMEMAP);
  10642. if(battle_config.pk_mode) // disable ranking functions if pk_mode is on [Valaris]
  10643. return SCRIPT_CMD_SUCCESS;
  10644. iter = mapit_getallusers();
  10645. for( sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); sd = (TBL_PC*)mapit_next(iter) )
  10646. {
  10647. if( sd->bl.m != m || sd->pvp_timer != INVALID_TIMER )
  10648. continue; // not applicable
  10649. sd->pvp_timer = add_timer(gettick()+200,pc_calc_pvprank_timer,sd->bl.id,0);
  10650. sd->pvp_rank = 0;
  10651. sd->pvp_lastusers = 0;
  10652. sd->pvp_point = 5;
  10653. sd->pvp_won = 0;
  10654. sd->pvp_lost = 0;
  10655. }
  10656. mapit_free(iter);
  10657. return SCRIPT_CMD_SUCCESS;
  10658. }
  10659. static int buildin_pvpoff_sub(struct block_list *bl,va_list ap)
  10660. {
  10661. TBL_PC* sd = (TBL_PC*)bl;
  10662. clif_pvpset(sd, 0, 0, 2);
  10663. if (sd->pvp_timer != INVALID_TIMER) {
  10664. delete_timer(sd->pvp_timer, pc_calc_pvprank_timer);
  10665. sd->pvp_timer = INVALID_TIMER;
  10666. }
  10667. return 0;
  10668. }
  10669. BUILDIN_FUNC(pvpoff)
  10670. {
  10671. int16 m;
  10672. const char *str;
  10673. struct block_list bl;
  10674. str=script_getstr(st,2);
  10675. m = map_mapname2mapid(str);
  10676. if(m < 0 || !map[m].flag.pvp)
  10677. return SCRIPT_CMD_SUCCESS; //fixed Lupus
  10678. map[m].flag.pvp = 0;
  10679. clif_map_property_mapall(m, MAPPROPERTY_NOTHING);
  10680. bl.type = BL_NUL;
  10681. bl.m = m;
  10682. clif_maptypeproperty2(&bl,ALL_SAMEMAP);
  10683. if(battle_config.pk_mode) // disable ranking options if pk_mode is on [Valaris]
  10684. return SCRIPT_CMD_SUCCESS;
  10685. map_foreachinmap(buildin_pvpoff_sub, m, BL_PC);
  10686. return SCRIPT_CMD_SUCCESS;
  10687. }
  10688. BUILDIN_FUNC(gvgon)
  10689. {
  10690. int16 m;
  10691. const char *str;
  10692. struct block_list bl;
  10693. str=script_getstr(st,2);
  10694. m = map_mapname2mapid(str);
  10695. if(m >= 0 && !map[m].flag.gvg) {
  10696. map[m].flag.gvg = 1;
  10697. clif_map_property_mapall(m, MAPPROPERTY_AGITZONE);
  10698. bl.type = BL_NUL;
  10699. bl.m = m;
  10700. clif_maptypeproperty2(&bl,ALL_SAMEMAP);
  10701. }
  10702. return SCRIPT_CMD_SUCCESS;
  10703. }
  10704. BUILDIN_FUNC(gvgoff)
  10705. {
  10706. int16 m;
  10707. const char *str;
  10708. str=script_getstr(st,2);
  10709. m = map_mapname2mapid(str);
  10710. if(m >= 0 && map[m].flag.gvg) {
  10711. struct block_list bl;
  10712. map[m].flag.gvg = 0;
  10713. clif_map_property_mapall(m, MAPPROPERTY_NOTHING);
  10714. bl.type = BL_NUL;
  10715. bl.m = m;
  10716. clif_maptypeproperty2(&bl,ALL_SAMEMAP);
  10717. }
  10718. return SCRIPT_CMD_SUCCESS;
  10719. }
  10720. /*==========================================
  10721. * Shows an emoticon on top of the player/npc
  10722. * emotion emotion#, <target: 0 - NPC, 1 - PC>, <NPC/PC name>
  10723. *------------------------------------------*/
  10724. //Optional second parameter added by [Skotlex]
  10725. BUILDIN_FUNC(emotion)
  10726. {
  10727. int type;
  10728. int player=0;
  10729. type=script_getnum(st,2);
  10730. if(type < 0 || type > 100)
  10731. return SCRIPT_CMD_SUCCESS;
  10732. if( script_hasdata(st,3) )
  10733. player=script_getnum(st,3);
  10734. if (player) {
  10735. TBL_PC *sd = NULL;
  10736. if( script_hasdata(st,4) )
  10737. sd = map_nick2sd(script_getstr(st,4));
  10738. else
  10739. sd = script_rid2sd(st);
  10740. if (sd)
  10741. clif_emotion(&sd->bl,type);
  10742. } else
  10743. if( script_hasdata(st,4) )
  10744. {
  10745. TBL_NPC *nd = npc_name2id(script_getstr(st,4));
  10746. if(nd)
  10747. clif_emotion(&nd->bl,type);
  10748. }
  10749. else
  10750. clif_emotion(map_id2bl(st->oid),type);
  10751. return SCRIPT_CMD_SUCCESS;
  10752. }
  10753. static int buildin_maprespawnguildid_sub_pc(struct map_session_data* sd, va_list ap)
  10754. {
  10755. int16 m=va_arg(ap,int);
  10756. int g_id=va_arg(ap,int);
  10757. int flag=va_arg(ap,int);
  10758. if(!sd || sd->bl.m != m)
  10759. return 0;
  10760. if(
  10761. (sd->status.guild_id == g_id && flag&1) || //Warp out owners
  10762. (sd->status.guild_id != g_id && flag&2) || //Warp out outsiders
  10763. (sd->status.guild_id == 0 && flag&2) // Warp out players not in guild
  10764. )
  10765. pc_setpos(sd,sd->status.save_point.map,sd->status.save_point.x,sd->status.save_point.y,CLR_TELEPORT);
  10766. return 1;
  10767. }
  10768. static int buildin_maprespawnguildid_sub_mob(struct block_list *bl,va_list ap)
  10769. {
  10770. struct mob_data *md=(struct mob_data *)bl;
  10771. if(!md->guardian_data && md->mob_id != MOBID_EMPERIUM)
  10772. status_kill(bl);
  10773. return 0;
  10774. }
  10775. /*
  10776. * Function to kick guild members out of a map and to their save points.
  10777. * m : mapid
  10778. * g_id : owner guild id
  10779. * flag & 1 : Warp out owners
  10780. * flag & 2 : Warp out outsiders
  10781. * flag & 4 : reserved for mobs
  10782. * */
  10783. BUILDIN_FUNC(maprespawnguildid)
  10784. {
  10785. const char *mapname=script_getstr(st,2);
  10786. int g_id=script_getnum(st,3);
  10787. int flag=script_getnum(st,4);
  10788. int16 m=map_mapname2mapid(mapname);
  10789. if(m == -1)
  10790. return SCRIPT_CMD_SUCCESS;
  10791. //Catch ALL players (in case some are 'between maps' on execution time)
  10792. map_foreachpc(buildin_maprespawnguildid_sub_pc,m,g_id,flag);
  10793. if (flag&4) //Remove script mobs.
  10794. map_foreachinmap(buildin_maprespawnguildid_sub_mob,m,BL_MOB);
  10795. return SCRIPT_CMD_SUCCESS;
  10796. }
  10797. BUILDIN_FUNC(agitstart)
  10798. {
  10799. if(agit_flag==1) return SCRIPT_CMD_SUCCESS; // Agit already Start.
  10800. agit_flag=1;
  10801. guild_agit_start();
  10802. return SCRIPT_CMD_SUCCESS;
  10803. }
  10804. BUILDIN_FUNC(agitend)
  10805. {
  10806. if(agit_flag==0) return SCRIPT_CMD_SUCCESS; // Agit already End.
  10807. agit_flag=0;
  10808. guild_agit_end();
  10809. return SCRIPT_CMD_SUCCESS;
  10810. }
  10811. BUILDIN_FUNC(agitstart2)
  10812. {
  10813. if(agit2_flag==1) return SCRIPT_CMD_SUCCESS; // Agit2 already Start.
  10814. agit2_flag=1;
  10815. guild_agit2_start();
  10816. return SCRIPT_CMD_SUCCESS;
  10817. }
  10818. BUILDIN_FUNC(agitend2)
  10819. {
  10820. if(agit2_flag==0) return SCRIPT_CMD_SUCCESS; // Agit2 already End.
  10821. agit2_flag=0;
  10822. guild_agit2_end();
  10823. return SCRIPT_CMD_SUCCESS;
  10824. }
  10825. /*==========================================
  10826. * Returns whether woe is on or off. // choice script
  10827. *------------------------------------------*/
  10828. BUILDIN_FUNC(agitcheck)
  10829. {
  10830. script_pushint(st,agit_flag);
  10831. return SCRIPT_CMD_SUCCESS;
  10832. }
  10833. /*==========================================
  10834. * Returns whether woese is on or off. // choice script
  10835. *------------------------------------------*/
  10836. BUILDIN_FUNC(agitcheck2)
  10837. {
  10838. script_pushint(st,agit2_flag);
  10839. return SCRIPT_CMD_SUCCESS;
  10840. }
  10841. /// Sets the guild_id of this npc.
  10842. ///
  10843. /// flagemblem <guild_id>;
  10844. BUILDIN_FUNC(flagemblem)
  10845. {
  10846. TBL_NPC* nd;
  10847. int g_id = script_getnum(st,2);
  10848. if(g_id < 0)
  10849. return SCRIPT_CMD_SUCCESS;
  10850. nd = (TBL_NPC*)map_id2nd(st->oid);
  10851. if( nd == NULL ) {
  10852. ShowError("script:flagemblem: npc %d not found\n", st->oid);
  10853. } else if( nd->subtype != NPCTYPE_SCRIPT ) {
  10854. ShowError("script:flagemblem: unexpected subtype %d for npc %d '%s'\n", nd->subtype, st->oid, nd->exname);
  10855. } else {
  10856. bool changed = ( nd->u.scr.guild_id != g_id )?true:false;
  10857. nd->u.scr.guild_id = g_id;
  10858. clif_guild_emblem_area(&nd->bl);
  10859. /* guild flag caching */
  10860. if( g_id ) /* adding a id */
  10861. guild_flag_add(nd);
  10862. else if( changed ) /* removing a flag */
  10863. guild_flag_remove(nd);
  10864. }
  10865. return SCRIPT_CMD_SUCCESS;
  10866. }
  10867. BUILDIN_FUNC(getcastlename)
  10868. {
  10869. const char* mapname = mapindex_getmapname(script_getstr(st,2),NULL);
  10870. struct guild_castle* gc = guild_mapname2gc(mapname);
  10871. const char* name = (gc) ? gc->castle_name : "";
  10872. script_pushstrcopy(st,name);
  10873. return SCRIPT_CMD_SUCCESS;
  10874. }
  10875. BUILDIN_FUNC(getcastledata)
  10876. {
  10877. const char *mapname = mapindex_getmapname(script_getstr(st,2),NULL);
  10878. int index = script_getnum(st,3);
  10879. struct guild_castle *gc = guild_mapname2gc(mapname);
  10880. if (gc == NULL) {
  10881. script_pushint(st,0);
  10882. ShowWarning("buildin_setcastledata: guild castle for map '%s' not found\n", mapname);
  10883. return SCRIPT_CMD_FAILURE;
  10884. }
  10885. switch (index) {
  10886. case 1:
  10887. script_pushint(st,gc->guild_id); break;
  10888. case 2:
  10889. script_pushint(st,gc->economy); break;
  10890. case 3:
  10891. script_pushint(st,gc->defense); break;
  10892. case 4:
  10893. script_pushint(st,gc->triggerE); break;
  10894. case 5:
  10895. script_pushint(st,gc->triggerD); break;
  10896. case 6:
  10897. script_pushint(st,gc->nextTime); break;
  10898. case 7:
  10899. script_pushint(st,gc->payTime); break;
  10900. case 8:
  10901. script_pushint(st,gc->createTime); break;
  10902. case 9:
  10903. script_pushint(st,gc->visibleC); break;
  10904. default:
  10905. if (index > 9 && index <= 9+MAX_GUARDIANS) {
  10906. script_pushint(st,gc->guardian[index-10].visible);
  10907. break;
  10908. }
  10909. script_pushint(st,0);
  10910. ShowWarning("buildin_setcastledata: index = '%d' is out of allowed range\n", index);
  10911. return SCRIPT_CMD_FAILURE;
  10912. }
  10913. return SCRIPT_CMD_SUCCESS;
  10914. }
  10915. BUILDIN_FUNC(setcastledata)
  10916. {
  10917. const char *mapname = mapindex_getmapname(script_getstr(st,2),NULL);
  10918. int index = script_getnum(st,3);
  10919. int value = script_getnum(st,4);
  10920. struct guild_castle *gc = guild_mapname2gc(mapname);
  10921. if (gc == NULL) {
  10922. ShowWarning("buildin_setcastledata: guild castle for map '%s' not found\n", mapname);
  10923. return SCRIPT_CMD_FAILURE;
  10924. }
  10925. if (index <= 0 || index > 9+MAX_GUARDIANS) {
  10926. ShowWarning("buildin_setcastledata: index = '%d' is out of allowed range\n", index);
  10927. return SCRIPT_CMD_FAILURE;
  10928. }
  10929. guild_castledatasave(gc->castle_id, index, value);
  10930. return SCRIPT_CMD_SUCCESS;
  10931. }
  10932. /* =====================================================================
  10933. * ---------------------------------------------------------------------*/
  10934. BUILDIN_FUNC(requestguildinfo)
  10935. {
  10936. int guild_id=script_getnum(st,2);
  10937. const char *event=NULL;
  10938. if( script_hasdata(st,3) ){
  10939. event=script_getstr(st,3);
  10940. check_event(st, event);
  10941. }
  10942. if(guild_id>0)
  10943. guild_npc_request_info(guild_id,event);
  10944. return SCRIPT_CMD_SUCCESS;
  10945. }
  10946. /// Returns the number of cards that have been compounded onto the specified equipped item.
  10947. /// getequipcardcnt(<equipment slot>);
  10948. BUILDIN_FUNC(getequipcardcnt)
  10949. {
  10950. int i=-1,j,num;
  10951. TBL_PC *sd;
  10952. int count;
  10953. num=script_getnum(st,2);
  10954. sd=script_rid2sd(st);
  10955. if (num > 0 && num <= ARRAYLENGTH(equip))
  10956. i=pc_checkequip(sd,equip[num-1]);
  10957. if (i < 0 || !sd->inventory_data[i]) {
  10958. script_pushint(st,0);
  10959. return SCRIPT_CMD_SUCCESS;
  10960. }
  10961. if(itemdb_isspecial(sd->inventory.u.items_inventory[i].card[0]))
  10962. {
  10963. script_pushint(st,0);
  10964. return SCRIPT_CMD_SUCCESS;
  10965. }
  10966. count = 0;
  10967. for( j = 0; j < sd->inventory_data[i]->slot; j++ )
  10968. if( sd->inventory.u.items_inventory[i].card[j] && itemdb_type(sd->inventory.u.items_inventory[i].card[j]) == IT_CARD )
  10969. count++;
  10970. script_pushint(st,count);
  10971. return SCRIPT_CMD_SUCCESS;
  10972. }
  10973. /// Removes all cards from the item found in the specified equipment slot of the invoking character,
  10974. /// and give them to the character. If any cards were removed in this manner, it will also show a success effect.
  10975. /// successremovecards <slot>;
  10976. BUILDIN_FUNC(successremovecards) {
  10977. int i=-1,c,cardflag=0;
  10978. TBL_PC* sd = script_rid2sd(st);
  10979. int num = script_getnum(st,2);
  10980. if (num > 0 && num <= ARRAYLENGTH(equip))
  10981. i=pc_checkequip(sd,equip[num-1]);
  10982. if (i < 0 || !sd->inventory_data[i]) {
  10983. return SCRIPT_CMD_SUCCESS;
  10984. }
  10985. if(itemdb_isspecial(sd->inventory.u.items_inventory[i].card[0]))
  10986. return SCRIPT_CMD_SUCCESS;
  10987. for( c = sd->inventory_data[i]->slot - 1; c >= 0; --c ) {
  10988. if( sd->inventory.u.items_inventory[i].card[c] && itemdb_type(sd->inventory.u.items_inventory[i].card[c]) == IT_CARD ) {// extract this card from the item
  10989. unsigned char flag = 0;
  10990. struct item item_tmp;
  10991. memset(&item_tmp,0,sizeof(item_tmp));
  10992. cardflag = 1;
  10993. item_tmp.nameid = sd->inventory.u.items_inventory[i].card[c];
  10994. item_tmp.identify = 1;
  10995. if((flag=pc_additem(sd,&item_tmp,1,LOG_TYPE_SCRIPT))){ // get back the cart in inventory
  10996. clif_additem(sd,0,0,flag);
  10997. map_addflooritem(&item_tmp,1,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0,0);
  10998. }
  10999. }
  11000. }
  11001. if(cardflag == 1) {//if card was remove remplace item with no card
  11002. unsigned char flag = 0, j;
  11003. struct item item_tmp;
  11004. memset(&item_tmp,0,sizeof(item_tmp));
  11005. item_tmp.nameid = sd->inventory.u.items_inventory[i].nameid;
  11006. item_tmp.identify = 1;
  11007. item_tmp.refine = sd->inventory.u.items_inventory[i].refine;
  11008. item_tmp.attribute = sd->inventory.u.items_inventory[i].attribute;
  11009. item_tmp.expire_time = sd->inventory.u.items_inventory[i].expire_time;
  11010. item_tmp.bound = sd->inventory.u.items_inventory[i].bound;
  11011. for (j = sd->inventory_data[i]->slot; j < MAX_SLOTS; j++)
  11012. item_tmp.card[j]=sd->inventory.u.items_inventory[i].card[j];
  11013. pc_delitem(sd,i,1,0,3,LOG_TYPE_SCRIPT);
  11014. if((flag=pc_additem(sd,&item_tmp,1,LOG_TYPE_SCRIPT))){ //chk if can be spawn in inventory otherwise put on floor
  11015. clif_additem(sd,0,0,flag);
  11016. map_addflooritem(&item_tmp,1,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0,0);
  11017. }
  11018. clif_misceffect(&sd->bl,3);
  11019. }
  11020. return SCRIPT_CMD_SUCCESS;
  11021. }
  11022. /// Removes all cards from the item found in the specified equipment slot of the invoking character.
  11023. /// failedremovecards <slot>, <type>;
  11024. /// <type>=0 : will destroy both the item and the cards.
  11025. /// <type>=1 : will keep the item, but destroy the cards.
  11026. /// <type>=2 : will keep the cards, but destroy the item.
  11027. /// <type>=? : will just display the failure effect.
  11028. BUILDIN_FUNC(failedremovecards) {
  11029. int i=-1,c,cardflag=0;
  11030. TBL_PC* sd = script_rid2sd(st);
  11031. int num = script_getnum(st,2);
  11032. int typefail = script_getnum(st,3);
  11033. if (num > 0 && num <= ARRAYLENGTH(equip))
  11034. i=pc_checkequip(sd,equip[num-1]);
  11035. if (i < 0 || !sd->inventory_data[i])
  11036. return SCRIPT_CMD_SUCCESS;
  11037. if(itemdb_isspecial(sd->inventory.u.items_inventory[i].card[0]))
  11038. return SCRIPT_CMD_SUCCESS;
  11039. for( c = sd->inventory_data[i]->slot - 1; c >= 0; --c ) {
  11040. if( sd->inventory.u.items_inventory[i].card[c] && itemdb_type(sd->inventory.u.items_inventory[i].card[c]) == IT_CARD ) {
  11041. cardflag = 1;
  11042. if(typefail == 2) {// add cards to inventory, clear
  11043. unsigned char flag = 0;
  11044. struct item item_tmp;
  11045. memset(&item_tmp,0,sizeof(item_tmp));
  11046. item_tmp.nameid = sd->inventory.u.items_inventory[i].card[c];
  11047. item_tmp.identify = 1;
  11048. if((flag=pc_additem(sd,&item_tmp,1,LOG_TYPE_SCRIPT))){
  11049. clif_additem(sd,0,0,flag);
  11050. map_addflooritem(&item_tmp,1,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0,0);
  11051. }
  11052. }
  11053. }
  11054. }
  11055. if(cardflag == 1) {
  11056. if(typefail == 0 || typefail == 2){ // destroy the item
  11057. pc_delitem(sd,i,1,0,2,LOG_TYPE_SCRIPT);
  11058. }
  11059. if(typefail == 1){ // destroy the card
  11060. unsigned char flag = 0, j;
  11061. struct item item_tmp;
  11062. memset(&item_tmp,0,sizeof(item_tmp));
  11063. item_tmp.nameid = sd->inventory.u.items_inventory[i].nameid;
  11064. item_tmp.identify = 1;
  11065. item_tmp.refine = sd->inventory.u.items_inventory[i].refine;
  11066. item_tmp.attribute = sd->inventory.u.items_inventory[i].attribute;
  11067. item_tmp.expire_time = sd->inventory.u.items_inventory[i].expire_time;
  11068. item_tmp.bound = sd->inventory.u.items_inventory[i].bound;
  11069. for (j = sd->inventory_data[i]->slot; j < MAX_SLOTS; j++)
  11070. item_tmp.card[j]=sd->inventory.u.items_inventory[i].card[j];
  11071. pc_delitem(sd,i,1,0,2,LOG_TYPE_SCRIPT);
  11072. if((flag=pc_additem(sd,&item_tmp,1,LOG_TYPE_SCRIPT))){
  11073. clif_additem(sd,0,0,flag);
  11074. map_addflooritem(&item_tmp,1,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0,0);
  11075. }
  11076. }
  11077. clif_misceffect(&sd->bl,2);
  11078. }
  11079. return SCRIPT_CMD_SUCCESS;
  11080. }
  11081. /* ================================================================
  11082. * mapwarp "<from map>","<to map>",<x>,<y>,<type>,<ID for Type>;
  11083. * type: 0=everyone, 1=guild, 2=party; [Reddozen]
  11084. * improved by [Lance]
  11085. * ================================================================*/
  11086. BUILDIN_FUNC(mapwarp) // Added by RoVeRT
  11087. {
  11088. int x,y,m,check_val=0,check_ID=0,i=0;
  11089. struct guild *g = NULL;
  11090. struct party_data *p = NULL;
  11091. const char *str;
  11092. const char *mapname;
  11093. unsigned int index;
  11094. mapname=script_getstr(st,2);
  11095. str=script_getstr(st,3);
  11096. x=script_getnum(st,4);
  11097. y=script_getnum(st,5);
  11098. if(script_hasdata(st,7)){
  11099. check_val=script_getnum(st,6);
  11100. check_ID=script_getnum(st,7);
  11101. }
  11102. if((m=map_mapname2mapid(mapname))< 0)
  11103. return SCRIPT_CMD_SUCCESS;
  11104. if(!(index=mapindex_name2id(str)))
  11105. return SCRIPT_CMD_SUCCESS;
  11106. switch(check_val){
  11107. case 1:
  11108. g = guild_search(check_ID);
  11109. if (g){
  11110. for( i=0; i < g->max_member; i++)
  11111. {
  11112. if(g->member[i].sd && g->member[i].sd->bl.m==m){
  11113. pc_setpos(g->member[i].sd,index,x,y,CLR_TELEPORT);
  11114. }
  11115. }
  11116. }
  11117. break;
  11118. case 2:
  11119. p = party_search(check_ID);
  11120. if(p){
  11121. for(i=0;i<MAX_PARTY; i++){
  11122. if(p->data[i].sd && p->data[i].sd->bl.m == m){
  11123. pc_setpos(p->data[i].sd,index,x,y,CLR_TELEPORT);
  11124. }
  11125. }
  11126. }
  11127. break;
  11128. default:
  11129. map_foreachinmap(buildin_areawarp_sub,m,BL_PC,index,x,y,0,0);
  11130. break;
  11131. }
  11132. return SCRIPT_CMD_SUCCESS;
  11133. }
  11134. static int buildin_mobcount_sub(struct block_list *bl,va_list ap) // Added by RoVeRT
  11135. {
  11136. char *event=va_arg(ap,char *);
  11137. struct mob_data *md = ((struct mob_data *)bl);
  11138. if( md->status.hp > 0 && (!event || strcmp(event,md->npc_event) == 0) )
  11139. return 1;
  11140. return SCRIPT_CMD_SUCCESS;
  11141. }
  11142. BUILDIN_FUNC(mobcount) // Added by RoVeRT
  11143. {
  11144. const char *mapname,*event;
  11145. int16 m;
  11146. mapname=script_getstr(st,2);
  11147. event=script_getstr(st,3);
  11148. if( strcmp(event, "all") == 0 )
  11149. event = NULL;
  11150. else
  11151. check_event(st, event);
  11152. if( strcmp(mapname, "this") == 0 ) {
  11153. struct map_session_data *sd = script_rid2sd(st);
  11154. if( sd )
  11155. m = sd->bl.m;
  11156. else {
  11157. script_pushint(st,-1);
  11158. return SCRIPT_CMD_SUCCESS;
  11159. }
  11160. }
  11161. else if( (m = map_mapname2mapid(mapname)) < 0 ) {
  11162. script_pushint(st,-1);
  11163. return SCRIPT_CMD_SUCCESS;
  11164. }
  11165. script_pushint(st,map_foreachinmap(buildin_mobcount_sub, m, BL_MOB, event));
  11166. return SCRIPT_CMD_SUCCESS;
  11167. }
  11168. BUILDIN_FUNC(marriage)
  11169. {
  11170. const char *partner=script_getstr(st,2);
  11171. TBL_PC *sd=script_rid2sd(st);
  11172. TBL_PC *p_sd=map_nick2sd(partner);
  11173. if(!sd || !p_sd || !pc_marriage(sd,p_sd)){
  11174. script_pushint(st,0);
  11175. return SCRIPT_CMD_SUCCESS;
  11176. }
  11177. script_pushint(st,1);
  11178. return SCRIPT_CMD_SUCCESS;
  11179. }
  11180. BUILDIN_FUNC(wedding_effect)
  11181. {
  11182. TBL_PC *sd=script_rid2sd(st);
  11183. struct block_list *bl;
  11184. if(sd==NULL) {
  11185. bl=map_id2bl(st->oid);
  11186. } else
  11187. bl=&sd->bl;
  11188. clif_wedding_effect(bl);
  11189. return SCRIPT_CMD_SUCCESS;
  11190. }
  11191. /**
  11192. * divorce({<char_id>})
  11193. **/
  11194. BUILDIN_FUNC(divorce)
  11195. {
  11196. TBL_PC *sd;
  11197. if (!script_charid2sd(2,sd) || !pc_divorce(sd)) {
  11198. script_pushint(st,0);
  11199. return SCRIPT_CMD_FAILURE;
  11200. }
  11201. script_pushint(st,1);
  11202. return SCRIPT_CMD_SUCCESS;
  11203. }
  11204. /**
  11205. * ispartneron({<char_id>})
  11206. **/
  11207. BUILDIN_FUNC(ispartneron)
  11208. {
  11209. TBL_PC *sd;
  11210. if (!script_charid2sd(2,sd) || !pc_ismarried(sd) ||
  11211. map_charid2sd(sd->status.partner_id) == NULL)
  11212. {
  11213. script_pushint(st,0);
  11214. return SCRIPT_CMD_FAILURE;
  11215. }
  11216. script_pushint(st,1);
  11217. return SCRIPT_CMD_SUCCESS;
  11218. }
  11219. /**
  11220. * getpartnerid({<char_id>})
  11221. **/
  11222. BUILDIN_FUNC(getpartnerid)
  11223. {
  11224. TBL_PC *sd;
  11225. if (!script_charid2sd(2,sd)) {
  11226. script_pushint(st,0);
  11227. return SCRIPT_CMD_FAILURE;
  11228. }
  11229. script_pushint(st,sd->status.partner_id);
  11230. return SCRIPT_CMD_SUCCESS;
  11231. }
  11232. /**
  11233. * getchildid({<char_id>})
  11234. **/
  11235. BUILDIN_FUNC(getchildid)
  11236. {
  11237. TBL_PC *sd;
  11238. if (!script_charid2sd(2,sd)) {
  11239. script_pushint(st,0);
  11240. return SCRIPT_CMD_FAILURE;
  11241. }
  11242. script_pushint(st,sd->status.child);
  11243. return SCRIPT_CMD_SUCCESS;
  11244. }
  11245. /**
  11246. * getmotherid({<char_id>})
  11247. **/
  11248. BUILDIN_FUNC(getmotherid)
  11249. {
  11250. TBL_PC *sd;
  11251. if (!script_charid2sd(2,sd)) {
  11252. script_pushint(st,0);
  11253. return SCRIPT_CMD_FAILURE;
  11254. }
  11255. script_pushint(st,sd->status.mother);
  11256. return SCRIPT_CMD_SUCCESS;
  11257. }
  11258. /**
  11259. * getfatherid({<char_id>})
  11260. **/
  11261. BUILDIN_FUNC(getfatherid)
  11262. {
  11263. TBL_PC *sd;
  11264. if (!script_charid2sd(2,sd)) {
  11265. script_pushint(st,0);
  11266. return SCRIPT_CMD_FAILURE;
  11267. }
  11268. script_pushint(st,sd->status.father);
  11269. return SCRIPT_CMD_SUCCESS;
  11270. }
  11271. BUILDIN_FUNC(warppartner)
  11272. {
  11273. int x,y;
  11274. unsigned short mapindex;
  11275. const char *str;
  11276. TBL_PC *sd=script_rid2sd(st);
  11277. TBL_PC *p_sd=NULL;
  11278. if(sd==NULL || !pc_ismarried(sd) ||
  11279. (p_sd=map_charid2sd(sd->status.partner_id)) == NULL) {
  11280. script_pushint(st,0);
  11281. return SCRIPT_CMD_SUCCESS;
  11282. }
  11283. str=script_getstr(st,2);
  11284. x=script_getnum(st,3);
  11285. y=script_getnum(st,4);
  11286. mapindex = mapindex_name2id(str);
  11287. if (mapindex) {
  11288. pc_setpos(p_sd,mapindex,x,y,CLR_OUTSIGHT);
  11289. script_pushint(st,1);
  11290. } else
  11291. script_pushint(st,0);
  11292. return SCRIPT_CMD_SUCCESS;
  11293. }
  11294. /*================================================
  11295. * Script for Displaying MOB Information [Valaris]
  11296. *------------------------------------------------*/
  11297. BUILDIN_FUNC(strmobinfo)
  11298. {
  11299. int num=script_getnum(st,2);
  11300. int class_=script_getnum(st,3);
  11301. if(!mobdb_checkid(class_))
  11302. {
  11303. if (num < 3) //requested a string
  11304. script_pushconststr(st,"");
  11305. else
  11306. script_pushint(st,0);
  11307. return SCRIPT_CMD_SUCCESS;
  11308. }
  11309. switch (num) {
  11310. case 1: script_pushstrcopy(st,mob_db(class_)->name); break;
  11311. case 2: script_pushstrcopy(st,mob_db(class_)->jname); break;
  11312. case 3: script_pushint(st,mob_db(class_)->lv); break;
  11313. case 4: script_pushint(st,mob_db(class_)->status.max_hp); break;
  11314. case 5: script_pushint(st,mob_db(class_)->status.max_sp); break;
  11315. case 6: script_pushint(st,mob_db(class_)->base_exp); break;
  11316. case 7: script_pushint(st,mob_db(class_)->job_exp); break;
  11317. default:
  11318. script_pushint(st,0);
  11319. break;
  11320. }
  11321. return SCRIPT_CMD_SUCCESS;
  11322. }
  11323. /*==========================================
  11324. * Summon guardians [Valaris]
  11325. * guardian("<map name>",<x>,<y>,"<name to show>",<mob id>{,"<event label>"}{,<guardian index>}) -> <id>
  11326. *------------------------------------------*/
  11327. BUILDIN_FUNC(guardian)
  11328. {
  11329. int class_=0,x=0,y=0,guardian=0;
  11330. const char *str,*mapname,*evt="";
  11331. bool has_index = false;
  11332. mapname =script_getstr(st,2);
  11333. x =script_getnum(st,3);
  11334. y =script_getnum(st,4);
  11335. str =script_getstr(st,5);
  11336. class_=script_getnum(st,6);
  11337. if( script_hasdata(st,8) )
  11338. {// "<event label>",<guardian index>
  11339. evt=script_getstr(st,7);
  11340. guardian=script_getnum(st,8);
  11341. has_index = true;
  11342. } else if( script_hasdata(st,7) ){
  11343. struct script_data *data = script_getdata(st,7);
  11344. get_val(st,data);
  11345. if( data_isstring(data) )
  11346. {// "<event label>"
  11347. evt=script_getstr(st,7);
  11348. } else if( data_isint(data) )
  11349. {// <guardian index>
  11350. guardian=script_getnum(st,7);
  11351. has_index = true;
  11352. } else {
  11353. ShowError("script:guardian: invalid data type for argument #6 (from 1)\n");
  11354. script_reportdata(data);
  11355. return SCRIPT_CMD_FAILURE;
  11356. }
  11357. }
  11358. check_event(st, evt);
  11359. script_pushint(st, mob_spawn_guardian(mapname,x,y,str,class_,evt,guardian,has_index));
  11360. return SCRIPT_CMD_SUCCESS;
  11361. }
  11362. /*==========================================
  11363. * Invisible Walls [Zephyrus]
  11364. *------------------------------------------*/
  11365. BUILDIN_FUNC(setwall)
  11366. {
  11367. const char *mapname, *name;
  11368. int x, y, m, size, dir;
  11369. bool shootable;
  11370. mapname = script_getstr(st,2);
  11371. x = script_getnum(st,3);
  11372. y = script_getnum(st,4);
  11373. size = script_getnum(st,5);
  11374. dir = script_getnum(st,6);
  11375. shootable = script_getnum(st,7);
  11376. name = script_getstr(st,8);
  11377. if( (m = map_mapname2mapid(mapname)) < 0 )
  11378. return SCRIPT_CMD_SUCCESS; // Invalid Map
  11379. map_iwall_set(m, x, y, size, dir, shootable, name);
  11380. return SCRIPT_CMD_SUCCESS;
  11381. }
  11382. BUILDIN_FUNC(delwall)
  11383. {
  11384. const char *name = script_getstr(st,2);
  11385. map_iwall_remove(name);
  11386. return SCRIPT_CMD_SUCCESS;
  11387. }
  11388. /// Retrieves various information about the specified guardian.
  11389. ///
  11390. /// guardianinfo("<map_name>", <index>, <type>) -> <value>
  11391. /// type: 0 - whether it is deployed or not
  11392. /// 1 - maximum hp
  11393. /// 2 - current hp
  11394. ///
  11395. BUILDIN_FUNC(guardianinfo)
  11396. {
  11397. const char* mapname = mapindex_getmapname(script_getstr(st,2),NULL);
  11398. int id = script_getnum(st,3);
  11399. int type = script_getnum(st,4);
  11400. struct guild_castle* gc = guild_mapname2gc(mapname);
  11401. struct mob_data* gd;
  11402. if( gc == NULL || id < 0 || id >= MAX_GUARDIANS )
  11403. {
  11404. script_pushint(st,-1);
  11405. return SCRIPT_CMD_SUCCESS;
  11406. }
  11407. if( type == 0 )
  11408. script_pushint(st, gc->guardian[id].visible);
  11409. else
  11410. if( !gc->guardian[id].visible )
  11411. script_pushint(st,-1);
  11412. else
  11413. if( (gd = map_id2md(gc->guardian[id].id)) == NULL )
  11414. script_pushint(st,-1);
  11415. else
  11416. {
  11417. if ( type == 1 ) script_pushint(st,gd->status.max_hp);
  11418. else if( type == 2 ) script_pushint(st,gd->status.hp);
  11419. else
  11420. script_pushint(st,-1);
  11421. }
  11422. return SCRIPT_CMD_SUCCESS;
  11423. }
  11424. /*==========================================
  11425. * Get the item name by item_id or null
  11426. *------------------------------------------*/
  11427. BUILDIN_FUNC(getitemname)
  11428. {
  11429. unsigned short item_id = 0;
  11430. struct item_data *i_data;
  11431. char *item_name;
  11432. struct script_data *data;
  11433. data=script_getdata(st,2);
  11434. get_val(st,data);
  11435. if( data_isstring(data) ){
  11436. const char *name=conv_str(st,data);
  11437. struct item_data *item_data = itemdb_searchname(name);
  11438. if( item_data )
  11439. item_id=item_data->nameid;
  11440. }else
  11441. item_id=conv_num(st,data);
  11442. i_data = itemdb_exists(item_id);
  11443. if (i_data == NULL)
  11444. {
  11445. script_pushconststr(st,"null");
  11446. return SCRIPT_CMD_SUCCESS;
  11447. }
  11448. item_name=(char *)aMalloc(ITEM_NAME_LENGTH*sizeof(char));
  11449. memcpy(item_name, i_data->jname, ITEM_NAME_LENGTH);
  11450. script_pushstr(st,item_name);
  11451. return SCRIPT_CMD_SUCCESS;
  11452. }
  11453. /*==========================================
  11454. * Returns number of slots an item has. [Skotlex]
  11455. *------------------------------------------*/
  11456. BUILDIN_FUNC(getitemslots)
  11457. {
  11458. unsigned short item_id;
  11459. struct item_data *i_data;
  11460. item_id=script_getnum(st,2);
  11461. i_data = itemdb_exists(item_id);
  11462. if (i_data)
  11463. script_pushint(st,i_data->slot);
  11464. else
  11465. script_pushint(st,-1);
  11466. return SCRIPT_CMD_SUCCESS;
  11467. }
  11468. // TODO: add matk here if needed/once we get rid of RENEWAL
  11469. /*==========================================
  11470. * Returns some values of an item [Lupus]
  11471. * Price, Weight, etc...
  11472. getiteminfo(itemID,n), where n
  11473. 0 value_buy;
  11474. 1 value_sell;
  11475. 2 type;
  11476. 3 maxchance = Max drop chance of this item e.g. 1 = 0.01% , etc..
  11477. if = 0, then monsters don't drop it at all (rare or a quest item)
  11478. if = -1, then this item is sold in NPC shops only
  11479. 4 sex;
  11480. 5 equip;
  11481. 6 weight;
  11482. 7 atk;
  11483. 8 def;
  11484. 9 range;
  11485. 10 slot;
  11486. 11 look;
  11487. 12 elv;
  11488. 13 wlv;
  11489. 14 view id
  11490. 15 eLvmax
  11491. 16 matk (renewal)
  11492. *------------------------------------------*/
  11493. BUILDIN_FUNC(getiteminfo)
  11494. {
  11495. unsigned short item_id,n;
  11496. struct item_data *i_data;
  11497. item_id = script_getnum(st,2);
  11498. n = script_getnum(st,3);
  11499. i_data = itemdb_exists(item_id);
  11500. if (i_data && n <= 16) {
  11501. int *item_arr = (int*)&i_data->value_buy;
  11502. #ifndef RENEWAL
  11503. if (n == 16)
  11504. script_pushint(st,0);
  11505. else
  11506. #endif
  11507. script_pushint(st,item_arr[n]);
  11508. } else
  11509. script_pushint(st,-1);
  11510. return SCRIPT_CMD_SUCCESS;
  11511. }
  11512. /*==========================================
  11513. * Set some values of an item [Lupus]
  11514. * Price, Weight, etc...
  11515. setiteminfo(itemID,n,Value), where n
  11516. 0 value_buy;
  11517. 1 value_sell;
  11518. 2 type;
  11519. 3 maxchance = Max drop chance of this item e.g. 1 = 0.01% , etc..
  11520. if = 0, then monsters don't drop it at all (rare or a quest item)
  11521. if = -1, then this item is sold in NPC shops only
  11522. 4 sex;
  11523. 5 equip;
  11524. 6 weight;
  11525. 7 atk;
  11526. 8 def;
  11527. 9 range;
  11528. 10 slot;
  11529. 11 look;
  11530. 12 elv;
  11531. 13 wlv;
  11532. 14 view id
  11533. * Returns Value or -1 if the wrong field's been set
  11534. *------------------------------------------*/
  11535. BUILDIN_FUNC(setiteminfo)
  11536. {
  11537. unsigned short item_id;
  11538. int n,value;
  11539. struct item_data *i_data;
  11540. item_id = script_getnum(st,2);
  11541. n = script_getnum(st,3);
  11542. value = script_getnum(st,4);
  11543. i_data = itemdb_exists(item_id);
  11544. if (i_data && n>=0 && n<=14) {
  11545. int *item_arr = (int*)&i_data->value_buy;
  11546. item_arr[n] = value;
  11547. script_pushint(st,value);
  11548. } else
  11549. script_pushint(st,-1);
  11550. return SCRIPT_CMD_SUCCESS;
  11551. }
  11552. /*==========================================
  11553. * Returns value from equipped item slot n [Lupus]
  11554. getequipcardid(num,slot)
  11555. where
  11556. num = eqip position slot
  11557. slot = 0,1,2,3 (Card Slot N)
  11558. This func returns CARD ID, 255,254,-255 (for card 0, if the item is produced)
  11559. it's useful when you want to check item cards or if it's signed
  11560. Useful for such quests as "Sign this refined item with players name" etc
  11561. Hat[0] +4 -> Player's Hat[0] +4
  11562. *------------------------------------------*/
  11563. BUILDIN_FUNC(getequipcardid)
  11564. {
  11565. int i=-1,num,slot;
  11566. TBL_PC *sd;
  11567. num=script_getnum(st,2);
  11568. slot=script_getnum(st,3);
  11569. sd=script_rid2sd(st);
  11570. if (num > 0 && num <= ARRAYLENGTH(equip))
  11571. i=pc_checkequip(sd,equip[num-1]);
  11572. if(i >= 0 && slot>=0 && slot<4)
  11573. script_pushint(st,sd->inventory.u.items_inventory[i].card[slot]);
  11574. else
  11575. script_pushint(st,0);
  11576. return SCRIPT_CMD_SUCCESS;
  11577. }
  11578. /*==========================================
  11579. * petskillbonus [Valaris] //Rewritten by [Skotlex]
  11580. *------------------------------------------*/
  11581. BUILDIN_FUNC(petskillbonus)
  11582. {
  11583. struct pet_data *pd;
  11584. TBL_PC *sd = script_rid2sd(st);
  11585. if(sd == NULL || sd->pd == NULL)
  11586. return SCRIPT_CMD_FAILURE;
  11587. pd = sd->pd;
  11588. if (pd->bonus)
  11589. { //Clear previous bonus
  11590. if (pd->bonus->timer != INVALID_TIMER)
  11591. delete_timer(pd->bonus->timer, pet_skill_bonus_timer);
  11592. } else //init
  11593. pd->bonus = (struct pet_bonus *) aMalloc(sizeof(struct pet_bonus));
  11594. pd->bonus->type = script_getnum(st,2);
  11595. pd->bonus->val = script_getnum(st,3);
  11596. pd->bonus->duration = script_getnum(st,4);
  11597. pd->bonus->delay = script_getnum(st,5);
  11598. if (pd->state.skillbonus == 1)
  11599. pd->state.skillbonus = 0; // waiting state
  11600. // wait for timer to start
  11601. if (battle_config.pet_equip_required && pd->pet.equip == 0)
  11602. pd->bonus->timer = INVALID_TIMER;
  11603. else
  11604. pd->bonus->timer = add_timer(gettick()+pd->bonus->delay*1000, pet_skill_bonus_timer, sd->bl.id, 0);
  11605. return SCRIPT_CMD_SUCCESS;
  11606. }
  11607. /*==========================================
  11608. * pet looting [Valaris] //Rewritten by [Skotlex]
  11609. *------------------------------------------*/
  11610. BUILDIN_FUNC(petloot)
  11611. {
  11612. int max;
  11613. struct pet_data *pd;
  11614. TBL_PC *sd=script_rid2sd(st);
  11615. if(sd==NULL || sd->pd==NULL)
  11616. return SCRIPT_CMD_SUCCESS;
  11617. max=script_getnum(st,2);
  11618. if(max < 1)
  11619. max = 1; //Let'em loot at least 1 item.
  11620. else if (max > MAX_PETLOOT_SIZE)
  11621. max = MAX_PETLOOT_SIZE;
  11622. pd = sd->pd;
  11623. if (pd->loot != NULL)
  11624. { //Release whatever was there already and reallocate memory
  11625. pet_lootitem_drop(pd, pd->master);
  11626. aFree(pd->loot->item);
  11627. }
  11628. else
  11629. pd->loot = (struct pet_loot *)aMalloc(sizeof(struct pet_loot));
  11630. pd->loot->item = (struct item *)aCalloc(max,sizeof(struct item));
  11631. pd->loot->max=max;
  11632. pd->loot->count = 0;
  11633. pd->loot->weight = 0;
  11634. return SCRIPT_CMD_SUCCESS;
  11635. }
  11636. /*==========================================
  11637. * Set arrays with info of all sd inventory :
  11638. * @inventorylist_id, @inventorylist_amount, @inventorylist_equip,
  11639. * @inventorylist_refine, @inventorylist_identify, @inventorylist_attribute,
  11640. * @inventorylist_card(0..3), @inventorylist_expire
  11641. * @inventorylist_count = scalar
  11642. *------------------------------------------*/
  11643. BUILDIN_FUNC(getinventorylist)
  11644. {
  11645. TBL_PC *sd;
  11646. char card_var[NAME_LENGTH];
  11647. int i,j=0,k;
  11648. if (!script_charid2sd(2,sd))
  11649. return SCRIPT_CMD_FAILURE;
  11650. for(i=0;i<MAX_INVENTORY;i++){
  11651. if(sd->inventory.u.items_inventory[i].nameid > 0 && sd->inventory.u.items_inventory[i].amount > 0){
  11652. pc_setreg(sd,reference_uid(add_str("@inventorylist_id"), j),sd->inventory.u.items_inventory[i].nameid);
  11653. pc_setreg(sd,reference_uid(add_str("@inventorylist_amount"), j),sd->inventory.u.items_inventory[i].amount);
  11654. pc_setreg(sd,reference_uid(add_str("@inventorylist_equip"), j),sd->inventory.u.items_inventory[i].equip);
  11655. pc_setreg(sd,reference_uid(add_str("@inventorylist_refine"), j),sd->inventory.u.items_inventory[i].refine);
  11656. pc_setreg(sd,reference_uid(add_str("@inventorylist_identify"), j),sd->inventory.u.items_inventory[i].identify);
  11657. pc_setreg(sd,reference_uid(add_str("@inventorylist_attribute"), j),sd->inventory.u.items_inventory[i].attribute);
  11658. for (k = 0; k < MAX_SLOTS; k++)
  11659. {
  11660. sprintf(card_var, "@inventorylist_card%d",k+1);
  11661. pc_setreg(sd,reference_uid(add_str(card_var), j),sd->inventory.u.items_inventory[i].card[k]);
  11662. }
  11663. pc_setreg(sd,reference_uid(add_str("@inventorylist_expire"), j),sd->inventory.u.items_inventory[i].expire_time);
  11664. pc_setreg(sd,reference_uid(add_str("@inventorylist_bound"), j),sd->inventory.u.items_inventory[i].bound);
  11665. j++;
  11666. }
  11667. }
  11668. pc_setreg(sd,add_str("@inventorylist_count"),j);
  11669. return SCRIPT_CMD_SUCCESS;
  11670. }
  11671. /**
  11672. * getskilllist ({<char_id>});
  11673. **/
  11674. BUILDIN_FUNC(getskilllist)
  11675. {
  11676. TBL_PC *sd;
  11677. int i,j=0;
  11678. if (!script_charid2sd(2,sd))
  11679. return SCRIPT_CMD_FAILURE;
  11680. for(i=0;i<MAX_SKILL;i++){
  11681. if(sd->status.skill[i].id > 0 && sd->status.skill[i].lv > 0){
  11682. pc_setreg(sd,reference_uid(add_str("@skilllist_id"), j),sd->status.skill[i].id);
  11683. pc_setreg(sd,reference_uid(add_str("@skilllist_lv"), j),sd->status.skill[i].lv);
  11684. pc_setreg(sd,reference_uid(add_str("@skilllist_flag"), j),sd->status.skill[i].flag);
  11685. j++;
  11686. }
  11687. }
  11688. pc_setreg(sd,add_str("@skilllist_count"),j);
  11689. return SCRIPT_CMD_SUCCESS;
  11690. }
  11691. /**
  11692. * clearitem ({<char_id>});
  11693. **/
  11694. BUILDIN_FUNC(clearitem)
  11695. {
  11696. TBL_PC *sd;
  11697. int i;
  11698. if (!script_charid2sd(2,sd))
  11699. return SCRIPT_CMD_FAILURE;
  11700. for (i=0; i<MAX_INVENTORY; i++) {
  11701. if (sd->inventory.u.items_inventory[i].amount) {
  11702. pc_delitem(sd, i, sd->inventory.u.items_inventory[i].amount, 0, 0, LOG_TYPE_SCRIPT);
  11703. }
  11704. }
  11705. return SCRIPT_CMD_SUCCESS;
  11706. }
  11707. /**
  11708. * Disguise Player (returns Mob/NPC ID if success, 0 on fail)
  11709. * disguise <Monster ID>{,<char_id>};
  11710. **/
  11711. BUILDIN_FUNC(disguise)
  11712. {
  11713. int id;
  11714. TBL_PC* sd;
  11715. if (!script_charid2sd(3,sd))
  11716. return SCRIPT_CMD_FAILURE;
  11717. id = script_getnum(st,2);
  11718. if (mobdb_checkid(id) || npcdb_checkid(id)) {
  11719. pc_disguise(sd, id);
  11720. script_pushint(st,id);
  11721. } else
  11722. script_pushint(st,0);
  11723. return SCRIPT_CMD_SUCCESS;
  11724. }
  11725. /**
  11726. * Undisguise Player (returns 1 if success, 0 on fail)
  11727. * undisguise {<char_id>};
  11728. **/
  11729. BUILDIN_FUNC(undisguise)
  11730. {
  11731. TBL_PC* sd;
  11732. if (!script_charid2sd(2,sd))
  11733. return SCRIPT_CMD_FAILURE;
  11734. if (sd->disguise) {
  11735. pc_disguise(sd, 0);
  11736. script_pushint(st,0);
  11737. } else {
  11738. script_pushint(st,1);
  11739. }
  11740. return SCRIPT_CMD_SUCCESS;
  11741. }
  11742. /*==========================================
  11743. * Transform a bl to another _class,
  11744. * @type unused
  11745. *------------------------------------------*/
  11746. BUILDIN_FUNC(classchange)
  11747. {
  11748. int _class,type;
  11749. struct block_list *bl=map_id2bl(st->oid);
  11750. if (bl==NULL)
  11751. return SCRIPT_CMD_FAILURE;
  11752. _class=script_getnum(st,2);
  11753. type=script_getnum(st,3);
  11754. clif_class_change(bl,_class,type);
  11755. return SCRIPT_CMD_SUCCESS;
  11756. }
  11757. /*==========================================
  11758. * Display an effect
  11759. *------------------------------------------*/
  11760. BUILDIN_FUNC(misceffect)
  11761. {
  11762. int type;
  11763. type=script_getnum(st,2);
  11764. if(st->oid && st->oid != fake_nd->bl.id) {
  11765. struct block_list *bl = map_id2bl(st->oid);
  11766. if (bl)
  11767. clif_specialeffect(bl,type,AREA);
  11768. } else{
  11769. TBL_PC *sd=script_rid2sd(st);
  11770. if(sd)
  11771. clif_specialeffect(&sd->bl,type,AREA);
  11772. }
  11773. return SCRIPT_CMD_SUCCESS;
  11774. }
  11775. /*==========================================
  11776. * Play a BGM on a single client [Rikter/Yommy]
  11777. *------------------------------------------*/
  11778. BUILDIN_FUNC(playBGM)
  11779. {
  11780. struct map_session_data* sd;
  11781. if( ( sd = script_rid2sd(st) ) != NULL ) {
  11782. clif_playBGM(sd, script_getstr(st,2));
  11783. }
  11784. return SCRIPT_CMD_SUCCESS;
  11785. }
  11786. static int playBGM_sub(struct block_list* bl,va_list ap)
  11787. {
  11788. const char* name = va_arg(ap,const char*);
  11789. clif_playBGM(BL_CAST(BL_PC, bl), name);
  11790. return 0;
  11791. }
  11792. static int playBGM_foreachpc_sub(struct map_session_data* sd, va_list args)
  11793. {
  11794. const char* name = va_arg(args, const char*);
  11795. clif_playBGM(sd, name);
  11796. return 0;
  11797. }
  11798. /*==========================================
  11799. * Play a BGM on multiple client [Rikter/Yommy]
  11800. *------------------------------------------*/
  11801. BUILDIN_FUNC(playBGMall)
  11802. {
  11803. const char* name;
  11804. name = script_getstr(st,2);
  11805. if( script_hasdata(st,7) ) {// specified part of map
  11806. const char* mapname = script_getstr(st,3);
  11807. int x0 = script_getnum(st,4);
  11808. int y0 = script_getnum(st,5);
  11809. int x1 = script_getnum(st,6);
  11810. int y1 = script_getnum(st,7);
  11811. map_foreachinarea(playBGM_sub, map_mapname2mapid(mapname), x0, y0, x1, y1, BL_PC, name);
  11812. }
  11813. else if( script_hasdata(st,3) ) {// entire map
  11814. const char* mapname = script_getstr(st,3);
  11815. map_foreachinmap(playBGM_sub, map_mapname2mapid(mapname), BL_PC, name);
  11816. }
  11817. else {// entire server
  11818. map_foreachpc(&playBGM_foreachpc_sub, name);
  11819. }
  11820. return SCRIPT_CMD_SUCCESS;
  11821. }
  11822. /*==========================================
  11823. * Play a .wav sound for sd
  11824. *------------------------------------------*/
  11825. BUILDIN_FUNC(soundeffect)
  11826. {
  11827. TBL_PC* sd = script_rid2sd(st);
  11828. const char* name = script_getstr(st,2);
  11829. int type = script_getnum(st,3);
  11830. if(sd) {
  11831. clif_soundeffect(sd,&sd->bl,name,type);
  11832. }
  11833. return SCRIPT_CMD_SUCCESS;
  11834. }
  11835. int soundeffect_sub(struct block_list* bl,va_list ap)
  11836. {
  11837. char* name = va_arg(ap,char*);
  11838. int type = va_arg(ap,int);
  11839. clif_soundeffect((TBL_PC *)bl, bl, name, type);
  11840. return 0;
  11841. }
  11842. /*==========================================
  11843. * Play a sound effect (.wav) on multiple clients
  11844. * soundeffectall "<filepath>",<type>{,"<map name>"}{,<x0>,<y0>,<x1>,<y1>};
  11845. *------------------------------------------*/
  11846. BUILDIN_FUNC(soundeffectall)
  11847. {
  11848. struct block_list* bl;
  11849. const char* name;
  11850. int type;
  11851. bl = (st->rid) ? &(script_rid2sd(st)->bl) : map_id2bl(st->oid);
  11852. if (!bl)
  11853. return SCRIPT_CMD_SUCCESS;
  11854. name = script_getstr(st,2);
  11855. type = script_getnum(st,3);
  11856. //FIXME: enumerating map squares (map_foreach) is slower than enumerating the list of online players (map_foreachpc?) [ultramage]
  11857. if(!script_hasdata(st,4))
  11858. { // area around
  11859. clif_soundeffectall(bl, name, type, AREA);
  11860. }
  11861. else
  11862. if(!script_hasdata(st,5))
  11863. { // entire map
  11864. const char* mapname = script_getstr(st,4);
  11865. map_foreachinmap(soundeffect_sub, map_mapname2mapid(mapname), BL_PC, name, type);
  11866. }
  11867. else
  11868. if(script_hasdata(st,8))
  11869. { // specified part of map
  11870. const char* mapname = script_getstr(st,4);
  11871. int x0 = script_getnum(st,5);
  11872. int y0 = script_getnum(st,6);
  11873. int x1 = script_getnum(st,7);
  11874. int y1 = script_getnum(st,8);
  11875. map_foreachinarea(soundeffect_sub, map_mapname2mapid(mapname), x0, y0, x1, y1, BL_PC, name, type);
  11876. }
  11877. else
  11878. {
  11879. ShowError("buildin_soundeffectall: insufficient arguments for specific area broadcast.\n");
  11880. }
  11881. return SCRIPT_CMD_SUCCESS;
  11882. }
  11883. /*==========================================
  11884. * pet status recovery [Valaris] / Rewritten by [Skotlex]
  11885. *------------------------------------------*/
  11886. BUILDIN_FUNC(petrecovery)
  11887. {
  11888. struct pet_data *pd;
  11889. TBL_PC *sd=script_rid2sd(st);
  11890. int sc;
  11891. if(sd == NULL || sd->pd == NULL)
  11892. return SCRIPT_CMD_FAILURE;
  11893. sc = script_getnum(st,2);
  11894. if (sc <= SC_NONE || sc >= SC_MAX) {
  11895. ShowError("buildin_petrecovery: Invalid SC type: %d\n", sc);
  11896. return SCRIPT_CMD_FAILURE;
  11897. }
  11898. pd = sd->pd;
  11899. if (pd->recovery)
  11900. { //Halt previous bonus
  11901. if (pd->recovery->timer != INVALID_TIMER)
  11902. delete_timer(pd->recovery->timer, pet_recovery_timer);
  11903. } else //Init
  11904. pd->recovery = (struct pet_recovery *)aMalloc(sizeof(struct pet_recovery));
  11905. pd->recovery->type = (sc_type)sc;
  11906. pd->recovery->delay = script_getnum(st,3);
  11907. pd->recovery->timer = INVALID_TIMER;
  11908. return SCRIPT_CMD_SUCCESS;
  11909. }
  11910. /*==========================================
  11911. * pet healing [Valaris] //Rewritten by [Skotlex]
  11912. *------------------------------------------*/
  11913. BUILDIN_FUNC(petheal)
  11914. {
  11915. struct pet_data *pd;
  11916. TBL_PC *sd=script_rid2sd(st);
  11917. if(sd==NULL || sd->pd==NULL)
  11918. return SCRIPT_CMD_SUCCESS;
  11919. pd=sd->pd;
  11920. if (pd->s_skill)
  11921. { //Clear previous skill
  11922. if (pd->s_skill->timer != INVALID_TIMER)
  11923. {
  11924. if (pd->s_skill->id)
  11925. delete_timer(pd->s_skill->timer, pet_skill_support_timer);
  11926. else
  11927. delete_timer(pd->s_skill->timer, pet_heal_timer);
  11928. }
  11929. } else //init memory
  11930. pd->s_skill = (struct pet_skill_support *) aMalloc(sizeof(struct pet_skill_support));
  11931. pd->s_skill->id=0; //This id identifies that it IS petheal rather than pet_skillsupport
  11932. //Use the lv as the amount to heal
  11933. pd->s_skill->lv=script_getnum(st,2);
  11934. pd->s_skill->delay=script_getnum(st,3);
  11935. pd->s_skill->hp=script_getnum(st,4);
  11936. pd->s_skill->sp=script_getnum(st,5);
  11937. //Use delay as initial offset to avoid skill/heal exploits
  11938. if (battle_config.pet_equip_required && pd->pet.equip == 0)
  11939. pd->s_skill->timer = INVALID_TIMER;
  11940. else
  11941. pd->s_skill->timer = add_timer(gettick()+pd->s_skill->delay*1000,pet_heal_timer,sd->bl.id,0);
  11942. return SCRIPT_CMD_SUCCESS;
  11943. }
  11944. /*==========================================
  11945. * pet attack skills [Valaris] //Rewritten by [Skotlex]
  11946. *------------------------------------------*/
  11947. /// petskillattack <skill id>,<level>,<rate>,<bonusrate>
  11948. /// petskillattack "<skill name>",<level>,<rate>,<bonusrate>
  11949. BUILDIN_FUNC(petskillattack)
  11950. {
  11951. struct pet_data *pd;
  11952. struct script_data *data;
  11953. TBL_PC *sd = script_rid2sd(st);
  11954. int id = 0;
  11955. if(sd == NULL || sd->pd == NULL)
  11956. return SCRIPT_CMD_FAILURE;
  11957. data = script_getdata(st, 2);
  11958. get_val(st, data);
  11959. id = (data_isstring(data) ? skill_name2id(script_getstr(st,2)) : skill_get_index(script_getnum(st,2)));
  11960. if (!id) {
  11961. ShowError("buildin_petskillattack: Invalid skill defined!\n");
  11962. return SCRIPT_CMD_FAILURE;
  11963. }
  11964. pd = sd->pd;
  11965. if (pd->a_skill == NULL)
  11966. pd->a_skill = (struct pet_skill_attack *)aMalloc(sizeof(struct pet_skill_attack));
  11967. pd->a_skill->id = id;
  11968. pd->a_skill->damage = 0;
  11969. pd->a_skill->lv = (unsigned short)min(script_getnum(st,3), skill_get_max(pd->a_skill->id));
  11970. pd->a_skill->div_ = 0;
  11971. pd->a_skill->rate = script_getnum(st,4);
  11972. pd->a_skill->bonusrate = script_getnum(st,5);
  11973. return SCRIPT_CMD_SUCCESS;
  11974. }
  11975. /*==========================================
  11976. * pet attack skills [Valaris]
  11977. *------------------------------------------*/
  11978. /// petskillattack2 <skill id>,<damage>,<div>,<rate>,<bonusrate>
  11979. /// petskillattack2 "<skill name>",<damage>,<div>,<rate>,<bonusrate>
  11980. BUILDIN_FUNC(petskillattack2)
  11981. {
  11982. struct pet_data *pd;
  11983. struct script_data *data;
  11984. TBL_PC *sd = script_rid2sd(st);
  11985. int id = 0;
  11986. if(sd == NULL || sd->pd == NULL)
  11987. return SCRIPT_CMD_FAILURE;
  11988. data = script_getdata(st, 2);
  11989. get_val(st, data);
  11990. id = (data_isstring(data) ? skill_name2id(script_getstr(st,2)) : skill_get_index(script_getnum(st,2)));
  11991. if (!id) {
  11992. ShowError("buildin_petskillattack2: Invalid skill defined!\n");
  11993. return SCRIPT_CMD_FAILURE;
  11994. }
  11995. pd = sd->pd;
  11996. if (pd->a_skill == NULL)
  11997. pd->a_skill = (struct pet_skill_attack *)aMalloc(sizeof(struct pet_skill_attack));
  11998. pd->a_skill->id = id;
  11999. pd->a_skill->damage = script_getnum(st,3); // Fixed damage
  12000. pd->a_skill->lv = (unsigned short)skill_get_max(pd->a_skill->id); // Adjust to max skill level
  12001. pd->a_skill->div_ = script_getnum(st,4);
  12002. pd->a_skill->rate = script_getnum(st,5);
  12003. pd->a_skill->bonusrate = script_getnum(st,6);
  12004. return SCRIPT_CMD_SUCCESS;
  12005. }
  12006. /*==========================================
  12007. * pet support skills [Skotlex]
  12008. *------------------------------------------*/
  12009. /// petskillsupport <skill id>,<level>,<delay>,<hp>,<sp>
  12010. /// petskillsupport "<skill name>",<level>,<delay>,<hp>,<sp>
  12011. BUILDIN_FUNC(petskillsupport)
  12012. {
  12013. struct pet_data *pd;
  12014. struct script_data *data;
  12015. TBL_PC *sd = script_rid2sd(st);
  12016. int id = 0;
  12017. if(sd == NULL || sd->pd == NULL)
  12018. return SCRIPT_CMD_FAILURE;
  12019. data = script_getdata(st, 2);
  12020. get_val(st, data);
  12021. id = (data_isstring(data) ? skill_name2id(script_getstr(st,2)) : skill_get_index(script_getnum(st,2)));
  12022. if (!id) {
  12023. ShowError("buildin_petskillsupport: Invalid skill defined!\n");
  12024. return SCRIPT_CMD_FAILURE;
  12025. }
  12026. pd = sd->pd;
  12027. if (pd->s_skill)
  12028. { //Clear previous skill
  12029. if (pd->s_skill->timer != INVALID_TIMER)
  12030. {
  12031. if (pd->s_skill->id)
  12032. delete_timer(pd->s_skill->timer, pet_skill_support_timer);
  12033. else
  12034. delete_timer(pd->s_skill->timer, pet_heal_timer);
  12035. }
  12036. } else //init memory
  12037. pd->s_skill = (struct pet_skill_support *) aMalloc(sizeof(struct pet_skill_support));
  12038. pd->s_skill->id = id;
  12039. pd->s_skill->lv = script_getnum(st,3);
  12040. pd->s_skill->delay = script_getnum(st,4);
  12041. pd->s_skill->hp = script_getnum(st,5);
  12042. pd->s_skill->sp = script_getnum(st,6);
  12043. //Use delay as initial offset to avoid skill/heal exploits
  12044. if (battle_config.pet_equip_required && pd->pet.equip == 0)
  12045. pd->s_skill->timer = INVALID_TIMER;
  12046. else
  12047. pd->s_skill->timer = add_timer(gettick()+pd->s_skill->delay*1000,pet_skill_support_timer,sd->bl.id,0);
  12048. return SCRIPT_CMD_SUCCESS;
  12049. }
  12050. /*==========================================
  12051. * Scripted skill effects [Celest]
  12052. *------------------------------------------*/
  12053. /// skilleffect <skill id>,<level>
  12054. /// skilleffect "<skill name>",<level>
  12055. BUILDIN_FUNC(skilleffect)
  12056. {
  12057. TBL_PC *sd;
  12058. uint16 skill_id, skill_lv;
  12059. struct script_data *data = script_getdata(st, 2);
  12060. get_val(st, data); // Convert into value in case of a variable
  12061. skill_id = ( data_isstring(data) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  12062. skill_lv = script_getnum(st,3);
  12063. sd = script_rid2sd(st);
  12064. /* Ensure we're standing because the following packet causes the client to virtually set the char to stand,
  12065. * which leaves the server thinking it still is sitting. */
  12066. if( pc_issit(sd) && pc_setstand(sd, false) ) {
  12067. skill_sit(sd, 0);
  12068. clif_standing(&sd->bl);
  12069. }
  12070. clif_skill_nodamage(&sd->bl,&sd->bl,skill_id,skill_lv,1);
  12071. return SCRIPT_CMD_SUCCESS;
  12072. }
  12073. /*==========================================
  12074. * NPC skill effects [Valaris]
  12075. *------------------------------------------*/
  12076. /// npcskilleffect <skill id>,<level>,<x>,<y>
  12077. /// npcskilleffect "<skill name>",<level>,<x>,<y>
  12078. BUILDIN_FUNC(npcskilleffect)
  12079. {
  12080. struct block_list *bl= map_id2bl(st->oid);
  12081. uint16 skill_id, skill_lv;
  12082. int x, y;
  12083. struct script_data *data = script_getdata(st, 2);
  12084. get_val(st, data); // Convert into value in case of a variable
  12085. skill_id=( data_isstring(data) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  12086. skill_lv=script_getnum(st,3);
  12087. x=script_getnum(st,4);
  12088. y=script_getnum(st,5);
  12089. if (bl)
  12090. clif_skill_poseffect(bl,skill_id,skill_lv,x,y,gettick());
  12091. return SCRIPT_CMD_SUCCESS;
  12092. }
  12093. /*==========================================
  12094. * Special effects [Valaris]
  12095. *------------------------------------------*/
  12096. BUILDIN_FUNC(specialeffect)
  12097. {
  12098. struct block_list *bl=map_id2bl(st->oid);
  12099. int type = script_getnum(st,2);
  12100. enum send_target target = script_hasdata(st,3) ? (send_target)script_getnum(st,3) : AREA;
  12101. if(bl==NULL)
  12102. return SCRIPT_CMD_SUCCESS;
  12103. if( script_hasdata(st,4) )
  12104. {
  12105. TBL_NPC *nd = npc_name2id(script_getstr(st,4));
  12106. if(nd)
  12107. clif_specialeffect(&nd->bl, type, target);
  12108. }
  12109. else
  12110. {
  12111. if (target == SELF) {
  12112. TBL_PC *sd=script_rid2sd(st);
  12113. if (sd)
  12114. clif_specialeffect_single(bl,type,sd->fd);
  12115. } else {
  12116. clif_specialeffect(bl, type, target);
  12117. }
  12118. }
  12119. return SCRIPT_CMD_SUCCESS;
  12120. }
  12121. BUILDIN_FUNC(specialeffect2)
  12122. {
  12123. TBL_PC *sd=script_rid2sd(st);
  12124. int type = script_getnum(st,2);
  12125. enum send_target target = script_hasdata(st,3) ? (send_target)script_getnum(st,3) : AREA;
  12126. if( script_hasdata(st,4) )
  12127. sd = map_nick2sd(script_getstr(st,4));
  12128. if (sd)
  12129. clif_specialeffect(&sd->bl, type, target);
  12130. return SCRIPT_CMD_SUCCESS;
  12131. }
  12132. /**
  12133. * nude({<char_id>});
  12134. * @author [Valaris]
  12135. **/
  12136. BUILDIN_FUNC(nude)
  12137. {
  12138. TBL_PC *sd;
  12139. int i, calcflag = 0;
  12140. if (!script_charid2sd(2,sd))
  12141. return SCRIPT_CMD_FAILURE;
  12142. for( i = 0 ; i < EQI_MAX; i++ ) {
  12143. if( sd->equip_index[ i ] >= 0 ) {
  12144. if( !calcflag )
  12145. calcflag = 1;
  12146. pc_unequipitem( sd , sd->equip_index[ i ] , 2);
  12147. }
  12148. }
  12149. if( calcflag )
  12150. status_calc_pc(sd,SCO_NONE);
  12151. return SCRIPT_CMD_SUCCESS;
  12152. }
  12153. int atcommand_sub(struct script_state* st,int type) {
  12154. TBL_PC *sd, dummy_sd;
  12155. int fd;
  12156. const char *cmd;
  12157. cmd = script_getstr(st,2);
  12158. if (st->rid) {
  12159. sd = script_rid2sd(st);
  12160. fd = sd->fd;
  12161. } else { //Use a dummy character.
  12162. sd = &dummy_sd;
  12163. fd = 0;
  12164. memset(&dummy_sd, 0, sizeof(TBL_PC));
  12165. if (st->oid) {
  12166. struct block_list* bl = map_id2bl(st->oid);
  12167. memcpy(&dummy_sd.bl, bl, sizeof(struct block_list));
  12168. if (bl->type == BL_NPC)
  12169. safestrncpy(dummy_sd.status.name, ((TBL_NPC*)bl)->name, NAME_LENGTH);
  12170. sd->mapindex = (bl->m > 0) ? bl->m : mapindex_name2id(map_default.mapname);
  12171. }
  12172. // Init Group ID, Level, & permissions
  12173. sd->group_id = sd->group_level = 99;
  12174. sd->permissions |= PC_PERM_ALLPERMISSION;
  12175. }
  12176. if (!is_atcommand(fd, sd, cmd, type)) {
  12177. ShowWarning("buildin_atcommand: failed to execute command '%s'\n", cmd);
  12178. script_reportsrc(st);
  12179. return SCRIPT_CMD_FAILURE;
  12180. }
  12181. return SCRIPT_CMD_SUCCESS;
  12182. }
  12183. /*==========================================
  12184. * gmcommand [MouseJstr]
  12185. *------------------------------------------*/
  12186. BUILDIN_FUNC(atcommand) {
  12187. return atcommand_sub(st,0);
  12188. }
  12189. /** Displays a message for the player only (like system messages like "you got an apple" )
  12190. * dispbottom("<message>"{,<color>{,<char_id>}})
  12191. * @param message
  12192. * @param color Hex color default (Green)
  12193. */
  12194. BUILDIN_FUNC(dispbottom)
  12195. {
  12196. TBL_PC *sd;
  12197. int color = 0;
  12198. const char *message;
  12199. if (!script_charid2sd(4,sd))
  12200. return SCRIPT_CMD_FAILURE;
  12201. message = script_getstr(st,2);
  12202. if (script_hasdata(st,3))
  12203. color = script_getnum(st,3); // <color>
  12204. if(sd) {
  12205. if (script_hasdata(st,3))
  12206. clif_messagecolor2(sd, color, message); // [Napster]
  12207. else
  12208. clif_disp_onlyself(sd, message, (int)strlen(message));
  12209. }
  12210. return SCRIPT_CMD_SUCCESS;
  12211. }
  12212. /*===================================
  12213. * Heal portion of recovery command
  12214. *-----------------------------------*/
  12215. int recovery_sub(struct map_session_data* sd, int revive)
  12216. {
  12217. if(revive&(1|4) && pc_isdead(sd)) {
  12218. status_revive(&sd->bl, 100, 100);
  12219. clif_displaymessage(sd->fd,msg_txt(sd,16)); // You've been revived!
  12220. clif_specialeffect(&sd->bl, 77, AREA);
  12221. } else if(revive&(1|2) && !pc_isdead(sd)) {
  12222. status_percent_heal(&sd->bl, 100, 100);
  12223. clif_displaymessage(sd->fd,msg_txt(sd,680)); // You have been recovered!
  12224. }
  12225. return SCRIPT_CMD_SUCCESS;
  12226. }
  12227. /*=========================================================================
  12228. * Fully Recover a Character's HP/SP - [Capuche] & [Akinari]
  12229. * recovery <type>{,<option>,<revive_flag>{,<map name>}};
  12230. * <type> determines <option>:
  12231. * 0 : char_id
  12232. * 1 : party_id
  12233. * 2 : guild_id
  12234. * 3 : map_name
  12235. * 4 : all characters
  12236. * <revive_flag>:
  12237. * 1 : Revive and heal all players (default)
  12238. * 2 : Heal living players only
  12239. * 4 : Revive dead players only
  12240. * <map name>:
  12241. * for types 1-2 : map_name (null = all maps)
  12242. *-------------------------------------------------------------------------*/
  12243. BUILDIN_FUNC(recovery)
  12244. {
  12245. TBL_PC *sd;
  12246. int map_idx = 0, type = 0, revive = 1;
  12247. type = script_getnum(st,2);
  12248. if(script_hasdata(st,4))
  12249. revive = script_getnum(st,4);
  12250. switch(type) {
  12251. case 0:
  12252. if(script_hasdata(st,3))
  12253. sd=map_charid2sd(script_getnum(st,3));
  12254. else if((sd = script_rid2sd(st)) == NULL)
  12255. return SCRIPT_CMD_SUCCESS; //If we don't have sd by now, bail out
  12256. recovery_sub(sd, revive);
  12257. break;
  12258. case 1:
  12259. {
  12260. struct party_data* p;
  12261. //When no party given, we use invoker party
  12262. int p_id = 0, i;
  12263. if(script_hasdata(st,5)) {//Bad maps shouldn't cause issues
  12264. map_idx = map_mapname2mapid(script_getstr(st,5));
  12265. if(map_idx < 1) { //But we'll check anyways
  12266. ShowDebug("recovery: bad map name given (%s)\n", script_getstr(st,5));
  12267. return SCRIPT_CMD_FAILURE;
  12268. }
  12269. }
  12270. if(script_hasdata(st,3))
  12271. p_id = script_getnum(st,3);
  12272. else if((sd = script_rid2sd(st)))
  12273. p_id = sd->status.party_id;
  12274. p = party_search(p_id);
  12275. if(p == NULL)
  12276. return SCRIPT_CMD_SUCCESS;
  12277. for (i = 0; i < MAX_PARTY; i++) {
  12278. struct map_session_data* pl_sd;
  12279. if((!(pl_sd = p->data[i].sd) || pl_sd->status.party_id != p_id)
  12280. || (map_idx && pl_sd->bl.m != map_idx))
  12281. continue;
  12282. recovery_sub(pl_sd, revive);
  12283. }
  12284. break;
  12285. }
  12286. case 2:
  12287. {
  12288. struct guild* g;
  12289. //When no guild given, we use invoker guild
  12290. int g_id = 0, i;
  12291. if(script_hasdata(st,5)) {//Bad maps shouldn't cause issues
  12292. map_idx = map_mapname2mapid(script_getstr(st,5));
  12293. if(map_idx < 1) { //But we'll check anyways
  12294. ShowDebug("recovery: bad map name given (%s)\n", script_getstr(st,5));
  12295. return SCRIPT_CMD_FAILURE;
  12296. }
  12297. }
  12298. if(script_hasdata(st,3))
  12299. g_id = script_getnum(st,3);
  12300. else if((sd = script_rid2sd(st)))
  12301. g_id = sd->status.guild_id;
  12302. g = guild_search(g_id);
  12303. if(g == NULL)
  12304. return SCRIPT_CMD_SUCCESS;
  12305. for (i = 0; i < MAX_GUILD; i++) {
  12306. struct map_session_data* pl_sd;
  12307. if((!(pl_sd = g->member[i].sd) || pl_sd->status.guild_id != g_id)
  12308. || (map_idx && pl_sd->bl.m != map_idx))
  12309. continue;
  12310. recovery_sub(pl_sd, revive);
  12311. }
  12312. break;
  12313. }
  12314. case 3:
  12315. if(script_hasdata(st,3))
  12316. map_idx = map_mapname2mapid(script_getstr(st,3));
  12317. else if((sd = script_rid2sd(st)))
  12318. map_idx = sd->bl.m;
  12319. if(map_idx < 1)
  12320. return SCRIPT_CMD_FAILURE; //No sd and no map given - return
  12321. case 4:
  12322. {
  12323. struct s_mapiterator *iter;
  12324. struct script_data *data = script_getdata(st, 3);
  12325. get_val(st, data); // Convert into value in case of a variable
  12326. if(script_hasdata(st,3) && !data_isstring(data))
  12327. revive = script_getnum(st,3); // recovery 4,<revive_flag>;
  12328. iter = mapit_getallusers();
  12329. for (sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); sd = (TBL_PC*)mapit_next(iter)) {
  12330. if(type == 3 && sd->bl.m != map_idx)
  12331. continue;
  12332. recovery_sub(sd, revive);
  12333. }
  12334. mapit_free(iter);
  12335. break;
  12336. }
  12337. default:
  12338. ShowWarning("script: buildin_recovery: Invalid type %d\n", type);
  12339. script_pushint(st,-1);
  12340. return SCRIPT_CMD_FAILURE;
  12341. }
  12342. script_pushint(st,1); //Successfully executed without errors
  12343. return SCRIPT_CMD_SUCCESS;
  12344. }
  12345. /*==========================================
  12346. * Get your pet info: getpetinfo <type>{,<char_id>}
  12347. * n -> 0:pet_id 1:pet_class 2:pet_name
  12348. * 3:friendly 4:hungry, 5: rename flag.6:level,
  12349. * 7:game id
  12350. *------------------------------------------*/
  12351. BUILDIN_FUNC(getpetinfo)
  12352. {
  12353. TBL_PC *sd=script_rid2sd(st);
  12354. TBL_PET *pd;
  12355. int type = script_getnum(st,2);
  12356. if (!script_charid2sd(3, sd) || !(pd = sd->pd)) {
  12357. if (type == 2)
  12358. script_pushconststr(st,"null");
  12359. else
  12360. script_pushint(st,0);
  12361. return SCRIPT_CMD_SUCCESS;
  12362. }
  12363. switch(type){
  12364. case PETINFO_ID: script_pushint(st,pd->pet.pet_id); break;
  12365. case PETINFO_CLASS: script_pushint(st,pd->pet.class_); break;
  12366. case PETINFO_NAME: script_pushstrcopy(st,pd->pet.name); break;
  12367. case PETINFO_INTIMATE: script_pushint(st,pd->pet.intimate); break;
  12368. case PETINFO_HUNGRY: script_pushint(st,pd->pet.hungry); break;
  12369. case PETINFO_RENAMED: script_pushint(st,pd->pet.rename_flag); break;
  12370. case PETINFO_LEVEL: script_pushint(st,(int)pd->pet.level); break;
  12371. case PETINFO_BLOCKID: script_pushint(st,pd->bl.id); break;
  12372. default:
  12373. script_pushint(st,0);
  12374. break;
  12375. }
  12376. return SCRIPT_CMD_SUCCESS;
  12377. }
  12378. /*==========================================
  12379. * Get your homunculus info: gethominfo <type>{,<char_id>};
  12380. * type -> 0:hom_id 1:class 2:name
  12381. * 3:friendly 4:hungry, 5: rename flag.
  12382. * 6: level, 7: game id
  12383. *------------------------------------------*/
  12384. BUILDIN_FUNC(gethominfo)
  12385. {
  12386. TBL_PC *sd=script_rid2sd(st);
  12387. TBL_HOM *hd;
  12388. int type=script_getnum(st,2);
  12389. if (!script_charid2sd(3, sd) || !(hd = sd->hd)) {
  12390. if (type == 2)
  12391. script_pushconststr(st,"null");
  12392. else
  12393. script_pushint(st,0);
  12394. return SCRIPT_CMD_SUCCESS;
  12395. }
  12396. switch(type){
  12397. case 0: script_pushint(st,hd->homunculus.hom_id); break;
  12398. case 1: script_pushint(st,hd->homunculus.class_); break;
  12399. case 2: script_pushstrcopy(st,hd->homunculus.name); break;
  12400. case 3: script_pushint(st,hd->homunculus.intimacy); break;
  12401. case 4: script_pushint(st,hd->homunculus.hunger); break;
  12402. case 5: script_pushint(st,hd->homunculus.rename_flag); break;
  12403. case 6: script_pushint(st,hd->homunculus.level); break;
  12404. case 7: script_pushint(st,hd->bl.id); break;
  12405. default:
  12406. script_pushint(st,0);
  12407. break;
  12408. }
  12409. return SCRIPT_CMD_SUCCESS;
  12410. }
  12411. /// Retrieves information about character's mercenary
  12412. /// getmercinfo <type>[,<char id>];
  12413. BUILDIN_FUNC(getmercinfo)
  12414. {
  12415. int type;
  12416. struct map_session_data* sd;
  12417. struct mercenary_data* md;
  12418. type = script_getnum(st,2);
  12419. if( script_hasdata(st,3) )
  12420. {
  12421. uint32 char_id = script_getnum(st,3);
  12422. if( ( sd = map_charid2sd(char_id) ) == NULL )
  12423. {
  12424. ShowError("buildin_getmercinfo: No such character (char_id=%d).\n", char_id);
  12425. script_pushnil(st);
  12426. return SCRIPT_CMD_FAILURE;
  12427. }
  12428. }
  12429. else
  12430. {
  12431. if( ( sd = script_rid2sd(st) ) == NULL )
  12432. {
  12433. script_pushnil(st);
  12434. return SCRIPT_CMD_SUCCESS;
  12435. }
  12436. }
  12437. md = ( sd->status.mer_id && sd->md ) ? sd->md : NULL;
  12438. switch( type )
  12439. {
  12440. case 0: script_pushint(st,md ? md->mercenary.mercenary_id : 0); break;
  12441. case 1: script_pushint(st,md ? md->mercenary.class_ : 0); break;
  12442. case 2:
  12443. if( md )
  12444. script_pushstrcopy(st,md->db->name);
  12445. else
  12446. script_pushconststr(st,"");
  12447. break;
  12448. case 3: script_pushint(st,md ? mercenary_get_faith(md) : 0); break;
  12449. case 4: script_pushint(st,md ? mercenary_get_calls(md) : 0); break;
  12450. case 5: script_pushint(st,md ? md->mercenary.kill_count : 0); break;
  12451. case 6: script_pushint(st,md ? mercenary_get_lifetime(md) : 0); break;
  12452. case 7: script_pushint(st,md ? md->db->lv : 0); break;
  12453. case 8: script_pushint(st,md ? md->bl.id : 0); break;
  12454. default:
  12455. ShowError("buildin_getmercinfo: Invalid type %d (char_id=%d).\n", type, sd->status.char_id);
  12456. script_pushnil(st);
  12457. return SCRIPT_CMD_FAILURE;
  12458. }
  12459. return SCRIPT_CMD_SUCCESS;
  12460. }
  12461. /*==========================================
  12462. * Shows wether your inventory(and equips) contain
  12463. selected card or not.
  12464. checkequipedcard(4001);
  12465. *------------------------------------------*/
  12466. BUILDIN_FUNC(checkequipedcard)
  12467. {
  12468. TBL_PC *sd=script_rid2sd(st);
  12469. if(sd){
  12470. int n,i,c=0;
  12471. c=script_getnum(st,2);
  12472. for(i=0;i<MAX_INVENTORY;i++){
  12473. if(sd->inventory.u.items_inventory[i].nameid > 0 && sd->inventory.u.items_inventory[i].amount && sd->inventory_data[i]){
  12474. if (itemdb_isspecial(sd->inventory.u.items_inventory[i].card[0]))
  12475. continue;
  12476. for(n=0;n<sd->inventory_data[i]->slot;n++){
  12477. if(sd->inventory.u.items_inventory[i].card[n] == c) {
  12478. script_pushint(st,1);
  12479. return SCRIPT_CMD_SUCCESS;
  12480. }
  12481. }
  12482. }
  12483. }
  12484. }
  12485. script_pushint(st,0);
  12486. return SCRIPT_CMD_SUCCESS;
  12487. }
  12488. BUILDIN_FUNC(jump_zero)
  12489. {
  12490. int sel;
  12491. sel=script_getnum(st,2);
  12492. if(!sel) {
  12493. int pos;
  12494. if( !data_islabel(script_getdata(st,3)) ){
  12495. ShowError("script: jump_zero: not label !\n");
  12496. st->state=END;
  12497. return SCRIPT_CMD_FAILURE;
  12498. }
  12499. pos=script_getnum(st,3);
  12500. st->pos=pos;
  12501. st->state=GOTO;
  12502. }
  12503. return SCRIPT_CMD_SUCCESS;
  12504. }
  12505. /*==========================================
  12506. * movenpc [MouseJstr]
  12507. *------------------------------------------*/
  12508. BUILDIN_FUNC(movenpc)
  12509. {
  12510. TBL_NPC *nd = NULL;
  12511. const char *npc;
  12512. int x,y;
  12513. npc = script_getstr(st,2);
  12514. x = script_getnum(st,3);
  12515. y = script_getnum(st,4);
  12516. if ((nd = npc_name2id(npc)) == NULL){
  12517. ShowError("script: movenpc: NPC with ID '%s' was not found!\n", npc );
  12518. return -1;
  12519. }
  12520. if (script_hasdata(st,5))
  12521. nd->ud.dir = script_getnum(st,5) % 8;
  12522. npc_movenpc(nd, x, y);
  12523. return SCRIPT_CMD_SUCCESS;
  12524. }
  12525. /*==========================================
  12526. * message [MouseJstr]
  12527. *------------------------------------------*/
  12528. BUILDIN_FUNC(message)
  12529. {
  12530. const char *msg,*player;
  12531. TBL_PC *pl_sd = NULL;
  12532. player = script_getstr(st,2);
  12533. msg = script_getstr(st,3);
  12534. if((pl_sd=map_nick2sd((char *) player)) == NULL)
  12535. return SCRIPT_CMD_SUCCESS;
  12536. clif_displaymessage(pl_sd->fd, msg);
  12537. return SCRIPT_CMD_SUCCESS;
  12538. }
  12539. /*==========================================
  12540. * npctalk (sends message to surrounding area)
  12541. *------------------------------------------*/
  12542. BUILDIN_FUNC(npctalk)
  12543. {
  12544. const char* str;
  12545. struct npc_data* nd = (struct npc_data *)map_id2bl(st->oid);
  12546. str = script_getstr(st,2);
  12547. if(nd)
  12548. {
  12549. char name[NAME_LENGTH], message[256];
  12550. safestrncpy(name, nd->name, sizeof(name));
  12551. strtok(name, "#"); // discard extra name identifier if present
  12552. safesnprintf(message, sizeof(message), "%s : %s", name, str);
  12553. clif_disp_overhead(&nd->bl, message);
  12554. }
  12555. return SCRIPT_CMD_SUCCESS;
  12556. }
  12557. // change npc walkspeed [Valaris]
  12558. BUILDIN_FUNC(npcspeed)
  12559. {
  12560. struct npc_data* nd;
  12561. int speed;
  12562. speed = script_getnum(st,2);
  12563. nd =(struct npc_data *)map_id2bl(st->oid);
  12564. if( nd ) {
  12565. nd->speed = speed;
  12566. nd->ud.state.speed_changed = 1;
  12567. }
  12568. return SCRIPT_CMD_SUCCESS;
  12569. }
  12570. // make an npc walk to a position [Valaris]
  12571. BUILDIN_FUNC(npcwalkto)
  12572. {
  12573. struct npc_data *nd=(struct npc_data *)map_id2bl(st->oid);
  12574. int x=0,y=0;
  12575. x=script_getnum(st,2);
  12576. y=script_getnum(st,3);
  12577. if(nd) {
  12578. if (!nd->status.hp)
  12579. status_calc_npc(nd, SCO_FIRST);
  12580. else
  12581. status_calc_npc(nd, SCO_NONE);
  12582. unit_walktoxy(&nd->bl,x,y,0);
  12583. }
  12584. return SCRIPT_CMD_SUCCESS;
  12585. }
  12586. // stop an npc's movement [Valaris]
  12587. BUILDIN_FUNC(npcstop)
  12588. {
  12589. struct npc_data *nd=(struct npc_data *)map_id2bl(st->oid);
  12590. if(nd) {
  12591. unit_stop_walking(&nd->bl,1|4);
  12592. }
  12593. return SCRIPT_CMD_SUCCESS;
  12594. }
  12595. /**
  12596. * getlook(<type>{,<char_id>})
  12597. **/
  12598. BUILDIN_FUNC(getlook)
  12599. {
  12600. int type,val;
  12601. TBL_PC *sd;
  12602. if (!script_charid2sd(3,sd)) {
  12603. script_pushint(st,-1);
  12604. return SCRIPT_CMD_FAILURE;
  12605. }
  12606. type=script_getnum(st,2);
  12607. val=-1;
  12608. switch(type) {
  12609. // TODO: implement LOOK_BASE as stated in script doc
  12610. case LOOK_HAIR: val=sd->status.hair; break; //1
  12611. case LOOK_WEAPON: val=sd->status.weapon; break; //2
  12612. case LOOK_HEAD_BOTTOM: val=sd->status.head_bottom; break; //3
  12613. case LOOK_HEAD_TOP: val=sd->status.head_top; break; //4
  12614. case LOOK_HEAD_MID: val=sd->status.head_mid; break; //5
  12615. case LOOK_HAIR_COLOR: val=sd->status.hair_color; break; //6
  12616. case LOOK_CLOTHES_COLOR: val=sd->status.clothes_color; break; //7
  12617. case LOOK_SHIELD: val=sd->status.shield; break; //8
  12618. case LOOK_SHOES: break; //9
  12619. case LOOK_ROBE: val=sd->status.robe; break; //12
  12620. case LOOK_BODY2: val=sd->status.body; break; //13
  12621. }
  12622. script_pushint(st,val);
  12623. return SCRIPT_CMD_SUCCESS;
  12624. }
  12625. /**
  12626. * getsavepoint(<information type>{,<char_id>})
  12627. * @param type 0- map name, 1- x, 2- y
  12628. **/
  12629. BUILDIN_FUNC(getsavepoint)
  12630. {
  12631. TBL_PC* sd;
  12632. int type;
  12633. if (!script_charid2sd(3,sd)) {
  12634. script_pushint(st,0);
  12635. return SCRIPT_CMD_SUCCESS;
  12636. }
  12637. type = script_getnum(st,2);
  12638. switch(type) {
  12639. case 0: script_pushstrcopy(st,mapindex_id2name(sd->status.save_point.map)); break;
  12640. case 1: script_pushint(st,sd->status.save_point.x); break;
  12641. case 2: script_pushint(st,sd->status.save_point.y); break;
  12642. default:
  12643. script_pushint(st,0);
  12644. break;
  12645. }
  12646. return SCRIPT_CMD_SUCCESS;
  12647. }
  12648. /**
  12649. * Get position for BL objects.
  12650. * getmapxy(<map name>,<x>,<y>,<type>{,<char name>});
  12651. * @param mapname: String variable for output map name
  12652. * @param x: Integer variable for output coord X
  12653. * @param y: Integer variable for output coord Y
  12654. * @param type: Type of object
  12655. * UNITTYPE_PC - Character coord
  12656. * UNITTYPE_NPC - NPC coord
  12657. * UNITTYPE_PET - Pet coord
  12658. * UNITTYPE_HOM - Homun coord
  12659. * UNITTYPE_MER - Mercenary coord
  12660. * UNITTYPE_ELEM - Elemental coord
  12661. * @param charname: Name object. If empty or "this" use the current object
  12662. * @return 0 - success; -1 - some error, MapName$,MapX,MapY contains unknown value.
  12663. */
  12664. BUILDIN_FUNC(getmapxy)
  12665. {
  12666. struct block_list *bl = NULL;
  12667. TBL_PC *sd=NULL;
  12668. int64 num;
  12669. const char *name;
  12670. char prefix;
  12671. int x,y,type;
  12672. char mapname[MAP_NAME_LENGTH];
  12673. if( !data_isreference(script_getdata(st,2)) ) {
  12674. ShowWarning("script: buildin_getmapxy: mapname value is not a variable.\n");
  12675. script_pushint(st,-1);
  12676. return SCRIPT_CMD_FAILURE;
  12677. }
  12678. if( !data_isreference(script_getdata(st,3)) ) {
  12679. ShowWarning("script: buildin_getmapxy: mapx value is not a variable.\n");
  12680. script_pushint(st,-1);
  12681. return SCRIPT_CMD_FAILURE;
  12682. }
  12683. if( !data_isreference(script_getdata(st,4)) ) {
  12684. ShowWarning("script: buildin_getmapxy: mapy value is not a variable.\n");
  12685. script_pushint(st,-1);
  12686. return SCRIPT_CMD_FAILURE;
  12687. }
  12688. if( !is_string_variable(reference_getname(script_getdata(st, 2))) ) {
  12689. ShowWarning("script: buildin_getmapxy: %s is not a string variable.\n",reference_getname(script_getdata(st, 2)));
  12690. script_pushint(st,-1);
  12691. return SCRIPT_CMD_FAILURE;
  12692. }
  12693. if( is_string_variable(reference_getname(script_getdata(st, 3))) ) {
  12694. ShowWarning("script: buildin_getmapxy: %s is a string variable, should be an INT.\n",reference_getname(script_getdata(st, 3)));
  12695. script_pushint(st,-1);
  12696. return SCRIPT_CMD_FAILURE;
  12697. }
  12698. if( is_string_variable(reference_getname(script_getdata(st, 4))) ) {
  12699. ShowWarning("script: buildin_getmapxy: %s is a string variable, should be an INT.\n",reference_getname(script_getdata(st, 4)));
  12700. script_pushint(st,-1);
  12701. return SCRIPT_CMD_FAILURE;
  12702. }
  12703. // Possible needly check function parameters on C_STR,C_INT,C_INT
  12704. type=script_getnum(st,5);
  12705. switch (type) {
  12706. case UNITTYPE_PC: //Get Character Position
  12707. if( script_hasdata(st,6) )
  12708. sd=map_nick2sd(script_getstr(st,6));
  12709. else
  12710. sd=script_rid2sd(st);
  12711. if (sd)
  12712. bl = &sd->bl;
  12713. break;
  12714. case UNITTYPE_NPC: //Get NPC Position
  12715. if( script_hasdata(st,6) )
  12716. {
  12717. struct npc_data *nd;
  12718. nd=npc_name2id(script_getstr(st,6));
  12719. if (nd)
  12720. bl = &nd->bl;
  12721. } else //In case the origin is not an npc?
  12722. bl=map_id2bl(st->oid);
  12723. break;
  12724. case UNITTYPE_PET: //Get Pet Position
  12725. if(script_hasdata(st,6))
  12726. sd=map_nick2sd(script_getstr(st,6));
  12727. else
  12728. sd=script_rid2sd(st);
  12729. if (sd && sd->pd)
  12730. bl = &sd->pd->bl;
  12731. break;
  12732. case UNITTYPE_HOM: //Get Homun Position
  12733. if(script_hasdata(st,6))
  12734. sd=map_nick2sd(script_getstr(st,6));
  12735. else
  12736. sd=script_rid2sd(st);
  12737. if (sd && sd->hd)
  12738. bl = &sd->hd->bl;
  12739. break;
  12740. case UNITTYPE_MER: //Get Mercenary Position
  12741. if(script_hasdata(st,6))
  12742. sd=map_nick2sd(script_getstr(st,6));
  12743. else
  12744. sd=script_rid2sd(st);
  12745. if (sd && sd->md)
  12746. bl = &sd->md->bl;
  12747. break;
  12748. case UNITTYPE_ELEM: //Get Elemental Position
  12749. if(script_hasdata(st,6))
  12750. sd=map_nick2sd(script_getstr(st,6));
  12751. else
  12752. sd=script_rid2sd(st);
  12753. if (sd && sd->ed)
  12754. bl = &sd->ed->bl;
  12755. break;
  12756. default:
  12757. ShowWarning("script: buildin_getmapxy: Invalid type %d.\n", type);
  12758. script_pushint(st,-1);
  12759. return SCRIPT_CMD_FAILURE;
  12760. }
  12761. if (!bl) { //No object found.
  12762. script_pushint(st,-1);
  12763. return SCRIPT_CMD_SUCCESS;
  12764. }
  12765. x= bl->x;
  12766. y= bl->y;
  12767. safestrncpy(mapname, map[bl->m].name, MAP_NAME_LENGTH);
  12768. //Set MapName$
  12769. num=st->stack->stack_data[st->start+2].u.num;
  12770. name=get_str(script_getvarid(num));
  12771. prefix=*name;
  12772. if(not_server_variable(prefix))
  12773. sd=script_rid2sd(st);
  12774. else
  12775. sd=NULL;
  12776. set_reg(st,sd,num,name,(void*)mapname,script_getref(st,2));
  12777. //Set MapX
  12778. num=st->stack->stack_data[st->start+3].u.num;
  12779. name=get_str(script_getvarid(num));
  12780. prefix=*name;
  12781. if(not_server_variable(prefix))
  12782. sd=script_rid2sd(st);
  12783. else
  12784. sd=NULL;
  12785. set_reg(st,sd,num,name,(void*)__64BPRTSIZE(x),script_getref(st,3));
  12786. //Set MapY
  12787. num=st->stack->stack_data[st->start+4].u.num;
  12788. name=get_str(script_getvarid(num));
  12789. prefix=*name;
  12790. if(not_server_variable(prefix))
  12791. sd=script_rid2sd(st);
  12792. else
  12793. sd=NULL;
  12794. set_reg(st,sd,num,name,(void*)__64BPRTSIZE(y),script_getref(st,4));
  12795. //Return Success value
  12796. script_pushint(st,0);
  12797. return SCRIPT_CMD_SUCCESS;
  12798. }
  12799. /// Returns the map name of given map ID.
  12800. ///
  12801. /// mapid2name <map ID>;
  12802. BUILDIN_FUNC(mapid2name)
  12803. {
  12804. uint16 m = script_getnum(st, 2);
  12805. if (m < 0 || m >= MAX_MAP_PER_SERVER) {
  12806. script_pushstr(st, "");
  12807. return SCRIPT_CMD_FAILURE;
  12808. }
  12809. script_pushstrcopy(st, map_mapid2mapname(m));
  12810. return SCRIPT_CMD_SUCCESS;
  12811. }
  12812. /*==========================================
  12813. * Allows player to write NPC logs (i.e. Bank NPC, etc) [Lupus]
  12814. *------------------------------------------*/
  12815. BUILDIN_FUNC(logmes)
  12816. {
  12817. const char *str;
  12818. TBL_PC* sd;
  12819. sd = script_rid2sd(st);
  12820. if( sd == NULL )
  12821. return SCRIPT_CMD_FAILURE;
  12822. str = script_getstr(st,2);
  12823. log_npc(sd,str);
  12824. return SCRIPT_CMD_SUCCESS;
  12825. }
  12826. BUILDIN_FUNC(summon)
  12827. {
  12828. int _class, timeout=0;
  12829. const char *str,*event="";
  12830. TBL_PC *sd;
  12831. struct mob_data *md;
  12832. int tick = gettick();
  12833. sd=script_rid2sd(st);
  12834. if (!sd)
  12835. return SCRIPT_CMD_SUCCESS;
  12836. str =script_getstr(st,2);
  12837. _class=script_getnum(st,3);
  12838. if( script_hasdata(st,4) )
  12839. timeout=script_getnum(st,4);
  12840. if( script_hasdata(st,5) ){
  12841. event=script_getstr(st,5);
  12842. check_event(st, event);
  12843. }
  12844. clif_skill_poseffect(&sd->bl,AM_CALLHOMUN,1,sd->bl.x,sd->bl.y,tick);
  12845. md = mob_once_spawn_sub(&sd->bl, sd->bl.m, sd->bl.x, sd->bl.y, str, _class, event, SZ_SMALL, AI_NONE);
  12846. if (md) {
  12847. md->master_id=sd->bl.id;
  12848. md->special_state.ai = AI_ATTACK;
  12849. if( md->deletetimer != INVALID_TIMER )
  12850. delete_timer(md->deletetimer, mob_timer_delete);
  12851. md->deletetimer = add_timer(tick+(timeout>0?timeout:60000),mob_timer_delete,md->bl.id,0);
  12852. mob_spawn (md); //Now it is ready for spawning.
  12853. clif_specialeffect(&md->bl,344,AREA);
  12854. sc_start4(NULL,&md->bl, SC_MODECHANGE, 100, 1, 0, MD_AGGRESSIVE, 0, 60000);
  12855. }
  12856. script_pushint(st, md->bl.id);
  12857. return SCRIPT_CMD_SUCCESS;
  12858. }
  12859. /*==========================================
  12860. * Checks whether it is daytime/nighttime
  12861. *------------------------------------------*/
  12862. BUILDIN_FUNC(isnight)
  12863. {
  12864. script_pushint(st,(night_flag == 1));
  12865. return SCRIPT_CMD_SUCCESS;
  12866. }
  12867. BUILDIN_FUNC(isday)
  12868. {
  12869. script_pushint(st,(night_flag == 0));
  12870. return SCRIPT_CMD_SUCCESS;
  12871. }
  12872. /*================================================
  12873. * Check how many items/cards in the list are
  12874. * equipped - used for 2/15's cards patch [celest]
  12875. *------------------------------------------------*/
  12876. BUILDIN_FUNC(isequippedcnt)
  12877. {
  12878. TBL_PC *sd;
  12879. int i, id = 1;
  12880. int ret = 0;
  12881. sd = script_rid2sd(st);
  12882. if (!sd) { //If the player is not attached it is a script error anyway... but better prevent the map server from crashing...
  12883. script_pushint(st,0);
  12884. return SCRIPT_CMD_SUCCESS;
  12885. }
  12886. for (i=0; id!=0; i++) {
  12887. short j;
  12888. FETCH (i+2, id) else id = 0;
  12889. if (id <= 0)
  12890. continue;
  12891. for (j=0; j<EQI_MAX; j++) {
  12892. short index = sd->equip_index[j];
  12893. if(index < 0)
  12894. continue;
  12895. if (pc_is_same_equip_index((enum equip_index)j, sd->equip_index, index))
  12896. continue;
  12897. if(!sd->inventory_data[index])
  12898. continue;
  12899. if (itemdb_type(id) != IT_CARD) { //No card. Count amount in inventory.
  12900. if (sd->inventory_data[index]->nameid == id)
  12901. ret+= sd->inventory.u.items_inventory[index].amount;
  12902. } else { //Count cards.
  12903. short k;
  12904. if (itemdb_isspecial(sd->inventory.u.items_inventory[index].card[0]))
  12905. continue; //No cards
  12906. for(k=0; k<sd->inventory_data[index]->slot; k++) {
  12907. if (sd->inventory.u.items_inventory[index].card[k] == id)
  12908. ret++; //[Lupus]
  12909. }
  12910. }
  12911. }
  12912. }
  12913. script_pushint(st,ret);
  12914. return SCRIPT_CMD_SUCCESS;
  12915. }
  12916. /*================================================
  12917. * Check whether another card has been
  12918. * equipped - used for 2/15's cards patch [celest]
  12919. * -- Items checked cannot be reused in another
  12920. * card set to prevent exploits
  12921. *------------------------------------------------*/
  12922. BUILDIN_FUNC(isequipped)
  12923. {
  12924. TBL_PC *sd;
  12925. int i, id = 1;
  12926. int ret = -1;
  12927. //Original hash to reverse it when full check fails.
  12928. unsigned int setitem_hash = 0, setitem_hash2 = 0;
  12929. sd = script_rid2sd(st);
  12930. if (!sd) { //If the player is not attached it is a script error anyway... but better prevent the map server from crashing...
  12931. script_pushint(st,0);
  12932. return SCRIPT_CMD_SUCCESS;
  12933. }
  12934. setitem_hash = sd->bonus.setitem_hash;
  12935. setitem_hash2 = sd->bonus.setitem_hash2;
  12936. for (i=0; id!=0; i++) {
  12937. int flag = 0;
  12938. short j;
  12939. FETCH (i+2, id) else id = 0;
  12940. if (id <= 0)
  12941. continue;
  12942. for (j=0; j<EQI_MAX; j++) {
  12943. short index = sd->equip_index[j];
  12944. if(index < 0)
  12945. continue;
  12946. if (pc_is_same_equip_index((enum equip_index)i, sd->equip_index, index))
  12947. continue;
  12948. if(!sd->inventory_data[index])
  12949. continue;
  12950. if (itemdb_type(id) != IT_CARD) {
  12951. if (sd->inventory_data[index]->nameid != id)
  12952. continue;
  12953. flag = 1;
  12954. break;
  12955. } else { //Cards
  12956. short k;
  12957. if (sd->inventory_data[index]->slot == 0 ||
  12958. itemdb_isspecial(sd->inventory.u.items_inventory[index].card[0]))
  12959. continue;
  12960. for (k = 0; k < sd->inventory_data[index]->slot; k++)
  12961. { //New hash system which should support up to 4 slots on any equipment. [Skotlex]
  12962. unsigned int hash = 0;
  12963. if (sd->inventory.u.items_inventory[index].card[k] != id)
  12964. continue;
  12965. hash = 1<<((j<5?j:j-5)*4 + k);
  12966. // check if card is already used by another set
  12967. if ( ( j < 5 ? sd->bonus.setitem_hash : sd->bonus.setitem_hash2 ) & hash)
  12968. continue;
  12969. // We have found a match
  12970. flag = 1;
  12971. // Set hash so this card cannot be used by another
  12972. if (j<5)
  12973. sd->bonus.setitem_hash |= hash;
  12974. else
  12975. sd->bonus.setitem_hash2 |= hash;
  12976. break;
  12977. }
  12978. }
  12979. if (flag) break; //Card found
  12980. }
  12981. if (ret == -1)
  12982. ret = flag;
  12983. else
  12984. ret &= flag;
  12985. if (!ret) break;
  12986. }
  12987. if (!ret) {//When check fails, restore original hash values. [Skotlex]
  12988. sd->bonus.setitem_hash = setitem_hash;
  12989. sd->bonus.setitem_hash2 = setitem_hash2;
  12990. }
  12991. script_pushint(st,ret);
  12992. return SCRIPT_CMD_SUCCESS;
  12993. }
  12994. /*================================================
  12995. * Check how many given inserted cards in the CURRENT
  12996. * weapon - used for 2/15's cards patch [Lupus]
  12997. *------------------------------------------------*/
  12998. BUILDIN_FUNC(cardscnt)
  12999. {
  13000. TBL_PC *sd;
  13001. int i, k, id = 1;
  13002. int ret = 0;
  13003. int index;
  13004. sd = script_rid2sd(st);
  13005. for (i=0; id!=0; i++) {
  13006. FETCH (i+2, id) else id = 0;
  13007. if (id <= 0)
  13008. continue;
  13009. index = current_equip_item_index; //we get CURRENT WEAPON inventory index from status.c [Lupus]
  13010. if(index < 0) continue;
  13011. if(!sd->inventory_data[index])
  13012. continue;
  13013. if(itemdb_type(id) != IT_CARD) {
  13014. if (sd->inventory_data[index]->nameid == id)
  13015. ret+= sd->inventory.u.items_inventory[index].amount;
  13016. } else {
  13017. if (itemdb_isspecial(sd->inventory.u.items_inventory[index].card[0]))
  13018. continue;
  13019. for(k=0; k<sd->inventory_data[index]->slot; k++) {
  13020. if (sd->inventory.u.items_inventory[index].card[k] == id)
  13021. ret++;
  13022. }
  13023. }
  13024. }
  13025. script_pushint(st,ret);
  13026. // script_pushint(st,current_equip_item_index);
  13027. return SCRIPT_CMD_SUCCESS;
  13028. }
  13029. /*=======================================================
  13030. * Returns the refined number of the current item, or an
  13031. * item with inventory index specified
  13032. *-------------------------------------------------------*/
  13033. BUILDIN_FUNC(getrefine)
  13034. {
  13035. TBL_PC *sd;
  13036. if ((sd = script_rid2sd(st))!= NULL)
  13037. script_pushint(st,sd->inventory.u.items_inventory[current_equip_item_index].refine);
  13038. else
  13039. script_pushint(st,0);
  13040. return SCRIPT_CMD_SUCCESS;
  13041. }
  13042. /*=======================================================
  13043. * Day/Night controls
  13044. *-------------------------------------------------------*/
  13045. BUILDIN_FUNC(night)
  13046. {
  13047. if (night_flag != 1) map_night_timer(night_timer_tid, 0, 0, 1);
  13048. return SCRIPT_CMD_SUCCESS;
  13049. }
  13050. BUILDIN_FUNC(day)
  13051. {
  13052. if (night_flag != 0) map_day_timer(day_timer_tid, 0, 0, 1);
  13053. return SCRIPT_CMD_SUCCESS;
  13054. }
  13055. /**
  13056. * unequip <equipment slot>{,<char_id>};
  13057. * @author [Spectre]
  13058. **/
  13059. BUILDIN_FUNC(unequip) {
  13060. int pos;
  13061. TBL_PC *sd;
  13062. if (!script_charid2sd(3,sd))
  13063. return SCRIPT_CMD_FAILURE;
  13064. pos = script_getnum(st,2);
  13065. if (pos >= 1 && pos <= ARRAYLENGTH(equip)) {
  13066. short i = pc_checkequip(sd,equip[pos-1]);
  13067. if (i >= 0) {
  13068. pc_unequipitem(sd,i,1|2);
  13069. script_pushint(st, 1);
  13070. return SCRIPT_CMD_SUCCESS;
  13071. }
  13072. }
  13073. ShowError("buildin_unequip: No item equipped at pos %d (CID=%d/AID=%d).\n", pos, sd->status.char_id, sd->status.account_id);
  13074. script_pushint(st, 0);
  13075. return SCRIPT_CMD_FAILURE;
  13076. }
  13077. /**
  13078. * equip <item id>{,<char_id>};
  13079. **/
  13080. BUILDIN_FUNC(equip) {
  13081. unsigned short nameid = 0;
  13082. TBL_PC *sd;
  13083. struct item_data *item_data;
  13084. if (!script_charid2sd(3,sd))
  13085. return SCRIPT_CMD_FAILURE;
  13086. nameid = script_getnum(st,2);
  13087. if ((item_data = itemdb_exists(nameid))) {
  13088. int i;
  13089. ARR_FIND( 0, MAX_INVENTORY, i, sd->inventory.u.items_inventory[i].nameid == nameid );
  13090. if (i < MAX_INVENTORY) {
  13091. pc_equipitem(sd,i,item_data->equip);
  13092. script_pushint(st,1);
  13093. return SCRIPT_CMD_SUCCESS;
  13094. }
  13095. }
  13096. ShowError("buildin_equip: Item %hu cannot be equipped\n",nameid);
  13097. script_pushint(st,0);
  13098. return SCRIPT_CMD_FAILURE;
  13099. }
  13100. BUILDIN_FUNC(autoequip)
  13101. {
  13102. unsigned short nameid;
  13103. int flag;
  13104. struct item_data *item_data;
  13105. nameid=script_getnum(st,2);
  13106. flag=script_getnum(st,3);
  13107. if( ( item_data = itemdb_exists(nameid) ) == NULL )
  13108. {
  13109. ShowError("buildin_autoequip: Invalid item '%hu'.\n", nameid);
  13110. return SCRIPT_CMD_FAILURE;
  13111. }
  13112. if( !itemdb_isequip2(item_data) )
  13113. {
  13114. ShowError("buildin_autoequip: Item '%hu' cannot be equipped.\n", nameid);
  13115. return SCRIPT_CMD_FAILURE;
  13116. }
  13117. item_data->flag.autoequip = flag>0?1:0;
  13118. return SCRIPT_CMD_SUCCESS;
  13119. }
  13120. BUILDIN_FUNC(setbattleflag)
  13121. {
  13122. const char *flag, *value;
  13123. flag = script_getstr(st,2);
  13124. value = script_getstr(st,3); // HACK: Retrieve number as string (auto-converted) for battle_set_value
  13125. if (battle_set_value(flag, value) == 0)
  13126. ShowWarning("buildin_setbattleflag: unknown battle_config flag '%s'\n",flag);
  13127. else
  13128. ShowInfo("buildin_setbattleflag: battle_config flag '%s' is now set to '%s'.\n",flag,value);
  13129. return SCRIPT_CMD_SUCCESS;
  13130. }
  13131. BUILDIN_FUNC(getbattleflag)
  13132. {
  13133. const char *flag;
  13134. flag = script_getstr(st,2);
  13135. script_pushint(st,battle_get_value(flag));
  13136. return SCRIPT_CMD_SUCCESS;
  13137. }
  13138. //=======================================================
  13139. // strlen [Valaris]
  13140. //-------------------------------------------------------
  13141. BUILDIN_FUNC(getstrlen)
  13142. {
  13143. const char *str = script_getstr(st,2);
  13144. int len = (str) ? (int)strlen(str) : 0;
  13145. script_pushint(st,len);
  13146. return SCRIPT_CMD_SUCCESS;
  13147. }
  13148. //=======================================================
  13149. // isalpha [Valaris]
  13150. //-------------------------------------------------------
  13151. BUILDIN_FUNC(charisalpha)
  13152. {
  13153. const char *str=script_getstr(st,2);
  13154. int pos=script_getnum(st,3);
  13155. int val = ( str && pos >= 0 && (unsigned int)pos < strlen(str) ) ? ISALPHA( str[pos] ) != 0 : 0;
  13156. script_pushint(st,val);
  13157. return SCRIPT_CMD_SUCCESS;
  13158. }
  13159. //=======================================================
  13160. // charisupper <str>, <index>
  13161. //-------------------------------------------------------
  13162. BUILDIN_FUNC(charisupper)
  13163. {
  13164. const char *str = script_getstr(st,2);
  13165. int pos = script_getnum(st,3);
  13166. int val = ( str && pos >= 0 && (unsigned int)pos < strlen(str) ) ? ISUPPER( str[pos] ) : 0;
  13167. script_pushint(st,val);
  13168. return SCRIPT_CMD_SUCCESS;
  13169. }
  13170. //=======================================================
  13171. // charislower <str>, <index>
  13172. //-------------------------------------------------------
  13173. BUILDIN_FUNC(charislower)
  13174. {
  13175. const char *str = script_getstr(st,2);
  13176. int pos = script_getnum(st,3);
  13177. int val = ( str && pos >= 0 && (unsigned int)pos < strlen(str) ) ? ISLOWER( str[pos] ) : 0;
  13178. script_pushint(st,val);
  13179. return SCRIPT_CMD_SUCCESS;
  13180. }
  13181. //=======================================================
  13182. // charat <str>, <index>
  13183. //-------------------------------------------------------
  13184. BUILDIN_FUNC(charat) {
  13185. const char *str = script_getstr(st,2);
  13186. int pos = script_getnum(st,3);
  13187. if( pos >= 0 && (unsigned int)pos < strlen(str) ) {
  13188. char output[2];
  13189. output[0] = str[pos];
  13190. output[1] = '\0';
  13191. script_pushstrcopy(st, output);
  13192. } else
  13193. script_pushconststr(st, "");
  13194. return SCRIPT_CMD_SUCCESS;
  13195. }
  13196. //=======================================================
  13197. // setchar <string>, <char>, <index>
  13198. //-------------------------------------------------------
  13199. BUILDIN_FUNC(setchar)
  13200. {
  13201. const char *str = script_getstr(st,2);
  13202. const char *c = script_getstr(st,3);
  13203. int index = script_getnum(st,4);
  13204. char *output = aStrdup(str);
  13205. if(index >= 0 && index < strlen(output))
  13206. output[index] = *c;
  13207. script_pushstr(st, output);
  13208. return SCRIPT_CMD_SUCCESS;
  13209. }
  13210. //=======================================================
  13211. // insertchar <string>, <char>, <index>
  13212. //-------------------------------------------------------
  13213. BUILDIN_FUNC(insertchar)
  13214. {
  13215. const char *str = script_getstr(st,2);
  13216. const char *c = script_getstr(st,3);
  13217. int index = script_getnum(st,4);
  13218. char *output;
  13219. size_t len = strlen(str);
  13220. if(index < 0)
  13221. index = 0;
  13222. else if(index > len)
  13223. index = len;
  13224. output = (char*)aMalloc(len + 2);
  13225. memcpy(output, str, index);
  13226. output[index] = c[0];
  13227. memcpy(&output[index+1], &str[index], len - index);
  13228. output[len+1] = '\0';
  13229. script_pushstr(st, output);
  13230. return SCRIPT_CMD_SUCCESS;
  13231. }
  13232. //=======================================================
  13233. // delchar <string>, <index>
  13234. //-------------------------------------------------------
  13235. BUILDIN_FUNC(delchar)
  13236. {
  13237. const char *str = script_getstr(st,2);
  13238. int index = script_getnum(st,3);
  13239. char *output;
  13240. size_t len = strlen(str);
  13241. if(index < 0 || index > len) {
  13242. //return original
  13243. output = aStrdup(str);
  13244. script_pushstr(st, output);
  13245. return SCRIPT_CMD_SUCCESS;
  13246. }
  13247. output = (char*)aMalloc(len);
  13248. memcpy(output, str, index);
  13249. memcpy(&output[index], &str[index+1], len - index);
  13250. script_pushstr(st, output);
  13251. return SCRIPT_CMD_SUCCESS;
  13252. }
  13253. //=======================================================
  13254. // strtoupper <str>
  13255. //-------------------------------------------------------
  13256. BUILDIN_FUNC(strtoupper)
  13257. {
  13258. const char *str = script_getstr(st,2);
  13259. char *output = aStrdup(str);
  13260. char *cursor = output;
  13261. while (*cursor != '\0') {
  13262. *cursor = TOUPPER(*cursor);
  13263. cursor++;
  13264. }
  13265. script_pushstr(st, output);
  13266. return SCRIPT_CMD_SUCCESS;
  13267. }
  13268. //=======================================================
  13269. // strtolower <str>
  13270. //-------------------------------------------------------
  13271. BUILDIN_FUNC(strtolower)
  13272. {
  13273. const char *str = script_getstr(st,2);
  13274. char *output = aStrdup(str);
  13275. char *cursor = output;
  13276. while (*cursor != '\0') {
  13277. *cursor = TOLOWER(*cursor);
  13278. cursor++;
  13279. }
  13280. script_pushstr(st, output);
  13281. return SCRIPT_CMD_SUCCESS;
  13282. }
  13283. //=======================================================
  13284. // substr <str>, <start>, <end>
  13285. //-------------------------------------------------------
  13286. BUILDIN_FUNC(substr)
  13287. {
  13288. const char *str = script_getstr(st,2);
  13289. char *output;
  13290. int start = script_getnum(st,3);
  13291. int end = script_getnum(st,4);
  13292. int len = 0;
  13293. if(start >= 0 && end < strlen(str) && start <= end) {
  13294. len = end - start + 1;
  13295. output = (char*)aMalloc(len + 1);
  13296. memcpy(output, &str[start], len);
  13297. } else
  13298. output = (char*)aMalloc(1);
  13299. output[len] = '\0';
  13300. script_pushstr(st, output);
  13301. return SCRIPT_CMD_SUCCESS;
  13302. }
  13303. //=======================================================
  13304. // explode <dest_string_array>, <str>, <delimiter>
  13305. // Note: delimiter is limited to 1 char
  13306. //-------------------------------------------------------
  13307. BUILDIN_FUNC(explode)
  13308. {
  13309. struct script_data* data = script_getdata(st, 2);
  13310. const char *str = script_getstr(st,3);
  13311. const char delimiter = script_getstr(st, 4)[0];
  13312. int32 id;
  13313. size_t len = strlen(str);
  13314. int i = 0, j = 0;
  13315. int start;
  13316. char *temp;
  13317. const char* name;
  13318. TBL_PC* sd = NULL;
  13319. temp = (char*)aMalloc(len + 1);
  13320. if( !data_isreference(data) ) {
  13321. ShowError("script:explode: not a variable\n");
  13322. script_reportdata(data);
  13323. st->state = END;
  13324. return SCRIPT_CMD_FAILURE;// not a variable
  13325. }
  13326. id = reference_getid(data);
  13327. start = reference_getindex(data);
  13328. name = reference_getname(data);
  13329. if( !is_string_variable(name) ) {
  13330. ShowError("script:explode: not string array\n");
  13331. script_reportdata(data);
  13332. st->state = END;
  13333. return SCRIPT_CMD_FAILURE;// data type mismatch
  13334. }
  13335. if( not_server_variable(*name) ) {
  13336. sd = script_rid2sd(st);
  13337. if( sd == NULL )
  13338. return SCRIPT_CMD_SUCCESS;// no player attached
  13339. }
  13340. while(str[i] != '\0') {
  13341. if(str[i] == delimiter && start < SCRIPT_MAX_ARRAYSIZE-1) { //break at delimiter but ignore after reaching last array index
  13342. temp[j] = '\0';
  13343. set_reg(st, sd, reference_uid(id, start++), name, (void*)temp, reference_getref(data));
  13344. j = 0;
  13345. ++i;
  13346. } else {
  13347. temp[j++] = str[i++];
  13348. }
  13349. }
  13350. //set last string
  13351. temp[j] = '\0';
  13352. set_reg(st, sd, reference_uid(id, start), name, (void*)temp, reference_getref(data));
  13353. aFree(temp);
  13354. return SCRIPT_CMD_SUCCESS;
  13355. }
  13356. //=======================================================
  13357. // implode <string_array>
  13358. // implode <string_array>, <glue>
  13359. //-------------------------------------------------------
  13360. BUILDIN_FUNC(implode)
  13361. {
  13362. struct script_data* data = script_getdata(st, 2);
  13363. const char *name;
  13364. uint32 glue_len = 0, array_size, id;
  13365. char *output;
  13366. TBL_PC* sd = NULL;
  13367. if( !data_isreference(data) ) {
  13368. ShowError("script:implode: not a variable\n");
  13369. script_reportdata(data);
  13370. st->state = END;
  13371. return SCRIPT_CMD_FAILURE;// not a variable
  13372. }
  13373. id = reference_getid(data);
  13374. name = reference_getname(data);
  13375. if( !is_string_variable(name) ) {
  13376. ShowError("script:implode: not string array\n");
  13377. script_reportdata(data);
  13378. st->state = END;
  13379. return SCRIPT_CMD_FAILURE;// data type mismatch
  13380. }
  13381. if( not_server_variable(*name) ) {
  13382. sd = script_rid2sd(st);
  13383. if( sd == NULL )
  13384. return SCRIPT_CMD_SUCCESS;// no player attached
  13385. }
  13386. //count chars
  13387. array_size = script_array_highest_key(st, sd, name, reference_getref(data)) - 1;
  13388. if(array_size == -1) { //empty array check (AmsTaff)
  13389. ShowWarning("script:implode: array length = 0\n");
  13390. output = (char*)aMalloc(sizeof(char)*5);
  13391. sprintf(output,"%s","NULL");
  13392. } else {
  13393. const char *glue = NULL, *temp;
  13394. size_t len = 0;
  13395. int i, k = 0;
  13396. for(i = 0; i <= array_size; ++i) {
  13397. temp = (char*) get_val2(st, reference_uid(id, i), reference_getref(data));
  13398. len += strlen(temp);
  13399. script_removetop(st, -1, 0);
  13400. }
  13401. //allocate mem
  13402. if( script_hasdata(st,3) ) {
  13403. glue = script_getstr(st,3);
  13404. glue_len = strlen(glue);
  13405. len += glue_len * (array_size);
  13406. }
  13407. output = (char*)aMalloc(len + 1);
  13408. //build output
  13409. for(i = 0; i < array_size; ++i) {
  13410. temp = (char*) get_val2(st, reference_uid(id, i), reference_getref(data));
  13411. len = strlen(temp);
  13412. memcpy(&output[k], temp, len);
  13413. k += len;
  13414. if(glue_len != 0) {
  13415. memcpy(&output[k], glue, glue_len);
  13416. k += glue_len;
  13417. }
  13418. script_removetop(st, -1, 0);
  13419. }
  13420. temp = (char*) get_val2(st, reference_uid(id, array_size), reference_getref(data));
  13421. len = strlen(temp);
  13422. memcpy(&output[k], temp, len);
  13423. k += len;
  13424. script_removetop(st, -1, 0);
  13425. output[k] = '\0';
  13426. }
  13427. script_pushstr(st, output);
  13428. return SCRIPT_CMD_SUCCESS;
  13429. }
  13430. //=======================================================
  13431. // sprintf(<format>, ...);
  13432. // Implements C sprintf, except format %n. The resulting string is
  13433. // returned, instead of being saved in variable by reference.
  13434. //-------------------------------------------------------
  13435. BUILDIN_FUNC(sprintf)
  13436. {
  13437. unsigned int len, argc = 0, arg = 0, buf2_len = 0;
  13438. const char* format;
  13439. char* p;
  13440. char* q;
  13441. char* buf = NULL;
  13442. char* buf2 = NULL;
  13443. struct script_data* data;
  13444. StringBuf final_buf;
  13445. // Fetch init data
  13446. format = script_getstr(st, 2);
  13447. argc = script_lastdata(st)-2;
  13448. len = strlen(format);
  13449. // Skip parsing, where no parsing is required.
  13450. if(len == 0) {
  13451. script_pushconststr(st,"");
  13452. return SCRIPT_CMD_SUCCESS;
  13453. }
  13454. // Pessimistic alloc
  13455. CREATE(buf, char, len+1);
  13456. // Need not be parsed, just solve stuff like %%.
  13457. if(argc == 0) {
  13458. memcpy(buf,format,len+1);
  13459. script_pushstrcopy(st, buf);
  13460. aFree(buf);
  13461. return SCRIPT_CMD_SUCCESS;
  13462. }
  13463. safestrncpy(buf, format, len+1);
  13464. // Issue sprintf for each parameter
  13465. StringBuf_Init(&final_buf);
  13466. q = buf;
  13467. while((p = strchr(q, '%')) != NULL) {
  13468. if(p != q) {
  13469. len = p - q + 1;
  13470. if(buf2_len < len) {
  13471. RECREATE(buf2, char, len);
  13472. buf2_len = len;
  13473. }
  13474. safestrncpy(buf2, q, len);
  13475. StringBuf_AppendStr(&final_buf, buf2);
  13476. q = p;
  13477. }
  13478. p = q + 1;
  13479. if(*p == '%') { // %%
  13480. StringBuf_AppendStr(&final_buf, "%");
  13481. q+=2;
  13482. continue;
  13483. }
  13484. if(*p == 'n') { // %n
  13485. ShowWarning("buildin_sprintf: Format %%n not supported! Skipping...\n");
  13486. script_reportsrc(st);
  13487. q+=2;
  13488. continue;
  13489. }
  13490. if(arg >= argc) {
  13491. ShowError("buildin_sprintf: Not enough arguments passed!\n");
  13492. if(buf) aFree(buf);
  13493. if(buf2) aFree(buf2);
  13494. StringBuf_Destroy(&final_buf);
  13495. script_pushconststr(st,"");
  13496. return SCRIPT_CMD_FAILURE;
  13497. }
  13498. if((p = strchr(q+1, '%')) == NULL)
  13499. p = strchr(q, 0); // EOS
  13500. len = p - q + 1;
  13501. if(buf2_len < len) {
  13502. RECREATE(buf2, char, len);
  13503. buf2_len = len;
  13504. }
  13505. safestrncpy(buf2, q, len);
  13506. q = p;
  13507. // Note: This assumes the passed value being the correct
  13508. // type to the current format specifier. If not, the server
  13509. // probably crashes or returns anything else, than expected,
  13510. // but it would behave in normal code the same way so it's
  13511. // the scripter's responsibility.
  13512. data = script_getdata(st, arg+3);
  13513. if(data_isstring(data)) // String
  13514. StringBuf_Printf(&final_buf, buf2, script_getstr(st, arg+3));
  13515. else if(data_isint(data)) // Number
  13516. StringBuf_Printf(&final_buf, buf2, script_getnum(st, arg+3));
  13517. else if(data_isreference(data)) { // Variable
  13518. char* name = reference_getname(data);
  13519. if(name[strlen(name)-1]=='$') // var Str
  13520. StringBuf_Printf(&final_buf, buf2, script_getstr(st, arg+3));
  13521. else // var Int
  13522. StringBuf_Printf(&final_buf, buf2, script_getnum(st, arg+3));
  13523. } else { // Unsupported type
  13524. ShowError("buildin_sprintf: Unknown argument type!\n");
  13525. if(buf) aFree(buf);
  13526. if(buf2) aFree(buf2);
  13527. StringBuf_Destroy(&final_buf);
  13528. script_pushconststr(st,"");
  13529. return SCRIPT_CMD_FAILURE;
  13530. }
  13531. arg++;
  13532. }
  13533. // Append anything left
  13534. if(*q)
  13535. StringBuf_AppendStr(&final_buf, q);
  13536. // Passed more, than needed
  13537. if(arg < argc) {
  13538. ShowWarning("buildin_sprintf: Unused arguments passed.\n");
  13539. script_reportsrc(st);
  13540. }
  13541. script_pushstrcopy(st, StringBuf_Value(&final_buf));
  13542. if(buf) aFree(buf);
  13543. if(buf2) aFree(buf2);
  13544. StringBuf_Destroy(&final_buf);
  13545. return SCRIPT_CMD_SUCCESS;
  13546. }
  13547. //=======================================================
  13548. // sscanf(<str>, <format>, ...);
  13549. // Implements C sscanf.
  13550. //-------------------------------------------------------
  13551. BUILDIN_FUNC(sscanf){
  13552. unsigned int argc, arg = 0, len;
  13553. struct script_data* data;
  13554. struct map_session_data* sd = NULL;
  13555. const char* str;
  13556. const char* format;
  13557. const char* p;
  13558. const char* q;
  13559. char* buf = NULL;
  13560. char* buf_p;
  13561. char* ref_str = NULL;
  13562. int ref_int;
  13563. // Get data
  13564. str = script_getstr(st, 2);
  13565. format = script_getstr(st, 3);
  13566. argc = script_lastdata(st)-3;
  13567. len = strlen(format);
  13568. if (len != 0 && strlen(str) == 0) {
  13569. // If the source string is empty but the format string is not, we return -1
  13570. // according to the C specs. (if the format string is also empty, we shall
  13571. // continue and return 0: 0 conversions took place out of the 0 attempted.)
  13572. script_pushint(st, -1);
  13573. return SCRIPT_CMD_SUCCESS;
  13574. }
  13575. CREATE(buf, char, len*2+1);
  13576. // Issue sscanf for each parameter
  13577. *buf = 0;
  13578. q = format;
  13579. while((p = strchr(q, '%'))){
  13580. if(p!=q){
  13581. strncat(buf, q, (size_t)(p-q));
  13582. q = p;
  13583. }
  13584. p = q+1;
  13585. if(*p=='*' || *p=='%'){ // Skip
  13586. strncat(buf, q, 2);
  13587. q+=2;
  13588. continue;
  13589. }
  13590. if(arg>=argc){
  13591. ShowError("buildin_sscanf: Not enough arguments passed!\n");
  13592. script_pushint(st, -1);
  13593. if(buf) aFree(buf);
  13594. if(ref_str) aFree(ref_str);
  13595. return SCRIPT_CMD_FAILURE;
  13596. }
  13597. if((p = strchr(q+1, '%'))==NULL){
  13598. p = strchr(q, 0); // EOS
  13599. }
  13600. len = p-q;
  13601. strncat(buf, q, len);
  13602. q = p;
  13603. // Validate output
  13604. data = script_getdata(st, arg+4);
  13605. if(!data_isreference(data) || !reference_tovariable(data)){
  13606. ShowError("buildin_sscanf: Target argument is not a variable!\n");
  13607. script_pushint(st, -1);
  13608. if(buf) aFree(buf);
  13609. if(ref_str) aFree(ref_str);
  13610. return SCRIPT_CMD_FAILURE;
  13611. }
  13612. buf_p = reference_getname(data);
  13613. if(not_server_variable(*buf_p) && (sd = script_rid2sd(st))==NULL){
  13614. script_pushint(st, -1);
  13615. if(buf) aFree(buf);
  13616. if(ref_str) aFree(ref_str);
  13617. return SCRIPT_CMD_SUCCESS;
  13618. }
  13619. // Save value if any
  13620. if(buf_p[strlen(buf_p)-1]=='$'){ // String
  13621. if(ref_str==NULL){
  13622. CREATE(ref_str, char, strlen(str)+1);
  13623. }
  13624. if(sscanf(str, buf, ref_str)==0){
  13625. break;
  13626. }
  13627. set_reg(st, sd, reference_uid( reference_getid(data), reference_getindex(data) ), buf_p, (void *)(ref_str), reference_getref(data));
  13628. } else { // Number
  13629. if(sscanf(str, buf, &ref_int)==0){
  13630. break;
  13631. }
  13632. set_reg(st, sd, reference_uid( reference_getid(data), reference_getindex(data) ), buf_p, (void *)__64BPRTSIZE(ref_int), reference_getref(data));
  13633. }
  13634. arg++;
  13635. // Disable used format (%... -> %*...)
  13636. buf_p = strchr(buf, 0);
  13637. memmove(buf_p-len+2, buf_p-len+1, len);
  13638. *(buf_p-len+1) = '*';
  13639. }
  13640. script_pushint(st, arg);
  13641. if(buf) aFree(buf);
  13642. if(ref_str) aFree(ref_str);
  13643. return SCRIPT_CMD_SUCCESS;
  13644. }
  13645. //=======================================================
  13646. // strpos(<haystack>, <needle>)
  13647. // strpos(<haystack>, <needle>, <offset>)
  13648. //
  13649. // Implements PHP style strpos. Adapted from code from
  13650. // http://www.daniweb.com/code/snippet313.html, Dave Sinkula
  13651. //-------------------------------------------------------
  13652. BUILDIN_FUNC(strpos) {
  13653. const char *haystack = script_getstr(st,2);
  13654. const char *needle = script_getstr(st,3);
  13655. int i;
  13656. size_t len;
  13657. if( script_hasdata(st,4) )
  13658. i = script_getnum(st,4);
  13659. else
  13660. i = 0;
  13661. if (needle[0] == '\0') {
  13662. script_pushint(st, -1);
  13663. return SCRIPT_CMD_SUCCESS;
  13664. }
  13665. len = strlen(haystack);
  13666. for ( ; i < len; ++i ) {
  13667. if ( haystack[i] == *needle ) {
  13668. // matched starting char -- loop through remaining chars
  13669. const char *h, *n;
  13670. for ( h = &haystack[i], n = needle; *h && *n; ++h, ++n ) {
  13671. if ( *h != *n ) {
  13672. break;
  13673. }
  13674. }
  13675. if ( !*n ) { // matched all of 'needle' to null termination
  13676. script_pushint(st, i);
  13677. return SCRIPT_CMD_SUCCESS;
  13678. }
  13679. }
  13680. }
  13681. script_pushint(st, -1);
  13682. return SCRIPT_CMD_SUCCESS;
  13683. }
  13684. //===============================================================
  13685. // replacestr <input>, <search>, <replace>{, <usecase>{, <count>}}
  13686. //
  13687. // Note: Finds all instances of <search> in <input> and replaces
  13688. // with <replace>. If specified will only replace as many
  13689. // instances as specified in <count>. By default will be case
  13690. // sensitive.
  13691. //---------------------------------------------------------------
  13692. BUILDIN_FUNC(replacestr)
  13693. {
  13694. const char *input = script_getstr(st, 2);
  13695. const char *find = script_getstr(st, 3);
  13696. const char *replace = script_getstr(st, 4);
  13697. size_t inputlen = strlen(input);
  13698. size_t findlen = strlen(find);
  13699. struct StringBuf output;
  13700. bool usecase = true;
  13701. int count = 0;
  13702. int numFinds = 0;
  13703. int i = 0, f = 0;
  13704. if(findlen == 0) {
  13705. ShowError("script:replacestr: Invalid search length.\n");
  13706. st->state = END;
  13707. return SCRIPT_CMD_FAILURE;
  13708. }
  13709. if(script_hasdata(st, 5)) {
  13710. struct script_data *data = script_getdata(st, 5);
  13711. get_val(st, data); // Convert into value in case of a variable
  13712. if( !data_isstring(data) )
  13713. usecase = script_getnum(st, 5) != 0;
  13714. else {
  13715. ShowError("script:replacestr: Invalid usecase value. Expected int got string\n");
  13716. st->state = END;
  13717. return SCRIPT_CMD_FAILURE;
  13718. }
  13719. }
  13720. if(script_hasdata(st, 6)) {
  13721. count = script_getnum(st, 6);
  13722. if(count == 0) {
  13723. ShowError("script:replacestr: Invalid count value. Expected int got string\n");
  13724. st->state = END;
  13725. return SCRIPT_CMD_FAILURE;
  13726. }
  13727. }
  13728. StringBuf_Init(&output);
  13729. for(; i < inputlen; i++) {
  13730. if(count && count == numFinds) { //found enough, stop looking
  13731. break;
  13732. }
  13733. for(f = 0; f <= findlen; f++) {
  13734. if(f == findlen) { //complete match
  13735. numFinds++;
  13736. StringBuf_AppendStr(&output, replace);
  13737. i += findlen - 1;
  13738. break;
  13739. } else {
  13740. if(usecase) {
  13741. if((i + f) > inputlen || input[i + f] != find[f]) {
  13742. StringBuf_Printf(&output, "%c", input[i]);
  13743. break;
  13744. }
  13745. } else {
  13746. if(((i + f) > inputlen || input[i + f] != find[f]) && TOUPPER(input[i+f]) != TOUPPER(find[f])) {
  13747. StringBuf_Printf(&output, "%c", input[i]);
  13748. break;
  13749. }
  13750. }
  13751. }
  13752. }
  13753. }
  13754. //append excess after enough found
  13755. if(i < inputlen)
  13756. StringBuf_AppendStr(&output, &(input[i]));
  13757. script_pushstrcopy(st, StringBuf_Value(&output));
  13758. StringBuf_Destroy(&output);
  13759. return SCRIPT_CMD_SUCCESS;
  13760. }
  13761. //========================================================
  13762. // countstr <input>, <search>{, <usecase>}
  13763. //
  13764. // Note: Counts the number of times <search> occurs in
  13765. // <input>. By default will be case sensitive.
  13766. //--------------------------------------------------------
  13767. BUILDIN_FUNC(countstr)
  13768. {
  13769. const char *input = script_getstr(st, 2);
  13770. const char *find = script_getstr(st, 3);
  13771. size_t inputlen = strlen(input);
  13772. size_t findlen = strlen(find);
  13773. bool usecase = true;
  13774. int numFinds = 0;
  13775. int i = 0, f = 0;
  13776. if(findlen == 0) {
  13777. ShowError("script:countstr: Invalid search length.\n");
  13778. st->state = END;
  13779. return SCRIPT_CMD_FAILURE;
  13780. }
  13781. if(script_hasdata(st, 4)) {
  13782. struct script_data *data;
  13783. data = script_getdata(st, 4);
  13784. get_val(st, data); // Convert into value in case of a variable
  13785. if( !data_isstring(data) )
  13786. usecase = script_getnum(st, 4) != 0;
  13787. else {
  13788. ShowError("script:countstr: Invalid usecase value. Expected int got string\n");
  13789. st->state = END;
  13790. return SCRIPT_CMD_FAILURE;
  13791. }
  13792. }
  13793. for(; i < inputlen; i++) {
  13794. for(f = 0; f <= findlen; f++) {
  13795. if(f == findlen) { //complete match
  13796. numFinds++;
  13797. i += findlen - 1;
  13798. break;
  13799. } else {
  13800. if(usecase) {
  13801. if((i + f) > inputlen || input[i + f] != find[f]) {
  13802. break;
  13803. }
  13804. } else {
  13805. if(((i + f) > inputlen || input[i + f] != find[f]) && TOUPPER(input[i+f]) != TOUPPER(find[f])) {
  13806. break;
  13807. }
  13808. }
  13809. }
  13810. }
  13811. }
  13812. script_pushint(st, numFinds);
  13813. return SCRIPT_CMD_SUCCESS;
  13814. }
  13815. /// Changes the display name and/or display class of the npc.
  13816. /// Returns 0 is successful, 1 if the npc does not exist.
  13817. ///
  13818. /// setnpcdisplay("<npc name>", "<new display name>", <new class id>, <new size>) -> <int>
  13819. /// setnpcdisplay("<npc name>", "<new display name>", <new class id>) -> <int>
  13820. /// setnpcdisplay("<npc name>", "<new display name>") -> <int>
  13821. /// setnpcdisplay("<npc name>", <new class id>) -> <int>
  13822. BUILDIN_FUNC(setnpcdisplay)
  13823. {
  13824. const char* name;
  13825. const char* newname = NULL;
  13826. int class_ = -1, size = -1;
  13827. struct script_data* data;
  13828. struct npc_data* nd;
  13829. name = script_getstr(st,2);
  13830. data = script_getdata(st,3);
  13831. if( script_hasdata(st,4) )
  13832. class_ = script_getnum(st,4);
  13833. if( script_hasdata(st,5) )
  13834. size = script_getnum(st,5);
  13835. get_val(st, data);
  13836. if( data_isstring(data) )
  13837. newname = conv_str(st,data);
  13838. else if( data_isint(data) )
  13839. class_ = conv_num(st,data);
  13840. else
  13841. {
  13842. ShowError("script:setnpcdisplay: expected string or number\n");
  13843. script_reportdata(data);
  13844. return SCRIPT_CMD_FAILURE;
  13845. }
  13846. nd = npc_name2id(name);
  13847. if( nd == NULL )
  13848. {// not found
  13849. script_pushint(st,1);
  13850. return SCRIPT_CMD_SUCCESS;
  13851. }
  13852. // update npc
  13853. if( newname )
  13854. npc_setdisplayname(nd, newname);
  13855. if( size != -1 && size != (int)nd->size )
  13856. nd->size = size;
  13857. else
  13858. size = -1;
  13859. if( class_ != -1 && nd->class_ != class_ )
  13860. npc_setclass(nd, class_);
  13861. else if( size != -1 )
  13862. { // Required to update the visual size
  13863. clif_clearunit_area(&nd->bl, CLR_OUTSIGHT);
  13864. clif_spawn(&nd->bl);
  13865. }
  13866. script_pushint(st,0);
  13867. return SCRIPT_CMD_SUCCESS;
  13868. }
  13869. BUILDIN_FUNC(atoi)
  13870. {
  13871. const char *value;
  13872. value = script_getstr(st,2);
  13873. script_pushint(st,atoi(value));
  13874. return SCRIPT_CMD_SUCCESS;
  13875. }
  13876. BUILDIN_FUNC(axtoi)
  13877. {
  13878. const char *hex = script_getstr(st,2);
  13879. long value = strtol(hex, NULL, 16);
  13880. #if LONG_MAX > INT_MAX || LONG_MIN < INT_MIN
  13881. value = cap_value(value, INT_MIN, INT_MAX);
  13882. #endif
  13883. script_pushint(st, (int)value);
  13884. return SCRIPT_CMD_SUCCESS;
  13885. }
  13886. BUILDIN_FUNC(strtol)
  13887. {
  13888. const char *string = script_getstr(st, 2);
  13889. int base = script_getnum(st, 3);
  13890. long value = strtol(string, NULL, base);
  13891. #if LONG_MAX > INT_MAX || LONG_MIN < INT_MIN
  13892. value = cap_value(value, INT_MIN, INT_MAX);
  13893. #endif
  13894. script_pushint(st, (int)value);
  13895. return SCRIPT_CMD_SUCCESS;
  13896. }
  13897. // case-insensitive substring search [lordalfa]
  13898. BUILDIN_FUNC(compare)
  13899. {
  13900. const char *message;
  13901. const char *cmpstring;
  13902. message = script_getstr(st,2);
  13903. cmpstring = script_getstr(st,3);
  13904. script_pushint(st,(stristr(message,cmpstring) != NULL));
  13905. return SCRIPT_CMD_SUCCESS;
  13906. }
  13907. BUILDIN_FUNC(strcmp)
  13908. {
  13909. const char *str1;
  13910. const char *str2;
  13911. str1 = script_getstr(st,2);
  13912. str2 = script_getstr(st,3);
  13913. script_pushint(st,strcmp(str1, str2));
  13914. return SCRIPT_CMD_SUCCESS;
  13915. }
  13916. // [zBuffer] List of mathematics commands --->
  13917. BUILDIN_FUNC(sqrt)
  13918. {
  13919. double i, a;
  13920. i = script_getnum(st,2);
  13921. a = sqrt(i);
  13922. script_pushint(st,(int)a);
  13923. return SCRIPT_CMD_SUCCESS;
  13924. }
  13925. BUILDIN_FUNC(pow)
  13926. {
  13927. double i, a, b;
  13928. a = script_getnum(st,2);
  13929. b = script_getnum(st,3);
  13930. i = pow(a,b);
  13931. script_pushint(st,(int)i);
  13932. return SCRIPT_CMD_SUCCESS;
  13933. }
  13934. BUILDIN_FUNC(distance)
  13935. {
  13936. int x0, y0, x1, y1;
  13937. x0 = script_getnum(st,2);
  13938. y0 = script_getnum(st,3);
  13939. x1 = script_getnum(st,4);
  13940. y1 = script_getnum(st,5);
  13941. script_pushint(st,distance_xy(x0,y0,x1,y1));
  13942. return SCRIPT_CMD_SUCCESS;
  13943. }
  13944. // <--- [zBuffer] List of mathematics commands
  13945. BUILDIN_FUNC(md5)
  13946. {
  13947. const char *tmpstr;
  13948. char *md5str;
  13949. tmpstr = script_getstr(st,2);
  13950. md5str = (char *)aMalloc((32+1)*sizeof(char));
  13951. MD5_String(tmpstr, md5str);
  13952. script_pushstr(st, md5str);
  13953. return SCRIPT_CMD_SUCCESS;
  13954. }
  13955. // [zBuffer] List of dynamic var commands --->
  13956. /**
  13957. * setd "<variable name>",<value>{,<char_id>};
  13958. **/
  13959. BUILDIN_FUNC(setd)
  13960. {
  13961. TBL_PC *sd = NULL;
  13962. char varname[100];
  13963. const char *buffer;
  13964. int elem;
  13965. buffer = script_getstr(st, 2);
  13966. if(sscanf(buffer, "%99[^[][%11d]", varname, &elem) < 2)
  13967. elem = 0;
  13968. if( not_server_variable(*varname) ) {
  13969. if (!script_charid2sd(4,sd))
  13970. return SCRIPT_CMD_FAILURE;
  13971. }
  13972. if( is_string_variable(varname) ) {
  13973. setd_sub(st, sd, varname, elem, (void *)script_getstr(st, 3), NULL);
  13974. } else {
  13975. setd_sub(st, sd, varname, elem, (void *)__64BPRTSIZE(script_getnum(st, 3)), NULL);
  13976. }
  13977. return SCRIPT_CMD_SUCCESS;
  13978. }
  13979. int buildin_query_sql_sub(struct script_state* st, Sql* handle)
  13980. {
  13981. int i, j;
  13982. TBL_PC* sd = NULL;
  13983. const char* query;
  13984. struct script_data* data;
  13985. const char* name;
  13986. unsigned int max_rows = SCRIPT_MAX_ARRAYSIZE; // maximum number of rows
  13987. int num_vars;
  13988. int num_cols;
  13989. // check target variables
  13990. for( i = 3; script_hasdata(st,i); ++i ) {
  13991. data = script_getdata(st, i);
  13992. if( data_isreference(data) ) { // it's a variable
  13993. name = reference_getname(data);
  13994. if( not_server_variable(*name) && sd == NULL ) { // requires a player
  13995. sd = script_rid2sd(st);
  13996. if( sd == NULL ) { // no player attached
  13997. script_reportdata(data);
  13998. st->state = END;
  13999. return SCRIPT_CMD_FAILURE;
  14000. }
  14001. }
  14002. } else {
  14003. ShowError("script:query_sql: not a variable\n");
  14004. script_reportdata(data);
  14005. st->state = END;
  14006. return SCRIPT_CMD_FAILURE;
  14007. }
  14008. }
  14009. num_vars = i - 3;
  14010. // Execute the query
  14011. query = script_getstr(st,2);
  14012. if( SQL_ERROR == Sql_QueryStr(handle, query) ) {
  14013. Sql_ShowDebug(handle);
  14014. script_pushint(st, -1);
  14015. return SCRIPT_CMD_FAILURE;
  14016. }
  14017. if( Sql_NumRows(handle) == 0 ) { // No data received
  14018. Sql_FreeResult(handle);
  14019. script_pushint(st, -1);
  14020. return SCRIPT_CMD_SUCCESS;
  14021. }
  14022. // Count the number of columns to store
  14023. num_cols = Sql_NumColumns(handle);
  14024. if( num_vars < num_cols ) {
  14025. ShowWarning("script:query_sql: Too many columns, discarding last %u columns.\n", (unsigned int)(num_cols-num_vars));
  14026. script_reportsrc(st);
  14027. } else if( num_vars > num_cols ) {
  14028. ShowWarning("script:query_sql: Too many variables (%u extra).\n", (unsigned int)(num_vars-num_cols));
  14029. script_reportsrc(st);
  14030. }
  14031. // Store data
  14032. for( i = 0; i < max_rows && SQL_SUCCESS == Sql_NextRow(handle); ++i ) {
  14033. for( j = 0; j < num_vars; ++j ) {
  14034. char* str = NULL;
  14035. if( j < num_cols )
  14036. Sql_GetData(handle, j, &str, NULL);
  14037. data = script_getdata(st, j+3);
  14038. name = reference_getname(data);
  14039. if( is_string_variable(name) )
  14040. setd_sub(st, sd, name, i, (void *)(str?str:""), reference_getref(data));
  14041. else
  14042. setd_sub(st, sd, name, i, (void *)__64BPRTSIZE((str?atoi(str):0)), reference_getref(data));
  14043. }
  14044. }
  14045. if( i == max_rows && max_rows < Sql_NumRows(handle) ) {
  14046. ShowWarning("script:query_sql: Only %d/%u rows have been stored.\n", max_rows, (unsigned int)Sql_NumRows(handle));
  14047. script_reportsrc(st);
  14048. }
  14049. // Free data
  14050. Sql_FreeResult(handle);
  14051. script_pushint(st, i);
  14052. return SCRIPT_CMD_SUCCESS;
  14053. }
  14054. BUILDIN_FUNC(query_sql) {
  14055. #ifdef BETA_THREAD_TEST
  14056. if( st->state != RERUNLINE ) {
  14057. queryThread_add(st,false);
  14058. st->state = RERUNLINE;/* will continue when the query is finished running. */
  14059. } else
  14060. st->state = RUN;
  14061. return SCRIPT_CMD_SUCCESS;
  14062. #else
  14063. return buildin_query_sql_sub(st, qsmysql_handle);
  14064. #endif
  14065. }
  14066. BUILDIN_FUNC(query_logsql) {
  14067. if( !log_config.sql_logs ) {// logmysql_handle == NULL
  14068. ShowWarning("buildin_query_logsql: SQL logs are disabled, query '%s' will not be executed.\n", script_getstr(st,2));
  14069. script_pushint(st,-1);
  14070. return SCRIPT_CMD_FAILURE;
  14071. }
  14072. #ifdef BETA_THREAD_TEST
  14073. if( st->state != RERUNLINE ) {
  14074. queryThread_add(st,true);
  14075. st->state = RERUNLINE;/* will continue when the query is finished running. */
  14076. } else
  14077. st->state = RUN;
  14078. return SCRIPT_CMD_SUCCESS;
  14079. #else
  14080. return buildin_query_sql_sub(st, logmysql_handle);
  14081. #endif
  14082. }
  14083. //Allows escaping of a given string.
  14084. BUILDIN_FUNC(escape_sql)
  14085. {
  14086. const char *str;
  14087. char *esc_str;
  14088. size_t len;
  14089. str = script_getstr(st,2);
  14090. len = strlen(str);
  14091. esc_str = (char*)aMalloc(len*2+1);
  14092. Sql_EscapeStringLen(mmysql_handle, esc_str, str, len);
  14093. script_pushstr(st, esc_str);
  14094. return SCRIPT_CMD_SUCCESS;
  14095. }
  14096. BUILDIN_FUNC(getd)
  14097. {
  14098. char varname[100];
  14099. const char *buffer;
  14100. int elem;
  14101. buffer = script_getstr(st, 2);
  14102. if(sscanf(buffer, "%99[^[][%11d]", varname, &elem) < 2)
  14103. elem = 0;
  14104. // Push the 'pointer' so it's more flexible [Lance]
  14105. push_val(st->stack, C_NAME, reference_uid(add_str(varname), elem));
  14106. return SCRIPT_CMD_SUCCESS;
  14107. }
  14108. BUILDIN_FUNC(callshop)
  14109. {
  14110. TBL_PC *sd = NULL;
  14111. struct npc_data *nd;
  14112. const char *shopname;
  14113. int flag = 0;
  14114. sd = script_rid2sd(st);
  14115. if (!sd) {
  14116. script_pushint(st,0);
  14117. return SCRIPT_CMD_SUCCESS;
  14118. }
  14119. shopname = script_getstr(st, 2);
  14120. if (script_hasdata(st,3))
  14121. flag = script_getnum(st,3);
  14122. nd = npc_name2id(shopname);
  14123. 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) ) {
  14124. ShowError("buildin_callshop: Shop [%s] not found (or NPC is not shop type)\n", shopname);
  14125. script_pushint(st,0);
  14126. return SCRIPT_CMD_FAILURE;
  14127. }
  14128. if (nd->subtype == NPCTYPE_SHOP) {
  14129. // flag the user as using a valid script call for opening the shop (for floating NPCs)
  14130. sd->state.callshop = 1;
  14131. switch (flag) {
  14132. case 1: npc_buysellsel(sd,nd->bl.id,0); break; //Buy window
  14133. case 2: npc_buysellsel(sd,nd->bl.id,1); break; //Sell window
  14134. default: clif_npcbuysell(sd,nd->bl.id); break; //Show menu
  14135. }
  14136. }
  14137. #if PACKETVER >= 20131223
  14138. else if (nd->subtype == NPCTYPE_MARKETSHOP) {
  14139. unsigned short i;
  14140. for (i = 0; i < nd->u.shop.count; i++) {
  14141. if (nd->u.shop.shop_item[i].qty)
  14142. break;
  14143. }
  14144. if (i == nd->u.shop.count) {
  14145. clif_colormes(sd->fd, color_table[COLOR_RED], msg_txt(sd, 534));
  14146. return SCRIPT_CMD_FAILURE;
  14147. }
  14148. sd->npc_shopid = nd->bl.id;
  14149. clif_npc_market_open(sd, nd);
  14150. script_pushint(st,1);
  14151. return SCRIPT_CMD_SUCCESS;
  14152. }
  14153. #endif
  14154. else
  14155. clif_cashshop_show(sd, nd);
  14156. sd->npc_shopid = nd->bl.id;
  14157. script_pushint(st,1);
  14158. return SCRIPT_CMD_SUCCESS;
  14159. }
  14160. BUILDIN_FUNC(npcshopitem)
  14161. {
  14162. const char* npcname = script_getstr(st, 2);
  14163. struct npc_data* nd = npc_name2id(npcname);
  14164. int n, i;
  14165. int amount;
  14166. uint16 offs = 2;
  14167. if( !nd || ( nd->subtype != NPCTYPE_SHOP && nd->subtype != NPCTYPE_CASHSHOP && nd->subtype != NPCTYPE_ITEMSHOP && nd->subtype != NPCTYPE_POINTSHOP && nd->subtype != NPCTYPE_MARKETSHOP ) ) { // Not found.
  14168. script_pushint(st,0);
  14169. return SCRIPT_CMD_SUCCESS;
  14170. }
  14171. #if PACKETVER >= 20131223
  14172. if (nd->subtype == NPCTYPE_MARKETSHOP) {
  14173. offs = 3;
  14174. npc_market_delfromsql_(nd->exname, 0, true);
  14175. }
  14176. #endif
  14177. // get the count of new entries
  14178. amount = (script_lastdata(st)-2)/offs;
  14179. // generate new shop item list
  14180. RECREATE(nd->u.shop.shop_item, struct npc_item_list, amount);
  14181. for (n = 0, i = 3; n < amount; n++, i+=offs) {
  14182. nd->u.shop.shop_item[n].nameid = script_getnum(st,i);
  14183. nd->u.shop.shop_item[n].value = script_getnum(st,i+1);
  14184. #if PACKETVER >= 20131223
  14185. if (nd->subtype == NPCTYPE_MARKETSHOP) {
  14186. nd->u.shop.shop_item[n].qty = script_getnum(st,i+2);
  14187. nd->u.shop.shop_item[n].flag = 1;
  14188. npc_market_tosql(nd->exname, &nd->u.shop.shop_item[n]);
  14189. }
  14190. #endif
  14191. }
  14192. nd->u.shop.count = n;
  14193. script_pushint(st,1);
  14194. return SCRIPT_CMD_SUCCESS;
  14195. }
  14196. BUILDIN_FUNC(npcshopadditem)
  14197. {
  14198. const char* npcname = script_getstr(st,2);
  14199. struct npc_data* nd = npc_name2id(npcname);
  14200. int n, i;
  14201. int amount;
  14202. uint16 offs = 2;
  14203. if (!nd || ( nd->subtype != NPCTYPE_SHOP && nd->subtype != NPCTYPE_CASHSHOP && nd->subtype != NPCTYPE_ITEMSHOP && nd->subtype != NPCTYPE_POINTSHOP && nd->subtype != NPCTYPE_MARKETSHOP)) { // Not found.
  14204. script_pushint(st,0);
  14205. return SCRIPT_CMD_SUCCESS;
  14206. }
  14207. if (nd->subtype == NPCTYPE_MARKETSHOP)
  14208. offs = 3;
  14209. // get the count of new entries
  14210. amount = (script_lastdata(st)-2)/offs;
  14211. #if PACKETVER >= 20131223
  14212. if (nd->subtype == NPCTYPE_MARKETSHOP) {
  14213. for (n = 0, i = 3; n < amount; n++, i += offs) {
  14214. uint16 nameid = script_getnum(st,i), j;
  14215. // Check existing entries
  14216. ARR_FIND(0, nd->u.shop.count, j, nd->u.shop.shop_item[j].nameid == nameid);
  14217. if (j == nd->u.shop.count) {
  14218. RECREATE(nd->u.shop.shop_item, struct npc_item_list, nd->u.shop.count+1);
  14219. j = nd->u.shop.count;
  14220. nd->u.shop.shop_item[j].flag = 1;
  14221. nd->u.shop.count++;
  14222. }
  14223. nd->u.shop.shop_item[j].nameid = nameid;
  14224. nd->u.shop.shop_item[j].value = script_getnum(st,i+1);
  14225. nd->u.shop.shop_item[j].qty = script_getnum(st,i+2);
  14226. npc_market_tosql(nd->exname, &nd->u.shop.shop_item[j]);
  14227. }
  14228. script_pushint(st,1);
  14229. return SCRIPT_CMD_SUCCESS;
  14230. }
  14231. #endif
  14232. // append new items to existing shop item list
  14233. RECREATE(nd->u.shop.shop_item, struct npc_item_list, nd->u.shop.count+amount);
  14234. for (n = nd->u.shop.count, i = 3; n < nd->u.shop.count+amount; n++, i+=offs)
  14235. {
  14236. nd->u.shop.shop_item[n].nameid = script_getnum(st,i);
  14237. nd->u.shop.shop_item[n].value = script_getnum(st,i+1);
  14238. }
  14239. nd->u.shop.count = n;
  14240. script_pushint(st,1);
  14241. return SCRIPT_CMD_SUCCESS;
  14242. }
  14243. BUILDIN_FUNC(npcshopdelitem)
  14244. {
  14245. const char* npcname = script_getstr(st,2);
  14246. struct npc_data* nd = npc_name2id(npcname);
  14247. int n, i;
  14248. int amount;
  14249. int size;
  14250. if (!nd || ( nd->subtype != NPCTYPE_SHOP && nd->subtype != NPCTYPE_CASHSHOP && nd->subtype != NPCTYPE_ITEMSHOP && nd->subtype != NPCTYPE_POINTSHOP && nd->subtype != NPCTYPE_MARKETSHOP)) { // Not found.
  14251. script_pushint(st,0);
  14252. return SCRIPT_CMD_SUCCESS;
  14253. }
  14254. amount = script_lastdata(st)-2;
  14255. size = nd->u.shop.count;
  14256. // remove specified items from the shop item list
  14257. for( i = 3; i < 3 + amount; i++ ) {
  14258. unsigned short nameid = script_getnum(st,i);
  14259. ARR_FIND( 0, size, n, nd->u.shop.shop_item[n].nameid == nameid );
  14260. if( n < size ) {
  14261. if (n+1 != size)
  14262. memmove(&nd->u.shop.shop_item[n], &nd->u.shop.shop_item[n+1], sizeof(nd->u.shop.shop_item[0])*(size-n));
  14263. #if PACKETVER >= 20131223
  14264. if (nd->subtype == NPCTYPE_MARKETSHOP)
  14265. npc_market_delfromsql_(nd->exname, nameid, false);
  14266. #endif
  14267. size--;
  14268. }
  14269. }
  14270. RECREATE(nd->u.shop.shop_item, struct npc_item_list, size);
  14271. nd->u.shop.count = size;
  14272. script_pushint(st,1);
  14273. return SCRIPT_CMD_SUCCESS;
  14274. }
  14275. /**
  14276. * Sets a script to attach to a shop npc.
  14277. * npcshopattach "<npc_name>";
  14278. **/
  14279. BUILDIN_FUNC(npcshopattach)
  14280. {
  14281. const char* npcname = script_getstr(st,2);
  14282. struct npc_data* nd = npc_name2id(npcname);
  14283. int flag = 1;
  14284. if( script_hasdata(st,3) )
  14285. flag = script_getnum(st,3);
  14286. if (!nd || ( nd->subtype != NPCTYPE_SHOP && nd->subtype != NPCTYPE_CASHSHOP && nd->subtype != NPCTYPE_ITEMSHOP && nd->subtype != NPCTYPE_POINTSHOP && nd->subtype != NPCTYPE_MARKETSHOP)) { // Not found.
  14287. script_pushint(st,0);
  14288. return SCRIPT_CMD_SUCCESS;
  14289. }
  14290. if (flag)
  14291. nd->master_nd = ((struct npc_data *)map_id2bl(st->oid));
  14292. else
  14293. nd->master_nd = NULL;
  14294. script_pushint(st,1);
  14295. return SCRIPT_CMD_SUCCESS;
  14296. }
  14297. /*==========================================
  14298. * Returns some values of an item [Lupus]
  14299. * Price, Weight, etc...
  14300. setitemscript(itemID,"{new item bonus script}",[n]);
  14301. Where n:
  14302. 0 - script
  14303. 1 - Equip script
  14304. 2 - Unequip script
  14305. *------------------------------------------*/
  14306. BUILDIN_FUNC(setitemscript)
  14307. {
  14308. unsigned short item_id;
  14309. int n = 0;
  14310. const char *script;
  14311. struct item_data *i_data;
  14312. struct script_code **dstscript;
  14313. item_id = script_getnum(st,2);
  14314. script = script_getstr(st,3);
  14315. if( script_hasdata(st,4) )
  14316. n=script_getnum(st,4);
  14317. i_data = itemdb_exists(item_id);
  14318. if (!i_data || script==NULL || ( script[0] && script[0]!='{' )) {
  14319. script_pushint(st,0);
  14320. return SCRIPT_CMD_SUCCESS;
  14321. }
  14322. switch (n) {
  14323. case 2:
  14324. dstscript = &i_data->unequip_script;
  14325. break;
  14326. case 1:
  14327. dstscript = &i_data->equip_script;
  14328. break;
  14329. default:
  14330. dstscript = &i_data->script;
  14331. break;
  14332. }
  14333. if(*dstscript)
  14334. script_free_code(*dstscript);
  14335. *dstscript = script[0] ? parse_script(script, "script_setitemscript", 0, 0) : NULL;
  14336. script_pushint(st,1);
  14337. return SCRIPT_CMD_SUCCESS;
  14338. }
  14339. /*=======================================================
  14340. * Add or Update a mob drop temporarily [Akinari]
  14341. * Original Idea By: [Lupus]
  14342. *
  14343. * addmonsterdrop <mob_id or name>,<item_id>,<rate>;
  14344. *
  14345. * If given an item the mob already drops, the rate
  14346. * is updated to the new rate. Rate cannot exceed 10000
  14347. * Returns 1 if succeeded (added/updated a mob drop)
  14348. *-------------------------------------------------------*/
  14349. BUILDIN_FUNC(addmonsterdrop)
  14350. {
  14351. struct mob_db *mob;
  14352. struct script_data *data;
  14353. unsigned short item_id;
  14354. int rate;
  14355. data = script_getdata(st, 2);
  14356. get_val(st, data); // Convert into value in case of a variable
  14357. if(data_isstring(data))
  14358. mob = mob_db(mobdb_searchname(script_getstr(st,2)));
  14359. else
  14360. mob = mob_db(script_getnum(st,2));
  14361. item_id=script_getnum(st,3);
  14362. rate=script_getnum(st,4);
  14363. if(!itemdb_exists(item_id)){
  14364. ShowError("addmonsterdrop: Nonexistant item %hu requested.\n", item_id );
  14365. return SCRIPT_CMD_FAILURE;
  14366. }
  14367. if(mob) { //We got a valid monster, check for available drop slot
  14368. unsigned char i, c = 0;
  14369. for(i = 0; i < MAX_MOB_DROP; i++) {
  14370. if(mob->dropitem[i].nameid) {
  14371. if(mob->dropitem[i].nameid == item_id) { //If it equals item_id we update that drop
  14372. c = i;
  14373. break;
  14374. }
  14375. continue;
  14376. }
  14377. if(!c) //Accept first available slot only
  14378. c = i;
  14379. }
  14380. if(c) { //Fill in the slot with the item and rate
  14381. mob->dropitem[c].nameid = item_id;
  14382. mob->dropitem[c].p = (rate > 10000)?10000:rate;
  14383. script_pushint(st,1);
  14384. } else //No place to put the new drop
  14385. script_pushint(st,0);
  14386. } else {
  14387. ShowWarning("addmonsterdrop: bad mob id given %d\n",script_getnum(st,2));
  14388. return SCRIPT_CMD_FAILURE;
  14389. }
  14390. return SCRIPT_CMD_SUCCESS;
  14391. }
  14392. /*=======================================================
  14393. * Delete a mob drop temporarily [Akinari]
  14394. * Original Idea By: [Lupus]
  14395. *
  14396. * delmonsterdrop <mob_id or name>,<item_id>;
  14397. *
  14398. * Returns 1 if succeeded (deleted a mob drop)
  14399. *-------------------------------------------------------*/
  14400. BUILDIN_FUNC(delmonsterdrop)
  14401. {
  14402. struct mob_db *mob;
  14403. struct script_data *data;
  14404. unsigned short item_id;
  14405. data = script_getdata(st, 2);
  14406. get_val(st, data); // Convert into value in case of a variable
  14407. if(data_isstring(data))
  14408. mob = mob_db(mobdb_searchname(script_getstr(st,2)));
  14409. else
  14410. mob = mob_db(script_getnum(st,2));
  14411. item_id=script_getnum(st,3);
  14412. if(!itemdb_exists(item_id)){
  14413. ShowError("delmonsterdrop: Nonexistant item %hu requested.\n", item_id );
  14414. return SCRIPT_CMD_FAILURE;
  14415. }
  14416. if(mob) { //We got a valid monster, check for item drop on monster
  14417. unsigned char i;
  14418. for(i = 0; i < MAX_MOB_DROP; i++) {
  14419. if(mob->dropitem[i].nameid == item_id) {
  14420. mob->dropitem[i].nameid = 0;
  14421. mob->dropitem[i].p = 0;
  14422. script_pushint(st,1);
  14423. return SCRIPT_CMD_SUCCESS;
  14424. }
  14425. }
  14426. //No drop on that monster
  14427. script_pushint(st,0);
  14428. } else {
  14429. ShowWarning("delmonsterdrop: bad mob id given %d\n",script_getnum(st,2));
  14430. return SCRIPT_CMD_FAILURE;
  14431. }
  14432. return SCRIPT_CMD_SUCCESS;
  14433. }
  14434. /*==========================================
  14435. * Returns some values of a monster [Lupus]
  14436. * Name, Level, race, size, etc...
  14437. getmonsterinfo(monsterID,queryIndex);
  14438. *------------------------------------------*/
  14439. BUILDIN_FUNC(getmonsterinfo)
  14440. {
  14441. struct mob_db *mob;
  14442. int mob_id;
  14443. mob_id = script_getnum(st,2);
  14444. if (!mobdb_checkid(mob_id)) {
  14445. //ShowError("buildin_getmonsterinfo: Wrong Monster ID: %i\n", mob_id);
  14446. if ( script_getnum(st,3) == MOB_NAME ) // requested the name
  14447. script_pushconststr(st,"null");
  14448. else
  14449. script_pushint(st,-1);
  14450. return SCRIPT_CMD_SUCCESS;
  14451. }
  14452. mob = mob_db(mob_id);
  14453. switch ( script_getnum(st,3) ) {
  14454. case MOB_NAME: script_pushstrcopy(st,mob->jname); break;
  14455. case MOB_LV: script_pushint(st,mob->lv); break;
  14456. case MOB_MAXHP: script_pushint(st,mob->status.max_hp); break;
  14457. case MOB_BASEEXP: script_pushint(st,mob->base_exp); break;
  14458. case MOB_JOBEXP: script_pushint(st,mob->job_exp); break;
  14459. case MOB_ATK1: script_pushint(st,mob->status.rhw.atk); break;
  14460. case MOB_ATK2: script_pushint(st,mob->status.rhw.atk2); break;
  14461. case MOB_DEF: script_pushint(st,mob->status.def); break;
  14462. case MOB_MDEF: script_pushint(st,mob->status.mdef); break;
  14463. case MOB_STR: script_pushint(st,mob->status.str); break;
  14464. case MOB_AGI: script_pushint(st,mob->status.agi); break;
  14465. case MOB_VIT: script_pushint(st,mob->status.vit); break;
  14466. case MOB_INT: script_pushint(st,mob->status.int_); break;
  14467. case MOB_DEX: script_pushint(st,mob->status.dex); break;
  14468. case MOB_LUK: script_pushint(st,mob->status.luk); break;
  14469. case MOB_RANGE: script_pushint(st,mob->status.rhw.range); break;
  14470. case MOB_RANGE2: script_pushint(st,mob->range2); break;
  14471. case MOB_RANGE3: script_pushint(st,mob->range3); break;
  14472. case MOB_SIZE: script_pushint(st,mob->status.size); break;
  14473. case MOB_RACE: script_pushint(st,mob->status.race); break;
  14474. case MOB_ELEMENT: script_pushint(st,mob->status.def_ele); break;
  14475. case MOB_MODE: script_pushint(st,mob->status.mode); break;
  14476. case MOB_MVPEXP: script_pushint(st,mob->mexp); break;
  14477. default: script_pushint(st,-1); //wrong Index
  14478. }
  14479. return SCRIPT_CMD_SUCCESS;
  14480. }
  14481. /**
  14482. * Check player's vending/buyingstore/autotrade state
  14483. * checkvending({"<Player Name>"})
  14484. * @author [Nab4]
  14485. */
  14486. BUILDIN_FUNC(checkvending) {
  14487. TBL_PC *sd = NULL;
  14488. if (script_hasdata(st,2)) {
  14489. if (!(sd = map_nick2sd(script_getstr(st,2)))) {
  14490. ShowError("buildin_checkvending: Player '%s' is not online!\n", script_getstr(st,2));
  14491. return SCRIPT_CMD_FAILURE;
  14492. }
  14493. }
  14494. else
  14495. sd = script_rid2sd(st);
  14496. if (!sd) {
  14497. script_pushint(st,0);
  14498. return SCRIPT_CMD_SUCCESS;
  14499. }
  14500. else {
  14501. int8 ret = 0;
  14502. if (sd->state.vending)
  14503. ret = 1;
  14504. else if (sd->state.buyingstore)
  14505. ret = 4;
  14506. if (sd->state.autotrade)
  14507. ret |= 2;
  14508. script_pushint(st, ret);
  14509. }
  14510. return SCRIPT_CMD_SUCCESS;
  14511. }
  14512. BUILDIN_FUNC(checkchatting) // check chatting [Marka]
  14513. {
  14514. TBL_PC *sd = NULL;
  14515. if(script_hasdata(st,2))
  14516. sd = map_nick2sd(script_getstr(st,2));
  14517. else
  14518. sd = script_rid2sd(st);
  14519. if(sd)
  14520. script_pushint(st,(sd->chatID != 0));
  14521. else
  14522. script_pushint(st,0);
  14523. return SCRIPT_CMD_SUCCESS;
  14524. }
  14525. BUILDIN_FUNC(checkidle)
  14526. {
  14527. TBL_PC *sd = NULL;
  14528. if (script_hasdata(st, 2))
  14529. sd = map_nick2sd(script_getstr(st, 2));
  14530. else
  14531. sd = script_rid2sd(st);
  14532. if (sd)
  14533. script_pushint(st, DIFF_TICK(last_tick, sd->idletime));
  14534. else
  14535. script_pushint(st, 0);
  14536. return SCRIPT_CMD_SUCCESS;
  14537. }
  14538. BUILDIN_FUNC(searchitem)
  14539. {
  14540. struct script_data* data = script_getdata(st, 2);
  14541. const char *itemname = script_getstr(st,3);
  14542. struct item_data *items[MAX_SEARCH];
  14543. int count;
  14544. char* name;
  14545. int32 start;
  14546. int32 id;
  14547. int32 i;
  14548. TBL_PC* sd = NULL;
  14549. if ((items[0] = itemdb_exists(atoi(itemname))))
  14550. count = 1;
  14551. else {
  14552. count = itemdb_searchname_array(items, ARRAYLENGTH(items), itemname);
  14553. if (count > MAX_SEARCH) count = MAX_SEARCH;
  14554. }
  14555. if (!count) {
  14556. script_pushint(st, 0);
  14557. return SCRIPT_CMD_SUCCESS;
  14558. }
  14559. if( !data_isreference(data) )
  14560. {
  14561. ShowError("script:searchitem: not a variable\n");
  14562. script_reportdata(data);
  14563. st->state = END;
  14564. return SCRIPT_CMD_FAILURE;// not a variable
  14565. }
  14566. id = reference_getid(data);
  14567. start = reference_getindex(data);
  14568. name = reference_getname(data);
  14569. if( not_server_variable(*name) )
  14570. {
  14571. sd = script_rid2sd(st);
  14572. if( sd == NULL )
  14573. return SCRIPT_CMD_SUCCESS;// no player attached
  14574. }
  14575. if( is_string_variable(name) )
  14576. {// string array
  14577. ShowError("script:searchitem: not an integer array reference\n");
  14578. script_reportdata(data);
  14579. st->state = END;
  14580. return SCRIPT_CMD_FAILURE;// not supported
  14581. }
  14582. for( i = 0; i < count; ++start, ++i )
  14583. {// Set array
  14584. void* v = (void*)__64BPRTSIZE((int)items[i]->nameid);
  14585. set_reg(st, sd, reference_uid(id, start), name, v, reference_getref(data));
  14586. }
  14587. script_pushint(st, count);
  14588. return SCRIPT_CMD_SUCCESS;
  14589. }
  14590. // [zBuffer] List of player cont commands --->
  14591. BUILDIN_FUNC(rid2name)
  14592. {
  14593. struct block_list *bl = NULL;
  14594. int rid = script_getnum(st,2);
  14595. if((bl = map_id2bl(rid)))
  14596. {
  14597. switch(bl->type) {
  14598. case BL_MOB: script_pushstrcopy(st,((TBL_MOB*)bl)->name); break;
  14599. case BL_PC: script_pushstrcopy(st,((TBL_PC*)bl)->status.name); break;
  14600. case BL_NPC: script_pushstrcopy(st,((TBL_NPC*)bl)->exname); break;
  14601. case BL_PET: script_pushstrcopy(st,((TBL_PET*)bl)->pet.name); break;
  14602. case BL_HOM: script_pushstrcopy(st,((TBL_HOM*)bl)->homunculus.name); break;
  14603. case BL_MER: script_pushstrcopy(st,((TBL_MER*)bl)->db->name); break;
  14604. default:
  14605. ShowError("buildin_rid2name: BL type unknown.\n");
  14606. script_pushconststr(st,"");
  14607. break;
  14608. }
  14609. } else {
  14610. ShowError("buildin_rid2name: invalid RID\n");
  14611. script_pushconststr(st,"(null)");
  14612. }
  14613. return SCRIPT_CMD_SUCCESS;
  14614. }
  14615. BUILDIN_FUNC(pcblockmove)
  14616. {
  14617. struct block_list *bl = NULL;
  14618. if (script_getnum(st, 2))
  14619. bl = map_id2bl(script_getnum(st,2));
  14620. else
  14621. bl = map_id2bl(st->rid);
  14622. if (bl) {
  14623. struct unit_data *ud = unit_bl2ud(bl);
  14624. if (ud)
  14625. ud->state.blockedmove = script_getnum(st,3) > 0;
  14626. }
  14627. return SCRIPT_CMD_SUCCESS;
  14628. }
  14629. BUILDIN_FUNC(pcfollow)
  14630. {
  14631. int id, targetid;
  14632. TBL_PC *sd = NULL;
  14633. id = script_getnum(st,2);
  14634. targetid = script_getnum(st,3);
  14635. if(id)
  14636. sd = map_id2sd(id);
  14637. else
  14638. sd = script_rid2sd(st);
  14639. if(sd)
  14640. pc_follow(sd, targetid);
  14641. return SCRIPT_CMD_SUCCESS;
  14642. }
  14643. BUILDIN_FUNC(pcstopfollow)
  14644. {
  14645. int id;
  14646. TBL_PC *sd = NULL;
  14647. id = script_getnum(st,2);
  14648. if(id)
  14649. sd = map_id2sd(id);
  14650. else
  14651. sd = script_rid2sd(st);
  14652. if(sd)
  14653. pc_stop_following(sd);
  14654. return SCRIPT_CMD_SUCCESS;
  14655. }
  14656. // <--- [zBuffer] List of player cont commands
  14657. // [zBuffer] List of unit control commands --->
  14658. /// Gets the type of the given Game ID.
  14659. ///
  14660. /// getunittype <unit id>;
  14661. BUILDIN_FUNC(getunittype)
  14662. {
  14663. struct block_list* bl;
  14664. uint8 value = 0;
  14665. bl = map_id2bl(script_getnum(st, 2));
  14666. if (!bl) {
  14667. ShowWarning("buildin_getunittype: Error in finding object with given game ID %d!\n", script_getnum(st, 2));
  14668. script_pushint(st, -1);
  14669. return SCRIPT_CMD_FAILURE;
  14670. }
  14671. switch (bl->type) {
  14672. case BL_PC: value = UNITTYPE_PC; break;
  14673. case BL_NPC: value = UNITTYPE_NPC; break;
  14674. case BL_PET: value = UNITTYPE_PET; break;
  14675. case BL_MOB: value = UNITTYPE_MOB; break;
  14676. case BL_HOM: value = UNITTYPE_HOM; break;
  14677. case BL_MER: value = UNITTYPE_MER; break;
  14678. case BL_ELEM: value = UNITTYPE_ELEM; break;
  14679. default: value = -1; break;
  14680. }
  14681. script_pushint(st, value);
  14682. return SCRIPT_CMD_SUCCESS;
  14683. }
  14684. /// Gets specific live information of a bl.
  14685. ///
  14686. /// getunitdata <unit id>,<arrayname>;
  14687. BUILDIN_FUNC(getunitdata)
  14688. {
  14689. TBL_PC *sd = st->rid ? map_id2sd(st->rid) : NULL;
  14690. struct block_list* bl;
  14691. TBL_MOB* md = NULL;
  14692. TBL_HOM* hd = NULL;
  14693. TBL_MER* mc = NULL;
  14694. TBL_PET* pd = NULL;
  14695. TBL_ELEM* ed = NULL;
  14696. TBL_NPC* nd = NULL;
  14697. char* name;
  14698. struct script_data *data = script_getdata(st, 3);
  14699. if (!data_isreference(data)) {
  14700. ShowWarning("buildin_getunitdata: Error in argument! Please give a variable to store values in.\n");
  14701. return SCRIPT_CMD_FAILURE;
  14702. }
  14703. bl = map_id2bl(script_getnum(st, 2));
  14704. if (!bl) {
  14705. ShowWarning("buildin_getunitdata: Error in finding object with given game ID %d!\n", script_getnum(st, 2));
  14706. return SCRIPT_CMD_FAILURE;
  14707. }
  14708. switch (bl->type) {
  14709. case BL_MOB: md = map_id2md(bl->id); break;
  14710. case BL_HOM: hd = map_id2hd(bl->id); break;
  14711. case BL_PET: pd = map_id2pd(bl->id); break;
  14712. case BL_MER: mc = map_id2mc(bl->id); break;
  14713. case BL_ELEM: ed = map_id2ed(bl->id); break;
  14714. case BL_NPC: nd = map_id2nd(bl->id); break;
  14715. }
  14716. name = reference_getname(data);
  14717. #define getunitdata_sub(idx__,var__) setd_sub(st,sd,name,(idx__),(void *)__64BPRTSIZE((int)(var__)),data->ref)
  14718. switch(bl->type) {
  14719. case BL_MOB:
  14720. if (!md) {
  14721. ShowWarning("buildin_getunitdata: Error in finding object BL_MOB!\n");
  14722. return SCRIPT_CMD_FAILURE;
  14723. }
  14724. getunitdata_sub(UMOB_SIZE, md->status.size);
  14725. getunitdata_sub(UMOB_LEVEL, md->level);
  14726. getunitdata_sub(UMOB_HP, md->status.hp);
  14727. getunitdata_sub(UMOB_MAXHP, md->status.max_hp);
  14728. getunitdata_sub(UMOB_MASTERAID, md->master_id);
  14729. getunitdata_sub(UMOB_MAPID, md->bl.m);
  14730. getunitdata_sub(UMOB_X, md->bl.x);
  14731. getunitdata_sub(UMOB_Y, md->bl.y);
  14732. getunitdata_sub(UMOB_SPEED, md->status.speed);
  14733. getunitdata_sub(UMOB_MODE, md->status.mode);
  14734. getunitdata_sub(UMOB_AI, md->special_state.ai);
  14735. getunitdata_sub(UMOB_SCOPTION, md->sc.option);
  14736. getunitdata_sub(UMOB_SEX, md->vd->sex);
  14737. getunitdata_sub(UMOB_CLASS, md->vd->class_);
  14738. getunitdata_sub(UMOB_HAIRSTYLE, md->vd->hair_style);
  14739. getunitdata_sub(UMOB_HAIRCOLOR, md->vd->hair_color);
  14740. getunitdata_sub(UMOB_HEADBOTTOM, md->vd->head_bottom);
  14741. getunitdata_sub(UMOB_HEADMIDDLE, md->vd->head_mid);
  14742. getunitdata_sub(UMOB_HEADTOP, md->vd->head_top);
  14743. getunitdata_sub(UMOB_CLOTHCOLOR, md->vd->cloth_color);
  14744. getunitdata_sub(UMOB_SHIELD, md->vd->shield);
  14745. getunitdata_sub(UMOB_WEAPON, md->vd->weapon);
  14746. getunitdata_sub(UMOB_LOOKDIR, md->ud.dir);
  14747. getunitdata_sub(UMOB_CANMOVETICK, md->ud.canmove_tick);
  14748. getunitdata_sub(UMOB_STR, md->status.str);
  14749. getunitdata_sub(UMOB_AGI, md->status.agi);
  14750. getunitdata_sub(UMOB_VIT, md->status.vit);
  14751. getunitdata_sub(UMOB_INT, md->status.int_);
  14752. getunitdata_sub(UMOB_DEX, md->status.dex);
  14753. getunitdata_sub(UMOB_LUK, md->status.luk);
  14754. getunitdata_sub(UMOB_SLAVECPYMSTRMD, md->state.copy_master_mode);
  14755. getunitdata_sub(UMOB_DMGIMMUNE, md->ud.immune_attack);
  14756. getunitdata_sub(UMOB_ATKRANGE, md->status.rhw.range);
  14757. getunitdata_sub(UMOB_ATKMIN, md->status.rhw.atk);
  14758. getunitdata_sub(UMOB_ATKMAX, md->status.rhw.atk2);
  14759. getunitdata_sub(UMOB_MATKMIN, md->status.matk_min);
  14760. getunitdata_sub(UMOB_MATKMAX, md->status.matk_max);
  14761. getunitdata_sub(UMOB_DEF, md->status.def);
  14762. getunitdata_sub(UMOB_MDEF, md->status.mdef);
  14763. getunitdata_sub(UMOB_HIT, md->status.hit);
  14764. getunitdata_sub(UMOB_FLEE, md->status.flee);
  14765. getunitdata_sub(UMOB_PDODGE, md->status.flee2);
  14766. getunitdata_sub(UMOB_CRIT, md->status.cri);
  14767. getunitdata_sub(UMOB_RACE, md->status.race);
  14768. getunitdata_sub(UMOB_ELETYPE, md->status.def_ele);
  14769. getunitdata_sub(UMOB_ELELEVEL, md->status.ele_lv);
  14770. getunitdata_sub(UMOB_AMOTION, md->status.amotion);
  14771. getunitdata_sub(UMOB_ADELAY, md->status.adelay);
  14772. getunitdata_sub(UMOB_DMOTION, md->status.dmotion);
  14773. break;
  14774. case BL_HOM:
  14775. if (!hd) {
  14776. ShowWarning("buildin_getunitdata: Error in finding object BL_HOM!\n");
  14777. return SCRIPT_CMD_FAILURE;
  14778. }
  14779. getunitdata_sub(UHOM_SIZE, hd->base_status.size);
  14780. getunitdata_sub(UHOM_LEVEL, hd->homunculus.level);
  14781. getunitdata_sub(UHOM_HP, hd->homunculus.hp);
  14782. getunitdata_sub(UHOM_MAXHP, hd->homunculus.max_hp);
  14783. getunitdata_sub(UHOM_SP, hd->homunculus.sp);
  14784. getunitdata_sub(UHOM_MAXSP, hd->homunculus.max_sp);
  14785. getunitdata_sub(UHOM_MASTERCID, hd->homunculus.char_id);
  14786. getunitdata_sub(UHOM_MAPID, hd->bl.m);
  14787. getunitdata_sub(UHOM_X, hd->bl.x);
  14788. getunitdata_sub(UHOM_Y, hd->bl.y);
  14789. getunitdata_sub(UHOM_HUNGER, hd->homunculus.hunger);
  14790. getunitdata_sub(UHOM_INTIMACY, hd->homunculus.intimacy);
  14791. getunitdata_sub(UHOM_SPEED, hd->base_status.speed);
  14792. getunitdata_sub(UHOM_LOOKDIR, hd->ud.dir);
  14793. getunitdata_sub(UHOM_CANMOVETICK, hd->ud.canmove_tick);
  14794. getunitdata_sub(UHOM_STR, hd->base_status.str);
  14795. getunitdata_sub(UHOM_AGI, hd->base_status.agi);
  14796. getunitdata_sub(UHOM_VIT, hd->base_status.vit);
  14797. getunitdata_sub(UHOM_INT, hd->base_status.int_);
  14798. getunitdata_sub(UHOM_DEX, hd->base_status.dex);
  14799. getunitdata_sub(UHOM_LUK, hd->base_status.luk);
  14800. getunitdata_sub(UHOM_DMGIMMUNE, hd->ud.immune_attack);
  14801. getunitdata_sub(UHOM_ATKRANGE, hd->battle_status.rhw.range);
  14802. getunitdata_sub(UHOM_ATKMIN, hd->base_status.rhw.atk);
  14803. getunitdata_sub(UHOM_ATKMAX, hd->base_status.rhw.atk2);
  14804. getunitdata_sub(UHOM_MATKMIN, hd->base_status.matk_min);
  14805. getunitdata_sub(UHOM_MATKMAX, hd->base_status.matk_max);
  14806. getunitdata_sub(UHOM_DEF, hd->battle_status.def);
  14807. getunitdata_sub(UHOM_MDEF, hd->battle_status.mdef);
  14808. getunitdata_sub(UHOM_HIT, hd->battle_status.hit);
  14809. getunitdata_sub(UHOM_FLEE, hd->battle_status.flee);
  14810. getunitdata_sub(UHOM_PDODGE, hd->battle_status.flee2);
  14811. getunitdata_sub(UHOM_CRIT, hd->battle_status.cri);
  14812. getunitdata_sub(UHOM_RACE, hd->battle_status.race);
  14813. getunitdata_sub(UHOM_ELETYPE, hd->battle_status.def_ele);
  14814. getunitdata_sub(UHOM_ELELEVEL, hd->battle_status.ele_lv);
  14815. getunitdata_sub(UHOM_AMOTION, hd->battle_status.amotion);
  14816. getunitdata_sub(UHOM_ADELAY, hd->battle_status.adelay);
  14817. getunitdata_sub(UHOM_DMOTION, hd->battle_status.dmotion);
  14818. break;
  14819. case BL_PET:
  14820. if (!pd) {
  14821. ShowWarning("buildin_getunitdata: Error in finding object BL_PET!\n");
  14822. return SCRIPT_CMD_FAILURE;
  14823. }
  14824. getunitdata_sub(UPET_SIZE, pd->status.size);
  14825. getunitdata_sub(UPET_LEVEL, pd->pet.level);
  14826. getunitdata_sub(UPET_HP, pd->status.hp);
  14827. getunitdata_sub(UPET_MAXHP, pd->status.max_hp);
  14828. getunitdata_sub(UPET_MASTERAID, pd->pet.account_id);
  14829. getunitdata_sub(UPET_MAPID, pd->bl.m);
  14830. getunitdata_sub(UPET_X, pd->bl.x);
  14831. getunitdata_sub(UPET_Y, pd->bl.y);
  14832. getunitdata_sub(UPET_HUNGER, pd->pet.hungry);
  14833. getunitdata_sub(UPET_INTIMACY, pd->pet.intimate);
  14834. getunitdata_sub(UPET_SPEED, pd->status.speed);
  14835. getunitdata_sub(UPET_LOOKDIR, pd->ud.dir);
  14836. getunitdata_sub(UPET_CANMOVETICK, pd->ud.canmove_tick);
  14837. getunitdata_sub(UPET_STR, pd->status.str);
  14838. getunitdata_sub(UPET_AGI, pd->status.agi);
  14839. getunitdata_sub(UPET_VIT, pd->status.vit);
  14840. getunitdata_sub(UPET_INT, pd->status.int_);
  14841. getunitdata_sub(UPET_DEX, pd->status.dex);
  14842. getunitdata_sub(UPET_LUK, pd->status.luk);
  14843. getunitdata_sub(UPET_DMGIMMUNE, pd->ud.immune_attack);
  14844. getunitdata_sub(UPET_ATKRANGE, pd->status.rhw.range);
  14845. getunitdata_sub(UPET_ATKMIN, pd->status.rhw.atk);
  14846. getunitdata_sub(UPET_ATKMAX, pd->status.rhw.atk2);
  14847. getunitdata_sub(UPET_MATKMIN, pd->status.matk_min);
  14848. getunitdata_sub(UPET_MATKMAX, pd->status.matk_max);
  14849. getunitdata_sub(UPET_DEF, pd->status.def);
  14850. getunitdata_sub(UPET_MDEF, pd->status.mdef);
  14851. getunitdata_sub(UPET_HIT, pd->status.hit);
  14852. getunitdata_sub(UPET_FLEE, pd->status.flee);
  14853. getunitdata_sub(UPET_PDODGE, pd->status.flee2);
  14854. getunitdata_sub(UPET_CRIT, pd->status.cri);
  14855. getunitdata_sub(UPET_RACE, pd->status.race);
  14856. getunitdata_sub(UPET_ELETYPE, pd->status.def_ele);
  14857. getunitdata_sub(UPET_ELELEVEL, pd->status.ele_lv);
  14858. getunitdata_sub(UPET_AMOTION, pd->status.amotion);
  14859. getunitdata_sub(UPET_ADELAY, pd->status.adelay);
  14860. getunitdata_sub(UPET_DMOTION, pd->status.dmotion);
  14861. break;
  14862. case BL_MER:
  14863. if (!mc) {
  14864. ShowWarning("buildin_getunitdata: Error in finding object BL_MER!\n");
  14865. return SCRIPT_CMD_FAILURE;
  14866. }
  14867. getunitdata_sub(UMER_SIZE, mc->base_status.size);
  14868. getunitdata_sub(UMER_HP, mc->base_status.hp);
  14869. getunitdata_sub(UMER_MAXHP, mc->base_status.max_hp);
  14870. getunitdata_sub(UMER_MASTERCID, mc->mercenary.char_id);
  14871. getunitdata_sub(UMER_MAPID, mc->bl.m);
  14872. getunitdata_sub(UMER_X, mc->bl.x);
  14873. getunitdata_sub(UMER_Y, mc->bl.y);
  14874. getunitdata_sub(UMER_KILLCOUNT, mc->mercenary.kill_count);
  14875. getunitdata_sub(UMER_LIFETIME, mc->mercenary.life_time);
  14876. getunitdata_sub(UMER_SPEED, mc->base_status.speed);
  14877. getunitdata_sub(UMER_LOOKDIR, mc->ud.dir);
  14878. getunitdata_sub(UMER_CANMOVETICK, mc->ud.canmove_tick);
  14879. getunitdata_sub(UMER_STR, mc->base_status.str);
  14880. getunitdata_sub(UMER_AGI, mc->base_status.agi);
  14881. getunitdata_sub(UMER_VIT, mc->base_status.vit);
  14882. getunitdata_sub(UMER_INT, mc->base_status.int_);
  14883. getunitdata_sub(UMER_DEX, mc->base_status.dex);
  14884. getunitdata_sub(UMER_LUK, mc->base_status.luk);
  14885. getunitdata_sub(UMER_DMGIMMUNE, mc->ud.immune_attack);
  14886. getunitdata_sub(UMER_ATKRANGE, mc->base_status.rhw.range);
  14887. getunitdata_sub(UMER_ATKMIN, mc->base_status.rhw.atk);
  14888. getunitdata_sub(UMER_ATKMAX, mc->base_status.rhw.atk2);
  14889. getunitdata_sub(UMER_MATKMIN, mc->base_status.matk_min);
  14890. getunitdata_sub(UMER_MATKMAX, mc->base_status.matk_max);
  14891. getunitdata_sub(UMER_DEF, mc->base_status.def);
  14892. getunitdata_sub(UMER_MDEF, mc->base_status.mdef);
  14893. getunitdata_sub(UMER_HIT, mc->base_status.hit);
  14894. getunitdata_sub(UMER_FLEE, mc->base_status.flee);
  14895. getunitdata_sub(UMER_PDODGE, mc->base_status.flee2);
  14896. getunitdata_sub(UMER_CRIT, mc->base_status.cri);
  14897. getunitdata_sub(UMER_RACE, mc->base_status.race);
  14898. getunitdata_sub(UMER_ELETYPE, mc->base_status.def_ele);
  14899. getunitdata_sub(UMER_ELELEVEL, mc->base_status.ele_lv);
  14900. getunitdata_sub(UMER_AMOTION, mc->base_status.amotion);
  14901. getunitdata_sub(UMER_ADELAY, mc->base_status.adelay);
  14902. getunitdata_sub(UMER_DMOTION, mc->base_status.dmotion);
  14903. break;
  14904. case BL_ELEM:
  14905. if (!ed) {
  14906. ShowWarning("buildin_getunitdata: Error in finding object BL_ELEM!\n");
  14907. return SCRIPT_CMD_FAILURE;
  14908. }
  14909. getunitdata_sub(UELE_SIZE, ed->base_status.size);
  14910. getunitdata_sub(UELE_HP, ed->elemental.hp);
  14911. getunitdata_sub(UELE_MAXHP, ed->elemental.max_hp);
  14912. getunitdata_sub(UELE_SP, ed->elemental.sp);
  14913. getunitdata_sub(UELE_MAXSP, ed->elemental.max_sp);
  14914. getunitdata_sub(UELE_MASTERCID, ed->elemental.char_id);
  14915. getunitdata_sub(UELE_MAPID, ed->bl.m);
  14916. getunitdata_sub(UELE_X, ed->bl.x);
  14917. getunitdata_sub(UELE_Y, ed->bl.y);
  14918. getunitdata_sub(UELE_LIFETIME, ed->elemental.life_time);
  14919. getunitdata_sub(UELE_MODE, ed->elemental.mode);
  14920. getunitdata_sub(UELE_SP, ed->base_status.speed);
  14921. getunitdata_sub(UELE_LOOKDIR, ed->ud.dir);
  14922. getunitdata_sub(UELE_CANMOVETICK, ed->ud.canmove_tick);
  14923. getunitdata_sub(UELE_STR, ed->base_status.str);
  14924. getunitdata_sub(UELE_AGI, ed->base_status.agi);
  14925. getunitdata_sub(UELE_VIT, ed->base_status.vit);
  14926. getunitdata_sub(UELE_INT, ed->base_status.int_);
  14927. getunitdata_sub(UELE_DEX, ed->base_status.dex);
  14928. getunitdata_sub(UELE_LUK, ed->base_status.luk);
  14929. getunitdata_sub(UELE_DMGIMMUNE, ed->ud.immune_attack);
  14930. getunitdata_sub(UELE_ATKRANGE, ed->base_status.rhw.range);
  14931. getunitdata_sub(UELE_ATKMIN, ed->base_status.rhw.atk);
  14932. getunitdata_sub(UELE_ATKMAX, ed->base_status.rhw.atk2);
  14933. getunitdata_sub(UELE_MATKMIN, ed->base_status.matk_min);
  14934. getunitdata_sub(UELE_MATKMAX, ed->base_status.matk_max);
  14935. getunitdata_sub(UELE_DEF, ed->base_status.def);
  14936. getunitdata_sub(UELE_MDEF, ed->base_status.mdef);
  14937. getunitdata_sub(UELE_HIT, ed->base_status.hit);
  14938. getunitdata_sub(UELE_FLEE, ed->base_status.flee);
  14939. getunitdata_sub(UELE_PDODGE, ed->base_status.flee2);
  14940. getunitdata_sub(UELE_CRIT, ed->base_status.cri);
  14941. getunitdata_sub(UELE_RACE, ed->base_status.race);
  14942. getunitdata_sub(UELE_ELETYPE, ed->base_status.def_ele);
  14943. getunitdata_sub(UELE_ELELEVEL, ed->base_status.ele_lv);
  14944. getunitdata_sub(UELE_AMOTION, ed->base_status.amotion);
  14945. getunitdata_sub(UELE_ADELAY, ed->base_status.adelay);
  14946. getunitdata_sub(UELE_DMOTION, ed->base_status.dmotion);
  14947. break;
  14948. case BL_NPC:
  14949. if (!nd) {
  14950. ShowWarning("buildin_getunitdata: Error in finding object BL_NPC!\n");
  14951. return SCRIPT_CMD_FAILURE;
  14952. }
  14953. getunitdata_sub(UNPC_DISPLAY, nd->class_);
  14954. getunitdata_sub(UNPC_LEVEL, nd->level);
  14955. getunitdata_sub(UNPC_HP, nd->status.hp);
  14956. getunitdata_sub(UNPC_MAXHP, nd->status.max_hp);
  14957. getunitdata_sub(UNPC_MAPID, nd->bl.m);
  14958. getunitdata_sub(UNPC_X, nd->bl.x);
  14959. getunitdata_sub(UNPC_Y, nd->bl.y);
  14960. getunitdata_sub(UNPC_LOOKDIR, nd->ud.dir);
  14961. getunitdata_sub(UNPC_STR, nd->status.str);
  14962. getunitdata_sub(UNPC_AGI, nd->status.agi);
  14963. getunitdata_sub(UNPC_VIT, nd->status.vit);
  14964. getunitdata_sub(UNPC_INT, nd->status.int_);
  14965. getunitdata_sub(UNPC_DEX, nd->status.dex);
  14966. getunitdata_sub(UNPC_LUK, nd->status.luk);
  14967. getunitdata_sub(UNPC_PLUSALLSTAT, nd->stat_point);
  14968. getunitdata_sub(UNPC_DMGIMMUNE, nd->ud.immune_attack);
  14969. getunitdata_sub(UNPC_ATKRANGE, nd->status.rhw.range);
  14970. getunitdata_sub(UNPC_ATKMIN, nd->status.rhw.atk);
  14971. getunitdata_sub(UNPC_ATKMAX, nd->status.rhw.atk2);
  14972. getunitdata_sub(UNPC_MATKMIN, nd->status.matk_min);
  14973. getunitdata_sub(UNPC_MATKMAX, nd->status.matk_max);
  14974. getunitdata_sub(UNPC_DEF, nd->status.def);
  14975. getunitdata_sub(UNPC_MDEF, nd->status.mdef);
  14976. getunitdata_sub(UNPC_HIT, nd->status.hit);
  14977. getunitdata_sub(UNPC_FLEE, nd->status.flee);
  14978. getunitdata_sub(UNPC_PDODGE, nd->status.flee2);
  14979. getunitdata_sub(UNPC_CRIT, nd->status.cri);
  14980. getunitdata_sub(UNPC_RACE, nd->status.race);
  14981. getunitdata_sub(UNPC_ELETYPE, nd->status.def_ele);
  14982. getunitdata_sub(UNPC_ELELEVEL, nd->status.ele_lv);
  14983. getunitdata_sub(UNPC_AMOTION, nd->status.amotion);
  14984. getunitdata_sub(UNPC_ADELAY, nd->status.adelay);
  14985. getunitdata_sub(UNPC_DMOTION, nd->status.dmotion);
  14986. break;
  14987. default:
  14988. ShowWarning("buildin_getunitdata: Unknown object type!\n");
  14989. return SCRIPT_CMD_FAILURE;
  14990. }
  14991. #undef getunitdata_sub
  14992. return SCRIPT_CMD_SUCCESS;
  14993. }
  14994. /// Changes the live data of a bl.
  14995. ///
  14996. /// setunitdata <unit id>,<type>,<value>;
  14997. BUILDIN_FUNC(setunitdata)
  14998. {
  14999. struct block_list* bl = NULL;
  15000. struct script_data* data;
  15001. const char *mapname = NULL;
  15002. TBL_MOB* md = NULL;
  15003. TBL_HOM* hd = NULL;
  15004. TBL_MER* mc = NULL;
  15005. TBL_PET* pd = NULL;
  15006. TBL_ELEM* ed = NULL;
  15007. TBL_NPC* nd = NULL;
  15008. int type, value = 0;
  15009. bl = map_id2bl(script_getnum(st, 2));
  15010. if (!bl) {
  15011. ShowWarning("buildin_setunitdata: Error in finding object with given game ID %d!\n", script_getnum(st, 2));
  15012. return SCRIPT_CMD_FAILURE;
  15013. }
  15014. switch (bl->type) {
  15015. case BL_MOB: md = map_id2md(bl->id); break;
  15016. case BL_HOM: hd = map_id2hd(bl->id); break;
  15017. case BL_PET: pd = map_id2pd(bl->id); break;
  15018. case BL_MER: mc = map_id2mc(bl->id); break;
  15019. case BL_ELEM: ed = map_id2ed(bl->id); break;
  15020. case BL_NPC:
  15021. nd = map_id2nd(bl->id);
  15022. if (!nd->status.hp)
  15023. status_calc_npc(nd, SCO_FIRST);
  15024. else
  15025. status_calc_npc(nd, SCO_NONE);
  15026. break;
  15027. default:
  15028. ShowError("buildin_setunitdata: Invalid object!\n");
  15029. return SCRIPT_CMD_FAILURE;
  15030. }
  15031. type = script_getnum(st, 3);
  15032. data = script_getdata(st, 4);
  15033. get_val(st, data);
  15034. if (type == 5 && data_isstring(data))
  15035. mapname = conv_str(st, data);
  15036. else if (data_isint(data))
  15037. value = conv_num(st, data);
  15038. else {
  15039. ShowError("buildin_setunitdata: Invalid data type for argument #3 (%d).\n", data->type);
  15040. return SCRIPT_CMD_FAILURE;
  15041. }
  15042. switch (bl->type) {
  15043. case BL_MOB:
  15044. if (!md) {
  15045. ShowWarning("buildin_setunitdata: Error in finding object BL_MOB!\n");
  15046. return SCRIPT_CMD_FAILURE;
  15047. }
  15048. switch (type) {
  15049. case UMOB_SIZE: md->status.size = (unsigned char)value; break;
  15050. case UMOB_LEVEL: md->level = (unsigned short)value; break;
  15051. case UMOB_HP: status_set_hp(bl, (unsigned int)value, 0); clif_charnameack(0, &md->bl); break;
  15052. case UMOB_MAXHP: status_set_maxhp(bl, (unsigned int)value, 0); clif_charnameack(0, &md->bl); break;
  15053. case UMOB_MASTERAID: md->master_id = value; break;
  15054. case UMOB_MAPID: if (mapname) value = map_mapname2mapid(mapname); unit_warp(bl, (short)value, 0, 0, CLR_TELEPORT); break;
  15055. case UMOB_X: if (!unit_walktoxy(bl, (short)value, md->bl.y, 2)) unit_movepos(bl, (short)value, md->bl.y, 0, 0); break;
  15056. case UMOB_Y: if (!unit_walktoxy(bl, md->bl.x, (short)value, 2)) unit_movepos(bl, md->bl.x, (short)value, 0, 0); break;
  15057. case UMOB_SPEED: md->status.speed = (unsigned short)value; status_calc_misc(bl, &md->status, md->level); break;
  15058. case UMOB_MODE: md->status.mode = (enum e_mode)value; break;
  15059. case UMOB_AI: md->special_state.ai = (enum mob_ai)value; break;
  15060. case UMOB_SCOPTION: md->sc.option = (unsigned short)value; break;
  15061. case UMOB_SEX: md->vd->sex = (char)value; break;
  15062. case UMOB_CLASS: status_set_viewdata(bl, (unsigned short)value); break;
  15063. case UMOB_HAIRSTYLE: clif_changelook(bl, LOOK_HAIR, (unsigned short)value); break;
  15064. case UMOB_HAIRCOLOR: clif_changelook(bl, LOOK_HAIR_COLOR, (unsigned short)value); break;
  15065. case UMOB_HEADBOTTOM: clif_changelook(bl, LOOK_HEAD_BOTTOM, (unsigned short)value); break;
  15066. case UMOB_HEADMIDDLE: clif_changelook(bl, LOOK_HEAD_MID, (unsigned short)value); break;
  15067. case UMOB_HEADTOP: clif_changelook(bl, LOOK_HEAD_TOP, (unsigned short)value); break;
  15068. case UMOB_CLOTHCOLOR: clif_changelook(bl, LOOK_CLOTHES_COLOR, (unsigned short)value); break;
  15069. case UMOB_SHIELD: clif_changelook(bl, LOOK_SHIELD, (unsigned short)value); break;
  15070. case UMOB_WEAPON: clif_changelook(bl, LOOK_WEAPON, (unsigned short)value); break;
  15071. case UMOB_LOOKDIR: unit_setdir(bl, (uint8)value); break;
  15072. case UMOB_CANMOVETICK: md->ud.canmove_tick = value > 0 ? (unsigned int)value : 0; break;
  15073. case UMOB_STR: md->status.str = (unsigned short)value; status_calc_misc(bl, &md->status, md->level); break;
  15074. case UMOB_AGI: md->status.agi = (unsigned short)value; status_calc_misc(bl, &md->status, md->level); break;
  15075. case UMOB_VIT: md->status.vit = (unsigned short)value; status_calc_misc(bl, &md->status, md->level); break;
  15076. case UMOB_INT: md->status.int_ = (unsigned short)value; status_calc_misc(bl, &md->status, md->level); break;
  15077. case UMOB_DEX: md->status.dex = (unsigned short)value; status_calc_misc(bl, &md->status, md->level); break;
  15078. case UMOB_LUK: md->status.luk = (unsigned short)value; status_calc_misc(bl, &md->status, md->level); break;
  15079. case UMOB_SLAVECPYMSTRMD:
  15080. if (value > 0) {
  15081. TBL_MOB *md2 = NULL;
  15082. if (!md->master_id || !(md2 = map_id2md(md->master_id))) {
  15083. ShowWarning("buildin_setunitdata: Trying to set UMOB_SLAVECPYMSTRMD on mob without master!\n");
  15084. break;
  15085. }
  15086. md->status.mode = md2->status.mode;
  15087. md->state.copy_master_mode = 1;
  15088. } else
  15089. md->state.copy_master_mode = 0;
  15090. break;
  15091. case UMOB_DMGIMMUNE: md->ud.immune_attack = (bool)value > 0 ? 1 : 0; break;
  15092. case UMOB_ATKRANGE: md->status.rhw.range = (unsigned short)value; break;
  15093. case UMOB_ATKMIN: md->status.rhw.atk = (unsigned short)value; break;
  15094. case UMOB_ATKMAX: md->status.rhw.atk2 = (unsigned short)value; break;
  15095. case UMOB_MATKMIN: md->status.matk_min = (unsigned short)value; break;
  15096. case UMOB_MATKMAX: md->status.matk_max = (unsigned short)value; break;
  15097. case UMOB_DEF: md->status.def = (defType)value; break;
  15098. case UMOB_MDEF: md->status.mdef = (defType)value; break;
  15099. case UMOB_HIT: md->status.hit = (short)value; break;
  15100. case UMOB_FLEE: md->status.flee = (short)value; break;
  15101. case UMOB_PDODGE: md->status.flee2 = (short)value; break;
  15102. case UMOB_CRIT: md->status.cri = (short)value; break;
  15103. case UMOB_RACE: md->status.race = (unsigned char)value; break;
  15104. case UMOB_ELETYPE: md->status.def_ele = (unsigned char)value; break;
  15105. case UMOB_ELELEVEL: md->status.ele_lv = (unsigned char)value; break;
  15106. case UMOB_AMOTION: md->status.amotion = (short)value; break;
  15107. case UMOB_ADELAY: md->status.adelay = (short)value; break;
  15108. case UMOB_DMOTION: md->status.dmotion = (short)value; break;
  15109. default:
  15110. ShowError("buildin_setunitdata: Unknown data identifier %d for BL_MOB.\n", type);
  15111. return SCRIPT_CMD_FAILURE;
  15112. }
  15113. break;
  15114. case BL_HOM:
  15115. if (!hd) {
  15116. ShowWarning("buildin_setunitdata: Error in finding object BL_HOM!\n");
  15117. return SCRIPT_CMD_FAILURE;
  15118. }
  15119. switch (type) {
  15120. case UHOM_SIZE: hd->base_status.size = (unsigned char)value; break;
  15121. case UHOM_LEVEL: hd->homunculus.level = (unsigned short)value; break;
  15122. case UHOM_HP: status_set_hp(bl, (unsigned int)value, 0); break;
  15123. case UHOM_MAXHP: status_set_maxhp(bl, (unsigned int)value, 0); break;
  15124. case UHOM_SP: status_set_sp(bl, (unsigned int)value, 0); break;
  15125. case UHOM_MAXSP: status_set_maxsp(bl, (unsigned int)value, 0); break;
  15126. case UHOM_MASTERCID: hd->homunculus.char_id = (uint32)value; break;
  15127. case UHOM_MAPID: if (mapname) value = map_mapname2mapid(mapname); unit_warp(bl, (short)value, 0, 0, CLR_TELEPORT); break;
  15128. case UHOM_X: if (!unit_walktoxy(bl, (short)value, hd->bl.y, 2)) unit_movepos(bl, (short)value, hd->bl.y, 0, 0); break;
  15129. case UHOM_Y: if (!unit_walktoxy(bl, hd->bl.x, (short)value, 2)) unit_movepos(bl, hd->bl.x, (short)value, 0, 0); break;
  15130. case UHOM_HUNGER: hd->homunculus.hunger = (short)value; clif_send_homdata(map_charid2sd(hd->homunculus.char_id), SP_HUNGRY, hd->homunculus.hunger); break;
  15131. 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;
  15132. case UHOM_SPEED: hd->base_status.speed = (unsigned short)value; status_calc_misc(bl, &hd->base_status, hd->homunculus.level); break;
  15133. case UHOM_LOOKDIR: unit_setdir(bl, (uint8)value); break;
  15134. case UHOM_CANMOVETICK: hd->ud.canmove_tick = value > 0 ? (unsigned int)value : 0; break;
  15135. case UHOM_STR: hd->base_status.str = (unsigned short)value; status_calc_misc(bl, &hd->base_status, hd->homunculus.level); break;
  15136. case UHOM_AGI: hd->base_status.agi = (unsigned short)value; status_calc_misc(bl, &hd->base_status, hd->homunculus.level); break;
  15137. case UHOM_VIT: hd->base_status.vit = (unsigned short)value; status_calc_misc(bl, &hd->base_status, hd->homunculus.level); break;
  15138. case UHOM_INT: hd->base_status.int_ = (unsigned short)value; status_calc_misc(bl, &hd->base_status, hd->homunculus.level); break;
  15139. case UHOM_DEX: hd->base_status.dex = (unsigned short)value; status_calc_misc(bl, &hd->base_status, hd->homunculus.level); break;
  15140. case UHOM_LUK: hd->base_status.luk = (unsigned short)value; status_calc_misc(bl, &hd->base_status, hd->homunculus.level); break;
  15141. case UHOM_DMGIMMUNE: hd->ud.immune_attack = (bool)value > 0 ? 1 : 0; break;
  15142. case UHOM_ATKRANGE: hd->base_status.rhw.range = (unsigned short)value; break;
  15143. case UHOM_ATKMIN: hd->base_status.rhw.atk = (unsigned short)value; break;
  15144. case UHOM_ATKMAX: hd->base_status.rhw.atk2 = (unsigned short)value; break;
  15145. case UHOM_MATKMIN: hd->base_status.matk_min = (unsigned short)value; break;
  15146. case UHOM_MATKMAX: hd->base_status.matk_max = (unsigned short)value; break;
  15147. case UHOM_DEF: hd->base_status.def = (defType)value; break;
  15148. case UHOM_MDEF: hd->base_status.mdef = (defType)value; break;
  15149. case UHOM_HIT: hd->base_status.hit = (short)value; break;
  15150. case UHOM_FLEE: hd->base_status.flee = (short)value; break;
  15151. case UHOM_PDODGE: hd->base_status.flee2 = (short)value; break;
  15152. case UHOM_CRIT: hd->base_status.cri = (short)value; break;
  15153. case UHOM_RACE: hd->base_status.race = (unsigned char)value; break;
  15154. case UHOM_ELETYPE: hd->base_status.def_ele = (unsigned char)value; break;
  15155. case UHOM_ELELEVEL: hd->base_status.ele_lv = (unsigned char)value; break;
  15156. case UHOM_AMOTION: hd->base_status.amotion = (short)value; break;
  15157. case UHOM_ADELAY: hd->base_status.adelay = (short)value; break;
  15158. case UHOM_DMOTION: hd->base_status.dmotion = (short)value; break;
  15159. default:
  15160. ShowError("buildin_setunitdata: Unknown data identifier %d for BL_HOM.\n", type);
  15161. return SCRIPT_CMD_FAILURE;
  15162. }
  15163. break;
  15164. case BL_PET:
  15165. if (!pd) {
  15166. ShowWarning("buildin_setunitdata: Error in finding object BL_PET!\n");
  15167. return SCRIPT_CMD_FAILURE;
  15168. }
  15169. switch (type) {
  15170. case UPET_SIZE: pd->status.size = (unsigned char)value; break;
  15171. case UPET_LEVEL: pd->pet.level = (unsigned short)value; break;
  15172. case UPET_HP: status_set_hp(bl, (unsigned int)value, 0); break;
  15173. case UPET_MAXHP: status_set_maxhp(bl, (unsigned int)value, 0); break;
  15174. case UPET_MASTERAID: pd->pet.account_id = (unsigned int)value; break;
  15175. case UPET_MAPID: if (mapname) value = map_mapname2mapid(mapname); unit_warp(bl, (short)value, 0, 0, CLR_TELEPORT); break;
  15176. case UPET_X: if (!unit_walktoxy(bl, (short)value, pd->bl.y, 2)) unit_movepos(bl, (short)value, md->bl.y, 0, 0); break;
  15177. case UPET_Y: if (!unit_walktoxy(bl, pd->bl.x, (short)value, 2)) unit_movepos(bl, pd->bl.x, (short)value, 0, 0); break;
  15178. case UPET_HUNGER: pd->pet.hungry = (short)value; clif_send_petdata(map_id2sd(pd->pet.account_id), pd, 2, pd->pet.hungry); break;
  15179. 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;
  15180. case UPET_SPEED: pd->status.speed = (unsigned short)value; status_calc_misc(bl, &pd->status, pd->pet.level); break;
  15181. case UPET_LOOKDIR: unit_setdir(bl, (uint8)value); break;
  15182. case UPET_CANMOVETICK: pd->ud.canmove_tick = value > 0 ? (unsigned int)value : 0; break;
  15183. case UPET_STR: pd->status.str = (unsigned short)value; status_calc_misc(bl, &pd->status, pd->pet.level); break;
  15184. case UPET_AGI: pd->status.agi = (unsigned short)value; status_calc_misc(bl, &pd->status, pd->pet.level); break;
  15185. case UPET_VIT: pd->status.vit = (unsigned short)value; status_calc_misc(bl, &pd->status, pd->pet.level); break;
  15186. case UPET_INT: pd->status.int_ = (unsigned short)value; status_calc_misc(bl, &pd->status, pd->pet.level); break;
  15187. case UPET_DEX: pd->status.dex = (unsigned short)value; status_calc_misc(bl, &pd->status, pd->pet.level); break;
  15188. case UPET_LUK: pd->status.luk = (unsigned short)value; status_calc_misc(bl, &pd->status, pd->pet.level); break;
  15189. case UPET_DMGIMMUNE: pd->ud.immune_attack = (bool)value > 0 ? 1 : 0; break;
  15190. case UPET_ATKRANGE: pd->status.rhw.range = (unsigned short)value; break;
  15191. case UPET_ATKMIN: pd->status.rhw.atk = (unsigned short)value; break;
  15192. case UPET_ATKMAX: pd->status.rhw.atk2 = (unsigned short)value; break;
  15193. case UPET_MATKMIN: pd->status.matk_min = (unsigned short)value; break;
  15194. case UPET_MATKMAX: pd->status.matk_max = (unsigned short)value; break;
  15195. case UPET_DEF: pd->status.def = (defType)value; break;
  15196. case UPET_MDEF: pd->status.mdef = (defType)value; break;
  15197. case UPET_HIT: pd->status.hit = (short)value; break;
  15198. case UPET_FLEE: pd->status.flee = (short)value; break;
  15199. case UPET_PDODGE: pd->status.flee2 = (short)value; break;
  15200. case UPET_CRIT: pd->status.cri = (short)value; break;
  15201. case UPET_RACE: pd->status.race = (unsigned char)value; break;
  15202. case UPET_ELETYPE: pd->status.def_ele = (unsigned char)value; break;
  15203. case UPET_ELELEVEL: pd->status.ele_lv = (unsigned char)value; break;
  15204. case UPET_AMOTION: pd->status.amotion = (short)value; break;
  15205. case UPET_ADELAY: pd->status.adelay = (short)value; break;
  15206. case UPET_DMOTION: pd->status.dmotion = (short)value; break;
  15207. default:
  15208. ShowError("buildin_setunitdata: Unknown data identifier %d for BL_PET.\n", type);
  15209. return SCRIPT_CMD_FAILURE;
  15210. }
  15211. break;
  15212. case BL_MER:
  15213. if (!mc) {
  15214. ShowWarning("buildin_setunitdata: Error in finding object BL_MER!\n");
  15215. return SCRIPT_CMD_FAILURE;
  15216. }
  15217. switch (type) {
  15218. case UMER_SIZE: mc->base_status.size = (unsigned char)value; break;
  15219. case UMER_HP: status_set_hp(bl, (unsigned int)value, 0); break;
  15220. case UMER_MAXHP: status_set_maxhp(bl, (unsigned int)value, 0); break;
  15221. case UMER_MASTERCID: mc->mercenary.char_id = (uint32)value; break;
  15222. case UMER_MAPID: if (mapname) value = map_mapname2mapid(mapname); unit_warp(bl, (short)value, 0, 0, CLR_TELEPORT); break;
  15223. case UMER_X: if (!unit_walktoxy(bl, (short)value, mc->bl.y, 2)) unit_movepos(bl, (short)value, mc->bl.y, 0, 0); break;
  15224. case UMER_Y: if (!unit_walktoxy(bl, mc->bl.x, (short)value, 2)) unit_movepos(bl, mc->bl.x, (short)value, 0, 0); break;
  15225. case UMER_KILLCOUNT: mc->mercenary.kill_count = (unsigned int)value; break;
  15226. case UMER_LIFETIME: mc->mercenary.life_time = (unsigned int)value; break;
  15227. case UMER_SPEED: mc->base_status.speed = (unsigned short)value; status_calc_misc(bl, &mc->base_status, mc->db->lv); break;
  15228. case UMER_LOOKDIR: unit_setdir(bl, (uint8)value); break;
  15229. case UMER_CANMOVETICK: mc->ud.canmove_tick = value > 0 ? (unsigned int)value : 0; break;
  15230. case UMER_STR: mc->base_status.str = (unsigned short)value; status_calc_misc(bl, &mc->base_status, mc->db->lv); break;
  15231. case UMER_AGI: mc->base_status.agi = (unsigned short)value; status_calc_misc(bl, &mc->base_status, mc->db->lv); break;
  15232. case UMER_VIT: mc->base_status.vit = (unsigned short)value; status_calc_misc(bl, &mc->base_status, mc->db->lv); break;
  15233. case UMER_INT: mc->base_status.int_ = (unsigned short)value; status_calc_misc(bl, &mc->base_status, mc->db->lv); break;
  15234. case UMER_DEX: mc->base_status.dex = (unsigned short)value; status_calc_misc(bl, &mc->base_status, mc->db->lv); break;
  15235. case UMER_LUK: mc->base_status.luk = (unsigned short)value; status_calc_misc(bl, &mc->base_status, mc->db->lv); break;
  15236. case UMER_DMGIMMUNE: mc->ud.immune_attack = (bool)value > 0 ? 1 : 0; break;
  15237. case UMER_ATKRANGE: mc->base_status.rhw.range = (unsigned short)value; break;
  15238. case UMER_ATKMIN: mc->base_status.rhw.atk = (unsigned short)value; break;
  15239. case UMER_ATKMAX: mc->base_status.rhw.atk2 = (unsigned short)value; break;
  15240. case UMER_MATKMIN: mc->base_status.matk_min = (unsigned short)value; break;
  15241. case UMER_MATKMAX: mc->base_status.matk_max = (unsigned short)value; break;
  15242. case UMER_DEF: mc->base_status.def = (defType)value; break;
  15243. case UMER_MDEF: mc->base_status.mdef = (defType)value; break;
  15244. case UMER_HIT: mc->base_status.hit = (short)value; break;
  15245. case UMER_FLEE: mc->base_status.flee = (short)value; break;
  15246. case UMER_PDODGE: mc->base_status.flee2 = (short)value; break;
  15247. case UMER_CRIT: mc->base_status.cri = (short)value; break;
  15248. case UMER_RACE: mc->base_status.race = (unsigned char)value; break;
  15249. case UMER_ELETYPE: mc->base_status.def_ele = (unsigned char)value; break;
  15250. case UMER_ELELEVEL: mc->base_status.ele_lv = (unsigned char)value; break;
  15251. case UMER_AMOTION: mc->base_status.amotion = (short)value; break;
  15252. case UMER_ADELAY: mc->base_status.adelay = (short)value; break;
  15253. case UMER_DMOTION: mc->base_status.dmotion = (short)value; break;
  15254. default:
  15255. ShowError("buildin_setunitdata: Unknown data identifier %d for BL_MER.\n", type);
  15256. return SCRIPT_CMD_FAILURE;
  15257. }
  15258. break;
  15259. case BL_ELEM:
  15260. if (!ed) {
  15261. ShowWarning("buildin_setunitdata: Error in finding object BL_ELEM!\n");
  15262. return SCRIPT_CMD_FAILURE;
  15263. }
  15264. switch (type) {
  15265. case UELE_SIZE: ed->base_status.size = (unsigned char)value; break;
  15266. case UELE_HP: status_set_hp(bl, (unsigned int)value, 0); break;
  15267. case UELE_MAXHP: status_set_maxhp(bl, (unsigned int)value, 0); break;
  15268. case UELE_SP: status_set_sp(bl, (unsigned int)value, 0); break;
  15269. case UELE_MAXSP: status_set_maxsp(bl, (unsigned int)value, 0); break;
  15270. case UELE_MASTERCID: ed->elemental.char_id = (uint32)value; break;
  15271. case UELE_MAPID: if (mapname) value = map_mapname2mapid(mapname); unit_warp(bl, (short)value, 0, 0, CLR_TELEPORT); break;
  15272. case UELE_X: if (!unit_walktoxy(bl, (short)value, ed->bl.y, 2)) unit_movepos(bl, (short)value, ed->bl.y, 0, 0); break;
  15273. case UELE_Y: if (!unit_walktoxy(bl, ed->bl.x, (short)value, 2)) unit_movepos(bl, ed->bl.x, (short)value, 0, 0); break;
  15274. case UELE_LIFETIME: ed->elemental.life_time = (unsigned int)value; break;
  15275. case UELE_MODE: ed->elemental.mode = (enum e_mode)value; break;
  15276. case UELE_SPEED: ed->base_status.speed = (unsigned short)value; status_calc_misc(bl, &ed->base_status, ed->db->lv); break;
  15277. case UELE_LOOKDIR: unit_setdir(bl, (uint8)value); break;
  15278. case UELE_CANMOVETICK: ed->ud.canmove_tick = value > 0 ? (unsigned int)value : 0; break;
  15279. case UELE_STR: ed->base_status.str = (unsigned short)value; status_calc_misc(bl, &ed->base_status, ed->db->lv); break;
  15280. case UELE_AGI: ed->base_status.agi = (unsigned short)value; status_calc_misc(bl, &ed->base_status, ed->db->lv); break;
  15281. case UELE_VIT: ed->base_status.vit = (unsigned short)value; status_calc_misc(bl, &ed->base_status, ed->db->lv); break;
  15282. case UELE_INT: ed->base_status.int_ = (unsigned short)value; status_calc_misc(bl, &ed->base_status, ed->db->lv); break;
  15283. case UELE_DEX: ed->base_status.dex = (unsigned short)value; status_calc_misc(bl, &ed->base_status, ed->db->lv); break;
  15284. case UELE_LUK: ed->base_status.luk = (unsigned short)value; status_calc_misc(bl, &ed->base_status, ed->db->lv); break;
  15285. case UELE_DMGIMMUNE: ed->ud.immune_attack = (bool)value > 0 ? 1 : 0; break;
  15286. case UELE_ATKRANGE: ed->base_status.rhw.range = (unsigned short)value; break;
  15287. case UELE_ATKMIN: ed->base_status.rhw.atk = (unsigned short)value; break;
  15288. case UELE_ATKMAX: ed->base_status.rhw.atk2 = (unsigned short)value; break;
  15289. case UELE_MATKMIN: ed->base_status.matk_min = (unsigned short)value; break;
  15290. case UELE_MATKMAX: ed->base_status.matk_max = (unsigned short)value; break;
  15291. case UELE_DEF: ed->base_status.def = (defType)value; break;
  15292. case UELE_MDEF: ed->base_status.mdef = (defType)value; break;
  15293. case UELE_HIT: ed->base_status.hit = (short)value; break;
  15294. case UELE_FLEE: ed->base_status.flee = (short)value; break;
  15295. case UELE_PDODGE: ed->base_status.flee2 = (short)value; break;
  15296. case UELE_CRIT: ed->base_status.cri = (short)value; break;
  15297. case UELE_RACE: ed->base_status.race = (unsigned char)value; break;
  15298. case UELE_ELETYPE: ed->base_status.def_ele = (unsigned char)value; break;
  15299. case UELE_ELELEVEL: ed->base_status.ele_lv = (unsigned char)value; break;
  15300. case UELE_AMOTION: ed->base_status.amotion = (short)value; break;
  15301. case UELE_ADELAY: ed->base_status.adelay = (short)value; break;
  15302. case UELE_DMOTION: ed->base_status.dmotion = (short)value; break;
  15303. default:
  15304. ShowError("buildin_setunitdata: Unknown data identifier %d for BL_ELEM.\n", type);
  15305. return SCRIPT_CMD_FAILURE;
  15306. }
  15307. break;
  15308. case BL_NPC:
  15309. if (!nd) {
  15310. ShowWarning("buildin_setunitdata: Error in finding object BL_NPC!\n");
  15311. return SCRIPT_CMD_FAILURE;
  15312. }
  15313. switch (type) {
  15314. case UNPC_DISPLAY: status_set_viewdata(bl, (unsigned short)value); break;
  15315. case UNPC_LEVEL: nd->level = (unsigned int)value; break;
  15316. case UNPC_HP: status_set_hp(bl, (unsigned int)value, 0); break;
  15317. case UNPC_MAXHP: status_set_maxhp(bl, (unsigned int)value, 0); break;
  15318. case UNPC_MAPID: if (mapname) value = map_mapname2mapid(mapname); unit_warp(bl, (short)value, 0, 0, CLR_TELEPORT); break;
  15319. case UNPC_X: if (!unit_walktoxy(bl, (short)value, nd->bl.y, 2)) unit_movepos(bl, (short)value, nd->bl.x, 0, 0); break;
  15320. case UNPC_Y: if (!unit_walktoxy(bl, nd->bl.x, (short)value, 2)) unit_movepos(bl, nd->bl.x, (short)value, 0, 0); break;
  15321. case UNPC_LOOKDIR: unit_setdir(bl, (uint8)value); break;
  15322. case UNPC_STR: nd->params.str = (unsigned short)value; status_calc_misc(bl, &nd->status, nd->level); break;
  15323. case UNPC_AGI: nd->params.agi = (unsigned short)value; status_calc_misc(bl, &nd->status, nd->level); break;
  15324. case UNPC_VIT: nd->params.vit = (unsigned short)value; status_calc_misc(bl, &nd->status, nd->level); break;
  15325. case UNPC_INT: nd->params.int_ = (unsigned short)value; status_calc_misc(bl, &nd->status, nd->level); break;
  15326. case UNPC_DEX: nd->params.dex = (unsigned short)value; status_calc_misc(bl, &nd->status, nd->level); break;
  15327. case UNPC_LUK: nd->params.luk = (unsigned short)value; status_calc_misc(bl, &nd->status, nd->level); break;
  15328. case UNPC_PLUSALLSTAT: nd->stat_point = (unsigned int)value; break;
  15329. case UNPC_ATKRANGE: nd->status.rhw.range = (unsigned short)value; break;
  15330. case UNPC_ATKMIN: nd->status.rhw.atk = (unsigned short)value; break;
  15331. case UNPC_ATKMAX: nd->status.rhw.atk2 = (unsigned short)value; break;
  15332. case UNPC_MATKMIN: nd->status.matk_min = (unsigned short)value; break;
  15333. case UNPC_MATKMAX: nd->status.matk_max = (unsigned short)value; break;
  15334. case UNPC_DEF: nd->status.def = (defType)value; break;
  15335. case UNPC_MDEF: nd->status.mdef = (defType)value; break;
  15336. case UNPC_HIT: nd->status.hit = (short)value; break;
  15337. case UNPC_FLEE: nd->status.flee = (short)value; break;
  15338. case UNPC_PDODGE: nd->status.flee2 = (short)value; break;
  15339. case UNPC_CRIT: nd->status.cri = (short)value; break;
  15340. case UNPC_RACE: nd->status.race = (unsigned char)value; break;
  15341. case UNPC_ELETYPE: nd->status.def_ele = (unsigned char)value; break;
  15342. case UNPC_ELELEVEL: nd->status.ele_lv = (unsigned char)value; break;
  15343. case UNPC_AMOTION: nd->status.amotion = (short)value; break;
  15344. case UNPC_ADELAY: nd->status.adelay = (short)value; break;
  15345. case UNPC_DMOTION: nd->status.dmotion = (short)value; break;
  15346. default:
  15347. ShowError("buildin_setunitdata: Unknown data identifier %d for BL_NPC.\n", type);
  15348. return SCRIPT_CMD_FAILURE;
  15349. }
  15350. break;
  15351. default:
  15352. ShowWarning("buildin_setunitdata: Unknown object type!\n");
  15353. return SCRIPT_CMD_FAILURE;
  15354. }
  15355. // Client information updates
  15356. switch (bl->type) {
  15357. case BL_HOM:
  15358. clif_send_homdata(hd->master, SP_ACK, 0);
  15359. break;
  15360. case BL_PET:
  15361. clif_send_petstatus(pd->master);
  15362. break;
  15363. case BL_MER:
  15364. clif_mercenary_info(map_charid2sd(mc->mercenary.char_id));
  15365. clif_mercenary_skillblock(map_charid2sd(mc->mercenary.char_id));
  15366. break;
  15367. case BL_ELEM:
  15368. clif_elemental_info(ed->master);
  15369. break;
  15370. }
  15371. return SCRIPT_CMD_SUCCESS;
  15372. }
  15373. /// Gets the name of a bl.
  15374. /// Supported types are [MOB|HOM|PET|NPC].
  15375. /// MER and ELEM don't support custom names.
  15376. ///
  15377. /// getunitname <unit id>;
  15378. BUILDIN_FUNC(getunitname)
  15379. {
  15380. struct block_list* bl = NULL;
  15381. bl = map_id2bl(script_getnum(st, 2));
  15382. if (!bl) {
  15383. ShowWarning("buildin_getunitname: Error in finding object with given game ID %d!\n", script_getnum(st, 2));
  15384. return SCRIPT_CMD_FAILURE;
  15385. }
  15386. script_pushstrcopy(st, status_get_name(bl));
  15387. return SCRIPT_CMD_SUCCESS;
  15388. }
  15389. /// Changes the name of a bl.
  15390. /// Supported types are [MOB|HOM|PET].
  15391. /// For NPC see 'setnpcdisplay', MER and ELEM don't support custom names.
  15392. ///
  15393. /// setunitname <unit id>,<name>;
  15394. BUILDIN_FUNC(setunitname)
  15395. {
  15396. struct block_list* bl = NULL;
  15397. TBL_MOB* md = NULL;
  15398. TBL_HOM* hd = NULL;
  15399. TBL_PET* pd = NULL;
  15400. bl = map_id2bl(script_getnum(st, 2));
  15401. if (!bl) {
  15402. ShowWarning("buildin_setunitname: Error in finding object with given game ID %d!\n", script_getnum(st, 2));
  15403. return SCRIPT_CMD_FAILURE;
  15404. }
  15405. switch (bl->type) {
  15406. case BL_MOB: md = map_id2md(bl->id); break;
  15407. case BL_HOM: hd = map_id2hd(bl->id); break;
  15408. case BL_PET: pd = map_id2pd(bl->id); break;
  15409. }
  15410. switch (bl->type) {
  15411. case BL_MOB:
  15412. if (!md) {
  15413. ShowWarning("buildin_setunitname: Error in finding object BL_MOB!\n");
  15414. return SCRIPT_CMD_FAILURE;
  15415. }
  15416. safestrncpy(md->name, script_getstr(st, 3), NAME_LENGTH);
  15417. break;
  15418. case BL_HOM:
  15419. if (!hd) {
  15420. ShowWarning("buildin_setunitname: Error in finding object BL_HOM!\n");
  15421. return SCRIPT_CMD_FAILURE;
  15422. }
  15423. safestrncpy(hd->homunculus.name, script_getstr(st, 3), NAME_LENGTH);
  15424. break;
  15425. case BL_PET:
  15426. if (!pd) {
  15427. ShowWarning("buildin_setunitname: Error in finding object BL_PET!\n");
  15428. return SCRIPT_CMD_FAILURE;
  15429. }
  15430. safestrncpy(pd->pet.name, script_getstr(st, 3), NAME_LENGTH);
  15431. break;
  15432. default:
  15433. ShowWarning("buildin_setunitname: Unknown object type!\n");
  15434. return SCRIPT_CMD_FAILURE;
  15435. }
  15436. clif_charnameack(0, bl); // Send update to client.
  15437. return SCRIPT_CMD_SUCCESS;
  15438. }
  15439. /// Makes the unit walk to target position or map.
  15440. /// Returns if it was successful.
  15441. ///
  15442. /// unitwalk(<unit_id>,<x>,<y>{,<event_label>}) -> <bool>
  15443. /// unitwalkto(<unit_id>,<target_id>{,<event_label>}) -> <bool>
  15444. BUILDIN_FUNC(unitwalk)
  15445. {
  15446. struct block_list* bl;
  15447. struct unit_data *ud = NULL;
  15448. const char *cmd = script_getfuncname(st), *done_label = "";
  15449. uint8 off = 5;
  15450. bl = map_id2bl(script_getnum(st,2));
  15451. if (!bl) {
  15452. ShowError("buildin_unitwalk: Invalid unit with ID '%d'.\n", script_getnum(st,2));
  15453. script_pushint(st, 0);
  15454. return SCRIPT_CMD_FAILURE;
  15455. }
  15456. ud = unit_bl2ud(bl);
  15457. if (!strcmp(cmd,"unitwalk")) {
  15458. int x = script_getnum(st,3);
  15459. int y = script_getnum(st,4);
  15460. if (script_pushint(st, unit_can_reach_pos(bl,x,y,0)))
  15461. add_timer(gettick()+50, unit_delay_walktoxy_timer, bl->id, (x<<16)|(y&0xFFFF)); // Need timer to avoid mismatches
  15462. } else {
  15463. struct block_list* tbl = map_id2bl(script_getnum(st,3));
  15464. if (!tbl) {
  15465. ShowError("buildin_unitwalk: Bad target destination.\n");
  15466. script_pushint(st, 0);
  15467. return SCRIPT_CMD_FAILURE;
  15468. } else if (script_pushint(st, unit_can_reach_bl(bl, tbl, distance_bl(bl, tbl)+1, 0, NULL, NULL)))
  15469. add_timer(gettick()+50, unit_delay_walktobl_timer, bl->id, tbl->id); // Need timer to avoid mismatches
  15470. off = 4;
  15471. }
  15472. if (ud && script_hasdata(st, off)) {
  15473. done_label = script_getstr(st, off);
  15474. check_event(st, done_label);
  15475. safestrncpy(ud->walk_done_event, done_label, sizeof(ud->walk_done_event));
  15476. }
  15477. return SCRIPT_CMD_SUCCESS;
  15478. }
  15479. /// Kills the unit.
  15480. ///
  15481. /// unitkill <unit_id>;
  15482. BUILDIN_FUNC(unitkill)
  15483. {
  15484. struct block_list* bl = map_id2bl(script_getnum(st,2));
  15485. if (bl != NULL)
  15486. status_kill(bl);
  15487. return SCRIPT_CMD_SUCCESS;
  15488. }
  15489. /// Warps the unit to the target position in the target map.
  15490. /// Returns if it was successful.
  15491. ///
  15492. /// unitwarp(<unit_id>,"<map name>",<x>,<y>) -> <bool>
  15493. BUILDIN_FUNC(unitwarp)
  15494. {
  15495. int unit_id;
  15496. int map_idx;
  15497. short x;
  15498. short y;
  15499. struct block_list* bl;
  15500. const char *mapname;
  15501. unit_id = script_getnum(st,2);
  15502. mapname = script_getstr(st, 3);
  15503. x = (short)script_getnum(st,4);
  15504. y = (short)script_getnum(st,5);
  15505. if (!unit_id) //Warp the script's runner
  15506. bl = map_id2bl(st->rid);
  15507. else
  15508. bl = map_id2bl(unit_id);
  15509. if (!strcmp(mapname,"this"))
  15510. map_idx = bl?bl->m:-1;
  15511. else
  15512. map_idx = map_mapname2mapid(mapname);
  15513. if (map_idx >= 0 && bl != NULL)
  15514. script_pushint(st, unit_warp(bl,map_idx,x,y,CLR_OUTSIGHT));
  15515. else
  15516. script_pushint(st, 0);
  15517. return SCRIPT_CMD_SUCCESS;
  15518. }
  15519. /// Makes the unit attack the target.
  15520. /// If the unit is a player and <action type> is not 0, it does a continuous
  15521. /// attack instead of a single attack.
  15522. /// Returns if the request was successful.
  15523. ///
  15524. /// unitattack(<unit_id>,"<target name>"{,<action type>}) -> <bool>
  15525. /// unitattack(<unit_id>,<target_id>{,<action type>}) -> <bool>
  15526. BUILDIN_FUNC(unitattack)
  15527. {
  15528. struct block_list* unit_bl;
  15529. struct block_list* target_bl = NULL;
  15530. struct script_data* data;
  15531. int actiontype = 0;
  15532. unit_bl = map_id2bl(script_getnum(st,2));
  15533. if (!unit_bl) {
  15534. script_pushint(st, 0);
  15535. return SCRIPT_CMD_FAILURE;
  15536. }
  15537. data = script_getdata(st, 3);
  15538. get_val(st, data);
  15539. if (data_isstring(data)) {
  15540. TBL_PC* sd = map_nick2sd(conv_str(st, data));
  15541. if( sd != NULL )
  15542. target_bl = &sd->bl;
  15543. } else
  15544. target_bl = map_id2bl(conv_num(st, data));
  15545. if (!target_bl) {
  15546. script_pushint(st, 0);
  15547. return SCRIPT_CMD_FAILURE;
  15548. }
  15549. if (script_hasdata(st,4))
  15550. actiontype = script_getnum(st,4);
  15551. switch(unit_bl->type) {
  15552. case BL_PC:
  15553. clif_parse_ActionRequest_sub(((TBL_PC *)unit_bl), actiontype > 0 ? 0x07 : 0x00, target_bl->id, gettick());
  15554. script_pushint(st, 1);
  15555. return SCRIPT_CMD_SUCCESS;
  15556. case BL_MOB:
  15557. ((TBL_MOB *)unit_bl)->target_id = target_bl->id;
  15558. break;
  15559. case BL_PET:
  15560. ((TBL_PET *)unit_bl)->target_id = target_bl->id;
  15561. break;
  15562. default:
  15563. ShowError("buildin_unitattack: Unsupported source unit type %d.\n", unit_bl->type);
  15564. script_pushint(st, 0);
  15565. return SCRIPT_CMD_FAILURE;
  15566. }
  15567. script_pushint(st, unit_walktobl(unit_bl, target_bl, 65025, 2));
  15568. return SCRIPT_CMD_SUCCESS;
  15569. }
  15570. /// Makes the unit stop attacking.
  15571. ///
  15572. /// unitstopattack <unit_id>;
  15573. BUILDIN_FUNC(unitstopattack)
  15574. {
  15575. int unit_id;
  15576. struct block_list* bl;
  15577. unit_id = script_getnum(st,2);
  15578. bl = map_id2bl(unit_id);
  15579. if (bl != NULL) {
  15580. unit_stop_attack(bl);
  15581. if (bl->type == BL_MOB)
  15582. ((TBL_MOB*)bl)->target_id = 0;
  15583. }
  15584. return SCRIPT_CMD_SUCCESS;
  15585. }
  15586. /// Makes the unit stop walking.
  15587. ///
  15588. /// unitstopwalk <unit_id>;
  15589. BUILDIN_FUNC(unitstopwalk)
  15590. {
  15591. int unit_id;
  15592. struct block_list* bl;
  15593. unit_id = script_getnum(st,2);
  15594. bl = map_id2bl(unit_id);
  15595. if (bl != NULL)
  15596. unit_stop_walking(bl, 0);
  15597. return SCRIPT_CMD_SUCCESS;
  15598. }
  15599. /// Makes the unit say the given message.
  15600. ///
  15601. /// unittalk <unit_id>,"<message>";
  15602. BUILDIN_FUNC(unittalk)
  15603. {
  15604. int unit_id;
  15605. const char* message;
  15606. struct block_list* bl;
  15607. unit_id = script_getnum(st,2);
  15608. message = script_getstr(st, 3);
  15609. bl = map_id2bl(unit_id);
  15610. if (bl != NULL) {
  15611. struct StringBuf sbuf;
  15612. StringBuf_Init(&sbuf);
  15613. StringBuf_Printf(&sbuf, "%s : %s", status_get_name(bl), message);
  15614. clif_disp_overhead(bl, StringBuf_Value(&sbuf));
  15615. StringBuf_Destroy(&sbuf);
  15616. }
  15617. return SCRIPT_CMD_SUCCESS;
  15618. }
  15619. /// Makes the unit do an emotion.
  15620. ///
  15621. /// unitemote <unit_id>,<emotion>;
  15622. ///
  15623. /// @see e_* in db/const.txt
  15624. BUILDIN_FUNC(unitemote)
  15625. {
  15626. int unit_id;
  15627. int emotion;
  15628. struct block_list* bl;
  15629. unit_id = script_getnum(st,2);
  15630. emotion = script_getnum(st,3);
  15631. bl = map_id2bl(unit_id);
  15632. if (bl != NULL)
  15633. clif_emotion(bl, emotion);
  15634. return SCRIPT_CMD_SUCCESS;
  15635. }
  15636. /// Makes the unit cast the skill on the target or self if no target is specified.
  15637. ///
  15638. /// unitskilluseid <unit_id>,<skill_id>,<skill_lv>{,<target_id>,<casttime>};
  15639. /// unitskilluseid <unit_id>,"<skill name>",<skill_lv>{,<target_id>,<casttime>};
  15640. BUILDIN_FUNC(unitskilluseid)
  15641. {
  15642. int unit_id, target_id, casttime;
  15643. uint16 skill_id, skill_lv;
  15644. struct block_list* bl;
  15645. struct script_data *data;
  15646. unit_id = script_getnum(st,2);
  15647. data = script_getdata(st, 3);
  15648. get_val(st, data); // Convert into value in case of a variable
  15649. skill_id = ( data_isstring(data) ? skill_name2id(script_getstr(st,3)) : script_getnum(st,3) );
  15650. skill_lv = script_getnum(st,4);
  15651. target_id = ( script_hasdata(st,5) ? script_getnum(st,5) : unit_id );
  15652. casttime = ( script_hasdata(st,6) ? script_getnum(st,6) : 0 );
  15653. bl = map_id2bl(unit_id);
  15654. if (bl != NULL) {
  15655. if (bl->type == BL_NPC) {
  15656. if (!((TBL_NPC*)bl)->status.hp)
  15657. status_calc_npc(((TBL_NPC*)bl), SCO_FIRST);
  15658. else
  15659. status_calc_npc(((TBL_NPC*)bl), SCO_NONE);
  15660. }
  15661. unit_skilluse_id2(bl, target_id, skill_id, skill_lv, (casttime * 1000) + skill_castfix(bl, skill_id, skill_lv), skill_get_castcancel(skill_id));
  15662. }
  15663. return SCRIPT_CMD_SUCCESS;
  15664. }
  15665. /// Makes the unit cast the skill on the target position.
  15666. ///
  15667. /// unitskillusepos <unit_id>,<skill_id>,<skill_lv>,<target_x>,<target_y>{,<casttime>};
  15668. /// unitskillusepos <unit_id>,"<skill name>",<skill_lv>,<target_x>,<target_y>{,<casttime>};
  15669. BUILDIN_FUNC(unitskillusepos)
  15670. {
  15671. int unit_id, skill_x, skill_y, casttime;
  15672. uint16 skill_id, skill_lv;
  15673. struct block_list* bl;
  15674. struct script_data *data;
  15675. unit_id = script_getnum(st,2);
  15676. data = script_getdata(st, 3);
  15677. get_val(st, data); // Convert into value in case of a variable
  15678. skill_id = ( data_isstring(data) ? skill_name2id(script_getstr(st,3)) : script_getnum(st,3) );
  15679. skill_lv = script_getnum(st,4);
  15680. skill_x = script_getnum(st,5);
  15681. skill_y = script_getnum(st,6);
  15682. casttime = ( script_hasdata(st,7) ? script_getnum(st,7) : 0 );
  15683. bl = map_id2bl(unit_id);
  15684. if (bl != NULL) {
  15685. if (bl->type == BL_NPC) {
  15686. if (!((TBL_NPC*)bl)->status.hp)
  15687. status_calc_npc(((TBL_NPC*)bl), SCO_FIRST);
  15688. else
  15689. status_calc_npc(((TBL_NPC*)bl), SCO_NONE);
  15690. }
  15691. 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));
  15692. }
  15693. return SCRIPT_CMD_SUCCESS;
  15694. }
  15695. // <--- [zBuffer] List of unit control commands
  15696. /// Pauses the execution of the script, detaching the player
  15697. ///
  15698. /// sleep <mili seconds>;
  15699. BUILDIN_FUNC(sleep)
  15700. {
  15701. int ticks;
  15702. ticks = script_getnum(st,2);
  15703. // detach the player
  15704. script_detach_rid(st);
  15705. if( ticks <= 0 )
  15706. {// do nothing
  15707. }
  15708. else if( st->sleep.tick == 0 )
  15709. {// sleep for the target amount of time
  15710. st->state = RERUNLINE;
  15711. st->sleep.tick = ticks;
  15712. }
  15713. else
  15714. {// sleep time is over
  15715. st->state = RUN;
  15716. st->sleep.tick = 0;
  15717. }
  15718. return SCRIPT_CMD_SUCCESS;
  15719. }
  15720. /// Pauses the execution of the script, keeping the player attached
  15721. /// Returns if a player is still attached
  15722. ///
  15723. /// sleep2(<mili secconds>) -> <bool>
  15724. BUILDIN_FUNC(sleep2)
  15725. {
  15726. int ticks;
  15727. ticks = script_getnum(st,2);
  15728. if( ticks <= 0 )
  15729. {// do nothing
  15730. script_pushint(st, (map_id2sd(st->rid)!=NULL));
  15731. }
  15732. else if( !st->sleep.tick )
  15733. {// sleep for the target amount of time
  15734. st->state = RERUNLINE;
  15735. st->sleep.tick = ticks;
  15736. }
  15737. else
  15738. {// sleep time is over
  15739. st->state = RUN;
  15740. st->sleep.tick = 0;
  15741. script_pushint(st, (map_id2sd(st->rid)!=NULL));
  15742. }
  15743. return SCRIPT_CMD_SUCCESS;
  15744. }
  15745. /// Awakes all the sleep timers of the target npc
  15746. ///
  15747. /// awake "<npc name>";
  15748. BUILDIN_FUNC(awake)
  15749. {
  15750. DBIterator *iter;
  15751. struct script_state *tst;
  15752. struct npc_data* nd;
  15753. if ((nd = npc_name2id(script_getstr(st, 2))) == NULL) {
  15754. ShowError("buildin_awake: NPC \"%s\" not found\n", script_getstr(st, 2));
  15755. return SCRIPT_CMD_FAILURE;
  15756. }
  15757. iter = db_iterator(st_db);
  15758. for (tst = dbi_first(iter); dbi_exists(iter); tst = dbi_next(iter)) {
  15759. if (tst->oid == nd->bl.id) {
  15760. TBL_PC* sd = map_id2sd(tst->rid);
  15761. if (tst->sleep.timer == INVALID_TIMER) { // already awake ???
  15762. continue;
  15763. }
  15764. if ((sd && sd->status.char_id != tst->sleep.charid) || (tst->rid && !sd)) {
  15765. // char not online anymore / another char of the same account is online - Cancel execution
  15766. tst->state = END;
  15767. tst->rid = 0;
  15768. }
  15769. delete_timer(tst->sleep.timer, run_script_timer);
  15770. tst->sleep.timer = INVALID_TIMER;
  15771. if (tst->state != RERUNLINE)
  15772. tst->sleep.tick = 0;
  15773. run_script_main(tst);
  15774. }
  15775. }
  15776. dbi_destroy(iter);
  15777. return SCRIPT_CMD_SUCCESS;
  15778. }
  15779. /// Returns a reference to a variable of the target NPC.
  15780. /// Returns 0 if an error occurs.
  15781. ///
  15782. /// getvariableofnpc(<variable>, "<npc name>") -> <reference>
  15783. BUILDIN_FUNC(getvariableofnpc)
  15784. {
  15785. struct script_data* data;
  15786. const char* name;
  15787. struct npc_data* nd;
  15788. data = script_getdata(st,2);
  15789. if( !data_isreference(data) )
  15790. {// Not a reference (aka varaible name)
  15791. ShowError("buildin_getvariableofnpc: not a variable\n");
  15792. script_reportdata(data);
  15793. script_pushnil(st);
  15794. st->state = END;
  15795. return SCRIPT_CMD_FAILURE;
  15796. }
  15797. name = reference_getname(data);
  15798. if( *name != '.' || name[1] == '@' )
  15799. {// not a npc variable
  15800. ShowError("buildin_getvariableofnpc: invalid scope (not npc variable)\n");
  15801. script_reportdata(data);
  15802. script_pushnil(st);
  15803. st->state = END;
  15804. return SCRIPT_CMD_FAILURE;
  15805. }
  15806. nd = npc_name2id(script_getstr(st,3));
  15807. if( nd == NULL || nd->subtype != NPCTYPE_SCRIPT || nd->u.scr.script == NULL )
  15808. {// NPC not found or has no script
  15809. ShowError("buildin_getvariableofnpc: can't find npc %s\n", script_getstr(st,3));
  15810. script_pushnil(st);
  15811. st->state = END;
  15812. return SCRIPT_CMD_FAILURE;
  15813. }
  15814. if (!nd->u.scr.script->local.vars)
  15815. nd->u.scr.script->local.vars = i64db_alloc(DB_OPT_RELEASE_DATA);
  15816. push_val2(st->stack, C_NAME, reference_getuid(data), &nd->u.scr.script->local);
  15817. return SCRIPT_CMD_SUCCESS;
  15818. }
  15819. /// Opens a warp portal.
  15820. /// Has no "portal opening" effect/sound, it opens the portal immediately.
  15821. ///
  15822. /// warpportal <source x>,<source y>,"<target map>",<target x>,<target y>;
  15823. ///
  15824. /// @author blackhole89
  15825. BUILDIN_FUNC(warpportal)
  15826. {
  15827. int spx;
  15828. int spy;
  15829. unsigned short mapindex;
  15830. int tpx;
  15831. int tpy;
  15832. struct skill_unit_group* group;
  15833. struct block_list* bl;
  15834. bl = map_id2bl(st->oid);
  15835. if( bl == NULL ) {
  15836. ShowError("buildin_warpportal: NPC is needed\n");
  15837. return SCRIPT_CMD_FAILURE;
  15838. }
  15839. spx = script_getnum(st,2);
  15840. spy = script_getnum(st,3);
  15841. mapindex = mapindex_name2id(script_getstr(st, 4));
  15842. tpx = script_getnum(st,5);
  15843. tpy = script_getnum(st,6);
  15844. if( mapindex == 0 ) {
  15845. ShowError("buildin_warpportal: Target map not found %s.\n", script_getstr(st, 4));
  15846. return SCRIPT_CMD_FAILURE;
  15847. }
  15848. group = skill_unitsetting(bl, AL_WARP, 4, spx, spy, 0);
  15849. if( group == NULL )
  15850. return SCRIPT_CMD_FAILURE;// failed
  15851. group->val1 = (group->val1<<16)|(short)0;
  15852. group->val2 = (tpx<<16) | tpy;
  15853. group->val3 = mapindex;
  15854. return SCRIPT_CMD_SUCCESS;
  15855. }
  15856. /**
  15857. * openmail({<char_id>});
  15858. **/
  15859. BUILDIN_FUNC(openmail)
  15860. {
  15861. TBL_PC* sd;
  15862. if (!script_charid2sd(2,sd))
  15863. return SCRIPT_CMD_FAILURE;
  15864. mail_openmail(sd);
  15865. return SCRIPT_CMD_SUCCESS;
  15866. }
  15867. /**
  15868. * openauction({<char_id>});
  15869. **/
  15870. BUILDIN_FUNC(openauction)
  15871. {
  15872. TBL_PC* sd;
  15873. if (!script_charid2sd(2,sd))
  15874. return SCRIPT_CMD_FAILURE;
  15875. if( !battle_config.feature_auction ) {
  15876. clif_colormes(sd->fd, color_table[COLOR_RED], msg_txt(sd, 517));
  15877. return SCRIPT_CMD_SUCCESS;
  15878. }
  15879. clif_Auction_openwindow(sd);
  15880. return SCRIPT_CMD_SUCCESS;
  15881. }
  15882. /// Retrieves the value of the specified flag of the specified cell.
  15883. ///
  15884. /// checkcell("<map name>",<x>,<y>,<type>) -> <bool>
  15885. ///
  15886. /// @see cell_chk* constants in const.txt for the types
  15887. BUILDIN_FUNC(checkcell)
  15888. {
  15889. int16 m = map_mapname2mapid(script_getstr(st,2));
  15890. int16 x = script_getnum(st,3);
  15891. int16 y = script_getnum(st,4);
  15892. cell_chk type = (cell_chk)script_getnum(st,5);
  15893. script_pushint(st, map_getcell(m, x, y, type));
  15894. return SCRIPT_CMD_SUCCESS;
  15895. }
  15896. /// Modifies flags of cells in the specified area.
  15897. ///
  15898. /// setcell "<map name>",<x1>,<y1>,<x2>,<y2>,<type>,<flag>;
  15899. ///
  15900. /// @see cell_* constants in const.txt for the types
  15901. BUILDIN_FUNC(setcell)
  15902. {
  15903. int16 m = map_mapname2mapid(script_getstr(st,2));
  15904. int16 x1 = script_getnum(st,3);
  15905. int16 y1 = script_getnum(st,4);
  15906. int16 x2 = script_getnum(st,5);
  15907. int16 y2 = script_getnum(st,6);
  15908. cell_t type = (cell_t)script_getnum(st,7);
  15909. bool flag = (bool)script_getnum(st,8);
  15910. int x,y;
  15911. if( x1 > x2 ) swap(x1,x2);
  15912. if( y1 > y2 ) swap(y1,y2);
  15913. for( y = y1; y <= y2; ++y )
  15914. for( x = x1; x <= x2; ++x )
  15915. map_setcell(m, x, y, type, flag);
  15916. return SCRIPT_CMD_SUCCESS;
  15917. }
  15918. /*==========================================
  15919. * Mercenary Commands
  15920. *------------------------------------------*/
  15921. BUILDIN_FUNC(mercenary_create)
  15922. {
  15923. struct map_session_data *sd;
  15924. int class_, contract_time;
  15925. if( (sd = script_rid2sd(st)) == NULL || sd->md || sd->status.mer_id != 0 )
  15926. return SCRIPT_CMD_SUCCESS;
  15927. class_ = script_getnum(st,2);
  15928. if( !mercenary_class(class_) )
  15929. return SCRIPT_CMD_SUCCESS;
  15930. contract_time = script_getnum(st,3);
  15931. mercenary_create(sd, class_, contract_time);
  15932. return SCRIPT_CMD_SUCCESS;
  15933. }
  15934. BUILDIN_FUNC(mercenary_heal)
  15935. {
  15936. struct map_session_data *sd = script_rid2sd(st);
  15937. int hp, sp;
  15938. if( sd == NULL || sd->md == NULL )
  15939. return SCRIPT_CMD_SUCCESS;
  15940. hp = script_getnum(st,2);
  15941. sp = script_getnum(st,3);
  15942. status_heal(&sd->md->bl, hp, sp, 0);
  15943. return SCRIPT_CMD_SUCCESS;
  15944. }
  15945. BUILDIN_FUNC(mercenary_sc_start)
  15946. {
  15947. struct map_session_data *sd = script_rid2sd(st);
  15948. enum sc_type type;
  15949. int tick, val1;
  15950. if( sd == NULL || sd->md == NULL )
  15951. return SCRIPT_CMD_SUCCESS;
  15952. type = (sc_type)script_getnum(st,2);
  15953. tick = script_getnum(st,3);
  15954. val1 = script_getnum(st,4);
  15955. status_change_start(NULL, &sd->md->bl, type, 10000, val1, 0, 0, 0, tick, SCSTART_NOTICKDEF);
  15956. return SCRIPT_CMD_SUCCESS;
  15957. }
  15958. BUILDIN_FUNC(mercenary_get_calls)
  15959. {
  15960. struct map_session_data *sd = script_rid2sd(st);
  15961. int guild;
  15962. if( sd == NULL )
  15963. return SCRIPT_CMD_SUCCESS;
  15964. guild = script_getnum(st,2);
  15965. switch( guild )
  15966. {
  15967. case ARCH_MERC_GUILD:
  15968. script_pushint(st,sd->status.arch_calls);
  15969. break;
  15970. case SPEAR_MERC_GUILD:
  15971. script_pushint(st,sd->status.spear_calls);
  15972. break;
  15973. case SWORD_MERC_GUILD:
  15974. script_pushint(st,sd->status.sword_calls);
  15975. break;
  15976. default:
  15977. script_pushint(st,0);
  15978. break;
  15979. }
  15980. return SCRIPT_CMD_SUCCESS;
  15981. }
  15982. BUILDIN_FUNC(mercenary_set_calls)
  15983. {
  15984. struct map_session_data *sd = script_rid2sd(st);
  15985. int guild, value, *calls;
  15986. if( sd == NULL )
  15987. return SCRIPT_CMD_SUCCESS;
  15988. guild = script_getnum(st,2);
  15989. value = script_getnum(st,3);
  15990. switch( guild )
  15991. {
  15992. case ARCH_MERC_GUILD:
  15993. calls = &sd->status.arch_calls;
  15994. break;
  15995. case SPEAR_MERC_GUILD:
  15996. calls = &sd->status.spear_calls;
  15997. break;
  15998. case SWORD_MERC_GUILD:
  15999. calls = &sd->status.sword_calls;
  16000. break;
  16001. default:
  16002. ShowError("buildin_mercenary_set_calls: Invalid guild '%d'.\n", guild);
  16003. return SCRIPT_CMD_SUCCESS; // Invalid Guild
  16004. }
  16005. *calls += value;
  16006. *calls = cap_value(*calls, 0, INT_MAX);
  16007. return SCRIPT_CMD_SUCCESS;
  16008. }
  16009. BUILDIN_FUNC(mercenary_get_faith)
  16010. {
  16011. struct map_session_data *sd = script_rid2sd(st);
  16012. int guild;
  16013. if( sd == NULL )
  16014. return SCRIPT_CMD_SUCCESS;
  16015. guild = script_getnum(st,2);
  16016. switch( guild )
  16017. {
  16018. case ARCH_MERC_GUILD:
  16019. script_pushint(st,sd->status.arch_faith);
  16020. break;
  16021. case SPEAR_MERC_GUILD:
  16022. script_pushint(st,sd->status.spear_faith);
  16023. break;
  16024. case SWORD_MERC_GUILD:
  16025. script_pushint(st,sd->status.sword_faith);
  16026. break;
  16027. default:
  16028. script_pushint(st,0);
  16029. break;
  16030. }
  16031. return SCRIPT_CMD_SUCCESS;
  16032. }
  16033. BUILDIN_FUNC(mercenary_set_faith)
  16034. {
  16035. struct map_session_data *sd = script_rid2sd(st);
  16036. int guild, value, *calls;
  16037. if( sd == NULL )
  16038. return SCRIPT_CMD_SUCCESS;
  16039. guild = script_getnum(st,2);
  16040. value = script_getnum(st,3);
  16041. switch( guild )
  16042. {
  16043. case ARCH_MERC_GUILD:
  16044. calls = &sd->status.arch_faith;
  16045. break;
  16046. case SPEAR_MERC_GUILD:
  16047. calls = &sd->status.spear_faith;
  16048. break;
  16049. case SWORD_MERC_GUILD:
  16050. calls = &sd->status.sword_faith;
  16051. break;
  16052. default:
  16053. ShowError("buildin_mercenary_set_faith: Invalid guild '%d'.\n", guild);
  16054. return SCRIPT_CMD_SUCCESS; // Invalid Guild
  16055. }
  16056. *calls += value;
  16057. *calls = cap_value(*calls, 0, INT_MAX);
  16058. if( mercenary_get_guild(sd->md) == guild )
  16059. clif_mercenary_updatestatus(sd,SP_MERCFAITH);
  16060. return SCRIPT_CMD_SUCCESS;
  16061. }
  16062. /*------------------------------------------
  16063. * Book Reading
  16064. *------------------------------------------*/
  16065. BUILDIN_FUNC(readbook)
  16066. {
  16067. struct map_session_data *sd;
  16068. int book_id, page;
  16069. if( (sd = script_rid2sd(st)) == NULL )
  16070. return SCRIPT_CMD_SUCCESS;
  16071. book_id = script_getnum(st,2);
  16072. page = script_getnum(st,3);
  16073. clif_readbook(sd->fd, book_id, page);
  16074. return SCRIPT_CMD_SUCCESS;
  16075. }
  16076. /******************
  16077. Questlog script commands
  16078. *******************/
  16079. /**
  16080. * Add job criteria to questinfo
  16081. * @param qi Quest Info
  16082. * @param job
  16083. * @author [Cydh]
  16084. **/
  16085. static void buildin_questinfo_setjob(struct questinfo *qi, int job) {
  16086. RECREATE(qi->jobid, unsigned short, qi->jobid_count+1);
  16087. qi->jobid[qi->jobid_count++] = job;
  16088. }
  16089. /**
  16090. * questinfo <Quest ID>,<Icon>{,<Map Mark Color>{,<Job Class>}};
  16091. **/
  16092. BUILDIN_FUNC(questinfo)
  16093. {
  16094. TBL_NPC* nd = map_id2nd(st->oid);
  16095. int quest_id, icon;
  16096. struct questinfo qi, *q2;
  16097. if( nd == NULL || nd->bl.m == -1 ) {
  16098. ShowError("buildin_questinfo: No NPC attached.\n");
  16099. return SCRIPT_CMD_FAILURE;
  16100. }
  16101. quest_id = script_getnum(st, 2);
  16102. icon = script_getnum(st, 3);
  16103. #if PACKETVER >= 20120410
  16104. switch(icon){
  16105. case QTYPE_QUEST:
  16106. case QTYPE_QUEST2:
  16107. case QTYPE_JOB:
  16108. case QTYPE_JOB2:
  16109. case QTYPE_EVENT:
  16110. case QTYPE_EVENT2:
  16111. case QTYPE_WARG:
  16112. case QTYPE_WARG2:
  16113. // Leave everything as it is
  16114. break;
  16115. case QTYPE_NONE:
  16116. default:
  16117. // Default to nothing if icon id is invalid.
  16118. icon = QTYPE_NONE;
  16119. break;
  16120. }
  16121. #else
  16122. if(icon < QTYPE_QUEST || icon > 7) // TODO: check why 7 and not QTYPE_WARG, might be related to icon + 1 below
  16123. icon = QTYPE_QUEST;
  16124. else
  16125. icon = icon + 1;
  16126. #endif
  16127. qi.quest_id = quest_id;
  16128. qi.icon = (unsigned char)icon;
  16129. qi.nd = nd;
  16130. if( script_hasdata(st, 4) ) {
  16131. int color = script_getnum(st, 4);
  16132. if( color < 0 || color > 3 ) {
  16133. ShowWarning("buildin_questinfo: invalid color '%d', changing to 0\n",color);
  16134. script_reportfunc(st);
  16135. color = 0;
  16136. }
  16137. qi.color = (unsigned char)color;
  16138. }
  16139. qi.min_level = 1;
  16140. qi.max_level = MAX_LEVEL;
  16141. q2 = map_add_questinfo(nd->bl.m, &qi);
  16142. q2->req = NULL;
  16143. q2->req_count = 0;
  16144. q2->jobid = NULL;
  16145. q2->jobid_count = 0;
  16146. if(script_hasdata(st, 5)) {
  16147. int job = script_getnum(st, 5);
  16148. if (!pcdb_checkid(job))
  16149. ShowError("buildin_questinfo: Nonexistant Job Class.\n");
  16150. else {
  16151. buildin_questinfo_setjob(q2, job);
  16152. }
  16153. }
  16154. return SCRIPT_CMD_SUCCESS;
  16155. }
  16156. /**
  16157. * setquest <ID>{,<char_id>};
  16158. **/
  16159. BUILDIN_FUNC(setquest)
  16160. {
  16161. struct map_session_data *sd;
  16162. int quest_id;
  16163. quest_id = script_getnum(st, 2);
  16164. if (!script_charid2sd(3,sd))
  16165. return SCRIPT_CMD_FAILURE;
  16166. if( quest_add(sd, quest_id) == -1 ){
  16167. script_reportsrc(st);
  16168. script_reportfunc(st);
  16169. }
  16170. //20120410 or 20090218 ? no reason that shouldn't work for 2009
  16171. pc_show_questinfo(sd);
  16172. return SCRIPT_CMD_SUCCESS;
  16173. }
  16174. /**
  16175. * erasequest <ID>{,<char_id>};
  16176. **/
  16177. BUILDIN_FUNC(erasequest)
  16178. {
  16179. struct map_session_data *sd;
  16180. if (!script_charid2sd(3,sd))
  16181. return SCRIPT_CMD_FAILURE;
  16182. if( quest_delete(sd, script_getnum(st, 2)) == -1 ){
  16183. script_reportsrc(st);
  16184. script_reportfunc(st);
  16185. }
  16186. return SCRIPT_CMD_SUCCESS;
  16187. }
  16188. /**
  16189. * completequest <ID>{,<char_id>};
  16190. **/
  16191. BUILDIN_FUNC(completequest)
  16192. {
  16193. struct map_session_data *sd;
  16194. if (!script_charid2sd(3,sd))
  16195. return SCRIPT_CMD_FAILURE;
  16196. quest_update_status(sd, script_getnum(st, 2), Q_COMPLETE);
  16197. //20120410 or 20090218
  16198. pc_show_questinfo(sd);
  16199. return SCRIPT_CMD_SUCCESS;
  16200. }
  16201. /**
  16202. * changequest <ID>,<ID2>{,<char_id>};
  16203. **/
  16204. BUILDIN_FUNC(changequest)
  16205. {
  16206. struct map_session_data *sd;
  16207. if (!script_charid2sd(4,sd))
  16208. return SCRIPT_CMD_FAILURE;
  16209. if( quest_change(sd, script_getnum(st, 2),script_getnum(st, 3)) == -1 ){
  16210. script_reportsrc(st);
  16211. script_reportfunc(st);
  16212. }
  16213. //20120410 or 20090218
  16214. pc_show_questinfo(sd);
  16215. return SCRIPT_CMD_SUCCESS;
  16216. }
  16217. /**
  16218. * checkquest(<ID>{,PLAYTIME|HUNTING{,<char_id>}})
  16219. **/
  16220. BUILDIN_FUNC(checkquest)
  16221. {
  16222. struct map_session_data *sd;
  16223. enum quest_check_type type = HAVEQUEST;
  16224. if( script_hasdata(st, 3) )
  16225. type = (enum quest_check_type)script_getnum(st, 3);
  16226. if (!script_charid2sd(4,sd))
  16227. return SCRIPT_CMD_FAILURE;
  16228. script_pushint(st, quest_check(sd, script_getnum(st, 2), type));
  16229. return SCRIPT_CMD_SUCCESS;
  16230. }
  16231. /**
  16232. * isbegin_quest(<ID>{,<char_id>})
  16233. **/
  16234. BUILDIN_FUNC(isbegin_quest)
  16235. {
  16236. struct map_session_data *sd;
  16237. int i;
  16238. if (!script_charid2sd(3,sd))
  16239. return SCRIPT_CMD_FAILURE;
  16240. i = quest_check(sd, script_getnum(st, 2), (enum quest_check_type) HAVEQUEST);
  16241. script_pushint(st, i + (i < 1));
  16242. return SCRIPT_CMD_SUCCESS;
  16243. }
  16244. /**
  16245. * showevent <icon>{,<mark color>{,<char_id>}}
  16246. **/
  16247. BUILDIN_FUNC(showevent)
  16248. {
  16249. TBL_PC *sd;
  16250. struct npc_data *nd = map_id2nd(st->oid);
  16251. int icon, color = 0;
  16252. if (!script_charid2sd(4,sd))
  16253. return SCRIPT_CMD_FAILURE;
  16254. if( sd == NULL || nd == NULL )
  16255. return SCRIPT_CMD_SUCCESS;
  16256. icon = script_getnum(st, 2);
  16257. if( script_hasdata(st, 3) ) {
  16258. color = script_getnum(st, 3);
  16259. if( color < 0 || color > 3 ) {
  16260. ShowWarning("buildin_showevent: invalid color '%d', changing to 0\n",color);
  16261. script_reportfunc(st);
  16262. color = 0;
  16263. }
  16264. }
  16265. #if PACKETVER >= 20120410
  16266. if(icon < 0 || (icon > 8 && icon != 9999) || icon == 7)
  16267. icon = 9999; // Default to nothing if icon id is invalid.
  16268. #else
  16269. if(icon < 0 || icon > 7)
  16270. icon = 0;
  16271. else
  16272. icon = icon + 1;
  16273. #endif
  16274. clif_quest_show_event(sd, &nd->bl, icon, color);
  16275. return SCRIPT_CMD_SUCCESS;
  16276. }
  16277. /*==========================================
  16278. * BattleGround System
  16279. *------------------------------------------*/
  16280. BUILDIN_FUNC(waitingroom2bg)
  16281. {
  16282. struct npc_data *nd;
  16283. struct chat_data *cd;
  16284. const char *map_name, *ev = "", *dev = "";
  16285. int x, y, mapindex = 0, bg_id;
  16286. unsigned char i,c=0;
  16287. if( script_hasdata(st,7) )
  16288. nd = npc_name2id(script_getstr(st,7));
  16289. else
  16290. nd = (struct npc_data *)map_id2bl(st->oid);
  16291. if( nd == NULL || (cd = (struct chat_data *)map_id2bl(nd->chat_id)) == NULL )
  16292. {
  16293. script_pushint(st,0);
  16294. return SCRIPT_CMD_SUCCESS;
  16295. }
  16296. map_name = script_getstr(st,2);
  16297. if( strcmp(map_name,"-") != 0 )
  16298. {
  16299. mapindex = mapindex_name2id(map_name);
  16300. if( mapindex == 0 )
  16301. { // Invalid Map
  16302. script_pushint(st,0);
  16303. return SCRIPT_CMD_SUCCESS;
  16304. }
  16305. }
  16306. x = script_getnum(st,3);
  16307. y = script_getnum(st,4);
  16308. ev = script_getstr(st,5); // Logout Event
  16309. dev = script_getstr(st,6); // Die Event
  16310. if( (bg_id = bg_create(mapindex, x, y, ev, dev)) == 0 )
  16311. { // Creation failed
  16312. script_pushint(st,0);
  16313. return SCRIPT_CMD_SUCCESS;
  16314. }
  16315. for (i = 0; i < cd->users; i++) { // Only add those who are in the chat room
  16316. struct map_session_data *sd;
  16317. if( (sd = cd->usersd[i]) != NULL && bg_team_join(bg_id, sd) ){
  16318. mapreg_setreg(reference_uid(add_str("$@arenamembers"), c), sd->bl.id);
  16319. ++c;
  16320. }
  16321. }
  16322. mapreg_setreg(add_str("$@arenamembersnum"), c);
  16323. script_pushint(st,bg_id);
  16324. return SCRIPT_CMD_SUCCESS;
  16325. }
  16326. BUILDIN_FUNC(waitingroom2bg_single)
  16327. {
  16328. const char* map_name;
  16329. struct npc_data *nd;
  16330. struct chat_data *cd;
  16331. struct map_session_data *sd;
  16332. int x, y, mapindex, bg_id;
  16333. bg_id = script_getnum(st,2);
  16334. map_name = script_getstr(st,3);
  16335. if( (mapindex = mapindex_name2id(map_name)) == 0 )
  16336. return SCRIPT_CMD_SUCCESS; // Invalid Map
  16337. x = script_getnum(st,4);
  16338. y = script_getnum(st,5);
  16339. nd = npc_name2id(script_getstr(st,6));
  16340. if( nd == NULL || (cd = (struct chat_data *)map_id2bl(nd->chat_id)) == NULL || cd->users <= 0 )
  16341. return SCRIPT_CMD_SUCCESS;
  16342. if( (sd = cd->usersd[0]) == NULL )
  16343. return SCRIPT_CMD_SUCCESS;
  16344. if( bg_team_join(bg_id, sd) )
  16345. {
  16346. pc_setpos(sd, mapindex, x, y, CLR_TELEPORT);
  16347. script_pushint(st,1);
  16348. }
  16349. else
  16350. script_pushint(st,0);
  16351. return SCRIPT_CMD_SUCCESS;
  16352. }
  16353. BUILDIN_FUNC(bg_team_setxy)
  16354. {
  16355. struct battleground_data *bg;
  16356. int bg_id;
  16357. bg_id = script_getnum(st,2);
  16358. if( (bg = bg_team_search(bg_id)) == NULL )
  16359. return SCRIPT_CMD_SUCCESS;
  16360. bg->x = script_getnum(st,3);
  16361. bg->y = script_getnum(st,4);
  16362. return SCRIPT_CMD_SUCCESS;
  16363. }
  16364. BUILDIN_FUNC(bg_warp)
  16365. {
  16366. int x, y, mapindex, bg_id;
  16367. const char* map_name;
  16368. bg_id = script_getnum(st,2);
  16369. map_name = script_getstr(st,3);
  16370. if( (mapindex = mapindex_name2id(map_name)) == 0 )
  16371. return SCRIPT_CMD_SUCCESS; // Invalid Map
  16372. x = script_getnum(st,4);
  16373. y = script_getnum(st,5);
  16374. bg_team_warp(bg_id, mapindex, x, y);
  16375. return SCRIPT_CMD_SUCCESS;
  16376. }
  16377. BUILDIN_FUNC(bg_monster)
  16378. {
  16379. int class_ = 0, x = 0, y = 0, bg_id = 0;
  16380. const char *str,*mapname, *evt="";
  16381. bg_id = script_getnum(st,2);
  16382. mapname = script_getstr(st,3);
  16383. x = script_getnum(st,4);
  16384. y = script_getnum(st,5);
  16385. str = script_getstr(st,6);
  16386. class_ = script_getnum(st,7);
  16387. if( script_hasdata(st,8) ) evt = script_getstr(st,8);
  16388. check_event(st, evt);
  16389. script_pushint(st, mob_spawn_bg(mapname,x,y,str,class_,evt,bg_id));
  16390. return SCRIPT_CMD_SUCCESS;
  16391. }
  16392. BUILDIN_FUNC(bg_monster_set_team)
  16393. {
  16394. struct mob_data *md;
  16395. struct block_list *mbl;
  16396. int id = script_getnum(st,2),
  16397. bg_id = script_getnum(st,3);
  16398. if( id == 0 || (mbl = map_id2bl(id)) == NULL || mbl->type != BL_MOB )
  16399. return SCRIPT_CMD_SUCCESS;
  16400. md = (TBL_MOB *)mbl;
  16401. md->bg_id = bg_id;
  16402. mob_stop_attack(md);
  16403. mob_stop_walking(md, 0);
  16404. md->target_id = md->attacked_id = 0;
  16405. clif_charnameack(0, &md->bl);
  16406. return SCRIPT_CMD_SUCCESS;
  16407. }
  16408. BUILDIN_FUNC(bg_leave)
  16409. {
  16410. struct map_session_data *sd = NULL;
  16411. if( !script_charid2sd(2,sd) || !sd->bg_id )
  16412. return SCRIPT_CMD_SUCCESS;
  16413. bg_team_leave(sd,0);
  16414. return SCRIPT_CMD_SUCCESS;
  16415. }
  16416. BUILDIN_FUNC(bg_destroy)
  16417. {
  16418. int bg_id = script_getnum(st,2);
  16419. bg_team_delete(bg_id);
  16420. return SCRIPT_CMD_SUCCESS;
  16421. }
  16422. BUILDIN_FUNC(bg_getareausers)
  16423. {
  16424. const char *str;
  16425. int16 m, x0, y0, x1, y1;
  16426. int bg_id;
  16427. int i = 0, c = 0;
  16428. struct battleground_data *bg = NULL;
  16429. bg_id = script_getnum(st,2);
  16430. str = script_getstr(st,3);
  16431. if( (bg = bg_team_search(bg_id)) == NULL || (m = map_mapname2mapid(str)) < 0 )
  16432. {
  16433. script_pushint(st,0);
  16434. return SCRIPT_CMD_SUCCESS;
  16435. }
  16436. x0 = script_getnum(st,4);
  16437. y0 = script_getnum(st,5);
  16438. x1 = script_getnum(st,6);
  16439. y1 = script_getnum(st,7);
  16440. for( i = 0; i < MAX_BG_MEMBERS; i++ )
  16441. {
  16442. struct map_session_data *sd;
  16443. if( (sd = bg->members[i].sd) == NULL )
  16444. continue;
  16445. if( sd->bl.m != m || sd->bl.x < x0 || sd->bl.y < y0 || sd->bl.x > x1 || sd->bl.y > y1 )
  16446. continue;
  16447. c++;
  16448. }
  16449. script_pushint(st,c);
  16450. return SCRIPT_CMD_SUCCESS;
  16451. }
  16452. BUILDIN_FUNC(bg_updatescore)
  16453. {
  16454. const char *str;
  16455. int16 m;
  16456. str = script_getstr(st,2);
  16457. if( (m = map_mapname2mapid(str)) < 0 )
  16458. return SCRIPT_CMD_SUCCESS;
  16459. map[m].bgscore_lion = script_getnum(st,3);
  16460. map[m].bgscore_eagle = script_getnum(st,4);
  16461. clif_bg_updatescore(m);
  16462. return SCRIPT_CMD_SUCCESS;
  16463. }
  16464. BUILDIN_FUNC(bg_get_data)
  16465. {
  16466. struct battleground_data *bg;
  16467. int bg_id = script_getnum(st,2),
  16468. type = script_getnum(st,3);
  16469. if( (bg = bg_team_search(bg_id)) == NULL )
  16470. {
  16471. script_pushint(st,0);
  16472. return SCRIPT_CMD_SUCCESS;
  16473. }
  16474. switch( type )
  16475. {
  16476. case 0: script_pushint(st, bg->count); break;
  16477. default:
  16478. ShowError("script:bg_get_data: unknown data identifier %d\n", type);
  16479. break;
  16480. }
  16481. return SCRIPT_CMD_SUCCESS;
  16482. }
  16483. /*==========================================
  16484. * Instancing System
  16485. *------------------------------------------*/
  16486. //Returns an Instance ID
  16487. //Checks NPC first, then if player is attached we check party
  16488. int script_instancegetid(struct script_state* st)
  16489. {
  16490. short instance_id = 0;
  16491. struct npc_data *nd;
  16492. if( (nd = map_id2nd(st->oid)) && nd->instance_id > 0 )
  16493. instance_id = nd->instance_id;
  16494. else {
  16495. struct map_session_data *sd;
  16496. struct party_data *p;
  16497. if( (sd = script_rid2sd(st)) != NULL && sd->status.party_id && (p = party_search(sd->status.party_id)) != NULL && p->instance_id )
  16498. instance_id = p->instance_id;
  16499. }
  16500. return instance_id;
  16501. }
  16502. /*==========================================
  16503. * Creates the instance
  16504. * Returns Instance ID if created successfully
  16505. *------------------------------------------*/
  16506. BUILDIN_FUNC(instance_create)
  16507. {
  16508. struct map_session_data *sd;
  16509. if((sd = script_rid2sd(st)) == NULL)
  16510. return -1;
  16511. script_pushint(st,instance_create(sd->status.party_id, script_getstr(st, 2)));
  16512. return SCRIPT_CMD_SUCCESS;
  16513. }
  16514. /*==========================================
  16515. * Destroys an instance (unofficial)
  16516. * Officially instances are only destroyed by timeout
  16517. *
  16518. * instance_destroy {<instance_id>};
  16519. *------------------------------------------*/
  16520. BUILDIN_FUNC(instance_destroy)
  16521. {
  16522. short instance_id;
  16523. if( script_hasdata(st,2) )
  16524. instance_id = script_getnum(st,2);
  16525. else
  16526. instance_id = script_instancegetid(st);
  16527. if( instance_id <= 0 || instance_id >= MAX_MAP_PER_SERVER ) {
  16528. ShowError("script:instance_destroy: Trying to destroy invalid instance %d.\n", instance_id);
  16529. return SCRIPT_CMD_SUCCESS;
  16530. }
  16531. instance_destroy(instance_id);
  16532. return SCRIPT_CMD_SUCCESS;
  16533. }
  16534. /*==========================================
  16535. * Warps player to instance
  16536. * Results:
  16537. * 0: Success
  16538. * 1: Character not in party
  16539. * 2: Party doesn't have instance
  16540. * 3: Other errors (instance not in DB, instance doesn't match with party, etc.)
  16541. *------------------------------------------*/
  16542. BUILDIN_FUNC(instance_enter)
  16543. {
  16544. struct map_session_data *sd = NULL;
  16545. int x = script_hasdata(st,3) ? script_getnum(st, 3) : -1;
  16546. int y = script_hasdata(st,4) ? script_getnum(st, 4) : -1;
  16547. if (!script_charid2sd(5,sd))
  16548. return SCRIPT_CMD_FAILURE;
  16549. if (x != -1 && y != -1)
  16550. script_pushint(st,instance_enter_position(sd,script_getstr(st, 2),x,y));
  16551. else
  16552. script_pushint(st,instance_enter(sd,script_getstr(st, 2)));
  16553. return SCRIPT_CMD_SUCCESS;
  16554. }
  16555. /*==========================================
  16556. * Returns the name of a duplicated NPC
  16557. *
  16558. * instance_npcname <npc_name>{,<instance_id>};
  16559. * <npc_name> is the full name of an NPC.
  16560. *------------------------------------------*/
  16561. BUILDIN_FUNC(instance_npcname)
  16562. {
  16563. const char *str;
  16564. short instance_id = 0;
  16565. struct npc_data *nd;
  16566. str = script_getstr(st,2);
  16567. if( script_hasdata(st,3) )
  16568. instance_id = script_getnum(st,3);
  16569. else
  16570. instance_id = script_instancegetid(st);
  16571. if( instance_id && (nd = npc_name2id(str)) != NULL ) {
  16572. static char npcname[NAME_LENGTH];
  16573. snprintf(npcname, sizeof(npcname), "dup_%d_%d", instance_id, nd->bl.id);
  16574. script_pushconststr(st,npcname);
  16575. } else {
  16576. ShowError("script:instance_npcname: invalid instance NPC (instance_id: %d, NPC name: \"%s\".)\n", instance_id, str);
  16577. st->state = END;
  16578. return SCRIPT_CMD_FAILURE;
  16579. }
  16580. return SCRIPT_CMD_SUCCESS;
  16581. }
  16582. /*==========================================
  16583. * Returns the name of a duplicated map
  16584. *
  16585. * instance_mapname <map_name>{,<instance_id>};
  16586. *------------------------------------------*/
  16587. BUILDIN_FUNC(instance_mapname)
  16588. {
  16589. const char *str;
  16590. int16 m;
  16591. short instance_id = 0;
  16592. str = script_getstr(st,2);
  16593. if( script_hasdata(st,3) )
  16594. instance_id = script_getnum(st,3);
  16595. else
  16596. instance_id = script_instancegetid(st);
  16597. // Check that instance mapname is a valid map
  16598. if(!instance_id || (m = instance_mapname2mapid(str,instance_id)) < 0)
  16599. script_pushconststr(st, "");
  16600. else
  16601. script_pushconststr(st, map[m].name);
  16602. return SCRIPT_CMD_SUCCESS;
  16603. }
  16604. /*==========================================
  16605. * Returns an Instance ID
  16606. *------------------------------------------*/
  16607. BUILDIN_FUNC(instance_id)
  16608. {
  16609. short instance_id;
  16610. instance_id = script_instancegetid(st);
  16611. if(!instance_id) {
  16612. //ShowError("script:instance_id: No instance attached to NPC or player");
  16613. script_pushint(st, 0);
  16614. return SCRIPT_CMD_SUCCESS;
  16615. }
  16616. script_pushint(st, instance_id);
  16617. return SCRIPT_CMD_SUCCESS;
  16618. }
  16619. /*==========================================
  16620. * Warps all players inside an instance
  16621. *
  16622. * instance_warpall <map_name>,<x>,<y>{,<instance_id>};
  16623. *------------------------------------------*/
  16624. BUILDIN_FUNC(instance_warpall)
  16625. {
  16626. struct party_data *p;
  16627. int16 m, i;
  16628. short instance_id;
  16629. const char *mapn;
  16630. int x, y;
  16631. mapn = script_getstr(st,2);
  16632. x = script_getnum(st,3);
  16633. y = script_getnum(st,4);
  16634. if( script_hasdata(st,5) )
  16635. instance_id = script_getnum(st,5);
  16636. else
  16637. instance_id = script_instancegetid(st);
  16638. if( !instance_id || (m = map_mapname2mapid(mapn)) < 0 || (m = instance_mapname2mapid(map[m].name,instance_id)) < 0)
  16639. return SCRIPT_CMD_SUCCESS;
  16640. if( !(p = party_search(instance_data[instance_id].party_id)) )
  16641. return SCRIPT_CMD_SUCCESS;
  16642. for( i = 0; i < MAX_PARTY; i++ ) {
  16643. struct map_session_data *pl_sd;
  16644. 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);
  16645. }
  16646. return SCRIPT_CMD_SUCCESS;
  16647. }
  16648. /*==========================================
  16649. * Broadcasts to all maps inside an instance
  16650. *
  16651. * instance_announce <instance id>,"<text>",<flag>{,<fontColor>{,<fontType>{,<fontSize>{,<fontAlign>{,<fontY>}}}}};
  16652. * Using -1 for <instance id> will auto-detect the id.
  16653. *------------------------------------------*/
  16654. BUILDIN_FUNC(instance_announce) {
  16655. int instance_id = script_getnum(st,2);
  16656. const char *mes = script_getstr(st,3);
  16657. int flag = script_getnum(st,4);
  16658. const char *fontColor = script_hasdata(st,5) ? script_getstr(st,5) : NULL;
  16659. int fontType = script_hasdata(st,6) ? script_getnum(st,6) : FW_NORMAL; // default fontType
  16660. int fontSize = script_hasdata(st,7) ? script_getnum(st,7) : 12; // default fontSize
  16661. int fontAlign = script_hasdata(st,8) ? script_getnum(st,8) : 0; // default fontAlign
  16662. int fontY = script_hasdata(st,9) ? script_getnum(st,9) : 0; // default fontY
  16663. int i;
  16664. if( instance_id == -1 ) {
  16665. instance_id = script_instancegetid(st);
  16666. }
  16667. if( !instance_id && &instance_data[instance_id] != NULL) {
  16668. ShowError("buildin_instance_announce: Intance is not found.\n");
  16669. return SCRIPT_CMD_FAILURE;
  16670. }
  16671. for( i = 0; i < instance_data[instance_id].cnt_map; i++ )
  16672. map_foreachinmap(buildin_announce_sub, instance_data[instance_id].map[i].m, BL_PC,
  16673. mes, strlen(mes)+1, flag&BC_COLOR_MASK, fontColor, fontType, fontSize, fontAlign, fontY);
  16674. return SCRIPT_CMD_SUCCESS;
  16675. }
  16676. /*==========================================
  16677. * instance_check_party [malufett]
  16678. * Values:
  16679. * party_id : Party ID of the invoking character. [Required Parameter]
  16680. * amount : Amount of needed Partymembers for the Instance. [Optional Parameter]
  16681. * min : Minimum Level needed to join the Instance. [Optional Parameter]
  16682. * max : Maxium Level allowed to join the Instance. [Optional Parameter]
  16683. * Example: instance_check_party (getcharid(1){,amount}{,min}{,max});
  16684. * Example 2: instance_check_party (getcharid(1),1,1,99);
  16685. *------------------------------------------*/
  16686. BUILDIN_FUNC(instance_check_party)
  16687. {
  16688. int amount, min, max, i, party_id, c = 0;
  16689. struct party_data *p = NULL;
  16690. amount = script_hasdata(st,3) ? script_getnum(st,3) : 1; // Amount of needed Partymembers for the Instance.
  16691. min = script_hasdata(st,4) ? script_getnum(st,4) : 1; // Minimum Level needed to join the Instance.
  16692. max = script_hasdata(st,5) ? script_getnum(st,5) : MAX_LEVEL; // Maxium Level allowed to join the Instance.
  16693. if( min < 1 || min > MAX_LEVEL) {
  16694. ShowError("buildin_instance_check_party: Invalid min level, %d\n", min);
  16695. return SCRIPT_CMD_FAILURE;
  16696. } else if( max < 1 || max > MAX_LEVEL) {
  16697. ShowError("buildin_instance_check_party: Invalid max level, %d\n", max);
  16698. return SCRIPT_CMD_FAILURE;
  16699. }
  16700. if( script_hasdata(st,2) )
  16701. party_id = script_getnum(st,2);
  16702. else return SCRIPT_CMD_SUCCESS;
  16703. if( !(p = party_search(party_id)) ) {
  16704. script_pushint(st, 0); // Returns false if party does not exist.
  16705. return SCRIPT_CMD_SUCCESS;
  16706. }
  16707. for( i = 0; i < MAX_PARTY; i++ ) {
  16708. struct map_session_data *pl_sd;
  16709. if( (pl_sd = p->data[i].sd) )
  16710. if(map_id2bl(pl_sd->bl.id)) {
  16711. if(pl_sd->status.base_level < min) {
  16712. script_pushint(st, 0);
  16713. return SCRIPT_CMD_SUCCESS;
  16714. } else if(pl_sd->status.base_level > max) {
  16715. script_pushint(st, 0);
  16716. return SCRIPT_CMD_SUCCESS;
  16717. }
  16718. c++;
  16719. }
  16720. }
  16721. if(c < amount)
  16722. script_pushint(st, 0); // Not enough Members in the Party to join Instance.
  16723. else
  16724. script_pushint(st, 1);
  16725. return SCRIPT_CMD_SUCCESS;
  16726. }
  16727. /*==========================================
  16728. * Custom Fonts
  16729. *------------------------------------------*/
  16730. BUILDIN_FUNC(setfont)
  16731. {
  16732. struct map_session_data *sd = script_rid2sd(st);
  16733. int font = script_getnum(st,2);
  16734. if( sd == NULL )
  16735. return SCRIPT_CMD_SUCCESS;
  16736. if( sd->status.font != font )
  16737. sd->status.font = font;
  16738. else
  16739. sd->status.font = 0;
  16740. clif_font(sd);
  16741. return SCRIPT_CMD_SUCCESS;
  16742. }
  16743. static int buildin_mobuseskill_sub(struct block_list *bl,va_list ap)
  16744. {
  16745. TBL_MOB* md = (TBL_MOB*)bl;
  16746. struct block_list *tbl;
  16747. int mobid = va_arg(ap,int);
  16748. uint16 skill_id = va_arg(ap,int);
  16749. uint16 skill_lv = va_arg(ap,int);
  16750. int casttime = va_arg(ap,int);
  16751. int cancel = va_arg(ap,int);
  16752. int emotion = va_arg(ap,int);
  16753. int target = va_arg(ap,int);
  16754. if( md->mob_id != mobid )
  16755. return 0;
  16756. // 0:self, 1:target, 2:master, default:random
  16757. switch( target ) {
  16758. case 0: tbl = map_id2bl(md->bl.id); break;
  16759. case 1: tbl = map_id2bl(md->target_id); break;
  16760. case 2: tbl = map_id2bl(md->master_id); break;
  16761. default:tbl = battle_getenemy(&md->bl, DEFAULT_ENEMY_TYPE(md), skill_get_range2(&md->bl, skill_id, skill_lv, true)); break;
  16762. }
  16763. if( !tbl )
  16764. return 0;
  16765. if( md->ud.skilltimer != INVALID_TIMER ) // Cancel the casting skill.
  16766. unit_skillcastcancel(bl,0);
  16767. if( skill_get_casttype(skill_id) == CAST_GROUND )
  16768. unit_skilluse_pos2(&md->bl, tbl->x, tbl->y, skill_id, skill_lv, casttime, cancel);
  16769. else
  16770. unit_skilluse_id2(&md->bl, tbl->id, skill_id, skill_lv, casttime, cancel);
  16771. clif_emotion(&md->bl, emotion);
  16772. return 1;
  16773. }
  16774. /*==========================================
  16775. * areamobuseskill "Map Name",<x>,<y>,<range>,<Mob ID>,"Skill Name"/<Skill ID>,<Skill Lv>,<Cast Time>,<Cancelable>,<Emotion>,<Target Type>;
  16776. *------------------------------------------*/
  16777. BUILDIN_FUNC(areamobuseskill)
  16778. {
  16779. struct block_list center;
  16780. struct script_data *data;
  16781. int16 m;
  16782. int range,mobid,skill_id,skill_lv,casttime,emotion,target,cancel;
  16783. if( (m = map_mapname2mapid(script_getstr(st,2))) < 0 ) {
  16784. ShowError("areamobuseskill: invalid map name.\n");
  16785. return SCRIPT_CMD_SUCCESS;
  16786. }
  16787. center.m = m;
  16788. center.x = script_getnum(st,3);
  16789. center.y = script_getnum(st,4);
  16790. range = script_getnum(st,5);
  16791. mobid = script_getnum(st,6);
  16792. data = script_getdata(st, 7);
  16793. get_val(st, data); // Convert into value in case of a variable
  16794. skill_id = ( data_isstring(data) ? skill_name2id(script_getstr(st,7)) : script_getnum(st,7) );
  16795. if( (skill_lv = script_getnum(st,8)) > battle_config.mob_max_skilllvl )
  16796. skill_lv = battle_config.mob_max_skilllvl;
  16797. casttime = script_getnum(st,9);
  16798. cancel = script_getnum(st,10);
  16799. emotion = script_getnum(st,11);
  16800. target = script_getnum(st,12);
  16801. map_foreachinrange(buildin_mobuseskill_sub, &center, range, BL_MOB, mobid, skill_id, skill_lv, casttime, cancel, emotion, target);
  16802. return SCRIPT_CMD_SUCCESS;
  16803. }
  16804. BUILDIN_FUNC(progressbar)
  16805. {
  16806. struct map_session_data * sd = script_rid2sd(st);
  16807. const char * color;
  16808. unsigned int second;
  16809. if( !st || !sd )
  16810. return SCRIPT_CMD_SUCCESS;
  16811. st->state = STOP;
  16812. color = script_getstr(st,2);
  16813. second = script_getnum(st,3);
  16814. sd->progressbar.npc_id = st->oid;
  16815. sd->progressbar.timeout = gettick() + second*1000;
  16816. clif_progressbar(sd, strtol(color, (char **)NULL, 0), second);
  16817. return SCRIPT_CMD_SUCCESS;
  16818. }
  16819. BUILDIN_FUNC(pushpc)
  16820. {
  16821. uint8 dir;
  16822. int cells, dx, dy;
  16823. struct map_session_data* sd;
  16824. if((sd = script_rid2sd(st))==NULL)
  16825. {
  16826. return SCRIPT_CMD_SUCCESS;
  16827. }
  16828. dir = script_getnum(st,2);
  16829. cells = script_getnum(st,3);
  16830. if(dir>7)
  16831. {
  16832. ShowWarning("buildin_pushpc: Invalid direction %d specified.\n", dir);
  16833. script_reportsrc(st);
  16834. dir%= 8; // trim spin-over
  16835. }
  16836. if(!cells)
  16837. {// zero distance
  16838. return SCRIPT_CMD_SUCCESS;
  16839. }
  16840. else if(cells<0)
  16841. {// pushing backwards
  16842. dir = (dir+4)%8; // turn around
  16843. cells = -cells;
  16844. }
  16845. dx = dirx[dir];
  16846. dy = diry[dir];
  16847. unit_blown(&sd->bl, dx, dy, cells, 0);
  16848. return SCRIPT_CMD_SUCCESS;
  16849. }
  16850. /// Invokes buying store preparation window
  16851. /// buyingstore <slots>;
  16852. BUILDIN_FUNC(buyingstore)
  16853. {
  16854. struct map_session_data* sd;
  16855. if( ( sd = script_rid2sd(st) ) == NULL )
  16856. {
  16857. return SCRIPT_CMD_SUCCESS;
  16858. }
  16859. if( npc_isnear(&sd->bl) ) {
  16860. char output[150];
  16861. sprintf(output, msg_txt(sd,662), battle_config.min_npc_vendchat_distance);
  16862. clif_displaymessage(sd->fd, output);
  16863. return SCRIPT_CMD_SUCCESS;
  16864. }
  16865. buyingstore_setup(sd, script_getnum(st,2));
  16866. return SCRIPT_CMD_SUCCESS;
  16867. }
  16868. /// Invokes search store info window
  16869. /// searchstores <uses>,<effect>;
  16870. BUILDIN_FUNC(searchstores)
  16871. {
  16872. unsigned short effect;
  16873. unsigned int uses;
  16874. struct map_session_data* sd;
  16875. if( ( sd = script_rid2sd(st) ) == NULL )
  16876. {
  16877. return SCRIPT_CMD_SUCCESS;
  16878. }
  16879. uses = script_getnum(st,2);
  16880. effect = script_getnum(st,3);
  16881. if( !uses )
  16882. {
  16883. ShowError("buildin_searchstores: Amount of uses cannot be zero.\n");
  16884. return SCRIPT_CMD_FAILURE;
  16885. }
  16886. if( effect > 1 )
  16887. {
  16888. ShowError("buildin_searchstores: Invalid effect id %hu, specified.\n", effect);
  16889. return SCRIPT_CMD_FAILURE;
  16890. }
  16891. searchstore_open(sd, uses, effect);
  16892. return SCRIPT_CMD_SUCCESS;
  16893. }
  16894. /// Displays a number as large digital clock.
  16895. /// showdigit <value>[,<type>];
  16896. BUILDIN_FUNC(showdigit)
  16897. {
  16898. unsigned int type = 0;
  16899. int value;
  16900. struct map_session_data* sd;
  16901. if( ( sd = script_rid2sd(st) ) == NULL )
  16902. {
  16903. return SCRIPT_CMD_SUCCESS;
  16904. }
  16905. value = script_getnum(st,2);
  16906. if( script_hasdata(st,3) )
  16907. {
  16908. type = script_getnum(st,3);
  16909. if( type > 3 )
  16910. {
  16911. ShowError("buildin_showdigit: Invalid type %u.\n", type);
  16912. return SCRIPT_CMD_FAILURE;
  16913. }
  16914. }
  16915. clif_showdigit(sd, (unsigned char)type, value);
  16916. return SCRIPT_CMD_SUCCESS;
  16917. }
  16918. /**
  16919. * Rune Knight
  16920. * makerune <% success bonus>{,<char_id>};
  16921. **/
  16922. BUILDIN_FUNC(makerune) {
  16923. TBL_PC* sd;
  16924. if (!script_charid2sd(3,sd))
  16925. return SCRIPT_CMD_FAILURE;
  16926. clif_skill_produce_mix_list(sd,RK_RUNEMASTERY,24);
  16927. sd->itemid = script_getnum(st,2);
  16928. return SCRIPT_CMD_SUCCESS;
  16929. }
  16930. /**
  16931. * checkdragon({<char_id>}) returns 1 if mounting a dragon or 0 otherwise.
  16932. **/
  16933. BUILDIN_FUNC(checkdragon) {
  16934. TBL_PC* sd;
  16935. if (!script_charid2sd(2,sd))
  16936. return SCRIPT_CMD_FAILURE;
  16937. if( pc_isridingdragon(sd) )
  16938. script_pushint(st,1);
  16939. else
  16940. script_pushint(st,0);
  16941. return SCRIPT_CMD_SUCCESS;
  16942. }
  16943. /**
  16944. * setdragon({optional Color{,<char_id>}}) returns 1 on success or 0 otherwise
  16945. * - Toggles the dragon on a RK if he can mount;
  16946. * @param Color - when not provided uses the green dragon;
  16947. * - 1 : Green Dragon
  16948. * - 2 : Brown Dragon
  16949. * - 3 : Gray Dragon
  16950. * - 4 : Blue Dragon
  16951. * - 5 : Red Dragon
  16952. **/
  16953. BUILDIN_FUNC(setdragon) {
  16954. TBL_PC* sd;
  16955. int color = script_hasdata(st,2) ? script_getnum(st,2) : 0;
  16956. if (!script_charid2sd(3,sd))
  16957. return SCRIPT_CMD_FAILURE;
  16958. if( !pc_checkskill(sd,RK_DRAGONTRAINING) || (sd->class_&MAPID_THIRDMASK) != MAPID_RUNE_KNIGHT )
  16959. script_pushint(st,0);//Doesn't have the skill or it's not a Rune Knight
  16960. else if ( pc_isridingdragon(sd) ) {//Is mounted; release
  16961. pc_setoption(sd, sd->sc.option&~OPTION_DRAGON);
  16962. script_pushint(st,1);
  16963. } else {//Not mounted; Mount now.
  16964. unsigned int option = OPTION_DRAGON1;
  16965. if( color ) {
  16966. option = ( color == 1 ? OPTION_DRAGON1 :
  16967. color == 2 ? OPTION_DRAGON2 :
  16968. color == 3 ? OPTION_DRAGON3 :
  16969. color == 4 ? OPTION_DRAGON4 :
  16970. color == 5 ? OPTION_DRAGON5 : 0);
  16971. if( !option ) {
  16972. ShowWarning("script_setdragon: Unknown Color %d used; changing to green (1)\n",color);
  16973. option = OPTION_DRAGON1;
  16974. }
  16975. }
  16976. pc_setoption(sd, sd->sc.option|option);
  16977. script_pushint(st,1);
  16978. }
  16979. return SCRIPT_CMD_SUCCESS;
  16980. }
  16981. /**
  16982. * ismounting({<char_id>}) returns 1 if mounting a new mount or 0 otherwise
  16983. **/
  16984. BUILDIN_FUNC(ismounting) {
  16985. TBL_PC* sd;
  16986. if (!script_charid2sd(2,sd))
  16987. return SCRIPT_CMD_FAILURE;
  16988. if( &sd->sc && sd->sc.data[SC_ALL_RIDING] )
  16989. script_pushint(st,1);
  16990. else
  16991. script_pushint(st,0);
  16992. return SCRIPT_CMD_SUCCESS;
  16993. }
  16994. /**
  16995. * setmounting({<char_id>}) returns 1 on success or 0 otherwise
  16996. * - Toggles new mounts on a player when he can mount
  16997. * - Will fail if the player is mounting a non-new mount, e.g. dragon, peco, wug, etc.
  16998. * - Will unmount the player is he is already mounting
  16999. **/
  17000. BUILDIN_FUNC(setmounting) {
  17001. TBL_PC* sd;
  17002. if (!script_charid2sd(2,sd))
  17003. return SCRIPT_CMD_FAILURE;
  17004. if( &sd->sc && sd->sc.option&(OPTION_WUGRIDER|OPTION_RIDING|OPTION_DRAGON|OPTION_MADOGEAR) ) {
  17005. clif_msg(sd, NEED_REINS_OF_MOUNT);
  17006. script_pushint(st,0); //can't mount with one of these
  17007. } else {
  17008. if( &sd->sc && sd->sc.data[SC_ALL_RIDING] )
  17009. status_change_end(&sd->bl, SC_ALL_RIDING, INVALID_TIMER); //release mount
  17010. else
  17011. sc_start(NULL, &sd->bl, SC_ALL_RIDING, 10000, 1, INVALID_TIMER); //mount
  17012. script_pushint(st,1);//in both cases, return 1.
  17013. }
  17014. return SCRIPT_CMD_SUCCESS;
  17015. }
  17016. /**
  17017. * Retrieves quantity of arguments provided to callfunc/callsub.
  17018. * getargcount() -> amount of arguments received in a function
  17019. **/
  17020. BUILDIN_FUNC(getargcount) {
  17021. struct script_retinfo* ri;
  17022. if( st->stack->defsp < 1 || st->stack->stack_data[st->stack->defsp - 1].type != C_RETINFO ) {
  17023. ShowError("script:getargcount: used out of function or callsub label!\n");
  17024. st->state = END;
  17025. return SCRIPT_CMD_FAILURE;
  17026. }
  17027. ri = st->stack->stack_data[st->stack->defsp - 1].u.ri;
  17028. script_pushint(st, ri->nargs);
  17029. return SCRIPT_CMD_SUCCESS;
  17030. }
  17031. /**
  17032. * getcharip(<account ID>/<character ID>/<character name>)
  17033. **/
  17034. BUILDIN_FUNC(getcharip)
  17035. {
  17036. struct map_session_data* sd = NULL;
  17037. /* check if a character name is specified */
  17038. if( script_hasdata(st, 2) )
  17039. {
  17040. struct script_data *data;
  17041. data = script_getdata(st, 2);
  17042. get_val(st, data); // Convert into value in case of a variable
  17043. if (data_isstring(data))
  17044. sd = map_nick2sd(script_getstr(st, 2));
  17045. else if (data_isint(data) || script_getnum(st, 2))
  17046. {
  17047. int id = 0;
  17048. id = script_getnum(st, 2);
  17049. sd = (map_id2sd(id) ? map_id2sd(id) : map_charid2sd(id));
  17050. }
  17051. }
  17052. else
  17053. sd = script_rid2sd(st);
  17054. /* check for sd and IP */
  17055. if (!sd || !session[sd->fd]->client_addr)
  17056. {
  17057. script_pushconststr(st, "");
  17058. return SCRIPT_CMD_SUCCESS;
  17059. }
  17060. /* return the client ip_addr converted for output */
  17061. if (sd && sd->fd && session[sd->fd])
  17062. {
  17063. /* initiliaze */
  17064. const char *ip_addr = NULL;
  17065. uint32 ip;
  17066. /* set ip, ip_addr and convert to ip and push str */
  17067. ip = session[sd->fd]->client_addr;
  17068. ip_addr = ip2str(ip, NULL);
  17069. script_pushstrcopy(st, ip_addr);
  17070. }
  17071. return SCRIPT_CMD_SUCCESS;
  17072. }
  17073. /**
  17074. * is_function(<function name>) -> 1 if function exists, 0 otherwise
  17075. **/
  17076. BUILDIN_FUNC(is_function) {
  17077. const char* str = script_getstr(st,2);
  17078. if( strdb_exists(userfunc_db, str) )
  17079. script_pushint(st,1);
  17080. else
  17081. script_pushint(st,0);
  17082. return SCRIPT_CMD_SUCCESS;
  17083. }
  17084. /**
  17085. * get_revision() -> retrieves the current svn revision (if available)
  17086. **/
  17087. BUILDIN_FUNC(get_revision) {
  17088. const char *svn = get_svn_revision();
  17089. if ( svn[0] != UNKNOWN_VERSION )
  17090. script_pushint(st,atoi(svn));
  17091. else
  17092. script_pushint(st,-1); //unknown
  17093. return SCRIPT_CMD_SUCCESS;
  17094. }
  17095. /* get_hash() -> retrieves the current git hash (if available)*/
  17096. BUILDIN_FUNC(get_githash) {
  17097. const char* git = get_git_hash();
  17098. char buf[CHAT_SIZE_MAX];
  17099. safestrncpy(buf,git,strlen(git)+1);
  17100. if ( git[0] != UNKNOWN_VERSION )
  17101. script_pushstr(st,buf);
  17102. else
  17103. script_pushstr(st,"Unknown"); //unknown
  17104. return SCRIPT_CMD_SUCCESS;
  17105. }
  17106. /**
  17107. * freeloop(<toggle>) -> toggles this script instance's looping-check ability
  17108. **/
  17109. BUILDIN_FUNC(freeloop) {
  17110. if( script_hasdata(st,2) ) {
  17111. if( script_getnum(st,2) )
  17112. st->freeloop = 1;
  17113. else
  17114. st->freeloop = 0;
  17115. }
  17116. script_pushint(st, st->freeloop);
  17117. return SCRIPT_CMD_SUCCESS;
  17118. }
  17119. /**
  17120. * @commands (script based)
  17121. **/
  17122. BUILDIN_FUNC(bindatcmd) {
  17123. const char* atcmd;
  17124. const char* eventName;
  17125. int i, level = 0, level2 = 0;
  17126. bool create = false;
  17127. atcmd = script_getstr(st,2);
  17128. eventName = script_getstr(st,3);
  17129. if( *atcmd == atcommand_symbol || *atcmd == charcommand_symbol )
  17130. atcmd++;
  17131. if( script_hasdata(st,4) ) level = script_getnum(st,4);
  17132. if( script_hasdata(st,5) ) level2 = script_getnum(st,5);
  17133. if( atcmd_binding_count == 0 ) {
  17134. CREATE(atcmd_binding,struct atcmd_binding_data*,1);
  17135. create = true;
  17136. } else {
  17137. ARR_FIND(0, atcmd_binding_count, i, strcmp(atcmd_binding[i]->command,atcmd) == 0);
  17138. if( i < atcmd_binding_count ) {/* update existent entry */
  17139. safestrncpy(atcmd_binding[i]->npc_event, eventName, 50);
  17140. atcmd_binding[i]->level = level;
  17141. atcmd_binding[i]->level2 = level2;
  17142. } else
  17143. create = true;
  17144. }
  17145. if( create ) {
  17146. i = atcmd_binding_count;
  17147. if( atcmd_binding_count++ != 0 )
  17148. RECREATE(atcmd_binding,struct atcmd_binding_data*,atcmd_binding_count);
  17149. CREATE(atcmd_binding[i],struct atcmd_binding_data,1);
  17150. safestrncpy(atcmd_binding[i]->command, atcmd, 50);
  17151. safestrncpy(atcmd_binding[i]->npc_event, eventName, 50);
  17152. atcmd_binding[i]->level = level;
  17153. atcmd_binding[i]->level2 = level2;
  17154. }
  17155. return SCRIPT_CMD_SUCCESS;
  17156. }
  17157. BUILDIN_FUNC(unbindatcmd) {
  17158. const char* atcmd;
  17159. int i = 0;
  17160. atcmd = script_getstr(st, 2);
  17161. if( *atcmd == atcommand_symbol || *atcmd == charcommand_symbol )
  17162. atcmd++;
  17163. if( atcmd_binding_count == 0 ) {
  17164. script_pushint(st, 0);
  17165. return SCRIPT_CMD_SUCCESS;
  17166. }
  17167. ARR_FIND(0, atcmd_binding_count, i, strcmp(atcmd_binding[i]->command, atcmd) == 0);
  17168. if( i < atcmd_binding_count ) {
  17169. int cursor = 0;
  17170. aFree(atcmd_binding[i]);
  17171. atcmd_binding[i] = NULL;
  17172. /* compact the list now that we freed a slot somewhere */
  17173. for( i = 0, cursor = 0; i < atcmd_binding_count; i++ ) {
  17174. if( atcmd_binding[i] == NULL )
  17175. continue;
  17176. if( cursor != i ) {
  17177. memmove(&atcmd_binding[cursor], &atcmd_binding[i], sizeof(struct atcmd_binding_data*));
  17178. }
  17179. cursor++;
  17180. }
  17181. if( (atcmd_binding_count = cursor) == 0 )
  17182. aFree(atcmd_binding);
  17183. script_pushint(st, 1);
  17184. } else
  17185. script_pushint(st, 0);/* not found */
  17186. return SCRIPT_CMD_SUCCESS;
  17187. }
  17188. BUILDIN_FUNC(useatcmd) {
  17189. return atcommand_sub(st,3);
  17190. }
  17191. BUILDIN_FUNC(checkre)
  17192. {
  17193. int num;
  17194. num=script_getnum(st,2);
  17195. switch(num){
  17196. case 0:
  17197. #ifdef RENEWAL
  17198. script_pushint(st, 1);
  17199. #else
  17200. script_pushint(st, 0);
  17201. #endif
  17202. break;
  17203. case 1:
  17204. #ifdef RENEWAL_CAST
  17205. script_pushint(st, 1);
  17206. #else
  17207. script_pushint(st, 0);
  17208. #endif
  17209. break;
  17210. case 2:
  17211. #ifdef RENEWAL_DROP
  17212. script_pushint(st, 1);
  17213. #else
  17214. script_pushint(st, 0);
  17215. #endif
  17216. break;
  17217. case 3:
  17218. #ifdef RENEWAL_EXP
  17219. script_pushint(st, 1);
  17220. #else
  17221. script_pushint(st, 0);
  17222. #endif
  17223. break;
  17224. case 4:
  17225. #ifdef RENEWAL_LVDMG
  17226. script_pushint(st, 1);
  17227. #else
  17228. script_pushint(st, 0);
  17229. #endif
  17230. break;
  17231. case 5:
  17232. #ifdef RENEWAL_ASPD
  17233. script_pushint(st, 1);
  17234. #else
  17235. script_pushint(st, 0);
  17236. #endif
  17237. break;
  17238. default:
  17239. ShowWarning("buildin_checkre: unknown parameter.\n");
  17240. break;
  17241. }
  17242. return SCRIPT_CMD_SUCCESS;
  17243. }
  17244. /* getrandgroupitem <group_id>{,<quantity>{,<sub_group>}} */
  17245. BUILDIN_FUNC(getrandgroupitem) {
  17246. TBL_PC* sd;
  17247. int i, get_count = 0;
  17248. uint16 group = script_getnum(st,2), qty = 0;
  17249. uint8 sub_group = 1;
  17250. struct item item_tmp;
  17251. struct s_item_group_entry *entry = NULL;
  17252. if (!(sd = script_rid2sd(st)))
  17253. return SCRIPT_CMD_SUCCESS;
  17254. if (!group) {
  17255. ShowError("buildin_getrandgroupitem: Invalid group id (%d)!\n",script_getnum(st,2));
  17256. return SCRIPT_CMD_FAILURE;
  17257. }
  17258. FETCH(3, qty);
  17259. FETCH(4, sub_group);
  17260. entry = itemdb_get_randgroupitem(group,sub_group);
  17261. if (!entry)
  17262. return SCRIPT_CMD_FAILURE; //ensure valid itemid
  17263. memset(&item_tmp,0,sizeof(item_tmp));
  17264. item_tmp.nameid = entry->nameid;
  17265. item_tmp.identify = itemdb_isidentified(entry->nameid);
  17266. if (!qty)
  17267. qty = entry->amount;
  17268. //Check if it's stackable.
  17269. if (!itemdb_isstackable(entry->nameid)) {
  17270. item_tmp.amount = 1;
  17271. get_count = qty;
  17272. }
  17273. else {
  17274. item_tmp.amount = qty;
  17275. get_count = 1;
  17276. }
  17277. for (i = 0; i < get_count; i++) {
  17278. // if not pet egg
  17279. if (!pet_create_egg(sd, entry->nameid)) {
  17280. unsigned char flag = 0;
  17281. if ((flag = pc_additem(sd,&item_tmp,item_tmp.amount,LOG_TYPE_SCRIPT))) {
  17282. clif_additem(sd,0,0,flag);
  17283. if (pc_candrop(sd,&item_tmp))
  17284. map_addflooritem(&item_tmp,item_tmp.amount,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0,0);
  17285. }
  17286. }
  17287. }
  17288. return SCRIPT_CMD_SUCCESS;
  17289. }
  17290. /* getgroupitem <group_id>{,<char_id>};
  17291. * Gives item(s) to the attached player based on item group contents
  17292. */
  17293. BUILDIN_FUNC(getgroupitem) {
  17294. TBL_PC *sd;
  17295. int group_id = script_getnum(st,2);
  17296. if (!script_charid2sd(3,sd))
  17297. return SCRIPT_CMD_SUCCESS;
  17298. if (itemdb_pc_get_itemgroup(group_id,sd)) {
  17299. ShowError("buildin_getgroupitem: Invalid group id '%d' specified.\n",group_id);
  17300. return SCRIPT_CMD_FAILURE;
  17301. }
  17302. return SCRIPT_CMD_SUCCESS;
  17303. }
  17304. /* cleanmap <map_name>;
  17305. * cleanarea <map_name>, <x0>, <y0>, <x1>, <y1>; */
  17306. static int atcommand_cleanfloor_sub(struct block_list *bl, va_list ap)
  17307. {
  17308. nullpo_ret(bl);
  17309. map_clearflooritem(bl);
  17310. return SCRIPT_CMD_SUCCESS;
  17311. }
  17312. BUILDIN_FUNC(cleanmap)
  17313. {
  17314. const char *mapname;
  17315. int16 m;
  17316. mapname = script_getstr(st, 2);
  17317. m = map_mapname2mapid(mapname);
  17318. if (!m)
  17319. return SCRIPT_CMD_FAILURE;
  17320. if ((script_lastdata(st) - 2) < 4) {
  17321. map_foreachinmap(atcommand_cleanfloor_sub, m, BL_ITEM);
  17322. } else {
  17323. int16 x0 = script_getnum(st, 3);
  17324. int16 y0 = script_getnum(st, 4);
  17325. int16 x1 = script_getnum(st, 5);
  17326. int16 y1 = script_getnum(st, 6);
  17327. if (x0 > 0 && y0 > 0 && x1 > 0 && y1 > 0) {
  17328. map_foreachinarea(atcommand_cleanfloor_sub, m, x0, y0, x1, y1, BL_ITEM);
  17329. } else {
  17330. ShowError("cleanarea: invalid coordinate defined!\n");
  17331. return SCRIPT_CMD_FAILURE;
  17332. }
  17333. }
  17334. return SCRIPT_CMD_SUCCESS;
  17335. }
  17336. /* Cast a skill on the attached player.
  17337. * npcskill <skill id>, <skill lvl>, <stat point>, <NPC level>;
  17338. * npcskill "<skill name>", <skill lvl>, <stat point>, <NPC level>; */
  17339. BUILDIN_FUNC(npcskill)
  17340. {
  17341. uint16 skill_id;
  17342. unsigned short skill_level;
  17343. unsigned int stat_point;
  17344. unsigned int npc_level;
  17345. struct npc_data *nd;
  17346. struct map_session_data *sd;
  17347. struct script_data *data;
  17348. data = script_getdata(st, 2);
  17349. get_val(st, data); // Convert into value in case of a variable
  17350. skill_id = data_isstring(data) ? skill_name2id(script_getstr(st, 2)) : script_getnum(st, 2);
  17351. skill_level = script_getnum(st, 3);
  17352. stat_point = script_getnum(st, 4);
  17353. npc_level = script_getnum(st, 5);
  17354. sd = script_rid2sd(st);
  17355. nd = (struct npc_data *)map_id2bl(sd->npc_id);
  17356. if (stat_point > battle_config.max_third_parameter) {
  17357. ShowError("npcskill: stat point exceeded maximum of %d.\n",battle_config.max_third_parameter );
  17358. return SCRIPT_CMD_FAILURE;
  17359. }
  17360. if (npc_level > MAX_LEVEL) {
  17361. ShowError("npcskill: level exceeded maximum of %d.\n", MAX_LEVEL);
  17362. return SCRIPT_CMD_FAILURE;
  17363. }
  17364. if (sd == NULL || nd == NULL) { //ain't possible, but I don't trust people.
  17365. return SCRIPT_CMD_FAILURE;
  17366. }
  17367. nd->level = npc_level;
  17368. nd->stat_point = stat_point;
  17369. if (!nd->status.hp)
  17370. status_calc_npc(nd, SCO_FIRST);
  17371. else
  17372. status_calc_npc(nd, SCO_NONE);
  17373. if (skill_get_inf(skill_id)&INF_GROUND_SKILL)
  17374. unit_skilluse_pos(&nd->bl, sd->bl.x, sd->bl.y, skill_id, skill_level);
  17375. else
  17376. unit_skilluse_id(&nd->bl, sd->bl.id, skill_id, skill_level);
  17377. return SCRIPT_CMD_SUCCESS;
  17378. }
  17379. /* Consumes an item.
  17380. * consumeitem <item id>{,<char_id>};
  17381. * consumeitem "<item name>"{,<char_id>};
  17382. * @param item: Item ID or name
  17383. */
  17384. BUILDIN_FUNC(consumeitem)
  17385. {
  17386. TBL_PC *sd;
  17387. struct script_data *data;
  17388. struct item_data *item_data;
  17389. if (!script_charid2sd(3, sd))
  17390. return SCRIPT_CMD_FAILURE;
  17391. data = script_getdata( st, 2 );
  17392. get_val( st, data );
  17393. if( data_isstring( data ) ){
  17394. const char *name = conv_str( st, data );
  17395. if( ( item_data = itemdb_searchname( name ) ) == NULL ){
  17396. ShowError( "buildin_consumeitem: Nonexistant item %s requested.\n", name );
  17397. return SCRIPT_CMD_FAILURE;
  17398. }
  17399. }else if( data_isint( data ) ){
  17400. unsigned short nameid = conv_num( st, data );
  17401. if( ( item_data = itemdb_exists( nameid ) ) == NULL ){
  17402. ShowError("buildin_consumeitem: Nonexistant item %hu requested.\n", nameid );
  17403. return SCRIPT_CMD_FAILURE;
  17404. }
  17405. }else{
  17406. ShowError("buildin_consumeitem: invalid data type for argument #1 (%d).\n", data->type );
  17407. return SCRIPT_CMD_FAILURE;
  17408. }
  17409. run_script( item_data->script, 0, sd->bl.id, 0 );
  17410. return SCRIPT_CMD_SUCCESS;
  17411. }
  17412. /** Makes a player sit/stand.
  17413. * sit {"<character name>"};
  17414. * stand {"<character name>"};
  17415. * Note: Use readparam(Sitting) which returns 1 or 0 (sitting or standing).
  17416. * @param name: Player name that will be invoked
  17417. */
  17418. BUILDIN_FUNC(sit)
  17419. {
  17420. TBL_PC *sd;
  17421. if( script_hasdata(st, 2) )
  17422. sd = map_nick2sd(script_getstr(st, 2));
  17423. else
  17424. sd = script_rid2sd(st);
  17425. if( sd == NULL)
  17426. return SCRIPT_CMD_FAILURE;
  17427. if( !pc_issit(sd) ) {
  17428. pc_setsit(sd);
  17429. skill_sit(sd, 1);
  17430. clif_sitting(&sd->bl);
  17431. }
  17432. return SCRIPT_CMD_SUCCESS;
  17433. }
  17434. /** Makes player to stand
  17435. * @param name: Player name that will be set
  17436. */
  17437. BUILDIN_FUNC(stand)
  17438. {
  17439. TBL_PC *sd;
  17440. if( script_hasdata(st, 2) )
  17441. sd = map_nick2sd(script_getstr(st, 2));
  17442. else
  17443. sd = script_rid2sd(st);
  17444. if( sd == NULL)
  17445. return SCRIPT_CMD_FAILURE;
  17446. if( pc_issit(sd) && pc_setstand(sd, false)) {
  17447. skill_sit(sd, 0);
  17448. clif_standing(&sd->bl);
  17449. }
  17450. return SCRIPT_CMD_SUCCESS;
  17451. }
  17452. /** Creates an array of bounded item IDs
  17453. * countbound {<type>{,<char_id>}};
  17454. * @param type: 0 - All bound items; 1 - Account Bound; 2 - Guild Bound; 3 - Party Bound
  17455. * @return amt: Amount of items found
  17456. */
  17457. BUILDIN_FUNC(countbound)
  17458. {
  17459. int i, type, j = 0, k = 0;
  17460. TBL_PC *sd;
  17461. if (!script_charid2sd(3,sd))
  17462. return SCRIPT_CMD_FAILURE;
  17463. type = script_getnum(st,2);
  17464. for( i = 0; i < MAX_INVENTORY; i ++ ) {
  17465. if( sd->inventory.u.items_inventory[i].nameid > 0 && (
  17466. (!type && sd->inventory.u.items_inventory[i].bound) || (type && sd->inventory.u.items_inventory[i].bound == type)
  17467. ))
  17468. {
  17469. pc_setreg(sd,reference_uid(add_str("@bound_items"), k),sd->inventory.u.items_inventory[i].nameid);
  17470. k++;
  17471. j += sd->inventory.u.items_inventory[i].amount;
  17472. }
  17473. }
  17474. script_pushint(st,j);
  17475. return SCRIPT_CMD_SUCCESS;
  17476. }
  17477. /** Creates new party
  17478. * party_create "<party name>"{,<char id>{,<item share>{,<item share type>}}};
  17479. * @param party_name: String of party name that will be created
  17480. * @param char_id: Chara that will be leader of this new party. If no char_id specified, the invoker will be party leader
  17481. * @param item_share: 0-Each Take. 1-Party Share
  17482. * @param item_share_type: 0-Each Take. 1-Even Share
  17483. * @return val: Result value
  17484. * -3 - party name is exist
  17485. * -2 - player is in party already
  17486. * -1 - player is not found
  17487. * 0 - unknown error
  17488. * 1 - success, will return party id $@party_create_id
  17489. */
  17490. BUILDIN_FUNC(party_create)
  17491. {
  17492. char party_name[NAME_LENGTH];
  17493. int item1 = 0, item2 = 0;
  17494. TBL_PC *sd;
  17495. if( (!script_hasdata(st,3) && !(sd = script_rid2sd(st))) || (script_hasdata(st,3) && !(sd = map_charid2sd(script_getnum(st,3)))) ) {
  17496. script_pushint(st,-1);
  17497. return SCRIPT_CMD_FAILURE;
  17498. }
  17499. if( sd->status.party_id ) {
  17500. script_pushint(st,-2);
  17501. return SCRIPT_CMD_FAILURE;
  17502. }
  17503. safestrncpy(party_name,script_getstr(st,2),NAME_LENGTH);
  17504. trim(party_name);
  17505. if( party_searchname(party_name) ) {
  17506. script_pushint(st,-3);
  17507. return SCRIPT_CMD_FAILURE;
  17508. }
  17509. if( script_getnum(st,4) )
  17510. item1 = 1;
  17511. if( script_getnum(st,5) )
  17512. item2 = 1;
  17513. party_create_byscript = 1;
  17514. script_pushint(st,party_create(sd,party_name,item1,item2));
  17515. return SCRIPT_CMD_SUCCESS;
  17516. }
  17517. /** Adds player to specified party
  17518. * party_addmember <party id>,<char id>;
  17519. * @param party_id: The party that will be entered by player
  17520. * @param char_id: Char id of player that will be joined to the party
  17521. * @return val: Result value
  17522. * -4 - party is full
  17523. * -3 - party is not found
  17524. * -2 - player is in party already
  17525. * -1 - player is not found
  17526. * 0 - unknown error
  17527. * 1 - success
  17528. */
  17529. BUILDIN_FUNC(party_addmember)
  17530. {
  17531. int party_id = script_getnum(st,2);
  17532. TBL_PC *sd;
  17533. struct party_data *party;
  17534. if( !(sd = map_charid2sd(script_getnum(st,3))) ) {
  17535. script_pushint(st,-1);
  17536. return SCRIPT_CMD_FAILURE;
  17537. }
  17538. if( sd->status.party_id ) {
  17539. script_pushint(st,-2);
  17540. return SCRIPT_CMD_FAILURE;
  17541. }
  17542. if( !(party = party_search(party_id)) ) {
  17543. script_pushint(st,-3);
  17544. return SCRIPT_CMD_FAILURE;
  17545. }
  17546. if( party->party.count >= MAX_PARTY ) {
  17547. script_pushint(st,-4);
  17548. return SCRIPT_CMD_FAILURE;
  17549. }
  17550. sd->party_invite = party_id;
  17551. script_pushint(st,party_add_member(party_id,sd));
  17552. return SCRIPT_CMD_SUCCESS;
  17553. }
  17554. /** Removes player from his/her party. If party_id and char_id is empty remove the invoker from his/her party
  17555. * party_delmember {<char id>,<party_id>};
  17556. * @param: char_id
  17557. * @param: party_id
  17558. * @return val: Result value
  17559. * -3 - player is not in party
  17560. * -2 - party is not found
  17561. * -1 - player is not found
  17562. * 0 - unknown error
  17563. * 1 - success
  17564. */
  17565. BUILDIN_FUNC(party_delmember)
  17566. {
  17567. TBL_PC *sd = NULL;
  17568. if( !script_hasdata(st,2) && !script_hasdata(st,3) && !(sd = script_rid2sd(st)) ) {
  17569. script_pushint(st,-1);
  17570. return SCRIPT_CMD_FAILURE;
  17571. }
  17572. if( sd || (script_getnum(st,2) && (sd = map_charid2sd(script_getnum(st,2)))) )
  17573. script_pushint(st,party_removemember2(sd,0,0));
  17574. else
  17575. script_pushint(st,party_removemember2(NULL,script_getnum(st,2),script_getnum(st,3)));
  17576. return SCRIPT_CMD_SUCCESS;
  17577. }
  17578. /** Changes party leader of specified party (even the leader is offline)
  17579. * party_changeleader <party id>,<char id>;
  17580. * @param party_id: ID of party
  17581. * @param char_id: Char ID of new leader
  17582. * @return val: Result value
  17583. * -4 - player is party leader already
  17584. * -3 - player is not in this party
  17585. * -2 - player is not found
  17586. * -1 - party is not found
  17587. * 0 - unknown error
  17588. * 1 - success */
  17589. BUILDIN_FUNC(party_changeleader)
  17590. {
  17591. int i, party_id = script_getnum(st,2);
  17592. TBL_PC *sd = NULL;
  17593. TBL_PC *tsd = NULL;
  17594. struct party_data *party = NULL;
  17595. if( !(party = party_search(party_id)) ) {
  17596. script_pushint(st,-1);
  17597. return SCRIPT_CMD_FAILURE;
  17598. }
  17599. if( !(tsd = map_charid2sd(script_getnum(st,3))) ) {
  17600. script_pushint(st,-2);
  17601. return SCRIPT_CMD_FAILURE;
  17602. }
  17603. if( tsd->status.party_id != party_id ) {
  17604. script_pushint(st,-3);
  17605. return SCRIPT_CMD_FAILURE;
  17606. }
  17607. ARR_FIND(0,MAX_PARTY,i,party->party.member[i].leader);
  17608. if( i >= MAX_PARTY ) { //this is should impossible!
  17609. script_pushint(st,0);
  17610. return SCRIPT_CMD_FAILURE;
  17611. }
  17612. if( party->data[i].sd == tsd ) {
  17613. script_pushint(st,-4);
  17614. return SCRIPT_CMD_FAILURE;
  17615. }
  17616. script_pushint(st,party_changeleader(sd,tsd,party));
  17617. return SCRIPT_CMD_SUCCESS;
  17618. }
  17619. /** Changes party option
  17620. * party_changeoption <party id>,<option>,<flag>;
  17621. * @param party_id: ID of party that will be changed
  17622. * @param option: Type of option
  17623. * @return val: -1 - party is not found, 0 - invalid option, 1 - success
  17624. */
  17625. BUILDIN_FUNC(party_changeoption)
  17626. {
  17627. struct party_data *party;
  17628. if( !(party = party_search(script_getnum(st,2))) ) {
  17629. script_pushint(st,-1);
  17630. return SCRIPT_CMD_FAILURE;
  17631. }
  17632. script_pushint(st,party_setoption(party,script_getnum(st,3),script_getnum(st,4)));
  17633. return SCRIPT_CMD_SUCCESS;
  17634. }
  17635. /** Destroys party with party id.
  17636. * party_destroy <party id>;
  17637. * @param party_id: ID of party that will be destroyed
  17638. */
  17639. BUILDIN_FUNC(party_destroy)
  17640. {
  17641. int i;
  17642. struct party_data *party;
  17643. if( !(party = party_search(script_getnum(st,2))) ) {
  17644. script_pushint(st,0);
  17645. return SCRIPT_CMD_FAILURE;
  17646. }
  17647. ARR_FIND(0,MAX_PARTY,i,party->party.member[i].leader);
  17648. if( i >= MAX_PARTY || !party->data[i].sd ) { //leader not online
  17649. int j;
  17650. for( j = 0; j < MAX_PARTY; j++ ) {
  17651. TBL_PC *sd = party->data[j].sd;
  17652. if(sd)
  17653. party_member_withdraw(party->party.party_id,sd->status.account_id,sd->status.char_id);
  17654. else if( party->party.member[j].char_id )
  17655. intif_party_leave(party->party.party_id,party->party.member[j].account_id,party->party.member[j].char_id);
  17656. }
  17657. party_broken(party->party.party_id);
  17658. script_pushint(st,1);
  17659. }
  17660. else //leader leave = party broken
  17661. script_pushint(st,party_leave(party->data[i].sd));
  17662. return SCRIPT_CMD_SUCCESS;
  17663. }
  17664. /** Checks if a player's client version meets a required version or date.
  17665. * @param type: 0 - check by version number; 1 - check by date
  17666. * @param data: Input
  17667. */
  17668. BUILDIN_FUNC(is_clientver) {
  17669. TBL_PC *sd = NULL;
  17670. int type = script_getnum(st,2);
  17671. int data = script_getnum(st,3);
  17672. int ret = 0;
  17673. if (script_hasdata(st,4))
  17674. sd = map_charid2sd(script_getnum(st,4));
  17675. else
  17676. sd = script_rid2sd(st);
  17677. if (sd == NULL) {
  17678. script_pushint(st,0);
  17679. return SCRIPT_CMD_FAILURE;
  17680. }
  17681. switch(type){
  17682. case 0:
  17683. ret = (sd->packet_ver >= data)?1:0;
  17684. break;
  17685. case 1:
  17686. ret = (sd->packet_ver >= date2version(data))?1:0;
  17687. break;
  17688. }
  17689. script_pushint(st,ret);
  17690. return SCRIPT_CMD_SUCCESS;
  17691. }
  17692. /** Returns various information about a player's VIP status. Need to enable VIP system
  17693. * vip_status <type>,{"<character name>"};
  17694. * @param type: Info type, 1: VIP status, 2: Expired date, 3: Remaining time
  17695. * @param name: Character name (Optional)
  17696. */
  17697. BUILDIN_FUNC(vip_status) {
  17698. #ifdef VIP_ENABLE
  17699. TBL_PC *sd;
  17700. char vip_str[26];
  17701. time_t now = time(NULL);
  17702. int type = script_getnum(st, 2);
  17703. if (script_hasdata(st, 3))
  17704. sd = map_nick2sd(script_getstr(st, 3));
  17705. else
  17706. sd = script_rid2sd(st);
  17707. if (sd == NULL)
  17708. return SCRIPT_CMD_FAILURE;
  17709. switch(type) {
  17710. case 1: // Get VIP status.
  17711. script_pushint(st, pc_isvip(sd));
  17712. break;
  17713. case 2: // Get VIP expire date.
  17714. if (pc_isvip(sd)) {
  17715. time_t viptime = sd->vip.time;
  17716. strftime(vip_str, 24, "%Y-%m-%d %H:%M", localtime(&viptime));
  17717. vip_str[25] = '\0';
  17718. script_pushstrcopy(st, vip_str);
  17719. } else
  17720. script_pushint(st, 0);
  17721. break;
  17722. case 3: // Get remaining time.
  17723. if (pc_isvip(sd)) {
  17724. time_t viptime_remain = sd->vip.time - now;
  17725. int year=0,month=0,day=0,hour=0,min=0,sec=0;
  17726. split_time((int)viptime_remain,&year,&month,&day,&hour,&min,&sec);
  17727. safesnprintf(vip_str,sizeof(vip_str),"%d-%d-%d %d:%d",year,month,day,hour,min);
  17728. script_pushstrcopy(st, vip_str);
  17729. } else
  17730. script_pushint(st, 0);
  17731. break;
  17732. }
  17733. #else
  17734. script_pushint(st, 0);
  17735. #endif
  17736. return SCRIPT_CMD_SUCCESS;
  17737. }
  17738. /** Adds or removes VIP time in minutes. Need to enable VIP system
  17739. * vip_time <time in mn>,{"<character name>"};
  17740. * @param time: VIP duration in minutes. If time < 0 remove time, else add time.
  17741. * @param name: Character name (optional)
  17742. */
  17743. BUILDIN_FUNC(vip_time) {
  17744. #ifdef VIP_ENABLE //would be a pain for scripting npc otherwise
  17745. TBL_PC *sd;
  17746. int viptime = script_getnum(st, 2) * 60; // Convert since it's given in minutes.
  17747. if (script_hasdata(st, 3))
  17748. sd = map_nick2sd(script_getstr(st, 3));
  17749. else
  17750. sd = script_rid2sd(st);
  17751. if (sd == NULL)
  17752. return SCRIPT_CMD_FAILURE;
  17753. chrif_req_login_operation(sd->status.account_id, sd->status.name, CHRIF_OP_LOGIN_VIP, viptime, 7, 0);
  17754. #endif
  17755. return SCRIPT_CMD_SUCCESS;
  17756. }
  17757. /** Turns a player into a monster and grants SC attribute effect. [malufett/Hercules]
  17758. * montransform <monster name/ID>, <duration>, <sc type>, <val1>, <val2>, <val3>, <val4>;
  17759. * @param monster: Monster ID or name
  17760. * @param duration: Transform duration in millisecond (ms)
  17761. * @param sc_type: Type of SC that will be affected during the transformation
  17762. * @param val1: Value for SC
  17763. * @param val2: Value for SC
  17764. * @param val3: Value for SC
  17765. * @param val4: Value for SC
  17766. */
  17767. BUILDIN_FUNC(montransform) {
  17768. TBL_PC *sd;
  17769. enum sc_type type;
  17770. int tick, mob_id, val1, val2, val3, val4;
  17771. struct script_data *data;
  17772. val1 = val2 = val3 = val4 = 0;
  17773. if( (sd = script_rid2sd(st)) == NULL )
  17774. return SCRIPT_CMD_FAILURE;
  17775. data = script_getdata(st, 2);
  17776. get_val(st, data); // Convert into value in case of a variable
  17777. if( data_isstring(data) )
  17778. mob_id = mobdb_searchname(script_getstr(st, 2));
  17779. else
  17780. mob_id = mobdb_checkid(script_getnum(st, 2));
  17781. tick = script_getnum(st, 3);
  17782. if (script_hasdata(st, 4))
  17783. type = (sc_type)script_getnum(st, 4);
  17784. else
  17785. type = SC_NONE;
  17786. if (mob_id == 0) {
  17787. if( data_isstring(data) )
  17788. ShowWarning("buildin_montransform: Attempted to use non-existing monster '%s'.\n", script_getstr(st, 2));
  17789. else
  17790. ShowWarning("buildin_montransform: Attempted to use non-existing monster of ID '%d'.\n", script_getnum(st, 2));
  17791. return SCRIPT_CMD_FAILURE;
  17792. }
  17793. if (mob_id == MOBID_EMPERIUM) {
  17794. ShowWarning("buildin_montransform: Monster 'Emperium' cannot be used.\n");
  17795. return SCRIPT_CMD_FAILURE;
  17796. }
  17797. if (!(type >= SC_NONE && type < SC_MAX)) {
  17798. ShowWarning("buildin_montransform: Unsupported status change id %d\n", type);
  17799. return SCRIPT_CMD_FAILURE;
  17800. }
  17801. if (script_hasdata(st, 5))
  17802. val1 = script_getnum(st, 5);
  17803. if (script_hasdata(st, 6))
  17804. val2 = script_getnum(st, 6);
  17805. if (script_hasdata(st, 7))
  17806. val3 = script_getnum(st, 7);
  17807. if (script_hasdata(st, 8))
  17808. val4 = script_getnum(st, 8);
  17809. if (tick != 0) {
  17810. if (battle_config.mon_trans_disable_in_gvg && map_flag_gvg2(sd->bl.m)) {
  17811. clif_displaymessage(sd->fd, msg_txt(sd,731)); // Transforming into monster is not allowed in Guild Wars.
  17812. return SCRIPT_CMD_FAILURE;
  17813. }
  17814. if (sd->disguise){
  17815. clif_displaymessage(sd->fd, msg_txt(sd,729)); // Cannot transform into monster while in disguise.
  17816. return SCRIPT_CMD_FAILURE;
  17817. }
  17818. status_change_end(&sd->bl, SC_MONSTER_TRANSFORM, INVALID_TIMER); // Clear previous
  17819. sc_start2(NULL, &sd->bl, SC_MONSTER_TRANSFORM, 100, mob_id, type, tick);
  17820. if (type != SC_NONE)
  17821. sc_start4(NULL, &sd->bl, type, 100, val1, val2, val3, val4, tick);
  17822. }
  17823. return SCRIPT_CMD_SUCCESS;
  17824. }
  17825. /**
  17826. * Attach script to player for certain duration
  17827. * bonus_script "<script code>",<duration>{,<flag>{,<type>{,<status_icon>{,<char_id>}}}};
  17828. * @param "script code"
  17829. * @param duration
  17830. * @param flag
  17831. * @param icon
  17832. * @param char_id
  17833. * @author [Cydh]
  17834. **/
  17835. BUILDIN_FUNC(bonus_script) {
  17836. uint16 flag = 0;
  17837. int16 icon = SI_BLANK;
  17838. uint32 dur;
  17839. uint8 type = 0;
  17840. TBL_PC* sd;
  17841. const char *script_str = NULL;
  17842. struct s_bonus_script_entry *entry = NULL;
  17843. if (script_hasdata(st,7)) {
  17844. if (!(sd = map_charid2sd(script_getnum(st,7)))) {
  17845. ShowError("buildin_bonus_script: Player CID=%d is not found.\n", script_getnum(st,7));
  17846. return SCRIPT_CMD_FAILURE;
  17847. }
  17848. }
  17849. else
  17850. sd = script_rid2sd(st);
  17851. if (sd == NULL)
  17852. return SCRIPT_CMD_FAILURE;
  17853. script_str = script_getstr(st,2);
  17854. dur = 1000 * abs(script_getnum(st,3));
  17855. FETCH(4, flag);
  17856. FETCH(5, type);
  17857. FETCH(6, icon);
  17858. // No Script string, No Duration!
  17859. if (script_str[0] == '\0' || !dur) {
  17860. ShowError("buildin_bonus_script: Invalid! Script: \"%s\". Duration: %d\n", script_str, dur);
  17861. return SCRIPT_CMD_FAILURE;
  17862. }
  17863. if (strlen(script_str) >= MAX_BONUS_SCRIPT_LENGTH) {
  17864. ShowError("buildin_bonus_script: Script string to long: \"%s\".\n", script_str);
  17865. return SCRIPT_CMD_FAILURE;
  17866. }
  17867. if (icon <= SI_BLANK || icon >= SI_MAX)
  17868. icon = SI_BLANK;
  17869. if ((entry = pc_bonus_script_add(sd, script_str, dur, (enum si_type)icon, flag, type))) {
  17870. linkdb_insert(&sd->bonus_script.head, (void *)((intptr_t)entry), entry);
  17871. status_calc_pc(sd,SCO_NONE);
  17872. }
  17873. return SCRIPT_CMD_SUCCESS;
  17874. }
  17875. /**
  17876. * Removes all bonus script from player
  17877. * bonus_script_clear {<flag>,{<char_id>}};
  17878. * @param flag 0 - Except permanent bonus, 1 - With permanent bonus
  17879. * @param char_id Clear script from this character
  17880. * @author [Cydh]
  17881. */
  17882. BUILDIN_FUNC(bonus_script_clear) {
  17883. TBL_PC* sd;
  17884. bool flag = false;
  17885. if (script_hasdata(st,2))
  17886. flag = script_getnum(st,2);
  17887. if (script_hasdata(st,3))
  17888. sd = map_charid2sd(script_getnum(st,3));
  17889. else
  17890. sd = script_rid2sd(st);
  17891. if (sd == NULL)
  17892. return SCRIPT_CMD_FAILURE;
  17893. pc_bonus_script_clear(sd,(flag ? BSF_PERMANENT : BSF_REM_ALL));
  17894. return SCRIPT_CMD_SUCCESS;
  17895. }
  17896. /** Allows player to use atcommand while talking with NPC
  17897. * enable_command;
  17898. * @author [Cydh], [Kichi]
  17899. */
  17900. BUILDIN_FUNC(enable_command) {
  17901. TBL_PC* sd = script_rid2sd(st);
  17902. if (!sd)
  17903. return SCRIPT_CMD_FAILURE;
  17904. sd->state.disable_atcommand_on_npc = 0;
  17905. return SCRIPT_CMD_SUCCESS;
  17906. }
  17907. /** Prevents player to use atcommand while talking with NPC
  17908. * disable_command;
  17909. * @author [Cydh], [Kichi]
  17910. */
  17911. BUILDIN_FUNC(disable_command) {
  17912. TBL_PC* sd = script_rid2sd(st);
  17913. if (!sd)
  17914. return SCRIPT_CMD_FAILURE;
  17915. sd->state.disable_atcommand_on_npc = 1;
  17916. return SCRIPT_CMD_SUCCESS;
  17917. }
  17918. /** Get the information of the members of a guild by type.
  17919. * getguildmember <guild_id>{,<type>};
  17920. * @param guild_id: ID of guild
  17921. * @param type: Type of option (optional)
  17922. */
  17923. BUILDIN_FUNC(getguildmember)
  17924. {
  17925. struct guild *g = NULL;
  17926. uint8 j = 0;
  17927. g = guild_search(script_getnum(st,2));
  17928. if (g) {
  17929. uint8 i, type = 0;
  17930. struct script_data *data = NULL;
  17931. char *varname = NULL;
  17932. if (script_hasdata(st,3))
  17933. type = script_getnum(st,3);
  17934. if (script_hasdata(st,4)) {
  17935. data = script_getdata(st, 4);
  17936. if (!data_isreference(data)) {
  17937. ShowError("buildin_getguildmember: Error in argument! Please give a variable to store values in.\n");
  17938. return SCRIPT_CMD_FAILURE;
  17939. }
  17940. varname = reference_getname(data);
  17941. if (type <= 0 && varname[strlen(varname)-1] != '$') {
  17942. ShowError("buildin_getguildmember: The array %s is not string type.\n", varname);
  17943. return SCRIPT_CMD_FAILURE;
  17944. }
  17945. }
  17946. for (i = 0; i < MAX_GUILD; i++) {
  17947. if (g->member[i].account_id) {
  17948. switch (type) {
  17949. case 2:
  17950. if (data)
  17951. setd_sub(st, NULL, varname, j, (void *)__64BPRTSIZE(g->member[i].account_id), data->ref);
  17952. else
  17953. mapreg_setreg(reference_uid(add_str("$@guildmemberaid"), j),g->member[i].account_id);
  17954. break;
  17955. case 1:
  17956. if (data)
  17957. setd_sub(st, NULL, varname, j, (void *)__64BPRTSIZE(g->member[i].char_id), data->ref);
  17958. else
  17959. mapreg_setreg(reference_uid(add_str("$@guildmembercid"), j), g->member[i].char_id);
  17960. break;
  17961. default:
  17962. if (data)
  17963. setd_sub(st, NULL, varname, j, (void *)__64BPRTSIZE(g->member[i].name), data->ref);
  17964. else
  17965. mapreg_setregstr(reference_uid(add_str("$@guildmembername$"), j), g->member[i].name);
  17966. break;
  17967. }
  17968. j++;
  17969. }
  17970. }
  17971. }
  17972. mapreg_setreg(add_str("$@guildmembercount"), j);
  17973. script_pushint(st, j);
  17974. return SCRIPT_CMD_SUCCESS;
  17975. }
  17976. /** Adds spirit ball to player for 'duration' in milisecond
  17977. * addspiritball <count>,<duration>{,<char_id>};
  17978. * @param count How many spirit ball will be added
  17979. * @param duration How long spiritball is active until it disappears
  17980. * @param char_id Target player (Optional)
  17981. * @author [Cydh]
  17982. */
  17983. BUILDIN_FUNC(addspiritball) {
  17984. uint8 i, count = script_getnum(st,2);
  17985. uint16 duration = script_getnum(st,3);
  17986. struct map_session_data *sd = NULL;
  17987. if (count == 0)
  17988. return SCRIPT_CMD_SUCCESS;
  17989. if (script_hasdata(st,4)) {
  17990. if (!script_isstring(st,4))
  17991. sd = map_charid2sd(script_getnum(st,4));
  17992. else
  17993. sd = map_nick2sd(script_getstr(st,4));
  17994. }
  17995. else
  17996. sd = script_rid2sd(st);
  17997. if (!sd)
  17998. return SCRIPT_CMD_FAILURE;
  17999. for (i = 0; i < count; i++)
  18000. pc_addspiritball(sd,duration,10);
  18001. return SCRIPT_CMD_SUCCESS;
  18002. }
  18003. /** Deletes the spirit ball(s) from player
  18004. * delspiritball <count>{,<char_id>};
  18005. * @param count How many spirit ball will be deleted
  18006. * @param char_id Target player (Optional)
  18007. * @author [Cydh]
  18008. */
  18009. BUILDIN_FUNC(delspiritball) {
  18010. uint8 count = script_getnum(st,2);
  18011. struct map_session_data *sd = NULL;
  18012. if (count == 0)
  18013. count = 1;
  18014. if (script_hasdata(st,3)) {
  18015. if (!script_isstring(st,3))
  18016. sd = map_charid2sd(script_getnum(st,3));
  18017. else
  18018. sd = map_nick2sd(script_getstr(st,3));
  18019. }
  18020. else
  18021. sd = script_rid2sd(st);
  18022. if (!sd)
  18023. return SCRIPT_CMD_FAILURE;
  18024. pc_delspiritball(sd,count,0);
  18025. return SCRIPT_CMD_SUCCESS;
  18026. }
  18027. /** Counts the spirit ball that player has
  18028. * countspiritball {<char_id>};
  18029. * @param char_id Target player (Optional)
  18030. * @author [Cydh]
  18031. */
  18032. BUILDIN_FUNC(countspiritball) {
  18033. struct map_session_data *sd;
  18034. if (script_hasdata(st,2)) {
  18035. if (!script_isstring(st,2))
  18036. sd = map_charid2sd(script_getnum(st,2));
  18037. else
  18038. sd = map_nick2sd(script_getstr(st,2));
  18039. }
  18040. else
  18041. sd = script_rid2sd(st);
  18042. if (!sd)
  18043. return SCRIPT_CMD_FAILURE;
  18044. script_pushint(st,sd->spiritball);
  18045. return SCRIPT_CMD_SUCCESS;
  18046. }
  18047. /** Merges separated stackable items because of guid
  18048. * mergeitem {<char_id>};
  18049. * @param char_id Char ID (Optional)
  18050. * @author [Cydh]
  18051. */
  18052. BUILDIN_FUNC(mergeitem) {
  18053. struct map_session_data *sd;
  18054. if (!script_charid2sd(2, sd))
  18055. return SCRIPT_CMD_FAILURE;
  18056. clif_merge_item_open(sd);
  18057. return SCRIPT_CMD_SUCCESS;
  18058. }
  18059. /** Merges separated stackable items because of guid (Unofficial)
  18060. * mergeitem2 {<item_id>,{<char_id>}};
  18061. * mergeitem2 {"<item name>",{<char_id>}};
  18062. * @param item Item ID/Name for merging specific item (Optional)
  18063. * @author [Cydh]
  18064. */
  18065. BUILDIN_FUNC(mergeitem2) {
  18066. struct map_session_data *sd;
  18067. struct item *items = NULL;
  18068. uint16 i, count = 0;
  18069. int nameid = 0;
  18070. if (!script_charid2sd(3, sd))
  18071. return SCRIPT_CMD_FAILURE;
  18072. if (script_hasdata(st, 2)) {
  18073. struct script_data *data = script_getdata(st, 2);
  18074. get_val(st, data);
  18075. if (data_isstring(data)) {// "<item name>"
  18076. const char *name = conv_str(st,data);
  18077. struct item_data *id;
  18078. if (!(id = itemdb_searchname(name))) {
  18079. ShowError("buildin_mergeitem2: Nonexistant item %s requested.\n", name);
  18080. script_pushint(st, count);
  18081. return SCRIPT_CMD_FAILURE;
  18082. }
  18083. nameid = id->nameid;
  18084. }
  18085. else if (data_isint(data)) {// <item id>
  18086. nameid = conv_num(st,data);
  18087. if (!itemdb_exists(nameid)) {
  18088. ShowError("buildin_mergeitem: Nonexistant item %d requested.\n", nameid);
  18089. script_pushint(st, count);
  18090. return SCRIPT_CMD_FAILURE;
  18091. }
  18092. }
  18093. }
  18094. for (i = 0; i < MAX_INVENTORY; i++) {
  18095. struct item *it = &sd->inventory.u.items_inventory[i];
  18096. if (!it || !it->unique_id || it->expire_time || !itemdb_isstackable(it->nameid))
  18097. continue;
  18098. if ((!nameid || (nameid == it->nameid))) {
  18099. uint8 k;
  18100. if (!count) {
  18101. CREATE(items, struct item, 1);
  18102. memcpy(&items[count++], it, sizeof(struct item));
  18103. pc_delitem(sd, i, it->amount, 0, 0, LOG_TYPE_NPC);
  18104. continue;
  18105. }
  18106. for (k = 0; k < count; k++) {
  18107. // Find Match
  18108. if (&items[k] && items[k].nameid == it->nameid && items[k].bound == it->bound && memcmp(items[k].card, it->card, sizeof(it->card)) == 0) {
  18109. items[k].amount += it->amount;
  18110. pc_delitem(sd, i, it->amount, 0, 0, LOG_TYPE_NPC);
  18111. break;
  18112. }
  18113. }
  18114. if (k >= count) {
  18115. // New entry
  18116. RECREATE(items, struct item, count+1);
  18117. memcpy(&items[count++], it, sizeof(struct item));
  18118. pc_delitem(sd, i, it->amount, 0, 0, LOG_TYPE_NPC);
  18119. }
  18120. }
  18121. }
  18122. if (!items) // Nothing todo here
  18123. return SCRIPT_CMD_SUCCESS;
  18124. // Retrieve the items
  18125. for (i = 0; i < count; i++) {
  18126. uint8 flag = 0;
  18127. if (!&items[i])
  18128. continue;
  18129. items[i].id = 0;
  18130. items[i].unique_id = 0;
  18131. if ((flag = pc_additem(sd, &items[i], items[i].amount, LOG_TYPE_NPC)))
  18132. clif_additem(sd, i, items[i].amount, flag);
  18133. }
  18134. aFree(items);
  18135. script_pushint(st, count);
  18136. return SCRIPT_CMD_SUCCESS;
  18137. }
  18138. /**
  18139. * Update an entry from NPC shop.
  18140. * npcshopupdate "<name>",<item_id>,<price>{,<stock>}
  18141. **/
  18142. BUILDIN_FUNC(npcshopupdate) {
  18143. const char* npcname = script_getstr(st, 2);
  18144. struct npc_data* nd = npc_name2id(npcname);
  18145. uint16 nameid = script_getnum(st, 3);
  18146. int price = script_getnum(st, 4);
  18147. #if PACKETVER >= 20131223
  18148. uint16 stock = script_hasdata(st,5) ? script_getnum(st,5) : 0;
  18149. #endif
  18150. int i;
  18151. if( !nd || ( nd->subtype != NPCTYPE_SHOP && nd->subtype != NPCTYPE_CASHSHOP && nd->subtype != NPCTYPE_ITEMSHOP && nd->subtype != NPCTYPE_POINTSHOP && nd->subtype != NPCTYPE_MARKETSHOP ) ) { // Not found.
  18152. script_pushint(st,0);
  18153. return SCRIPT_CMD_FAILURE;
  18154. }
  18155. if (!nd->u.shop.count) {
  18156. ShowError("buildin_npcshopupdate: Attempt to update empty shop from '%s'.\n", nd->exname);
  18157. script_pushint(st,0);
  18158. return SCRIPT_CMD_FAILURE;
  18159. }
  18160. for (i = 0; i < nd->u.shop.count; i++) {
  18161. if (nd->u.shop.shop_item[i].nameid == nameid) {
  18162. if (price != 0)
  18163. nd->u.shop.shop_item[i].value = price;
  18164. #if PACKETVER >= 20131223
  18165. if (nd->subtype == NPCTYPE_MARKETSHOP) {
  18166. nd->u.shop.shop_item[i].qty = stock;
  18167. npc_market_tosql(nd->exname, &nd->u.shop.shop_item[i]);
  18168. }
  18169. #endif
  18170. }
  18171. }
  18172. script_pushint(st,1);
  18173. return SCRIPT_CMD_SUCCESS;
  18174. }
  18175. /**
  18176. * Get rid from running script.
  18177. * getattachedrid();
  18178. **/
  18179. BUILDIN_FUNC(getattachedrid) {
  18180. script_pushint(st, st->rid);
  18181. return SCRIPT_CMD_SUCCESS;
  18182. }
  18183. /**
  18184. * Get variable from a Player
  18185. * getvar <variable>,<char_id>;
  18186. */
  18187. BUILDIN_FUNC(getvar) {
  18188. int char_id = script_getnum(st, 3);
  18189. struct map_session_data *sd = map_charid2sd(char_id);
  18190. struct script_data *data = NULL;
  18191. const char *name = NULL;
  18192. if (!sd) {
  18193. ShowError("buildin_getvar: No player found with char id '%d'.\n", char_id);
  18194. return SCRIPT_CMD_FAILURE;
  18195. }
  18196. data = script_getdata(st, 2);
  18197. if (!data_isreference(data)) {
  18198. ShowError("buildin_getvar: Not a variable\n");
  18199. script_reportdata(data);
  18200. script_pushnil(st);
  18201. st->state = END;
  18202. return SCRIPT_CMD_FAILURE;
  18203. }
  18204. name = reference_getname(data);
  18205. if (name[0] == '.' || name[0] == '$' || name[0] == '\'') { // Not a PC variable
  18206. ShowError("buildin_getvar: Invalid scope (not PC variable)\n");
  18207. script_reportdata(data);
  18208. script_pushnil(st);
  18209. st->state = END;
  18210. return SCRIPT_CMD_FAILURE;
  18211. }
  18212. get_val_(st, data, sd);
  18213. if (data_isint(data))
  18214. script_pushint(st, conv_num_(st, data, sd));
  18215. else
  18216. script_pushstrcopy(st, conv_str_(st, data, sd));
  18217. push_val2(st->stack, C_NAME, reference_getuid(data), reference_getref(data));
  18218. return SCRIPT_CMD_SUCCESS;
  18219. }
  18220. /**
  18221. * Display script message
  18222. * showscript "<message>"{,<GID>};
  18223. **/
  18224. BUILDIN_FUNC(showscript) {
  18225. struct block_list *bl = NULL;
  18226. const char *msg = script_getstr(st,2);
  18227. int id = 0;
  18228. if (script_hasdata(st,3)) {
  18229. id = script_getnum(st,3);
  18230. bl = map_id2bl(id);
  18231. }
  18232. else {
  18233. bl = st->rid ? map_id2bl(st->rid) : map_id2bl(st->oid);
  18234. }
  18235. if (!bl) {
  18236. ShowError("buildin_showscript: Script not attached. (id=%d, rid=%d, oid=%d)\n", id, st->rid, st->oid);
  18237. script_pushint(st,0);
  18238. return SCRIPT_CMD_FAILURE;
  18239. }
  18240. clif_showscript(bl, msg);
  18241. script_pushint(st,1);
  18242. return SCRIPT_CMD_SUCCESS;
  18243. }
  18244. /**
  18245. * Ignore the SECURE_NPCTIMEOUT function.
  18246. * ignoretimeout <flag>{,<char_id>};
  18247. */
  18248. BUILDIN_FUNC(ignoretimeout)
  18249. {
  18250. #ifdef SECURE_NPCTIMEOUT
  18251. struct map_session_data *sd = NULL;
  18252. if (script_hasdata(st,3)) {
  18253. if (!script_isstring(st,3))
  18254. sd = map_charid2sd(script_getnum(st,3));
  18255. else
  18256. sd = map_nick2sd(script_getstr(st,3));
  18257. } else
  18258. sd = script_rid2sd(st);
  18259. if (!sd)
  18260. return SCRIPT_CMD_FAILURE;
  18261. sd->state.ignoretimeout = script_getnum(st,2) > 0;
  18262. #endif
  18263. return SCRIPT_CMD_SUCCESS;
  18264. }
  18265. /**
  18266. * geteleminfo <type>{,<char_id>};
  18267. **/
  18268. BUILDIN_FUNC(geteleminfo) {
  18269. TBL_ELEM *ed = NULL;
  18270. TBL_PC *sd = NULL;
  18271. int type = script_getnum(st,2);
  18272. if (!script_charid2sd(3, sd)) {
  18273. script_pushint(st, 0);
  18274. return SCRIPT_CMD_SUCCESS;
  18275. }
  18276. if (!(ed = sd->ed)) {
  18277. //ShowDebug("buildin_geteleminfo: Player doesn't have Elemental.\n");
  18278. script_pushint(st, 0);
  18279. return SCRIPT_CMD_SUCCESS;
  18280. }
  18281. switch (type) {
  18282. case 0: script_pushint(st, ed->elemental.elemental_id); break;
  18283. case 1: script_pushint(st, ed->bl.id); break;
  18284. default:
  18285. ShowError("buildin_geteleminfo: Invalid type '%d'.\n", type);
  18286. script_pushint(st, 0);
  18287. return SCRIPT_CMD_FAILURE;
  18288. }
  18289. return SCRIPT_CMD_SUCCESS;
  18290. }
  18291. /**
  18292. * Set the quest info of quest_id only showed on player in level range.
  18293. * setquestinfo_level <quest_id>,<min_level>,<max_level>
  18294. * @author [Cydh]
  18295. **/
  18296. BUILDIN_FUNC(setquestinfo_level) {
  18297. TBL_NPC* nd = map_id2nd(st->oid);
  18298. int quest_id = script_getnum(st, 2);
  18299. struct questinfo *qi = map_has_questinfo(nd->bl.m, nd, quest_id);
  18300. if (!qi) {
  18301. ShowError("buildin_setquestinfo_level: Quest with ID '%d' is not defined yet.\n", quest_id);
  18302. return SCRIPT_CMD_FAILURE;
  18303. }
  18304. qi->min_level = script_getnum(st, 3);
  18305. qi->max_level = script_getnum(st, 4);
  18306. if (!qi->max_level)
  18307. qi->max_level = MAX_LEVEL;
  18308. return SCRIPT_CMD_SUCCESS;
  18309. }
  18310. /**
  18311. * Set the quest info of quest_id only showed for player that has quest criteria
  18312. * setquestinfo_req <quest_id>,<quest_req_id>,<state>{,<quest_req_id>,<state>,...};
  18313. * @author [Cydh]
  18314. **/
  18315. BUILDIN_FUNC(setquestinfo_req) {
  18316. TBL_NPC* nd = map_id2nd(st->oid);
  18317. int quest_id = script_getnum(st, 2);
  18318. struct questinfo *qi = map_has_questinfo(nd->bl.m, nd, quest_id);
  18319. uint8 i = 0;
  18320. uint8 num = script_lastdata(st);
  18321. if (!qi) {
  18322. ShowError("buildin_setquestinfo_req: Quest with ID '%d' is not defined yet.\n", quest_id);
  18323. return SCRIPT_CMD_FAILURE;
  18324. }
  18325. if (quest_search(quest_id) == &quest_dummy) {
  18326. ShowError("buildin_setquestinfo_req: Quest with ID '%d' is not found in Quest DB.\n", quest_id);
  18327. return SCRIPT_CMD_FAILURE;
  18328. }
  18329. if (num%2) {
  18330. ShowError("buildin_setquestinfo_req: Odd number of parameters(%d) - pairs of requirements are expected.\n", num-2);
  18331. return SCRIPT_CMD_FAILURE;
  18332. }
  18333. for (i = 3; i <= num; i += 2) {
  18334. RECREATE(qi->req, struct questinfo_req, qi->req_count+1);
  18335. qi->req[qi->req_count].quest_id = script_getnum(st, i);
  18336. qi->req[qi->req_count].state = (script_getnum(st, i+1) >= 2) ? 2 : (script_getnum(st, i+1) <= 0) ? 0 : 1;
  18337. qi->req_count++;
  18338. }
  18339. return SCRIPT_CMD_SUCCESS;
  18340. }
  18341. /**
  18342. * Set the quest info of quest_id only showed for player that has specified Job
  18343. * setquestinfo_job <quest_id>,<job>{,<job>...};
  18344. * @author [Cydh]
  18345. **/
  18346. BUILDIN_FUNC(setquestinfo_job) {
  18347. TBL_NPC* nd = map_id2nd(st->oid);
  18348. int quest_id = script_getnum(st, 2);
  18349. struct questinfo *qi = map_has_questinfo(nd->bl.m, nd, quest_id);
  18350. int job_id = 0;
  18351. uint8 i = 0;
  18352. uint8 num = script_lastdata(st)+1;
  18353. if (!qi) {
  18354. ShowError("buildin_setquestinfo_job: Quest with ID '%d' is not defined yet.\n", quest_id);
  18355. return SCRIPT_CMD_FAILURE;
  18356. }
  18357. for (i = 3; i < num; i++) {
  18358. job_id = script_getnum(st, i);
  18359. if (!pcdb_checkid(job_id)) {
  18360. ShowError("buildin_setquestinfo_job: Invalid job id '%d' in Quest with ID %d.\n", job_id, quest_id);
  18361. continue;
  18362. }
  18363. buildin_questinfo_setjob(qi, job_id);
  18364. }
  18365. return SCRIPT_CMD_SUCCESS;
  18366. }
  18367. /**
  18368. * opendressroom(<flag>{,<char_id>});
  18369. */
  18370. BUILDIN_FUNC(opendressroom)
  18371. {
  18372. #if PACKETVER >= 20150513
  18373. int flag = 1;
  18374. TBL_PC* sd;
  18375. if( script_hasdata(st,2) )
  18376. flag = script_getnum(st,2);
  18377. if (!script_charid2sd(3, sd))
  18378. return SCRIPT_CMD_FAILURE;
  18379. clif_dressing_room(sd, flag);
  18380. return SCRIPT_CMD_SUCCESS;
  18381. #else
  18382. return SCRIPT_CMD_FAILURE;
  18383. #endif
  18384. }
  18385. /**
  18386. * navigateto("<map>"{,<x>,<y>,<flag>,<hide_window>,<monster_id>,<char_id>});
  18387. */
  18388. BUILDIN_FUNC(navigateto){
  18389. #if PACKETVER >= 20111010
  18390. TBL_PC* sd;
  18391. const char *map;
  18392. uint16 x = 0, y = 0, monster_id = 0;
  18393. uint8 flag = NAV_KAFRA_AND_AIRSHIP;
  18394. bool hideWindow = true;
  18395. map = script_getstr(st,2);
  18396. if( script_hasdata(st,3) )
  18397. x = script_getnum(st,3);
  18398. if( script_hasdata(st,4) )
  18399. y = script_getnum(st,4);
  18400. if( script_hasdata(st,5) )
  18401. flag = (uint8)script_getnum(st,5);
  18402. if( script_hasdata(st,6) )
  18403. hideWindow = script_getnum(st,6) ? true : false;
  18404. if( script_hasdata(st,7) )
  18405. monster_id = script_getnum(st,7);
  18406. if (!script_charid2sd(8, sd))
  18407. return SCRIPT_CMD_FAILURE;
  18408. clif_navigateTo(sd,map,x,y,flag,hideWindow,monster_id);
  18409. return SCRIPT_CMD_SUCCESS;
  18410. #else
  18411. return SCRIPT_CMD_FAILURE;
  18412. #endif
  18413. }
  18414. /**
  18415. * adopt("<parent_name>","<baby_name>");
  18416. * adopt(<parent_id>,<baby_id>);
  18417. * https://rathena.org/board/topic/104014-suggestion-add-adopt-or-etc/
  18418. */
  18419. BUILDIN_FUNC(adopt)
  18420. {
  18421. TBL_PC *sd, *b_sd;
  18422. struct script_data *data;
  18423. enum adopt_responses response;
  18424. data = script_getdata(st, 2);
  18425. get_val(st, data);
  18426. if (data_isstring(data)) {
  18427. const char *name = conv_str(st, data);
  18428. sd = map_nick2sd(name);
  18429. if (sd == NULL) {
  18430. ShowError("buildin_adopt: Non-existant parent character %s requested.\n", name);
  18431. return SCRIPT_CMD_FAILURE;
  18432. }
  18433. } else if (data_isint(data)) {
  18434. uint32 char_id = conv_num(st, data);
  18435. sd = map_charid2sd(char_id);
  18436. if (sd == NULL) {
  18437. ShowError("buildin_adopt: Non-existant parent character %d requested.\n", char_id);
  18438. return SCRIPT_CMD_FAILURE;
  18439. }
  18440. } else {
  18441. ShowError("buildin_adopt: Invalid data type for argument #1 (%d).", data->type);
  18442. return SCRIPT_CMD_FAILURE;
  18443. }
  18444. data = script_getdata(st, 3);
  18445. get_val(st, data);
  18446. if (data_isstring(data)) {
  18447. const char *name = conv_str(st, data);
  18448. b_sd = map_nick2sd(name);
  18449. if (b_sd == NULL) {
  18450. ShowError("buildin_adopt: Non-existant baby character %s requested.\n", name);
  18451. return SCRIPT_CMD_FAILURE;
  18452. }
  18453. } else if (data_isint(data)) {
  18454. uint32 char_id = conv_num(st, data);
  18455. b_sd = map_charid2sd(char_id);
  18456. if (b_sd == NULL) {
  18457. ShowError("buildin_adopt: Non-existant baby character %d requested.\n", char_id);
  18458. return SCRIPT_CMD_FAILURE;
  18459. }
  18460. } else {
  18461. ShowError("buildin_adopt: Invalid data type for argument #2 (%d).", data->type);
  18462. return SCRIPT_CMD_FAILURE;
  18463. }
  18464. response = pc_try_adopt(sd, map_charid2sd(sd->status.partner_id), b_sd);
  18465. if (response == ADOPT_ALLOWED) {
  18466. TBL_PC *p_sd = map_charid2sd(sd->status.partner_id);
  18467. b_sd->adopt_invite = sd->status.account_id;
  18468. clif_Adopt_request(b_sd, sd, p_sd->status.account_id);
  18469. script_pushint(st, ADOPT_ALLOWED);
  18470. return SCRIPT_CMD_SUCCESS;
  18471. }
  18472. script_pushint(st, response);
  18473. return SCRIPT_CMD_FAILURE;
  18474. }
  18475. /**
  18476. * Safety Base/Job EXP addition than using `set BaseExp,n;` or `set JobExp,n;`
  18477. * Unlike `getexp` that affected by some adjustments.
  18478. * getexp2 <base_exp>,<job_exp>{,<char_id>};
  18479. * @author [Cydh]
  18480. **/
  18481. BUILDIN_FUNC(getexp2) {
  18482. TBL_PC *sd = NULL;
  18483. int base_exp = script_getnum(st, 2);
  18484. int job_exp = script_getnum(st, 3);
  18485. if (!script_charid2sd(4, sd))
  18486. return SCRIPT_CMD_FAILURE;
  18487. if (base_exp == 0 && job_exp == 0)
  18488. return SCRIPT_CMD_SUCCESS;
  18489. if (base_exp > 0)
  18490. pc_gainexp(sd, NULL, base_exp, 0, 2);
  18491. else if (base_exp < 0)
  18492. pc_lostexp(sd, base_exp * -1, 0);
  18493. if (job_exp > 0)
  18494. pc_gainexp(sd, NULL, 0, job_exp, 2);
  18495. else if (job_exp < 0)
  18496. pc_lostexp(sd, 0, job_exp * -1);
  18497. return SCRIPT_CMD_SUCCESS;
  18498. }
  18499. #include "../custom/script.inc"
  18500. // declarations that were supposed to be exported from npc_chat.c
  18501. #ifdef PCRE_SUPPORT
  18502. BUILDIN_FUNC(defpattern);
  18503. BUILDIN_FUNC(activatepset);
  18504. BUILDIN_FUNC(deactivatepset);
  18505. BUILDIN_FUNC(deletepset);
  18506. #endif
  18507. /** Regular expression matching
  18508. * preg_match(<pattern>,<string>{,<offset>})
  18509. */
  18510. BUILDIN_FUNC(preg_match) {
  18511. #ifdef PCRE_SUPPORT
  18512. pcre *re;
  18513. pcre_extra *pcreExtra;
  18514. const char *error;
  18515. int erroffset, r, offset = 0;
  18516. int subStrVec[30];
  18517. const char* pattern = script_getstr(st,2);
  18518. const char* subject = script_getstr(st,3);
  18519. if (script_hasdata(st,4))
  18520. offset = script_getnum(st,4);
  18521. re = pcre_compile(pattern, 0, &error, &erroffset, NULL);
  18522. pcreExtra = pcre_study(re, 0, &error);
  18523. r = pcre_exec(re, pcreExtra, subject, (int)strlen(subject), offset, 0, subStrVec, 30);
  18524. pcre_free(re);
  18525. if (pcreExtra != NULL)
  18526. pcre_free(pcreExtra);
  18527. if (r < 0)
  18528. script_pushint(st,0);
  18529. else
  18530. script_pushint(st,(r > 0) ? r : 30 / 3);
  18531. return SCRIPT_CMD_SUCCESS;
  18532. #else
  18533. ShowDebug("script:preg_match: cannot run without PCRE library enabled.\n");
  18534. script_pushint(st,0);
  18535. return SCRIPT_CMD_SUCCESS;
  18536. #endif
  18537. }
  18538. /// script command definitions
  18539. /// for an explanation on args, see add_buildin_func
  18540. struct script_function buildin_func[] = {
  18541. // NPC interaction
  18542. BUILDIN_DEF(mes,"s*"),
  18543. BUILDIN_DEF(next,""),
  18544. BUILDIN_DEF(close,""),
  18545. BUILDIN_DEF(close2,""),
  18546. BUILDIN_DEF(menu,"sl*"),
  18547. BUILDIN_DEF(select,"s*"), //for future jA script compatibility
  18548. BUILDIN_DEF(prompt,"s*"),
  18549. //
  18550. BUILDIN_DEF(goto,"l"),
  18551. BUILDIN_DEF(callsub,"l*"),
  18552. BUILDIN_DEF(callfunc,"s*"),
  18553. BUILDIN_DEF(return,"?"),
  18554. BUILDIN_DEF(getarg,"i?"),
  18555. BUILDIN_DEF(jobchange,"i??"),
  18556. BUILDIN_DEF(jobname,"i"),
  18557. BUILDIN_DEF(input,"r??"),
  18558. BUILDIN_DEF(warp,"sii"),
  18559. BUILDIN_DEF(areawarp,"siiiisii??"),
  18560. BUILDIN_DEF(warpchar,"siii"), // [LuzZza]
  18561. BUILDIN_DEF(warpparty,"siii?"), // [Fredzilla] [Paradox924X]
  18562. BUILDIN_DEF(warpguild,"siii"), // [Fredzilla]
  18563. BUILDIN_DEF(setlook,"ii?"),
  18564. BUILDIN_DEF(changelook,"ii?"), // Simulates but don't Store it
  18565. BUILDIN_DEF(set,"rv?"),
  18566. BUILDIN_DEF(setarray,"rv*"),
  18567. BUILDIN_DEF(cleararray,"rvi"),
  18568. BUILDIN_DEF(copyarray,"rri"),
  18569. BUILDIN_DEF(getarraysize,"r"),
  18570. BUILDIN_DEF(deletearray,"r?"),
  18571. BUILDIN_DEF(getelementofarray,"ri"),
  18572. BUILDIN_DEF(getitem,"vi?"),
  18573. BUILDIN_DEF(rentitem,"vi?"),
  18574. BUILDIN_DEF(rentitem2,"viiiiiiii?"),
  18575. BUILDIN_DEF(getitem2,"viiiiiiii?"),
  18576. BUILDIN_DEF(getnameditem,"vv"),
  18577. BUILDIN_DEF2(grouprandomitem,"groupranditem","i?"),
  18578. BUILDIN_DEF(makeitem,"visii"),
  18579. BUILDIN_DEF(makeitem2,"visiiiiiiiii"),
  18580. BUILDIN_DEF(delitem,"vi?"),
  18581. BUILDIN_DEF2(delitem,"storagedelitem","vi?"),
  18582. BUILDIN_DEF2(delitem,"guildstoragedelitem","vi?"),
  18583. BUILDIN_DEF2(delitem,"cartdelitem","vi?"),
  18584. BUILDIN_DEF(delitem2,"viiiiiiii?"),
  18585. BUILDIN_DEF2(delitem2,"storagedelitem2","viiiiiiii?"),
  18586. BUILDIN_DEF2(delitem2,"guildstoragedelitem2","viiiiiiii?"),
  18587. BUILDIN_DEF2(delitem2,"cartdelitem2","viiiiiiii?"),
  18588. BUILDIN_DEF2(enableitemuse,"enable_items",""),
  18589. BUILDIN_DEF2(disableitemuse,"disable_items",""),
  18590. BUILDIN_DEF(cutin,"si"),
  18591. BUILDIN_DEF(viewpoint,"iiiii"),
  18592. BUILDIN_DEF(heal,"ii?"),
  18593. BUILDIN_DEF(itemheal,"ii?"),
  18594. BUILDIN_DEF(percentheal,"ii?"),
  18595. BUILDIN_DEF(rand,"i?"),
  18596. BUILDIN_DEF(countitem,"v?"),
  18597. BUILDIN_DEF2(countitem,"storagecountitem","v?"),
  18598. BUILDIN_DEF2(countitem,"guildstoragecountitem","v?"),
  18599. BUILDIN_DEF2(countitem,"cartcountitem","v?"),
  18600. BUILDIN_DEF2(countitem,"countitem2","viiiiiii?"),
  18601. BUILDIN_DEF2(countitem,"storagecountitem2","viiiiiii?"),
  18602. BUILDIN_DEF2(countitem,"guildstoragecountitem2","viiiiiii?"),
  18603. BUILDIN_DEF2(countitem,"cartcountitem2","viiiiiii?"),
  18604. BUILDIN_DEF(checkweight,"vi*"),
  18605. BUILDIN_DEF(checkweight2,"rr"),
  18606. BUILDIN_DEF(readparam,"i?"),
  18607. BUILDIN_DEF(getcharid,"i?"),
  18608. BUILDIN_DEF(getnpcid,"i?"),
  18609. BUILDIN_DEF(getpartyname,"i"),
  18610. BUILDIN_DEF(getpartymember,"i??"),
  18611. BUILDIN_DEF(getpartyleader,"i?"),
  18612. BUILDIN_DEF(getguildname,"i"),
  18613. BUILDIN_DEF(getguildmaster,"i"),
  18614. BUILDIN_DEF(getguildmasterid,"i"),
  18615. BUILDIN_DEF(strcharinfo,"i?"),
  18616. BUILDIN_DEF(strnpcinfo,"i"),
  18617. BUILDIN_DEF(getequipid,"i?"),
  18618. BUILDIN_DEF(getequipuniqueid,"i?"),
  18619. BUILDIN_DEF(getequipname,"i?"),
  18620. BUILDIN_DEF(getbrokenid,"i?"), // [Valaris]
  18621. BUILDIN_DEF(repair,"i?"), // [Valaris]
  18622. BUILDIN_DEF(repairall,"?"),
  18623. BUILDIN_DEF(getequipisequiped,"i?"),
  18624. BUILDIN_DEF(getequipisenableref,"i?"),
  18625. BUILDIN_DEF(getequiprefinerycnt,"i?"),
  18626. BUILDIN_DEF(getequipweaponlv,"i?"),
  18627. BUILDIN_DEF(getequippercentrefinery,"i?"),
  18628. BUILDIN_DEF(successrefitem,"i??"),
  18629. BUILDIN_DEF(failedrefitem,"i?"),
  18630. BUILDIN_DEF(downrefitem,"i??"),
  18631. BUILDIN_DEF(statusup,"i?"),
  18632. BUILDIN_DEF(statusup2,"ii?"),
  18633. BUILDIN_DEF(bonus,"iv"),
  18634. BUILDIN_DEF2(bonus,"bonus2","ivi"),
  18635. BUILDIN_DEF2(bonus,"bonus3","ivii"),
  18636. BUILDIN_DEF2(bonus,"bonus4","ivvii"),
  18637. BUILDIN_DEF2(bonus,"bonus5","ivviii"),
  18638. BUILDIN_DEF(autobonus,"sii??"),
  18639. BUILDIN_DEF(autobonus2,"sii??"),
  18640. BUILDIN_DEF(autobonus3,"siiv?"),
  18641. BUILDIN_DEF(skill,"vi?"),
  18642. BUILDIN_DEF2(skill,"addtoskill","vi?"), // [Valaris]
  18643. BUILDIN_DEF(guildskill,"vi"),
  18644. BUILDIN_DEF(getskilllv,"v"),
  18645. BUILDIN_DEF(getgdskilllv,"iv"),
  18646. BUILDIN_DEF(basicskillcheck,""),
  18647. BUILDIN_DEF(getgmlevel,"?"),
  18648. BUILDIN_DEF(getgroupid,"?"),
  18649. BUILDIN_DEF(end,""),
  18650. BUILDIN_DEF(checkoption,"i?"),
  18651. BUILDIN_DEF(setoption,"i??"),
  18652. BUILDIN_DEF(setcart,"??"),
  18653. BUILDIN_DEF(checkcart,"?"),
  18654. BUILDIN_DEF(setfalcon,"??"),
  18655. BUILDIN_DEF(checkfalcon,"?"),
  18656. BUILDIN_DEF(setriding,"??"),
  18657. BUILDIN_DEF(checkriding,"?"),
  18658. BUILDIN_DEF(checkwug,"?"),
  18659. BUILDIN_DEF(checkmadogear,"?"),
  18660. BUILDIN_DEF(setmadogear,"??"),
  18661. BUILDIN_DEF2(savepoint,"save","sii?"),
  18662. BUILDIN_DEF(savepoint,"sii?"),
  18663. BUILDIN_DEF(gettimetick,"i"),
  18664. BUILDIN_DEF(gettime,"i"),
  18665. BUILDIN_DEF(gettimestr,"si"),
  18666. BUILDIN_DEF(openstorage,""),
  18667. BUILDIN_DEF(guildopenstorage,""),
  18668. BUILDIN_DEF(itemskill,"vi"),
  18669. BUILDIN_DEF(produce,"i"),
  18670. BUILDIN_DEF(cooking,"i"),
  18671. BUILDIN_DEF(monster,"siisii???"),
  18672. BUILDIN_DEF(getmobdrops,"i"),
  18673. BUILDIN_DEF(areamonster,"siiiisii???"),
  18674. BUILDIN_DEF(killmonster,"ss?"),
  18675. BUILDIN_DEF(killmonsterall,"s?"),
  18676. BUILDIN_DEF(clone,"siisi????"),
  18677. BUILDIN_DEF(doevent,"s"),
  18678. BUILDIN_DEF(donpcevent,"s"),
  18679. BUILDIN_DEF(cmdothernpc,"ss"),
  18680. BUILDIN_DEF(addtimer,"is"),
  18681. BUILDIN_DEF(deltimer,"s"),
  18682. BUILDIN_DEF(addtimercount,"is"),
  18683. BUILDIN_DEF(initnpctimer,"??"),
  18684. BUILDIN_DEF(stopnpctimer,"??"),
  18685. BUILDIN_DEF(startnpctimer,"??"),
  18686. BUILDIN_DEF(setnpctimer,"i?"),
  18687. BUILDIN_DEF(getnpctimer,"i?"),
  18688. BUILDIN_DEF(attachnpctimer,"?"), // attached the player id to the npc timer [Celest]
  18689. BUILDIN_DEF(detachnpctimer,"?"), // detached the player id from the npc timer [Celest]
  18690. BUILDIN_DEF(playerattached,""), // returns id of the current attached player. [Skotlex]
  18691. BUILDIN_DEF(announce,"si?????"),
  18692. BUILDIN_DEF(mapannounce,"ssi?????"),
  18693. BUILDIN_DEF(areaannounce,"siiiisi?????"),
  18694. BUILDIN_DEF(getusers,"i"),
  18695. BUILDIN_DEF(getmapguildusers,"si"),
  18696. BUILDIN_DEF(getmapusers,"s"),
  18697. BUILDIN_DEF(getareausers,"siiii"),
  18698. BUILDIN_DEF(getareadropitem,"siiiiv"),
  18699. BUILDIN_DEF(enablenpc,"s"),
  18700. BUILDIN_DEF(disablenpc,"s"),
  18701. BUILDIN_DEF(hideoffnpc,"s"),
  18702. BUILDIN_DEF(hideonnpc,"s"),
  18703. BUILDIN_DEF(sc_start,"iii???"),
  18704. BUILDIN_DEF2(sc_start,"sc_start2","iiii???"),
  18705. BUILDIN_DEF2(sc_start,"sc_start4","iiiiii???"),
  18706. BUILDIN_DEF(sc_end,"i?"),
  18707. BUILDIN_DEF(getstatus, "i??"),
  18708. BUILDIN_DEF(getscrate,"ii?"),
  18709. BUILDIN_DEF(debugmes,"s"),
  18710. BUILDIN_DEF2(catchpet,"pet","i"),
  18711. BUILDIN_DEF2(birthpet,"bpet",""),
  18712. BUILDIN_DEF(resetlvl,"i?"),
  18713. BUILDIN_DEF(resetstatus,"?"),
  18714. BUILDIN_DEF(resetskill,"?"),
  18715. BUILDIN_DEF(skillpointcount,"?"),
  18716. BUILDIN_DEF(changebase,"i?"),
  18717. BUILDIN_DEF(changesex,"?"),
  18718. BUILDIN_DEF(changecharsex,"?"),
  18719. BUILDIN_DEF(waitingroom,"si?????"),
  18720. BUILDIN_DEF(delwaitingroom,"?"),
  18721. BUILDIN_DEF(waitingroomkick,"ss"),
  18722. BUILDIN_DEF(getwaitingroomusers, "?"),
  18723. BUILDIN_DEF2(waitingroomkickall,"kickwaitingroomall","?"),
  18724. BUILDIN_DEF(enablewaitingroomevent,"?"),
  18725. BUILDIN_DEF(disablewaitingroomevent,"?"),
  18726. BUILDIN_DEF2(enablewaitingroomevent,"enablearena",""), // Added by RoVeRT
  18727. BUILDIN_DEF2(disablewaitingroomevent,"disablearena",""), // Added by RoVeRT
  18728. BUILDIN_DEF(getwaitingroomstate,"i?"),
  18729. BUILDIN_DEF(warpwaitingpc,"sii?"),
  18730. BUILDIN_DEF(attachrid,"i"),
  18731. BUILDIN_DEF(addrid,"i?????"),
  18732. BUILDIN_DEF(detachrid,""),
  18733. BUILDIN_DEF(isloggedin,"i?"),
  18734. BUILDIN_DEF(setmapflagnosave,"ssii"),
  18735. BUILDIN_DEF(getmapflag,"si?"),
  18736. BUILDIN_DEF(setmapflag,"si??"),
  18737. BUILDIN_DEF(removemapflag,"si?"),
  18738. BUILDIN_DEF(pvpon,"s"),
  18739. BUILDIN_DEF(pvpoff,"s"),
  18740. BUILDIN_DEF(gvgon,"s"),
  18741. BUILDIN_DEF(gvgoff,"s"),
  18742. BUILDIN_DEF(emotion,"i??"),
  18743. BUILDIN_DEF(maprespawnguildid,"sii"),
  18744. BUILDIN_DEF(agitstart,""), // <Agit>
  18745. BUILDIN_DEF(agitend,""),
  18746. BUILDIN_DEF(agitcheck,""), // <Agitcheck>
  18747. BUILDIN_DEF(flagemblem,"i"), // Flag Emblem
  18748. BUILDIN_DEF(getcastlename,"s"),
  18749. BUILDIN_DEF(getcastledata,"si"),
  18750. BUILDIN_DEF(setcastledata,"sii"),
  18751. BUILDIN_DEF(requestguildinfo,"i?"),
  18752. BUILDIN_DEF(getequipcardcnt,"i"),
  18753. BUILDIN_DEF(successremovecards,"i"),
  18754. BUILDIN_DEF(failedremovecards,"ii"),
  18755. BUILDIN_DEF(marriage,"s"),
  18756. BUILDIN_DEF2(wedding_effect,"wedding",""),
  18757. BUILDIN_DEF(divorce,"?"),
  18758. BUILDIN_DEF(ispartneron,"?"),
  18759. BUILDIN_DEF(getpartnerid,"?"),
  18760. BUILDIN_DEF(getchildid,"?"),
  18761. BUILDIN_DEF(getmotherid,"?"),
  18762. BUILDIN_DEF(getfatherid,"?"),
  18763. BUILDIN_DEF(warppartner,"sii"),
  18764. BUILDIN_DEF(getitemname,"v"),
  18765. BUILDIN_DEF(getitemslots,"i"),
  18766. BUILDIN_DEF(makepet,"i"),
  18767. BUILDIN_DEF(getexp,"ii?"),
  18768. BUILDIN_DEF(getinventorylist,"?"),
  18769. BUILDIN_DEF(getskilllist,"?"),
  18770. BUILDIN_DEF(clearitem,"?"),
  18771. BUILDIN_DEF(classchange,"ii"),
  18772. BUILDIN_DEF(misceffect,"i"),
  18773. BUILDIN_DEF(playBGM,"s"),
  18774. BUILDIN_DEF(playBGMall,"s?????"),
  18775. BUILDIN_DEF(soundeffect,"si"),
  18776. BUILDIN_DEF(soundeffectall,"si?????"), // SoundEffectAll [Codemaster]
  18777. BUILDIN_DEF(strmobinfo,"ii"), // display mob data [Valaris]
  18778. BUILDIN_DEF(guardian,"siisi??"), // summon guardians
  18779. BUILDIN_DEF(guardianinfo,"sii"), // display guardian data [Valaris]
  18780. BUILDIN_DEF(petskillbonus,"iiii"), // [Valaris]
  18781. BUILDIN_DEF(petrecovery,"ii"), // [Valaris]
  18782. BUILDIN_DEF(petloot,"i"), // [Valaris]
  18783. BUILDIN_DEF(petheal,"iiii"), // [Valaris]
  18784. BUILDIN_DEF(petskillattack,"viii"), // [Skotlex]
  18785. BUILDIN_DEF(petskillattack2,"viiii"), // [Valaris]
  18786. BUILDIN_DEF(petskillsupport,"viiii"), // [Skotlex]
  18787. BUILDIN_DEF(skilleffect,"vi"), // skill effect [Celest]
  18788. BUILDIN_DEF(npcskilleffect,"viii"), // npc skill effect [Valaris]
  18789. BUILDIN_DEF(specialeffect,"i??"), // npc skill effect [Valaris]
  18790. BUILDIN_DEF(specialeffect2,"i??"), // skill effect on players[Valaris]
  18791. BUILDIN_DEF(nude,"?"), // nude command [Valaris]
  18792. BUILDIN_DEF(mapwarp,"ssii??"), // Added by RoVeRT
  18793. BUILDIN_DEF(atcommand,"s"), // [MouseJstr]
  18794. BUILDIN_DEF2(atcommand,"charcommand","s"), // [MouseJstr]
  18795. BUILDIN_DEF(movenpc,"sii?"), // [MouseJstr]
  18796. BUILDIN_DEF(message,"ss"), // [MouseJstr]
  18797. BUILDIN_DEF(npctalk,"s"), // [Valaris]
  18798. BUILDIN_DEF(mobcount,"ss"),
  18799. BUILDIN_DEF(getlook,"i?"),
  18800. BUILDIN_DEF(getsavepoint,"i?"),
  18801. BUILDIN_DEF(npcspeed,"i"), // [Valaris]
  18802. BUILDIN_DEF(npcwalkto,"ii"), // [Valaris]
  18803. BUILDIN_DEF(npcstop,""), // [Valaris]
  18804. BUILDIN_DEF(getmapxy,"rrri?"), //by Lorky [Lupus]
  18805. BUILDIN_DEF(mapid2name,"i"),
  18806. BUILDIN_DEF(checkoption1,"i?"),
  18807. BUILDIN_DEF(checkoption2,"i?"),
  18808. BUILDIN_DEF(guildgetexp,"i"),
  18809. BUILDIN_DEF(guildchangegm,"is"),
  18810. BUILDIN_DEF(logmes,"s"), //this command actls as MES but rints info into LOG file either SQL/TXT [Lupus]
  18811. BUILDIN_DEF(summon,"si??"), // summons a slave monster [Celest]
  18812. BUILDIN_DEF(isnight,""), // check whether it is night time [Celest]
  18813. BUILDIN_DEF(isday,""), // check whether it is day time [Celest]
  18814. BUILDIN_DEF(isequipped,"i*"), // check whether another item/card has been equipped [Celest]
  18815. BUILDIN_DEF(isequippedcnt,"i*"), // check how many items/cards are being equipped [Celest]
  18816. BUILDIN_DEF(cardscnt,"i*"), // check how many items/cards are being equipped in the same arm [Lupus]
  18817. BUILDIN_DEF(getrefine,""), // returns the refined number of the current item, or an item with index specified [celest]
  18818. BUILDIN_DEF(night,""), // sets the server to night time
  18819. BUILDIN_DEF(day,""), // sets the server to day time
  18820. #ifdef PCRE_SUPPORT
  18821. BUILDIN_DEF(defpattern,"iss"), // Define pattern to listen for [MouseJstr]
  18822. BUILDIN_DEF(activatepset,"i"), // Activate a pattern set [MouseJstr]
  18823. BUILDIN_DEF(deactivatepset,"i"), // Deactive a pattern set [MouseJstr]
  18824. BUILDIN_DEF(deletepset,"i"), // Delete a pattern set [MouseJstr]
  18825. #endif
  18826. BUILDIN_DEF(preg_match,"ss?"),
  18827. BUILDIN_DEF(dispbottom,"s??"), //added from jA [Lupus]
  18828. BUILDIN_DEF(recovery,"i???"),
  18829. BUILDIN_DEF(getpetinfo,"i?"),
  18830. BUILDIN_DEF(gethominfo,"i?"),
  18831. BUILDIN_DEF(getmercinfo,"i?"),
  18832. BUILDIN_DEF(checkequipedcard,"i"),
  18833. BUILDIN_DEF(jump_zero,"il"), //for future jA script compatibility
  18834. BUILDIN_DEF(globalmes,"s?"), //end jA addition
  18835. BUILDIN_DEF(unequip,"i?"), // unequip command [Spectre]
  18836. BUILDIN_DEF(getstrlen,"s"), //strlen [Valaris]
  18837. BUILDIN_DEF(charisalpha,"si"), //isalpha [Valaris]
  18838. BUILDIN_DEF(charat,"si"),
  18839. BUILDIN_DEF(setchar,"ssi"),
  18840. BUILDIN_DEF(insertchar,"ssi"),
  18841. BUILDIN_DEF(delchar,"si"),
  18842. BUILDIN_DEF(strtoupper,"s"),
  18843. BUILDIN_DEF(strtolower,"s"),
  18844. BUILDIN_DEF(charisupper, "si"),
  18845. BUILDIN_DEF(charislower, "si"),
  18846. BUILDIN_DEF(substr,"sii"),
  18847. BUILDIN_DEF(explode, "rss"),
  18848. BUILDIN_DEF(implode, "r?"),
  18849. BUILDIN_DEF(sprintf,"s*"), // [Mirei]
  18850. BUILDIN_DEF(sscanf,"ss*"), // [Mirei]
  18851. BUILDIN_DEF(strpos,"ss?"),
  18852. BUILDIN_DEF(replacestr,"sss??"),
  18853. BUILDIN_DEF(countstr,"ss?"),
  18854. BUILDIN_DEF(setnpcdisplay,"sv??"),
  18855. BUILDIN_DEF(compare,"ss"), // Lordalfa - To bring strstr to scripting Engine.
  18856. BUILDIN_DEF(strcmp,"ss"),
  18857. BUILDIN_DEF(getiteminfo,"ii"), //[Lupus] returns Items Buy / sell Price, etc info
  18858. BUILDIN_DEF(setiteminfo,"iii"), //[Lupus] set Items Buy / sell Price, etc info
  18859. BUILDIN_DEF(getequipcardid,"ii"), //[Lupus] returns CARD ID or other info from CARD slot N of equipped item
  18860. // [zBuffer] List of mathematics commands --->
  18861. BUILDIN_DEF(sqrt,"i"),
  18862. BUILDIN_DEF(pow,"ii"),
  18863. BUILDIN_DEF(distance,"iiii"),
  18864. // <--- [zBuffer] List of mathematics commands
  18865. BUILDIN_DEF(md5,"s"),
  18866. // [zBuffer] List of dynamic var commands --->
  18867. BUILDIN_DEF(getd,"s"),
  18868. BUILDIN_DEF(setd,"sv?"),
  18869. BUILDIN_DEF(callshop,"s?"), // [Skotlex]
  18870. BUILDIN_DEF(npcshopitem,"sii*"), // [Lance]
  18871. BUILDIN_DEF(npcshopadditem,"sii*"),
  18872. BUILDIN_DEF(npcshopdelitem,"si*"),
  18873. BUILDIN_DEF(npcshopattach,"s?"),
  18874. BUILDIN_DEF(equip,"i?"),
  18875. BUILDIN_DEF(autoequip,"ii"),
  18876. BUILDIN_DEF(setbattleflag,"si"),
  18877. BUILDIN_DEF(getbattleflag,"s"),
  18878. BUILDIN_DEF(setitemscript,"is?"), //Set NEW item bonus script. Lupus
  18879. BUILDIN_DEF(disguise,"i?"), //disguise player. Lupus
  18880. BUILDIN_DEF(undisguise,"?"), //undisguise player. Lupus
  18881. BUILDIN_DEF(getmonsterinfo,"ii"), //Lupus
  18882. BUILDIN_DEF(addmonsterdrop,"vii"), //Akinari [Lupus]
  18883. BUILDIN_DEF(delmonsterdrop,"vi"), //Akinari [Lupus]
  18884. BUILDIN_DEF(axtoi,"s"),
  18885. BUILDIN_DEF(query_sql,"s*"),
  18886. BUILDIN_DEF(query_logsql,"s*"),
  18887. BUILDIN_DEF(escape_sql,"v"),
  18888. BUILDIN_DEF(atoi,"s"),
  18889. BUILDIN_DEF(strtol,"si"),
  18890. // [zBuffer] List of player cont commands --->
  18891. BUILDIN_DEF(rid2name,"i"),
  18892. BUILDIN_DEF(pcfollow,"ii"),
  18893. BUILDIN_DEF(pcstopfollow,"i"),
  18894. BUILDIN_DEF(pcblockmove,"ii"),
  18895. // <--- [zBuffer] List of player cont commands
  18896. // [zBuffer] List of unit control commands --->
  18897. BUILDIN_DEF(getunittype,"i"),
  18898. BUILDIN_DEF(getunitname,"i"),
  18899. BUILDIN_DEF(setunitname,"is"),
  18900. BUILDIN_DEF(getunitdata,"i*"),
  18901. BUILDIN_DEF(setunitdata,"iii"),
  18902. BUILDIN_DEF(unitwalk,"iii?"),
  18903. BUILDIN_DEF2(unitwalk,"unitwalkto","ii?"),
  18904. BUILDIN_DEF(unitkill,"i"),
  18905. BUILDIN_DEF(unitwarp,"isii"),
  18906. BUILDIN_DEF(unitattack,"iv?"),
  18907. BUILDIN_DEF(unitstopattack,"i"),
  18908. BUILDIN_DEF(unitstopwalk,"i"),
  18909. BUILDIN_DEF2(pcblockmove,"unitblockmove","ii"),
  18910. BUILDIN_DEF(unittalk,"is"),
  18911. BUILDIN_DEF(unitemote,"ii"),
  18912. BUILDIN_DEF(unitskilluseid,"ivi??"), // originally by Qamera [Celest]
  18913. BUILDIN_DEF(unitskillusepos,"iviii?"), // [Celest]
  18914. // <--- [zBuffer] List of unit control commands
  18915. BUILDIN_DEF(sleep,"i"),
  18916. BUILDIN_DEF(sleep2,"i"),
  18917. BUILDIN_DEF(awake,"s"),
  18918. BUILDIN_DEF(getvariableofnpc,"rs"),
  18919. BUILDIN_DEF(warpportal,"iisii"),
  18920. BUILDIN_DEF2(homunculus_evolution,"homevolution",""), //[orn]
  18921. BUILDIN_DEF2(homunculus_mutate,"hommutate","?"),
  18922. BUILDIN_DEF(morphembryo,""),
  18923. BUILDIN_DEF2(homunculus_shuffle,"homshuffle",""), //[Zephyrus]
  18924. BUILDIN_DEF(checkhomcall,""),
  18925. BUILDIN_DEF(eaclass,"??"), //[Skotlex]
  18926. BUILDIN_DEF(roclass,"i?"), //[Skotlex]
  18927. BUILDIN_DEF(checkvending,"?"),
  18928. BUILDIN_DEF(checkchatting,"?"),
  18929. BUILDIN_DEF(checkidle,"?"),
  18930. BUILDIN_DEF(openmail,"?"),
  18931. BUILDIN_DEF(openauction,"?"),
  18932. BUILDIN_DEF(checkcell,"siii"),
  18933. BUILDIN_DEF(setcell,"siiiiii"),
  18934. BUILDIN_DEF(setwall,"siiiiis"),
  18935. BUILDIN_DEF(delwall,"s"),
  18936. BUILDIN_DEF(searchitem,"rs"),
  18937. BUILDIN_DEF(mercenary_create,"ii"),
  18938. BUILDIN_DEF(mercenary_heal,"ii"),
  18939. BUILDIN_DEF(mercenary_sc_start,"iii"),
  18940. BUILDIN_DEF(mercenary_get_calls,"i"),
  18941. BUILDIN_DEF(mercenary_get_faith,"i"),
  18942. BUILDIN_DEF(mercenary_set_calls,"ii"),
  18943. BUILDIN_DEF(mercenary_set_faith,"ii"),
  18944. BUILDIN_DEF(readbook,"ii"),
  18945. BUILDIN_DEF(setfont,"i"),
  18946. BUILDIN_DEF(areamobuseskill,"siiiiviiiii"),
  18947. BUILDIN_DEF(progressbar,"si"),
  18948. BUILDIN_DEF(pushpc,"ii"),
  18949. BUILDIN_DEF(buyingstore,"i"),
  18950. BUILDIN_DEF(searchstores,"ii"),
  18951. BUILDIN_DEF(showdigit,"i?"),
  18952. // WoE SE
  18953. BUILDIN_DEF(agitstart2,""),
  18954. BUILDIN_DEF(agitend2,""),
  18955. BUILDIN_DEF(agitcheck2,""),
  18956. // BattleGround
  18957. BUILDIN_DEF(waitingroom2bg,"siiss?"),
  18958. BUILDIN_DEF(waitingroom2bg_single,"isiis"),
  18959. BUILDIN_DEF(bg_team_setxy,"iii"),
  18960. BUILDIN_DEF(bg_warp,"isii"),
  18961. BUILDIN_DEF(bg_monster,"isiisi?"),
  18962. BUILDIN_DEF(bg_monster_set_team,"ii"),
  18963. BUILDIN_DEF(bg_leave,"?"),
  18964. BUILDIN_DEF(bg_destroy,"i"),
  18965. BUILDIN_DEF(areapercentheal,"siiiiii"),
  18966. BUILDIN_DEF(bg_get_data,"ii"),
  18967. BUILDIN_DEF(bg_getareausers,"isiiii"),
  18968. BUILDIN_DEF(bg_updatescore,"sii"),
  18969. // Instancing
  18970. BUILDIN_DEF(instance_create,"s"),
  18971. BUILDIN_DEF(instance_destroy,"?"),
  18972. BUILDIN_DEF(instance_id,""),
  18973. BUILDIN_DEF(instance_enter,"s???"),
  18974. BUILDIN_DEF(instance_npcname,"s?"),
  18975. BUILDIN_DEF(instance_mapname,"s?"),
  18976. BUILDIN_DEF(instance_warpall,"sii?"),
  18977. BUILDIN_DEF(instance_announce,"isi?????"),
  18978. BUILDIN_DEF(instance_check_party,"i???"),
  18979. /**
  18980. * 3rd-related
  18981. **/
  18982. BUILDIN_DEF(makerune,"i?"),
  18983. BUILDIN_DEF(checkdragon,"?"),//[Ind]
  18984. BUILDIN_DEF(setdragon,"??"),//[Ind]
  18985. BUILDIN_DEF(ismounting,"?"),//[Ind]
  18986. BUILDIN_DEF(setmounting,"?"),//[Ind]
  18987. BUILDIN_DEF(checkre,"i"),
  18988. /**
  18989. * rAthena and beyond!
  18990. **/
  18991. BUILDIN_DEF(getargcount,""),
  18992. BUILDIN_DEF(getcharip,"?"),
  18993. BUILDIN_DEF(is_function,"s"),
  18994. BUILDIN_DEF(get_revision,""),
  18995. BUILDIN_DEF(get_githash,""),
  18996. BUILDIN_DEF(freeloop,"?"),
  18997. BUILDIN_DEF(getrandgroupitem,"i??"),
  18998. BUILDIN_DEF(cleanmap,"s"),
  18999. BUILDIN_DEF2(cleanmap,"cleanarea","siiii"),
  19000. BUILDIN_DEF(npcskill,"viii"),
  19001. BUILDIN_DEF(consumeitem,"v?"),
  19002. BUILDIN_DEF(delequip,"i?"),
  19003. BUILDIN_DEF(breakequip,"i?"),
  19004. BUILDIN_DEF(sit,"?"),
  19005. BUILDIN_DEF(stand,"?"),
  19006. //@commands (script based)
  19007. BUILDIN_DEF(bindatcmd, "ss??"),
  19008. BUILDIN_DEF(unbindatcmd, "s"),
  19009. BUILDIN_DEF(useatcmd, "s"),
  19010. //Quest Log System [Inkfish]
  19011. BUILDIN_DEF(questinfo, "ii??"),
  19012. BUILDIN_DEF(setquest, "i?"),
  19013. BUILDIN_DEF(erasequest, "i?"),
  19014. BUILDIN_DEF(completequest, "i?"),
  19015. BUILDIN_DEF(checkquest, "i??"),
  19016. BUILDIN_DEF(isbegin_quest,"i?"),
  19017. BUILDIN_DEF(changequest, "ii?"),
  19018. BUILDIN_DEF(showevent, "i??"),
  19019. //Bound items [Xantara] & [Akinari]
  19020. BUILDIN_DEF2(getitem,"getitembound","vii?"),
  19021. BUILDIN_DEF2(getitem2,"getitembound2","viiiiiiiii?"),
  19022. BUILDIN_DEF(countbound, "??"),
  19023. // Party related
  19024. BUILDIN_DEF(party_create,"s???"),
  19025. BUILDIN_DEF(party_addmember,"ii"),
  19026. BUILDIN_DEF(party_delmember,"??"),
  19027. BUILDIN_DEF(party_changeleader,"ii"),
  19028. BUILDIN_DEF(party_changeoption,"iii"),
  19029. BUILDIN_DEF(party_destroy,"i"),
  19030. BUILDIN_DEF(is_clientver,"ii?"),
  19031. BUILDIN_DEF2(montransform, "transform", "vi?????"), // Monster Transform [malufett/Hercules]
  19032. BUILDIN_DEF(vip_status,"i?"),
  19033. BUILDIN_DEF(vip_time,"i?"),
  19034. BUILDIN_DEF(bonus_script,"si????"),
  19035. BUILDIN_DEF(bonus_script_clear,"??"),
  19036. BUILDIN_DEF(getgroupitem,"i?"),
  19037. BUILDIN_DEF(enable_command,""),
  19038. BUILDIN_DEF(disable_command,""),
  19039. BUILDIN_DEF(getguildmember,"i??"),
  19040. BUILDIN_DEF(addspiritball,"ii?"),
  19041. BUILDIN_DEF(delspiritball,"i?"),
  19042. BUILDIN_DEF(countspiritball,"?"),
  19043. BUILDIN_DEF(mergeitem,"?"),
  19044. BUILDIN_DEF(mergeitem2,"??"),
  19045. BUILDIN_DEF(npcshopupdate,"sii?"),
  19046. BUILDIN_DEF(getattachedrid,""),
  19047. BUILDIN_DEF(getvar,"vi"),
  19048. BUILDIN_DEF(showscript,"s?"),
  19049. BUILDIN_DEF(ignoretimeout,"i?"),
  19050. BUILDIN_DEF(geteleminfo,"i?"),
  19051. BUILDIN_DEF(setquestinfo_level,"iii"),
  19052. BUILDIN_DEF(setquestinfo_req,"iii*"),
  19053. BUILDIN_DEF(setquestinfo_job,"ii*"),
  19054. BUILDIN_DEF(opendressroom,"i?"),
  19055. BUILDIN_DEF(navigateto,"s???????"),
  19056. BUILDIN_DEF(adopt,"vv"),
  19057. BUILDIN_DEF(getexp2,"ii?"),
  19058. #include "../custom/script_def.inc"
  19059. {NULL,NULL,NULL},
  19060. };