script.c 574 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112131131311413115131161311713118131191312013121131221312313124131251312613127131281312913130131311313213133131341313513136131371313813139131401314113142131431314413145131461314713148131491315013151131521315313154131551315613157131581315913160131611316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208132091321013211132121321313214132151321613217132181321913220132211322213223132241322513226132271322813229132301323113232132331323413235132361323713238132391324013241132421324313244132451324613247132481324913250132511325213253132541325513256132571325813259132601326113262132631326413265132661326713268132691327013271132721327313274132751327613277132781327913280132811328213283132841328513286132871328813289132901329113292132931329413295132961329713298132991330013301133021330313304133051330613307133081330913310133111331213313133141331513316133171331813319133201332113322133231332413325133261332713328133291333013331133321333313334133351333613337133381333913340133411334213343133441334513346133471334813349133501335113352133531335413355133561335713358133591336013361133621336313364133651336613367133681336913370133711337213373133741337513376133771337813379133801338113382133831338413385133861338713388133891339013391133921339313394133951339613397133981339913400134011340213403134041340513406134071340813409134101341113412134131341413415134161341713418134191342013421134221342313424134251342613427134281342913430134311343213433134341343513436134371343813439134401344113442134431344413445134461344713448134491345013451134521345313454134551345613457134581345913460134611346213463134641346513466134671346813469134701347113472134731347413475134761347713478134791348013481134821348313484134851348613487134881348913490134911349213493134941349513496134971349813499135001350113502135031350413505135061350713508135091351013511135121351313514135151351613517135181351913520135211352213523135241352513526135271352813529135301353113532135331353413535135361353713538135391354013541135421354313544135451354613547135481354913550135511355213553135541355513556135571355813559135601356113562135631356413565135661356713568135691357013571135721357313574135751357613577135781357913580135811358213583135841358513586135871358813589135901359113592135931359413595135961359713598135991360013601136021360313604136051360613607136081360913610136111361213613136141361513616136171361813619136201362113622136231362413625136261362713628136291363013631136321363313634136351363613637136381363913640136411364213643136441364513646136471364813649136501365113652136531365413655136561365713658136591366013661136621366313664136651366613667136681366913670136711367213673136741367513676136771367813679136801368113682136831368413685136861368713688136891369013691136921369313694136951369613697136981369913700137011370213703137041370513706137071370813709137101371113712137131371413715137161371713718137191372013721137221372313724137251372613727137281372913730137311373213733137341373513736137371373813739137401374113742137431374413745137461374713748137491375013751137521375313754137551375613757137581375913760137611376213763137641376513766137671376813769137701377113772137731377413775137761377713778137791378013781137821378313784137851378613787137881378913790137911379213793137941379513796137971379813799138001380113802138031380413805138061380713808138091381013811138121381313814138151381613817138181381913820138211382213823138241382513826138271382813829138301383113832138331383413835138361383713838138391384013841138421384313844138451384613847138481384913850138511385213853138541385513856138571385813859138601386113862138631386413865138661386713868138691387013871138721387313874138751387613877138781387913880138811388213883138841388513886138871388813889138901389113892138931389413895138961389713898138991390013901139021390313904139051390613907139081390913910139111391213913139141391513916139171391813919139201392113922139231392413925139261392713928139291393013931139321393313934139351393613937139381393913940139411394213943139441394513946139471394813949139501395113952139531395413955139561395713958139591396013961139621396313964139651396613967139681396913970139711397213973139741397513976139771397813979139801398113982139831398413985139861398713988139891399013991139921399313994139951399613997139981399914000140011400214003140041400514006140071400814009140101401114012140131401414015140161401714018140191402014021140221402314024140251402614027140281402914030140311403214033140341403514036140371403814039140401404114042140431404414045140461404714048140491405014051140521405314054140551405614057140581405914060140611406214063140641406514066140671406814069140701407114072140731407414075140761407714078140791408014081140821408314084140851408614087140881408914090140911409214093140941409514096140971409814099141001410114102141031410414105141061410714108141091411014111141121411314114141151411614117141181411914120141211412214123141241412514126141271412814129141301413114132141331413414135141361413714138141391414014141141421414314144141451414614147141481414914150141511415214153141541415514156141571415814159141601416114162141631416414165141661416714168141691417014171141721417314174141751417614177141781417914180141811418214183141841418514186141871418814189141901419114192141931419414195141961419714198141991420014201142021420314204142051420614207142081420914210142111421214213142141421514216142171421814219142201422114222142231422414225142261422714228142291423014231142321423314234142351423614237142381423914240142411424214243142441424514246142471424814249142501425114252142531425414255142561425714258142591426014261142621426314264142651426614267142681426914270142711427214273142741427514276142771427814279142801428114282142831428414285142861428714288142891429014291142921429314294142951429614297142981429914300143011430214303143041430514306143071430814309143101431114312143131431414315143161431714318143191432014321143221432314324143251432614327143281432914330143311433214333143341433514336143371433814339143401434114342143431434414345143461434714348143491435014351143521435314354143551435614357143581435914360143611436214363143641436514366143671436814369143701437114372143731437414375143761437714378143791438014381143821438314384143851438614387143881438914390143911439214393143941439514396143971439814399144001440114402144031440414405144061440714408144091441014411144121441314414144151441614417144181441914420144211442214423144241442514426144271442814429144301443114432144331443414435144361443714438144391444014441144421444314444144451444614447144481444914450144511445214453144541445514456144571445814459144601446114462144631446414465144661446714468144691447014471144721447314474144751447614477144781447914480144811448214483144841448514486144871448814489144901449114492144931449414495144961449714498144991450014501145021450314504145051450614507145081450914510145111451214513145141451514516145171451814519145201452114522145231452414525145261452714528145291453014531145321453314534145351453614537145381453914540145411454214543145441454514546145471454814549145501455114552145531455414555145561455714558145591456014561145621456314564145651456614567145681456914570145711457214573145741457514576145771457814579145801458114582145831458414585145861458714588145891459014591145921459314594145951459614597145981459914600146011460214603146041460514606146071460814609146101461114612146131461414615146161461714618146191462014621146221462314624146251462614627146281462914630146311463214633146341463514636146371463814639146401464114642146431464414645146461464714648146491465014651146521465314654146551465614657146581465914660146611466214663146641466514666146671466814669146701467114672146731467414675146761467714678146791468014681146821468314684146851468614687146881468914690146911469214693146941469514696146971469814699147001470114702147031470414705147061470714708147091471014711147121471314714147151471614717147181471914720147211472214723147241472514726147271472814729147301473114732147331473414735147361473714738147391474014741147421474314744147451474614747147481474914750147511475214753147541475514756147571475814759147601476114762147631476414765147661476714768147691477014771147721477314774147751477614777147781477914780147811478214783147841478514786147871478814789147901479114792147931479414795147961479714798147991480014801148021480314804148051480614807148081480914810148111481214813148141481514816148171481814819148201482114822148231482414825148261482714828148291483014831148321483314834148351483614837148381483914840148411484214843148441484514846148471484814849148501485114852148531485414855148561485714858148591486014861148621486314864148651486614867148681486914870148711487214873148741487514876148771487814879148801488114882148831488414885148861488714888148891489014891148921489314894148951489614897148981489914900149011490214903149041490514906149071490814909149101491114912149131491414915149161491714918149191492014921149221492314924149251492614927149281492914930149311493214933149341493514936149371493814939149401494114942149431494414945149461494714948149491495014951149521495314954149551495614957149581495914960149611496214963149641496514966149671496814969149701497114972149731497414975149761497714978149791498014981149821498314984149851498614987149881498914990149911499214993149941499514996149971499814999150001500115002150031500415005150061500715008150091501015011150121501315014150151501615017150181501915020150211502215023150241502515026150271502815029150301503115032150331503415035150361503715038150391504015041150421504315044150451504615047150481504915050150511505215053150541505515056150571505815059150601506115062150631506415065150661506715068150691507015071150721507315074150751507615077150781507915080150811508215083150841508515086150871508815089150901509115092150931509415095150961509715098150991510015101151021510315104151051510615107151081510915110151111511215113151141511515116151171511815119151201512115122151231512415125151261512715128151291513015131151321513315134151351513615137151381513915140151411514215143151441514515146151471514815149151501515115152151531515415155151561515715158151591516015161151621516315164151651516615167151681516915170151711517215173151741517515176151771517815179151801518115182151831518415185151861518715188151891519015191151921519315194151951519615197151981519915200152011520215203152041520515206152071520815209152101521115212152131521415215152161521715218152191522015221152221522315224152251522615227152281522915230152311523215233152341523515236152371523815239152401524115242152431524415245152461524715248152491525015251152521525315254152551525615257152581525915260152611526215263152641526515266152671526815269152701527115272152731527415275152761527715278152791528015281152821528315284152851528615287152881528915290152911529215293152941529515296152971529815299153001530115302153031530415305153061530715308153091531015311153121531315314153151531615317153181531915320153211532215323153241532515326153271532815329153301533115332153331533415335153361533715338153391534015341153421534315344153451534615347153481534915350153511535215353153541535515356153571535815359153601536115362153631536415365153661536715368153691537015371153721537315374153751537615377153781537915380153811538215383153841538515386153871538815389153901539115392153931539415395153961539715398153991540015401154021540315404154051540615407154081540915410154111541215413154141541515416154171541815419154201542115422154231542415425154261542715428154291543015431154321543315434154351543615437154381543915440154411544215443154441544515446154471544815449154501545115452154531545415455154561545715458154591546015461154621546315464154651546615467154681546915470154711547215473154741547515476154771547815479154801548115482154831548415485154861548715488154891549015491154921549315494154951549615497154981549915500155011550215503155041550515506155071550815509155101551115512155131551415515155161551715518155191552015521155221552315524155251552615527155281552915530155311553215533155341553515536155371553815539155401554115542155431554415545155461554715548155491555015551155521555315554155551555615557155581555915560155611556215563155641556515566155671556815569155701557115572155731557415575155761557715578155791558015581155821558315584155851558615587155881558915590155911559215593155941559515596155971559815599156001560115602156031560415605156061560715608156091561015611156121561315614156151561615617156181561915620156211562215623156241562515626156271562815629156301563115632156331563415635156361563715638156391564015641156421564315644156451564615647156481564915650156511565215653156541565515656156571565815659156601566115662156631566415665156661566715668156691567015671156721567315674156751567615677156781567915680156811568215683156841568515686156871568815689156901569115692156931569415695156961569715698156991570015701157021570315704157051570615707157081570915710157111571215713157141571515716157171571815719157201572115722157231572415725157261572715728157291573015731157321573315734157351573615737157381573915740157411574215743157441574515746157471574815749157501575115752157531575415755157561575715758157591576015761157621576315764157651576615767157681576915770157711577215773157741577515776157771577815779157801578115782157831578415785157861578715788157891579015791157921579315794157951579615797157981579915800158011580215803158041580515806158071580815809158101581115812158131581415815158161581715818158191582015821158221582315824158251582615827158281582915830158311583215833158341583515836158371583815839158401584115842158431584415845158461584715848158491585015851158521585315854158551585615857158581585915860158611586215863158641586515866158671586815869158701587115872158731587415875158761587715878158791588015881158821588315884158851588615887158881588915890158911589215893158941589515896158971589815899159001590115902159031590415905159061590715908159091591015911159121591315914159151591615917159181591915920159211592215923159241592515926159271592815929159301593115932159331593415935159361593715938159391594015941159421594315944159451594615947159481594915950159511595215953159541595515956159571595815959159601596115962159631596415965159661596715968159691597015971159721597315974159751597615977159781597915980159811598215983159841598515986159871598815989159901599115992159931599415995159961599715998159991600016001160021600316004160051600616007160081600916010160111601216013160141601516016160171601816019160201602116022160231602416025160261602716028160291603016031160321603316034160351603616037160381603916040160411604216043160441604516046160471604816049160501605116052160531605416055160561605716058160591606016061160621606316064160651606616067160681606916070160711607216073160741607516076160771607816079160801608116082160831608416085160861608716088160891609016091160921609316094160951609616097160981609916100161011610216103161041610516106161071610816109161101611116112161131611416115161161611716118161191612016121161221612316124161251612616127161281612916130161311613216133161341613516136161371613816139161401614116142161431614416145161461614716148161491615016151161521615316154161551615616157161581615916160161611616216163161641616516166161671616816169161701617116172161731617416175161761617716178161791618016181161821618316184161851618616187161881618916190161911619216193161941619516196161971619816199162001620116202162031620416205162061620716208162091621016211162121621316214162151621616217162181621916220162211622216223162241622516226162271622816229162301623116232162331623416235162361623716238162391624016241162421624316244162451624616247162481624916250162511625216253162541625516256162571625816259162601626116262162631626416265162661626716268162691627016271162721627316274162751627616277162781627916280162811628216283162841628516286162871628816289162901629116292162931629416295162961629716298162991630016301163021630316304163051630616307163081630916310163111631216313163141631516316163171631816319163201632116322163231632416325163261632716328163291633016331163321633316334163351633616337163381633916340163411634216343163441634516346163471634816349163501635116352163531635416355163561635716358163591636016361163621636316364163651636616367163681636916370163711637216373163741637516376163771637816379163801638116382163831638416385163861638716388163891639016391163921639316394163951639616397163981639916400164011640216403164041640516406164071640816409164101641116412164131641416415164161641716418164191642016421164221642316424164251642616427164281642916430164311643216433164341643516436164371643816439164401644116442164431644416445164461644716448164491645016451164521645316454164551645616457164581645916460164611646216463164641646516466164671646816469164701647116472164731647416475164761647716478164791648016481164821648316484164851648616487164881648916490164911649216493164941649516496164971649816499165001650116502165031650416505165061650716508165091651016511165121651316514165151651616517165181651916520165211652216523165241652516526165271652816529165301653116532165331653416535165361653716538165391654016541165421654316544165451654616547165481654916550165511655216553165541655516556165571655816559165601656116562165631656416565165661656716568165691657016571165721657316574165751657616577165781657916580165811658216583165841658516586165871658816589165901659116592165931659416595165961659716598165991660016601166021660316604166051660616607166081660916610166111661216613166141661516616166171661816619166201662116622166231662416625166261662716628166291663016631166321663316634166351663616637166381663916640166411664216643166441664516646166471664816649166501665116652166531665416655166561665716658166591666016661166621666316664166651666616667166681666916670166711667216673166741667516676166771667816679166801668116682166831668416685166861668716688166891669016691166921669316694166951669616697166981669916700167011670216703167041670516706167071670816709167101671116712167131671416715167161671716718167191672016721167221672316724167251672616727167281672916730167311673216733167341673516736167371673816739167401674116742167431674416745167461674716748167491675016751167521675316754167551675616757167581675916760167611676216763167641676516766167671676816769167701677116772167731677416775167761677716778167791678016781167821678316784167851678616787167881678916790167911679216793167941679516796167971679816799168001680116802168031680416805168061680716808168091681016811168121681316814168151681616817168181681916820168211682216823168241682516826168271682816829168301683116832168331683416835168361683716838168391684016841168421684316844168451684616847168481684916850168511685216853168541685516856168571685816859168601686116862168631686416865168661686716868168691687016871168721687316874168751687616877168781687916880168811688216883168841688516886168871688816889168901689116892168931689416895168961689716898168991690016901169021690316904169051690616907169081690916910169111691216913169141691516916169171691816919169201692116922169231692416925169261692716928169291693016931169321693316934169351693616937169381693916940169411694216943169441694516946169471694816949169501695116952169531695416955169561695716958169591696016961169621696316964169651696616967169681696916970169711697216973169741697516976169771697816979169801698116982169831698416985169861698716988169891699016991169921699316994169951699616997169981699917000170011700217003170041700517006170071700817009170101701117012170131701417015170161701717018170191702017021170221702317024170251702617027170281702917030170311703217033170341703517036170371703817039170401704117042170431704417045170461704717048170491705017051170521705317054170551705617057170581705917060170611706217063170641706517066170671706817069170701707117072170731707417075170761707717078170791708017081170821708317084170851708617087170881708917090170911709217093170941709517096170971709817099171001710117102171031710417105171061710717108171091711017111171121711317114171151711617117171181711917120171211712217123171241712517126171271712817129171301713117132171331713417135171361713717138171391714017141171421714317144171451714617147171481714917150171511715217153171541715517156171571715817159171601716117162171631716417165171661716717168171691717017171171721717317174171751717617177171781717917180171811718217183171841718517186171871718817189171901719117192171931719417195171961719717198171991720017201172021720317204172051720617207172081720917210172111721217213172141721517216172171721817219172201722117222172231722417225172261722717228172291723017231172321723317234172351723617237172381723917240172411724217243172441724517246172471724817249172501725117252172531725417255172561725717258172591726017261172621726317264172651726617267172681726917270172711727217273172741727517276172771727817279172801728117282172831728417285172861728717288172891729017291172921729317294172951729617297172981729917300173011730217303173041730517306173071730817309173101731117312173131731417315173161731717318173191732017321173221732317324173251732617327173281732917330173311733217333173341733517336173371733817339173401734117342173431734417345173461734717348173491735017351173521735317354173551735617357173581735917360173611736217363173641736517366173671736817369173701737117372173731737417375173761737717378173791738017381173821738317384173851738617387173881738917390173911739217393173941739517396173971739817399174001740117402174031740417405174061740717408174091741017411174121741317414174151741617417174181741917420174211742217423174241742517426174271742817429174301743117432174331743417435174361743717438174391744017441174421744317444174451744617447174481744917450174511745217453174541745517456174571745817459174601746117462174631746417465174661746717468174691747017471174721747317474174751747617477174781747917480174811748217483174841748517486174871748817489174901749117492174931749417495174961749717498174991750017501175021750317504175051750617507175081750917510175111751217513175141751517516175171751817519175201752117522175231752417525175261752717528175291753017531175321753317534175351753617537175381753917540175411754217543175441754517546175471754817549175501755117552175531755417555175561755717558175591756017561175621756317564175651756617567175681756917570175711757217573175741757517576175771757817579175801758117582175831758417585175861758717588175891759017591175921759317594175951759617597175981759917600176011760217603176041760517606176071760817609176101761117612176131761417615176161761717618176191762017621176221762317624176251762617627176281762917630176311763217633176341763517636176371763817639176401764117642176431764417645176461764717648176491765017651176521765317654176551765617657176581765917660176611766217663176641766517666176671766817669176701767117672176731767417675176761767717678176791768017681176821768317684176851768617687176881768917690176911769217693176941769517696176971769817699177001770117702177031770417705177061770717708177091771017711177121771317714177151771617717177181771917720177211772217723177241772517726177271772817729177301773117732177331773417735177361773717738177391774017741177421774317744177451774617747177481774917750177511775217753177541775517756177571775817759177601776117762177631776417765177661776717768177691777017771177721777317774177751777617777177781777917780177811778217783177841778517786177871778817789177901779117792177931779417795177961779717798177991780017801178021780317804178051780617807178081780917810178111781217813178141781517816178171781817819178201782117822178231782417825178261782717828178291783017831178321783317834178351783617837178381783917840178411784217843178441784517846178471784817849178501785117852178531785417855178561785717858178591786017861178621786317864178651786617867178681786917870178711787217873178741787517876178771787817879178801788117882178831788417885178861788717888178891789017891178921789317894178951789617897178981789917900179011790217903179041790517906179071790817909179101791117912179131791417915179161791717918179191792017921179221792317924179251792617927179281792917930179311793217933179341793517936179371793817939179401794117942179431794417945179461794717948179491795017951179521795317954179551795617957179581795917960179611796217963179641796517966179671796817969179701797117972179731797417975179761797717978179791798017981179821798317984179851798617987179881798917990179911799217993179941799517996179971799817999180001800118002180031800418005180061800718008180091801018011180121801318014180151801618017180181801918020180211802218023180241802518026180271802818029180301803118032180331803418035180361803718038180391804018041180421804318044180451804618047180481804918050180511805218053180541805518056180571805818059180601806118062180631806418065180661806718068180691807018071180721807318074180751807618077180781807918080180811808218083180841808518086180871808818089180901809118092180931809418095180961809718098180991810018101181021810318104181051810618107181081810918110181111811218113181141811518116181171811818119181201812118122181231812418125181261812718128181291813018131181321813318134181351813618137181381813918140181411814218143181441814518146181471814818149181501815118152181531815418155181561815718158181591816018161181621816318164181651816618167181681816918170181711817218173181741817518176181771817818179181801818118182181831818418185181861818718188181891819018191181921819318194181951819618197181981819918200182011820218203182041820518206182071820818209182101821118212182131821418215182161821718218182191822018221182221822318224182251822618227182281822918230182311823218233182341823518236182371823818239182401824118242182431824418245182461824718248182491825018251182521825318254182551825618257182581825918260182611826218263182641826518266182671826818269182701827118272182731827418275182761827718278182791828018281182821828318284182851828618287182881828918290182911829218293182941829518296182971829818299183001830118302183031830418305183061830718308183091831018311183121831318314183151831618317183181831918320183211832218323183241832518326183271832818329183301833118332183331833418335183361833718338183391834018341183421834318344183451834618347183481834918350183511835218353183541835518356183571835818359183601836118362183631836418365183661836718368183691837018371183721837318374183751837618377183781837918380183811838218383183841838518386183871838818389183901839118392183931839418395183961839718398183991840018401184021840318404184051840618407184081840918410184111841218413184141841518416184171841818419184201842118422184231842418425184261842718428184291843018431184321843318434184351843618437184381843918440184411844218443184441844518446184471844818449184501845118452184531845418455184561845718458184591846018461184621846318464184651846618467184681846918470184711847218473184741847518476184771847818479184801848118482184831848418485184861848718488184891849018491184921849318494184951849618497184981849918500185011850218503185041850518506185071850818509185101851118512185131851418515185161851718518185191852018521185221852318524185251852618527185281852918530185311853218533185341853518536185371853818539185401854118542185431854418545185461854718548185491855018551185521855318554185551855618557185581855918560185611856218563185641856518566185671856818569185701857118572185731857418575185761857718578185791858018581185821858318584185851858618587185881858918590185911859218593185941859518596185971859818599186001860118602186031860418605186061860718608186091861018611186121861318614186151861618617186181861918620186211862218623186241862518626186271862818629186301863118632186331863418635186361863718638186391864018641186421864318644186451864618647186481864918650186511865218653186541865518656186571865818659186601866118662186631866418665186661866718668186691867018671186721867318674186751867618677186781867918680186811868218683186841868518686186871868818689186901869118692186931869418695186961869718698186991870018701187021870318704187051870618707187081870918710187111871218713187141871518716187171871818719187201872118722187231872418725187261872718728187291873018731187321873318734187351873618737187381873918740187411874218743187441874518746187471874818749187501875118752187531875418755187561875718758187591876018761187621876318764187651876618767187681876918770187711877218773187741877518776187771877818779187801878118782187831878418785187861878718788187891879018791187921879318794187951879618797187981879918800188011880218803188041880518806188071880818809188101881118812188131881418815188161881718818188191882018821188221882318824188251882618827188281882918830188311883218833188341883518836188371883818839188401884118842188431884418845188461884718848188491885018851188521885318854188551885618857188581885918860188611886218863188641886518866188671886818869188701887118872188731887418875188761887718878188791888018881188821888318884188851888618887188881888918890188911889218893188941889518896188971889818899189001890118902189031890418905189061890718908189091891018911189121891318914189151891618917189181891918920189211892218923189241892518926189271892818929189301893118932189331893418935189361893718938189391894018941189421894318944189451894618947189481894918950189511895218953189541895518956189571895818959189601896118962189631896418965189661896718968189691897018971189721897318974189751897618977189781897918980189811898218983189841898518986189871898818989189901899118992189931899418995189961899718998189991900019001190021900319004190051900619007190081900919010190111901219013190141901519016190171901819019190201902119022190231902419025190261902719028190291903019031190321903319034190351903619037190381903919040190411904219043190441904519046190471904819049190501905119052190531905419055190561905719058190591906019061190621906319064190651906619067190681906919070190711907219073190741907519076190771907819079190801908119082190831908419085190861908719088190891909019091190921909319094190951909619097190981909919100191011910219103191041910519106191071910819109191101911119112191131911419115191161911719118191191912019121191221912319124191251912619127191281912919130191311913219133191341913519136191371913819139191401914119142191431914419145191461914719148191491915019151191521915319154191551915619157191581915919160191611916219163191641916519166191671916819169191701917119172191731917419175191761917719178191791918019181191821918319184191851918619187191881918919190191911919219193191941919519196191971919819199192001920119202192031920419205192061920719208192091921019211192121921319214192151921619217192181921919220192211922219223192241922519226192271922819229192301923119232192331923419235192361923719238192391924019241192421924319244192451924619247192481924919250192511925219253192541925519256192571925819259192601926119262192631926419265192661926719268192691927019271192721927319274192751927619277192781927919280192811928219283192841928519286192871928819289192901929119292192931929419295192961929719298192991930019301193021930319304193051930619307193081930919310193111931219313193141931519316193171931819319193201932119322193231932419325193261932719328193291933019331193321933319334193351933619337193381933919340193411934219343193441934519346193471934819349193501935119352193531935419355193561935719358193591936019361193621936319364193651936619367193681936919370193711937219373193741937519376193771937819379193801938119382193831938419385193861938719388193891939019391193921939319394193951939619397193981939919400194011940219403194041940519406194071940819409194101941119412194131941419415194161941719418194191942019421194221942319424194251942619427194281942919430194311943219433194341943519436194371943819439194401944119442194431944419445194461944719448194491945019451194521945319454194551945619457194581945919460194611946219463194641946519466194671946819469194701947119472194731947419475194761947719478194791948019481194821948319484194851948619487194881948919490194911949219493194941949519496194971949819499195001950119502195031950419505195061950719508195091951019511195121951319514195151951619517195181951919520195211952219523195241952519526195271952819529195301953119532195331953419535195361953719538195391954019541195421954319544195451954619547195481954919550195511955219553195541955519556195571955819559195601956119562195631956419565195661956719568195691957019571195721957319574195751957619577195781957919580195811958219583195841958519586195871958819589195901959119592195931959419595195961959719598195991960019601196021960319604196051960619607196081960919610196111961219613196141961519616196171961819619196201962119622196231962419625196261962719628196291963019631196321963319634196351963619637196381963919640196411964219643196441964519646196471964819649196501965119652196531965419655196561965719658196591966019661196621966319664196651966619667196681966919670196711967219673196741967519676196771967819679196801968119682196831968419685196861968719688196891969019691196921969319694196951969619697196981969919700197011970219703197041970519706197071970819709197101971119712197131971419715197161971719718197191972019721197221972319724197251972619727197281972919730197311973219733197341973519736197371973819739197401974119742197431974419745197461974719748197491975019751197521975319754197551975619757197581975919760197611976219763197641976519766197671976819769197701977119772197731977419775197761977719778197791978019781197821978319784197851978619787197881978919790197911979219793197941979519796197971979819799198001980119802198031980419805198061980719808198091981019811198121981319814198151981619817198181981919820198211982219823198241982519826198271982819829198301983119832198331983419835198361983719838198391984019841198421984319844198451984619847198481984919850198511985219853198541985519856198571985819859198601986119862198631986419865198661986719868198691987019871198721987319874198751987619877198781987919880198811988219883198841988519886198871988819889198901989119892198931989419895198961989719898198991990019901199021990319904199051990619907199081990919910199111991219913199141991519916199171991819919199201992119922199231992419925199261992719928199291993019931199321993319934199351993619937199381993919940199411994219943199441994519946199471994819949199501995119952199531995419955199561995719958199591996019961199621996319964199651996619967199681996919970199711997219973199741997519976199771997819979199801998119982199831998419985199861998719988199891999019991199921999319994199951999619997199981999920000200012000220003200042000520006200072000820009200102001120012200132001420015200162001720018200192002020021200222002320024200252002620027200282002920030200312003220033200342003520036200372003820039200402004120042200432004420045200462004720048200492005020051200522005320054200552005620057200582005920060200612006220063200642006520066200672006820069200702007120072200732007420075200762007720078200792008020081200822008320084200852008620087200882008920090200912009220093200942009520096200972009820099201002010120102201032010420105201062010720108201092011020111201122011320114201152011620117201182011920120201212012220123201242012520126201272012820129201302013120132201332013420135201362013720138201392014020141201422014320144201452014620147201482014920150201512015220153201542015520156201572015820159201602016120162201632016420165201662016720168201692017020171201722017320174201752017620177201782017920180201812018220183201842018520186201872018820189201902019120192201932019420195201962019720198201992020020201202022020320204202052020620207202082020920210202112021220213202142021520216202172021820219202202022120222202232022420225202262022720228202292023020231202322023320234202352023620237202382023920240202412024220243202442024520246202472024820249202502025120252202532025420255202562025720258202592026020261202622026320264202652026620267202682026920270202712027220273202742027520276202772027820279202802028120282202832028420285202862028720288202892029020291202922029320294202952029620297202982029920300203012030220303203042030520306203072030820309203102031120312203132031420315203162031720318203192032020321203222032320324203252032620327203282032920330203312033220333203342033520336203372033820339203402034120342203432034420345203462034720348203492035020351203522035320354203552035620357203582035920360203612036220363203642036520366203672036820369203702037120372203732037420375203762037720378203792038020381203822038320384203852038620387203882038920390203912039220393203942039520396203972039820399204002040120402204032040420405204062040720408204092041020411204122041320414204152041620417204182041920420204212042220423204242042520426204272042820429204302043120432204332043420435204362043720438204392044020441204422044320444204452044620447204482044920450204512045220453204542045520456204572045820459204602046120462204632046420465204662046720468204692047020471204722047320474204752047620477204782047920480204812048220483204842048520486204872048820489204902049120492204932049420495204962049720498204992050020501205022050320504205052050620507205082050920510205112051220513205142051520516205172051820519205202052120522205232052420525205262052720528205292053020531205322053320534205352053620537205382053920540205412054220543205442054520546205472054820549205502055120552205532055420555205562055720558205592056020561205622056320564205652056620567205682056920570205712057220573205742057520576205772057820579205802058120582205832058420585205862058720588205892059020591205922059320594205952059620597205982059920600206012060220603206042060520606206072060820609206102061120612206132061420615206162061720618206192062020621206222062320624206252062620627206282062920630206312063220633206342063520636206372063820639206402064120642206432064420645206462064720648206492065020651206522065320654206552065620657206582065920660206612066220663206642066520666206672066820669206702067120672206732067420675206762067720678206792068020681206822068320684206852068620687206882068920690206912069220693206942069520696206972069820699207002070120702207032070420705207062070720708207092071020711207122071320714207152071620717207182071920720207212072220723207242072520726207272072820729207302073120732207332073420735207362073720738207392074020741207422074320744207452074620747207482074920750207512075220753207542075520756207572075820759207602076120762207632076420765207662076720768207692077020771207722077320774207752077620777207782077920780207812078220783207842078520786207872078820789207902079120792207932079420795207962079720798207992080020801208022080320804208052080620807208082080920810208112081220813208142081520816208172081820819208202082120822208232082420825208262082720828208292083020831208322083320834208352083620837208382083920840208412084220843208442084520846208472084820849208502085120852208532085420855208562085720858208592086020861208622086320864208652086620867208682086920870208712087220873208742087520876208772087820879208802088120882208832088420885208862088720888208892089020891208922089320894208952089620897208982089920900209012090220903209042090520906209072090820909209102091120912209132091420915209162091720918209192092020921209222092320924209252092620927209282092920930209312093220933209342093520936209372093820939209402094120942209432094420945209462094720948209492095020951209522095320954209552095620957209582095920960209612096220963209642096520966209672096820969209702097120972209732097420975209762097720978209792098020981209822098320984209852098620987209882098920990209912099220993209942099520996209972099820999210002100121002210032100421005210062100721008210092101021011210122101321014210152101621017210182101921020210212102221023210242102521026210272102821029210302103121032210332103421035210362103721038210392104021041210422104321044210452104621047210482104921050210512105221053210542105521056210572105821059210602106121062210632106421065210662106721068210692107021071210722107321074210752107621077210782107921080210812108221083210842108521086210872108821089210902109121092210932109421095210962109721098210992110021101211022110321104211052110621107211082110921110211112111221113211142111521116211172111821119211202112121122211232112421125211262112721128211292113021131211322113321134211352113621137211382113921140211412114221143211442114521146211472114821149211502115121152211532115421155211562115721158211592116021161211622116321164211652116621167211682116921170211712117221173211742117521176211772117821179211802118121182211832118421185211862118721188211892119021191211922119321194211952119621197211982119921200212012120221203212042120521206212072120821209212102121121212212132121421215212162121721218212192122021221212222122321224212252122621227212282122921230212312123221233212342123521236212372123821239212402124121242212432124421245212462124721248212492125021251212522125321254212552125621257212582125921260212612126221263212642126521266212672126821269212702127121272212732127421275212762127721278212792128021281212822128321284212852128621287212882128921290212912129221293212942129521296212972129821299213002130121302213032130421305213062130721308213092131021311213122131321314213152131621317213182131921320213212132221323213242132521326213272132821329213302133121332213332133421335213362133721338213392134021341213422134321344213452134621347213482134921350213512135221353213542135521356213572135821359213602136121362213632136421365213662136721368213692137021371213722137321374213752137621377213782137921380213812138221383213842138521386213872138821389213902139121392213932139421395213962139721398213992140021401214022140321404214052140621407214082140921410214112141221413214142141521416214172141821419214202142121422214232142421425214262142721428214292143021431214322143321434214352143621437214382143921440214412144221443214442144521446214472144821449214502145121452214532145421455214562145721458214592146021461214622146321464214652146621467214682146921470214712147221473214742147521476214772147821479214802148121482214832148421485214862148721488214892149021491214922149321494214952149621497214982149921500215012150221503215042150521506215072150821509215102151121512215132151421515215162151721518215192152021521215222152321524215252152621527215282152921530215312153221533215342153521536215372153821539215402154121542215432154421545215462154721548215492155021551215522155321554215552155621557215582155921560215612156221563215642156521566215672156821569215702157121572215732157421575215762157721578215792158021581215822158321584215852158621587215882158921590215912159221593215942159521596215972159821599216002160121602216032160421605216062160721608216092161021611216122161321614216152161621617216182161921620216212162221623216242162521626216272162821629216302163121632216332163421635
  1. // Copyright (c) Athena Dev Teams - Licensed under GNU GPL
  2. // For more information, see LICENCE in the main folder
  3. //#define DEBUG_DISP
  4. //#define DEBUG_DISASM
  5. //#define DEBUG_RUN
  6. //#define DEBUG_HASH
  7. //#define DEBUG_DUMP_STACK
  8. #ifdef PCRE_SUPPORT
  9. #include "../../3rdparty/pcre/include/pcre.h" // preg_match
  10. #endif
  11. #include "../common/cbasetypes.h"
  12. #include "../common/malloc.h"
  13. #include "../common/md5calc.h"
  14. #include "../common/nullpo.h"
  15. #include "../common/random.h"
  16. #include "../common/showmsg.h"
  17. #include "../common/socket.h"
  18. #include "../common/strlib.h"
  19. #include "../common/timer.h"
  20. #include "../common/utils.h"
  21. #ifdef BETA_THREAD_TEST
  22. #include "../common/atomic.h"
  23. #include "../common/spinlock.h"
  24. #include "../common/thread.h"
  25. #include "../common/mutex.h"
  26. #endif
  27. #include "map.h"
  28. #include "path.h"
  29. #include "clif.h"
  30. #include "chrif.h"
  31. #include "itemdb.h"
  32. #include "pc.h"
  33. #include "storage.h"
  34. #include "pet.h"
  35. #include "mapreg.h"
  36. #include "homunculus.h"
  37. #include "instance.h"
  38. #include "mercenary.h"
  39. #include "intif.h"
  40. #include "chat.h"
  41. #include "battleground.h"
  42. #include "party.h"
  43. #include "mail.h"
  44. #include "script.h"
  45. #include "quest.h"
  46. #include "elemental.h"
  47. #include <stdio.h>
  48. #include <stdlib.h>
  49. #include <string.h>
  50. #include <math.h>
  51. #ifndef WIN32
  52. #endif
  53. #include <setjmp.h>
  54. #include <errno.h>
  55. TBL_PC *script_rid2sd(struct script_state *st);
  56. /**
  57. * Get `sd` from a account id in `loc` param instead of attached rid
  58. * @param st Script
  59. * @param loc Location to look account id in script parameter
  60. * @param sd Variable that will be assigned
  61. * @return True if `sd` is assigned, false otherwise
  62. **/
  63. static bool script_accid2sd_(struct script_state *st, uint8 loc, struct map_session_data **sd, const char *func) {
  64. if (script_hasdata(st, loc)) {
  65. int id_ = script_getnum(st, loc);
  66. if (!(*sd = map_id2sd(id_)))
  67. ShowError("%s: Player with account id '%d' is not found.\n", func, id_);
  68. }
  69. else
  70. *sd = script_rid2sd(st);
  71. return (*sd) ? true : false;
  72. }
  73. /**
  74. * Get `sd` from a char id in `loc` param instead of attached rid
  75. * @param st Script
  76. * @param loc Location to look char id in script parameter
  77. * @param sd Variable that will be assigned
  78. * @return True if `sd` is assigned, false otherwise
  79. **/
  80. static bool script_charid2sd_(struct script_state *st, uint8 loc, struct map_session_data **sd, const char *func) {
  81. if (script_hasdata(st, loc)) {
  82. int id_ = script_getnum(st, loc);
  83. if (!(*sd = map_charid2sd(id_)))
  84. ShowError("%s: Player with char id '%d' is not found.\n", func, id_);
  85. }
  86. else
  87. *sd = script_rid2sd(st);
  88. return (*sd) ? true : false;
  89. }
  90. /**
  91. * Get `sd` from a nick in `loc` param instead of attached rid
  92. * @param st Script
  93. * @param loc Location to look nick in script parameter
  94. * @param sd Variable that will be assigned
  95. * @return True if `sd` is assigned, false otherwise
  96. **/
  97. static bool script_nick2sd_(struct script_state *st, uint8 loc, struct map_session_data **sd, const char *func) {
  98. if (script_hasdata(st, loc)) {
  99. const char *name_ = script_getstr(st, loc);
  100. if (!(*sd = map_nick2sd(name_)))
  101. ShowError("%s: Player with nick '%s' is not found.\n", func, name_);
  102. }
  103. else
  104. *sd = script_rid2sd(st);
  105. return (*sd) ? true : false;
  106. }
  107. #define script_accid2sd(loc,sd) script_accid2sd_(st,(loc),&(sd),__FUNCTION__)
  108. #define script_charid2sd(loc,sd) script_charid2sd_(st,(loc),&(sd),__FUNCTION__)
  109. #define script_nick2sd(loc,sd) script_nick2sd_(st,(loc),&(sd),__FUNCTION__)
  110. /// temporary buffer for passing around compiled bytecode
  111. /// @see add_scriptb, set_label, parse_script
  112. static unsigned char* script_buf = NULL;
  113. static int script_pos = 0, script_size = 0;
  114. static inline int GETVALUE(const unsigned char* buf, int i)
  115. {
  116. return (int)MakeDWord(MakeWord(buf[i], buf[i+1]), MakeWord(buf[i+2], 0));
  117. }
  118. static inline void SETVALUE(unsigned char* buf, int i, int n)
  119. {
  120. buf[i] = GetByte(n, 0);
  121. buf[i+1] = GetByte(n, 1);
  122. buf[i+2] = GetByte(n, 2);
  123. }
  124. // String buffer structures.
  125. // str_data stores string information
  126. static struct str_data_struct {
  127. enum c_op type;
  128. int str;
  129. int backpatch;
  130. int label;
  131. int (*func)(struct script_state *st);
  132. int val;
  133. int next;
  134. } *str_data = NULL;
  135. static int str_data_size = 0; // size of the data
  136. static int str_num = LABEL_START; // next id to be assigned
  137. // str_buf holds the strings themselves
  138. static char *str_buf;
  139. static int str_size = 0; // size of the buffer
  140. static int str_pos = 0; // next position to be assigned
  141. // Using a prime number for SCRIPT_HASH_SIZE should give better distributions
  142. #define SCRIPT_HASH_SIZE 1021
  143. int str_hash[SCRIPT_HASH_SIZE];
  144. // Specifies which string hashing method to use
  145. //#define SCRIPT_HASH_DJB2
  146. //#define SCRIPT_HASH_SDBM
  147. #define SCRIPT_HASH_ELF
  148. static DBMap* scriptlabel_db = NULL; // const char* label_name -> int script_pos
  149. static DBMap* userfunc_db = NULL; // const char* func_name -> struct script_code*
  150. static int parse_options = 0;
  151. DBMap* script_get_label_db(void) { return scriptlabel_db; }
  152. DBMap* script_get_userfunc_db(void) { return userfunc_db; }
  153. // important buildin function references for usage in scripts
  154. static int buildin_set_ref = 0;
  155. static int buildin_callsub_ref = 0;
  156. static int buildin_callfunc_ref = 0;
  157. static int buildin_getelementofarray_ref = 0;
  158. // Caches compiled autoscript item code.
  159. // Note: This is not cleared when reloading itemdb.
  160. static DBMap* autobonus_db = NULL; // char* script -> char* bytecode
  161. struct Script_Config script_config = {
  162. 1, // warn_func_mismatch_argtypes
  163. 1, 65535, 2048, //warn_func_mismatch_paramnum/check_cmdcount/check_gotocount
  164. 0, INT_MAX, // input_min_value/input_max_value
  165. "OnPCDieEvent", //die_event_name
  166. "OnPCKillEvent", //kill_pc_event_name
  167. "OnNPCKillEvent", //kill_mob_event_name
  168. "OnPCLoginEvent", //login_event_name
  169. "OnPCLogoutEvent", //logout_event_name
  170. "OnPCLoadMapEvent", //loadmap_event_name
  171. "OnPCBaseLvUpEvent", //baselvup_event_name
  172. "OnPCJobLvUpEvent", //joblvup_event_name
  173. "OnPCStatCalcEvent", //stat_calc_event_name
  174. "OnTouch_", //ontouch_name (runs on first visible char to enter area, picks another char if the first char leaves)
  175. "OnTouch", //ontouch2_name (run whenever a char walks into the OnTouch area)
  176. };
  177. static jmp_buf error_jump;
  178. static char* error_msg;
  179. static const char* error_pos;
  180. static int error_report; // if the error should produce output
  181. // Used by disp_warning_message
  182. static const char* parser_current_src;
  183. static const char* parser_current_file;
  184. static int parser_current_line;
  185. // for advanced scripting support ( nested if, switch, while, for, do-while, function, etc )
  186. // [Eoe / jA 1080, 1081, 1094, 1164]
  187. enum curly_type {
  188. TYPE_NULL = 0,
  189. TYPE_IF,
  190. TYPE_SWITCH,
  191. TYPE_WHILE,
  192. TYPE_FOR,
  193. TYPE_DO,
  194. TYPE_USERFUNC,
  195. TYPE_ARGLIST // function argument list
  196. };
  197. enum e_arglist
  198. {
  199. ARGLIST_UNDEFINED = 0,
  200. ARGLIST_NO_PAREN = 1,
  201. ARGLIST_PAREN = 2,
  202. };
  203. static struct {
  204. struct {
  205. enum curly_type type;
  206. int index;
  207. int count;
  208. int flag;
  209. struct linkdb_node *case_label;
  210. } curly[256]; // Information right parenthesis
  211. int curly_count; // The number of right brackets
  212. int index; // Number of the syntax used in the script
  213. } syntax;
  214. const char* parse_curly_close(const char* p);
  215. const char* parse_syntax_close(const char* p);
  216. const char* parse_syntax_close_sub(const char* p,int* flag);
  217. const char* parse_syntax(const char* p);
  218. static int parse_syntax_for_flag = 0;
  219. extern short current_equip_item_index; //for New CARDS Scripts. It contains Inventory Index of the EQUIP_SCRIPT caller item. [Lupus]
  220. extern unsigned int current_equip_combo_pos;
  221. int potion_flag=0; //For use on Alchemist improved potions/Potion Pitcher. [Skotlex]
  222. int potion_hp=0, potion_per_hp=0, potion_sp=0, potion_per_sp=0;
  223. int potion_target = 0;
  224. unsigned int *generic_ui_array = NULL;
  225. unsigned int generic_ui_array_size = 0;
  226. c_op get_com(unsigned char *script,int *pos);
  227. int get_num(unsigned char *script,int *pos);
  228. typedef struct script_function {
  229. int (*func)(struct script_state *st);
  230. const char *name;
  231. const char *arg;
  232. } script_function;
  233. extern script_function buildin_func[];
  234. #ifdef BETA_THREAD_TEST
  235. /**
  236. * MySQL Query Slave
  237. **/
  238. static SPIN_LOCK queryThreadLock;
  239. static rAthread queryThread = NULL;
  240. static ramutex queryThreadMutex = NULL;
  241. static racond queryThreadCond = NULL;
  242. static volatile int32 queryThreadTerminate = 0;
  243. struct queryThreadEntry {
  244. bool ok;
  245. bool type; /* main db or log db? */
  246. struct script_state *st;
  247. };
  248. /* Ladies and Gentleman the Manager! */
  249. struct {
  250. struct queryThreadEntry **entry;/* array of structs */
  251. int count;
  252. int timer;/* used to receive processed entries */
  253. } queryThreadData;
  254. #endif
  255. /*==========================================
  256. * (Only those needed) local declaration prototype
  257. *------------------------------------------*/
  258. const char* parse_subexpr(const char* p,int limit);
  259. int run_func(struct script_state *st);
  260. int script_instancegetid(struct script_state *st);
  261. enum {
  262. MF_NOMEMO, //0
  263. MF_NOTELEPORT,
  264. MF_NOSAVE,
  265. MF_NOBRANCH,
  266. MF_NOPENALTY,
  267. MF_NOZENYPENALTY,
  268. MF_PVP,
  269. MF_PVP_NOPARTY,
  270. MF_PVP_NOGUILD,
  271. MF_GVG,
  272. MF_GVG_NOPARTY, //10
  273. MF_NOTRADE,
  274. MF_NOSKILL,
  275. MF_NOWARP,
  276. MF_PARTYLOCK,
  277. MF_NOICEWALL,
  278. MF_SNOW,
  279. MF_FOG,
  280. MF_SAKURA,
  281. MF_LEAVES,
  282. /**
  283. * No longer available, keeping here just in case it's back someday. [Ind]
  284. **/
  285. //MF_RAIN, //20
  286. // 21 free
  287. MF_NOGO = 22,
  288. MF_CLOUDS,
  289. MF_CLOUDS2,
  290. MF_FIREWORKS,
  291. MF_GVG_CASTLE,
  292. MF_GVG_DUNGEON,
  293. MF_NIGHTENABLED,
  294. MF_NOBASEEXP,
  295. MF_NOJOBEXP, //30
  296. MF_NOMOBLOOT,
  297. MF_NOMVPLOOT,
  298. MF_NORETURN,
  299. MF_NOWARPTO,
  300. MF_NIGHTMAREDROP,
  301. MF_RESTRICTED,
  302. MF_NOCOMMAND,
  303. MF_NODROP,
  304. MF_JEXP,
  305. MF_BEXP, //40
  306. MF_NOVENDING,
  307. MF_LOADEVENT,
  308. MF_NOCHAT,
  309. MF_NOEXPPENALTY,
  310. MF_GUILDLOCK,
  311. MF_TOWN,
  312. MF_AUTOTRADE,
  313. MF_ALLOWKS,
  314. MF_MONSTER_NOTELEPORT,
  315. MF_PVP_NOCALCRANK, //50
  316. MF_BATTLEGROUND,
  317. MF_RESET,
  318. MF_CHANNELAUTOJOIN,
  319. MF_NOUSECART,
  320. MF_NOITEMCONSUMPTION,
  321. MF_SUMSTARTMIRACLE,
  322. MF_NOMINEEFFECT,
  323. MF_NOLOCKON,
  324. MF_NOTOMB,
  325. MF_SKILL_DAMAGE //60
  326. };
  327. const char* script_op2name(int op)
  328. {
  329. #define RETURN_OP_NAME(type) case type: return #type
  330. switch( op )
  331. {
  332. RETURN_OP_NAME(C_NOP);
  333. RETURN_OP_NAME(C_POS);
  334. RETURN_OP_NAME(C_INT);
  335. RETURN_OP_NAME(C_PARAM);
  336. RETURN_OP_NAME(C_FUNC);
  337. RETURN_OP_NAME(C_STR);
  338. RETURN_OP_NAME(C_CONSTSTR);
  339. RETURN_OP_NAME(C_ARG);
  340. RETURN_OP_NAME(C_NAME);
  341. RETURN_OP_NAME(C_EOL);
  342. RETURN_OP_NAME(C_RETINFO);
  343. RETURN_OP_NAME(C_USERFUNC);
  344. RETURN_OP_NAME(C_USERFUNC_POS);
  345. // operators
  346. RETURN_OP_NAME(C_OP3);
  347. RETURN_OP_NAME(C_LOR);
  348. RETURN_OP_NAME(C_LAND);
  349. RETURN_OP_NAME(C_LE);
  350. RETURN_OP_NAME(C_LT);
  351. RETURN_OP_NAME(C_GE);
  352. RETURN_OP_NAME(C_GT);
  353. RETURN_OP_NAME(C_EQ);
  354. RETURN_OP_NAME(C_NE);
  355. RETURN_OP_NAME(C_XOR);
  356. RETURN_OP_NAME(C_OR);
  357. RETURN_OP_NAME(C_AND);
  358. RETURN_OP_NAME(C_ADD);
  359. RETURN_OP_NAME(C_SUB);
  360. RETURN_OP_NAME(C_MUL);
  361. RETURN_OP_NAME(C_DIV);
  362. RETURN_OP_NAME(C_MOD);
  363. RETURN_OP_NAME(C_NEG);
  364. RETURN_OP_NAME(C_LNOT);
  365. RETURN_OP_NAME(C_NOT);
  366. RETURN_OP_NAME(C_R_SHIFT);
  367. RETURN_OP_NAME(C_L_SHIFT);
  368. default:
  369. ShowDebug("script_op2name: unexpected op=%d\n", op);
  370. return "???";
  371. }
  372. #undef RETURN_OP_NAME
  373. }
  374. #ifdef DEBUG_DUMP_STACK
  375. static void script_dump_stack(struct script_state* st)
  376. {
  377. int i;
  378. ShowMessage("\tstart = %d\n", st->start);
  379. ShowMessage("\tend = %d\n", st->end);
  380. ShowMessage("\tdefsp = %d\n", st->stack->defsp);
  381. ShowMessage("\tsp = %d\n", st->stack->sp);
  382. for( i = 0; i < st->stack->sp; ++i )
  383. {
  384. struct script_data* data = &st->stack->stack_data[i];
  385. ShowMessage("\t[%d] %s", i, script_op2name(data->type));
  386. switch( data->type )
  387. {
  388. case C_INT:
  389. case C_POS:
  390. ShowMessage(" %d\n", data->u.num);
  391. break;
  392. case C_STR:
  393. case C_CONSTSTR:
  394. ShowMessage(" \"%s\"\n", data->u.str);
  395. break;
  396. case C_NAME:
  397. ShowMessage(" \"%s\" (id=%d ref=%p subtype=%s)\n", reference_getname(data), data->u.num, data->ref, script_op2name(str_data[data->u.num].type));
  398. break;
  399. case C_RETINFO:
  400. {
  401. struct script_retinfo* ri = data->u.ri;
  402. ShowMessage(" %p {scope.vars=%p, scope.arrays=%p, script=%p, pos=%d, nargs=%d, defsp=%d}\n", ri, ri->scope.vars, ri->scope.arrays, ri->script, ri->pos, ri->nargs, ri->defsp);
  403. }
  404. break;
  405. default:
  406. ShowMessage("\n");
  407. break;
  408. }
  409. }
  410. }
  411. #endif
  412. /// Reports on the console the src of a script error.
  413. static void script_reportsrc(struct script_state *st)
  414. {
  415. struct block_list* bl;
  416. if( st->oid == 0 )
  417. return; //Can't report source.
  418. bl = map_id2bl(st->oid);
  419. if( bl == NULL )
  420. return;
  421. switch( bl->type ) {
  422. case BL_NPC:
  423. if( bl->m >= 0 )
  424. ShowDebug("Source (NPC): %s at %s (%d,%d)\n", ((struct npc_data *)bl)->name, map[bl->m].name, bl->x, bl->y);
  425. else
  426. ShowDebug("Source (NPC): %s (invisible/not on a map)\n", ((struct npc_data *)bl)->name);
  427. break;
  428. default:
  429. if( bl->m >= 0 )
  430. ShowDebug("Source (Non-NPC type %d): name %s at %s (%d,%d)\n", bl->type, status_get_name(bl), map[bl->m].name, bl->x, bl->y);
  431. else
  432. ShowDebug("Source (Non-NPC type %d): name %s (invisible/not on a map)\n", bl->type, status_get_name(bl));
  433. break;
  434. }
  435. }
  436. /// Reports on the console information about the script data.
  437. static void script_reportdata(struct script_data* data)
  438. {
  439. if( data == NULL )
  440. return;
  441. switch( data->type ) {
  442. case C_NOP:// no value
  443. ShowDebug("Data: nothing (nil)\n");
  444. break;
  445. case C_INT:// number
  446. ShowDebug("Data: number value=%"PRId64"\n", data->u.num);
  447. break;
  448. case C_STR:
  449. case C_CONSTSTR:// string
  450. if( data->u.str ) {
  451. ShowDebug("Data: string value=\"%s\"\n", data->u.str);
  452. } else {
  453. ShowDebug("Data: string value=NULL\n");
  454. }
  455. break;
  456. case C_NAME:// reference
  457. if( reference_tovariable(data) ) {// variable
  458. const char* name = reference_getname(data);
  459. ShowDebug("Data: variable name='%s' index=%d\n", name, reference_getindex(data));
  460. } else if( reference_toconstant(data) ) {// constant
  461. ShowDebug("Data: constant name='%s' value=%d\n", reference_getname(data), reference_getconstant(data));
  462. } else if( reference_toparam(data) ) {// param
  463. ShowDebug("Data: param name='%s' type=%d\n", reference_getname(data), reference_getparamtype(data));
  464. } else {// ???
  465. ShowDebug("Data: reference name='%s' type=%s\n", reference_getname(data), script_op2name(data->type));
  466. ShowDebug("Please report this!!! - script->str_data.type=%s\n", script_op2name(str_data[reference_getid(data)].type));
  467. }
  468. break;
  469. case C_POS:// label
  470. ShowDebug("Data: label pos=%"PRId64"\n", data->u.num);
  471. break;
  472. default:
  473. ShowDebug("Data: %s\n", script_op2name(data->type));
  474. break;
  475. }
  476. }
  477. /// Reports on the console information about the current built-in function.
  478. static void script_reportfunc(struct script_state* st)
  479. {
  480. int params, id;
  481. struct script_data* data;
  482. if( !script_hasdata(st,0) )
  483. {// no stack
  484. return;
  485. }
  486. data = script_getdata(st,0);
  487. if( !data_isreference(data) || str_data[reference_getid(data)].type != C_FUNC )
  488. {// script currently not executing a built-in function or corrupt stack
  489. return;
  490. }
  491. id = reference_getid(data);
  492. params = script_lastdata(st)-1;
  493. if( params > 0 )
  494. {
  495. int i;
  496. ShowDebug("Function: %s (%d parameter%s):\n", get_str(id), params, ( params == 1 ) ? "" : "s");
  497. for( i = 2; i <= script_lastdata(st); i++ )
  498. {
  499. script_reportdata(script_getdata(st,i));
  500. }
  501. }
  502. else
  503. {
  504. ShowDebug("Function: %s (no parameters)\n", get_str(id));
  505. }
  506. }
  507. /*==========================================
  508. * Output error message
  509. *------------------------------------------*/
  510. static void disp_error_message2(const char *mes,const char *pos,int report)
  511. {
  512. error_msg = aStrdup(mes);
  513. error_pos = pos;
  514. error_report = report;
  515. longjmp( error_jump, 1 );
  516. }
  517. #define disp_error_message(mes,pos) disp_error_message2(mes,pos,1)
  518. static void disp_warning_message(const char *mes, const char *pos) {
  519. script_warning(parser_current_src,parser_current_file,parser_current_line,mes,pos);
  520. }
  521. /// Checks event parameter validity
  522. static void check_event(struct script_state *st, const char *evt)
  523. {
  524. if( evt && evt[0] && !stristr(evt, "::On") )
  525. {
  526. ShowWarning("NPC event parameter deprecated! Please use 'NPCNAME::OnEVENT' instead of '%s'.\n", evt);
  527. script_reportsrc(st);
  528. }
  529. }
  530. /*==========================================
  531. * Hashes the input string
  532. *------------------------------------------*/
  533. static unsigned int calc_hash(const char* p)
  534. {
  535. unsigned int h;
  536. #if defined(SCRIPT_HASH_DJB2)
  537. h = 5381;
  538. while( *p ) // hash*33 + c
  539. h = ( h << 5 ) + h + ((unsigned char)TOLOWER(*p++));
  540. #elif defined(SCRIPT_HASH_SDBM)
  541. h = 0;
  542. while( *p ) // hash*65599 + c
  543. h = ( h << 6 ) + ( h << 16 ) - h + ((unsigned char)TOLOWER(*p++));
  544. #elif defined(SCRIPT_HASH_ELF) // UNIX ELF hash
  545. h = 0;
  546. while( *p ){
  547. unsigned int g;
  548. h = ( h << 4 ) + ((unsigned char)TOLOWER(*p++));
  549. g = h & 0xF0000000;
  550. if( g )
  551. {
  552. h ^= g >> 24;
  553. h &= ~g;
  554. }
  555. }
  556. #else // athena hash
  557. h = 0;
  558. while( *p )
  559. h = ( h << 1 ) + ( h >> 3 ) + ( h >> 5 ) + ( h >> 8 ) + (unsigned char)TOLOWER(*p++);
  560. #endif
  561. return h % SCRIPT_HASH_SIZE;
  562. }
  563. /*==========================================
  564. * str_data manipulation functions
  565. *------------------------------------------*/
  566. /// Looks up string using the provided id.
  567. const char* get_str(int id)
  568. {
  569. Assert( id >= LABEL_START && id < str_size );
  570. return str_buf+str_data[id].str;
  571. }
  572. /// Returns the uid of the string, or -1.
  573. static int search_str(const char* p)
  574. {
  575. int i;
  576. for( i = str_hash[calc_hash(p)]; i != 0; i = str_data[i].next )
  577. if( strcasecmp(get_str(i),p) == 0 )
  578. return i;
  579. return -1;
  580. }
  581. /// Stores a copy of the string and returns its id.
  582. /// If an identical string is already present, returns its id instead.
  583. int add_str(const char* p)
  584. {
  585. int h;
  586. int len;
  587. h = calc_hash(p);
  588. if( str_hash[h] == 0 )
  589. {// empty bucket, add new node here
  590. str_hash[h] = str_num;
  591. }
  592. else
  593. {// scan for end of list, or occurence of identical string
  594. int i;
  595. for( i = str_hash[h]; ; i = str_data[i].next )
  596. {
  597. if( strcasecmp(get_str(i),p) == 0 )
  598. return i; // string already in list
  599. if( str_data[i].next == 0 )
  600. break; // reached the end
  601. }
  602. // append node to end of list
  603. str_data[i].next = str_num;
  604. }
  605. // grow list if neccessary
  606. if( str_num >= str_data_size )
  607. {
  608. str_data_size += 128;
  609. RECREATE(str_data,struct str_data_struct,str_data_size);
  610. memset(str_data + (str_data_size - 128), '\0', 128);
  611. }
  612. len=(int)strlen(p);
  613. // grow string buffer if neccessary
  614. while( str_pos+len+1 >= str_size )
  615. {
  616. str_size += 256;
  617. RECREATE(str_buf,char,str_size);
  618. memset(str_buf + (str_size - 256), '\0', 256);
  619. }
  620. safestrncpy(str_buf+str_pos, p, len+1);
  621. str_data[str_num].type = C_NOP;
  622. str_data[str_num].str = str_pos;
  623. str_data[str_num].next = 0;
  624. str_data[str_num].func = NULL;
  625. str_data[str_num].backpatch = -1;
  626. str_data[str_num].label = -1;
  627. str_pos += len+1;
  628. return str_num++;
  629. }
  630. /// Appends 1 byte to the script buffer.
  631. static void add_scriptb(int a)
  632. {
  633. if( script_pos+1 >= script_size )
  634. {
  635. script_size += SCRIPT_BLOCK_SIZE;
  636. RECREATE(script_buf,unsigned char,script_size);
  637. }
  638. script_buf[script_pos++] = (uint8)(a);
  639. }
  640. /// Appends a c_op value to the script buffer.
  641. /// The value is variable-length encoded into 8-bit blocks.
  642. /// The encoding scheme is ( 01?????? )* 00??????, LSB first.
  643. /// All blocks but the last hold 7 bits of data, topmost bit is always 1 (carries).
  644. static void add_scriptc(int a)
  645. {
  646. while( a >= 0x40 )
  647. {
  648. add_scriptb((a&0x3f)|0x40);
  649. a = (a - 0x40) >> 6;
  650. }
  651. add_scriptb(a);
  652. }
  653. /// Appends an integer value to the script buffer.
  654. /// The value is variable-length encoded into 8-bit blocks.
  655. /// The encoding scheme is ( 11?????? )* 10??????, LSB first.
  656. /// All blocks but the last hold 7 bits of data, topmost bit is always 1 (carries).
  657. static void add_scripti(int a)
  658. {
  659. while( a >= 0x40 )
  660. {
  661. add_scriptb((a&0x3f)|0xc0);
  662. a = (a - 0x40) >> 6;
  663. }
  664. add_scriptb(a|0x80);
  665. }
  666. /// Appends a str_data object (label/function/variable/integer) to the script buffer.
  667. ///
  668. /// @param l The id of the str_data entry
  669. // Maximum up to 16M
  670. static void add_scriptl(int l)
  671. {
  672. int backpatch = str_data[l].backpatch;
  673. switch(str_data[l].type){
  674. case C_POS:
  675. case C_USERFUNC_POS:
  676. add_scriptc(C_POS);
  677. add_scriptb(str_data[l].label);
  678. add_scriptb(str_data[l].label>>8);
  679. add_scriptb(str_data[l].label>>16);
  680. break;
  681. case C_NOP:
  682. case C_USERFUNC:
  683. // Embedded data backpatch there is a possibility of label
  684. add_scriptc(C_NAME);
  685. str_data[l].backpatch = script_pos;
  686. add_scriptb(backpatch);
  687. add_scriptb(backpatch>>8);
  688. add_scriptb(backpatch>>16);
  689. break;
  690. case C_INT:
  691. add_scripti(abs(str_data[l].val));
  692. if( str_data[l].val < 0 ) //Notice that this is negative, from jA (Rayce)
  693. add_scriptc(C_NEG);
  694. break;
  695. default: // assume C_NAME
  696. add_scriptc(C_NAME);
  697. add_scriptb(l);
  698. add_scriptb(l>>8);
  699. add_scriptb(l>>16);
  700. break;
  701. }
  702. }
  703. /*==========================================
  704. * Resolve the label
  705. *------------------------------------------*/
  706. void set_label(int l,int pos, const char* script_pos_cur)
  707. {
  708. int i;
  709. if(str_data[l].type==C_INT || str_data[l].type==C_PARAM || str_data[l].type==C_FUNC)
  710. { //Prevent overwriting constants values, parameters and built-in functions [Skotlex]
  711. disp_error_message("set_label: invalid label name",script_pos_cur);
  712. return;
  713. }
  714. if(str_data[l].label!=-1){
  715. disp_error_message("set_label: dup label ",script_pos_cur);
  716. return;
  717. }
  718. str_data[l].type=(str_data[l].type == C_USERFUNC ? C_USERFUNC_POS : C_POS);
  719. str_data[l].label=pos;
  720. for(i=str_data[l].backpatch;i>=0 && i!=0x00ffffff;){
  721. int next=GETVALUE(script_buf,i);
  722. script_buf[i-1]=(str_data[l].type == C_USERFUNC ? C_USERFUNC_POS : C_POS);
  723. SETVALUE(script_buf,i,pos);
  724. i=next;
  725. }
  726. }
  727. /// Skips spaces and/or comments.
  728. const char* skip_space(const char* p)
  729. {
  730. if( p == NULL )
  731. return NULL;
  732. for(;;)
  733. {
  734. while( ISSPACE(*p) )
  735. ++p;
  736. if( *p == '/' && p[1] == '/' )
  737. {// line comment
  738. while(*p && *p!='\n')
  739. ++p;
  740. }
  741. else if( *p == '/' && p[1] == '*' )
  742. {// block comment
  743. p += 2;
  744. for(;;)
  745. {
  746. if( *p == '\0' ) {
  747. disp_warning_message("script:script->skip_space: end of file while parsing block comment. expected "CL_BOLD"*/"CL_NORM, p);
  748. return p;
  749. }
  750. if( *p == '*' && p[1] == '/' )
  751. {// end of block comment
  752. p += 2;
  753. break;
  754. }
  755. ++p;
  756. }
  757. }
  758. else
  759. break;
  760. }
  761. return p;
  762. }
  763. /// Skips a word.
  764. /// A word consists of undercores and/or alphanumeric characters,
  765. /// and valid variable prefixes/postfixes.
  766. static const char* skip_word(const char* p)
  767. {
  768. // prefix
  769. switch( *p ) {
  770. case '@':// temporary char variable
  771. ++p; break;
  772. case '#':// account variable
  773. p += ( p[1] == '#' ? 2 : 1 ); break;
  774. case '\'':// instance variable
  775. ++p; break;
  776. case '.':// npc variable
  777. p += ( p[1] == '@' ? 2 : 1 ); break;
  778. case '$':// global variable
  779. p += ( p[1] == '@' ? 2 : 1 ); break;
  780. }
  781. while( ISALNUM(*p) || *p == '_' )
  782. ++p;
  783. // postfix
  784. if( *p == '$' )// string
  785. p++;
  786. return p;
  787. }
  788. /// Adds a word to str_data.
  789. /// @see skip_word
  790. /// @see add_str
  791. static int add_word(const char* p)
  792. {
  793. char* word;
  794. int len;
  795. int i;
  796. // Check for a word
  797. len = skip_word(p) - p;
  798. if( len == 0 )
  799. disp_error_message("script:add_word: invalid word. A word consists of undercores and/or alphanumeric characters, and valid variable prefixes/postfixes.", p);
  800. // Duplicate the word
  801. word = (char*)aMalloc(len+1);
  802. memcpy(word, p, len);
  803. word[len] = 0;
  804. // add the word
  805. i = add_str(word);
  806. aFree(word);
  807. return i;
  808. }
  809. /// Parses a function call.
  810. /// The argument list can have parenthesis or not.
  811. /// The number of arguments is checked.
  812. static
  813. const char* parse_callfunc(const char* p, int require_paren, int is_custom)
  814. {
  815. const char* p2;
  816. const char* arg=NULL;
  817. int func;
  818. func = add_word(p);
  819. if( str_data[func].type == C_FUNC ){
  820. // buildin function
  821. add_scriptl(func);
  822. add_scriptc(C_ARG);
  823. arg = buildin_func[str_data[func].val].arg;
  824. } else if( str_data[func].type == C_USERFUNC || str_data[func].type == C_USERFUNC_POS ){
  825. // script defined function
  826. add_scriptl(buildin_callsub_ref);
  827. add_scriptc(C_ARG);
  828. add_scriptl(func);
  829. arg = buildin_func[str_data[buildin_callsub_ref].val].arg;
  830. if( *arg == 0 )
  831. disp_error_message("parse_callfunc: callsub has no arguments, please review its definition",p);
  832. if( *arg != '*' )
  833. ++arg; // count func as argument
  834. } else {
  835. #ifdef SCRIPT_CALLFUNC_CHECK
  836. const char* name = get_str(func);
  837. if( !is_custom && strdb_get(userfunc_db, name) == NULL ) {
  838. #endif
  839. disp_error_message("parse_line: expect command, missing function name or calling undeclared function",p);
  840. #ifdef SCRIPT_CALLFUNC_CHECK
  841. } else {;
  842. add_scriptl(buildin_callfunc_ref);
  843. add_scriptc(C_ARG);
  844. add_scriptc(C_STR);
  845. while( *name ) add_scriptb(*name ++);
  846. add_scriptb(0);
  847. arg = buildin_func[str_data[buildin_callfunc_ref].val].arg;
  848. if( *arg != '*' ) ++ arg;
  849. }
  850. #endif
  851. }
  852. p = skip_word(p);
  853. p = skip_space(p);
  854. syntax.curly[syntax.curly_count].type = TYPE_ARGLIST;
  855. syntax.curly[syntax.curly_count].count = 0;
  856. if( *p == ';' )
  857. {// <func name> ';'
  858. syntax.curly[syntax.curly_count].flag = ARGLIST_NO_PAREN;
  859. } else if( *p == '(' && *(p2=skip_space(p+1)) == ')' )
  860. {// <func name> '(' ')'
  861. syntax.curly[syntax.curly_count].flag = ARGLIST_PAREN;
  862. p = p2;
  863. /*
  864. } else if( 0 && require_paren && *p != '(' )
  865. {// <func name>
  866. syntax.curly[syntax.curly_count].flag = ARGLIST_NO_PAREN;
  867. */
  868. } else
  869. {// <func name> <arg list>
  870. if( require_paren ){
  871. if( *p != '(' )
  872. disp_error_message("need '('",p);
  873. ++p; // skip '('
  874. syntax.curly[syntax.curly_count].flag = ARGLIST_PAREN;
  875. } else if( *p == '(' ){
  876. syntax.curly[syntax.curly_count].flag = ARGLIST_UNDEFINED;
  877. } else {
  878. syntax.curly[syntax.curly_count].flag = ARGLIST_NO_PAREN;
  879. }
  880. ++syntax.curly_count;
  881. while( *arg ) {
  882. p2=parse_subexpr(p,-1);
  883. if( p == p2 )
  884. break; // not an argument
  885. if( *arg != '*' )
  886. ++arg; // next argument
  887. p=skip_space(p2);
  888. if( *arg == 0 || *p != ',' )
  889. break; // no more arguments
  890. ++p; // skip comma
  891. }
  892. --syntax.curly_count;
  893. }
  894. if( *arg && *arg != '?' && *arg != '*' )
  895. disp_error_message2("parse_callfunc: not enough arguments, expected ','", p, script_config.warn_func_mismatch_paramnum);
  896. if( syntax.curly[syntax.curly_count].type != TYPE_ARGLIST )
  897. disp_error_message("parse_callfunc: DEBUG last curly is not an argument list",p);
  898. if( syntax.curly[syntax.curly_count].flag == ARGLIST_PAREN ){
  899. if( *p != ')' )
  900. disp_error_message("parse_callfunc: expected ')' to close argument list",p);
  901. ++p;
  902. }
  903. add_scriptc(C_FUNC);
  904. return p;
  905. }
  906. /// Processes end of logical script line.
  907. /// @param first When true, only fix up scheduling data is initialized
  908. /// @param p Script position for error reporting in set_label
  909. static void parse_nextline(bool first, const char* p)
  910. {
  911. if( !first )
  912. {
  913. add_scriptc(C_EOL); // mark end of line for stack cleanup
  914. set_label(LABEL_NEXTLINE, script_pos, p); // fix up '-' labels
  915. }
  916. // initialize data for new '-' label fix up scheduling
  917. str_data[LABEL_NEXTLINE].type = C_NOP;
  918. str_data[LABEL_NEXTLINE].backpatch = -1;
  919. str_data[LABEL_NEXTLINE].label = -1;
  920. }
  921. /// Parse a variable assignment using the direct equals operator
  922. /// @param p script position where the function should run from
  923. /// @return NULL if not a variable assignment, the new position otherwise
  924. const char* parse_variable(const char* p) {
  925. int word;
  926. c_op type = C_NOP;
  927. const char *p2 = NULL;
  928. const char *var = p;
  929. // skip the variable where applicable
  930. p = skip_word(p);
  931. p = skip_space(p);
  932. if( p == NULL ) {// end of the line or invalid buffer
  933. return NULL;
  934. }
  935. if( *p == '[' ) {// array variable so process the array as appropriate
  936. int i,j;
  937. for( p2 = p, i = 0, j = 1; p; ++ i ) {
  938. if( *p ++ == ']' && --(j) == 0 ) break;
  939. if( *p == '[' ) ++ j;
  940. }
  941. if( !(p = skip_space(p)) ) {// end of line or invalid characters remaining
  942. disp_error_message("Missing right expression or closing bracket for variable.", p);
  943. }
  944. }
  945. if( type == C_NOP &&
  946. !( ( p[0] == '=' && p[1] != '=' && (type = C_EQ) ) // =
  947. || ( p[0] == '+' && p[1] == '=' && (type = C_ADD) ) // +=
  948. || ( p[0] == '-' && p[1] == '=' && (type = C_SUB) ) // -=
  949. || ( p[0] == '^' && p[1] == '=' && (type = C_XOR) ) // ^=
  950. || ( p[0] == '|' && p[1] == '=' && (type = C_OR ) ) // |=
  951. || ( p[0] == '&' && p[1] == '=' && (type = C_AND) ) // &=
  952. || ( p[0] == '*' && p[1] == '=' && (type = C_MUL) ) // *=
  953. || ( p[0] == '/' && p[1] == '=' && (type = C_DIV) ) // /=
  954. || ( p[0] == '%' && p[1] == '=' && (type = C_MOD) ) // %=
  955. || ( p[0] == '~' && p[1] == '=' && (type = C_NOT) ) // ~=
  956. || ( p[0] == '+' && p[1] == '+' && (type = C_ADD_PP) ) // ++
  957. || ( p[0] == '-' && p[1] == '-' && (type = C_SUB_PP) ) // --
  958. || ( p[0] == '<' && p[1] == '<' && p[2] == '=' && (type = C_L_SHIFT) ) // <<=
  959. || ( p[0] == '>' && p[1] == '>' && p[2] == '=' && (type = C_R_SHIFT) ) // >>=
  960. ) )
  961. {// failed to find a matching operator combination so invalid
  962. return NULL;
  963. }
  964. switch( type ) {
  965. case C_EQ: {// incremental modifier
  966. p = skip_space( &p[1] );
  967. }
  968. break;
  969. case C_L_SHIFT:
  970. case C_R_SHIFT: {// left or right shift modifier
  971. p = skip_space( &p[3] );
  972. }
  973. break;
  974. default: {// normal incremental command
  975. p = skip_space( &p[2] );
  976. }
  977. }
  978. if( p == NULL ) {// end of line or invalid buffer
  979. return NULL;
  980. }
  981. // push the set function onto the stack
  982. add_scriptl(buildin_set_ref);
  983. add_scriptc(C_ARG);
  984. // always append parenthesis to avoid errors
  985. syntax.curly[syntax.curly_count].type = TYPE_ARGLIST;
  986. syntax.curly[syntax.curly_count].count = 0;
  987. syntax.curly[syntax.curly_count].flag = ARGLIST_PAREN;
  988. // increment the total curly count for the position in the script
  989. ++ syntax.curly_count;
  990. // parse the variable currently being modified
  991. word = add_word(var);
  992. if( str_data[word].type == C_FUNC || str_data[word].type == C_USERFUNC || str_data[word].type == C_USERFUNC_POS )
  993. {// cannot assign a variable which exists as a function or label
  994. disp_error_message("Cannot modify a variable which has the same name as a function or label.", p);
  995. }
  996. if( p2 ) {// process the variable index
  997. const char* p3 = NULL;
  998. // push the getelementofarray method into the stack
  999. add_scriptl(buildin_getelementofarray_ref);
  1000. add_scriptc(C_ARG);
  1001. add_scriptl(word);
  1002. // process the sub-expression for this assignment
  1003. p3 = parse_subexpr(p2 + 1, 1);
  1004. p3 = skip_space(p3);
  1005. if( *p3 != ']' ) {// closing parenthesis is required for this script
  1006. disp_error_message("Missing closing ']' parenthesis for the variable assignment.", p3);
  1007. }
  1008. // push the closing function stack operator onto the stack
  1009. add_scriptc(C_FUNC);
  1010. p3 ++;
  1011. } else {// simply push the variable or value onto the stack
  1012. add_scriptl(word);
  1013. }
  1014. if( type != C_EQ )
  1015. add_scriptc(C_REF);
  1016. if( type == C_ADD_PP || type == C_SUB_PP ) {// incremental operator for the method
  1017. add_scripti(1);
  1018. add_scriptc(type == C_ADD_PP ? C_ADD : C_SUB);
  1019. } else {// process the value as an expression
  1020. p = parse_subexpr(p, -1);
  1021. if( type != C_EQ )
  1022. {// push the type of modifier onto the stack
  1023. add_scriptc(type);
  1024. }
  1025. }
  1026. // decrement the curly count for the position within the script
  1027. -- syntax.curly_count;
  1028. // close the script by appending the function operator
  1029. add_scriptc(C_FUNC);
  1030. // push the buffer from the method
  1031. return p;
  1032. }
  1033. /*
  1034. * Checks whether the gives string is a number literal
  1035. *
  1036. * Mainly necessary to differentiate between number literals and NPC name
  1037. * constants, since several of those start with a digit.
  1038. *
  1039. * All this does is to check if the string begins with an optional + or - sign,
  1040. * followed by a hexadecimal or decimal number literal literal and is NOT
  1041. * followed by a underscore or letter.
  1042. *
  1043. * @author : Hercules.ws
  1044. * @param p Pointer to the string to check
  1045. * @return Whether the string is a number literal
  1046. */
  1047. bool is_number(const char *p) {
  1048. const char *np;
  1049. if (!p)
  1050. return false;
  1051. if (*p == '-' || *p == '+')
  1052. p++;
  1053. np = p;
  1054. if (*p == '0' && p[1] == 'x') {
  1055. p+=2;
  1056. np = p;
  1057. // Hexadecimal
  1058. while (ISXDIGIT(*np))
  1059. np++;
  1060. } else {
  1061. // Decimal
  1062. while (ISDIGIT(*np))
  1063. np++;
  1064. }
  1065. if (p != np && *np != '_' && !ISALPHA(*np)) // At least one digit, and next isn't a letter or _
  1066. return true;
  1067. return false;
  1068. }
  1069. /*==========================================
  1070. * Analysis section
  1071. *------------------------------------------*/
  1072. const char* parse_simpleexpr(const char *p)
  1073. {
  1074. long long i;
  1075. p=skip_space(p);
  1076. if(*p==';' || *p==',')
  1077. disp_error_message("parse_simpleexpr: unexpected end of expression",p);
  1078. if(*p=='('){
  1079. if( (i=syntax.curly_count-1) >= 0 && syntax.curly[i].type == TYPE_ARGLIST )
  1080. ++syntax.curly[i].count;
  1081. p=parse_subexpr(p+1,-1);
  1082. p=skip_space(p);
  1083. if( (i=syntax.curly_count-1) >= 0 && syntax.curly[i].type == TYPE_ARGLIST &&
  1084. syntax.curly[i].flag == ARGLIST_UNDEFINED && --syntax.curly[i].count == 0
  1085. ){
  1086. if( *p == ',' ){
  1087. syntax.curly[i].flag = ARGLIST_PAREN;
  1088. return p;
  1089. } else
  1090. syntax.curly[i].flag = ARGLIST_NO_PAREN;
  1091. }
  1092. if( *p != ')' )
  1093. disp_error_message("parse_simpleexpr: unmatched ')'",p);
  1094. ++p;
  1095. } else if(is_number(p)) {
  1096. char *np;
  1097. while(*p == '0' && ISDIGIT(p[1])) p++;
  1098. i=strtoll(p,&np,0);
  1099. if( i < INT_MIN ) {
  1100. i = INT_MIN;
  1101. disp_warning_message("parse_simpleexpr: underflow detected, capping value to INT_MIN",p);
  1102. } else if( i > INT_MAX ) {
  1103. i = INT_MAX;
  1104. disp_warning_message("parse_simpleexpr: overflow detected, capping value to INT_MAX",p);
  1105. }
  1106. add_scripti((int)i);
  1107. p=np;
  1108. } else if(*p=='"'){
  1109. add_scriptc(C_STR);
  1110. p++;
  1111. while( *p && *p != '"' ){
  1112. if( (unsigned char)p[-1] <= 0x7e && *p == '\\' )
  1113. {
  1114. char buf[8];
  1115. size_t len = skip_escaped_c(p) - p;
  1116. size_t n = sv_unescape_c(buf, p, len);
  1117. if( n != 1 )
  1118. ShowDebug("parse_simpleexpr: unexpected length %d after unescape (\"%.*s\" -> %.*s)\n", (int)n, (int)len, p, (int)n, buf);
  1119. p += len;
  1120. add_scriptb(*buf);
  1121. continue;
  1122. }
  1123. else if( *p == '\n' )
  1124. disp_error_message("parse_simpleexpr: unexpected newline @ string",p);
  1125. add_scriptb(*p++);
  1126. }
  1127. if(!*p)
  1128. disp_error_message("parse_simpleexpr: unexpected eof @ string",p);
  1129. add_scriptb(0);
  1130. p++; //'"'
  1131. } else {
  1132. int l;
  1133. const char* pv;
  1134. // label , register , function etc
  1135. if(skip_word(p)==p)
  1136. disp_error_message("parse_simpleexpr: unexpected character",p);
  1137. l=add_word(p);
  1138. if( str_data[l].type == C_FUNC || str_data[l].type == C_USERFUNC || str_data[l].type == C_USERFUNC_POS)
  1139. return parse_callfunc(p,1,0);
  1140. #ifdef SCRIPT_CALLFUNC_CHECK
  1141. else {
  1142. const char* name = get_str(l);
  1143. if( strdb_get(userfunc_db,name) != NULL ) {
  1144. return parse_callfunc(p,1,1);
  1145. }
  1146. }
  1147. #endif
  1148. if( (pv = parse_variable(p)) )
  1149. {// successfully processed a variable assignment
  1150. return pv;
  1151. }
  1152. p=skip_word(p);
  1153. if( *p == '[' ){
  1154. // array(name[i] => getelementofarray(name,i) )
  1155. add_scriptl(buildin_getelementofarray_ref);
  1156. add_scriptc(C_ARG);
  1157. add_scriptl(l);
  1158. p=parse_subexpr(p+1,-1);
  1159. p=skip_space(p);
  1160. if( *p != ']' )
  1161. disp_error_message("parse_simpleexpr: unmatched ']'",p);
  1162. ++p;
  1163. add_scriptc(C_FUNC);
  1164. }else
  1165. add_scriptl(l);
  1166. }
  1167. return p;
  1168. }
  1169. /*==========================================
  1170. * Analysis of the expression
  1171. *------------------------------------------*/
  1172. const char* parse_subexpr(const char* p,int limit)
  1173. {
  1174. int op,opl,len;
  1175. p=skip_space(p);
  1176. if( *p == '-' ){
  1177. const char* tmpp = skip_space(p+1);
  1178. if( *tmpp == ';' || *tmpp == ',' ){
  1179. add_scriptl(LABEL_NEXTLINE);
  1180. p++;
  1181. return p;
  1182. }
  1183. }
  1184. if((op=C_NEG,*p=='-') || (op=C_LNOT,*p=='!') || (op=C_NOT,*p=='~')){
  1185. p=parse_subexpr(p+1,10);
  1186. add_scriptc(op);
  1187. } else
  1188. p=parse_simpleexpr(p);
  1189. p=skip_space(p);
  1190. while((
  1191. (op=C_OP3,opl=0,len=1,*p=='?') ||
  1192. (op=C_ADD,opl=8,len=1,*p=='+') ||
  1193. (op=C_SUB,opl=8,len=1,*p=='-') ||
  1194. (op=C_MUL,opl=9,len=1,*p=='*') ||
  1195. (op=C_DIV,opl=9,len=1,*p=='/') ||
  1196. (op=C_MOD,opl=9,len=1,*p=='%') ||
  1197. (op=C_LAND,opl=2,len=2,*p=='&' && p[1]=='&') ||
  1198. (op=C_AND,opl=6,len=1,*p=='&') ||
  1199. (op=C_LOR,opl=1,len=2,*p=='|' && p[1]=='|') ||
  1200. (op=C_OR,opl=5,len=1,*p=='|') ||
  1201. (op=C_XOR,opl=4,len=1,*p=='^') ||
  1202. (op=C_EQ,opl=3,len=2,*p=='=' && p[1]=='=') ||
  1203. (op=C_NE,opl=3,len=2,*p=='!' && p[1]=='=') ||
  1204. (op=C_R_SHIFT,opl=7,len=2,*p=='>' && p[1]=='>') ||
  1205. (op=C_GE,opl=3,len=2,*p=='>' && p[1]=='=') ||
  1206. (op=C_GT,opl=3,len=1,*p=='>') ||
  1207. (op=C_L_SHIFT,opl=7,len=2,*p=='<' && p[1]=='<') ||
  1208. (op=C_LE,opl=3,len=2,*p=='<' && p[1]=='=') ||
  1209. (op=C_LT,opl=3,len=1,*p=='<')) && opl>limit){
  1210. p+=len;
  1211. if(op == C_OP3) {
  1212. p=parse_subexpr(p,-1);
  1213. p=skip_space(p);
  1214. if( *(p++) != ':')
  1215. disp_error_message("parse_subexpr: expected ':'", p-1);
  1216. p=parse_subexpr(p,-1);
  1217. } else {
  1218. p=parse_subexpr(p,opl);
  1219. }
  1220. add_scriptc(op);
  1221. p=skip_space(p);
  1222. }
  1223. return p; /* return first untreated operator */
  1224. }
  1225. /*==========================================
  1226. * Evaluation of the expression
  1227. *------------------------------------------*/
  1228. const char* parse_expr(const char *p)
  1229. {
  1230. switch(*p){
  1231. case ')': case ';': case ':': case '[': case ']':
  1232. case '}':
  1233. disp_error_message("parse_expr: unexpected character",p);
  1234. }
  1235. p=parse_subexpr(p,-1);
  1236. return p;
  1237. }
  1238. /*==========================================
  1239. * Analysis of the line
  1240. *------------------------------------------*/
  1241. const char* parse_line(const char* p)
  1242. {
  1243. const char* p2;
  1244. p=skip_space(p);
  1245. if(*p==';') {
  1246. //Close decision for if(); for(); while();
  1247. p = parse_syntax_close(p + 1);
  1248. return p;
  1249. }
  1250. if(*p==')' && parse_syntax_for_flag)
  1251. return p+1;
  1252. p = skip_space(p);
  1253. if(p[0] == '{') {
  1254. syntax.curly[syntax.curly_count].type = TYPE_NULL;
  1255. syntax.curly[syntax.curly_count].count = -1;
  1256. syntax.curly[syntax.curly_count].index = -1;
  1257. syntax.curly_count++;
  1258. return p + 1;
  1259. } else if(p[0] == '}') {
  1260. return parse_curly_close(p);
  1261. }
  1262. // Syntax-related processing
  1263. p2 = parse_syntax(p);
  1264. if(p2 != NULL)
  1265. return p2;
  1266. // attempt to process a variable assignment
  1267. p2 = parse_variable(p);
  1268. if( p2 != NULL )
  1269. {// variable assignment processed so leave the method
  1270. return parse_syntax_close(p2 + 1);
  1271. }
  1272. p = parse_callfunc(p,0,0);
  1273. p = skip_space(p);
  1274. if(parse_syntax_for_flag) {
  1275. if( *p != ')' )
  1276. disp_error_message("parse_line: expected ')'",p);
  1277. } else {
  1278. if( *p != ';' )
  1279. disp_error_message("parse_line: expected ';'",p);
  1280. }
  1281. //Binding decision for if(), for(), while()
  1282. p = parse_syntax_close(p+1);
  1283. return p;
  1284. }
  1285. // { ... } Closing process
  1286. const char* parse_curly_close(const char* p)
  1287. {
  1288. if(syntax.curly_count <= 0) {
  1289. disp_error_message("parse_curly_close: unexpected string",p);
  1290. return p + 1;
  1291. } else if(syntax.curly[syntax.curly_count-1].type == TYPE_NULL) {
  1292. syntax.curly_count--;
  1293. //Close decision if, for , while
  1294. p = parse_syntax_close(p + 1);
  1295. return p;
  1296. } else if(syntax.curly[syntax.curly_count-1].type == TYPE_SWITCH) {
  1297. //Closing switch()
  1298. int pos = syntax.curly_count-1;
  1299. char label[256];
  1300. int l;
  1301. // Remove temporary variables
  1302. sprintf(label,"set $@__SW%x_VAL,0;",syntax.curly[pos].index);
  1303. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1304. parse_line(label);
  1305. syntax.curly_count--;
  1306. // Go to the end pointer unconditionally
  1307. sprintf(label,"goto __SW%x_FIN;",syntax.curly[pos].index);
  1308. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1309. parse_line(label);
  1310. syntax.curly_count--;
  1311. // You are here labeled
  1312. sprintf(label,"__SW%x_%x",syntax.curly[pos].index,syntax.curly[pos].count);
  1313. l=add_str(label);
  1314. set_label(l,script_pos, p);
  1315. if(syntax.curly[pos].flag) {
  1316. //Exists default
  1317. sprintf(label,"goto __SW%x_DEF;",syntax.curly[pos].index);
  1318. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1319. parse_line(label);
  1320. syntax.curly_count--;
  1321. }
  1322. // Label end
  1323. sprintf(label,"__SW%x_FIN",syntax.curly[pos].index);
  1324. l=add_str(label);
  1325. set_label(l,script_pos, p);
  1326. linkdb_final(&syntax.curly[pos].case_label); // free the list of case label
  1327. syntax.curly_count--;
  1328. //Closing decision if, for , while
  1329. p = parse_syntax_close(p + 1);
  1330. return p;
  1331. } else {
  1332. disp_error_message("parse_curly_close: unexpected string",p);
  1333. return p + 1;
  1334. }
  1335. }
  1336. // Syntax-related processing
  1337. // break, case, continue, default, do, for, function,
  1338. // if, switch, while ? will handle this internally.
  1339. const char* parse_syntax(const char* p)
  1340. {
  1341. const char *p2 = skip_word(p);
  1342. switch(*p) {
  1343. case 'B':
  1344. case 'b':
  1345. if(p2 - p == 5 && !strncasecmp(p,"break",5)) {
  1346. // break Processing
  1347. char label[256];
  1348. int pos = syntax.curly_count - 1;
  1349. while(pos >= 0) {
  1350. if(syntax.curly[pos].type == TYPE_DO) {
  1351. sprintf(label,"goto __DO%x_FIN;",syntax.curly[pos].index);
  1352. break;
  1353. } else if(syntax.curly[pos].type == TYPE_FOR) {
  1354. sprintf(label,"goto __FR%x_FIN;",syntax.curly[pos].index);
  1355. break;
  1356. } else if(syntax.curly[pos].type == TYPE_WHILE) {
  1357. sprintf(label,"goto __WL%x_FIN;",syntax.curly[pos].index);
  1358. break;
  1359. } else if(syntax.curly[pos].type == TYPE_SWITCH) {
  1360. sprintf(label,"goto __SW%x_FIN;",syntax.curly[pos].index);
  1361. break;
  1362. }
  1363. pos--;
  1364. }
  1365. if(pos < 0) {
  1366. disp_error_message("parse_syntax: unexpected 'break'",p);
  1367. } else {
  1368. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1369. parse_line(label);
  1370. syntax.curly_count--;
  1371. }
  1372. p = skip_space(p2);
  1373. if(*p != ';')
  1374. disp_error_message("parse_syntax: expected ';'",p);
  1375. // Closing decision if, for , while
  1376. p = parse_syntax_close(p + 1);
  1377. return p;
  1378. }
  1379. break;
  1380. case 'c':
  1381. case 'C':
  1382. if(p2 - p == 4 && !strncasecmp(p,"case",4)) {
  1383. //Processing case
  1384. int pos = syntax.curly_count-1;
  1385. if(pos < 0 || syntax.curly[pos].type != TYPE_SWITCH) {
  1386. disp_error_message("parse_syntax: unexpected 'case' ",p);
  1387. return p+1;
  1388. } else {
  1389. char label[256];
  1390. int l,v;
  1391. char *np;
  1392. if(syntax.curly[pos].count != 1) {
  1393. //Jump for FALLTHRU
  1394. sprintf(label,"goto __SW%x_%xJ;",syntax.curly[pos].index,syntax.curly[pos].count);
  1395. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1396. parse_line(label);
  1397. syntax.curly_count--;
  1398. // You are here labeled
  1399. sprintf(label,"__SW%x_%x",syntax.curly[pos].index,syntax.curly[pos].count);
  1400. l=add_str(label);
  1401. set_label(l,script_pos, p);
  1402. }
  1403. //Decision statement switch
  1404. p = skip_space(p2);
  1405. if(p == p2) {
  1406. disp_error_message("parse_syntax: expected a space ' '",p);
  1407. }
  1408. // check whether case label is integer or not
  1409. if(is_number(p)) {
  1410. //Numeric value
  1411. v = (int)strtol(p,&np,0);
  1412. if((*p == '-' || *p == '+') && ISDIGIT(p[1])) // pre-skip because '-' can not skip_word
  1413. p++;
  1414. p = skip_word(p);
  1415. if(np != p)
  1416. disp_error_message("parse_syntax: 'case' label is not an integer",np);
  1417. } else {
  1418. //Check for constants
  1419. p2 = skip_word(p);
  1420. v = (int)(size_t) (p2-p); // length of word at p2
  1421. memcpy(label,p,v);
  1422. label[v]='\0';
  1423. if( !script_get_constant(label, &v) )
  1424. disp_error_message("parse_syntax: 'case' label is not an integer",p);
  1425. p = skip_word(p);
  1426. }
  1427. p = skip_space(p);
  1428. if(*p != ':')
  1429. disp_error_message("parse_syntax: expect ':'",p);
  1430. sprintf(label,"if(%d != $@__SW%x_VAL) goto __SW%x_%x;",
  1431. v,syntax.curly[pos].index,syntax.curly[pos].index,syntax.curly[pos].count+1);
  1432. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1433. // Bad I do not parse twice
  1434. p2 = parse_line(label);
  1435. parse_line(p2);
  1436. syntax.curly_count--;
  1437. if(syntax.curly[pos].count != 1) {
  1438. // Label after the completion of FALLTHRU
  1439. sprintf(label,"__SW%x_%xJ",syntax.curly[pos].index,syntax.curly[pos].count);
  1440. l=add_str(label);
  1441. set_label(l,script_pos,p);
  1442. }
  1443. // check duplication of case label [Rayce]
  1444. if(linkdb_search(&syntax.curly[pos].case_label, (void*)__64BPRTSIZE(v)) != NULL)
  1445. disp_error_message("parse_syntax: dup 'case'",p);
  1446. linkdb_insert(&syntax.curly[pos].case_label, (void*)__64BPRTSIZE(v), (void*)1);
  1447. sprintf(label,"set $@__SW%x_VAL,0;",syntax.curly[pos].index);
  1448. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1449. parse_line(label);
  1450. syntax.curly_count--;
  1451. syntax.curly[pos].count++;
  1452. }
  1453. return p + 1;
  1454. } else if(p2 - p == 8 && !strncasecmp(p,"continue",8)) {
  1455. // Processing continue
  1456. char label[256];
  1457. int pos = syntax.curly_count - 1;
  1458. while(pos >= 0) {
  1459. if(syntax.curly[pos].type == TYPE_DO) {
  1460. sprintf(label,"goto __DO%x_NXT;",syntax.curly[pos].index);
  1461. syntax.curly[pos].flag = 1; //Flag put the link for continue
  1462. break;
  1463. } else if(syntax.curly[pos].type == TYPE_FOR) {
  1464. sprintf(label,"goto __FR%x_NXT;",syntax.curly[pos].index);
  1465. break;
  1466. } else if(syntax.curly[pos].type == TYPE_WHILE) {
  1467. sprintf(label,"goto __WL%x_NXT;",syntax.curly[pos].index);
  1468. break;
  1469. }
  1470. pos--;
  1471. }
  1472. if(pos < 0) {
  1473. disp_error_message("parse_syntax: unexpected 'continue'",p);
  1474. } else {
  1475. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1476. parse_line(label);
  1477. syntax.curly_count--;
  1478. }
  1479. p = skip_space(p2);
  1480. if(*p != ';')
  1481. disp_error_message("parse_syntax: expected ';'",p);
  1482. //Closing decision if, for , while
  1483. p = parse_syntax_close(p + 1);
  1484. return p;
  1485. }
  1486. break;
  1487. case 'd':
  1488. case 'D':
  1489. if(p2 - p == 7 && !strncasecmp(p,"default",7)) {
  1490. // Switch - default processing
  1491. int pos = syntax.curly_count-1;
  1492. if(pos < 0 || syntax.curly[pos].type != TYPE_SWITCH) {
  1493. disp_error_message("parse_syntax: unexpected 'default'",p);
  1494. } else if(syntax.curly[pos].flag) {
  1495. disp_error_message("parse_syntax: dup 'default'",p);
  1496. } else {
  1497. char label[256];
  1498. int l;
  1499. // Put the label location
  1500. p = skip_space(p2);
  1501. if(*p != ':') {
  1502. disp_error_message("parse_syntax: expected ':'",p);
  1503. }
  1504. sprintf(label,"__SW%x_%x",syntax.curly[pos].index,syntax.curly[pos].count);
  1505. l=add_str(label);
  1506. set_label(l,script_pos,p);
  1507. // Skip to the next link w/o condition
  1508. sprintf(label,"goto __SW%x_%x;",syntax.curly[pos].index,syntax.curly[pos].count+1);
  1509. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1510. parse_line(label);
  1511. syntax.curly_count--;
  1512. // The default label
  1513. sprintf(label,"__SW%x_DEF",syntax.curly[pos].index);
  1514. l=add_str(label);
  1515. set_label(l,script_pos,p);
  1516. syntax.curly[syntax.curly_count - 1].flag = 1;
  1517. syntax.curly[pos].count++;
  1518. }
  1519. return p + 1;
  1520. } else if(p2 - p == 2 && !strncasecmp(p,"do",2)) {
  1521. int l;
  1522. char label[256];
  1523. p=skip_space(p2);
  1524. syntax.curly[syntax.curly_count].type = TYPE_DO;
  1525. syntax.curly[syntax.curly_count].count = 1;
  1526. syntax.curly[syntax.curly_count].index = syntax.index++;
  1527. syntax.curly[syntax.curly_count].flag = 0;
  1528. // Label of the (do) form here
  1529. sprintf(label,"__DO%x_BGN",syntax.curly[syntax.curly_count].index);
  1530. l=add_str(label);
  1531. set_label(l,script_pos,p);
  1532. syntax.curly_count++;
  1533. return p;
  1534. }
  1535. break;
  1536. case 'f':
  1537. case 'F':
  1538. if(p2 - p == 3 && !strncasecmp(p,"for",3)) {
  1539. int l;
  1540. char label[256];
  1541. int pos = syntax.curly_count;
  1542. syntax.curly[syntax.curly_count].type = TYPE_FOR;
  1543. syntax.curly[syntax.curly_count].count = 1;
  1544. syntax.curly[syntax.curly_count].index = syntax.index++;
  1545. syntax.curly[syntax.curly_count].flag = 0;
  1546. syntax.curly_count++;
  1547. p=skip_space(p2);
  1548. if(*p != '(')
  1549. disp_error_message("parse_syntax: expected '('",p);
  1550. p++;
  1551. // Execute the initialization statement
  1552. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1553. p=parse_line(p);
  1554. syntax.curly_count--;
  1555. // Form the start of label decision
  1556. sprintf(label,"__FR%x_J",syntax.curly[pos].index);
  1557. l=add_str(label);
  1558. set_label(l,script_pos,p);
  1559. p=skip_space(p);
  1560. if(*p == ';') {
  1561. // For (; Because the pattern of always true ;)
  1562. ;
  1563. } else {
  1564. // Skip to the end point if the condition is false
  1565. sprintf(label,"__FR%x_FIN",syntax.curly[pos].index);
  1566. add_scriptl(add_str("jump_zero"));
  1567. add_scriptc(C_ARG);
  1568. p=parse_expr(p);
  1569. p=skip_space(p);
  1570. add_scriptl(add_str(label));
  1571. add_scriptc(C_FUNC);
  1572. }
  1573. if(*p != ';')
  1574. disp_error_message("parse_syntax: expected ';'",p);
  1575. p++;
  1576. // Skip to the beginning of the loop
  1577. sprintf(label,"goto __FR%x_BGN;",syntax.curly[pos].index);
  1578. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1579. parse_line(label);
  1580. syntax.curly_count--;
  1581. // Labels to form the next loop
  1582. sprintf(label,"__FR%x_NXT",syntax.curly[pos].index);
  1583. l=add_str(label);
  1584. set_label(l,script_pos,p);
  1585. // Process the next time you enter the loop
  1586. // A ')' last for; flag to be treated as'
  1587. parse_syntax_for_flag = 1;
  1588. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1589. p=parse_line(p);
  1590. syntax.curly_count--;
  1591. parse_syntax_for_flag = 0;
  1592. // Skip to the determination process conditions
  1593. sprintf(label,"goto __FR%x_J;",syntax.curly[pos].index);
  1594. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1595. parse_line(label);
  1596. syntax.curly_count--;
  1597. // Loop start labeling
  1598. sprintf(label,"__FR%x_BGN",syntax.curly[pos].index);
  1599. l=add_str(label);
  1600. set_label(l,script_pos,p);
  1601. return p;
  1602. }
  1603. else if( p2 - p == 8 && strncasecmp(p,"function",8) == 0 )
  1604. {// internal script function
  1605. const char *func_name;
  1606. func_name = skip_space(p2);
  1607. p = skip_word(func_name);
  1608. if( p == func_name )
  1609. disp_error_message("parse_syntax:function: function name is missing or invalid", p);
  1610. p2 = skip_space(p);
  1611. if( *p2 == ';' )
  1612. {// function <name> ;
  1613. // function declaration - just register the name
  1614. int l;
  1615. l = add_word(func_name);
  1616. if( str_data[l].type == C_NOP )// register only, if the name was not used by something else
  1617. str_data[l].type = C_USERFUNC;
  1618. else if( str_data[l].type == C_USERFUNC )
  1619. ; // already registered
  1620. else
  1621. disp_error_message("parse_syntax:function: function name is invalid", func_name);
  1622. // Close condition of if, for, while
  1623. p = parse_syntax_close(p2 + 1);
  1624. return p;
  1625. }
  1626. else if(*p2 == '{')
  1627. {// function <name> <line/block of code>
  1628. char label[256];
  1629. int l;
  1630. syntax.curly[syntax.curly_count].type = TYPE_USERFUNC;
  1631. syntax.curly[syntax.curly_count].count = 1;
  1632. syntax.curly[syntax.curly_count].index = syntax.index++;
  1633. syntax.curly[syntax.curly_count].flag = 0;
  1634. ++syntax.curly_count;
  1635. // Jump over the function code
  1636. sprintf(label, "goto __FN%x_FIN;", syntax.curly[syntax.curly_count-1].index);
  1637. syntax.curly[syntax.curly_count].type = TYPE_NULL;
  1638. ++syntax.curly_count;
  1639. parse_line(label);
  1640. --syntax.curly_count;
  1641. // Set the position of the function (label)
  1642. l=add_word(func_name);
  1643. if( str_data[l].type == C_NOP || str_data[l].type == C_USERFUNC )// register only, if the name was not used by something else
  1644. {
  1645. str_data[l].type = C_USERFUNC;
  1646. set_label(l, script_pos, p);
  1647. if( parse_options&SCRIPT_USE_LABEL_DB )
  1648. strdb_iput(scriptlabel_db, get_str(l), script_pos);
  1649. }
  1650. else
  1651. disp_error_message("parse_syntax:function: function name is invalid", func_name);
  1652. return skip_space(p);
  1653. }
  1654. else
  1655. {
  1656. disp_error_message("expect ';' or '{' at function syntax",p);
  1657. }
  1658. }
  1659. break;
  1660. case 'i':
  1661. case 'I':
  1662. if(p2 - p == 2 && !strncasecmp(p,"if",2)) {
  1663. // If process
  1664. char label[256];
  1665. p=skip_space(p2);
  1666. if(*p != '(') { //Prevent if this {} non-c syntax. from Rayce (jA)
  1667. disp_error_message("need '('",p);
  1668. }
  1669. syntax.curly[syntax.curly_count].type = TYPE_IF;
  1670. syntax.curly[syntax.curly_count].count = 1;
  1671. syntax.curly[syntax.curly_count].index = syntax.index++;
  1672. syntax.curly[syntax.curly_count].flag = 0;
  1673. sprintf(label,"__IF%x_%x",syntax.curly[syntax.curly_count].index,syntax.curly[syntax.curly_count].count);
  1674. syntax.curly_count++;
  1675. add_scriptl(add_str("jump_zero"));
  1676. add_scriptc(C_ARG);
  1677. p=parse_expr(p);
  1678. p=skip_space(p);
  1679. add_scriptl(add_str(label));
  1680. add_scriptc(C_FUNC);
  1681. return p;
  1682. }
  1683. break;
  1684. case 's':
  1685. case 'S':
  1686. if(p2 - p == 6 && !strncasecmp(p,"switch",6)) {
  1687. // Processing of switch ()
  1688. char label[256];
  1689. p=skip_space(p2);
  1690. if(*p != '(') {
  1691. disp_error_message("need '('",p);
  1692. }
  1693. syntax.curly[syntax.curly_count].type = TYPE_SWITCH;
  1694. syntax.curly[syntax.curly_count].count = 1;
  1695. syntax.curly[syntax.curly_count].index = syntax.index++;
  1696. syntax.curly[syntax.curly_count].flag = 0;
  1697. sprintf(label,"$@__SW%x_VAL",syntax.curly[syntax.curly_count].index);
  1698. syntax.curly_count++;
  1699. add_scriptl(add_str("set"));
  1700. add_scriptc(C_ARG);
  1701. add_scriptl(add_str(label));
  1702. p=parse_expr(p);
  1703. p=skip_space(p);
  1704. if(*p != '{') {
  1705. disp_error_message("parse_syntax: expected '{'",p);
  1706. }
  1707. add_scriptc(C_FUNC);
  1708. return p + 1;
  1709. }
  1710. break;
  1711. case 'w':
  1712. case 'W':
  1713. if(p2 - p == 5 && !strncasecmp(p,"while",5)) {
  1714. int l;
  1715. char label[256];
  1716. p=skip_space(p2);
  1717. if(*p != '(') {
  1718. disp_error_message("need '('",p);
  1719. }
  1720. syntax.curly[syntax.curly_count].type = TYPE_WHILE;
  1721. syntax.curly[syntax.curly_count].count = 1;
  1722. syntax.curly[syntax.curly_count].index = syntax.index++;
  1723. syntax.curly[syntax.curly_count].flag = 0;
  1724. // Form the start of label decision
  1725. sprintf(label,"__WL%x_NXT",syntax.curly[syntax.curly_count].index);
  1726. l=add_str(label);
  1727. set_label(l,script_pos,p);
  1728. // Skip to the end point if the condition is false
  1729. sprintf(label,"__WL%x_FIN",syntax.curly[syntax.curly_count].index);
  1730. syntax.curly_count++;
  1731. add_scriptl(add_str("jump_zero"));
  1732. add_scriptc(C_ARG);
  1733. p=parse_expr(p);
  1734. p=skip_space(p);
  1735. add_scriptl(add_str(label));
  1736. add_scriptc(C_FUNC);
  1737. return p;
  1738. }
  1739. break;
  1740. }
  1741. return NULL;
  1742. }
  1743. const char* parse_syntax_close(const char *p) {
  1744. // If (...) for (...) hoge (); as to make sure closed closed once again
  1745. int flag;
  1746. do {
  1747. p = parse_syntax_close_sub(p,&flag);
  1748. } while(flag);
  1749. return p;
  1750. }
  1751. // Close judgment if, for, while, of do
  1752. // flag == 1 : closed
  1753. // flag == 0 : not closed
  1754. const char* parse_syntax_close_sub(const char* p,int* flag)
  1755. {
  1756. char label[256];
  1757. int pos = syntax.curly_count - 1;
  1758. int l;
  1759. *flag = 1;
  1760. if(syntax.curly_count <= 0) {
  1761. *flag = 0;
  1762. return p;
  1763. } else if(syntax.curly[pos].type == TYPE_IF) {
  1764. const char *bp = p;
  1765. const char *p2;
  1766. // if-block and else-block end is a new line
  1767. parse_nextline(false, p);
  1768. // Skip to the last location if
  1769. sprintf(label,"goto __IF%x_FIN;",syntax.curly[pos].index);
  1770. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1771. parse_line(label);
  1772. syntax.curly_count--;
  1773. // Put the label of the location
  1774. sprintf(label,"__IF%x_%x",syntax.curly[pos].index,syntax.curly[pos].count);
  1775. l=add_str(label);
  1776. set_label(l,script_pos,p);
  1777. syntax.curly[pos].count++;
  1778. p = skip_space(p);
  1779. p2 = skip_word(p);
  1780. if(!syntax.curly[pos].flag && p2 - p == 4 && !strncasecmp(p,"else",4)) {
  1781. // else or else - if
  1782. p = skip_space(p2);
  1783. p2 = skip_word(p);
  1784. if(p2 - p == 2 && !strncasecmp(p,"if",2)) {
  1785. // else - if
  1786. p=skip_space(p2);
  1787. if(*p != '(') {
  1788. disp_error_message("need '('",p);
  1789. }
  1790. sprintf(label,"__IF%x_%x",syntax.curly[pos].index,syntax.curly[pos].count);
  1791. add_scriptl(add_str("jump_zero"));
  1792. add_scriptc(C_ARG);
  1793. p=parse_expr(p);
  1794. p=skip_space(p);
  1795. add_scriptl(add_str(label));
  1796. add_scriptc(C_FUNC);
  1797. *flag = 0;
  1798. return p;
  1799. } else {
  1800. // else
  1801. if(!syntax.curly[pos].flag) {
  1802. syntax.curly[pos].flag = 1;
  1803. *flag = 0;
  1804. return p;
  1805. }
  1806. }
  1807. }
  1808. // Close if
  1809. syntax.curly_count--;
  1810. // Put the label of the final location
  1811. sprintf(label,"__IF%x_FIN",syntax.curly[pos].index);
  1812. l=add_str(label);
  1813. set_label(l,script_pos,p);
  1814. if(syntax.curly[pos].flag == 1) {
  1815. // Because the position of the pointer is the same if not else for this
  1816. return bp;
  1817. }
  1818. return p;
  1819. } else if(syntax.curly[pos].type == TYPE_DO) {
  1820. int l2;
  1821. char label2[256];
  1822. const char *p2;
  1823. if(syntax.curly[pos].flag) {
  1824. // (Come here continue) to form the label here
  1825. sprintf(label2,"__DO%x_NXT",syntax.curly[pos].index);
  1826. l2=add_str(label2);
  1827. set_label(l2,script_pos,p);
  1828. }
  1829. // Skip to the end point if the condition is false
  1830. p = skip_space(p);
  1831. p2 = skip_word(p);
  1832. if(p2 - p != 5 || strncasecmp(p,"while",5))
  1833. disp_error_message("parse_syntax: expected 'while'",p);
  1834. p = skip_space(p2);
  1835. if(*p != '(') {
  1836. disp_error_message("need '('",p);
  1837. }
  1838. // do-block end is a new line
  1839. parse_nextline(false, p);
  1840. sprintf(label2,"__DO%x_FIN",syntax.curly[pos].index);
  1841. add_scriptl(add_str("jump_zero"));
  1842. add_scriptc(C_ARG);
  1843. p=parse_expr(p);
  1844. p=skip_space(p);
  1845. add_scriptl(add_str(label2));
  1846. add_scriptc(C_FUNC);
  1847. // Skip to the starting point
  1848. sprintf(label2,"goto __DO%x_BGN;",syntax.curly[pos].index);
  1849. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1850. parse_line(label2);
  1851. syntax.curly_count--;
  1852. // Form label of the end point conditions
  1853. sprintf(label2,"__DO%x_FIN",syntax.curly[pos].index);
  1854. l2=add_str(label2);
  1855. set_label(l2,script_pos,p);
  1856. p = skip_space(p);
  1857. if(*p != ';') {
  1858. disp_error_message("parse_syntax: expected ';'",p);
  1859. return p+1;
  1860. }
  1861. p++;
  1862. syntax.curly_count--;
  1863. return p;
  1864. } else if(syntax.curly[pos].type == TYPE_FOR) {
  1865. // for-block end is a new line
  1866. parse_nextline(false, p);
  1867. // Skip to the next loop
  1868. sprintf(label,"goto __FR%x_NXT;",syntax.curly[pos].index);
  1869. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1870. parse_line(label);
  1871. syntax.curly_count--;
  1872. // End for labeling
  1873. sprintf(label,"__FR%x_FIN",syntax.curly[pos].index);
  1874. l=add_str(label);
  1875. set_label(l,script_pos,p);
  1876. syntax.curly_count--;
  1877. return p;
  1878. } else if(syntax.curly[pos].type == TYPE_WHILE) {
  1879. // while-block end is a new line
  1880. parse_nextline(false, p);
  1881. // Skip to the decision while
  1882. sprintf(label,"goto __WL%x_NXT;",syntax.curly[pos].index);
  1883. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1884. parse_line(label);
  1885. syntax.curly_count--;
  1886. // End while labeling
  1887. sprintf(label,"__WL%x_FIN",syntax.curly[pos].index);
  1888. l=add_str(label);
  1889. set_label(l,script_pos,p);
  1890. syntax.curly_count--;
  1891. return p;
  1892. } else if(syntax.curly[syntax.curly_count-1].type == TYPE_USERFUNC) {
  1893. int pos2 = syntax.curly_count-1;
  1894. char label2[256];
  1895. int l2;
  1896. // Back
  1897. sprintf(label2,"return;");
  1898. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1899. parse_line(label2);
  1900. syntax.curly_count--;
  1901. // Put the label of the location
  1902. sprintf(label2,"__FN%x_FIN",syntax.curly[pos2].index);
  1903. l2=add_str(label2);
  1904. set_label(l2,script_pos,p);
  1905. syntax.curly_count--;
  1906. return p;
  1907. } else {
  1908. *flag = 0;
  1909. return p;
  1910. }
  1911. }
  1912. /*==========================================
  1913. * Added built-in functions
  1914. *------------------------------------------*/
  1915. static void add_buildin_func(void)
  1916. {
  1917. int i;
  1918. for( i = 0; buildin_func[i].func; i++ )
  1919. {
  1920. // arg must follow the pattern: (v|s|i|r|l)*\?*\*?
  1921. // 'v' - value (either string or int or reference)
  1922. // 's' - string
  1923. // 'i' - int
  1924. // 'r' - reference (of a variable)
  1925. // 'l' - label
  1926. // '?' - one optional parameter
  1927. // '*' - unknown number of optional parameters
  1928. const char* p = buildin_func[i].arg;
  1929. while( *p == 'v' || *p == 's' || *p == 'i' || *p == 'r' || *p == 'l' ) ++p;
  1930. while( *p == '?' ) ++p;
  1931. if( *p == '*' ) ++p;
  1932. if( *p != 0){
  1933. ShowWarning("add_buildin_func: ignoring function \"%s\" with invalid arg \"%s\".\n", buildin_func[i].name, buildin_func[i].arg);
  1934. } else if( *skip_word(buildin_func[i].name) != 0 ){
  1935. ShowWarning("add_buildin_func: ignoring function with invalid name \"%s\" (must be a word).\n", buildin_func[i].name);
  1936. } else {
  1937. int n = add_str(buildin_func[i].name);
  1938. str_data[n].type = C_FUNC;
  1939. str_data[n].val = i;
  1940. str_data[n].func = buildin_func[i].func;
  1941. if (!strcmp(buildin_func[i].name, "set")) buildin_set_ref = n;
  1942. else if (!strcmp(buildin_func[i].name, "callsub")) buildin_callsub_ref = n;
  1943. else if (!strcmp(buildin_func[i].name, "callfunc")) buildin_callfunc_ref = n;
  1944. else if( !strcmp(buildin_func[i].name, "getelementofarray") ) buildin_getelementofarray_ref = n;
  1945. }
  1946. }
  1947. }
  1948. /// Retrieves the value of a constant.
  1949. bool script_get_constant(const char* name, int* value)
  1950. {
  1951. int n = search_str(name);
  1952. if( n == -1 || str_data[n].type != C_INT )
  1953. {// not found or not a constant
  1954. return false;
  1955. }
  1956. value[0] = str_data[n].val;
  1957. return true;
  1958. }
  1959. /// Creates new constant or parameter with given value.
  1960. void script_set_constant(const char* name, int value, bool isparameter)
  1961. {
  1962. int n = add_str(name);
  1963. if( str_data[n].type == C_NOP )
  1964. {// new
  1965. str_data[n].type = isparameter ? C_PARAM : C_INT;
  1966. str_data[n].val = value;
  1967. }
  1968. else if( str_data[n].type == C_PARAM || str_data[n].type == C_INT )
  1969. {// existing parameter or constant
  1970. ShowError("script_set_constant: Attempted to overwrite existing %s '%s' (old value=%d, new value=%d).\n", ( str_data[n].type == C_PARAM ) ? "parameter" : "constant", name, str_data[n].val, value);
  1971. }
  1972. else
  1973. {// existing name
  1974. ShowError("script_set_constant: Invalid name for %s '%s' (already defined as %s).\n", isparameter ? "parameter" : "constant", name, script_op2name(str_data[n].type));
  1975. }
  1976. }
  1977. /*==========================================
  1978. * Reading constant databases
  1979. * const.txt
  1980. *------------------------------------------*/
  1981. static void read_constdb(void)
  1982. {
  1983. FILE *fp;
  1984. char line[1024],name[1024],val[1024];
  1985. int type;
  1986. int entries=0, skipped=0, linenum=0;
  1987. sprintf(line, "%s/const.txt", db_path);
  1988. fp=fopen(line, "r");
  1989. if(fp==NULL){
  1990. ShowError("can't read %s\n", line);
  1991. return ;
  1992. }
  1993. while(fgets(line, sizeof(line), fp))
  1994. {
  1995. linenum++;
  1996. if( line[0] == '\0' || line[0] == '\n' || line[0] == '\r') //ignore empty line
  1997. continue;
  1998. if(line[0]=='/' && line[1]=='/') //ignore commented line
  1999. continue;
  2000. type=0;
  2001. if(sscanf(line,"%1023[A-Za-z0-9/_],%1023[A-Za-z0-9/_-],%11d",name,val,&type)>=2 ||
  2002. sscanf(line,"%1023[A-Za-z0-9/_] %1023[A-Za-z0-9/_-] %11d",name,val,&type)>=2){
  2003. entries++;
  2004. script_set_constant(name, (int)strtol(val, NULL, 0), (bool)type);
  2005. }
  2006. else {
  2007. skipped++;
  2008. ShowWarning("Skipping line '"CL_WHITE"%d"CL_RESET"', invalide constant definition\n",linenum);
  2009. }
  2010. }
  2011. fclose(fp);
  2012. ShowStatus("Done reading '"CL_WHITE"%d"CL_RESET"' entries in '"CL_WHITE"%s/const.txt"CL_RESET"'.\n", entries, db_path);
  2013. if(skipped){
  2014. ShowWarning("Skipped '"CL_WHITE"%d"CL_RESET"', entries\n",skipped);
  2015. }
  2016. }
  2017. /**
  2018. * Sets source-end constants for NPC scripts to access.
  2019. **/
  2020. void script_hardcoded_constants(void) {
  2021. #include "script_constants.h"
  2022. }
  2023. /*==========================================
  2024. * Display emplacement line of script
  2025. *------------------------------------------*/
  2026. static const char* script_print_line(StringBuf* buf, const char* p, const char* mark, int line)
  2027. {
  2028. int i;
  2029. if( p == NULL || !p[0] ) return NULL;
  2030. if( line < 0 )
  2031. StringBuf_Printf(buf, "*% 5d : ", -line);
  2032. else
  2033. StringBuf_Printf(buf, " % 5d : ", line);
  2034. for(i=0;p[i] && p[i] != '\n';i++){
  2035. if(p + i != mark)
  2036. StringBuf_Printf(buf, "%c", p[i]);
  2037. else
  2038. StringBuf_Printf(buf, "\'%c\'", p[i]);
  2039. }
  2040. StringBuf_AppendStr(buf, "\n");
  2041. return p+i+(p[i] == '\n' ? 1 : 0);
  2042. }
  2043. void script_errorwarning_sub(StringBuf *buf, const char* src, const char* file, int start_line, const char* error_msg_cur, const char* error_pos_cur) {
  2044. // Find the line where the error occurred
  2045. int j;
  2046. int line = start_line;
  2047. const char *p;
  2048. const char *linestart[5] = { NULL, NULL, NULL, NULL, NULL };
  2049. for(p=src;p && *p;line++){
  2050. const char *lineend=strchr(p,'\n');
  2051. if(lineend==NULL || error_pos_cur<lineend){
  2052. break;
  2053. }
  2054. for( j = 0; j < 4; j++ ) {
  2055. linestart[j] = linestart[j+1];
  2056. }
  2057. linestart[4] = p;
  2058. p=lineend+1;
  2059. }
  2060. StringBuf_Printf(buf, "script error on %s line %d\n", file, line);
  2061. StringBuf_Printf(buf, " %s\n", error_msg_cur);
  2062. for(j = 0; j < 5; j++ ) {
  2063. script_print_line(buf, linestart[j], NULL, line + j - 5);
  2064. }
  2065. p = script_print_line(buf, p, error_pos_cur, -line);
  2066. for(j = 0; j < 5; j++) {
  2067. p = script_print_line(buf, p, NULL, line + j + 1 );
  2068. }
  2069. }
  2070. void script_error(const char* src, const char* file, int start_line, const char* error_msg_cur, const char* error_pos_cur) {
  2071. StringBuf buf;
  2072. StringBuf_Init(&buf);
  2073. StringBuf_AppendStr(&buf, "\a\n");
  2074. script_errorwarning_sub(&buf, src, file, start_line, error_msg_cur, error_pos_cur);
  2075. ShowError("%s", StringBuf_Value(&buf));
  2076. StringBuf_Destroy(&buf);
  2077. }
  2078. void script_warning(const char* src, const char* file, int start_line, const char* error_msg_cur, const char* error_pos_cur) {
  2079. StringBuf buf;
  2080. StringBuf_Init(&buf);
  2081. script_errorwarning_sub(&buf, src, file, start_line, error_msg_cur, error_pos_cur);
  2082. ShowWarning("%s", StringBuf_Value(&buf));
  2083. StringBuf_Destroy(&buf);
  2084. }
  2085. /*==========================================
  2086. * Analysis of the script
  2087. *------------------------------------------*/
  2088. struct script_code* parse_script(const char *src,const char *file,int line,int options)
  2089. {
  2090. const char *p,*tmpp;
  2091. int i;
  2092. struct script_code* code = NULL;
  2093. static int first=1;
  2094. char end;
  2095. bool unresolved_names = false;
  2096. parser_current_src = src;
  2097. parser_current_file = file;
  2098. parser_current_line = line;
  2099. if( src == NULL )
  2100. return NULL;// empty script
  2101. memset(&syntax,0,sizeof(syntax));
  2102. if(first){
  2103. add_buildin_func();
  2104. read_constdb();
  2105. script_hardcoded_constants();
  2106. first=0;
  2107. }
  2108. script_buf=(unsigned char *)aMalloc(SCRIPT_BLOCK_SIZE*sizeof(unsigned char));
  2109. script_pos=0;
  2110. script_size=SCRIPT_BLOCK_SIZE;
  2111. parse_nextline(true, NULL);
  2112. // who called parse_script is responsible for clearing the database after using it, but just in case... lets clear it here
  2113. if( options&SCRIPT_USE_LABEL_DB )
  2114. db_clear(scriptlabel_db);
  2115. parse_options = options;
  2116. if( setjmp( error_jump ) != 0 ) {
  2117. //Restore program state when script has problems. [from jA]
  2118. int j;
  2119. const int size = ARRAYLENGTH(syntax.curly);
  2120. if( error_report )
  2121. script_error(src,file,line,error_msg,error_pos);
  2122. aFree( error_msg );
  2123. aFree( script_buf );
  2124. script_pos = 0;
  2125. script_size = 0;
  2126. script_buf = NULL;
  2127. for(j=LABEL_START;j<str_num;j++)
  2128. if(str_data[j].type == C_NOP) str_data[j].type = C_NAME;
  2129. for(j=0; j<size; j++)
  2130. linkdb_final(&syntax.curly[j].case_label);
  2131. return NULL;
  2132. }
  2133. parse_syntax_for_flag=0;
  2134. p=src;
  2135. p=skip_space(p);
  2136. if( options&SCRIPT_IGNORE_EXTERNAL_BRACKETS )
  2137. {// does not require brackets around the script
  2138. if( *p == '\0' && !(options&SCRIPT_RETURN_EMPTY_SCRIPT) )
  2139. {// empty script and can return NULL
  2140. aFree( script_buf );
  2141. script_pos = 0;
  2142. script_size = 0;
  2143. script_buf = NULL;
  2144. return NULL;
  2145. }
  2146. end = '\0';
  2147. }
  2148. else
  2149. {// requires brackets around the script
  2150. if( *p != '{' )
  2151. disp_error_message("not found '{'",p);
  2152. p = skip_space(p+1);
  2153. if( *p == '}' && !(options&SCRIPT_RETURN_EMPTY_SCRIPT) )
  2154. {// empty script and can return NULL
  2155. aFree( script_buf );
  2156. script_pos = 0;
  2157. script_size = 0;
  2158. script_buf = NULL;
  2159. return NULL;
  2160. }
  2161. end = '}';
  2162. }
  2163. // clear references of labels, variables and internal functions
  2164. for(i=LABEL_START;i<str_num;i++){
  2165. if(
  2166. str_data[i].type==C_POS || str_data[i].type==C_NAME ||
  2167. str_data[i].type==C_USERFUNC || str_data[i].type == C_USERFUNC_POS
  2168. ){
  2169. str_data[i].type=C_NOP;
  2170. str_data[i].backpatch=-1;
  2171. str_data[i].label=-1;
  2172. }
  2173. }
  2174. while( syntax.curly_count != 0 || *p != end )
  2175. {
  2176. if( *p == '\0' )
  2177. disp_error_message("unexpected end of script",p);
  2178. // Special handling only label
  2179. tmpp=skip_space(skip_word(p));
  2180. if(*tmpp==':' && !(!strncasecmp(p,"default:",8) && p + 7 == tmpp)){
  2181. i=add_word(p);
  2182. set_label(i,script_pos,p);
  2183. if( parse_options&SCRIPT_USE_LABEL_DB )
  2184. strdb_iput(scriptlabel_db, get_str(i), script_pos);
  2185. p=tmpp+1;
  2186. p=skip_space(p);
  2187. continue;
  2188. }
  2189. // All other lumped
  2190. p=parse_line(p);
  2191. p=skip_space(p);
  2192. parse_nextline(false, p);
  2193. }
  2194. add_scriptc(C_NOP);
  2195. // trim code to size
  2196. script_size = script_pos;
  2197. RECREATE(script_buf,unsigned char,script_pos);
  2198. // default unknown references to variables
  2199. for(i=LABEL_START;i<str_num;i++){
  2200. if(str_data[i].type==C_NOP){
  2201. int j;
  2202. str_data[i].type=C_NAME;
  2203. str_data[i].label=i;
  2204. for(j=str_data[i].backpatch;j>=0 && j!=0x00ffffff;){
  2205. int next=GETVALUE(script_buf,j);
  2206. SETVALUE(script_buf,j,i);
  2207. j=next;
  2208. }
  2209. }
  2210. else if( str_data[i].type == C_USERFUNC )
  2211. {// 'function name;' without follow-up code
  2212. ShowError("parse_script: function '%s' declared but not defined.\n", str_buf+str_data[i].str);
  2213. unresolved_names = true;
  2214. }
  2215. }
  2216. if( unresolved_names )
  2217. {
  2218. disp_error_message("parse_script: unresolved function references", p);
  2219. }
  2220. #ifdef DEBUG_DISP
  2221. for(i=0;i<script_pos;i++){
  2222. if((i&15)==0) ShowMessage("%04x : ",i);
  2223. ShowMessage("%02x ",script_buf[i]);
  2224. if((i&15)==15) ShowMessage("\n");
  2225. }
  2226. ShowMessage("\n");
  2227. #endif
  2228. #ifdef DEBUG_DISASM
  2229. {
  2230. int i = 0,j;
  2231. while(i < script_pos) {
  2232. c_op op = get_com(script_buf,&i);
  2233. ShowMessage("%06x %s", i, script_op2name(op));
  2234. j = i;
  2235. switch(op) {
  2236. case C_INT:
  2237. ShowMessage(" %d", get_num(script_buf,&i));
  2238. break;
  2239. case C_POS:
  2240. ShowMessage(" 0x%06x", *(int*)(script_buf+i)&0xffffff);
  2241. i += 3;
  2242. break;
  2243. case C_NAME:
  2244. j = (*(int*)(script_buf+i)&0xffffff);
  2245. ShowMessage(" %s", ( j == 0xffffff ) ? "?? unknown ??" : get_str(j));
  2246. i += 3;
  2247. break;
  2248. case C_STR:
  2249. j = strlen(script_buf + i);
  2250. ShowMessage(" %s", script_buf + i);
  2251. i += j+1;
  2252. break;
  2253. }
  2254. ShowMessage(CL_CLL"\n");
  2255. }
  2256. }
  2257. #endif
  2258. CREATE(code,struct script_code,1);
  2259. code->script_buf = script_buf;
  2260. code->script_size = script_size;
  2261. code->local.vars = NULL;
  2262. code->local.arrays = NULL;
  2263. return code;
  2264. }
  2265. /// Returns the player attached to this script, identified by the rid.
  2266. /// If there is no player attached, the script is terminated.
  2267. TBL_PC *script_rid2sd(struct script_state *st)
  2268. {
  2269. TBL_PC *sd=map_id2sd(st->rid);
  2270. if(!sd){
  2271. ShowError("script_rid2sd: fatal error ! player not attached!\n");
  2272. script_reportfunc(st);
  2273. script_reportsrc(st);
  2274. st->state = END;
  2275. }
  2276. return sd;
  2277. }
  2278. /**
  2279. * Dereferences a variable/constant, replacing it with a copy of the value.
  2280. * @param st Script state
  2281. * @param data Variable/constant
  2282. * @param sd If NULL, will try to use sd from st->rid (for player's variables)
  2283. */
  2284. void get_val_(struct script_state* st, struct script_data* data, struct map_session_data *sd)
  2285. {
  2286. const char* name;
  2287. char prefix;
  2288. char postfix;
  2289. if( !data_isreference(data) )
  2290. return;// not a variable/constant
  2291. name = reference_getname(data);
  2292. prefix = name[0];
  2293. postfix = name[strlen(name) - 1];
  2294. //##TODO use reference_tovariable(data) when it's confirmed that it works [FlavioJS]
  2295. if( !reference_toconstant(data) && not_server_variable(prefix) ) {
  2296. if (sd == NULL)
  2297. sd = script_rid2sd(st);
  2298. if( sd == NULL ) {// needs player attached
  2299. if( postfix == '$' ) {// string variable
  2300. ShowWarning("script:get_val: cannot access player variable '%s', defaulting to \"\"\n", name);
  2301. data->type = C_CONSTSTR;
  2302. data->u.str = "";
  2303. } else {// integer variable
  2304. ShowWarning("script:get_val: cannot access player variable '%s', defaulting to 0\n", name);
  2305. data->type = C_INT;
  2306. data->u.num = 0;
  2307. }
  2308. return;
  2309. }
  2310. }
  2311. if( postfix == '$' ) {// string variable
  2312. switch( prefix ) {
  2313. case '@':
  2314. data->u.str = pc_readregstr(sd, data->u.num);
  2315. break;
  2316. case '$':
  2317. data->u.str = mapreg_readregstr(data->u.num);
  2318. break;
  2319. case '#':
  2320. if( name[1] == '#' )
  2321. data->u.str = pc_readaccountreg2str(sd, data->u.num);// global
  2322. else
  2323. data->u.str = pc_readaccountregstr(sd, data->u.num);// local
  2324. break;
  2325. case '.':
  2326. {
  2327. struct DBMap* n = data->ref ?
  2328. data->ref->vars : name[1] == '@' ?
  2329. st->stack->scope.vars : // instance/scope variable
  2330. st->script->local.vars; // npc variable
  2331. if( n )
  2332. data->u.str = (char*)i64db_get(n,reference_getuid(data));
  2333. else
  2334. data->u.str = NULL;
  2335. }
  2336. break;
  2337. case '\'':
  2338. {
  2339. int instance_id = script_instancegetid(st);
  2340. if( instance_id )
  2341. data->u.str = (char*)i64db_get(instance_data[instance_id].regs.vars,reference_getuid(data));
  2342. else {
  2343. ShowWarning("script:get_val: cannot access instance variable '%s', defaulting to \"\"\n", name);
  2344. data->u.str = NULL;
  2345. }
  2346. }
  2347. break;
  2348. default:
  2349. data->u.str = pc_readglobalreg_str(sd, data->u.num);
  2350. break;
  2351. }
  2352. if( data->u.str == NULL || data->u.str[0] == '\0' ) {// empty string
  2353. data->type = C_CONSTSTR;
  2354. data->u.str = "";
  2355. } else {// duplicate string
  2356. data->type = C_STR;
  2357. data->u.str = aStrdup(data->u.str);
  2358. }
  2359. } else {// integer variable
  2360. data->type = C_INT;
  2361. if( reference_toconstant(data) ) {
  2362. data->u.num = reference_getconstant(data);
  2363. } else if( reference_toparam(data) ) {
  2364. data->u.num = pc_readparam(sd, reference_getparamtype(data));
  2365. } else
  2366. switch( prefix ) {
  2367. case '@':
  2368. data->u.num = pc_readreg(sd, data->u.num);
  2369. break;
  2370. case '$':
  2371. data->u.num = mapreg_readreg(data->u.num);
  2372. break;
  2373. case '#':
  2374. if( name[1] == '#' )
  2375. data->u.num = pc_readaccountreg2(sd, data->u.num);// global
  2376. else
  2377. data->u.num = pc_readaccountreg(sd, data->u.num);// local
  2378. break;
  2379. case '.':
  2380. {
  2381. struct DBMap* n = data->ref ?
  2382. data->ref->vars : name[1] == '@' ?
  2383. st->stack->scope.vars : // instance/scope variable
  2384. st->script->local.vars; // npc variable
  2385. if( n )
  2386. data->u.num = (int)i64db_iget(n,reference_getuid(data));
  2387. else
  2388. data->u.num = 0;
  2389. }
  2390. break;
  2391. case '\'':
  2392. {
  2393. int instance_id = script_instancegetid(st);
  2394. if( instance_id )
  2395. data->u.num = (int)i64db_iget(instance_data[instance_id].regs.vars,reference_getuid(data));
  2396. else {
  2397. ShowWarning("script:get_val: cannot access instance variable '%s', defaulting to 0\n", name);
  2398. data->u.num = 0;
  2399. }
  2400. }
  2401. break;
  2402. default:
  2403. data->u.num = pc_readglobalreg(sd, data->u.num);
  2404. break;
  2405. }
  2406. }
  2407. data->ref = NULL;
  2408. return;
  2409. }
  2410. void get_val(struct script_state* st, struct script_data* data)
  2411. {
  2412. get_val_(st,data,NULL);
  2413. }
  2414. struct script_data* push_val2(struct script_stack* stack, enum c_op type, int64 val, struct reg_db* ref);
  2415. /// Retrieves the value of a reference identified by uid (variable, constant, param)
  2416. /// The value is left in the top of the stack and needs to be removed manually.
  2417. /// @param st[in]: script state.
  2418. /// @param uid[in]: reference identifier.
  2419. /// @param ref[in]: the container to look up the reference into.
  2420. /// @return: the retrieved value of the reference.
  2421. void* get_val2(struct script_state* st, int64 uid, struct reg_db *ref)
  2422. {
  2423. struct script_data* data;
  2424. push_val2(st->stack, C_NAME, uid, ref);
  2425. data = script_getdatatop(st, -1);
  2426. get_val(st, data);
  2427. //! TODO: Support data->u.num as int64 instead cast it to int? (in get_val, it was casted to int).
  2428. return (data->type == C_INT ? (void*)__64BPRTSIZE((int)data->u.num) : (void*)__64BPRTSIZE(data->u.str));
  2429. }
  2430. /**
  2431. * Because, currently, array members with key 0 are indifferenciable from normal variables, we should ensure its actually in
  2432. * Will be gone as soon as undefined var feature is implemented
  2433. **/
  2434. void script_array_ensure_zero(struct script_state *st, struct map_session_data *sd, int64 uid, struct reg_db *ref)
  2435. {
  2436. const char *name = get_str(script_getvarid(uid));
  2437. // is here st can be null pointer and st->rid is wrong?
  2438. struct reg_db *src = script_array_src(st, sd ? sd : st->rid ? map_id2sd(st->rid) : NULL, name, ref);
  2439. bool insert = false;
  2440. if (sd && !st) {
  2441. // when sd comes, st isn't available
  2442. insert = true;
  2443. } else {
  2444. if( is_string_variable(name) ) {
  2445. char* str = (char*)get_val2(st, uid, ref);
  2446. if( str && *str )
  2447. insert = true;
  2448. script_removetop(st, -1, 0);
  2449. } else {
  2450. int32 num = (int32)__64BPRTSIZE(get_val2(st, uid, ref));
  2451. if( num )
  2452. insert = true;
  2453. script_removetop(st, -1, 0);
  2454. }
  2455. }
  2456. if (src && src->arrays) {
  2457. struct script_array *sa = idb_get(src->arrays, script_getvarid(uid));
  2458. if (sa) {
  2459. unsigned int i;
  2460. ARR_FIND(0, sa->size, i, sa->members[i] == 0);
  2461. if( i != sa->size ) {
  2462. if( !insert )
  2463. script_array_remove_member(src,sa,i);
  2464. return;
  2465. }
  2466. script_array_add_member(sa,0);
  2467. } else if (insert) {
  2468. script_array_update(src,reference_uid(script_getvarid(uid), 0),false);
  2469. }
  2470. }
  2471. }
  2472. /**
  2473. * Returns array size by ID
  2474. **/
  2475. unsigned int script_array_size(struct script_state *st, struct map_session_data *sd, const char *name, struct reg_db *ref)
  2476. {
  2477. struct script_array *sa = NULL;
  2478. struct reg_db *src = script_array_src(st, sd, name, ref);
  2479. if (src && src->arrays)
  2480. sa = idb_get(src->arrays, search_str(name));
  2481. return sa ? sa->size : 0;
  2482. }
  2483. /**
  2484. * Returns array's highest key (for that awful getarraysize implementation that doesn't really gets the array size)
  2485. **/
  2486. unsigned int script_array_highest_key(struct script_state *st, struct map_session_data *sd, const char *name, struct reg_db *ref)
  2487. {
  2488. struct script_array *sa = NULL;
  2489. struct reg_db *src = script_array_src(st, sd, name, ref);
  2490. if (src && src->arrays) {
  2491. int key = add_word(name);
  2492. script_array_ensure_zero(st,sd,reference_uid(key, 0), ref);
  2493. if( ( sa = idb_get(src->arrays, key) ) ) {
  2494. unsigned int i, highest_key = 0;
  2495. for(i = 0; i < sa->size; i++) {
  2496. if( sa->members[i] > highest_key )
  2497. highest_key = sa->members[i];
  2498. }
  2499. return sa->size ? highest_key + 1 : 0;
  2500. }
  2501. }
  2502. return SCRIPT_CMD_SUCCESS;
  2503. }
  2504. int script_free_array_db(DBKey key, DBData *data, va_list ap)
  2505. {
  2506. struct script_array *sa = db_data2ptr(data);
  2507. aFree(sa->members);
  2508. ers_free(array_ers, sa);
  2509. return SCRIPT_CMD_SUCCESS;
  2510. }
  2511. /**
  2512. * Clears script_array and removes it from script->array_db
  2513. **/
  2514. void script_array_delete(struct reg_db *src, struct script_array *sa)
  2515. {
  2516. aFree(sa->members);
  2517. idb_remove(src->arrays, sa->id);
  2518. ers_free(array_ers, sa);
  2519. }
  2520. /**
  2521. * Removes a member from a script_array list
  2522. *
  2523. * @param idx the index of the member in script_array struct list, not of the actual array member
  2524. **/
  2525. void script_array_remove_member(struct reg_db *src, struct script_array *sa, unsigned int idx)
  2526. {
  2527. unsigned int i, cursor;
  2528. // it's the only member left, no need to do anything other than delete the array data
  2529. if( sa->size == 1 ) {
  2530. script_array_delete(src,sa);
  2531. return;
  2532. }
  2533. sa->members[idx] = UINT_MAX;
  2534. for(i = 0, cursor = 0; i < sa->size; i++) {
  2535. if( sa->members[i] == UINT_MAX )
  2536. continue;
  2537. if( i != cursor )
  2538. sa->members[cursor] = sa->members[i];
  2539. cursor++;
  2540. }
  2541. sa->size = cursor;
  2542. }
  2543. /**
  2544. * Appends a new array index to the list in script_array
  2545. *
  2546. * @param idx the index of the array member being inserted
  2547. **/
  2548. void script_array_add_member(struct script_array *sa, unsigned int idx)
  2549. {
  2550. RECREATE(sa->members, unsigned int, ++sa->size);
  2551. sa->members[sa->size - 1] = idx;
  2552. }
  2553. /**
  2554. * Obtains the source of the array database for this type and scenario
  2555. * Initializes such database when not yet initialized.
  2556. **/
  2557. struct reg_db *script_array_src(struct script_state *st, struct map_session_data *sd, const char *name, struct reg_db *ref)
  2558. {
  2559. struct reg_db *src = NULL;
  2560. switch( name[0] ) {
  2561. // from player
  2562. default: // char reg
  2563. case '@': // temp char reg
  2564. case '#': // account reg
  2565. src = &sd->regs;
  2566. break;
  2567. case '$': // map reg
  2568. src = &regs;
  2569. break;
  2570. case '.': // npc/script
  2571. if (ref)
  2572. src = ref;
  2573. else
  2574. src = (name[1] == '@') ? &st->stack->scope : &st->script->local;
  2575. break;
  2576. case '\'': // instance
  2577. {
  2578. int instance_id = script_instancegetid(st);
  2579. if( instance_id ) {
  2580. src = &instance_data[instance_id].regs;
  2581. }
  2582. break;
  2583. }
  2584. }
  2585. if( src ) {
  2586. if( !src->arrays )
  2587. src->arrays = idb_alloc(DB_OPT_BASE);
  2588. return src;
  2589. }
  2590. return NULL;
  2591. }
  2592. /**
  2593. * Processes a array member modification, and update data accordingly
  2594. *
  2595. * @param src[in,out] Variable source database. If the array database doesn't exist, it is created.
  2596. * @param num[in] Variable ID
  2597. * @param empty[in] Whether the modified member is empty (needs to be removed)
  2598. **/
  2599. void script_array_update(struct reg_db *src, int64 num, bool empty)
  2600. {
  2601. struct script_array *sa = NULL;
  2602. int id = script_getvarid(num);
  2603. unsigned int index = script_getvaridx(num);
  2604. if (!src->arrays) {
  2605. src->arrays = idb_alloc(DB_OPT_BASE);
  2606. } else {
  2607. sa = idb_get(src->arrays, id);
  2608. }
  2609. if( sa ) {
  2610. unsigned int i;
  2611. // search
  2612. for(i = 0; i < sa->size; i++) {
  2613. if( sa->members[i] == index )
  2614. break;
  2615. }
  2616. // if existent
  2617. if( i != sa->size ) {
  2618. // if empty, we gotta remove it
  2619. if( empty ) {
  2620. script_array_remove_member(src, sa, i);
  2621. }
  2622. } else if( !empty ) { /* new entry */
  2623. script_array_add_member(sa,index);
  2624. // we do nothing if its empty, no point in modifying array data for a new empty member
  2625. }
  2626. } else if ( !empty ) { // we only move to create if not empty
  2627. sa = ers_alloc(array_ers, struct script_array);
  2628. sa->id = id;
  2629. sa->members = NULL;
  2630. sa->size = 0;
  2631. script_array_add_member(sa,index);
  2632. idb_put(src->arrays, id, sa);
  2633. }
  2634. }
  2635. /**
  2636. * Stores the value of a script variable
  2637. *
  2638. * @param st: current script state.
  2639. * @param sd: current character data.
  2640. * @param num: variable identifier.
  2641. * @param name: variable name.
  2642. * @param value: new value.
  2643. * @param ref: variable container, in case of a npc/scope variable reference outside the current scope.
  2644. * @return: 0 failure, 1 success.
  2645. *
  2646. * TODO: return values are screwed up, have been for some time (reaad: years), e.g. some functions return 1 failure and success.
  2647. *------------------------------------------*/
  2648. int set_reg(struct script_state* st, TBL_PC* sd, int64 num, const char* name, const void* value, struct reg_db *ref)
  2649. {
  2650. char prefix = name[0];
  2651. if( is_string_variable(name) ) {// string variable
  2652. const char *str = (const char*)value;
  2653. switch (prefix) {
  2654. case '@':
  2655. pc_setregstr(sd, num, str);
  2656. return 1;
  2657. case '$':
  2658. return mapreg_setregstr(num, str);
  2659. case '#':
  2660. return (name[1] == '#') ?
  2661. pc_setaccountreg2str(sd, num, str) :
  2662. pc_setaccountregstr(sd, num, str);
  2663. case '.':
  2664. {
  2665. struct reg_db *n = (ref) ? ref : (name[1] == '@') ? &st->stack->scope : &st->script->local;
  2666. if( n ) {
  2667. if (str[0]) {
  2668. i64db_put(n->vars, num, aStrdup(str));
  2669. if( script_getvaridx(num) )
  2670. script_array_update(n, num, false);
  2671. } else {
  2672. i64db_remove(n->vars, num);
  2673. if( script_getvaridx(num) )
  2674. script_array_update(n, num, true);
  2675. }
  2676. }
  2677. }
  2678. return 1;
  2679. case '\'':
  2680. {
  2681. int instance_id = script_instancegetid(st);
  2682. if( instance_id ) {
  2683. if( str[0] ) {
  2684. i64db_put(instance_data[instance_id].regs.vars, num, aStrdup(str));
  2685. if( script_getvaridx(num) )
  2686. script_array_update(&instance_data[instance_id].regs, num, false);
  2687. } else {
  2688. i64db_remove(instance_data[instance_id].regs.vars, num);
  2689. if (script_getvaridx(num))
  2690. script_array_update(&instance_data[instance_id].regs, num, true);
  2691. }
  2692. } else {
  2693. ShowError("script_set_reg: cannot write instance variable '%s', NPC not in a instance!\n", name);
  2694. script_reportsrc(st);
  2695. }
  2696. return 1;
  2697. }
  2698. default:
  2699. return pc_setglobalreg_str(sd, num, str);
  2700. }
  2701. } else {// integer variable
  2702. int val = (int)__64BPRTSIZE(value);
  2703. if(str_data[script_getvarid(num)].type == C_PARAM) {
  2704. if( pc_setparam(sd, str_data[script_getvarid(num)].val, val) == 0 ) {
  2705. if( st != NULL ) {
  2706. ShowError("script_set_reg: failed to set param '%s' to %d.\n", name, val);
  2707. script_reportsrc(st);
  2708. st->state = END;
  2709. }
  2710. return 0;
  2711. }
  2712. return 1;
  2713. }
  2714. switch (prefix) {
  2715. case '@':
  2716. pc_setreg(sd, num, val);
  2717. return 1;
  2718. case '$':
  2719. return mapreg_setreg(num, val);
  2720. case '#':
  2721. return (name[1] == '#') ?
  2722. pc_setaccountreg2(sd, num, val) :
  2723. pc_setaccountreg(sd, num, val);
  2724. case '.':
  2725. {
  2726. struct reg_db *n = (ref) ? ref : (name[1] == '@') ? &st->stack->scope : &st->script->local;
  2727. if( n ) {
  2728. if( val != 0 ) {
  2729. i64db_iput(n->vars, num, val);
  2730. if( script_getvaridx(num) )
  2731. script_array_update(n, num, false);
  2732. } else {
  2733. i64db_remove(n->vars, num);
  2734. if( script_getvaridx(num) )
  2735. script_array_update(n, num, true);
  2736. }
  2737. }
  2738. }
  2739. return 1;
  2740. case '\'':
  2741. {
  2742. int instance_id = script_instancegetid(st);
  2743. if( instance_id ) {
  2744. if( val != 0 ) {
  2745. i64db_iput(instance_data[instance_id].regs.vars, num, val);
  2746. if( script_getvaridx(num) )
  2747. script_array_update(&instance_data[instance_id].regs, num, false);
  2748. } else {
  2749. i64db_remove(instance_data[instance_id].regs.vars, num);
  2750. if (script_getvaridx(num))
  2751. script_array_update(&instance_data[instance_id].regs, num, true);
  2752. }
  2753. } else {
  2754. ShowError("script_set_reg: cannot write instance variable '%s', NPC not in a instance!\n", name);
  2755. script_reportsrc(st);
  2756. }
  2757. return 1;
  2758. }
  2759. default:
  2760. return pc_setglobalreg(sd, num, val);
  2761. }
  2762. }
  2763. }
  2764. int set_var(TBL_PC* sd, char* name, void* val)
  2765. {
  2766. return set_reg(NULL, sd, reference_uid(add_str(name),0), name, val, NULL);
  2767. }
  2768. void setd_sub(struct script_state *st, TBL_PC *sd, const char *varname, int elem, void *value, struct reg_db *ref)
  2769. {
  2770. set_reg(st, sd, reference_uid(add_str(varname),elem), varname, value, ref);
  2771. }
  2772. /**
  2773. * Converts the data to a string
  2774. * @param st
  2775. * @param data
  2776. * @param sd
  2777. */
  2778. const char* conv_str_(struct script_state* st, struct script_data* data, struct map_session_data *sd)
  2779. {
  2780. char* p;
  2781. get_val_(st, data, sd);
  2782. if( data_isstring(data) )
  2783. {// nothing to convert
  2784. }
  2785. else if( data_isint(data) )
  2786. {// int -> string
  2787. CREATE(p, char, ITEM_NAME_LENGTH);
  2788. snprintf(p, ITEM_NAME_LENGTH, "%"PRId64"", data->u.num);
  2789. p[ITEM_NAME_LENGTH-1] = '\0';
  2790. data->type = C_STR;
  2791. data->u.str = p;
  2792. }
  2793. else if( data_isreference(data) )
  2794. {// reference -> string
  2795. //##TODO when does this happen (check get_val) [FlavioJS]
  2796. data->type = C_CONSTSTR;
  2797. data->u.str = reference_getname(data);
  2798. }
  2799. else
  2800. {// unsupported data type
  2801. ShowError("script:conv_str: cannot convert to string, defaulting to \"\"\n");
  2802. script_reportdata(data);
  2803. script_reportsrc(st);
  2804. data->type = C_CONSTSTR;
  2805. data->u.str = "";
  2806. }
  2807. return data->u.str;
  2808. }
  2809. const char* conv_str(struct script_state* st, struct script_data* data)
  2810. {
  2811. return conv_str_(st, data, NULL);
  2812. }
  2813. /**
  2814. * Converts the data to an int
  2815. * @param st
  2816. * @param data
  2817. * @param sd
  2818. */
  2819. int conv_num_(struct script_state* st, struct script_data* data, struct map_session_data *sd)
  2820. {
  2821. get_val_(st, data, sd);
  2822. if( data_isint(data) )
  2823. {// nothing to convert
  2824. }
  2825. else if( data_isstring(data) )
  2826. {// string -> int
  2827. // the result does not overflow or underflow, it is capped instead
  2828. // ex: 999999999999 is capped to INT_MAX (2147483647)
  2829. char* p = data->u.str;
  2830. long num;
  2831. errno = 0;
  2832. num = strtol(data->u.str, NULL, 10);// change radix to 0 to support octal numbers "o377" and hex numbers "0xFF"
  2833. if( errno == ERANGE
  2834. #if LONG_MAX > INT_MAX
  2835. || num < INT_MIN || num > INT_MAX
  2836. #endif
  2837. )
  2838. {
  2839. if( num <= INT_MIN )
  2840. {
  2841. num = INT_MIN;
  2842. ShowError("script:conv_num: underflow detected, capping to %ld\n", num);
  2843. }
  2844. else//if( num >= INT_MAX )
  2845. {
  2846. num = INT_MAX;
  2847. ShowError("script:conv_num: overflow detected, capping to %ld\n", num);
  2848. }
  2849. script_reportdata(data);
  2850. script_reportsrc(st);
  2851. }
  2852. if( data->type == C_STR )
  2853. aFree(p);
  2854. data->type = C_INT;
  2855. data->u.num = (int)num;
  2856. }
  2857. #if 0
  2858. // FIXME this function is being used to retrieve the position of labels and
  2859. // probably other stuff [FlavioJS]
  2860. else
  2861. {// unsupported data type
  2862. ShowError("script:conv_num: cannot convert to number, defaulting to 0\n");
  2863. script_reportdata(data);
  2864. script_reportsrc(st);
  2865. data->type = C_INT;
  2866. data->u.num = 0;
  2867. }
  2868. #endif
  2869. return (int)data->u.num;
  2870. }
  2871. int conv_num(struct script_state* st, struct script_data* data)
  2872. {
  2873. return conv_num_(st, data, NULL);
  2874. }
  2875. //
  2876. // Stack operations
  2877. //
  2878. /// Increases the size of the stack
  2879. void stack_expand(struct script_stack* stack)
  2880. {
  2881. stack->sp_max += 64;
  2882. stack->stack_data = (struct script_data*)aRealloc(stack->stack_data,
  2883. stack->sp_max * sizeof(stack->stack_data[0]) );
  2884. memset(stack->stack_data + (stack->sp_max - 64), 0,
  2885. 64 * sizeof(stack->stack_data[0]) );
  2886. }
  2887. /// Pushes a value into the stack
  2888. #define push_val(stack,type,val) push_val2(stack, type, val, NULL)
  2889. /// Pushes a value into the stack (with reference)
  2890. struct script_data* push_val2(struct script_stack* stack, enum c_op type, int64 val, struct reg_db *ref)
  2891. {
  2892. if( stack->sp >= stack->sp_max )
  2893. stack_expand(stack);
  2894. stack->stack_data[stack->sp].type = type;
  2895. stack->stack_data[stack->sp].u.num = val;
  2896. stack->stack_data[stack->sp].ref = ref;
  2897. stack->sp++;
  2898. return &stack->stack_data[stack->sp-1];
  2899. }
  2900. /// Pushes a string into the stack
  2901. struct script_data* push_str(struct script_stack* stack, enum c_op type, char* str)
  2902. {
  2903. if( stack->sp >= stack->sp_max )
  2904. stack_expand(stack);
  2905. stack->stack_data[stack->sp].type = type;
  2906. stack->stack_data[stack->sp].u.str = str;
  2907. stack->stack_data[stack->sp].ref = NULL;
  2908. stack->sp++;
  2909. return &stack->stack_data[stack->sp-1];
  2910. }
  2911. /// Pushes a retinfo into the stack
  2912. struct script_data* push_retinfo(struct script_stack* stack, struct script_retinfo* ri, struct reg_db *ref)
  2913. {
  2914. if( stack->sp >= stack->sp_max )
  2915. stack_expand(stack);
  2916. stack->stack_data[stack->sp].type = C_RETINFO;
  2917. stack->stack_data[stack->sp].u.ri = ri;
  2918. stack->stack_data[stack->sp].ref = ref;
  2919. stack->sp++;
  2920. return &stack->stack_data[stack->sp-1];
  2921. }
  2922. /// Pushes a copy of the target position into the stack
  2923. struct script_data* push_copy(struct script_stack* stack, int pos)
  2924. {
  2925. switch( stack->stack_data[pos].type ) {
  2926. case C_CONSTSTR:
  2927. return push_str(stack, C_CONSTSTR, stack->stack_data[pos].u.str);
  2928. break;
  2929. case C_STR:
  2930. return push_str(stack, C_STR, aStrdup(stack->stack_data[pos].u.str));
  2931. break;
  2932. case C_RETINFO:
  2933. ShowFatalError("script:push_copy: can't create copies of C_RETINFO. Exiting...\n");
  2934. exit(1);
  2935. break;
  2936. default:
  2937. return push_val2(
  2938. stack,stack->stack_data[pos].type,
  2939. stack->stack_data[pos].u.num,
  2940. stack->stack_data[pos].ref
  2941. );
  2942. break;
  2943. }
  2944. }
  2945. /// Removes the values in indexes [start,end] from the stack.
  2946. /// Adjusts all stack pointers.
  2947. void pop_stack(struct script_state* st, int start, int end)
  2948. {
  2949. struct script_stack* stack = st->stack;
  2950. struct script_data* data;
  2951. int i;
  2952. if( start < 0 )
  2953. start = 0;
  2954. if( end > stack->sp )
  2955. end = stack->sp;
  2956. if( start >= end )
  2957. return;// nothing to pop
  2958. // free stack elements
  2959. for( i = start; i < end; i++ )
  2960. {
  2961. data = &stack->stack_data[i];
  2962. if( data->type == C_STR )
  2963. aFree(data->u.str);
  2964. if( data->type == C_RETINFO ) {
  2965. struct script_retinfo* ri = data->u.ri;
  2966. if (ri->scope.vars) {
  2967. script_free_vars(ri->scope.vars);
  2968. ri->scope.vars = NULL;
  2969. }
  2970. if (ri->scope.arrays) {
  2971. ri->scope.arrays->destroy(ri->scope.arrays, script_free_array_db);
  2972. ri->scope.arrays = NULL;
  2973. }
  2974. if( data->ref )
  2975. aFree(data->ref);
  2976. aFree(ri);
  2977. }
  2978. data->type = C_NOP;
  2979. }
  2980. // move the rest of the elements
  2981. if( stack->sp > end )
  2982. {
  2983. memmove(&stack->stack_data[start], &stack->stack_data[end], sizeof(stack->stack_data[0])*(stack->sp - end));
  2984. for( i = start + stack->sp - end; i < stack->sp; ++i )
  2985. stack->stack_data[i].type = C_NOP;
  2986. }
  2987. // adjust stack pointers
  2988. if( st->start > end ) st->start -= end - start;
  2989. else if( st->start > start ) st->start = start;
  2990. if( st->end > end ) st->end -= end - start;
  2991. else if( st->end > start ) st->end = start;
  2992. if( stack->defsp > end ) stack->defsp -= end - start;
  2993. else if( stack->defsp > start ) stack->defsp = start;
  2994. stack->sp -= end - start;
  2995. }
  2996. ///
  2997. ///
  2998. ///
  2999. /*==========================================
  3000. * Release script dependent variable, dependent variable of function
  3001. *------------------------------------------*/
  3002. void script_free_vars(struct DBMap* storage)
  3003. {
  3004. if( storage ) {
  3005. // destroy the storage construct containing the variables
  3006. db_destroy(storage);
  3007. }
  3008. }
  3009. void script_free_code(struct script_code* code)
  3010. {
  3011. nullpo_retv(code);
  3012. if (code->instances)
  3013. script_stop_instances(code);
  3014. script_free_vars(code->local.vars);
  3015. if (code->local.arrays)
  3016. code->local.arrays->destroy(code->local.arrays, script_free_array_db);
  3017. aFree(code->script_buf);
  3018. aFree(code);
  3019. }
  3020. /// Creates a new script state.
  3021. ///
  3022. /// @param script Script code
  3023. /// @param pos Position in the code
  3024. /// @param rid Who is running the script (attached player)
  3025. /// @param oid Where the code is being run (npc 'object')
  3026. /// @return Script state
  3027. struct script_state* script_alloc_state(struct script_code* rootscript, int pos, int rid, int oid)
  3028. {
  3029. struct script_state* st;
  3030. st = ers_alloc(st_ers, struct script_state);
  3031. st->stack = ers_alloc(stack_ers, struct script_stack);
  3032. st->stack->sp = 0;
  3033. st->stack->sp_max = 64;
  3034. CREATE(st->stack->stack_data, struct script_data, st->stack->sp_max);
  3035. st->stack->defsp = st->stack->sp;
  3036. st->stack->scope.vars = i64db_alloc(DB_OPT_RELEASE_DATA);
  3037. st->stack->scope.arrays = NULL;
  3038. st->state = RUN;
  3039. st->script = rootscript;
  3040. st->pos = pos;
  3041. st->rid = rid;
  3042. st->oid = oid;
  3043. st->sleep.timer = INVALID_TIMER;
  3044. st->npc_item_flag = battle_config.item_enabled_npc;
  3045. if( st->script->instances != USHRT_MAX )
  3046. st->script->instances++;
  3047. else {
  3048. struct npc_data *nd = map_id2nd(oid);
  3049. ShowError("Over 65k instances of '%s' script are being run!\n",nd ? nd->name : "unknown");
  3050. }
  3051. if (!st->script->local.vars)
  3052. st->script->local.vars = i64db_alloc(DB_OPT_RELEASE_DATA);
  3053. st->id = next_id++;
  3054. active_scripts++;
  3055. idb_put(st_db, st->id, st);
  3056. return st;
  3057. }
  3058. /// Frees a script state.
  3059. ///
  3060. /// @param st Script state
  3061. void script_free_state(struct script_state* st)
  3062. {
  3063. if (idb_exists(st_db, st->id)) {
  3064. struct map_session_data *sd = st->rid ? map_id2sd(st->rid) : NULL;
  3065. if (st->bk_st) // backup was not restored
  3066. ShowDebug("script_free_state: Previous script state lost (rid=%d, oid=%d, state=%d, bk_npcid=%d).\n", st->bk_st->rid, st->bk_st->oid, st->bk_st->state, st->bk_npcid);
  3067. if (sd && sd->st == st) { // Current script is aborted.
  3068. if(sd->state.using_fake_npc) {
  3069. clif_clearunit_single(sd->npc_id, CLR_OUTSIGHT, sd->fd);
  3070. sd->state.using_fake_npc = 0;
  3071. }
  3072. sd->st = NULL;
  3073. sd->npc_id = 0;
  3074. }
  3075. if (st->sleep.timer != INVALID_TIMER)
  3076. delete_timer(st->sleep.timer, run_script_timer);
  3077. if (st->stack) {
  3078. script_free_vars(st->stack->scope.vars);
  3079. if (st->stack->scope.arrays)
  3080. st->stack->scope.arrays->destroy(st->stack->scope.arrays, script_free_array_db);
  3081. pop_stack(st, 0, st->stack->sp);
  3082. aFree(st->stack->stack_data);
  3083. ers_free(stack_ers, st->stack);
  3084. st->stack = NULL;
  3085. }
  3086. if (st->script && st->script->instances != USHRT_MAX && --st->script->instances == 0) {
  3087. if (st->script->local.vars && !db_size(st->script->local.vars)) {
  3088. script_free_vars(st->script->local.vars);
  3089. st->script->local.vars = NULL;
  3090. }
  3091. if (st->script->local.arrays && !db_size(st->script->local.arrays)) {
  3092. st->script->local.arrays->destroy(st->script->local.arrays, script_free_array_db);
  3093. st->script->local.arrays = NULL;
  3094. }
  3095. }
  3096. st->pos = -1;
  3097. idb_remove(st_db, st->id);
  3098. ers_free(st_ers, st);
  3099. if (--active_scripts == 0)
  3100. next_id = 0;
  3101. }
  3102. }
  3103. //
  3104. // Main execution unit
  3105. //
  3106. /*==========================================
  3107. * Read command
  3108. *------------------------------------------*/
  3109. c_op get_com(unsigned char *script,int *pos)
  3110. {
  3111. int i = 0, j = 0;
  3112. if(script[*pos]>=0x80){
  3113. return C_INT;
  3114. }
  3115. while(script[*pos]>=0x40){
  3116. i=script[(*pos)++]<<j;
  3117. j+=6;
  3118. }
  3119. return (c_op)(i+(script[(*pos)++]<<j));
  3120. }
  3121. /*==========================================
  3122. * Income figures
  3123. *------------------------------------------*/
  3124. int get_num(unsigned char *script,int *pos)
  3125. {
  3126. int i,j;
  3127. i=0; j=0;
  3128. while(script[*pos]>=0xc0){
  3129. i+=(script[(*pos)++]&0x7f)<<j;
  3130. j+=6;
  3131. }
  3132. return i+((script[(*pos)++]&0x7f)<<j);
  3133. }
  3134. /// Ternary operators
  3135. /// test ? if_true : if_false
  3136. void op_3(struct script_state* st, int op)
  3137. {
  3138. struct script_data* data;
  3139. int flag = 0;
  3140. data = script_getdatatop(st, -3);
  3141. get_val(st, data);
  3142. if( data_isstring(data) )
  3143. flag = data->u.str[0];// "" -> false
  3144. else if( data_isint(data) )
  3145. flag = data->u.num == 0 ? 0 : 1;// 0 -> false
  3146. else
  3147. {
  3148. ShowError("script:op_3: invalid data for the ternary operator test\n");
  3149. script_reportdata(data);
  3150. script_reportsrc(st);
  3151. script_removetop(st, -3, 0);
  3152. script_pushnil(st);
  3153. return;
  3154. }
  3155. if( flag )
  3156. script_pushcopytop(st, -2);
  3157. else
  3158. script_pushcopytop(st, -1);
  3159. script_removetop(st, -4, -1);
  3160. }
  3161. /// Binary string operators
  3162. /// s1 EQ s2 -> i
  3163. /// s1 NE s2 -> i
  3164. /// s1 GT s2 -> i
  3165. /// s1 GE s2 -> i
  3166. /// s1 LT s2 -> i
  3167. /// s1 LE s2 -> i
  3168. /// s1 ADD s2 -> s
  3169. void op_2str(struct script_state* st, int op, const char* s1, const char* s2)
  3170. {
  3171. int a = 0;
  3172. switch(op){
  3173. case C_EQ: a = (strcmp(s1,s2) == 0); break;
  3174. case C_NE: a = (strcmp(s1,s2) != 0); break;
  3175. case C_GT: a = (strcmp(s1,s2) > 0); break;
  3176. case C_GE: a = (strcmp(s1,s2) >= 0); break;
  3177. case C_LT: a = (strcmp(s1,s2) < 0); break;
  3178. case C_LE: a = (strcmp(s1,s2) <= 0); break;
  3179. case C_ADD:
  3180. {
  3181. char* buf = (char *)aMalloc((strlen(s1)+strlen(s2)+1)*sizeof(char));
  3182. strcpy(buf, s1);
  3183. strcat(buf, s2);
  3184. script_pushstr(st, buf);
  3185. return;
  3186. }
  3187. default:
  3188. ShowError("script:op2_str: unexpected string operator %s\n", script_op2name(op));
  3189. script_reportsrc(st);
  3190. script_pushnil(st);
  3191. st->state = END;
  3192. return;
  3193. }
  3194. script_pushint(st,a);
  3195. }
  3196. /// Binary number operators
  3197. /// i OP i -> i
  3198. void op_2num(struct script_state* st, int op, int i1, int i2)
  3199. {
  3200. int ret;
  3201. double ret_double;
  3202. switch( op ) {
  3203. case C_AND: ret = i1 & i2; break;
  3204. case C_OR: ret = i1 | i2; break;
  3205. case C_XOR: ret = i1 ^ i2; break;
  3206. case C_LAND: ret = (i1 && i2); break;
  3207. case C_LOR: ret = (i1 || i2); break;
  3208. case C_EQ: ret = (i1 == i2); break;
  3209. case C_NE: ret = (i1 != i2); break;
  3210. case C_GT: ret = (i1 > i2); break;
  3211. case C_GE: ret = (i1 >= i2); break;
  3212. case C_LT: ret = (i1 < i2); break;
  3213. case C_LE: ret = (i1 <= i2); break;
  3214. case C_R_SHIFT: ret = i1>>i2; break;
  3215. case C_L_SHIFT: ret = i1<<i2; break;
  3216. case C_DIV:
  3217. case C_MOD:
  3218. if( i2 == 0 )
  3219. {
  3220. ShowError("script:op_2num: division by zero detected op=%s i1=%d i2=%d\n", script_op2name(op), i1, i2);
  3221. script_reportsrc(st);
  3222. script_pushnil(st);
  3223. st->state = END;
  3224. return;
  3225. }
  3226. else if( op == C_DIV )
  3227. ret = i1 / i2;
  3228. else//if( op == C_MOD )
  3229. ret = i1 % i2;
  3230. break;
  3231. default:
  3232. switch( op ) {// operators that can overflow/underflow
  3233. case C_ADD: ret = i1 + i2; ret_double = (double)i1 + (double)i2; break;
  3234. case C_SUB: ret = i1 - i2; ret_double = (double)i1 - (double)i2; break;
  3235. case C_MUL: ret = i1 * i2; ret_double = (double)i1 * (double)i2; break;
  3236. default:
  3237. ShowError("script:op_2num: unexpected number operator %s i1=%d i2=%d\n", script_op2name(op), i1, i2);
  3238. script_reportsrc(st);
  3239. script_pushnil(st);
  3240. return;
  3241. }
  3242. if( ret_double < (double)INT_MIN ) {
  3243. ShowWarning("script:op_2num: underflow detected op=%s i1=%d i2=%d\n", script_op2name(op), i1, i2);
  3244. script_reportsrc(st);
  3245. ret = INT_MIN;
  3246. }
  3247. else if( ret_double > (double)INT_MAX ) {
  3248. ShowWarning("script:op_2num: overflow detected op=%s i1=%d i2=%d\n", script_op2name(op), i1, i2);
  3249. script_reportsrc(st);
  3250. ret = INT_MAX;
  3251. }
  3252. }
  3253. script_pushint(st, ret);
  3254. }
  3255. /// Binary operators
  3256. void op_2(struct script_state *st, int op)
  3257. {
  3258. struct script_data* left, leftref;
  3259. struct script_data* right;
  3260. leftref.type = C_NOP;
  3261. left = script_getdatatop(st, -2);
  3262. right = script_getdatatop(st, -1);
  3263. if (st->op2ref) {
  3264. if (data_isreference(left)) {
  3265. leftref = *left;
  3266. }
  3267. st->op2ref = 0;
  3268. }
  3269. get_val(st, left);
  3270. get_val(st, right);
  3271. // automatic conversions
  3272. switch( op )
  3273. {
  3274. case C_ADD:
  3275. if( data_isint(left) && data_isstring(right) )
  3276. {// convert int-string to string-string
  3277. conv_str(st, left);
  3278. }
  3279. else if( data_isstring(left) && data_isint(right) )
  3280. {// convert string-int to string-string
  3281. conv_str(st, right);
  3282. }
  3283. break;
  3284. }
  3285. if( data_isstring(left) && data_isstring(right) )
  3286. {// ss => op_2str
  3287. op_2str(st, op, left->u.str, right->u.str);
  3288. script_removetop(st, leftref.type == C_NOP ? -3 : -2, -1);// pop the two values before the top one
  3289. if (leftref.type != C_NOP)
  3290. {
  3291. if (left->type == C_STR) // don't free C_CONSTSTR
  3292. aFree(left->u.str);
  3293. *left = leftref;
  3294. }
  3295. }
  3296. else if( data_isint(left) && data_isint(right) )
  3297. {// ii => op_2num
  3298. int i1 = (int)left->u.num;
  3299. int i2 = (int)right->u.num;
  3300. script_removetop(st, leftref.type == C_NOP ? -2 : -1, 0);
  3301. op_2num(st, op, i1, i2);
  3302. if (leftref.type != C_NOP)
  3303. *left = leftref;
  3304. }
  3305. else
  3306. {// invalid argument
  3307. ShowError("script:op_2: invalid data for operator %s\n", script_op2name(op));
  3308. script_reportdata(left);
  3309. script_reportdata(right);
  3310. script_reportsrc(st);
  3311. script_removetop(st, -2, 0);
  3312. script_pushnil(st);
  3313. st->state = END;
  3314. }
  3315. }
  3316. /// Unary operators
  3317. /// NEG i -> i
  3318. /// NOT i -> i
  3319. /// LNOT i -> i
  3320. void op_1(struct script_state* st, int op)
  3321. {
  3322. struct script_data* data;
  3323. int i1;
  3324. data = script_getdatatop(st, -1);
  3325. get_val(st, data);
  3326. if( !data_isint(data) )
  3327. {// not a number
  3328. ShowError("script:op_1: argument is not a number (op=%s)\n", script_op2name(op));
  3329. script_reportdata(data);
  3330. script_reportsrc(st);
  3331. script_pushnil(st);
  3332. st->state = END;
  3333. return;
  3334. }
  3335. i1 = (int)data->u.num;
  3336. script_removetop(st, -1, 0);
  3337. switch( op )
  3338. {
  3339. case C_NEG: i1 = -i1; break;
  3340. case C_NOT: i1 = ~i1; break;
  3341. case C_LNOT: i1 = !i1; break;
  3342. default:
  3343. ShowError("script:op_1: unexpected operator %s i1=%d\n", script_op2name(op), i1);
  3344. script_reportsrc(st);
  3345. script_pushnil(st);
  3346. st->state = END;
  3347. return;
  3348. }
  3349. script_pushint(st, i1);
  3350. }
  3351. /// Checks the type of all arguments passed to a built-in function.
  3352. ///
  3353. /// @param st Script state whose stack arguments should be inspected.
  3354. /// @param func Built-in function for which the arguments are intended.
  3355. static void script_check_buildin_argtype(struct script_state* st, int func)
  3356. {
  3357. int idx, invalid = 0;
  3358. for( idx = 2; script_hasdata(st, idx); idx++ )
  3359. {
  3360. struct script_data* data = script_getdata(st, idx);
  3361. script_function* sf = &buildin_func[str_data[func].val];
  3362. char type = sf->arg[idx-2];
  3363. if( type == '?' || type == '*' )
  3364. {// optional argument or unknown number of optional parameters ( no types are after this )
  3365. break;
  3366. }
  3367. else if( type == 0 )
  3368. {// more arguments than necessary ( should not happen, as it is checked before )
  3369. ShowWarning("Found more arguments than necessary. unexpected arg type %s\n",script_op2name(data->type));
  3370. invalid++;
  3371. break;
  3372. }
  3373. else
  3374. {
  3375. const char* name = NULL;
  3376. if( data_isreference(data) )
  3377. {// get name for variables to determine the type they refer to
  3378. name = reference_getname(data);
  3379. }
  3380. switch( type )
  3381. {
  3382. case 'v':
  3383. if( !data_isstring(data) && !data_isint(data) && !data_isreference(data) )
  3384. {// variant
  3385. ShowWarning("Unexpected type for argument %d. Expected string, number or variable.\n", idx-1);
  3386. script_reportdata(data);
  3387. invalid++;
  3388. }
  3389. break;
  3390. case 's':
  3391. if( !data_isstring(data) && !( data_isreference(data) && is_string_variable(name) ) )
  3392. {// string
  3393. ShowWarning("Unexpected type for argument %d. Expected string.\n", idx-1);
  3394. script_reportdata(data);
  3395. invalid++;
  3396. }
  3397. break;
  3398. case 'i':
  3399. if( !data_isint(data) && !( data_isreference(data) && ( reference_toparam(data) || reference_toconstant(data) || !is_string_variable(name) ) ) )
  3400. {// int ( params and constants are always int )
  3401. ShowWarning("Unexpected type for argument %d. Expected number.\n", idx-1);
  3402. script_reportdata(data);
  3403. invalid++;
  3404. }
  3405. break;
  3406. case 'r':
  3407. if( !data_isreference(data) )
  3408. {// variables
  3409. ShowWarning("Unexpected type for argument %d. Expected variable, got %s.\n", idx-1,script_op2name(data->type));
  3410. script_reportdata(data);
  3411. invalid++;
  3412. }
  3413. break;
  3414. case 'l':
  3415. if( !data_islabel(data) && !data_isfunclabel(data) )
  3416. {// label
  3417. ShowWarning("Unexpected type for argument %d. Expected label, got %s\n", idx-1,script_op2name(data->type));
  3418. script_reportdata(data);
  3419. invalid++;
  3420. }
  3421. break;
  3422. }
  3423. }
  3424. }
  3425. if(invalid)
  3426. {
  3427. ShowDebug("Function: %s\n", get_str(func));
  3428. script_reportsrc(st);
  3429. }
  3430. }
  3431. /// Executes a buildin command.
  3432. /// Stack: C_NAME(<command>) C_ARG <arg0> <arg1> ... <argN>
  3433. int run_func(struct script_state *st)
  3434. {
  3435. struct script_data* data;
  3436. int i,start_sp,end_sp,func;
  3437. end_sp = st->stack->sp;// position after the last argument
  3438. for( i = end_sp-1; i > 0 ; --i )
  3439. if( st->stack->stack_data[i].type == C_ARG )
  3440. break;
  3441. if( i == 0 ) {
  3442. ShowError("script:run_func: C_ARG not found. please report this!!!\n");
  3443. st->state = END;
  3444. script_reportsrc(st);
  3445. return 1;
  3446. }
  3447. start_sp = i-1;// C_NAME of the command
  3448. st->start = start_sp;
  3449. st->end = end_sp;
  3450. data = &st->stack->stack_data[st->start];
  3451. if( data->type == C_NAME && str_data[data->u.num].type == C_FUNC ) {
  3452. func = (int)data->u.num;
  3453. st->funcname = reference_getname(data);
  3454. } else {
  3455. ShowError("script:run_func: not a buildin command.\n");
  3456. script_reportdata(data);
  3457. script_reportsrc(st);
  3458. st->state = END;
  3459. return 1;
  3460. }
  3461. if( script_config.warn_func_mismatch_argtypes ) {
  3462. script_check_buildin_argtype(st, func);
  3463. }
  3464. if(str_data[func].func) {
  3465. if (str_data[func].func(st) == SCRIPT_CMD_FAILURE) //Report error
  3466. script_reportsrc(st);
  3467. } else {
  3468. ShowError("script:run_func: '%s' (id=%d type=%s) has no C function. please report this!!!\n", get_str(func), func, script_op2name(str_data[func].type));
  3469. script_reportsrc(st);
  3470. st->state = END;
  3471. }
  3472. // Stack's datum are used when re-running functions [Eoe]
  3473. if( st->state == RERUNLINE )
  3474. return 0;
  3475. pop_stack(st, st->start, st->end);
  3476. if( st->state == RETFUNC ) {// return from a user-defined function
  3477. struct script_retinfo* ri;
  3478. int olddefsp = st->stack->defsp;
  3479. int nargs;
  3480. pop_stack(st, st->stack->defsp, st->start);// pop distractions from the stack
  3481. if( st->stack->defsp < 1 || st->stack->stack_data[st->stack->defsp-1].type != C_RETINFO )
  3482. {
  3483. ShowWarning("script:run_func: return without callfunc or callsub!\n");
  3484. script_reportsrc(st);
  3485. st->state = END;
  3486. return 1;
  3487. }
  3488. script_free_vars(st->stack->scope.vars);
  3489. st->stack->scope.arrays->destroy(st->stack->scope.arrays, script_free_array_db);
  3490. ri = st->stack->stack_data[st->stack->defsp-1].u.ri;
  3491. nargs = ri->nargs;
  3492. st->pos = ri->pos;
  3493. st->script = ri->script;
  3494. st->stack->scope.vars = ri->scope.vars;
  3495. st->stack->scope.arrays = ri->scope.arrays;
  3496. st->stack->defsp = ri->defsp;
  3497. memset(ri, 0, sizeof(struct script_retinfo));
  3498. pop_stack(st, olddefsp-nargs-1, olddefsp);// pop arguments and retinfo
  3499. st->state = GOTO;
  3500. }
  3501. return 0;
  3502. }
  3503. /*==========================================
  3504. * script execution
  3505. *------------------------------------------*/
  3506. void run_script(struct script_code *rootscript, int pos, int rid, int oid)
  3507. {
  3508. struct script_state *st;
  3509. if( rootscript == NULL || pos < 0 )
  3510. return;
  3511. // TODO In jAthena, this function can take over the pending script in the player. [FlavioJS]
  3512. // It is unclear how that can be triggered, so it needs the be traced/checked in more detail.
  3513. // NOTE At the time of this change, this function wasn't capable of taking over the script state because st->scriptroot was never set.
  3514. st = script_alloc_state(rootscript, pos, rid, oid);
  3515. run_script_main(st);
  3516. }
  3517. void script_stop_instances(struct script_code *code) {
  3518. DBIterator *iter;
  3519. struct script_state* st;
  3520. if( !active_scripts )
  3521. return; // Don't even bother.
  3522. iter = db_iterator(st_db);
  3523. for( st = dbi_first(iter); dbi_exists(iter); st = dbi_next(iter) ) {
  3524. if( st->script == code )
  3525. script_free_state(st);
  3526. }
  3527. dbi_destroy(iter);
  3528. }
  3529. /*==========================================
  3530. * Timer function for sleep
  3531. *------------------------------------------*/
  3532. int run_script_timer(int tid, unsigned int tick, int id, intptr_t data)
  3533. {
  3534. struct script_state *st = (struct script_state *)data;
  3535. TBL_PC *sd = map_id2sd(st->rid);
  3536. if ((sd && sd->status.char_id != id) || (st->rid && !sd)) { // Character mismatch. Cancel execution.
  3537. st->rid = 0;
  3538. st->state = END;
  3539. }
  3540. st->sleep.timer = INVALID_TIMER;
  3541. if(st->state != RERUNLINE)
  3542. st->sleep.tick = 0;
  3543. run_script_main(st);
  3544. return 0;
  3545. }
  3546. /// Detaches script state from possibly attached character and restores it's previous script if any.
  3547. ///
  3548. /// @param st Script state to detach.
  3549. /// @param dequeue_event Whether to schedule any queued events, when there was no previous script.
  3550. static void script_detach_state(struct script_state* st, bool dequeue_event)
  3551. {
  3552. struct map_session_data* sd;
  3553. if(st->rid && (sd = map_id2sd(st->rid))!=NULL) {
  3554. sd->st = st->bk_st;
  3555. sd->npc_id = st->bk_npcid;
  3556. sd->state.disable_atcommand_on_npc = 0;
  3557. if(st->bk_st) {
  3558. //Remove tag for removal.
  3559. st->bk_st = NULL;
  3560. st->bk_npcid = 0;
  3561. } else if(dequeue_event) {
  3562. #ifdef SECURE_NPCTIMEOUT
  3563. /**
  3564. * We're done with this NPC session, so we cancel the timer (if existent) and move on
  3565. **/
  3566. if( sd->npc_idle_timer != INVALID_TIMER ) {
  3567. delete_timer(sd->npc_idle_timer,npc_rr_secure_timeout_timer);
  3568. sd->npc_idle_timer = INVALID_TIMER;
  3569. }
  3570. #endif
  3571. npc_event_dequeue(sd);
  3572. }
  3573. }
  3574. else if(st->bk_st)
  3575. {// rid was set to 0, before detaching the script state
  3576. ShowError("script_detach_state: Found previous script state without attached player (rid=%d, oid=%d, state=%d, bk_npcid=%d)\n", st->bk_st->rid, st->bk_st->oid, st->bk_st->state, st->bk_npcid);
  3577. script_reportsrc(st->bk_st);
  3578. script_free_state(st->bk_st);
  3579. st->bk_st = NULL;
  3580. }
  3581. }
  3582. /// Attaches script state to possibly attached character and backups it's previous script, if any.
  3583. ///
  3584. /// @param st Script state to attach.
  3585. static void script_attach_state(struct script_state* st)
  3586. {
  3587. struct map_session_data* sd;
  3588. if(st->rid && (sd = map_id2sd(st->rid))!=NULL)
  3589. {
  3590. if(st!=sd->st)
  3591. {
  3592. if(st->bk_st)
  3593. {// there is already a backup
  3594. ShowDebug("script_attach_state: Previous script state lost (rid=%d, oid=%d, state=%d, bk_npcid=%d).\n", st->bk_st->rid, st->bk_st->oid, st->bk_st->state, st->bk_npcid);
  3595. }
  3596. st->bk_st = sd->st;
  3597. st->bk_npcid = sd->npc_id;
  3598. }
  3599. sd->st = st;
  3600. sd->npc_id = st->oid;
  3601. sd->npc_item_flag = st->npc_item_flag; // load default.
  3602. sd->state.disable_atcommand_on_npc = (!pc_has_permission(sd, PC_PERM_ENABLE_COMMAND));
  3603. #ifdef SECURE_NPCTIMEOUT
  3604. if( sd->npc_idle_timer == INVALID_TIMER )
  3605. sd->npc_idle_timer = add_timer(gettick() + (SECURE_NPCTIMEOUT_INTERVAL*1000),npc_rr_secure_timeout_timer,sd->bl.id,0);
  3606. sd->npc_idle_tick = gettick();
  3607. #endif
  3608. }
  3609. }
  3610. /*==========================================
  3611. * The main part of the script execution
  3612. *------------------------------------------*/
  3613. void run_script_main(struct script_state *st)
  3614. {
  3615. int cmdcount = script_config.check_cmdcount;
  3616. int gotocount = script_config.check_gotocount;
  3617. TBL_PC *sd;
  3618. struct script_stack *stack = st->stack;
  3619. script_attach_state(st);
  3620. if(st->state == RERUNLINE) {
  3621. run_func(st);
  3622. if(st->state == GOTO)
  3623. st->state = RUN;
  3624. } else if(st->state != END)
  3625. st->state = RUN;
  3626. while(st->state == RUN) {
  3627. enum c_op c = get_com(st->script->script_buf,&st->pos);
  3628. switch(c){
  3629. case C_EOL:
  3630. if( stack->defsp > stack->sp )
  3631. ShowError("script:run_script_main: unexpected stack position (defsp=%d sp=%d). please report this!!!\n", stack->defsp, stack->sp);
  3632. else
  3633. pop_stack(st, stack->defsp, stack->sp);// pop unused stack data. (unused return value)
  3634. break;
  3635. case C_INT:
  3636. push_val(stack,C_INT,get_num(st->script->script_buf,&st->pos));
  3637. break;
  3638. case C_POS:
  3639. case C_NAME:
  3640. push_val(stack,c,GETVALUE(st->script->script_buf,st->pos));
  3641. st->pos+=3;
  3642. break;
  3643. case C_ARG:
  3644. push_val(stack,c,0);
  3645. break;
  3646. case C_STR:
  3647. push_str(stack,C_CONSTSTR,(char*)(st->script->script_buf+st->pos));
  3648. while(st->script->script_buf[st->pos++]);
  3649. break;
  3650. case C_FUNC:
  3651. run_func(st);
  3652. if(st->state==GOTO){
  3653. st->state = RUN;
  3654. if( !st->freeloop && gotocount>0 && (--gotocount)<=0 ){
  3655. ShowError("script:run_script_main: infinity loop !\n");
  3656. script_reportsrc(st);
  3657. st->state=END;
  3658. }
  3659. }
  3660. break;
  3661. case C_REF:
  3662. st->op2ref = 1;
  3663. break;
  3664. case C_NEG:
  3665. case C_NOT:
  3666. case C_LNOT:
  3667. op_1(st ,c);
  3668. break;
  3669. case C_ADD:
  3670. case C_SUB:
  3671. case C_MUL:
  3672. case C_DIV:
  3673. case C_MOD:
  3674. case C_EQ:
  3675. case C_NE:
  3676. case C_GT:
  3677. case C_GE:
  3678. case C_LT:
  3679. case C_LE:
  3680. case C_AND:
  3681. case C_OR:
  3682. case C_XOR:
  3683. case C_LAND:
  3684. case C_LOR:
  3685. case C_R_SHIFT:
  3686. case C_L_SHIFT:
  3687. op_2(st, c);
  3688. break;
  3689. case C_OP3:
  3690. op_3(st, c);
  3691. break;
  3692. case C_NOP:
  3693. st->state=END;
  3694. break;
  3695. default:
  3696. ShowError("script:run_script_main:unknown command : %d @ %d\n",c,st->pos);
  3697. st->state=END;
  3698. break;
  3699. }
  3700. if( !st->freeloop && cmdcount>0 && (--cmdcount)<=0 ){
  3701. ShowError("script:run_script_main: infinity loop !\n");
  3702. script_reportsrc(st);
  3703. st->state=END;
  3704. }
  3705. }
  3706. if(st->sleep.tick > 0) {
  3707. //Restore previous script
  3708. script_detach_state(st, false);
  3709. //Delay execution
  3710. sd = map_id2sd(st->rid); // Get sd since script might have attached someone while running. [Inkfish]
  3711. st->sleep.charid = sd?sd->status.char_id:0;
  3712. st->sleep.timer = add_timer(gettick()+st->sleep.tick,
  3713. run_script_timer, st->sleep.charid, (intptr_t)st);
  3714. } else if(st->state != END && st->rid) {
  3715. //Resume later (st is already attached to player).
  3716. if(st->bk_st) {
  3717. ShowWarning("Unable to restore stack! Double continuation!\n");
  3718. //Report BOTH scripts to see if that can help somehow.
  3719. ShowDebug("Previous script (lost):\n");
  3720. script_reportsrc(st->bk_st);
  3721. ShowDebug("Current script:\n");
  3722. script_reportsrc(st);
  3723. script_free_state(st->bk_st);
  3724. st->bk_st = NULL;
  3725. }
  3726. } else {
  3727. //Dispose of script.
  3728. if ((sd = map_id2sd(st->rid))!=NULL)
  3729. { //Restore previous stack and save char.
  3730. if(sd->state.using_fake_npc){
  3731. clif_clearunit_single(sd->npc_id, CLR_OUTSIGHT, sd->fd);
  3732. sd->state.using_fake_npc = 0;
  3733. }
  3734. //Restore previous script if any.
  3735. script_detach_state(st, true);
  3736. if (sd->vars_dirty)
  3737. intif_saveregistry(sd);
  3738. }
  3739. script_free_state(st);
  3740. }
  3741. }
  3742. int script_config_read(char *cfgName)
  3743. {
  3744. int i;
  3745. char line[1024],w1[1024],w2[1024];
  3746. FILE *fp;
  3747. fp=fopen(cfgName,"r");
  3748. if(fp==NULL){
  3749. ShowError("File not found: %s\n", cfgName);
  3750. return 1;
  3751. }
  3752. while(fgets(line, sizeof(line), fp))
  3753. {
  3754. if(line[0] == '/' && line[1] == '/')
  3755. continue;
  3756. i=sscanf(line,"%1023[^:]: %1023[^\r\n]",w1,w2);
  3757. if(i!=2)
  3758. continue;
  3759. if(strcmpi(w1,"warn_func_mismatch_paramnum")==0) {
  3760. script_config.warn_func_mismatch_paramnum = config_switch(w2);
  3761. }
  3762. else if(strcmpi(w1,"check_cmdcount")==0) {
  3763. script_config.check_cmdcount = config_switch(w2);
  3764. }
  3765. else if(strcmpi(w1,"check_gotocount")==0) {
  3766. script_config.check_gotocount = config_switch(w2);
  3767. }
  3768. else if(strcmpi(w1,"input_min_value")==0) {
  3769. script_config.input_min_value = config_switch(w2);
  3770. }
  3771. else if(strcmpi(w1,"input_max_value")==0) {
  3772. script_config.input_max_value = config_switch(w2);
  3773. }
  3774. else if(strcmpi(w1,"warn_func_mismatch_argtypes")==0) {
  3775. script_config.warn_func_mismatch_argtypes = config_switch(w2);
  3776. }
  3777. else if(strcmpi(w1,"import")==0){
  3778. script_config_read(w2);
  3779. }
  3780. else {
  3781. ShowWarning("Unknown setting '%s' in file %s\n", w1, cfgName);
  3782. }
  3783. }
  3784. fclose(fp);
  3785. return 0;
  3786. }
  3787. /**
  3788. * @see DBApply
  3789. */
  3790. static int db_script_free_code_sub(DBKey key, DBData *data, va_list ap)
  3791. {
  3792. struct script_code *code = db_data2ptr(data);
  3793. if (code)
  3794. script_free_code(code);
  3795. return 0;
  3796. }
  3797. void script_run_autobonus(const char *autobonus, struct map_session_data *sd, unsigned int pos)
  3798. {
  3799. struct script_code *script = (struct script_code *)strdb_get(autobonus_db, autobonus);
  3800. if( script )
  3801. {
  3802. int j;
  3803. ARR_FIND( 0, EQI_MAX, j, sd->equip_index[j] >= 0 && sd->status.inventory[sd->equip_index[j]].equip == pos );
  3804. if( j < EQI_MAX ) {
  3805. //Single item autobonus
  3806. current_equip_item_index = sd->equip_index[j];
  3807. current_equip_combo_pos = 0;
  3808. } else {
  3809. //Combo autobonus
  3810. current_equip_item_index = -1;
  3811. current_equip_combo_pos = pos;
  3812. }
  3813. run_script(script,0,sd->bl.id,0);
  3814. }
  3815. }
  3816. void script_add_autobonus(const char *autobonus)
  3817. {
  3818. if( strdb_get(autobonus_db, autobonus) == NULL )
  3819. {
  3820. struct script_code *script = parse_script(autobonus, "autobonus", 0, 0);
  3821. if( script )
  3822. strdb_put(autobonus_db, autobonus, script);
  3823. }
  3824. }
  3825. /// resets a temporary character array variable to given value
  3826. void script_cleararray_pc(struct map_session_data* sd, const char* varname, void* value)
  3827. {
  3828. struct script_array *sa = NULL;
  3829. struct reg_db *src = NULL;
  3830. unsigned int i, *list = NULL, size = 0;
  3831. int key;
  3832. key = add_str(varname);
  3833. if( !(src = script_array_src(NULL,sd,varname,NULL) ) )
  3834. return;
  3835. if( value )
  3836. script_array_ensure_zero(NULL,sd,reference_uid(key,0), NULL);
  3837. if( !(sa = idb_get(src->arrays, key)) ) /* non-existent array, nothing to empty */
  3838. return;
  3839. size = sa->size;
  3840. list = script_array_cpy_list(sa);
  3841. for(i = 0; i < size; i++) {
  3842. set_reg(NULL,sd,reference_uid(key, list[i]),varname,value,NULL);
  3843. }
  3844. }
  3845. /// sets a temporary character array variable element idx to given value
  3846. /// @param refcache Pointer to an int variable, which keeps a copy of the reference to varname and must be initialized to 0. Can be NULL if only one element is set.
  3847. void script_setarray_pc(struct map_session_data* sd, const char* varname, uint32 idx, void* value, int* refcache)
  3848. {
  3849. int key;
  3850. if( idx >= SCRIPT_MAX_ARRAYSIZE ) {
  3851. ShowError("script_setarray_pc: Variable '%s' has invalid index '%u' (char_id=%d).\n", varname, idx, sd->status.char_id);
  3852. return;
  3853. }
  3854. key = ( refcache && refcache[0] ) ? refcache[0] : add_str(varname);
  3855. set_reg(NULL,sd,reference_uid(key, idx),varname,value,NULL);
  3856. if( refcache ) {
  3857. // save to avoid repeated script->add_str calls
  3858. refcache[0] = key;
  3859. }
  3860. }
  3861. /**
  3862. * Clears persistent variables from memory
  3863. **/
  3864. int script_reg_destroy(DBKey key, DBData *data, va_list ap)
  3865. {
  3866. struct script_reg_state *src;
  3867. if( data->type != DB_DATA_PTR ) // got no need for those!
  3868. return 0;
  3869. src = db_data2ptr(data);
  3870. if( src->type ) {
  3871. struct script_reg_str *p = (struct script_reg_str *)src;
  3872. if( p->value )
  3873. aFree(p->value);
  3874. ers_free(str_reg_ers,p);
  3875. } else {
  3876. ers_free(num_reg_ers,(struct script_reg_num*)src);
  3877. }
  3878. return 0;
  3879. }
  3880. /**
  3881. * Clears a single persistent variable
  3882. **/
  3883. void script_reg_destroy_single(struct map_session_data *sd, int64 reg, struct script_reg_state *data)
  3884. {
  3885. i64db_remove(sd->regs.vars, reg);
  3886. if( data->type ) {
  3887. struct script_reg_str *p = (struct script_reg_str*)data;
  3888. if( p->value )
  3889. aFree(p->value);
  3890. ers_free(str_reg_ers,p);
  3891. } else {
  3892. ers_free(num_reg_ers,(struct script_reg_num*)data);
  3893. }
  3894. }
  3895. unsigned int *script_array_cpy_list(struct script_array *sa)
  3896. {
  3897. if( sa->size > generic_ui_array_size )
  3898. script_generic_ui_array_expand(sa->size);
  3899. memcpy(generic_ui_array, sa->members, sizeof(unsigned int)*sa->size);
  3900. return generic_ui_array;
  3901. }
  3902. void script_generic_ui_array_expand (unsigned int plus)
  3903. {
  3904. generic_ui_array_size += plus + 100;
  3905. RECREATE(generic_ui_array, unsigned int, generic_ui_array_size);
  3906. }
  3907. int buildin_query_sql_sub(struct script_state *st, Sql *handle);
  3908. #ifdef BETA_THREAD_TEST
  3909. /* used to receive items the queryThread has already processed */
  3910. int queryThread_timer(int tid, unsigned int tick, int id, intptr_t data) {
  3911. int i, cursor = 0;
  3912. bool allOk = true;
  3913. EnterSpinLock(&queryThreadLock);
  3914. for( i = 0; i < queryThreadData.count; i++ ) {
  3915. struct queryThreadEntry *entry = queryThreadData.entry[i];
  3916. if( !entry->ok ) {
  3917. allOk = false;
  3918. continue;
  3919. }
  3920. run_script_main(entry->st);
  3921. entry->st = NULL;/* empty entries */
  3922. aFree(entry);
  3923. queryThreadData.entry[i] = NULL;
  3924. }
  3925. if( allOk ) {
  3926. /* cancel the repeating timer -- it'll re-create itself when necessary, dont need to remain looping */
  3927. delete_timer(queryThreadData.timer, queryThread_timer);
  3928. queryThreadData.timer = INVALID_TIMER;
  3929. }
  3930. /* now lets clear the mess. */
  3931. for( i = 0; i < queryThreadData.count; i++ ) {
  3932. struct queryThreadEntry *entry = queryThreadData.entry[i];
  3933. if( entry == NULL )
  3934. continue;/* entry on hold */
  3935. /* move */
  3936. memmove(&queryThreadData.entry[cursor], &queryThreadData.entry[i], sizeof(struct queryThreadEntry*));
  3937. cursor++;
  3938. }
  3939. queryThreadData.count = cursor;
  3940. LeaveSpinLock(&queryThreadLock);
  3941. return 0;
  3942. }
  3943. void queryThread_add(struct script_state *st, bool type) {
  3944. int idx = 0;
  3945. struct queryThreadEntry* entry = NULL;
  3946. EnterSpinLock(&queryThreadLock);
  3947. if( queryThreadData.count++ != 0 )
  3948. RECREATE(queryThreadData.entry, struct queryThreadEntry* , queryThreadData.count);
  3949. idx = queryThreadData.count-1;
  3950. CREATE(queryThreadData.entry[idx],struct queryThreadEntry,1);
  3951. entry = queryThreadData.entry[idx];
  3952. entry->st = st;
  3953. entry->ok = false;
  3954. entry->type = type;
  3955. if( queryThreadData.timer == INVALID_TIMER ) { /* start the receiver timer */
  3956. queryThreadData.timer = add_timer_interval(gettick() + 100, queryThread_timer, 0, 0, 100);
  3957. }
  3958. LeaveSpinLock(&queryThreadLock);
  3959. /* unlock the queryThread */
  3960. racond_signal(queryThreadCond);
  3961. }
  3962. /* adds a new log to the queue */
  3963. void queryThread_log(char * entry, int length) {
  3964. int idx = logThreadData.count;
  3965. EnterSpinLock(&queryThreadLock);
  3966. if( logThreadData.count++ != 0 )
  3967. RECREATE(logThreadData.entry, char* , logThreadData.count);
  3968. CREATE(logThreadData.entry[idx], char, length + 1 );
  3969. safestrncpy(logThreadData.entry[idx], entry, length + 1 );
  3970. LeaveSpinLock(&queryThreadLock);
  3971. /* unlock the queryThread */
  3972. racond_signal(queryThreadCond);
  3973. }
  3974. /* queryThread_main */
  3975. static void *queryThread_main(void *x) {
  3976. Sql *queryThread_handle = Sql_Malloc();
  3977. int i;
  3978. if ( SQL_ERROR == Sql_Connect(queryThread_handle, map_server_id, map_server_pw, map_server_ip, map_server_port, map_server_db) ){
  3979. ShowError("Couldn't connect with uname='%s',passwd='%s',host='%s',port='%d',database='%s'\n",
  3980. map_server_id, map_server_pw, map_server_ip, map_server_port, map_server_db);
  3981. Sql_ShowDebug(queryThread_handle);
  3982. Sql_Free(queryThread_handle);
  3983. exit(EXIT_FAILURE);
  3984. }
  3985. if( strlen(default_codepage) > 0 )
  3986. if ( SQL_ERROR == Sql_SetEncoding(queryThread_handle, default_codepage) )
  3987. Sql_ShowDebug(queryThread_handle);
  3988. if( log_config.sql_logs ) {
  3989. logmysql_handle = Sql_Malloc();
  3990. if ( SQL_ERROR == Sql_Connect(logmysql_handle, log_db_id, log_db_pw, log_db_ip, log_db_port, log_db_db) ){
  3991. ShowError("Couldn't connect with uname='%s',passwd='%s',host='%s',port='%d',database='%s'\n",
  3992. log_db_id, log_db_pw, log_db_ip, log_db_port, log_db_db);
  3993. Sql_ShowDebug(logmysql_handle);
  3994. Sql_Free(logmysql_handle);
  3995. exit(EXIT_FAILURE);
  3996. }
  3997. if( strlen(default_codepage) > 0 )
  3998. if ( SQL_ERROR == Sql_SetEncoding(logmysql_handle, default_codepage) )
  3999. Sql_ShowDebug(logmysql_handle);
  4000. }
  4001. while( 1 ) {
  4002. if(queryThreadTerminate > 0)
  4003. break;
  4004. EnterSpinLock(&queryThreadLock);
  4005. /* mess with queryThreadData within the lock */
  4006. for( i = 0; i < queryThreadData.count; i++ ) {
  4007. struct queryThreadEntry *entry = queryThreadData.entry[i];
  4008. if( entry->ok )
  4009. continue;
  4010. else if ( !entry->st || !entry->st->stack ) {
  4011. entry->ok = true;/* dispose */
  4012. continue;
  4013. }
  4014. buildin_query_sql_sub(entry->st, entry->type ? logmysql_handle : queryThread_handle);
  4015. entry->ok = true;/* we're done with this */
  4016. }
  4017. /* also check for any logs in need to be sent */
  4018. if( log_config.sql_logs ) {
  4019. for( i = 0; i < logThreadData.count; i++ ) {
  4020. if( SQL_ERROR == Sql_Query(logmysql_handle, logThreadData.entry[i]) )
  4021. Sql_ShowDebug(logmysql_handle);
  4022. aFree(logThreadData.entry[i]);
  4023. }
  4024. logThreadData.count = 0;
  4025. }
  4026. LeaveSpinLock(&queryThreadLock);
  4027. ramutex_lock( queryThreadMutex );
  4028. racond_wait( queryThreadCond, queryThreadMutex, -1 );
  4029. ramutex_unlock( queryThreadMutex );
  4030. }
  4031. Sql_Free(queryThread_handle);
  4032. if( log_config.sql_logs ) {
  4033. Sql_Free(logmysql_handle);
  4034. }
  4035. return NULL;
  4036. }
  4037. #endif
  4038. /*==========================================
  4039. * Destructor
  4040. *------------------------------------------*/
  4041. void do_final_script() {
  4042. int i;
  4043. DBIterator *iter;
  4044. struct script_state *st;
  4045. #ifdef DEBUG_HASH
  4046. if (battle_config.etc_log)
  4047. {
  4048. FILE *fp = fopen("hash_dump.txt","wt");
  4049. if(fp) {
  4050. int count[SCRIPT_HASH_SIZE];
  4051. int count2[SCRIPT_HASH_SIZE]; // number of buckets with a certain number of items
  4052. int n=0;
  4053. int min=INT_MAX,max=0,zero=0;
  4054. double mean=0.0f;
  4055. double median=0.0f;
  4056. ShowNotice("Dumping script str hash information to hash_dump.txt\n");
  4057. memset(count, 0, sizeof(count));
  4058. fprintf(fp,"num : hash : data_name\n");
  4059. fprintf(fp,"---------------------------------------------------------------\n");
  4060. for(i=LABEL_START; i<str_num; i++) {
  4061. unsigned int h = calc_hash(get_str(i));
  4062. fprintf(fp,"%04d : %4u : %s\n",i,h, get_str(i));
  4063. ++count[h];
  4064. }
  4065. fprintf(fp,"--------------------\n\n");
  4066. memset(count2, 0, sizeof(count2));
  4067. for(i=0; i<SCRIPT_HASH_SIZE; i++) {
  4068. fprintf(fp," hash %3d = %d\n",i,count[i]);
  4069. if(min > count[i])
  4070. min = count[i]; // minimun count of collision
  4071. if(max < count[i])
  4072. max = count[i]; // maximun count of collision
  4073. if(count[i] == 0)
  4074. zero++;
  4075. ++count2[count[i]];
  4076. }
  4077. fprintf(fp,"\n--------------------\n items : buckets\n--------------------\n");
  4078. for( i=min; i <= max; ++i ){
  4079. fprintf(fp," %5d : %7d\n",i,count2[i]);
  4080. mean += 1.0f*i*count2[i]/SCRIPT_HASH_SIZE; // Note: this will always result in <nr labels>/<nr buckets>
  4081. }
  4082. for( i=min; i <= max; ++i ){
  4083. n += count2[i];
  4084. if( n*2 >= SCRIPT_HASH_SIZE )
  4085. {
  4086. if( SCRIPT_HASH_SIZE%2 == 0 && SCRIPT_HASH_SIZE/2 == n )
  4087. median = (i+i+1)/2.0f;
  4088. else
  4089. median = i;
  4090. break;
  4091. }
  4092. }
  4093. fprintf(fp,"--------------------\n min = %d, max = %d, zero = %d\n mean = %lf, median = %lf\n",min,max,zero,mean,median);
  4094. fclose(fp);
  4095. }
  4096. }
  4097. #endif
  4098. mapreg_final();
  4099. db_destroy(scriptlabel_db);
  4100. userfunc_db->destroy(userfunc_db, db_script_free_code_sub);
  4101. autobonus_db->destroy(autobonus_db, db_script_free_code_sub);
  4102. ers_destroy(array_ers);
  4103. if (generic_ui_array)
  4104. aFree(generic_ui_array);
  4105. iter = db_iterator(st_db);
  4106. for(st = dbi_first(iter); dbi_exists(iter); st = dbi_next(iter))
  4107. script_free_state(st);
  4108. dbi_destroy(iter);
  4109. if (str_data)
  4110. aFree(str_data);
  4111. if (str_buf)
  4112. aFree(str_buf);
  4113. for( i = 0; i < atcmd_binding_count; i++ ) {
  4114. aFree(atcmd_binding[i]);
  4115. }
  4116. if( atcmd_binding_count != 0 )
  4117. aFree(atcmd_binding);
  4118. ers_destroy(st_ers);
  4119. ers_destroy(stack_ers);
  4120. db_destroy(st_db);
  4121. #ifdef BETA_THREAD_TEST
  4122. /* QueryThread */
  4123. InterlockedIncrement(&queryThreadTerminate);
  4124. racond_signal(queryThreadCond);
  4125. rathread_wait(queryThread, NULL);
  4126. // Destroy cond var and mutex.
  4127. racond_destroy( queryThreadCond );
  4128. ramutex_destroy( queryThreadMutex );
  4129. /* Clear missing vars */
  4130. for( i = 0; i < queryThreadData.count; i++ ) {
  4131. aFree(queryThreadData.entry[i]);
  4132. }
  4133. aFree(queryThreadData.entry);
  4134. for( i = 0; i < logThreadData.count; i++ ) {
  4135. aFree(logThreadData.entry[i]);
  4136. }
  4137. aFree(logThreadData.entry);
  4138. #endif
  4139. }
  4140. /*==========================================
  4141. * Initialization
  4142. *------------------------------------------*/
  4143. void do_init_script(void) {
  4144. st_db = idb_alloc(DB_OPT_BASE);
  4145. userfunc_db = strdb_alloc(DB_OPT_DUP_KEY,0);
  4146. scriptlabel_db = strdb_alloc(DB_OPT_DUP_KEY,50);
  4147. autobonus_db = strdb_alloc(DB_OPT_DUP_KEY,0);
  4148. st_ers = ers_new(sizeof(struct script_state), "script.c::st_ers", ERS_CACHE_OPTIONS);
  4149. stack_ers = ers_new(sizeof(struct script_stack), "script.c::script_stack", ERS_OPT_FLEX_CHUNK);
  4150. array_ers = ers_new(sizeof(struct script_array), "script.c:array_ers", ERS_OPT_CLEAN|ERS_OPT_CLEAR); // TODO: This value is not yet in the enum
  4151. ers_chunk_size(st_ers, 10);
  4152. ers_chunk_size(stack_ers, 10);
  4153. active_scripts = 0;
  4154. next_id = 0;
  4155. mapreg_init();
  4156. #ifdef BETA_THREAD_TEST
  4157. CREATE(queryThreadData.entry, struct queryThreadEntry*, 1);
  4158. queryThreadData.count = 0;
  4159. CREATE(logThreadData.entry, char *, 1);
  4160. logThreadData.count = 0;
  4161. /* QueryThread Start */
  4162. InitializeSpinLock(&queryThreadLock);
  4163. queryThreadData.timer = INVALID_TIMER;
  4164. queryThreadTerminate = 0;
  4165. queryThreadMutex = ramutex_create();
  4166. queryThreadCond = racond_create();
  4167. queryThread = rathread_create(queryThread_main, NULL);
  4168. if(queryThread == NULL){
  4169. ShowFatalError("do_init_script: cannot spawn Query Thread.\n");
  4170. exit(EXIT_FAILURE);
  4171. }
  4172. add_timer_func_list(queryThread_timer, "queryThread_timer");
  4173. #endif
  4174. }
  4175. void script_reload(void) {
  4176. int i;
  4177. DBIterator *iter;
  4178. struct script_state *st;
  4179. #ifdef BETA_THREAD_TEST
  4180. /* we're reloading so any queries undergoing should be...exterminated. */
  4181. EnterSpinLock(&queryThreadLock);
  4182. for( i = 0; i < queryThreadData.count; i++ ) {
  4183. aFree(queryThreadData.entry[i]);
  4184. }
  4185. queryThreadData.count = 0;
  4186. if( queryThreadData.timer != INVALID_TIMER ) {
  4187. delete_timer(queryThreadData.timer, queryThread_timer);
  4188. queryThreadData.timer = INVALID_TIMER;
  4189. }
  4190. LeaveSpinLock(&queryThreadLock);
  4191. #endif
  4192. userfunc_db->clear(userfunc_db, db_script_free_code_sub);
  4193. db_clear(scriptlabel_db);
  4194. // @commands (script based)
  4195. // Clear bindings
  4196. for( i = 0; i < atcmd_binding_count; i++ ) {
  4197. aFree(atcmd_binding[i]);
  4198. }
  4199. if( atcmd_binding_count != 0 )
  4200. aFree(atcmd_binding);
  4201. atcmd_binding_count = 0;
  4202. iter = db_iterator(st_db);
  4203. for(st = dbi_first(iter); dbi_exists(iter); st = dbi_next(iter))
  4204. script_free_state(st);
  4205. dbi_destroy(iter);
  4206. db_clear(st_db);
  4207. mapreg_reload();
  4208. }
  4209. //-----------------------------------------------------------------------------
  4210. // buildin functions
  4211. //
  4212. #define BUILDIN_DEF(x,args) { buildin_ ## x , #x , args }
  4213. #define BUILDIN_DEF2(x,x2,args) { buildin_ ## x , x2 , args }
  4214. #define BUILDIN_FUNC(x) int buildin_ ## x (struct script_state* st)
  4215. /////////////////////////////////////////////////////////////////////
  4216. // NPC interaction
  4217. //
  4218. /// Appends a message to the npc dialog.
  4219. /// If a dialog doesn't exist yet, one is created.
  4220. ///
  4221. /// mes "<message>";
  4222. BUILDIN_FUNC(mes)
  4223. {
  4224. TBL_PC* sd = script_rid2sd(st);
  4225. if( sd == NULL )
  4226. return SCRIPT_CMD_SUCCESS;
  4227. if( !script_hasdata(st, 3) )
  4228. {// only a single line detected in the script
  4229. clif_scriptmes(sd, st->oid, script_getstr(st, 2));
  4230. }
  4231. else
  4232. {// parse multiple lines as they exist
  4233. int i;
  4234. for( i = 2; script_hasdata(st, i); i++ )
  4235. {
  4236. // send the message to the client
  4237. clif_scriptmes(sd, st->oid, script_getstr(st, i));
  4238. }
  4239. }
  4240. st->mes_active = 1; // Invoking character has a NPC dialog box open.
  4241. return SCRIPT_CMD_SUCCESS;
  4242. }
  4243. /// Displays the button 'next' in the npc dialog.
  4244. /// The dialog text is cleared and the script continues when the button is pressed.
  4245. ///
  4246. /// next;
  4247. BUILDIN_FUNC(next)
  4248. {
  4249. TBL_PC* sd;
  4250. sd = script_rid2sd(st);
  4251. if( sd == NULL )
  4252. return SCRIPT_CMD_SUCCESS;
  4253. #ifdef SECURE_NPCTIMEOUT
  4254. sd->npc_idle_type = NPCT_WAIT;
  4255. #endif
  4256. st->state = STOP;
  4257. clif_scriptnext(sd, st->oid);
  4258. return SCRIPT_CMD_SUCCESS;
  4259. }
  4260. /// Ends the script and displays the button 'close' on the npc dialog.
  4261. /// The dialog is closed when the button is pressed.
  4262. ///
  4263. /// close;
  4264. BUILDIN_FUNC(close)
  4265. {
  4266. TBL_PC* sd;
  4267. sd = script_rid2sd(st);
  4268. if( sd == NULL )
  4269. return SCRIPT_CMD_SUCCESS;
  4270. if( !st->mes_active ) {
  4271. TBL_NPC* nd = map_id2nd(st->oid);
  4272. st->state = END; // Keep backwards compatibility.
  4273. ShowWarning("Incorrect use of 'close' command! (source:%s / path:%s)\n",nd?nd->name:"Unknown",nd?nd->path:"Unknown");
  4274. } else {
  4275. st->state = CLOSE;
  4276. st->mes_active = 0;
  4277. }
  4278. clif_scriptclose(sd, st->oid);
  4279. return SCRIPT_CMD_SUCCESS;
  4280. }
  4281. /// Displays the button 'close' on the npc dialog.
  4282. /// The dialog is closed and the script continues when the button is pressed.
  4283. ///
  4284. /// close2;
  4285. BUILDIN_FUNC(close2)
  4286. {
  4287. TBL_PC* sd;
  4288. sd = script_rid2sd(st);
  4289. if( sd == NULL )
  4290. return SCRIPT_CMD_SUCCESS;
  4291. st->state = STOP;
  4292. if( st->mes_active )
  4293. st->mes_active = 0;
  4294. clif_scriptclose(sd, st->oid);
  4295. return SCRIPT_CMD_SUCCESS;
  4296. }
  4297. /// Counts the number of valid and total number of options in 'str'
  4298. /// If max_count > 0 the counting stops when that valid option is reached
  4299. /// total is incremented for each option (NULL is supported)
  4300. static int menu_countoptions(const char* str, int max_count, int* total)
  4301. {
  4302. int count = 0;
  4303. int bogus_total;
  4304. if( total == NULL )
  4305. total = &bogus_total;
  4306. ++(*total);
  4307. // initial empty options
  4308. while( *str == ':' )
  4309. {
  4310. ++str;
  4311. ++(*total);
  4312. }
  4313. // count menu options
  4314. while( *str != '\0' )
  4315. {
  4316. ++count;
  4317. --max_count;
  4318. if( max_count == 0 )
  4319. break;
  4320. while( *str != ':' && *str != '\0' )
  4321. ++str;
  4322. while( *str == ':' )
  4323. {
  4324. ++str;
  4325. ++(*total);
  4326. }
  4327. }
  4328. return count;
  4329. }
  4330. /// Displays a menu with options and goes to the target label.
  4331. /// The script is stopped if cancel is pressed.
  4332. /// Options with no text are not displayed in the client.
  4333. ///
  4334. /// Options can be grouped together, separated by the character ':' in the text:
  4335. /// ex: menu "A:B:C",L_target;
  4336. /// All these options go to the specified target label.
  4337. ///
  4338. /// The index of the selected option is put in the variable @menu.
  4339. /// Indexes start with 1 and are consistent with grouped and empty options.
  4340. /// ex: menu "A::B",-,"",L_Impossible,"C",-;
  4341. /// // displays "A", "B" and "C", corresponding to indexes 1, 3 and 5
  4342. ///
  4343. /// NOTE: the client closes the npc dialog when cancel is pressed
  4344. ///
  4345. /// menu "<option_text>",<target_label>{,"<option_text>",<target_label>,...};
  4346. BUILDIN_FUNC(menu)
  4347. {
  4348. int i;
  4349. const char* text;
  4350. TBL_PC* sd;
  4351. sd = script_rid2sd(st);
  4352. if( sd == NULL )
  4353. return SCRIPT_CMD_SUCCESS;
  4354. #ifdef SECURE_NPCTIMEOUT
  4355. sd->npc_idle_type = NPCT_MENU;
  4356. #endif
  4357. // TODO detect multiple scripts waiting for input at the same time, and what to do when that happens
  4358. if( sd->state.menu_or_input == 0 )
  4359. {
  4360. struct StringBuf buf;
  4361. if( script_lastdata(st) % 2 == 0 )
  4362. {// argument count is not even (1st argument is at index 2)
  4363. ShowError("buildin_menu: Illegal number of arguments (%d).\n", (script_lastdata(st) - 1));
  4364. st->state = END;
  4365. return SCRIPT_CMD_FAILURE;
  4366. }
  4367. StringBuf_Init(&buf);
  4368. sd->npc_menu = 0;
  4369. for( i = 2; i < script_lastdata(st); i += 2 )
  4370. {
  4371. struct script_data* data;
  4372. // menu options
  4373. text = script_getstr(st, i);
  4374. // target label
  4375. data = script_getdata(st, i+1);
  4376. if( !data_islabel(data) )
  4377. {// not a label
  4378. StringBuf_Destroy(&buf);
  4379. ShowError("buildin_menu: Argument #%d (from 1) is not a label or label not found.\n", i);
  4380. script_reportdata(data);
  4381. st->state = END;
  4382. return SCRIPT_CMD_FAILURE;
  4383. }
  4384. // append option(s)
  4385. if( text[0] == '\0' )
  4386. continue;// empty string, ignore
  4387. if( sd->npc_menu > 0 )
  4388. StringBuf_AppendStr(&buf, ":");
  4389. StringBuf_AppendStr(&buf, text);
  4390. sd->npc_menu += menu_countoptions(text, 0, NULL);
  4391. }
  4392. st->state = RERUNLINE;
  4393. sd->state.menu_or_input = 1;
  4394. /**
  4395. * menus beyond this length crash the client (see bugreport:6402)
  4396. **/
  4397. if( StringBuf_Length(&buf) >= 2047 ) {
  4398. struct npc_data * nd = map_id2nd(st->oid);
  4399. char* menu;
  4400. CREATE(menu, char, 2048);
  4401. safestrncpy(menu, StringBuf_Value(&buf), 2047);
  4402. ShowWarning("buildin_menu: NPC Menu too long! (source:%s / length:%d)\n",nd?nd->name:"Unknown",StringBuf_Length(&buf));
  4403. clif_scriptmenu(sd, st->oid, menu);
  4404. aFree(menu);
  4405. } else
  4406. clif_scriptmenu(sd, st->oid, StringBuf_Value(&buf));
  4407. StringBuf_Destroy(&buf);
  4408. if( sd->npc_menu >= 0xff )
  4409. {// client supports only up to 254 entries; 0 is not used and 255 is reserved for cancel; excess entries are displayed but cause 'uint8' overflow
  4410. ShowWarning("buildin_menu: Too many options specified (current=%d, max=254).\n", sd->npc_menu);
  4411. script_reportsrc(st);
  4412. }
  4413. }
  4414. else if( sd->npc_menu == 0xff )
  4415. {// Cancel was pressed
  4416. sd->state.menu_or_input = 0;
  4417. st->state = END;
  4418. }
  4419. else
  4420. {// goto target label
  4421. int menu = 0;
  4422. sd->state.menu_or_input = 0;
  4423. if( sd->npc_menu <= 0 )
  4424. {
  4425. ShowDebug("buildin_menu: Unexpected selection (%d)\n", sd->npc_menu);
  4426. st->state = END;
  4427. return SCRIPT_CMD_FAILURE;
  4428. }
  4429. // get target label
  4430. for( i = 2; i < script_lastdata(st); i += 2 )
  4431. {
  4432. text = script_getstr(st, i);
  4433. sd->npc_menu -= menu_countoptions(text, sd->npc_menu, &menu);
  4434. if( sd->npc_menu <= 0 )
  4435. break;// entry found
  4436. }
  4437. if( sd->npc_menu > 0 )
  4438. {// Invalid selection
  4439. ShowDebug("buildin_menu: Selection is out of range (%d pairs are missing?) - please report this\n", sd->npc_menu);
  4440. st->state = END;
  4441. return SCRIPT_CMD_FAILURE;
  4442. }
  4443. if( !data_islabel(script_getdata(st, i + 1)) )
  4444. {// TODO remove this temporary crash-prevention code (fallback for multiple scripts requesting user input)
  4445. ShowError("buildin_menu: Unexpected data in label argument\n");
  4446. script_reportdata(script_getdata(st, i + 1));
  4447. st->state = END;
  4448. return SCRIPT_CMD_FAILURE;
  4449. }
  4450. pc_setreg(sd, add_str("@menu"), menu);
  4451. st->pos = script_getnum(st, i + 1);
  4452. st->state = GOTO;
  4453. }
  4454. return SCRIPT_CMD_SUCCESS;
  4455. }
  4456. /// Displays a menu with options and returns the selected option.
  4457. /// Behaves like 'menu' without the target labels.
  4458. ///
  4459. /// select(<option_text>{,<option_text>,...}) -> <selected_option>
  4460. ///
  4461. /// @see menu
  4462. BUILDIN_FUNC(select)
  4463. {
  4464. int i;
  4465. const char* text;
  4466. TBL_PC* sd;
  4467. sd = script_rid2sd(st);
  4468. if( sd == NULL )
  4469. return SCRIPT_CMD_SUCCESS;
  4470. #ifdef SECURE_NPCTIMEOUT
  4471. sd->npc_idle_type = NPCT_MENU;
  4472. #endif
  4473. if( sd->state.menu_or_input == 0 ) {
  4474. struct StringBuf buf;
  4475. StringBuf_Init(&buf);
  4476. sd->npc_menu = 0;
  4477. for( i = 2; i <= script_lastdata(st); ++i ) {
  4478. text = script_getstr(st, i);
  4479. if( sd->npc_menu > 0 )
  4480. StringBuf_AppendStr(&buf, ":");
  4481. StringBuf_AppendStr(&buf, text);
  4482. sd->npc_menu += menu_countoptions(text, 0, NULL);
  4483. }
  4484. st->state = RERUNLINE;
  4485. sd->state.menu_or_input = 1;
  4486. /**
  4487. * menus beyond this length crash the client (see bugreport:6402)
  4488. **/
  4489. if( StringBuf_Length(&buf) >= 2047 ) {
  4490. struct npc_data * nd = map_id2nd(st->oid);
  4491. char* menu;
  4492. CREATE(menu, char, 2048);
  4493. safestrncpy(menu, StringBuf_Value(&buf), 2047);
  4494. ShowWarning("buildin_select: NPC Menu too long! (source:%s / length:%d)\n",nd?nd->name:"Unknown",StringBuf_Length(&buf));
  4495. clif_scriptmenu(sd, st->oid, menu);
  4496. aFree(menu);
  4497. } else
  4498. clif_scriptmenu(sd, st->oid, StringBuf_Value(&buf));
  4499. StringBuf_Destroy(&buf);
  4500. if( sd->npc_menu >= 0xff ) {
  4501. ShowWarning("buildin_select: Too many options specified (current=%d, max=254).\n", sd->npc_menu);
  4502. script_reportsrc(st);
  4503. }
  4504. } else if( sd->npc_menu == 0xff ) {// Cancel was pressed
  4505. sd->state.menu_or_input = 0;
  4506. st->state = END;
  4507. } else {// return selected option
  4508. int menu = 0;
  4509. sd->state.menu_or_input = 0;
  4510. for( i = 2; i <= script_lastdata(st); ++i ) {
  4511. text = script_getstr(st, i);
  4512. sd->npc_menu -= menu_countoptions(text, sd->npc_menu, &menu);
  4513. if( sd->npc_menu <= 0 )
  4514. break;// entry found
  4515. }
  4516. pc_setreg(sd, add_str("@menu"), menu);
  4517. script_pushint(st, menu);
  4518. st->state = RUN;
  4519. }
  4520. return SCRIPT_CMD_SUCCESS;
  4521. }
  4522. /// Displays a menu with options and returns the selected option.
  4523. /// Behaves like 'menu' without the target labels, except when cancel is
  4524. /// pressed.
  4525. /// When cancel is pressed, the script continues and 255 is returned.
  4526. ///
  4527. /// prompt(<option_text>{,<option_text>,...}) -> <selected_option>
  4528. ///
  4529. /// @see menu
  4530. BUILDIN_FUNC(prompt)
  4531. {
  4532. int i;
  4533. const char *text;
  4534. TBL_PC* sd;
  4535. sd = script_rid2sd(st);
  4536. if( sd == NULL )
  4537. return SCRIPT_CMD_SUCCESS;
  4538. #ifdef SECURE_NPCTIMEOUT
  4539. sd->npc_idle_type = NPCT_MENU;
  4540. #endif
  4541. if( sd->state.menu_or_input == 0 )
  4542. {
  4543. struct StringBuf buf;
  4544. StringBuf_Init(&buf);
  4545. sd->npc_menu = 0;
  4546. for( i = 2; i <= script_lastdata(st); ++i )
  4547. {
  4548. text = script_getstr(st, i);
  4549. if( sd->npc_menu > 0 )
  4550. StringBuf_AppendStr(&buf, ":");
  4551. StringBuf_AppendStr(&buf, text);
  4552. sd->npc_menu += menu_countoptions(text, 0, NULL);
  4553. }
  4554. st->state = RERUNLINE;
  4555. sd->state.menu_or_input = 1;
  4556. /**
  4557. * menus beyond this length crash the client (see bugreport:6402)
  4558. **/
  4559. if( StringBuf_Length(&buf) >= 2047 ) {
  4560. struct npc_data * nd = map_id2nd(st->oid);
  4561. char* menu;
  4562. CREATE(menu, char, 2048);
  4563. safestrncpy(menu, StringBuf_Value(&buf), 2047);
  4564. ShowWarning("buildin_prompt: NPC Menu too long! (source:%s / length:%d)\n",nd?nd->name:"Unknown",StringBuf_Length(&buf));
  4565. clif_scriptmenu(sd, st->oid, menu);
  4566. aFree(menu);
  4567. } else
  4568. clif_scriptmenu(sd, st->oid, StringBuf_Value(&buf));
  4569. StringBuf_Destroy(&buf);
  4570. if( sd->npc_menu >= 0xff )
  4571. {
  4572. ShowWarning("buildin_prompt: Too many options specified (current=%d, max=254).\n", sd->npc_menu);
  4573. script_reportsrc(st);
  4574. }
  4575. }
  4576. else if( sd->npc_menu == 0xff )
  4577. {// Cancel was pressed
  4578. sd->state.menu_or_input = 0;
  4579. pc_setreg(sd, add_str("@menu"), 0xff);
  4580. script_pushint(st, 0xff);
  4581. st->state = RUN;
  4582. }
  4583. else
  4584. {// return selected option
  4585. int menu = 0;
  4586. sd->state.menu_or_input = 0;
  4587. for( i = 2; i <= script_lastdata(st); ++i )
  4588. {
  4589. text = script_getstr(st, i);
  4590. sd->npc_menu -= menu_countoptions(text, sd->npc_menu, &menu);
  4591. if( sd->npc_menu <= 0 )
  4592. break;// entry found
  4593. }
  4594. pc_setreg(sd, add_str("@menu"), menu);
  4595. script_pushint(st, menu);
  4596. st->state = RUN;
  4597. }
  4598. return SCRIPT_CMD_SUCCESS;
  4599. }
  4600. /////////////////////////////////////////////////////////////////////
  4601. // ...
  4602. //
  4603. /// Jumps to the target script label.
  4604. ///
  4605. /// goto <label>;
  4606. BUILDIN_FUNC(goto)
  4607. {
  4608. if( !data_islabel(script_getdata(st,2)) )
  4609. {
  4610. ShowError("buildin_goto: Not a label\n");
  4611. script_reportdata(script_getdata(st,2));
  4612. st->state = END;
  4613. return SCRIPT_CMD_FAILURE;
  4614. }
  4615. st->pos = script_getnum(st,2);
  4616. st->state = GOTO;
  4617. return SCRIPT_CMD_SUCCESS;
  4618. }
  4619. /*==========================================
  4620. * user-defined function call
  4621. *------------------------------------------*/
  4622. BUILDIN_FUNC(callfunc)
  4623. {
  4624. int i, j;
  4625. struct script_retinfo* ri;
  4626. struct script_code* scr;
  4627. const char* str = script_getstr(st,2);
  4628. struct reg_db *ref = NULL;
  4629. scr = (struct script_code*)strdb_get(userfunc_db, str);
  4630. if(!scr) {
  4631. ShowError("buildin_callfunc: Function not found! [%s]\n", str);
  4632. st->state = END;
  4633. return SCRIPT_CMD_FAILURE;
  4634. }
  4635. ref = (struct reg_db *)aCalloc(sizeof(struct reg_db), 2);
  4636. ref[0].vars = st->stack->scope.vars;
  4637. if (!st->stack->scope.arrays)
  4638. st->stack->scope.arrays = idb_alloc(DB_OPT_BASE); // TODO: Can this happen? when?
  4639. ref[0].arrays = st->stack->scope.arrays;
  4640. ref[1].vars = st->script->local.vars;
  4641. if (!st->script->local.arrays)
  4642. st->script->local.arrays = idb_alloc(DB_OPT_BASE); // TODO: Can this happen? when?
  4643. ref[1].arrays = st->script->local.arrays;
  4644. for(i = st->start+3, j = 0; i < st->end; i++, j++) {
  4645. struct script_data* data = push_copy(st->stack,i);
  4646. if (data_isreference(data) && !data->ref) {
  4647. const char* name = reference_getname(data);
  4648. if (name[0] == '.')
  4649. data->ref = (name[1] == '@' ? &ref[0] : &ref[1]);
  4650. }
  4651. }
  4652. CREATE(ri, struct script_retinfo, 1);
  4653. ri->script = st->script; // script code
  4654. ri->scope.vars = st->stack->scope.vars; // scope variables
  4655. ri->scope.arrays = st->stack->scope.arrays; // scope arrays
  4656. ri->pos = st->pos; // script location
  4657. ri->nargs = j; // argument count
  4658. ri->defsp = st->stack->defsp; // default stack pointer
  4659. push_retinfo(st->stack, ri, ref);
  4660. st->pos = 0;
  4661. st->script = scr;
  4662. st->stack->defsp = st->stack->sp;
  4663. st->state = GOTO;
  4664. st->stack->scope.vars = i64db_alloc(DB_OPT_RELEASE_DATA);
  4665. st->stack->scope.arrays = idb_alloc(DB_OPT_BASE);
  4666. if (!st->script->local.vars)
  4667. st->script->local.vars = i64db_alloc(DB_OPT_RELEASE_DATA);
  4668. return SCRIPT_CMD_SUCCESS;
  4669. }
  4670. /*==========================================
  4671. * subroutine call
  4672. *------------------------------------------*/
  4673. BUILDIN_FUNC(callsub)
  4674. {
  4675. int i,j;
  4676. struct script_retinfo* ri;
  4677. int pos = script_getnum(st,2);
  4678. struct reg_db *ref = NULL;
  4679. if( !data_islabel(script_getdata(st,2)) && !data_isfunclabel(script_getdata(st,2)) ) {
  4680. ShowError("buildin_callsub: Argument is not a label\n");
  4681. script_reportdata(script_getdata(st,2));
  4682. st->state = END;
  4683. return SCRIPT_CMD_FAILURE;
  4684. }
  4685. ref = (struct reg_db *)aCalloc(sizeof(struct reg_db), 1);
  4686. ref[0].vars = st->stack->scope.vars;
  4687. if (!st->stack->scope.arrays)
  4688. st->stack->scope.arrays = idb_alloc(DB_OPT_BASE); // TODO: Can this happen? when?
  4689. ref[0].arrays = st->stack->scope.arrays;
  4690. for(i = st->start+3, j = 0; i < st->end; i++, j++) {
  4691. struct script_data* data = push_copy(st->stack,i);
  4692. if (data_isreference(data) && !data->ref) {
  4693. const char* name = reference_getname(data);
  4694. if (name[0] == '.' && name[1] == '@')
  4695. data->ref = &ref[0];
  4696. }
  4697. }
  4698. CREATE(ri, struct script_retinfo, 1);
  4699. ri->script = st->script; // script code
  4700. ri->scope.vars = st->stack->scope.vars; // scope variables
  4701. ri->scope.arrays = st->stack->scope.arrays; // scope arrays
  4702. ri->pos = st->pos; // script location
  4703. ri->nargs = j; // argument count
  4704. ri->defsp = st->stack->defsp; // default stack pointer
  4705. push_retinfo(st->stack, ri, ref);
  4706. st->pos = pos;
  4707. st->stack->defsp = st->stack->sp;
  4708. st->state = GOTO;
  4709. st->stack->scope.vars = i64db_alloc(DB_OPT_RELEASE_DATA);
  4710. st->stack->scope.arrays = idb_alloc(DB_OPT_BASE);
  4711. return SCRIPT_CMD_SUCCESS;
  4712. }
  4713. /// Retrieves an argument provided to callfunc/callsub.
  4714. /// If the argument doesn't exist
  4715. ///
  4716. /// getarg(<index>{,<default_value>}) -> <value>
  4717. BUILDIN_FUNC(getarg)
  4718. {
  4719. struct script_retinfo* ri;
  4720. int idx;
  4721. if( st->stack->defsp < 1 || st->stack->stack_data[st->stack->defsp - 1].type != C_RETINFO )
  4722. {
  4723. ShowError("buildin_getarg: No callfunc or callsub!\n");
  4724. st->state = END;
  4725. return SCRIPT_CMD_FAILURE;
  4726. }
  4727. ri = st->stack->stack_data[st->stack->defsp - 1].u.ri;
  4728. idx = script_getnum(st,2);
  4729. if( idx >= 0 && idx < ri->nargs )
  4730. push_copy(st->stack, st->stack->defsp - 1 - ri->nargs + idx);
  4731. else if( script_hasdata(st,3) )
  4732. script_pushcopy(st, 3);
  4733. else
  4734. {
  4735. ShowError("buildin_getarg: Index (idx=%d) out of range (nargs=%d) and no default value found\n", idx, ri->nargs);
  4736. st->state = END;
  4737. return SCRIPT_CMD_FAILURE;
  4738. }
  4739. return SCRIPT_CMD_SUCCESS;
  4740. }
  4741. /// Returns from the current function, optionaly returning a value from the functions.
  4742. /// Don't use outside script functions.
  4743. ///
  4744. /// return;
  4745. /// return <value>;
  4746. BUILDIN_FUNC(return)
  4747. {
  4748. if( script_hasdata(st,2) )
  4749. {// return value
  4750. struct script_data* data;
  4751. script_pushcopy(st, 2);
  4752. data = script_getdatatop(st, -1);
  4753. if( data_isreference(data) ) {
  4754. const char* name = reference_getname(data);
  4755. if( name[0] == '.' && name[1] == '@' ) { // scope variable
  4756. if( !data->ref || data->ref->vars == st->stack->scope.vars )
  4757. get_val(st, data); // current scope, convert to value
  4758. if( data->ref && data->ref->vars == st->stack->stack_data[st->stack->defsp-1].u.ri->scope.vars )
  4759. data->ref = NULL; // Reference to the parent scope, remove reference pointer
  4760. } else if( name[0] == '.' && !data->ref ) { // script variable, link to current script
  4761. data->ref = (struct reg_db *)aCalloc(sizeof(struct reg_db), 1);
  4762. data->ref->vars = st->script->local.vars;
  4763. if (!st->script->local.arrays)
  4764. st->script->local.arrays = idb_alloc(DB_OPT_BASE);
  4765. data->ref->arrays = st->script->local.arrays;
  4766. }
  4767. }
  4768. }
  4769. else
  4770. {// no return value
  4771. script_pushnil(st);
  4772. }
  4773. st->state = RETFUNC;
  4774. return SCRIPT_CMD_SUCCESS;
  4775. }
  4776. /// Returns a random number from 0 to <range>-1.
  4777. /// Or returns a random number from <min> to <max>.
  4778. /// If <min> is greater than <max>, their numbers are switched.
  4779. /// rand(<range>) -> <int>
  4780. /// rand(<min>,<max>) -> <int>
  4781. BUILDIN_FUNC(rand)
  4782. {
  4783. int range;
  4784. int min;
  4785. if( script_hasdata(st,3) )
  4786. {// min,max
  4787. int max = script_getnum(st,3);
  4788. min = script_getnum(st,2);
  4789. if( max < min )
  4790. swap(min, max);
  4791. range = max - min + 1;
  4792. }
  4793. else
  4794. {// range
  4795. min = 0;
  4796. range = script_getnum(st,2);
  4797. }
  4798. if( range <= 1 )
  4799. script_pushint(st, min);
  4800. else
  4801. script_pushint(st, rnd()%range + min);
  4802. return SCRIPT_CMD_SUCCESS;
  4803. }
  4804. /*==========================================
  4805. * Warp sd to str,x,y or Random or SavePoint/Save
  4806. *------------------------------------------*/
  4807. BUILDIN_FUNC(warp)
  4808. {
  4809. int ret;
  4810. int x,y;
  4811. const char* str;
  4812. TBL_PC* sd;
  4813. sd = script_rid2sd(st);
  4814. if( sd == NULL )
  4815. return SCRIPT_CMD_SUCCESS;
  4816. str = script_getstr(st,2);
  4817. x = script_getnum(st,3);
  4818. y = script_getnum(st,4);
  4819. if(strcmp(str,"Random")==0)
  4820. ret = pc_randomwarp(sd,CLR_TELEPORT);
  4821. else if(strcmp(str,"SavePoint")==0 || strcmp(str,"Save")==0)
  4822. ret = pc_setpos(sd,sd->status.save_point.map,sd->status.save_point.x,sd->status.save_point.y,CLR_TELEPORT);
  4823. else
  4824. ret = pc_setpos(sd,mapindex_name2id(str),x,y,CLR_OUTSIGHT);
  4825. if( ret ) {
  4826. ShowError("buildin_warp: moving player '%s' to \"%s\",%d,%d failed.\n", sd->status.name, str, x, y);
  4827. return SCRIPT_CMD_FAILURE;
  4828. }
  4829. return SCRIPT_CMD_SUCCESS;
  4830. }
  4831. /*==========================================
  4832. * Warp a specified area
  4833. *------------------------------------------*/
  4834. static int buildin_areawarp_sub(struct block_list *bl,va_list ap)
  4835. {
  4836. int x2,y2,x3,y3;
  4837. unsigned int index;
  4838. index = va_arg(ap,unsigned int);
  4839. x2 = va_arg(ap,int);
  4840. y2 = va_arg(ap,int);
  4841. x3 = va_arg(ap,int);
  4842. y3 = va_arg(ap,int);
  4843. if(index == 0)
  4844. pc_randomwarp((TBL_PC *)bl,CLR_TELEPORT);
  4845. else if(x3 && y3) {
  4846. int max, tx, ty, j = 0;
  4847. // choose a suitable max number of attempts
  4848. if( (max = (y3-y2+1)*(x3-x2+1)*3) > 1000 )
  4849. max = 1000;
  4850. // find a suitable map cell
  4851. do {
  4852. tx = rnd()%(x3-x2+1)+x2;
  4853. ty = rnd()%(y3-y2+1)+y2;
  4854. j++;
  4855. } while( map_getcell(index,tx,ty,CELL_CHKNOPASS) && j < max );
  4856. pc_setpos((TBL_PC *)bl,index,tx,ty,CLR_OUTSIGHT);
  4857. }
  4858. else
  4859. pc_setpos((TBL_PC *)bl,index,x2,y2,CLR_OUTSIGHT);
  4860. return 0;
  4861. }
  4862. BUILDIN_FUNC(areawarp)
  4863. {
  4864. int16 m, x0,y0,x1,y1, x2,y2,x3=0,y3=0;
  4865. unsigned int index;
  4866. const char *str;
  4867. const char *mapname;
  4868. mapname = script_getstr(st,2);
  4869. x0 = script_getnum(st,3);
  4870. y0 = script_getnum(st,4);
  4871. x1 = script_getnum(st,5);
  4872. y1 = script_getnum(st,6);
  4873. str = script_getstr(st,7);
  4874. x2 = script_getnum(st,8);
  4875. y2 = script_getnum(st,9);
  4876. if( script_hasdata(st,10) && script_hasdata(st,11) ) { // Warp area to area
  4877. if( (x3 = script_getnum(st,10)) < 0 || (y3 = script_getnum(st,11)) < 0 ){
  4878. x3 = 0;
  4879. y3 = 0;
  4880. } else if( x3 && y3 ) {
  4881. // normalize x3/y3 coordinates
  4882. if( x3 < x2 ) swap(x3,x2);
  4883. if( y3 < y2 ) swap(y3,y2);
  4884. }
  4885. }
  4886. if( (m = map_mapname2mapid(mapname)) < 0 )
  4887. return SCRIPT_CMD_FAILURE;
  4888. if( strcmp(str,"Random") == 0 )
  4889. index = 0;
  4890. else if( !(index=mapindex_name2id(str)) )
  4891. return SCRIPT_CMD_FAILURE;
  4892. map_foreachinarea(buildin_areawarp_sub, m,x0,y0,x1,y1, BL_PC, index,x2,y2,x3,y3);
  4893. return SCRIPT_CMD_SUCCESS;
  4894. }
  4895. /*==========================================
  4896. * areapercentheal <map>,<x1>,<y1>,<x2>,<y2>,<hp>,<sp>
  4897. *------------------------------------------*/
  4898. static int buildin_areapercentheal_sub(struct block_list *bl,va_list ap)
  4899. {
  4900. int hp, sp;
  4901. hp = va_arg(ap, int);
  4902. sp = va_arg(ap, int);
  4903. pc_percentheal((TBL_PC *)bl,hp,sp);
  4904. return 0;
  4905. }
  4906. BUILDIN_FUNC(areapercentheal)
  4907. {
  4908. int hp,sp,m;
  4909. const char *mapname;
  4910. int x0,y0,x1,y1;
  4911. mapname=script_getstr(st,2);
  4912. x0=script_getnum(st,3);
  4913. y0=script_getnum(st,4);
  4914. x1=script_getnum(st,5);
  4915. y1=script_getnum(st,6);
  4916. hp=script_getnum(st,7);
  4917. sp=script_getnum(st,8);
  4918. if( (m=map_mapname2mapid(mapname))< 0)
  4919. return SCRIPT_CMD_FAILURE;
  4920. map_foreachinarea(buildin_areapercentheal_sub,m,x0,y0,x1,y1,BL_PC,hp,sp);
  4921. return SCRIPT_CMD_SUCCESS;
  4922. }
  4923. /*==========================================
  4924. * warpchar [LuzZza]
  4925. * Useful for warp one player from
  4926. * another player npc-session.
  4927. * Using: warpchar "mapname",x,y,Char_ID;
  4928. *------------------------------------------*/
  4929. BUILDIN_FUNC(warpchar)
  4930. {
  4931. int x,y,a;
  4932. const char *str;
  4933. TBL_PC *sd;
  4934. str=script_getstr(st,2);
  4935. x=script_getnum(st,3);
  4936. y=script_getnum(st,4);
  4937. a=script_getnum(st,5);
  4938. sd = map_charid2sd(a);
  4939. if( sd == NULL )
  4940. return SCRIPT_CMD_SUCCESS;
  4941. if(strcmp(str, "Random") == 0)
  4942. pc_randomwarp(sd, CLR_TELEPORT);
  4943. else
  4944. if(strcmp(str, "SavePoint") == 0)
  4945. pc_setpos(sd, sd->status.save_point.map,sd->status.save_point.x, sd->status.save_point.y, CLR_TELEPORT);
  4946. else
  4947. pc_setpos(sd, mapindex_name2id(str), x, y, CLR_TELEPORT);
  4948. return SCRIPT_CMD_SUCCESS;
  4949. }
  4950. /*==========================================
  4951. * Warpparty - [Fredzilla] [Paradox924X]
  4952. * Syntax: warpparty "to_mapname",x,y,Party_ID,{"from_mapname"};
  4953. * If 'from_mapname' is specified, only the party members on that map will be warped
  4954. *------------------------------------------*/
  4955. BUILDIN_FUNC(warpparty)
  4956. {
  4957. TBL_PC *sd = NULL;
  4958. TBL_PC *pl_sd;
  4959. struct party_data* p;
  4960. int type;
  4961. int mapindex;
  4962. int i;
  4963. const char* str = script_getstr(st,2);
  4964. int x = script_getnum(st,3);
  4965. int y = script_getnum(st,4);
  4966. int p_id = script_getnum(st,5);
  4967. const char* str2 = NULL;
  4968. if ( script_hasdata(st,6) )
  4969. str2 = script_getstr(st,6);
  4970. p = party_search(p_id);
  4971. if(!p)
  4972. return SCRIPT_CMD_SUCCESS;
  4973. type = ( strcmp(str,"Random")==0 ) ? 0
  4974. : ( strcmp(str,"SavePointAll")==0 ) ? 1
  4975. : ( strcmp(str,"SavePoint")==0 ) ? 2
  4976. : ( strcmp(str,"Leader")==0 ) ? 3
  4977. : 4;
  4978. switch (type)
  4979. {
  4980. case 3:
  4981. for(i = 0; i < MAX_PARTY && !p->party.member[i].leader; i++);
  4982. if (i == MAX_PARTY || !p->data[i].sd) //Leader not found / not online
  4983. return SCRIPT_CMD_FAILURE;
  4984. pl_sd = p->data[i].sd;
  4985. mapindex = pl_sd->mapindex;
  4986. x = pl_sd->bl.x;
  4987. y = pl_sd->bl.y;
  4988. break;
  4989. case 4:
  4990. mapindex = mapindex_name2id(str);
  4991. break;
  4992. case 2:
  4993. //"SavePoint" uses save point of the currently attached player
  4994. if (( sd = script_rid2sd(st) ) == NULL )
  4995. return SCRIPT_CMD_SUCCESS;
  4996. default:
  4997. mapindex = 0;
  4998. break;
  4999. }
  5000. for (i = 0; i < MAX_PARTY; i++)
  5001. {
  5002. if( !(pl_sd = p->data[i].sd) || pl_sd->status.party_id != p_id )
  5003. continue;
  5004. if( str2 && strcmp(str2, map[pl_sd->bl.m].name) != 0 )
  5005. continue;
  5006. if( pc_isdead(pl_sd) )
  5007. continue;
  5008. switch( type )
  5009. {
  5010. case 0: // Random
  5011. if(!map[pl_sd->bl.m].flag.nowarp)
  5012. pc_randomwarp(pl_sd,CLR_TELEPORT);
  5013. break;
  5014. case 1: // SavePointAll
  5015. if(!map[pl_sd->bl.m].flag.noreturn)
  5016. pc_setpos(pl_sd,pl_sd->status.save_point.map,pl_sd->status.save_point.x,pl_sd->status.save_point.y,CLR_TELEPORT);
  5017. break;
  5018. case 2: // SavePoint
  5019. if(!map[pl_sd->bl.m].flag.noreturn)
  5020. pc_setpos(pl_sd,sd->status.save_point.map,sd->status.save_point.x,sd->status.save_point.y,CLR_TELEPORT);
  5021. break;
  5022. case 3: // Leader
  5023. case 4: // m,x,y
  5024. if(!map[pl_sd->bl.m].flag.noreturn && !map[pl_sd->bl.m].flag.nowarp)
  5025. pc_setpos(pl_sd,mapindex,x,y,CLR_TELEPORT);
  5026. break;
  5027. }
  5028. }
  5029. return SCRIPT_CMD_SUCCESS;
  5030. }
  5031. /*==========================================
  5032. * Warpguild - [Fredzilla]
  5033. * Syntax: warpguild "mapname",x,y,Guild_ID;
  5034. *------------------------------------------*/
  5035. BUILDIN_FUNC(warpguild)
  5036. {
  5037. TBL_PC *sd = NULL;
  5038. TBL_PC *pl_sd;
  5039. struct guild* g;
  5040. struct s_mapiterator* iter;
  5041. int type;
  5042. const char* str = script_getstr(st,2);
  5043. int x = script_getnum(st,3);
  5044. int y = script_getnum(st,4);
  5045. int gid = script_getnum(st,5);
  5046. g = guild_search(gid);
  5047. if( g == NULL )
  5048. return SCRIPT_CMD_SUCCESS;
  5049. type = ( strcmp(str,"Random")==0 ) ? 0
  5050. : ( strcmp(str,"SavePointAll")==0 ) ? 1
  5051. : ( strcmp(str,"SavePoint")==0 ) ? 2
  5052. : 3;
  5053. if( type == 2 && ( sd = script_rid2sd(st) ) == NULL )
  5054. {// "SavePoint" uses save point of the currently attached player
  5055. return SCRIPT_CMD_SUCCESS;
  5056. }
  5057. iter = mapit_getallusers();
  5058. for( pl_sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); pl_sd = (TBL_PC*)mapit_next(iter) )
  5059. {
  5060. if( pl_sd->status.guild_id != gid )
  5061. continue;
  5062. switch( type )
  5063. {
  5064. case 0: // Random
  5065. if(!map[pl_sd->bl.m].flag.nowarp)
  5066. pc_randomwarp(pl_sd,CLR_TELEPORT);
  5067. break;
  5068. case 1: // SavePointAll
  5069. if(!map[pl_sd->bl.m].flag.noreturn)
  5070. pc_setpos(pl_sd,pl_sd->status.save_point.map,pl_sd->status.save_point.x,pl_sd->status.save_point.y,CLR_TELEPORT);
  5071. break;
  5072. case 2: // SavePoint
  5073. if(!map[pl_sd->bl.m].flag.noreturn)
  5074. pc_setpos(pl_sd,sd->status.save_point.map,sd->status.save_point.x,sd->status.save_point.y,CLR_TELEPORT);
  5075. break;
  5076. case 3: // m,x,y
  5077. if(!map[pl_sd->bl.m].flag.noreturn && !map[pl_sd->bl.m].flag.nowarp)
  5078. pc_setpos(pl_sd,mapindex_name2id(str),x,y,CLR_TELEPORT);
  5079. break;
  5080. }
  5081. }
  5082. mapit_free(iter);
  5083. return SCRIPT_CMD_SUCCESS;
  5084. }
  5085. /*==========================================
  5086. * Force Heal a player (hp and sp)
  5087. *------------------------------------------*/
  5088. BUILDIN_FUNC(heal)
  5089. {
  5090. TBL_PC *sd;
  5091. int hp,sp;
  5092. if (!script_charid2sd(4,sd))
  5093. return SCRIPT_CMD_SUCCESS;
  5094. hp=script_getnum(st,2);
  5095. sp=script_getnum(st,3);
  5096. status_heal(&sd->bl, hp, sp, 1);
  5097. return SCRIPT_CMD_SUCCESS;
  5098. }
  5099. /*==========================================
  5100. * Heal a player by item (get vit bonus etc)
  5101. *------------------------------------------*/
  5102. BUILDIN_FUNC(itemheal)
  5103. {
  5104. TBL_PC *sd;
  5105. int hp,sp;
  5106. hp=script_getnum(st,2);
  5107. sp=script_getnum(st,3);
  5108. if(potion_flag==1) {
  5109. potion_hp = hp;
  5110. potion_sp = sp;
  5111. return SCRIPT_CMD_SUCCESS;
  5112. }
  5113. if (!script_charid2sd(4,sd))
  5114. return SCRIPT_CMD_SUCCESS;
  5115. pc_itemheal(sd,sd->itemid,hp,sp);
  5116. return SCRIPT_CMD_SUCCESS;
  5117. }
  5118. /*==========================================
  5119. *
  5120. *------------------------------------------*/
  5121. BUILDIN_FUNC(percentheal)
  5122. {
  5123. int hp,sp;
  5124. TBL_PC* sd;
  5125. hp=script_getnum(st,2);
  5126. sp=script_getnum(st,3);
  5127. if(potion_flag==1) {
  5128. potion_per_hp = hp;
  5129. potion_per_sp = sp;
  5130. return SCRIPT_CMD_SUCCESS;
  5131. }
  5132. if (!script_charid2sd(4,sd))
  5133. return SCRIPT_CMD_SUCCESS;
  5134. #ifdef RENEWAL
  5135. if( sd->sc.data[SC_EXTREMITYFIST2] )
  5136. sp = 0;
  5137. #endif
  5138. if (sd->sc.data[SC_NORECOVER_STATE]) {
  5139. hp = 0;
  5140. sp = 0;
  5141. }
  5142. pc_percentheal(sd,hp,sp);
  5143. return SCRIPT_CMD_SUCCESS;
  5144. }
  5145. /*==========================================
  5146. *
  5147. *------------------------------------------*/
  5148. BUILDIN_FUNC(jobchange)
  5149. {
  5150. int job, upper=-1;
  5151. job=script_getnum(st,2);
  5152. if( script_hasdata(st,3) )
  5153. upper=script_getnum(st,3);
  5154. if (pcdb_checkid(job))
  5155. {
  5156. TBL_PC* sd;
  5157. if (!script_charid2sd(4,sd))
  5158. return SCRIPT_CMD_SUCCESS;
  5159. pc_jobchange(sd, job, upper);
  5160. }
  5161. return SCRIPT_CMD_SUCCESS;
  5162. }
  5163. /*==========================================
  5164. *
  5165. *------------------------------------------*/
  5166. BUILDIN_FUNC(jobname)
  5167. {
  5168. int class_=script_getnum(st,2);
  5169. script_pushconststr(st, (char*)job_name(class_));
  5170. return SCRIPT_CMD_SUCCESS;
  5171. }
  5172. /// Get input from the player.
  5173. /// For numeric inputs the value is capped to the range [min,max]. Returns 1 if
  5174. /// the value was higher than 'max', -1 if lower than 'min' and 0 otherwise.
  5175. /// For string inputs it returns 1 if the string was longer than 'max', -1 is
  5176. /// shorter than 'min' and 0 otherwise.
  5177. ///
  5178. /// input(<var>{,<min>{,<max>}}) -> <int>
  5179. BUILDIN_FUNC(input)
  5180. {
  5181. TBL_PC* sd;
  5182. struct script_data* data;
  5183. int64 uid;
  5184. const char* name;
  5185. int min;
  5186. int max;
  5187. sd = script_rid2sd(st);
  5188. if( sd == NULL )
  5189. return SCRIPT_CMD_SUCCESS;
  5190. data = script_getdata(st,2);
  5191. if( !data_isreference(data) ){
  5192. ShowError("script:input: not a variable\n");
  5193. script_reportdata(data);
  5194. st->state = END;
  5195. return SCRIPT_CMD_FAILURE;
  5196. }
  5197. uid = reference_getuid(data);
  5198. name = reference_getname(data);
  5199. min = (script_hasdata(st,3) ? script_getnum(st,3) : script_config.input_min_value);
  5200. max = (script_hasdata(st,4) ? script_getnum(st,4) : script_config.input_max_value);
  5201. #ifdef SECURE_NPCTIMEOUT
  5202. sd->npc_idle_type = NPCT_WAIT;
  5203. #endif
  5204. if( !sd->state.menu_or_input )
  5205. { // first invocation, display npc input box
  5206. sd->state.menu_or_input = 1;
  5207. st->state = RERUNLINE;
  5208. if( is_string_variable(name) )
  5209. clif_scriptinputstr(sd,st->oid);
  5210. else
  5211. clif_scriptinput(sd,st->oid);
  5212. }
  5213. else
  5214. { // take received text/value and store it in the designated variable
  5215. sd->state.menu_or_input = 0;
  5216. if( is_string_variable(name) )
  5217. {
  5218. int len = (int)strlen(sd->npc_str);
  5219. set_reg(st, sd, uid, name, (void*)sd->npc_str, script_getref(st,2));
  5220. script_pushint(st, (len > max ? 1 : len < min ? -1 : 0));
  5221. }
  5222. else
  5223. {
  5224. int amount = sd->npc_amount;
  5225. set_reg(st, sd, uid, name, (void*)__64BPRTSIZE(cap_value(amount,min,max)), script_getref(st,2));
  5226. script_pushint(st, (amount > max ? 1 : amount < min ? -1 : 0));
  5227. }
  5228. st->state = RUN;
  5229. }
  5230. return SCRIPT_CMD_SUCCESS;
  5231. }
  5232. // declare the copyarray method here for future reference
  5233. BUILDIN_FUNC(copyarray);
  5234. /// Sets the value of a variable.
  5235. /// The value is converted to the type of the variable.
  5236. ///
  5237. /// set(<variable>,<value>{,<charid>})
  5238. BUILDIN_FUNC(set)
  5239. {
  5240. TBL_PC* sd = NULL;
  5241. struct script_data* data;
  5242. //struct script_data* datavalue;
  5243. int64 num;
  5244. const char* name;
  5245. char prefix;
  5246. data = script_getdata(st,2);
  5247. //datavalue = script_getdata(st,3);
  5248. if( !data_isreference(data) )
  5249. {
  5250. ShowError("script:set: not a variable\n");
  5251. script_reportdata(script_getdata(st,2));
  5252. st->state = END;
  5253. return SCRIPT_CMD_FAILURE;
  5254. }
  5255. num = reference_getuid(data);
  5256. name = reference_getname(data);
  5257. prefix = *name;
  5258. if (not_server_variable(prefix) && !script_charid2sd(4,sd)) {
  5259. ShowError("buildin_set: No player attached for player variable '%s'\n", name);
  5260. return SCRIPT_CMD_FAILURE;
  5261. }
  5262. #if 0
  5263. if( data_isreference(datavalue) )
  5264. {// the value being referenced is a variable
  5265. const char* namevalue = reference_getname(datavalue);
  5266. if( !not_array_variable(*namevalue) )
  5267. {// array variable being copied into another array variable
  5268. if( sd == NULL && not_server_variable(*namevalue) && !(sd = script_rid2sd(st)) )
  5269. {// player must be attached in order to copy a player variable
  5270. ShowError("script:set: no player attached for player variable '%s'\n", namevalue);
  5271. return SCRIPT_CMD_SUCCESS;
  5272. }
  5273. if( is_string_variable(namevalue) != is_string_variable(name) )
  5274. {// non-matching array value types
  5275. ShowWarning("script:set: two array variables do not match in type.\n");
  5276. return SCRIPT_CMD_SUCCESS;
  5277. }
  5278. // push the maximum number of array values to the stack
  5279. push_val(st->stack, C_INT, SCRIPT_MAX_ARRAYSIZE);
  5280. // call the copy array method directly
  5281. return buildin_copyarray(st);
  5282. }
  5283. }
  5284. #endif
  5285. if( is_string_variable(name) )
  5286. set_reg(st,sd,num,name,(void*)script_getstr(st,3),script_getref(st,2));
  5287. else
  5288. set_reg(st,sd,num,name,(void*)__64BPRTSIZE(script_getnum(st,3)),script_getref(st,2));
  5289. // return a copy of the variable reference
  5290. script_pushcopy(st,2);
  5291. return SCRIPT_CMD_SUCCESS;
  5292. }
  5293. /////////////////////////////////////////////////////////////////////
  5294. /// Array variables
  5295. ///
  5296. /// Sets values of an array, from the starting index.
  5297. /// ex: setarray arr[1],1,2,3;
  5298. ///
  5299. /// setarray <array variable>,<value1>{,<value2>...};
  5300. BUILDIN_FUNC(setarray)
  5301. {
  5302. struct script_data* data;
  5303. const char* name;
  5304. uint32 start;
  5305. uint32 end;
  5306. int32 id;
  5307. int32 i;
  5308. TBL_PC* sd = NULL;
  5309. data = script_getdata(st, 2);
  5310. if( !data_isreference(data) )
  5311. {
  5312. ShowError("script:setarray: not a variable\n");
  5313. script_reportdata(data);
  5314. st->state = END;
  5315. return SCRIPT_CMD_FAILURE;// not a variable
  5316. }
  5317. id = reference_getid(data);
  5318. start = reference_getindex(data);
  5319. name = reference_getname(data);
  5320. if( not_server_variable(*name) )
  5321. {
  5322. sd = script_rid2sd(st);
  5323. if( sd == NULL )
  5324. return SCRIPT_CMD_SUCCESS;// no player attached
  5325. }
  5326. end = start + script_lastdata(st) - 2;
  5327. if( end > SCRIPT_MAX_ARRAYSIZE )
  5328. end = SCRIPT_MAX_ARRAYSIZE;
  5329. if( is_string_variable(name) )
  5330. {// string array
  5331. for( i = 3; start < end; ++start, ++i )
  5332. set_reg(st, sd, reference_uid(id, start), name, (void*)script_getstr(st,i), reference_getref(data));
  5333. }
  5334. else
  5335. {// int array
  5336. for( i = 3; start < end; ++start, ++i )
  5337. set_reg(st, sd, reference_uid(id, start), name, (void*)__64BPRTSIZE(script_getnum(st,i)), reference_getref(data));
  5338. }
  5339. return SCRIPT_CMD_SUCCESS;
  5340. }
  5341. /// Sets count values of an array, from the starting index.
  5342. /// ex: cleararray arr[0],0,1;
  5343. ///
  5344. /// cleararray <array variable>,<value>,<count>;
  5345. BUILDIN_FUNC(cleararray)
  5346. {
  5347. struct script_data* data;
  5348. const char* name;
  5349. uint32 start;
  5350. uint32 end;
  5351. int32 id;
  5352. void* v;
  5353. TBL_PC* sd = NULL;
  5354. data = script_getdata(st, 2);
  5355. if( !data_isreference(data) )
  5356. {
  5357. ShowError("script:cleararray: not a variable\n");
  5358. script_reportdata(data);
  5359. st->state = END;
  5360. return SCRIPT_CMD_FAILURE;// not a variable
  5361. }
  5362. id = reference_getid(data);
  5363. start = reference_getindex(data);
  5364. name = reference_getname(data);
  5365. if( not_server_variable(*name) )
  5366. {
  5367. sd = script_rid2sd(st);
  5368. if( sd == NULL )
  5369. return SCRIPT_CMD_SUCCESS;// no player attached
  5370. }
  5371. if( is_string_variable(name) )
  5372. v = (void*)script_getstr(st, 3);
  5373. else
  5374. v = (void*)__64BPRTSIZE(script_getnum(st, 3));
  5375. end = start + script_getnum(st, 4);
  5376. if( end > SCRIPT_MAX_ARRAYSIZE )
  5377. end = SCRIPT_MAX_ARRAYSIZE;
  5378. for( ; start < end; ++start )
  5379. set_reg(st, sd, reference_uid(id, start), name, v, script_getref(st,2));
  5380. return SCRIPT_CMD_SUCCESS;
  5381. }
  5382. /// Copies data from one array to another.
  5383. /// ex: copyarray arr[0],arr[2],2;
  5384. ///
  5385. /// copyarray <destination array variable>,<source array variable>,<count>;
  5386. BUILDIN_FUNC(copyarray)
  5387. {
  5388. struct script_data* data1;
  5389. struct script_data* data2;
  5390. const char* name1;
  5391. const char* name2;
  5392. int32 idx1;
  5393. int32 idx2;
  5394. int32 id1;
  5395. int32 id2;
  5396. void* v;
  5397. int32 i;
  5398. uint32 count;
  5399. TBL_PC* sd = NULL;
  5400. data1 = script_getdata(st, 2);
  5401. data2 = script_getdata(st, 3);
  5402. if( !data_isreference(data1) || !data_isreference(data2) )
  5403. {
  5404. ShowError("script:copyarray: not a variable\n");
  5405. script_reportdata(data1);
  5406. script_reportdata(data2);
  5407. st->state = END;
  5408. return SCRIPT_CMD_FAILURE;// not a variable
  5409. }
  5410. id1 = reference_getid(data1);
  5411. id2 = reference_getid(data2);
  5412. idx1 = reference_getindex(data1);
  5413. idx2 = reference_getindex(data2);
  5414. name1 = reference_getname(data1);
  5415. name2 = reference_getname(data2);
  5416. if( is_string_variable(name1) != is_string_variable(name2) )
  5417. {
  5418. ShowError("script:copyarray: type mismatch\n");
  5419. script_reportdata(data1);
  5420. script_reportdata(data2);
  5421. st->state = END;
  5422. return SCRIPT_CMD_FAILURE;// data type mismatch
  5423. }
  5424. if( not_server_variable(*name1) || not_server_variable(*name2) )
  5425. {
  5426. sd = script_rid2sd(st);
  5427. if( sd == NULL )
  5428. return SCRIPT_CMD_SUCCESS;// no player attached
  5429. }
  5430. count = script_getnum(st, 4);
  5431. if( count > SCRIPT_MAX_ARRAYSIZE - idx1 )
  5432. count = SCRIPT_MAX_ARRAYSIZE - idx1;
  5433. if( count <= 0 || (id1 == id2 && idx1 == idx2) )
  5434. return SCRIPT_CMD_SUCCESS;// nothing to copy
  5435. if( id1 == id2 && idx1 > idx2 )
  5436. {// destination might be overlapping the source - copy in reverse order
  5437. for( i = count - 1; i >= 0; --i )
  5438. {
  5439. v = get_val2(st, reference_uid(id2, idx2 + i), reference_getref(data2));
  5440. set_reg(st, sd, reference_uid(id1, idx1 + i), name1, v, reference_getref(data1));
  5441. script_removetop(st, -1, 0);
  5442. }
  5443. }
  5444. else
  5445. {// normal copy
  5446. for( i = 0; i < count; ++i )
  5447. {
  5448. if( idx2 + i < SCRIPT_MAX_ARRAYSIZE )
  5449. {
  5450. v = get_val2(st, reference_uid(id2, idx2 + i), reference_getref(data2));
  5451. set_reg(st, sd, reference_uid(id1, idx1 + i), name1, v, reference_getref(data1));
  5452. script_removetop(st, -1, 0);
  5453. }
  5454. else// out of range - assume ""/0
  5455. set_reg(st, sd, reference_uid(id1, idx1 + i), name1, (is_string_variable(name1)?(void*)"":(void*)0), reference_getref(data1));
  5456. }
  5457. }
  5458. return SCRIPT_CMD_SUCCESS;
  5459. }
  5460. /// Returns the size of the array.
  5461. /// Assumes that everything before the starting index exists.
  5462. /// ex: getarraysize(arr[3])
  5463. ///
  5464. /// getarraysize(<array variable>) -> <int>
  5465. BUILDIN_FUNC(getarraysize)
  5466. {
  5467. struct script_data* data;
  5468. data = script_getdata(st, 2);
  5469. if( !data_isreference(data) )
  5470. {
  5471. ShowError("script:getarraysize: not a variable\n");
  5472. script_reportdata(data);
  5473. script_pushnil(st);
  5474. st->state = END;
  5475. return SCRIPT_CMD_FAILURE;// not a variable
  5476. }
  5477. script_pushint(st, script_array_highest_key(st, st->rid ? script_rid2sd(st) : NULL, reference_getname(data), reference_getref(data)));
  5478. return SCRIPT_CMD_SUCCESS;
  5479. }
  5480. int script_array_index_cmp(const void *a, const void *b) {
  5481. return ( *(unsigned int*)a - *(unsigned int*)b );
  5482. }
  5483. /// Deletes count or all the elements in an array, from the starting index.
  5484. /// ex: deletearray arr[4],2;
  5485. ///
  5486. /// deletearray <array variable>;
  5487. /// deletearray <array variable>,<count>;
  5488. BUILDIN_FUNC(deletearray)
  5489. {
  5490. struct script_data* data;
  5491. const char* name;
  5492. unsigned int start, end, i;
  5493. int id;
  5494. TBL_PC *sd = NULL;
  5495. struct script_array *sa = NULL;
  5496. struct reg_db *src = NULL;
  5497. void *value;
  5498. data = script_getdata(st, 2);
  5499. if( !data_isreference(data) ) {
  5500. ShowError("script:deletearray: not a variable\n");
  5501. script_reportdata(data);
  5502. st->state = END;
  5503. return SCRIPT_CMD_FAILURE;// not a variable
  5504. }
  5505. id = reference_getid(data);
  5506. start = reference_getindex(data);
  5507. name = reference_getname(data);
  5508. if( not_server_variable(*name) ) {
  5509. sd = script_rid2sd(st);
  5510. if( sd == NULL )
  5511. return SCRIPT_CMD_SUCCESS;// no player attached
  5512. }
  5513. if (!(src = script_array_src(st, sd, name, reference_getref(data)))) {
  5514. ShowError("script:deletearray: not a array\n");
  5515. script_reportdata(data);
  5516. st->state = END;
  5517. return SCRIPT_CMD_FAILURE;// not a variable
  5518. }
  5519. script_array_ensure_zero(st, NULL, data->u.num, reference_getref(data));
  5520. if ( !(sa = idb_get(src->arrays, id)) ) { // non-existent array, nothing to empty
  5521. return SCRIPT_CMD_SUCCESS;// not a variable
  5522. }
  5523. end = script_array_highest_key(st, sd, name, reference_getref(data));
  5524. if( start >= end )
  5525. return SCRIPT_CMD_SUCCESS;// nothing to free
  5526. if( is_string_variable(name) )
  5527. value = (void *)"";
  5528. else
  5529. value = (void *)0;
  5530. if( script_hasdata(st,3) ) {
  5531. unsigned int count = script_getnum(st, 3);
  5532. if( count > end - start )
  5533. count = end - start;
  5534. if( count <= 0 )
  5535. return SCRIPT_CMD_SUCCESS;// nothing to free
  5536. if( end - start < sa->size ) {
  5537. // Better to iterate directly on the array, no speed-up from using sa
  5538. for( ; start + count < end; ++start ) {
  5539. // Compact and overwrite
  5540. void* v = get_val2(st, reference_uid(id, start + count), reference_getref(data));
  5541. set_reg(st, sd, reference_uid(id, start), name, v, reference_getref(data));
  5542. script_removetop(st, -1, 0);
  5543. }
  5544. for( ; start < end; start++ ) {
  5545. // Clean up any leftovers that weren't overwritten
  5546. set_reg(st, sd, reference_uid(id, start), name, value, reference_getref(data));
  5547. }
  5548. } else {
  5549. // using sa to speed up
  5550. unsigned int *list = NULL, size = 0;
  5551. list = script_array_cpy_list(sa);
  5552. size = sa->size;
  5553. qsort(list, size, sizeof(unsigned int), script_array_index_cmp);
  5554. ARR_FIND(0, size, i, list[i] >= start);
  5555. for( ; i < size && list[i] < start + count; i++ ) {
  5556. // Clear any entries between start and start+count, if they exist
  5557. set_reg(st, sd, reference_uid(id, list[i]), name, value, reference_getref(data));
  5558. }
  5559. for( ; i < size && list[i] < end; i++ ) {
  5560. // Move back count positions any entries between start+count to fill the gaps
  5561. void* v = get_val2(st, reference_uid(id, list[i]), reference_getref(data));
  5562. set_reg(st, sd, reference_uid(id, list[i]-count), name, v, reference_getref(data));
  5563. script_removetop(st, -1, 0);
  5564. // Clear their originals
  5565. set_reg(st, sd, reference_uid(id, list[i]), name, value, reference_getref(data));
  5566. }
  5567. }
  5568. } else {
  5569. unsigned int *list = NULL, size = 0;
  5570. list = script_array_cpy_list(sa);
  5571. size = sa->size;
  5572. for(i = 0; i < size; i++) {
  5573. if( list[i] >= start ) // Less expensive than sorting it, most likely
  5574. set_reg(st, sd, reference_uid(id, list[i]), name, value, reference_getref(data));
  5575. }
  5576. }
  5577. return SCRIPT_CMD_SUCCESS;
  5578. }
  5579. /// Returns a reference to the target index of the array variable.
  5580. /// Equivalent to var[index].
  5581. ///
  5582. /// getelementofarray(<array variable>,<index>) -> <variable reference>
  5583. BUILDIN_FUNC(getelementofarray)
  5584. {
  5585. struct script_data* data;
  5586. int32 id;
  5587. int64 i;
  5588. data = script_getdata(st, 2);
  5589. if( !data_isreference(data) )
  5590. {
  5591. ShowError("script:getelementofarray: not a variable\n");
  5592. script_reportdata(data);
  5593. script_pushnil(st);
  5594. st->state = END;
  5595. return SCRIPT_CMD_SUCCESS;// not a variable
  5596. }
  5597. id = reference_getid(data);
  5598. i = script_getnum(st, 3);
  5599. if (i < 0 || i >= SCRIPT_MAX_ARRAYSIZE) {
  5600. ShowWarning("script:getelementofarray: index out of range (%"PRId64")\n", i);
  5601. script_reportdata(data);
  5602. script_pushnil(st);
  5603. st->state = END;
  5604. return SCRIPT_CMD_FAILURE;// out of range
  5605. }
  5606. push_val2(st->stack, C_NAME, reference_uid(id, i), reference_getref(data));
  5607. return SCRIPT_CMD_SUCCESS;
  5608. }
  5609. /////////////////////////////////////////////////////////////////////
  5610. /// ...
  5611. ///
  5612. /*==========================================
  5613. *
  5614. *------------------------------------------*/
  5615. BUILDIN_FUNC(setlook)
  5616. {
  5617. int type,val;
  5618. TBL_PC* sd;
  5619. type = script_getnum(st,2);
  5620. val = script_getnum(st,3);
  5621. if (!script_charid2sd(4,sd))
  5622. return SCRIPT_CMD_SUCCESS;
  5623. pc_changelook(sd,type,val);
  5624. return SCRIPT_CMD_SUCCESS;
  5625. }
  5626. BUILDIN_FUNC(changelook)
  5627. { // As setlook but only client side
  5628. int type,val;
  5629. TBL_PC* sd;
  5630. type = script_getnum(st,2);
  5631. val = script_getnum(st,3);
  5632. if (!script_charid2sd(4,sd))
  5633. return SCRIPT_CMD_SUCCESS;
  5634. clif_changelook(&sd->bl,type,val);
  5635. return SCRIPT_CMD_SUCCESS;
  5636. }
  5637. /*==========================================
  5638. *
  5639. *------------------------------------------*/
  5640. BUILDIN_FUNC(cutin)
  5641. {
  5642. TBL_PC* sd;
  5643. sd = script_rid2sd(st);
  5644. if( sd == NULL )
  5645. return SCRIPT_CMD_SUCCESS;
  5646. clif_cutin(sd,script_getstr(st,2),script_getnum(st,3));
  5647. return SCRIPT_CMD_SUCCESS;
  5648. }
  5649. /*==========================================
  5650. *
  5651. *------------------------------------------*/
  5652. BUILDIN_FUNC(viewpoint)
  5653. {
  5654. int type,x,y,id,color;
  5655. TBL_PC* sd;
  5656. type=script_getnum(st,2);
  5657. x=script_getnum(st,3);
  5658. y=script_getnum(st,4);
  5659. id=script_getnum(st,5);
  5660. color=script_getnum(st,6);
  5661. sd = script_rid2sd(st);
  5662. if( sd == NULL )
  5663. return SCRIPT_CMD_SUCCESS;
  5664. clif_viewpoint(sd,st->oid,type,x,y,id,color);
  5665. return SCRIPT_CMD_SUCCESS;
  5666. }
  5667. /// Returns number of items in inventory/cart/storage
  5668. /// countitem <nameID>{,<accountID>});
  5669. /// countitem2 <nameID>,<Identified>,<Refine>,<Attribute>,<Card0>,<Card1>,<Card2>,<Card3>{,<accountID>}) [Lupus]
  5670. /// cartcountitem <nameID>{,<accountID>});
  5671. /// cartcountitem2 <nameID>,<Identified>,<Refine>,<Attribute>,<Card0>,<Card1>,<Card2>,<Card3>{,<accountID>})
  5672. /// storagecountitem <nameID>{,<accountID>});
  5673. /// storagecountitem2 <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. if( command[strlen(command)-1] == '2' ) {
  5685. i = 1;
  5686. aid = 10;
  5687. }
  5688. if( script_hasdata(st,aid) ) {
  5689. if( !(sd = map_id2sd( (aid = script_getnum(st, aid)) )) ) {
  5690. ShowError("buildin_%s: player not found (AID=%d).\n", command, aid);
  5691. st->state = END;
  5692. return SCRIPT_CMD_FAILURE;
  5693. }
  5694. }
  5695. else {
  5696. if( !(sd = script_rid2sd(st)) )
  5697. return SCRIPT_CMD_SUCCESS;
  5698. }
  5699. if( !strncmp(command, "cart", 4) ) {
  5700. loc = 1;
  5701. size = MAX_CART;
  5702. items = sd->status.cart;
  5703. }
  5704. else if( !strncmp(command, "storage", 7) ) {
  5705. loc = 2;
  5706. size = MAX_STORAGE;
  5707. items = sd->status.storage.items;
  5708. }
  5709. //TODO: 3 - Guild Storage
  5710. else {
  5711. size = MAX_INVENTORY;
  5712. items = sd->status.inventory;
  5713. }
  5714. if( loc == 1 && !pc_iscarton(sd) ) {
  5715. ShowError("buildin_%s: Player doesn't have cart (CID:%d).\n", command, sd->status.char_id);
  5716. script_pushint(st,-1);
  5717. return SCRIPT_CMD_SUCCESS;
  5718. }
  5719. data = script_getdata(st, 2);
  5720. get_val(st, data); // Convert into value in case of a variable
  5721. if( data_isstring(data) ) // item name
  5722. id = itemdb_searchname(conv_str(st, data));
  5723. else // item id
  5724. id = itemdb_exists(conv_num(st, data));
  5725. if( id == NULL ) {
  5726. ShowError("buildin_%s: Invalid item '%s'.\n", command, script_getstr(st,2)); // returns string, regardless of what it was
  5727. script_pushint(st,0);
  5728. return SCRIPT_CMD_FAILURE;
  5729. }
  5730. if( !i ) { // For count/cart/storagecountitem function
  5731. unsigned short nameid = id->nameid;
  5732. for( i = 0; i < size; i++ )
  5733. if( &items[i] && items[i].nameid == nameid )
  5734. count += items[i].amount;
  5735. }
  5736. else { // For count/cart/storagecountitem2 function
  5737. unsigned short nameid;
  5738. int iden, ref, attr, c1, c2, c3, c4;
  5739. nameid = id->nameid;
  5740. iden = script_getnum(st,3);
  5741. ref = script_getnum(st,4);
  5742. attr = script_getnum(st,5);
  5743. c1 = script_getnum(st,6);
  5744. c2 = script_getnum(st,7);
  5745. c3 = script_getnum(st,8);
  5746. c4 = script_getnum(st,9);
  5747. for( i = 0; i < size; i++ )
  5748. if( &items[i] && items[i].nameid > 0 && items[i].nameid == nameid &&
  5749. items[i].amount > 0 && items[i].identify == iden &&
  5750. items[i].refine == ref && items[i].attribute == attr &&
  5751. items[i].card[0] == c1 && items[i].card[1] == c2 &&
  5752. items[i].card[2] == c3 && items[i].card[3] == c4 )
  5753. {
  5754. count += items[i].amount;
  5755. }
  5756. }
  5757. script_pushint(st, count);
  5758. return SCRIPT_CMD_SUCCESS;
  5759. }
  5760. /*==========================================
  5761. * Check if item with this amount can fit in inventory
  5762. * Checking : weight, stack amount >(MAX_AMOUNT), slots amount >(MAX_INVENTORY)
  5763. * Return
  5764. * 0 : fail
  5765. * 1 : success (npc side only)
  5766. *------------------------------------------*/
  5767. BUILDIN_FUNC(checkweight)
  5768. {
  5769. int slots, amount2 = 0;
  5770. unsigned int weight = 0, i, nbargs;
  5771. struct item_data* id = NULL;
  5772. struct map_session_data* sd;
  5773. if( ( sd = script_rid2sd(st) ) == NULL )
  5774. return SCRIPT_CMD_SUCCESS;
  5775. nbargs = script_lastdata(st)+1;
  5776. if(nbargs%2) {
  5777. ShowError("buildin_checkweight: Invalid nb of args should be a multiple of 2.\n");
  5778. script_pushint(st,0);
  5779. return SCRIPT_CMD_FAILURE;
  5780. }
  5781. slots = pc_inventoryblank(sd); //nb of empty slot
  5782. for (i = 2; i < nbargs; i += 2) {
  5783. unsigned short nameid, amount;
  5784. struct script_data* data = script_getdata(st,i);
  5785. get_val(st, data); // Convert into value in case of a variable
  5786. if( data_isstring(data) ) // item name
  5787. id = itemdb_searchname(conv_str(st, data));
  5788. else // item id
  5789. id = itemdb_exists(conv_num(st, data));
  5790. if( id == NULL ) {
  5791. ShowError("buildin_checkweight: Invalid item '%s'.\n", script_getstr(st,i)); // returns string, regardless of what it was
  5792. script_pushint(st,0);
  5793. return SCRIPT_CMD_FAILURE;
  5794. }
  5795. nameid = id->nameid;
  5796. amount = script_getnum(st,i+1);
  5797. if( amount < 1 ) {
  5798. ShowError("buildin_checkweight: Invalid amount '%d'.\n", amount);
  5799. script_pushint(st,0);
  5800. return SCRIPT_CMD_FAILURE;
  5801. }
  5802. weight += itemdb_weight(nameid)*amount; //total weight for all chk
  5803. if( weight + sd->weight > sd->max_weight )
  5804. {// too heavy
  5805. script_pushint(st,0);
  5806. return SCRIPT_CMD_SUCCESS;
  5807. }
  5808. switch( pc_checkadditem(sd, nameid, amount) ) {
  5809. case CHKADDITEM_EXIST:
  5810. // item is already in inventory, but there is still space for the requested amount
  5811. break;
  5812. case CHKADDITEM_NEW:
  5813. if( itemdb_isstackable(nameid) ) {
  5814. // stackable
  5815. amount2++;
  5816. if( slots < amount2 ) {
  5817. script_pushint(st,0);
  5818. return SCRIPT_CMD_SUCCESS;
  5819. }
  5820. } else {
  5821. // non-stackable
  5822. amount2 += amount;
  5823. if( slots < amount2) {
  5824. script_pushint(st,0);
  5825. return SCRIPT_CMD_SUCCESS;
  5826. }
  5827. }
  5828. break;
  5829. case CHKADDITEM_OVERAMOUNT:
  5830. script_pushint(st,0);
  5831. return SCRIPT_CMD_SUCCESS;
  5832. }
  5833. }
  5834. script_pushint(st,1);
  5835. return SCRIPT_CMD_SUCCESS;
  5836. }
  5837. BUILDIN_FUNC(checkweight2)
  5838. {
  5839. //variable sub checkweight
  5840. int i = 0, amount2 = 0, slots = 0, weight = 0;
  5841. short fail = 0;
  5842. //variable for array parsing
  5843. struct script_data* data_it;
  5844. struct script_data* data_nb;
  5845. const char* name_it;
  5846. const char* name_nb;
  5847. int32 id_it, id_nb;
  5848. int32 idx_it, idx_nb;
  5849. int nb_it, nb_nb; //array size
  5850. TBL_PC *sd = script_rid2sd(st);
  5851. nullpo_retr(1,sd);
  5852. data_it = script_getdata(st, 2);
  5853. data_nb = script_getdata(st, 3);
  5854. if( !data_isreference(data_it) || !data_isreference(data_nb)) {
  5855. ShowError("buildin_checkweight2: parameter not a variable\n");
  5856. script_pushint(st,0);
  5857. return SCRIPT_CMD_FAILURE;// not a variable
  5858. }
  5859. id_it = reference_getid(data_it);
  5860. id_nb = reference_getid(data_nb);
  5861. idx_it = reference_getindex(data_it);
  5862. idx_nb = reference_getindex(data_nb);
  5863. name_it = reference_getname(data_it);
  5864. name_nb = reference_getname(data_nb);
  5865. if(is_string_variable(name_it) || is_string_variable(name_nb)) {
  5866. ShowError("buildin_checkweight2: illegal type, need int\n");
  5867. script_pushint(st,0);
  5868. return SCRIPT_CMD_FAILURE;// not supported
  5869. }
  5870. nb_it = script_array_highest_key(st, sd, reference_getname(data_it), reference_getref(data_it));
  5871. nb_nb = script_array_highest_key(st, sd, reference_getname(data_nb), reference_getref(data_nb));
  5872. if(nb_it != nb_nb) {
  5873. ShowError("buildin_checkweight2: Size mistmatch: nb_it=%d, nb_nb=%d\n",nb_it,nb_nb);
  5874. fail = 1;
  5875. }
  5876. slots = pc_inventoryblank(sd);
  5877. for(i=0; i<nb_it; i++) {
  5878. unsigned short amount, nameid = (int32)__64BPRTSIZE(get_val2(st,reference_uid(id_it,idx_it+i),reference_getref(data_it)));
  5879. script_removetop(st, -1, 0);
  5880. amount = (int32)__64BPRTSIZE(get_val2(st,reference_uid(id_nb,idx_nb+i),reference_getref(data_nb)));
  5881. script_removetop(st, -1, 0);
  5882. if(fail)
  5883. continue; //cpntonie to depop rest
  5884. if(itemdb_exists(nameid) == NULL ) {
  5885. ShowError("buildin_checkweight2: Invalid item '%d'.\n", nameid);
  5886. fail=1;
  5887. continue;
  5888. }
  5889. if(amount < 0 ) {
  5890. ShowError("buildin_checkweight2: Invalid amount '%d'.\n", amount);
  5891. fail = 1;
  5892. continue;
  5893. }
  5894. weight += itemdb_weight(nameid)*amount;
  5895. if( weight + sd->weight > sd->max_weight ) {
  5896. fail = 1;
  5897. continue;
  5898. }
  5899. switch( pc_checkadditem(sd, nameid, amount) ) {
  5900. case CHKADDITEM_EXIST:
  5901. // item is already in inventory, but there is still space for the requested amount
  5902. break;
  5903. case CHKADDITEM_NEW:
  5904. if( itemdb_isstackable(nameid) ) {// stackable
  5905. amount2++;
  5906. if( slots < amount2 )
  5907. fail = 1;
  5908. } else {// non-stackable
  5909. amount2 += amount;
  5910. if( slots < amount2 ) {
  5911. fail = 1;
  5912. }
  5913. }
  5914. break;
  5915. case CHKADDITEM_OVERAMOUNT:
  5916. fail = 1;
  5917. } //end switch
  5918. } //end loop DO NOT break it prematurly we need to depop all stack
  5919. fail ? script_pushint(st,0) : script_pushint(st,1);
  5920. return SCRIPT_CMD_SUCCESS;
  5921. }
  5922. /*==========================================
  5923. * getitem <item id>,<amount>{,<account ID>};
  5924. * getitem "<item name>",<amount>{,<account ID>};
  5925. *
  5926. * getitembound <item id>,<amount>,<type>{,<account ID>};
  5927. * getitembound "<item id>",<amount>,<type>{,<account ID>};
  5928. * Type:
  5929. * 0 - No bound
  5930. * 1 - Account Bound
  5931. * 2 - Guild Bound
  5932. * 3 - Party Bound
  5933. * 4 - Character Bound
  5934. *------------------------------------------*/
  5935. BUILDIN_FUNC(getitem)
  5936. {
  5937. int amount, get_count, i;
  5938. unsigned short nameid;
  5939. struct item it;
  5940. TBL_PC *sd;
  5941. struct script_data *data;
  5942. unsigned char flag = 0;
  5943. const char* command = script_getfuncname(st);
  5944. struct item_data *id = NULL;
  5945. data = script_getdata(st,2);
  5946. get_val(st,data);
  5947. if( data_isstring(data) ) {// "<item name>"
  5948. const char *name = conv_str(st,data);
  5949. id = itemdb_searchname(name);
  5950. if( id == NULL ){
  5951. ShowError("buildin_getitem: Nonexistant item %s requested.\n", name);
  5952. return SCRIPT_CMD_FAILURE; //No item created.
  5953. }
  5954. nameid = id->nameid;
  5955. } else if( data_isint(data) ) {// <item id>
  5956. nameid = conv_num(st,data);
  5957. if( !(id = itemdb_exists(nameid)) ){
  5958. ShowError("buildin_getitem: Nonexistant item %d requested.\n", nameid);
  5959. return SCRIPT_CMD_FAILURE; //No item created.
  5960. }
  5961. } else {
  5962. ShowError("buildin_getitem: invalid data type for argument #1 (%d).", data->type);
  5963. return SCRIPT_CMD_FAILURE;
  5964. }
  5965. // <amount>
  5966. if( (amount = script_getnum(st,3)) <= 0)
  5967. return SCRIPT_CMD_SUCCESS; //return if amount <=0, skip the useles iteration
  5968. memset(&it,0,sizeof(it));
  5969. it.nameid = nameid;
  5970. it.identify = 1;
  5971. it.bound = BOUND_NONE;
  5972. if( !strcmp(command,"getitembound") ) {
  5973. char bound = script_getnum(st,4);
  5974. if( bound < BOUND_NONE || bound >= BOUND_MAX ) {
  5975. ShowError("script_getitembound: Not a correct bound type! Type=%d\n",bound);
  5976. return SCRIPT_CMD_FAILURE;
  5977. }
  5978. if( script_hasdata(st,5) )
  5979. sd = map_id2sd(script_getnum(st,5));
  5980. else
  5981. sd = script_rid2sd(st); // Attached player
  5982. it.bound = bound;
  5983. } else if( script_hasdata(st,4) )
  5984. sd = map_id2sd(script_getnum(st,4)); // <Account ID>
  5985. else
  5986. sd = script_rid2sd(st); // Attached player
  5987. if( sd == NULL ) // no target
  5988. return SCRIPT_CMD_SUCCESS;
  5989. //Check if it's stackable.
  5990. if (!itemdb_isstackable2(id))
  5991. get_count = 1;
  5992. else
  5993. get_count = amount;
  5994. for (i = 0; i < amount; i += get_count)
  5995. {
  5996. // if not pet egg
  5997. if (!pet_create_egg(sd, nameid))
  5998. {
  5999. if ((flag = pc_additem(sd, &it, get_count, LOG_TYPE_SCRIPT)))
  6000. {
  6001. clif_additem(sd, 0, 0, flag);
  6002. if( pc_candrop(sd,&it) )
  6003. map_addflooritem(&it,get_count,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0,0);
  6004. }
  6005. }
  6006. }
  6007. return SCRIPT_CMD_SUCCESS;
  6008. }
  6009. /*==========================================
  6010. * getitem2 <item id>,<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>{,<account ID>};
  6011. * getitem2 "<item name>",<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>{,<account ID>};
  6012. *
  6013. * getitembound2 <item id>,<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>,<bound type>{,<account ID>};
  6014. * getitembound2 "<item name>",<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>,<bound type>{,<account ID>};
  6015. * Type:
  6016. * 0 - No bound
  6017. * 1 - Account Bound
  6018. * 2 - Guild Bound
  6019. * 3 - Party Bound
  6020. * 4 - Character Bound
  6021. *------------------------------------------*/
  6022. BUILDIN_FUNC(getitem2)
  6023. {
  6024. int amount;
  6025. unsigned short nameid;
  6026. int iden, ref, attr;
  6027. unsigned short c1, c2, c3, c4;
  6028. char bound = BOUND_NONE;
  6029. struct item_data *item_data = NULL;
  6030. struct item item_tmp;
  6031. TBL_PC *sd;
  6032. struct script_data *data;
  6033. const char* command = script_getfuncname(st);
  6034. if( !strcmp(command,"getitembound2") ) {
  6035. bound = script_getnum(st,11);
  6036. if( bound < BOUND_NONE || bound >= BOUND_MAX ) {
  6037. ShowError("script_getitembound2: Not a correct bound type! Type=%d\n",bound);
  6038. return SCRIPT_CMD_FAILURE;
  6039. }
  6040. if( script_hasdata(st,12) )
  6041. sd = map_id2sd(script_getnum(st,12));
  6042. else
  6043. sd = script_rid2sd(st); // Attached player
  6044. } else if( script_hasdata(st,11) )
  6045. sd = map_id2sd(script_getnum(st,11)); // <Account ID>
  6046. else
  6047. sd = script_rid2sd(st); // Attached player
  6048. if( sd == NULL ) // no target
  6049. return SCRIPT_CMD_SUCCESS;
  6050. data = script_getdata(st,2);
  6051. get_val(st,data);
  6052. if( data_isstring(data) ) {
  6053. const char *name = conv_str(st,data);
  6054. if( (item_data = itemdb_searchname(name)) == NULL ){
  6055. ShowError("buildin_getitem2: Nonexistant item %s requested (by conv_str).\n", name);
  6056. return SCRIPT_CMD_FAILURE; //No item created.
  6057. }
  6058. nameid = item_data->nameid;
  6059. } else {
  6060. nameid = conv_num(st,data);
  6061. if( (item_data = itemdb_exists(nameid)) == NULL ){
  6062. ShowError("buildin_getitem2: Nonexistant item %d requested (by conv_num).\n", nameid);
  6063. return SCRIPT_CMD_FAILURE; //No item created.
  6064. }
  6065. }
  6066. amount = script_getnum(st,3);
  6067. iden = script_getnum(st,4);
  6068. ref = script_getnum(st,5);
  6069. attr = script_getnum(st,6);
  6070. c1 = (unsigned short)script_getnum(st,7);
  6071. c2 = (unsigned short)script_getnum(st,8);
  6072. c3 = (unsigned short)script_getnum(st,9);
  6073. c4 = (unsigned short)script_getnum(st,10);
  6074. if( item_data ) {
  6075. int get_count = 0, i;
  6076. memset(&item_tmp,0,sizeof(item_tmp));
  6077. if( item_data->type == IT_WEAPON || item_data->type == IT_ARMOR || item_data->type == IT_SHADOWGEAR ) {
  6078. if(ref > MAX_REFINE)
  6079. ref = MAX_REFINE;
  6080. }
  6081. else if( item_data->type == IT_PETEGG ) {
  6082. iden = 1;
  6083. ref = 0;
  6084. }
  6085. else {
  6086. iden = 1;
  6087. ref = attr = 0;
  6088. }
  6089. item_tmp.nameid = nameid;
  6090. item_tmp.identify = iden;
  6091. item_tmp.refine = ref;
  6092. item_tmp.attribute = attr;
  6093. item_tmp.card[0] = c1;
  6094. item_tmp.card[1] = c2;
  6095. item_tmp.card[2] = c3;
  6096. item_tmp.card[3] = c4;
  6097. item_tmp.bound = bound;
  6098. //Check if it's stackable.
  6099. if (!itemdb_isstackable2(item_data))
  6100. get_count = 1;
  6101. else
  6102. get_count = amount;
  6103. for (i = 0; i < amount; i += get_count)
  6104. {
  6105. // if not pet egg
  6106. if (!pet_create_egg(sd, nameid))
  6107. {
  6108. unsigned char flag = 0;
  6109. if ((flag = pc_additem(sd, &item_tmp, get_count, LOG_TYPE_SCRIPT)))
  6110. {
  6111. clif_additem(sd, 0, 0, flag);
  6112. if( pc_candrop(sd,&item_tmp) )
  6113. map_addflooritem(&item_tmp,get_count,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0,0);
  6114. }
  6115. }
  6116. }
  6117. }
  6118. return SCRIPT_CMD_SUCCESS;
  6119. }
  6120. /** Gives rental item to player
  6121. * rentitem <item id>,<seconds>{,<account_id>}
  6122. * rentitem "<item name>",<seconds>{,<account_id>}
  6123. */
  6124. BUILDIN_FUNC(rentitem) {
  6125. struct map_session_data *sd;
  6126. struct script_data *data;
  6127. struct item it;
  6128. int seconds;
  6129. unsigned short nameid = 0;
  6130. unsigned char flag = 0;
  6131. data = script_getdata(st,2);
  6132. get_val(st,data);
  6133. if (!script_accid2sd(4,sd))
  6134. return SCRIPT_CMD_FAILURE;
  6135. if( data_isstring(data) )
  6136. {
  6137. const char *name = conv_str(st,data);
  6138. struct item_data *itd = itemdb_searchname(name);
  6139. if( itd == NULL )
  6140. {
  6141. ShowError("buildin_rentitem: Nonexistant item %s requested.\n", name);
  6142. return SCRIPT_CMD_FAILURE;
  6143. }
  6144. nameid = itd->nameid;
  6145. }
  6146. else if( data_isint(data) )
  6147. {
  6148. nameid = conv_num(st,data);
  6149. if( nameid == 0 || !itemdb_exists(nameid) )
  6150. {
  6151. ShowError("buildin_rentitem: Nonexistant item %hu requested.\n", nameid);
  6152. return SCRIPT_CMD_FAILURE;
  6153. }
  6154. }
  6155. else
  6156. {
  6157. ShowError("buildin_rentitem: invalid data type for argument #1 (%d).\n", data->type);
  6158. return SCRIPT_CMD_FAILURE;
  6159. }
  6160. seconds = script_getnum(st,3);
  6161. memset(&it, 0, sizeof(it));
  6162. it.nameid = nameid;
  6163. it.identify = 1;
  6164. it.expire_time = (unsigned int)(time(NULL) + seconds);
  6165. it.bound = BOUND_NONE;
  6166. if( (flag = pc_additem(sd, &it, 1, LOG_TYPE_SCRIPT)) )
  6167. {
  6168. clif_additem(sd, 0, 0, flag);
  6169. return SCRIPT_CMD_FAILURE;
  6170. }
  6171. return SCRIPT_CMD_SUCCESS;
  6172. }
  6173. /** Gives rental item to player with advanced option
  6174. * rentitem2 <item id>,<time>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>{,<account_id>};
  6175. * rentitem2 "<item name>",<time>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>{,<account_id>};
  6176. */
  6177. BUILDIN_FUNC(rentitem2) {
  6178. struct map_session_data *sd;
  6179. struct script_data *data;
  6180. struct item it;
  6181. struct item_data *id;
  6182. int seconds;
  6183. unsigned short nameid = 0;
  6184. unsigned char flag = 0;
  6185. int iden,ref,attr,c1,c2,c3,c4;
  6186. data = script_getdata(st,2);
  6187. get_val(st,data);
  6188. if (!script_accid2sd(11,sd))
  6189. return SCRIPT_CMD_FAILURE;
  6190. if( data_isstring(data) ) {
  6191. const char *name = conv_str(st,data);
  6192. id = itemdb_searchname(name);
  6193. if( id == NULL ) {
  6194. ShowError("buildin_rentitem2: Nonexistant item %s requested.\n", name);
  6195. return SCRIPT_CMD_FAILURE;
  6196. }
  6197. nameid = id->nameid;
  6198. }
  6199. else if( data_isint(data) ) {
  6200. nameid = conv_num(st,data);
  6201. if( !(id = itemdb_search(nameid))) {
  6202. ShowError("buildin_rentitem2: Nonexistant item %hu requested.\n", nameid);
  6203. return SCRIPT_CMD_FAILURE;
  6204. }
  6205. }
  6206. else {
  6207. ShowError("buildin_rentitem2: invalid data type for argument #1 (%d).\n", data->type);
  6208. return SCRIPT_CMD_FAILURE;
  6209. }
  6210. seconds = script_getnum(st,3);
  6211. iden = script_getnum(st,4);
  6212. ref = script_getnum(st,5);
  6213. attr = script_getnum(st,6);
  6214. if (id->type==IT_WEAPON || id->type==IT_ARMOR || id->type==IT_SHADOWGEAR) {
  6215. if(ref > MAX_REFINE) ref = MAX_REFINE;
  6216. }
  6217. else if (id->type==IT_PETEGG) {
  6218. iden = 1;
  6219. ref = 0;
  6220. }
  6221. else {
  6222. iden = 1;
  6223. ref = attr = 0;
  6224. }
  6225. c1 = (short)script_getnum(st,7);
  6226. c2 = (short)script_getnum(st,8);
  6227. c3 = (short)script_getnum(st,9);
  6228. c4 = (short)script_getnum(st,10);
  6229. memset(&it, 0, sizeof(it));
  6230. it.nameid = nameid;
  6231. it.identify = iden;
  6232. it.refine = ref;
  6233. it.attribute = attr;
  6234. it.card[0] = (short)c1;
  6235. it.card[1] = (short)c2;
  6236. it.card[2] = (short)c3;
  6237. it.card[3] = (short)c4;
  6238. it.expire_time = (unsigned int)(time(NULL) + seconds);
  6239. if( (flag = pc_additem(sd, &it, 1, LOG_TYPE_SCRIPT)) ) {
  6240. clif_additem(sd, 0, 0, flag);
  6241. return SCRIPT_CMD_FAILURE;
  6242. }
  6243. return SCRIPT_CMD_SUCCESS;
  6244. }
  6245. /*==========================================
  6246. * gets an item with someone's name inscribed [Skotlex]
  6247. * getinscribeditem item_num, character_name
  6248. * Returned Qty is always 1, only works on equip-able
  6249. * equipment
  6250. *------------------------------------------*/
  6251. BUILDIN_FUNC(getnameditem)
  6252. {
  6253. unsigned short nameid;
  6254. struct item item_tmp;
  6255. TBL_PC *sd, *tsd;
  6256. struct script_data *data;
  6257. sd = script_rid2sd(st);
  6258. if (sd == NULL)
  6259. { //Player not attached!
  6260. script_pushint(st,0);
  6261. return SCRIPT_CMD_SUCCESS;
  6262. }
  6263. data=script_getdata(st,2);
  6264. get_val(st,data);
  6265. if( data_isstring(data) ){
  6266. const char *name=conv_str(st,data);
  6267. struct item_data *item_data = itemdb_searchname(name);
  6268. if( item_data == NULL)
  6269. { //Failed
  6270. script_pushint(st,0);
  6271. return SCRIPT_CMD_SUCCESS;
  6272. }
  6273. nameid = item_data->nameid;
  6274. }else
  6275. nameid = conv_num(st,data);
  6276. if(!itemdb_exists(nameid)/* || itemdb_isstackable(nameid)*/)
  6277. { //Even though named stackable items "could" be risky, they are required for certain quests.
  6278. script_pushint(st,0);
  6279. return SCRIPT_CMD_SUCCESS;
  6280. }
  6281. data=script_getdata(st,3);
  6282. get_val(st,data);
  6283. if( data_isstring(data) ) //Char Name
  6284. tsd=map_nick2sd(conv_str(st,data));
  6285. else //Char Id was given
  6286. tsd=map_charid2sd(conv_num(st,data));
  6287. if( tsd == NULL )
  6288. { //Failed
  6289. script_pushint(st,0);
  6290. return SCRIPT_CMD_SUCCESS;
  6291. }
  6292. memset(&item_tmp,0,sizeof(item_tmp));
  6293. item_tmp.nameid=nameid;
  6294. item_tmp.amount=1;
  6295. item_tmp.identify=1;
  6296. item_tmp.card[0]=CARD0_CREATE; //we don't use 255! because for example SIGNED WEAPON shouldn't get TOP10 BS Fame bonus [Lupus]
  6297. item_tmp.card[2]=tsd->status.char_id;
  6298. item_tmp.card[3]=tsd->status.char_id >> 16;
  6299. if(pc_additem(sd,&item_tmp,1,LOG_TYPE_SCRIPT)) {
  6300. script_pushint(st,0);
  6301. return SCRIPT_CMD_SUCCESS; //Failed to add item, we will not drop if they don't fit
  6302. }
  6303. script_pushint(st,1);
  6304. return SCRIPT_CMD_SUCCESS;
  6305. }
  6306. /*==========================================
  6307. * gets a random item ID from an item group [Skotlex]
  6308. * groupranditem <group_num>{,<sub_group>};
  6309. *------------------------------------------*/
  6310. BUILDIN_FUNC(grouprandomitem) {
  6311. struct s_item_group_entry *entry = NULL;
  6312. int sub_group = 1;
  6313. FETCH(3, sub_group);
  6314. entry = itemdb_get_randgroupitem(script_getnum(st,2),sub_group);
  6315. if (!entry) {
  6316. ShowError("buildin_grouprandomitem: Invalid item group with group_id '%d', sub_group '%d'.\n", script_getnum(st,2), sub_group);
  6317. script_pushint(st,UNKNOWN_ITEM_ID);
  6318. return SCRIPT_CMD_FAILURE;
  6319. }
  6320. script_pushint(st,entry->nameid);
  6321. return SCRIPT_CMD_SUCCESS;
  6322. }
  6323. /**
  6324. * makeitem <item id>,<amount>,"<map name>",<X>,<Y>;
  6325. * makeitem "<item name>",<amount>,"<map name>",<X>,<Y>;
  6326. */
  6327. BUILDIN_FUNC(makeitem) {
  6328. int16 nameid;
  6329. uint16 amount, flag = 0, x, y;
  6330. const char *mapname;
  6331. int m;
  6332. struct item item_tmp;
  6333. struct script_data *data;
  6334. data = script_getdata(st,2);
  6335. get_val(st,data);
  6336. if( data_isstring(data) ){
  6337. const char *name = conv_str(st,data);
  6338. struct item_data *item_data = itemdb_searchname(name);
  6339. if( item_data )
  6340. nameid = item_data->nameid;
  6341. else
  6342. nameid = UNKNOWN_ITEM_ID;
  6343. }
  6344. else
  6345. nameid = conv_num(st,data);
  6346. amount = script_getnum(st,3);
  6347. mapname = script_getstr(st,4);
  6348. x = script_getnum(st,5);
  6349. y = script_getnum(st,6);
  6350. if(strcmp(mapname,"this")==0) {
  6351. TBL_PC *sd;
  6352. sd = script_rid2sd(st);
  6353. if (!sd)
  6354. return SCRIPT_CMD_SUCCESS; //Failed...
  6355. m = sd->bl.m;
  6356. } else
  6357. m = map_mapname2mapid(mapname);
  6358. if(nameid<0) {
  6359. nameid = -nameid;
  6360. flag = 1;
  6361. }
  6362. if(nameid > 0) {
  6363. memset(&item_tmp,0,sizeof(item_tmp));
  6364. item_tmp.nameid = nameid;
  6365. if(!flag)
  6366. item_tmp.identify = 1;
  6367. else
  6368. item_tmp.identify = itemdb_isidentified(nameid);
  6369. map_addflooritem(&item_tmp,amount,m,x,y,0,0,0,4,0);
  6370. }
  6371. return SCRIPT_CMD_SUCCESS;
  6372. }
  6373. /**
  6374. * makeitem2 <item id>,<amount>,"<map name>",<X>,<Y>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>;
  6375. * makeitem2 "<item name>",<amount>,"<map name>",<X>,<Y>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>;
  6376. */
  6377. BUILDIN_FUNC(makeitem2) {
  6378. uint16 nameid, amount, x, y;
  6379. const char *mapname;
  6380. int m;
  6381. struct item item_tmp;
  6382. struct script_data *data;
  6383. struct item_data *id;
  6384. data = script_getdata(st,2);
  6385. get_val(st,data);
  6386. if( data_isstring(data) ){
  6387. const char *name = conv_str(st,data);
  6388. struct item_data *item_data = itemdb_searchname(name);
  6389. if( item_data )
  6390. nameid = item_data->nameid;
  6391. else
  6392. nameid = UNKNOWN_ITEM_ID;
  6393. }
  6394. else
  6395. nameid = conv_num(st,data);
  6396. amount = script_getnum(st,3);
  6397. mapname = script_getstr(st,4);
  6398. x = script_getnum(st,5);
  6399. y = script_getnum(st,6);
  6400. if (strcmp(mapname,"this")==0) {
  6401. TBL_PC *sd;
  6402. sd = script_rid2sd(st);
  6403. if (!sd)
  6404. return SCRIPT_CMD_SUCCESS; //Failed...
  6405. m = sd->bl.m;
  6406. }
  6407. else
  6408. m = map_mapname2mapid(mapname);
  6409. if ((id = itemdb_search(nameid))) {
  6410. char iden, ref, attr;
  6411. memset(&item_tmp,0,sizeof(item_tmp));
  6412. item_tmp.nameid = nameid;
  6413. iden = (char)script_getnum(st,7);
  6414. ref = (char)script_getnum(st,8);
  6415. attr = (char)script_getnum(st,9);
  6416. if (id->type==IT_WEAPON || id->type==IT_ARMOR || id->type==IT_SHADOWGEAR) {
  6417. if(ref > MAX_REFINE) ref = MAX_REFINE;
  6418. }
  6419. else if (id->type==IT_PETEGG) {
  6420. iden = 1;
  6421. ref = 0;
  6422. }
  6423. else {
  6424. iden = 1;
  6425. ref = attr = 0;
  6426. }
  6427. item_tmp.identify = iden;
  6428. item_tmp.refine = ref;
  6429. item_tmp.attribute = attr;
  6430. item_tmp.card[0] = script_getnum(st,10);
  6431. item_tmp.card[1] = script_getnum(st,11);
  6432. item_tmp.card[2] = script_getnum(st,12);
  6433. item_tmp.card[3] = script_getnum(st,13);
  6434. map_addflooritem(&item_tmp,amount,m,x,y,0,0,0,4,0);
  6435. }
  6436. else
  6437. return SCRIPT_CMD_FAILURE;
  6438. return SCRIPT_CMD_SUCCESS;
  6439. }
  6440. /// Counts / deletes the current item given by idx.
  6441. /// Used by buildin_delitem_search
  6442. /// Relies on all input data being already fully valid.
  6443. static void buildin_delitem_delete(struct map_session_data* sd, int idx, int* amount, uint8 loc, bool delete_items)
  6444. {
  6445. int delamount;
  6446. struct item *itm = NULL;
  6447. switch(loc) {
  6448. case 1: // cart
  6449. itm = &sd->status.cart[idx];
  6450. break;
  6451. case 2: // storage
  6452. itm = &sd->status.storage.items[idx];
  6453. break;
  6454. default: //inventory
  6455. itm = &sd->status.inventory[idx];
  6456. break;
  6457. }
  6458. delamount = ( amount[0] < itm->amount ) ? amount[0] : itm->amount;
  6459. if( delete_items )
  6460. {
  6461. if( itemdb_type(itm->nameid) == IT_PETEGG && itm->card[0] == CARD0_PET )
  6462. {// delete associated pet
  6463. intif_delete_petdata(MakeDWord(itm->card[1], itm->card[2]));
  6464. }
  6465. switch(loc) {
  6466. case 1:
  6467. pc_cart_delitem(sd,idx,delamount,0,LOG_TYPE_SCRIPT);
  6468. break;
  6469. case 2:
  6470. storage_delitem(sd,idx,delamount);
  6471. log_pick_pc(sd,LOG_TYPE_SCRIPT,-delamount,itm);
  6472. break;
  6473. default:
  6474. pc_delitem(sd, idx, delamount, 0, 0, LOG_TYPE_SCRIPT);
  6475. break;
  6476. }
  6477. }
  6478. amount[0]-= delamount;
  6479. }
  6480. /// Searches for item(s) and checks, if there is enough of them.
  6481. /// Used by delitem and delitem2
  6482. /// Relies on all input data being already fully valid.
  6483. /// @param exact_match will also match item attributes and cards, not just name id
  6484. /// @return true when all items could be deleted, false when there were not enough items to delete
  6485. static bool buildin_delitem_search(struct map_session_data* sd, struct item* it, bool exact_match, uint8 loc)
  6486. {
  6487. bool delete_items = false;
  6488. int i, amount, size;
  6489. struct item *items;
  6490. // prefer always non-equipped items
  6491. it->equip = 0;
  6492. // when searching for nameid only, prefer additionally
  6493. if( !exact_match )
  6494. {
  6495. // non-refined items
  6496. it->refine = 0;
  6497. // card-less items
  6498. memset(it->card, 0, sizeof(it->card));
  6499. }
  6500. switch(loc) {
  6501. case 1: // cart
  6502. size = MAX_CART;
  6503. items = sd->status.cart;
  6504. break;
  6505. case 2: // storage
  6506. size = MAX_STORAGE;
  6507. items = sd->status.storage.items;
  6508. break;
  6509. default: //inventory
  6510. size = MAX_INVENTORY;
  6511. items = sd->status.inventory;
  6512. break;
  6513. }
  6514. for(;;)
  6515. {
  6516. unsigned short important = 0;
  6517. amount = it->amount;
  6518. // 1st pass -- less important items / exact match
  6519. for( i = 0; amount && i < size; i++ )
  6520. {
  6521. struct item *itm = NULL;
  6522. if( !&items[i] || !(itm = &items[i])->nameid || itm->nameid != it->nameid )
  6523. {// wrong/invalid item
  6524. continue;
  6525. }
  6526. if( itm->equip != it->equip || itm->refine != it->refine )
  6527. {// not matching attributes
  6528. important++;
  6529. continue;
  6530. }
  6531. if( exact_match )
  6532. {
  6533. if( itm->identify != it->identify || itm->attribute != it->attribute || memcmp(itm->card, it->card, sizeof(itm->card)) )
  6534. {// not matching exact attributes
  6535. continue;
  6536. }
  6537. }
  6538. else
  6539. {
  6540. if( itemdb_type(itm->nameid) == IT_PETEGG )
  6541. {
  6542. if( itm->card[0] == CARD0_PET && CheckForCharServer() )
  6543. {// pet which cannot be deleted
  6544. continue;
  6545. }
  6546. }
  6547. else if( memcmp(itm->card, it->card, sizeof(itm->card)) )
  6548. {// named/carded item
  6549. important++;
  6550. continue;
  6551. }
  6552. }
  6553. // count / delete item
  6554. buildin_delitem_delete(sd, i, &amount, loc, delete_items);
  6555. }
  6556. // 2nd pass -- any matching item
  6557. if( amount == 0 || important == 0 )
  6558. {// either everything was already consumed or no items were skipped
  6559. ;
  6560. }
  6561. else for( i = 0; amount && i < size; i++ )
  6562. {
  6563. struct item *itm = NULL;
  6564. if( !&items[i] || !(itm = &items[i])->nameid || itm->nameid != it->nameid )
  6565. {// wrong/invalid item
  6566. continue;
  6567. }
  6568. if( itemdb_type(itm->nameid) == IT_PETEGG && itm->card[0] == CARD0_PET && CheckForCharServer() )
  6569. {// pet which cannot be deleted
  6570. continue;
  6571. }
  6572. if( exact_match )
  6573. {
  6574. if( itm->refine != it->refine || itm->identify != it->identify || itm->attribute != it->attribute || memcmp(itm->card, it->card, sizeof(itm->card)) )
  6575. {// not matching attributes
  6576. continue;
  6577. }
  6578. }
  6579. // count / delete item
  6580. buildin_delitem_delete(sd, i, &amount, loc, delete_items);
  6581. }
  6582. if( amount )
  6583. {// not enough items
  6584. return false;
  6585. }
  6586. else if( delete_items )
  6587. {// we are done with the work
  6588. return true;
  6589. }
  6590. else
  6591. {// get rid of the items now
  6592. delete_items = true;
  6593. }
  6594. }
  6595. }
  6596. /// Deletes items from the target/attached player.
  6597. /// Prioritizes ordinary items.
  6598. ///
  6599. /// delitem <item id>,<amount>{,<account id>}
  6600. /// delitem "<item name>",<amount>{,<account id>}
  6601. /// cartdelitem <item id>,<amount>{,<account id>}
  6602. /// cartdelitem "<item name>",<amount>{,<account id>}
  6603. /// storagedelitem <item id>,<amount>{,<account id>}
  6604. /// storagedelitem "<item name>",<amount>{,<account id>}
  6605. BUILDIN_FUNC(delitem)
  6606. {
  6607. TBL_PC *sd;
  6608. struct item it;
  6609. struct script_data *data;
  6610. uint8 loc = 0;
  6611. char* command = (char*)script_getfuncname(st);
  6612. if(!strncmp(command, "cart", 4))
  6613. loc = 1;
  6614. else if(!strncmp(command, "storage", 7))
  6615. loc = 2;
  6616. //TODO: 3 - Guild Storage
  6617. if( script_hasdata(st,4) )
  6618. {
  6619. uint32 account_id = script_getnum(st,4);
  6620. sd = map_id2sd(account_id); // <account id>
  6621. if( sd == NULL )
  6622. {
  6623. ShowError("buildin_%s: player not found (AID=%d).\n", command, account_id);
  6624. st->state = END;
  6625. return SCRIPT_CMD_FAILURE;
  6626. }
  6627. }
  6628. else
  6629. {
  6630. sd = script_rid2sd(st);// attached player
  6631. if( sd == NULL )
  6632. return SCRIPT_CMD_SUCCESS;
  6633. }
  6634. if (loc == 1 && !pc_iscarton(sd)) {
  6635. ShowError("buildin_cartdelitem: player doesn't have cart (CID=%d).\n", sd->status.char_id);
  6636. return SCRIPT_CMD_FAILURE;
  6637. }
  6638. data = script_getdata(st,2);
  6639. get_val(st,data);
  6640. if( data_isstring(data) )
  6641. {
  6642. const char* item_name = conv_str(st,data);
  6643. struct item_data* id = itemdb_searchname(item_name);
  6644. if( id == NULL )
  6645. {
  6646. ShowError("buildin_%s: unknown item \"%s\".\n", command, item_name);
  6647. st->state = END;
  6648. return SCRIPT_CMD_FAILURE;
  6649. }
  6650. it.nameid = id->nameid;// "<item name>"
  6651. }
  6652. else
  6653. {
  6654. it.nameid = conv_num(st,data);// <item id>
  6655. if( !itemdb_exists( it.nameid ) )
  6656. {
  6657. ShowError("buildin_%s: unknown item \"%hu\".\n", command, it.nameid);
  6658. st->state = END;
  6659. return SCRIPT_CMD_FAILURE;
  6660. }
  6661. }
  6662. it.amount=script_getnum(st,3);
  6663. if( it.amount <= 0 )
  6664. return SCRIPT_CMD_SUCCESS;// nothing to do
  6665. if( buildin_delitem_search(sd, &it, false, loc) )
  6666. {// success
  6667. return SCRIPT_CMD_SUCCESS;
  6668. }
  6669. ShowError("buildin_%s: failed to delete %d items (AID=%d item_id=%hu).\n", command, it.amount, sd->status.account_id, it.nameid);
  6670. st->state = END;
  6671. st->mes_active = 0;
  6672. clif_scriptclose(sd, st->oid);
  6673. return SCRIPT_CMD_FAILURE;
  6674. }
  6675. /// Deletes items from the target/attached player.
  6676. ///
  6677. /// delitem2 <item id>,<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>{,<account ID>}
  6678. /// delitem2 "<Item name>",<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>{,<account ID>}
  6679. /// cartdelitem2 <item id>,<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>{,<account ID>}
  6680. /// cartdelitem2 "<Item name>",<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>{,<account ID>}
  6681. /// storagedelitem2 <item id>,<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>{,<account ID>}
  6682. /// storagedelitem2 "<Item name>",<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>{,<account ID>}
  6683. BUILDIN_FUNC(delitem2)
  6684. {
  6685. TBL_PC *sd;
  6686. struct item it;
  6687. struct script_data *data;
  6688. uint8 loc = 0;
  6689. char* command = (char*)script_getfuncname(st);
  6690. if(!strncmp(command, "cart", 4))
  6691. loc = 1;
  6692. else if(!strncmp(command, "storage", 7))
  6693. loc = 2;
  6694. //TODO: 3 - Guild Storage
  6695. if( script_hasdata(st,11) )
  6696. {
  6697. uint32 account_id = script_getnum(st,11);
  6698. sd = map_id2sd(account_id); // <account id>
  6699. if( sd == NULL )
  6700. {
  6701. ShowError("buildin_%s: player not found (AID=%d).\n", command, account_id);
  6702. st->state = END;
  6703. return SCRIPT_CMD_FAILURE;
  6704. }
  6705. }
  6706. else
  6707. {
  6708. sd = script_rid2sd(st);// attached player
  6709. if( sd == NULL )
  6710. return SCRIPT_CMD_SUCCESS;
  6711. }
  6712. if (loc == 1 && !pc_iscarton(sd)) {
  6713. ShowError("buildin_cartdelitem: player doesn't have cart (CID=%d).\n", sd->status.char_id);
  6714. script_pushint(st,-1);
  6715. return SCRIPT_CMD_FAILURE;
  6716. }
  6717. data = script_getdata(st,2);
  6718. get_val(st,data);
  6719. if( data_isstring(data) )
  6720. {
  6721. const char* item_name = conv_str(st,data);
  6722. struct item_data* id = itemdb_searchname(item_name);
  6723. if( id == NULL )
  6724. {
  6725. ShowError("buildin_%s: unknown item \"%s\".\n", command, item_name);
  6726. st->state = END;
  6727. return SCRIPT_CMD_FAILURE;
  6728. }
  6729. it.nameid = id->nameid;// "<item name>"
  6730. }
  6731. else
  6732. {
  6733. it.nameid = conv_num(st,data);// <item id>
  6734. if( !itemdb_exists( it.nameid ) )
  6735. {
  6736. ShowError("buildin_%s: unknown item \"%hu\".\n", command, it.nameid);
  6737. st->state = END;
  6738. return SCRIPT_CMD_FAILURE;
  6739. }
  6740. }
  6741. it.amount=script_getnum(st,3);
  6742. it.identify=script_getnum(st,4);
  6743. it.refine=script_getnum(st,5);
  6744. it.attribute=script_getnum(st,6);
  6745. it.card[0]=(short)script_getnum(st,7);
  6746. it.card[1]=(short)script_getnum(st,8);
  6747. it.card[2]=(short)script_getnum(st,9);
  6748. it.card[3]=(short)script_getnum(st,10);
  6749. if( it.amount <= 0 )
  6750. return SCRIPT_CMD_SUCCESS;// nothing to do
  6751. if( buildin_delitem_search(sd, &it, true, loc) )
  6752. {// success
  6753. return SCRIPT_CMD_SUCCESS;
  6754. }
  6755. ShowError("buildin_%s: failed to delete %d items (AID=%d item_id=%hu).\n", command, it.amount, sd->status.account_id, it.nameid);
  6756. st->state = END;
  6757. st->mes_active = 0;
  6758. clif_scriptclose(sd, st->oid);
  6759. return SCRIPT_CMD_FAILURE;
  6760. }
  6761. /*==========================================
  6762. * Enables/Disables use of items while in an NPC [Skotlex]
  6763. *------------------------------------------*/
  6764. BUILDIN_FUNC(enableitemuse)
  6765. {
  6766. TBL_PC *sd;
  6767. sd=script_rid2sd(st);
  6768. if (sd)
  6769. st->npc_item_flag = sd->npc_item_flag = 1;
  6770. return SCRIPT_CMD_SUCCESS;
  6771. }
  6772. BUILDIN_FUNC(disableitemuse)
  6773. {
  6774. TBL_PC *sd;
  6775. sd=script_rid2sd(st);
  6776. if (sd)
  6777. st->npc_item_flag = sd->npc_item_flag = 0;
  6778. return SCRIPT_CMD_SUCCESS;
  6779. }
  6780. /*==========================================
  6781. * Returns a character's specified stat.
  6782. * Check pc_readparam for available options.
  6783. * readparam <param>{,"<nick>"}
  6784. *------------------------------------------*/
  6785. BUILDIN_FUNC(readparam)
  6786. {
  6787. int type;
  6788. struct script_data *data = script_getdata(st, 2);
  6789. TBL_PC *sd;
  6790. if (!script_nick2sd(3,sd)) {
  6791. script_pushint(st,-1);
  6792. return SCRIPT_CMD_FAILURE;
  6793. }
  6794. if (data->type == C_NAME) { // If using constant name, just get the val
  6795. get_val_(st, data, sd);
  6796. script_pushint(st, (int)data->u.num);
  6797. return SCRIPT_CMD_SUCCESS;
  6798. }
  6799. type = script_getnum(st, 2);
  6800. script_pushint(st,pc_readparam(sd,type));
  6801. return SCRIPT_CMD_SUCCESS;
  6802. }
  6803. /*==========================================
  6804. * Return charid identification
  6805. * return by @num :
  6806. * 0 : char_id
  6807. * 1 : party_id
  6808. * 2 : guild_id
  6809. * 3 : account_id
  6810. * 4 : bg_id
  6811. *------------------------------------------*/
  6812. BUILDIN_FUNC(getcharid)
  6813. {
  6814. int num;
  6815. TBL_PC *sd;
  6816. num = script_getnum(st,2);
  6817. if( script_hasdata(st,3) )
  6818. sd=map_nick2sd(script_getstr(st,3));
  6819. else
  6820. sd=script_rid2sd(st);
  6821. if(sd==NULL){
  6822. script_pushint(st,0); //return 0, according docs
  6823. return SCRIPT_CMD_SUCCESS;
  6824. }
  6825. switch( num ) {
  6826. case 0: script_pushint(st,sd->status.char_id); break;
  6827. case 1: script_pushint(st,sd->status.party_id); break;
  6828. case 2: script_pushint(st,sd->status.guild_id); break;
  6829. case 3: script_pushint(st,sd->status.account_id); break;
  6830. case 4: script_pushint(st,sd->bg_id); break;
  6831. default:
  6832. ShowError("buildin_getcharid: invalid parameter (%d).\n", num);
  6833. script_pushint(st,0);
  6834. break;
  6835. }
  6836. return SCRIPT_CMD_SUCCESS;
  6837. }
  6838. /*==========================================
  6839. * returns the GID of an NPC
  6840. *------------------------------------------*/
  6841. BUILDIN_FUNC(getnpcid)
  6842. {
  6843. int num = script_getnum(st,2);
  6844. struct npc_data* nd = NULL;
  6845. if( script_hasdata(st,3) )
  6846. {// unique npc name
  6847. if( ( nd = npc_name2id(script_getstr(st,3)) ) == NULL )
  6848. {
  6849. ShowError("buildin_getnpcid: No such NPC '%s'.\n", script_getstr(st,3));
  6850. script_pushint(st,0);
  6851. return SCRIPT_CMD_FAILURE;
  6852. }
  6853. }
  6854. switch (num) {
  6855. case 0:
  6856. script_pushint(st,nd ? nd->bl.id : st->oid);
  6857. break;
  6858. default:
  6859. ShowError("buildin_getnpcid: invalid parameter (%d).\n", num);
  6860. script_pushint(st,0);
  6861. return SCRIPT_CMD_FAILURE;
  6862. }
  6863. return SCRIPT_CMD_SUCCESS;
  6864. }
  6865. /*==========================================
  6866. * Return the name of the party_id
  6867. * null if not found
  6868. *------------------------------------------*/
  6869. BUILDIN_FUNC(getpartyname)
  6870. {
  6871. int party_id;
  6872. struct party_data* p;
  6873. party_id = script_getnum(st,2);
  6874. if( ( p = party_search(party_id) ) != NULL )
  6875. {
  6876. script_pushstrcopy(st,p->party.name);
  6877. }
  6878. else
  6879. {
  6880. script_pushconststr(st,"null");
  6881. }
  6882. return SCRIPT_CMD_SUCCESS;
  6883. }
  6884. /*==========================================
  6885. * Get the information of the members of a party by type
  6886. * @party_id, @type
  6887. * return by @type :
  6888. * - : nom des membres
  6889. * 1 : char_id des membres
  6890. * 2 : account_id des membres
  6891. *------------------------------------------*/
  6892. BUILDIN_FUNC(getpartymember)
  6893. {
  6894. struct party_data *p;
  6895. uint8 j = 0;
  6896. p = party_search(script_getnum(st,2));
  6897. if (p != NULL) {
  6898. uint8 i, type = 0;
  6899. struct script_data *data = NULL;
  6900. char *varname = NULL;
  6901. if (script_hasdata(st,3))
  6902. type = script_getnum(st,3);
  6903. if (script_hasdata(st,4)) {
  6904. data = script_getdata(st, 4);
  6905. if (!data_isreference(data)) {
  6906. ShowError("buildin_getpartymember: Error in argument! Please give a variable to store values in.\n");
  6907. return SCRIPT_CMD_FAILURE;
  6908. }
  6909. varname = reference_getname(data);
  6910. if (type <= 0 && varname[strlen(varname)-1] != '$') {
  6911. ShowError("buildin_getpartymember: The array %s is not string type.\n", varname);
  6912. return SCRIPT_CMD_FAILURE;
  6913. }
  6914. }
  6915. for (i = 0; i < MAX_PARTY; i++) {
  6916. if (p->party.member[i].account_id) {
  6917. switch (type) {
  6918. case 2:
  6919. if (data)
  6920. setd_sub(st, NULL, varname, j, (void *)__64BPRTSIZE(p->party.member[i].account_id), data->ref);
  6921. else
  6922. mapreg_setreg(reference_uid(add_str("$@partymemberaid"), j),p->party.member[i].account_id);
  6923. break;
  6924. case 1:
  6925. if (data)
  6926. setd_sub(st, NULL, varname, j, (void *)__64BPRTSIZE(p->party.member[i].char_id), data->ref);
  6927. else
  6928. mapreg_setreg(reference_uid(add_str("$@partymembercid"), j),p->party.member[i].char_id);
  6929. break;
  6930. default:
  6931. if (data)
  6932. setd_sub(st, NULL, varname, j, (void *)__64BPRTSIZE(p->party.member[i].name), data->ref);
  6933. else
  6934. mapreg_setregstr(reference_uid(add_str("$@partymembername$"), j),p->party.member[i].name);
  6935. break;
  6936. }
  6937. j++;
  6938. }
  6939. }
  6940. }
  6941. mapreg_setreg(add_str("$@partymembercount"),j);
  6942. script_pushint(st, j);
  6943. return SCRIPT_CMD_SUCCESS;
  6944. }
  6945. /*==========================================
  6946. * Retrieves party leader. if flag is specified,
  6947. * return some of the leader data. Otherwise, return name.
  6948. *------------------------------------------*/
  6949. BUILDIN_FUNC(getpartyleader)
  6950. {
  6951. int party_id, type = 0, i=0;
  6952. struct party_data *p;
  6953. party_id=script_getnum(st,2);
  6954. if( script_hasdata(st,3) )
  6955. type=script_getnum(st,3);
  6956. p=party_search(party_id);
  6957. if (p) //Search leader
  6958. for(i = 0; i < MAX_PARTY && !p->party.member[i].leader; i++);
  6959. if (!p || i == MAX_PARTY) { //leader not found
  6960. if (type)
  6961. script_pushint(st,-1);
  6962. else
  6963. script_pushconststr(st,"null");
  6964. return SCRIPT_CMD_SUCCESS;
  6965. }
  6966. switch (type) {
  6967. case 1: script_pushint(st,p->party.member[i].account_id); break;
  6968. case 2: script_pushint(st,p->party.member[i].char_id); break;
  6969. case 3: script_pushint(st,p->party.member[i].class_); break;
  6970. case 4: script_pushstrcopy(st,mapindex_id2name(p->party.member[i].map)); break;
  6971. case 5: script_pushint(st,p->party.member[i].lv); break;
  6972. default: script_pushstrcopy(st,p->party.member[i].name); break;
  6973. }
  6974. return SCRIPT_CMD_SUCCESS;
  6975. }
  6976. /*==========================================
  6977. * Return the name of the @guild_id
  6978. * null if not found
  6979. *------------------------------------------*/
  6980. BUILDIN_FUNC(getguildname)
  6981. {
  6982. int guild_id;
  6983. struct guild* g;
  6984. guild_id = script_getnum(st,2);
  6985. if( ( g = guild_search(guild_id) ) != NULL )
  6986. script_pushstrcopy(st,g->name);
  6987. else
  6988. script_pushconststr(st,"null");
  6989. return SCRIPT_CMD_SUCCESS;
  6990. }
  6991. /*==========================================
  6992. * Return the name of the guild master of @guild_id
  6993. * null if not found
  6994. *------------------------------------------*/
  6995. BUILDIN_FUNC(getguildmaster)
  6996. {
  6997. int guild_id;
  6998. struct guild* g;
  6999. guild_id = script_getnum(st,2);
  7000. if( ( g = guild_search(guild_id) ) != NULL )
  7001. script_pushstrcopy(st,g->member[0].name);
  7002. else
  7003. script_pushconststr(st,"null");
  7004. return SCRIPT_CMD_SUCCESS;
  7005. }
  7006. BUILDIN_FUNC(getguildmasterid)
  7007. {
  7008. int guild_id;
  7009. struct guild* g;
  7010. guild_id = script_getnum(st,2);
  7011. if( ( g = guild_search(guild_id) ) != NULL )
  7012. script_pushint(st,g->member[0].char_id);
  7013. else
  7014. script_pushint(st,0);
  7015. return SCRIPT_CMD_SUCCESS;
  7016. }
  7017. /*==========================================
  7018. * Get char string information by type :
  7019. * Return by @type :
  7020. * 0 : char_name
  7021. * 1 : party_name or ""
  7022. * 2 : guild_name or ""
  7023. * 3 : map_name
  7024. * - : ""
  7025. * strcharinfo(<type>{,<char_id>})
  7026. *------------------------------------------*/
  7027. BUILDIN_FUNC(strcharinfo)
  7028. {
  7029. TBL_PC *sd;
  7030. int num;
  7031. struct guild* g;
  7032. struct party_data* p;
  7033. if (!script_charid2sd(3,sd)) {
  7034. script_pushconststr(st,"");
  7035. return SCRIPT_CMD_FAILURE;
  7036. }
  7037. num=script_getnum(st,2);
  7038. switch(num){
  7039. case 0:
  7040. script_pushstrcopy(st,sd->status.name);
  7041. break;
  7042. case 1:
  7043. if( ( p = party_search(sd->status.party_id) ) != NULL ) {
  7044. script_pushstrcopy(st,p->party.name);
  7045. } else {
  7046. script_pushconststr(st,"");
  7047. }
  7048. break;
  7049. case 2:
  7050. if( ( g = sd->guild ) != NULL ) {
  7051. script_pushstrcopy(st,g->name);
  7052. } else {
  7053. script_pushconststr(st,"");
  7054. }
  7055. break;
  7056. case 3:
  7057. script_pushconststr(st,map[sd->bl.m].name);
  7058. break;
  7059. default:
  7060. ShowWarning("buildin_strcharinfo: unknown parameter.\n");
  7061. script_pushconststr(st,"");
  7062. break;
  7063. }
  7064. return SCRIPT_CMD_SUCCESS;
  7065. }
  7066. /*==========================================
  7067. * Get npc string information by type
  7068. * return by @type:
  7069. * 0 : name
  7070. * 1 : str#
  7071. * 2 : #str
  7072. * 3 : ::str
  7073. * 4 : map name
  7074. *------------------------------------------*/
  7075. BUILDIN_FUNC(strnpcinfo)
  7076. {
  7077. TBL_NPC* nd;
  7078. int num;
  7079. char *buf,*name=NULL;
  7080. nd = map_id2nd(st->oid);
  7081. if (!nd) {
  7082. script_pushconststr(st, "");
  7083. return SCRIPT_CMD_SUCCESS;
  7084. }
  7085. num = script_getnum(st,2);
  7086. switch(num){
  7087. case 0: // display name
  7088. name = aStrdup(nd->name);
  7089. break;
  7090. case 1: // visible part of display name
  7091. if((buf = strchr(nd->name,'#')) != NULL)
  7092. {
  7093. name = aStrdup(nd->name);
  7094. name[buf - nd->name] = 0;
  7095. } else // Return the name, there is no '#' present
  7096. name = aStrdup(nd->name);
  7097. break;
  7098. case 2: // # fragment
  7099. if((buf = strchr(nd->name,'#')) != NULL)
  7100. name = aStrdup(buf+1);
  7101. break;
  7102. case 3: // unique name
  7103. name = aStrdup(nd->exname);
  7104. break;
  7105. case 4: // map name
  7106. name = aStrdup(map[nd->bl.m].name);
  7107. break;
  7108. }
  7109. if(name)
  7110. script_pushstr(st, name);
  7111. else
  7112. script_pushconststr(st, "");
  7113. return SCRIPT_CMD_SUCCESS;
  7114. }
  7115. /// aegis->athena slot position conversion table
  7116. /// Index is used by EQI_ in db/const.txt
  7117. static unsigned int equip[] = {
  7118. EQP_HEAD_TOP,
  7119. EQP_ARMOR,
  7120. EQP_HAND_L,
  7121. EQP_HAND_R,
  7122. EQP_GARMENT,
  7123. EQP_SHOES,
  7124. EQP_ACC_L,
  7125. EQP_ACC_R,
  7126. EQP_HEAD_MID,
  7127. EQP_HEAD_LOW,
  7128. EQP_COSTUME_HEAD_LOW,
  7129. EQP_COSTUME_HEAD_MID,
  7130. EQP_COSTUME_HEAD_TOP,
  7131. EQP_COSTUME_GARMENT,
  7132. EQP_AMMO,
  7133. EQP_SHADOW_ARMOR,
  7134. EQP_SHADOW_WEAPON,
  7135. EQP_SHADOW_SHIELD,
  7136. EQP_SHADOW_SHOES,
  7137. EQP_SHADOW_ACC_R,
  7138. EQP_SHADOW_ACC_L
  7139. };
  7140. /**
  7141. * getequipid(<equipment slot>{,<char_id>})
  7142. **/
  7143. BUILDIN_FUNC(getequipid)
  7144. {
  7145. int i, num;
  7146. TBL_PC* sd;
  7147. struct item_data* item;
  7148. if (!script_charid2sd(3, sd)) {
  7149. script_pushint(st,-1);
  7150. return SCRIPT_CMD_FAILURE;
  7151. }
  7152. num = script_getnum(st,2) - 1;
  7153. if( num < 0 || num >= ARRAYLENGTH(equip) )
  7154. {
  7155. script_pushint(st,-1);
  7156. return SCRIPT_CMD_FAILURE;
  7157. }
  7158. // get inventory position of item
  7159. i = pc_checkequip(sd,equip[num]);
  7160. if( i < 0 )
  7161. {
  7162. script_pushint(st,-1);
  7163. return SCRIPT_CMD_SUCCESS;
  7164. }
  7165. item = sd->inventory_data[i];
  7166. if( item != 0 )
  7167. script_pushint(st,item->nameid);
  7168. else
  7169. script_pushint(st,0);
  7170. return SCRIPT_CMD_SUCCESS;
  7171. }
  7172. /*==========================================
  7173. * getequipuniqueid(<equipment slot>{,<char_id>})
  7174. *------------------------------------------*/
  7175. BUILDIN_FUNC(getequipuniqueid)
  7176. {
  7177. int i, num;
  7178. TBL_PC* sd;
  7179. struct item* item;
  7180. if (!script_charid2sd(3, sd)) {
  7181. script_pushconststr(st, "");
  7182. return SCRIPT_CMD_FAILURE;
  7183. }
  7184. num = script_getnum(st,2) - 1;
  7185. if (num < 0 || num >= ARRAYLENGTH(equip)) {
  7186. script_pushconststr(st, "");
  7187. return SCRIPT_CMD_FAILURE;
  7188. }
  7189. // get inventory position of item
  7190. i = pc_checkequip(sd,equip[num]);
  7191. if (i < 0) {
  7192. script_pushconststr(st, "");
  7193. return SCRIPT_CMD_FAILURE;
  7194. }
  7195. item = &sd->status.inventory[i];
  7196. if (item != 0) {
  7197. int maxlen = 256;
  7198. char *buf = (char *)aMalloc(maxlen*sizeof(char));
  7199. memset(buf, 0, maxlen);
  7200. snprintf(buf, maxlen-1, "%llu", (unsigned long long)item->unique_id);
  7201. script_pushstr(st, buf);
  7202. } else
  7203. script_pushconststr(st, "");
  7204. return SCRIPT_CMD_SUCCESS;
  7205. }
  7206. /*==========================================
  7207. * Get the equipement name at pos
  7208. * return item jname or ""
  7209. * getequipname(<equipment slot>{,<char_id>})
  7210. *------------------------------------------*/
  7211. BUILDIN_FUNC(getequipname)
  7212. {
  7213. int i, num;
  7214. TBL_PC* sd;
  7215. struct item_data* item;
  7216. if (!script_charid2sd(3, sd)) {
  7217. script_pushconststr(st, "");
  7218. return SCRIPT_CMD_FAILURE;
  7219. }
  7220. num = script_getnum(st,2) - 1;
  7221. if( num < 0 || num >= ARRAYLENGTH(equip) )
  7222. {
  7223. script_pushconststr(st,"");
  7224. return SCRIPT_CMD_SUCCESS;
  7225. }
  7226. // get inventory position of item
  7227. i = pc_checkequip(sd,equip[num]);
  7228. if( i < 0 )
  7229. {
  7230. script_pushconststr(st,"");
  7231. return SCRIPT_CMD_SUCCESS;
  7232. }
  7233. item = sd->inventory_data[i];
  7234. if( item != 0 )
  7235. script_pushstrcopy(st,item->jname);
  7236. else
  7237. script_pushconststr(st,"");
  7238. return SCRIPT_CMD_SUCCESS;
  7239. }
  7240. /*==========================================
  7241. * getbrokenid [Valaris]
  7242. * getbrokenid(<number>{,<char_id>})
  7243. *------------------------------------------*/
  7244. BUILDIN_FUNC(getbrokenid)
  7245. {
  7246. int i,num,id=0,brokencounter=0;
  7247. TBL_PC *sd;
  7248. if (!script_charid2sd(3, sd)) {
  7249. script_pushint(st,0);
  7250. return SCRIPT_CMD_FAILURE;
  7251. }
  7252. num=script_getnum(st,2);
  7253. for(i=0; i<MAX_INVENTORY; i++) {
  7254. if(sd->status.inventory[i].attribute){
  7255. brokencounter++;
  7256. if(num==brokencounter){
  7257. id=sd->status.inventory[i].nameid;
  7258. break;
  7259. }
  7260. }
  7261. }
  7262. script_pushint(st,id);
  7263. return SCRIPT_CMD_SUCCESS;
  7264. }
  7265. /*==========================================
  7266. * repair [Valaris]
  7267. * repair <num>{,<char_id>};
  7268. *------------------------------------------*/
  7269. BUILDIN_FUNC(repair)
  7270. {
  7271. int i,num;
  7272. int repaircounter=0;
  7273. TBL_PC *sd;
  7274. if (!script_charid2sd(3,sd))
  7275. return SCRIPT_CMD_FAILURE;
  7276. num=script_getnum(st,2);
  7277. for(i=0; i<MAX_INVENTORY; i++) {
  7278. if(sd->status.inventory[i].attribute){
  7279. repaircounter++;
  7280. if(num==repaircounter){
  7281. sd->status.inventory[i].attribute=0;
  7282. clif_equiplist(sd);
  7283. clif_produceeffect(sd, 0, sd->status.inventory[i].nameid);
  7284. clif_misceffect(&sd->bl, 3);
  7285. break;
  7286. }
  7287. }
  7288. }
  7289. return SCRIPT_CMD_SUCCESS;
  7290. }
  7291. /*==========================================
  7292. * repairall {<char_id>}
  7293. *------------------------------------------*/
  7294. BUILDIN_FUNC(repairall)
  7295. {
  7296. int i, repaircounter = 0;
  7297. TBL_PC *sd;
  7298. if (!script_charid2sd(2,sd))
  7299. return SCRIPT_CMD_FAILURE;
  7300. for(i = 0; i < MAX_INVENTORY; i++)
  7301. {
  7302. if(sd->status.inventory[i].nameid && sd->status.inventory[i].attribute)
  7303. {
  7304. sd->status.inventory[i].attribute = 0;
  7305. clif_produceeffect(sd,0,sd->status.inventory[i].nameid);
  7306. repaircounter++;
  7307. }
  7308. }
  7309. if(repaircounter)
  7310. {
  7311. clif_misceffect(&sd->bl, 3);
  7312. clif_equiplist(sd);
  7313. }
  7314. return SCRIPT_CMD_SUCCESS;
  7315. }
  7316. /*==========================================
  7317. * Chk if player have something equiped at pos
  7318. * getequipisequiped <pos>{,<char_id>}
  7319. *------------------------------------------*/
  7320. BUILDIN_FUNC(getequipisequiped)
  7321. {
  7322. int i = -1,num;
  7323. TBL_PC *sd;
  7324. num = script_getnum(st,2);
  7325. if (!script_charid2sd(3,sd))
  7326. return SCRIPT_CMD_FAILURE;
  7327. if (num > 0 && num <= ARRAYLENGTH(equip))
  7328. i=pc_checkequip(sd,equip[num-1]);
  7329. if(i >= 0)
  7330. script_pushint(st,1);
  7331. else
  7332. script_pushint(st,0);
  7333. return SCRIPT_CMD_SUCCESS;
  7334. }
  7335. /*==========================================
  7336. * Chk if the player have something equiped at pos
  7337. * if so chk if this item ain't marked not refinable or rental
  7338. * return (npc)
  7339. * 1 : true
  7340. * 0 : false
  7341. * getequipisenableref(<equipment slot>{,<char_id>})
  7342. *------------------------------------------*/
  7343. BUILDIN_FUNC(getequipisenableref)
  7344. {
  7345. int i = -1,num;
  7346. TBL_PC *sd;
  7347. num = script_getnum(st,2);
  7348. if (!script_charid2sd(3, sd)) {
  7349. script_pushint(st,0);
  7350. return SCRIPT_CMD_FAILURE;
  7351. }
  7352. if( num > 0 && num <= ARRAYLENGTH(equip) )
  7353. i = pc_checkequip(sd,equip[num-1]);
  7354. if( i >= 0 && sd->inventory_data[i] && !sd->inventory_data[i]->flag.no_refine && !sd->status.inventory[i].expire_time )
  7355. script_pushint(st,1);
  7356. else
  7357. script_pushint(st,0);
  7358. return SCRIPT_CMD_SUCCESS;
  7359. }
  7360. /*==========================================
  7361. * Get the item refined value at pos
  7362. * return (npc)
  7363. * x : refine amount
  7364. * 0 : false (not refined)
  7365. * getequiprefinerycnt(<equipment slot>{,<char_id>})
  7366. *------------------------------------------*/
  7367. BUILDIN_FUNC(getequiprefinerycnt)
  7368. {
  7369. int i = -1,num;
  7370. TBL_PC *sd;
  7371. num = script_getnum(st,2);
  7372. if (!script_charid2sd(3, sd)) {
  7373. script_pushint(st,0);
  7374. return SCRIPT_CMD_FAILURE;
  7375. }
  7376. if (num > 0 && num <= ARRAYLENGTH(equip))
  7377. i=pc_checkequip(sd,equip[num-1]);
  7378. if(i >= 0)
  7379. script_pushint(st,sd->status.inventory[i].refine);
  7380. else
  7381. script_pushint(st,0);
  7382. return SCRIPT_CMD_SUCCESS;
  7383. }
  7384. /*==========================================
  7385. * Get the weapon level value at pos
  7386. * (pos should normally only be EQI_HAND_L or EQI_HAND_R)
  7387. * return (npc)
  7388. * x : weapon level
  7389. * 0 : false
  7390. * getequipweaponlv(<equipment slot>{,<char_id>})
  7391. *------------------------------------------*/
  7392. BUILDIN_FUNC(getequipweaponlv)
  7393. {
  7394. int i = -1,num;
  7395. TBL_PC *sd;
  7396. num = script_getnum(st,2);
  7397. if (!script_charid2sd(3, sd)) {
  7398. script_pushint(st,0);
  7399. return SCRIPT_CMD_FAILURE;
  7400. }
  7401. if (num > 0 && num <= ARRAYLENGTH(equip))
  7402. i=pc_checkequip(sd,equip[num-1]);
  7403. if(i >= 0 && sd->inventory_data[i])
  7404. script_pushint(st,sd->inventory_data[i]->wlv);
  7405. else
  7406. script_pushint(st,0);
  7407. return SCRIPT_CMD_SUCCESS;
  7408. }
  7409. /*==========================================
  7410. * Get the item refine chance (from refine.txt) for item at pos
  7411. * return (npc)
  7412. * x : refine chance
  7413. * 0 : false (max refine level or unequip..)
  7414. * getequippercentrefinery(<equipment slot>{,<char_id>})
  7415. *------------------------------------------*/
  7416. BUILDIN_FUNC(getequippercentrefinery)
  7417. {
  7418. int i = -1,num;
  7419. TBL_PC *sd;
  7420. num = script_getnum(st,2);
  7421. if (!script_charid2sd(3, sd)) {
  7422. script_pushint(st,0);
  7423. return SCRIPT_CMD_FAILURE;
  7424. }
  7425. if (num > 0 && num <= ARRAYLENGTH(equip))
  7426. i = pc_checkequip(sd,equip[num-1]);
  7427. if(i >= 0 && sd->status.inventory[i].nameid && sd->status.inventory[i].refine < MAX_REFINE)
  7428. script_pushint(st,status_get_refine_chance((enum refine_type)itemdb_wlv(sd->status.inventory[i].nameid), (int)sd->status.inventory[i].refine));
  7429. else
  7430. script_pushint(st,0);
  7431. return SCRIPT_CMD_SUCCESS;
  7432. }
  7433. /*==========================================
  7434. * Refine +1 item at pos and log and display refine
  7435. * successrefitem <equipment slot>{,<count>{,<char_id>}}
  7436. *------------------------------------------*/
  7437. BUILDIN_FUNC(successrefitem) {
  7438. short i = -1, up = 1;
  7439. int pos;
  7440. TBL_PC *sd;
  7441. pos = script_getnum(st,2);
  7442. if (!script_charid2sd(4, sd)) {
  7443. script_pushint(st, -1);
  7444. return SCRIPT_CMD_FAILURE;
  7445. }
  7446. if (script_hasdata(st, 3))
  7447. up = script_getnum(st, 3);
  7448. if (pos > 0 && pos <= ARRAYLENGTH(equip))
  7449. i = pc_checkequip(sd,equip[pos-1]);
  7450. if (i >= 0) {
  7451. unsigned int ep = sd->status.inventory[i].equip;
  7452. //Logs items, got from (N)PC scripts [Lupus]
  7453. log_pick_pc(sd, LOG_TYPE_SCRIPT, -1, &sd->status.inventory[i]);
  7454. if (sd->status.inventory[i].refine >= MAX_REFINE) {
  7455. script_pushint(st, MAX_REFINE);
  7456. return SCRIPT_CMD_SUCCESS;
  7457. }
  7458. sd->status.inventory[i].refine += up;
  7459. sd->status.inventory[i].refine = cap_value( sd->status.inventory[i].refine, 0, MAX_REFINE);
  7460. pc_unequipitem(sd,i,2); // status calc will happen in pc_equipitem() below
  7461. clif_refine(sd->fd,0,i,sd->status.inventory[i].refine);
  7462. clif_delitem(sd,i,1,3);
  7463. //Logs items, got from (N)PC scripts [Lupus]
  7464. log_pick_pc(sd, LOG_TYPE_SCRIPT, 1, &sd->status.inventory[i]);
  7465. clif_additem(sd,i,1,0);
  7466. pc_equipitem(sd,i,ep);
  7467. clif_misceffect(&sd->bl,3);
  7468. if (sd->status.inventory[i].refine == MAX_REFINE &&
  7469. sd->status.inventory[i].card[0] == CARD0_FORGE &&
  7470. sd->status.char_id == (int)MakeDWord(sd->status.inventory[i].card[2],sd->status.inventory[i].card[3]))
  7471. { // Fame point system [DracoRPG]
  7472. switch (sd->inventory_data[i]->wlv){
  7473. case 1:
  7474. pc_addfame(sd, battle_config.fame_refine_lv1); // Success to refine to +10 a lv1 weapon you forged = +1 fame point
  7475. break;
  7476. case 2:
  7477. pc_addfame(sd, battle_config.fame_refine_lv2); // Success to refine to +10 a lv2 weapon you forged = +25 fame point
  7478. break;
  7479. case 3:
  7480. pc_addfame(sd, battle_config.fame_refine_lv3); // Success to refine to +10 a lv3 weapon you forged = +1000 fame point
  7481. break;
  7482. }
  7483. }
  7484. script_pushint(st, sd->status.inventory[i].refine);
  7485. return SCRIPT_CMD_SUCCESS;
  7486. }
  7487. ShowError("buildin_successrefitem: No item equipped at pos %d (CID=%d/AID=%d).\n", pos, sd->status.char_id, sd->status.account_id);
  7488. script_pushint(st, -1);
  7489. return SCRIPT_CMD_FAILURE;
  7490. }
  7491. /*==========================================
  7492. * Show a failed Refine +1 attempt
  7493. * failedrefitem <equipment slot>{,<char_id>}
  7494. *------------------------------------------*/
  7495. BUILDIN_FUNC(failedrefitem) {
  7496. short i = -1;
  7497. int pos;
  7498. TBL_PC *sd;
  7499. pos = script_getnum(st,2);
  7500. if (!script_charid2sd(3, sd)) {
  7501. script_pushint(st, 0);
  7502. return SCRIPT_CMD_FAILURE;
  7503. }
  7504. if (pos > 0 && pos <= ARRAYLENGTH(equip))
  7505. i = pc_checkequip(sd,equip[pos-1]);
  7506. if (i >= 0) {
  7507. sd->status.inventory[i].refine = 0;
  7508. pc_unequipitem(sd,i,3); //recalculate bonus
  7509. clif_refine(sd->fd,1,i,sd->status.inventory[i].refine); //notify client of failure
  7510. pc_delitem(sd,i,1,0,2,LOG_TYPE_SCRIPT);
  7511. clif_misceffect(&sd->bl,2); // display failure effect
  7512. script_pushint(st, 1);
  7513. return SCRIPT_CMD_SUCCESS;
  7514. }
  7515. ShowError("buildin_failedrefitem: No item equipped at pos %d (CID=%d/AID=%d).\n", pos, sd->status.char_id, sd->status.account_id);
  7516. script_pushint(st, 0);
  7517. return SCRIPT_CMD_FAILURE;
  7518. }
  7519. /*==========================================
  7520. * Downgrades an Equipment Part by -1 . [Masao]
  7521. * downrefitem <equipment slot>{,<count>{,<char_id>}}
  7522. *------------------------------------------*/
  7523. BUILDIN_FUNC(downrefitem) {
  7524. short i = -1, down = 1;
  7525. int pos;
  7526. TBL_PC *sd;
  7527. if (!script_charid2sd(4, sd)) {
  7528. script_pushint(st, -1);
  7529. return SCRIPT_CMD_FAILURE;
  7530. }
  7531. pos = script_getnum(st,2);
  7532. if (script_hasdata(st, 3))
  7533. down = script_getnum(st, 3);
  7534. if (pos > 0 && pos <= ARRAYLENGTH(equip))
  7535. i = pc_checkequip(sd,equip[pos-1]);
  7536. if (i >= 0) {
  7537. unsigned int ep = sd->status.inventory[i].equip;
  7538. //Logs items, got from (N)PC scripts [Lupus]
  7539. log_pick_pc(sd, LOG_TYPE_SCRIPT, -1, &sd->status.inventory[i]);
  7540. pc_unequipitem(sd,i,2); // status calc will happen in pc_equipitem() below
  7541. sd->status.inventory[i].refine -= down;
  7542. sd->status.inventory[i].refine = cap_value( sd->status.inventory[i].refine, 0, MAX_REFINE);
  7543. clif_refine(sd->fd,2,i,sd->status.inventory[i].refine);
  7544. clif_delitem(sd,i,1,3);
  7545. //Logs items, got from (N)PC scripts [Lupus]
  7546. log_pick_pc(sd, LOG_TYPE_SCRIPT, 1, &sd->status.inventory[i]);
  7547. clif_additem(sd,i,1,0);
  7548. pc_equipitem(sd,i,ep);
  7549. clif_misceffect(&sd->bl,2);
  7550. script_pushint(st, sd->status.inventory[i].refine);
  7551. return SCRIPT_CMD_SUCCESS;
  7552. }
  7553. ShowError("buildin_downrefitem: No item equipped at pos %d (CID=%d/AID=%d).\n", pos, sd->status.char_id, sd->status.account_id);
  7554. script_pushint(st, -1);
  7555. return SCRIPT_CMD_FAILURE;
  7556. }
  7557. /**
  7558. * Delete the item equipped at pos.
  7559. * delequip <equipment slot>{,<char_id>};
  7560. **/
  7561. BUILDIN_FUNC(delequip) {
  7562. short i = -1;
  7563. int pos;
  7564. int8 ret;
  7565. TBL_PC *sd;
  7566. pos = script_getnum(st,2);
  7567. if (!script_charid2sd(3,sd)) {
  7568. st->state = END;
  7569. return SCRIPT_CMD_FAILURE;
  7570. }
  7571. if (pos > 0 && pos <= ARRAYLENGTH(equip))
  7572. i = pc_checkequip(sd,equip[pos-1]);
  7573. if (i >= 0) {
  7574. pc_unequipitem(sd,i,3); //recalculate bonus
  7575. ret = !(pc_delitem(sd,i,1,0,2,LOG_TYPE_SCRIPT));
  7576. }
  7577. else {
  7578. ShowError("buildin_delequip: No item equipped at pos %d (CID=%d/AID=%d).\n", pos, sd->status.char_id, sd->status.account_id);
  7579. st->state = END;
  7580. return SCRIPT_CMD_FAILURE;
  7581. }
  7582. script_pushint(st,ret);
  7583. return SCRIPT_CMD_SUCCESS;
  7584. }
  7585. /**
  7586. * Break the item equipped at pos.
  7587. * breakequip <equipment slot>{,<char_id>};
  7588. **/
  7589. BUILDIN_FUNC(breakequip) {
  7590. short i = -1;
  7591. int pos;
  7592. TBL_PC *sd;
  7593. pos = script_getnum(st,2);
  7594. if (!script_charid2sd(3,sd))
  7595. return SCRIPT_CMD_FAILURE;
  7596. if (pos > 0 && pos <= ARRAYLENGTH(equip))
  7597. i = pc_checkequip(sd,equip[pos-1]);
  7598. if (i >= 0) {
  7599. sd->status.inventory[i].attribute = 1;
  7600. pc_unequipitem(sd,i,3);
  7601. clif_equiplist(sd);
  7602. script_pushint(st,1);
  7603. return SCRIPT_CMD_SUCCESS;
  7604. }
  7605. ShowError("buildin_breakequip: No item equipped at pos %d (CID=%d/AID=%d).\n", pos, sd->status.char_id, sd->status.account_id);
  7606. script_pushint(st,0);
  7607. return SCRIPT_CMD_FAILURE;
  7608. }
  7609. /**
  7610. * statusup <stat>{,<char_id>};
  7611. **/
  7612. BUILDIN_FUNC(statusup)
  7613. {
  7614. int type;
  7615. TBL_PC *sd;
  7616. type = script_getnum(st,2);
  7617. if (!script_charid2sd(3, sd))
  7618. return SCRIPT_CMD_FAILURE;
  7619. pc_statusup(sd, type, 1);
  7620. return SCRIPT_CMD_SUCCESS;
  7621. }
  7622. /**
  7623. * statusup2 <stat>,<amount>{,<char_id>};
  7624. **/
  7625. BUILDIN_FUNC(statusup2)
  7626. {
  7627. int type,val;
  7628. TBL_PC *sd;
  7629. type=script_getnum(st,2);
  7630. val=script_getnum(st,3);
  7631. if (!script_charid2sd(4, sd))
  7632. return SCRIPT_CMD_FAILURE;
  7633. pc_statusup2(sd,type,val);
  7634. return SCRIPT_CMD_SUCCESS;
  7635. }
  7636. /// See 'doc/item_bonus.txt'
  7637. ///
  7638. /// bonus <bonus type>,<val1>;
  7639. /// bonus2 <bonus type>,<val1>,<val2>;
  7640. /// bonus3 <bonus type>,<val1>,<val2>,<val3>;
  7641. /// bonus4 <bonus type>,<val1>,<val2>,<val3>,<val4>;
  7642. /// bonus5 <bonus type>,<val1>,<val2>,<val3>,<val4>,<val5>;
  7643. BUILDIN_FUNC(bonus)
  7644. {
  7645. int type;
  7646. int val1;
  7647. int val2 = 0;
  7648. int val3 = 0;
  7649. int val4 = 0;
  7650. int val5 = 0;
  7651. TBL_PC* sd;
  7652. struct script_data *data;
  7653. sd = script_rid2sd(st);
  7654. if( sd == NULL )
  7655. return SCRIPT_CMD_SUCCESS; // no player attached
  7656. type = script_getnum(st,2);
  7657. switch( type ) {
  7658. case SP_AUTOSPELL:
  7659. case SP_AUTOSPELL_WHENHIT:
  7660. case SP_AUTOSPELL_ONSKILL:
  7661. case SP_SKILL_ATK:
  7662. case SP_SKILL_HEAL:
  7663. case SP_SKILL_HEAL2:
  7664. case SP_ADD_SKILL_BLOW:
  7665. case SP_CASTRATE:
  7666. case SP_ADDEFF_ONSKILL:
  7667. case SP_SKILL_USE_SP_RATE:
  7668. case SP_SKILL_COOLDOWN:
  7669. case SP_SKILL_FIXEDCAST:
  7670. case SP_SKILL_VARIABLECAST:
  7671. case SP_VARCASTRATE:
  7672. case SP_FIXCASTRATE:
  7673. case SP_SKILL_USE_SP:
  7674. case SP_SUB_SKILL:
  7675. // these bonuses support skill names
  7676. data = script_getdata(st, 3);
  7677. get_val(st, data); // Convert into value in case of a variable
  7678. val1 = ( data_isstring(data) ? skill_name2id(script_getstr(st,3)) : script_getnum(st,3) );
  7679. break;
  7680. default:
  7681. val1 = script_getnum(st,3);
  7682. break;
  7683. }
  7684. switch( script_lastdata(st)-2 ) {
  7685. case 1:
  7686. pc_bonus(sd, type, val1);
  7687. break;
  7688. case 2:
  7689. val2 = script_getnum(st,4);
  7690. pc_bonus2(sd, type, val1, val2);
  7691. break;
  7692. case 3:
  7693. val2 = script_getnum(st,4);
  7694. val3 = script_getnum(st,5);
  7695. pc_bonus3(sd, type, val1, val2, val3);
  7696. break;
  7697. case 4:
  7698. data = script_getdata(st, 4);
  7699. get_val(st, data); // Convert into value in case of a variable
  7700. if( type == SP_AUTOSPELL_ONSKILL && data_isstring(data) )
  7701. val2 = skill_name2id(script_getstr(st,4)); // 2nd value can be skill name
  7702. else
  7703. val2 = script_getnum(st,4);
  7704. val3 = script_getnum(st,5);
  7705. val4 = script_getnum(st,6);
  7706. pc_bonus4(sd, type, val1, val2, val3, val4);
  7707. break;
  7708. case 5:
  7709. data = script_getdata(st, 4);
  7710. get_val(st, data); // Convert into value in case of a variable
  7711. if( type == SP_AUTOSPELL_ONSKILL && data_isstring(data) )
  7712. val2 = skill_name2id(script_getstr(st,4)); // 2nd value can be skill name
  7713. else
  7714. val2 = script_getnum(st,4);
  7715. val3 = script_getnum(st,5);
  7716. val4 = script_getnum(st,6);
  7717. val5 = script_getnum(st,7);
  7718. pc_bonus5(sd, type, val1, val2, val3, val4, val5);
  7719. break;
  7720. default:
  7721. ShowDebug("buildin_bonus: unexpected number of arguments (%d)\n", (script_lastdata(st) - 1));
  7722. break;
  7723. }
  7724. return SCRIPT_CMD_SUCCESS;
  7725. }
  7726. BUILDIN_FUNC(autobonus)
  7727. {
  7728. unsigned int dur, pos;
  7729. short rate;
  7730. short atk_type = 0;
  7731. TBL_PC* sd;
  7732. const char *bonus_script, *other_script = NULL;
  7733. sd = script_rid2sd(st);
  7734. if( sd == NULL )
  7735. return SCRIPT_CMD_SUCCESS; // no player attached
  7736. if (current_equip_combo_pos)
  7737. pos = current_equip_combo_pos;
  7738. else
  7739. pos = sd->status.inventory[current_equip_item_index].equip;
  7740. if((sd->state.autobonus&pos) == pos)
  7741. return SCRIPT_CMD_SUCCESS;
  7742. rate = script_getnum(st,3);
  7743. dur = script_getnum(st,4);
  7744. bonus_script = script_getstr(st,2);
  7745. if( !rate || !dur || !pos || !bonus_script )
  7746. return SCRIPT_CMD_SUCCESS;
  7747. if( script_hasdata(st,5) )
  7748. atk_type = script_getnum(st,5);
  7749. if( script_hasdata(st,6) )
  7750. other_script = script_getstr(st,6);
  7751. if( pc_addautobonus(sd->autobonus,ARRAYLENGTH(sd->autobonus),
  7752. bonus_script,rate,dur,atk_type,other_script,pos,false) )
  7753. {
  7754. script_add_autobonus(bonus_script);
  7755. if( other_script )
  7756. script_add_autobonus(other_script);
  7757. }
  7758. return SCRIPT_CMD_SUCCESS;
  7759. }
  7760. BUILDIN_FUNC(autobonus2)
  7761. {
  7762. unsigned int dur, pos;
  7763. short rate;
  7764. short atk_type = 0;
  7765. TBL_PC* sd;
  7766. const char *bonus_script, *other_script = NULL;
  7767. sd = script_rid2sd(st);
  7768. if( sd == NULL )
  7769. return SCRIPT_CMD_SUCCESS; // no player attached
  7770. if (current_equip_combo_pos)
  7771. pos = current_equip_combo_pos;
  7772. else
  7773. pos = sd->status.inventory[current_equip_item_index].equip;
  7774. if((sd->state.autobonus&pos) == pos)
  7775. return SCRIPT_CMD_SUCCESS;
  7776. rate = script_getnum(st,3);
  7777. dur = script_getnum(st,4);
  7778. bonus_script = script_getstr(st,2);
  7779. if( !rate || !dur || !pos || !bonus_script )
  7780. return SCRIPT_CMD_SUCCESS;
  7781. if( script_hasdata(st,5) )
  7782. atk_type = script_getnum(st,5);
  7783. if( script_hasdata(st,6) )
  7784. other_script = script_getstr(st,6);
  7785. if( pc_addautobonus(sd->autobonus2,ARRAYLENGTH(sd->autobonus2),
  7786. bonus_script,rate,dur,atk_type,other_script,pos,false) )
  7787. {
  7788. script_add_autobonus(bonus_script);
  7789. if( other_script )
  7790. script_add_autobonus(other_script);
  7791. }
  7792. return SCRIPT_CMD_SUCCESS;
  7793. }
  7794. BUILDIN_FUNC(autobonus3)
  7795. {
  7796. unsigned int dur, pos;
  7797. short rate,atk_type;
  7798. TBL_PC* sd;
  7799. const char *bonus_script, *other_script = NULL;
  7800. struct script_data *data;
  7801. sd = script_rid2sd(st);
  7802. if( sd == NULL )
  7803. return SCRIPT_CMD_SUCCESS; // no player attached
  7804. if (current_equip_combo_pos)
  7805. pos = current_equip_combo_pos;
  7806. else
  7807. pos = sd->status.inventory[current_equip_item_index].equip;
  7808. if((sd->state.autobonus&pos) == pos)
  7809. return SCRIPT_CMD_SUCCESS;
  7810. rate = script_getnum(st,3);
  7811. dur = script_getnum(st,4);
  7812. data = script_getdata(st, 5);
  7813. get_val(st, data); // Convert into value in case of a variable
  7814. atk_type = ( data_isstring(data) ? skill_name2id(script_getstr(st,5)) : script_getnum(st,5) );
  7815. bonus_script = script_getstr(st,2);
  7816. if( !rate || !dur || !pos || !atk_type || !bonus_script )
  7817. return SCRIPT_CMD_SUCCESS;
  7818. if( script_hasdata(st,6) )
  7819. other_script = script_getstr(st,6);
  7820. if( pc_addautobonus(sd->autobonus3,ARRAYLENGTH(sd->autobonus3),
  7821. bonus_script,rate,dur,atk_type,other_script,pos,true) )
  7822. {
  7823. script_add_autobonus(bonus_script);
  7824. if( other_script )
  7825. script_add_autobonus(other_script);
  7826. }
  7827. return SCRIPT_CMD_SUCCESS;
  7828. }
  7829. /// Changes the level of a player skill.
  7830. /// <flag> defaults to 1
  7831. /// <flag>=0 : set the level of the skill
  7832. /// <flag>=1 : set the temporary level of the skill
  7833. /// <flag>=2 : add to the level of the skill
  7834. ///
  7835. /// skill <skill id>,<level>,<flag>
  7836. /// skill <skill id>,<level>
  7837. /// skill "<skill name>",<level>,<flag>
  7838. /// skill "<skill name>",<level>
  7839. BUILDIN_FUNC(skill)
  7840. {
  7841. int id;
  7842. int level;
  7843. int flag = ADDSKILL_TEMP;
  7844. TBL_PC* sd;
  7845. struct script_data *data;
  7846. const char* command = script_getfuncname(st);
  7847. sd = script_rid2sd(st);
  7848. if( sd == NULL )
  7849. return SCRIPT_CMD_SUCCESS;// no player attached, report source
  7850. if (strcmpi(command, "addtoskill") == 0)
  7851. flag = ADDSKILL_TEMP_ADDLEVEL;
  7852. data = script_getdata(st, 2);
  7853. get_val(st, data); // Convert into value in case of a variable
  7854. id = ( data_isstring(data) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  7855. level = script_getnum(st,3);
  7856. if( script_hasdata(st,4) )
  7857. flag = script_getnum(st,4);
  7858. pc_skill(sd, id, level, (enum e_addskill_type)flag);
  7859. return SCRIPT_CMD_SUCCESS;
  7860. }
  7861. /// Increases the level of a guild skill.
  7862. ///
  7863. /// guildskill <skill id>,<amount>;
  7864. /// guildskill "<skill name>",<amount>;
  7865. BUILDIN_FUNC(guildskill)
  7866. {
  7867. int id;
  7868. int level;
  7869. TBL_PC* sd;
  7870. int i;
  7871. struct script_data *data;
  7872. sd = script_rid2sd(st);
  7873. if( sd == NULL )
  7874. return SCRIPT_CMD_SUCCESS;// no player attached, report source
  7875. data = script_getdata(st, 2);
  7876. get_val(st, data); // Convert into value in case of a variable
  7877. id = ( data_isstring(data) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  7878. level = script_getnum(st,3);
  7879. for( i=0; i < level; i++ )
  7880. guild_skillup(sd, id);
  7881. return SCRIPT_CMD_SUCCESS;
  7882. }
  7883. /// Returns the level of the player skill.
  7884. ///
  7885. /// getskilllv(<skill id>) -> <level>
  7886. /// getskilllv("<skill name>") -> <level>
  7887. BUILDIN_FUNC(getskilllv)
  7888. {
  7889. int id;
  7890. TBL_PC* sd;
  7891. struct script_data *data;
  7892. sd = script_rid2sd(st);
  7893. if( sd == NULL )
  7894. return SCRIPT_CMD_SUCCESS;// no player attached, report source
  7895. data = script_getdata(st, 2);
  7896. get_val(st, data); // Convert into value in case of a variable
  7897. id = ( data_isstring(data) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  7898. script_pushint(st, pc_checkskill(sd,id));
  7899. return SCRIPT_CMD_SUCCESS;
  7900. }
  7901. /// Returns the level of the guild skill.
  7902. ///
  7903. /// getgdskilllv(<guild id>,<skill id>) -> <level>
  7904. /// getgdskilllv(<guild id>,"<skill name>") -> <level>
  7905. BUILDIN_FUNC(getgdskilllv)
  7906. {
  7907. int guild_id;
  7908. uint16 skill_id;
  7909. struct guild* g;
  7910. struct script_data *data;
  7911. guild_id = script_getnum(st,2);
  7912. data = script_getdata(st, 3);
  7913. get_val(st, data); // Convert into value in case of a variable
  7914. skill_id = ( data_isstring(data) ? skill_name2id(script_getstr(st,3)) : script_getnum(st,3) );
  7915. g = guild_search(guild_id);
  7916. if( g == NULL )
  7917. script_pushint(st, -1);
  7918. else
  7919. script_pushint(st, guild_checkskill(g,skill_id));
  7920. return SCRIPT_CMD_SUCCESS;
  7921. }
  7922. /// Returns the 'basic_skill_check' setting.
  7923. /// This config determines if the server checks the skill level of NV_BASIC
  7924. /// before allowing the basic actions.
  7925. ///
  7926. /// basicskillcheck() -> <bool>
  7927. BUILDIN_FUNC(basicskillcheck)
  7928. {
  7929. script_pushint(st, battle_config.basic_skill_check);
  7930. return SCRIPT_CMD_SUCCESS;
  7931. }
  7932. /// Returns the GM level of the player.
  7933. ///
  7934. /// getgmlevel({<char_id>}) -> <level>
  7935. BUILDIN_FUNC(getgmlevel)
  7936. {
  7937. TBL_PC* sd;
  7938. if (!script_charid2sd(2,sd))
  7939. return SCRIPT_CMD_FAILURE;
  7940. script_pushint(st, pc_get_group_level(sd));
  7941. return SCRIPT_CMD_SUCCESS;
  7942. }
  7943. /// Returns the group ID of the player.
  7944. ///
  7945. /// getgroupid({<char_id>}) -> <int>
  7946. BUILDIN_FUNC(getgroupid)
  7947. {
  7948. TBL_PC* sd;
  7949. if (!script_charid2sd(2,sd))
  7950. return SCRIPT_CMD_FAILURE;
  7951. script_pushint(st, pc_get_group_id(sd));
  7952. return SCRIPT_CMD_SUCCESS;
  7953. }
  7954. /// Terminates the execution of this script instance.
  7955. ///
  7956. /// end
  7957. BUILDIN_FUNC(end)
  7958. {
  7959. TBL_PC* sd;
  7960. sd = map_id2sd(st->rid);
  7961. st->state = END;
  7962. if (st->stack->defsp >= 1 && st->stack->stack_data[st->stack->defsp-1].type == C_RETINFO) {
  7963. int i;
  7964. for(i = 0; i < st->stack->sp; i++) {
  7965. if (st->stack->stack_data[i].type == C_RETINFO) { // Grab the first, aka the original
  7966. struct script_retinfo *ri = st->stack->stack_data[i].u.ri;
  7967. st->script = ri->script;
  7968. break;
  7969. }
  7970. }
  7971. }
  7972. if( st->mes_active )
  7973. st->mes_active = 0;
  7974. if (sd)
  7975. clif_scriptclose(sd, st->oid); // If a menu/select/prompt is active, close it.
  7976. return SCRIPT_CMD_SUCCESS;
  7977. }
  7978. /// Checks if the player has that effect state (option).
  7979. ///
  7980. /// checkoption(<option>{,<char_id>}) -> <bool>
  7981. BUILDIN_FUNC(checkoption)
  7982. {
  7983. int option;
  7984. TBL_PC* sd;
  7985. if (!script_charid2sd(3,sd))
  7986. return SCRIPT_CMD_FAILURE;
  7987. option = script_getnum(st,2);
  7988. if( sd->sc.option&option )
  7989. script_pushint(st, 1);
  7990. else
  7991. script_pushint(st, 0);
  7992. return SCRIPT_CMD_SUCCESS;
  7993. }
  7994. /// Checks if the player is in that body state (opt1).
  7995. ///
  7996. /// checkoption1(<opt1>{,<char_id>}) -> <bool>
  7997. BUILDIN_FUNC(checkoption1)
  7998. {
  7999. int opt1;
  8000. TBL_PC* sd;
  8001. if (!script_charid2sd(3,sd))
  8002. return SCRIPT_CMD_FAILURE;
  8003. opt1 = script_getnum(st,2);
  8004. if( sd->sc.opt1 == opt1 )
  8005. script_pushint(st, 1);
  8006. else
  8007. script_pushint(st, 0);
  8008. return SCRIPT_CMD_SUCCESS;
  8009. }
  8010. /// Checks if the player has that health state (opt2).
  8011. ///
  8012. /// checkoption2(<opt2>{,<char_id>}) -> <bool>
  8013. BUILDIN_FUNC(checkoption2)
  8014. {
  8015. int opt2;
  8016. TBL_PC* sd;
  8017. if (!script_charid2sd(3,sd))
  8018. return SCRIPT_CMD_FAILURE;
  8019. opt2 = script_getnum(st,2);
  8020. if( sd->sc.opt2&opt2 )
  8021. script_pushint(st, 1);
  8022. else
  8023. script_pushint(st, 0);
  8024. return SCRIPT_CMD_SUCCESS;
  8025. }
  8026. /// Changes the effect state (option) of the player.
  8027. /// <flag> defaults to 1
  8028. /// <flag>=0 : removes the option
  8029. /// <flag>=other : adds the option
  8030. ///
  8031. /// setoption <option>{,<flag>{,<char_id>}};
  8032. BUILDIN_FUNC(setoption)
  8033. {
  8034. int option;
  8035. int flag = 1;
  8036. TBL_PC* sd;
  8037. if (!script_charid2sd(4,sd))
  8038. return SCRIPT_CMD_FAILURE;
  8039. option = script_getnum(st,2);
  8040. if( script_hasdata(st,3) )
  8041. flag = script_getnum(st,3);
  8042. else if( !option ){// Request to remove everything.
  8043. flag = 0;
  8044. option = OPTION_FALCON|OPTION_RIDING;
  8045. #ifndef NEW_CARTS
  8046. option |= OPTION_CART;
  8047. #endif
  8048. }
  8049. if( flag ){// Add option
  8050. if( option&OPTION_WEDDING && !battle_config.wedding_modifydisplay )
  8051. option &= ~OPTION_WEDDING;// Do not show the wedding sprites
  8052. pc_setoption(sd, sd->sc.option|option);
  8053. } else// Remove option
  8054. pc_setoption(sd, sd->sc.option&~option);
  8055. return SCRIPT_CMD_SUCCESS;
  8056. }
  8057. /// Returns if the player has a cart.
  8058. ///
  8059. /// checkcart({char_id}) -> <bool>
  8060. ///
  8061. /// @author Valaris
  8062. BUILDIN_FUNC(checkcart)
  8063. {
  8064. TBL_PC* sd;
  8065. if (!script_charid2sd(2,sd))
  8066. return SCRIPT_CMD_FAILURE;
  8067. if( pc_iscarton(sd) )
  8068. script_pushint(st, 1);
  8069. else
  8070. script_pushint(st, 0);
  8071. return SCRIPT_CMD_SUCCESS;
  8072. }
  8073. /// Sets the cart of the player.
  8074. /// <type> defaults to 1
  8075. /// <type>=0 : removes the cart
  8076. /// <type>=1 : Normal cart
  8077. /// <type>=2 : Wooden cart
  8078. /// <type>=3 : Covered cart with flowers and ferns
  8079. /// <type>=4 : Wooden cart with a Panda doll on the back
  8080. /// <type>=5 : Normal cart with bigger wheels, a roof and a banner on the back
  8081. ///
  8082. /// setcart {<type>{,<char_id>}};
  8083. BUILDIN_FUNC(setcart)
  8084. {
  8085. int type = 1;
  8086. TBL_PC* sd;
  8087. if (!script_charid2sd(3,sd))
  8088. return SCRIPT_CMD_FAILURE;
  8089. if( script_hasdata(st,2) )
  8090. type = script_getnum(st,2);
  8091. pc_setcart(sd, type);
  8092. return SCRIPT_CMD_SUCCESS;
  8093. }
  8094. /// Returns if the player has a falcon.
  8095. ///
  8096. /// checkfalcon({char_id}) -> <bool>
  8097. ///
  8098. /// @author Valaris
  8099. BUILDIN_FUNC(checkfalcon)
  8100. {
  8101. TBL_PC* sd;
  8102. if (!script_charid2sd(2,sd))
  8103. return SCRIPT_CMD_FAILURE;
  8104. if( pc_isfalcon(sd) )
  8105. script_pushint(st, 1);
  8106. else
  8107. script_pushint(st, 0);
  8108. return SCRIPT_CMD_SUCCESS;
  8109. }
  8110. /// Sets if the player has a falcon or not.
  8111. /// <flag> defaults to 1
  8112. ///
  8113. /// setfalcon {<flag>{,<char_id>}};
  8114. BUILDIN_FUNC(setfalcon)
  8115. {
  8116. int flag = 1;
  8117. TBL_PC* sd;
  8118. if (!script_charid2sd(3,sd))
  8119. return SCRIPT_CMD_FAILURE;
  8120. if( script_hasdata(st,2) )
  8121. flag = script_getnum(st,2);
  8122. pc_setfalcon(sd, flag);
  8123. return SCRIPT_CMD_SUCCESS;
  8124. }
  8125. /// Returns if the player is riding.
  8126. ///
  8127. /// checkriding({char_id}) -> <bool>
  8128. ///
  8129. /// @author Valaris
  8130. BUILDIN_FUNC(checkriding)
  8131. {
  8132. TBL_PC* sd;
  8133. if (!script_charid2sd(2,sd))
  8134. return SCRIPT_CMD_FAILURE;
  8135. if( pc_isriding(sd) )
  8136. script_pushint(st, 1);
  8137. else
  8138. script_pushint(st, 0);
  8139. return SCRIPT_CMD_SUCCESS;
  8140. }
  8141. /// Sets if the player is riding.
  8142. /// <flag> defaults to 1
  8143. ///
  8144. /// setriding {<flag>{,<char_id>}};
  8145. BUILDIN_FUNC(setriding)
  8146. {
  8147. int flag = 1;
  8148. TBL_PC* sd;
  8149. if (!script_charid2sd(3,sd))
  8150. return SCRIPT_CMD_FAILURE;
  8151. if( script_hasdata(st,2) )
  8152. flag = script_getnum(st,2);
  8153. pc_setriding(sd, flag);
  8154. return SCRIPT_CMD_SUCCESS;
  8155. }
  8156. /// Returns if the player has a warg.
  8157. ///
  8158. /// checkwug({char_id}) -> <bool>
  8159. ///
  8160. BUILDIN_FUNC(checkwug)
  8161. {
  8162. TBL_PC* sd;
  8163. if (!script_charid2sd(2,sd))
  8164. return SCRIPT_CMD_FAILURE;
  8165. if( pc_iswug(sd) || pc_isridingwug(sd) )
  8166. script_pushint(st, 1);
  8167. else
  8168. script_pushint(st, 0);
  8169. return SCRIPT_CMD_SUCCESS;
  8170. }
  8171. /// Returns if the player is wearing MADO Gear.
  8172. ///
  8173. /// checkmadogear({<char_id>}) -> <bool>
  8174. ///
  8175. BUILDIN_FUNC(checkmadogear)
  8176. {
  8177. TBL_PC* sd;
  8178. if (!script_charid2sd(2,sd))
  8179. return SCRIPT_CMD_FAILURE;
  8180. if( pc_ismadogear(sd) )
  8181. script_pushint(st, 1);
  8182. else
  8183. script_pushint(st, 0);
  8184. return SCRIPT_CMD_SUCCESS;
  8185. }
  8186. /// Sets if the player is riding MADO Gear.
  8187. /// <flag> defaults to 1
  8188. ///
  8189. /// setmadogear {<flag>{,<char_id>}};
  8190. BUILDIN_FUNC(setmadogear)
  8191. {
  8192. int flag = 1;
  8193. TBL_PC* sd;
  8194. if (!script_charid2sd(3,sd))
  8195. return SCRIPT_CMD_FAILURE;
  8196. if( script_hasdata(st,2) )
  8197. flag = script_getnum(st,2);
  8198. pc_setmadogear(sd, flag);
  8199. return SCRIPT_CMD_SUCCESS;
  8200. }
  8201. /// Sets the save point of the player.
  8202. ///
  8203. /// save "<map name>",<x>,<y>{,<char_id>}
  8204. /// savepoint "<map name>",<x>,<y>{,<char_id>}
  8205. BUILDIN_FUNC(savepoint)
  8206. {
  8207. int x;
  8208. int y;
  8209. short map_idx;
  8210. const char* str;
  8211. TBL_PC* sd;
  8212. if (!script_charid2sd(5,sd))
  8213. return SCRIPT_CMD_FAILURE;// no player attached, report source
  8214. str = script_getstr(st, 2);
  8215. x = script_getnum(st,3);
  8216. y = script_getnum(st,4);
  8217. map_idx = mapindex_name2id(str);
  8218. if( map_idx )
  8219. pc_setsavepoint(sd, map_idx, x, y);
  8220. return SCRIPT_CMD_SUCCESS;
  8221. }
  8222. /*==========================================
  8223. * GetTimeTick(0: System Tick, 1: Time Second Tick)
  8224. *------------------------------------------*/
  8225. BUILDIN_FUNC(gettimetick) /* Asgard Version */
  8226. {
  8227. int type;
  8228. time_t timer;
  8229. struct tm *t;
  8230. type=script_getnum(st,2);
  8231. switch(type){
  8232. case 2:
  8233. //type 2:(Get the number of seconds elapsed since 00:00 hours, Jan 1, 1970 UTC
  8234. // from the system clock.)
  8235. script_pushint(st,(int)time(NULL));
  8236. break;
  8237. case 1:
  8238. //type 1:(Second Ticks: 0-86399, 00:00:00-23:59:59)
  8239. time(&timer);
  8240. t=localtime(&timer);
  8241. script_pushint(st,((t->tm_hour)*3600+(t->tm_min)*60+t->tm_sec));
  8242. break;
  8243. case 0:
  8244. default:
  8245. //type 0:(System Ticks)
  8246. script_pushint(st,gettick());
  8247. break;
  8248. }
  8249. return SCRIPT_CMD_SUCCESS;
  8250. }
  8251. /*==========================================
  8252. * GetTime(Type);
  8253. * 1: Sec 2: Min 3: Hour
  8254. * 4: WeekDay 5: MonthDay 6: Month
  8255. * 7: Year
  8256. *------------------------------------------*/
  8257. BUILDIN_FUNC(gettime) /* Asgard Version */
  8258. {
  8259. int type;
  8260. time_t timer;
  8261. struct tm *t;
  8262. type=script_getnum(st,2);
  8263. time(&timer);
  8264. t=localtime(&timer);
  8265. switch(type){
  8266. case 1://Sec(0~59)
  8267. script_pushint(st,t->tm_sec);
  8268. break;
  8269. case 2://Min(0~59)
  8270. script_pushint(st,t->tm_min);
  8271. break;
  8272. case 3://Hour(0~23)
  8273. script_pushint(st,t->tm_hour);
  8274. break;
  8275. case 4://WeekDay(0~6)
  8276. script_pushint(st,t->tm_wday);
  8277. break;
  8278. case 5://MonthDay(01~31)
  8279. script_pushint(st,t->tm_mday);
  8280. break;
  8281. case 6://Month(01~12)
  8282. script_pushint(st,t->tm_mon+1);
  8283. break;
  8284. case 7://Year(20xx)
  8285. script_pushint(st,t->tm_year+1900);
  8286. break;
  8287. case 8://Year Day(01~366)
  8288. script_pushint(st,t->tm_yday+1);
  8289. break;
  8290. default://(format error)
  8291. script_pushint(st,-1);
  8292. break;
  8293. }
  8294. return SCRIPT_CMD_SUCCESS;
  8295. }
  8296. /*==========================================
  8297. * GetTimeStr("TimeFMT", Length);
  8298. *------------------------------------------*/
  8299. BUILDIN_FUNC(gettimestr)
  8300. {
  8301. char *tmpstr;
  8302. const char *fmtstr;
  8303. int maxlen;
  8304. time_t now = time(NULL);
  8305. fmtstr=script_getstr(st,2);
  8306. maxlen=script_getnum(st,3);
  8307. tmpstr=(char *)aMalloc((maxlen+1)*sizeof(char));
  8308. strftime(tmpstr,maxlen,fmtstr,localtime(&now));
  8309. tmpstr[maxlen]='\0';
  8310. script_pushstr(st,tmpstr);
  8311. return SCRIPT_CMD_SUCCESS;
  8312. }
  8313. /*==========================================
  8314. * Open player storage
  8315. *------------------------------------------*/
  8316. BUILDIN_FUNC(openstorage)
  8317. {
  8318. TBL_PC* sd;
  8319. sd = script_rid2sd(st);
  8320. if( sd == NULL )
  8321. return SCRIPT_CMD_SUCCESS;
  8322. storage_storageopen(sd);
  8323. return SCRIPT_CMD_SUCCESS;
  8324. }
  8325. BUILDIN_FUNC(guildopenstorage)
  8326. {
  8327. TBL_PC* sd;
  8328. int ret;
  8329. sd = script_rid2sd(st);
  8330. if( sd == NULL )
  8331. return SCRIPT_CMD_SUCCESS;
  8332. ret = gstorage_storageopen(sd);
  8333. script_pushint(st,ret);
  8334. return SCRIPT_CMD_SUCCESS;
  8335. }
  8336. /*==========================================
  8337. * Make player use a skill trought item usage
  8338. *------------------------------------------*/
  8339. /// itemskill <skill id>,<level>
  8340. /// itemskill "<skill name>",<level>
  8341. BUILDIN_FUNC(itemskill)
  8342. {
  8343. int id;
  8344. int lv;
  8345. TBL_PC* sd;
  8346. struct script_data *data;
  8347. sd = script_rid2sd(st);
  8348. if( sd == NULL || sd->ud.skilltimer != INVALID_TIMER )
  8349. return SCRIPT_CMD_SUCCESS;
  8350. data = script_getdata(st, 2);
  8351. get_val(st, data); // Convert into value in case of a variable
  8352. id = ( data_isstring(data) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  8353. lv = script_getnum(st,3);
  8354. sd->skillitem=id;
  8355. sd->skillitemlv=lv;
  8356. clif_item_skill(sd,id,lv);
  8357. return SCRIPT_CMD_SUCCESS;
  8358. }
  8359. /*==========================================
  8360. * Attempt to create an item
  8361. *------------------------------------------*/
  8362. BUILDIN_FUNC(produce)
  8363. {
  8364. int trigger;
  8365. TBL_PC* sd;
  8366. sd = script_rid2sd(st);
  8367. if( sd == NULL )
  8368. return SCRIPT_CMD_SUCCESS;
  8369. trigger=script_getnum(st,2);
  8370. clif_skill_produce_mix_list(sd, -1, trigger);
  8371. return SCRIPT_CMD_SUCCESS;
  8372. }
  8373. /*==========================================
  8374. *
  8375. *------------------------------------------*/
  8376. BUILDIN_FUNC(cooking)
  8377. {
  8378. int trigger;
  8379. TBL_PC* sd;
  8380. sd = script_rid2sd(st);
  8381. if( sd == NULL )
  8382. return SCRIPT_CMD_SUCCESS;
  8383. trigger=script_getnum(st,2);
  8384. clif_cooking_list(sd, trigger, AM_PHARMACY, 1, 1);
  8385. return SCRIPT_CMD_SUCCESS;
  8386. }
  8387. /*==========================================
  8388. * Create a pet
  8389. *------------------------------------------*/
  8390. BUILDIN_FUNC(makepet)
  8391. {
  8392. TBL_PC* sd;
  8393. int id,pet_id;
  8394. id=script_getnum(st,2);
  8395. sd = script_rid2sd(st);
  8396. if( sd == NULL )
  8397. return SCRIPT_CMD_SUCCESS;
  8398. pet_id = search_petDB_index(id, PET_CLASS);
  8399. if (pet_id < 0)
  8400. pet_id = search_petDB_index(id, PET_EGG);
  8401. if (pet_id >= 0 && sd) {
  8402. sd->catch_target_class = pet_db[pet_id].class_;
  8403. intif_create_pet(
  8404. sd->status.account_id, sd->status.char_id,
  8405. (short)pet_db[pet_id].class_, (short)mob_db(pet_db[pet_id].class_)->lv,
  8406. (short)pet_db[pet_id].EggID, 0, (short)pet_db[pet_id].intimate,
  8407. 100, 0, 1, pet_db[pet_id].jname);
  8408. }
  8409. return SCRIPT_CMD_SUCCESS;
  8410. }
  8411. /**
  8412. * Give player exp base,job * quest_exp_rate/100
  8413. * getexp <base xp>,<job xp>{,<char_id>};
  8414. **/
  8415. BUILDIN_FUNC(getexp)
  8416. {
  8417. TBL_PC* sd;
  8418. int base=0,job=0;
  8419. double bonus;
  8420. if (!script_charid2sd(4,sd))
  8421. return SCRIPT_CMD_FAILURE;
  8422. base=script_getnum(st,2);
  8423. job =script_getnum(st,3);
  8424. if(base<0 || job<0)
  8425. return SCRIPT_CMD_SUCCESS;
  8426. // bonus for npc-given exp
  8427. bonus = battle_config.quest_exp_rate / 100.;
  8428. if (base)
  8429. base = (int) cap_value(base * bonus, 0, INT_MAX);
  8430. if (job)
  8431. job = (int) cap_value(job * bonus, 0, INT_MAX);
  8432. pc_gainexp(sd, NULL, base, job, true);
  8433. return SCRIPT_CMD_SUCCESS;
  8434. }
  8435. /*==========================================
  8436. * Gain guild exp [Celest]
  8437. *------------------------------------------*/
  8438. BUILDIN_FUNC(guildgetexp)
  8439. {
  8440. TBL_PC* sd;
  8441. int exp;
  8442. sd = script_rid2sd(st);
  8443. if( sd == NULL )
  8444. return SCRIPT_CMD_SUCCESS;
  8445. exp = script_getnum(st,2);
  8446. if(exp < 0)
  8447. return SCRIPT_CMD_SUCCESS;
  8448. if(sd && sd->status.guild_id > 0)
  8449. guild_getexp (sd, exp);
  8450. return SCRIPT_CMD_SUCCESS;
  8451. }
  8452. /*==========================================
  8453. * Changes the guild master of a guild [Skotlex]
  8454. *------------------------------------------*/
  8455. BUILDIN_FUNC(guildchangegm)
  8456. {
  8457. TBL_PC *sd;
  8458. int guild_id;
  8459. const char *name;
  8460. guild_id = script_getnum(st,2);
  8461. name = script_getstr(st,3);
  8462. sd=map_nick2sd(name);
  8463. if (!sd)
  8464. script_pushint(st,0);
  8465. else
  8466. script_pushint(st,guild_gm_change(guild_id, sd));
  8467. return SCRIPT_CMD_SUCCESS;
  8468. }
  8469. /*==========================================
  8470. * Spawn a monster:
  8471. * *monster "<map name>",<x>,<y>,"<name to show>",<mob id>,<amount>{,"<event label>",<size>,<ai>};
  8472. *------------------------------------------*/
  8473. BUILDIN_FUNC(monster)
  8474. {
  8475. const char* mapn = script_getstr(st,2);
  8476. int x = script_getnum(st,3);
  8477. int y = script_getnum(st,4);
  8478. const char* str = script_getstr(st,5);
  8479. int class_ = script_getnum(st,6);
  8480. int amount = script_getnum(st,7);
  8481. const char* event = "";
  8482. unsigned int size = SZ_SMALL;
  8483. unsigned int ai = AI_NONE;
  8484. struct map_session_data* sd;
  8485. int16 m;
  8486. int i;
  8487. if (script_hasdata(st, 8)) {
  8488. event = script_getstr(st, 8);
  8489. check_event(st, event);
  8490. }
  8491. if (script_hasdata(st, 9)) {
  8492. size = script_getnum(st, 9);
  8493. if (size > 3) {
  8494. ShowWarning("buildin_monster: Attempted to spawn non-existing size %d for monster class %d\n", size, class_);
  8495. return SCRIPT_CMD_FAILURE;
  8496. }
  8497. }
  8498. if (script_hasdata(st, 10)) {
  8499. ai = script_getnum(st, 10);
  8500. if (ai >= AI_MAX) {
  8501. ShowWarning("buildin_monster: Attempted to spawn non-existing ai %d for monster class %d\n", ai, class_);
  8502. return SCRIPT_CMD_FAILURE;
  8503. }
  8504. }
  8505. if (class_ >= 0 && !mobdb_checkid(class_)) {
  8506. ShowWarning("buildin_monster: Attempted to spawn non-existing monster class %d\n", class_);
  8507. return SCRIPT_CMD_FAILURE;
  8508. }
  8509. sd = map_id2sd(st->rid);
  8510. if (sd && strcmp(mapn, "this") == 0)
  8511. m = sd->bl.m;
  8512. else
  8513. m = map_mapname2mapid(mapn);
  8514. for(i = 0; i < amount; i++) { //not optimised
  8515. int mobid = mob_once_spawn(sd, m, x, y, str, class_, 1, event, size, ai);
  8516. if (mobid)
  8517. mapreg_setreg(reference_uid(add_str("$@mobid"), i), mobid);
  8518. }
  8519. return SCRIPT_CMD_SUCCESS;
  8520. }
  8521. /*==========================================
  8522. * Request List of Monster Drops
  8523. *------------------------------------------*/
  8524. BUILDIN_FUNC(getmobdrops)
  8525. {
  8526. int class_ = script_getnum(st,2);
  8527. int i, j = 0;
  8528. struct mob_db *mob;
  8529. if( !mobdb_checkid(class_) )
  8530. {
  8531. script_pushint(st, 0);
  8532. return SCRIPT_CMD_SUCCESS;
  8533. }
  8534. mob = mob_db(class_);
  8535. for( i = 0; i < MAX_MOB_DROP; i++ )
  8536. {
  8537. if( mob->dropitem[i].nameid < 1 )
  8538. continue;
  8539. if( itemdb_exists(mob->dropitem[i].nameid) == NULL )
  8540. continue;
  8541. mapreg_setreg(reference_uid(add_str("$@MobDrop_item"), j), mob->dropitem[i].nameid);
  8542. mapreg_setreg(reference_uid(add_str("$@MobDrop_rate"), j), mob->dropitem[i].p);
  8543. j++;
  8544. }
  8545. mapreg_setreg(add_str("$@MobDrop_count"), j);
  8546. script_pushint(st, 1);
  8547. return SCRIPT_CMD_SUCCESS;
  8548. }
  8549. /*==========================================
  8550. * Spawn a monster in a random location
  8551. * in x0,x1,y0,y1 area.
  8552. *------------------------------------------*/
  8553. BUILDIN_FUNC(areamonster)
  8554. {
  8555. const char* mapn = script_getstr(st,2);
  8556. int x0 = script_getnum(st,3);
  8557. int y0 = script_getnum(st,4);
  8558. int x1 = script_getnum(st,5);
  8559. int y1 = script_getnum(st,6);
  8560. const char* str = script_getstr(st,7);
  8561. int class_ = script_getnum(st,8);
  8562. int amount = script_getnum(st,9);
  8563. const char* event = "";
  8564. unsigned int size = SZ_SMALL;
  8565. unsigned int ai = AI_NONE;
  8566. struct map_session_data* sd;
  8567. int16 m;
  8568. int i;
  8569. if (script_hasdata(st,10)) {
  8570. event = script_getstr(st, 10);
  8571. check_event(st, event);
  8572. }
  8573. if (script_hasdata(st, 11)) {
  8574. size = script_getnum(st, 11);
  8575. if (size > 3) {
  8576. ShowWarning("buildin_monster: Attempted to spawn non-existing size %d for monster class %d\n", size, class_);
  8577. return SCRIPT_CMD_FAILURE;
  8578. }
  8579. }
  8580. if (script_hasdata(st, 12)) {
  8581. ai = script_getnum(st, 12);
  8582. if (ai >= AI_MAX) {
  8583. ShowWarning("buildin_monster: Attempted to spawn non-existing ai %d for monster class %d\n", ai, class_);
  8584. return SCRIPT_CMD_FAILURE;
  8585. }
  8586. }
  8587. sd = map_id2sd(st->rid);
  8588. if (sd && strcmp(mapn, "this") == 0)
  8589. m = sd->bl.m;
  8590. else
  8591. m = map_mapname2mapid(mapn);
  8592. for(i = 0; i < amount; i++) { //not optimised
  8593. int mobid = mob_once_spawn_area(sd, m, x0, y0, x1, y1, str, class_, 1, event, size, ai);
  8594. if (mobid)
  8595. mapreg_setreg(reference_uid(add_str("$@mobid"), i), mobid);
  8596. }
  8597. return SCRIPT_CMD_SUCCESS;
  8598. }
  8599. /*==========================================
  8600. * KillMonster subcheck, verify if mob to kill ain't got an even to handle, could be force kill by allflag
  8601. *------------------------------------------*/
  8602. static int buildin_killmonster_sub_strip(struct block_list *bl,va_list ap)
  8603. { //same fix but with killmonster instead - stripping events from mobs.
  8604. TBL_MOB* md = (TBL_MOB*)bl;
  8605. char *event=va_arg(ap,char *);
  8606. int allflag=va_arg(ap,int);
  8607. md->state.npc_killmonster = 1;
  8608. if(!allflag){
  8609. if(strcmp(event,md->npc_event)==0)
  8610. status_kill(bl);
  8611. }else{
  8612. if(!md->spawn)
  8613. status_kill(bl);
  8614. }
  8615. md->state.npc_killmonster = 0;
  8616. return SCRIPT_CMD_SUCCESS;
  8617. }
  8618. static int buildin_killmonster_sub(struct block_list *bl,va_list ap)
  8619. {
  8620. TBL_MOB* md = (TBL_MOB*)bl;
  8621. char *event=va_arg(ap,char *);
  8622. int allflag=va_arg(ap,int);
  8623. if(!allflag){
  8624. if(strcmp(event,md->npc_event)==0)
  8625. status_kill(bl);
  8626. }else{
  8627. if(!md->spawn)
  8628. status_kill(bl);
  8629. }
  8630. return SCRIPT_CMD_SUCCESS;
  8631. }
  8632. BUILDIN_FUNC(killmonster)
  8633. {
  8634. const char *mapname,*event;
  8635. int16 m,allflag=0;
  8636. mapname=script_getstr(st,2);
  8637. event=script_getstr(st,3);
  8638. if(strcmp(event,"All")==0)
  8639. allflag = 1;
  8640. else
  8641. check_event(st, event);
  8642. if( (m=map_mapname2mapid(mapname))<0 )
  8643. return SCRIPT_CMD_SUCCESS;
  8644. if( script_hasdata(st,4) ) {
  8645. if ( script_getnum(st,4) == 1 ) {
  8646. map_foreachinmap(buildin_killmonster_sub, m, BL_MOB, event ,allflag);
  8647. return SCRIPT_CMD_SUCCESS;
  8648. }
  8649. }
  8650. map_freeblock_lock();
  8651. map_foreachinmap(buildin_killmonster_sub_strip, m, BL_MOB, event ,allflag);
  8652. map_freeblock_unlock();
  8653. return SCRIPT_CMD_SUCCESS;
  8654. }
  8655. static int buildin_killmonsterall_sub_strip(struct block_list *bl,va_list ap)
  8656. { //Strips the event from the mob if it's killed the old method.
  8657. struct mob_data *md;
  8658. md = BL_CAST(BL_MOB, bl);
  8659. if (md->npc_event[0])
  8660. md->npc_event[0] = 0;
  8661. status_kill(bl);
  8662. return 0;
  8663. }
  8664. static int buildin_killmonsterall_sub(struct block_list *bl,va_list ap)
  8665. {
  8666. status_kill(bl);
  8667. return 0;
  8668. }
  8669. BUILDIN_FUNC(killmonsterall)
  8670. {
  8671. const char *mapname;
  8672. int16 m;
  8673. mapname=script_getstr(st,2);
  8674. if( (m = map_mapname2mapid(mapname))<0 )
  8675. return SCRIPT_CMD_SUCCESS;
  8676. if( script_hasdata(st,3) ) {
  8677. if ( script_getnum(st,3) == 1 ) {
  8678. map_foreachinmap(buildin_killmonsterall_sub,m,BL_MOB);
  8679. return SCRIPT_CMD_SUCCESS;
  8680. }
  8681. }
  8682. map_foreachinmap(buildin_killmonsterall_sub_strip,m,BL_MOB);
  8683. return SCRIPT_CMD_SUCCESS;
  8684. }
  8685. /*==========================================
  8686. * Creates a clone of a player.
  8687. * clone map, x, y, event, char_id, master_id, mode, flag, duration
  8688. *------------------------------------------*/
  8689. BUILDIN_FUNC(clone)
  8690. {
  8691. TBL_PC *sd, *msd=NULL;
  8692. uint32 char_id;
  8693. int master_id=0,x,y, mode = 0, flag = 0, m;
  8694. unsigned int duration = 0;
  8695. const char *mapname,*event;
  8696. mapname=script_getstr(st,2);
  8697. x=script_getnum(st,3);
  8698. y=script_getnum(st,4);
  8699. event=script_getstr(st,5);
  8700. char_id=script_getnum(st,6);
  8701. if( script_hasdata(st,7) )
  8702. master_id=script_getnum(st,7);
  8703. if( script_hasdata(st,8) )
  8704. mode=script_getnum(st,8);
  8705. if( script_hasdata(st,9) )
  8706. flag=script_getnum(st,9);
  8707. if( script_hasdata(st,10) )
  8708. duration=script_getnum(st,10);
  8709. check_event(st, event);
  8710. m = map_mapname2mapid(mapname);
  8711. if (m < 0)
  8712. return SCRIPT_CMD_SUCCESS;
  8713. sd = map_charid2sd(char_id);
  8714. if (master_id) {
  8715. msd = map_charid2sd(master_id);
  8716. if (msd)
  8717. master_id = msd->bl.id;
  8718. else
  8719. master_id = 0;
  8720. }
  8721. if (sd) //Return ID of newly crafted clone.
  8722. script_pushint(st,mob_clone_spawn(sd, m, x, y, event, master_id, mode, flag, 1000*duration));
  8723. else //Failed to create clone.
  8724. script_pushint(st,0);
  8725. return SCRIPT_CMD_SUCCESS;
  8726. }
  8727. /*==========================================
  8728. *------------------------------------------*/
  8729. BUILDIN_FUNC(doevent)
  8730. {
  8731. const char* event = script_getstr(st,2);
  8732. struct map_session_data* sd;
  8733. if( ( sd = script_rid2sd(st) ) == NULL )
  8734. {
  8735. return SCRIPT_CMD_SUCCESS;
  8736. }
  8737. check_event(st, event);
  8738. npc_event(sd, event, 0);
  8739. return SCRIPT_CMD_SUCCESS;
  8740. }
  8741. /*==========================================
  8742. *------------------------------------------*/
  8743. BUILDIN_FUNC(donpcevent)
  8744. {
  8745. const char* event = script_getstr(st,2);
  8746. check_event(st, event);
  8747. if( !npc_event_do(event) ) {
  8748. struct npc_data * nd = map_id2nd(st->oid);
  8749. ShowDebug("NPCEvent '%s' not found! (source: %s)\n",event,nd?nd->name:"Unknown");
  8750. script_pushint(st, 0);
  8751. } else
  8752. script_pushint(st, 1);
  8753. return SCRIPT_CMD_SUCCESS;
  8754. }
  8755. /// for Aegis compatibility
  8756. /// basically a specialized 'donpcevent', with the event specified as two arguments instead of one
  8757. BUILDIN_FUNC(cmdothernpc) // Added by RoVeRT
  8758. {
  8759. const char* npc = script_getstr(st,2);
  8760. const char* command = script_getstr(st,3);
  8761. char event[EVENT_NAME_LENGTH];
  8762. snprintf(event, sizeof(event), "%s::OnCommand%s", npc, command);
  8763. check_event(st, event);
  8764. npc_event_do(event);
  8765. return SCRIPT_CMD_SUCCESS;
  8766. }
  8767. /*==========================================
  8768. *------------------------------------------*/
  8769. BUILDIN_FUNC(addtimer)
  8770. {
  8771. int tick = script_getnum(st,2);
  8772. const char* event = script_getstr(st, 3);
  8773. TBL_PC* sd;
  8774. check_event(st, event);
  8775. sd = script_rid2sd(st);
  8776. if( sd == NULL )
  8777. return SCRIPT_CMD_SUCCESS;
  8778. if (!pc_addeventtimer(sd,tick,event)) {
  8779. ShowWarning("buildin_addtimer: Event timer is full, can't add new event timer. (cid:%d timer:%s)\n",sd->status.char_id,event);
  8780. return SCRIPT_CMD_FAILURE;
  8781. }
  8782. return SCRIPT_CMD_SUCCESS;
  8783. }
  8784. /*==========================================
  8785. *------------------------------------------*/
  8786. BUILDIN_FUNC(deltimer)
  8787. {
  8788. const char *event;
  8789. TBL_PC* sd;
  8790. event=script_getstr(st, 2);
  8791. sd = script_rid2sd(st);
  8792. if( sd == NULL )
  8793. return SCRIPT_CMD_SUCCESS;
  8794. check_event(st, event);
  8795. pc_deleventtimer(sd,event);
  8796. return SCRIPT_CMD_SUCCESS;
  8797. }
  8798. /*==========================================
  8799. *------------------------------------------*/
  8800. BUILDIN_FUNC(addtimercount)
  8801. {
  8802. const char *event;
  8803. int tick;
  8804. TBL_PC* sd;
  8805. tick=script_getnum(st,2);
  8806. event=script_getstr(st,3);
  8807. sd = script_rid2sd(st);
  8808. if( sd == NULL )
  8809. return SCRIPT_CMD_SUCCESS;
  8810. check_event(st, event);
  8811. pc_addeventtimercount(sd,event,tick);
  8812. return SCRIPT_CMD_SUCCESS;
  8813. }
  8814. /*==========================================
  8815. *------------------------------------------*/
  8816. BUILDIN_FUNC(initnpctimer)
  8817. {
  8818. struct npc_data *nd;
  8819. int flag = 0;
  8820. if( script_hasdata(st,3) )
  8821. { //Two arguments: NPC name and attach flag.
  8822. nd = npc_name2id(script_getstr(st, 2));
  8823. flag = script_getnum(st,3);
  8824. }
  8825. else if( script_hasdata(st,2) )
  8826. { //Check if argument is numeric (flag) or string (npc name)
  8827. struct script_data *data;
  8828. data = script_getdata(st,2);
  8829. get_val(st,data);
  8830. if( data_isstring(data) ) //NPC name
  8831. nd = npc_name2id(conv_str(st, data));
  8832. else if( data_isint(data) ) //Flag
  8833. {
  8834. nd = (struct npc_data *)map_id2bl(st->oid);
  8835. flag = conv_num(st,data);
  8836. }
  8837. else
  8838. {
  8839. ShowError("initnpctimer: invalid argument type #1 (needs be int or string)).\n");
  8840. return SCRIPT_CMD_FAILURE;
  8841. }
  8842. }
  8843. else
  8844. nd = (struct npc_data *)map_id2bl(st->oid);
  8845. if( !nd )
  8846. return SCRIPT_CMD_SUCCESS;
  8847. if( flag ) //Attach
  8848. {
  8849. TBL_PC* sd = script_rid2sd(st);
  8850. if( sd == NULL )
  8851. return SCRIPT_CMD_SUCCESS;
  8852. nd->u.scr.rid = sd->bl.id;
  8853. }
  8854. nd->u.scr.timertick = 0;
  8855. npc_settimerevent_tick(nd,0);
  8856. npc_timerevent_start(nd, st->rid);
  8857. return SCRIPT_CMD_SUCCESS;
  8858. }
  8859. /*==========================================
  8860. *------------------------------------------*/
  8861. BUILDIN_FUNC(startnpctimer)
  8862. {
  8863. struct npc_data *nd;
  8864. int flag = 0;
  8865. if( script_hasdata(st,3) )
  8866. { //Two arguments: NPC name and attach flag.
  8867. nd = npc_name2id(script_getstr(st, 2));
  8868. flag = script_getnum(st,3);
  8869. }
  8870. else if( script_hasdata(st,2) )
  8871. { //Check if argument is numeric (flag) or string (npc name)
  8872. struct script_data *data;
  8873. data = script_getdata(st,2);
  8874. get_val(st,data);
  8875. if( data_isstring(data) ) //NPC name
  8876. nd = npc_name2id(conv_str(st, data));
  8877. else if( data_isint(data) ) //Flag
  8878. {
  8879. nd = (struct npc_data *)map_id2bl(st->oid);
  8880. flag = conv_num(st,data);
  8881. }
  8882. else
  8883. {
  8884. ShowError("initnpctimer: invalid argument type #1 (needs be int or string)).\n");
  8885. return SCRIPT_CMD_FAILURE;
  8886. }
  8887. }
  8888. else
  8889. nd=(struct npc_data *)map_id2bl(st->oid);
  8890. if( !nd )
  8891. return SCRIPT_CMD_SUCCESS;
  8892. if( flag ) //Attach
  8893. {
  8894. TBL_PC* sd = script_rid2sd(st);
  8895. if( sd == NULL )
  8896. return SCRIPT_CMD_SUCCESS;
  8897. nd->u.scr.rid = sd->bl.id;
  8898. }
  8899. npc_timerevent_start(nd, st->rid);
  8900. return SCRIPT_CMD_SUCCESS;
  8901. }
  8902. /*==========================================
  8903. *------------------------------------------*/
  8904. BUILDIN_FUNC(stopnpctimer)
  8905. {
  8906. struct npc_data *nd;
  8907. int flag = 0;
  8908. if( script_hasdata(st,3) )
  8909. { //Two arguments: NPC name and attach flag.
  8910. nd = npc_name2id(script_getstr(st, 2));
  8911. flag = script_getnum(st,3);
  8912. }
  8913. else if( script_hasdata(st,2) )
  8914. { //Check if argument is numeric (flag) or string (npc name)
  8915. struct script_data *data;
  8916. data = script_getdata(st,2);
  8917. get_val(st,data);
  8918. if( data_isstring(data) ) //NPC name
  8919. nd = npc_name2id(conv_str(st, data));
  8920. else if( data_isint(data) ) //Flag
  8921. {
  8922. nd = (struct npc_data *)map_id2bl(st->oid);
  8923. flag = conv_num(st,data);
  8924. }
  8925. else
  8926. {
  8927. ShowError("initnpctimer: invalid argument type #1 (needs be int or string)).\n");
  8928. return SCRIPT_CMD_FAILURE;
  8929. }
  8930. }
  8931. else
  8932. nd=(struct npc_data *)map_id2bl(st->oid);
  8933. if( !nd )
  8934. return SCRIPT_CMD_SUCCESS;
  8935. if( flag ) //Detach
  8936. nd->u.scr.rid = 0;
  8937. npc_timerevent_stop(nd);
  8938. return SCRIPT_CMD_SUCCESS;
  8939. }
  8940. /*==========================================
  8941. *------------------------------------------*/
  8942. BUILDIN_FUNC(getnpctimer)
  8943. {
  8944. struct npc_data *nd;
  8945. TBL_PC *sd;
  8946. int type = script_getnum(st,2);
  8947. int val = 0;
  8948. if( script_hasdata(st,3) )
  8949. nd = npc_name2id(script_getstr(st,3));
  8950. else
  8951. nd = (struct npc_data *)map_id2bl(st->oid);
  8952. if( !nd || nd->bl.type != BL_NPC )
  8953. {
  8954. script_pushint(st,0);
  8955. ShowError("getnpctimer: Invalid NPC.\n");
  8956. return SCRIPT_CMD_FAILURE;
  8957. }
  8958. switch( type )
  8959. {
  8960. case 0: val = npc_gettimerevent_tick(nd); break;
  8961. case 1:
  8962. if( nd->u.scr.rid )
  8963. {
  8964. sd = map_id2sd(nd->u.scr.rid);
  8965. if( !sd )
  8966. {
  8967. ShowError("buildin_getnpctimer: Attached player not found!\n");
  8968. break;
  8969. }
  8970. val = (sd->npc_timer_id != INVALID_TIMER);
  8971. }
  8972. else
  8973. val = (nd->u.scr.timerid != INVALID_TIMER);
  8974. break;
  8975. case 2: val = nd->u.scr.timeramount; break;
  8976. }
  8977. script_pushint(st,val);
  8978. return SCRIPT_CMD_SUCCESS;
  8979. }
  8980. /*==========================================
  8981. *------------------------------------------*/
  8982. BUILDIN_FUNC(setnpctimer)
  8983. {
  8984. int tick;
  8985. struct npc_data *nd;
  8986. tick = script_getnum(st,2);
  8987. if( script_hasdata(st,3) )
  8988. nd = npc_name2id(script_getstr(st,3));
  8989. else
  8990. nd = (struct npc_data *)map_id2bl(st->oid);
  8991. if( !nd || nd->bl.type != BL_NPC )
  8992. {
  8993. script_pushint(st,1);
  8994. ShowError("setnpctimer: Invalid NPC.\n");
  8995. return SCRIPT_CMD_FAILURE;
  8996. }
  8997. npc_settimerevent_tick(nd,tick);
  8998. script_pushint(st,0);
  8999. return SCRIPT_CMD_SUCCESS;
  9000. }
  9001. /*==========================================
  9002. * attaches the player rid to the timer [Celest]
  9003. *------------------------------------------*/
  9004. BUILDIN_FUNC(attachnpctimer)
  9005. {
  9006. TBL_PC *sd;
  9007. struct npc_data *nd = (struct npc_data *)map_id2bl(st->oid);
  9008. if( !nd || nd->bl.type != BL_NPC )
  9009. {
  9010. script_pushint(st,1);
  9011. ShowError("setnpctimer: Invalid NPC.\n");
  9012. return SCRIPT_CMD_FAILURE;
  9013. }
  9014. if( script_hasdata(st,2) )
  9015. sd = map_nick2sd(script_getstr(st,2));
  9016. else
  9017. sd = script_rid2sd(st);
  9018. if( !sd )
  9019. {
  9020. script_pushint(st,1);
  9021. ShowWarning("attachnpctimer: Invalid player.\n");
  9022. return SCRIPT_CMD_FAILURE;
  9023. }
  9024. nd->u.scr.rid = sd->bl.id;
  9025. script_pushint(st,0);
  9026. return SCRIPT_CMD_SUCCESS;
  9027. }
  9028. /*==========================================
  9029. * detaches a player rid from the timer [Celest]
  9030. *------------------------------------------*/
  9031. BUILDIN_FUNC(detachnpctimer)
  9032. {
  9033. struct npc_data *nd;
  9034. if( script_hasdata(st,2) )
  9035. nd = npc_name2id(script_getstr(st,2));
  9036. else
  9037. nd = (struct npc_data *)map_id2bl(st->oid);
  9038. if( !nd || nd->bl.type != BL_NPC )
  9039. {
  9040. script_pushint(st,1);
  9041. ShowError("detachnpctimer: Invalid NPC.\n");
  9042. return SCRIPT_CMD_FAILURE;
  9043. }
  9044. nd->u.scr.rid = 0;
  9045. script_pushint(st,0);
  9046. return SCRIPT_CMD_SUCCESS;
  9047. }
  9048. /*==========================================
  9049. * To avoid "player not attached" script errors, this function is provided,
  9050. * it checks if there is a player attached to the current script. [Skotlex]
  9051. * If no, returns 0, if yes, returns the account_id of the attached player.
  9052. *------------------------------------------*/
  9053. BUILDIN_FUNC(playerattached)
  9054. {
  9055. if(st->rid == 0 || map_id2sd(st->rid) == NULL)
  9056. script_pushint(st,0);
  9057. else
  9058. script_pushint(st,st->rid);
  9059. return SCRIPT_CMD_SUCCESS;
  9060. }
  9061. /*==========================================
  9062. *------------------------------------------*/
  9063. BUILDIN_FUNC(announce)
  9064. {
  9065. const char *mes = script_getstr(st,2);
  9066. int flag = script_getnum(st,3);
  9067. const char *fontColor = script_hasdata(st,4) ? script_getstr(st,4) : NULL;
  9068. int fontType = script_hasdata(st,5) ? script_getnum(st,5) : FW_NORMAL; // default fontType
  9069. int fontSize = script_hasdata(st,6) ? script_getnum(st,6) : 12; // default fontSize
  9070. int fontAlign = script_hasdata(st,7) ? script_getnum(st,7) : 0; // default fontAlign
  9071. int fontY = script_hasdata(st,8) ? script_getnum(st,8) : 0; // default fontY
  9072. if (flag&(BC_TARGET_MASK|BC_SOURCE_MASK)) // Broadcast source or broadcast region defined
  9073. {
  9074. send_target target;
  9075. 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
  9076. if (bl == NULL)
  9077. return SCRIPT_CMD_SUCCESS;
  9078. switch (flag&BC_TARGET_MASK) {
  9079. case BC_MAP: target = ALL_SAMEMAP; break;
  9080. case BC_AREA: target = AREA; break;
  9081. case BC_SELF: target = SELF; break;
  9082. default: target = ALL_CLIENT; break; // BC_ALL
  9083. }
  9084. if (fontColor)
  9085. clif_broadcast2(bl, mes, (int)strlen(mes)+1, strtol(fontColor, (char **)NULL, 0), fontType, fontSize, fontAlign, fontY, target);
  9086. else
  9087. clif_broadcast(bl, mes, (int)strlen(mes)+1, flag&BC_COLOR_MASK, target);
  9088. }
  9089. else
  9090. {
  9091. if (fontColor)
  9092. intif_broadcast2(mes, (int)strlen(mes)+1, strtol(fontColor, (char **)NULL, 0), fontType, fontSize, fontAlign, fontY);
  9093. else
  9094. intif_broadcast(mes, (int)strlen(mes)+1, flag&BC_COLOR_MASK);
  9095. }
  9096. return SCRIPT_CMD_SUCCESS;
  9097. }
  9098. /*==========================================
  9099. *------------------------------------------*/
  9100. static int buildin_announce_sub(struct block_list *bl, va_list ap)
  9101. {
  9102. char *mes = va_arg(ap, char *);
  9103. int len = va_arg(ap, int);
  9104. int type = va_arg(ap, int);
  9105. char *fontColor = va_arg(ap, char *);
  9106. short fontType = (short)va_arg(ap, int);
  9107. short fontSize = (short)va_arg(ap, int);
  9108. short fontAlign = (short)va_arg(ap, int);
  9109. short fontY = (short)va_arg(ap, int);
  9110. if (fontColor)
  9111. clif_broadcast2(bl, mes, len, strtol(fontColor, (char **)NULL, 0), fontType, fontSize, fontAlign, fontY, SELF);
  9112. else
  9113. clif_broadcast(bl, mes, len, type, SELF);
  9114. return SCRIPT_CMD_SUCCESS;
  9115. }
  9116. BUILDIN_FUNC(mapannounce)
  9117. {
  9118. const char *mapname = script_getstr(st,2);
  9119. const char *mes = script_getstr(st,3);
  9120. int flag = script_getnum(st,4);
  9121. const char *fontColor = script_hasdata(st,5) ? script_getstr(st,5) : NULL;
  9122. int fontType = script_hasdata(st,6) ? script_getnum(st,6) : FW_NORMAL; // default fontType
  9123. int fontSize = script_hasdata(st,7) ? script_getnum(st,7) : 12; // default fontSize
  9124. int fontAlign = script_hasdata(st,8) ? script_getnum(st,8) : 0; // default fontAlign
  9125. int fontY = script_hasdata(st,9) ? script_getnum(st,9) : 0; // default fontY
  9126. int16 m;
  9127. if ((m = map_mapname2mapid(mapname)) < 0)
  9128. return SCRIPT_CMD_SUCCESS;
  9129. map_foreachinmap(buildin_announce_sub, m, BL_PC,
  9130. mes, strlen(mes)+1, flag&BC_COLOR_MASK, fontColor, fontType, fontSize, fontAlign, fontY);
  9131. return SCRIPT_CMD_SUCCESS;
  9132. }
  9133. /*==========================================
  9134. *------------------------------------------*/
  9135. BUILDIN_FUNC(areaannounce)
  9136. {
  9137. const char *mapname = script_getstr(st,2);
  9138. int x0 = script_getnum(st,3);
  9139. int y0 = script_getnum(st,4);
  9140. int x1 = script_getnum(st,5);
  9141. int y1 = script_getnum(st,6);
  9142. const char *mes = script_getstr(st,7);
  9143. int flag = script_getnum(st,8);
  9144. const char *fontColor = script_hasdata(st,9) ? script_getstr(st,9) : NULL;
  9145. int fontType = script_hasdata(st,10) ? script_getnum(st,10) : FW_NORMAL; // default fontType
  9146. int fontSize = script_hasdata(st,11) ? script_getnum(st,11) : 12; // default fontSize
  9147. int fontAlign = script_hasdata(st,12) ? script_getnum(st,12) : 0; // default fontAlign
  9148. int fontY = script_hasdata(st,13) ? script_getnum(st,13) : 0; // default fontY
  9149. int16 m;
  9150. if ((m = map_mapname2mapid(mapname)) < 0)
  9151. return SCRIPT_CMD_SUCCESS;
  9152. map_foreachinarea(buildin_announce_sub, m, x0, y0, x1, y1, BL_PC,
  9153. mes, strlen(mes)+1, flag&BC_COLOR_MASK, fontColor, fontType, fontSize, fontAlign, fontY);
  9154. return SCRIPT_CMD_SUCCESS;
  9155. }
  9156. /*==========================================
  9157. *------------------------------------------*/
  9158. BUILDIN_FUNC(getusers)
  9159. {
  9160. int flag, val = 0;
  9161. struct map_session_data* sd;
  9162. struct block_list* bl = NULL;
  9163. flag = script_getnum(st,2);
  9164. switch(flag&0x07)
  9165. {
  9166. case 0:
  9167. if(flag&0x8)
  9168. {// npc
  9169. bl = map_id2bl(st->oid);
  9170. }
  9171. else if((sd = script_rid2sd(st))!=NULL)
  9172. {// pc
  9173. bl = &sd->bl;
  9174. }
  9175. if(bl)
  9176. {
  9177. val = map[bl->m].users;
  9178. }
  9179. break;
  9180. case 1:
  9181. val = map_getusers();
  9182. break;
  9183. default:
  9184. ShowWarning("buildin_getusers: Unknown type %d.\n", flag);
  9185. script_pushint(st,0);
  9186. return SCRIPT_CMD_FAILURE;
  9187. }
  9188. script_pushint(st,val);
  9189. return SCRIPT_CMD_SUCCESS;
  9190. }
  9191. /*==========================================
  9192. * getmapguildusers("mapname",guild ID) Returns the number guild members present on a map [Reddozen]
  9193. *------------------------------------------*/
  9194. BUILDIN_FUNC(getmapguildusers)
  9195. {
  9196. const char *str;
  9197. int16 m;
  9198. int gid;
  9199. int c=0;
  9200. struct guild *g = NULL;
  9201. str=script_getstr(st,2);
  9202. gid=script_getnum(st,3);
  9203. if ((m = map_mapname2mapid(str)) < 0) { // map id on this server (m == -1 if not in actual map-server)
  9204. script_pushint(st,-1);
  9205. return SCRIPT_CMD_SUCCESS;
  9206. }
  9207. g = guild_search(gid);
  9208. if (g){
  9209. unsigned short i;
  9210. for(i = 0; i < g->max_member; i++)
  9211. {
  9212. if (g->member[i].sd && g->member[i].sd->bl.m == m)
  9213. c++;
  9214. }
  9215. }
  9216. script_pushint(st,c);
  9217. return SCRIPT_CMD_SUCCESS;
  9218. }
  9219. /*==========================================
  9220. *------------------------------------------*/
  9221. BUILDIN_FUNC(getmapusers)
  9222. {
  9223. const char *str;
  9224. int16 m;
  9225. str=script_getstr(st,2);
  9226. if( (m=map_mapname2mapid(str))< 0){
  9227. script_pushint(st,-1);
  9228. return SCRIPT_CMD_SUCCESS;
  9229. }
  9230. script_pushint(st,map[m].users);
  9231. return SCRIPT_CMD_SUCCESS;
  9232. }
  9233. /*==========================================
  9234. *------------------------------------------*/
  9235. static int buildin_getareausers_sub(struct block_list *bl,va_list ap)
  9236. {
  9237. int *users=va_arg(ap,int *);
  9238. (*users)++;
  9239. return SCRIPT_CMD_SUCCESS;
  9240. }
  9241. BUILDIN_FUNC(getareausers)
  9242. {
  9243. const char *str;
  9244. int16 m,x0,y0,x1,y1,users=0; //doubt we can have more then 32k users on
  9245. str=script_getstr(st,2);
  9246. x0=script_getnum(st,3);
  9247. y0=script_getnum(st,4);
  9248. x1=script_getnum(st,5);
  9249. y1=script_getnum(st,6);
  9250. if( (m=map_mapname2mapid(str))< 0){
  9251. script_pushint(st,-1);
  9252. return SCRIPT_CMD_SUCCESS;
  9253. }
  9254. map_foreachinarea(buildin_getareausers_sub,
  9255. m,x0,y0,x1,y1,BL_PC,&users);
  9256. script_pushint(st,users);
  9257. return SCRIPT_CMD_SUCCESS;
  9258. }
  9259. /*==========================================
  9260. *------------------------------------------*/
  9261. static int buildin_getareadropitem_sub(struct block_list *bl,va_list ap)
  9262. {
  9263. int nameid=va_arg(ap,int);
  9264. int *amount=va_arg(ap,int *);
  9265. struct flooritem_data *drop=(struct flooritem_data *)bl;
  9266. if(drop->item.nameid==nameid)
  9267. (*amount)+=drop->item.amount;
  9268. return SCRIPT_CMD_SUCCESS;
  9269. }
  9270. BUILDIN_FUNC(getareadropitem)
  9271. {
  9272. const char *str;
  9273. int16 m,x0,y0,x1,y1;
  9274. int nameid,amount=0;
  9275. struct script_data *data;
  9276. str=script_getstr(st,2);
  9277. x0=script_getnum(st,3);
  9278. y0=script_getnum(st,4);
  9279. x1=script_getnum(st,5);
  9280. y1=script_getnum(st,6);
  9281. data=script_getdata(st,7);
  9282. get_val(st,data);
  9283. if( data_isstring(data) ){
  9284. const char *name=conv_str(st,data);
  9285. struct item_data *item_data = itemdb_searchname(name);
  9286. nameid=UNKNOWN_ITEM_ID;
  9287. if( item_data )
  9288. nameid=item_data->nameid;
  9289. }else
  9290. nameid=conv_num(st,data);
  9291. if( (m=map_mapname2mapid(str))< 0){
  9292. script_pushint(st,-1);
  9293. return SCRIPT_CMD_SUCCESS;
  9294. }
  9295. map_foreachinarea(buildin_getareadropitem_sub,
  9296. m,x0,y0,x1,y1,BL_ITEM,nameid,&amount);
  9297. script_pushint(st,amount);
  9298. return SCRIPT_CMD_SUCCESS;
  9299. }
  9300. /*==========================================
  9301. *------------------------------------------*/
  9302. BUILDIN_FUNC(enablenpc)
  9303. {
  9304. const char *str;
  9305. str=script_getstr(st,2);
  9306. npc_enable(str,1);
  9307. return SCRIPT_CMD_SUCCESS;
  9308. }
  9309. /*==========================================
  9310. *------------------------------------------*/
  9311. BUILDIN_FUNC(disablenpc)
  9312. {
  9313. const char *str;
  9314. str=script_getstr(st,2);
  9315. npc_enable(str,0);
  9316. return SCRIPT_CMD_SUCCESS;
  9317. }
  9318. /*==========================================
  9319. *------------------------------------------*/
  9320. BUILDIN_FUNC(hideoffnpc)
  9321. {
  9322. const char *str;
  9323. str=script_getstr(st,2);
  9324. npc_enable(str,2);
  9325. return SCRIPT_CMD_SUCCESS;
  9326. }
  9327. /*==========================================
  9328. *------------------------------------------*/
  9329. BUILDIN_FUNC(hideonnpc)
  9330. {
  9331. const char *str;
  9332. str=script_getstr(st,2);
  9333. npc_enable(str,4);
  9334. return SCRIPT_CMD_SUCCESS;
  9335. }
  9336. /* Starts a status effect on the target unit or on the attached player.
  9337. *
  9338. * sc_start <effect_id>,<duration>,<val1>{,<rate>,<flag>,{<unit_id>}};
  9339. * sc_start2 <effect_id>,<duration>,<val1>,<val2>{,<rate,<flag>,{<unit_id>}};
  9340. * sc_start4 <effect_id>,<duration>,<val1>,<val2>,<val3>,<val4>{,<rate,<flag>,{<unit_id>}};
  9341. * <flag>: enum e_status_change_start_flags
  9342. */
  9343. BUILDIN_FUNC(sc_start)
  9344. {
  9345. TBL_NPC * nd = map_id2nd(st->oid);
  9346. struct block_list* bl;
  9347. enum sc_type type;
  9348. int tick, val1, val2, val3, val4=0, rate, flag;
  9349. char start_type;
  9350. const char* command = script_getfuncname(st);
  9351. if(strstr(command, "4"))
  9352. start_type = 4;
  9353. else if(strstr(command, "2"))
  9354. start_type = 2;
  9355. else
  9356. start_type = 1;
  9357. type = (sc_type)script_getnum(st,2);
  9358. tick = script_getnum(st,3);
  9359. val1 = script_getnum(st,4);
  9360. //If from NPC we make default flag 1 to be unavoidable
  9361. if(nd && nd->bl.id == fake_nd->bl.id)
  9362. flag = script_hasdata(st,5+start_type)?script_getnum(st,5+start_type):SCSTART_NOTICKDEF;
  9363. else
  9364. flag = script_hasdata(st,5+start_type)?script_getnum(st,5+start_type):SCSTART_NOAVOID;
  9365. rate = script_hasdata(st,4+start_type)?min(script_getnum(st,4+start_type),10000):10000;
  9366. if(script_hasdata(st,(6+start_type)))
  9367. bl = map_id2bl(script_getnum(st,(6+start_type)));
  9368. else
  9369. bl = map_id2bl(st->rid);
  9370. if(tick == 0 && val1 > 0 && type > SC_NONE && type < SC_MAX && status_sc2skill(type) != 0)
  9371. {// When there isn't a duration specified, try to get it from the skill_db
  9372. tick = skill_get_time(status_sc2skill(type), val1);
  9373. }
  9374. if(potion_flag == 1 && potion_target) { //skill.c set the flags before running the script, this is a potion-pitched effect.
  9375. bl = map_id2bl(potion_target);
  9376. tick /= 2;// Thrown potions only last half.
  9377. val4 = 1;// Mark that this was a thrown sc_effect
  9378. }
  9379. if(!bl)
  9380. return SCRIPT_CMD_SUCCESS;
  9381. switch(start_type) {
  9382. case 1:
  9383. status_change_start(bl, bl, type, rate, val1, 0, 0, val4, tick, flag);
  9384. break;
  9385. case 2:
  9386. val2 = script_getnum(st,5);
  9387. status_change_start(bl, bl, type, rate, val1, val2, 0, val4, tick, flag);
  9388. break;
  9389. case 4:
  9390. val2 = script_getnum(st,5);
  9391. val3 = script_getnum(st,6);
  9392. val4 = script_getnum(st,7);
  9393. status_change_start(bl, bl, type, rate, val1, val2, val3, val4, tick, flag);
  9394. break;
  9395. }
  9396. return SCRIPT_CMD_SUCCESS;
  9397. }
  9398. /// Ends one or all status effects on the target unit or on the attached player.
  9399. ///
  9400. /// sc_end <effect_id>{,<unit_id>};
  9401. BUILDIN_FUNC(sc_end)
  9402. {
  9403. struct block_list* bl;
  9404. int type;
  9405. type = script_getnum(st, 2);
  9406. if (script_hasdata(st, 3))
  9407. bl = map_id2bl(script_getnum(st, 3));
  9408. else
  9409. bl = map_id2bl(st->rid);
  9410. if (potion_flag == 1 && potion_target) //##TODO how does this work [FlavioJS]
  9411. bl = map_id2bl(potion_target);
  9412. if (!bl)
  9413. return SCRIPT_CMD_SUCCESS;
  9414. if (type >= 0 && type < SC_MAX) {
  9415. struct status_change *sc = status_get_sc(bl);
  9416. struct status_change_entry *sce = sc ? sc->data[type] : NULL;
  9417. if (!sce)
  9418. return SCRIPT_CMD_SUCCESS;
  9419. switch (type) {
  9420. case SC_WEIGHT50:
  9421. case SC_WEIGHT90:
  9422. case SC_NOCHAT:
  9423. case SC_PUSH_CART:
  9424. case SC_ALL_RIDING:
  9425. case SC_STYLE_CHANGE:
  9426. case SC_MONSTER_TRANSFORM:
  9427. case SC_MTF_ASPD:
  9428. case SC_MTF_RANGEATK:
  9429. case SC_MTF_MATK:
  9430. case SC_MTF_MLEATKED:
  9431. case SC_MTF_CRIDAMAGE:
  9432. case SC_MTF_ASPD2:
  9433. case SC_MTF_RANGEATK2:
  9434. case SC_MTF_MATK2:
  9435. case SC_MTF_MHP:
  9436. case SC_MTF_MSP:
  9437. case SC_MTF_PUMPKIN:
  9438. case SC_MTF_HITFLEE:
  9439. case SC_ATTHASTE_CASH:
  9440. case SC_REUSE_LIMIT_A: case SC_REUSE_LIMIT_B: case SC_REUSE_LIMIT_C:
  9441. case SC_REUSE_LIMIT_D: case SC_REUSE_LIMIT_E: case SC_REUSE_LIMIT_F:
  9442. case SC_REUSE_LIMIT_G: case SC_REUSE_LIMIT_H: case SC_REUSE_LIMIT_MTF:
  9443. case SC_REUSE_LIMIT_ASPD_POTION: case SC_REUSE_MILLENNIUMSHIELD: case SC_REUSE_CRUSHSTRIKE:
  9444. case SC_REUSE_STORMBLAST: case SC_ALL_RIDING_REUSE_LIMIT: case SC_REUSE_REFRESH:
  9445. case SC_REUSE_LIMIT_ECL: case SC_REUSE_LIMIT_RECALL:
  9446. return SCRIPT_CMD_SUCCESS;
  9447. default:
  9448. break;
  9449. }
  9450. //This should help status_change_end force disabling the SC in case it has no limit.
  9451. sce->val1 = sce->val2 = sce->val3 = sce->val4 = 0;
  9452. status_change_end(bl, (sc_type)type, INVALID_TIMER);
  9453. } else
  9454. status_change_clear(bl, 3); // remove all effects
  9455. return SCRIPT_CMD_SUCCESS;
  9456. }
  9457. /*==========================================
  9458. * @FIXME atm will return reduced tick, 0 immune, 1 no tick
  9459. *------------------------------------------*/
  9460. BUILDIN_FUNC(getscrate)
  9461. {
  9462. struct block_list *bl;
  9463. int type,rate;
  9464. type=script_getnum(st,2);
  9465. rate=script_getnum(st,3);
  9466. if( script_hasdata(st,4) ) //get for the bl assigned
  9467. bl = map_id2bl(script_getnum(st,4));
  9468. else
  9469. bl = map_id2bl(st->rid);
  9470. if (bl)
  9471. rate = status_get_sc_def(NULL,bl, (sc_type)type, 10000, 10000, SCSTART_NONE);
  9472. script_pushint(st,rate);
  9473. return SCRIPT_CMD_SUCCESS;
  9474. }
  9475. /**
  9476. * getstatus(<effect type>{,<type>{,<char_id>}});
  9477. **/
  9478. BUILDIN_FUNC(getstatus)
  9479. {
  9480. int id, type;
  9481. struct map_session_data* sd;
  9482. if (!script_charid2sd(4,sd))
  9483. return SCRIPT_CMD_FAILURE;
  9484. id = script_getnum(st, 2);
  9485. type = script_hasdata(st, 3) ? script_getnum(st, 3) : 0;
  9486. if( id <= SC_NONE || id >= SC_MAX )
  9487. {// invalid status type given
  9488. ShowWarning("script.c:getstatus: Invalid status type given (%d).\n", id);
  9489. return SCRIPT_CMD_SUCCESS;
  9490. }
  9491. if( sd->sc.count == 0 || !sd->sc.data[id] )
  9492. {// no status is active
  9493. script_pushint(st, 0);
  9494. return SCRIPT_CMD_SUCCESS;
  9495. }
  9496. switch( type )
  9497. {
  9498. case 1: script_pushint(st, sd->sc.data[id]->val1); break;
  9499. case 2: script_pushint(st, sd->sc.data[id]->val2); break;
  9500. case 3: script_pushint(st, sd->sc.data[id]->val3); break;
  9501. case 4: script_pushint(st, sd->sc.data[id]->val4); break;
  9502. case 5:
  9503. {
  9504. struct TimerData* timer = (struct TimerData*)get_timer(sd->sc.data[id]->timer);
  9505. if( timer )
  9506. {// return the amount of time remaining
  9507. script_pushint(st, timer->tick - gettick());
  9508. } else {
  9509. script_pushint(st, -1);
  9510. }
  9511. }
  9512. break;
  9513. default: script_pushint(st, 1); break;
  9514. }
  9515. return SCRIPT_CMD_SUCCESS;
  9516. }
  9517. /*==========================================
  9518. *
  9519. *------------------------------------------*/
  9520. BUILDIN_FUNC(debugmes)
  9521. {
  9522. const char *str;
  9523. str=script_getstr(st,2);
  9524. ShowDebug("script debug : %d %d : %s\n",st->rid,st->oid,str);
  9525. return SCRIPT_CMD_SUCCESS;
  9526. }
  9527. /*==========================================
  9528. *------------------------------------------*/
  9529. BUILDIN_FUNC(catchpet)
  9530. {
  9531. int pet_id;
  9532. TBL_PC *sd;
  9533. pet_id= script_getnum(st,2);
  9534. sd=script_rid2sd(st);
  9535. if( sd == NULL )
  9536. return SCRIPT_CMD_SUCCESS;
  9537. pet_catch_process1(sd,pet_id);
  9538. return SCRIPT_CMD_SUCCESS;
  9539. }
  9540. /*==========================================
  9541. * [orn]
  9542. *------------------------------------------*/
  9543. BUILDIN_FUNC(homunculus_evolution)
  9544. {
  9545. TBL_PC *sd;
  9546. sd=script_rid2sd(st);
  9547. if( sd == NULL )
  9548. return SCRIPT_CMD_SUCCESS;
  9549. if(hom_is_active(sd->hd))
  9550. {
  9551. if (sd->hd->homunculus.intimacy >= battle_config.homunculus_evo_intimacy_need)
  9552. hom_evolution(sd->hd);
  9553. else
  9554. clif_emotion(&sd->hd->bl, E_SWT);
  9555. }
  9556. return SCRIPT_CMD_SUCCESS;
  9557. }
  9558. /*==========================================
  9559. * Checks for vaporized morph state
  9560. * and deletes ITEMID_STRANGE_EMBRYO.
  9561. *------------------------------------------*/
  9562. BUILDIN_FUNC(homunculus_mutate)
  9563. {
  9564. int homun_id;
  9565. TBL_PC *sd;
  9566. sd = script_rid2sd(st);
  9567. if( sd == NULL || sd->hd == NULL )
  9568. return SCRIPT_CMD_SUCCESS;
  9569. if(script_hasdata(st,2))
  9570. homun_id = script_getnum(st,2);
  9571. else
  9572. homun_id = 6048 + (rnd() % 4);
  9573. if( sd->hd->homunculus.vaporize == HOM_ST_MORPH ) {
  9574. int m_class = hom_class2mapid(sd->hd->homunculus.class_);
  9575. int m_id = hom_class2mapid(homun_id);
  9576. short i = pc_search_inventory(sd, ITEMID_STRANGE_EMBRYO);
  9577. if ( m_class != -1 && m_id != -1 && m_class&HOM_EVO && m_id&HOM_S && sd->hd->homunculus.level >= 99 && i >= 0 ) {
  9578. sd->hd->homunculus.vaporize = HOM_ST_REST; // Remove morph state.
  9579. hom_call(sd); // Respawn homunculus.
  9580. hom_mutate(sd->hd, homun_id);
  9581. pc_delitem(sd, i, 1, 0, 0, LOG_TYPE_SCRIPT);
  9582. script_pushint(st, 1);
  9583. return SCRIPT_CMD_SUCCESS;
  9584. } else
  9585. clif_emotion(&sd->bl, E_SWT);
  9586. } else
  9587. clif_emotion(&sd->bl, E_SWT);
  9588. script_pushint(st, 0);
  9589. return SCRIPT_CMD_SUCCESS;
  9590. }
  9591. /*==========================================
  9592. * Puts homunculus into morph state
  9593. * and gives ITEMID_STRANGE_EMBRYO.
  9594. *------------------------------------------*/
  9595. BUILDIN_FUNC(morphembryo)
  9596. {
  9597. struct item item_tmp;
  9598. TBL_PC *sd;
  9599. sd = script_rid2sd(st);
  9600. if( sd == NULL || sd->hd == NULL )
  9601. return SCRIPT_CMD_SUCCESS;
  9602. if( hom_is_active(sd->hd) ) {
  9603. int m_class = hom_class2mapid(sd->hd->homunculus.class_);
  9604. if ( m_class != -1 && m_class&HOM_EVO && sd->hd->homunculus.level >= 99 ) {
  9605. char i;
  9606. memset(&item_tmp, 0, sizeof(item_tmp));
  9607. item_tmp.nameid = ITEMID_STRANGE_EMBRYO;
  9608. item_tmp.identify = 1;
  9609. if( (i = pc_additem(sd, &item_tmp, 1, LOG_TYPE_SCRIPT)) ) {
  9610. clif_additem(sd, 0, 0, i);
  9611. clif_emotion(&sd->bl, E_SWT); // Fail to avoid item drop exploit.
  9612. } else {
  9613. hom_vaporize(sd, HOM_ST_MORPH);
  9614. script_pushint(st, 1);
  9615. return SCRIPT_CMD_SUCCESS;
  9616. }
  9617. } else
  9618. clif_emotion(&sd->hd->bl, E_SWT);
  9619. } else
  9620. clif_emotion(&sd->bl, E_SWT);
  9621. script_pushint(st, 0);
  9622. return SCRIPT_CMD_SUCCESS;
  9623. }
  9624. // [Zephyrus]
  9625. BUILDIN_FUNC(homunculus_shuffle)
  9626. {
  9627. TBL_PC *sd;
  9628. sd=script_rid2sd(st);
  9629. if( sd == NULL )
  9630. return SCRIPT_CMD_SUCCESS;
  9631. if(hom_is_active(sd->hd))
  9632. hom_shuffle(sd->hd);
  9633. return SCRIPT_CMD_SUCCESS;
  9634. }
  9635. /*==========================================
  9636. * Check for homunculus state.
  9637. * Return: -1 = No homunculus
  9638. * 0 = Homunculus is active
  9639. * 1 = Homunculus is vaporized (rest)
  9640. * 2 = Homunculus is in morph state
  9641. *------------------------------------------*/
  9642. BUILDIN_FUNC(checkhomcall)
  9643. {
  9644. TBL_PC *sd = script_rid2sd(st);
  9645. TBL_HOM *hd;
  9646. if( sd == NULL )
  9647. return SCRIPT_CMD_SUCCESS;
  9648. hd = sd->hd;
  9649. if( !hd )
  9650. script_pushint(st, -1);
  9651. else
  9652. script_pushint(st, hd->homunculus.vaporize);
  9653. return SCRIPT_CMD_SUCCESS;
  9654. }
  9655. //These two functions bring the eA MAPID_* class functionality to scripts.
  9656. BUILDIN_FUNC(eaclass)
  9657. {
  9658. int class_;
  9659. if( script_hasdata(st,2) )
  9660. class_ = script_getnum(st,2);
  9661. else {
  9662. TBL_PC *sd;
  9663. if (!script_charid2sd(3,sd)) {
  9664. script_pushint(st,-1);
  9665. return SCRIPT_CMD_SUCCESS;
  9666. }
  9667. class_ = sd->status.class_;
  9668. }
  9669. script_pushint(st,pc_jobid2mapid(class_));
  9670. return SCRIPT_CMD_SUCCESS;
  9671. }
  9672. BUILDIN_FUNC(roclass)
  9673. {
  9674. int class_ =script_getnum(st,2);
  9675. int sex;
  9676. if( script_hasdata(st,3) )
  9677. sex = script_getnum(st,3);
  9678. else {
  9679. TBL_PC *sd;
  9680. if (st->rid && (sd=script_rid2sd(st)))
  9681. sex = sd->status.sex;
  9682. else
  9683. sex = 1; //Just use male when not found.
  9684. }
  9685. script_pushint(st,pc_mapid2jobid(class_, sex));
  9686. return SCRIPT_CMD_SUCCESS;
  9687. }
  9688. /*==========================================
  9689. * Tells client to open a hatching window, used for pet incubator
  9690. *------------------------------------------*/
  9691. BUILDIN_FUNC(birthpet)
  9692. {
  9693. TBL_PC *sd;
  9694. sd=script_rid2sd(st);
  9695. if( sd == NULL )
  9696. return SCRIPT_CMD_SUCCESS;
  9697. if( sd->status.pet_id )
  9698. {// do not send egg list, when you already have a pet
  9699. return SCRIPT_CMD_SUCCESS;
  9700. }
  9701. clif_sendegg(sd);
  9702. return SCRIPT_CMD_SUCCESS;
  9703. }
  9704. /**
  9705. * resetlvl <action type>{,<char_id>};
  9706. * @param action_type:
  9707. * 1 : make like after rebirth
  9708. * 2 : blvl,jlvl=1, skillpoint=0
  9709. * 3 : don't reset skill, blvl=1
  9710. * 4 : jlvl=0
  9711. * @author AppleGirl
  9712. **/
  9713. BUILDIN_FUNC(resetlvl)
  9714. {
  9715. TBL_PC *sd;
  9716. int type=script_getnum(st,2);
  9717. if (!script_charid2sd(3,sd))
  9718. return SCRIPT_CMD_FAILURE;
  9719. pc_resetlvl(sd,type);
  9720. return SCRIPT_CMD_SUCCESS;
  9721. }
  9722. /**
  9723. * Reset a player status point
  9724. * resetstatus({<char_id>});
  9725. **/
  9726. BUILDIN_FUNC(resetstatus)
  9727. {
  9728. TBL_PC *sd;
  9729. if (!script_charid2sd(2,sd))
  9730. return SCRIPT_CMD_FAILURE;
  9731. pc_resetstate(sd);
  9732. return SCRIPT_CMD_SUCCESS;
  9733. }
  9734. /**
  9735. * Reset player's skill
  9736. * resetskill({<char_id>});
  9737. **/
  9738. BUILDIN_FUNC(resetskill)
  9739. {
  9740. TBL_PC *sd;
  9741. if (!script_charid2sd(2,sd))
  9742. return SCRIPT_CMD_FAILURE;
  9743. pc_resetskill(sd,1);
  9744. return SCRIPT_CMD_SUCCESS;
  9745. }
  9746. /**
  9747. * Counts total amount of skill points.
  9748. * skillpointcount({<char_id>})
  9749. **/
  9750. BUILDIN_FUNC(skillpointcount)
  9751. {
  9752. TBL_PC *sd;
  9753. if (!script_charid2sd(2,sd))
  9754. return SCRIPT_CMD_FAILURE;
  9755. script_pushint(st,sd->status.skill_point + pc_resetskill(sd,2));
  9756. return SCRIPT_CMD_SUCCESS;
  9757. }
  9758. /*==========================================
  9759. *
  9760. *------------------------------------------*/
  9761. BUILDIN_FUNC(changebase)
  9762. {
  9763. TBL_PC *sd=NULL;
  9764. int vclass;
  9765. if( script_hasdata(st,3) )
  9766. sd=map_id2sd(script_getnum(st,3));
  9767. else
  9768. sd=script_rid2sd(st);
  9769. if(sd == NULL)
  9770. return SCRIPT_CMD_SUCCESS;
  9771. vclass = script_getnum(st,2);
  9772. if(vclass == JOB_WEDDING)
  9773. {
  9774. if (!battle_config.wedding_modifydisplay || //Do not show the wedding sprites
  9775. sd->class_&JOBL_BABY //Baby classes screw up when showing wedding sprites. [Skotlex] They don't seem to anymore.
  9776. )
  9777. return SCRIPT_CMD_SUCCESS;
  9778. }
  9779. if(!sd->disguise && vclass != sd->vd.class_) {
  9780. status_set_viewdata(&sd->bl, vclass);
  9781. //Updated client view. Base, Weapon and Cloth Colors.
  9782. clif_changelook(&sd->bl,LOOK_BASE,sd->vd.class_);
  9783. clif_changelook(&sd->bl,LOOK_WEAPON,sd->status.weapon);
  9784. if (sd->vd.cloth_color)
  9785. clif_changelook(&sd->bl,LOOK_CLOTHES_COLOR,sd->vd.cloth_color);
  9786. if (sd->vd.body_style)
  9787. clif_changelook(&sd->bl,LOOK_BODY2,sd->vd.body_style);
  9788. clif_skillinfoblock(sd);
  9789. }
  9790. return SCRIPT_CMD_SUCCESS;
  9791. }
  9792. /**
  9793. * Change account sex and unequip all item and request for a changesex to char-serv
  9794. * changesex({<char_id>});
  9795. */
  9796. BUILDIN_FUNC(changesex)
  9797. {
  9798. int i;
  9799. TBL_PC *sd = NULL;
  9800. if (!script_charid2sd(2,sd))
  9801. return SCRIPT_CMD_FAILURE;
  9802. pc_resetskill(sd,4);
  9803. // to avoid any problem with equipment and invalid sex, equipment is unequiped.
  9804. for(i = 0; i < EQI_MAX; i++) {
  9805. if (sd->equip_index[i] >= 0)
  9806. pc_unequipitem(sd, sd->equip_index[i], 3);
  9807. }
  9808. chrif_changesex(sd, true);
  9809. return SCRIPT_CMD_SUCCESS;
  9810. }
  9811. /**
  9812. * Change character's sex and unequip all item and request for a changesex to char-serv
  9813. * changecharsex({<char_id>});
  9814. */
  9815. BUILDIN_FUNC(changecharsex)
  9816. {
  9817. #if PACKETVER >= 20141016
  9818. int i;
  9819. TBL_PC *sd = NULL;
  9820. if (!script_charid2sd(2,sd))
  9821. return SCRIPT_CMD_FAILURE;
  9822. pc_resetskill(sd,4);
  9823. // to avoid any problem with equipment and invalid sex, equipment is unequiped.
  9824. for (i = 0; i < EQI_MAX; i++) {
  9825. if (sd->equip_index[i] >= 0)
  9826. pc_unequipitem(sd, sd->equip_index[i], 3);
  9827. }
  9828. chrif_changesex(sd, false);
  9829. return SCRIPT_CMD_SUCCESS;
  9830. #else
  9831. return SCRIPT_CMD_FAILURE;
  9832. #endif
  9833. }
  9834. /*==========================================
  9835. * Works like 'announce' but outputs in the common chat window
  9836. *------------------------------------------*/
  9837. BUILDIN_FUNC(globalmes)
  9838. {
  9839. struct block_list *bl = map_id2bl(st->oid);
  9840. struct npc_data *nd = (struct npc_data *)bl;
  9841. const char *name=NULL,*mes;
  9842. mes=script_getstr(st,2);
  9843. if(mes==NULL)
  9844. return SCRIPT_CMD_SUCCESS;
  9845. if(script_hasdata(st,3)){ // npc name to display
  9846. name=script_getstr(st,3);
  9847. } else {
  9848. name=nd->name; //use current npc name
  9849. }
  9850. npc_globalmessage(name,mes); // broadcast to all players connected
  9851. return SCRIPT_CMD_SUCCESS;
  9852. }
  9853. /////////////////////////////////////////////////////////////////////
  9854. // NPC waiting room (chat room)
  9855. //
  9856. /// Creates a waiting room (chat room) for this npc.
  9857. ///
  9858. /// waitingroom "<title>",<limit>{,"<event>"{,<trigger>{,<zeny>{,<minlvl>{,<maxlvl>}}}}};
  9859. BUILDIN_FUNC(waitingroom)
  9860. {
  9861. struct npc_data* nd;
  9862. const char* title = script_getstr(st, 2);
  9863. int limit = script_getnum(st, 3);
  9864. const char* ev = script_hasdata(st,4) ? script_getstr(st,4) : "";
  9865. int trigger = script_hasdata(st,5) ? script_getnum(st,5) : limit;
  9866. int zeny = script_hasdata(st,6) ? script_getnum(st,6) : 0;
  9867. int minLvl = script_hasdata(st,7) ? script_getnum(st,7) : 1;
  9868. int maxLvl = script_hasdata(st,8) ? script_getnum(st,8) : MAX_LEVEL;
  9869. nd = (struct npc_data *)map_id2bl(st->oid);
  9870. if( nd != NULL )
  9871. chat_createnpcchat(nd, title, limit, 1, trigger, ev, zeny, minLvl, maxLvl);
  9872. return SCRIPT_CMD_SUCCESS;
  9873. }
  9874. /// Removes the waiting room of the current or target npc.
  9875. ///
  9876. /// delwaitingroom "<npc_name>";
  9877. /// delwaitingroom;
  9878. BUILDIN_FUNC(delwaitingroom)
  9879. {
  9880. struct npc_data* nd;
  9881. if( script_hasdata(st,2) )
  9882. nd = npc_name2id(script_getstr(st, 2));
  9883. else
  9884. nd = (struct npc_data *)map_id2bl(st->oid);
  9885. if( nd != NULL )
  9886. chat_deletenpcchat(nd);
  9887. return SCRIPT_CMD_SUCCESS;
  9888. }
  9889. /// Kick the specified player from the waiting room of the target npc.
  9890. ///
  9891. /// waitingroomkick "<npc_name>", <kickusername>;
  9892. BUILDIN_FUNC(waitingroomkick)
  9893. {
  9894. struct npc_data* nd;
  9895. struct chat_data* cd;
  9896. const char* kickusername;
  9897. nd = npc_name2id(script_getstr(st,2));
  9898. kickusername = script_getstr(st,3);
  9899. if( nd != NULL && (cd=(struct chat_data *)map_id2bl(nd->chat_id)) != NULL )
  9900. chat_npckickchat(cd, kickusername);
  9901. return SCRIPT_CMD_SUCCESS;
  9902. }
  9903. /// Get Users in waiting room and stores gids in .@waitingroom_users[]
  9904. /// Num users stored in .@waitingroom_usercount
  9905. ///
  9906. /// getwaitingroomusers "<npc_name>";
  9907. BUILDIN_FUNC(getwaitingroomusers)
  9908. {
  9909. struct npc_data* nd;
  9910. struct chat_data* cd;
  9911. int i, j=0;
  9912. if( script_hasdata(st,2) )
  9913. nd = npc_name2id(script_getstr(st, 2));
  9914. else
  9915. nd = (struct npc_data *)map_id2bl(st->oid);
  9916. if( nd != NULL && (cd=(struct chat_data *)map_id2bl(nd->chat_id)) != NULL ) {
  9917. for(i = 0; i < cd->users; ++i) {
  9918. setd_sub(st, NULL, ".@waitingroom_users", j, (void *)__64BPRTSIZE(cd->usersd[i]->status.account_id), NULL);
  9919. j++;
  9920. }
  9921. setd_sub(st, NULL, ".@waitingroom_usercount", 0, (void *)__64BPRTSIZE(j), NULL);
  9922. }
  9923. return SCRIPT_CMD_SUCCESS;
  9924. }
  9925. /// Kicks all the players from the waiting room of the current or target npc.
  9926. ///
  9927. /// kickwaitingroomall "<npc_name>";
  9928. /// kickwaitingroomall;
  9929. BUILDIN_FUNC(waitingroomkickall)
  9930. {
  9931. struct npc_data* nd;
  9932. struct chat_data* cd;
  9933. if( script_hasdata(st,2) )
  9934. nd = npc_name2id(script_getstr(st,2));
  9935. else
  9936. nd = (struct npc_data *)map_id2bl(st->oid);
  9937. if( nd != NULL && (cd=(struct chat_data *)map_id2bl(nd->chat_id)) != NULL )
  9938. chat_npckickall(cd);
  9939. return SCRIPT_CMD_SUCCESS;
  9940. }
  9941. /// Enables the waiting room event of the current or target npc.
  9942. ///
  9943. /// enablewaitingroomevent "<npc_name>";
  9944. /// enablewaitingroomevent;
  9945. BUILDIN_FUNC(enablewaitingroomevent)
  9946. {
  9947. struct npc_data* nd;
  9948. struct chat_data* cd;
  9949. if( script_hasdata(st,2) )
  9950. nd = npc_name2id(script_getstr(st, 2));
  9951. else
  9952. nd = (struct npc_data *)map_id2bl(st->oid);
  9953. if( nd != NULL && (cd=(struct chat_data *)map_id2bl(nd->chat_id)) != NULL )
  9954. chat_enableevent(cd);
  9955. return SCRIPT_CMD_SUCCESS;
  9956. }
  9957. /// Disables the waiting room event of the current or target npc.
  9958. ///
  9959. /// disablewaitingroomevent "<npc_name>";
  9960. /// disablewaitingroomevent;
  9961. BUILDIN_FUNC(disablewaitingroomevent)
  9962. {
  9963. struct npc_data *nd;
  9964. struct chat_data *cd;
  9965. if( script_hasdata(st,2) )
  9966. nd = npc_name2id(script_getstr(st, 2));
  9967. else
  9968. nd = (struct npc_data *)map_id2bl(st->oid);
  9969. if( nd != NULL && (cd=(struct chat_data *)map_id2bl(nd->chat_id)) != NULL )
  9970. chat_disableevent(cd);
  9971. return SCRIPT_CMD_SUCCESS;
  9972. }
  9973. /// Returns info on the waiting room of the current or target npc.
  9974. /// Returns -1 if the type unknown
  9975. /// <type>=0 : current number of users
  9976. /// <type>=1 : maximum number of users allowed
  9977. /// <type>=2 : the number of users that trigger the event
  9978. /// <type>=3 : if the trigger is disabled
  9979. /// <type>=4 : the title of the waiting room
  9980. /// <type>=5 : the password of the waiting room
  9981. /// <type>=16 : the name of the waiting room event
  9982. /// <type>=32 : if the waiting room is full
  9983. /// <type>=33 : if there are enough users to trigger the event
  9984. ///
  9985. /// getwaitingroomstate(<type>,"<npc_name>") -> <info>
  9986. /// getwaitingroomstate(<type>) -> <info>
  9987. BUILDIN_FUNC(getwaitingroomstate)
  9988. {
  9989. struct npc_data *nd;
  9990. struct chat_data *cd;
  9991. int type;
  9992. type = script_getnum(st,2);
  9993. if( script_hasdata(st,3) )
  9994. nd = npc_name2id(script_getstr(st, 3));
  9995. else
  9996. nd = (struct npc_data *)map_id2bl(st->oid);
  9997. if( nd == NULL || (cd=(struct chat_data *)map_id2bl(nd->chat_id)) == NULL )
  9998. {
  9999. script_pushint(st, -1);
  10000. return SCRIPT_CMD_SUCCESS;
  10001. }
  10002. switch(type)
  10003. {
  10004. case 0: script_pushint(st, cd->users); break;
  10005. case 1: script_pushint(st, cd->limit); break;
  10006. case 2: script_pushint(st, cd->trigger&0x7f); break;
  10007. case 3: script_pushint(st, ((cd->trigger&0x80)!=0)); break;
  10008. case 4: script_pushstrcopy(st, cd->title); break;
  10009. case 5: script_pushstrcopy(st, cd->pass); break;
  10010. case 16: script_pushstrcopy(st, cd->npc_event);break;
  10011. case 32: script_pushint(st, (cd->users >= cd->limit)); break;
  10012. case 33: script_pushint(st, (cd->users >= cd->trigger)); break;
  10013. default: script_pushint(st, -1); break;
  10014. }
  10015. return SCRIPT_CMD_SUCCESS;
  10016. }
  10017. /// Warps the trigger or target amount of players to the target map and position.
  10018. /// Players are automatically removed from the waiting room.
  10019. /// Those waiting the longest will get warped first.
  10020. /// The target map can be "Random" for a random position in the current map,
  10021. /// and "SavePoint" for the savepoint map+position.
  10022. /// The map flag noteleport of the current map is only considered when teleporting to the savepoint.
  10023. ///
  10024. /// The id's of the teleported players are put into the array $@warpwaitingpc[]
  10025. /// The total number of teleported players is put into $@warpwaitingpcnum
  10026. ///
  10027. /// warpwaitingpc "<map name>",<x>,<y>,<number of players>;
  10028. /// warpwaitingpc "<map name>",<x>,<y>;
  10029. BUILDIN_FUNC(warpwaitingpc)
  10030. {
  10031. int x;
  10032. int y;
  10033. int i;
  10034. int n;
  10035. const char* map_name;
  10036. struct npc_data* nd;
  10037. struct chat_data* cd;
  10038. nd = (struct npc_data *)map_id2bl(st->oid);
  10039. if( nd == NULL || (cd=(struct chat_data *)map_id2bl(nd->chat_id)) == NULL )
  10040. return SCRIPT_CMD_SUCCESS;
  10041. map_name = script_getstr(st,2);
  10042. x = script_getnum(st,3);
  10043. y = script_getnum(st,4);
  10044. n = cd->trigger&0x7f;
  10045. if( script_hasdata(st,5) )
  10046. n = script_getnum(st,5);
  10047. for( i = 0; i < n && cd->users > 0; i++ )
  10048. {
  10049. TBL_PC* sd = cd->usersd[0];
  10050. if( strcmp(map_name,"SavePoint") == 0 && map[sd->bl.m].flag.noteleport )
  10051. {// can't teleport on this map
  10052. break;
  10053. }
  10054. if( cd->zeny )
  10055. {// fee set
  10056. if( (uint32)sd->status.zeny < cd->zeny )
  10057. {// no zeny to cover set fee
  10058. break;
  10059. }
  10060. pc_payzeny(sd, cd->zeny, LOG_TYPE_NPC, NULL);
  10061. }
  10062. mapreg_setreg(reference_uid(add_str("$@warpwaitingpc"), i), sd->bl.id);
  10063. if( strcmp(map_name,"Random") == 0 )
  10064. pc_randomwarp(sd,CLR_TELEPORT);
  10065. else if( strcmp(map_name,"SavePoint") == 0 )
  10066. pc_setpos(sd, sd->status.save_point.map, sd->status.save_point.x, sd->status.save_point.y, CLR_TELEPORT);
  10067. else
  10068. pc_setpos(sd, mapindex_name2id(map_name), x, y, CLR_OUTSIGHT);
  10069. }
  10070. mapreg_setreg(add_str("$@warpwaitingpcnum"), i);
  10071. return SCRIPT_CMD_SUCCESS;
  10072. }
  10073. /////////////////////////////////////////////////////////////////////
  10074. // ...
  10075. //
  10076. /// Detaches a character from a script.
  10077. ///
  10078. /// @param st Script state to detach the character from.
  10079. static void script_detach_rid(struct script_state* st)
  10080. {
  10081. if(st->rid)
  10082. {
  10083. script_detach_state(st, false);
  10084. st->rid = 0;
  10085. }
  10086. }
  10087. /*=========================================================================
  10088. * Attaches a set of RIDs to the current script. [digitalhamster]
  10089. * addrid(<type>{,<flag>{,<parameters>}});
  10090. * <type>:
  10091. * 0 : All players in the server.
  10092. * 1 : All players in the map of the invoking player, or the invoking NPC if no player is attached.
  10093. * 2 : Party members of a specified party ID.
  10094. * [ Parameters: <party id> ]
  10095. * 3 : Guild members of a specified guild ID.
  10096. * [ Parameters: <guild id> ]
  10097. * 4 : All players in a specified area of the map of the invoking player (or NPC).
  10098. * [ Parameters: <x0>,<y0>,<x1>,<y1> ]
  10099. * Account ID: The specified account ID.
  10100. * <flag>:
  10101. * 0 : Players are always attached. (default)
  10102. * 1 : Players currently running another script will not be attached.
  10103. *-------------------------------------------------------------------------*/
  10104. static int buildin_addrid_sub(struct block_list *bl,va_list ap)
  10105. {
  10106. int forceflag;
  10107. struct map_session_data *sd = (TBL_PC *)bl;
  10108. struct script_state* st;
  10109. st = va_arg(ap,struct script_state*);
  10110. forceflag = va_arg(ap,int);
  10111. if(!forceflag || !sd->st)
  10112. if(sd->status.account_id != st->rid)
  10113. run_script(st->script,st->pos,sd->status.account_id,st->oid);
  10114. return 0;
  10115. }
  10116. BUILDIN_FUNC(addrid)
  10117. {
  10118. struct s_mapiterator* iter;
  10119. struct block_list *bl;
  10120. TBL_PC *sd;
  10121. if(st->rid < 1) {
  10122. st->state = END;
  10123. bl = map_id2bl(st->oid);
  10124. } else
  10125. bl = map_id2bl(st->rid); //if run without rid it'd error,also oid if npc, else rid for map
  10126. iter = mapit_getallusers();
  10127. switch(script_getnum(st,2)) {
  10128. case 0:
  10129. for( sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); sd = (TBL_PC*)mapit_next(iter)) {
  10130. if(!script_getnum(st,3) || !sd->st)
  10131. if(sd->status.account_id != st->rid) //attached player already runs.
  10132. run_script(st->script,st->pos,sd->status.account_id,st->oid);
  10133. }
  10134. break;
  10135. case 1:
  10136. for( sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); sd = (TBL_PC*)mapit_next(iter)) {
  10137. if(!script_getnum(st,3) || !sd->st)
  10138. if((sd->bl.m == bl->m) && (sd->status.account_id != st->rid))
  10139. run_script(st->script,st->pos,sd->status.account_id,st->oid);
  10140. }
  10141. break;
  10142. case 2:
  10143. if(script_getnum(st,4) == 0) {
  10144. script_pushint(st,0);
  10145. return SCRIPT_CMD_SUCCESS;
  10146. }
  10147. for( sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); sd = (TBL_PC*)mapit_next(iter)) {
  10148. if(!script_getnum(st,3) || !sd->st)
  10149. if((sd->status.account_id != st->rid) && (sd->status.party_id == script_getnum(st,4))) //attached player already runs.
  10150. run_script(st->script,st->pos,sd->status.account_id,st->oid);
  10151. }
  10152. break;
  10153. case 3:
  10154. if(script_getnum(st,4) == 0) {
  10155. script_pushint(st,0);
  10156. return SCRIPT_CMD_SUCCESS;
  10157. }
  10158. for( sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); sd = (TBL_PC*)mapit_next(iter)) {
  10159. if(!script_getnum(st,3) || !sd->st)
  10160. if((sd->status.account_id != st->rid) && (sd->status.guild_id == script_getnum(st,4))) //attached player already runs.
  10161. run_script(st->script,st->pos,sd->status.account_id,st->oid);
  10162. }
  10163. break;
  10164. case 4:
  10165. map_foreachinarea(buildin_addrid_sub,
  10166. bl->m,script_getnum(st,4),script_getnum(st,5),script_getnum(st,6),script_getnum(st,7),BL_PC,
  10167. st,script_getnum(st,3));//4-x0 , 5-y0 , 6-x1, 7-y1
  10168. break;
  10169. default:
  10170. if((map_id2sd(script_getnum(st,2))) == NULL) { // Player not found.
  10171. script_pushint(st,0);
  10172. return SCRIPT_CMD_SUCCESS;
  10173. }
  10174. if(!script_getnum(st,3) || !map_id2sd(script_getnum(st,2))->st) {
  10175. run_script(st->script,st->pos,script_getnum(st,2),st->oid);
  10176. script_pushint(st,1);
  10177. }
  10178. return SCRIPT_CMD_SUCCESS;
  10179. }
  10180. mapit_free(iter);
  10181. script_pushint(st,1);
  10182. return SCRIPT_CMD_SUCCESS;
  10183. }
  10184. /*==========================================
  10185. * Attach sd char id to script and detach current one if any
  10186. *------------------------------------------*/
  10187. BUILDIN_FUNC(attachrid)
  10188. {
  10189. int rid = script_getnum(st,2);
  10190. if (map_id2sd(rid) != NULL) {
  10191. script_detach_rid(st);
  10192. st->rid = rid;
  10193. script_attach_state(st);
  10194. script_pushint(st,1);
  10195. } else
  10196. script_pushint(st,0);
  10197. return SCRIPT_CMD_SUCCESS;
  10198. }
  10199. /*==========================================
  10200. * Detach script to rid
  10201. *------------------------------------------*/
  10202. BUILDIN_FUNC(detachrid)
  10203. {
  10204. script_detach_rid(st);
  10205. return SCRIPT_CMD_SUCCESS;
  10206. }
  10207. /*==========================================
  10208. * Chk if account connected, (and charid from account if specified)
  10209. *------------------------------------------*/
  10210. BUILDIN_FUNC(isloggedin)
  10211. {
  10212. TBL_PC* sd = map_id2sd(script_getnum(st,2));
  10213. if (script_hasdata(st,3) && sd &&
  10214. sd->status.char_id != script_getnum(st,3))
  10215. sd = NULL;
  10216. push_val(st->stack,C_INT,sd!=NULL);
  10217. return SCRIPT_CMD_SUCCESS;
  10218. }
  10219. /*==========================================
  10220. *
  10221. *------------------------------------------*/
  10222. BUILDIN_FUNC(setmapflagnosave)
  10223. {
  10224. int16 m,x,y;
  10225. unsigned short mapindex;
  10226. const char *str,*str2;
  10227. str=script_getstr(st,2);
  10228. str2=script_getstr(st,3);
  10229. x=script_getnum(st,4);
  10230. y=script_getnum(st,5);
  10231. m = map_mapname2mapid(str);
  10232. mapindex = mapindex_name2id(str2);
  10233. if(m >= 0 && mapindex) {
  10234. map[m].flag.nosave=1;
  10235. map[m].save.map=mapindex;
  10236. map[m].save.x=x;
  10237. map[m].save.y=y;
  10238. }
  10239. return SCRIPT_CMD_SUCCESS;
  10240. }
  10241. BUILDIN_FUNC(getmapflag)
  10242. {
  10243. int16 m,i;
  10244. const char *str;
  10245. str=script_getstr(st,2);
  10246. i=script_getnum(st,3);
  10247. m = map_mapname2mapid(str);
  10248. if(m >= 0) {
  10249. switch(i) {
  10250. case MF_NOMEMO: script_pushint(st,map[m].flag.nomemo); break;
  10251. case MF_NOTELEPORT: script_pushint(st,map[m].flag.noteleport); break;
  10252. case MF_NOSAVE: script_pushint(st,map[m].flag.nosave); break;
  10253. case MF_NOBRANCH: script_pushint(st,map[m].flag.nobranch); break;
  10254. case MF_NOPENALTY: script_pushint(st,map[m].flag.noexppenalty); break;
  10255. case MF_NOZENYPENALTY: script_pushint(st,map[m].flag.nozenypenalty); break;
  10256. case MF_PVP: script_pushint(st,map[m].flag.pvp); break;
  10257. case MF_PVP_NOPARTY: script_pushint(st,map[m].flag.pvp_noparty); break;
  10258. case MF_PVP_NOGUILD: script_pushint(st,map[m].flag.pvp_noguild); break;
  10259. case MF_GVG: script_pushint(st,map[m].flag.gvg); break;
  10260. case MF_GVG_NOPARTY: script_pushint(st,map[m].flag.gvg_noparty); break;
  10261. case MF_NOTRADE: script_pushint(st,map[m].flag.notrade); break;
  10262. case MF_NOSKILL: script_pushint(st,map[m].flag.noskill); break;
  10263. case MF_NOWARP: script_pushint(st,map[m].flag.nowarp); break;
  10264. case MF_PARTYLOCK: script_pushint(st,map[m].flag.partylock); break;
  10265. case MF_NOICEWALL: script_pushint(st,map[m].flag.noicewall); break;
  10266. case MF_SNOW: script_pushint(st,map[m].flag.snow); break;
  10267. case MF_FOG: script_pushint(st,map[m].flag.fog); break;
  10268. case MF_SAKURA: script_pushint(st,map[m].flag.sakura); break;
  10269. case MF_LEAVES: script_pushint(st,map[m].flag.leaves); break;
  10270. case MF_NOGO: script_pushint(st,map[m].flag.nogo); break;
  10271. case MF_CLOUDS: script_pushint(st,map[m].flag.clouds); break;
  10272. case MF_CLOUDS2: script_pushint(st,map[m].flag.clouds2); break;
  10273. case MF_FIREWORKS: script_pushint(st,map[m].flag.fireworks); break;
  10274. case MF_GVG_CASTLE: script_pushint(st,map[m].flag.gvg_castle); break;
  10275. case MF_GVG_DUNGEON: script_pushint(st,map[m].flag.gvg_dungeon); break;
  10276. case MF_NIGHTENABLED: script_pushint(st,map[m].flag.nightenabled); break;
  10277. case MF_NOBASEEXP: script_pushint(st,map[m].flag.nobaseexp); break;
  10278. case MF_NOJOBEXP: script_pushint(st,map[m].flag.nojobexp); break;
  10279. case MF_NOMOBLOOT: script_pushint(st,map[m].flag.nomobloot); break;
  10280. case MF_NOMVPLOOT: script_pushint(st,map[m].flag.nomvploot); break;
  10281. case MF_NORETURN: script_pushint(st,map[m].flag.noreturn); break;
  10282. case MF_NOWARPTO: script_pushint(st,map[m].flag.nowarpto); break;
  10283. case MF_NIGHTMAREDROP: script_pushint(st,map[m].flag.pvp_nightmaredrop); break;
  10284. case MF_RESTRICTED: script_pushint(st,map[m].flag.restricted); break;
  10285. case MF_NOCOMMAND: script_pushint(st,map[m].nocommand); break;
  10286. case MF_NODROP: script_pushint(st,map[m].flag.nodrop); break;
  10287. case MF_JEXP: script_pushint(st,map[m].adjust.jexp); break;
  10288. case MF_BEXP: script_pushint(st,map[m].adjust.bexp); break;
  10289. case MF_NOVENDING: script_pushint(st,map[m].flag.novending); break;
  10290. case MF_LOADEVENT: script_pushint(st,map[m].flag.loadevent); break;
  10291. case MF_NOCHAT: script_pushint(st,map[m].flag.nochat); break;
  10292. case MF_NOEXPPENALTY: script_pushint(st,map[m].flag.noexppenalty ); break;
  10293. case MF_GUILDLOCK: script_pushint(st,map[m].flag.guildlock); break;
  10294. case MF_TOWN: script_pushint(st,map[m].flag.town); break;
  10295. case MF_AUTOTRADE: script_pushint(st,map[m].flag.autotrade); break;
  10296. case MF_ALLOWKS: script_pushint(st,map[m].flag.allowks); break;
  10297. case MF_MONSTER_NOTELEPORT: script_pushint(st,map[m].flag.monster_noteleport); break;
  10298. case MF_PVP_NOCALCRANK: script_pushint(st,map[m].flag.pvp_nocalcrank); break;
  10299. case MF_BATTLEGROUND: script_pushint(st,map[m].flag.battleground); break;
  10300. case MF_RESET: script_pushint(st,map[m].flag.reset); break;
  10301. case MF_CHANNELAUTOJOIN: script_pushint(st,map[m].flag.chmautojoin); break;
  10302. case MF_NOUSECART: script_pushint(st,map[m].flag.nousecart); break;
  10303. case MF_NOITEMCONSUMPTION: script_pushint(st,map[m].flag.noitemconsumption); break;
  10304. case MF_SUMSTARTMIRACLE: script_pushint(st,map[m].flag.nosumstarmiracle); break;
  10305. case MF_NOMINEEFFECT: script_pushint(st,map[m].flag.nomineeffect); break;
  10306. case MF_NOLOCKON: script_pushint(st,map[m].flag.nolockon); break;
  10307. case MF_NOTOMB: script_pushint(st,map[m].flag.notomb); break;
  10308. #ifdef ADJUST_SKILL_DAMAGE
  10309. case MF_SKILL_DAMAGE:
  10310. {
  10311. int ret_val=0, type=0;
  10312. FETCH(4,type);
  10313. switch (type) {
  10314. case 1: ret_val = map[m].adjust.damage.pc; break;
  10315. case 2: ret_val = map[m].adjust.damage.mob; break;
  10316. case 3: ret_val = map[m].adjust.damage.boss; break;
  10317. case 4: ret_val = map[m].adjust.damage.other; break;
  10318. case 5: ret_val = map[m].adjust.damage.caster; break;
  10319. default: ret_val = map[m].flag.skill_damage; break;
  10320. }
  10321. script_pushint(st,ret_val); break;
  10322. } break;
  10323. #endif
  10324. }
  10325. }
  10326. return SCRIPT_CMD_SUCCESS;
  10327. }
  10328. /* pvp timer handling */
  10329. static int script_mapflag_pvp_sub(struct block_list *bl,va_list ap) {
  10330. TBL_PC* sd = (TBL_PC*)bl;
  10331. if (sd->pvp_timer == INVALID_TIMER) {
  10332. sd->pvp_timer = add_timer(gettick() + 200, pc_calc_pvprank_timer, sd->bl.id, 0);
  10333. sd->pvp_rank = 0;
  10334. sd->pvp_lastusers = 0;
  10335. sd->pvp_point = 5;
  10336. sd->pvp_won = 0;
  10337. sd->pvp_lost = 0;
  10338. }
  10339. clif_map_property(sd, MAPPROPERTY_FREEPVPZONE);
  10340. clif_maptypeproperty2(&sd->bl,SELF);
  10341. return 0;
  10342. }
  10343. BUILDIN_FUNC(setmapflag)
  10344. {
  10345. int16 m,i;
  10346. const char *str;
  10347. int val=0;
  10348. str=script_getstr(st,2);
  10349. i=script_getnum(st,3);
  10350. FETCH(4,val);
  10351. m = map_mapname2mapid(str);
  10352. if(m >= 0) {
  10353. switch(i) {
  10354. case MF_NOMEMO: map[m].flag.nomemo = 1; break;
  10355. case MF_NOTELEPORT: map[m].flag.noteleport = 1; break;
  10356. case MF_NOSAVE: map[m].flag.nosave = 1; break;
  10357. case MF_NOBRANCH: map[m].flag.nobranch = 1; break;
  10358. case MF_NOPENALTY: map[m].flag.noexppenalty = 1; map[m].flag.nozenypenalty = 1; break;
  10359. case MF_NOZENYPENALTY: map[m].flag.nozenypenalty = 1; break;
  10360. case MF_PVP:
  10361. map[m].flag.pvp = 1;
  10362. if( !battle_config.pk_mode ) {
  10363. map_foreachinmap(script_mapflag_pvp_sub,m,BL_PC);
  10364. }
  10365. break;
  10366. case MF_PVP_NOPARTY: map[m].flag.pvp_noparty = 1; break;
  10367. case MF_PVP_NOGUILD: map[m].flag.pvp_noguild = 1; break;
  10368. case MF_GVG: {
  10369. struct block_list bl;
  10370. map[m].flag.gvg = 1;
  10371. clif_map_property_mapall(m, MAPPROPERTY_AGITZONE);
  10372. bl.type = BL_NUL;
  10373. bl.m = m;
  10374. clif_maptypeproperty2(&bl,ALL_SAMEMAP);
  10375. }
  10376. break;
  10377. case MF_GVG_NOPARTY: map[m].flag.gvg_noparty = 1; break;
  10378. case MF_NOTRADE: map[m].flag.notrade = 1; break;
  10379. case MF_NOSKILL: map[m].flag.noskill = 1; break;
  10380. case MF_NOWARP: map[m].flag.nowarp = 1; break;
  10381. case MF_PARTYLOCK: map[m].flag.partylock = 1; break;
  10382. case MF_NOICEWALL: map[m].flag.noicewall = 1; break;
  10383. case MF_SNOW: map[m].flag.snow = 1; break;
  10384. case MF_FOG: map[m].flag.fog = 1; break;
  10385. case MF_SAKURA: map[m].flag.sakura = 1; break;
  10386. case MF_LEAVES: map[m].flag.leaves = 1; break;
  10387. case MF_NOGO: map[m].flag.nogo = 1; break;
  10388. case MF_CLOUDS: map[m].flag.clouds = 1; break;
  10389. case MF_CLOUDS2: map[m].flag.clouds2 = 1; break;
  10390. case MF_FIREWORKS: map[m].flag.fireworks = 1; break;
  10391. case MF_GVG_CASTLE: map[m].flag.gvg_castle = 1; break;
  10392. case MF_GVG_DUNGEON: map[m].flag.gvg_dungeon = 1; break;
  10393. case MF_NIGHTENABLED: map[m].flag.nightenabled = 1; break;
  10394. case MF_NOBASEEXP: map[m].flag.nobaseexp = 1; break;
  10395. case MF_NOJOBEXP: map[m].flag.nojobexp = 1; break;
  10396. case MF_NOMOBLOOT: map[m].flag.nomobloot = 1; break;
  10397. case MF_NOMVPLOOT: map[m].flag.nomvploot = 1; break;
  10398. case MF_NORETURN: map[m].flag.noreturn = 1; break;
  10399. case MF_NOWARPTO: map[m].flag.nowarpto = 1; break;
  10400. case MF_NIGHTMAREDROP: map[m].flag.pvp_nightmaredrop = 1; break;
  10401. case MF_RESTRICTED:
  10402. map[m].zone |= 1<<(val+1);
  10403. map[m].flag.restricted=1;
  10404. break;
  10405. case MF_NOCOMMAND: map[m].nocommand = (val <= 0) ? 100 : val; break;
  10406. case MF_NODROP: map[m].flag.nodrop = 1; break;
  10407. case MF_JEXP: map[m].adjust.jexp = (val <= 0) ? 100 : val; break;
  10408. case MF_BEXP: map[m].adjust.bexp = (val <= 0) ? 100 : val; break;
  10409. case MF_NOVENDING: map[m].flag.novending = 1; break;
  10410. case MF_LOADEVENT: map[m].flag.loadevent = 1; break;
  10411. case MF_NOCHAT: map[m].flag.nochat = 1; break;
  10412. case MF_NOEXPPENALTY: map[m].flag.noexppenalty = 1; break;
  10413. case MF_GUILDLOCK: map[m].flag.guildlock = 1; break;
  10414. case MF_TOWN: map[m].flag.town = 1; break;
  10415. case MF_AUTOTRADE: map[m].flag.autotrade = 1; break;
  10416. case MF_ALLOWKS: map[m].flag.allowks = 1; break;
  10417. case MF_MONSTER_NOTELEPORT: map[m].flag.monster_noteleport = 1; break;
  10418. case MF_PVP_NOCALCRANK: map[m].flag.pvp_nocalcrank = 1; break;
  10419. case MF_BATTLEGROUND: map[m].flag.battleground = (val <= 0 || val > 2) ? 1 : val; break;
  10420. case MF_RESET: map[m].flag.reset = 1; break;
  10421. case MF_CHANNELAUTOJOIN: map[m].flag.chmautojoin = 1 ; break;
  10422. case MF_NOUSECART: map[m].flag.nousecart = 1 ; break;
  10423. case MF_NOITEMCONSUMPTION: map[m].flag.noitemconsumption = 1 ; break;
  10424. case MF_SUMSTARTMIRACLE: map[m].flag.nosumstarmiracle = 1 ; break;
  10425. case MF_NOMINEEFFECT: map[m].flag.nomineeffect = 1 ; break;
  10426. case MF_NOLOCKON: map[m].flag.nolockon = 1 ; break;
  10427. case MF_NOTOMB: map[m].flag.notomb = 1; break;
  10428. #ifdef ADJUST_SKILL_DAMAGE
  10429. case MF_SKILL_DAMAGE:
  10430. {
  10431. int type=0;
  10432. FETCH(5,type);
  10433. switch (type) {
  10434. case 1: map[m].adjust.damage.pc = val; break;
  10435. case 2: map[m].adjust.damage.mob = val; break;
  10436. case 3: map[m].adjust.damage.boss = val; break;
  10437. case 4: map[m].adjust.damage.other = val; break;
  10438. case 5: map[m].adjust.damage.caster = val; break;
  10439. }
  10440. map[m].flag.skill_damage = 1;
  10441. } break;
  10442. #endif
  10443. }
  10444. }
  10445. return SCRIPT_CMD_SUCCESS;
  10446. }
  10447. BUILDIN_FUNC(removemapflag)
  10448. {
  10449. int16 m,i;
  10450. const char *str;
  10451. int val=0;
  10452. str=script_getstr(st,2);
  10453. i=script_getnum(st,3);
  10454. FETCH(4,val);
  10455. m = map_mapname2mapid(str);
  10456. if(m >= 0) {
  10457. switch(i) {
  10458. case MF_NOMEMO: map[m].flag.nomemo = 0; break;
  10459. case MF_NOTELEPORT: map[m].flag.noteleport = 0; break;
  10460. case MF_NOSAVE: map[m].flag.nosave = 0; break;
  10461. case MF_NOBRANCH: map[m].flag.nobranch = 0; break;
  10462. case MF_NOPENALTY: map[m].flag.noexppenalty = 0; map[m].flag.nozenypenalty = 0; break;
  10463. case MF_NOZENYPENALTY: map[m].flag.nozenypenalty = 0; break;
  10464. case MF_PVP: {
  10465. struct block_list bl;
  10466. bl.type = BL_NUL;
  10467. bl.m = m;
  10468. map[m].flag.pvp = 0;
  10469. clif_map_property_mapall(m, MAPPROPERTY_NOTHING);
  10470. clif_maptypeproperty2(&bl,ALL_SAMEMAP);
  10471. }
  10472. break;
  10473. case MF_PVP_NOPARTY: map[m].flag.pvp_noparty = 0; break;
  10474. case MF_PVP_NOGUILD: map[m].flag.pvp_noguild = 0; break;
  10475. case MF_GVG: {
  10476. struct block_list bl;
  10477. bl.type = BL_NUL;
  10478. bl.m = m;
  10479. map[m].flag.gvg = 0;
  10480. clif_map_property_mapall(m, MAPPROPERTY_NOTHING);
  10481. clif_maptypeproperty2(&bl,ALL_SAMEMAP);
  10482. }
  10483. break;
  10484. case MF_GVG_NOPARTY: map[m].flag.gvg_noparty = 0; break;
  10485. case MF_NOTRADE: map[m].flag.notrade = 0; break;
  10486. case MF_NOSKILL: map[m].flag.noskill = 0; break;
  10487. case MF_NOWARP: map[m].flag.nowarp = 0; break;
  10488. case MF_PARTYLOCK: map[m].flag.partylock = 0; break;
  10489. case MF_NOICEWALL: map[m].flag.noicewall = 0; break;
  10490. case MF_SNOW: map[m].flag.snow = 0; break;
  10491. case MF_FOG: map[m].flag.fog = 0; break;
  10492. case MF_SAKURA: map[m].flag.sakura = 0; break;
  10493. case MF_LEAVES: map[m].flag.leaves = 0; break;
  10494. case MF_NOGO: map[m].flag.nogo = 0; break;
  10495. case MF_CLOUDS: map[m].flag.clouds = 0; break;
  10496. case MF_CLOUDS2: map[m].flag.clouds2 = 0; break;
  10497. case MF_FIREWORKS: map[m].flag.fireworks = 0; break;
  10498. case MF_GVG_CASTLE: map[m].flag.gvg_castle = 0; break;
  10499. case MF_GVG_DUNGEON: map[m].flag.gvg_dungeon = 0; break;
  10500. case MF_NIGHTENABLED: map[m].flag.nightenabled = 0; break;
  10501. case MF_NOBASEEXP: map[m].flag.nobaseexp = 0; break;
  10502. case MF_NOJOBEXP: map[m].flag.nojobexp = 0; break;
  10503. case MF_NOMOBLOOT: map[m].flag.nomobloot = 0; break;
  10504. case MF_NOMVPLOOT: map[m].flag.nomvploot = 0; break;
  10505. case MF_NORETURN: map[m].flag.noreturn = 0; break;
  10506. case MF_NOWARPTO: map[m].flag.nowarpto = 0; break;
  10507. case MF_NIGHTMAREDROP: map[m].flag.pvp_nightmaredrop = 0; break;
  10508. case MF_RESTRICTED:
  10509. map[m].zone ^= 1<<(val+1);
  10510. if (map[m].zone == 0){
  10511. map[m].flag.restricted=0;
  10512. }
  10513. break;
  10514. case MF_NOCOMMAND: map[m].nocommand = 0; break;
  10515. case MF_NODROP: map[m].flag.nodrop = 0; break;
  10516. case MF_JEXP: map[m].adjust.jexp = 0; break;
  10517. case MF_BEXP: map[m].adjust.bexp = 0; break;
  10518. case MF_NOVENDING: map[m].flag.novending = 0; break;
  10519. case MF_LOADEVENT: map[m].flag.loadevent = 0; break;
  10520. case MF_NOCHAT: map[m].flag.nochat = 0; break;
  10521. case MF_NOEXPPENALTY: map[m].flag.noexppenalty = 0; break;
  10522. case MF_GUILDLOCK: map[m].flag.guildlock = 0; break;
  10523. case MF_TOWN: map[m].flag.town = 0; break;
  10524. case MF_AUTOTRADE: map[m].flag.autotrade = 0; break;
  10525. case MF_ALLOWKS: map[m].flag.allowks = 0; break;
  10526. case MF_MONSTER_NOTELEPORT: map[m].flag.monster_noteleport = 0; break;
  10527. case MF_PVP_NOCALCRANK: map[m].flag.pvp_nocalcrank = 0; break;
  10528. case MF_BATTLEGROUND: map[m].flag.battleground = 0; break;
  10529. case MF_RESET: map[m].flag.reset = 0; break;
  10530. case MF_CHANNELAUTOJOIN: map[m].flag.chmautojoin = 0 ; break;
  10531. case MF_NOUSECART: map[m].flag.nousecart = 0 ; break;
  10532. case MF_NOITEMCONSUMPTION: map[m].flag.noitemconsumption = 0 ; break;
  10533. case MF_SUMSTARTMIRACLE: map[m].flag.nosumstarmiracle = 0 ; break;
  10534. case MF_NOMINEEFFECT: map[m].flag.nomineeffect = 0 ; break;
  10535. case MF_NOLOCKON: map[m].flag.nolockon = 0 ; break;
  10536. case MF_NOTOMB: map[m].flag.notomb = 0; break;
  10537. #ifdef ADJUST_SKILL_DAMAGE
  10538. case MF_SKILL_DAMAGE:
  10539. {
  10540. map[m].flag.skill_damage = 0;
  10541. memset(&map[m].adjust.damage, 0, sizeof(map[m].adjust.damage));
  10542. if (map[m].skill_damage.count)
  10543. map_skill_damage_free(&map[m]);
  10544. } break;
  10545. #endif
  10546. }
  10547. }
  10548. return SCRIPT_CMD_SUCCESS;
  10549. }
  10550. BUILDIN_FUNC(pvpon)
  10551. {
  10552. int16 m;
  10553. const char *str;
  10554. TBL_PC* sd = NULL;
  10555. struct s_mapiterator* iter;
  10556. struct block_list bl;
  10557. str = script_getstr(st,2);
  10558. m = map_mapname2mapid(str);
  10559. if( m < 0 || map[m].flag.pvp )
  10560. return SCRIPT_CMD_SUCCESS; // nothing to do
  10561. map[m].flag.pvp = 1;
  10562. clif_map_property_mapall(m, MAPPROPERTY_FREEPVPZONE);
  10563. bl.type = BL_NUL;
  10564. bl.m = m;
  10565. clif_maptypeproperty2(&bl,ALL_SAMEMAP);
  10566. if(battle_config.pk_mode) // disable ranking functions if pk_mode is on [Valaris]
  10567. return SCRIPT_CMD_SUCCESS;
  10568. iter = mapit_getallusers();
  10569. for( sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); sd = (TBL_PC*)mapit_next(iter) )
  10570. {
  10571. if( sd->bl.m != m || sd->pvp_timer != INVALID_TIMER )
  10572. continue; // not applicable
  10573. sd->pvp_timer = add_timer(gettick()+200,pc_calc_pvprank_timer,sd->bl.id,0);
  10574. sd->pvp_rank = 0;
  10575. sd->pvp_lastusers = 0;
  10576. sd->pvp_point = 5;
  10577. sd->pvp_won = 0;
  10578. sd->pvp_lost = 0;
  10579. }
  10580. mapit_free(iter);
  10581. return SCRIPT_CMD_SUCCESS;
  10582. }
  10583. static int buildin_pvpoff_sub(struct block_list *bl,va_list ap)
  10584. {
  10585. TBL_PC* sd = (TBL_PC*)bl;
  10586. clif_pvpset(sd, 0, 0, 2);
  10587. if (sd->pvp_timer != INVALID_TIMER) {
  10588. delete_timer(sd->pvp_timer, pc_calc_pvprank_timer);
  10589. sd->pvp_timer = INVALID_TIMER;
  10590. }
  10591. return 0;
  10592. }
  10593. BUILDIN_FUNC(pvpoff)
  10594. {
  10595. int16 m;
  10596. const char *str;
  10597. struct block_list bl;
  10598. str=script_getstr(st,2);
  10599. m = map_mapname2mapid(str);
  10600. if(m < 0 || !map[m].flag.pvp)
  10601. return SCRIPT_CMD_SUCCESS; //fixed Lupus
  10602. map[m].flag.pvp = 0;
  10603. clif_map_property_mapall(m, MAPPROPERTY_NOTHING);
  10604. bl.type = BL_NUL;
  10605. bl.m = m;
  10606. clif_maptypeproperty2(&bl,ALL_SAMEMAP);
  10607. if(battle_config.pk_mode) // disable ranking options if pk_mode is on [Valaris]
  10608. return SCRIPT_CMD_SUCCESS;
  10609. map_foreachinmap(buildin_pvpoff_sub, m, BL_PC);
  10610. return SCRIPT_CMD_SUCCESS;
  10611. }
  10612. BUILDIN_FUNC(gvgon)
  10613. {
  10614. int16 m;
  10615. const char *str;
  10616. struct block_list bl;
  10617. str=script_getstr(st,2);
  10618. m = map_mapname2mapid(str);
  10619. if(m >= 0 && !map[m].flag.gvg) {
  10620. map[m].flag.gvg = 1;
  10621. clif_map_property_mapall(m, MAPPROPERTY_AGITZONE);
  10622. bl.type = BL_NUL;
  10623. bl.m = m;
  10624. clif_maptypeproperty2(&bl,ALL_SAMEMAP);
  10625. }
  10626. return SCRIPT_CMD_SUCCESS;
  10627. }
  10628. BUILDIN_FUNC(gvgoff)
  10629. {
  10630. int16 m;
  10631. const char *str;
  10632. str=script_getstr(st,2);
  10633. m = map_mapname2mapid(str);
  10634. if(m >= 0 && map[m].flag.gvg) {
  10635. struct block_list bl;
  10636. map[m].flag.gvg = 0;
  10637. clif_map_property_mapall(m, MAPPROPERTY_NOTHING);
  10638. bl.type = BL_NUL;
  10639. bl.m = m;
  10640. clif_maptypeproperty2(&bl,ALL_SAMEMAP);
  10641. }
  10642. return SCRIPT_CMD_SUCCESS;
  10643. }
  10644. /*==========================================
  10645. * Shows an emoticon on top of the player/npc
  10646. * emotion emotion#, <target: 0 - NPC, 1 - PC>, <NPC/PC name>
  10647. *------------------------------------------*/
  10648. //Optional second parameter added by [Skotlex]
  10649. BUILDIN_FUNC(emotion)
  10650. {
  10651. int type;
  10652. int player=0;
  10653. type=script_getnum(st,2);
  10654. if(type < 0 || type > 100)
  10655. return SCRIPT_CMD_SUCCESS;
  10656. if( script_hasdata(st,3) )
  10657. player=script_getnum(st,3);
  10658. if (player) {
  10659. TBL_PC *sd = NULL;
  10660. if( script_hasdata(st,4) )
  10661. sd = map_nick2sd(script_getstr(st,4));
  10662. else
  10663. sd = script_rid2sd(st);
  10664. if (sd)
  10665. clif_emotion(&sd->bl,type);
  10666. } else
  10667. if( script_hasdata(st,4) )
  10668. {
  10669. TBL_NPC *nd = npc_name2id(script_getstr(st,4));
  10670. if(nd)
  10671. clif_emotion(&nd->bl,type);
  10672. }
  10673. else
  10674. clif_emotion(map_id2bl(st->oid),type);
  10675. return SCRIPT_CMD_SUCCESS;
  10676. }
  10677. static int buildin_maprespawnguildid_sub_pc(struct map_session_data* sd, va_list ap)
  10678. {
  10679. int16 m=va_arg(ap,int);
  10680. int g_id=va_arg(ap,int);
  10681. int flag=va_arg(ap,int);
  10682. if(!sd || sd->bl.m != m)
  10683. return 0;
  10684. if(
  10685. (sd->status.guild_id == g_id && flag&1) || //Warp out owners
  10686. (sd->status.guild_id != g_id && flag&2) || //Warp out outsiders
  10687. (sd->status.guild_id == 0 && flag&2) // Warp out players not in guild
  10688. )
  10689. pc_setpos(sd,sd->status.save_point.map,sd->status.save_point.x,sd->status.save_point.y,CLR_TELEPORT);
  10690. return 1;
  10691. }
  10692. static int buildin_maprespawnguildid_sub_mob(struct block_list *bl,va_list ap)
  10693. {
  10694. struct mob_data *md=(struct mob_data *)bl;
  10695. if(!md->guardian_data && md->mob_id != MOBID_EMPERIUM)
  10696. status_kill(bl);
  10697. return 0;
  10698. }
  10699. /*
  10700. * Function to kick guild members out of a map and to their save points.
  10701. * m : mapid
  10702. * g_id : owner guild id
  10703. * flag & 1 : Warp out owners
  10704. * flag & 2 : Warp out outsiders
  10705. * flag & 4 : reserved for mobs
  10706. * */
  10707. BUILDIN_FUNC(maprespawnguildid)
  10708. {
  10709. const char *mapname=script_getstr(st,2);
  10710. int g_id=script_getnum(st,3);
  10711. int flag=script_getnum(st,4);
  10712. int16 m=map_mapname2mapid(mapname);
  10713. if(m == -1)
  10714. return SCRIPT_CMD_SUCCESS;
  10715. //Catch ALL players (in case some are 'between maps' on execution time)
  10716. map_foreachpc(buildin_maprespawnguildid_sub_pc,m,g_id,flag);
  10717. if (flag&4) //Remove script mobs.
  10718. map_foreachinmap(buildin_maprespawnguildid_sub_mob,m,BL_MOB);
  10719. return SCRIPT_CMD_SUCCESS;
  10720. }
  10721. BUILDIN_FUNC(agitstart)
  10722. {
  10723. if(agit_flag==1) return SCRIPT_CMD_SUCCESS; // Agit already Start.
  10724. agit_flag=1;
  10725. guild_agit_start();
  10726. return SCRIPT_CMD_SUCCESS;
  10727. }
  10728. BUILDIN_FUNC(agitend)
  10729. {
  10730. if(agit_flag==0) return SCRIPT_CMD_SUCCESS; // Agit already End.
  10731. agit_flag=0;
  10732. guild_agit_end();
  10733. return SCRIPT_CMD_SUCCESS;
  10734. }
  10735. BUILDIN_FUNC(agitstart2)
  10736. {
  10737. if(agit2_flag==1) return SCRIPT_CMD_SUCCESS; // Agit2 already Start.
  10738. agit2_flag=1;
  10739. guild_agit2_start();
  10740. return SCRIPT_CMD_SUCCESS;
  10741. }
  10742. BUILDIN_FUNC(agitend2)
  10743. {
  10744. if(agit2_flag==0) return SCRIPT_CMD_SUCCESS; // Agit2 already End.
  10745. agit2_flag=0;
  10746. guild_agit2_end();
  10747. return SCRIPT_CMD_SUCCESS;
  10748. }
  10749. /*==========================================
  10750. * Returns whether woe is on or off. // choice script
  10751. *------------------------------------------*/
  10752. BUILDIN_FUNC(agitcheck)
  10753. {
  10754. script_pushint(st,agit_flag);
  10755. return SCRIPT_CMD_SUCCESS;
  10756. }
  10757. /*==========================================
  10758. * Returns whether woese is on or off. // choice script
  10759. *------------------------------------------*/
  10760. BUILDIN_FUNC(agitcheck2)
  10761. {
  10762. script_pushint(st,agit2_flag);
  10763. return SCRIPT_CMD_SUCCESS;
  10764. }
  10765. /// Sets the guild_id of this npc.
  10766. ///
  10767. /// flagemblem <guild_id>;
  10768. BUILDIN_FUNC(flagemblem)
  10769. {
  10770. TBL_NPC* nd;
  10771. int g_id = script_getnum(st,2);
  10772. if(g_id < 0)
  10773. return SCRIPT_CMD_SUCCESS;
  10774. nd = (TBL_NPC*)map_id2nd(st->oid);
  10775. if( nd == NULL ) {
  10776. ShowError("script:flagemblem: npc %d not found\n", st->oid);
  10777. } else if( nd->subtype != NPCTYPE_SCRIPT ) {
  10778. ShowError("script:flagemblem: unexpected subtype %d for npc %d '%s'\n", nd->subtype, st->oid, nd->exname);
  10779. } else {
  10780. bool changed = ( nd->u.scr.guild_id != g_id )?true:false;
  10781. nd->u.scr.guild_id = g_id;
  10782. clif_guild_emblem_area(&nd->bl);
  10783. /* guild flag caching */
  10784. if( g_id ) /* adding a id */
  10785. guild_flag_add(nd);
  10786. else if( changed ) /* removing a flag */
  10787. guild_flag_remove(nd);
  10788. }
  10789. return SCRIPT_CMD_SUCCESS;
  10790. }
  10791. BUILDIN_FUNC(getcastlename)
  10792. {
  10793. const char* mapname = mapindex_getmapname(script_getstr(st,2),NULL);
  10794. struct guild_castle* gc = guild_mapname2gc(mapname);
  10795. const char* name = (gc) ? gc->castle_name : "";
  10796. script_pushstrcopy(st,name);
  10797. return SCRIPT_CMD_SUCCESS;
  10798. }
  10799. BUILDIN_FUNC(getcastledata)
  10800. {
  10801. const char *mapname = mapindex_getmapname(script_getstr(st,2),NULL);
  10802. int index = script_getnum(st,3);
  10803. struct guild_castle *gc = guild_mapname2gc(mapname);
  10804. if (gc == NULL) {
  10805. script_pushint(st,0);
  10806. ShowWarning("buildin_setcastledata: guild castle for map '%s' not found\n", mapname);
  10807. return SCRIPT_CMD_FAILURE;
  10808. }
  10809. switch (index) {
  10810. case 1:
  10811. script_pushint(st,gc->guild_id); break;
  10812. case 2:
  10813. script_pushint(st,gc->economy); break;
  10814. case 3:
  10815. script_pushint(st,gc->defense); break;
  10816. case 4:
  10817. script_pushint(st,gc->triggerE); break;
  10818. case 5:
  10819. script_pushint(st,gc->triggerD); break;
  10820. case 6:
  10821. script_pushint(st,gc->nextTime); break;
  10822. case 7:
  10823. script_pushint(st,gc->payTime); break;
  10824. case 8:
  10825. script_pushint(st,gc->createTime); break;
  10826. case 9:
  10827. script_pushint(st,gc->visibleC); break;
  10828. default:
  10829. if (index > 9 && index <= 9+MAX_GUARDIANS) {
  10830. script_pushint(st,gc->guardian[index-10].visible);
  10831. break;
  10832. }
  10833. script_pushint(st,0);
  10834. ShowWarning("buildin_setcastledata: index = '%d' is out of allowed range\n", index);
  10835. return SCRIPT_CMD_FAILURE;
  10836. }
  10837. return SCRIPT_CMD_SUCCESS;
  10838. }
  10839. BUILDIN_FUNC(setcastledata)
  10840. {
  10841. const char *mapname = mapindex_getmapname(script_getstr(st,2),NULL);
  10842. int index = script_getnum(st,3);
  10843. int value = script_getnum(st,4);
  10844. struct guild_castle *gc = guild_mapname2gc(mapname);
  10845. if (gc == NULL) {
  10846. ShowWarning("buildin_setcastledata: guild castle for map '%s' not found\n", mapname);
  10847. return SCRIPT_CMD_FAILURE;
  10848. }
  10849. if (index <= 0 || index > 9+MAX_GUARDIANS) {
  10850. ShowWarning("buildin_setcastledata: index = '%d' is out of allowed range\n", index);
  10851. return SCRIPT_CMD_FAILURE;
  10852. }
  10853. guild_castledatasave(gc->castle_id, index, value);
  10854. return SCRIPT_CMD_SUCCESS;
  10855. }
  10856. /* =====================================================================
  10857. * ---------------------------------------------------------------------*/
  10858. BUILDIN_FUNC(requestguildinfo)
  10859. {
  10860. int guild_id=script_getnum(st,2);
  10861. const char *event=NULL;
  10862. if( script_hasdata(st,3) ){
  10863. event=script_getstr(st,3);
  10864. check_event(st, event);
  10865. }
  10866. if(guild_id>0)
  10867. guild_npc_request_info(guild_id,event);
  10868. return SCRIPT_CMD_SUCCESS;
  10869. }
  10870. /// Returns the number of cards that have been compounded onto the specified equipped item.
  10871. /// getequipcardcnt(<equipment slot>);
  10872. BUILDIN_FUNC(getequipcardcnt)
  10873. {
  10874. int i=-1,j,num;
  10875. TBL_PC *sd;
  10876. int count;
  10877. num=script_getnum(st,2);
  10878. sd=script_rid2sd(st);
  10879. if (num > 0 && num <= ARRAYLENGTH(equip))
  10880. i=pc_checkequip(sd,equip[num-1]);
  10881. if (i < 0 || !sd->inventory_data[i]) {
  10882. script_pushint(st,0);
  10883. return SCRIPT_CMD_SUCCESS;
  10884. }
  10885. if(itemdb_isspecial(sd->status.inventory[i].card[0]))
  10886. {
  10887. script_pushint(st,0);
  10888. return SCRIPT_CMD_SUCCESS;
  10889. }
  10890. count = 0;
  10891. for( j = 0; j < sd->inventory_data[i]->slot; j++ )
  10892. if( sd->status.inventory[i].card[j] && itemdb_type(sd->status.inventory[i].card[j]) == IT_CARD )
  10893. count++;
  10894. script_pushint(st,count);
  10895. return SCRIPT_CMD_SUCCESS;
  10896. }
  10897. /// Removes all cards from the item found in the specified equipment slot of the invoking character,
  10898. /// and give them to the character. If any cards were removed in this manner, it will also show a success effect.
  10899. /// successremovecards <slot>;
  10900. BUILDIN_FUNC(successremovecards) {
  10901. int i=-1,c,cardflag=0;
  10902. TBL_PC* sd = script_rid2sd(st);
  10903. int num = script_getnum(st,2);
  10904. if (num > 0 && num <= ARRAYLENGTH(equip))
  10905. i=pc_checkequip(sd,equip[num-1]);
  10906. if (i < 0 || !sd->inventory_data[i]) {
  10907. return SCRIPT_CMD_SUCCESS;
  10908. }
  10909. if(itemdb_isspecial(sd->status.inventory[i].card[0]))
  10910. return SCRIPT_CMD_SUCCESS;
  10911. for( c = sd->inventory_data[i]->slot - 1; c >= 0; --c ) {
  10912. if( sd->status.inventory[i].card[c] && itemdb_type(sd->status.inventory[i].card[c]) == IT_CARD ) {// extract this card from the item
  10913. unsigned char flag = 0;
  10914. struct item item_tmp;
  10915. memset(&item_tmp,0,sizeof(item_tmp));
  10916. cardflag = 1;
  10917. item_tmp.nameid = sd->status.inventory[i].card[c];
  10918. item_tmp.identify = 1;
  10919. if((flag=pc_additem(sd,&item_tmp,1,LOG_TYPE_SCRIPT))){ // get back the cart in inventory
  10920. clif_additem(sd,0,0,flag);
  10921. map_addflooritem(&item_tmp,1,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0,0);
  10922. }
  10923. }
  10924. }
  10925. if(cardflag == 1) {//if card was remove remplace item with no card
  10926. unsigned char flag = 0, j;
  10927. struct item item_tmp;
  10928. memset(&item_tmp,0,sizeof(item_tmp));
  10929. item_tmp.nameid = sd->status.inventory[i].nameid;
  10930. item_tmp.identify = 1;
  10931. item_tmp.refine = sd->status.inventory[i].refine;
  10932. item_tmp.attribute = sd->status.inventory[i].attribute;
  10933. item_tmp.expire_time = sd->status.inventory[i].expire_time;
  10934. item_tmp.bound = sd->status.inventory[i].bound;
  10935. for (j = sd->inventory_data[i]->slot; j < MAX_SLOTS; j++)
  10936. item_tmp.card[j]=sd->status.inventory[i].card[j];
  10937. pc_delitem(sd,i,1,0,3,LOG_TYPE_SCRIPT);
  10938. if((flag=pc_additem(sd,&item_tmp,1,LOG_TYPE_SCRIPT))){ //chk if can be spawn in inventory otherwise put on floor
  10939. clif_additem(sd,0,0,flag);
  10940. map_addflooritem(&item_tmp,1,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0,0);
  10941. }
  10942. clif_misceffect(&sd->bl,3);
  10943. }
  10944. return SCRIPT_CMD_SUCCESS;
  10945. }
  10946. /// Removes all cards from the item found in the specified equipment slot of the invoking character.
  10947. /// failedremovecards <slot>, <type>;
  10948. /// <type>=0 : will destroy both the item and the cards.
  10949. /// <type>=1 : will keep the item, but destroy the cards.
  10950. /// <type>=2 : will keep the cards, but destroy the item.
  10951. /// <type>=? : will just display the failure effect.
  10952. BUILDIN_FUNC(failedremovecards) {
  10953. int i=-1,c,cardflag=0;
  10954. TBL_PC* sd = script_rid2sd(st);
  10955. int num = script_getnum(st,2);
  10956. int typefail = script_getnum(st,3);
  10957. if (num > 0 && num <= ARRAYLENGTH(equip))
  10958. i=pc_checkequip(sd,equip[num-1]);
  10959. if (i < 0 || !sd->inventory_data[i])
  10960. return SCRIPT_CMD_SUCCESS;
  10961. if(itemdb_isspecial(sd->status.inventory[i].card[0]))
  10962. return SCRIPT_CMD_SUCCESS;
  10963. for( c = sd->inventory_data[i]->slot - 1; c >= 0; --c ) {
  10964. if( sd->status.inventory[i].card[c] && itemdb_type(sd->status.inventory[i].card[c]) == IT_CARD ) {
  10965. cardflag = 1;
  10966. if(typefail == 2) {// add cards to inventory, clear
  10967. unsigned char flag = 0;
  10968. struct item item_tmp;
  10969. memset(&item_tmp,0,sizeof(item_tmp));
  10970. item_tmp.nameid = sd->status.inventory[i].card[c];
  10971. item_tmp.identify = 1;
  10972. if((flag=pc_additem(sd,&item_tmp,1,LOG_TYPE_SCRIPT))){
  10973. clif_additem(sd,0,0,flag);
  10974. map_addflooritem(&item_tmp,1,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0,0);
  10975. }
  10976. }
  10977. }
  10978. }
  10979. if(cardflag == 1) {
  10980. if(typefail == 0 || typefail == 2){ // destroy the item
  10981. pc_delitem(sd,i,1,0,2,LOG_TYPE_SCRIPT);
  10982. }
  10983. if(typefail == 1){ // destroy the card
  10984. unsigned char flag = 0, j;
  10985. struct item item_tmp;
  10986. memset(&item_tmp,0,sizeof(item_tmp));
  10987. item_tmp.nameid = sd->status.inventory[i].nameid;
  10988. item_tmp.identify = 1;
  10989. item_tmp.refine = sd->status.inventory[i].refine;
  10990. item_tmp.attribute = sd->status.inventory[i].attribute;
  10991. item_tmp.expire_time = sd->status.inventory[i].expire_time;
  10992. item_tmp.bound = sd->status.inventory[i].bound;
  10993. for (j = sd->inventory_data[i]->slot; j < MAX_SLOTS; j++)
  10994. item_tmp.card[j]=sd->status.inventory[i].card[j];
  10995. pc_delitem(sd,i,1,0,2,LOG_TYPE_SCRIPT);
  10996. if((flag=pc_additem(sd,&item_tmp,1,LOG_TYPE_SCRIPT))){
  10997. clif_additem(sd,0,0,flag);
  10998. map_addflooritem(&item_tmp,1,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0,0);
  10999. }
  11000. }
  11001. clif_misceffect(&sd->bl,2);
  11002. }
  11003. return SCRIPT_CMD_SUCCESS;
  11004. }
  11005. /* ================================================================
  11006. * mapwarp "<from map>","<to map>",<x>,<y>,<type>,<ID for Type>;
  11007. * type: 0=everyone, 1=guild, 2=party; [Reddozen]
  11008. * improved by [Lance]
  11009. * ================================================================*/
  11010. BUILDIN_FUNC(mapwarp) // Added by RoVeRT
  11011. {
  11012. int x,y,m,check_val=0,check_ID=0,i=0;
  11013. struct guild *g = NULL;
  11014. struct party_data *p = NULL;
  11015. const char *str;
  11016. const char *mapname;
  11017. unsigned int index;
  11018. mapname=script_getstr(st,2);
  11019. str=script_getstr(st,3);
  11020. x=script_getnum(st,4);
  11021. y=script_getnum(st,5);
  11022. if(script_hasdata(st,7)){
  11023. check_val=script_getnum(st,6);
  11024. check_ID=script_getnum(st,7);
  11025. }
  11026. if((m=map_mapname2mapid(mapname))< 0)
  11027. return SCRIPT_CMD_SUCCESS;
  11028. if(!(index=mapindex_name2id(str)))
  11029. return SCRIPT_CMD_SUCCESS;
  11030. switch(check_val){
  11031. case 1:
  11032. g = guild_search(check_ID);
  11033. if (g){
  11034. for( i=0; i < g->max_member; i++)
  11035. {
  11036. if(g->member[i].sd && g->member[i].sd->bl.m==m){
  11037. pc_setpos(g->member[i].sd,index,x,y,CLR_TELEPORT);
  11038. }
  11039. }
  11040. }
  11041. break;
  11042. case 2:
  11043. p = party_search(check_ID);
  11044. if(p){
  11045. for(i=0;i<MAX_PARTY; i++){
  11046. if(p->data[i].sd && p->data[i].sd->bl.m == m){
  11047. pc_setpos(p->data[i].sd,index,x,y,CLR_TELEPORT);
  11048. }
  11049. }
  11050. }
  11051. break;
  11052. default:
  11053. map_foreachinmap(buildin_areawarp_sub,m,BL_PC,index,x,y,0,0);
  11054. break;
  11055. }
  11056. return SCRIPT_CMD_SUCCESS;
  11057. }
  11058. static int buildin_mobcount_sub(struct block_list *bl,va_list ap) // Added by RoVeRT
  11059. {
  11060. char *event=va_arg(ap,char *);
  11061. struct mob_data *md = ((struct mob_data *)bl);
  11062. if( md->status.hp > 0 && (!event || strcmp(event,md->npc_event) == 0) )
  11063. return 1;
  11064. return SCRIPT_CMD_SUCCESS;
  11065. }
  11066. BUILDIN_FUNC(mobcount) // Added by RoVeRT
  11067. {
  11068. const char *mapname,*event;
  11069. int16 m;
  11070. mapname=script_getstr(st,2);
  11071. event=script_getstr(st,3);
  11072. if( strcmp(event, "all") == 0 )
  11073. event = NULL;
  11074. else
  11075. check_event(st, event);
  11076. if( strcmp(mapname, "this") == 0 ) {
  11077. struct map_session_data *sd = script_rid2sd(st);
  11078. if( sd )
  11079. m = sd->bl.m;
  11080. else {
  11081. script_pushint(st,-1);
  11082. return SCRIPT_CMD_SUCCESS;
  11083. }
  11084. }
  11085. else if( (m = map_mapname2mapid(mapname)) < 0 ) {
  11086. script_pushint(st,-1);
  11087. return SCRIPT_CMD_SUCCESS;
  11088. }
  11089. script_pushint(st,map_foreachinmap(buildin_mobcount_sub, m, BL_MOB, event));
  11090. return SCRIPT_CMD_SUCCESS;
  11091. }
  11092. BUILDIN_FUNC(marriage)
  11093. {
  11094. const char *partner=script_getstr(st,2);
  11095. TBL_PC *sd=script_rid2sd(st);
  11096. TBL_PC *p_sd=map_nick2sd(partner);
  11097. if(!sd || !p_sd || !pc_marriage(sd,p_sd)){
  11098. script_pushint(st,0);
  11099. return SCRIPT_CMD_SUCCESS;
  11100. }
  11101. script_pushint(st,1);
  11102. return SCRIPT_CMD_SUCCESS;
  11103. }
  11104. BUILDIN_FUNC(wedding_effect)
  11105. {
  11106. TBL_PC *sd=script_rid2sd(st);
  11107. struct block_list *bl;
  11108. if(sd==NULL) {
  11109. bl=map_id2bl(st->oid);
  11110. } else
  11111. bl=&sd->bl;
  11112. clif_wedding_effect(bl);
  11113. return SCRIPT_CMD_SUCCESS;
  11114. }
  11115. /**
  11116. * divorce({<char_id>})
  11117. **/
  11118. BUILDIN_FUNC(divorce)
  11119. {
  11120. TBL_PC *sd;
  11121. if (!script_charid2sd(2,sd) || !pc_divorce(sd)) {
  11122. script_pushint(st,0);
  11123. return SCRIPT_CMD_FAILURE;
  11124. }
  11125. script_pushint(st,1);
  11126. return SCRIPT_CMD_SUCCESS;
  11127. }
  11128. /**
  11129. * ispartneron({<char_id>})
  11130. **/
  11131. BUILDIN_FUNC(ispartneron)
  11132. {
  11133. TBL_PC *sd;
  11134. if (!script_charid2sd(2,sd) || !pc_ismarried(sd) ||
  11135. map_charid2sd(sd->status.partner_id) == NULL)
  11136. {
  11137. script_pushint(st,0);
  11138. return SCRIPT_CMD_FAILURE;
  11139. }
  11140. script_pushint(st,1);
  11141. return SCRIPT_CMD_SUCCESS;
  11142. }
  11143. /**
  11144. * getpartnerid({<char_id>})
  11145. **/
  11146. BUILDIN_FUNC(getpartnerid)
  11147. {
  11148. TBL_PC *sd;
  11149. if (!script_charid2sd(2,sd)) {
  11150. script_pushint(st,0);
  11151. return SCRIPT_CMD_FAILURE;
  11152. }
  11153. script_pushint(st,sd->status.partner_id);
  11154. return SCRIPT_CMD_SUCCESS;
  11155. }
  11156. /**
  11157. * getchildid({<char_id>})
  11158. **/
  11159. BUILDIN_FUNC(getchildid)
  11160. {
  11161. TBL_PC *sd;
  11162. if (!script_charid2sd(2,sd)) {
  11163. script_pushint(st,0);
  11164. return SCRIPT_CMD_FAILURE;
  11165. }
  11166. script_pushint(st,sd->status.child);
  11167. return SCRIPT_CMD_SUCCESS;
  11168. }
  11169. /**
  11170. * getmotherid({<char_id>})
  11171. **/
  11172. BUILDIN_FUNC(getmotherid)
  11173. {
  11174. TBL_PC *sd;
  11175. if (!script_charid2sd(2,sd)) {
  11176. script_pushint(st,0);
  11177. return SCRIPT_CMD_FAILURE;
  11178. }
  11179. script_pushint(st,sd->status.mother);
  11180. return SCRIPT_CMD_SUCCESS;
  11181. }
  11182. /**
  11183. * getfatherid({<char_id>})
  11184. **/
  11185. BUILDIN_FUNC(getfatherid)
  11186. {
  11187. TBL_PC *sd;
  11188. if (!script_charid2sd(2,sd)) {
  11189. script_pushint(st,0);
  11190. return SCRIPT_CMD_FAILURE;
  11191. }
  11192. script_pushint(st,sd->status.father);
  11193. return SCRIPT_CMD_SUCCESS;
  11194. }
  11195. BUILDIN_FUNC(warppartner)
  11196. {
  11197. int x,y;
  11198. unsigned short mapindex;
  11199. const char *str;
  11200. TBL_PC *sd=script_rid2sd(st);
  11201. TBL_PC *p_sd=NULL;
  11202. if(sd==NULL || !pc_ismarried(sd) ||
  11203. (p_sd=map_charid2sd(sd->status.partner_id)) == NULL) {
  11204. script_pushint(st,0);
  11205. return SCRIPT_CMD_SUCCESS;
  11206. }
  11207. str=script_getstr(st,2);
  11208. x=script_getnum(st,3);
  11209. y=script_getnum(st,4);
  11210. mapindex = mapindex_name2id(str);
  11211. if (mapindex) {
  11212. pc_setpos(p_sd,mapindex,x,y,CLR_OUTSIGHT);
  11213. script_pushint(st,1);
  11214. } else
  11215. script_pushint(st,0);
  11216. return SCRIPT_CMD_SUCCESS;
  11217. }
  11218. /*================================================
  11219. * Script for Displaying MOB Information [Valaris]
  11220. *------------------------------------------------*/
  11221. BUILDIN_FUNC(strmobinfo)
  11222. {
  11223. int num=script_getnum(st,2);
  11224. int class_=script_getnum(st,3);
  11225. if(!mobdb_checkid(class_))
  11226. {
  11227. if (num < 3) //requested a string
  11228. script_pushconststr(st,"");
  11229. else
  11230. script_pushint(st,0);
  11231. return SCRIPT_CMD_SUCCESS;
  11232. }
  11233. switch (num) {
  11234. case 1: script_pushstrcopy(st,mob_db(class_)->name); break;
  11235. case 2: script_pushstrcopy(st,mob_db(class_)->jname); break;
  11236. case 3: script_pushint(st,mob_db(class_)->lv); break;
  11237. case 4: script_pushint(st,mob_db(class_)->status.max_hp); break;
  11238. case 5: script_pushint(st,mob_db(class_)->status.max_sp); break;
  11239. case 6: script_pushint(st,mob_db(class_)->base_exp); break;
  11240. case 7: script_pushint(st,mob_db(class_)->job_exp); break;
  11241. default:
  11242. script_pushint(st,0);
  11243. break;
  11244. }
  11245. return SCRIPT_CMD_SUCCESS;
  11246. }
  11247. /*==========================================
  11248. * Summon guardians [Valaris]
  11249. * guardian("<map name>",<x>,<y>,"<name to show>",<mob id>{,"<event label>"}{,<guardian index>}) -> <id>
  11250. *------------------------------------------*/
  11251. BUILDIN_FUNC(guardian)
  11252. {
  11253. int class_=0,x=0,y=0,guardian=0;
  11254. const char *str,*mapname,*evt="";
  11255. bool has_index = false;
  11256. mapname =script_getstr(st,2);
  11257. x =script_getnum(st,3);
  11258. y =script_getnum(st,4);
  11259. str =script_getstr(st,5);
  11260. class_=script_getnum(st,6);
  11261. if( script_hasdata(st,8) )
  11262. {// "<event label>",<guardian index>
  11263. evt=script_getstr(st,7);
  11264. guardian=script_getnum(st,8);
  11265. has_index = true;
  11266. } else if( script_hasdata(st,7) ){
  11267. struct script_data *data = script_getdata(st,7);
  11268. get_val(st,data);
  11269. if( data_isstring(data) )
  11270. {// "<event label>"
  11271. evt=script_getstr(st,7);
  11272. } else if( data_isint(data) )
  11273. {// <guardian index>
  11274. guardian=script_getnum(st,7);
  11275. has_index = true;
  11276. } else {
  11277. ShowError("script:guardian: invalid data type for argument #6 (from 1)\n");
  11278. script_reportdata(data);
  11279. return SCRIPT_CMD_FAILURE;
  11280. }
  11281. }
  11282. check_event(st, evt);
  11283. script_pushint(st, mob_spawn_guardian(mapname,x,y,str,class_,evt,guardian,has_index));
  11284. return SCRIPT_CMD_SUCCESS;
  11285. }
  11286. /*==========================================
  11287. * Invisible Walls [Zephyrus]
  11288. *------------------------------------------*/
  11289. BUILDIN_FUNC(setwall)
  11290. {
  11291. const char *mapname, *name;
  11292. int x, y, m, size, dir;
  11293. bool shootable;
  11294. mapname = script_getstr(st,2);
  11295. x = script_getnum(st,3);
  11296. y = script_getnum(st,4);
  11297. size = script_getnum(st,5);
  11298. dir = script_getnum(st,6);
  11299. shootable = script_getnum(st,7);
  11300. name = script_getstr(st,8);
  11301. if( (m = map_mapname2mapid(mapname)) < 0 )
  11302. return SCRIPT_CMD_SUCCESS; // Invalid Map
  11303. map_iwall_set(m, x, y, size, dir, shootable, name);
  11304. return SCRIPT_CMD_SUCCESS;
  11305. }
  11306. BUILDIN_FUNC(delwall)
  11307. {
  11308. const char *name = script_getstr(st,2);
  11309. map_iwall_remove(name);
  11310. return SCRIPT_CMD_SUCCESS;
  11311. }
  11312. /// Retrieves various information about the specified guardian.
  11313. ///
  11314. /// guardianinfo("<map_name>", <index>, <type>) -> <value>
  11315. /// type: 0 - whether it is deployed or not
  11316. /// 1 - maximum hp
  11317. /// 2 - current hp
  11318. ///
  11319. BUILDIN_FUNC(guardianinfo)
  11320. {
  11321. const char* mapname = mapindex_getmapname(script_getstr(st,2),NULL);
  11322. int id = script_getnum(st,3);
  11323. int type = script_getnum(st,4);
  11324. struct guild_castle* gc = guild_mapname2gc(mapname);
  11325. struct mob_data* gd;
  11326. if( gc == NULL || id < 0 || id >= MAX_GUARDIANS )
  11327. {
  11328. script_pushint(st,-1);
  11329. return SCRIPT_CMD_SUCCESS;
  11330. }
  11331. if( type == 0 )
  11332. script_pushint(st, gc->guardian[id].visible);
  11333. else
  11334. if( !gc->guardian[id].visible )
  11335. script_pushint(st,-1);
  11336. else
  11337. if( (gd = map_id2md(gc->guardian[id].id)) == NULL )
  11338. script_pushint(st,-1);
  11339. else
  11340. {
  11341. if ( type == 1 ) script_pushint(st,gd->status.max_hp);
  11342. else if( type == 2 ) script_pushint(st,gd->status.hp);
  11343. else
  11344. script_pushint(st,-1);
  11345. }
  11346. return SCRIPT_CMD_SUCCESS;
  11347. }
  11348. /*==========================================
  11349. * Get the item name by item_id or null
  11350. *------------------------------------------*/
  11351. BUILDIN_FUNC(getitemname)
  11352. {
  11353. unsigned short item_id = 0;
  11354. struct item_data *i_data;
  11355. char *item_name;
  11356. struct script_data *data;
  11357. data=script_getdata(st,2);
  11358. get_val(st,data);
  11359. if( data_isstring(data) ){
  11360. const char *name=conv_str(st,data);
  11361. struct item_data *item_data = itemdb_searchname(name);
  11362. if( item_data )
  11363. item_id=item_data->nameid;
  11364. }else
  11365. item_id=conv_num(st,data);
  11366. i_data = itemdb_exists(item_id);
  11367. if (i_data == NULL)
  11368. {
  11369. script_pushconststr(st,"null");
  11370. return SCRIPT_CMD_SUCCESS;
  11371. }
  11372. item_name=(char *)aMalloc(ITEM_NAME_LENGTH*sizeof(char));
  11373. memcpy(item_name, i_data->jname, ITEM_NAME_LENGTH);
  11374. script_pushstr(st,item_name);
  11375. return SCRIPT_CMD_SUCCESS;
  11376. }
  11377. /*==========================================
  11378. * Returns number of slots an item has. [Skotlex]
  11379. *------------------------------------------*/
  11380. BUILDIN_FUNC(getitemslots)
  11381. {
  11382. unsigned short item_id;
  11383. struct item_data *i_data;
  11384. item_id=script_getnum(st,2);
  11385. i_data = itemdb_exists(item_id);
  11386. if (i_data)
  11387. script_pushint(st,i_data->slot);
  11388. else
  11389. script_pushint(st,-1);
  11390. return SCRIPT_CMD_SUCCESS;
  11391. }
  11392. // TODO: add matk here if needed/once we get rid of RENEWAL
  11393. /*==========================================
  11394. * Returns some values of an item [Lupus]
  11395. * Price, Weight, etc...
  11396. getiteminfo(itemID,n), where n
  11397. 0 value_buy;
  11398. 1 value_sell;
  11399. 2 type;
  11400. 3 maxchance = Max drop chance of this item e.g. 1 = 0.01% , etc..
  11401. if = 0, then monsters don't drop it at all (rare or a quest item)
  11402. if = -1, then this item is sold in NPC shops only
  11403. 4 sex;
  11404. 5 equip;
  11405. 6 weight;
  11406. 7 atk;
  11407. 8 def;
  11408. 9 range;
  11409. 10 slot;
  11410. 11 look;
  11411. 12 elv;
  11412. 13 wlv;
  11413. 14 view id
  11414. 15 eLvmax
  11415. 16 matk (renewal)
  11416. *------------------------------------------*/
  11417. BUILDIN_FUNC(getiteminfo)
  11418. {
  11419. unsigned short item_id,n;
  11420. struct item_data *i_data;
  11421. item_id = script_getnum(st,2);
  11422. n = script_getnum(st,3);
  11423. i_data = itemdb_exists(item_id);
  11424. if (i_data && n <= 16) {
  11425. int *item_arr = (int*)&i_data->value_buy;
  11426. #ifndef RENEWAL
  11427. if (n == 16)
  11428. script_pushint(st,0);
  11429. else
  11430. #endif
  11431. script_pushint(st,item_arr[n]);
  11432. } else
  11433. script_pushint(st,-1);
  11434. return SCRIPT_CMD_SUCCESS;
  11435. }
  11436. /*==========================================
  11437. * Set some values of an item [Lupus]
  11438. * Price, Weight, etc...
  11439. setiteminfo(itemID,n,Value), where n
  11440. 0 value_buy;
  11441. 1 value_sell;
  11442. 2 type;
  11443. 3 maxchance = Max drop chance of this item e.g. 1 = 0.01% , etc..
  11444. if = 0, then monsters don't drop it at all (rare or a quest item)
  11445. if = -1, then this item is sold in NPC shops only
  11446. 4 sex;
  11447. 5 equip;
  11448. 6 weight;
  11449. 7 atk;
  11450. 8 def;
  11451. 9 range;
  11452. 10 slot;
  11453. 11 look;
  11454. 12 elv;
  11455. 13 wlv;
  11456. 14 view id
  11457. * Returns Value or -1 if the wrong field's been set
  11458. *------------------------------------------*/
  11459. BUILDIN_FUNC(setiteminfo)
  11460. {
  11461. unsigned short item_id;
  11462. int n,value;
  11463. struct item_data *i_data;
  11464. item_id = script_getnum(st,2);
  11465. n = script_getnum(st,3);
  11466. value = script_getnum(st,4);
  11467. i_data = itemdb_exists(item_id);
  11468. if (i_data && n>=0 && n<=14) {
  11469. int *item_arr = (int*)&i_data->value_buy;
  11470. item_arr[n] = value;
  11471. script_pushint(st,value);
  11472. } else
  11473. script_pushint(st,-1);
  11474. return SCRIPT_CMD_SUCCESS;
  11475. }
  11476. /*==========================================
  11477. * Returns value from equipped item slot n [Lupus]
  11478. getequipcardid(num,slot)
  11479. where
  11480. num = eqip position slot
  11481. slot = 0,1,2,3 (Card Slot N)
  11482. This func returns CARD ID, 255,254,-255 (for card 0, if the item is produced)
  11483. it's useful when you want to check item cards or if it's signed
  11484. Useful for such quests as "Sign this refined item with players name" etc
  11485. Hat[0] +4 -> Player's Hat[0] +4
  11486. *------------------------------------------*/
  11487. BUILDIN_FUNC(getequipcardid)
  11488. {
  11489. int i=-1,num,slot;
  11490. TBL_PC *sd;
  11491. num=script_getnum(st,2);
  11492. slot=script_getnum(st,3);
  11493. sd=script_rid2sd(st);
  11494. if (num > 0 && num <= ARRAYLENGTH(equip))
  11495. i=pc_checkequip(sd,equip[num-1]);
  11496. if(i >= 0 && slot>=0 && slot<4)
  11497. script_pushint(st,sd->status.inventory[i].card[slot]);
  11498. else
  11499. script_pushint(st,0);
  11500. return SCRIPT_CMD_SUCCESS;
  11501. }
  11502. /*==========================================
  11503. * petskillbonus [Valaris] //Rewritten by [Skotlex]
  11504. *------------------------------------------*/
  11505. BUILDIN_FUNC(petskillbonus)
  11506. {
  11507. struct pet_data *pd;
  11508. TBL_PC *sd = script_rid2sd(st);
  11509. if(sd == NULL || sd->pd == NULL)
  11510. return SCRIPT_CMD_FAILURE;
  11511. pd = sd->pd;
  11512. if (pd->bonus)
  11513. { //Clear previous bonus
  11514. if (pd->bonus->timer != INVALID_TIMER)
  11515. delete_timer(pd->bonus->timer, pet_skill_bonus_timer);
  11516. } else //init
  11517. pd->bonus = (struct pet_bonus *) aMalloc(sizeof(struct pet_bonus));
  11518. pd->bonus->type = script_getnum(st,2);
  11519. pd->bonus->val = script_getnum(st,3);
  11520. pd->bonus->duration = script_getnum(st,4);
  11521. pd->bonus->delay = script_getnum(st,5);
  11522. if (pd->state.skillbonus == 1)
  11523. pd->state.skillbonus = 0; // waiting state
  11524. // wait for timer to start
  11525. if (battle_config.pet_equip_required && pd->pet.equip == 0)
  11526. pd->bonus->timer = INVALID_TIMER;
  11527. else
  11528. pd->bonus->timer = add_timer(gettick()+pd->bonus->delay*1000, pet_skill_bonus_timer, sd->bl.id, 0);
  11529. return SCRIPT_CMD_SUCCESS;
  11530. }
  11531. /*==========================================
  11532. * pet looting [Valaris] //Rewritten by [Skotlex]
  11533. *------------------------------------------*/
  11534. BUILDIN_FUNC(petloot)
  11535. {
  11536. int max;
  11537. struct pet_data *pd;
  11538. TBL_PC *sd=script_rid2sd(st);
  11539. if(sd==NULL || sd->pd==NULL)
  11540. return SCRIPT_CMD_SUCCESS;
  11541. max=script_getnum(st,2);
  11542. if(max < 1)
  11543. max = 1; //Let'em loot at least 1 item.
  11544. else if (max > MAX_PETLOOT_SIZE)
  11545. max = MAX_PETLOOT_SIZE;
  11546. pd = sd->pd;
  11547. if (pd->loot != NULL)
  11548. { //Release whatever was there already and reallocate memory
  11549. pet_lootitem_drop(pd, pd->master);
  11550. aFree(pd->loot->item);
  11551. }
  11552. else
  11553. pd->loot = (struct pet_loot *)aMalloc(sizeof(struct pet_loot));
  11554. pd->loot->item = (struct item *)aCalloc(max,sizeof(struct item));
  11555. pd->loot->max=max;
  11556. pd->loot->count = 0;
  11557. pd->loot->weight = 0;
  11558. return SCRIPT_CMD_SUCCESS;
  11559. }
  11560. /*==========================================
  11561. * Set arrays with info of all sd inventory :
  11562. * @inventorylist_id, @inventorylist_amount, @inventorylist_equip,
  11563. * @inventorylist_refine, @inventorylist_identify, @inventorylist_attribute,
  11564. * @inventorylist_card(0..3), @inventorylist_expire
  11565. * @inventorylist_count = scalar
  11566. *------------------------------------------*/
  11567. BUILDIN_FUNC(getinventorylist)
  11568. {
  11569. TBL_PC *sd;
  11570. char card_var[NAME_LENGTH];
  11571. int i,j=0,k;
  11572. if (!script_charid2sd(2,sd))
  11573. return SCRIPT_CMD_FAILURE;
  11574. for(i=0;i<MAX_INVENTORY;i++){
  11575. if(sd->status.inventory[i].nameid > 0 && sd->status.inventory[i].amount > 0){
  11576. pc_setreg(sd,reference_uid(add_str("@inventorylist_id"), j),sd->status.inventory[i].nameid);
  11577. pc_setreg(sd,reference_uid(add_str("@inventorylist_amount"), j),sd->status.inventory[i].amount);
  11578. pc_setreg(sd,reference_uid(add_str("@inventorylist_equip"), j),sd->status.inventory[i].equip);
  11579. pc_setreg(sd,reference_uid(add_str("@inventorylist_refine"), j),sd->status.inventory[i].refine);
  11580. pc_setreg(sd,reference_uid(add_str("@inventorylist_identify"), j),sd->status.inventory[i].identify);
  11581. pc_setreg(sd,reference_uid(add_str("@inventorylist_attribute"), j),sd->status.inventory[i].attribute);
  11582. for (k = 0; k < MAX_SLOTS; k++)
  11583. {
  11584. sprintf(card_var, "@inventorylist_card%d",k+1);
  11585. pc_setreg(sd,reference_uid(add_str(card_var), j),sd->status.inventory[i].card[k]);
  11586. }
  11587. pc_setreg(sd,reference_uid(add_str("@inventorylist_expire"), j),sd->status.inventory[i].expire_time);
  11588. pc_setreg(sd,reference_uid(add_str("@inventorylist_bound"), j),sd->status.inventory[i].bound);
  11589. j++;
  11590. }
  11591. }
  11592. pc_setreg(sd,add_str("@inventorylist_count"),j);
  11593. return SCRIPT_CMD_SUCCESS;
  11594. }
  11595. /**
  11596. * getskilllist ({<char_id>});
  11597. **/
  11598. BUILDIN_FUNC(getskilllist)
  11599. {
  11600. TBL_PC *sd;
  11601. int i,j=0;
  11602. if (!script_charid2sd(2,sd))
  11603. return SCRIPT_CMD_FAILURE;
  11604. for(i=0;i<MAX_SKILL;i++){
  11605. if(sd->status.skill[i].id > 0 && sd->status.skill[i].lv > 0){
  11606. pc_setreg(sd,reference_uid(add_str("@skilllist_id"), j),sd->status.skill[i].id);
  11607. pc_setreg(sd,reference_uid(add_str("@skilllist_lv"), j),sd->status.skill[i].lv);
  11608. pc_setreg(sd,reference_uid(add_str("@skilllist_flag"), j),sd->status.skill[i].flag);
  11609. j++;
  11610. }
  11611. }
  11612. pc_setreg(sd,add_str("@skilllist_count"),j);
  11613. return SCRIPT_CMD_SUCCESS;
  11614. }
  11615. /**
  11616. * clearitem ({<char_id>});
  11617. **/
  11618. BUILDIN_FUNC(clearitem)
  11619. {
  11620. TBL_PC *sd;
  11621. int i;
  11622. if (!script_charid2sd(2,sd))
  11623. return SCRIPT_CMD_FAILURE;
  11624. for (i=0; i<MAX_INVENTORY; i++) {
  11625. if (sd->status.inventory[i].amount) {
  11626. pc_delitem(sd, i, sd->status.inventory[i].amount, 0, 0, LOG_TYPE_SCRIPT);
  11627. }
  11628. }
  11629. return SCRIPT_CMD_SUCCESS;
  11630. }
  11631. /**
  11632. * Disguise Player (returns Mob/NPC ID if success, 0 on fail)
  11633. * disguise <Monster ID>{,<char_id>};
  11634. **/
  11635. BUILDIN_FUNC(disguise)
  11636. {
  11637. int id;
  11638. TBL_PC* sd;
  11639. if (!script_charid2sd(3,sd))
  11640. return SCRIPT_CMD_FAILURE;
  11641. id = script_getnum(st,2);
  11642. if (mobdb_checkid(id) || npcdb_checkid(id)) {
  11643. pc_disguise(sd, id);
  11644. script_pushint(st,id);
  11645. } else
  11646. script_pushint(st,0);
  11647. return SCRIPT_CMD_SUCCESS;
  11648. }
  11649. /**
  11650. * Undisguise Player (returns 1 if success, 0 on fail)
  11651. * undisguise {<char_id>};
  11652. **/
  11653. BUILDIN_FUNC(undisguise)
  11654. {
  11655. TBL_PC* sd;
  11656. if (!script_charid2sd(2,sd))
  11657. return SCRIPT_CMD_FAILURE;
  11658. if (sd->disguise) {
  11659. pc_disguise(sd, 0);
  11660. script_pushint(st,0);
  11661. } else {
  11662. script_pushint(st,1);
  11663. }
  11664. return SCRIPT_CMD_SUCCESS;
  11665. }
  11666. /*==========================================
  11667. * Transform a bl to another _class,
  11668. * @type unused
  11669. *------------------------------------------*/
  11670. BUILDIN_FUNC(classchange)
  11671. {
  11672. int _class,type;
  11673. struct block_list *bl=map_id2bl(st->oid);
  11674. if (bl==NULL)
  11675. return SCRIPT_CMD_FAILURE;
  11676. _class=script_getnum(st,2);
  11677. type=script_getnum(st,3);
  11678. clif_class_change(bl,_class,type);
  11679. return SCRIPT_CMD_SUCCESS;
  11680. }
  11681. /*==========================================
  11682. * Display an effect
  11683. *------------------------------------------*/
  11684. BUILDIN_FUNC(misceffect)
  11685. {
  11686. int type;
  11687. type=script_getnum(st,2);
  11688. if(st->oid && st->oid != fake_nd->bl.id) {
  11689. struct block_list *bl = map_id2bl(st->oid);
  11690. if (bl)
  11691. clif_specialeffect(bl,type,AREA);
  11692. } else{
  11693. TBL_PC *sd=script_rid2sd(st);
  11694. if(sd)
  11695. clif_specialeffect(&sd->bl,type,AREA);
  11696. }
  11697. return SCRIPT_CMD_SUCCESS;
  11698. }
  11699. /*==========================================
  11700. * Play a BGM on a single client [Rikter/Yommy]
  11701. *------------------------------------------*/
  11702. BUILDIN_FUNC(playBGM)
  11703. {
  11704. struct map_session_data* sd;
  11705. if( ( sd = script_rid2sd(st) ) != NULL ) {
  11706. clif_playBGM(sd, script_getstr(st,2));
  11707. }
  11708. return SCRIPT_CMD_SUCCESS;
  11709. }
  11710. static int playBGM_sub(struct block_list* bl,va_list ap)
  11711. {
  11712. const char* name = va_arg(ap,const char*);
  11713. clif_playBGM(BL_CAST(BL_PC, bl), name);
  11714. return 0;
  11715. }
  11716. static int playBGM_foreachpc_sub(struct map_session_data* sd, va_list args)
  11717. {
  11718. const char* name = va_arg(args, const char*);
  11719. clif_playBGM(sd, name);
  11720. return 0;
  11721. }
  11722. /*==========================================
  11723. * Play a BGM on multiple client [Rikter/Yommy]
  11724. *------------------------------------------*/
  11725. BUILDIN_FUNC(playBGMall)
  11726. {
  11727. const char* name;
  11728. name = script_getstr(st,2);
  11729. if( script_hasdata(st,7) ) {// specified part of map
  11730. const char* mapname = script_getstr(st,3);
  11731. int x0 = script_getnum(st,4);
  11732. int y0 = script_getnum(st,5);
  11733. int x1 = script_getnum(st,6);
  11734. int y1 = script_getnum(st,7);
  11735. map_foreachinarea(playBGM_sub, map_mapname2mapid(mapname), x0, y0, x1, y1, BL_PC, name);
  11736. }
  11737. else if( script_hasdata(st,3) ) {// entire map
  11738. const char* mapname = script_getstr(st,3);
  11739. map_foreachinmap(playBGM_sub, map_mapname2mapid(mapname), BL_PC, name);
  11740. }
  11741. else {// entire server
  11742. map_foreachpc(&playBGM_foreachpc_sub, name);
  11743. }
  11744. return SCRIPT_CMD_SUCCESS;
  11745. }
  11746. /*==========================================
  11747. * Play a .wav sound for sd
  11748. *------------------------------------------*/
  11749. BUILDIN_FUNC(soundeffect)
  11750. {
  11751. TBL_PC* sd = script_rid2sd(st);
  11752. const char* name = script_getstr(st,2);
  11753. int type = script_getnum(st,3);
  11754. if(sd) {
  11755. clif_soundeffect(sd,&sd->bl,name,type);
  11756. }
  11757. return SCRIPT_CMD_SUCCESS;
  11758. }
  11759. int soundeffect_sub(struct block_list* bl,va_list ap)
  11760. {
  11761. char* name = va_arg(ap,char*);
  11762. int type = va_arg(ap,int);
  11763. clif_soundeffect((TBL_PC *)bl, bl, name, type);
  11764. return 0;
  11765. }
  11766. /*==========================================
  11767. * Play a sound effect (.wav) on multiple clients
  11768. * soundeffectall "<filepath>",<type>{,"<map name>"}{,<x0>,<y0>,<x1>,<y1>};
  11769. *------------------------------------------*/
  11770. BUILDIN_FUNC(soundeffectall)
  11771. {
  11772. struct block_list* bl;
  11773. const char* name;
  11774. int type;
  11775. bl = (st->rid) ? &(script_rid2sd(st)->bl) : map_id2bl(st->oid);
  11776. if (!bl)
  11777. return SCRIPT_CMD_SUCCESS;
  11778. name = script_getstr(st,2);
  11779. type = script_getnum(st,3);
  11780. //FIXME: enumerating map squares (map_foreach) is slower than enumerating the list of online players (map_foreachpc?) [ultramage]
  11781. if(!script_hasdata(st,4))
  11782. { // area around
  11783. clif_soundeffectall(bl, name, type, AREA);
  11784. }
  11785. else
  11786. if(!script_hasdata(st,5))
  11787. { // entire map
  11788. const char* mapname = script_getstr(st,4);
  11789. map_foreachinmap(soundeffect_sub, map_mapname2mapid(mapname), BL_PC, name, type);
  11790. }
  11791. else
  11792. if(script_hasdata(st,8))
  11793. { // specified part of map
  11794. const char* mapname = script_getstr(st,4);
  11795. int x0 = script_getnum(st,5);
  11796. int y0 = script_getnum(st,6);
  11797. int x1 = script_getnum(st,7);
  11798. int y1 = script_getnum(st,8);
  11799. map_foreachinarea(soundeffect_sub, map_mapname2mapid(mapname), x0, y0, x1, y1, BL_PC, name, type);
  11800. }
  11801. else
  11802. {
  11803. ShowError("buildin_soundeffectall: insufficient arguments for specific area broadcast.\n");
  11804. }
  11805. return SCRIPT_CMD_SUCCESS;
  11806. }
  11807. /*==========================================
  11808. * pet status recovery [Valaris] / Rewritten by [Skotlex]
  11809. *------------------------------------------*/
  11810. BUILDIN_FUNC(petrecovery)
  11811. {
  11812. struct pet_data *pd;
  11813. TBL_PC *sd=script_rid2sd(st);
  11814. int sc;
  11815. if(sd == NULL || sd->pd == NULL)
  11816. return SCRIPT_CMD_FAILURE;
  11817. sc = script_getnum(st,2);
  11818. if (sc <= SC_NONE || sc >= SC_MAX) {
  11819. ShowError("buildin_petrecovery: Invalid SC type: %d\n", sc);
  11820. return SCRIPT_CMD_FAILURE;
  11821. }
  11822. pd = sd->pd;
  11823. if (pd->recovery)
  11824. { //Halt previous bonus
  11825. if (pd->recovery->timer != INVALID_TIMER)
  11826. delete_timer(pd->recovery->timer, pet_recovery_timer);
  11827. } else //Init
  11828. pd->recovery = (struct pet_recovery *)aMalloc(sizeof(struct pet_recovery));
  11829. pd->recovery->type = (sc_type)sc;
  11830. pd->recovery->delay = script_getnum(st,3);
  11831. pd->recovery->timer = INVALID_TIMER;
  11832. return SCRIPT_CMD_SUCCESS;
  11833. }
  11834. /*==========================================
  11835. * pet healing [Valaris] //Rewritten by [Skotlex]
  11836. *------------------------------------------*/
  11837. BUILDIN_FUNC(petheal)
  11838. {
  11839. struct pet_data *pd;
  11840. TBL_PC *sd=script_rid2sd(st);
  11841. if(sd==NULL || sd->pd==NULL)
  11842. return SCRIPT_CMD_SUCCESS;
  11843. pd=sd->pd;
  11844. if (pd->s_skill)
  11845. { //Clear previous skill
  11846. if (pd->s_skill->timer != INVALID_TIMER)
  11847. {
  11848. if (pd->s_skill->id)
  11849. delete_timer(pd->s_skill->timer, pet_skill_support_timer);
  11850. else
  11851. delete_timer(pd->s_skill->timer, pet_heal_timer);
  11852. }
  11853. } else //init memory
  11854. pd->s_skill = (struct pet_skill_support *) aMalloc(sizeof(struct pet_skill_support));
  11855. pd->s_skill->id=0; //This id identifies that it IS petheal rather than pet_skillsupport
  11856. //Use the lv as the amount to heal
  11857. pd->s_skill->lv=script_getnum(st,2);
  11858. pd->s_skill->delay=script_getnum(st,3);
  11859. pd->s_skill->hp=script_getnum(st,4);
  11860. pd->s_skill->sp=script_getnum(st,5);
  11861. //Use delay as initial offset to avoid skill/heal exploits
  11862. if (battle_config.pet_equip_required && pd->pet.equip == 0)
  11863. pd->s_skill->timer = INVALID_TIMER;
  11864. else
  11865. pd->s_skill->timer = add_timer(gettick()+pd->s_skill->delay*1000,pet_heal_timer,sd->bl.id,0);
  11866. return SCRIPT_CMD_SUCCESS;
  11867. }
  11868. /*==========================================
  11869. * pet attack skills [Valaris] //Rewritten by [Skotlex]
  11870. *------------------------------------------*/
  11871. /// petskillattack <skill id>,<level>,<rate>,<bonusrate>
  11872. /// petskillattack "<skill name>",<level>,<rate>,<bonusrate>
  11873. BUILDIN_FUNC(petskillattack)
  11874. {
  11875. struct pet_data *pd;
  11876. struct script_data *data;
  11877. TBL_PC *sd = script_rid2sd(st);
  11878. int id = 0;
  11879. if(sd == NULL || sd->pd == NULL)
  11880. return SCRIPT_CMD_FAILURE;
  11881. data = script_getdata(st, 2);
  11882. get_val(st, data);
  11883. id = (data_isstring(data) ? skill_name2id(script_getstr(st,2)) : skill_get_index(script_getnum(st,2)));
  11884. if (!id) {
  11885. ShowError("buildin_petskillattack: Invalid skill defined!\n");
  11886. return SCRIPT_CMD_FAILURE;
  11887. }
  11888. pd = sd->pd;
  11889. if (pd->a_skill == NULL)
  11890. pd->a_skill = (struct pet_skill_attack *)aMalloc(sizeof(struct pet_skill_attack));
  11891. pd->a_skill->id = id;
  11892. pd->a_skill->damage = 0;
  11893. pd->a_skill->lv = (unsigned short)min(script_getnum(st,3), skill_get_max(pd->a_skill->id));
  11894. pd->a_skill->div_ = 0;
  11895. pd->a_skill->rate = script_getnum(st,4);
  11896. pd->a_skill->bonusrate = script_getnum(st,5);
  11897. return SCRIPT_CMD_SUCCESS;
  11898. }
  11899. /*==========================================
  11900. * pet attack skills [Valaris]
  11901. *------------------------------------------*/
  11902. /// petskillattack2 <skill id>,<damage>,<div>,<rate>,<bonusrate>
  11903. /// petskillattack2 "<skill name>",<damage>,<div>,<rate>,<bonusrate>
  11904. BUILDIN_FUNC(petskillattack2)
  11905. {
  11906. struct pet_data *pd;
  11907. struct script_data *data;
  11908. TBL_PC *sd = script_rid2sd(st);
  11909. int id = 0;
  11910. if(sd == NULL || sd->pd == NULL)
  11911. return SCRIPT_CMD_FAILURE;
  11912. data = script_getdata(st, 2);
  11913. get_val(st, data);
  11914. id = (data_isstring(data) ? skill_name2id(script_getstr(st,2)) : skill_get_index(script_getnum(st,2)));
  11915. if (!id) {
  11916. ShowError("buildin_petskillattack2: Invalid skill defined!\n");
  11917. return SCRIPT_CMD_FAILURE;
  11918. }
  11919. pd = sd->pd;
  11920. if (pd->a_skill == NULL)
  11921. pd->a_skill = (struct pet_skill_attack *)aMalloc(sizeof(struct pet_skill_attack));
  11922. pd->a_skill->id = id;
  11923. pd->a_skill->damage = script_getnum(st,3); // Fixed damage
  11924. pd->a_skill->lv = (unsigned short)skill_get_max(pd->a_skill->id); // Adjust to max skill level
  11925. pd->a_skill->div_ = script_getnum(st,4);
  11926. pd->a_skill->rate = script_getnum(st,5);
  11927. pd->a_skill->bonusrate = script_getnum(st,6);
  11928. return SCRIPT_CMD_SUCCESS;
  11929. }
  11930. /*==========================================
  11931. * pet support skills [Skotlex]
  11932. *------------------------------------------*/
  11933. /// petskillsupport <skill id>,<level>,<delay>,<hp>,<sp>
  11934. /// petskillsupport "<skill name>",<level>,<delay>,<hp>,<sp>
  11935. BUILDIN_FUNC(petskillsupport)
  11936. {
  11937. struct pet_data *pd;
  11938. struct script_data *data;
  11939. TBL_PC *sd = script_rid2sd(st);
  11940. int id = 0;
  11941. if(sd == NULL || sd->pd == NULL)
  11942. return SCRIPT_CMD_FAILURE;
  11943. data = script_getdata(st, 2);
  11944. get_val(st, data);
  11945. id = (data_isstring(data) ? skill_name2id(script_getstr(st,2)) : skill_get_index(script_getnum(st,2)));
  11946. if (!id) {
  11947. ShowError("buildin_petskillsupport: Invalid skill defined!\n");
  11948. return SCRIPT_CMD_FAILURE;
  11949. }
  11950. pd = sd->pd;
  11951. if (pd->s_skill)
  11952. { //Clear previous skill
  11953. if (pd->s_skill->timer != INVALID_TIMER)
  11954. {
  11955. if (pd->s_skill->id)
  11956. delete_timer(pd->s_skill->timer, pet_skill_support_timer);
  11957. else
  11958. delete_timer(pd->s_skill->timer, pet_heal_timer);
  11959. }
  11960. } else //init memory
  11961. pd->s_skill = (struct pet_skill_support *) aMalloc(sizeof(struct pet_skill_support));
  11962. pd->s_skill->id = id;
  11963. pd->s_skill->lv = script_getnum(st,3);
  11964. pd->s_skill->delay = script_getnum(st,4);
  11965. pd->s_skill->hp = script_getnum(st,5);
  11966. pd->s_skill->sp = script_getnum(st,6);
  11967. //Use delay as initial offset to avoid skill/heal exploits
  11968. if (battle_config.pet_equip_required && pd->pet.equip == 0)
  11969. pd->s_skill->timer = INVALID_TIMER;
  11970. else
  11971. pd->s_skill->timer = add_timer(gettick()+pd->s_skill->delay*1000,pet_skill_support_timer,sd->bl.id,0);
  11972. return SCRIPT_CMD_SUCCESS;
  11973. }
  11974. /*==========================================
  11975. * Scripted skill effects [Celest]
  11976. *------------------------------------------*/
  11977. /// skilleffect <skill id>,<level>
  11978. /// skilleffect "<skill name>",<level>
  11979. BUILDIN_FUNC(skilleffect)
  11980. {
  11981. TBL_PC *sd;
  11982. uint16 skill_id, skill_lv;
  11983. struct script_data *data = script_getdata(st, 2);
  11984. get_val(st, data); // Convert into value in case of a variable
  11985. skill_id = ( data_isstring(data) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  11986. skill_lv = script_getnum(st,3);
  11987. sd = script_rid2sd(st);
  11988. /* Ensure we're standing because the following packet causes the client to virtually set the char to stand,
  11989. * which leaves the server thinking it still is sitting. */
  11990. if( pc_issit(sd) && pc_setstand(sd, false) ) {
  11991. skill_sit(sd, 0);
  11992. clif_standing(&sd->bl);
  11993. }
  11994. clif_skill_nodamage(&sd->bl,&sd->bl,skill_id,skill_lv,1);
  11995. return SCRIPT_CMD_SUCCESS;
  11996. }
  11997. /*==========================================
  11998. * NPC skill effects [Valaris]
  11999. *------------------------------------------*/
  12000. /// npcskilleffect <skill id>,<level>,<x>,<y>
  12001. /// npcskilleffect "<skill name>",<level>,<x>,<y>
  12002. BUILDIN_FUNC(npcskilleffect)
  12003. {
  12004. struct block_list *bl= map_id2bl(st->oid);
  12005. uint16 skill_id, skill_lv;
  12006. int x, y;
  12007. struct script_data *data = script_getdata(st, 2);
  12008. get_val(st, data); // Convert into value in case of a variable
  12009. skill_id=( data_isstring(data) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  12010. skill_lv=script_getnum(st,3);
  12011. x=script_getnum(st,4);
  12012. y=script_getnum(st,5);
  12013. if (bl)
  12014. clif_skill_poseffect(bl,skill_id,skill_lv,x,y,gettick());
  12015. return SCRIPT_CMD_SUCCESS;
  12016. }
  12017. /*==========================================
  12018. * Special effects [Valaris]
  12019. *------------------------------------------*/
  12020. BUILDIN_FUNC(specialeffect)
  12021. {
  12022. struct block_list *bl=map_id2bl(st->oid);
  12023. int type = script_getnum(st,2);
  12024. enum send_target target = script_hasdata(st,3) ? (send_target)script_getnum(st,3) : AREA;
  12025. if(bl==NULL)
  12026. return SCRIPT_CMD_SUCCESS;
  12027. if( script_hasdata(st,4) )
  12028. {
  12029. TBL_NPC *nd = npc_name2id(script_getstr(st,4));
  12030. if(nd)
  12031. clif_specialeffect(&nd->bl, type, target);
  12032. }
  12033. else
  12034. {
  12035. if (target == SELF) {
  12036. TBL_PC *sd=script_rid2sd(st);
  12037. if (sd)
  12038. clif_specialeffect_single(bl,type,sd->fd);
  12039. } else {
  12040. clif_specialeffect(bl, type, target);
  12041. }
  12042. }
  12043. return SCRIPT_CMD_SUCCESS;
  12044. }
  12045. BUILDIN_FUNC(specialeffect2)
  12046. {
  12047. TBL_PC *sd=script_rid2sd(st);
  12048. int type = script_getnum(st,2);
  12049. enum send_target target = script_hasdata(st,3) ? (send_target)script_getnum(st,3) : AREA;
  12050. if( script_hasdata(st,4) )
  12051. sd = map_nick2sd(script_getstr(st,4));
  12052. if (sd)
  12053. clif_specialeffect(&sd->bl, type, target);
  12054. return SCRIPT_CMD_SUCCESS;
  12055. }
  12056. /**
  12057. * nude({<char_id>});
  12058. * @author [Valaris]
  12059. **/
  12060. BUILDIN_FUNC(nude)
  12061. {
  12062. TBL_PC *sd;
  12063. int i, calcflag = 0;
  12064. if (!script_charid2sd(2,sd))
  12065. return SCRIPT_CMD_FAILURE;
  12066. for( i = 0 ; i < EQI_MAX; i++ ) {
  12067. if( sd->equip_index[ i ] >= 0 ) {
  12068. if( !calcflag )
  12069. calcflag = 1;
  12070. pc_unequipitem( sd , sd->equip_index[ i ] , 2);
  12071. }
  12072. }
  12073. if( calcflag )
  12074. status_calc_pc(sd,SCO_NONE);
  12075. return SCRIPT_CMD_SUCCESS;
  12076. }
  12077. int atcommand_sub(struct script_state* st,int type) {
  12078. TBL_PC *sd, dummy_sd;
  12079. int fd;
  12080. const char *cmd;
  12081. cmd = script_getstr(st,2);
  12082. if (st->rid) {
  12083. sd = script_rid2sd(st);
  12084. fd = sd->fd;
  12085. } else { //Use a dummy character.
  12086. sd = &dummy_sd;
  12087. fd = 0;
  12088. memset(&dummy_sd, 0, sizeof(TBL_PC));
  12089. if (st->oid) {
  12090. struct block_list* bl = map_id2bl(st->oid);
  12091. memcpy(&dummy_sd.bl, bl, sizeof(struct block_list));
  12092. if (bl->type == BL_NPC)
  12093. safestrncpy(dummy_sd.status.name, ((TBL_NPC*)bl)->name, NAME_LENGTH);
  12094. sd->mapindex = (bl->m > 0) ? bl->m : mapindex_name2id(map_default.mapname);
  12095. }
  12096. // Init Group ID, Level, & permissions
  12097. sd->group_id = sd->group_level = 99;
  12098. sd->permissions |= PC_PERM_ALLPERMISSION;
  12099. }
  12100. if (!is_atcommand(fd, sd, cmd, type)) {
  12101. ShowWarning("buildin_atcommand: failed to execute command '%s'\n", cmd);
  12102. script_reportsrc(st);
  12103. return SCRIPT_CMD_FAILURE;
  12104. }
  12105. return SCRIPT_CMD_SUCCESS;
  12106. }
  12107. /*==========================================
  12108. * gmcommand [MouseJstr]
  12109. *------------------------------------------*/
  12110. BUILDIN_FUNC(atcommand) {
  12111. return atcommand_sub(st,0);
  12112. }
  12113. /** Displays a message for the player only (like system messages like "you got an apple" )
  12114. * dispbottom("<message>"{,<color>{,<char_id>}})
  12115. * @param message
  12116. * @param color Hex color default (Green)
  12117. */
  12118. BUILDIN_FUNC(dispbottom)
  12119. {
  12120. TBL_PC *sd;
  12121. int color = 0;
  12122. const char *message;
  12123. if (!script_charid2sd(4,sd))
  12124. return SCRIPT_CMD_FAILURE;
  12125. message = script_getstr(st,2);
  12126. if (script_hasdata(st,3))
  12127. color = script_getnum(st,3); // <color>
  12128. if(sd) {
  12129. if (script_hasdata(st,3))
  12130. clif_messagecolor2(sd, color, message); // [Napster]
  12131. else
  12132. clif_disp_onlyself(sd, message, (int)strlen(message));
  12133. }
  12134. return SCRIPT_CMD_SUCCESS;
  12135. }
  12136. /*===================================
  12137. * Heal portion of recovery command
  12138. *-----------------------------------*/
  12139. int recovery_sub(struct map_session_data* sd, int revive)
  12140. {
  12141. if(revive&(1|4) && pc_isdead(sd)) {
  12142. status_revive(&sd->bl, 100, 100);
  12143. clif_displaymessage(sd->fd,msg_txt(sd,16)); // You've been revived!
  12144. clif_specialeffect(&sd->bl, 77, AREA);
  12145. } else if(revive&(1|2) && !pc_isdead(sd)) {
  12146. status_percent_heal(&sd->bl, 100, 100);
  12147. clif_displaymessage(sd->fd,msg_txt(sd,680)); // You have been recovered!
  12148. }
  12149. return SCRIPT_CMD_SUCCESS;
  12150. }
  12151. /*=========================================================================
  12152. * Fully Recover a Character's HP/SP - [Capuche] & [Akinari]
  12153. * recovery <type>{,<option>,<revive_flag>{,<map name>}};
  12154. * <type> determines <option>:
  12155. * 0 : char_id
  12156. * 1 : party_id
  12157. * 2 : guild_id
  12158. * 3 : map_name
  12159. * 4 : all characters
  12160. * <revive_flag>:
  12161. * 1 : Revive and heal all players (default)
  12162. * 2 : Heal living players only
  12163. * 4 : Revive dead players only
  12164. * <map name>:
  12165. * for types 1-2 : map_name (null = all maps)
  12166. *-------------------------------------------------------------------------*/
  12167. BUILDIN_FUNC(recovery)
  12168. {
  12169. TBL_PC *sd;
  12170. int map_idx = 0, type = 0, revive = 1;
  12171. type = script_getnum(st,2);
  12172. if(script_hasdata(st,4))
  12173. revive = script_getnum(st,4);
  12174. switch(type) {
  12175. case 0:
  12176. if(script_hasdata(st,3))
  12177. sd=map_charid2sd(script_getnum(st,3));
  12178. else if((sd = script_rid2sd(st)) == NULL)
  12179. return SCRIPT_CMD_SUCCESS; //If we don't have sd by now, bail out
  12180. recovery_sub(sd, revive);
  12181. break;
  12182. case 1:
  12183. {
  12184. struct party_data* p;
  12185. //When no party given, we use invoker party
  12186. int p_id = 0, i;
  12187. if(script_hasdata(st,5)) {//Bad maps shouldn't cause issues
  12188. map_idx = map_mapname2mapid(script_getstr(st,5));
  12189. if(map_idx < 1) { //But we'll check anyways
  12190. ShowDebug("recovery: bad map name given (%s)\n", script_getstr(st,5));
  12191. return SCRIPT_CMD_FAILURE;
  12192. }
  12193. }
  12194. if(script_hasdata(st,3))
  12195. p_id = script_getnum(st,3);
  12196. else if((sd = script_rid2sd(st)))
  12197. p_id = sd->status.party_id;
  12198. p = party_search(p_id);
  12199. if(p == NULL)
  12200. return SCRIPT_CMD_SUCCESS;
  12201. for (i = 0; i < MAX_PARTY; i++) {
  12202. struct map_session_data* pl_sd;
  12203. if((!(pl_sd = p->data[i].sd) || pl_sd->status.party_id != p_id)
  12204. || (map_idx && pl_sd->bl.m != map_idx))
  12205. continue;
  12206. recovery_sub(pl_sd, revive);
  12207. }
  12208. break;
  12209. }
  12210. case 2:
  12211. {
  12212. struct guild* g;
  12213. //When no guild given, we use invoker guild
  12214. int g_id = 0, i;
  12215. if(script_hasdata(st,5)) {//Bad maps shouldn't cause issues
  12216. map_idx = map_mapname2mapid(script_getstr(st,5));
  12217. if(map_idx < 1) { //But we'll check anyways
  12218. ShowDebug("recovery: bad map name given (%s)\n", script_getstr(st,5));
  12219. return SCRIPT_CMD_FAILURE;
  12220. }
  12221. }
  12222. if(script_hasdata(st,3))
  12223. g_id = script_getnum(st,3);
  12224. else if((sd = script_rid2sd(st)))
  12225. g_id = sd->status.guild_id;
  12226. g = guild_search(g_id);
  12227. if(g == NULL)
  12228. return SCRIPT_CMD_SUCCESS;
  12229. for (i = 0; i < MAX_GUILD; i++) {
  12230. struct map_session_data* pl_sd;
  12231. if((!(pl_sd = g->member[i].sd) || pl_sd->status.guild_id != g_id)
  12232. || (map_idx && pl_sd->bl.m != map_idx))
  12233. continue;
  12234. recovery_sub(pl_sd, revive);
  12235. }
  12236. break;
  12237. }
  12238. case 3:
  12239. if(script_hasdata(st,3))
  12240. map_idx = map_mapname2mapid(script_getstr(st,3));
  12241. else if((sd = script_rid2sd(st)))
  12242. map_idx = sd->bl.m;
  12243. if(map_idx < 1)
  12244. return SCRIPT_CMD_FAILURE; //No sd and no map given - return
  12245. case 4:
  12246. {
  12247. struct s_mapiterator *iter;
  12248. struct script_data *data = script_getdata(st, 3);
  12249. get_val(st, data); // Convert into value in case of a variable
  12250. if(script_hasdata(st,3) && !data_isstring(data))
  12251. revive = script_getnum(st,3); // recovery 4,<revive_flag>;
  12252. iter = mapit_getallusers();
  12253. for (sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); sd = (TBL_PC*)mapit_next(iter)) {
  12254. if(type == 3 && sd->bl.m != map_idx)
  12255. continue;
  12256. recovery_sub(sd, revive);
  12257. }
  12258. mapit_free(iter);
  12259. break;
  12260. }
  12261. default:
  12262. ShowWarning("script: buildin_recovery: Invalid type %d\n", type);
  12263. script_pushint(st,-1);
  12264. return SCRIPT_CMD_FAILURE;
  12265. }
  12266. script_pushint(st,1); //Successfully executed without errors
  12267. return SCRIPT_CMD_SUCCESS;
  12268. }
  12269. /*==========================================
  12270. * Get your pet info: getpetinfo <type>{,<char_id>}
  12271. * n -> 0:pet_id 1:pet_class 2:pet_name
  12272. * 3:friendly 4:hungry, 5: rename flag.6:level,
  12273. * 7:game id
  12274. *------------------------------------------*/
  12275. BUILDIN_FUNC(getpetinfo)
  12276. {
  12277. TBL_PC *sd=script_rid2sd(st);
  12278. TBL_PET *pd;
  12279. int type = script_getnum(st,2);
  12280. if (!script_charid2sd(3, sd) || !(pd = sd->pd)) {
  12281. if (type == 2)
  12282. script_pushconststr(st,"null");
  12283. else
  12284. script_pushint(st,0);
  12285. return SCRIPT_CMD_SUCCESS;
  12286. }
  12287. switch(type){
  12288. case PETINFO_ID: script_pushint(st,pd->pet.pet_id); break;
  12289. case PETINFO_CLASS: script_pushint(st,pd->pet.class_); break;
  12290. case PETINFO_NAME: script_pushstrcopy(st,pd->pet.name); break;
  12291. case PETINFO_INTIMATE: script_pushint(st,pd->pet.intimate); break;
  12292. case PETINFO_HUNGRY: script_pushint(st,pd->pet.hungry); break;
  12293. case PETINFO_RENAMED: script_pushint(st,pd->pet.rename_flag); break;
  12294. case PETINFO_LEVEL: script_pushint(st,(int)pd->pet.level); break;
  12295. case PETINFO_BLOCKID: script_pushint(st,pd->bl.id); break;
  12296. default:
  12297. script_pushint(st,0);
  12298. break;
  12299. }
  12300. return SCRIPT_CMD_SUCCESS;
  12301. }
  12302. /*==========================================
  12303. * Get your homunculus info: gethominfo <type>{,<char_id>};
  12304. * type -> 0:hom_id 1:class 2:name
  12305. * 3:friendly 4:hungry, 5: rename flag.
  12306. * 6: level, 7: game id
  12307. *------------------------------------------*/
  12308. BUILDIN_FUNC(gethominfo)
  12309. {
  12310. TBL_PC *sd=script_rid2sd(st);
  12311. TBL_HOM *hd;
  12312. int type=script_getnum(st,2);
  12313. if (!script_charid2sd(3, sd) || !(hd = sd->hd)) {
  12314. if (type == 2)
  12315. script_pushconststr(st,"null");
  12316. else
  12317. script_pushint(st,0);
  12318. return SCRIPT_CMD_SUCCESS;
  12319. }
  12320. switch(type){
  12321. case 0: script_pushint(st,hd->homunculus.hom_id); break;
  12322. case 1: script_pushint(st,hd->homunculus.class_); break;
  12323. case 2: script_pushstrcopy(st,hd->homunculus.name); break;
  12324. case 3: script_pushint(st,hd->homunculus.intimacy); break;
  12325. case 4: script_pushint(st,hd->homunculus.hunger); break;
  12326. case 5: script_pushint(st,hd->homunculus.rename_flag); break;
  12327. case 6: script_pushint(st,hd->homunculus.level); break;
  12328. case 7: script_pushint(st,hd->bl.id); break;
  12329. default:
  12330. script_pushint(st,0);
  12331. break;
  12332. }
  12333. return SCRIPT_CMD_SUCCESS;
  12334. }
  12335. /// Retrieves information about character's mercenary
  12336. /// getmercinfo <type>[,<char id>];
  12337. BUILDIN_FUNC(getmercinfo)
  12338. {
  12339. int type;
  12340. struct map_session_data* sd;
  12341. struct mercenary_data* md;
  12342. type = script_getnum(st,2);
  12343. if( script_hasdata(st,3) )
  12344. {
  12345. int char_id = script_getnum(st,3);
  12346. if( ( sd = map_charid2sd(char_id) ) == NULL )
  12347. {
  12348. ShowError("buildin_getmercinfo: No such character (char_id=%d).\n", char_id);
  12349. script_pushnil(st);
  12350. return SCRIPT_CMD_FAILURE;
  12351. }
  12352. }
  12353. else
  12354. {
  12355. if( ( sd = script_rid2sd(st) ) == NULL )
  12356. {
  12357. script_pushnil(st);
  12358. return SCRIPT_CMD_SUCCESS;
  12359. }
  12360. }
  12361. md = ( sd->status.mer_id && sd->md ) ? sd->md : NULL;
  12362. switch( type )
  12363. {
  12364. case 0: script_pushint(st,md ? md->mercenary.mercenary_id : 0); break;
  12365. case 1: script_pushint(st,md ? md->mercenary.class_ : 0); break;
  12366. case 2:
  12367. if( md )
  12368. script_pushstrcopy(st,md->db->name);
  12369. else
  12370. script_pushconststr(st,"");
  12371. break;
  12372. case 3: script_pushint(st,md ? mercenary_get_faith(md) : 0); break;
  12373. case 4: script_pushint(st,md ? mercenary_get_calls(md) : 0); break;
  12374. case 5: script_pushint(st,md ? md->mercenary.kill_count : 0); break;
  12375. case 6: script_pushint(st,md ? mercenary_get_lifetime(md) : 0); break;
  12376. case 7: script_pushint(st,md ? md->db->lv : 0); break;
  12377. case 8: script_pushint(st,md ? md->bl.id : 0); break;
  12378. default:
  12379. ShowError("buildin_getmercinfo: Invalid type %d (char_id=%d).\n", type, sd->status.char_id);
  12380. script_pushnil(st);
  12381. return SCRIPT_CMD_FAILURE;
  12382. }
  12383. return SCRIPT_CMD_SUCCESS;
  12384. }
  12385. /*==========================================
  12386. * Shows wether your inventory(and equips) contain
  12387. selected card or not.
  12388. checkequipedcard(4001);
  12389. *------------------------------------------*/
  12390. BUILDIN_FUNC(checkequipedcard)
  12391. {
  12392. TBL_PC *sd=script_rid2sd(st);
  12393. if(sd){
  12394. int n,i,c=0;
  12395. c=script_getnum(st,2);
  12396. for(i=0;i<MAX_INVENTORY;i++){
  12397. if(sd->status.inventory[i].nameid > 0 && sd->status.inventory[i].amount && sd->inventory_data[i]){
  12398. if (itemdb_isspecial(sd->status.inventory[i].card[0]))
  12399. continue;
  12400. for(n=0;n<sd->inventory_data[i]->slot;n++){
  12401. if(sd->status.inventory[i].card[n]==c){
  12402. script_pushint(st,1);
  12403. return SCRIPT_CMD_SUCCESS;
  12404. }
  12405. }
  12406. }
  12407. }
  12408. }
  12409. script_pushint(st,0);
  12410. return SCRIPT_CMD_SUCCESS;
  12411. }
  12412. BUILDIN_FUNC(jump_zero)
  12413. {
  12414. int sel;
  12415. sel=script_getnum(st,2);
  12416. if(!sel) {
  12417. int pos;
  12418. if( !data_islabel(script_getdata(st,3)) ){
  12419. ShowError("script: jump_zero: not label !\n");
  12420. st->state=END;
  12421. return SCRIPT_CMD_FAILURE;
  12422. }
  12423. pos=script_getnum(st,3);
  12424. st->pos=pos;
  12425. st->state=GOTO;
  12426. }
  12427. return SCRIPT_CMD_SUCCESS;
  12428. }
  12429. /*==========================================
  12430. * movenpc [MouseJstr]
  12431. *------------------------------------------*/
  12432. BUILDIN_FUNC(movenpc)
  12433. {
  12434. TBL_NPC *nd = NULL;
  12435. const char *npc;
  12436. int x,y;
  12437. npc = script_getstr(st,2);
  12438. x = script_getnum(st,3);
  12439. y = script_getnum(st,4);
  12440. if ((nd = npc_name2id(npc)) == NULL){
  12441. ShowError("script: movenpc: NPC with ID '%s' was not found!\n", npc );
  12442. return -1;
  12443. }
  12444. if (script_hasdata(st,5))
  12445. nd->ud.dir = script_getnum(st,5) % 8;
  12446. npc_movenpc(nd, x, y);
  12447. return SCRIPT_CMD_SUCCESS;
  12448. }
  12449. /*==========================================
  12450. * message [MouseJstr]
  12451. *------------------------------------------*/
  12452. BUILDIN_FUNC(message)
  12453. {
  12454. const char *msg,*player;
  12455. TBL_PC *pl_sd = NULL;
  12456. player = script_getstr(st,2);
  12457. msg = script_getstr(st,3);
  12458. if((pl_sd=map_nick2sd((char *) player)) == NULL)
  12459. return SCRIPT_CMD_SUCCESS;
  12460. clif_displaymessage(pl_sd->fd, msg);
  12461. return SCRIPT_CMD_SUCCESS;
  12462. }
  12463. /*==========================================
  12464. * npctalk (sends message to surrounding area)
  12465. *------------------------------------------*/
  12466. BUILDIN_FUNC(npctalk)
  12467. {
  12468. const char* str;
  12469. struct npc_data* nd = (struct npc_data *)map_id2bl(st->oid);
  12470. str = script_getstr(st,2);
  12471. if(nd)
  12472. {
  12473. char name[NAME_LENGTH], message[256];
  12474. safestrncpy(name, nd->name, sizeof(name));
  12475. strtok(name, "#"); // discard extra name identifier if present
  12476. safesnprintf(message, sizeof(message), "%s : %s", name, str);
  12477. clif_disp_overhead(&nd->bl, message);
  12478. }
  12479. return SCRIPT_CMD_SUCCESS;
  12480. }
  12481. // change npc walkspeed [Valaris]
  12482. BUILDIN_FUNC(npcspeed)
  12483. {
  12484. struct npc_data* nd;
  12485. int speed;
  12486. speed = script_getnum(st,2);
  12487. nd =(struct npc_data *)map_id2bl(st->oid);
  12488. if( nd ) {
  12489. nd->speed = speed;
  12490. nd->ud.state.speed_changed = 1;
  12491. }
  12492. return SCRIPT_CMD_SUCCESS;
  12493. }
  12494. // make an npc walk to a position [Valaris]
  12495. BUILDIN_FUNC(npcwalkto)
  12496. {
  12497. struct npc_data *nd=(struct npc_data *)map_id2bl(st->oid);
  12498. int x=0,y=0;
  12499. x=script_getnum(st,2);
  12500. y=script_getnum(st,3);
  12501. if(nd) {
  12502. if (!nd->status.hp)
  12503. status_calc_npc(nd, SCO_FIRST);
  12504. else
  12505. status_calc_npc(nd, SCO_NONE);
  12506. unit_walktoxy(&nd->bl,x,y,0);
  12507. }
  12508. return SCRIPT_CMD_SUCCESS;
  12509. }
  12510. // stop an npc's movement [Valaris]
  12511. BUILDIN_FUNC(npcstop)
  12512. {
  12513. struct npc_data *nd=(struct npc_data *)map_id2bl(st->oid);
  12514. if(nd) {
  12515. unit_stop_walking(&nd->bl,1|4);
  12516. }
  12517. return SCRIPT_CMD_SUCCESS;
  12518. }
  12519. /**
  12520. * getlook(<type>{,<char_id>})
  12521. **/
  12522. BUILDIN_FUNC(getlook)
  12523. {
  12524. int type,val;
  12525. TBL_PC *sd;
  12526. if (!script_charid2sd(3,sd)) {
  12527. script_pushint(st,-1);
  12528. return SCRIPT_CMD_FAILURE;
  12529. }
  12530. type=script_getnum(st,2);
  12531. val=-1;
  12532. switch(type) {
  12533. // TODO: implement LOOK_BASE as stated in script doc
  12534. case LOOK_HAIR: val=sd->status.hair; break; //1
  12535. case LOOK_WEAPON: val=sd->status.weapon; break; //2
  12536. case LOOK_HEAD_BOTTOM: val=sd->status.head_bottom; break; //3
  12537. case LOOK_HEAD_TOP: val=sd->status.head_top; break; //4
  12538. case LOOK_HEAD_MID: val=sd->status.head_mid; break; //5
  12539. case LOOK_HAIR_COLOR: val=sd->status.hair_color; break; //6
  12540. case LOOK_CLOTHES_COLOR: val=sd->status.clothes_color; break; //7
  12541. case LOOK_SHIELD: val=sd->status.shield; break; //8
  12542. case LOOK_SHOES: break; //9
  12543. case LOOK_ROBE: val=sd->status.robe; break; //12
  12544. case LOOK_BODY2: val=sd->status.body; break; //13
  12545. }
  12546. script_pushint(st,val);
  12547. return SCRIPT_CMD_SUCCESS;
  12548. }
  12549. /**
  12550. * getsavepoint(<information type>{,<char_id>})
  12551. * @param type 0- map name, 1- x, 2- y
  12552. **/
  12553. BUILDIN_FUNC(getsavepoint)
  12554. {
  12555. TBL_PC* sd;
  12556. int type;
  12557. if (!script_charid2sd(3,sd)) {
  12558. script_pushint(st,0);
  12559. return SCRIPT_CMD_SUCCESS;
  12560. }
  12561. type = script_getnum(st,2);
  12562. switch(type) {
  12563. case 0: script_pushstrcopy(st,mapindex_id2name(sd->status.save_point.map)); break;
  12564. case 1: script_pushint(st,sd->status.save_point.x); break;
  12565. case 2: script_pushint(st,sd->status.save_point.y); break;
  12566. default:
  12567. script_pushint(st,0);
  12568. break;
  12569. }
  12570. return SCRIPT_CMD_SUCCESS;
  12571. }
  12572. /*==========================================
  12573. * Get position for char/NPC/pet/hom/merc/elem objects. Added by Lorky
  12574. *
  12575. * int getMapXY(MapName$,MapX,MapY,type,[CharName$]);
  12576. * where type:
  12577. * MapName$ - String variable for output map name
  12578. * MapX - Integer variable for output coord X
  12579. * MapY - Integer variable for output coord Y
  12580. * type - type of object
  12581. * 0 - Character coord
  12582. * 1 - NPC coord
  12583. * 2 - Pet coord
  12584. * 3 - Mob coord (see 'getunitdata')
  12585. * 4 - Homun coord
  12586. * 5 - Mercenary coord
  12587. * 6 - Elemental coord
  12588. * CharName$ - Name object. If miss or "this" the current object
  12589. *
  12590. * Return:
  12591. * 0 - success
  12592. * -1 - some error, MapName$,MapX,MapY contains unknown value.
  12593. *------------------------------------------*/
  12594. BUILDIN_FUNC(getmapxy)
  12595. {
  12596. struct block_list *bl = NULL;
  12597. TBL_PC *sd=NULL;
  12598. int64 num;
  12599. const char *name;
  12600. char prefix;
  12601. int x,y,type;
  12602. char mapname[MAP_NAME_LENGTH];
  12603. if( !data_isreference(script_getdata(st,2)) ) {
  12604. ShowWarning("script: buildin_getmapxy: not mapname variable\n");
  12605. script_pushint(st,-1);
  12606. return SCRIPT_CMD_FAILURE;
  12607. }
  12608. if( !data_isreference(script_getdata(st,3)) ) {
  12609. ShowWarning("script: buildin_getmapxy: not mapx variable\n");
  12610. script_pushint(st,-1);
  12611. return SCRIPT_CMD_FAILURE;
  12612. }
  12613. if( !data_isreference(script_getdata(st,4)) ) {
  12614. ShowWarning("script: buildin_getmapxy: not mapy variable\n");
  12615. script_pushint(st,-1);
  12616. return SCRIPT_CMD_FAILURE;
  12617. }
  12618. if( !is_string_variable(reference_getname(script_getdata(st, 2))) ) {
  12619. ShowWarning("script: buildin_getmapxy: %s is not a string variable\n",reference_getname(script_getdata(st, 2)));
  12620. script_pushint(st,-1);
  12621. return SCRIPT_CMD_FAILURE;
  12622. }
  12623. if( is_string_variable(reference_getname(script_getdata(st, 3))) ) {
  12624. ShowWarning("script: buildin_getmapxy: %s is a string variable, should be int\n",reference_getname(script_getdata(st, 3)));
  12625. script_pushint(st,-1);
  12626. return SCRIPT_CMD_FAILURE;
  12627. }
  12628. if( is_string_variable(reference_getname(script_getdata(st, 4))) ) {
  12629. ShowWarning("script: buildin_getmapxy: %s is a string variable, should be int\n",reference_getname(script_getdata(st, 4)));
  12630. script_pushint(st,-1);
  12631. return SCRIPT_CMD_FAILURE;
  12632. }
  12633. // Possible needly check function parameters on C_STR,C_INT,C_INT
  12634. type=script_getnum(st,5);
  12635. switch (type) {
  12636. case 0: //Get Character Position
  12637. if( script_hasdata(st,6) )
  12638. sd=map_nick2sd(script_getstr(st,6));
  12639. else
  12640. sd=script_rid2sd(st);
  12641. if (sd)
  12642. bl = &sd->bl;
  12643. break;
  12644. case 1: //Get NPC Position
  12645. if( script_hasdata(st,6) )
  12646. {
  12647. struct npc_data *nd;
  12648. nd=npc_name2id(script_getstr(st,6));
  12649. if (nd)
  12650. bl = &nd->bl;
  12651. } else //In case the origin is not an npc?
  12652. bl=map_id2bl(st->oid);
  12653. break;
  12654. case 2: //Get Pet Position
  12655. if(script_hasdata(st,6))
  12656. sd=map_nick2sd(script_getstr(st,6));
  12657. else
  12658. sd=script_rid2sd(st);
  12659. if (sd && sd->pd)
  12660. bl = &sd->pd->bl;
  12661. break;
  12662. case 3: //Get Mob Position
  12663. break; //see 'getunitdata'
  12664. case 4: //Get Homun Position
  12665. if(script_hasdata(st,6))
  12666. sd=map_nick2sd(script_getstr(st,6));
  12667. else
  12668. sd=script_rid2sd(st);
  12669. if (sd && sd->hd)
  12670. bl = &sd->hd->bl;
  12671. break;
  12672. case 5: //Get Mercenary Position
  12673. if(script_hasdata(st,6))
  12674. sd=map_nick2sd(script_getstr(st,6));
  12675. else
  12676. sd=script_rid2sd(st);
  12677. if (sd && sd->md)
  12678. bl = &sd->md->bl;
  12679. break;
  12680. case 6: //Get Elemental Position
  12681. if(script_hasdata(st,6))
  12682. sd=map_nick2sd(script_getstr(st,6));
  12683. else
  12684. sd=script_rid2sd(st);
  12685. if (sd && sd->ed)
  12686. bl = &sd->ed->bl;
  12687. break;
  12688. default:
  12689. ShowWarning("script: buildin_getmapxy: Invalid type %d\n", type);
  12690. script_pushint(st,-1);
  12691. return SCRIPT_CMD_FAILURE;
  12692. }
  12693. if (!bl) { //No object found.
  12694. script_pushint(st,-1);
  12695. return SCRIPT_CMD_SUCCESS;
  12696. }
  12697. x= bl->x;
  12698. y= bl->y;
  12699. safestrncpy(mapname, map[bl->m].name, MAP_NAME_LENGTH);
  12700. //Set MapName$
  12701. num=st->stack->stack_data[st->start+2].u.num;
  12702. name=get_str(script_getvarid(num));
  12703. prefix=*name;
  12704. if(not_server_variable(prefix))
  12705. sd=script_rid2sd(st);
  12706. else
  12707. sd=NULL;
  12708. set_reg(st,sd,num,name,(void*)mapname,script_getref(st,2));
  12709. //Set MapX
  12710. num=st->stack->stack_data[st->start+3].u.num;
  12711. name=get_str(script_getvarid(num));
  12712. prefix=*name;
  12713. if(not_server_variable(prefix))
  12714. sd=script_rid2sd(st);
  12715. else
  12716. sd=NULL;
  12717. set_reg(st,sd,num,name,(void*)__64BPRTSIZE(x),script_getref(st,3));
  12718. //Set MapY
  12719. num=st->stack->stack_data[st->start+4].u.num;
  12720. name=get_str(script_getvarid(num));
  12721. prefix=*name;
  12722. if(not_server_variable(prefix))
  12723. sd=script_rid2sd(st);
  12724. else
  12725. sd=NULL;
  12726. set_reg(st,sd,num,name,(void*)__64BPRTSIZE(y),script_getref(st,4));
  12727. //Return Success value
  12728. script_pushint(st,0);
  12729. return SCRIPT_CMD_SUCCESS;
  12730. }
  12731. /// Returns the map name of given map ID.
  12732. ///
  12733. /// mapid2name <map ID>;
  12734. BUILDIN_FUNC(mapid2name)
  12735. {
  12736. uint16 m = script_getnum(st, 2);
  12737. if (m < 0 || m >= MAX_MAP_PER_SERVER) {
  12738. script_pushstr(st, "");
  12739. return SCRIPT_CMD_FAILURE;
  12740. }
  12741. script_pushstrcopy(st, map_mapid2mapname(m));
  12742. return SCRIPT_CMD_SUCCESS;
  12743. }
  12744. /*==========================================
  12745. * Allows player to write NPC logs (i.e. Bank NPC, etc) [Lupus]
  12746. *------------------------------------------*/
  12747. BUILDIN_FUNC(logmes)
  12748. {
  12749. const char *str;
  12750. TBL_PC* sd;
  12751. sd = script_rid2sd(st);
  12752. if( sd == NULL )
  12753. return SCRIPT_CMD_FAILURE;
  12754. str = script_getstr(st,2);
  12755. log_npc(sd,str);
  12756. return SCRIPT_CMD_SUCCESS;
  12757. }
  12758. BUILDIN_FUNC(summon)
  12759. {
  12760. int _class, timeout=0;
  12761. const char *str,*event="";
  12762. TBL_PC *sd;
  12763. struct mob_data *md;
  12764. int tick = gettick();
  12765. sd=script_rid2sd(st);
  12766. if (!sd)
  12767. return SCRIPT_CMD_SUCCESS;
  12768. str =script_getstr(st,2);
  12769. _class=script_getnum(st,3);
  12770. if( script_hasdata(st,4) )
  12771. timeout=script_getnum(st,4);
  12772. if( script_hasdata(st,5) ){
  12773. event=script_getstr(st,5);
  12774. check_event(st, event);
  12775. }
  12776. clif_skill_poseffect(&sd->bl,AM_CALLHOMUN,1,sd->bl.x,sd->bl.y,tick);
  12777. md = mob_once_spawn_sub(&sd->bl, sd->bl.m, sd->bl.x, sd->bl.y, str, _class, event, SZ_SMALL, AI_NONE);
  12778. if (md) {
  12779. md->master_id=sd->bl.id;
  12780. md->special_state.ai = AI_ATTACK;
  12781. if( md->deletetimer != INVALID_TIMER )
  12782. delete_timer(md->deletetimer, mob_timer_delete);
  12783. md->deletetimer = add_timer(tick+(timeout>0?timeout:60000),mob_timer_delete,md->bl.id,0);
  12784. mob_spawn (md); //Now it is ready for spawning.
  12785. clif_specialeffect(&md->bl,344,AREA);
  12786. sc_start4(NULL,&md->bl, SC_MODECHANGE, 100, 1, 0, MD_AGGRESSIVE, 0, 60000);
  12787. }
  12788. script_pushint(st, md->bl.id);
  12789. return SCRIPT_CMD_SUCCESS;
  12790. }
  12791. /*==========================================
  12792. * Checks whether it is daytime/nighttime
  12793. *------------------------------------------*/
  12794. BUILDIN_FUNC(isnight)
  12795. {
  12796. script_pushint(st,(night_flag == 1));
  12797. return SCRIPT_CMD_SUCCESS;
  12798. }
  12799. BUILDIN_FUNC(isday)
  12800. {
  12801. script_pushint(st,(night_flag == 0));
  12802. return SCRIPT_CMD_SUCCESS;
  12803. }
  12804. /*================================================
  12805. * Check how many items/cards in the list are
  12806. * equipped - used for 2/15's cards patch [celest]
  12807. *------------------------------------------------*/
  12808. BUILDIN_FUNC(isequippedcnt)
  12809. {
  12810. TBL_PC *sd;
  12811. int i, id = 1;
  12812. int ret = 0;
  12813. sd = script_rid2sd(st);
  12814. if (!sd) { //If the player is not attached it is a script error anyway... but better prevent the map server from crashing...
  12815. script_pushint(st,0);
  12816. return SCRIPT_CMD_SUCCESS;
  12817. }
  12818. for (i=0; id!=0; i++) {
  12819. short j;
  12820. FETCH (i+2, id) else id = 0;
  12821. if (id <= 0)
  12822. continue;
  12823. for (j=0; j<EQI_MAX; j++) {
  12824. short index = sd->equip_index[j];
  12825. if(index < 0)
  12826. continue;
  12827. if (pc_is_same_equip_index((enum equip_index)j, sd->equip_index, index))
  12828. continue;
  12829. if(!sd->inventory_data[index])
  12830. continue;
  12831. if (itemdb_type(id) != IT_CARD) { //No card. Count amount in inventory.
  12832. if (sd->inventory_data[index]->nameid == id)
  12833. ret+= sd->status.inventory[index].amount;
  12834. } else { //Count cards.
  12835. short k;
  12836. if (itemdb_isspecial(sd->status.inventory[index].card[0]))
  12837. continue; //No cards
  12838. for(k=0; k<sd->inventory_data[index]->slot; k++) {
  12839. if (sd->status.inventory[index].card[k] == id)
  12840. ret++; //[Lupus]
  12841. }
  12842. }
  12843. }
  12844. }
  12845. script_pushint(st,ret);
  12846. return SCRIPT_CMD_SUCCESS;
  12847. }
  12848. /*================================================
  12849. * Check whether another card has been
  12850. * equipped - used for 2/15's cards patch [celest]
  12851. * -- Items checked cannot be reused in another
  12852. * card set to prevent exploits
  12853. *------------------------------------------------*/
  12854. BUILDIN_FUNC(isequipped)
  12855. {
  12856. TBL_PC *sd;
  12857. int i, id = 1;
  12858. int ret = -1;
  12859. //Original hash to reverse it when full check fails.
  12860. unsigned int setitem_hash = 0, setitem_hash2 = 0;
  12861. sd = script_rid2sd(st);
  12862. if (!sd) { //If the player is not attached it is a script error anyway... but better prevent the map server from crashing...
  12863. script_pushint(st,0);
  12864. return SCRIPT_CMD_SUCCESS;
  12865. }
  12866. setitem_hash = sd->bonus.setitem_hash;
  12867. setitem_hash2 = sd->bonus.setitem_hash2;
  12868. for (i=0; id!=0; i++) {
  12869. int flag = 0;
  12870. short j;
  12871. FETCH (i+2, id) else id = 0;
  12872. if (id <= 0)
  12873. continue;
  12874. for (j=0; j<EQI_MAX; j++) {
  12875. short index = sd->equip_index[j];
  12876. if(index < 0)
  12877. continue;
  12878. if (pc_is_same_equip_index((enum equip_index)i, sd->equip_index, index))
  12879. continue;
  12880. if(!sd->inventory_data[index])
  12881. continue;
  12882. if (itemdb_type(id) != IT_CARD) {
  12883. if (sd->inventory_data[index]->nameid != id)
  12884. continue;
  12885. flag = 1;
  12886. break;
  12887. } else { //Cards
  12888. short k;
  12889. if (sd->inventory_data[index]->slot == 0 ||
  12890. itemdb_isspecial(sd->status.inventory[index].card[0]))
  12891. continue;
  12892. for (k = 0; k < sd->inventory_data[index]->slot; k++)
  12893. { //New hash system which should support up to 4 slots on any equipment. [Skotlex]
  12894. unsigned int hash = 0;
  12895. if (sd->status.inventory[index].card[k] != id)
  12896. continue;
  12897. hash = 1<<((j<5?j:j-5)*4 + k);
  12898. // check if card is already used by another set
  12899. if ( ( j < 5 ? sd->bonus.setitem_hash : sd->bonus.setitem_hash2 ) & hash)
  12900. continue;
  12901. // We have found a match
  12902. flag = 1;
  12903. // Set hash so this card cannot be used by another
  12904. if (j<5)
  12905. sd->bonus.setitem_hash |= hash;
  12906. else
  12907. sd->bonus.setitem_hash2 |= hash;
  12908. break;
  12909. }
  12910. }
  12911. if (flag) break; //Card found
  12912. }
  12913. if (ret == -1)
  12914. ret = flag;
  12915. else
  12916. ret &= flag;
  12917. if (!ret) break;
  12918. }
  12919. if (!ret) {//When check fails, restore original hash values. [Skotlex]
  12920. sd->bonus.setitem_hash = setitem_hash;
  12921. sd->bonus.setitem_hash2 = setitem_hash2;
  12922. }
  12923. script_pushint(st,ret);
  12924. return SCRIPT_CMD_SUCCESS;
  12925. }
  12926. /*================================================
  12927. * Check how many given inserted cards in the CURRENT
  12928. * weapon - used for 2/15's cards patch [Lupus]
  12929. *------------------------------------------------*/
  12930. BUILDIN_FUNC(cardscnt)
  12931. {
  12932. TBL_PC *sd;
  12933. int i, k, id = 1;
  12934. int ret = 0;
  12935. int index;
  12936. sd = script_rid2sd(st);
  12937. for (i=0; id!=0; i++) {
  12938. FETCH (i+2, id) else id = 0;
  12939. if (id <= 0)
  12940. continue;
  12941. index = current_equip_item_index; //we get CURRENT WEAPON inventory index from status.c [Lupus]
  12942. if(index < 0) continue;
  12943. if(!sd->inventory_data[index])
  12944. continue;
  12945. if(itemdb_type(id) != IT_CARD) {
  12946. if (sd->inventory_data[index]->nameid == id)
  12947. ret+= sd->status.inventory[index].amount;
  12948. } else {
  12949. if (itemdb_isspecial(sd->status.inventory[index].card[0]))
  12950. continue;
  12951. for(k=0; k<sd->inventory_data[index]->slot; k++) {
  12952. if (sd->status.inventory[index].card[k] == id)
  12953. ret++;
  12954. }
  12955. }
  12956. }
  12957. script_pushint(st,ret);
  12958. // script_pushint(st,current_equip_item_index);
  12959. return SCRIPT_CMD_SUCCESS;
  12960. }
  12961. /*=======================================================
  12962. * Returns the refined number of the current item, or an
  12963. * item with inventory index specified
  12964. *-------------------------------------------------------*/
  12965. BUILDIN_FUNC(getrefine)
  12966. {
  12967. TBL_PC *sd;
  12968. if ((sd = script_rid2sd(st))!= NULL)
  12969. script_pushint(st,sd->status.inventory[current_equip_item_index].refine);
  12970. else
  12971. script_pushint(st,0);
  12972. return SCRIPT_CMD_SUCCESS;
  12973. }
  12974. /*=======================================================
  12975. * Day/Night controls
  12976. *-------------------------------------------------------*/
  12977. BUILDIN_FUNC(night)
  12978. {
  12979. if (night_flag != 1) map_night_timer(night_timer_tid, 0, 0, 1);
  12980. return SCRIPT_CMD_SUCCESS;
  12981. }
  12982. BUILDIN_FUNC(day)
  12983. {
  12984. if (night_flag != 0) map_day_timer(day_timer_tid, 0, 0, 1);
  12985. return SCRIPT_CMD_SUCCESS;
  12986. }
  12987. /**
  12988. * unequip <equipment slot>{,<char_id>};
  12989. * @author [Spectre]
  12990. **/
  12991. BUILDIN_FUNC(unequip) {
  12992. int pos;
  12993. TBL_PC *sd;
  12994. if (!script_charid2sd(3,sd))
  12995. return SCRIPT_CMD_FAILURE;
  12996. pos = script_getnum(st,2);
  12997. if (pos >= 1 && pos <= ARRAYLENGTH(equip)) {
  12998. short i = pc_checkequip(sd,equip[pos-1]);
  12999. if (i >= 0) {
  13000. pc_unequipitem(sd,i,1|2);
  13001. script_pushint(st, 1);
  13002. return SCRIPT_CMD_SUCCESS;
  13003. }
  13004. }
  13005. ShowError("buildin_unequip: No item equipped at pos %d (CID=%d/AID=%d).\n", pos, sd->status.char_id, sd->status.account_id);
  13006. script_pushint(st, 0);
  13007. return SCRIPT_CMD_FAILURE;
  13008. }
  13009. /**
  13010. * equip <item id>{,<char_id>};
  13011. **/
  13012. BUILDIN_FUNC(equip) {
  13013. unsigned short nameid = 0;
  13014. TBL_PC *sd;
  13015. struct item_data *item_data;
  13016. if (!script_charid2sd(3,sd))
  13017. return SCRIPT_CMD_FAILURE;
  13018. nameid = script_getnum(st,2);
  13019. if ((item_data = itemdb_exists(nameid))) {
  13020. int i;
  13021. ARR_FIND( 0, MAX_INVENTORY, i, sd->status.inventory[i].nameid == nameid );
  13022. if (i < MAX_INVENTORY) {
  13023. pc_equipitem(sd,i,item_data->equip);
  13024. script_pushint(st,1);
  13025. return SCRIPT_CMD_SUCCESS;
  13026. }
  13027. }
  13028. ShowError("buildin_equip: Item %hu cannot be equipped\n",nameid);
  13029. script_pushint(st,0);
  13030. return SCRIPT_CMD_FAILURE;
  13031. }
  13032. BUILDIN_FUNC(autoequip)
  13033. {
  13034. unsigned short nameid;
  13035. int flag;
  13036. struct item_data *item_data;
  13037. nameid=script_getnum(st,2);
  13038. flag=script_getnum(st,3);
  13039. if( ( item_data = itemdb_exists(nameid) ) == NULL )
  13040. {
  13041. ShowError("buildin_autoequip: Invalid item '%hu'.\n", nameid);
  13042. return SCRIPT_CMD_FAILURE;
  13043. }
  13044. if( !itemdb_isequip2(item_data) )
  13045. {
  13046. ShowError("buildin_autoequip: Item '%hu' cannot be equipped.\n", nameid);
  13047. return SCRIPT_CMD_FAILURE;
  13048. }
  13049. item_data->flag.autoequip = flag>0?1:0;
  13050. return SCRIPT_CMD_SUCCESS;
  13051. }
  13052. BUILDIN_FUNC(setbattleflag)
  13053. {
  13054. const char *flag, *value;
  13055. flag = script_getstr(st,2);
  13056. value = script_getstr(st,3); // HACK: Retrieve number as string (auto-converted) for battle_set_value
  13057. if (battle_set_value(flag, value) == 0)
  13058. ShowWarning("buildin_setbattleflag: unknown battle_config flag '%s'\n",flag);
  13059. else
  13060. ShowInfo("buildin_setbattleflag: battle_config flag '%s' is now set to '%s'.\n",flag,value);
  13061. return SCRIPT_CMD_SUCCESS;
  13062. }
  13063. BUILDIN_FUNC(getbattleflag)
  13064. {
  13065. const char *flag;
  13066. flag = script_getstr(st,2);
  13067. script_pushint(st,battle_get_value(flag));
  13068. return SCRIPT_CMD_SUCCESS;
  13069. }
  13070. //=======================================================
  13071. // strlen [Valaris]
  13072. //-------------------------------------------------------
  13073. BUILDIN_FUNC(getstrlen)
  13074. {
  13075. const char *str = script_getstr(st,2);
  13076. int len = (str) ? (int)strlen(str) : 0;
  13077. script_pushint(st,len);
  13078. return SCRIPT_CMD_SUCCESS;
  13079. }
  13080. //=======================================================
  13081. // isalpha [Valaris]
  13082. //-------------------------------------------------------
  13083. BUILDIN_FUNC(charisalpha)
  13084. {
  13085. const char *str=script_getstr(st,2);
  13086. int pos=script_getnum(st,3);
  13087. int val = ( str && pos >= 0 && (unsigned int)pos < strlen(str) ) ? ISALPHA( str[pos] ) != 0 : 0;
  13088. script_pushint(st,val);
  13089. return SCRIPT_CMD_SUCCESS;
  13090. }
  13091. //=======================================================
  13092. // charisupper <str>, <index>
  13093. //-------------------------------------------------------
  13094. BUILDIN_FUNC(charisupper)
  13095. {
  13096. const char *str = script_getstr(st,2);
  13097. int pos = script_getnum(st,3);
  13098. int val = ( str && pos >= 0 && (unsigned int)pos < strlen(str) ) ? ISUPPER( str[pos] ) : 0;
  13099. script_pushint(st,val);
  13100. return SCRIPT_CMD_SUCCESS;
  13101. }
  13102. //=======================================================
  13103. // charislower <str>, <index>
  13104. //-------------------------------------------------------
  13105. BUILDIN_FUNC(charislower)
  13106. {
  13107. const char *str = script_getstr(st,2);
  13108. int pos = script_getnum(st,3);
  13109. int val = ( str && pos >= 0 && (unsigned int)pos < strlen(str) ) ? ISLOWER( str[pos] ) : 0;
  13110. script_pushint(st,val);
  13111. return SCRIPT_CMD_SUCCESS;
  13112. }
  13113. //=======================================================
  13114. // charat <str>, <index>
  13115. //-------------------------------------------------------
  13116. BUILDIN_FUNC(charat) {
  13117. const char *str = script_getstr(st,2);
  13118. int pos = script_getnum(st,3);
  13119. if( pos >= 0 && (unsigned int)pos < strlen(str) ) {
  13120. char output[2];
  13121. output[0] = str[pos];
  13122. output[1] = '\0';
  13123. script_pushstrcopy(st, output);
  13124. } else
  13125. script_pushconststr(st, "");
  13126. return SCRIPT_CMD_SUCCESS;
  13127. }
  13128. //=======================================================
  13129. // setchar <string>, <char>, <index>
  13130. //-------------------------------------------------------
  13131. BUILDIN_FUNC(setchar)
  13132. {
  13133. const char *str = script_getstr(st,2);
  13134. const char *c = script_getstr(st,3);
  13135. int index = script_getnum(st,4);
  13136. char *output = aStrdup(str);
  13137. if(index >= 0 && index < strlen(output))
  13138. output[index] = *c;
  13139. script_pushstr(st, output);
  13140. return SCRIPT_CMD_SUCCESS;
  13141. }
  13142. //=======================================================
  13143. // insertchar <string>, <char>, <index>
  13144. //-------------------------------------------------------
  13145. BUILDIN_FUNC(insertchar)
  13146. {
  13147. const char *str = script_getstr(st,2);
  13148. const char *c = script_getstr(st,3);
  13149. int index = script_getnum(st,4);
  13150. char *output;
  13151. size_t len = strlen(str);
  13152. if(index < 0)
  13153. index = 0;
  13154. else if(index > len)
  13155. index = len;
  13156. output = (char*)aMalloc(len + 2);
  13157. memcpy(output, str, index);
  13158. output[index] = c[0];
  13159. memcpy(&output[index+1], &str[index], len - index);
  13160. output[len+1] = '\0';
  13161. script_pushstr(st, output);
  13162. return SCRIPT_CMD_SUCCESS;
  13163. }
  13164. //=======================================================
  13165. // delchar <string>, <index>
  13166. //-------------------------------------------------------
  13167. BUILDIN_FUNC(delchar)
  13168. {
  13169. const char *str = script_getstr(st,2);
  13170. int index = script_getnum(st,3);
  13171. char *output;
  13172. size_t len = strlen(str);
  13173. if(index < 0 || index > len) {
  13174. //return original
  13175. output = aStrdup(str);
  13176. script_pushstr(st, output);
  13177. return SCRIPT_CMD_SUCCESS;
  13178. }
  13179. output = (char*)aMalloc(len);
  13180. memcpy(output, str, index);
  13181. memcpy(&output[index], &str[index+1], len - index);
  13182. script_pushstr(st, output);
  13183. return SCRIPT_CMD_SUCCESS;
  13184. }
  13185. //=======================================================
  13186. // strtoupper <str>
  13187. //-------------------------------------------------------
  13188. BUILDIN_FUNC(strtoupper)
  13189. {
  13190. const char *str = script_getstr(st,2);
  13191. char *output = aStrdup(str);
  13192. char *cursor = output;
  13193. while (*cursor != '\0') {
  13194. *cursor = TOUPPER(*cursor);
  13195. cursor++;
  13196. }
  13197. script_pushstr(st, output);
  13198. return SCRIPT_CMD_SUCCESS;
  13199. }
  13200. //=======================================================
  13201. // strtolower <str>
  13202. //-------------------------------------------------------
  13203. BUILDIN_FUNC(strtolower)
  13204. {
  13205. const char *str = script_getstr(st,2);
  13206. char *output = aStrdup(str);
  13207. char *cursor = output;
  13208. while (*cursor != '\0') {
  13209. *cursor = TOLOWER(*cursor);
  13210. cursor++;
  13211. }
  13212. script_pushstr(st, output);
  13213. return SCRIPT_CMD_SUCCESS;
  13214. }
  13215. //=======================================================
  13216. // substr <str>, <start>, <end>
  13217. //-------------------------------------------------------
  13218. BUILDIN_FUNC(substr)
  13219. {
  13220. const char *str = script_getstr(st,2);
  13221. char *output;
  13222. int start = script_getnum(st,3);
  13223. int end = script_getnum(st,4);
  13224. int len = 0;
  13225. if(start >= 0 && end < strlen(str) && start <= end) {
  13226. len = end - start + 1;
  13227. output = (char*)aMalloc(len + 1);
  13228. memcpy(output, &str[start], len);
  13229. } else
  13230. output = (char*)aMalloc(1);
  13231. output[len] = '\0';
  13232. script_pushstr(st, output);
  13233. return SCRIPT_CMD_SUCCESS;
  13234. }
  13235. //=======================================================
  13236. // explode <dest_string_array>, <str>, <delimiter>
  13237. // Note: delimiter is limited to 1 char
  13238. //-------------------------------------------------------
  13239. BUILDIN_FUNC(explode)
  13240. {
  13241. struct script_data* data = script_getdata(st, 2);
  13242. const char *str = script_getstr(st,3);
  13243. const char delimiter = script_getstr(st, 4)[0];
  13244. int32 id;
  13245. size_t len = strlen(str);
  13246. int i = 0, j = 0;
  13247. int start;
  13248. char *temp;
  13249. const char* name;
  13250. TBL_PC* sd = NULL;
  13251. temp = (char*)aMalloc(len + 1);
  13252. if( !data_isreference(data) ) {
  13253. ShowError("script:explode: not a variable\n");
  13254. script_reportdata(data);
  13255. st->state = END;
  13256. return SCRIPT_CMD_FAILURE;// not a variable
  13257. }
  13258. id = reference_getid(data);
  13259. start = reference_getindex(data);
  13260. name = reference_getname(data);
  13261. if( !is_string_variable(name) ) {
  13262. ShowError("script:explode: not string array\n");
  13263. script_reportdata(data);
  13264. st->state = END;
  13265. return SCRIPT_CMD_FAILURE;// data type mismatch
  13266. }
  13267. if( not_server_variable(*name) ) {
  13268. sd = script_rid2sd(st);
  13269. if( sd == NULL )
  13270. return SCRIPT_CMD_SUCCESS;// no player attached
  13271. }
  13272. while(str[i] != '\0') {
  13273. if(str[i] == delimiter && start < SCRIPT_MAX_ARRAYSIZE-1) { //break at delimiter but ignore after reaching last array index
  13274. temp[j] = '\0';
  13275. set_reg(st, sd, reference_uid(id, start++), name, (void*)temp, reference_getref(data));
  13276. j = 0;
  13277. ++i;
  13278. } else {
  13279. temp[j++] = str[i++];
  13280. }
  13281. }
  13282. //set last string
  13283. temp[j] = '\0';
  13284. set_reg(st, sd, reference_uid(id, start), name, (void*)temp, reference_getref(data));
  13285. aFree(temp);
  13286. return SCRIPT_CMD_SUCCESS;
  13287. }
  13288. //=======================================================
  13289. // implode <string_array>
  13290. // implode <string_array>, <glue>
  13291. //-------------------------------------------------------
  13292. BUILDIN_FUNC(implode)
  13293. {
  13294. struct script_data* data = script_getdata(st, 2);
  13295. const char *name;
  13296. uint32 glue_len = 0, array_size, id;
  13297. char *output;
  13298. TBL_PC* sd = NULL;
  13299. if( !data_isreference(data) ) {
  13300. ShowError("script:implode: not a variable\n");
  13301. script_reportdata(data);
  13302. st->state = END;
  13303. return SCRIPT_CMD_FAILURE;// not a variable
  13304. }
  13305. id = reference_getid(data);
  13306. name = reference_getname(data);
  13307. if( !is_string_variable(name) ) {
  13308. ShowError("script:implode: not string array\n");
  13309. script_reportdata(data);
  13310. st->state = END;
  13311. return SCRIPT_CMD_FAILURE;// data type mismatch
  13312. }
  13313. if( not_server_variable(*name) ) {
  13314. sd = script_rid2sd(st);
  13315. if( sd == NULL )
  13316. return SCRIPT_CMD_SUCCESS;// no player attached
  13317. }
  13318. //count chars
  13319. array_size = script_array_highest_key(st, sd, name, reference_getref(data)) - 1;
  13320. if(array_size == -1) { //empty array check (AmsTaff)
  13321. ShowWarning("script:implode: array length = 0\n");
  13322. output = (char*)aMalloc(sizeof(char)*5);
  13323. sprintf(output,"%s","NULL");
  13324. } else {
  13325. const char *glue = NULL, *temp;
  13326. size_t len = 0;
  13327. int i, k = 0;
  13328. for(i = 0; i <= array_size; ++i) {
  13329. temp = (char*) get_val2(st, reference_uid(id, i), reference_getref(data));
  13330. len += strlen(temp);
  13331. script_removetop(st, -1, 0);
  13332. }
  13333. //allocate mem
  13334. if( script_hasdata(st,3) ) {
  13335. glue = script_getstr(st,3);
  13336. glue_len = strlen(glue);
  13337. len += glue_len * (array_size);
  13338. }
  13339. output = (char*)aMalloc(len + 1);
  13340. //build output
  13341. for(i = 0; i < array_size; ++i) {
  13342. temp = (char*) get_val2(st, reference_uid(id, i), reference_getref(data));
  13343. len = strlen(temp);
  13344. memcpy(&output[k], temp, len);
  13345. k += len;
  13346. if(glue_len != 0) {
  13347. memcpy(&output[k], glue, glue_len);
  13348. k += glue_len;
  13349. }
  13350. script_removetop(st, -1, 0);
  13351. }
  13352. temp = (char*) get_val2(st, reference_uid(id, array_size), reference_getref(data));
  13353. len = strlen(temp);
  13354. memcpy(&output[k], temp, len);
  13355. k += len;
  13356. script_removetop(st, -1, 0);
  13357. output[k] = '\0';
  13358. }
  13359. script_pushstr(st, output);
  13360. return SCRIPT_CMD_SUCCESS;
  13361. }
  13362. //=======================================================
  13363. // sprintf(<format>, ...);
  13364. // Implements C sprintf, except format %n. The resulting string is
  13365. // returned, instead of being saved in variable by reference.
  13366. //-------------------------------------------------------
  13367. BUILDIN_FUNC(sprintf)
  13368. {
  13369. unsigned int len, argc = 0, arg = 0, buf2_len = 0;
  13370. const char* format;
  13371. char* p;
  13372. char* q;
  13373. char* buf = NULL;
  13374. char* buf2 = NULL;
  13375. struct script_data* data;
  13376. StringBuf final_buf;
  13377. // Fetch init data
  13378. format = script_getstr(st, 2);
  13379. argc = script_lastdata(st)-2;
  13380. len = strlen(format);
  13381. // Skip parsing, where no parsing is required.
  13382. if(len == 0) {
  13383. script_pushconststr(st,"");
  13384. return SCRIPT_CMD_SUCCESS;
  13385. }
  13386. // Pessimistic alloc
  13387. CREATE(buf, char, len+1);
  13388. // Need not be parsed, just solve stuff like %%.
  13389. if(argc == 0) {
  13390. memcpy(buf,format,len+1);
  13391. script_pushstrcopy(st, buf);
  13392. aFree(buf);
  13393. return SCRIPT_CMD_SUCCESS;
  13394. }
  13395. safestrncpy(buf, format, len+1);
  13396. // Issue sprintf for each parameter
  13397. StringBuf_Init(&final_buf);
  13398. q = buf;
  13399. while((p = strchr(q, '%')) != NULL) {
  13400. if(p != q) {
  13401. len = p - q + 1;
  13402. if(buf2_len < len) {
  13403. RECREATE(buf2, char, len);
  13404. buf2_len = len;
  13405. }
  13406. safestrncpy(buf2, q, len);
  13407. StringBuf_AppendStr(&final_buf, buf2);
  13408. q = p;
  13409. }
  13410. p = q + 1;
  13411. if(*p == '%') { // %%
  13412. StringBuf_AppendStr(&final_buf, "%");
  13413. q+=2;
  13414. continue;
  13415. }
  13416. if(*p == 'n') { // %n
  13417. ShowWarning("buildin_sprintf: Format %%n not supported! Skipping...\n");
  13418. script_reportsrc(st);
  13419. q+=2;
  13420. continue;
  13421. }
  13422. if(arg >= argc) {
  13423. ShowError("buildin_sprintf: Not enough arguments passed!\n");
  13424. if(buf) aFree(buf);
  13425. if(buf2) aFree(buf2);
  13426. StringBuf_Destroy(&final_buf);
  13427. script_pushconststr(st,"");
  13428. return SCRIPT_CMD_FAILURE;
  13429. }
  13430. if((p = strchr(q+1, '%')) == NULL)
  13431. p = strchr(q, 0); // EOS
  13432. len = p - q + 1;
  13433. if(buf2_len < len) {
  13434. RECREATE(buf2, char, len);
  13435. buf2_len = len;
  13436. }
  13437. safestrncpy(buf2, q, len);
  13438. q = p;
  13439. // Note: This assumes the passed value being the correct
  13440. // type to the current format specifier. If not, the server
  13441. // probably crashes or returns anything else, than expected,
  13442. // but it would behave in normal code the same way so it's
  13443. // the scripter's responsibility.
  13444. data = script_getdata(st, arg+3);
  13445. if(data_isstring(data)) // String
  13446. StringBuf_Printf(&final_buf, buf2, script_getstr(st, arg+3));
  13447. else if(data_isint(data)) // Number
  13448. StringBuf_Printf(&final_buf, buf2, script_getnum(st, arg+3));
  13449. else if(data_isreference(data)) { // Variable
  13450. char* name = reference_getname(data);
  13451. if(name[strlen(name)-1]=='$') // var Str
  13452. StringBuf_Printf(&final_buf, buf2, script_getstr(st, arg+3));
  13453. else // var Int
  13454. StringBuf_Printf(&final_buf, buf2, script_getnum(st, arg+3));
  13455. } else { // Unsupported type
  13456. ShowError("buildin_sprintf: Unknown argument type!\n");
  13457. if(buf) aFree(buf);
  13458. if(buf2) aFree(buf2);
  13459. StringBuf_Destroy(&final_buf);
  13460. script_pushconststr(st,"");
  13461. return SCRIPT_CMD_FAILURE;
  13462. }
  13463. arg++;
  13464. }
  13465. // Append anything left
  13466. if(*q)
  13467. StringBuf_AppendStr(&final_buf, q);
  13468. // Passed more, than needed
  13469. if(arg < argc) {
  13470. ShowWarning("buildin_sprintf: Unused arguments passed.\n");
  13471. script_reportsrc(st);
  13472. }
  13473. script_pushstrcopy(st, StringBuf_Value(&final_buf));
  13474. if(buf) aFree(buf);
  13475. if(buf2) aFree(buf2);
  13476. StringBuf_Destroy(&final_buf);
  13477. return SCRIPT_CMD_SUCCESS;
  13478. }
  13479. //=======================================================
  13480. // sscanf(<str>, <format>, ...);
  13481. // Implements C sscanf.
  13482. //-------------------------------------------------------
  13483. BUILDIN_FUNC(sscanf){
  13484. unsigned int argc, arg = 0, len;
  13485. struct script_data* data;
  13486. struct map_session_data* sd = NULL;
  13487. const char* str;
  13488. const char* format;
  13489. const char* p;
  13490. const char* q;
  13491. char* buf = NULL;
  13492. char* buf_p;
  13493. char* ref_str = NULL;
  13494. int ref_int;
  13495. // Get data
  13496. str = script_getstr(st, 2);
  13497. format = script_getstr(st, 3);
  13498. argc = script_lastdata(st)-3;
  13499. len = strlen(format);
  13500. if (len != 0 && strlen(str) == 0) {
  13501. // If the source string is empty but the format string is not, we return -1
  13502. // according to the C specs. (if the format string is also empty, we shall
  13503. // continue and return 0: 0 conversions took place out of the 0 attempted.)
  13504. script_pushint(st, -1);
  13505. return SCRIPT_CMD_SUCCESS;
  13506. }
  13507. CREATE(buf, char, len*2+1);
  13508. // Issue sscanf for each parameter
  13509. *buf = 0;
  13510. q = format;
  13511. while((p = strchr(q, '%'))){
  13512. if(p!=q){
  13513. strncat(buf, q, (size_t)(p-q));
  13514. q = p;
  13515. }
  13516. p = q+1;
  13517. if(*p=='*' || *p=='%'){ // Skip
  13518. strncat(buf, q, 2);
  13519. q+=2;
  13520. continue;
  13521. }
  13522. if(arg>=argc){
  13523. ShowError("buildin_sscanf: Not enough arguments passed!\n");
  13524. script_pushint(st, -1);
  13525. if(buf) aFree(buf);
  13526. if(ref_str) aFree(ref_str);
  13527. return SCRIPT_CMD_FAILURE;
  13528. }
  13529. if((p = strchr(q+1, '%'))==NULL){
  13530. p = strchr(q, 0); // EOS
  13531. }
  13532. len = p-q;
  13533. strncat(buf, q, len);
  13534. q = p;
  13535. // Validate output
  13536. data = script_getdata(st, arg+4);
  13537. if(!data_isreference(data) || !reference_tovariable(data)){
  13538. ShowError("buildin_sscanf: Target argument is not a variable!\n");
  13539. script_pushint(st, -1);
  13540. if(buf) aFree(buf);
  13541. if(ref_str) aFree(ref_str);
  13542. return SCRIPT_CMD_FAILURE;
  13543. }
  13544. buf_p = reference_getname(data);
  13545. if(not_server_variable(*buf_p) && (sd = script_rid2sd(st))==NULL){
  13546. script_pushint(st, -1);
  13547. if(buf) aFree(buf);
  13548. if(ref_str) aFree(ref_str);
  13549. return SCRIPT_CMD_SUCCESS;
  13550. }
  13551. // Save value if any
  13552. if(buf_p[strlen(buf_p)-1]=='$'){ // String
  13553. if(ref_str==NULL){
  13554. CREATE(ref_str, char, strlen(str)+1);
  13555. }
  13556. if(sscanf(str, buf, ref_str)==0){
  13557. break;
  13558. }
  13559. set_reg(st, sd, reference_uid( reference_getid(data), reference_getindex(data) ), buf_p, (void *)(ref_str), reference_getref(data));
  13560. } else { // Number
  13561. if(sscanf(str, buf, &ref_int)==0){
  13562. break;
  13563. }
  13564. set_reg(st, sd, reference_uid( reference_getid(data), reference_getindex(data) ), buf_p, (void *)__64BPRTSIZE(ref_int), reference_getref(data));
  13565. }
  13566. arg++;
  13567. // Disable used format (%... -> %*...)
  13568. buf_p = strchr(buf, 0);
  13569. memmove(buf_p-len+2, buf_p-len+1, len);
  13570. *(buf_p-len+1) = '*';
  13571. }
  13572. script_pushint(st, arg);
  13573. if(buf) aFree(buf);
  13574. if(ref_str) aFree(ref_str);
  13575. return SCRIPT_CMD_SUCCESS;
  13576. }
  13577. //=======================================================
  13578. // strpos(<haystack>, <needle>)
  13579. // strpos(<haystack>, <needle>, <offset>)
  13580. //
  13581. // Implements PHP style strpos. Adapted from code from
  13582. // http://www.daniweb.com/code/snippet313.html, Dave Sinkula
  13583. //-------------------------------------------------------
  13584. BUILDIN_FUNC(strpos) {
  13585. const char *haystack = script_getstr(st,2);
  13586. const char *needle = script_getstr(st,3);
  13587. int i;
  13588. size_t len;
  13589. if( script_hasdata(st,4) )
  13590. i = script_getnum(st,4);
  13591. else
  13592. i = 0;
  13593. if (needle[0] == '\0') {
  13594. script_pushint(st, -1);
  13595. return SCRIPT_CMD_SUCCESS;
  13596. }
  13597. len = strlen(haystack);
  13598. for ( ; i < len; ++i ) {
  13599. if ( haystack[i] == *needle ) {
  13600. // matched starting char -- loop through remaining chars
  13601. const char *h, *n;
  13602. for ( h = &haystack[i], n = needle; *h && *n; ++h, ++n ) {
  13603. if ( *h != *n ) {
  13604. break;
  13605. }
  13606. }
  13607. if ( !*n ) { // matched all of 'needle' to null termination
  13608. script_pushint(st, i);
  13609. return SCRIPT_CMD_SUCCESS;
  13610. }
  13611. }
  13612. }
  13613. script_pushint(st, -1);
  13614. return SCRIPT_CMD_SUCCESS;
  13615. }
  13616. //===============================================================
  13617. // replacestr <input>, <search>, <replace>{, <usecase>{, <count>}}
  13618. //
  13619. // Note: Finds all instances of <search> in <input> and replaces
  13620. // with <replace>. If specified will only replace as many
  13621. // instances as specified in <count>. By default will be case
  13622. // sensitive.
  13623. //---------------------------------------------------------------
  13624. BUILDIN_FUNC(replacestr)
  13625. {
  13626. const char *input = script_getstr(st, 2);
  13627. const char *find = script_getstr(st, 3);
  13628. const char *replace = script_getstr(st, 4);
  13629. size_t inputlen = strlen(input);
  13630. size_t findlen = strlen(find);
  13631. struct StringBuf output;
  13632. bool usecase = true;
  13633. int count = 0;
  13634. int numFinds = 0;
  13635. int i = 0, f = 0;
  13636. if(findlen == 0) {
  13637. ShowError("script:replacestr: Invalid search length.\n");
  13638. st->state = END;
  13639. return SCRIPT_CMD_FAILURE;
  13640. }
  13641. if(script_hasdata(st, 5)) {
  13642. struct script_data *data = script_getdata(st, 5);
  13643. get_val(st, data); // Convert into value in case of a variable
  13644. if( !data_isstring(data) )
  13645. usecase = script_getnum(st, 5) != 0;
  13646. else {
  13647. ShowError("script:replacestr: Invalid usecase value. Expected int got string\n");
  13648. st->state = END;
  13649. return SCRIPT_CMD_FAILURE;
  13650. }
  13651. }
  13652. if(script_hasdata(st, 6)) {
  13653. count = script_getnum(st, 6);
  13654. if(count == 0) {
  13655. ShowError("script:replacestr: Invalid count value. Expected int got string\n");
  13656. st->state = END;
  13657. return SCRIPT_CMD_FAILURE;
  13658. }
  13659. }
  13660. StringBuf_Init(&output);
  13661. for(; i < inputlen; i++) {
  13662. if(count && count == numFinds) { //found enough, stop looking
  13663. break;
  13664. }
  13665. for(f = 0; f <= findlen; f++) {
  13666. if(f == findlen) { //complete match
  13667. numFinds++;
  13668. StringBuf_AppendStr(&output, replace);
  13669. i += findlen - 1;
  13670. break;
  13671. } else {
  13672. if(usecase) {
  13673. if((i + f) > inputlen || input[i + f] != find[f]) {
  13674. StringBuf_Printf(&output, "%c", input[i]);
  13675. break;
  13676. }
  13677. } else {
  13678. if(((i + f) > inputlen || input[i + f] != find[f]) && TOUPPER(input[i+f]) != TOUPPER(find[f])) {
  13679. StringBuf_Printf(&output, "%c", input[i]);
  13680. break;
  13681. }
  13682. }
  13683. }
  13684. }
  13685. }
  13686. //append excess after enough found
  13687. if(i < inputlen)
  13688. StringBuf_AppendStr(&output, &(input[i]));
  13689. script_pushstrcopy(st, StringBuf_Value(&output));
  13690. StringBuf_Destroy(&output);
  13691. return SCRIPT_CMD_SUCCESS;
  13692. }
  13693. //========================================================
  13694. // countstr <input>, <search>{, <usecase>}
  13695. //
  13696. // Note: Counts the number of times <search> occurs in
  13697. // <input>. By default will be case sensitive.
  13698. //--------------------------------------------------------
  13699. BUILDIN_FUNC(countstr)
  13700. {
  13701. const char *input = script_getstr(st, 2);
  13702. const char *find = script_getstr(st, 3);
  13703. size_t inputlen = strlen(input);
  13704. size_t findlen = strlen(find);
  13705. bool usecase = true;
  13706. int numFinds = 0;
  13707. int i = 0, f = 0;
  13708. if(findlen == 0) {
  13709. ShowError("script:countstr: Invalid search length.\n");
  13710. st->state = END;
  13711. return SCRIPT_CMD_FAILURE;
  13712. }
  13713. if(script_hasdata(st, 4)) {
  13714. struct script_data *data;
  13715. data = script_getdata(st, 4);
  13716. get_val(st, data); // Convert into value in case of a variable
  13717. if( !data_isstring(data) )
  13718. usecase = script_getnum(st, 4) != 0;
  13719. else {
  13720. ShowError("script:countstr: Invalid usecase value. Expected int got string\n");
  13721. st->state = END;
  13722. return SCRIPT_CMD_FAILURE;
  13723. }
  13724. }
  13725. for(; i < inputlen; i++) {
  13726. for(f = 0; f <= findlen; f++) {
  13727. if(f == findlen) { //complete match
  13728. numFinds++;
  13729. i += findlen - 1;
  13730. break;
  13731. } else {
  13732. if(usecase) {
  13733. if((i + f) > inputlen || input[i + f] != find[f]) {
  13734. break;
  13735. }
  13736. } else {
  13737. if(((i + f) > inputlen || input[i + f] != find[f]) && TOUPPER(input[i+f]) != TOUPPER(find[f])) {
  13738. break;
  13739. }
  13740. }
  13741. }
  13742. }
  13743. }
  13744. script_pushint(st, numFinds);
  13745. return SCRIPT_CMD_SUCCESS;
  13746. }
  13747. /// Changes the display name and/or display class of the npc.
  13748. /// Returns 0 is successful, 1 if the npc does not exist.
  13749. ///
  13750. /// setnpcdisplay("<npc name>", "<new display name>", <new class id>, <new size>) -> <int>
  13751. /// setnpcdisplay("<npc name>", "<new display name>", <new class id>) -> <int>
  13752. /// setnpcdisplay("<npc name>", "<new display name>") -> <int>
  13753. /// setnpcdisplay("<npc name>", <new class id>) -> <int>
  13754. BUILDIN_FUNC(setnpcdisplay)
  13755. {
  13756. const char* name;
  13757. const char* newname = NULL;
  13758. int class_ = -1, size = -1;
  13759. struct script_data* data;
  13760. struct npc_data* nd;
  13761. name = script_getstr(st,2);
  13762. data = script_getdata(st,3);
  13763. if( script_hasdata(st,4) )
  13764. class_ = script_getnum(st,4);
  13765. if( script_hasdata(st,5) )
  13766. size = script_getnum(st,5);
  13767. get_val(st, data);
  13768. if( data_isstring(data) )
  13769. newname = conv_str(st,data);
  13770. else if( data_isint(data) )
  13771. class_ = conv_num(st,data);
  13772. else
  13773. {
  13774. ShowError("script:setnpcdisplay: expected string or number\n");
  13775. script_reportdata(data);
  13776. return SCRIPT_CMD_FAILURE;
  13777. }
  13778. nd = npc_name2id(name);
  13779. if( nd == NULL )
  13780. {// not found
  13781. script_pushint(st,1);
  13782. return SCRIPT_CMD_SUCCESS;
  13783. }
  13784. // update npc
  13785. if( newname )
  13786. npc_setdisplayname(nd, newname);
  13787. if( size != -1 && size != (int)nd->size )
  13788. nd->size = size;
  13789. else
  13790. size = -1;
  13791. if( class_ != -1 && nd->class_ != class_ )
  13792. npc_setclass(nd, class_);
  13793. else if( size != -1 )
  13794. { // Required to update the visual size
  13795. clif_clearunit_area(&nd->bl, CLR_OUTSIGHT);
  13796. clif_spawn(&nd->bl);
  13797. }
  13798. script_pushint(st,0);
  13799. return SCRIPT_CMD_SUCCESS;
  13800. }
  13801. BUILDIN_FUNC(atoi)
  13802. {
  13803. const char *value;
  13804. value = script_getstr(st,2);
  13805. script_pushint(st,atoi(value));
  13806. return SCRIPT_CMD_SUCCESS;
  13807. }
  13808. BUILDIN_FUNC(axtoi)
  13809. {
  13810. const char *hex = script_getstr(st,2);
  13811. long value = strtol(hex, NULL, 16);
  13812. #if LONG_MAX > INT_MAX || LONG_MIN < INT_MIN
  13813. value = cap_value(value, INT_MIN, INT_MAX);
  13814. #endif
  13815. script_pushint(st, (int)value);
  13816. return SCRIPT_CMD_SUCCESS;
  13817. }
  13818. BUILDIN_FUNC(strtol)
  13819. {
  13820. const char *string = script_getstr(st, 2);
  13821. int base = script_getnum(st, 3);
  13822. long value = strtol(string, NULL, base);
  13823. #if LONG_MAX > INT_MAX || LONG_MIN < INT_MIN
  13824. value = cap_value(value, INT_MIN, INT_MAX);
  13825. #endif
  13826. script_pushint(st, (int)value);
  13827. return SCRIPT_CMD_SUCCESS;
  13828. }
  13829. // case-insensitive substring search [lordalfa]
  13830. BUILDIN_FUNC(compare)
  13831. {
  13832. const char *message;
  13833. const char *cmpstring;
  13834. message = script_getstr(st,2);
  13835. cmpstring = script_getstr(st,3);
  13836. script_pushint(st,(stristr(message,cmpstring) != NULL));
  13837. return SCRIPT_CMD_SUCCESS;
  13838. }
  13839. BUILDIN_FUNC(strcmp)
  13840. {
  13841. const char *str1;
  13842. const char *str2;
  13843. str1 = script_getstr(st,2);
  13844. str2 = script_getstr(st,3);
  13845. script_pushint(st,strcmp(str1, str2));
  13846. return SCRIPT_CMD_SUCCESS;
  13847. }
  13848. // [zBuffer] List of mathematics commands --->
  13849. BUILDIN_FUNC(sqrt)
  13850. {
  13851. double i, a;
  13852. i = script_getnum(st,2);
  13853. a = sqrt(i);
  13854. script_pushint(st,(int)a);
  13855. return SCRIPT_CMD_SUCCESS;
  13856. }
  13857. BUILDIN_FUNC(pow)
  13858. {
  13859. double i, a, b;
  13860. a = script_getnum(st,2);
  13861. b = script_getnum(st,3);
  13862. i = pow(a,b);
  13863. script_pushint(st,(int)i);
  13864. return SCRIPT_CMD_SUCCESS;
  13865. }
  13866. BUILDIN_FUNC(distance)
  13867. {
  13868. int x0, y0, x1, y1;
  13869. x0 = script_getnum(st,2);
  13870. y0 = script_getnum(st,3);
  13871. x1 = script_getnum(st,4);
  13872. y1 = script_getnum(st,5);
  13873. script_pushint(st,distance_xy(x0,y0,x1,y1));
  13874. return SCRIPT_CMD_SUCCESS;
  13875. }
  13876. // <--- [zBuffer] List of mathematics commands
  13877. BUILDIN_FUNC(md5)
  13878. {
  13879. const char *tmpstr;
  13880. char *md5str;
  13881. tmpstr = script_getstr(st,2);
  13882. md5str = (char *)aMalloc((32+1)*sizeof(char));
  13883. MD5_String(tmpstr, md5str);
  13884. script_pushstr(st, md5str);
  13885. return SCRIPT_CMD_SUCCESS;
  13886. }
  13887. // [zBuffer] List of dynamic var commands --->
  13888. /**
  13889. * setd "<variable name>",<value>{,<char_id>};
  13890. **/
  13891. BUILDIN_FUNC(setd)
  13892. {
  13893. TBL_PC *sd = NULL;
  13894. char varname[100];
  13895. const char *buffer;
  13896. int elem;
  13897. buffer = script_getstr(st, 2);
  13898. if(sscanf(buffer, "%99[^[][%11d]", varname, &elem) < 2)
  13899. elem = 0;
  13900. if( not_server_variable(*varname) ) {
  13901. if (!script_charid2sd(4,sd))
  13902. return SCRIPT_CMD_FAILURE;
  13903. }
  13904. if( is_string_variable(varname) ) {
  13905. setd_sub(st, sd, varname, elem, (void *)script_getstr(st, 3), NULL);
  13906. } else {
  13907. setd_sub(st, sd, varname, elem, (void *)__64BPRTSIZE(script_getnum(st, 3)), NULL);
  13908. }
  13909. return SCRIPT_CMD_SUCCESS;
  13910. }
  13911. int buildin_query_sql_sub(struct script_state* st, Sql* handle)
  13912. {
  13913. int i, j;
  13914. TBL_PC* sd = NULL;
  13915. const char* query;
  13916. struct script_data* data;
  13917. const char* name;
  13918. unsigned int max_rows = SCRIPT_MAX_ARRAYSIZE; // maximum number of rows
  13919. int num_vars;
  13920. int num_cols;
  13921. // check target variables
  13922. for( i = 3; script_hasdata(st,i); ++i ) {
  13923. data = script_getdata(st, i);
  13924. if( data_isreference(data) ) { // it's a variable
  13925. name = reference_getname(data);
  13926. if( not_server_variable(*name) && sd == NULL ) { // requires a player
  13927. sd = script_rid2sd(st);
  13928. if( sd == NULL ) { // no player attached
  13929. script_reportdata(data);
  13930. st->state = END;
  13931. return SCRIPT_CMD_FAILURE;
  13932. }
  13933. }
  13934. } else {
  13935. ShowError("script:query_sql: not a variable\n");
  13936. script_reportdata(data);
  13937. st->state = END;
  13938. return SCRIPT_CMD_FAILURE;
  13939. }
  13940. }
  13941. num_vars = i - 3;
  13942. // Execute the query
  13943. query = script_getstr(st,2);
  13944. if( SQL_ERROR == Sql_QueryStr(handle, query) ) {
  13945. Sql_ShowDebug(handle);
  13946. script_pushint(st, -1);
  13947. return SCRIPT_CMD_FAILURE;
  13948. }
  13949. if( Sql_NumRows(handle) == 0 ) { // No data received
  13950. Sql_FreeResult(handle);
  13951. script_pushint(st, -1);
  13952. return SCRIPT_CMD_SUCCESS;
  13953. }
  13954. // Count the number of columns to store
  13955. num_cols = Sql_NumColumns(handle);
  13956. if( num_vars < num_cols ) {
  13957. ShowWarning("script:query_sql: Too many columns, discarding last %u columns.\n", (unsigned int)(num_cols-num_vars));
  13958. script_reportsrc(st);
  13959. } else if( num_vars > num_cols ) {
  13960. ShowWarning("script:query_sql: Too many variables (%u extra).\n", (unsigned int)(num_vars-num_cols));
  13961. script_reportsrc(st);
  13962. }
  13963. // Store data
  13964. for( i = 0; i < max_rows && SQL_SUCCESS == Sql_NextRow(handle); ++i ) {
  13965. for( j = 0; j < num_vars; ++j ) {
  13966. char* str = NULL;
  13967. if( j < num_cols )
  13968. Sql_GetData(handle, j, &str, NULL);
  13969. data = script_getdata(st, j+3);
  13970. name = reference_getname(data);
  13971. if( is_string_variable(name) )
  13972. setd_sub(st, sd, name, i, (void *)(str?str:""), reference_getref(data));
  13973. else
  13974. setd_sub(st, sd, name, i, (void *)__64BPRTSIZE((str?atoi(str):0)), reference_getref(data));
  13975. }
  13976. }
  13977. if( i == max_rows && max_rows < Sql_NumRows(handle) ) {
  13978. ShowWarning("script:query_sql: Only %d/%u rows have been stored.\n", max_rows, (unsigned int)Sql_NumRows(handle));
  13979. script_reportsrc(st);
  13980. }
  13981. // Free data
  13982. Sql_FreeResult(handle);
  13983. script_pushint(st, i);
  13984. return SCRIPT_CMD_SUCCESS;
  13985. }
  13986. BUILDIN_FUNC(query_sql) {
  13987. #ifdef BETA_THREAD_TEST
  13988. if( st->state != RERUNLINE ) {
  13989. queryThread_add(st,false);
  13990. st->state = RERUNLINE;/* will continue when the query is finished running. */
  13991. } else
  13992. st->state = RUN;
  13993. return SCRIPT_CMD_SUCCESS;
  13994. #else
  13995. return buildin_query_sql_sub(st, qsmysql_handle);
  13996. #endif
  13997. }
  13998. BUILDIN_FUNC(query_logsql) {
  13999. if( !log_config.sql_logs ) {// logmysql_handle == NULL
  14000. ShowWarning("buildin_query_logsql: SQL logs are disabled, query '%s' will not be executed.\n", script_getstr(st,2));
  14001. script_pushint(st,-1);
  14002. return SCRIPT_CMD_FAILURE;
  14003. }
  14004. #ifdef BETA_THREAD_TEST
  14005. if( st->state != RERUNLINE ) {
  14006. queryThread_add(st,true);
  14007. st->state = RERUNLINE;/* will continue when the query is finished running. */
  14008. } else
  14009. st->state = RUN;
  14010. return SCRIPT_CMD_SUCCESS;
  14011. #else
  14012. return buildin_query_sql_sub(st, logmysql_handle);
  14013. #endif
  14014. }
  14015. //Allows escaping of a given string.
  14016. BUILDIN_FUNC(escape_sql)
  14017. {
  14018. const char *str;
  14019. char *esc_str;
  14020. size_t len;
  14021. str = script_getstr(st,2);
  14022. len = strlen(str);
  14023. esc_str = (char*)aMalloc(len*2+1);
  14024. Sql_EscapeStringLen(mmysql_handle, esc_str, str, len);
  14025. script_pushstr(st, esc_str);
  14026. return SCRIPT_CMD_SUCCESS;
  14027. }
  14028. BUILDIN_FUNC(getd)
  14029. {
  14030. char varname[100];
  14031. const char *buffer;
  14032. int elem;
  14033. buffer = script_getstr(st, 2);
  14034. if(sscanf(buffer, "%99[^[][%11d]", varname, &elem) < 2)
  14035. elem = 0;
  14036. // Push the 'pointer' so it's more flexible [Lance]
  14037. push_val(st->stack, C_NAME, reference_uid(add_str(varname), elem));
  14038. return SCRIPT_CMD_SUCCESS;
  14039. }
  14040. BUILDIN_FUNC(callshop)
  14041. {
  14042. TBL_PC *sd = NULL;
  14043. struct npc_data *nd;
  14044. const char *shopname;
  14045. int flag = 0;
  14046. sd = script_rid2sd(st);
  14047. if (!sd) {
  14048. script_pushint(st,0);
  14049. return SCRIPT_CMD_SUCCESS;
  14050. }
  14051. shopname = script_getstr(st, 2);
  14052. if (script_hasdata(st,3))
  14053. flag = script_getnum(st,3);
  14054. nd = npc_name2id(shopname);
  14055. 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) ) {
  14056. ShowError("buildin_callshop: Shop [%s] not found (or NPC is not shop type)\n", shopname);
  14057. script_pushint(st,0);
  14058. return SCRIPT_CMD_FAILURE;
  14059. }
  14060. if (nd->subtype == NPCTYPE_SHOP || nd->subtype == NPCTYPE_ITEMSHOP || nd->subtype == NPCTYPE_POINTSHOP) {
  14061. // flag the user as using a valid script call for opening the shop (for floating NPCs)
  14062. sd->state.callshop = 1;
  14063. switch (flag) {
  14064. case 1: npc_buysellsel(sd,nd->bl.id,0); break; //Buy window
  14065. case 2: npc_buysellsel(sd,nd->bl.id,1); break; //Sell window
  14066. default: clif_npcbuysell(sd,nd->bl.id); break; //Show menu
  14067. }
  14068. }
  14069. #if PACKETVER >= 20131223
  14070. else if (nd->subtype == NPCTYPE_MARKETSHOP) {
  14071. unsigned short i;
  14072. for (i = 0; i < nd->u.shop.count; i++) {
  14073. if (nd->u.shop.shop_item[i].qty)
  14074. break;
  14075. }
  14076. if (i == nd->u.shop.count) {
  14077. clif_colormes(sd->fd, color_table[COLOR_RED], msg_txt(sd, 534));
  14078. return SCRIPT_CMD_FAILURE;
  14079. }
  14080. sd->npc_shopid = nd->bl.id;
  14081. clif_npc_market_open(sd, nd);
  14082. script_pushint(st,1);
  14083. return SCRIPT_CMD_SUCCESS;
  14084. }
  14085. #endif
  14086. else
  14087. clif_cashshop_show(sd, nd);
  14088. sd->npc_shopid = nd->bl.id;
  14089. script_pushint(st,1);
  14090. return SCRIPT_CMD_SUCCESS;
  14091. }
  14092. BUILDIN_FUNC(npcshopitem)
  14093. {
  14094. const char* npcname = script_getstr(st, 2);
  14095. struct npc_data* nd = npc_name2id(npcname);
  14096. int n, i;
  14097. int amount;
  14098. uint16 offs = 2;
  14099. if( !nd || ( nd->subtype != NPCTYPE_SHOP && nd->subtype != NPCTYPE_CASHSHOP && nd->subtype != NPCTYPE_ITEMSHOP && nd->subtype != NPCTYPE_POINTSHOP && nd->subtype != NPCTYPE_MARKETSHOP ) ) { // Not found.
  14100. script_pushint(st,0);
  14101. return SCRIPT_CMD_SUCCESS;
  14102. }
  14103. #if PACKETVER >= 20131223
  14104. if (nd->subtype == NPCTYPE_MARKETSHOP) {
  14105. offs = 3;
  14106. npc_market_delfromsql_(nd->exname, 0, true);
  14107. }
  14108. #endif
  14109. // get the count of new entries
  14110. amount = (script_lastdata(st)-2)/offs;
  14111. // generate new shop item list
  14112. RECREATE(nd->u.shop.shop_item, struct npc_item_list, amount);
  14113. for (n = 0, i = 3; n < amount; n++, i+=offs) {
  14114. nd->u.shop.shop_item[n].nameid = script_getnum(st,i);
  14115. nd->u.shop.shop_item[n].value = script_getnum(st,i+1);
  14116. #if PACKETVER >= 20131223
  14117. if (nd->subtype == NPCTYPE_MARKETSHOP) {
  14118. nd->u.shop.shop_item[n].qty = script_getnum(st,i+2);
  14119. nd->u.shop.shop_item[n].flag = 1;
  14120. npc_market_tosql(nd->exname, &nd->u.shop.shop_item[n]);
  14121. }
  14122. #endif
  14123. }
  14124. nd->u.shop.count = n;
  14125. script_pushint(st,1);
  14126. return SCRIPT_CMD_SUCCESS;
  14127. }
  14128. BUILDIN_FUNC(npcshopadditem)
  14129. {
  14130. const char* npcname = script_getstr(st,2);
  14131. struct npc_data* nd = npc_name2id(npcname);
  14132. int n, i;
  14133. int amount;
  14134. uint16 offs = 2;
  14135. if (!nd || ( nd->subtype != NPCTYPE_SHOP && nd->subtype != NPCTYPE_CASHSHOP && nd->subtype != NPCTYPE_ITEMSHOP && nd->subtype != NPCTYPE_POINTSHOP && nd->subtype != NPCTYPE_MARKETSHOP)) { // Not found.
  14136. script_pushint(st,0);
  14137. return SCRIPT_CMD_SUCCESS;
  14138. }
  14139. if (nd->subtype == NPCTYPE_MARKETSHOP)
  14140. offs = 3;
  14141. // get the count of new entries
  14142. amount = (script_lastdata(st)-2)/offs;
  14143. #if PACKETVER >= 20131223
  14144. if (nd->subtype == NPCTYPE_MARKETSHOP) {
  14145. for (n = 0, i = 3; n < amount; n++, i += offs) {
  14146. uint16 nameid = script_getnum(st,i), j;
  14147. // Check existing entries
  14148. ARR_FIND(0, nd->u.shop.count, j, nd->u.shop.shop_item[j].nameid == nameid);
  14149. if (j == nd->u.shop.count) {
  14150. RECREATE(nd->u.shop.shop_item, struct npc_item_list, nd->u.shop.count+1);
  14151. j = nd->u.shop.count;
  14152. nd->u.shop.shop_item[j].flag = 1;
  14153. nd->u.shop.count++;
  14154. }
  14155. nd->u.shop.shop_item[j].nameid = nameid;
  14156. nd->u.shop.shop_item[j].value = script_getnum(st,i+1);
  14157. nd->u.shop.shop_item[j].qty = script_getnum(st,i+2);
  14158. npc_market_tosql(nd->exname, &nd->u.shop.shop_item[j]);
  14159. }
  14160. script_pushint(st,1);
  14161. return SCRIPT_CMD_SUCCESS;
  14162. }
  14163. #endif
  14164. // append new items to existing shop item list
  14165. RECREATE(nd->u.shop.shop_item, struct npc_item_list, nd->u.shop.count+amount);
  14166. for (n = nd->u.shop.count, i = 3; n < nd->u.shop.count+amount; n++, i+=offs)
  14167. {
  14168. nd->u.shop.shop_item[n].nameid = script_getnum(st,i);
  14169. nd->u.shop.shop_item[n].value = script_getnum(st,i+1);
  14170. }
  14171. nd->u.shop.count = n;
  14172. script_pushint(st,1);
  14173. return SCRIPT_CMD_SUCCESS;
  14174. }
  14175. BUILDIN_FUNC(npcshopdelitem)
  14176. {
  14177. const char* npcname = script_getstr(st,2);
  14178. struct npc_data* nd = npc_name2id(npcname);
  14179. int n, i;
  14180. int amount;
  14181. int size;
  14182. if (!nd || ( nd->subtype != NPCTYPE_SHOP && nd->subtype != NPCTYPE_CASHSHOP && nd->subtype != NPCTYPE_ITEMSHOP && nd->subtype != NPCTYPE_POINTSHOP && nd->subtype != NPCTYPE_MARKETSHOP)) { // Not found.
  14183. script_pushint(st,0);
  14184. return SCRIPT_CMD_SUCCESS;
  14185. }
  14186. amount = script_lastdata(st)-2;
  14187. size = nd->u.shop.count;
  14188. // remove specified items from the shop item list
  14189. for( i = 3; i < 3 + amount; i++ ) {
  14190. unsigned short nameid = script_getnum(st,i);
  14191. ARR_FIND( 0, size, n, nd->u.shop.shop_item[n].nameid == nameid );
  14192. if( n < size ) {
  14193. if (n+1 != size)
  14194. memmove(&nd->u.shop.shop_item[n], &nd->u.shop.shop_item[n+1], sizeof(nd->u.shop.shop_item[0])*(size-n));
  14195. #if PACKETVER >= 20131223
  14196. if (nd->subtype == NPCTYPE_MARKETSHOP)
  14197. npc_market_delfromsql_(nd->exname, nameid, false);
  14198. #endif
  14199. size--;
  14200. }
  14201. }
  14202. RECREATE(nd->u.shop.shop_item, struct npc_item_list, size);
  14203. nd->u.shop.count = size;
  14204. script_pushint(st,1);
  14205. return SCRIPT_CMD_SUCCESS;
  14206. }
  14207. /**
  14208. * Sets a script to attach to a shop npc.
  14209. * npcshopattach "<npc_name>";
  14210. **/
  14211. BUILDIN_FUNC(npcshopattach)
  14212. {
  14213. const char* npcname = script_getstr(st,2);
  14214. struct npc_data* nd = npc_name2id(npcname);
  14215. int flag = 1;
  14216. if( script_hasdata(st,3) )
  14217. flag = script_getnum(st,3);
  14218. if (!nd || ( nd->subtype != NPCTYPE_SHOP && nd->subtype != NPCTYPE_CASHSHOP && nd->subtype != NPCTYPE_ITEMSHOP && nd->subtype != NPCTYPE_POINTSHOP && nd->subtype != NPCTYPE_MARKETSHOP)) { // Not found.
  14219. script_pushint(st,0);
  14220. return SCRIPT_CMD_SUCCESS;
  14221. }
  14222. if (flag)
  14223. nd->master_nd = ((struct npc_data *)map_id2bl(st->oid));
  14224. else
  14225. nd->master_nd = NULL;
  14226. script_pushint(st,1);
  14227. return SCRIPT_CMD_SUCCESS;
  14228. }
  14229. /*==========================================
  14230. * Returns some values of an item [Lupus]
  14231. * Price, Weight, etc...
  14232. setitemscript(itemID,"{new item bonus script}",[n]);
  14233. Where n:
  14234. 0 - script
  14235. 1 - Equip script
  14236. 2 - Unequip script
  14237. *------------------------------------------*/
  14238. BUILDIN_FUNC(setitemscript)
  14239. {
  14240. unsigned short item_id;
  14241. int n = 0;
  14242. const char *script;
  14243. struct item_data *i_data;
  14244. struct script_code **dstscript;
  14245. item_id = script_getnum(st,2);
  14246. script = script_getstr(st,3);
  14247. if( script_hasdata(st,4) )
  14248. n=script_getnum(st,4);
  14249. i_data = itemdb_exists(item_id);
  14250. if (!i_data || script==NULL || ( script[0] && script[0]!='{' )) {
  14251. script_pushint(st,0);
  14252. return SCRIPT_CMD_SUCCESS;
  14253. }
  14254. switch (n) {
  14255. case 2:
  14256. dstscript = &i_data->unequip_script;
  14257. break;
  14258. case 1:
  14259. dstscript = &i_data->equip_script;
  14260. break;
  14261. default:
  14262. dstscript = &i_data->script;
  14263. break;
  14264. }
  14265. if(*dstscript)
  14266. script_free_code(*dstscript);
  14267. *dstscript = script[0] ? parse_script(script, "script_setitemscript", 0, 0) : NULL;
  14268. script_pushint(st,1);
  14269. return SCRIPT_CMD_SUCCESS;
  14270. }
  14271. /*=======================================================
  14272. * Add or Update a mob drop temporarily [Akinari]
  14273. * Original Idea By: [Lupus]
  14274. *
  14275. * addmonsterdrop <mob_id or name>,<item_id>,<rate>;
  14276. *
  14277. * If given an item the mob already drops, the rate
  14278. * is updated to the new rate. Rate cannot exceed 10000
  14279. * Returns 1 if succeeded (added/updated a mob drop)
  14280. *-------------------------------------------------------*/
  14281. BUILDIN_FUNC(addmonsterdrop)
  14282. {
  14283. struct mob_db *mob;
  14284. struct script_data *data;
  14285. unsigned short item_id;
  14286. int rate;
  14287. data = script_getdata(st, 2);
  14288. get_val(st, data); // Convert into value in case of a variable
  14289. if(data_isstring(data))
  14290. mob = mob_db(mobdb_searchname(script_getstr(st,2)));
  14291. else
  14292. mob = mob_db(script_getnum(st,2));
  14293. item_id=script_getnum(st,3);
  14294. rate=script_getnum(st,4);
  14295. if(!itemdb_exists(item_id)){
  14296. ShowError("addmonsterdrop: Nonexistant item %hu requested.\n", item_id );
  14297. return SCRIPT_CMD_FAILURE;
  14298. }
  14299. if(mob) { //We got a valid monster, check for available drop slot
  14300. unsigned char i, c = 0;
  14301. for(i = 0; i < MAX_MOB_DROP; i++) {
  14302. if(mob->dropitem[i].nameid) {
  14303. if(mob->dropitem[i].nameid == item_id) { //If it equals item_id we update that drop
  14304. c = i;
  14305. break;
  14306. }
  14307. continue;
  14308. }
  14309. if(!c) //Accept first available slot only
  14310. c = i;
  14311. }
  14312. if(c) { //Fill in the slot with the item and rate
  14313. mob->dropitem[c].nameid = item_id;
  14314. mob->dropitem[c].p = (rate > 10000)?10000:rate;
  14315. script_pushint(st,1);
  14316. } else //No place to put the new drop
  14317. script_pushint(st,0);
  14318. } else {
  14319. ShowWarning("addmonsterdrop: bad mob id given %d\n",script_getnum(st,2));
  14320. return SCRIPT_CMD_FAILURE;
  14321. }
  14322. return SCRIPT_CMD_SUCCESS;
  14323. }
  14324. /*=======================================================
  14325. * Delete a mob drop temporarily [Akinari]
  14326. * Original Idea By: [Lupus]
  14327. *
  14328. * delmonsterdrop <mob_id or name>,<item_id>;
  14329. *
  14330. * Returns 1 if succeeded (deleted a mob drop)
  14331. *-------------------------------------------------------*/
  14332. BUILDIN_FUNC(delmonsterdrop)
  14333. {
  14334. struct mob_db *mob;
  14335. struct script_data *data;
  14336. unsigned short item_id;
  14337. data = script_getdata(st, 2);
  14338. get_val(st, data); // Convert into value in case of a variable
  14339. if(data_isstring(data))
  14340. mob = mob_db(mobdb_searchname(script_getstr(st,2)));
  14341. else
  14342. mob = mob_db(script_getnum(st,2));
  14343. item_id=script_getnum(st,3);
  14344. if(!itemdb_exists(item_id)){
  14345. ShowError("delmonsterdrop: Nonexistant item %hu requested.\n", item_id );
  14346. return SCRIPT_CMD_FAILURE;
  14347. }
  14348. if(mob) { //We got a valid monster, check for item drop on monster
  14349. unsigned char i;
  14350. for(i = 0; i < MAX_MOB_DROP; i++) {
  14351. if(mob->dropitem[i].nameid == item_id) {
  14352. mob->dropitem[i].nameid = 0;
  14353. mob->dropitem[i].p = 0;
  14354. script_pushint(st,1);
  14355. return SCRIPT_CMD_SUCCESS;
  14356. }
  14357. }
  14358. //No drop on that monster
  14359. script_pushint(st,0);
  14360. } else {
  14361. ShowWarning("delmonsterdrop: bad mob id given %d\n",script_getnum(st,2));
  14362. return SCRIPT_CMD_FAILURE;
  14363. }
  14364. return SCRIPT_CMD_SUCCESS;
  14365. }
  14366. /*==========================================
  14367. * Returns some values of a monster [Lupus]
  14368. * Name, Level, race, size, etc...
  14369. getmonsterinfo(monsterID,queryIndex);
  14370. *------------------------------------------*/
  14371. BUILDIN_FUNC(getmonsterinfo)
  14372. {
  14373. struct mob_db *mob;
  14374. int mob_id;
  14375. mob_id = script_getnum(st,2);
  14376. if (!mobdb_checkid(mob_id)) {
  14377. //ShowError("buildin_getmonsterinfo: Wrong Monster ID: %i\n", mob_id);
  14378. if ( script_getnum(st,3) == MOB_NAME ) // requested the name
  14379. script_pushconststr(st,"null");
  14380. else
  14381. script_pushint(st,-1);
  14382. return SCRIPT_CMD_SUCCESS;
  14383. }
  14384. mob = mob_db(mob_id);
  14385. switch ( script_getnum(st,3) ) {
  14386. case MOB_NAME: script_pushstrcopy(st,mob->jname); break;
  14387. case MOB_LV: script_pushint(st,mob->lv); break;
  14388. case MOB_MAXHP: script_pushint(st,mob->status.max_hp); break;
  14389. case MOB_BASEEXP: script_pushint(st,mob->base_exp); break;
  14390. case MOB_JOBEXP: script_pushint(st,mob->job_exp); break;
  14391. case MOB_ATK1: script_pushint(st,mob->status.rhw.atk); break;
  14392. case MOB_ATK2: script_pushint(st,mob->status.rhw.atk2); break;
  14393. case MOB_DEF: script_pushint(st,mob->status.def); break;
  14394. case MOB_MDEF: script_pushint(st,mob->status.mdef); break;
  14395. case MOB_STR: script_pushint(st,mob->status.str); break;
  14396. case MOB_AGI: script_pushint(st,mob->status.agi); break;
  14397. case MOB_VIT: script_pushint(st,mob->status.vit); break;
  14398. case MOB_INT: script_pushint(st,mob->status.int_); break;
  14399. case MOB_DEX: script_pushint(st,mob->status.dex); break;
  14400. case MOB_LUK: script_pushint(st,mob->status.luk); break;
  14401. case MOB_RANGE: script_pushint(st,mob->status.rhw.range); break;
  14402. case MOB_RANGE2: script_pushint(st,mob->range2); break;
  14403. case MOB_RANGE3: script_pushint(st,mob->range3); break;
  14404. case MOB_SIZE: script_pushint(st,mob->status.size); break;
  14405. case MOB_RACE: script_pushint(st,mob->status.race); break;
  14406. case MOB_ELEMENT: script_pushint(st,mob->status.def_ele); break;
  14407. case MOB_MODE: script_pushint(st,mob->status.mode); break;
  14408. case MOB_MVPEXP: script_pushint(st,mob->mexp); break;
  14409. default: script_pushint(st,-1); //wrong Index
  14410. }
  14411. return SCRIPT_CMD_SUCCESS;
  14412. }
  14413. /**
  14414. * Check player's vending/buyingstore/autotrade state
  14415. * checkvending({"<Player Name>"})
  14416. * @author [Nab4]
  14417. */
  14418. BUILDIN_FUNC(checkvending) {
  14419. TBL_PC *sd = NULL;
  14420. if (script_hasdata(st,2)) {
  14421. if (!(sd = map_nick2sd(script_getstr(st,2)))) {
  14422. ShowError("buildin_checkvending: Player '%s' is not online!\n", script_getstr(st,2));
  14423. return SCRIPT_CMD_FAILURE;
  14424. }
  14425. }
  14426. else
  14427. sd = script_rid2sd(st);
  14428. if (!sd) {
  14429. script_pushint(st,0);
  14430. return SCRIPT_CMD_SUCCESS;
  14431. }
  14432. else {
  14433. int8 ret = 0;
  14434. if (sd->state.vending)
  14435. ret = 1;
  14436. else if (sd->state.buyingstore)
  14437. ret = 4;
  14438. if (sd->state.autotrade)
  14439. ret |= 2;
  14440. script_pushint(st, ret);
  14441. }
  14442. return SCRIPT_CMD_SUCCESS;
  14443. }
  14444. BUILDIN_FUNC(checkchatting) // check chatting [Marka]
  14445. {
  14446. TBL_PC *sd = NULL;
  14447. if(script_hasdata(st,2))
  14448. sd = map_nick2sd(script_getstr(st,2));
  14449. else
  14450. sd = script_rid2sd(st);
  14451. if(sd)
  14452. script_pushint(st,(sd->chatID != 0));
  14453. else
  14454. script_pushint(st,0);
  14455. return SCRIPT_CMD_SUCCESS;
  14456. }
  14457. BUILDIN_FUNC(checkidle)
  14458. {
  14459. TBL_PC *sd = NULL;
  14460. if (script_hasdata(st, 2))
  14461. sd = map_nick2sd(script_getstr(st, 2));
  14462. else
  14463. sd = script_rid2sd(st);
  14464. if (sd)
  14465. script_pushint(st, DIFF_TICK(last_tick, sd->idletime));
  14466. else
  14467. script_pushint(st, 0);
  14468. return SCRIPT_CMD_SUCCESS;
  14469. }
  14470. BUILDIN_FUNC(searchitem)
  14471. {
  14472. struct script_data* data = script_getdata(st, 2);
  14473. const char *itemname = script_getstr(st,3);
  14474. struct item_data *items[MAX_SEARCH];
  14475. int count;
  14476. char* name;
  14477. int32 start;
  14478. int32 id;
  14479. int32 i;
  14480. TBL_PC* sd = NULL;
  14481. if ((items[0] = itemdb_exists(atoi(itemname))))
  14482. count = 1;
  14483. else {
  14484. count = itemdb_searchname_array(items, ARRAYLENGTH(items), itemname);
  14485. if (count > MAX_SEARCH) count = MAX_SEARCH;
  14486. }
  14487. if (!count) {
  14488. script_pushint(st, 0);
  14489. return SCRIPT_CMD_SUCCESS;
  14490. }
  14491. if( !data_isreference(data) )
  14492. {
  14493. ShowError("script:searchitem: not a variable\n");
  14494. script_reportdata(data);
  14495. st->state = END;
  14496. return SCRIPT_CMD_FAILURE;// not a variable
  14497. }
  14498. id = reference_getid(data);
  14499. start = reference_getindex(data);
  14500. name = reference_getname(data);
  14501. if( not_server_variable(*name) )
  14502. {
  14503. sd = script_rid2sd(st);
  14504. if( sd == NULL )
  14505. return SCRIPT_CMD_SUCCESS;// no player attached
  14506. }
  14507. if( is_string_variable(name) )
  14508. {// string array
  14509. ShowError("script:searchitem: not an integer array reference\n");
  14510. script_reportdata(data);
  14511. st->state = END;
  14512. return SCRIPT_CMD_FAILURE;// not supported
  14513. }
  14514. for( i = 0; i < count; ++start, ++i )
  14515. {// Set array
  14516. void* v = (void*)__64BPRTSIZE((int)items[i]->nameid);
  14517. set_reg(st, sd, reference_uid(id, start), name, v, reference_getref(data));
  14518. }
  14519. script_pushint(st, count);
  14520. return SCRIPT_CMD_SUCCESS;
  14521. }
  14522. // [zBuffer] List of player cont commands --->
  14523. BUILDIN_FUNC(rid2name)
  14524. {
  14525. struct block_list *bl = NULL;
  14526. int rid = script_getnum(st,2);
  14527. if((bl = map_id2bl(rid)))
  14528. {
  14529. switch(bl->type) {
  14530. case BL_MOB: script_pushstrcopy(st,((TBL_MOB*)bl)->name); break;
  14531. case BL_PC: script_pushstrcopy(st,((TBL_PC*)bl)->status.name); break;
  14532. case BL_NPC: script_pushstrcopy(st,((TBL_NPC*)bl)->exname); break;
  14533. case BL_PET: script_pushstrcopy(st,((TBL_PET*)bl)->pet.name); break;
  14534. case BL_HOM: script_pushstrcopy(st,((TBL_HOM*)bl)->homunculus.name); break;
  14535. case BL_MER: script_pushstrcopy(st,((TBL_MER*)bl)->db->name); break;
  14536. default:
  14537. ShowError("buildin_rid2name: BL type unknown.\n");
  14538. script_pushconststr(st,"");
  14539. break;
  14540. }
  14541. } else {
  14542. ShowError("buildin_rid2name: invalid RID\n");
  14543. script_pushconststr(st,"(null)");
  14544. }
  14545. return SCRIPT_CMD_SUCCESS;
  14546. }
  14547. BUILDIN_FUNC(pcblockmove)
  14548. {
  14549. struct block_list *bl = NULL;
  14550. if (script_getnum(st, 2))
  14551. bl = map_id2bl(script_getnum(st,2));
  14552. else
  14553. bl = map_id2bl(st->rid);
  14554. if (bl) {
  14555. struct unit_data *ud = unit_bl2ud(bl);
  14556. if (ud)
  14557. ud->state.blockedmove = script_getnum(st,3) > 0;
  14558. }
  14559. return SCRIPT_CMD_SUCCESS;
  14560. }
  14561. BUILDIN_FUNC(pcfollow)
  14562. {
  14563. int id, targetid;
  14564. TBL_PC *sd = NULL;
  14565. id = script_getnum(st,2);
  14566. targetid = script_getnum(st,3);
  14567. if(id)
  14568. sd = map_id2sd(id);
  14569. else
  14570. sd = script_rid2sd(st);
  14571. if(sd)
  14572. pc_follow(sd, targetid);
  14573. return SCRIPT_CMD_SUCCESS;
  14574. }
  14575. BUILDIN_FUNC(pcstopfollow)
  14576. {
  14577. int id;
  14578. TBL_PC *sd = NULL;
  14579. id = script_getnum(st,2);
  14580. if(id)
  14581. sd = map_id2sd(id);
  14582. else
  14583. sd = script_rid2sd(st);
  14584. if(sd)
  14585. pc_stop_following(sd);
  14586. return SCRIPT_CMD_SUCCESS;
  14587. }
  14588. // <--- [zBuffer] List of player cont commands
  14589. // [zBuffer] List of unit control commands --->
  14590. /// Gets the type of the given Game ID.
  14591. ///
  14592. /// getunittype <unit id>;
  14593. BUILDIN_FUNC(getunittype)
  14594. {
  14595. struct block_list* bl;
  14596. uint8 value = 0;
  14597. bl = map_id2bl(script_getnum(st, 2));
  14598. if (!bl) {
  14599. ShowWarning("buildin_getunittype: Error in finding object with given game ID %d!\n", script_getnum(st, 2));
  14600. script_pushint(st, -1);
  14601. return SCRIPT_CMD_FAILURE;
  14602. }
  14603. switch (bl->type) {
  14604. case BL_MOB: value = 0; break;
  14605. case BL_HOM: value = 1; break;
  14606. case BL_PET: value = 2; break;
  14607. case BL_MER: value = 3; break;
  14608. case BL_ELEM: value = 4; break;
  14609. case BL_NPC: value = 5; break;
  14610. }
  14611. script_pushint(st, value);
  14612. return SCRIPT_CMD_SUCCESS;
  14613. }
  14614. /// Gets specific live information of a bl.
  14615. ///
  14616. /// getunitdata <unit id>,<arrayname>;
  14617. BUILDIN_FUNC(getunitdata)
  14618. {
  14619. TBL_PC *sd = st->rid ? map_id2sd(st->rid) : NULL;
  14620. struct block_list* bl;
  14621. TBL_MOB* md = NULL;
  14622. TBL_HOM* hd = NULL;
  14623. TBL_MER* mc = NULL;
  14624. TBL_PET* pd = NULL;
  14625. TBL_ELEM* ed = NULL;
  14626. TBL_NPC* nd = NULL;
  14627. char* name;
  14628. struct script_data *data = script_getdata(st, 3);
  14629. if (!data_isreference(data)) {
  14630. ShowWarning("buildin_getunitdata: Error in argument! Please give a variable to store values in.\n");
  14631. return SCRIPT_CMD_FAILURE;
  14632. }
  14633. bl = map_id2bl(script_getnum(st, 2));
  14634. if (!bl) {
  14635. ShowWarning("buildin_getunitdata: Error in finding object with given game ID %d!\n", script_getnum(st, 2));
  14636. return SCRIPT_CMD_FAILURE;
  14637. }
  14638. switch (bl->type) {
  14639. case BL_MOB: md = map_id2md(bl->id); break;
  14640. case BL_HOM: hd = map_id2hd(bl->id); break;
  14641. case BL_PET: pd = map_id2pd(bl->id); break;
  14642. case BL_MER: mc = map_id2mc(bl->id); break;
  14643. case BL_ELEM: ed = map_id2ed(bl->id); break;
  14644. case BL_NPC: nd = map_id2nd(bl->id); break;
  14645. }
  14646. name = reference_getname(data);
  14647. #define getunitdata_sub(idx__,var__) setd_sub(st,sd,name,(idx__),(void *)__64BPRTSIZE((int)(var__)),data->ref)
  14648. switch(bl->type) {
  14649. case BL_MOB:
  14650. if (!md) {
  14651. ShowWarning("buildin_getunitdata: Error in finding object BL_MOB!\n");
  14652. return SCRIPT_CMD_FAILURE;
  14653. }
  14654. getunitdata_sub(UMOB_SIZE, md->status.size);
  14655. getunitdata_sub(UMOB_LEVEL, md->level);
  14656. getunitdata_sub(UMOB_HP, md->status.hp);
  14657. getunitdata_sub(UMOB_MAXHP, md->status.max_hp);
  14658. getunitdata_sub(UMOB_MASTERAID, md->master_id);
  14659. getunitdata_sub(UMOB_MAPID, md->bl.m);
  14660. getunitdata_sub(UMOB_X, md->bl.x);
  14661. getunitdata_sub(UMOB_Y, md->bl.y);
  14662. getunitdata_sub(UMOB_SPEED, md->status.speed);
  14663. getunitdata_sub(UMOB_MODE, md->status.mode);
  14664. getunitdata_sub(UMOB_AI, md->special_state.ai);
  14665. getunitdata_sub(UMOB_SCOPTION, md->sc.option);
  14666. getunitdata_sub(UMOB_SEX, md->vd->sex);
  14667. getunitdata_sub(UMOB_CLASS, md->vd->class_);
  14668. getunitdata_sub(UMOB_HAIRSTYLE, md->vd->hair_style);
  14669. getunitdata_sub(UMOB_HAIRCOLOR, md->vd->hair_color);
  14670. getunitdata_sub(UMOB_HEADBOTTOM, md->vd->head_bottom);
  14671. getunitdata_sub(UMOB_HEADMIDDLE, md->vd->head_mid);
  14672. getunitdata_sub(UMOB_HEADTOP, md->vd->head_top);
  14673. getunitdata_sub(UMOB_CLOTHCOLOR, md->vd->cloth_color);
  14674. getunitdata_sub(UMOB_SHIELD, md->vd->shield);
  14675. getunitdata_sub(UMOB_WEAPON, md->vd->weapon);
  14676. getunitdata_sub(UMOB_LOOKDIR, md->ud.dir);
  14677. getunitdata_sub(UMOB_STR, md->status.str);
  14678. getunitdata_sub(UMOB_AGI, md->status.agi);
  14679. getunitdata_sub(UMOB_VIT, md->status.vit);
  14680. getunitdata_sub(UMOB_INT, md->status.int_);
  14681. getunitdata_sub(UMOB_DEX, md->status.dex);
  14682. getunitdata_sub(UMOB_LUK, md->status.luk);
  14683. getunitdata_sub(UMOB_SLAVECPYMSTRMD, md->state.copy_master_mode);
  14684. getunitdata_sub(UMOB_DMGIMMUNE, md->ud.immune_attack);
  14685. getunitdata_sub(UMOB_ATKRANGE, md->status.rhw.range);
  14686. getunitdata_sub(UMOB_ATKMIN, md->status.rhw.atk);
  14687. getunitdata_sub(UMOB_ATKMAX, md->status.rhw.atk2);
  14688. getunitdata_sub(UMOB_MATKMIN, md->status.matk_min);
  14689. getunitdata_sub(UMOB_MATKMAX, md->status.matk_max);
  14690. getunitdata_sub(UMOB_DEF, md->status.def);
  14691. getunitdata_sub(UMOB_MDEF, md->status.mdef);
  14692. getunitdata_sub(UMOB_HIT, md->status.hit);
  14693. getunitdata_sub(UMOB_FLEE, md->status.flee);
  14694. getunitdata_sub(UMOB_PDODGE, md->status.flee2);
  14695. getunitdata_sub(UMOB_CRIT, md->status.cri);
  14696. getunitdata_sub(UMOB_RACE, md->status.race);
  14697. getunitdata_sub(UMOB_ELETYPE, md->status.def_ele);
  14698. getunitdata_sub(UMOB_ELELEVEL, md->status.ele_lv);
  14699. getunitdata_sub(UMOB_AMOTION, md->status.amotion);
  14700. getunitdata_sub(UMOB_ADELAY, md->status.adelay);
  14701. getunitdata_sub(UMOB_DMOTION, md->status.dmotion);
  14702. break;
  14703. case BL_HOM:
  14704. if (!hd) {
  14705. ShowWarning("buildin_getunitdata: Error in finding object BL_HOM!\n");
  14706. return SCRIPT_CMD_FAILURE;
  14707. }
  14708. getunitdata_sub(UHOM_SIZE, hd->base_status.size);
  14709. getunitdata_sub(UHOM_LEVEL, hd->homunculus.level);
  14710. getunitdata_sub(UHOM_HP, hd->homunculus.hp);
  14711. getunitdata_sub(UHOM_MAXHP, hd->homunculus.max_hp);
  14712. getunitdata_sub(UHOM_SP, hd->homunculus.sp);
  14713. getunitdata_sub(UHOM_MAXSP, hd->homunculus.max_sp);
  14714. getunitdata_sub(UHOM_MASTERCID, hd->homunculus.char_id);
  14715. getunitdata_sub(UHOM_MAPID, hd->bl.m);
  14716. getunitdata_sub(UHOM_X, hd->bl.x);
  14717. getunitdata_sub(UHOM_Y, hd->bl.y);
  14718. getunitdata_sub(UHOM_HUNGER, hd->homunculus.hunger);
  14719. getunitdata_sub(UHOM_INTIMACY, hd->homunculus.intimacy);
  14720. getunitdata_sub(UHOM_SPEED, hd->base_status.speed);
  14721. getunitdata_sub(UHOM_LOOKDIR, hd->ud.dir);
  14722. getunitdata_sub(UHOM_CANMOVETICK, hd->ud.canmove_tick);
  14723. getunitdata_sub(UHOM_STR, hd->base_status.str);
  14724. getunitdata_sub(UHOM_AGI, hd->base_status.agi);
  14725. getunitdata_sub(UHOM_VIT, hd->base_status.vit);
  14726. getunitdata_sub(UHOM_INT, hd->base_status.int_);
  14727. getunitdata_sub(UHOM_DEX, hd->base_status.dex);
  14728. getunitdata_sub(UHOM_LUK, hd->base_status.luk);
  14729. getunitdata_sub(UHOM_DMGIMMUNE, hd->ud.immune_attack);
  14730. getunitdata_sub(UHOM_ATKRANGE, hd->battle_status.rhw.range);
  14731. getunitdata_sub(UHOM_ATKMIN, hd->base_status.rhw.atk);
  14732. getunitdata_sub(UHOM_ATKMAX, hd->base_status.rhw.atk2);
  14733. getunitdata_sub(UHOM_MATKMIN, hd->base_status.matk_min);
  14734. getunitdata_sub(UHOM_MATKMAX, hd->base_status.matk_max);
  14735. getunitdata_sub(UHOM_DEF, hd->battle_status.def);
  14736. getunitdata_sub(UHOM_MDEF, hd->battle_status.mdef);
  14737. getunitdata_sub(UHOM_HIT, hd->battle_status.hit);
  14738. getunitdata_sub(UHOM_FLEE, hd->battle_status.flee);
  14739. getunitdata_sub(UHOM_PDODGE, hd->battle_status.flee2);
  14740. getunitdata_sub(UHOM_CRIT, hd->battle_status.cri);
  14741. getunitdata_sub(UHOM_RACE, hd->battle_status.race);
  14742. getunitdata_sub(UHOM_ELETYPE, hd->battle_status.def_ele);
  14743. getunitdata_sub(UHOM_ELELEVEL, hd->battle_status.ele_lv);
  14744. getunitdata_sub(UHOM_AMOTION, hd->battle_status.amotion);
  14745. getunitdata_sub(UHOM_ADELAY, hd->battle_status.adelay);
  14746. getunitdata_sub(UHOM_DMOTION, hd->battle_status.dmotion);
  14747. break;
  14748. case BL_PET:
  14749. if (!pd) {
  14750. ShowWarning("buildin_getunitdata: Error in finding object BL_PET!\n");
  14751. return SCRIPT_CMD_FAILURE;
  14752. }
  14753. getunitdata_sub(UPET_SIZE, pd->status.size);
  14754. getunitdata_sub(UPET_LEVEL, pd->pet.level);
  14755. getunitdata_sub(UPET_HP, pd->status.hp);
  14756. getunitdata_sub(UPET_MAXHP, pd->status.max_hp);
  14757. getunitdata_sub(UPET_MASTERAID, pd->pet.account_id);
  14758. getunitdata_sub(UPET_MAPID, pd->bl.m);
  14759. getunitdata_sub(UPET_X, pd->bl.x);
  14760. getunitdata_sub(UPET_Y, pd->bl.y);
  14761. getunitdata_sub(UPET_HUNGER, pd->pet.hungry);
  14762. getunitdata_sub(UPET_INTIMACY, pd->pet.intimate);
  14763. getunitdata_sub(UPET_SPEED, pd->status.speed);
  14764. getunitdata_sub(UPET_LOOKDIR, pd->ud.dir);
  14765. getunitdata_sub(UPET_CANMOVETICK, pd->ud.canmove_tick);
  14766. getunitdata_sub(UPET_STR, pd->status.str);
  14767. getunitdata_sub(UPET_AGI, pd->status.agi);
  14768. getunitdata_sub(UPET_VIT, pd->status.vit);
  14769. getunitdata_sub(UPET_INT, pd->status.int_);
  14770. getunitdata_sub(UPET_DEX, pd->status.dex);
  14771. getunitdata_sub(UPET_LUK, pd->status.luk);
  14772. getunitdata_sub(UPET_DMGIMMUNE, pd->ud.immune_attack);
  14773. getunitdata_sub(UPET_ATKRANGE, pd->status.rhw.range);
  14774. getunitdata_sub(UPET_ATKMIN, pd->status.rhw.atk);
  14775. getunitdata_sub(UPET_ATKMAX, pd->status.rhw.atk2);
  14776. getunitdata_sub(UPET_MATKMIN, pd->status.matk_min);
  14777. getunitdata_sub(UPET_MATKMAX, pd->status.matk_max);
  14778. getunitdata_sub(UPET_DEF, pd->status.def);
  14779. getunitdata_sub(UPET_MDEF, pd->status.mdef);
  14780. getunitdata_sub(UPET_HIT, pd->status.hit);
  14781. getunitdata_sub(UPET_FLEE, pd->status.flee);
  14782. getunitdata_sub(UPET_PDODGE, pd->status.flee2);
  14783. getunitdata_sub(UPET_CRIT, pd->status.cri);
  14784. getunitdata_sub(UPET_RACE, pd->status.race);
  14785. getunitdata_sub(UPET_ELETYPE, pd->status.def_ele);
  14786. getunitdata_sub(UPET_ELELEVEL, pd->status.ele_lv);
  14787. getunitdata_sub(UPET_AMOTION, pd->status.amotion);
  14788. getunitdata_sub(UPET_ADELAY, pd->status.adelay);
  14789. getunitdata_sub(UPET_DMOTION, pd->status.dmotion);
  14790. break;
  14791. case BL_MER:
  14792. if (!mc) {
  14793. ShowWarning("buildin_getunitdata: Error in finding object BL_MER!\n");
  14794. return SCRIPT_CMD_FAILURE;
  14795. }
  14796. getunitdata_sub(UMER_SIZE, mc->base_status.size);
  14797. getunitdata_sub(UMER_HP, mc->base_status.hp);
  14798. getunitdata_sub(UMER_MAXHP, mc->base_status.max_hp);
  14799. getunitdata_sub(UMER_MASTERCID, mc->mercenary.char_id);
  14800. getunitdata_sub(UMER_MAPID, mc->bl.m);
  14801. getunitdata_sub(UMER_X, mc->bl.x);
  14802. getunitdata_sub(UMER_Y, mc->bl.y);
  14803. getunitdata_sub(UMER_KILLCOUNT, mc->mercenary.kill_count);
  14804. getunitdata_sub(UMER_LIFETIME, mc->mercenary.life_time);
  14805. getunitdata_sub(UMER_SPEED, mc->base_status.speed);
  14806. getunitdata_sub(UMER_LOOKDIR, mc->ud.dir);
  14807. getunitdata_sub(UMER_CANMOVETICK, mc->ud.canmove_tick);
  14808. getunitdata_sub(UMER_STR, mc->base_status.str);
  14809. getunitdata_sub(UMER_AGI, mc->base_status.agi);
  14810. getunitdata_sub(UMER_VIT, mc->base_status.vit);
  14811. getunitdata_sub(UMER_INT, mc->base_status.int_);
  14812. getunitdata_sub(UMER_DEX, mc->base_status.dex);
  14813. getunitdata_sub(UMER_LUK, mc->base_status.luk);
  14814. getunitdata_sub(UMER_DMGIMMUNE, mc->ud.immune_attack);
  14815. getunitdata_sub(UMER_ATKRANGE, mc->base_status.rhw.range);
  14816. getunitdata_sub(UMER_ATKMIN, mc->base_status.rhw.atk);
  14817. getunitdata_sub(UMER_ATKMAX, mc->base_status.rhw.atk2);
  14818. getunitdata_sub(UMER_MATKMIN, mc->base_status.matk_min);
  14819. getunitdata_sub(UMER_MATKMAX, mc->base_status.matk_max);
  14820. getunitdata_sub(UMER_DEF, mc->base_status.def);
  14821. getunitdata_sub(UMER_MDEF, mc->base_status.mdef);
  14822. getunitdata_sub(UMER_HIT, mc->base_status.hit);
  14823. getunitdata_sub(UMER_FLEE, mc->base_status.flee);
  14824. getunitdata_sub(UMER_PDODGE, mc->base_status.flee2);
  14825. getunitdata_sub(UMER_CRIT, mc->base_status.cri);
  14826. getunitdata_sub(UMER_RACE, mc->base_status.race);
  14827. getunitdata_sub(UMER_ELETYPE, mc->base_status.def_ele);
  14828. getunitdata_sub(UMER_ELELEVEL, mc->base_status.ele_lv);
  14829. getunitdata_sub(UMER_AMOTION, mc->base_status.amotion);
  14830. getunitdata_sub(UMER_ADELAY, mc->base_status.adelay);
  14831. getunitdata_sub(UMER_DMOTION, mc->base_status.dmotion);
  14832. break;
  14833. case BL_ELEM:
  14834. if (!ed) {
  14835. ShowWarning("buildin_getunitdata: Error in finding object BL_ELEM!\n");
  14836. return SCRIPT_CMD_FAILURE;
  14837. }
  14838. getunitdata_sub(UELE_SIZE, ed->base_status.size);
  14839. getunitdata_sub(UELE_HP, ed->elemental.hp);
  14840. getunitdata_sub(UELE_MAXHP, ed->elemental.max_hp);
  14841. getunitdata_sub(UELE_SP, ed->elemental.sp);
  14842. getunitdata_sub(UELE_MAXSP, ed->elemental.max_sp);
  14843. getunitdata_sub(UELE_MASTERCID, ed->elemental.char_id);
  14844. getunitdata_sub(UELE_MAPID, ed->bl.m);
  14845. getunitdata_sub(UELE_X, ed->bl.x);
  14846. getunitdata_sub(UELE_Y, ed->bl.y);
  14847. getunitdata_sub(UELE_LIFETIME, ed->elemental.life_time);
  14848. getunitdata_sub(UELE_MODE, ed->elemental.mode);
  14849. getunitdata_sub(UELE_SP, ed->base_status.speed);
  14850. getunitdata_sub(UELE_LOOKDIR, ed->ud.dir);
  14851. getunitdata_sub(UELE_CANMOVETICK, ed->ud.canmove_tick);
  14852. getunitdata_sub(UELE_STR, ed->base_status.str);
  14853. getunitdata_sub(UELE_AGI, ed->base_status.agi);
  14854. getunitdata_sub(UELE_VIT, ed->base_status.vit);
  14855. getunitdata_sub(UELE_INT, ed->base_status.int_);
  14856. getunitdata_sub(UELE_DEX, ed->base_status.dex);
  14857. getunitdata_sub(UELE_LUK, ed->base_status.luk);
  14858. getunitdata_sub(UELE_DMGIMMUNE, ed->ud.immune_attack);
  14859. getunitdata_sub(UELE_ATKRANGE, ed->base_status.rhw.range);
  14860. getunitdata_sub(UELE_ATKMIN, ed->base_status.rhw.atk);
  14861. getunitdata_sub(UELE_ATKMAX, ed->base_status.rhw.atk2);
  14862. getunitdata_sub(UELE_MATKMIN, ed->base_status.matk_min);
  14863. getunitdata_sub(UELE_MATKMAX, ed->base_status.matk_max);
  14864. getunitdata_sub(UELE_DEF, ed->base_status.def);
  14865. getunitdata_sub(UELE_MDEF, ed->base_status.mdef);
  14866. getunitdata_sub(UELE_HIT, ed->base_status.hit);
  14867. getunitdata_sub(UELE_FLEE, ed->base_status.flee);
  14868. getunitdata_sub(UELE_PDODGE, ed->base_status.flee2);
  14869. getunitdata_sub(UELE_CRIT, ed->base_status.cri);
  14870. getunitdata_sub(UELE_RACE, ed->base_status.race);
  14871. getunitdata_sub(UELE_ELETYPE, ed->base_status.def_ele);
  14872. getunitdata_sub(UELE_ELELEVEL, ed->base_status.ele_lv);
  14873. getunitdata_sub(UELE_AMOTION, ed->base_status.amotion);
  14874. getunitdata_sub(UELE_ADELAY, ed->base_status.adelay);
  14875. getunitdata_sub(UELE_DMOTION, ed->base_status.dmotion);
  14876. break;
  14877. case BL_NPC:
  14878. if (!nd) {
  14879. ShowWarning("buildin_getunitdata: Error in finding object BL_NPC!\n");
  14880. return SCRIPT_CMD_FAILURE;
  14881. }
  14882. getunitdata_sub(UNPC_DISPLAY, nd->class_);
  14883. getunitdata_sub(UNPC_LEVEL, nd->level);
  14884. getunitdata_sub(UNPC_HP, nd->status.hp);
  14885. getunitdata_sub(UNPC_MAXHP, nd->status.max_hp);
  14886. getunitdata_sub(UNPC_MAPID, nd->bl.m);
  14887. getunitdata_sub(UNPC_X, nd->bl.x);
  14888. getunitdata_sub(UNPC_Y, nd->bl.y);
  14889. getunitdata_sub(UNPC_LOOKDIR, nd->ud.dir);
  14890. getunitdata_sub(UNPC_STR, nd->status.str);
  14891. getunitdata_sub(UNPC_AGI, nd->status.agi);
  14892. getunitdata_sub(UNPC_VIT, nd->status.vit);
  14893. getunitdata_sub(UNPC_INT, nd->status.int_);
  14894. getunitdata_sub(UNPC_DEX, nd->status.dex);
  14895. getunitdata_sub(UNPC_LUK, nd->status.luk);
  14896. getunitdata_sub(UNPC_PLUSALLSTAT, nd->stat_point);
  14897. getunitdata_sub(UNPC_DMGIMMUNE, nd->ud.immune_attack);
  14898. getunitdata_sub(UNPC_ATKRANGE, nd->status.rhw.range);
  14899. getunitdata_sub(UNPC_ATKMIN, nd->status.rhw.atk);
  14900. getunitdata_sub(UNPC_ATKMAX, nd->status.rhw.atk2);
  14901. getunitdata_sub(UNPC_MATKMIN, nd->status.matk_min);
  14902. getunitdata_sub(UNPC_MATKMAX, nd->status.matk_max);
  14903. getunitdata_sub(UNPC_DEF, nd->status.def);
  14904. getunitdata_sub(UNPC_MDEF, nd->status.mdef);
  14905. getunitdata_sub(UNPC_HIT, nd->status.hit);
  14906. getunitdata_sub(UNPC_FLEE, nd->status.flee);
  14907. getunitdata_sub(UNPC_PDODGE, nd->status.flee2);
  14908. getunitdata_sub(UNPC_CRIT, nd->status.cri);
  14909. getunitdata_sub(UNPC_RACE, nd->status.race);
  14910. getunitdata_sub(UNPC_ELETYPE, nd->status.def_ele);
  14911. getunitdata_sub(UNPC_ELELEVEL, nd->status.ele_lv);
  14912. getunitdata_sub(UNPC_AMOTION, nd->status.amotion);
  14913. getunitdata_sub(UNPC_ADELAY, nd->status.adelay);
  14914. getunitdata_sub(UNPC_DMOTION, nd->status.dmotion);
  14915. break;
  14916. default:
  14917. ShowWarning("buildin_getunitdata: Unknown object type!\n");
  14918. return SCRIPT_CMD_FAILURE;
  14919. }
  14920. #undef getunitdata_sub
  14921. return SCRIPT_CMD_SUCCESS;
  14922. }
  14923. /// Changes the live data of a bl.
  14924. ///
  14925. /// setunitdata <unit id>,<type>,<value>;
  14926. BUILDIN_FUNC(setunitdata)
  14927. {
  14928. struct block_list* bl = NULL;
  14929. struct script_data* data;
  14930. const char *mapname = NULL;
  14931. TBL_MOB* md = NULL;
  14932. TBL_HOM* hd = NULL;
  14933. TBL_MER* mc = NULL;
  14934. TBL_PET* pd = NULL;
  14935. TBL_ELEM* ed = NULL;
  14936. TBL_NPC* nd = NULL;
  14937. int type, value = 0;
  14938. bl = map_id2bl(script_getnum(st, 2));
  14939. if (!bl) {
  14940. ShowWarning("buildin_setunitdata: Error in finding object with given game ID %d!\n", script_getnum(st, 2));
  14941. return SCRIPT_CMD_FAILURE;
  14942. }
  14943. switch (bl->type) {
  14944. case BL_MOB: md = map_id2md(bl->id); break;
  14945. case BL_HOM: hd = map_id2hd(bl->id); break;
  14946. case BL_PET: pd = map_id2pd(bl->id); break;
  14947. case BL_MER: mc = map_id2mc(bl->id); break;
  14948. case BL_ELEM: ed = map_id2ed(bl->id); break;
  14949. case BL_NPC:
  14950. nd = map_id2nd(bl->id);
  14951. if (!nd->status.hp)
  14952. status_calc_npc(nd, SCO_FIRST);
  14953. else
  14954. status_calc_npc(nd, SCO_NONE);
  14955. break;
  14956. default:
  14957. ShowError("buildin_setunitdata: Invalid object!");
  14958. return SCRIPT_CMD_FAILURE;
  14959. }
  14960. type = script_getnum(st, 3);
  14961. data = script_getdata(st, 4);
  14962. get_val(st, data);
  14963. if (type == 5 && data_isstring(data))
  14964. mapname = conv_str(st, data);
  14965. else if (data_isint(data))
  14966. value = conv_num(st, data);
  14967. else {
  14968. ShowError("buildin_setunitdata: Invalid data type for argument #3 (%d).", data->type);
  14969. return SCRIPT_CMD_FAILURE;
  14970. }
  14971. switch (bl->type) {
  14972. case BL_MOB:
  14973. if (!md) {
  14974. ShowWarning("buildin_setunitdata: Error in finding object BL_MOB!\n");
  14975. return SCRIPT_CMD_FAILURE;
  14976. }
  14977. switch (type) {
  14978. case UMOB_SIZE: md->status.size = (unsigned char)value; break;
  14979. case UMOB_LEVEL: md->level = (unsigned short)value; break;
  14980. case UMOB_HP: status_set_hp(bl, (unsigned int)value, 0); clif_charnameack(0, &md->bl); break;
  14981. case UMOB_MAXHP: status_set_maxhp(bl, (unsigned int)value, 0); clif_charnameack(0, &md->bl); break;
  14982. case UMOB_MASTERAID: md->master_id = value; break;
  14983. case UMOB_MAPID: if (mapname) value = map_mapname2mapid(mapname); unit_warp(bl, (short)value, 0, 0, CLR_TELEPORT); break;
  14984. case UMOB_X: if (!unit_walktoxy(bl, (short)value, md->bl.y, 2)) unit_movepos(bl, (short)value, md->bl.y, 0, 0); break;
  14985. case UMOB_Y: if (!unit_walktoxy(bl, md->bl.x, (short)value, 2)) unit_movepos(bl, md->bl.x, (short)value, 0, 0); break;
  14986. case UMOB_SPEED: md->status.speed = (unsigned short)value; status_calc_misc(bl, &md->status, md->level); break;
  14987. case UMOB_MODE: md->status.mode = (enum e_mode)value; break;
  14988. case UMOB_AI: md->special_state.ai = (enum mob_ai)value; break;
  14989. case UMOB_SCOPTION: md->sc.option = (unsigned short)value; break;
  14990. case UMOB_SEX: md->vd->sex = (char)value; break;
  14991. case UMOB_CLASS: status_set_viewdata(bl, (unsigned short)value); break;
  14992. case UMOB_HAIRSTYLE: clif_changelook(bl, LOOK_HAIR, (unsigned short)value); break;
  14993. case UMOB_HAIRCOLOR: clif_changelook(bl, LOOK_HAIR_COLOR, (unsigned short)value); break;
  14994. case UMOB_HEADBOTTOM: clif_changelook(bl, LOOK_HEAD_BOTTOM, (unsigned short)value); break;
  14995. case UMOB_HEADMIDDLE: clif_changelook(bl, LOOK_HEAD_MID, (unsigned short)value); break;
  14996. case UMOB_HEADTOP: clif_changelook(bl, LOOK_HEAD_TOP, (unsigned short)value); break;
  14997. case UMOB_CLOTHCOLOR: clif_changelook(bl, LOOK_CLOTHES_COLOR, (unsigned short)value); break;
  14998. case UMOB_SHIELD: clif_changelook(bl, LOOK_SHIELD, (unsigned short)value); break;
  14999. case UMOB_WEAPON: clif_changelook(bl, LOOK_WEAPON, (unsigned short)value); break;
  15000. case UMOB_LOOKDIR: unit_setdir(bl, (uint8)value); break;
  15001. case UMOB_STR: md->status.str = (unsigned short)value; status_calc_misc(bl, &md->status, md->level); break;
  15002. case UMOB_AGI: md->status.agi = (unsigned short)value; status_calc_misc(bl, &md->status, md->level); break;
  15003. case UMOB_VIT: md->status.vit = (unsigned short)value; status_calc_misc(bl, &md->status, md->level); break;
  15004. case UMOB_INT: md->status.int_ = (unsigned short)value; status_calc_misc(bl, &md->status, md->level); break;
  15005. case UMOB_DEX: md->status.dex = (unsigned short)value; status_calc_misc(bl, &md->status, md->level); break;
  15006. case UMOB_LUK: md->status.luk = (unsigned short)value; status_calc_misc(bl, &md->status, md->level); break;
  15007. case UMOB_SLAVECPYMSTRMD:
  15008. if (value > 0) {
  15009. TBL_MOB *md2 = NULL;
  15010. if (!md->master_id || !(md2 = map_id2md(md->master_id))) {
  15011. ShowWarning("buildin_setunitdata: Trying to set UMOB_SLAVECPYMSTRMD on mob without master!\n");
  15012. break;
  15013. }
  15014. md->status.mode = md2->status.mode;
  15015. md->state.copy_master_mode = 1;
  15016. } else
  15017. md->state.copy_master_mode = 0;
  15018. break;
  15019. case UMOB_DMGIMMUNE: md->ud.immune_attack = (bool)value > 0 ? 1 : 0; break;
  15020. case UMOB_ATKRANGE: md->status.rhw.range = (unsigned short)value; break;
  15021. case UMOB_ATKMIN: md->status.rhw.atk = (unsigned short)value; break;
  15022. case UMOB_ATKMAX: md->status.rhw.atk2 = (unsigned short)value; break;
  15023. case UMOB_MATKMIN: md->status.matk_min = (unsigned short)value; break;
  15024. case UMOB_MATKMAX: md->status.matk_max = (unsigned short)value; break;
  15025. case UMOB_DEF: md->status.def = (defType)value; break;
  15026. case UMOB_MDEF: md->status.mdef = (defType)value; break;
  15027. case UMOB_HIT: md->status.hit = (short)value; break;
  15028. case UMOB_FLEE: md->status.flee = (short)value; break;
  15029. case UMOB_PDODGE: md->status.flee2 = (short)value; break;
  15030. case UMOB_CRIT: md->status.cri = (short)value; break;
  15031. case UMOB_RACE: md->status.race = (unsigned char)value; break;
  15032. case UMOB_ELETYPE: md->status.def_ele = (unsigned char)value; break;
  15033. case UMOB_ELELEVEL: md->status.ele_lv = (unsigned char)value; break;
  15034. case UMOB_AMOTION: md->status.amotion = (short)value; break;
  15035. case UMOB_ADELAY: md->status.adelay = (short)value; break;
  15036. case UMOB_DMOTION: md->status.dmotion = (short)value; break;
  15037. default:
  15038. ShowError("buildin_setunitdata: Unknown data identifier %d for BL_MOB.\n", type);
  15039. return SCRIPT_CMD_FAILURE;
  15040. }
  15041. break;
  15042. case BL_HOM:
  15043. if (!hd) {
  15044. ShowWarning("buildin_setunitdata: Error in finding object BL_HOM!\n");
  15045. return SCRIPT_CMD_FAILURE;
  15046. }
  15047. switch (type) {
  15048. case UHOM_SIZE: hd->base_status.size = (unsigned char)value; break;
  15049. case UHOM_LEVEL: hd->homunculus.level = (unsigned short)value; break;
  15050. case UHOM_HP: status_set_hp(bl, (unsigned int)value, 0); break;
  15051. case UHOM_MAXHP: status_set_maxhp(bl, (unsigned int)value, 0); break;
  15052. case UHOM_SP: status_set_sp(bl, (unsigned int)value, 0); break;
  15053. case UHOM_MAXSP: status_set_maxsp(bl, (unsigned int)value, 0); break;
  15054. case UHOM_MASTERCID: hd->homunculus.char_id = (uint32)value; break;
  15055. case UHOM_MAPID: if (mapname) value = map_mapname2mapid(mapname); unit_warp(bl, (short)value, 0, 0, CLR_TELEPORT); break;
  15056. case UHOM_X: if (!unit_walktoxy(bl, (short)value, hd->bl.y, 2)) unit_movepos(bl, (short)value, hd->bl.y, 0, 0); break;
  15057. case UHOM_Y: if (!unit_walktoxy(bl, hd->bl.x, (short)value, 2)) unit_movepos(bl, hd->bl.x, (short)value, 0, 0); break;
  15058. case UHOM_HUNGER: hd->homunculus.hunger = (short)value; clif_send_homdata(map_charid2sd(hd->homunculus.char_id), SP_HUNGRY, hd->homunculus.hunger); break;
  15059. 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;
  15060. case UHOM_SPEED: hd->base_status.speed = (unsigned short)value; status_calc_misc(bl, &hd->base_status, hd->homunculus.level); break;
  15061. case UHOM_LOOKDIR: unit_setdir(bl, (uint8)value); break;
  15062. case UHOM_CANMOVETICK: hd->ud.canmove_tick = value > 0 ? (unsigned int)value : 0; break;
  15063. case UHOM_STR: hd->base_status.str = (unsigned short)value; status_calc_misc(bl, &hd->base_status, hd->homunculus.level); break;
  15064. case UHOM_AGI: hd->base_status.agi = (unsigned short)value; status_calc_misc(bl, &hd->base_status, hd->homunculus.level); break;
  15065. case UHOM_VIT: hd->base_status.vit = (unsigned short)value; status_calc_misc(bl, &hd->base_status, hd->homunculus.level); break;
  15066. case UHOM_INT: hd->base_status.int_ = (unsigned short)value; status_calc_misc(bl, &hd->base_status, hd->homunculus.level); break;
  15067. case UHOM_DEX: hd->base_status.dex = (unsigned short)value; status_calc_misc(bl, &hd->base_status, hd->homunculus.level); break;
  15068. case UHOM_LUK: hd->base_status.luk = (unsigned short)value; status_calc_misc(bl, &hd->base_status, hd->homunculus.level); break;
  15069. case UHOM_DMGIMMUNE: hd->ud.immune_attack = (bool)value > 0 ? 1 : 0; break;
  15070. case UHOM_ATKRANGE: hd->base_status.rhw.range = (unsigned short)value; break;
  15071. case UHOM_ATKMIN: hd->base_status.rhw.atk = (unsigned short)value; break;
  15072. case UHOM_ATKMAX: hd->base_status.rhw.atk2 = (unsigned short)value; break;
  15073. case UHOM_MATKMIN: hd->base_status.matk_min = (unsigned short)value; break;
  15074. case UHOM_MATKMAX: hd->base_status.matk_max = (unsigned short)value; break;
  15075. case UHOM_DEF: hd->base_status.def = (defType)value; break;
  15076. case UHOM_MDEF: hd->base_status.mdef = (defType)value; break;
  15077. case UHOM_HIT: hd->base_status.hit = (short)value; break;
  15078. case UHOM_FLEE: hd->base_status.flee = (short)value; break;
  15079. case UHOM_PDODGE: hd->base_status.flee2 = (short)value; break;
  15080. case UHOM_CRIT: hd->base_status.cri = (short)value; break;
  15081. case UHOM_RACE: hd->base_status.race = (unsigned char)value; break;
  15082. case UHOM_ELETYPE: hd->base_status.def_ele = (unsigned char)value; break;
  15083. case UHOM_ELELEVEL: hd->base_status.ele_lv = (unsigned char)value; break;
  15084. case UHOM_AMOTION: hd->base_status.amotion = (short)value; break;
  15085. case UHOM_ADELAY: hd->base_status.adelay = (short)value; break;
  15086. case UHOM_DMOTION: hd->base_status.dmotion = (short)value; break;
  15087. default:
  15088. ShowError("buildin_setunitdata: Unknown data identifier %d for BL_HOM.\n", type);
  15089. return SCRIPT_CMD_FAILURE;
  15090. }
  15091. break;
  15092. case BL_PET:
  15093. if (!pd) {
  15094. ShowWarning("buildin_setunitdata: Error in finding object BL_PET!\n");
  15095. return SCRIPT_CMD_FAILURE;
  15096. }
  15097. switch (type) {
  15098. case UPET_SIZE: pd->status.size = (unsigned char)value; break;
  15099. case UPET_LEVEL: pd->pet.level = (unsigned short)value; break;
  15100. case UPET_HP: status_set_hp(bl, (unsigned int)value, 0); break;
  15101. case UPET_MAXHP: status_set_maxhp(bl, (unsigned int)value, 0); break;
  15102. case UPET_MASTERAID: pd->pet.account_id = (unsigned int)value; break;
  15103. case UPET_MAPID: if (mapname) value = map_mapname2mapid(mapname); unit_warp(bl, (short)value, 0, 0, CLR_TELEPORT); break;
  15104. case UPET_X: if (!unit_walktoxy(bl, (short)value, pd->bl.y, 2)) unit_movepos(bl, (short)value, md->bl.y, 0, 0); break;
  15105. case UPET_Y: if (!unit_walktoxy(bl, pd->bl.x, (short)value, 2)) unit_movepos(bl, pd->bl.x, (short)value, 0, 0); break;
  15106. case UPET_HUNGER: pd->pet.hungry = (short)value; clif_send_petdata(map_id2sd(pd->pet.account_id), pd, 2, pd->pet.hungry); break;
  15107. 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;
  15108. case UPET_SPEED: pd->status.speed = (unsigned short)value; status_calc_misc(bl, &pd->status, pd->pet.level); break;
  15109. case UPET_LOOKDIR: unit_setdir(bl, (uint8)value); break;
  15110. case UPET_CANMOVETICK: pd->ud.canmove_tick = value > 0 ? (unsigned int)value : 0; break;
  15111. case UPET_STR: pd->status.str = (unsigned short)value; status_calc_misc(bl, &pd->status, pd->pet.level); break;
  15112. case UPET_AGI: pd->status.agi = (unsigned short)value; status_calc_misc(bl, &pd->status, pd->pet.level); break;
  15113. case UPET_VIT: pd->status.vit = (unsigned short)value; status_calc_misc(bl, &pd->status, pd->pet.level); break;
  15114. case UPET_INT: pd->status.int_ = (unsigned short)value; status_calc_misc(bl, &pd->status, pd->pet.level); break;
  15115. case UPET_DEX: pd->status.dex = (unsigned short)value; status_calc_misc(bl, &pd->status, pd->pet.level); break;
  15116. case UPET_LUK: pd->status.luk = (unsigned short)value; status_calc_misc(bl, &pd->status, pd->pet.level); break;
  15117. case UPET_DMGIMMUNE: pd->ud.immune_attack = (bool)value > 0 ? 1 : 0; break;
  15118. case UPET_ATKRANGE: pd->status.rhw.range = (unsigned short)value; break;
  15119. case UPET_ATKMIN: pd->status.rhw.atk = (unsigned short)value; break;
  15120. case UPET_ATKMAX: pd->status.rhw.atk2 = (unsigned short)value; break;
  15121. case UPET_MATKMIN: pd->status.matk_min = (unsigned short)value; break;
  15122. case UPET_MATKMAX: pd->status.matk_max = (unsigned short)value; break;
  15123. case UPET_DEF: pd->status.def = (defType)value; break;
  15124. case UPET_MDEF: pd->status.mdef = (defType)value; break;
  15125. case UPET_HIT: pd->status.hit = (short)value; break;
  15126. case UPET_FLEE: pd->status.flee = (short)value; break;
  15127. case UPET_PDODGE: pd->status.flee2 = (short)value; break;
  15128. case UPET_CRIT: pd->status.cri = (short)value; break;
  15129. case UPET_RACE: pd->status.race = (unsigned char)value; break;
  15130. case UPET_ELETYPE: pd->status.def_ele = (unsigned char)value; break;
  15131. case UPET_ELELEVEL: pd->status.ele_lv = (unsigned char)value; break;
  15132. case UPET_AMOTION: pd->status.amotion = (short)value; break;
  15133. case UPET_ADELAY: pd->status.adelay = (short)value; break;
  15134. case UPET_DMOTION: pd->status.dmotion = (short)value; break;
  15135. default:
  15136. ShowError("buildin_setunitdata: Unknown data identifier %d for BL_PET.\n", type);
  15137. return SCRIPT_CMD_FAILURE;
  15138. }
  15139. break;
  15140. case BL_MER:
  15141. if (!mc) {
  15142. ShowWarning("buildin_setunitdata: Error in finding object BL_MER!\n");
  15143. return SCRIPT_CMD_FAILURE;
  15144. }
  15145. switch (type) {
  15146. case UMER_SIZE: mc->base_status.size = (unsigned char)value; break;
  15147. case UMER_HP: status_set_hp(bl, (unsigned int)value, 0); break;
  15148. case UMER_MAXHP: status_set_maxhp(bl, (unsigned int)value, 0); break;
  15149. case UMER_MASTERCID: mc->mercenary.char_id = (uint32)value; break;
  15150. case UMER_MAPID: if (mapname) value = map_mapname2mapid(mapname); unit_warp(bl, (short)value, 0, 0, CLR_TELEPORT); break;
  15151. case UMER_X: if (!unit_walktoxy(bl, (short)value, mc->bl.y, 2)) unit_movepos(bl, (short)value, mc->bl.y, 0, 0); break;
  15152. case UMER_Y: if (!unit_walktoxy(bl, mc->bl.x, (short)value, 2)) unit_movepos(bl, mc->bl.x, (short)value, 0, 0); break;
  15153. case UMER_KILLCOUNT: mc->mercenary.kill_count = (unsigned int)value; break;
  15154. case UMER_LIFETIME: mc->mercenary.life_time = (unsigned int)value; break;
  15155. case UMER_SPEED: mc->base_status.speed = (unsigned short)value; status_calc_misc(bl, &mc->base_status, mc->db->lv); break;
  15156. case UMER_LOOKDIR: unit_setdir(bl, (uint8)value); break;
  15157. case UMER_CANMOVETICK: mc->ud.canmove_tick = value > 0 ? (unsigned int)value : 0; break;
  15158. case UMER_STR: mc->base_status.str = (unsigned short)value; status_calc_misc(bl, &mc->base_status, mc->db->lv); break;
  15159. case UMER_AGI: mc->base_status.agi = (unsigned short)value; status_calc_misc(bl, &mc->base_status, mc->db->lv); break;
  15160. case UMER_VIT: mc->base_status.vit = (unsigned short)value; status_calc_misc(bl, &mc->base_status, mc->db->lv); break;
  15161. case UMER_INT: mc->base_status.int_ = (unsigned short)value; status_calc_misc(bl, &mc->base_status, mc->db->lv); break;
  15162. case UMER_DEX: mc->base_status.dex = (unsigned short)value; status_calc_misc(bl, &mc->base_status, mc->db->lv); break;
  15163. case UMER_LUK: mc->base_status.luk = (unsigned short)value; status_calc_misc(bl, &mc->base_status, mc->db->lv); break;
  15164. case UMER_DMGIMMUNE: mc->ud.immune_attack = (bool)value > 0 ? 1 : 0; break;
  15165. case UMER_ATKRANGE: mc->base_status.rhw.range = (unsigned short)value; break;
  15166. case UMER_ATKMIN: mc->base_status.rhw.atk = (unsigned short)value; break;
  15167. case UMER_ATKMAX: mc->base_status.rhw.atk2 = (unsigned short)value; break;
  15168. case UMER_MATKMIN: mc->base_status.matk_min = (unsigned short)value; break;
  15169. case UMER_MATKMAX: mc->base_status.matk_max = (unsigned short)value; break;
  15170. case UMER_DEF: mc->base_status.def = (defType)value; break;
  15171. case UMER_MDEF: mc->base_status.mdef = (defType)value; break;
  15172. case UMER_HIT: mc->base_status.hit = (short)value; break;
  15173. case UMER_FLEE: mc->base_status.flee = (short)value; break;
  15174. case UMER_PDODGE: mc->base_status.flee2 = (short)value; break;
  15175. case UMER_CRIT: mc->base_status.cri = (short)value; break;
  15176. case UMER_RACE: mc->base_status.race = (unsigned char)value; break;
  15177. case UMER_ELETYPE: mc->base_status.def_ele = (unsigned char)value; break;
  15178. case UMER_ELELEVEL: mc->base_status.ele_lv = (unsigned char)value; break;
  15179. case UMER_AMOTION: mc->base_status.amotion = (short)value; break;
  15180. case UMER_ADELAY: mc->base_status.adelay = (short)value; break;
  15181. case UMER_DMOTION: mc->base_status.dmotion = (short)value; break;
  15182. default:
  15183. ShowError("buildin_setunitdata: Unknown data identifier %d for BL_MER.\n", type);
  15184. return SCRIPT_CMD_FAILURE;
  15185. }
  15186. break;
  15187. case BL_ELEM:
  15188. if (!ed) {
  15189. ShowWarning("buildin_setunitdata: Error in finding object BL_ELEM!\n");
  15190. return SCRIPT_CMD_FAILURE;
  15191. }
  15192. switch (type) {
  15193. case UELE_SIZE: ed->base_status.size = (unsigned char)value; break;
  15194. case UELE_HP: status_set_hp(bl, (unsigned int)value, 0); break;
  15195. case UELE_MAXHP: status_set_maxhp(bl, (unsigned int)value, 0); break;
  15196. case UELE_SP: status_set_sp(bl, (unsigned int)value, 0); break;
  15197. case UELE_MAXSP: status_set_maxsp(bl, (unsigned int)value, 0); break;
  15198. case UELE_MASTERCID: ed->elemental.char_id = (uint32)value; break;
  15199. case UELE_MAPID: if (mapname) value = map_mapname2mapid(mapname); unit_warp(bl, (short)value, 0, 0, CLR_TELEPORT); break;
  15200. case UELE_X: if (!unit_walktoxy(bl, (short)value, ed->bl.y, 2)) unit_movepos(bl, (short)value, ed->bl.y, 0, 0); break;
  15201. case UELE_Y: if (!unit_walktoxy(bl, ed->bl.x, (short)value, 2)) unit_movepos(bl, ed->bl.x, (short)value, 0, 0); break;
  15202. case UELE_LIFETIME: ed->elemental.life_time = (unsigned int)value; break;
  15203. case UELE_MODE: ed->elemental.mode = (enum e_mode)value; break;
  15204. case UELE_SPEED: ed->base_status.speed = (unsigned short)value; status_calc_misc(bl, &ed->base_status, ed->db->lv); break;
  15205. case UELE_LOOKDIR: unit_setdir(bl, (uint8)value); break;
  15206. case UELE_CANMOVETICK: ed->ud.canmove_tick = value > 0 ? (unsigned int)value : 0; break;
  15207. case UELE_STR: ed->base_status.str = (unsigned short)value; status_calc_misc(bl, &ed->base_status, ed->db->lv); break;
  15208. case UELE_AGI: ed->base_status.agi = (unsigned short)value; status_calc_misc(bl, &ed->base_status, ed->db->lv); break;
  15209. case UELE_VIT: ed->base_status.vit = (unsigned short)value; status_calc_misc(bl, &ed->base_status, ed->db->lv); break;
  15210. case UELE_INT: ed->base_status.int_ = (unsigned short)value; status_calc_misc(bl, &ed->base_status, ed->db->lv); break;
  15211. case UELE_DEX: ed->base_status.dex = (unsigned short)value; status_calc_misc(bl, &ed->base_status, ed->db->lv); break;
  15212. case UELE_LUK: ed->base_status.luk = (unsigned short)value; status_calc_misc(bl, &ed->base_status, ed->db->lv); break;
  15213. case UELE_DMGIMMUNE: ed->ud.immune_attack = (bool)value > 0 ? 1 : 0; break;
  15214. case UELE_ATKRANGE: ed->base_status.rhw.range = (unsigned short)value; break;
  15215. case UELE_ATKMIN: ed->base_status.rhw.atk = (unsigned short)value; break;
  15216. case UELE_ATKMAX: ed->base_status.rhw.atk2 = (unsigned short)value; break;
  15217. case UELE_MATKMIN: ed->base_status.matk_min = (unsigned short)value; break;
  15218. case UELE_MATKMAX: ed->base_status.matk_max = (unsigned short)value; break;
  15219. case UELE_DEF: ed->base_status.def = (defType)value; break;
  15220. case UELE_MDEF: ed->base_status.mdef = (defType)value; break;
  15221. case UELE_HIT: ed->base_status.hit = (short)value; break;
  15222. case UELE_FLEE: ed->base_status.flee = (short)value; break;
  15223. case UELE_PDODGE: ed->base_status.flee2 = (short)value; break;
  15224. case UELE_CRIT: ed->base_status.cri = (short)value; break;
  15225. case UELE_RACE: ed->base_status.race = (unsigned char)value; break;
  15226. case UELE_ELETYPE: ed->base_status.def_ele = (unsigned char)value; break;
  15227. case UELE_ELELEVEL: ed->base_status.ele_lv = (unsigned char)value; break;
  15228. case UELE_AMOTION: ed->base_status.amotion = (short)value; break;
  15229. case UELE_ADELAY: ed->base_status.adelay = (short)value; break;
  15230. case UELE_DMOTION: ed->base_status.dmotion = (short)value; break;
  15231. default:
  15232. ShowError("buildin_setunitdata: Unknown data identifier %d for BL_ELEM.\n", type);
  15233. return SCRIPT_CMD_FAILURE;
  15234. }
  15235. break;
  15236. case BL_NPC:
  15237. if (!nd) {
  15238. ShowWarning("buildin_setunitdata: Error in finding object BL_NPC!\n");
  15239. return SCRIPT_CMD_FAILURE;
  15240. }
  15241. switch (type) {
  15242. case UNPC_DISPLAY: status_set_viewdata(bl, (unsigned short)value); break;
  15243. case UNPC_LEVEL: nd->level = (unsigned int)value; break;
  15244. case UNPC_HP: status_set_hp(bl, (unsigned int)value, 0); break;
  15245. case UNPC_MAXHP: status_set_maxhp(bl, (unsigned int)value, 0); break;
  15246. case UNPC_MAPID: if (mapname) value = map_mapname2mapid(mapname); unit_warp(bl, (short)value, 0, 0, CLR_TELEPORT); break;
  15247. case UNPC_X: if (!unit_walktoxy(bl, (short)value, nd->bl.y, 2)) unit_movepos(bl, (short)value, nd->bl.x, 0, 0); break;
  15248. case UNPC_Y: if (!unit_walktoxy(bl, nd->bl.x, (short)value, 2)) unit_movepos(bl, nd->bl.x, (short)value, 0, 0); break;
  15249. case UNPC_LOOKDIR: unit_setdir(bl, (uint8)value); break;
  15250. case UNPC_STR: nd->params.str = (unsigned short)value; status_calc_misc(bl, &nd->status, nd->level); break;
  15251. case UNPC_AGI: nd->params.agi = (unsigned short)value; status_calc_misc(bl, &nd->status, nd->level); break;
  15252. case UNPC_VIT: nd->params.vit = (unsigned short)value; status_calc_misc(bl, &nd->status, nd->level); break;
  15253. case UNPC_INT: nd->params.int_ = (unsigned short)value; status_calc_misc(bl, &nd->status, nd->level); break;
  15254. case UNPC_DEX: nd->params.dex = (unsigned short)value; status_calc_misc(bl, &nd->status, nd->level); break;
  15255. case UNPC_LUK: nd->params.luk = (unsigned short)value; status_calc_misc(bl, &nd->status, nd->level); break;
  15256. case UNPC_PLUSALLSTAT: nd->stat_point = (unsigned int)value; break;
  15257. case UNPC_ATKRANGE: nd->status.rhw.range = (unsigned short)value; break;
  15258. case UNPC_ATKMIN: nd->status.rhw.atk = (unsigned short)value; break;
  15259. case UNPC_ATKMAX: nd->status.rhw.atk2 = (unsigned short)value; break;
  15260. case UNPC_MATKMIN: nd->status.matk_min = (unsigned short)value; break;
  15261. case UNPC_MATKMAX: nd->status.matk_max = (unsigned short)value; break;
  15262. case UNPC_DEF: nd->status.def = (defType)value; break;
  15263. case UNPC_MDEF: nd->status.mdef = (defType)value; break;
  15264. case UNPC_HIT: nd->status.hit = (short)value; break;
  15265. case UNPC_FLEE: nd->status.flee = (short)value; break;
  15266. case UNPC_PDODGE: nd->status.flee2 = (short)value; break;
  15267. case UNPC_CRIT: nd->status.cri = (short)value; break;
  15268. case UNPC_RACE: nd->status.race = (unsigned char)value; break;
  15269. case UNPC_ELETYPE: nd->status.def_ele = (unsigned char)value; break;
  15270. case UNPC_ELELEVEL: nd->status.ele_lv = (unsigned char)value; break;
  15271. case UNPC_AMOTION: nd->status.amotion = (short)value; break;
  15272. case UNPC_ADELAY: nd->status.adelay = (short)value; break;
  15273. case UNPC_DMOTION: nd->status.dmotion = (short)value; break;
  15274. default:
  15275. ShowError("buildin_setunitdata: Unknown data identifier %d for BL_NPC.\n", type);
  15276. return SCRIPT_CMD_FAILURE;
  15277. }
  15278. break;
  15279. default:
  15280. ShowWarning("buildin_setunitdata: Unknown object type!\n");
  15281. return SCRIPT_CMD_FAILURE;
  15282. }
  15283. // Client information updates
  15284. switch (bl->type) {
  15285. case BL_HOM:
  15286. clif_send_homdata(hd->master, SP_ACK, 0);
  15287. break;
  15288. case BL_PET:
  15289. clif_send_petstatus(pd->master);
  15290. break;
  15291. case BL_MER:
  15292. clif_mercenary_info(map_charid2sd(md->master_id));
  15293. clif_mercenary_skillblock(map_charid2sd(md->master_id));
  15294. break;
  15295. case BL_ELEM:
  15296. clif_elemental_info(ed->master);
  15297. break;
  15298. }
  15299. return SCRIPT_CMD_SUCCESS;
  15300. }
  15301. /// Gets the name of a bl.
  15302. /// Supported types are [MOB|HOM|PET|NPC].
  15303. /// MER and ELEM don't support custom names.
  15304. ///
  15305. /// getunitname <unit id>;
  15306. BUILDIN_FUNC(getunitname)
  15307. {
  15308. struct block_list* bl = NULL;
  15309. bl = map_id2bl(script_getnum(st, 2));
  15310. if (!bl) {
  15311. ShowWarning("buildin_getunitname: Error in finding object with given game ID %d!\n", script_getnum(st, 2));
  15312. return SCRIPT_CMD_FAILURE;
  15313. }
  15314. script_pushstrcopy(st, status_get_name(bl));
  15315. return SCRIPT_CMD_SUCCESS;
  15316. }
  15317. /// Changes the name of a bl.
  15318. /// Supported types are [MOB|HOM|PET].
  15319. /// For NPC see 'setnpcdisplay', MER and ELEM don't support custom names.
  15320. ///
  15321. /// setunitname <unit id>,<name>;
  15322. BUILDIN_FUNC(setunitname)
  15323. {
  15324. struct block_list* bl = NULL;
  15325. TBL_MOB* md = NULL;
  15326. TBL_HOM* hd = NULL;
  15327. TBL_PET* pd = NULL;
  15328. bl = map_id2bl(script_getnum(st, 2));
  15329. if (!bl) {
  15330. ShowWarning("buildin_setunitname: Error in finding object with given game ID %d!\n", script_getnum(st, 2));
  15331. return SCRIPT_CMD_FAILURE;
  15332. }
  15333. switch (bl->type) {
  15334. case BL_MOB: md = map_id2md(bl->id); break;
  15335. case BL_HOM: hd = map_id2hd(bl->id); break;
  15336. case BL_PET: pd = map_id2pd(bl->id); break;
  15337. }
  15338. switch (bl->type) {
  15339. case BL_MOB:
  15340. if (!md) {
  15341. ShowWarning("buildin_setunitname: Error in finding object BL_MOB!\n");
  15342. return SCRIPT_CMD_FAILURE;
  15343. }
  15344. safestrncpy(md->name, script_getstr(st, 3), NAME_LENGTH);
  15345. break;
  15346. case BL_HOM:
  15347. if (!hd) {
  15348. ShowWarning("buildin_setunitname: Error in finding object BL_HOM!\n");
  15349. return SCRIPT_CMD_FAILURE;
  15350. }
  15351. safestrncpy(hd->homunculus.name, script_getstr(st, 3), NAME_LENGTH);
  15352. break;
  15353. case BL_PET:
  15354. if (!pd) {
  15355. ShowWarning("buildin_setunitname: Error in finding object BL_PET!\n");
  15356. return SCRIPT_CMD_FAILURE;
  15357. }
  15358. safestrncpy(pd->pet.name, script_getstr(st, 3), NAME_LENGTH);
  15359. break;
  15360. default:
  15361. ShowWarning("buildin_setunitname: Unknown object type!\n");
  15362. return SCRIPT_CMD_FAILURE;
  15363. }
  15364. clif_charnameack(0, bl); // Send update to client.
  15365. return SCRIPT_CMD_SUCCESS;
  15366. }
  15367. /// Makes the unit walk to target position or map.
  15368. /// Returns if it was successful.
  15369. ///
  15370. /// unitwalk(<unit_id>,<x>,<y>{,<event_label>}) -> <bool>
  15371. /// unitwalkto(<unit_id>,<target_id>{,<event_label>}) -> <bool>
  15372. BUILDIN_FUNC(unitwalk)
  15373. {
  15374. struct block_list* bl;
  15375. struct unit_data *ud = NULL;
  15376. const char *cmd = script_getfuncname(st), *done_label = "";
  15377. uint8 off = 5;
  15378. bl = map_id2bl(script_getnum(st,2));
  15379. if (!bl) {
  15380. ShowError("buildin_unitwalk: Invalid unit with ID '%d'.\n", script_getnum(st,2));
  15381. script_pushint(st, 0);
  15382. return SCRIPT_CMD_FAILURE;
  15383. }
  15384. ud = unit_bl2ud(bl);
  15385. if (!strcmp(cmd,"unitwalk")) {
  15386. int x = script_getnum(st,3);
  15387. int y = script_getnum(st,4);
  15388. if (script_pushint(st, unit_can_reach_pos(bl,x,y,0)))
  15389. add_timer(gettick()+50, unit_delay_walktoxy_timer, bl->id, (x<<16)|(y&0xFFFF)); // Need timer to avoid mismatches
  15390. } else {
  15391. struct block_list* tbl = map_id2bl(script_getnum(st,3));
  15392. if (!tbl) {
  15393. ShowError("buildin_unitwalk: Bad target destination.\n");
  15394. script_pushint(st, 0);
  15395. return SCRIPT_CMD_FAILURE;
  15396. } else if (script_pushint(st, unit_can_reach_bl(bl, tbl, distance_bl(bl, tbl)+1, 0, NULL, NULL)))
  15397. add_timer(gettick()+50, unit_delay_walktobl_timer, bl->id, tbl->id); // Need timer to avoid mismatches
  15398. off = 4;
  15399. }
  15400. if (ud && script_hasdata(st, off)) {
  15401. done_label = script_getstr(st, off);
  15402. check_event(st, done_label);
  15403. safestrncpy(ud->walk_done_event, done_label, sizeof(ud->walk_done_event));
  15404. }
  15405. return SCRIPT_CMD_SUCCESS;
  15406. }
  15407. /// Kills the unit.
  15408. ///
  15409. /// unitkill <unit_id>;
  15410. BUILDIN_FUNC(unitkill)
  15411. {
  15412. struct block_list* bl = map_id2bl(script_getnum(st,2));
  15413. if (bl != NULL)
  15414. status_kill(bl);
  15415. return SCRIPT_CMD_SUCCESS;
  15416. }
  15417. /// Warps the unit to the target position in the target map.
  15418. /// Returns if it was successful.
  15419. ///
  15420. /// unitwarp(<unit_id>,"<map name>",<x>,<y>) -> <bool>
  15421. BUILDIN_FUNC(unitwarp)
  15422. {
  15423. int unit_id;
  15424. int map_idx;
  15425. short x;
  15426. short y;
  15427. struct block_list* bl;
  15428. const char *mapname;
  15429. unit_id = script_getnum(st,2);
  15430. mapname = script_getstr(st, 3);
  15431. x = (short)script_getnum(st,4);
  15432. y = (short)script_getnum(st,5);
  15433. if (!unit_id) //Warp the script's runner
  15434. bl = map_id2bl(st->rid);
  15435. else
  15436. bl = map_id2bl(unit_id);
  15437. if (!strcmp(mapname,"this"))
  15438. map_idx = bl?bl->m:-1;
  15439. else
  15440. map_idx = map_mapname2mapid(mapname);
  15441. if (map_idx >= 0 && bl != NULL)
  15442. script_pushint(st, unit_warp(bl,map_idx,x,y,CLR_OUTSIGHT));
  15443. else
  15444. script_pushint(st, 0);
  15445. return SCRIPT_CMD_SUCCESS;
  15446. }
  15447. /// Makes the unit attack the target.
  15448. /// If the unit is a player and <action type> is not 0, it does a continuous
  15449. /// attack instead of a single attack.
  15450. /// Returns if the request was successful.
  15451. ///
  15452. /// unitattack(<unit_id>,"<target name>"{,<action type>}) -> <bool>
  15453. /// unitattack(<unit_id>,<target_id>{,<action type>}) -> <bool>
  15454. BUILDIN_FUNC(unitattack)
  15455. {
  15456. struct block_list* unit_bl;
  15457. struct block_list* target_bl = NULL;
  15458. struct script_data* data;
  15459. int actiontype = 0;
  15460. unit_bl = map_id2bl(script_getnum(st,2));
  15461. if (!unit_bl) {
  15462. script_pushint(st, 0);
  15463. return SCRIPT_CMD_FAILURE;
  15464. }
  15465. data = script_getdata(st, 3);
  15466. get_val(st, data);
  15467. if (data_isstring(data)) {
  15468. TBL_PC* sd = map_nick2sd(conv_str(st, data));
  15469. if( sd != NULL )
  15470. target_bl = &sd->bl;
  15471. } else
  15472. target_bl = map_id2bl(conv_num(st, data));
  15473. if (!target_bl) {
  15474. script_pushint(st, 0);
  15475. return SCRIPT_CMD_FAILURE;
  15476. }
  15477. if (script_hasdata(st,4))
  15478. actiontype = script_getnum(st,4);
  15479. switch(unit_bl->type) {
  15480. case BL_PC:
  15481. clif_parse_ActionRequest_sub(((TBL_PC *)unit_bl), actiontype > 0 ? 0x07 : 0x00, target_bl->id, gettick());
  15482. script_pushint(st, 1);
  15483. return SCRIPT_CMD_SUCCESS;
  15484. case BL_MOB:
  15485. ((TBL_MOB *)unit_bl)->target_id = target_bl->id;
  15486. break;
  15487. case BL_PET:
  15488. ((TBL_PET *)unit_bl)->target_id = target_bl->id;
  15489. break;
  15490. default:
  15491. ShowError("buildin_unitattack: Unsupported source unit type %d.\n", unit_bl->type);
  15492. script_pushint(st, 0);
  15493. return SCRIPT_CMD_FAILURE;
  15494. }
  15495. script_pushint(st, unit_walktobl(unit_bl, target_bl, 65025, 2));
  15496. return SCRIPT_CMD_SUCCESS;
  15497. }
  15498. /// Makes the unit stop attacking.
  15499. ///
  15500. /// unitstopattack <unit_id>;
  15501. BUILDIN_FUNC(unitstopattack)
  15502. {
  15503. int unit_id;
  15504. struct block_list* bl;
  15505. unit_id = script_getnum(st,2);
  15506. bl = map_id2bl(unit_id);
  15507. if (bl != NULL) {
  15508. unit_stop_attack(bl);
  15509. if (bl->type == BL_MOB)
  15510. ((TBL_MOB*)bl)->target_id = 0;
  15511. }
  15512. return SCRIPT_CMD_SUCCESS;
  15513. }
  15514. /// Makes the unit stop walking.
  15515. ///
  15516. /// unitstopwalk <unit_id>;
  15517. BUILDIN_FUNC(unitstopwalk)
  15518. {
  15519. int unit_id;
  15520. struct block_list* bl;
  15521. unit_id = script_getnum(st,2);
  15522. bl = map_id2bl(unit_id);
  15523. if (bl != NULL)
  15524. unit_stop_walking(bl, 0);
  15525. return SCRIPT_CMD_SUCCESS;
  15526. }
  15527. /// Makes the unit say the given message.
  15528. ///
  15529. /// unittalk <unit_id>,"<message>";
  15530. BUILDIN_FUNC(unittalk)
  15531. {
  15532. int unit_id;
  15533. const char* message;
  15534. struct block_list* bl;
  15535. unit_id = script_getnum(st,2);
  15536. message = script_getstr(st, 3);
  15537. bl = map_id2bl(unit_id);
  15538. if (bl != NULL) {
  15539. struct StringBuf sbuf;
  15540. StringBuf_Init(&sbuf);
  15541. StringBuf_Printf(&sbuf, "%s : %s", status_get_name(bl), message);
  15542. clif_disp_overhead(bl, StringBuf_Value(&sbuf));
  15543. StringBuf_Destroy(&sbuf);
  15544. }
  15545. return SCRIPT_CMD_SUCCESS;
  15546. }
  15547. /// Makes the unit do an emotion.
  15548. ///
  15549. /// unitemote <unit_id>,<emotion>;
  15550. ///
  15551. /// @see e_* in db/const.txt
  15552. BUILDIN_FUNC(unitemote)
  15553. {
  15554. int unit_id;
  15555. int emotion;
  15556. struct block_list* bl;
  15557. unit_id = script_getnum(st,2);
  15558. emotion = script_getnum(st,3);
  15559. bl = map_id2bl(unit_id);
  15560. if (bl != NULL)
  15561. clif_emotion(bl, emotion);
  15562. return SCRIPT_CMD_SUCCESS;
  15563. }
  15564. /// Makes the unit cast the skill on the target or self if no target is specified.
  15565. ///
  15566. /// unitskilluseid <unit_id>,<skill_id>,<skill_lv>{,<target_id>,<casttime>};
  15567. /// unitskilluseid <unit_id>,"<skill name>",<skill_lv>{,<target_id>,<casttime>};
  15568. BUILDIN_FUNC(unitskilluseid)
  15569. {
  15570. int unit_id, target_id, casttime;
  15571. uint16 skill_id, skill_lv;
  15572. struct block_list* bl;
  15573. struct script_data *data;
  15574. unit_id = script_getnum(st,2);
  15575. data = script_getdata(st, 3);
  15576. get_val(st, data); // Convert into value in case of a variable
  15577. skill_id = ( data_isstring(data) ? skill_name2id(script_getstr(st,3)) : script_getnum(st,3) );
  15578. skill_lv = script_getnum(st,4);
  15579. target_id = ( script_hasdata(st,5) ? script_getnum(st,5) : unit_id );
  15580. casttime = ( script_hasdata(st,6) ? script_getnum(st,6) : 0 );
  15581. bl = map_id2bl(unit_id);
  15582. if (bl != NULL) {
  15583. if (bl->type == BL_NPC) {
  15584. if (!((TBL_NPC*)bl)->status.hp)
  15585. status_calc_npc(((TBL_NPC*)bl), SCO_FIRST);
  15586. else
  15587. status_calc_npc(((TBL_NPC*)bl), SCO_NONE);
  15588. }
  15589. unit_skilluse_id2(bl, target_id, skill_id, skill_lv, (casttime * 1000) + skill_castfix(bl, skill_id, skill_lv), skill_get_castcancel(skill_id));
  15590. }
  15591. return SCRIPT_CMD_SUCCESS;
  15592. }
  15593. /// Makes the unit cast the skill on the target position.
  15594. ///
  15595. /// unitskillusepos <unit_id>,<skill_id>,<skill_lv>,<target_x>,<target_y>{,<casttime>};
  15596. /// unitskillusepos <unit_id>,"<skill name>",<skill_lv>,<target_x>,<target_y>{,<casttime>};
  15597. BUILDIN_FUNC(unitskillusepos)
  15598. {
  15599. int unit_id, skill_x, skill_y, casttime;
  15600. uint16 skill_id, skill_lv;
  15601. struct block_list* bl;
  15602. struct script_data *data;
  15603. unit_id = script_getnum(st,2);
  15604. data = script_getdata(st, 3);
  15605. get_val(st, data); // Convert into value in case of a variable
  15606. skill_id = ( data_isstring(data) ? skill_name2id(script_getstr(st,3)) : script_getnum(st,3) );
  15607. skill_lv = script_getnum(st,4);
  15608. skill_x = script_getnum(st,5);
  15609. skill_y = script_getnum(st,6);
  15610. casttime = ( script_hasdata(st,7) ? script_getnum(st,7) : 0 );
  15611. bl = map_id2bl(unit_id);
  15612. if (bl != NULL) {
  15613. if (bl->type == BL_NPC) {
  15614. if (!((TBL_NPC*)bl)->status.hp)
  15615. status_calc_npc(((TBL_NPC*)bl), SCO_FIRST);
  15616. else
  15617. status_calc_npc(((TBL_NPC*)bl), SCO_NONE);
  15618. }
  15619. 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));
  15620. }
  15621. return SCRIPT_CMD_SUCCESS;
  15622. }
  15623. // <--- [zBuffer] List of unit control commands
  15624. /// Pauses the execution of the script, detaching the player
  15625. ///
  15626. /// sleep <mili seconds>;
  15627. BUILDIN_FUNC(sleep)
  15628. {
  15629. int ticks;
  15630. ticks = script_getnum(st,2);
  15631. // detach the player
  15632. script_detach_rid(st);
  15633. if( ticks <= 0 )
  15634. {// do nothing
  15635. }
  15636. else if( st->sleep.tick == 0 )
  15637. {// sleep for the target amount of time
  15638. st->state = RERUNLINE;
  15639. st->sleep.tick = ticks;
  15640. }
  15641. else
  15642. {// sleep time is over
  15643. st->state = RUN;
  15644. st->sleep.tick = 0;
  15645. }
  15646. return SCRIPT_CMD_SUCCESS;
  15647. }
  15648. /// Pauses the execution of the script, keeping the player attached
  15649. /// Returns if a player is still attached
  15650. ///
  15651. /// sleep2(<mili secconds>) -> <bool>
  15652. BUILDIN_FUNC(sleep2)
  15653. {
  15654. int ticks;
  15655. ticks = script_getnum(st,2);
  15656. if( ticks <= 0 )
  15657. {// do nothing
  15658. script_pushint(st, (map_id2sd(st->rid)!=NULL));
  15659. }
  15660. else if( !st->sleep.tick )
  15661. {// sleep for the target amount of time
  15662. st->state = RERUNLINE;
  15663. st->sleep.tick = ticks;
  15664. }
  15665. else
  15666. {// sleep time is over
  15667. st->state = RUN;
  15668. st->sleep.tick = 0;
  15669. script_pushint(st, (map_id2sd(st->rid)!=NULL));
  15670. }
  15671. return SCRIPT_CMD_SUCCESS;
  15672. }
  15673. /// Awakes all the sleep timers of the target npc
  15674. ///
  15675. /// awake "<npc name>";
  15676. BUILDIN_FUNC(awake)
  15677. {
  15678. DBIterator *iter;
  15679. struct script_state *tst;
  15680. struct npc_data* nd;
  15681. if ((nd = npc_name2id(script_getstr(st, 2))) == NULL) {
  15682. ShowError("buildin_awake: NPC \"%s\" not found\n", script_getstr(st, 2));
  15683. return SCRIPT_CMD_FAILURE;
  15684. }
  15685. iter = db_iterator(st_db);
  15686. for (tst = dbi_first(iter); dbi_exists(iter); tst = dbi_next(iter)) {
  15687. if (tst->oid == nd->bl.id) {
  15688. TBL_PC* sd = map_id2sd(tst->rid);
  15689. if (tst->sleep.timer == INVALID_TIMER) { // already awake ???
  15690. continue;
  15691. }
  15692. if ((sd && sd->status.char_id != tst->sleep.charid) || (tst->rid && !sd)) {
  15693. // char not online anymore / another char of the same account is online - Cancel execution
  15694. tst->state = END;
  15695. tst->rid = 0;
  15696. }
  15697. delete_timer(tst->sleep.timer, run_script_timer);
  15698. tst->sleep.timer = INVALID_TIMER;
  15699. if (tst->state != RERUNLINE)
  15700. tst->sleep.tick = 0;
  15701. run_script_main(tst);
  15702. }
  15703. }
  15704. dbi_destroy(iter);
  15705. return SCRIPT_CMD_SUCCESS;
  15706. }
  15707. /// Returns a reference to a variable of the target NPC.
  15708. /// Returns 0 if an error occurs.
  15709. ///
  15710. /// getvariableofnpc(<variable>, "<npc name>") -> <reference>
  15711. BUILDIN_FUNC(getvariableofnpc)
  15712. {
  15713. struct script_data* data;
  15714. const char* name;
  15715. struct npc_data* nd;
  15716. data = script_getdata(st,2);
  15717. if( !data_isreference(data) )
  15718. {// Not a reference (aka varaible name)
  15719. ShowError("buildin_getvariableofnpc: not a variable\n");
  15720. script_reportdata(data);
  15721. script_pushnil(st);
  15722. st->state = END;
  15723. return SCRIPT_CMD_FAILURE;
  15724. }
  15725. name = reference_getname(data);
  15726. if( *name != '.' || name[1] == '@' )
  15727. {// not a npc variable
  15728. ShowError("buildin_getvariableofnpc: invalid scope (not npc variable)\n");
  15729. script_reportdata(data);
  15730. script_pushnil(st);
  15731. st->state = END;
  15732. return SCRIPT_CMD_FAILURE;
  15733. }
  15734. nd = npc_name2id(script_getstr(st,3));
  15735. if( nd == NULL || nd->subtype != NPCTYPE_SCRIPT || nd->u.scr.script == NULL )
  15736. {// NPC not found or has no script
  15737. ShowError("buildin_getvariableofnpc: can't find npc %s\n", script_getstr(st,3));
  15738. script_pushnil(st);
  15739. st->state = END;
  15740. return SCRIPT_CMD_FAILURE;
  15741. }
  15742. if (!nd->u.scr.script->local.vars)
  15743. nd->u.scr.script->local.vars = i64db_alloc(DB_OPT_RELEASE_DATA);
  15744. push_val2(st->stack, C_NAME, reference_getuid(data), &nd->u.scr.script->local);
  15745. return SCRIPT_CMD_SUCCESS;
  15746. }
  15747. /// Opens a warp portal.
  15748. /// Has no "portal opening" effect/sound, it opens the portal immediately.
  15749. ///
  15750. /// warpportal <source x>,<source y>,"<target map>",<target x>,<target y>;
  15751. ///
  15752. /// @author blackhole89
  15753. BUILDIN_FUNC(warpportal)
  15754. {
  15755. int spx;
  15756. int spy;
  15757. unsigned short mapindex;
  15758. int tpx;
  15759. int tpy;
  15760. struct skill_unit_group* group;
  15761. struct block_list* bl;
  15762. bl = map_id2bl(st->oid);
  15763. if( bl == NULL ) {
  15764. ShowError("buildin_warpportal: NPC is needed\n");
  15765. return SCRIPT_CMD_FAILURE;
  15766. }
  15767. spx = script_getnum(st,2);
  15768. spy = script_getnum(st,3);
  15769. mapindex = mapindex_name2id(script_getstr(st, 4));
  15770. tpx = script_getnum(st,5);
  15771. tpy = script_getnum(st,6);
  15772. if( mapindex == 0 ) {
  15773. ShowError("buildin_warpportal: Target map not found %s.\n", script_getstr(st, 4));
  15774. return SCRIPT_CMD_FAILURE;
  15775. }
  15776. group = skill_unitsetting(bl, AL_WARP, 4, spx, spy, 0);
  15777. if( group == NULL )
  15778. return SCRIPT_CMD_FAILURE;// failed
  15779. group->val1 = (group->val1<<16)|(short)0;
  15780. group->val2 = (tpx<<16) | tpy;
  15781. group->val3 = mapindex;
  15782. return SCRIPT_CMD_SUCCESS;
  15783. }
  15784. /**
  15785. * openmail({<char_id>});
  15786. **/
  15787. BUILDIN_FUNC(openmail)
  15788. {
  15789. TBL_PC* sd;
  15790. if (!script_charid2sd(2,sd))
  15791. return SCRIPT_CMD_FAILURE;
  15792. mail_openmail(sd);
  15793. return SCRIPT_CMD_SUCCESS;
  15794. }
  15795. /**
  15796. * openauction({<char_id>});
  15797. **/
  15798. BUILDIN_FUNC(openauction)
  15799. {
  15800. TBL_PC* sd;
  15801. if (!script_charid2sd(2,sd))
  15802. return SCRIPT_CMD_FAILURE;
  15803. if( !battle_config.feature_auction ) {
  15804. clif_colormes(sd->fd, color_table[COLOR_RED], msg_txt(sd, 517));
  15805. return SCRIPT_CMD_SUCCESS;
  15806. }
  15807. clif_Auction_openwindow(sd);
  15808. return SCRIPT_CMD_SUCCESS;
  15809. }
  15810. /// Retrieves the value of the specified flag of the specified cell.
  15811. ///
  15812. /// checkcell("<map name>",<x>,<y>,<type>) -> <bool>
  15813. ///
  15814. /// @see cell_chk* constants in const.txt for the types
  15815. BUILDIN_FUNC(checkcell)
  15816. {
  15817. int16 m = map_mapname2mapid(script_getstr(st,2));
  15818. int16 x = script_getnum(st,3);
  15819. int16 y = script_getnum(st,4);
  15820. cell_chk type = (cell_chk)script_getnum(st,5);
  15821. script_pushint(st, map_getcell(m, x, y, type));
  15822. return SCRIPT_CMD_SUCCESS;
  15823. }
  15824. /// Modifies flags of cells in the specified area.
  15825. ///
  15826. /// setcell "<map name>",<x1>,<y1>,<x2>,<y2>,<type>,<flag>;
  15827. ///
  15828. /// @see cell_* constants in const.txt for the types
  15829. BUILDIN_FUNC(setcell)
  15830. {
  15831. int16 m = map_mapname2mapid(script_getstr(st,2));
  15832. int16 x1 = script_getnum(st,3);
  15833. int16 y1 = script_getnum(st,4);
  15834. int16 x2 = script_getnum(st,5);
  15835. int16 y2 = script_getnum(st,6);
  15836. cell_t type = (cell_t)script_getnum(st,7);
  15837. bool flag = (bool)script_getnum(st,8);
  15838. int x,y;
  15839. if( x1 > x2 ) swap(x1,x2);
  15840. if( y1 > y2 ) swap(y1,y2);
  15841. for( y = y1; y <= y2; ++y )
  15842. for( x = x1; x <= x2; ++x )
  15843. map_setcell(m, x, y, type, flag);
  15844. return SCRIPT_CMD_SUCCESS;
  15845. }
  15846. /*==========================================
  15847. * Mercenary Commands
  15848. *------------------------------------------*/
  15849. BUILDIN_FUNC(mercenary_create)
  15850. {
  15851. struct map_session_data *sd;
  15852. int class_, contract_time;
  15853. if( (sd = script_rid2sd(st)) == NULL || sd->md || sd->status.mer_id != 0 )
  15854. return SCRIPT_CMD_SUCCESS;
  15855. class_ = script_getnum(st,2);
  15856. if( !mercenary_class(class_) )
  15857. return SCRIPT_CMD_SUCCESS;
  15858. contract_time = script_getnum(st,3);
  15859. mercenary_create(sd, class_, contract_time);
  15860. return SCRIPT_CMD_SUCCESS;
  15861. }
  15862. BUILDIN_FUNC(mercenary_heal)
  15863. {
  15864. struct map_session_data *sd = script_rid2sd(st);
  15865. int hp, sp;
  15866. if( sd == NULL || sd->md == NULL )
  15867. return SCRIPT_CMD_SUCCESS;
  15868. hp = script_getnum(st,2);
  15869. sp = script_getnum(st,3);
  15870. status_heal(&sd->md->bl, hp, sp, 0);
  15871. return SCRIPT_CMD_SUCCESS;
  15872. }
  15873. BUILDIN_FUNC(mercenary_sc_start)
  15874. {
  15875. struct map_session_data *sd = script_rid2sd(st);
  15876. enum sc_type type;
  15877. int tick, val1;
  15878. if( sd == NULL || sd->md == NULL )
  15879. return SCRIPT_CMD_SUCCESS;
  15880. type = (sc_type)script_getnum(st,2);
  15881. tick = script_getnum(st,3);
  15882. val1 = script_getnum(st,4);
  15883. status_change_start(NULL, &sd->md->bl, type, 10000, val1, 0, 0, 0, tick, SCSTART_NOTICKDEF);
  15884. return SCRIPT_CMD_SUCCESS;
  15885. }
  15886. BUILDIN_FUNC(mercenary_get_calls)
  15887. {
  15888. struct map_session_data *sd = script_rid2sd(st);
  15889. int guild;
  15890. if( sd == NULL )
  15891. return SCRIPT_CMD_SUCCESS;
  15892. guild = script_getnum(st,2);
  15893. switch( guild )
  15894. {
  15895. case ARCH_MERC_GUILD:
  15896. script_pushint(st,sd->status.arch_calls);
  15897. break;
  15898. case SPEAR_MERC_GUILD:
  15899. script_pushint(st,sd->status.spear_calls);
  15900. break;
  15901. case SWORD_MERC_GUILD:
  15902. script_pushint(st,sd->status.sword_calls);
  15903. break;
  15904. default:
  15905. script_pushint(st,0);
  15906. break;
  15907. }
  15908. return SCRIPT_CMD_SUCCESS;
  15909. }
  15910. BUILDIN_FUNC(mercenary_set_calls)
  15911. {
  15912. struct map_session_data *sd = script_rid2sd(st);
  15913. int guild, value, *calls;
  15914. if( sd == NULL )
  15915. return SCRIPT_CMD_SUCCESS;
  15916. guild = script_getnum(st,2);
  15917. value = script_getnum(st,3);
  15918. switch( guild )
  15919. {
  15920. case ARCH_MERC_GUILD:
  15921. calls = &sd->status.arch_calls;
  15922. break;
  15923. case SPEAR_MERC_GUILD:
  15924. calls = &sd->status.spear_calls;
  15925. break;
  15926. case SWORD_MERC_GUILD:
  15927. calls = &sd->status.sword_calls;
  15928. break;
  15929. default:
  15930. ShowError("buildin_mercenary_set_calls: Invalid guild '%d'.\n", guild);
  15931. return SCRIPT_CMD_SUCCESS; // Invalid Guild
  15932. }
  15933. *calls += value;
  15934. *calls = cap_value(*calls, 0, INT_MAX);
  15935. return SCRIPT_CMD_SUCCESS;
  15936. }
  15937. BUILDIN_FUNC(mercenary_get_faith)
  15938. {
  15939. struct map_session_data *sd = script_rid2sd(st);
  15940. int guild;
  15941. if( sd == NULL )
  15942. return SCRIPT_CMD_SUCCESS;
  15943. guild = script_getnum(st,2);
  15944. switch( guild )
  15945. {
  15946. case ARCH_MERC_GUILD:
  15947. script_pushint(st,sd->status.arch_faith);
  15948. break;
  15949. case SPEAR_MERC_GUILD:
  15950. script_pushint(st,sd->status.spear_faith);
  15951. break;
  15952. case SWORD_MERC_GUILD:
  15953. script_pushint(st,sd->status.sword_faith);
  15954. break;
  15955. default:
  15956. script_pushint(st,0);
  15957. break;
  15958. }
  15959. return SCRIPT_CMD_SUCCESS;
  15960. }
  15961. BUILDIN_FUNC(mercenary_set_faith)
  15962. {
  15963. struct map_session_data *sd = script_rid2sd(st);
  15964. int guild, value, *calls;
  15965. if( sd == NULL )
  15966. return SCRIPT_CMD_SUCCESS;
  15967. guild = script_getnum(st,2);
  15968. value = script_getnum(st,3);
  15969. switch( guild )
  15970. {
  15971. case ARCH_MERC_GUILD:
  15972. calls = &sd->status.arch_faith;
  15973. break;
  15974. case SPEAR_MERC_GUILD:
  15975. calls = &sd->status.spear_faith;
  15976. break;
  15977. case SWORD_MERC_GUILD:
  15978. calls = &sd->status.sword_faith;
  15979. break;
  15980. default:
  15981. ShowError("buildin_mercenary_set_faith: Invalid guild '%d'.\n", guild);
  15982. return SCRIPT_CMD_SUCCESS; // Invalid Guild
  15983. }
  15984. *calls += value;
  15985. *calls = cap_value(*calls, 0, INT_MAX);
  15986. if( mercenary_get_guild(sd->md) == guild )
  15987. clif_mercenary_updatestatus(sd,SP_MERCFAITH);
  15988. return SCRIPT_CMD_SUCCESS;
  15989. }
  15990. /*------------------------------------------
  15991. * Book Reading
  15992. *------------------------------------------*/
  15993. BUILDIN_FUNC(readbook)
  15994. {
  15995. struct map_session_data *sd;
  15996. int book_id, page;
  15997. if( (sd = script_rid2sd(st)) == NULL )
  15998. return SCRIPT_CMD_SUCCESS;
  15999. book_id = script_getnum(st,2);
  16000. page = script_getnum(st,3);
  16001. clif_readbook(sd->fd, book_id, page);
  16002. return SCRIPT_CMD_SUCCESS;
  16003. }
  16004. /******************
  16005. Questlog script commands
  16006. *******************/
  16007. /**
  16008. * Add job criteria to questinfo
  16009. * @param qi Quest Info
  16010. * @param job
  16011. * @author [Cydh]
  16012. **/
  16013. static void buildin_questinfo_setjob(struct questinfo *qi, int job) {
  16014. RECREATE(qi->jobid, unsigned short, qi->jobid_count+1);
  16015. qi->jobid[qi->jobid_count++] = job;
  16016. }
  16017. /**
  16018. * questinfo <Quest ID>,<Icon>{,<Map Mark Color>{,<Job Class>}};
  16019. **/
  16020. BUILDIN_FUNC(questinfo)
  16021. {
  16022. TBL_NPC* nd = map_id2nd(st->oid);
  16023. int quest_id, icon;
  16024. struct questinfo qi, *q2;
  16025. if( nd == NULL || nd->bl.m == -1 ) {
  16026. ShowError("buildin_questinfo: No NPC attached.\n");
  16027. return SCRIPT_CMD_FAILURE;
  16028. }
  16029. quest_id = script_getnum(st, 2);
  16030. icon = script_getnum(st, 3);
  16031. #if PACKETVER >= 20120410
  16032. switch(icon){
  16033. case QTYPE_QUEST:
  16034. case QTYPE_QUEST2:
  16035. case QTYPE_JOB:
  16036. case QTYPE_JOB2:
  16037. case QTYPE_EVENT:
  16038. case QTYPE_EVENT2:
  16039. case QTYPE_WARG:
  16040. case QTYPE_WARG2:
  16041. // Leave everything as it is
  16042. break;
  16043. case QTYPE_NONE:
  16044. default:
  16045. // Default to nothing if icon id is invalid.
  16046. icon = QTYPE_NONE;
  16047. break;
  16048. }
  16049. #else
  16050. if(icon < QTYPE_QUEST || icon > 7) // TODO: check why 7 and not QTYPE_WARG, might be related to icon + 1 below
  16051. icon = QTYPE_QUEST;
  16052. else
  16053. icon = icon + 1;
  16054. #endif
  16055. qi.quest_id = quest_id;
  16056. qi.icon = (unsigned char)icon;
  16057. qi.nd = nd;
  16058. if( script_hasdata(st, 4) ) {
  16059. int color = script_getnum(st, 4);
  16060. if( color < 0 || color > 3 ) {
  16061. ShowWarning("buildin_questinfo: invalid color '%d', changing to 0\n",color);
  16062. script_reportfunc(st);
  16063. color = 0;
  16064. }
  16065. qi.color = (unsigned char)color;
  16066. }
  16067. qi.min_level = 1;
  16068. qi.max_level = MAX_LEVEL;
  16069. q2 = map_add_questinfo(nd->bl.m, &qi);
  16070. q2->req = NULL;
  16071. q2->req_count = 0;
  16072. q2->jobid = NULL;
  16073. q2->jobid_count = 0;
  16074. if(script_hasdata(st, 5)) {
  16075. int job = script_getnum(st, 5);
  16076. if (!pcdb_checkid(job))
  16077. ShowError("buildin_questinfo: Nonexistant Job Class.\n");
  16078. else {
  16079. buildin_questinfo_setjob(q2, job);
  16080. }
  16081. }
  16082. return SCRIPT_CMD_SUCCESS;
  16083. }
  16084. /**
  16085. * setquest <ID>{,<char_id>};
  16086. **/
  16087. BUILDIN_FUNC(setquest)
  16088. {
  16089. struct map_session_data *sd;
  16090. int quest_id;
  16091. quest_id = script_getnum(st, 2);
  16092. if (!script_charid2sd(3,sd))
  16093. return SCRIPT_CMD_FAILURE;
  16094. if( quest_add(sd, quest_id) == -1 ){
  16095. script_reportsrc(st);
  16096. script_reportfunc(st);
  16097. }
  16098. //20120410 or 20090218 ? no reason that shouldn't work for 2009
  16099. pc_show_questinfo(sd);
  16100. return SCRIPT_CMD_SUCCESS;
  16101. }
  16102. /**
  16103. * erasequest <ID>{,<char_id>};
  16104. **/
  16105. BUILDIN_FUNC(erasequest)
  16106. {
  16107. struct map_session_data *sd;
  16108. if (!script_charid2sd(3,sd))
  16109. return SCRIPT_CMD_FAILURE;
  16110. if( quest_delete(sd, script_getnum(st, 2)) == -1 ){
  16111. script_reportsrc(st);
  16112. script_reportfunc(st);
  16113. }
  16114. return SCRIPT_CMD_SUCCESS;
  16115. }
  16116. /**
  16117. * completequest <ID>{,<char_id>};
  16118. **/
  16119. BUILDIN_FUNC(completequest)
  16120. {
  16121. struct map_session_data *sd;
  16122. if (!script_charid2sd(3,sd))
  16123. return SCRIPT_CMD_FAILURE;
  16124. quest_update_status(sd, script_getnum(st, 2), Q_COMPLETE);
  16125. //20120410 or 20090218
  16126. pc_show_questinfo(sd);
  16127. return SCRIPT_CMD_SUCCESS;
  16128. }
  16129. /**
  16130. * changequest <ID>,<ID2>{,<char_id>};
  16131. **/
  16132. BUILDIN_FUNC(changequest)
  16133. {
  16134. struct map_session_data *sd;
  16135. if (!script_charid2sd(4,sd))
  16136. return SCRIPT_CMD_FAILURE;
  16137. if( quest_change(sd, script_getnum(st, 2),script_getnum(st, 3)) == -1 ){
  16138. script_reportsrc(st);
  16139. script_reportfunc(st);
  16140. }
  16141. //20120410 or 20090218
  16142. pc_show_questinfo(sd);
  16143. return SCRIPT_CMD_SUCCESS;
  16144. }
  16145. /**
  16146. * checkquest(<ID>{,PLAYTIME|HUNTING{,<char_id>}})
  16147. **/
  16148. BUILDIN_FUNC(checkquest)
  16149. {
  16150. struct map_session_data *sd;
  16151. enum quest_check_type type = HAVEQUEST;
  16152. if( script_hasdata(st, 3) )
  16153. type = (enum quest_check_type)script_getnum(st, 3);
  16154. if (!script_charid2sd(4,sd))
  16155. return SCRIPT_CMD_FAILURE;
  16156. script_pushint(st, quest_check(sd, script_getnum(st, 2), type));
  16157. return SCRIPT_CMD_SUCCESS;
  16158. }
  16159. /**
  16160. * isbegin_quest(<ID>{,<char_id>})
  16161. **/
  16162. BUILDIN_FUNC(isbegin_quest)
  16163. {
  16164. struct map_session_data *sd;
  16165. int i;
  16166. if (!script_charid2sd(3,sd))
  16167. return SCRIPT_CMD_FAILURE;
  16168. i = quest_check(sd, script_getnum(st, 2), (enum quest_check_type) HAVEQUEST);
  16169. script_pushint(st, i + (i < 1));
  16170. return SCRIPT_CMD_SUCCESS;
  16171. }
  16172. /**
  16173. * showevent <icon>{,<mark color>{,<char_id>}}
  16174. **/
  16175. BUILDIN_FUNC(showevent)
  16176. {
  16177. TBL_PC *sd;
  16178. struct npc_data *nd = map_id2nd(st->oid);
  16179. int icon, color = 0;
  16180. if (!script_charid2sd(4,sd))
  16181. return SCRIPT_CMD_FAILURE;
  16182. if( sd == NULL || nd == NULL )
  16183. return SCRIPT_CMD_SUCCESS;
  16184. icon = script_getnum(st, 2);
  16185. if( script_hasdata(st, 3) ) {
  16186. color = script_getnum(st, 3);
  16187. if( color < 0 || color > 3 ) {
  16188. ShowWarning("buildin_showevent: invalid color '%d', changing to 0\n",color);
  16189. script_reportfunc(st);
  16190. color = 0;
  16191. }
  16192. }
  16193. #if PACKETVER >= 20120410
  16194. if(icon < 0 || (icon > 8 && icon != 9999) || icon == 7)
  16195. icon = 9999; // Default to nothing if icon id is invalid.
  16196. #else
  16197. if(icon < 0 || icon > 7)
  16198. icon = 0;
  16199. else
  16200. icon = icon + 1;
  16201. #endif
  16202. clif_quest_show_event(sd, &nd->bl, icon, color);
  16203. return SCRIPT_CMD_SUCCESS;
  16204. }
  16205. /*==========================================
  16206. * BattleGround System
  16207. *------------------------------------------*/
  16208. BUILDIN_FUNC(waitingroom2bg)
  16209. {
  16210. struct npc_data *nd;
  16211. struct chat_data *cd;
  16212. const char *map_name, *ev = "", *dev = "";
  16213. int x, y, mapindex = 0, bg_id;
  16214. unsigned char i,c=0;
  16215. if( script_hasdata(st,7) )
  16216. nd = npc_name2id(script_getstr(st,7));
  16217. else
  16218. nd = (struct npc_data *)map_id2bl(st->oid);
  16219. if( nd == NULL || (cd = (struct chat_data *)map_id2bl(nd->chat_id)) == NULL )
  16220. {
  16221. script_pushint(st,0);
  16222. return SCRIPT_CMD_SUCCESS;
  16223. }
  16224. map_name = script_getstr(st,2);
  16225. if( strcmp(map_name,"-") != 0 )
  16226. {
  16227. mapindex = mapindex_name2id(map_name);
  16228. if( mapindex == 0 )
  16229. { // Invalid Map
  16230. script_pushint(st,0);
  16231. return SCRIPT_CMD_SUCCESS;
  16232. }
  16233. }
  16234. x = script_getnum(st,3);
  16235. y = script_getnum(st,4);
  16236. ev = script_getstr(st,5); // Logout Event
  16237. dev = script_getstr(st,6); // Die Event
  16238. if( (bg_id = bg_create(mapindex, x, y, ev, dev)) == 0 )
  16239. { // Creation failed
  16240. script_pushint(st,0);
  16241. return SCRIPT_CMD_SUCCESS;
  16242. }
  16243. for (i = 0; i < cd->users; i++) { // Only add those who are in the chat room
  16244. struct map_session_data *sd;
  16245. if( (sd = cd->usersd[i]) != NULL && bg_team_join(bg_id, sd) ){
  16246. mapreg_setreg(reference_uid(add_str("$@arenamembers"), c), sd->bl.id);
  16247. ++c;
  16248. }
  16249. }
  16250. mapreg_setreg(add_str("$@arenamembersnum"), c);
  16251. script_pushint(st,bg_id);
  16252. return SCRIPT_CMD_SUCCESS;
  16253. }
  16254. BUILDIN_FUNC(waitingroom2bg_single)
  16255. {
  16256. const char* map_name;
  16257. struct npc_data *nd;
  16258. struct chat_data *cd;
  16259. struct map_session_data *sd;
  16260. int x, y, mapindex, bg_id;
  16261. bg_id = script_getnum(st,2);
  16262. map_name = script_getstr(st,3);
  16263. if( (mapindex = mapindex_name2id(map_name)) == 0 )
  16264. return SCRIPT_CMD_SUCCESS; // Invalid Map
  16265. x = script_getnum(st,4);
  16266. y = script_getnum(st,5);
  16267. nd = npc_name2id(script_getstr(st,6));
  16268. if( nd == NULL || (cd = (struct chat_data *)map_id2bl(nd->chat_id)) == NULL || cd->users <= 0 )
  16269. return SCRIPT_CMD_SUCCESS;
  16270. if( (sd = cd->usersd[0]) == NULL )
  16271. return SCRIPT_CMD_SUCCESS;
  16272. if( bg_team_join(bg_id, sd) )
  16273. {
  16274. pc_setpos(sd, mapindex, x, y, CLR_TELEPORT);
  16275. script_pushint(st,1);
  16276. }
  16277. else
  16278. script_pushint(st,0);
  16279. return SCRIPT_CMD_SUCCESS;
  16280. }
  16281. BUILDIN_FUNC(bg_team_setxy)
  16282. {
  16283. struct battleground_data *bg;
  16284. int bg_id;
  16285. bg_id = script_getnum(st,2);
  16286. if( (bg = bg_team_search(bg_id)) == NULL )
  16287. return SCRIPT_CMD_SUCCESS;
  16288. bg->x = script_getnum(st,3);
  16289. bg->y = script_getnum(st,4);
  16290. return SCRIPT_CMD_SUCCESS;
  16291. }
  16292. BUILDIN_FUNC(bg_warp)
  16293. {
  16294. int x, y, mapindex, bg_id;
  16295. const char* map_name;
  16296. bg_id = script_getnum(st,2);
  16297. map_name = script_getstr(st,3);
  16298. if( (mapindex = mapindex_name2id(map_name)) == 0 )
  16299. return SCRIPT_CMD_SUCCESS; // Invalid Map
  16300. x = script_getnum(st,4);
  16301. y = script_getnum(st,5);
  16302. bg_team_warp(bg_id, mapindex, x, y);
  16303. return SCRIPT_CMD_SUCCESS;
  16304. }
  16305. BUILDIN_FUNC(bg_monster)
  16306. {
  16307. int class_ = 0, x = 0, y = 0, bg_id = 0;
  16308. const char *str,*mapname, *evt="";
  16309. bg_id = script_getnum(st,2);
  16310. mapname = script_getstr(st,3);
  16311. x = script_getnum(st,4);
  16312. y = script_getnum(st,5);
  16313. str = script_getstr(st,6);
  16314. class_ = script_getnum(st,7);
  16315. if( script_hasdata(st,8) ) evt = script_getstr(st,8);
  16316. check_event(st, evt);
  16317. script_pushint(st, mob_spawn_bg(mapname,x,y,str,class_,evt,bg_id));
  16318. return SCRIPT_CMD_SUCCESS;
  16319. }
  16320. BUILDIN_FUNC(bg_monster_set_team)
  16321. {
  16322. struct mob_data *md;
  16323. struct block_list *mbl;
  16324. int id = script_getnum(st,2),
  16325. bg_id = script_getnum(st,3);
  16326. if( id == 0 || (mbl = map_id2bl(id)) == NULL || mbl->type != BL_MOB )
  16327. return SCRIPT_CMD_SUCCESS;
  16328. md = (TBL_MOB *)mbl;
  16329. md->bg_id = bg_id;
  16330. mob_stop_attack(md);
  16331. mob_stop_walking(md, 0);
  16332. md->target_id = md->attacked_id = 0;
  16333. clif_charnameack(0, &md->bl);
  16334. return SCRIPT_CMD_SUCCESS;
  16335. }
  16336. BUILDIN_FUNC(bg_leave)
  16337. {
  16338. struct map_session_data *sd = NULL;
  16339. if( !script_charid2sd(2,sd) || !sd->bg_id )
  16340. return SCRIPT_CMD_SUCCESS;
  16341. bg_team_leave(sd,0);
  16342. return SCRIPT_CMD_SUCCESS;
  16343. }
  16344. BUILDIN_FUNC(bg_destroy)
  16345. {
  16346. int bg_id = script_getnum(st,2);
  16347. bg_team_delete(bg_id);
  16348. return SCRIPT_CMD_SUCCESS;
  16349. }
  16350. BUILDIN_FUNC(bg_getareausers)
  16351. {
  16352. const char *str;
  16353. int16 m, x0, y0, x1, y1;
  16354. int bg_id;
  16355. int i = 0, c = 0;
  16356. struct battleground_data *bg = NULL;
  16357. bg_id = script_getnum(st,2);
  16358. str = script_getstr(st,3);
  16359. if( (bg = bg_team_search(bg_id)) == NULL || (m = map_mapname2mapid(str)) < 0 )
  16360. {
  16361. script_pushint(st,0);
  16362. return SCRIPT_CMD_SUCCESS;
  16363. }
  16364. x0 = script_getnum(st,4);
  16365. y0 = script_getnum(st,5);
  16366. x1 = script_getnum(st,6);
  16367. y1 = script_getnum(st,7);
  16368. for( i = 0; i < MAX_BG_MEMBERS; i++ )
  16369. {
  16370. struct map_session_data *sd;
  16371. if( (sd = bg->members[i].sd) == NULL )
  16372. continue;
  16373. if( sd->bl.m != m || sd->bl.x < x0 || sd->bl.y < y0 || sd->bl.x > x1 || sd->bl.y > y1 )
  16374. continue;
  16375. c++;
  16376. }
  16377. script_pushint(st,c);
  16378. return SCRIPT_CMD_SUCCESS;
  16379. }
  16380. BUILDIN_FUNC(bg_updatescore)
  16381. {
  16382. const char *str;
  16383. int16 m;
  16384. str = script_getstr(st,2);
  16385. if( (m = map_mapname2mapid(str)) < 0 )
  16386. return SCRIPT_CMD_SUCCESS;
  16387. map[m].bgscore_lion = script_getnum(st,3);
  16388. map[m].bgscore_eagle = script_getnum(st,4);
  16389. clif_bg_updatescore(m);
  16390. return SCRIPT_CMD_SUCCESS;
  16391. }
  16392. BUILDIN_FUNC(bg_get_data)
  16393. {
  16394. struct battleground_data *bg;
  16395. int bg_id = script_getnum(st,2),
  16396. type = script_getnum(st,3);
  16397. if( (bg = bg_team_search(bg_id)) == NULL )
  16398. {
  16399. script_pushint(st,0);
  16400. return SCRIPT_CMD_SUCCESS;
  16401. }
  16402. switch( type )
  16403. {
  16404. case 0: script_pushint(st, bg->count); break;
  16405. default:
  16406. ShowError("script:bg_get_data: unknown data identifier %d\n", type);
  16407. break;
  16408. }
  16409. return SCRIPT_CMD_SUCCESS;
  16410. }
  16411. /*==========================================
  16412. * Instancing System
  16413. *------------------------------------------*/
  16414. //Returns an Instance ID
  16415. //Checks NPC first, then if player is attached we check party
  16416. int script_instancegetid(struct script_state* st)
  16417. {
  16418. short instance_id = 0;
  16419. struct npc_data *nd;
  16420. if( (nd = map_id2nd(st->oid)) && nd->instance_id > 0 )
  16421. instance_id = nd->instance_id;
  16422. else {
  16423. struct map_session_data *sd;
  16424. struct party_data *p;
  16425. if( (sd = script_rid2sd(st)) != NULL && sd->status.party_id && (p = party_search(sd->status.party_id)) != NULL && p->instance_id )
  16426. instance_id = p->instance_id;
  16427. }
  16428. return instance_id;
  16429. }
  16430. /*==========================================
  16431. * Creates the instance
  16432. * Returns Instance ID if created successfully
  16433. *------------------------------------------*/
  16434. BUILDIN_FUNC(instance_create)
  16435. {
  16436. struct map_session_data *sd;
  16437. if((sd = script_rid2sd(st)) == NULL)
  16438. return -1;
  16439. script_pushint(st,instance_create(sd->status.party_id, script_getstr(st, 2)));
  16440. return SCRIPT_CMD_SUCCESS;
  16441. }
  16442. /*==========================================
  16443. * Destroys an instance (unofficial)
  16444. * Officially instances are only destroyed by timeout
  16445. *
  16446. * instance_destroy {<instance_id>};
  16447. *------------------------------------------*/
  16448. BUILDIN_FUNC(instance_destroy)
  16449. {
  16450. short instance_id;
  16451. if( script_hasdata(st,2) )
  16452. instance_id = script_getnum(st,2);
  16453. else
  16454. instance_id = script_instancegetid(st);
  16455. if( instance_id <= 0 || instance_id >= MAX_MAP_PER_SERVER ) {
  16456. ShowError("script:instance_destroy: Trying to destroy invalid instance %d.\n", instance_id);
  16457. return SCRIPT_CMD_SUCCESS;
  16458. }
  16459. instance_destroy(instance_id);
  16460. return SCRIPT_CMD_SUCCESS;
  16461. }
  16462. /*==========================================
  16463. * Warps player to instance
  16464. * Results:
  16465. * 0: Success
  16466. * 1: Character not in party
  16467. * 2: Party doesn't have instance
  16468. * 3: Other errors (instance not in DB, instance doesn't match with party, etc.)
  16469. *------------------------------------------*/
  16470. BUILDIN_FUNC(instance_enter)
  16471. {
  16472. struct map_session_data *sd = NULL;
  16473. int x = script_hasdata(st,3) ? script_getnum(st, 3) : -1;
  16474. int y = script_hasdata(st,4) ? script_getnum(st, 4) : -1;
  16475. if (!script_charid2sd(5,sd))
  16476. return SCRIPT_CMD_FAILURE;
  16477. if (x != -1 && y != -1)
  16478. script_pushint(st,instance_enter_position(sd,script_getstr(st, 2),x,y));
  16479. else
  16480. script_pushint(st,instance_enter(sd,script_getstr(st, 2)));
  16481. return SCRIPT_CMD_SUCCESS;
  16482. }
  16483. /*==========================================
  16484. * Returns the name of a duplicated NPC
  16485. *
  16486. * instance_npcname <npc_name>{,<instance_id>};
  16487. * <npc_name> is the full name of an NPC.
  16488. *------------------------------------------*/
  16489. BUILDIN_FUNC(instance_npcname)
  16490. {
  16491. const char *str;
  16492. short instance_id = 0;
  16493. struct npc_data *nd;
  16494. str = script_getstr(st,2);
  16495. if( script_hasdata(st,3) )
  16496. instance_id = script_getnum(st,3);
  16497. else
  16498. instance_id = script_instancegetid(st);
  16499. if( instance_id && (nd = npc_name2id(str)) != NULL ) {
  16500. static char npcname[NAME_LENGTH];
  16501. snprintf(npcname, sizeof(npcname), "dup_%d_%d", instance_id, nd->bl.id);
  16502. script_pushconststr(st,npcname);
  16503. } else {
  16504. ShowError("script:instance_npcname: invalid instance NPC (instance_id: %d, NPC name: \"%s\".)\n", instance_id, str);
  16505. st->state = END;
  16506. return SCRIPT_CMD_FAILURE;
  16507. }
  16508. return SCRIPT_CMD_SUCCESS;
  16509. }
  16510. /*==========================================
  16511. * Returns the name of a duplicated map
  16512. *
  16513. * instance_mapname <map_name>{,<instance_id>};
  16514. *------------------------------------------*/
  16515. BUILDIN_FUNC(instance_mapname)
  16516. {
  16517. const char *str;
  16518. int16 m;
  16519. short instance_id = 0;
  16520. str = script_getstr(st,2);
  16521. if( script_hasdata(st,3) )
  16522. instance_id = script_getnum(st,3);
  16523. else
  16524. instance_id = script_instancegetid(st);
  16525. // Check that instance mapname is a valid map
  16526. if(!instance_id || (m = instance_mapname2mapid(str,instance_id)) < 0)
  16527. script_pushconststr(st, "");
  16528. else
  16529. script_pushconststr(st, map[m].name);
  16530. return SCRIPT_CMD_SUCCESS;
  16531. }
  16532. /*==========================================
  16533. * Returns an Instance ID
  16534. *------------------------------------------*/
  16535. BUILDIN_FUNC(instance_id)
  16536. {
  16537. short instance_id;
  16538. instance_id = script_instancegetid(st);
  16539. if(!instance_id) {
  16540. //ShowError("script:instance_id: No instance attached to NPC or player");
  16541. script_pushint(st, 0);
  16542. return SCRIPT_CMD_SUCCESS;
  16543. }
  16544. script_pushint(st, instance_id);
  16545. return SCRIPT_CMD_SUCCESS;
  16546. }
  16547. /*==========================================
  16548. * Warps all players inside an instance
  16549. *
  16550. * instance_warpall <map_name>,<x>,<y>{,<instance_id>};
  16551. *------------------------------------------*/
  16552. BUILDIN_FUNC(instance_warpall)
  16553. {
  16554. struct party_data *p;
  16555. int16 m, i;
  16556. short instance_id;
  16557. const char *mapn;
  16558. int x, y;
  16559. mapn = script_getstr(st,2);
  16560. x = script_getnum(st,3);
  16561. y = script_getnum(st,4);
  16562. if( script_hasdata(st,5) )
  16563. instance_id = script_getnum(st,5);
  16564. else
  16565. instance_id = script_instancegetid(st);
  16566. if( !instance_id || (m = map_mapname2mapid(mapn)) < 0 || (m = instance_mapname2mapid(map[m].name,instance_id)) < 0)
  16567. return SCRIPT_CMD_SUCCESS;
  16568. if( !(p = party_search(instance_data[instance_id].party_id)) )
  16569. return SCRIPT_CMD_SUCCESS;
  16570. for( i = 0; i < MAX_PARTY; i++ ) {
  16571. struct map_session_data *pl_sd;
  16572. 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);
  16573. }
  16574. return SCRIPT_CMD_SUCCESS;
  16575. }
  16576. /*==========================================
  16577. * Broadcasts to all maps inside an instance
  16578. *
  16579. * instance_announce <instance id>,"<text>",<flag>{,<fontColor>{,<fontType>{,<fontSize>{,<fontAlign>{,<fontY>}}}}};
  16580. * Using -1 for <instance id> will auto-detect the id.
  16581. *------------------------------------------*/
  16582. BUILDIN_FUNC(instance_announce) {
  16583. int instance_id = script_getnum(st,2);
  16584. const char *mes = script_getstr(st,3);
  16585. int flag = script_getnum(st,4);
  16586. const char *fontColor = script_hasdata(st,5) ? script_getstr(st,5) : NULL;
  16587. int fontType = script_hasdata(st,6) ? script_getnum(st,6) : FW_NORMAL; // default fontType
  16588. int fontSize = script_hasdata(st,7) ? script_getnum(st,7) : 12; // default fontSize
  16589. int fontAlign = script_hasdata(st,8) ? script_getnum(st,8) : 0; // default fontAlign
  16590. int fontY = script_hasdata(st,9) ? script_getnum(st,9) : 0; // default fontY
  16591. int i;
  16592. if( instance_id == -1 ) {
  16593. instance_id = script_instancegetid(st);
  16594. }
  16595. if( !instance_id && &instance_data[instance_id] != NULL) {
  16596. ShowError("buildin_instance_announce: Intance is not found.\n");
  16597. return SCRIPT_CMD_FAILURE;
  16598. }
  16599. for( i = 0; i < instance_data[instance_id].cnt_map; i++ )
  16600. map_foreachinmap(buildin_announce_sub, instance_data[instance_id].map[i].m, BL_PC,
  16601. mes, strlen(mes)+1, flag&BC_COLOR_MASK, fontColor, fontType, fontSize, fontAlign, fontY);
  16602. return SCRIPT_CMD_SUCCESS;
  16603. }
  16604. /*==========================================
  16605. * instance_check_party [malufett]
  16606. * Values:
  16607. * party_id : Party ID of the invoking character. [Required Parameter]
  16608. * amount : Amount of needed Partymembers for the Instance. [Optional Parameter]
  16609. * min : Minimum Level needed to join the Instance. [Optional Parameter]
  16610. * max : Maxium Level allowed to join the Instance. [Optional Parameter]
  16611. * Example: instance_check_party (getcharid(1){,amount}{,min}{,max});
  16612. * Example 2: instance_check_party (getcharid(1),1,1,99);
  16613. *------------------------------------------*/
  16614. BUILDIN_FUNC(instance_check_party)
  16615. {
  16616. int amount, min, max, i, party_id, c = 0;
  16617. struct party_data *p = NULL;
  16618. amount = script_hasdata(st,3) ? script_getnum(st,3) : 1; // Amount of needed Partymembers for the Instance.
  16619. min = script_hasdata(st,4) ? script_getnum(st,4) : 1; // Minimum Level needed to join the Instance.
  16620. max = script_hasdata(st,5) ? script_getnum(st,5) : MAX_LEVEL; // Maxium Level allowed to join the Instance.
  16621. if( min < 1 || min > MAX_LEVEL) {
  16622. ShowError("buildin_instance_check_party: Invalid min level, %d\n", min);
  16623. return SCRIPT_CMD_FAILURE;
  16624. } else if( max < 1 || max > MAX_LEVEL) {
  16625. ShowError("buildin_instance_check_party: Invalid max level, %d\n", max);
  16626. return SCRIPT_CMD_FAILURE;
  16627. }
  16628. if( script_hasdata(st,2) )
  16629. party_id = script_getnum(st,2);
  16630. else return SCRIPT_CMD_SUCCESS;
  16631. if( !(p = party_search(party_id)) ) {
  16632. script_pushint(st, 0); // Returns false if party does not exist.
  16633. return SCRIPT_CMD_SUCCESS;
  16634. }
  16635. for( i = 0; i < MAX_PARTY; i++ ) {
  16636. struct map_session_data *pl_sd;
  16637. if( (pl_sd = p->data[i].sd) )
  16638. if(map_id2bl(pl_sd->bl.id)) {
  16639. if(pl_sd->status.base_level < min) {
  16640. script_pushint(st, 0);
  16641. return SCRIPT_CMD_SUCCESS;
  16642. } else if(pl_sd->status.base_level > max) {
  16643. script_pushint(st, 0);
  16644. return SCRIPT_CMD_SUCCESS;
  16645. }
  16646. c++;
  16647. }
  16648. }
  16649. if(c < amount)
  16650. script_pushint(st, 0); // Not enough Members in the Party to join Instance.
  16651. else
  16652. script_pushint(st, 1);
  16653. return SCRIPT_CMD_SUCCESS;
  16654. }
  16655. /*==========================================
  16656. * Custom Fonts
  16657. *------------------------------------------*/
  16658. BUILDIN_FUNC(setfont)
  16659. {
  16660. struct map_session_data *sd = script_rid2sd(st);
  16661. int font = script_getnum(st,2);
  16662. if( sd == NULL )
  16663. return SCRIPT_CMD_SUCCESS;
  16664. if( sd->status.font != font )
  16665. sd->status.font = font;
  16666. else
  16667. sd->status.font = 0;
  16668. clif_font(sd);
  16669. return SCRIPT_CMD_SUCCESS;
  16670. }
  16671. static int buildin_mobuseskill_sub(struct block_list *bl,va_list ap)
  16672. {
  16673. TBL_MOB* md = (TBL_MOB*)bl;
  16674. struct block_list *tbl;
  16675. int mobid = va_arg(ap,int);
  16676. uint16 skill_id = va_arg(ap,int);
  16677. uint16 skill_lv = va_arg(ap,int);
  16678. int casttime = va_arg(ap,int);
  16679. int cancel = va_arg(ap,int);
  16680. int emotion = va_arg(ap,int);
  16681. int target = va_arg(ap,int);
  16682. if( md->mob_id != mobid )
  16683. return 0;
  16684. // 0:self, 1:target, 2:master, default:random
  16685. switch( target ) {
  16686. case 0: tbl = map_id2bl(md->bl.id); break;
  16687. case 1: tbl = map_id2bl(md->target_id); break;
  16688. case 2: tbl = map_id2bl(md->master_id); break;
  16689. default:tbl = battle_getenemy(&md->bl, DEFAULT_ENEMY_TYPE(md),skill_get_range2(&md->bl, skill_id, skill_lv)); break;
  16690. }
  16691. if( !tbl )
  16692. return 0;
  16693. if( md->ud.skilltimer != INVALID_TIMER ) // Cancel the casting skill.
  16694. unit_skillcastcancel(bl,0);
  16695. if( skill_get_casttype(skill_id) == CAST_GROUND )
  16696. unit_skilluse_pos2(&md->bl, tbl->x, tbl->y, skill_id, skill_lv, casttime, cancel);
  16697. else
  16698. unit_skilluse_id2(&md->bl, tbl->id, skill_id, skill_lv, casttime, cancel);
  16699. clif_emotion(&md->bl, emotion);
  16700. return 1;
  16701. }
  16702. /*==========================================
  16703. * areamobuseskill "Map Name",<x>,<y>,<range>,<Mob ID>,"Skill Name"/<Skill ID>,<Skill Lv>,<Cast Time>,<Cancelable>,<Emotion>,<Target Type>;
  16704. *------------------------------------------*/
  16705. BUILDIN_FUNC(areamobuseskill)
  16706. {
  16707. struct block_list center;
  16708. struct script_data *data;
  16709. int16 m;
  16710. int range,mobid,skill_id,skill_lv,casttime,emotion,target,cancel;
  16711. if( (m = map_mapname2mapid(script_getstr(st,2))) < 0 ) {
  16712. ShowError("areamobuseskill: invalid map name.\n");
  16713. return SCRIPT_CMD_SUCCESS;
  16714. }
  16715. center.m = m;
  16716. center.x = script_getnum(st,3);
  16717. center.y = script_getnum(st,4);
  16718. range = script_getnum(st,5);
  16719. mobid = script_getnum(st,6);
  16720. data = script_getdata(st, 7);
  16721. get_val(st, data); // Convert into value in case of a variable
  16722. skill_id = ( data_isstring(data) ? skill_name2id(script_getstr(st,7)) : script_getnum(st,7) );
  16723. if( (skill_lv = script_getnum(st,8)) > battle_config.mob_max_skilllvl )
  16724. skill_lv = battle_config.mob_max_skilllvl;
  16725. casttime = script_getnum(st,9);
  16726. cancel = script_getnum(st,10);
  16727. emotion = script_getnum(st,11);
  16728. target = script_getnum(st,12);
  16729. map_foreachinrange(buildin_mobuseskill_sub, &center, range, BL_MOB, mobid, skill_id, skill_lv, casttime, cancel, emotion, target);
  16730. return SCRIPT_CMD_SUCCESS;
  16731. }
  16732. BUILDIN_FUNC(progressbar)
  16733. {
  16734. struct map_session_data * sd = script_rid2sd(st);
  16735. const char * color;
  16736. unsigned int second;
  16737. if( !st || !sd )
  16738. return SCRIPT_CMD_SUCCESS;
  16739. st->state = STOP;
  16740. color = script_getstr(st,2);
  16741. second = script_getnum(st,3);
  16742. sd->progressbar.npc_id = st->oid;
  16743. sd->progressbar.timeout = gettick() + second*1000;
  16744. clif_progressbar(sd, strtol(color, (char **)NULL, 0), second);
  16745. return SCRIPT_CMD_SUCCESS;
  16746. }
  16747. BUILDIN_FUNC(pushpc)
  16748. {
  16749. uint8 dir;
  16750. int cells, dx, dy;
  16751. struct map_session_data* sd;
  16752. if((sd = script_rid2sd(st))==NULL)
  16753. {
  16754. return SCRIPT_CMD_SUCCESS;
  16755. }
  16756. dir = script_getnum(st,2);
  16757. cells = script_getnum(st,3);
  16758. if(dir>7)
  16759. {
  16760. ShowWarning("buildin_pushpc: Invalid direction %d specified.\n", dir);
  16761. script_reportsrc(st);
  16762. dir%= 8; // trim spin-over
  16763. }
  16764. if(!cells)
  16765. {// zero distance
  16766. return SCRIPT_CMD_SUCCESS;
  16767. }
  16768. else if(cells<0)
  16769. {// pushing backwards
  16770. dir = (dir+4)%8; // turn around
  16771. cells = -cells;
  16772. }
  16773. dx = dirx[dir];
  16774. dy = diry[dir];
  16775. unit_blown(&sd->bl, dx, dy, cells, 0);
  16776. return SCRIPT_CMD_SUCCESS;
  16777. }
  16778. /// Invokes buying store preparation window
  16779. /// buyingstore <slots>;
  16780. BUILDIN_FUNC(buyingstore)
  16781. {
  16782. struct map_session_data* sd;
  16783. if( ( sd = script_rid2sd(st) ) == NULL )
  16784. {
  16785. return SCRIPT_CMD_SUCCESS;
  16786. }
  16787. if( npc_isnear(&sd->bl) ) {
  16788. char output[150];
  16789. sprintf(output, msg_txt(sd,662), battle_config.min_npc_vendchat_distance);
  16790. clif_displaymessage(sd->fd, output);
  16791. return SCRIPT_CMD_SUCCESS;
  16792. }
  16793. buyingstore_setup(sd, script_getnum(st,2));
  16794. return SCRIPT_CMD_SUCCESS;
  16795. }
  16796. /// Invokes search store info window
  16797. /// searchstores <uses>,<effect>;
  16798. BUILDIN_FUNC(searchstores)
  16799. {
  16800. unsigned short effect;
  16801. unsigned int uses;
  16802. struct map_session_data* sd;
  16803. if( ( sd = script_rid2sd(st) ) == NULL )
  16804. {
  16805. return SCRIPT_CMD_SUCCESS;
  16806. }
  16807. uses = script_getnum(st,2);
  16808. effect = script_getnum(st,3);
  16809. if( !uses )
  16810. {
  16811. ShowError("buildin_searchstores: Amount of uses cannot be zero.\n");
  16812. return SCRIPT_CMD_FAILURE;
  16813. }
  16814. if( effect > 1 )
  16815. {
  16816. ShowError("buildin_searchstores: Invalid effect id %hu, specified.\n", effect);
  16817. return SCRIPT_CMD_FAILURE;
  16818. }
  16819. searchstore_open(sd, uses, effect);
  16820. return SCRIPT_CMD_SUCCESS;
  16821. }
  16822. /// Displays a number as large digital clock.
  16823. /// showdigit <value>[,<type>];
  16824. BUILDIN_FUNC(showdigit)
  16825. {
  16826. unsigned int type = 0;
  16827. int value;
  16828. struct map_session_data* sd;
  16829. if( ( sd = script_rid2sd(st) ) == NULL )
  16830. {
  16831. return SCRIPT_CMD_SUCCESS;
  16832. }
  16833. value = script_getnum(st,2);
  16834. if( script_hasdata(st,3) )
  16835. {
  16836. type = script_getnum(st,3);
  16837. if( type > 3 )
  16838. {
  16839. ShowError("buildin_showdigit: Invalid type %u.\n", type);
  16840. return SCRIPT_CMD_FAILURE;
  16841. }
  16842. }
  16843. clif_showdigit(sd, (unsigned char)type, value);
  16844. return SCRIPT_CMD_SUCCESS;
  16845. }
  16846. /**
  16847. * Rune Knight
  16848. * makerune <% success bonus>{,<char_id>};
  16849. **/
  16850. BUILDIN_FUNC(makerune) {
  16851. TBL_PC* sd;
  16852. if (!script_charid2sd(3,sd))
  16853. return SCRIPT_CMD_FAILURE;
  16854. clif_skill_produce_mix_list(sd,RK_RUNEMASTERY,24);
  16855. sd->itemid = script_getnum(st,2);
  16856. return SCRIPT_CMD_SUCCESS;
  16857. }
  16858. /**
  16859. * checkdragon({<char_id>}) returns 1 if mounting a dragon or 0 otherwise.
  16860. **/
  16861. BUILDIN_FUNC(checkdragon) {
  16862. TBL_PC* sd;
  16863. if (!script_charid2sd(2,sd))
  16864. return SCRIPT_CMD_FAILURE;
  16865. if( pc_isridingdragon(sd) )
  16866. script_pushint(st,1);
  16867. else
  16868. script_pushint(st,0);
  16869. return SCRIPT_CMD_SUCCESS;
  16870. }
  16871. /**
  16872. * setdragon({optional Color{,<char_id>}}) returns 1 on success or 0 otherwise
  16873. * - Toggles the dragon on a RK if he can mount;
  16874. * @param Color - when not provided uses the green dragon;
  16875. * - 1 : Green Dragon
  16876. * - 2 : Brown Dragon
  16877. * - 3 : Gray Dragon
  16878. * - 4 : Blue Dragon
  16879. * - 5 : Red Dragon
  16880. **/
  16881. BUILDIN_FUNC(setdragon) {
  16882. TBL_PC* sd;
  16883. int color = script_hasdata(st,2) ? script_getnum(st,2) : 0;
  16884. if (!script_charid2sd(3,sd))
  16885. return SCRIPT_CMD_FAILURE;
  16886. if( !pc_checkskill(sd,RK_DRAGONTRAINING) || (sd->class_&MAPID_THIRDMASK) != MAPID_RUNE_KNIGHT )
  16887. script_pushint(st,0);//Doesn't have the skill or it's not a Rune Knight
  16888. else if ( pc_isridingdragon(sd) ) {//Is mounted; release
  16889. pc_setoption(sd, sd->sc.option&~OPTION_DRAGON);
  16890. script_pushint(st,1);
  16891. } else {//Not mounted; Mount now.
  16892. unsigned int option = OPTION_DRAGON1;
  16893. if( color ) {
  16894. option = ( color == 1 ? OPTION_DRAGON1 :
  16895. color == 2 ? OPTION_DRAGON2 :
  16896. color == 3 ? OPTION_DRAGON3 :
  16897. color == 4 ? OPTION_DRAGON4 :
  16898. color == 5 ? OPTION_DRAGON5 : 0);
  16899. if( !option ) {
  16900. ShowWarning("script_setdragon: Unknown Color %d used; changing to green (1)\n",color);
  16901. option = OPTION_DRAGON1;
  16902. }
  16903. }
  16904. pc_setoption(sd, sd->sc.option|option);
  16905. script_pushint(st,1);
  16906. }
  16907. return SCRIPT_CMD_SUCCESS;
  16908. }
  16909. /**
  16910. * ismounting({<char_id>}) returns 1 if mounting a new mount or 0 otherwise
  16911. **/
  16912. BUILDIN_FUNC(ismounting) {
  16913. TBL_PC* sd;
  16914. if (!script_charid2sd(2,sd))
  16915. return SCRIPT_CMD_FAILURE;
  16916. if( &sd->sc && sd->sc.data[SC_ALL_RIDING] )
  16917. script_pushint(st,1);
  16918. else
  16919. script_pushint(st,0);
  16920. return SCRIPT_CMD_SUCCESS;
  16921. }
  16922. /**
  16923. * setmounting({<char_id>}) returns 1 on success or 0 otherwise
  16924. * - Toggles new mounts on a player when he can mount
  16925. * - Will fail if the player is mounting a non-new mount, e.g. dragon, peco, wug, etc.
  16926. * - Will unmount the player is he is already mounting
  16927. **/
  16928. BUILDIN_FUNC(setmounting) {
  16929. TBL_PC* sd;
  16930. if (!script_charid2sd(2,sd))
  16931. return SCRIPT_CMD_FAILURE;
  16932. if( &sd->sc && sd->sc.option&(OPTION_WUGRIDER|OPTION_RIDING|OPTION_DRAGON|OPTION_MADOGEAR) ) {
  16933. clif_msg(sd, NEED_REINS_OF_MOUNT);
  16934. script_pushint(st,0); //can't mount with one of these
  16935. } else {
  16936. if( &sd->sc && sd->sc.data[SC_ALL_RIDING] )
  16937. status_change_end(&sd->bl, SC_ALL_RIDING, INVALID_TIMER); //release mount
  16938. else
  16939. sc_start(NULL, &sd->bl, SC_ALL_RIDING, 10000, 1, INVALID_TIMER); //mount
  16940. script_pushint(st,1);//in both cases, return 1.
  16941. }
  16942. return SCRIPT_CMD_SUCCESS;
  16943. }
  16944. /**
  16945. * Retrieves quantity of arguments provided to callfunc/callsub.
  16946. * getargcount() -> amount of arguments received in a function
  16947. **/
  16948. BUILDIN_FUNC(getargcount) {
  16949. struct script_retinfo* ri;
  16950. if( st->stack->defsp < 1 || st->stack->stack_data[st->stack->defsp - 1].type != C_RETINFO ) {
  16951. ShowError("script:getargcount: used out of function or callsub label!\n");
  16952. st->state = END;
  16953. return SCRIPT_CMD_FAILURE;
  16954. }
  16955. ri = st->stack->stack_data[st->stack->defsp - 1].u.ri;
  16956. script_pushint(st, ri->nargs);
  16957. return SCRIPT_CMD_SUCCESS;
  16958. }
  16959. /**
  16960. * getcharip(<account ID>/<character ID>/<character name>)
  16961. **/
  16962. BUILDIN_FUNC(getcharip)
  16963. {
  16964. struct map_session_data* sd = NULL;
  16965. /* check if a character name is specified */
  16966. if( script_hasdata(st, 2) )
  16967. {
  16968. struct script_data *data;
  16969. data = script_getdata(st, 2);
  16970. get_val(st, data); // Convert into value in case of a variable
  16971. if (data_isstring(data))
  16972. sd = map_nick2sd(script_getstr(st, 2));
  16973. else if (data_isint(data) || script_getnum(st, 2))
  16974. {
  16975. int id = 0;
  16976. id = script_getnum(st, 2);
  16977. sd = (map_id2sd(id) ? map_id2sd(id) : map_charid2sd(id));
  16978. }
  16979. }
  16980. else
  16981. sd = script_rid2sd(st);
  16982. /* check for sd and IP */
  16983. if (!sd || !session[sd->fd]->client_addr)
  16984. {
  16985. script_pushconststr(st, "");
  16986. return SCRIPT_CMD_SUCCESS;
  16987. }
  16988. /* return the client ip_addr converted for output */
  16989. if (sd && sd->fd && session[sd->fd])
  16990. {
  16991. /* initiliaze */
  16992. const char *ip_addr = NULL;
  16993. uint32 ip;
  16994. /* set ip, ip_addr and convert to ip and push str */
  16995. ip = session[sd->fd]->client_addr;
  16996. ip_addr = ip2str(ip, NULL);
  16997. script_pushstrcopy(st, ip_addr);
  16998. }
  16999. return SCRIPT_CMD_SUCCESS;
  17000. }
  17001. /**
  17002. * is_function(<function name>) -> 1 if function exists, 0 otherwise
  17003. **/
  17004. BUILDIN_FUNC(is_function) {
  17005. const char* str = script_getstr(st,2);
  17006. if( strdb_exists(userfunc_db, str) )
  17007. script_pushint(st,1);
  17008. else
  17009. script_pushint(st,0);
  17010. return SCRIPT_CMD_SUCCESS;
  17011. }
  17012. /**
  17013. * get_revision() -> retrieves the current svn revision (if available)
  17014. **/
  17015. BUILDIN_FUNC(get_revision) {
  17016. const char *svn = get_svn_revision();
  17017. if ( svn[0] != UNKNOWN_VERSION )
  17018. script_pushint(st,atoi(svn));
  17019. else
  17020. script_pushint(st,-1); //unknown
  17021. return SCRIPT_CMD_SUCCESS;
  17022. }
  17023. /* get_hash() -> retrieves the current git hash (if available)*/
  17024. BUILDIN_FUNC(get_githash) {
  17025. const char* git = get_git_hash();
  17026. char buf[CHAT_SIZE_MAX];
  17027. safestrncpy(buf,git,strlen(git)+1);
  17028. if ( git[0] != UNKNOWN_VERSION )
  17029. script_pushstr(st,buf);
  17030. else
  17031. script_pushstr(st,"Unknown"); //unknown
  17032. return SCRIPT_CMD_SUCCESS;
  17033. }
  17034. /**
  17035. * freeloop(<toggle>) -> toggles this script instance's looping-check ability
  17036. **/
  17037. BUILDIN_FUNC(freeloop) {
  17038. if( script_hasdata(st,2) ) {
  17039. if( script_getnum(st,2) )
  17040. st->freeloop = 1;
  17041. else
  17042. st->freeloop = 0;
  17043. }
  17044. script_pushint(st, st->freeloop);
  17045. return SCRIPT_CMD_SUCCESS;
  17046. }
  17047. /**
  17048. * @commands (script based)
  17049. **/
  17050. BUILDIN_FUNC(bindatcmd) {
  17051. const char* atcmd;
  17052. const char* eventName;
  17053. int i, level = 0, level2 = 0;
  17054. bool create = false;
  17055. atcmd = script_getstr(st,2);
  17056. eventName = script_getstr(st,3);
  17057. if( *atcmd == atcommand_symbol || *atcmd == charcommand_symbol )
  17058. atcmd++;
  17059. if( script_hasdata(st,4) ) level = script_getnum(st,4);
  17060. if( script_hasdata(st,5) ) level2 = script_getnum(st,5);
  17061. if( atcmd_binding_count == 0 ) {
  17062. CREATE(atcmd_binding,struct atcmd_binding_data*,1);
  17063. create = true;
  17064. } else {
  17065. ARR_FIND(0, atcmd_binding_count, i, strcmp(atcmd_binding[i]->command,atcmd) == 0);
  17066. if( i < atcmd_binding_count ) {/* update existent entry */
  17067. safestrncpy(atcmd_binding[i]->npc_event, eventName, 50);
  17068. atcmd_binding[i]->level = level;
  17069. atcmd_binding[i]->level2 = level2;
  17070. } else
  17071. create = true;
  17072. }
  17073. if( create ) {
  17074. i = atcmd_binding_count;
  17075. if( atcmd_binding_count++ != 0 )
  17076. RECREATE(atcmd_binding,struct atcmd_binding_data*,atcmd_binding_count);
  17077. CREATE(atcmd_binding[i],struct atcmd_binding_data,1);
  17078. safestrncpy(atcmd_binding[i]->command, atcmd, 50);
  17079. safestrncpy(atcmd_binding[i]->npc_event, eventName, 50);
  17080. atcmd_binding[i]->level = level;
  17081. atcmd_binding[i]->level2 = level2;
  17082. }
  17083. return SCRIPT_CMD_SUCCESS;
  17084. }
  17085. BUILDIN_FUNC(unbindatcmd) {
  17086. const char* atcmd;
  17087. int i = 0;
  17088. atcmd = script_getstr(st, 2);
  17089. if( *atcmd == atcommand_symbol || *atcmd == charcommand_symbol )
  17090. atcmd++;
  17091. if( atcmd_binding_count == 0 ) {
  17092. script_pushint(st, 0);
  17093. return SCRIPT_CMD_SUCCESS;
  17094. }
  17095. ARR_FIND(0, atcmd_binding_count, i, strcmp(atcmd_binding[i]->command, atcmd) == 0);
  17096. if( i < atcmd_binding_count ) {
  17097. int cursor = 0;
  17098. aFree(atcmd_binding[i]);
  17099. atcmd_binding[i] = NULL;
  17100. /* compact the list now that we freed a slot somewhere */
  17101. for( i = 0, cursor = 0; i < atcmd_binding_count; i++ ) {
  17102. if( atcmd_binding[i] == NULL )
  17103. continue;
  17104. if( cursor != i ) {
  17105. memmove(&atcmd_binding[cursor], &atcmd_binding[i], sizeof(struct atcmd_binding_data*));
  17106. }
  17107. cursor++;
  17108. }
  17109. if( (atcmd_binding_count = cursor) == 0 )
  17110. aFree(atcmd_binding);
  17111. script_pushint(st, 1);
  17112. } else
  17113. script_pushint(st, 0);/* not found */
  17114. return SCRIPT_CMD_SUCCESS;
  17115. }
  17116. BUILDIN_FUNC(useatcmd) {
  17117. return atcommand_sub(st,3);
  17118. }
  17119. BUILDIN_FUNC(checkre)
  17120. {
  17121. int num;
  17122. num=script_getnum(st,2);
  17123. switch(num){
  17124. case 0:
  17125. #ifdef RENEWAL
  17126. script_pushint(st, 1);
  17127. #else
  17128. script_pushint(st, 0);
  17129. #endif
  17130. break;
  17131. case 1:
  17132. #ifdef RENEWAL_CAST
  17133. script_pushint(st, 1);
  17134. #else
  17135. script_pushint(st, 0);
  17136. #endif
  17137. break;
  17138. case 2:
  17139. #ifdef RENEWAL_DROP
  17140. script_pushint(st, 1);
  17141. #else
  17142. script_pushint(st, 0);
  17143. #endif
  17144. break;
  17145. case 3:
  17146. #ifdef RENEWAL_EXP
  17147. script_pushint(st, 1);
  17148. #else
  17149. script_pushint(st, 0);
  17150. #endif
  17151. break;
  17152. case 4:
  17153. #ifdef RENEWAL_LVDMG
  17154. script_pushint(st, 1);
  17155. #else
  17156. script_pushint(st, 0);
  17157. #endif
  17158. break;
  17159. case 5:
  17160. #ifdef RENEWAL_ASPD
  17161. script_pushint(st, 1);
  17162. #else
  17163. script_pushint(st, 0);
  17164. #endif
  17165. break;
  17166. default:
  17167. ShowWarning("buildin_checkre: unknown parameter.\n");
  17168. break;
  17169. }
  17170. return SCRIPT_CMD_SUCCESS;
  17171. }
  17172. /* getrandgroupitem <group_id>{,<quantity>{,<sub_group>}} */
  17173. BUILDIN_FUNC(getrandgroupitem) {
  17174. TBL_PC* sd;
  17175. int i, get_count = 0;
  17176. uint16 group = script_getnum(st,2), qty = 0;
  17177. uint8 sub_group = 1;
  17178. struct item item_tmp;
  17179. struct s_item_group_entry *entry = NULL;
  17180. if (!(sd = script_rid2sd(st)))
  17181. return SCRIPT_CMD_SUCCESS;
  17182. if (!group) {
  17183. ShowError("buildin_getrandgroupitem: Invalid group id (%d)!\n",script_getnum(st,2));
  17184. return SCRIPT_CMD_FAILURE;
  17185. }
  17186. FETCH(3, qty);
  17187. FETCH(4, sub_group);
  17188. entry = itemdb_get_randgroupitem(group,sub_group);
  17189. if (!entry)
  17190. return SCRIPT_CMD_FAILURE; //ensure valid itemid
  17191. memset(&item_tmp,0,sizeof(item_tmp));
  17192. item_tmp.nameid = entry->nameid;
  17193. item_tmp.identify = itemdb_isidentified(entry->nameid);
  17194. if (!qty)
  17195. qty = entry->amount;
  17196. //Check if it's stackable.
  17197. if (!itemdb_isstackable(entry->nameid)) {
  17198. item_tmp.amount = 1;
  17199. get_count = qty;
  17200. }
  17201. else {
  17202. item_tmp.amount = qty;
  17203. get_count = 1;
  17204. }
  17205. for (i = 0; i < get_count; i++) {
  17206. // if not pet egg
  17207. if (!pet_create_egg(sd, entry->nameid)) {
  17208. unsigned char flag = 0;
  17209. if ((flag = pc_additem(sd,&item_tmp,item_tmp.amount,LOG_TYPE_SCRIPT))) {
  17210. clif_additem(sd,0,0,flag);
  17211. if (pc_candrop(sd,&item_tmp))
  17212. map_addflooritem(&item_tmp,item_tmp.amount,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0,0);
  17213. }
  17214. }
  17215. }
  17216. return SCRIPT_CMD_SUCCESS;
  17217. }
  17218. /* getgroupitem <group_id>{,<char_id>};
  17219. * Gives item(s) to the attached player based on item group contents
  17220. */
  17221. BUILDIN_FUNC(getgroupitem) {
  17222. TBL_PC *sd;
  17223. int group_id = script_getnum(st,2);
  17224. if (!script_charid2sd(3,sd))
  17225. return SCRIPT_CMD_SUCCESS;
  17226. if (itemdb_pc_get_itemgroup(group_id,sd)) {
  17227. ShowError("buildin_getgroupitem: Invalid group id '%d' specified.\n",group_id);
  17228. return SCRIPT_CMD_FAILURE;
  17229. }
  17230. return SCRIPT_CMD_SUCCESS;
  17231. }
  17232. /* cleanmap <map_name>;
  17233. * cleanarea <map_name>, <x0>, <y0>, <x1>, <y1>; */
  17234. static int atcommand_cleanfloor_sub(struct block_list *bl, va_list ap)
  17235. {
  17236. nullpo_ret(bl);
  17237. map_clearflooritem(bl);
  17238. return SCRIPT_CMD_SUCCESS;
  17239. }
  17240. BUILDIN_FUNC(cleanmap)
  17241. {
  17242. const char *mapname;
  17243. int16 m;
  17244. mapname = script_getstr(st, 2);
  17245. m = map_mapname2mapid(mapname);
  17246. if (!m)
  17247. return SCRIPT_CMD_FAILURE;
  17248. if ((script_lastdata(st) - 2) < 4) {
  17249. map_foreachinmap(atcommand_cleanfloor_sub, m, BL_ITEM);
  17250. } else {
  17251. int16 x0 = script_getnum(st, 3);
  17252. int16 y0 = script_getnum(st, 4);
  17253. int16 x1 = script_getnum(st, 5);
  17254. int16 y1 = script_getnum(st, 6);
  17255. if (x0 > 0 && y0 > 0 && x1 > 0 && y1 > 0) {
  17256. map_foreachinarea(atcommand_cleanfloor_sub, m, x0, y0, x1, y1, BL_ITEM);
  17257. } else {
  17258. ShowError("cleanarea: invalid coordinate defined!\n");
  17259. return SCRIPT_CMD_FAILURE;
  17260. }
  17261. }
  17262. return SCRIPT_CMD_SUCCESS;
  17263. }
  17264. /* Cast a skill on the attached player.
  17265. * npcskill <skill id>, <skill lvl>, <stat point>, <NPC level>;
  17266. * npcskill "<skill name>", <skill lvl>, <stat point>, <NPC level>; */
  17267. BUILDIN_FUNC(npcskill)
  17268. {
  17269. uint16 skill_id;
  17270. unsigned short skill_level;
  17271. unsigned int stat_point;
  17272. unsigned int npc_level;
  17273. struct npc_data *nd;
  17274. struct map_session_data *sd;
  17275. struct script_data *data;
  17276. data = script_getdata(st, 2);
  17277. get_val(st, data); // Convert into value in case of a variable
  17278. skill_id = data_isstring(data) ? skill_name2id(script_getstr(st, 2)) : script_getnum(st, 2);
  17279. skill_level = script_getnum(st, 3);
  17280. stat_point = script_getnum(st, 4);
  17281. npc_level = script_getnum(st, 5);
  17282. sd = script_rid2sd(st);
  17283. nd = (struct npc_data *)map_id2bl(sd->npc_id);
  17284. if (stat_point > battle_config.max_third_parameter) {
  17285. ShowError("npcskill: stat point exceeded maximum of %d.\n",battle_config.max_third_parameter );
  17286. return SCRIPT_CMD_FAILURE;
  17287. }
  17288. if (npc_level > MAX_LEVEL) {
  17289. ShowError("npcskill: level exceeded maximum of %d.\n", MAX_LEVEL);
  17290. return SCRIPT_CMD_FAILURE;
  17291. }
  17292. if (sd == NULL || nd == NULL) { //ain't possible, but I don't trust people.
  17293. return SCRIPT_CMD_FAILURE;
  17294. }
  17295. nd->level = npc_level;
  17296. nd->stat_point = stat_point;
  17297. if (!nd->status.hp)
  17298. status_calc_npc(nd, SCO_FIRST);
  17299. else
  17300. status_calc_npc(nd, SCO_NONE);
  17301. if (skill_get_inf(skill_id)&INF_GROUND_SKILL)
  17302. unit_skilluse_pos(&nd->bl, sd->bl.x, sd->bl.y, skill_id, skill_level);
  17303. else
  17304. unit_skilluse_id(&nd->bl, sd->bl.id, skill_id, skill_level);
  17305. return SCRIPT_CMD_SUCCESS;
  17306. }
  17307. /* Consumes an item.
  17308. * consumeitem <item id>{,<char_id>};
  17309. * consumeitem "<item name>"{,<char_id>};
  17310. * @param item: Item ID or name
  17311. */
  17312. BUILDIN_FUNC(consumeitem)
  17313. {
  17314. TBL_PC *sd;
  17315. struct script_data *data;
  17316. struct item_data *item_data;
  17317. if (!script_charid2sd(3, sd))
  17318. return SCRIPT_CMD_FAILURE;
  17319. data = script_getdata( st, 2 );
  17320. get_val( st, data );
  17321. if( data_isstring( data ) ){
  17322. const char *name = conv_str( st, data );
  17323. if( ( item_data = itemdb_searchname( name ) ) == NULL ){
  17324. ShowError( "buildin_consumeitem: Nonexistant item %s requested.\n", name );
  17325. return SCRIPT_CMD_FAILURE;
  17326. }
  17327. }else if( data_isint( data ) ){
  17328. unsigned short nameid = conv_num( st, data );
  17329. if( ( item_data = itemdb_exists( nameid ) ) == NULL ){
  17330. ShowError("buildin_consumeitem: Nonexistant item %hu requested.\n", nameid );
  17331. return SCRIPT_CMD_FAILURE;
  17332. }
  17333. }else{
  17334. ShowError("buildin_consumeitem: invalid data type for argument #1 (%d).\n", data->type );
  17335. return SCRIPT_CMD_FAILURE;
  17336. }
  17337. run_script( item_data->script, 0, sd->bl.id, 0 );
  17338. return SCRIPT_CMD_SUCCESS;
  17339. }
  17340. /** Makes a player sit/stand.
  17341. * sit {"<character name>"};
  17342. * stand {"<character name>"};
  17343. * Note: Use readparam(Sitting) which returns 1 or 0 (sitting or standing).
  17344. * @param name: Player name that will be invoked
  17345. */
  17346. BUILDIN_FUNC(sit)
  17347. {
  17348. TBL_PC *sd;
  17349. if( script_hasdata(st, 2) )
  17350. sd = map_nick2sd(script_getstr(st, 2));
  17351. else
  17352. sd = script_rid2sd(st);
  17353. if( sd == NULL)
  17354. return SCRIPT_CMD_FAILURE;
  17355. if( !pc_issit(sd) ) {
  17356. pc_setsit(sd);
  17357. skill_sit(sd, 1);
  17358. clif_sitting(&sd->bl);
  17359. }
  17360. return SCRIPT_CMD_SUCCESS;
  17361. }
  17362. /** Makes player to stand
  17363. * @param name: Player name that will be set
  17364. */
  17365. BUILDIN_FUNC(stand)
  17366. {
  17367. TBL_PC *sd;
  17368. if( script_hasdata(st, 2) )
  17369. sd = map_nick2sd(script_getstr(st, 2));
  17370. else
  17371. sd = script_rid2sd(st);
  17372. if( sd == NULL)
  17373. return SCRIPT_CMD_FAILURE;
  17374. if( pc_issit(sd) && pc_setstand(sd, false)) {
  17375. skill_sit(sd, 0);
  17376. clif_standing(&sd->bl);
  17377. }
  17378. return SCRIPT_CMD_SUCCESS;
  17379. }
  17380. /** Creates an array of bounded item IDs
  17381. * countbound {<type>{,<char_id>}};
  17382. * @param type: 0 - All bound items; 1 - Account Bound; 2 - Guild Bound; 3 - Party Bound
  17383. * @return amt: Amount of items found
  17384. */
  17385. BUILDIN_FUNC(countbound)
  17386. {
  17387. int i, type, j = 0, k = 0;
  17388. TBL_PC *sd;
  17389. if (!script_charid2sd(3,sd))
  17390. return SCRIPT_CMD_FAILURE;
  17391. type = script_getnum(st,2);
  17392. for( i = 0; i < MAX_INVENTORY; i ++ ) {
  17393. if( sd->status.inventory[i].nameid > 0 && (
  17394. (!type && sd->status.inventory[i].bound) || (type && sd->status.inventory[i].bound == type)
  17395. ))
  17396. {
  17397. pc_setreg(sd,reference_uid(add_str("@bound_items"), k),sd->status.inventory[i].nameid);
  17398. k++;
  17399. j += sd->status.inventory[i].amount;
  17400. }
  17401. }
  17402. script_pushint(st,j);
  17403. return SCRIPT_CMD_SUCCESS;
  17404. }
  17405. /** Creates new party
  17406. * party_create "<party name>"{,<char id>{,<item share>{,<item share type>}}};
  17407. * @param party_name: String of party name that will be created
  17408. * @param char_id: Chara that will be leader of this new party. If no char_id specified, the invoker will be party leader
  17409. * @param item_share: 0-Each Take. 1-Party Share
  17410. * @param item_share_type: 0-Each Take. 1-Even Share
  17411. * @return val: Result value
  17412. * -3 - party name is exist
  17413. * -2 - player is in party already
  17414. * -1 - player is not found
  17415. * 0 - unknown error
  17416. * 1 - success, will return party id $@party_create_id
  17417. */
  17418. BUILDIN_FUNC(party_create)
  17419. {
  17420. char party_name[NAME_LENGTH];
  17421. int item1 = 0, item2 = 0;
  17422. TBL_PC *sd;
  17423. if( (!script_hasdata(st,3) && !(sd = script_rid2sd(st))) || (script_hasdata(st,3) && !(sd = map_charid2sd(script_getnum(st,3)))) ) {
  17424. script_pushint(st,-1);
  17425. return SCRIPT_CMD_FAILURE;
  17426. }
  17427. if( sd->status.party_id ) {
  17428. script_pushint(st,-2);
  17429. return SCRIPT_CMD_FAILURE;
  17430. }
  17431. safestrncpy(party_name,script_getstr(st,2),NAME_LENGTH);
  17432. trim(party_name);
  17433. if( party_searchname(party_name) ) {
  17434. script_pushint(st,-3);
  17435. return SCRIPT_CMD_FAILURE;
  17436. }
  17437. if( script_getnum(st,4) )
  17438. item1 = 1;
  17439. if( script_getnum(st,5) )
  17440. item2 = 1;
  17441. party_create_byscript = 1;
  17442. script_pushint(st,party_create(sd,party_name,item1,item2));
  17443. return SCRIPT_CMD_SUCCESS;
  17444. }
  17445. /** Adds player to specified party
  17446. * party_addmember <party id>,<char id>;
  17447. * @param party_id: The party that will be entered by player
  17448. * @param char_id: Char id of player that will be joined to the party
  17449. * @return val: Result value
  17450. * -4 - party is full
  17451. * -3 - party is not found
  17452. * -2 - player is in party already
  17453. * -1 - player is not found
  17454. * 0 - unknown error
  17455. * 1 - success
  17456. */
  17457. BUILDIN_FUNC(party_addmember)
  17458. {
  17459. int party_id = script_getnum(st,2);
  17460. TBL_PC *sd;
  17461. struct party_data *party;
  17462. if( !(sd = map_charid2sd(script_getnum(st,3))) ) {
  17463. script_pushint(st,-1);
  17464. return SCRIPT_CMD_FAILURE;
  17465. }
  17466. if( sd->status.party_id ) {
  17467. script_pushint(st,-2);
  17468. return SCRIPT_CMD_FAILURE;
  17469. }
  17470. if( !(party = party_search(party_id)) ) {
  17471. script_pushint(st,-3);
  17472. return SCRIPT_CMD_FAILURE;
  17473. }
  17474. if( party->party.count >= MAX_PARTY ) {
  17475. script_pushint(st,-4);
  17476. return SCRIPT_CMD_FAILURE;
  17477. }
  17478. sd->party_invite = party_id;
  17479. script_pushint(st,party_add_member(party_id,sd));
  17480. return SCRIPT_CMD_SUCCESS;
  17481. }
  17482. /** Removes player from his/her party. If party_id and char_id is empty remove the invoker from his/her party
  17483. * party_delmember {<char id>,<party_id>};
  17484. * @param: char_id
  17485. * @param: party_id
  17486. * @return val: Result value
  17487. * -3 - player is not in party
  17488. * -2 - party is not found
  17489. * -1 - player is not found
  17490. * 0 - unknown error
  17491. * 1 - success
  17492. */
  17493. BUILDIN_FUNC(party_delmember)
  17494. {
  17495. TBL_PC *sd = NULL;
  17496. if( !script_hasdata(st,2) && !script_hasdata(st,3) && !(sd = script_rid2sd(st)) ) {
  17497. script_pushint(st,-1);
  17498. return SCRIPT_CMD_FAILURE;
  17499. }
  17500. if( sd || (script_getnum(st,2) && (sd = map_charid2sd(script_getnum(st,2)))) )
  17501. script_pushint(st,party_removemember2(sd,0,0));
  17502. else
  17503. script_pushint(st,party_removemember2(NULL,script_getnum(st,2),script_getnum(st,3)));
  17504. return SCRIPT_CMD_SUCCESS;
  17505. }
  17506. /** Changes party leader of specified party (even the leader is offline)
  17507. * party_changeleader <party id>,<char id>;
  17508. * @param party_id: ID of party
  17509. * @param char_id: Char ID of new leader
  17510. * @return val: Result value
  17511. * -4 - player is party leader already
  17512. * -3 - player is not in this party
  17513. * -2 - player is not found
  17514. * -1 - party is not found
  17515. * 0 - unknown error
  17516. * 1 - success */
  17517. BUILDIN_FUNC(party_changeleader)
  17518. {
  17519. int i, party_id = script_getnum(st,2);
  17520. TBL_PC *sd = NULL;
  17521. TBL_PC *tsd = NULL;
  17522. struct party_data *party = NULL;
  17523. if( !(party = party_search(party_id)) ) {
  17524. script_pushint(st,-1);
  17525. return SCRIPT_CMD_FAILURE;
  17526. }
  17527. if( !(tsd = map_charid2sd(script_getnum(st,3))) ) {
  17528. script_pushint(st,-2);
  17529. return SCRIPT_CMD_FAILURE;
  17530. }
  17531. if( tsd->status.party_id != party_id ) {
  17532. script_pushint(st,-3);
  17533. return SCRIPT_CMD_FAILURE;
  17534. }
  17535. ARR_FIND(0,MAX_PARTY,i,party->party.member[i].leader);
  17536. if( i >= MAX_PARTY ) { //this is should impossible!
  17537. script_pushint(st,0);
  17538. return SCRIPT_CMD_FAILURE;
  17539. }
  17540. if( party->data[i].sd == tsd ) {
  17541. script_pushint(st,-4);
  17542. return SCRIPT_CMD_FAILURE;
  17543. }
  17544. script_pushint(st,party_changeleader(sd,tsd,party));
  17545. return SCRIPT_CMD_SUCCESS;
  17546. }
  17547. /** Changes party option
  17548. * party_changeoption <party id>,<option>,<flag>;
  17549. * @param party_id: ID of party that will be changed
  17550. * @param option: Type of option
  17551. * @return val: -1 - party is not found, 0 - invalid option, 1 - success
  17552. */
  17553. BUILDIN_FUNC(party_changeoption)
  17554. {
  17555. struct party_data *party;
  17556. if( !(party = party_search(script_getnum(st,2))) ) {
  17557. script_pushint(st,-1);
  17558. return SCRIPT_CMD_FAILURE;
  17559. }
  17560. script_pushint(st,party_setoption(party,script_getnum(st,3),script_getnum(st,4)));
  17561. return SCRIPT_CMD_SUCCESS;
  17562. }
  17563. /** Destroys party with party id.
  17564. * party_destroy <party id>;
  17565. * @param party_id: ID of party that will be destroyed
  17566. */
  17567. BUILDIN_FUNC(party_destroy)
  17568. {
  17569. int i;
  17570. struct party_data *party;
  17571. if( !(party = party_search(script_getnum(st,2))) ) {
  17572. script_pushint(st,0);
  17573. return SCRIPT_CMD_FAILURE;
  17574. }
  17575. ARR_FIND(0,MAX_PARTY,i,party->party.member[i].leader);
  17576. if( i >= MAX_PARTY || !party->data[i].sd ) { //leader not online
  17577. int j;
  17578. for( j = 0; j < MAX_PARTY; j++ ) {
  17579. TBL_PC *sd = party->data[j].sd;
  17580. if(sd)
  17581. party_member_withdraw(party->party.party_id,sd->status.account_id,sd->status.char_id);
  17582. else if( party->party.member[j].char_id )
  17583. intif_party_leave(party->party.party_id,party->party.member[j].account_id,party->party.member[j].char_id);
  17584. }
  17585. party_broken(party->party.party_id);
  17586. script_pushint(st,1);
  17587. }
  17588. else //leader leave = party broken
  17589. script_pushint(st,party_leave(party->data[i].sd));
  17590. return SCRIPT_CMD_SUCCESS;
  17591. }
  17592. /** Checks if a player's client version meets a required version or date.
  17593. * @param type: 0 - check by version number; 1 - check by date
  17594. * @param data: Input
  17595. */
  17596. BUILDIN_FUNC(is_clientver) {
  17597. TBL_PC *sd = NULL;
  17598. int type = script_getnum(st,2);
  17599. int data = script_getnum(st,3);
  17600. int ret = 0;
  17601. if (script_hasdata(st,4))
  17602. sd = map_charid2sd(script_getnum(st,4));
  17603. else
  17604. sd = script_rid2sd(st);
  17605. if (sd == NULL) {
  17606. script_pushint(st,0);
  17607. return SCRIPT_CMD_FAILURE;
  17608. }
  17609. switch(type){
  17610. case 0:
  17611. ret = (sd->packet_ver >= data)?1:0;
  17612. break;
  17613. case 1:
  17614. ret = (sd->packet_ver >= date2version(data))?1:0;
  17615. break;
  17616. }
  17617. script_pushint(st,ret);
  17618. return SCRIPT_CMD_SUCCESS;
  17619. }
  17620. /** Returns various information about a player's VIP status. Need to enable VIP system
  17621. * vip_status <type>,{"<character name>"};
  17622. * @param type: Info type, 1: VIP status, 2: Expired date, 3: Remaining time
  17623. * @param name: Character name (Optional)
  17624. */
  17625. BUILDIN_FUNC(vip_status) {
  17626. #ifdef VIP_ENABLE
  17627. TBL_PC *sd;
  17628. char vip_str[26];
  17629. time_t now = time(NULL);
  17630. int type = script_getnum(st, 2);
  17631. if (script_hasdata(st, 3))
  17632. sd = map_nick2sd(script_getstr(st, 3));
  17633. else
  17634. sd = script_rid2sd(st);
  17635. if (sd == NULL)
  17636. return SCRIPT_CMD_FAILURE;
  17637. switch(type) {
  17638. case 1: // Get VIP status.
  17639. script_pushint(st, pc_isvip(sd));
  17640. break;
  17641. case 2: // Get VIP expire date.
  17642. if (pc_isvip(sd)) {
  17643. time_t viptime = sd->vip.time;
  17644. strftime(vip_str, 24, "%Y-%m-%d %H:%M", localtime(&viptime));
  17645. vip_str[25] = '\0';
  17646. script_pushstrcopy(st, vip_str);
  17647. } else
  17648. script_pushint(st, 0);
  17649. break;
  17650. case 3: // Get remaining time.
  17651. if (pc_isvip(sd)) {
  17652. time_t viptime_remain = sd->vip.time - now;
  17653. int year=0,month=0,day=0,hour=0,min=0,sec=0;
  17654. split_time((int)viptime_remain,&year,&month,&day,&hour,&min,&sec);
  17655. safesnprintf(vip_str,sizeof(vip_str),"%d-%d-%d %d:%d",year,month,day,hour,min);
  17656. script_pushstrcopy(st, vip_str);
  17657. } else
  17658. script_pushint(st, 0);
  17659. break;
  17660. }
  17661. #else
  17662. script_pushint(st, 0);
  17663. #endif
  17664. return SCRIPT_CMD_SUCCESS;
  17665. }
  17666. /** Adds or removes VIP time in minutes. Need to enable VIP system
  17667. * vip_time <time in mn>,{"<character name>"};
  17668. * @param time: VIP duration in minutes. If time < 0 remove time, else add time.
  17669. * @param name: Character name (optional)
  17670. */
  17671. BUILDIN_FUNC(vip_time) {
  17672. #ifdef VIP_ENABLE //would be a pain for scripting npc otherwise
  17673. TBL_PC *sd;
  17674. int viptime = script_getnum(st, 2) * 60; // Convert since it's given in minutes.
  17675. if (script_hasdata(st, 3))
  17676. sd = map_nick2sd(script_getstr(st, 3));
  17677. else
  17678. sd = script_rid2sd(st);
  17679. if (sd == NULL)
  17680. return SCRIPT_CMD_FAILURE;
  17681. chrif_req_login_operation(sd->status.account_id, sd->status.name, CHRIF_OP_LOGIN_VIP, viptime, 7, 0);
  17682. #endif
  17683. return SCRIPT_CMD_SUCCESS;
  17684. }
  17685. /** Turns a player into a monster and grants SC attribute effect. [malufett/Hercules]
  17686. * montransform <monster name/ID>, <duration>, <sc type>, <val1>, <val2>, <val3>, <val4>;
  17687. * @param monster: Monster ID or name
  17688. * @param duration: Transform duration in millisecond (ms)
  17689. * @param sc_type: Type of SC that will be affected during the transformation
  17690. * @param val1: Value for SC
  17691. * @param val2: Value for SC
  17692. * @param val3: Value for SC
  17693. * @param val4: Value for SC
  17694. */
  17695. BUILDIN_FUNC(montransform) {
  17696. TBL_PC *sd;
  17697. enum sc_type type;
  17698. int tick, mob_id, val1, val2, val3, val4;
  17699. struct script_data *data;
  17700. val1 = val2 = val3 = val4 = 0;
  17701. if( (sd = script_rid2sd(st)) == NULL )
  17702. return SCRIPT_CMD_FAILURE;
  17703. data = script_getdata(st, 2);
  17704. get_val(st, data); // Convert into value in case of a variable
  17705. if( data_isstring(data) )
  17706. mob_id = mobdb_searchname(script_getstr(st, 2));
  17707. else
  17708. mob_id = mobdb_checkid(script_getnum(st, 2));
  17709. tick = script_getnum(st, 3);
  17710. if (script_hasdata(st, 4))
  17711. type = (sc_type)script_getnum(st, 4);
  17712. else
  17713. type = SC_NONE;
  17714. if (mob_id == 0) {
  17715. if( data_isstring(data) )
  17716. ShowWarning("buildin_montransform: Attempted to use non-existing monster '%s'.\n", script_getstr(st, 2));
  17717. else
  17718. ShowWarning("buildin_montransform: Attempted to use non-existing monster of ID '%d'.\n", script_getnum(st, 2));
  17719. return SCRIPT_CMD_FAILURE;
  17720. }
  17721. if (mob_id == MOBID_EMPERIUM) {
  17722. ShowWarning("buildin_montransform: Monster 'Emperium' cannot be used.\n");
  17723. return SCRIPT_CMD_FAILURE;
  17724. }
  17725. if (!(type >= SC_NONE && type < SC_MAX)) {
  17726. ShowWarning("buildin_montransform: Unsupported status change id %d\n", type);
  17727. return SCRIPT_CMD_FAILURE;
  17728. }
  17729. if (script_hasdata(st, 5))
  17730. val1 = script_getnum(st, 5);
  17731. if (script_hasdata(st, 6))
  17732. val2 = script_getnum(st, 6);
  17733. if (script_hasdata(st, 7))
  17734. val3 = script_getnum(st, 7);
  17735. if (script_hasdata(st, 8))
  17736. val4 = script_getnum(st, 8);
  17737. if (tick != 0) {
  17738. if (battle_config.mon_trans_disable_in_gvg && map_flag_gvg2(sd->bl.m)) {
  17739. clif_displaymessage(sd->fd, msg_txt(sd,731)); // Transforming into monster is not allowed in Guild Wars.
  17740. return SCRIPT_CMD_FAILURE;
  17741. }
  17742. if (sd->disguise){
  17743. clif_displaymessage(sd->fd, msg_txt(sd,729)); // Cannot transform into monster while in disguise.
  17744. return SCRIPT_CMD_FAILURE;
  17745. }
  17746. status_change_end(&sd->bl, SC_MONSTER_TRANSFORM, INVALID_TIMER); // Clear previous
  17747. sc_start2(NULL, &sd->bl, SC_MONSTER_TRANSFORM, 100, mob_id, type, tick);
  17748. if (type != SC_NONE)
  17749. sc_start4(NULL, &sd->bl, type, 100, val1, val2, val3, val4, tick);
  17750. }
  17751. return SCRIPT_CMD_SUCCESS;
  17752. }
  17753. /**
  17754. * Attach script to player for certain duration
  17755. * bonus_script "<script code>",<duration>{,<flag>{,<type>{,<status_icon>{,<char_id>}}}};
  17756. * @param "script code"
  17757. * @param duration
  17758. * @param flag
  17759. * @param icon
  17760. * @param char_id
  17761. * @author [Cydh]
  17762. **/
  17763. BUILDIN_FUNC(bonus_script) {
  17764. uint16 flag = 0;
  17765. int16 icon = SI_BLANK;
  17766. uint32 dur;
  17767. uint8 type = 0;
  17768. TBL_PC* sd;
  17769. const char *script_str = NULL;
  17770. struct s_bonus_script_entry *entry = NULL;
  17771. if (script_hasdata(st,7)) {
  17772. if (!(sd = map_charid2sd(script_getnum(st,7)))) {
  17773. ShowError("buildin_bonus_script: Player CID=%d is not found.\n", script_getnum(st,7));
  17774. return SCRIPT_CMD_FAILURE;
  17775. }
  17776. }
  17777. else
  17778. sd = script_rid2sd(st);
  17779. if (sd == NULL)
  17780. return SCRIPT_CMD_FAILURE;
  17781. script_str = script_getstr(st,2);
  17782. dur = 1000 * abs(script_getnum(st,3));
  17783. FETCH(4, flag);
  17784. FETCH(5, type);
  17785. FETCH(6, icon);
  17786. // No Script string, No Duration!
  17787. if (script_str[0] == '\0' || !dur) {
  17788. ShowError("buildin_bonus_script: Invalid! Script: \"%s\". Duration: %d\n", script_str, dur);
  17789. return SCRIPT_CMD_FAILURE;
  17790. }
  17791. if (strlen(script_str) >= MAX_BONUS_SCRIPT_LENGTH) {
  17792. ShowError("buildin_bonus_script: Script string to long: \"%s\".\n", script_str);
  17793. return SCRIPT_CMD_FAILURE;
  17794. }
  17795. if (icon <= SI_BLANK || icon >= SI_MAX)
  17796. icon = SI_BLANK;
  17797. if ((entry = pc_bonus_script_add(sd, script_str, dur, (enum si_type)icon, flag, type))) {
  17798. linkdb_insert(&sd->bonus_script.head, (void *)((intptr_t)entry), entry);
  17799. status_calc_pc(sd,SCO_NONE);
  17800. }
  17801. return SCRIPT_CMD_SUCCESS;
  17802. }
  17803. /**
  17804. * Removes all bonus script from player
  17805. * bonus_script_clear {<flag>,{<char_id>}};
  17806. * @param flag 0 - Except permanent bonus, 1 - With permanent bonus
  17807. * @param char_id Clear script from this character
  17808. * @author [Cydh]
  17809. */
  17810. BUILDIN_FUNC(bonus_script_clear) {
  17811. TBL_PC* sd;
  17812. bool flag = false;
  17813. if (script_hasdata(st,2))
  17814. flag = script_getnum(st,2);
  17815. if (script_hasdata(st,3))
  17816. sd = map_charid2sd(script_getnum(st,3));
  17817. else
  17818. sd = script_rid2sd(st);
  17819. if (sd == NULL)
  17820. return SCRIPT_CMD_FAILURE;
  17821. pc_bonus_script_clear(sd,(flag ? BSF_PERMANENT : BSF_REM_ALL));
  17822. return SCRIPT_CMD_SUCCESS;
  17823. }
  17824. /** Allows player to use atcommand while talking with NPC
  17825. * enable_command;
  17826. * @author [Cydh], [Kichi]
  17827. */
  17828. BUILDIN_FUNC(enable_command) {
  17829. TBL_PC* sd = script_rid2sd(st);
  17830. if (!sd)
  17831. return SCRIPT_CMD_FAILURE;
  17832. sd->state.disable_atcommand_on_npc = 0;
  17833. return SCRIPT_CMD_SUCCESS;
  17834. }
  17835. /** Prevents player to use atcommand while talking with NPC
  17836. * disable_command;
  17837. * @author [Cydh], [Kichi]
  17838. */
  17839. BUILDIN_FUNC(disable_command) {
  17840. TBL_PC* sd = script_rid2sd(st);
  17841. if (!sd)
  17842. return SCRIPT_CMD_FAILURE;
  17843. sd->state.disable_atcommand_on_npc = 1;
  17844. return SCRIPT_CMD_SUCCESS;
  17845. }
  17846. /** Get the information of the members of a guild by type.
  17847. * getguildmember <guild_id>{,<type>};
  17848. * @param guild_id: ID of guild
  17849. * @param type: Type of option (optional)
  17850. */
  17851. BUILDIN_FUNC(getguildmember)
  17852. {
  17853. struct guild *g = NULL;
  17854. uint8 j = 0;
  17855. g = guild_search(script_getnum(st,2));
  17856. if (g) {
  17857. uint8 i, type = 0;
  17858. struct script_data *data = NULL;
  17859. char *varname = NULL;
  17860. if (script_hasdata(st,3))
  17861. type = script_getnum(st,3);
  17862. if (script_hasdata(st,4)) {
  17863. data = script_getdata(st, 4);
  17864. if (!data_isreference(data)) {
  17865. ShowError("buildin_getguildmember: Error in argument! Please give a variable to store values in.\n");
  17866. return SCRIPT_CMD_FAILURE;
  17867. }
  17868. varname = reference_getname(data);
  17869. if (type <= 0 && varname[strlen(varname)-1] != '$') {
  17870. ShowError("buildin_getguildmember: The array %s is not string type.\n", varname);
  17871. return SCRIPT_CMD_FAILURE;
  17872. }
  17873. }
  17874. for (i = 0; i < MAX_GUILD; i++) {
  17875. if (g->member[i].account_id) {
  17876. switch (type) {
  17877. case 2:
  17878. if (data)
  17879. setd_sub(st, NULL, varname, j, (void *)__64BPRTSIZE(g->member[i].account_id), data->ref);
  17880. else
  17881. mapreg_setreg(reference_uid(add_str("$@guildmemberaid"), j),g->member[i].account_id);
  17882. break;
  17883. case 1:
  17884. if (data)
  17885. setd_sub(st, NULL, varname, j, (void *)__64BPRTSIZE(g->member[i].char_id), data->ref);
  17886. else
  17887. mapreg_setreg(reference_uid(add_str("$@guildmembercid"), j), g->member[i].char_id);
  17888. break;
  17889. default:
  17890. if (data)
  17891. setd_sub(st, NULL, varname, j, (void *)__64BPRTSIZE(g->member[i].name), data->ref);
  17892. else
  17893. mapreg_setregstr(reference_uid(add_str("$@guildmembername$"), j), g->member[i].name);
  17894. break;
  17895. }
  17896. j++;
  17897. }
  17898. }
  17899. }
  17900. mapreg_setreg(add_str("$@guildmembercount"), j);
  17901. script_pushint(st, j);
  17902. return SCRIPT_CMD_SUCCESS;
  17903. }
  17904. /** Adds spirit ball to player for 'duration' in milisecond
  17905. * addspiritball <count>,<duration>{,<char_id>};
  17906. * @param count How many spirit ball will be added
  17907. * @param duration How long spiritball is active until it disappears
  17908. * @param char_id Target player (Optional)
  17909. * @author [Cydh]
  17910. */
  17911. BUILDIN_FUNC(addspiritball) {
  17912. uint8 i, count = script_getnum(st,2);
  17913. uint16 duration = script_getnum(st,3);
  17914. struct map_session_data *sd = NULL;
  17915. if (count == 0)
  17916. return SCRIPT_CMD_SUCCESS;
  17917. if (script_hasdata(st,4)) {
  17918. if (!script_isstring(st,4))
  17919. sd = map_charid2sd(script_getnum(st,4));
  17920. else
  17921. sd = map_nick2sd(script_getstr(st,4));
  17922. }
  17923. else
  17924. sd = script_rid2sd(st);
  17925. if (!sd)
  17926. return SCRIPT_CMD_FAILURE;
  17927. for (i = 0; i < count; i++)
  17928. pc_addspiritball(sd,duration,10);
  17929. return SCRIPT_CMD_SUCCESS;
  17930. }
  17931. /** Deletes the spirit ball(s) from player
  17932. * delspiritball <count>{,<char_id>};
  17933. * @param count How many spirit ball will be deleted
  17934. * @param char_id Target player (Optional)
  17935. * @author [Cydh]
  17936. */
  17937. BUILDIN_FUNC(delspiritball) {
  17938. uint8 count = script_getnum(st,2);
  17939. struct map_session_data *sd = NULL;
  17940. if (count == 0)
  17941. count = 1;
  17942. if (script_hasdata(st,3)) {
  17943. if (!script_isstring(st,3))
  17944. sd = map_charid2sd(script_getnum(st,3));
  17945. else
  17946. sd = map_nick2sd(script_getstr(st,3));
  17947. }
  17948. else
  17949. sd = script_rid2sd(st);
  17950. if (!sd)
  17951. return SCRIPT_CMD_FAILURE;
  17952. pc_delspiritball(sd,count,0);
  17953. return SCRIPT_CMD_SUCCESS;
  17954. }
  17955. /** Counts the spirit ball that player has
  17956. * countspiritball {<char_id>};
  17957. * @param char_id Target player (Optional)
  17958. * @author [Cydh]
  17959. */
  17960. BUILDIN_FUNC(countspiritball) {
  17961. struct map_session_data *sd;
  17962. if (script_hasdata(st,2)) {
  17963. if (!script_isstring(st,2))
  17964. sd = map_charid2sd(script_getnum(st,2));
  17965. else
  17966. sd = map_nick2sd(script_getstr(st,2));
  17967. }
  17968. else
  17969. sd = script_rid2sd(st);
  17970. if (!sd)
  17971. return SCRIPT_CMD_FAILURE;
  17972. script_pushint(st,sd->spiritball);
  17973. return SCRIPT_CMD_SUCCESS;
  17974. }
  17975. /** Merges separated stackable items because of guid
  17976. * mergeitem {<char_id>};
  17977. * @param char_id Char ID (Optional)
  17978. * @author [Cydh]
  17979. */
  17980. BUILDIN_FUNC(mergeitem) {
  17981. struct map_session_data *sd;
  17982. if (!script_charid2sd(2, sd))
  17983. return SCRIPT_CMD_FAILURE;
  17984. clif_merge_item_open(sd);
  17985. return SCRIPT_CMD_SUCCESS;
  17986. }
  17987. /** Merges separated stackable items because of guid (Unofficial)
  17988. * mergeitem2 {<item_id>,{<char_id>}};
  17989. * mergeitem2 {"<item name>",{<char_id>}};
  17990. * @param item Item ID/Name for merging specific item (Optional)
  17991. * @author [Cydh]
  17992. */
  17993. BUILDIN_FUNC(mergeitem2) {
  17994. struct map_session_data *sd;
  17995. struct item *items = NULL;
  17996. uint16 i, count = 0;
  17997. int nameid = 0;
  17998. if (!script_charid2sd(3, sd))
  17999. return SCRIPT_CMD_FAILURE;
  18000. if (script_hasdata(st, 2)) {
  18001. struct script_data *data = script_getdata(st, 2);
  18002. get_val(st, data);
  18003. if (data_isstring(data)) {// "<item name>"
  18004. const char *name = conv_str(st,data);
  18005. struct item_data *id;
  18006. if (!(id = itemdb_searchname(name))) {
  18007. ShowError("buildin_mergeitem2: Nonexistant item %s requested.\n", name);
  18008. script_pushint(st, count);
  18009. return SCRIPT_CMD_FAILURE;
  18010. }
  18011. nameid = id->nameid;
  18012. }
  18013. else if (data_isint(data)) {// <item id>
  18014. nameid = conv_num(st,data);
  18015. if (!itemdb_exists(nameid)) {
  18016. ShowError("buildin_mergeitem: Nonexistant item %d requested.\n", nameid);
  18017. script_pushint(st, count);
  18018. return SCRIPT_CMD_FAILURE;
  18019. }
  18020. }
  18021. }
  18022. for (i = 0; i < MAX_INVENTORY; i++) {
  18023. struct item *it = &sd->status.inventory[i];
  18024. if (!it || !it->unique_id || it->expire_time || !itemdb_isstackable(it->nameid))
  18025. continue;
  18026. if ((!nameid || (nameid == it->nameid))) {
  18027. uint8 k;
  18028. if (!count) {
  18029. CREATE(items, struct item, 1);
  18030. memcpy(&items[count++], it, sizeof(struct item));
  18031. pc_delitem(sd, i, it->amount, 0, 0, LOG_TYPE_NPC);
  18032. continue;
  18033. }
  18034. for (k = 0; k < count; k++) {
  18035. // Find Match
  18036. if (&items[k] && items[k].nameid == it->nameid && items[k].bound == it->bound && memcmp(items[k].card, it->card, sizeof(it->card)) == 0) {
  18037. items[k].amount += it->amount;
  18038. pc_delitem(sd, i, it->amount, 0, 0, LOG_TYPE_NPC);
  18039. break;
  18040. }
  18041. }
  18042. if (k >= count) {
  18043. // New entry
  18044. RECREATE(items, struct item, count+1);
  18045. memcpy(&items[count++], it, sizeof(struct item));
  18046. pc_delitem(sd, i, it->amount, 0, 0, LOG_TYPE_NPC);
  18047. }
  18048. }
  18049. }
  18050. if (!items) // Nothing todo here
  18051. return SCRIPT_CMD_SUCCESS;
  18052. // Retrieve the items
  18053. for (i = 0; i < count; i++) {
  18054. uint8 flag = 0;
  18055. if (!&items[i])
  18056. continue;
  18057. items[i].id = 0;
  18058. items[i].unique_id = 0;
  18059. if ((flag = pc_additem(sd, &items[i], items[i].amount, LOG_TYPE_NPC)))
  18060. clif_additem(sd, i, items[i].amount, flag);
  18061. }
  18062. aFree(items);
  18063. script_pushint(st, count);
  18064. return SCRIPT_CMD_SUCCESS;
  18065. }
  18066. /**
  18067. * Update an entry from NPC shop.
  18068. * npcshopupdate "<name>",<item_id>,<price>{,<stock>}
  18069. **/
  18070. BUILDIN_FUNC(npcshopupdate) {
  18071. const char* npcname = script_getstr(st, 2);
  18072. struct npc_data* nd = npc_name2id(npcname);
  18073. uint16 nameid = script_getnum(st, 3);
  18074. int price = script_getnum(st, 4);
  18075. #if PACKETVER >= 20131223
  18076. uint16 stock = script_hasdata(st,5) ? script_getnum(st,5) : 0;
  18077. #endif
  18078. int i;
  18079. if( !nd || ( nd->subtype != NPCTYPE_SHOP && nd->subtype != NPCTYPE_CASHSHOP && nd->subtype != NPCTYPE_ITEMSHOP && nd->subtype != NPCTYPE_POINTSHOP && nd->subtype != NPCTYPE_MARKETSHOP ) ) { // Not found.
  18080. script_pushint(st,0);
  18081. return SCRIPT_CMD_FAILURE;
  18082. }
  18083. if (!nd->u.shop.count) {
  18084. ShowError("buildin_npcshopupdate: Attempt to update empty shop from '%s'.\n", nd->exname);
  18085. script_pushint(st,0);
  18086. return SCRIPT_CMD_FAILURE;
  18087. }
  18088. for (i = 0; i < nd->u.shop.count; i++) {
  18089. if (nd->u.shop.shop_item[i].nameid == nameid) {
  18090. if (price != 0)
  18091. nd->u.shop.shop_item[i].value = price;
  18092. #if PACKETVER >= 20131223
  18093. if (nd->subtype == NPCTYPE_MARKETSHOP) {
  18094. nd->u.shop.shop_item[i].qty = stock;
  18095. npc_market_tosql(nd->exname, &nd->u.shop.shop_item[i]);
  18096. }
  18097. #endif
  18098. }
  18099. }
  18100. script_pushint(st,1);
  18101. return SCRIPT_CMD_SUCCESS;
  18102. }
  18103. /**
  18104. * Get rid from running script.
  18105. * getattachedrid();
  18106. **/
  18107. BUILDIN_FUNC(getattachedrid) {
  18108. script_pushint(st, st->rid);
  18109. return SCRIPT_CMD_SUCCESS;
  18110. }
  18111. /**
  18112. * Get variable from a Player
  18113. * getvar <variable>,<char_id>;
  18114. */
  18115. BUILDIN_FUNC(getvar) {
  18116. int char_id = script_getnum(st, 3);
  18117. struct map_session_data *sd = map_charid2sd(char_id);
  18118. struct script_data *data = NULL;
  18119. const char *name = NULL;
  18120. if (!sd) {
  18121. ShowError("buildin_getvar: No player found with char id '%d'.\n", char_id);
  18122. return SCRIPT_CMD_FAILURE;
  18123. }
  18124. data = script_getdata(st, 2);
  18125. if (!data_isreference(data)) {
  18126. ShowError("buildin_getvar: Not a variable\n");
  18127. script_reportdata(data);
  18128. script_pushnil(st);
  18129. st->state = END;
  18130. return SCRIPT_CMD_FAILURE;
  18131. }
  18132. name = reference_getname(data);
  18133. if (name[0] == '.' || name[0] == '$' || name[0] == '\'') { // Not a PC variable
  18134. ShowError("buildin_getvar: Invalid scope (not PC variable)\n");
  18135. script_reportdata(data);
  18136. script_pushnil(st);
  18137. st->state = END;
  18138. return SCRIPT_CMD_FAILURE;
  18139. }
  18140. get_val_(st, data, sd);
  18141. if (data_isint(data))
  18142. script_pushint(st, conv_num_(st, data, sd));
  18143. else
  18144. script_pushstrcopy(st, conv_str_(st, data, sd));
  18145. push_val2(st->stack, C_NAME, reference_getuid(data), reference_getref(data));
  18146. return SCRIPT_CMD_SUCCESS;
  18147. }
  18148. /**
  18149. * Display script message
  18150. * showscript "<message>"{,<GID>};
  18151. **/
  18152. BUILDIN_FUNC(showscript) {
  18153. struct block_list *bl = NULL;
  18154. const char *msg = script_getstr(st,2);
  18155. int id = 0;
  18156. if (script_hasdata(st,3)) {
  18157. id = script_getnum(st,3);
  18158. bl = map_id2bl(id);
  18159. }
  18160. else {
  18161. bl = st->rid ? map_id2bl(st->rid) : map_id2bl(st->oid);
  18162. }
  18163. if (!bl) {
  18164. ShowError("buildin_showscript: Script not attached. (id=%d, rid=%d, oid=%d)\n", id, st->rid, st->oid);
  18165. script_pushint(st,0);
  18166. return SCRIPT_CMD_FAILURE;
  18167. }
  18168. clif_showscript(bl, msg);
  18169. script_pushint(st,1);
  18170. return SCRIPT_CMD_SUCCESS;
  18171. }
  18172. /**
  18173. * Ignore the SECURE_NPCTIMEOUT function.
  18174. * ignoretimeout <flag>{,<char_id>};
  18175. */
  18176. BUILDIN_FUNC(ignoretimeout)
  18177. {
  18178. #ifdef SECURE_NPCTIMEOUT
  18179. struct map_session_data *sd = NULL;
  18180. if (script_hasdata(st,3)) {
  18181. if (!script_isstring(st,3))
  18182. sd = map_charid2sd(script_getnum(st,3));
  18183. else
  18184. sd = map_nick2sd(script_getstr(st,3));
  18185. } else
  18186. sd = script_rid2sd(st);
  18187. if (!sd)
  18188. return SCRIPT_CMD_FAILURE;
  18189. sd->state.ignoretimeout = script_getnum(st,2) > 0;
  18190. #endif
  18191. return SCRIPT_CMD_SUCCESS;
  18192. }
  18193. /**
  18194. * geteleminfo <type>{,<char_id>};
  18195. **/
  18196. BUILDIN_FUNC(geteleminfo) {
  18197. TBL_ELEM *ed = NULL;
  18198. TBL_PC *sd = NULL;
  18199. int type = script_getnum(st,2);
  18200. if (!script_charid2sd(3, sd)) {
  18201. script_pushint(st, 0);
  18202. return SCRIPT_CMD_SUCCESS;
  18203. }
  18204. if (!(ed = sd->ed)) {
  18205. //ShowDebug("buildin_geteleminfo: Player doesn't have Elemental.\n");
  18206. script_pushint(st, 0);
  18207. return SCRIPT_CMD_SUCCESS;
  18208. }
  18209. switch (type) {
  18210. case 0: script_pushint(st, ed->elemental.elemental_id); break;
  18211. case 1: script_pushint(st, ed->bl.id); break;
  18212. default:
  18213. ShowError("buildin_geteleminfo: Invalid type '%d'.\n", type);
  18214. script_pushint(st, 0);
  18215. return SCRIPT_CMD_FAILURE;
  18216. }
  18217. return SCRIPT_CMD_SUCCESS;
  18218. }
  18219. /**
  18220. * Set the quest info of quest_id only showed on player in level range.
  18221. * setquestinfo_level <quest_id>,<min_level>,<max_level>
  18222. * @author [Cydh]
  18223. **/
  18224. BUILDIN_FUNC(setquestinfo_level) {
  18225. TBL_NPC* nd = map_id2nd(st->oid);
  18226. int quest_id = script_getnum(st, 2);
  18227. struct questinfo *qi = map_has_questinfo(nd->bl.m, nd, quest_id);
  18228. if (!qi) {
  18229. ShowError("buildin_setquestinfo_level: Quest with ID '%d' is not defined yet.\n", quest_id);
  18230. return SCRIPT_CMD_FAILURE;
  18231. }
  18232. qi->min_level = script_getnum(st, 3);
  18233. qi->max_level = script_getnum(st, 4);
  18234. if (!qi->max_level)
  18235. qi->max_level = MAX_LEVEL;
  18236. return SCRIPT_CMD_SUCCESS;
  18237. }
  18238. /**
  18239. * Set the quest info of quest_id only showed for player that has quest criteria
  18240. * setquestinfo_req <quest_id>,<quest_req_id>,<state>{,<quest_req_id>,<state>,...};
  18241. * @author [Cydh]
  18242. **/
  18243. BUILDIN_FUNC(setquestinfo_req) {
  18244. TBL_NPC* nd = map_id2nd(st->oid);
  18245. int quest_id = script_getnum(st, 2);
  18246. struct questinfo *qi = map_has_questinfo(nd->bl.m, nd, quest_id);
  18247. uint8 i = 0;
  18248. uint8 num = script_lastdata(st);
  18249. if (!qi) {
  18250. ShowError("buildin_setquestinfo_req: Quest with ID '%d' is not defined yet.\n", quest_id);
  18251. return SCRIPT_CMD_FAILURE;
  18252. }
  18253. if (quest_search(quest_id) == &quest_dummy) {
  18254. ShowError("buildin_setquestinfo_req: Quest with ID '%d' is not found in Quest DB.\n", quest_id);
  18255. return SCRIPT_CMD_FAILURE;
  18256. }
  18257. if (num%2) {
  18258. ShowError("buildin_setquestinfo_req: Odd number of parameters(%d) - pairs of requirements are expected.\n", num-2);
  18259. return SCRIPT_CMD_FAILURE;
  18260. }
  18261. for (i = 3; i <= num; i += 2) {
  18262. RECREATE(qi->req, struct questinfo_req, qi->req_count+1);
  18263. qi->req[qi->req_count].quest_id = script_getnum(st, i);
  18264. qi->req[qi->req_count].state = (script_getnum(st, i+1) >= 2) ? 2 : (script_getnum(st, i+1) <= 0) ? 0 : 1;
  18265. qi->req_count++;
  18266. }
  18267. return SCRIPT_CMD_SUCCESS;
  18268. }
  18269. /**
  18270. * Set the quest info of quest_id only showed for player that has specified Job
  18271. * setquestinfo_job <quest_id>,<job>{,<job>...};
  18272. * @author [Cydh]
  18273. **/
  18274. BUILDIN_FUNC(setquestinfo_job) {
  18275. TBL_NPC* nd = map_id2nd(st->oid);
  18276. int quest_id = script_getnum(st, 2);
  18277. struct questinfo *qi = map_has_questinfo(nd->bl.m, nd, quest_id);
  18278. int job_id = 0;
  18279. uint8 i = 0;
  18280. uint8 num = script_lastdata(st)+1;
  18281. if (!qi) {
  18282. ShowError("buildin_setquestinfo_job: Quest with ID '%d' is not defined yet.\n", quest_id);
  18283. return SCRIPT_CMD_FAILURE;
  18284. }
  18285. for (i = 3; i < num; i++) {
  18286. job_id = script_getnum(st, i);
  18287. if (!pcdb_checkid(job_id)) {
  18288. ShowError("buildin_setquestinfo_job: Invalid job id '%d' in Quest with ID %d.\n", job_id, quest_id);
  18289. continue;
  18290. }
  18291. buildin_questinfo_setjob(qi, job_id);
  18292. }
  18293. return SCRIPT_CMD_SUCCESS;
  18294. }
  18295. /**
  18296. * opendressroom({<char_id>});
  18297. */
  18298. BUILDIN_FUNC(opendressroom)
  18299. {
  18300. #if PACKETVER >= 20150513
  18301. TBL_PC* sd;
  18302. if (!script_charid2sd(2, sd))
  18303. return SCRIPT_CMD_FAILURE;
  18304. clif_dressing_room(sd, 1);
  18305. return SCRIPT_CMD_SUCCESS;
  18306. #else
  18307. return SCRIPT_CMD_FAILURE;
  18308. #endif
  18309. }
  18310. /**
  18311. * closedressroom({<char_id>});
  18312. */
  18313. BUILDIN_FUNC(closedressroom)
  18314. {
  18315. #if PACKETVER >= 20150513
  18316. TBL_PC* sd;
  18317. if (!script_charid2sd(2, sd))
  18318. return SCRIPT_CMD_FAILURE;
  18319. clif_dressing_room(sd, 0);
  18320. return SCRIPT_CMD_SUCCESS;
  18321. #else
  18322. return SCRIPT_CMD_FAILURE;
  18323. #endif
  18324. }
  18325. #include "../custom/script.inc"
  18326. // declarations that were supposed to be exported from npc_chat.c
  18327. #ifdef PCRE_SUPPORT
  18328. BUILDIN_FUNC(defpattern);
  18329. BUILDIN_FUNC(activatepset);
  18330. BUILDIN_FUNC(deactivatepset);
  18331. BUILDIN_FUNC(deletepset);
  18332. #endif
  18333. /** Regular expression matching
  18334. * preg_match(<pattern>,<string>{,<offset>})
  18335. */
  18336. BUILDIN_FUNC(preg_match) {
  18337. #ifdef PCRE_SUPPORT
  18338. pcre *re;
  18339. pcre_extra *pcreExtra;
  18340. const char *error;
  18341. int erroffset, r, offset = 0;
  18342. int subStrVec[30];
  18343. const char* pattern = script_getstr(st,2);
  18344. const char* subject = script_getstr(st,3);
  18345. if (script_hasdata(st,4))
  18346. offset = script_getnum(st,4);
  18347. re = pcre_compile(pattern, 0, &error, &erroffset, NULL);
  18348. pcreExtra = pcre_study(re, 0, &error);
  18349. r = pcre_exec(re, pcreExtra, subject, (int)strlen(subject), offset, 0, subStrVec, 30);
  18350. pcre_free(re);
  18351. if (pcreExtra != NULL)
  18352. pcre_free(pcreExtra);
  18353. if (r < 0)
  18354. script_pushint(st,0);
  18355. else
  18356. script_pushint(st,(r > 0) ? r : 30 / 3);
  18357. return SCRIPT_CMD_SUCCESS;
  18358. #else
  18359. ShowDebug("script:preg_match: cannot run without PCRE library enabled.\n");
  18360. script_pushint(st,0);
  18361. return SCRIPT_CMD_SUCCESS;
  18362. #endif
  18363. }
  18364. /// script command definitions
  18365. /// for an explanation on args, see add_buildin_func
  18366. struct script_function buildin_func[] = {
  18367. // NPC interaction
  18368. BUILDIN_DEF(mes,"s*"),
  18369. BUILDIN_DEF(next,""),
  18370. BUILDIN_DEF(close,""),
  18371. BUILDIN_DEF(close2,""),
  18372. BUILDIN_DEF(menu,"sl*"),
  18373. BUILDIN_DEF(select,"s*"), //for future jA script compatibility
  18374. BUILDIN_DEF(prompt,"s*"),
  18375. //
  18376. BUILDIN_DEF(goto,"l"),
  18377. BUILDIN_DEF(callsub,"l*"),
  18378. BUILDIN_DEF(callfunc,"s*"),
  18379. BUILDIN_DEF(return,"?"),
  18380. BUILDIN_DEF(getarg,"i?"),
  18381. BUILDIN_DEF(jobchange,"i??"),
  18382. BUILDIN_DEF(jobname,"i"),
  18383. BUILDIN_DEF(input,"r??"),
  18384. BUILDIN_DEF(warp,"sii"),
  18385. BUILDIN_DEF(areawarp,"siiiisii??"),
  18386. BUILDIN_DEF(warpchar,"siii"), // [LuzZza]
  18387. BUILDIN_DEF(warpparty,"siii?"), // [Fredzilla] [Paradox924X]
  18388. BUILDIN_DEF(warpguild,"siii"), // [Fredzilla]
  18389. BUILDIN_DEF(setlook,"ii?"),
  18390. BUILDIN_DEF(changelook,"ii?"), // Simulates but don't Store it
  18391. BUILDIN_DEF(set,"rv?"),
  18392. BUILDIN_DEF(setarray,"rv*"),
  18393. BUILDIN_DEF(cleararray,"rvi"),
  18394. BUILDIN_DEF(copyarray,"rri"),
  18395. BUILDIN_DEF(getarraysize,"r"),
  18396. BUILDIN_DEF(deletearray,"r?"),
  18397. BUILDIN_DEF(getelementofarray,"ri"),
  18398. BUILDIN_DEF(getitem,"vi?"),
  18399. BUILDIN_DEF(rentitem,"vi?"),
  18400. BUILDIN_DEF(rentitem2,"viiiiiiii?"),
  18401. BUILDIN_DEF(getitem2,"viiiiiiii?"),
  18402. BUILDIN_DEF(getnameditem,"vv"),
  18403. BUILDIN_DEF2(grouprandomitem,"groupranditem","i?"),
  18404. BUILDIN_DEF(makeitem,"visii"),
  18405. BUILDIN_DEF(makeitem2,"visiiiiiiiii"),
  18406. BUILDIN_DEF(delitem,"vi?"),
  18407. BUILDIN_DEF2(delitem,"storagedelitem","vi?"),
  18408. BUILDIN_DEF2(delitem,"cartdelitem","vi?"),
  18409. BUILDIN_DEF(delitem2,"viiiiiiii?"),
  18410. BUILDIN_DEF2(delitem2,"storagedelitem2","viiiiiiii?"),
  18411. BUILDIN_DEF2(delitem2,"cartdelitem2","viiiiiiii?"),
  18412. BUILDIN_DEF2(enableitemuse,"enable_items",""),
  18413. BUILDIN_DEF2(disableitemuse,"disable_items",""),
  18414. BUILDIN_DEF(cutin,"si"),
  18415. BUILDIN_DEF(viewpoint,"iiiii"),
  18416. BUILDIN_DEF(heal,"ii?"),
  18417. BUILDIN_DEF(itemheal,"ii?"),
  18418. BUILDIN_DEF(percentheal,"ii?"),
  18419. BUILDIN_DEF(rand,"i?"),
  18420. BUILDIN_DEF(countitem,"v?"),
  18421. BUILDIN_DEF2(countitem,"storagecountitem","v?"),
  18422. BUILDIN_DEF2(countitem,"cartcountitem","v?"),
  18423. BUILDIN_DEF2(countitem,"countitem2","viiiiiii?"),
  18424. BUILDIN_DEF2(countitem,"storagecountitem2","viiiiiii?"),
  18425. BUILDIN_DEF2(countitem,"cartcountitem2","viiiiiii?"),
  18426. BUILDIN_DEF(checkweight,"vi*"),
  18427. BUILDIN_DEF(checkweight2,"rr"),
  18428. BUILDIN_DEF(readparam,"i?"),
  18429. BUILDIN_DEF(getcharid,"i?"),
  18430. BUILDIN_DEF(getnpcid,"i?"),
  18431. BUILDIN_DEF(getpartyname,"i"),
  18432. BUILDIN_DEF(getpartymember,"i??"),
  18433. BUILDIN_DEF(getpartyleader,"i?"),
  18434. BUILDIN_DEF(getguildname,"i"),
  18435. BUILDIN_DEF(getguildmaster,"i"),
  18436. BUILDIN_DEF(getguildmasterid,"i"),
  18437. BUILDIN_DEF(strcharinfo,"i?"),
  18438. BUILDIN_DEF(strnpcinfo,"i"),
  18439. BUILDIN_DEF(getequipid,"i?"),
  18440. BUILDIN_DEF(getequipuniqueid,"i?"),
  18441. BUILDIN_DEF(getequipname,"i?"),
  18442. BUILDIN_DEF(getbrokenid,"i?"), // [Valaris]
  18443. BUILDIN_DEF(repair,"i?"), // [Valaris]
  18444. BUILDIN_DEF(repairall,"?"),
  18445. BUILDIN_DEF(getequipisequiped,"i?"),
  18446. BUILDIN_DEF(getequipisenableref,"i?"),
  18447. BUILDIN_DEF(getequiprefinerycnt,"i?"),
  18448. BUILDIN_DEF(getequipweaponlv,"i?"),
  18449. BUILDIN_DEF(getequippercentrefinery,"i?"),
  18450. BUILDIN_DEF(successrefitem,"i??"),
  18451. BUILDIN_DEF(failedrefitem,"i?"),
  18452. BUILDIN_DEF(downrefitem,"i??"),
  18453. BUILDIN_DEF(statusup,"i?"),
  18454. BUILDIN_DEF(statusup2,"ii?"),
  18455. BUILDIN_DEF(bonus,"iv"),
  18456. BUILDIN_DEF2(bonus,"bonus2","ivi"),
  18457. BUILDIN_DEF2(bonus,"bonus3","ivii"),
  18458. BUILDIN_DEF2(bonus,"bonus4","ivvii"),
  18459. BUILDIN_DEF2(bonus,"bonus5","ivviii"),
  18460. BUILDIN_DEF(autobonus,"sii??"),
  18461. BUILDIN_DEF(autobonus2,"sii??"),
  18462. BUILDIN_DEF(autobonus3,"siiv?"),
  18463. BUILDIN_DEF(skill,"vi?"),
  18464. BUILDIN_DEF2(skill,"addtoskill","vi?"), // [Valaris]
  18465. BUILDIN_DEF(guildskill,"vi"),
  18466. BUILDIN_DEF(getskilllv,"v"),
  18467. BUILDIN_DEF(getgdskilllv,"iv"),
  18468. BUILDIN_DEF(basicskillcheck,""),
  18469. BUILDIN_DEF(getgmlevel,"?"),
  18470. BUILDIN_DEF(getgroupid,"?"),
  18471. BUILDIN_DEF(end,""),
  18472. BUILDIN_DEF(checkoption,"i?"),
  18473. BUILDIN_DEF(setoption,"i??"),
  18474. BUILDIN_DEF(setcart,"??"),
  18475. BUILDIN_DEF(checkcart,"?"),
  18476. BUILDIN_DEF(setfalcon,"??"),
  18477. BUILDIN_DEF(checkfalcon,"?"),
  18478. BUILDIN_DEF(setriding,"??"),
  18479. BUILDIN_DEF(checkriding,"?"),
  18480. BUILDIN_DEF(checkwug,"?"),
  18481. BUILDIN_DEF(checkmadogear,"?"),
  18482. BUILDIN_DEF(setmadogear,"??"),
  18483. BUILDIN_DEF2(savepoint,"save","sii?"),
  18484. BUILDIN_DEF(savepoint,"sii?"),
  18485. BUILDIN_DEF(gettimetick,"i"),
  18486. BUILDIN_DEF(gettime,"i"),
  18487. BUILDIN_DEF(gettimestr,"si"),
  18488. BUILDIN_DEF(openstorage,""),
  18489. BUILDIN_DEF(guildopenstorage,""),
  18490. BUILDIN_DEF(itemskill,"vi"),
  18491. BUILDIN_DEF(produce,"i"),
  18492. BUILDIN_DEF(cooking,"i"),
  18493. BUILDIN_DEF(monster,"siisii???"),
  18494. BUILDIN_DEF(getmobdrops,"i"),
  18495. BUILDIN_DEF(areamonster,"siiiisii???"),
  18496. BUILDIN_DEF(killmonster,"ss?"),
  18497. BUILDIN_DEF(killmonsterall,"s?"),
  18498. BUILDIN_DEF(clone,"siisi????"),
  18499. BUILDIN_DEF(doevent,"s"),
  18500. BUILDIN_DEF(donpcevent,"s"),
  18501. BUILDIN_DEF(cmdothernpc,"ss"),
  18502. BUILDIN_DEF(addtimer,"is"),
  18503. BUILDIN_DEF(deltimer,"s"),
  18504. BUILDIN_DEF(addtimercount,"is"),
  18505. BUILDIN_DEF(initnpctimer,"??"),
  18506. BUILDIN_DEF(stopnpctimer,"??"),
  18507. BUILDIN_DEF(startnpctimer,"??"),
  18508. BUILDIN_DEF(setnpctimer,"i?"),
  18509. BUILDIN_DEF(getnpctimer,"i?"),
  18510. BUILDIN_DEF(attachnpctimer,"?"), // attached the player id to the npc timer [Celest]
  18511. BUILDIN_DEF(detachnpctimer,"?"), // detached the player id from the npc timer [Celest]
  18512. BUILDIN_DEF(playerattached,""), // returns id of the current attached player. [Skotlex]
  18513. BUILDIN_DEF(announce,"si?????"),
  18514. BUILDIN_DEF(mapannounce,"ssi?????"),
  18515. BUILDIN_DEF(areaannounce,"siiiisi?????"),
  18516. BUILDIN_DEF(getusers,"i"),
  18517. BUILDIN_DEF(getmapguildusers,"si"),
  18518. BUILDIN_DEF(getmapusers,"s"),
  18519. BUILDIN_DEF(getareausers,"siiii"),
  18520. BUILDIN_DEF(getareadropitem,"siiiiv"),
  18521. BUILDIN_DEF(enablenpc,"s"),
  18522. BUILDIN_DEF(disablenpc,"s"),
  18523. BUILDIN_DEF(hideoffnpc,"s"),
  18524. BUILDIN_DEF(hideonnpc,"s"),
  18525. BUILDIN_DEF(sc_start,"iii???"),
  18526. BUILDIN_DEF2(sc_start,"sc_start2","iiii???"),
  18527. BUILDIN_DEF2(sc_start,"sc_start4","iiiiii???"),
  18528. BUILDIN_DEF(sc_end,"i?"),
  18529. BUILDIN_DEF(getstatus, "i??"),
  18530. BUILDIN_DEF(getscrate,"ii?"),
  18531. BUILDIN_DEF(debugmes,"s"),
  18532. BUILDIN_DEF2(catchpet,"pet","i"),
  18533. BUILDIN_DEF2(birthpet,"bpet",""),
  18534. BUILDIN_DEF(resetlvl,"i?"),
  18535. BUILDIN_DEF(resetstatus,"?"),
  18536. BUILDIN_DEF(resetskill,"?"),
  18537. BUILDIN_DEF(skillpointcount,"?"),
  18538. BUILDIN_DEF(changebase,"i?"),
  18539. BUILDIN_DEF(changesex,"?"),
  18540. BUILDIN_DEF(changecharsex,"?"),
  18541. BUILDIN_DEF(waitingroom,"si?????"),
  18542. BUILDIN_DEF(delwaitingroom,"?"),
  18543. BUILDIN_DEF(waitingroomkick,"ss"),
  18544. BUILDIN_DEF(getwaitingroomusers, "?"),
  18545. BUILDIN_DEF2(waitingroomkickall,"kickwaitingroomall","?"),
  18546. BUILDIN_DEF(enablewaitingroomevent,"?"),
  18547. BUILDIN_DEF(disablewaitingroomevent,"?"),
  18548. BUILDIN_DEF2(enablewaitingroomevent,"enablearena",""), // Added by RoVeRT
  18549. BUILDIN_DEF2(disablewaitingroomevent,"disablearena",""), // Added by RoVeRT
  18550. BUILDIN_DEF(getwaitingroomstate,"i?"),
  18551. BUILDIN_DEF(warpwaitingpc,"sii?"),
  18552. BUILDIN_DEF(attachrid,"i"),
  18553. BUILDIN_DEF(addrid,"i?????"),
  18554. BUILDIN_DEF(detachrid,""),
  18555. BUILDIN_DEF(isloggedin,"i?"),
  18556. BUILDIN_DEF(setmapflagnosave,"ssii"),
  18557. BUILDIN_DEF(getmapflag,"si?"),
  18558. BUILDIN_DEF(setmapflag,"si??"),
  18559. BUILDIN_DEF(removemapflag,"si?"),
  18560. BUILDIN_DEF(pvpon,"s"),
  18561. BUILDIN_DEF(pvpoff,"s"),
  18562. BUILDIN_DEF(gvgon,"s"),
  18563. BUILDIN_DEF(gvgoff,"s"),
  18564. BUILDIN_DEF(emotion,"i??"),
  18565. BUILDIN_DEF(maprespawnguildid,"sii"),
  18566. BUILDIN_DEF(agitstart,""), // <Agit>
  18567. BUILDIN_DEF(agitend,""),
  18568. BUILDIN_DEF(agitcheck,""), // <Agitcheck>
  18569. BUILDIN_DEF(flagemblem,"i"), // Flag Emblem
  18570. BUILDIN_DEF(getcastlename,"s"),
  18571. BUILDIN_DEF(getcastledata,"si"),
  18572. BUILDIN_DEF(setcastledata,"sii"),
  18573. BUILDIN_DEF(requestguildinfo,"i?"),
  18574. BUILDIN_DEF(getequipcardcnt,"i"),
  18575. BUILDIN_DEF(successremovecards,"i"),
  18576. BUILDIN_DEF(failedremovecards,"ii"),
  18577. BUILDIN_DEF(marriage,"s"),
  18578. BUILDIN_DEF2(wedding_effect,"wedding",""),
  18579. BUILDIN_DEF(divorce,"?"),
  18580. BUILDIN_DEF(ispartneron,"?"),
  18581. BUILDIN_DEF(getpartnerid,"?"),
  18582. BUILDIN_DEF(getchildid,"?"),
  18583. BUILDIN_DEF(getmotherid,"?"),
  18584. BUILDIN_DEF(getfatherid,"?"),
  18585. BUILDIN_DEF(warppartner,"sii"),
  18586. BUILDIN_DEF(getitemname,"v"),
  18587. BUILDIN_DEF(getitemslots,"i"),
  18588. BUILDIN_DEF(makepet,"i"),
  18589. BUILDIN_DEF(getexp,"ii?"),
  18590. BUILDIN_DEF(getinventorylist,"?"),
  18591. BUILDIN_DEF(getskilllist,"?"),
  18592. BUILDIN_DEF(clearitem,"?"),
  18593. BUILDIN_DEF(classchange,"ii"),
  18594. BUILDIN_DEF(misceffect,"i"),
  18595. BUILDIN_DEF(playBGM,"s"),
  18596. BUILDIN_DEF(playBGMall,"s?????"),
  18597. BUILDIN_DEF(soundeffect,"si"),
  18598. BUILDIN_DEF(soundeffectall,"si?????"), // SoundEffectAll [Codemaster]
  18599. BUILDIN_DEF(strmobinfo,"ii"), // display mob data [Valaris]
  18600. BUILDIN_DEF(guardian,"siisi??"), // summon guardians
  18601. BUILDIN_DEF(guardianinfo,"sii"), // display guardian data [Valaris]
  18602. BUILDIN_DEF(petskillbonus,"iiii"), // [Valaris]
  18603. BUILDIN_DEF(petrecovery,"ii"), // [Valaris]
  18604. BUILDIN_DEF(petloot,"i"), // [Valaris]
  18605. BUILDIN_DEF(petheal,"iiii"), // [Valaris]
  18606. BUILDIN_DEF(petskillattack,"viii"), // [Skotlex]
  18607. BUILDIN_DEF(petskillattack2,"viiii"), // [Valaris]
  18608. BUILDIN_DEF(petskillsupport,"viiii"), // [Skotlex]
  18609. BUILDIN_DEF(skilleffect,"vi"), // skill effect [Celest]
  18610. BUILDIN_DEF(npcskilleffect,"viii"), // npc skill effect [Valaris]
  18611. BUILDIN_DEF(specialeffect,"i??"), // npc skill effect [Valaris]
  18612. BUILDIN_DEF(specialeffect2,"i??"), // skill effect on players[Valaris]
  18613. BUILDIN_DEF(nude,"?"), // nude command [Valaris]
  18614. BUILDIN_DEF(mapwarp,"ssii??"), // Added by RoVeRT
  18615. BUILDIN_DEF(atcommand,"s"), // [MouseJstr]
  18616. BUILDIN_DEF2(atcommand,"charcommand","s"), // [MouseJstr]
  18617. BUILDIN_DEF(movenpc,"sii?"), // [MouseJstr]
  18618. BUILDIN_DEF(message,"ss"), // [MouseJstr]
  18619. BUILDIN_DEF(npctalk,"s"), // [Valaris]
  18620. BUILDIN_DEF(mobcount,"ss"),
  18621. BUILDIN_DEF(getlook,"i?"),
  18622. BUILDIN_DEF(getsavepoint,"i?"),
  18623. BUILDIN_DEF(npcspeed,"i"), // [Valaris]
  18624. BUILDIN_DEF(npcwalkto,"ii"), // [Valaris]
  18625. BUILDIN_DEF(npcstop,""), // [Valaris]
  18626. BUILDIN_DEF(getmapxy,"rrri?"), //by Lorky [Lupus]
  18627. BUILDIN_DEF(mapid2name,"i"),
  18628. BUILDIN_DEF(checkoption1,"i?"),
  18629. BUILDIN_DEF(checkoption2,"i?"),
  18630. BUILDIN_DEF(guildgetexp,"i"),
  18631. BUILDIN_DEF(guildchangegm,"is"),
  18632. BUILDIN_DEF(logmes,"s"), //this command actls as MES but rints info into LOG file either SQL/TXT [Lupus]
  18633. BUILDIN_DEF(summon,"si??"), // summons a slave monster [Celest]
  18634. BUILDIN_DEF(isnight,""), // check whether it is night time [Celest]
  18635. BUILDIN_DEF(isday,""), // check whether it is day time [Celest]
  18636. BUILDIN_DEF(isequipped,"i*"), // check whether another item/card has been equipped [Celest]
  18637. BUILDIN_DEF(isequippedcnt,"i*"), // check how many items/cards are being equipped [Celest]
  18638. BUILDIN_DEF(cardscnt,"i*"), // check how many items/cards are being equipped in the same arm [Lupus]
  18639. BUILDIN_DEF(getrefine,""), // returns the refined number of the current item, or an item with index specified [celest]
  18640. BUILDIN_DEF(night,""), // sets the server to night time
  18641. BUILDIN_DEF(day,""), // sets the server to day time
  18642. #ifdef PCRE_SUPPORT
  18643. BUILDIN_DEF(defpattern,"iss"), // Define pattern to listen for [MouseJstr]
  18644. BUILDIN_DEF(activatepset,"i"), // Activate a pattern set [MouseJstr]
  18645. BUILDIN_DEF(deactivatepset,"i"), // Deactive a pattern set [MouseJstr]
  18646. BUILDIN_DEF(deletepset,"i"), // Delete a pattern set [MouseJstr]
  18647. #endif
  18648. BUILDIN_DEF(preg_match,"ss?"),
  18649. BUILDIN_DEF(dispbottom,"s??"), //added from jA [Lupus]
  18650. BUILDIN_DEF(recovery,"i???"),
  18651. BUILDIN_DEF(getpetinfo,"i?"),
  18652. BUILDIN_DEF(gethominfo,"i?"),
  18653. BUILDIN_DEF(getmercinfo,"i?"),
  18654. BUILDIN_DEF(checkequipedcard,"i"),
  18655. BUILDIN_DEF(jump_zero,"il"), //for future jA script compatibility
  18656. BUILDIN_DEF(globalmes,"s?"), //end jA addition
  18657. BUILDIN_DEF(unequip,"i?"), // unequip command [Spectre]
  18658. BUILDIN_DEF(getstrlen,"s"), //strlen [Valaris]
  18659. BUILDIN_DEF(charisalpha,"si"), //isalpha [Valaris]
  18660. BUILDIN_DEF(charat,"si"),
  18661. BUILDIN_DEF(setchar,"ssi"),
  18662. BUILDIN_DEF(insertchar,"ssi"),
  18663. BUILDIN_DEF(delchar,"si"),
  18664. BUILDIN_DEF(strtoupper,"s"),
  18665. BUILDIN_DEF(strtolower,"s"),
  18666. BUILDIN_DEF(charisupper, "si"),
  18667. BUILDIN_DEF(charislower, "si"),
  18668. BUILDIN_DEF(substr,"sii"),
  18669. BUILDIN_DEF(explode, "rss"),
  18670. BUILDIN_DEF(implode, "r?"),
  18671. BUILDIN_DEF(sprintf,"s*"), // [Mirei]
  18672. BUILDIN_DEF(sscanf,"ss*"), // [Mirei]
  18673. BUILDIN_DEF(strpos,"ss?"),
  18674. BUILDIN_DEF(replacestr,"sss??"),
  18675. BUILDIN_DEF(countstr,"ss?"),
  18676. BUILDIN_DEF(setnpcdisplay,"sv??"),
  18677. BUILDIN_DEF(compare,"ss"), // Lordalfa - To bring strstr to scripting Engine.
  18678. BUILDIN_DEF(strcmp,"ss"),
  18679. BUILDIN_DEF(getiteminfo,"ii"), //[Lupus] returns Items Buy / sell Price, etc info
  18680. BUILDIN_DEF(setiteminfo,"iii"), //[Lupus] set Items Buy / sell Price, etc info
  18681. BUILDIN_DEF(getequipcardid,"ii"), //[Lupus] returns CARD ID or other info from CARD slot N of equipped item
  18682. // [zBuffer] List of mathematics commands --->
  18683. BUILDIN_DEF(sqrt,"i"),
  18684. BUILDIN_DEF(pow,"ii"),
  18685. BUILDIN_DEF(distance,"iiii"),
  18686. // <--- [zBuffer] List of mathematics commands
  18687. BUILDIN_DEF(md5,"s"),
  18688. // [zBuffer] List of dynamic var commands --->
  18689. BUILDIN_DEF(getd,"s"),
  18690. BUILDIN_DEF(setd,"sv?"),
  18691. BUILDIN_DEF(callshop,"s?"), // [Skotlex]
  18692. BUILDIN_DEF(npcshopitem,"sii*"), // [Lance]
  18693. BUILDIN_DEF(npcshopadditem,"sii*"),
  18694. BUILDIN_DEF(npcshopdelitem,"si*"),
  18695. BUILDIN_DEF(npcshopattach,"s?"),
  18696. BUILDIN_DEF(equip,"i?"),
  18697. BUILDIN_DEF(autoequip,"ii"),
  18698. BUILDIN_DEF(setbattleflag,"si"),
  18699. BUILDIN_DEF(getbattleflag,"s"),
  18700. BUILDIN_DEF(setitemscript,"is?"), //Set NEW item bonus script. Lupus
  18701. BUILDIN_DEF(disguise,"i?"), //disguise player. Lupus
  18702. BUILDIN_DEF(undisguise,"?"), //undisguise player. Lupus
  18703. BUILDIN_DEF(getmonsterinfo,"ii"), //Lupus
  18704. BUILDIN_DEF(addmonsterdrop,"vii"), //Akinari [Lupus]
  18705. BUILDIN_DEF(delmonsterdrop,"vi"), //Akinari [Lupus]
  18706. BUILDIN_DEF(axtoi,"s"),
  18707. BUILDIN_DEF(query_sql,"s*"),
  18708. BUILDIN_DEF(query_logsql,"s*"),
  18709. BUILDIN_DEF(escape_sql,"v"),
  18710. BUILDIN_DEF(atoi,"s"),
  18711. BUILDIN_DEF(strtol,"si"),
  18712. // [zBuffer] List of player cont commands --->
  18713. BUILDIN_DEF(rid2name,"i"),
  18714. BUILDIN_DEF(pcfollow,"ii"),
  18715. BUILDIN_DEF(pcstopfollow,"i"),
  18716. BUILDIN_DEF(pcblockmove,"ii"),
  18717. // <--- [zBuffer] List of player cont commands
  18718. // [zBuffer] List of unit control commands --->
  18719. BUILDIN_DEF(getunittype,"i"),
  18720. BUILDIN_DEF(getunitname,"i"),
  18721. BUILDIN_DEF(setunitname,"is"),
  18722. BUILDIN_DEF(getunitdata,"i*"),
  18723. BUILDIN_DEF(setunitdata,"iii"),
  18724. BUILDIN_DEF(unitwalk,"iii?"),
  18725. BUILDIN_DEF2(unitwalk,"unitwalkto","ii?"),
  18726. BUILDIN_DEF(unitkill,"i"),
  18727. BUILDIN_DEF(unitwarp,"isii"),
  18728. BUILDIN_DEF(unitattack,"iv?"),
  18729. BUILDIN_DEF(unitstopattack,"i"),
  18730. BUILDIN_DEF(unitstopwalk,"i"),
  18731. BUILDIN_DEF2(pcblockmove,"unitblockmove","ii"),
  18732. BUILDIN_DEF(unittalk,"is"),
  18733. BUILDIN_DEF(unitemote,"ii"),
  18734. BUILDIN_DEF(unitskilluseid,"ivi??"), // originally by Qamera [Celest]
  18735. BUILDIN_DEF(unitskillusepos,"iviii?"), // [Celest]
  18736. // <--- [zBuffer] List of unit control commands
  18737. BUILDIN_DEF(sleep,"i"),
  18738. BUILDIN_DEF(sleep2,"i"),
  18739. BUILDIN_DEF(awake,"s"),
  18740. BUILDIN_DEF(getvariableofnpc,"rs"),
  18741. BUILDIN_DEF(warpportal,"iisii"),
  18742. BUILDIN_DEF2(homunculus_evolution,"homevolution",""), //[orn]
  18743. BUILDIN_DEF2(homunculus_mutate,"hommutate","?"),
  18744. BUILDIN_DEF(morphembryo,""),
  18745. BUILDIN_DEF2(homunculus_shuffle,"homshuffle",""), //[Zephyrus]
  18746. BUILDIN_DEF(checkhomcall,""),
  18747. BUILDIN_DEF(eaclass,"??"), //[Skotlex]
  18748. BUILDIN_DEF(roclass,"i?"), //[Skotlex]
  18749. BUILDIN_DEF(checkvending,"?"),
  18750. BUILDIN_DEF(checkchatting,"?"),
  18751. BUILDIN_DEF(checkidle,"?"),
  18752. BUILDIN_DEF(openmail,"?"),
  18753. BUILDIN_DEF(openauction,"?"),
  18754. BUILDIN_DEF(checkcell,"siii"),
  18755. BUILDIN_DEF(setcell,"siiiiii"),
  18756. BUILDIN_DEF(setwall,"siiiiis"),
  18757. BUILDIN_DEF(delwall,"s"),
  18758. BUILDIN_DEF(searchitem,"rs"),
  18759. BUILDIN_DEF(mercenary_create,"ii"),
  18760. BUILDIN_DEF(mercenary_heal,"ii"),
  18761. BUILDIN_DEF(mercenary_sc_start,"iii"),
  18762. BUILDIN_DEF(mercenary_get_calls,"i"),
  18763. BUILDIN_DEF(mercenary_get_faith,"i"),
  18764. BUILDIN_DEF(mercenary_set_calls,"ii"),
  18765. BUILDIN_DEF(mercenary_set_faith,"ii"),
  18766. BUILDIN_DEF(readbook,"ii"),
  18767. BUILDIN_DEF(setfont,"i"),
  18768. BUILDIN_DEF(areamobuseskill,"siiiiviiiii"),
  18769. BUILDIN_DEF(progressbar,"si"),
  18770. BUILDIN_DEF(pushpc,"ii"),
  18771. BUILDIN_DEF(buyingstore,"i"),
  18772. BUILDIN_DEF(searchstores,"ii"),
  18773. BUILDIN_DEF(showdigit,"i?"),
  18774. // WoE SE
  18775. BUILDIN_DEF(agitstart2,""),
  18776. BUILDIN_DEF(agitend2,""),
  18777. BUILDIN_DEF(agitcheck2,""),
  18778. // BattleGround
  18779. BUILDIN_DEF(waitingroom2bg,"siiss?"),
  18780. BUILDIN_DEF(waitingroom2bg_single,"isiis"),
  18781. BUILDIN_DEF(bg_team_setxy,"iii"),
  18782. BUILDIN_DEF(bg_warp,"isii"),
  18783. BUILDIN_DEF(bg_monster,"isiisi?"),
  18784. BUILDIN_DEF(bg_monster_set_team,"ii"),
  18785. BUILDIN_DEF(bg_leave,"?"),
  18786. BUILDIN_DEF(bg_destroy,"i"),
  18787. BUILDIN_DEF(areapercentheal,"siiiiii"),
  18788. BUILDIN_DEF(bg_get_data,"ii"),
  18789. BUILDIN_DEF(bg_getareausers,"isiiii"),
  18790. BUILDIN_DEF(bg_updatescore,"sii"),
  18791. // Instancing
  18792. BUILDIN_DEF(instance_create,"s"),
  18793. BUILDIN_DEF(instance_destroy,"?"),
  18794. BUILDIN_DEF(instance_id,""),
  18795. BUILDIN_DEF(instance_enter,"s???"),
  18796. BUILDIN_DEF(instance_npcname,"s?"),
  18797. BUILDIN_DEF(instance_mapname,"s?"),
  18798. BUILDIN_DEF(instance_warpall,"sii?"),
  18799. BUILDIN_DEF(instance_announce,"isi?????"),
  18800. BUILDIN_DEF(instance_check_party,"i???"),
  18801. /**
  18802. * 3rd-related
  18803. **/
  18804. BUILDIN_DEF(makerune,"i?"),
  18805. BUILDIN_DEF(checkdragon,"?"),//[Ind]
  18806. BUILDIN_DEF(setdragon,"??"),//[Ind]
  18807. BUILDIN_DEF(ismounting,"?"),//[Ind]
  18808. BUILDIN_DEF(setmounting,"?"),//[Ind]
  18809. BUILDIN_DEF(checkre,"i"),
  18810. /**
  18811. * rAthena and beyond!
  18812. **/
  18813. BUILDIN_DEF(getargcount,""),
  18814. BUILDIN_DEF(getcharip,"?"),
  18815. BUILDIN_DEF(is_function,"s"),
  18816. BUILDIN_DEF(get_revision,""),
  18817. BUILDIN_DEF(get_githash,""),
  18818. BUILDIN_DEF(freeloop,"?"),
  18819. BUILDIN_DEF(getrandgroupitem,"i??"),
  18820. BUILDIN_DEF(cleanmap,"s"),
  18821. BUILDIN_DEF2(cleanmap,"cleanarea","siiii"),
  18822. BUILDIN_DEF(npcskill,"viii"),
  18823. BUILDIN_DEF(consumeitem,"v?"),
  18824. BUILDIN_DEF(delequip,"i?"),
  18825. BUILDIN_DEF(breakequip,"i?"),
  18826. BUILDIN_DEF(sit,"?"),
  18827. BUILDIN_DEF(stand,"?"),
  18828. //@commands (script based)
  18829. BUILDIN_DEF(bindatcmd, "ss??"),
  18830. BUILDIN_DEF(unbindatcmd, "s"),
  18831. BUILDIN_DEF(useatcmd, "s"),
  18832. //Quest Log System [Inkfish]
  18833. BUILDIN_DEF(questinfo, "ii??"),
  18834. BUILDIN_DEF(setquest, "i?"),
  18835. BUILDIN_DEF(erasequest, "i?"),
  18836. BUILDIN_DEF(completequest, "i?"),
  18837. BUILDIN_DEF(checkquest, "i??"),
  18838. BUILDIN_DEF(isbegin_quest,"i?"),
  18839. BUILDIN_DEF(changequest, "ii?"),
  18840. BUILDIN_DEF(showevent, "i??"),
  18841. //Bound items [Xantara] & [Akinari]
  18842. BUILDIN_DEF2(getitem,"getitembound","vii?"),
  18843. BUILDIN_DEF2(getitem2,"getitembound2","viiiiiiiii?"),
  18844. BUILDIN_DEF(countbound, "??"),
  18845. // Party related
  18846. BUILDIN_DEF(party_create,"s???"),
  18847. BUILDIN_DEF(party_addmember,"ii"),
  18848. BUILDIN_DEF(party_delmember,"??"),
  18849. BUILDIN_DEF(party_changeleader,"ii"),
  18850. BUILDIN_DEF(party_changeoption,"iii"),
  18851. BUILDIN_DEF(party_destroy,"i"),
  18852. BUILDIN_DEF(is_clientver,"ii?"),
  18853. BUILDIN_DEF2(montransform, "transform", "vi?????"), // Monster Transform [malufett/Hercules]
  18854. BUILDIN_DEF(vip_status,"i?"),
  18855. BUILDIN_DEF(vip_time,"i?"),
  18856. BUILDIN_DEF(bonus_script,"si????"),
  18857. BUILDIN_DEF(bonus_script_clear,"??"),
  18858. BUILDIN_DEF(getgroupitem,"i?"),
  18859. BUILDIN_DEF(enable_command,""),
  18860. BUILDIN_DEF(disable_command,""),
  18861. BUILDIN_DEF(getguildmember,"i??"),
  18862. BUILDIN_DEF(addspiritball,"ii?"),
  18863. BUILDIN_DEF(delspiritball,"i?"),
  18864. BUILDIN_DEF(countspiritball,"?"),
  18865. BUILDIN_DEF(mergeitem,"?"),
  18866. BUILDIN_DEF(mergeitem2,"??"),
  18867. BUILDIN_DEF(npcshopupdate,"sii?"),
  18868. BUILDIN_DEF(getattachedrid,""),
  18869. BUILDIN_DEF(getvar,"vi"),
  18870. BUILDIN_DEF(showscript,"s?"),
  18871. BUILDIN_DEF(ignoretimeout,"i?"),
  18872. BUILDIN_DEF(geteleminfo,"i?"),
  18873. BUILDIN_DEF(setquestinfo_level,"iii"),
  18874. BUILDIN_DEF(setquestinfo_req,"iii*"),
  18875. BUILDIN_DEF(setquestinfo_job,"ii*"),
  18876. BUILDIN_DEF(opendressroom,"?"),
  18877. BUILDIN_DEF(closedressroom,"?"),
  18878. #include "../custom/script_def.inc"
  18879. {NULL,NULL,NULL},
  18880. };