script.c 538 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353133541335513356133571335813359133601336113362133631336413365133661336713368133691337013371133721337313374133751337613377133781337913380133811338213383133841338513386133871338813389133901339113392133931339413395133961339713398133991340013401134021340313404134051340613407134081340913410134111341213413134141341513416134171341813419134201342113422134231342413425134261342713428134291343013431134321343313434134351343613437134381343913440134411344213443134441344513446134471344813449134501345113452134531345413455134561345713458134591346013461134621346313464134651346613467134681346913470134711347213473134741347513476134771347813479134801348113482134831348413485134861348713488134891349013491134921349313494134951349613497134981349913500135011350213503135041350513506135071350813509135101351113512135131351413515135161351713518135191352013521135221352313524135251352613527135281352913530135311353213533135341353513536135371353813539135401354113542135431354413545135461354713548135491355013551135521355313554135551355613557135581355913560135611356213563135641356513566135671356813569135701357113572135731357413575135761357713578135791358013581135821358313584135851358613587135881358913590135911359213593135941359513596135971359813599136001360113602136031360413605136061360713608136091361013611136121361313614136151361613617136181361913620136211362213623136241362513626136271362813629136301363113632136331363413635136361363713638136391364013641136421364313644136451364613647136481364913650136511365213653136541365513656136571365813659136601366113662136631366413665136661366713668136691367013671136721367313674136751367613677136781367913680136811368213683136841368513686136871368813689136901369113692136931369413695136961369713698136991370013701137021370313704137051370613707137081370913710137111371213713137141371513716137171371813719137201372113722137231372413725137261372713728137291373013731137321373313734137351373613737137381373913740137411374213743137441374513746137471374813749137501375113752137531375413755137561375713758137591376013761137621376313764137651376613767137681376913770137711377213773137741377513776137771377813779137801378113782137831378413785137861378713788137891379013791137921379313794137951379613797137981379913800138011380213803138041380513806138071380813809138101381113812138131381413815138161381713818138191382013821138221382313824138251382613827138281382913830138311383213833138341383513836138371383813839138401384113842138431384413845138461384713848138491385013851138521385313854138551385613857138581385913860138611386213863138641386513866138671386813869138701387113872138731387413875138761387713878138791388013881138821388313884138851388613887138881388913890138911389213893138941389513896138971389813899139001390113902139031390413905139061390713908139091391013911139121391313914139151391613917139181391913920139211392213923139241392513926139271392813929139301393113932139331393413935139361393713938139391394013941139421394313944139451394613947139481394913950139511395213953139541395513956139571395813959139601396113962139631396413965139661396713968139691397013971139721397313974139751397613977139781397913980139811398213983139841398513986139871398813989139901399113992139931399413995139961399713998139991400014001140021400314004140051400614007140081400914010140111401214013140141401514016140171401814019140201402114022140231402414025140261402714028140291403014031140321403314034140351403614037140381403914040140411404214043140441404514046140471404814049140501405114052140531405414055140561405714058140591406014061140621406314064140651406614067140681406914070140711407214073140741407514076140771407814079140801408114082140831408414085140861408714088140891409014091140921409314094140951409614097140981409914100141011410214103141041410514106141071410814109141101411114112141131411414115141161411714118141191412014121141221412314124141251412614127141281412914130141311413214133141341413514136141371413814139141401414114142141431414414145141461414714148141491415014151141521415314154141551415614157141581415914160141611416214163141641416514166141671416814169141701417114172141731417414175141761417714178141791418014181141821418314184141851418614187141881418914190141911419214193141941419514196141971419814199142001420114202142031420414205142061420714208142091421014211142121421314214142151421614217142181421914220142211422214223142241422514226142271422814229142301423114232142331423414235142361423714238142391424014241142421424314244142451424614247142481424914250142511425214253142541425514256142571425814259142601426114262142631426414265142661426714268142691427014271142721427314274142751427614277142781427914280142811428214283142841428514286142871428814289142901429114292142931429414295142961429714298142991430014301143021430314304143051430614307143081430914310143111431214313143141431514316143171431814319143201432114322143231432414325143261432714328143291433014331143321433314334143351433614337143381433914340143411434214343143441434514346143471434814349143501435114352143531435414355143561435714358143591436014361143621436314364143651436614367143681436914370143711437214373143741437514376143771437814379143801438114382143831438414385143861438714388143891439014391143921439314394143951439614397143981439914400144011440214403144041440514406144071440814409144101441114412144131441414415144161441714418144191442014421144221442314424144251442614427144281442914430144311443214433144341443514436144371443814439144401444114442144431444414445144461444714448144491445014451144521445314454144551445614457144581445914460144611446214463144641446514466144671446814469144701447114472144731447414475144761447714478144791448014481144821448314484144851448614487144881448914490144911449214493144941449514496144971449814499145001450114502145031450414505145061450714508145091451014511145121451314514145151451614517145181451914520145211452214523145241452514526145271452814529145301453114532145331453414535145361453714538145391454014541145421454314544145451454614547145481454914550145511455214553145541455514556145571455814559145601456114562145631456414565145661456714568145691457014571145721457314574145751457614577145781457914580145811458214583145841458514586145871458814589145901459114592145931459414595145961459714598145991460014601146021460314604146051460614607146081460914610146111461214613146141461514616146171461814619146201462114622146231462414625146261462714628146291463014631146321463314634146351463614637146381463914640146411464214643146441464514646146471464814649146501465114652146531465414655146561465714658146591466014661146621466314664146651466614667146681466914670146711467214673146741467514676146771467814679146801468114682146831468414685146861468714688146891469014691146921469314694146951469614697146981469914700147011470214703147041470514706147071470814709147101471114712147131471414715147161471714718147191472014721147221472314724147251472614727147281472914730147311473214733147341473514736147371473814739147401474114742147431474414745147461474714748147491475014751147521475314754147551475614757147581475914760147611476214763147641476514766147671476814769147701477114772147731477414775147761477714778147791478014781147821478314784147851478614787147881478914790147911479214793147941479514796147971479814799148001480114802148031480414805148061480714808148091481014811148121481314814148151481614817148181481914820148211482214823148241482514826148271482814829148301483114832148331483414835148361483714838148391484014841148421484314844148451484614847148481484914850148511485214853148541485514856148571485814859148601486114862148631486414865148661486714868148691487014871148721487314874148751487614877148781487914880148811488214883148841488514886148871488814889148901489114892148931489414895148961489714898148991490014901149021490314904149051490614907149081490914910149111491214913149141491514916149171491814919149201492114922149231492414925149261492714928149291493014931149321493314934149351493614937149381493914940149411494214943149441494514946149471494814949149501495114952149531495414955149561495714958149591496014961149621496314964149651496614967149681496914970149711497214973149741497514976149771497814979149801498114982149831498414985149861498714988149891499014991149921499314994149951499614997149981499915000150011500215003150041500515006150071500815009150101501115012150131501415015150161501715018150191502015021150221502315024150251502615027150281502915030150311503215033150341503515036150371503815039150401504115042150431504415045150461504715048150491505015051150521505315054150551505615057150581505915060150611506215063150641506515066150671506815069150701507115072150731507415075150761507715078150791508015081150821508315084150851508615087150881508915090150911509215093150941509515096150971509815099151001510115102151031510415105151061510715108151091511015111151121511315114151151511615117151181511915120151211512215123151241512515126151271512815129151301513115132151331513415135151361513715138151391514015141151421514315144151451514615147151481514915150151511515215153151541515515156151571515815159151601516115162151631516415165151661516715168151691517015171151721517315174151751517615177151781517915180151811518215183151841518515186151871518815189151901519115192151931519415195151961519715198151991520015201152021520315204152051520615207152081520915210152111521215213152141521515216152171521815219152201522115222152231522415225152261522715228152291523015231152321523315234152351523615237152381523915240152411524215243152441524515246152471524815249152501525115252152531525415255152561525715258152591526015261152621526315264152651526615267152681526915270152711527215273152741527515276152771527815279152801528115282152831528415285152861528715288152891529015291152921529315294152951529615297152981529915300153011530215303153041530515306153071530815309153101531115312153131531415315153161531715318153191532015321153221532315324153251532615327153281532915330153311533215333153341533515336153371533815339153401534115342153431534415345153461534715348153491535015351153521535315354153551535615357153581535915360153611536215363153641536515366153671536815369153701537115372153731537415375153761537715378153791538015381153821538315384153851538615387153881538915390153911539215393153941539515396153971539815399154001540115402154031540415405154061540715408154091541015411154121541315414154151541615417154181541915420154211542215423154241542515426154271542815429154301543115432154331543415435154361543715438154391544015441154421544315444154451544615447154481544915450154511545215453154541545515456154571545815459154601546115462154631546415465154661546715468154691547015471154721547315474154751547615477154781547915480154811548215483154841548515486154871548815489154901549115492154931549415495154961549715498154991550015501155021550315504155051550615507155081550915510155111551215513155141551515516155171551815519155201552115522155231552415525155261552715528155291553015531155321553315534155351553615537155381553915540155411554215543155441554515546155471554815549155501555115552155531555415555155561555715558155591556015561155621556315564155651556615567155681556915570155711557215573155741557515576155771557815579155801558115582155831558415585155861558715588155891559015591155921559315594155951559615597155981559915600156011560215603156041560515606156071560815609156101561115612156131561415615156161561715618156191562015621156221562315624156251562615627156281562915630156311563215633156341563515636156371563815639156401564115642156431564415645156461564715648156491565015651156521565315654156551565615657156581565915660156611566215663156641566515666156671566815669156701567115672156731567415675156761567715678156791568015681156821568315684156851568615687156881568915690156911569215693156941569515696156971569815699157001570115702157031570415705157061570715708157091571015711157121571315714157151571615717157181571915720157211572215723157241572515726157271572815729157301573115732157331573415735157361573715738157391574015741157421574315744157451574615747157481574915750157511575215753157541575515756157571575815759157601576115762157631576415765157661576715768157691577015771157721577315774157751577615777157781577915780157811578215783157841578515786157871578815789157901579115792157931579415795157961579715798157991580015801158021580315804158051580615807158081580915810158111581215813158141581515816158171581815819158201582115822158231582415825158261582715828158291583015831158321583315834158351583615837158381583915840158411584215843158441584515846158471584815849158501585115852158531585415855158561585715858158591586015861158621586315864158651586615867158681586915870158711587215873158741587515876158771587815879158801588115882158831588415885158861588715888158891589015891158921589315894158951589615897158981589915900159011590215903159041590515906159071590815909159101591115912159131591415915159161591715918159191592015921159221592315924159251592615927159281592915930159311593215933159341593515936159371593815939159401594115942159431594415945159461594715948159491595015951159521595315954159551595615957159581595915960159611596215963159641596515966159671596815969159701597115972159731597415975159761597715978159791598015981159821598315984159851598615987159881598915990159911599215993159941599515996159971599815999160001600116002160031600416005160061600716008160091601016011160121601316014160151601616017160181601916020160211602216023160241602516026160271602816029160301603116032160331603416035160361603716038160391604016041160421604316044160451604616047160481604916050160511605216053160541605516056160571605816059160601606116062160631606416065160661606716068160691607016071160721607316074160751607616077160781607916080160811608216083160841608516086160871608816089160901609116092160931609416095160961609716098160991610016101161021610316104161051610616107161081610916110161111611216113161141611516116161171611816119161201612116122161231612416125161261612716128161291613016131161321613316134161351613616137161381613916140161411614216143161441614516146161471614816149161501615116152161531615416155161561615716158161591616016161161621616316164161651616616167161681616916170161711617216173161741617516176161771617816179161801618116182161831618416185161861618716188161891619016191161921619316194161951619616197161981619916200162011620216203162041620516206162071620816209162101621116212162131621416215162161621716218162191622016221162221622316224162251622616227162281622916230162311623216233162341623516236162371623816239162401624116242162431624416245162461624716248162491625016251162521625316254162551625616257162581625916260162611626216263162641626516266162671626816269162701627116272162731627416275162761627716278162791628016281162821628316284162851628616287162881628916290162911629216293162941629516296162971629816299163001630116302163031630416305163061630716308163091631016311163121631316314163151631616317163181631916320163211632216323163241632516326163271632816329163301633116332163331633416335163361633716338163391634016341163421634316344163451634616347163481634916350163511635216353163541635516356163571635816359163601636116362163631636416365163661636716368163691637016371163721637316374163751637616377163781637916380163811638216383163841638516386163871638816389163901639116392163931639416395163961639716398163991640016401164021640316404164051640616407164081640916410164111641216413164141641516416164171641816419164201642116422164231642416425164261642716428164291643016431164321643316434164351643616437164381643916440164411644216443164441644516446164471644816449164501645116452164531645416455164561645716458164591646016461164621646316464164651646616467164681646916470164711647216473164741647516476164771647816479164801648116482164831648416485164861648716488164891649016491164921649316494164951649616497164981649916500165011650216503165041650516506165071650816509165101651116512165131651416515165161651716518165191652016521165221652316524165251652616527165281652916530165311653216533165341653516536165371653816539165401654116542165431654416545165461654716548165491655016551165521655316554165551655616557165581655916560165611656216563165641656516566165671656816569165701657116572165731657416575165761657716578165791658016581165821658316584165851658616587165881658916590165911659216593165941659516596165971659816599166001660116602166031660416605166061660716608166091661016611166121661316614166151661616617166181661916620166211662216623166241662516626166271662816629166301663116632166331663416635166361663716638166391664016641166421664316644166451664616647166481664916650166511665216653166541665516656166571665816659166601666116662166631666416665166661666716668166691667016671166721667316674166751667616677166781667916680166811668216683166841668516686166871668816689166901669116692166931669416695166961669716698166991670016701167021670316704167051670616707167081670916710167111671216713167141671516716167171671816719167201672116722167231672416725167261672716728167291673016731167321673316734167351673616737167381673916740167411674216743167441674516746167471674816749167501675116752167531675416755167561675716758167591676016761167621676316764167651676616767167681676916770167711677216773167741677516776167771677816779167801678116782167831678416785167861678716788167891679016791167921679316794167951679616797167981679916800168011680216803168041680516806168071680816809168101681116812168131681416815168161681716818168191682016821168221682316824168251682616827168281682916830168311683216833168341683516836168371683816839168401684116842168431684416845168461684716848168491685016851168521685316854168551685616857168581685916860168611686216863168641686516866168671686816869168701687116872168731687416875168761687716878168791688016881168821688316884168851688616887168881688916890168911689216893168941689516896168971689816899169001690116902169031690416905169061690716908169091691016911169121691316914169151691616917169181691916920169211692216923169241692516926169271692816929169301693116932169331693416935169361693716938169391694016941169421694316944169451694616947169481694916950169511695216953169541695516956169571695816959169601696116962169631696416965169661696716968169691697016971169721697316974169751697616977169781697916980169811698216983169841698516986169871698816989169901699116992169931699416995169961699716998169991700017001170021700317004170051700617007170081700917010170111701217013170141701517016170171701817019170201702117022170231702417025170261702717028170291703017031170321703317034170351703617037170381703917040170411704217043170441704517046170471704817049170501705117052170531705417055170561705717058170591706017061170621706317064170651706617067170681706917070170711707217073170741707517076170771707817079170801708117082170831708417085170861708717088170891709017091170921709317094170951709617097170981709917100171011710217103171041710517106171071710817109171101711117112171131711417115171161711717118171191712017121171221712317124171251712617127171281712917130171311713217133171341713517136171371713817139171401714117142171431714417145171461714717148171491715017151171521715317154171551715617157171581715917160171611716217163171641716517166171671716817169171701717117172171731717417175171761717717178171791718017181171821718317184171851718617187171881718917190171911719217193171941719517196171971719817199172001720117202172031720417205172061720717208172091721017211172121721317214172151721617217172181721917220172211722217223172241722517226172271722817229172301723117232172331723417235172361723717238172391724017241172421724317244172451724617247172481724917250172511725217253172541725517256172571725817259172601726117262172631726417265172661726717268172691727017271172721727317274172751727617277172781727917280172811728217283172841728517286172871728817289172901729117292172931729417295172961729717298172991730017301173021730317304173051730617307173081730917310173111731217313173141731517316173171731817319173201732117322173231732417325173261732717328173291733017331173321733317334173351733617337173381733917340173411734217343173441734517346173471734817349173501735117352173531735417355173561735717358173591736017361173621736317364173651736617367173681736917370173711737217373173741737517376173771737817379173801738117382173831738417385173861738717388173891739017391173921739317394173951739617397173981739917400174011740217403174041740517406174071740817409174101741117412174131741417415174161741717418174191742017421174221742317424174251742617427174281742917430174311743217433174341743517436174371743817439174401744117442174431744417445174461744717448174491745017451174521745317454174551745617457174581745917460174611746217463174641746517466174671746817469174701747117472174731747417475174761747717478174791748017481174821748317484174851748617487174881748917490174911749217493174941749517496174971749817499175001750117502175031750417505175061750717508175091751017511175121751317514175151751617517175181751917520175211752217523175241752517526175271752817529175301753117532175331753417535175361753717538175391754017541175421754317544175451754617547175481754917550175511755217553175541755517556175571755817559175601756117562175631756417565175661756717568175691757017571175721757317574175751757617577175781757917580175811758217583175841758517586175871758817589175901759117592175931759417595175961759717598175991760017601176021760317604176051760617607176081760917610176111761217613176141761517616176171761817619176201762117622176231762417625176261762717628176291763017631176321763317634176351763617637176381763917640176411764217643176441764517646176471764817649176501765117652176531765417655176561765717658176591766017661176621766317664176651766617667176681766917670176711767217673176741767517676176771767817679176801768117682176831768417685176861768717688176891769017691176921769317694176951769617697176981769917700177011770217703177041770517706177071770817709177101771117712177131771417715177161771717718177191772017721177221772317724177251772617727177281772917730177311773217733177341773517736177371773817739177401774117742177431774417745177461774717748177491775017751177521775317754177551775617757177581775917760177611776217763177641776517766177671776817769177701777117772177731777417775177761777717778177791778017781177821778317784177851778617787177881778917790177911779217793177941779517796177971779817799178001780117802178031780417805178061780717808178091781017811178121781317814178151781617817178181781917820178211782217823178241782517826178271782817829178301783117832178331783417835178361783717838178391784017841178421784317844178451784617847178481784917850178511785217853178541785517856178571785817859178601786117862178631786417865178661786717868178691787017871178721787317874178751787617877178781787917880178811788217883178841788517886178871788817889178901789117892178931789417895178961789717898178991790017901179021790317904179051790617907179081790917910179111791217913179141791517916179171791817919179201792117922179231792417925179261792717928179291793017931179321793317934179351793617937179381793917940179411794217943179441794517946179471794817949179501795117952179531795417955179561795717958179591796017961179621796317964179651796617967179681796917970179711797217973179741797517976179771797817979179801798117982179831798417985179861798717988179891799017991179921799317994179951799617997179981799918000180011800218003180041800518006180071800818009180101801118012180131801418015180161801718018180191802018021180221802318024180251802618027180281802918030180311803218033180341803518036180371803818039180401804118042180431804418045180461804718048180491805018051180521805318054180551805618057180581805918060180611806218063180641806518066180671806818069180701807118072180731807418075180761807718078180791808018081180821808318084180851808618087180881808918090180911809218093180941809518096180971809818099181001810118102181031810418105181061810718108181091811018111181121811318114181151811618117181181811918120181211812218123181241812518126181271812818129181301813118132181331813418135181361813718138181391814018141181421814318144181451814618147181481814918150181511815218153181541815518156181571815818159181601816118162181631816418165181661816718168181691817018171181721817318174181751817618177181781817918180181811818218183181841818518186181871818818189181901819118192181931819418195181961819718198181991820018201182021820318204182051820618207182081820918210182111821218213182141821518216182171821818219182201822118222182231822418225182261822718228182291823018231182321823318234182351823618237182381823918240182411824218243182441824518246182471824818249182501825118252182531825418255182561825718258182591826018261182621826318264182651826618267182681826918270182711827218273182741827518276182771827818279182801828118282182831828418285182861828718288182891829018291182921829318294182951829618297182981829918300183011830218303183041830518306183071830818309183101831118312183131831418315183161831718318183191832018321183221832318324183251832618327183281832918330183311833218333183341833518336183371833818339183401834118342183431834418345183461834718348183491835018351183521835318354183551835618357183581835918360183611836218363183641836518366183671836818369183701837118372183731837418375183761837718378183791838018381183821838318384183851838618387183881838918390183911839218393183941839518396183971839818399184001840118402184031840418405184061840718408184091841018411184121841318414184151841618417184181841918420184211842218423184241842518426184271842818429184301843118432184331843418435184361843718438184391844018441184421844318444184451844618447184481844918450184511845218453184541845518456184571845818459184601846118462184631846418465184661846718468184691847018471184721847318474184751847618477184781847918480184811848218483184841848518486184871848818489184901849118492184931849418495184961849718498184991850018501185021850318504185051850618507185081850918510185111851218513185141851518516185171851818519185201852118522185231852418525185261852718528185291853018531185321853318534185351853618537185381853918540185411854218543185441854518546185471854818549185501855118552185531855418555185561855718558185591856018561185621856318564185651856618567185681856918570185711857218573185741857518576185771857818579185801858118582185831858418585185861858718588185891859018591185921859318594185951859618597185981859918600186011860218603186041860518606186071860818609186101861118612186131861418615186161861718618186191862018621186221862318624186251862618627186281862918630186311863218633186341863518636186371863818639186401864118642186431864418645186461864718648186491865018651186521865318654186551865618657186581865918660186611866218663186641866518666186671866818669186701867118672186731867418675186761867718678186791868018681186821868318684186851868618687186881868918690186911869218693186941869518696186971869818699187001870118702187031870418705187061870718708187091871018711187121871318714187151871618717187181871918720187211872218723187241872518726187271872818729187301873118732187331873418735187361873718738187391874018741187421874318744187451874618747187481874918750187511875218753187541875518756187571875818759187601876118762187631876418765187661876718768187691877018771187721877318774187751877618777187781877918780187811878218783187841878518786187871878818789187901879118792187931879418795187961879718798187991880018801188021880318804188051880618807188081880918810188111881218813188141881518816188171881818819188201882118822188231882418825188261882718828188291883018831188321883318834188351883618837188381883918840188411884218843188441884518846188471884818849188501885118852188531885418855188561885718858188591886018861188621886318864188651886618867188681886918870188711887218873188741887518876188771887818879188801888118882188831888418885188861888718888188891889018891188921889318894188951889618897188981889918900189011890218903189041890518906189071890818909189101891118912189131891418915189161891718918189191892018921189221892318924189251892618927189281892918930189311893218933189341893518936189371893818939189401894118942189431894418945189461894718948189491895018951189521895318954189551895618957189581895918960189611896218963189641896518966189671896818969189701897118972189731897418975189761897718978189791898018981189821898318984189851898618987189881898918990189911899218993189941899518996189971899818999190001900119002190031900419005190061900719008190091901019011190121901319014190151901619017190181901919020190211902219023190241902519026190271902819029190301903119032190331903419035190361903719038190391904019041190421904319044190451904619047190481904919050190511905219053190541905519056190571905819059190601906119062190631906419065190661906719068190691907019071190721907319074190751907619077190781907919080190811908219083190841908519086190871908819089190901909119092190931909419095190961909719098190991910019101191021910319104191051910619107191081910919110191111911219113191141911519116191171911819119191201912119122191231912419125191261912719128191291913019131191321913319134191351913619137191381913919140191411914219143191441914519146191471914819149191501915119152191531915419155191561915719158191591916019161191621916319164191651916619167191681916919170191711917219173191741917519176191771917819179191801918119182191831918419185191861918719188191891919019191191921919319194191951919619197191981919919200192011920219203192041920519206192071920819209192101921119212192131921419215192161921719218192191922019221192221922319224192251922619227192281922919230192311923219233192341923519236192371923819239192401924119242192431924419245192461924719248192491925019251192521925319254192551925619257192581925919260192611926219263192641926519266192671926819269192701927119272192731927419275192761927719278192791928019281192821928319284192851928619287192881928919290192911929219293192941929519296192971929819299193001930119302193031930419305193061930719308193091931019311193121931319314193151931619317193181931919320193211932219323193241932519326193271932819329193301933119332193331933419335193361933719338193391934019341193421934319344193451934619347193481934919350193511935219353193541935519356193571935819359193601936119362193631936419365193661936719368193691937019371193721937319374193751937619377193781937919380193811938219383193841938519386193871938819389193901939119392193931939419395193961939719398193991940019401194021940319404194051940619407194081940919410194111941219413194141941519416194171941819419194201942119422194231942419425194261942719428194291943019431194321943319434194351943619437194381943919440194411944219443194441944519446194471944819449194501945119452194531945419455194561945719458194591946019461194621946319464194651946619467194681946919470194711947219473194741947519476194771947819479194801948119482194831948419485194861948719488194891949019491194921949319494194951949619497194981949919500195011950219503195041950519506195071950819509195101951119512195131951419515195161951719518195191952019521195221952319524195251952619527195281952919530195311953219533195341953519536195371953819539195401954119542195431954419545195461954719548195491955019551195521955319554195551955619557195581955919560195611956219563195641956519566195671956819569195701957119572195731957419575195761957719578195791958019581195821958319584195851958619587195881958919590195911959219593195941959519596195971959819599196001960119602196031960419605196061960719608196091961019611196121961319614196151961619617196181961919620196211962219623196241962519626196271962819629196301963119632196331963419635196361963719638196391964019641196421964319644196451964619647196481964919650196511965219653196541965519656196571965819659196601966119662196631966419665196661966719668196691967019671196721967319674196751967619677196781967919680196811968219683196841968519686196871968819689196901969119692196931969419695196961969719698196991970019701197021970319704197051970619707197081970919710197111971219713197141971519716197171971819719197201972119722197231972419725197261972719728197291973019731197321973319734197351973619737197381973919740197411974219743197441974519746197471974819749197501975119752197531975419755197561975719758197591976019761197621976319764197651976619767197681976919770197711977219773197741977519776197771977819779197801978119782197831978419785197861978719788197891979019791197921979319794197951979619797197981979919800198011980219803198041980519806198071980819809198101981119812198131981419815198161981719818198191982019821198221982319824198251982619827198281982919830198311983219833198341983519836198371983819839198401984119842198431984419845198461984719848198491985019851198521985319854198551985619857198581985919860198611986219863198641986519866198671986819869198701987119872198731987419875198761987719878198791988019881198821988319884198851988619887198881988919890198911989219893198941989519896198971989819899199001990119902199031990419905199061990719908199091991019911199121991319914199151991619917199181991919920199211992219923199241992519926199271992819929199301993119932199331993419935199361993719938199391994019941199421994319944199451994619947199481994919950199511995219953199541995519956199571995819959199601996119962199631996419965199661996719968199691997019971199721997319974199751997619977199781997919980199811998219983199841998519986199871998819989199901999119992199931999419995199961999719998199992000020001200022000320004200052000620007200082000920010200112001220013200142001520016200172001820019200202002120022200232002420025200262002720028200292003020031200322003320034200352003620037200382003920040200412004220043200442004520046200472004820049200502005120052200532005420055200562005720058200592006020061200622006320064200652006620067200682006920070200712007220073200742007520076200772007820079200802008120082200832008420085200862008720088200892009020091200922009320094200952009620097200982009920100201012010220103201042010520106201072010820109201102011120112201132011420115201162011720118201192012020121201222012320124201252012620127201282012920130201312013220133201342013520136201372013820139201402014120142201432014420145201462014720148201492015020151201522015320154201552015620157201582015920160201612016220163201642016520166201672016820169201702017120172201732017420175201762017720178201792018020181201822018320184201852018620187201882018920190201912019220193201942019520196201972019820199202002020120202202032020420205202062020720208202092021020211202122021320214202152021620217202182021920220202212022220223202242022520226202272022820229202302023120232202332023420235202362023720238202392024020241202422024320244202452024620247202482024920250202512025220253202542025520256202572025820259202602026120262202632026420265202662026720268202692027020271202722027320274202752027620277202782027920280202812028220283202842028520286202872028820289202902029120292202932029420295202962029720298202992030020301203022030320304203052030620307203082030920310203112031220313203142031520316203172031820319203202032120322203232032420325203262032720328203292033020331203322033320334203352033620337203382033920340203412034220343203442034520346203472034820349203502035120352203532035420355203562035720358203592036020361203622036320364203652036620367203682036920370203712037220373203742037520376203772037820379203802038120382203832038420385203862038720388203892039020391203922039320394203952039620397203982039920400204012040220403204042040520406204072040820409204102041120412204132041420415204162041720418204192042020421204222042320424204252042620427204282042920430204312043220433204342043520436204372043820439204402044120442204432044420445204462044720448204492045020451204522045320454204552045620457204582045920460204612046220463204642046520466204672046820469204702047120472204732047420475204762047720478204792048020481204822048320484204852048620487204882048920490204912049220493204942049520496204972049820499205002050120502205032050420505205062050720508205092051020511205122051320514205152051620517205182051920520205212052220523205242052520526205272052820529205302053120532205332053420535205362053720538205392054020541205422054320544205452054620547205482054920550205512055220553205542055520556205572055820559205602056120562205632056420565205662056720568205692057020571205722057320574205752057620577205782057920580205812058220583205842058520586205872058820589205902059120592205932059420595205962059720598205992060020601206022060320604206052060620607206082060920610206112061220613206142061520616206172061820619206202062120622206232062420625206262062720628206292063020631206322063320634206352063620637206382063920640206412064220643206442064520646206472064820649206502065120652206532065420655206562065720658206592066020661206622066320664206652066620667206682066920670206712067220673206742067520676206772067820679206802068120682206832068420685206862068720688206892069020691206922069320694206952069620697206982069920700207012070220703207042070520706207072070820709207102071120712207132071420715207162071720718207192072020721207222072320724207252072620727207282072920730207312073220733207342073520736207372073820739207402074120742207432074420745207462074720748207492075020751207522075320754207552075620757207582075920760207612076220763207642076520766207672076820769207702077120772207732077420775207762077720778207792078020781207822078320784207852078620787207882078920790207912079220793207942079520796207972079820799208002080120802208032080420805208062080720808208092081020811208122081320814208152081620817208182081920820208212082220823208242082520826208272082820829208302083120832208332083420835208362083720838208392084020841208422084320844208452084620847208482084920850208512085220853
  1. // Copyright (c) Athena Dev Teams - Licensed under GNU GPL
  2. // For more information, see LICENCE in the main folder
  3. //#define DEBUG_DISP
  4. //#define DEBUG_DISASM
  5. //#define DEBUG_RUN
  6. //#define DEBUG_HASH
  7. //#define DEBUG_DUMP_STACK
  8. #include "../common/cbasetypes.h"
  9. #include "../common/malloc.h"
  10. #include "../common/md5calc.h"
  11. #include "../common/nullpo.h"
  12. #include "../common/random.h"
  13. #include "../common/showmsg.h"
  14. #include "../common/socket.h"
  15. #include "../common/strlib.h"
  16. #include "../common/timer.h"
  17. #include "../common/utils.h"
  18. #include "map.h"
  19. #include "path.h"
  20. #include "clif.h"
  21. #include "chrif.h"
  22. #include "itemdb.h"
  23. #include "pc.h"
  24. #include "storage.h"
  25. #include "pet.h"
  26. #include "mapreg.h"
  27. #include "homunculus.h"
  28. #include "instance.h"
  29. #include "mercenary.h"
  30. #include "intif.h"
  31. #include "chat.h"
  32. #include "battleground.h"
  33. #include "party.h"
  34. #include "mail.h"
  35. #include "script.h"
  36. #include "quest.h"
  37. #include "elemental.h"
  38. #ifdef PCRE_SUPPORT
  39. #include "../../3rdparty/pcre/include/pcre.h" // preg_match
  40. #endif
  41. #include <stdio.h>
  42. #include <stdlib.h>
  43. #include <string.h>
  44. #include <math.h>
  45. #ifndef WIN32
  46. #endif
  47. #include <setjmp.h>
  48. #include <errno.h>
  49. #ifdef BETA_THREAD_TEST
  50. #include "../common/atomic.h"
  51. #include "../common/spinlock.h"
  52. #include "../common/thread.h"
  53. #include "../common/mutex.h"
  54. #endif
  55. ///////////////////////////////////////////////////////////////////////////////
  56. //## TODO possible enhancements: [FlavioJS]
  57. // - 'callfunc' supporting labels in the current npc "::LabelName"
  58. // - 'callfunc' supporting labels in other npcs "NpcName::LabelName"
  59. // - 'function FuncName;' function declarations reverting to global functions
  60. // if local label isn't found
  61. // - join callfunc and callsub's functionality
  62. // - remove dynamic allocation in add_word()
  63. // - remove GETVALUE / SETVALUE
  64. // - clean up the set_reg / set_val / setd_sub mess
  65. // - detect invalid label references at parse-time
  66. //
  67. // struct script_state* st;
  68. //
  69. /// Returns the script_data at the target index
  70. #define script_getdata(st,i) ( &((st)->stack->stack_data[(st)->start + (i)]) )
  71. /// Returns if the stack contains data at the target index
  72. #define script_hasdata(st,i) ( (st)->end > (st)->start + (i) )
  73. /// Returns the index of the last data in the stack
  74. #define script_lastdata(st) ( (st)->end - (st)->start - 1 )
  75. /// Pushes an int into the stack
  76. #define script_pushint(st,val) push_val((st)->stack, C_INT, (val))
  77. /// Pushes a string into the stack (script engine frees it automatically)
  78. #define script_pushstr(st,val) push_str((st)->stack, C_STR, (val))
  79. /// Pushes a copy of a string into the stack
  80. #define script_pushstrcopy(st,val) push_str((st)->stack, C_STR, aStrdup(val))
  81. /// Pushes a constant string into the stack (must never change or be freed)
  82. #define script_pushconststr(st,val) push_str((st)->stack, C_CONSTSTR, (val))
  83. /// Pushes a nil into the stack
  84. #define script_pushnil(st) push_val((st)->stack, C_NOP, 0)
  85. /// Pushes a copy of the data in the target index
  86. #define script_pushcopy(st,i) push_copy((st)->stack, (st)->start + (i))
  87. #define script_isstring(st,i) data_isstring(script_getdata(st,i))
  88. #define script_isint(st,i) data_isint(script_getdata(st,i))
  89. #define script_getnum(st,val) conv_num(st, script_getdata(st,val))
  90. #define script_getstr(st,val) conv_str(st, script_getdata(st,val))
  91. #define script_getref(st,val) ( script_getdata(st,val)->ref )
  92. // Returns name of currently running function
  93. #define script_getfuncname(st) ( st->funcname )
  94. // Note: "top" functions/defines use indexes relative to the top of the stack
  95. // -1 is the index of the data at the top
  96. /// Returns the script_data at the target index relative to the top of the stack
  97. #define script_getdatatop(st,i) ( &((st)->stack->stack_data[(st)->stack->sp + (i)]) )
  98. /// Pushes a copy of the data in the target index relative to the top of the stack
  99. #define script_pushcopytop(st,i) push_copy((st)->stack, (st)->stack->sp + (i))
  100. /// Removes the range of values [start,end[ relative to the top of the stack
  101. #define script_removetop(st,start,end) ( pop_stack((st), ((st)->stack->sp + (start)), (st)->stack->sp + (end)) )
  102. //
  103. // struct script_data* data;
  104. //
  105. /// Returns if the script data is a string
  106. #define data_isstring(data) ( (data)->type == C_STR || (data)->type == C_CONSTSTR )
  107. /// Returns if the script data is an int
  108. #define data_isint(data) ( (data)->type == C_INT )
  109. /// Returns if the script data is a reference
  110. #define data_isreference(data) ( (data)->type == C_NAME )
  111. /// Returns if the script data is a label
  112. #define data_islabel(data) ( (data)->type == C_POS )
  113. /// Returns if the script data is an internal script function label
  114. #define data_isfunclabel(data) ( (data)->type == C_USERFUNC_POS )
  115. /// Returns if this is a reference to a constant
  116. #define reference_toconstant(data) ( str_data[reference_getid(data)].type == C_INT )
  117. /// Returns if this a reference to a param
  118. #define reference_toparam(data) ( str_data[reference_getid(data)].type == C_PARAM )
  119. /// Returns if this a reference to a variable
  120. //##TODO confirm it's C_NAME [FlavioJS]
  121. #define reference_tovariable(data) ( str_data[reference_getid(data)].type == C_NAME )
  122. /// Returns the unique id of the reference (id and index)
  123. #define reference_getuid(data) ( (data)->u.num )
  124. /// Returns the id of the reference
  125. #define reference_getid(data) ( (int32)(reference_getuid(data) & 0x00ffffff) )
  126. /// Returns the array index of the reference
  127. #define reference_getindex(data) ( (int32)(((uint32)(reference_getuid(data) & 0xff000000)) >> 24) )
  128. /// Returns the name of the reference
  129. #define reference_getname(data) ( str_buf + str_data[reference_getid(data)].str )
  130. /// Returns the linked list of uid-value pairs of the reference (can be NULL)
  131. #define reference_getref(data) ( (data)->ref )
  132. /// Returns the value of the constant
  133. #define reference_getconstant(data) ( str_data[reference_getid(data)].val )
  134. /// Returns the type of param
  135. #define reference_getparamtype(data) ( str_data[reference_getid(data)].val )
  136. /// Composes the uid of a reference from the id and the index
  137. #define reference_uid(id,idx) ( (int32)((((uint32)(id)) & 0x00ffffff) | (((uint32)(idx)) << 24)) )
  138. #define not_server_variable(prefix) ( (prefix) != '$' && (prefix) != '.' && (prefix) != '\'')
  139. #define not_array_variable(prefix) ( (prefix) != '$' && (prefix) != '@' && (prefix) != '.' && (prefix) != '\'' )
  140. #define is_string_variable(name) ( (name)[strlen(name) - 1] == '$' )
  141. #define FETCH(n, t) \
  142. if( script_hasdata(st,n) ) \
  143. (t)=script_getnum(st,n);
  144. /// Maximum amount of elements in script arrays
  145. #define SCRIPT_MAX_ARRAYSIZE 128
  146. #define SCRIPT_CMD_SUCCESS 0 ///when a buildin cmd was correctly done
  147. #define SCRIPT_CMD_FAILURE 1 ///when an errors appear in cmd, show_debug will follow
  148. #define SCRIPT_BLOCK_SIZE 512
  149. enum { LABEL_NEXTLINE=1,LABEL_START };
  150. TBL_PC *script_rid2sd(struct script_state *st);
  151. /**
  152. * Get `sd` from a account id in `loc` param instead of attached rid
  153. * @param st Script
  154. * @param loc Location to look account id in script parameter
  155. * @param sd Variable that will be assigned
  156. * @return True if `sd` is assigned, false otherwise
  157. **/
  158. static bool script_accid2sd_(struct script_state *st, uint8 loc, struct map_session_data **sd, const char *func) {
  159. if (script_hasdata(st, loc)) {
  160. int id_ = script_getnum(st, loc);
  161. if (!(*sd = map_id2sd(id_)))
  162. ShowError("%s: Player with account id '%d' is not found.\n", func, id_);
  163. }
  164. else
  165. *sd = script_rid2sd(st);
  166. return (*sd) ? true : false;
  167. }
  168. /**
  169. * Get `sd` from a char id in `loc` param instead of attached rid
  170. * @param st Script
  171. * @param loc Location to look char id in script parameter
  172. * @param sd Variable that will be assigned
  173. * @return True if `sd` is assigned, false otherwise
  174. **/
  175. static bool script_charid2sd_(struct script_state *st, uint8 loc, struct map_session_data **sd, const char *func) {
  176. if (script_hasdata(st, loc)) {
  177. int id_ = script_getnum(st, loc);
  178. if (!(*sd = map_charid2sd(id_)))
  179. ShowError("%s: Player with char id '%d' is not found.\n", func, id_);
  180. }
  181. else
  182. *sd = script_rid2sd(st);
  183. return (*sd) ? true : false;
  184. }
  185. /**
  186. * Get `sd` from a nick in `loc` param instead of attached rid
  187. * @param st Script
  188. * @param loc Location to look nick in script parameter
  189. * @param sd Variable that will be assigned
  190. * @return True if `sd` is assigned, false otherwise
  191. **/
  192. static bool script_nick2sd_(struct script_state *st, uint8 loc, struct map_session_data **sd, const char *func) {
  193. if (script_hasdata(st, loc)) {
  194. const char *name_ = script_getstr(st, loc);
  195. if (!(*sd = map_nick2sd(name_)))
  196. ShowError("%s: Player with nick '%s' is not found.\n", func, name_);
  197. }
  198. else
  199. *sd = script_rid2sd(st);
  200. return (*sd) ? true : false;
  201. }
  202. #define script_accid2sd(loc,sd) script_accid2sd_(st,(loc),&(sd),__FUNCTION__)
  203. #define script_charid2sd(loc,sd) script_charid2sd_(st,(loc),&(sd),__FUNCTION__)
  204. #define script_nick2sd(loc,sd) script_nick2sd_(st,(loc),&(sd),__FUNCTION__)
  205. /// temporary buffer for passing around compiled bytecode
  206. /// @see add_scriptb, set_label, parse_script
  207. static unsigned char* script_buf = NULL;
  208. static int script_pos = 0, script_size = 0;
  209. static inline int GETVALUE(const unsigned char* buf, int i)
  210. {
  211. return (int)MakeDWord(MakeWord(buf[i], buf[i+1]), MakeWord(buf[i+2], 0));
  212. }
  213. static inline void SETVALUE(unsigned char* buf, int i, int n)
  214. {
  215. buf[i] = GetByte(n, 0);
  216. buf[i+1] = GetByte(n, 1);
  217. buf[i+2] = GetByte(n, 2);
  218. }
  219. // String buffer structures.
  220. // str_data stores string information
  221. static struct str_data_struct {
  222. enum c_op type;
  223. int str;
  224. int backpatch;
  225. int label;
  226. int (*func)(struct script_state *st);
  227. int val;
  228. int next;
  229. } *str_data = NULL;
  230. static int str_data_size = 0; // size of the data
  231. static int str_num = LABEL_START; // next id to be assigned
  232. // str_buf holds the strings themselves
  233. static char *str_buf;
  234. static int str_size = 0; // size of the buffer
  235. static int str_pos = 0; // next position to be assigned
  236. // Using a prime number for SCRIPT_HASH_SIZE should give better distributions
  237. #define SCRIPT_HASH_SIZE 1021
  238. int str_hash[SCRIPT_HASH_SIZE];
  239. // Specifies which string hashing method to use
  240. //#define SCRIPT_HASH_DJB2
  241. //#define SCRIPT_HASH_SDBM
  242. #define SCRIPT_HASH_ELF
  243. static DBMap* scriptlabel_db=NULL; // const char* label_name -> int script_pos
  244. static DBMap* userfunc_db=NULL; // const char* func_name -> struct script_code*
  245. static int parse_options=0;
  246. DBMap* script_get_label_db(void){ return scriptlabel_db; }
  247. DBMap* script_get_userfunc_db(void){ return userfunc_db; }
  248. // important buildin function references for usage in scripts
  249. static int buildin_set_ref = 0;
  250. static int buildin_callsub_ref = 0;
  251. static int buildin_callfunc_ref = 0;
  252. static int buildin_getelementofarray_ref = 0;
  253. // Caches compiled autoscript item code.
  254. // Note: This is not cleared when reloading itemdb.
  255. static DBMap* autobonus_db=NULL; // char* script -> char* bytecode
  256. struct Script_Config script_config = {
  257. 1, // warn_func_mismatch_argtypes
  258. 1, 65535, 2048, //warn_func_mismatch_paramnum/check_cmdcount/check_gotocount
  259. 0, INT_MAX, // input_min_value/input_max_value
  260. "OnPCDieEvent", //die_event_name
  261. "OnPCKillEvent", //kill_pc_event_name
  262. "OnNPCKillEvent", //kill_mob_event_name
  263. "OnPCLoginEvent", //login_event_name
  264. "OnPCLogoutEvent", //logout_event_name
  265. "OnPCLoadMapEvent", //loadmap_event_name
  266. "OnPCBaseLvUpEvent", //baselvup_event_name
  267. "OnPCJobLvUpEvent", //joblvup_event_name
  268. "OnPCStatCalcEvent", //stat_calc_event_name
  269. "OnTouch_", //ontouch_name (runs on first visible char to enter area, picks another char if the first char leaves)
  270. "OnTouch", //ontouch2_name (run whenever a char walks into the OnTouch area)
  271. };
  272. static jmp_buf error_jump;
  273. static char* error_msg;
  274. static const char* error_pos;
  275. static int error_report; // if the error should produce output
  276. // Used by disp_warning_message
  277. static const char* parser_current_src;
  278. static const char* parser_current_file;
  279. static int parser_current_line;
  280. // for advanced scripting support ( nested if, switch, while, for, do-while, function, etc )
  281. // [Eoe / jA 1080, 1081, 1094, 1164]
  282. enum curly_type {
  283. TYPE_NULL = 0,
  284. TYPE_IF,
  285. TYPE_SWITCH,
  286. TYPE_WHILE,
  287. TYPE_FOR,
  288. TYPE_DO,
  289. TYPE_USERFUNC,
  290. TYPE_ARGLIST // function argument list
  291. };
  292. enum e_arglist
  293. {
  294. ARGLIST_UNDEFINED = 0,
  295. ARGLIST_NO_PAREN = 1,
  296. ARGLIST_PAREN = 2,
  297. };
  298. static struct {
  299. struct {
  300. enum curly_type type;
  301. int index;
  302. int count;
  303. int flag;
  304. struct linkdb_node *case_label;
  305. } curly[256]; // Information right parenthesis
  306. int curly_count; // The number of right brackets
  307. int index; // Number of the syntax used in the script
  308. } syntax;
  309. const char* parse_curly_close(const char* p);
  310. const char* parse_syntax_close(const char* p);
  311. const char* parse_syntax_close_sub(const char* p,int* flag);
  312. const char* parse_syntax(const char* p);
  313. static int parse_syntax_for_flag = 0;
  314. extern short current_equip_item_index; //for New CARDS Scripts. It contains Inventory Index of the EQUIP_SCRIPT caller item. [Lupus]
  315. extern unsigned int current_equip_combo_pos;
  316. int potion_flag=0; //For use on Alchemist improved potions/Potion Pitcher. [Skotlex]
  317. int potion_hp=0, potion_per_hp=0, potion_sp=0, potion_per_sp=0;
  318. int potion_target=0;
  319. c_op get_com(unsigned char *script,int *pos);
  320. int get_num(unsigned char *script,int *pos);
  321. typedef struct script_function {
  322. int (*func)(struct script_state *st);
  323. const char *name;
  324. const char *arg;
  325. } script_function;
  326. extern script_function buildin_func[];
  327. static struct linkdb_node* sleep_db;// int oid -> struct script_state*
  328. #ifdef BETA_THREAD_TEST
  329. /**
  330. * MySQL Query Slave
  331. **/
  332. static SPIN_LOCK queryThreadLock;
  333. static rAthread queryThread = NULL;
  334. static ramutex queryThreadMutex = NULL;
  335. static racond queryThreadCond = NULL;
  336. static volatile int32 queryThreadTerminate = 0;
  337. struct queryThreadEntry {
  338. bool ok;
  339. bool type; /* main db or log db? */
  340. struct script_state *st;
  341. };
  342. /* Ladies and Gentleman the Manager! */
  343. struct {
  344. struct queryThreadEntry **entry;/* array of structs */
  345. int count;
  346. int timer;/* used to receive processed entries */
  347. } queryThreadData;
  348. #endif
  349. /*==========================================
  350. * (Only those needed) local declaration prototype
  351. *------------------------------------------*/
  352. const char* parse_subexpr(const char* p,int limit);
  353. int run_func(struct script_state *st);
  354. int script_instancegetid(struct script_state *st);
  355. enum {
  356. MF_NOMEMO, //0
  357. MF_NOTELEPORT,
  358. MF_NOSAVE,
  359. MF_NOBRANCH,
  360. MF_NOPENALTY,
  361. MF_NOZENYPENALTY,
  362. MF_PVP,
  363. MF_PVP_NOPARTY,
  364. MF_PVP_NOGUILD,
  365. MF_GVG,
  366. MF_GVG_NOPARTY, //10
  367. MF_NOTRADE,
  368. MF_NOSKILL,
  369. MF_NOWARP,
  370. MF_PARTYLOCK,
  371. MF_NOICEWALL,
  372. MF_SNOW,
  373. MF_FOG,
  374. MF_SAKURA,
  375. MF_LEAVES,
  376. /**
  377. * No longer available, keeping here just in case it's back someday. [Ind]
  378. **/
  379. //MF_RAIN, //20
  380. // 21 free
  381. MF_NOGO = 22,
  382. MF_CLOUDS,
  383. MF_CLOUDS2,
  384. MF_FIREWORKS,
  385. MF_GVG_CASTLE,
  386. MF_GVG_DUNGEON,
  387. MF_NIGHTENABLED,
  388. MF_NOBASEEXP,
  389. MF_NOJOBEXP, //30
  390. MF_NOMOBLOOT,
  391. MF_NOMVPLOOT,
  392. MF_NORETURN,
  393. MF_NOWARPTO,
  394. MF_NIGHTMAREDROP,
  395. MF_RESTRICTED,
  396. MF_NOCOMMAND,
  397. MF_NODROP,
  398. MF_JEXP,
  399. MF_BEXP, //40
  400. MF_NOVENDING,
  401. MF_LOADEVENT,
  402. MF_NOCHAT,
  403. MF_NOEXPPENALTY,
  404. MF_GUILDLOCK,
  405. MF_TOWN,
  406. MF_AUTOTRADE,
  407. MF_ALLOWKS,
  408. MF_MONSTER_NOTELEPORT,
  409. MF_PVP_NOCALCRANK, //50
  410. MF_BATTLEGROUND,
  411. MF_RESET,
  412. MF_CHANNELAUTOJOIN,
  413. MF_NOUSECART,
  414. MF_NOITEMCONSUMPTION,
  415. MF_SUMSTARTMIRACLE,
  416. MF_NOMINEEFFECT,
  417. MF_NOLOCKON,
  418. MF_NOTOMB,
  419. MF_SKILL_DAMAGE //60
  420. };
  421. const char* script_op2name(int op)
  422. {
  423. #define RETURN_OP_NAME(type) case type: return #type
  424. switch( op )
  425. {
  426. RETURN_OP_NAME(C_NOP);
  427. RETURN_OP_NAME(C_POS);
  428. RETURN_OP_NAME(C_INT);
  429. RETURN_OP_NAME(C_PARAM);
  430. RETURN_OP_NAME(C_FUNC);
  431. RETURN_OP_NAME(C_STR);
  432. RETURN_OP_NAME(C_CONSTSTR);
  433. RETURN_OP_NAME(C_ARG);
  434. RETURN_OP_NAME(C_NAME);
  435. RETURN_OP_NAME(C_EOL);
  436. RETURN_OP_NAME(C_RETINFO);
  437. RETURN_OP_NAME(C_USERFUNC);
  438. RETURN_OP_NAME(C_USERFUNC_POS);
  439. // operators
  440. RETURN_OP_NAME(C_OP3);
  441. RETURN_OP_NAME(C_LOR);
  442. RETURN_OP_NAME(C_LAND);
  443. RETURN_OP_NAME(C_LE);
  444. RETURN_OP_NAME(C_LT);
  445. RETURN_OP_NAME(C_GE);
  446. RETURN_OP_NAME(C_GT);
  447. RETURN_OP_NAME(C_EQ);
  448. RETURN_OP_NAME(C_NE);
  449. RETURN_OP_NAME(C_XOR);
  450. RETURN_OP_NAME(C_OR);
  451. RETURN_OP_NAME(C_AND);
  452. RETURN_OP_NAME(C_ADD);
  453. RETURN_OP_NAME(C_SUB);
  454. RETURN_OP_NAME(C_MUL);
  455. RETURN_OP_NAME(C_DIV);
  456. RETURN_OP_NAME(C_MOD);
  457. RETURN_OP_NAME(C_NEG);
  458. RETURN_OP_NAME(C_LNOT);
  459. RETURN_OP_NAME(C_NOT);
  460. RETURN_OP_NAME(C_R_SHIFT);
  461. RETURN_OP_NAME(C_L_SHIFT);
  462. default:
  463. ShowDebug("script_op2name: unexpected op=%d\n", op);
  464. return "???";
  465. }
  466. #undef RETURN_OP_NAME
  467. }
  468. #ifdef DEBUG_DUMP_STACK
  469. static void script_dump_stack(struct script_state* st)
  470. {
  471. int i;
  472. ShowMessage("\tstart = %d\n", st->start);
  473. ShowMessage("\tend = %d\n", st->end);
  474. ShowMessage("\tdefsp = %d\n", st->stack->defsp);
  475. ShowMessage("\tsp = %d\n", st->stack->sp);
  476. for( i = 0; i < st->stack->sp; ++i )
  477. {
  478. struct script_data* data = &st->stack->stack_data[i];
  479. ShowMessage("\t[%d] %s", i, script_op2name(data->type));
  480. switch( data->type )
  481. {
  482. case C_INT:
  483. case C_POS:
  484. ShowMessage(" %d\n", data->u.num);
  485. break;
  486. case C_STR:
  487. case C_CONSTSTR:
  488. ShowMessage(" \"%s\"\n", data->u.str);
  489. break;
  490. case C_NAME:
  491. 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));
  492. break;
  493. case C_RETINFO:
  494. {
  495. struct script_retinfo* ri = data->u.ri;
  496. ShowMessage(" %p {var_function=%p, script=%p, pos=%d, nargs=%d, defsp=%d}\n", ri, ri->var_function, ri->script, ri->pos, ri->nargs, ri->defsp);
  497. }
  498. break;
  499. default:
  500. ShowMessage("\n");
  501. break;
  502. }
  503. }
  504. }
  505. #endif
  506. /// Reports on the console the src of a script error.
  507. static void script_reportsrc(struct script_state *st)
  508. {
  509. struct block_list* bl;
  510. if( st->oid == 0 )
  511. return; //Can't report source.
  512. bl = map_id2bl(st->oid);
  513. if( bl == NULL )
  514. return;
  515. switch( bl->type )
  516. {
  517. case BL_NPC:
  518. if( bl->m >= 0 )
  519. ShowDebug("Source (NPC): %s at %s (%d,%d)\n", ((struct npc_data *)bl)->name, map[bl->m].name, bl->x, bl->y);
  520. else
  521. ShowDebug("Source (NPC): %s (invisible/not on a map)\n", ((struct npc_data *)bl)->name);
  522. break;
  523. default:
  524. if( bl->m >= 0 )
  525. 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);
  526. else
  527. ShowDebug("Source (Non-NPC type %d): name %s (invisible/not on a map)\n", bl->type, status_get_name(bl));
  528. break;
  529. }
  530. }
  531. /// Reports on the console information about the script data.
  532. static void script_reportdata(struct script_data* data)
  533. {
  534. if( data == NULL )
  535. return;
  536. switch( data->type )
  537. {
  538. case C_NOP:// no value
  539. ShowDebug("Data: nothing (nil)\n");
  540. break;
  541. case C_INT:// number
  542. ShowDebug("Data: number value=%d\n", data->u.num);
  543. break;
  544. case C_STR:
  545. case C_CONSTSTR:// string
  546. if( data->u.str )
  547. {
  548. ShowDebug("Data: string value=\"%s\"\n", data->u.str);
  549. }
  550. else
  551. {
  552. ShowDebug("Data: string value=NULL\n");
  553. }
  554. break;
  555. case C_NAME:// reference
  556. if( reference_tovariable(data) )
  557. {// variable
  558. const char* name = reference_getname(data);
  559. if( not_array_variable(*name) )
  560. ShowDebug("Data: variable name='%s'\n", name);
  561. else
  562. ShowDebug("Data: variable name='%s' index=%d\n", name, reference_getindex(data));
  563. }
  564. else if( reference_toconstant(data) )
  565. {// constant
  566. ShowDebug("Data: constant name='%s' value=%d\n", reference_getname(data), reference_getconstant(data));
  567. }
  568. else if( reference_toparam(data) )
  569. {// param
  570. ShowDebug("Data: param name='%s' type=%d\n", reference_getname(data), reference_getparamtype(data));
  571. }
  572. else
  573. {// ???
  574. ShowDebug("Data: reference name='%s' type=%s\n", reference_getname(data), script_op2name(data->type));
  575. ShowDebug("Please report this!!! - str_data.type=%s\n", script_op2name(str_data[reference_getid(data)].type));
  576. }
  577. break;
  578. case C_POS:// label
  579. ShowDebug("Data: label pos=%d\n", data->u.num);
  580. break;
  581. default:
  582. ShowDebug("Data: %s\n", script_op2name(data->type));
  583. break;
  584. }
  585. }
  586. /// Reports on the console information about the current built-in function.
  587. static void script_reportfunc(struct script_state* st)
  588. {
  589. int params, id;
  590. struct script_data* data;
  591. if( !script_hasdata(st,0) )
  592. {// no stack
  593. return;
  594. }
  595. data = script_getdata(st,0);
  596. if( !data_isreference(data) || str_data[reference_getid(data)].type != C_FUNC )
  597. {// script currently not executing a built-in function or corrupt stack
  598. return;
  599. }
  600. id = reference_getid(data);
  601. params = script_lastdata(st)-1;
  602. if( params > 0 )
  603. {
  604. int i;
  605. ShowDebug("Function: %s (%d parameter%s):\n", get_str(id), params, ( params == 1 ) ? "" : "s");
  606. for( i = 2; i <= script_lastdata(st); i++ )
  607. {
  608. script_reportdata(script_getdata(st,i));
  609. }
  610. }
  611. else
  612. {
  613. ShowDebug("Function: %s (no parameters)\n", get_str(id));
  614. }
  615. }
  616. /*==========================================
  617. * Output error message
  618. *------------------------------------------*/
  619. static void disp_error_message2(const char *mes,const char *pos,int report)
  620. {
  621. error_msg = aStrdup(mes);
  622. error_pos = pos;
  623. error_report = report;
  624. longjmp( error_jump, 1 );
  625. }
  626. #define disp_error_message(mes,pos) disp_error_message2(mes,pos,1)
  627. static void disp_warning_message(const char *mes, const char *pos) {
  628. script_warning(parser_current_src,parser_current_file,parser_current_line,mes,pos);
  629. }
  630. /// Checks event parameter validity
  631. static void check_event(struct script_state *st, const char *evt)
  632. {
  633. if( evt && evt[0] && !stristr(evt, "::On") )
  634. {
  635. ShowWarning("NPC event parameter deprecated! Please use 'NPCNAME::OnEVENT' instead of '%s'.\n", evt);
  636. script_reportsrc(st);
  637. }
  638. }
  639. /*==========================================
  640. * Hashes the input string
  641. *------------------------------------------*/
  642. static unsigned int calc_hash(const char* p)
  643. {
  644. unsigned int h;
  645. #if defined(SCRIPT_HASH_DJB2)
  646. h = 5381;
  647. while( *p ) // hash*33 + c
  648. h = ( h << 5 ) + h + ((unsigned char)TOLOWER(*p++));
  649. #elif defined(SCRIPT_HASH_SDBM)
  650. h = 0;
  651. while( *p ) // hash*65599 + c
  652. h = ( h << 6 ) + ( h << 16 ) - h + ((unsigned char)TOLOWER(*p++));
  653. #elif defined(SCRIPT_HASH_ELF) // UNIX ELF hash
  654. h = 0;
  655. while( *p ){
  656. unsigned int g;
  657. h = ( h << 4 ) + ((unsigned char)TOLOWER(*p++));
  658. g = h & 0xF0000000;
  659. if( g )
  660. {
  661. h ^= g >> 24;
  662. h &= ~g;
  663. }
  664. }
  665. #else // athena hash
  666. h = 0;
  667. while( *p )
  668. h = ( h << 1 ) + ( h >> 3 ) + ( h >> 5 ) + ( h >> 8 ) + (unsigned char)TOLOWER(*p++);
  669. #endif
  670. return h % SCRIPT_HASH_SIZE;
  671. }
  672. /*==========================================
  673. * str_data manipulation functions
  674. *------------------------------------------*/
  675. /// Looks up string using the provided id.
  676. const char* get_str(int id)
  677. {
  678. Assert( id >= LABEL_START && id < str_size );
  679. return str_buf+str_data[id].str;
  680. }
  681. /// Returns the uid of the string, or -1.
  682. static int search_str(const char* p)
  683. {
  684. int i;
  685. for( i = str_hash[calc_hash(p)]; i != 0; i = str_data[i].next )
  686. if( strcasecmp(get_str(i),p) == 0 )
  687. return i;
  688. return -1;
  689. }
  690. /// Stores a copy of the string and returns its id.
  691. /// If an identical string is already present, returns its id instead.
  692. int add_str(const char* p)
  693. {
  694. int h;
  695. int len;
  696. h = calc_hash(p);
  697. if( str_hash[h] == 0 )
  698. {// empty bucket, add new node here
  699. str_hash[h] = str_num;
  700. }
  701. else
  702. {// scan for end of list, or occurence of identical string
  703. int i;
  704. for( i = str_hash[h]; ; i = str_data[i].next )
  705. {
  706. if( strcasecmp(get_str(i),p) == 0 )
  707. return i; // string already in list
  708. if( str_data[i].next == 0 )
  709. break; // reached the end
  710. }
  711. // append node to end of list
  712. str_data[i].next = str_num;
  713. }
  714. // grow list if neccessary
  715. if( str_num >= str_data_size )
  716. {
  717. str_data_size += 128;
  718. RECREATE(str_data,struct str_data_struct,str_data_size);
  719. memset(str_data + (str_data_size - 128), '\0', 128);
  720. }
  721. len=(int)strlen(p);
  722. // grow string buffer if neccessary
  723. while( str_pos+len+1 >= str_size )
  724. {
  725. str_size += 256;
  726. RECREATE(str_buf,char,str_size);
  727. memset(str_buf + (str_size - 256), '\0', 256);
  728. }
  729. safestrncpy(str_buf+str_pos, p, len+1);
  730. str_data[str_num].type = C_NOP;
  731. str_data[str_num].str = str_pos;
  732. str_data[str_num].next = 0;
  733. str_data[str_num].func = NULL;
  734. str_data[str_num].backpatch = -1;
  735. str_data[str_num].label = -1;
  736. str_pos += len+1;
  737. return str_num++;
  738. }
  739. /// Appends 1 byte to the script buffer.
  740. static void add_scriptb(int a)
  741. {
  742. if( script_pos+1 >= script_size )
  743. {
  744. script_size += SCRIPT_BLOCK_SIZE;
  745. RECREATE(script_buf,unsigned char,script_size);
  746. }
  747. script_buf[script_pos++] = (uint8)(a);
  748. }
  749. /// Appends a c_op value to the script buffer.
  750. /// The value is variable-length encoded into 8-bit blocks.
  751. /// The encoding scheme is ( 01?????? )* 00??????, LSB first.
  752. /// All blocks but the last hold 7 bits of data, topmost bit is always 1 (carries).
  753. static void add_scriptc(int a)
  754. {
  755. while( a >= 0x40 )
  756. {
  757. add_scriptb((a&0x3f)|0x40);
  758. a = (a - 0x40) >> 6;
  759. }
  760. add_scriptb(a);
  761. }
  762. /// Appends an integer value to the script buffer.
  763. /// The value is variable-length encoded into 8-bit blocks.
  764. /// The encoding scheme is ( 11?????? )* 10??????, LSB first.
  765. /// All blocks but the last hold 7 bits of data, topmost bit is always 1 (carries).
  766. static void add_scripti(int a)
  767. {
  768. while( a >= 0x40 )
  769. {
  770. add_scriptb((a&0x3f)|0xc0);
  771. a = (a - 0x40) >> 6;
  772. }
  773. add_scriptb(a|0x80);
  774. }
  775. /// Appends a str_data object (label/function/variable/integer) to the script buffer.
  776. ///
  777. /// @param l The id of the str_data entry
  778. // Maximum up to 16M
  779. static void add_scriptl(int l)
  780. {
  781. int backpatch = str_data[l].backpatch;
  782. switch(str_data[l].type){
  783. case C_POS:
  784. case C_USERFUNC_POS:
  785. add_scriptc(C_POS);
  786. add_scriptb(str_data[l].label);
  787. add_scriptb(str_data[l].label>>8);
  788. add_scriptb(str_data[l].label>>16);
  789. break;
  790. case C_NOP:
  791. case C_USERFUNC:
  792. // Embedded data backpatch there is a possibility of label
  793. add_scriptc(C_NAME);
  794. str_data[l].backpatch = script_pos;
  795. add_scriptb(backpatch);
  796. add_scriptb(backpatch>>8);
  797. add_scriptb(backpatch>>16);
  798. break;
  799. case C_INT:
  800. add_scripti(abs(str_data[l].val));
  801. if( str_data[l].val < 0 ) //Notice that this is negative, from jA (Rayce)
  802. add_scriptc(C_NEG);
  803. break;
  804. default: // assume C_NAME
  805. add_scriptc(C_NAME);
  806. add_scriptb(l);
  807. add_scriptb(l>>8);
  808. add_scriptb(l>>16);
  809. break;
  810. }
  811. }
  812. /*==========================================
  813. * Resolve the label
  814. *------------------------------------------*/
  815. void set_label(int l,int pos, const char* script_pos_cur)
  816. {
  817. int i;
  818. if(str_data[l].type==C_INT || str_data[l].type==C_PARAM || str_data[l].type==C_FUNC)
  819. { //Prevent overwriting constants values, parameters and built-in functions [Skotlex]
  820. disp_error_message("set_label: invalid label name",script_pos_cur);
  821. return;
  822. }
  823. if(str_data[l].label!=-1){
  824. disp_error_message("set_label: dup label ",script_pos_cur);
  825. return;
  826. }
  827. str_data[l].type=(str_data[l].type == C_USERFUNC ? C_USERFUNC_POS : C_POS);
  828. str_data[l].label=pos;
  829. for(i=str_data[l].backpatch;i>=0 && i!=0x00ffffff;){
  830. int next=GETVALUE(script_buf,i);
  831. script_buf[i-1]=(str_data[l].type == C_USERFUNC ? C_USERFUNC_POS : C_POS);
  832. SETVALUE(script_buf,i,pos);
  833. i=next;
  834. }
  835. }
  836. /// Skips spaces and/or comments.
  837. const char* skip_space(const char* p)
  838. {
  839. if( p == NULL )
  840. return NULL;
  841. for(;;)
  842. {
  843. while( ISSPACE(*p) )
  844. ++p;
  845. if( *p == '/' && p[1] == '/' )
  846. {// line comment
  847. while(*p && *p!='\n')
  848. ++p;
  849. }
  850. else if( *p == '/' && p[1] == '*' )
  851. {// block comment
  852. p += 2;
  853. for(;;)
  854. {
  855. if( *p == '\0' ) {
  856. disp_warning_message("script:script->skip_space: end of file while parsing block comment. expected "CL_BOLD"*/"CL_NORM, p);
  857. return p;
  858. }
  859. if( *p == '*' && p[1] == '/' )
  860. {// end of block comment
  861. p += 2;
  862. break;
  863. }
  864. ++p;
  865. }
  866. }
  867. else
  868. break;
  869. }
  870. return p;
  871. }
  872. /// Skips a word.
  873. /// A word consists of undercores and/or alphanumeric characters,
  874. /// and valid variable prefixes/postfixes.
  875. static
  876. const char* skip_word(const char* p)
  877. {
  878. // prefix
  879. switch( *p )
  880. {
  881. case '@':// temporary char variable
  882. ++p; break;
  883. case '#':// account variable
  884. p += ( p[1] == '#' ? 2 : 1 ); break;
  885. case '\'':// instance variable
  886. ++p; break;
  887. case '.':// npc variable
  888. p += ( p[1] == '@' ? 2 : 1 ); break;
  889. case '$':// global variable
  890. p += ( p[1] == '@' ? 2 : 1 ); break;
  891. }
  892. while( ISALNUM(*p) || *p == '_' )
  893. ++p;
  894. // postfix
  895. if( *p == '$' )// string
  896. p++;
  897. return p;
  898. }
  899. /// Adds a word to str_data.
  900. /// @see skip_word
  901. /// @see add_str
  902. static
  903. int add_word(const char* p)
  904. {
  905. char* word;
  906. int len;
  907. int i;
  908. // Check for a word
  909. len = skip_word(p) - p;
  910. if( len == 0 )
  911. disp_error_message("script:add_word: invalid word. A word consists of undercores and/or alphanumeric characters, and valid variable prefixes/postfixes.", p);
  912. // Duplicate the word
  913. word = (char*)aMalloc(len+1);
  914. memcpy(word, p, len);
  915. word[len] = 0;
  916. // add the word
  917. i = add_str(word);
  918. aFree(word);
  919. return i;
  920. }
  921. /// Parses a function call.
  922. /// The argument list can have parenthesis or not.
  923. /// The number of arguments is checked.
  924. static
  925. const char* parse_callfunc(const char* p, int require_paren, int is_custom)
  926. {
  927. const char* p2;
  928. const char* arg=NULL;
  929. int func;
  930. func = add_word(p);
  931. if( str_data[func].type == C_FUNC ){
  932. // buildin function
  933. add_scriptl(func);
  934. add_scriptc(C_ARG);
  935. arg = buildin_func[str_data[func].val].arg;
  936. } else if( str_data[func].type == C_USERFUNC || str_data[func].type == C_USERFUNC_POS ){
  937. // script defined function
  938. add_scriptl(buildin_callsub_ref);
  939. add_scriptc(C_ARG);
  940. add_scriptl(func);
  941. arg = buildin_func[str_data[buildin_callsub_ref].val].arg;
  942. if( *arg == 0 )
  943. disp_error_message("parse_callfunc: callsub has no arguments, please review its definition",p);
  944. if( *arg != '*' )
  945. ++arg; // count func as argument
  946. } else {
  947. #ifdef SCRIPT_CALLFUNC_CHECK
  948. const char* name = get_str(func);
  949. if( !is_custom && strdb_get(userfunc_db, name) == NULL ) {
  950. #endif
  951. disp_error_message("parse_line: expect command, missing function name or calling undeclared function",p);
  952. #ifdef SCRIPT_CALLFUNC_CHECK
  953. } else {;
  954. add_scriptl(buildin_callfunc_ref);
  955. add_scriptc(C_ARG);
  956. add_scriptc(C_STR);
  957. while( *name ) add_scriptb(*name ++);
  958. add_scriptb(0);
  959. arg = buildin_func[str_data[buildin_callfunc_ref].val].arg;
  960. if( *arg != '*' ) ++ arg;
  961. }
  962. #endif
  963. }
  964. p = skip_word(p);
  965. p = skip_space(p);
  966. syntax.curly[syntax.curly_count].type = TYPE_ARGLIST;
  967. syntax.curly[syntax.curly_count].count = 0;
  968. if( *p == ';' )
  969. {// <func name> ';'
  970. syntax.curly[syntax.curly_count].flag = ARGLIST_NO_PAREN;
  971. } else if( *p == '(' && *(p2=skip_space(p+1)) == ')' )
  972. {// <func name> '(' ')'
  973. syntax.curly[syntax.curly_count].flag = ARGLIST_PAREN;
  974. p = p2;
  975. /*
  976. } else if( 0 && require_paren && *p != '(' )
  977. {// <func name>
  978. syntax.curly[syntax.curly_count].flag = ARGLIST_NO_PAREN;
  979. */
  980. } else
  981. {// <func name> <arg list>
  982. if( require_paren ){
  983. if( *p != '(' )
  984. disp_error_message("need '('",p);
  985. ++p; // skip '('
  986. syntax.curly[syntax.curly_count].flag = ARGLIST_PAREN;
  987. } else if( *p == '(' ){
  988. syntax.curly[syntax.curly_count].flag = ARGLIST_UNDEFINED;
  989. } else {
  990. syntax.curly[syntax.curly_count].flag = ARGLIST_NO_PAREN;
  991. }
  992. ++syntax.curly_count;
  993. while( *arg ) {
  994. p2=parse_subexpr(p,-1);
  995. if( p == p2 )
  996. break; // not an argument
  997. if( *arg != '*' )
  998. ++arg; // next argument
  999. p=skip_space(p2);
  1000. if( *arg == 0 || *p != ',' )
  1001. break; // no more arguments
  1002. ++p; // skip comma
  1003. }
  1004. --syntax.curly_count;
  1005. }
  1006. if( *arg && *arg != '?' && *arg != '*' )
  1007. disp_error_message2("parse_callfunc: not enough arguments, expected ','", p, script_config.warn_func_mismatch_paramnum);
  1008. if( syntax.curly[syntax.curly_count].type != TYPE_ARGLIST )
  1009. disp_error_message("parse_callfunc: DEBUG last curly is not an argument list",p);
  1010. if( syntax.curly[syntax.curly_count].flag == ARGLIST_PAREN ){
  1011. if( *p != ')' )
  1012. disp_error_message("parse_callfunc: expected ')' to close argument list",p);
  1013. ++p;
  1014. }
  1015. add_scriptc(C_FUNC);
  1016. return p;
  1017. }
  1018. /// Processes end of logical script line.
  1019. /// @param first When true, only fix up scheduling data is initialized
  1020. /// @param p Script position for error reporting in set_label
  1021. static void parse_nextline(bool first, const char* p)
  1022. {
  1023. if( !first )
  1024. {
  1025. add_scriptc(C_EOL); // mark end of line for stack cleanup
  1026. set_label(LABEL_NEXTLINE, script_pos, p); // fix up '-' labels
  1027. }
  1028. // initialize data for new '-' label fix up scheduling
  1029. str_data[LABEL_NEXTLINE].type = C_NOP;
  1030. str_data[LABEL_NEXTLINE].backpatch = -1;
  1031. str_data[LABEL_NEXTLINE].label = -1;
  1032. }
  1033. /// Parse a variable assignment using the direct equals operator
  1034. /// @param p script position where the function should run from
  1035. /// @return NULL if not a variable assignment, the new position otherwise
  1036. const char* parse_variable(const char* p) {
  1037. int word;
  1038. c_op type = C_NOP;
  1039. const char *p2 = NULL;
  1040. const char *var = p;
  1041. // skip the variable where applicable
  1042. p = skip_word(p);
  1043. p = skip_space(p);
  1044. if( p == NULL ) {// end of the line or invalid buffer
  1045. return NULL;
  1046. }
  1047. if( *p == '[' ) {// array variable so process the array as appropriate
  1048. int i,j;
  1049. for( p2 = p, i = 0, j = 1; p; ++ i ) {
  1050. if( *p ++ == ']' && --(j) == 0 ) break;
  1051. if( *p == '[' ) ++ j;
  1052. }
  1053. if( !(p = skip_space(p)) ) {// end of line or invalid characters remaining
  1054. disp_error_message("Missing right expression or closing bracket for variable.", p);
  1055. }
  1056. }
  1057. if( type == C_NOP &&
  1058. !( ( p[0] == '=' && p[1] != '=' && (type = C_EQ) ) // =
  1059. || ( p[0] == '+' && p[1] == '=' && (type = C_ADD) ) // +=
  1060. || ( p[0] == '-' && p[1] == '=' && (type = C_SUB) ) // -=
  1061. || ( p[0] == '^' && p[1] == '=' && (type = C_XOR) ) // ^=
  1062. || ( p[0] == '|' && p[1] == '=' && (type = C_OR ) ) // |=
  1063. || ( p[0] == '&' && p[1] == '=' && (type = C_AND) ) // &=
  1064. || ( p[0] == '*' && p[1] == '=' && (type = C_MUL) ) // *=
  1065. || ( p[0] == '/' && p[1] == '=' && (type = C_DIV) ) // /=
  1066. || ( p[0] == '%' && p[1] == '=' && (type = C_MOD) ) // %=
  1067. || ( p[0] == '~' && p[1] == '=' && (type = C_NOT) ) // ~=
  1068. || ( p[0] == '+' && p[1] == '+' && (type = C_ADD_PP) ) // ++
  1069. || ( p[0] == '-' && p[1] == '-' && (type = C_SUB_PP) ) // --
  1070. || ( p[0] == '<' && p[1] == '<' && p[2] == '=' && (type = C_L_SHIFT) ) // <<=
  1071. || ( p[0] == '>' && p[1] == '>' && p[2] == '=' && (type = C_R_SHIFT) ) // >>=
  1072. ) )
  1073. {// failed to find a matching operator combination so invalid
  1074. return NULL;
  1075. }
  1076. switch( type ) {
  1077. case C_EQ: {// incremental modifier
  1078. p = skip_space( &p[1] );
  1079. }
  1080. break;
  1081. case C_L_SHIFT:
  1082. case C_R_SHIFT: {// left or right shift modifier
  1083. p = skip_space( &p[3] );
  1084. }
  1085. break;
  1086. default: {// normal incremental command
  1087. p = skip_space( &p[2] );
  1088. }
  1089. }
  1090. if( p == NULL ) {// end of line or invalid buffer
  1091. return NULL;
  1092. }
  1093. // push the set function onto the stack
  1094. add_scriptl(buildin_set_ref);
  1095. add_scriptc(C_ARG);
  1096. // always append parenthesis to avoid errors
  1097. syntax.curly[syntax.curly_count].type = TYPE_ARGLIST;
  1098. syntax.curly[syntax.curly_count].count = 0;
  1099. syntax.curly[syntax.curly_count].flag = ARGLIST_PAREN;
  1100. // increment the total curly count for the position in the script
  1101. ++ syntax.curly_count;
  1102. // parse the variable currently being modified
  1103. word = add_word(var);
  1104. if( str_data[word].type == C_FUNC || str_data[word].type == C_USERFUNC || str_data[word].type == C_USERFUNC_POS )
  1105. {// cannot assign a variable which exists as a function or label
  1106. disp_error_message("Cannot modify a variable which has the same name as a function or label.", p);
  1107. }
  1108. if( p2 ) {// process the variable index
  1109. const char* p3 = NULL;
  1110. // push the getelementofarray method into the stack
  1111. add_scriptl(buildin_getelementofarray_ref);
  1112. add_scriptc(C_ARG);
  1113. add_scriptl(word);
  1114. // process the sub-expression for this assignment
  1115. p3 = parse_subexpr(p2 + 1, 1);
  1116. p3 = skip_space(p3);
  1117. if( *p3 != ']' ) {// closing parenthesis is required for this script
  1118. disp_error_message("Missing closing ']' parenthesis for the variable assignment.", p3);
  1119. }
  1120. // push the closing function stack operator onto the stack
  1121. add_scriptc(C_FUNC);
  1122. p3 ++;
  1123. } else {// simply push the variable or value onto the stack
  1124. add_scriptl(word);
  1125. }
  1126. if( type != C_EQ )
  1127. add_scriptc(C_REF);
  1128. if( type == C_ADD_PP || type == C_SUB_PP ) {// incremental operator for the method
  1129. add_scripti(1);
  1130. add_scriptc(type == C_ADD_PP ? C_ADD : C_SUB);
  1131. } else {// process the value as an expression
  1132. p = parse_subexpr(p, -1);
  1133. if( type != C_EQ )
  1134. {// push the type of modifier onto the stack
  1135. add_scriptc(type);
  1136. }
  1137. }
  1138. // decrement the curly count for the position within the script
  1139. -- syntax.curly_count;
  1140. // close the script by appending the function operator
  1141. add_scriptc(C_FUNC);
  1142. // push the buffer from the method
  1143. return p;
  1144. }
  1145. /*
  1146. * Checks whether the gives string is a number literal
  1147. *
  1148. * Mainly necessary to differentiate between number literals and NPC name
  1149. * constants, since several of those start with a digit.
  1150. *
  1151. * All this does is to check if the string begins with an optional + or - sign,
  1152. * followed by a hexadecimal or decimal number literal literal and is NOT
  1153. * followed by a underscore or letter.
  1154. *
  1155. * @author : Hercules.ws
  1156. * @param p Pointer to the string to check
  1157. * @return Whether the string is a number literal
  1158. */
  1159. bool is_number(const char *p) {
  1160. const char *np;
  1161. if (!p)
  1162. return false;
  1163. if (*p == '-' || *p == '+')
  1164. p++;
  1165. np = p;
  1166. if (*p == '0' && p[1] == 'x') {
  1167. p+=2;
  1168. np = p;
  1169. // Hexadecimal
  1170. while (ISXDIGIT(*np))
  1171. np++;
  1172. } else {
  1173. // Decimal
  1174. while (ISDIGIT(*np))
  1175. np++;
  1176. }
  1177. if (p != np && *np != '_' && !ISALPHA(*np)) // At least one digit, and next isn't a letter or _
  1178. return true;
  1179. return false;
  1180. }
  1181. /*==========================================
  1182. * Analysis section
  1183. *------------------------------------------*/
  1184. const char* parse_simpleexpr(const char *p)
  1185. {
  1186. long long i;
  1187. p=skip_space(p);
  1188. if(*p==';' || *p==',')
  1189. disp_error_message("parse_simpleexpr: unexpected end of expression",p);
  1190. if(*p=='('){
  1191. if( (i=syntax.curly_count-1) >= 0 && syntax.curly[i].type == TYPE_ARGLIST )
  1192. ++syntax.curly[i].count;
  1193. p=parse_subexpr(p+1,-1);
  1194. p=skip_space(p);
  1195. if( (i=syntax.curly_count-1) >= 0 && syntax.curly[i].type == TYPE_ARGLIST &&
  1196. syntax.curly[i].flag == ARGLIST_UNDEFINED && --syntax.curly[i].count == 0
  1197. ){
  1198. if( *p == ',' ){
  1199. syntax.curly[i].flag = ARGLIST_PAREN;
  1200. return p;
  1201. } else
  1202. syntax.curly[i].flag = ARGLIST_NO_PAREN;
  1203. }
  1204. if( *p != ')' )
  1205. disp_error_message("parse_simpleexpr: unmatched ')'",p);
  1206. ++p;
  1207. } else if(is_number(p)) {
  1208. char *np;
  1209. while(*p == '0' && ISDIGIT(p[1])) p++;
  1210. i=strtoll(p,&np,0);
  1211. if( i < INT_MIN ) {
  1212. i = INT_MIN;
  1213. disp_warning_message("parse_simpleexpr: underflow detected, capping value to INT_MIN",p);
  1214. } else if( i > INT_MAX ) {
  1215. i = INT_MAX;
  1216. disp_warning_message("parse_simpleexpr: overflow detected, capping value to INT_MAX",p);
  1217. }
  1218. add_scripti((int)i);
  1219. p=np;
  1220. } else if(*p=='"'){
  1221. add_scriptc(C_STR);
  1222. p++;
  1223. while( *p && *p != '"' ){
  1224. if( (unsigned char)p[-1] <= 0x7e && *p == '\\' )
  1225. {
  1226. char buf[8];
  1227. size_t len = skip_escaped_c(p) - p;
  1228. size_t n = sv_unescape_c(buf, p, len);
  1229. if( n != 1 )
  1230. ShowDebug("parse_simpleexpr: unexpected length %d after unescape (\"%.*s\" -> %.*s)\n", (int)n, (int)len, p, (int)n, buf);
  1231. p += len;
  1232. add_scriptb(*buf);
  1233. continue;
  1234. }
  1235. else if( *p == '\n' )
  1236. disp_error_message("parse_simpleexpr: unexpected newline @ string",p);
  1237. add_scriptb(*p++);
  1238. }
  1239. if(!*p)
  1240. disp_error_message("parse_simpleexpr: unexpected eof @ string",p);
  1241. add_scriptb(0);
  1242. p++; //'"'
  1243. } else {
  1244. int l;
  1245. const char* pv;
  1246. // label , register , function etc
  1247. if(skip_word(p)==p)
  1248. disp_error_message("parse_simpleexpr: unexpected character",p);
  1249. l=add_word(p);
  1250. if( str_data[l].type == C_FUNC || str_data[l].type == C_USERFUNC || str_data[l].type == C_USERFUNC_POS)
  1251. return parse_callfunc(p,1,0);
  1252. #ifdef SCRIPT_CALLFUNC_CHECK
  1253. else {
  1254. const char* name = get_str(l);
  1255. if( strdb_get(userfunc_db,name) != NULL ) {
  1256. return parse_callfunc(p,1,1);
  1257. }
  1258. }
  1259. #endif
  1260. if( (pv = parse_variable(p)) )
  1261. {// successfully processed a variable assignment
  1262. return pv;
  1263. }
  1264. p=skip_word(p);
  1265. if( *p == '[' ){
  1266. // array(name[i] => getelementofarray(name,i) )
  1267. add_scriptl(buildin_getelementofarray_ref);
  1268. add_scriptc(C_ARG);
  1269. add_scriptl(l);
  1270. p=parse_subexpr(p+1,-1);
  1271. p=skip_space(p);
  1272. if( *p != ']' )
  1273. disp_error_message("parse_simpleexpr: unmatched ']'",p);
  1274. ++p;
  1275. add_scriptc(C_FUNC);
  1276. }else
  1277. add_scriptl(l);
  1278. }
  1279. return p;
  1280. }
  1281. /*==========================================
  1282. * Analysis of the expression
  1283. *------------------------------------------*/
  1284. const char* parse_subexpr(const char* p,int limit)
  1285. {
  1286. int op,opl,len;
  1287. p=skip_space(p);
  1288. if( *p == '-' ){
  1289. const char* tmpp = skip_space(p+1);
  1290. if( *tmpp == ';' || *tmpp == ',' ){
  1291. add_scriptl(LABEL_NEXTLINE);
  1292. p++;
  1293. return p;
  1294. }
  1295. }
  1296. if((op=C_NEG,*p=='-') || (op=C_LNOT,*p=='!') || (op=C_NOT,*p=='~')){
  1297. p=parse_subexpr(p+1,10);
  1298. add_scriptc(op);
  1299. } else
  1300. p=parse_simpleexpr(p);
  1301. p=skip_space(p);
  1302. while((
  1303. (op=C_OP3,opl=0,len=1,*p=='?') ||
  1304. (op=C_ADD,opl=8,len=1,*p=='+') ||
  1305. (op=C_SUB,opl=8,len=1,*p=='-') ||
  1306. (op=C_MUL,opl=9,len=1,*p=='*') ||
  1307. (op=C_DIV,opl=9,len=1,*p=='/') ||
  1308. (op=C_MOD,opl=9,len=1,*p=='%') ||
  1309. (op=C_LAND,opl=2,len=2,*p=='&' && p[1]=='&') ||
  1310. (op=C_AND,opl=6,len=1,*p=='&') ||
  1311. (op=C_LOR,opl=1,len=2,*p=='|' && p[1]=='|') ||
  1312. (op=C_OR,opl=5,len=1,*p=='|') ||
  1313. (op=C_XOR,opl=4,len=1,*p=='^') ||
  1314. (op=C_EQ,opl=3,len=2,*p=='=' && p[1]=='=') ||
  1315. (op=C_NE,opl=3,len=2,*p=='!' && p[1]=='=') ||
  1316. (op=C_R_SHIFT,opl=7,len=2,*p=='>' && p[1]=='>') ||
  1317. (op=C_GE,opl=3,len=2,*p=='>' && p[1]=='=') ||
  1318. (op=C_GT,opl=3,len=1,*p=='>') ||
  1319. (op=C_L_SHIFT,opl=7,len=2,*p=='<' && p[1]=='<') ||
  1320. (op=C_LE,opl=3,len=2,*p=='<' && p[1]=='=') ||
  1321. (op=C_LT,opl=3,len=1,*p=='<')) && opl>limit){
  1322. p+=len;
  1323. if(op == C_OP3) {
  1324. p=parse_subexpr(p,-1);
  1325. p=skip_space(p);
  1326. if( *(p++) != ':')
  1327. disp_error_message("parse_subexpr: expected ':'", p-1);
  1328. p=parse_subexpr(p,-1);
  1329. } else {
  1330. p=parse_subexpr(p,opl);
  1331. }
  1332. add_scriptc(op);
  1333. p=skip_space(p);
  1334. }
  1335. return p; /* return first untreated operator */
  1336. }
  1337. /*==========================================
  1338. * Evaluation of the expression
  1339. *------------------------------------------*/
  1340. const char* parse_expr(const char *p)
  1341. {
  1342. switch(*p){
  1343. case ')': case ';': case ':': case '[': case ']':
  1344. case '}':
  1345. disp_error_message("parse_expr: unexpected character",p);
  1346. }
  1347. p=parse_subexpr(p,-1);
  1348. return p;
  1349. }
  1350. /*==========================================
  1351. * Analysis of the line
  1352. *------------------------------------------*/
  1353. const char* parse_line(const char* p)
  1354. {
  1355. const char* p2;
  1356. p=skip_space(p);
  1357. if(*p==';') {
  1358. //Close decision for if(); for(); while();
  1359. p = parse_syntax_close(p + 1);
  1360. return p;
  1361. }
  1362. if(*p==')' && parse_syntax_for_flag)
  1363. return p+1;
  1364. p = skip_space(p);
  1365. if(p[0] == '{') {
  1366. syntax.curly[syntax.curly_count].type = TYPE_NULL;
  1367. syntax.curly[syntax.curly_count].count = -1;
  1368. syntax.curly[syntax.curly_count].index = -1;
  1369. syntax.curly_count++;
  1370. return p + 1;
  1371. } else if(p[0] == '}') {
  1372. return parse_curly_close(p);
  1373. }
  1374. // Syntax-related processing
  1375. p2 = parse_syntax(p);
  1376. if(p2 != NULL)
  1377. return p2;
  1378. // attempt to process a variable assignment
  1379. p2 = parse_variable(p);
  1380. if( p2 != NULL )
  1381. {// variable assignment processed so leave the method
  1382. return parse_syntax_close(p2 + 1);
  1383. }
  1384. p = parse_callfunc(p,0,0);
  1385. p = skip_space(p);
  1386. if(parse_syntax_for_flag) {
  1387. if( *p != ')' )
  1388. disp_error_message("parse_line: expected ')'",p);
  1389. } else {
  1390. if( *p != ';' )
  1391. disp_error_message("parse_line: expected ';'",p);
  1392. }
  1393. //Binding decision for if(), for(), while()
  1394. p = parse_syntax_close(p+1);
  1395. return p;
  1396. }
  1397. // { ... } Closing process
  1398. const char* parse_curly_close(const char* p)
  1399. {
  1400. if(syntax.curly_count <= 0) {
  1401. disp_error_message("parse_curly_close: unexpected string",p);
  1402. return p + 1;
  1403. } else if(syntax.curly[syntax.curly_count-1].type == TYPE_NULL) {
  1404. syntax.curly_count--;
  1405. //Close decision if, for , while
  1406. p = parse_syntax_close(p + 1);
  1407. return p;
  1408. } else if(syntax.curly[syntax.curly_count-1].type == TYPE_SWITCH) {
  1409. //Closing switch()
  1410. int pos = syntax.curly_count-1;
  1411. char label[256];
  1412. int l;
  1413. // Remove temporary variables
  1414. sprintf(label,"set $@__SW%x_VAL,0;",syntax.curly[pos].index);
  1415. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1416. parse_line(label);
  1417. syntax.curly_count--;
  1418. // Go to the end pointer unconditionally
  1419. sprintf(label,"goto __SW%x_FIN;",syntax.curly[pos].index);
  1420. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1421. parse_line(label);
  1422. syntax.curly_count--;
  1423. // You are here labeled
  1424. sprintf(label,"__SW%x_%x",syntax.curly[pos].index,syntax.curly[pos].count);
  1425. l=add_str(label);
  1426. set_label(l,script_pos, p);
  1427. if(syntax.curly[pos].flag) {
  1428. //Exists default
  1429. sprintf(label,"goto __SW%x_DEF;",syntax.curly[pos].index);
  1430. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1431. parse_line(label);
  1432. syntax.curly_count--;
  1433. }
  1434. // Label end
  1435. sprintf(label,"__SW%x_FIN",syntax.curly[pos].index);
  1436. l=add_str(label);
  1437. set_label(l,script_pos, p);
  1438. linkdb_final(&syntax.curly[pos].case_label); // free the list of case label
  1439. syntax.curly_count--;
  1440. //Closing decision if, for , while
  1441. p = parse_syntax_close(p + 1);
  1442. return p;
  1443. } else {
  1444. disp_error_message("parse_curly_close: unexpected string",p);
  1445. return p + 1;
  1446. }
  1447. }
  1448. // Syntax-related processing
  1449. // break, case, continue, default, do, for, function,
  1450. // if, switch, while ? will handle this internally.
  1451. const char* parse_syntax(const char* p)
  1452. {
  1453. const char *p2 = skip_word(p);
  1454. switch(*p) {
  1455. case 'B':
  1456. case 'b':
  1457. if(p2 - p == 5 && !strncasecmp(p,"break",5)) {
  1458. // break Processing
  1459. char label[256];
  1460. int pos = syntax.curly_count - 1;
  1461. while(pos >= 0) {
  1462. if(syntax.curly[pos].type == TYPE_DO) {
  1463. sprintf(label,"goto __DO%x_FIN;",syntax.curly[pos].index);
  1464. break;
  1465. } else if(syntax.curly[pos].type == TYPE_FOR) {
  1466. sprintf(label,"goto __FR%x_FIN;",syntax.curly[pos].index);
  1467. break;
  1468. } else if(syntax.curly[pos].type == TYPE_WHILE) {
  1469. sprintf(label,"goto __WL%x_FIN;",syntax.curly[pos].index);
  1470. break;
  1471. } else if(syntax.curly[pos].type == TYPE_SWITCH) {
  1472. sprintf(label,"goto __SW%x_FIN;",syntax.curly[pos].index);
  1473. break;
  1474. }
  1475. pos--;
  1476. }
  1477. if(pos < 0) {
  1478. disp_error_message("parse_syntax: unexpected 'break'",p);
  1479. } else {
  1480. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1481. parse_line(label);
  1482. syntax.curly_count--;
  1483. }
  1484. p = skip_space(p2);
  1485. if(*p != ';')
  1486. disp_error_message("parse_syntax: expected ';'",p);
  1487. // Closing decision if, for , while
  1488. p = parse_syntax_close(p + 1);
  1489. return p;
  1490. }
  1491. break;
  1492. case 'c':
  1493. case 'C':
  1494. if(p2 - p == 4 && !strncasecmp(p,"case",4)) {
  1495. //Processing case
  1496. int pos = syntax.curly_count-1;
  1497. if(pos < 0 || syntax.curly[pos].type != TYPE_SWITCH) {
  1498. disp_error_message("parse_syntax: unexpected 'case' ",p);
  1499. return p+1;
  1500. } else {
  1501. char label[256];
  1502. int l,v;
  1503. char *np;
  1504. if(syntax.curly[pos].count != 1) {
  1505. //Jump for FALLTHRU
  1506. sprintf(label,"goto __SW%x_%xJ;",syntax.curly[pos].index,syntax.curly[pos].count);
  1507. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1508. parse_line(label);
  1509. syntax.curly_count--;
  1510. // You are here labeled
  1511. sprintf(label,"__SW%x_%x",syntax.curly[pos].index,syntax.curly[pos].count);
  1512. l=add_str(label);
  1513. set_label(l,script_pos, p);
  1514. }
  1515. //Decision statement switch
  1516. p = skip_space(p2);
  1517. if(p == p2) {
  1518. disp_error_message("parse_syntax: expected a space ' '",p);
  1519. }
  1520. // check whether case label is integer or not
  1521. if(is_number(p)) {
  1522. //Numeric value
  1523. v = (int)strtol(p,&np,0);
  1524. if((*p == '-' || *p == '+') && ISDIGIT(p[1])) // pre-skip because '-' can not skip_word
  1525. p++;
  1526. p = skip_word(p);
  1527. if(np != p)
  1528. disp_error_message("parse_syntax: 'case' label is not an integer",np);
  1529. } else {
  1530. //Check for constants
  1531. p2 = skip_word(p);
  1532. v = (int)(size_t) (p2-p); // length of word at p2
  1533. memcpy(label,p,v);
  1534. label[v]='\0';
  1535. if( !script_get_constant(label, &v) )
  1536. disp_error_message("parse_syntax: 'case' label is not an integer",p);
  1537. p = skip_word(p);
  1538. }
  1539. p = skip_space(p);
  1540. if(*p != ':')
  1541. disp_error_message("parse_syntax: expect ':'",p);
  1542. sprintf(label,"if(%d != $@__SW%x_VAL) goto __SW%x_%x;",
  1543. v,syntax.curly[pos].index,syntax.curly[pos].index,syntax.curly[pos].count+1);
  1544. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1545. // Bad I do not parse twice
  1546. p2 = parse_line(label);
  1547. parse_line(p2);
  1548. syntax.curly_count--;
  1549. if(syntax.curly[pos].count != 1) {
  1550. // Label after the completion of FALLTHRU
  1551. sprintf(label,"__SW%x_%xJ",syntax.curly[pos].index,syntax.curly[pos].count);
  1552. l=add_str(label);
  1553. set_label(l,script_pos,p);
  1554. }
  1555. // check duplication of case label [Rayce]
  1556. if(linkdb_search(&syntax.curly[pos].case_label, (void*)__64BPRTSIZE(v)) != NULL)
  1557. disp_error_message("parse_syntax: dup 'case'",p);
  1558. linkdb_insert(&syntax.curly[pos].case_label, (void*)__64BPRTSIZE(v), (void*)1);
  1559. sprintf(label,"set $@__SW%x_VAL,0;",syntax.curly[pos].index);
  1560. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1561. parse_line(label);
  1562. syntax.curly_count--;
  1563. syntax.curly[pos].count++;
  1564. }
  1565. return p + 1;
  1566. } else if(p2 - p == 8 && !strncasecmp(p,"continue",8)) {
  1567. // Processing continue
  1568. char label[256];
  1569. int pos = syntax.curly_count - 1;
  1570. while(pos >= 0) {
  1571. if(syntax.curly[pos].type == TYPE_DO) {
  1572. sprintf(label,"goto __DO%x_NXT;",syntax.curly[pos].index);
  1573. syntax.curly[pos].flag = 1; //Flag put the link for continue
  1574. break;
  1575. } else if(syntax.curly[pos].type == TYPE_FOR) {
  1576. sprintf(label,"goto __FR%x_NXT;",syntax.curly[pos].index);
  1577. break;
  1578. } else if(syntax.curly[pos].type == TYPE_WHILE) {
  1579. sprintf(label,"goto __WL%x_NXT;",syntax.curly[pos].index);
  1580. break;
  1581. }
  1582. pos--;
  1583. }
  1584. if(pos < 0) {
  1585. disp_error_message("parse_syntax: unexpected 'continue'",p);
  1586. } else {
  1587. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1588. parse_line(label);
  1589. syntax.curly_count--;
  1590. }
  1591. p = skip_space(p2);
  1592. if(*p != ';')
  1593. disp_error_message("parse_syntax: expected ';'",p);
  1594. //Closing decision if, for , while
  1595. p = parse_syntax_close(p + 1);
  1596. return p;
  1597. }
  1598. break;
  1599. case 'd':
  1600. case 'D':
  1601. if(p2 - p == 7 && !strncasecmp(p,"default",7)) {
  1602. // Switch - default processing
  1603. int pos = syntax.curly_count-1;
  1604. if(pos < 0 || syntax.curly[pos].type != TYPE_SWITCH) {
  1605. disp_error_message("parse_syntax: unexpected 'default'",p);
  1606. } else if(syntax.curly[pos].flag) {
  1607. disp_error_message("parse_syntax: dup 'default'",p);
  1608. } else {
  1609. char label[256];
  1610. int l;
  1611. // Put the label location
  1612. p = skip_space(p2);
  1613. if(*p != ':') {
  1614. disp_error_message("parse_syntax: expected ':'",p);
  1615. }
  1616. sprintf(label,"__SW%x_%x",syntax.curly[pos].index,syntax.curly[pos].count);
  1617. l=add_str(label);
  1618. set_label(l,script_pos,p);
  1619. // Skip to the next link w/o condition
  1620. sprintf(label,"goto __SW%x_%x;",syntax.curly[pos].index,syntax.curly[pos].count+1);
  1621. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1622. parse_line(label);
  1623. syntax.curly_count--;
  1624. // The default label
  1625. sprintf(label,"__SW%x_DEF",syntax.curly[pos].index);
  1626. l=add_str(label);
  1627. set_label(l,script_pos,p);
  1628. syntax.curly[syntax.curly_count - 1].flag = 1;
  1629. syntax.curly[pos].count++;
  1630. }
  1631. return p + 1;
  1632. } else if(p2 - p == 2 && !strncasecmp(p,"do",2)) {
  1633. int l;
  1634. char label[256];
  1635. p=skip_space(p2);
  1636. syntax.curly[syntax.curly_count].type = TYPE_DO;
  1637. syntax.curly[syntax.curly_count].count = 1;
  1638. syntax.curly[syntax.curly_count].index = syntax.index++;
  1639. syntax.curly[syntax.curly_count].flag = 0;
  1640. // Label of the (do) form here
  1641. sprintf(label,"__DO%x_BGN",syntax.curly[syntax.curly_count].index);
  1642. l=add_str(label);
  1643. set_label(l,script_pos,p);
  1644. syntax.curly_count++;
  1645. return p;
  1646. }
  1647. break;
  1648. case 'f':
  1649. case 'F':
  1650. if(p2 - p == 3 && !strncasecmp(p,"for",3)) {
  1651. int l;
  1652. char label[256];
  1653. int pos = syntax.curly_count;
  1654. syntax.curly[syntax.curly_count].type = TYPE_FOR;
  1655. syntax.curly[syntax.curly_count].count = 1;
  1656. syntax.curly[syntax.curly_count].index = syntax.index++;
  1657. syntax.curly[syntax.curly_count].flag = 0;
  1658. syntax.curly_count++;
  1659. p=skip_space(p2);
  1660. if(*p != '(')
  1661. disp_error_message("parse_syntax: expected '('",p);
  1662. p++;
  1663. // Execute the initialization statement
  1664. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1665. p=parse_line(p);
  1666. syntax.curly_count--;
  1667. // Form the start of label decision
  1668. sprintf(label,"__FR%x_J",syntax.curly[pos].index);
  1669. l=add_str(label);
  1670. set_label(l,script_pos,p);
  1671. p=skip_space(p);
  1672. if(*p == ';') {
  1673. // For (; Because the pattern of always true ;)
  1674. ;
  1675. } else {
  1676. // Skip to the end point if the condition is false
  1677. sprintf(label,"__FR%x_FIN",syntax.curly[pos].index);
  1678. add_scriptl(add_str("jump_zero"));
  1679. add_scriptc(C_ARG);
  1680. p=parse_expr(p);
  1681. p=skip_space(p);
  1682. add_scriptl(add_str(label));
  1683. add_scriptc(C_FUNC);
  1684. }
  1685. if(*p != ';')
  1686. disp_error_message("parse_syntax: expected ';'",p);
  1687. p++;
  1688. // Skip to the beginning of the loop
  1689. sprintf(label,"goto __FR%x_BGN;",syntax.curly[pos].index);
  1690. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1691. parse_line(label);
  1692. syntax.curly_count--;
  1693. // Labels to form the next loop
  1694. sprintf(label,"__FR%x_NXT",syntax.curly[pos].index);
  1695. l=add_str(label);
  1696. set_label(l,script_pos,p);
  1697. // Process the next time you enter the loop
  1698. // A ')' last for; flag to be treated as'
  1699. parse_syntax_for_flag = 1;
  1700. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1701. p=parse_line(p);
  1702. syntax.curly_count--;
  1703. parse_syntax_for_flag = 0;
  1704. // Skip to the determination process conditions
  1705. sprintf(label,"goto __FR%x_J;",syntax.curly[pos].index);
  1706. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1707. parse_line(label);
  1708. syntax.curly_count--;
  1709. // Loop start labeling
  1710. sprintf(label,"__FR%x_BGN",syntax.curly[pos].index);
  1711. l=add_str(label);
  1712. set_label(l,script_pos,p);
  1713. return p;
  1714. }
  1715. else if( p2 - p == 8 && strncasecmp(p,"function",8) == 0 )
  1716. {// internal script function
  1717. const char *func_name;
  1718. func_name = skip_space(p2);
  1719. p = skip_word(func_name);
  1720. if( p == func_name )
  1721. disp_error_message("parse_syntax:function: function name is missing or invalid", p);
  1722. p2 = skip_space(p);
  1723. if( *p2 == ';' )
  1724. {// function <name> ;
  1725. // function declaration - just register the name
  1726. int l;
  1727. l = add_word(func_name);
  1728. if( str_data[l].type == C_NOP )// register only, if the name was not used by something else
  1729. str_data[l].type = C_USERFUNC;
  1730. else if( str_data[l].type == C_USERFUNC )
  1731. ; // already registered
  1732. else
  1733. disp_error_message("parse_syntax:function: function name is invalid", func_name);
  1734. // Close condition of if, for, while
  1735. p = parse_syntax_close(p2 + 1);
  1736. return p;
  1737. }
  1738. else if(*p2 == '{')
  1739. {// function <name> <line/block of code>
  1740. char label[256];
  1741. int l;
  1742. syntax.curly[syntax.curly_count].type = TYPE_USERFUNC;
  1743. syntax.curly[syntax.curly_count].count = 1;
  1744. syntax.curly[syntax.curly_count].index = syntax.index++;
  1745. syntax.curly[syntax.curly_count].flag = 0;
  1746. ++syntax.curly_count;
  1747. // Jump over the function code
  1748. sprintf(label, "goto __FN%x_FIN;", syntax.curly[syntax.curly_count-1].index);
  1749. syntax.curly[syntax.curly_count].type = TYPE_NULL;
  1750. ++syntax.curly_count;
  1751. parse_line(label);
  1752. --syntax.curly_count;
  1753. // Set the position of the function (label)
  1754. l=add_word(func_name);
  1755. if( str_data[l].type == C_NOP || str_data[l].type == C_USERFUNC )// register only, if the name was not used by something else
  1756. {
  1757. str_data[l].type = C_USERFUNC;
  1758. set_label(l, script_pos, p);
  1759. if( parse_options&SCRIPT_USE_LABEL_DB )
  1760. strdb_iput(scriptlabel_db, get_str(l), script_pos);
  1761. }
  1762. else
  1763. disp_error_message("parse_syntax:function: function name is invalid", func_name);
  1764. return skip_space(p);
  1765. }
  1766. else
  1767. {
  1768. disp_error_message("expect ';' or '{' at function syntax",p);
  1769. }
  1770. }
  1771. break;
  1772. case 'i':
  1773. case 'I':
  1774. if(p2 - p == 2 && !strncasecmp(p,"if",2)) {
  1775. // If process
  1776. char label[256];
  1777. p=skip_space(p2);
  1778. if(*p != '(') { //Prevent if this {} non-c syntax. from Rayce (jA)
  1779. disp_error_message("need '('",p);
  1780. }
  1781. syntax.curly[syntax.curly_count].type = TYPE_IF;
  1782. syntax.curly[syntax.curly_count].count = 1;
  1783. syntax.curly[syntax.curly_count].index = syntax.index++;
  1784. syntax.curly[syntax.curly_count].flag = 0;
  1785. sprintf(label,"__IF%x_%x",syntax.curly[syntax.curly_count].index,syntax.curly[syntax.curly_count].count);
  1786. syntax.curly_count++;
  1787. add_scriptl(add_str("jump_zero"));
  1788. add_scriptc(C_ARG);
  1789. p=parse_expr(p);
  1790. p=skip_space(p);
  1791. add_scriptl(add_str(label));
  1792. add_scriptc(C_FUNC);
  1793. return p;
  1794. }
  1795. break;
  1796. case 's':
  1797. case 'S':
  1798. if(p2 - p == 6 && !strncasecmp(p,"switch",6)) {
  1799. // Processing of switch ()
  1800. char label[256];
  1801. p=skip_space(p2);
  1802. if(*p != '(') {
  1803. disp_error_message("need '('",p);
  1804. }
  1805. syntax.curly[syntax.curly_count].type = TYPE_SWITCH;
  1806. syntax.curly[syntax.curly_count].count = 1;
  1807. syntax.curly[syntax.curly_count].index = syntax.index++;
  1808. syntax.curly[syntax.curly_count].flag = 0;
  1809. sprintf(label,"$@__SW%x_VAL",syntax.curly[syntax.curly_count].index);
  1810. syntax.curly_count++;
  1811. add_scriptl(add_str("set"));
  1812. add_scriptc(C_ARG);
  1813. add_scriptl(add_str(label));
  1814. p=parse_expr(p);
  1815. p=skip_space(p);
  1816. if(*p != '{') {
  1817. disp_error_message("parse_syntax: expected '{'",p);
  1818. }
  1819. add_scriptc(C_FUNC);
  1820. return p + 1;
  1821. }
  1822. break;
  1823. case 'w':
  1824. case 'W':
  1825. if(p2 - p == 5 && !strncasecmp(p,"while",5)) {
  1826. int l;
  1827. char label[256];
  1828. p=skip_space(p2);
  1829. if(*p != '(') {
  1830. disp_error_message("need '('",p);
  1831. }
  1832. syntax.curly[syntax.curly_count].type = TYPE_WHILE;
  1833. syntax.curly[syntax.curly_count].count = 1;
  1834. syntax.curly[syntax.curly_count].index = syntax.index++;
  1835. syntax.curly[syntax.curly_count].flag = 0;
  1836. // Form the start of label decision
  1837. sprintf(label,"__WL%x_NXT",syntax.curly[syntax.curly_count].index);
  1838. l=add_str(label);
  1839. set_label(l,script_pos,p);
  1840. // Skip to the end point if the condition is false
  1841. sprintf(label,"__WL%x_FIN",syntax.curly[syntax.curly_count].index);
  1842. syntax.curly_count++;
  1843. add_scriptl(add_str("jump_zero"));
  1844. add_scriptc(C_ARG);
  1845. p=parse_expr(p);
  1846. p=skip_space(p);
  1847. add_scriptl(add_str(label));
  1848. add_scriptc(C_FUNC);
  1849. return p;
  1850. }
  1851. break;
  1852. }
  1853. return NULL;
  1854. }
  1855. const char* parse_syntax_close(const char *p) {
  1856. // If (...) for (...) hoge (); as to make sure closed closed once again
  1857. int flag;
  1858. do {
  1859. p = parse_syntax_close_sub(p,&flag);
  1860. } while(flag);
  1861. return p;
  1862. }
  1863. // Close judgment if, for, while, of do
  1864. // flag == 1 : closed
  1865. // flag == 0 : not closed
  1866. const char* parse_syntax_close_sub(const char* p,int* flag)
  1867. {
  1868. char label[256];
  1869. int pos = syntax.curly_count - 1;
  1870. int l;
  1871. *flag = 1;
  1872. if(syntax.curly_count <= 0) {
  1873. *flag = 0;
  1874. return p;
  1875. } else if(syntax.curly[pos].type == TYPE_IF) {
  1876. const char *bp = p;
  1877. const char *p2;
  1878. // if-block and else-block end is a new line
  1879. parse_nextline(false, p);
  1880. // Skip to the last location if
  1881. sprintf(label,"goto __IF%x_FIN;",syntax.curly[pos].index);
  1882. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1883. parse_line(label);
  1884. syntax.curly_count--;
  1885. // Put the label of the location
  1886. sprintf(label,"__IF%x_%x",syntax.curly[pos].index,syntax.curly[pos].count);
  1887. l=add_str(label);
  1888. set_label(l,script_pos,p);
  1889. syntax.curly[pos].count++;
  1890. p = skip_space(p);
  1891. p2 = skip_word(p);
  1892. if(!syntax.curly[pos].flag && p2 - p == 4 && !strncasecmp(p,"else",4)) {
  1893. // else or else - if
  1894. p = skip_space(p2);
  1895. p2 = skip_word(p);
  1896. if(p2 - p == 2 && !strncasecmp(p,"if",2)) {
  1897. // else - if
  1898. p=skip_space(p2);
  1899. if(*p != '(') {
  1900. disp_error_message("need '('",p);
  1901. }
  1902. sprintf(label,"__IF%x_%x",syntax.curly[pos].index,syntax.curly[pos].count);
  1903. add_scriptl(add_str("jump_zero"));
  1904. add_scriptc(C_ARG);
  1905. p=parse_expr(p);
  1906. p=skip_space(p);
  1907. add_scriptl(add_str(label));
  1908. add_scriptc(C_FUNC);
  1909. *flag = 0;
  1910. return p;
  1911. } else {
  1912. // else
  1913. if(!syntax.curly[pos].flag) {
  1914. syntax.curly[pos].flag = 1;
  1915. *flag = 0;
  1916. return p;
  1917. }
  1918. }
  1919. }
  1920. // Close if
  1921. syntax.curly_count--;
  1922. // Put the label of the final location
  1923. sprintf(label,"__IF%x_FIN",syntax.curly[pos].index);
  1924. l=add_str(label);
  1925. set_label(l,script_pos,p);
  1926. if(syntax.curly[pos].flag == 1) {
  1927. // Because the position of the pointer is the same if not else for this
  1928. return bp;
  1929. }
  1930. return p;
  1931. } else if(syntax.curly[pos].type == TYPE_DO) {
  1932. int l2;
  1933. char label2[256];
  1934. const char *p2;
  1935. if(syntax.curly[pos].flag) {
  1936. // (Come here continue) to form the label here
  1937. sprintf(label2,"__DO%x_NXT",syntax.curly[pos].index);
  1938. l2=add_str(label2);
  1939. set_label(l2,script_pos,p);
  1940. }
  1941. // Skip to the end point if the condition is false
  1942. p = skip_space(p);
  1943. p2 = skip_word(p);
  1944. if(p2 - p != 5 || strncasecmp(p,"while",5))
  1945. disp_error_message("parse_syntax: expected 'while'",p);
  1946. p = skip_space(p2);
  1947. if(*p != '(') {
  1948. disp_error_message("need '('",p);
  1949. }
  1950. // do-block end is a new line
  1951. parse_nextline(false, p);
  1952. sprintf(label2,"__DO%x_FIN",syntax.curly[pos].index);
  1953. add_scriptl(add_str("jump_zero"));
  1954. add_scriptc(C_ARG);
  1955. p=parse_expr(p);
  1956. p=skip_space(p);
  1957. add_scriptl(add_str(label2));
  1958. add_scriptc(C_FUNC);
  1959. // Skip to the starting point
  1960. sprintf(label2,"goto __DO%x_BGN;",syntax.curly[pos].index);
  1961. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1962. parse_line(label2);
  1963. syntax.curly_count--;
  1964. // Form label of the end point conditions
  1965. sprintf(label2,"__DO%x_FIN",syntax.curly[pos].index);
  1966. l2=add_str(label2);
  1967. set_label(l2,script_pos,p);
  1968. p = skip_space(p);
  1969. if(*p != ';') {
  1970. disp_error_message("parse_syntax: expected ';'",p);
  1971. return p+1;
  1972. }
  1973. p++;
  1974. syntax.curly_count--;
  1975. return p;
  1976. } else if(syntax.curly[pos].type == TYPE_FOR) {
  1977. // for-block end is a new line
  1978. parse_nextline(false, p);
  1979. // Skip to the next loop
  1980. sprintf(label,"goto __FR%x_NXT;",syntax.curly[pos].index);
  1981. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1982. parse_line(label);
  1983. syntax.curly_count--;
  1984. // End for labeling
  1985. sprintf(label,"__FR%x_FIN",syntax.curly[pos].index);
  1986. l=add_str(label);
  1987. set_label(l,script_pos,p);
  1988. syntax.curly_count--;
  1989. return p;
  1990. } else if(syntax.curly[pos].type == TYPE_WHILE) {
  1991. // while-block end is a new line
  1992. parse_nextline(false, p);
  1993. // Skip to the decision while
  1994. sprintf(label,"goto __WL%x_NXT;",syntax.curly[pos].index);
  1995. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1996. parse_line(label);
  1997. syntax.curly_count--;
  1998. // End while labeling
  1999. sprintf(label,"__WL%x_FIN",syntax.curly[pos].index);
  2000. l=add_str(label);
  2001. set_label(l,script_pos,p);
  2002. syntax.curly_count--;
  2003. return p;
  2004. } else if(syntax.curly[syntax.curly_count-1].type == TYPE_USERFUNC) {
  2005. int pos2 = syntax.curly_count-1;
  2006. char label2[256];
  2007. int l2;
  2008. // Back
  2009. sprintf(label2,"return;");
  2010. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  2011. parse_line(label2);
  2012. syntax.curly_count--;
  2013. // Put the label of the location
  2014. sprintf(label2,"__FN%x_FIN",syntax.curly[pos2].index);
  2015. l2=add_str(label2);
  2016. set_label(l2,script_pos,p);
  2017. syntax.curly_count--;
  2018. return p;
  2019. } else {
  2020. *flag = 0;
  2021. return p;
  2022. }
  2023. }
  2024. /*==========================================
  2025. * Added built-in functions
  2026. *------------------------------------------*/
  2027. static void add_buildin_func(void)
  2028. {
  2029. int i;
  2030. for( i = 0; buildin_func[i].func; i++ )
  2031. {
  2032. // arg must follow the pattern: (v|s|i|r|l)*\?*\*?
  2033. // 'v' - value (either string or int or reference)
  2034. // 's' - string
  2035. // 'i' - int
  2036. // 'r' - reference (of a variable)
  2037. // 'l' - label
  2038. // '?' - one optional parameter
  2039. // '*' - unknown number of optional parameters
  2040. const char* p = buildin_func[i].arg;
  2041. while( *p == 'v' || *p == 's' || *p == 'i' || *p == 'r' || *p == 'l' ) ++p;
  2042. while( *p == '?' ) ++p;
  2043. if( *p == '*' ) ++p;
  2044. if( *p != 0){
  2045. ShowWarning("add_buildin_func: ignoring function \"%s\" with invalid arg \"%s\".\n", buildin_func[i].name, buildin_func[i].arg);
  2046. } else if( *skip_word(buildin_func[i].name) != 0 ){
  2047. ShowWarning("add_buildin_func: ignoring function with invalid name \"%s\" (must be a word).\n", buildin_func[i].name);
  2048. } else {
  2049. int n = add_str(buildin_func[i].name);
  2050. str_data[n].type = C_FUNC;
  2051. str_data[n].val = i;
  2052. str_data[n].func = buildin_func[i].func;
  2053. if (!strcmp(buildin_func[i].name, "set")) buildin_set_ref = n;
  2054. else if (!strcmp(buildin_func[i].name, "callsub")) buildin_callsub_ref = n;
  2055. else if (!strcmp(buildin_func[i].name, "callfunc")) buildin_callfunc_ref = n;
  2056. else if( !strcmp(buildin_func[i].name, "getelementofarray") ) buildin_getelementofarray_ref = n;
  2057. }
  2058. }
  2059. }
  2060. /// Retrieves the value of a constant.
  2061. bool script_get_constant(const char* name, int* value)
  2062. {
  2063. int n = search_str(name);
  2064. if( n == -1 || str_data[n].type != C_INT )
  2065. {// not found or not a constant
  2066. return false;
  2067. }
  2068. value[0] = str_data[n].val;
  2069. return true;
  2070. }
  2071. /// Creates new constant or parameter with given value.
  2072. void script_set_constant(const char* name, int value, bool isparameter)
  2073. {
  2074. int n = add_str(name);
  2075. if( str_data[n].type == C_NOP )
  2076. {// new
  2077. str_data[n].type = isparameter ? C_PARAM : C_INT;
  2078. str_data[n].val = value;
  2079. }
  2080. else if( str_data[n].type == C_PARAM || str_data[n].type == C_INT )
  2081. {// existing parameter or constant
  2082. 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);
  2083. }
  2084. else
  2085. {// existing name
  2086. ShowError("script_set_constant: Invalid name for %s '%s' (already defined as %s).\n", isparameter ? "parameter" : "constant", name, script_op2name(str_data[n].type));
  2087. }
  2088. }
  2089. /*==========================================
  2090. * Reading constant databases
  2091. * const.txt
  2092. *------------------------------------------*/
  2093. static void read_constdb(void)
  2094. {
  2095. FILE *fp;
  2096. char line[1024],name[1024],val[1024];
  2097. int type;
  2098. int entries=0, skipped=0, linenum=0;
  2099. sprintf(line, "%s/const.txt", db_path);
  2100. fp=fopen(line, "r");
  2101. if(fp==NULL){
  2102. ShowError("can't read %s\n", line);
  2103. return ;
  2104. }
  2105. while(fgets(line, sizeof(line), fp))
  2106. {
  2107. linenum++;
  2108. if( line[0] == '\0' || line[0] == '\n' || line[0] == '\r') //ignore empty line
  2109. continue;
  2110. if(line[0]=='/' && line[1]=='/') //ignore commented line
  2111. continue;
  2112. type=0;
  2113. if(sscanf(line,"%1023[A-Za-z0-9/_],%1023[A-Za-z0-9/_-],%d",name,val,&type)>=2 ||
  2114. sscanf(line,"%1023[A-Za-z0-9/_] %1023[A-Za-z0-9/_-] %d",name,val,&type)>=2){
  2115. entries++;
  2116. script_set_constant(name, (int)strtol(val, NULL, 0), (bool)type);
  2117. }
  2118. else {
  2119. skipped++;
  2120. ShowWarning("Skipping line '"CL_WHITE"%d"CL_RESET"', invalide constant definition\n",linenum);
  2121. }
  2122. }
  2123. fclose(fp);
  2124. ShowStatus("Done reading '"CL_WHITE"%d"CL_RESET"' entries in '"CL_WHITE"%s/const.txt"CL_RESET"'.\n", entries, db_path);
  2125. if(skipped){
  2126. ShowWarning("Skipped '"CL_WHITE"%d"CL_RESET"', entries\n",skipped);
  2127. }
  2128. }
  2129. /**
  2130. * Sets source-end constants for NPC scripts to access.
  2131. **/
  2132. void script_hardcoded_constants(void) {
  2133. /* server defines */
  2134. script_set_constant("PACKETVER",PACKETVER,false);
  2135. script_set_constant("MAX_LEVEL",MAX_LEVEL,false);
  2136. script_set_constant("MAX_STORAGE",MAX_STORAGE,false);
  2137. script_set_constant("MAX_INVENTORY",MAX_INVENTORY,false);
  2138. script_set_constant("MAX_CART",MAX_INVENTORY,false);
  2139. script_set_constant("MAX_ZENY",MAX_ZENY,false);
  2140. script_set_constant("MAX_PARTY",MAX_PARTY,false);
  2141. script_set_constant("MAX_GUILD",MAX_GUILD,false);
  2142. script_set_constant("MAX_GUILDLEVEL",MAX_GUILDLEVEL,false);
  2143. script_set_constant("MAX_GUILD_STORAGE",MAX_GUILD_STORAGE,false);
  2144. script_set_constant("MAX_BG_MEMBERS",MAX_BG_MEMBERS,false);
  2145. script_set_constant("MAX_CHAT_USERS",MAX_CHAT_USERS,false);
  2146. script_set_constant("VIP_SCRIPT",VIP_SCRIPT,false);
  2147. script_set_constant("MIN_STORAGE",MIN_STORAGE,false);
  2148. /* status options */
  2149. script_set_constant("Option_Nothing",OPTION_NOTHING,false);
  2150. script_set_constant("Option_Sight",OPTION_SIGHT,false);
  2151. script_set_constant("Option_Hide",OPTION_HIDE,false);
  2152. script_set_constant("Option_Cloak",OPTION_CLOAK,false);
  2153. script_set_constant("Option_Falcon",OPTION_FALCON,false);
  2154. script_set_constant("Option_Riding",OPTION_RIDING,false);
  2155. script_set_constant("Option_Invisible",OPTION_INVISIBLE,false);
  2156. script_set_constant("Option_Orcish",OPTION_ORCISH,false);
  2157. script_set_constant("Option_Wedding",OPTION_WEDDING,false);
  2158. script_set_constant("Option_Ruwach",OPTION_RUWACH,false);
  2159. script_set_constant("Option_Chasewalk",OPTION_CHASEWALK,false);
  2160. script_set_constant("Option_Flying",OPTION_FLYING,false);
  2161. script_set_constant("Option_Xmas",OPTION_XMAS,false);
  2162. script_set_constant("Option_Transform",OPTION_TRANSFORM,false);
  2163. script_set_constant("Option_Summer",OPTION_SUMMER,false);
  2164. script_set_constant("Option_Dragon1",OPTION_DRAGON1,false);
  2165. script_set_constant("Option_Wug",OPTION_WUG,false);
  2166. script_set_constant("Option_Wugrider",OPTION_WUGRIDER,false);
  2167. script_set_constant("Option_Madogear",OPTION_MADOGEAR,false);
  2168. script_set_constant("Option_Dragon2",OPTION_DRAGON2,false);
  2169. script_set_constant("Option_Dragon3",OPTION_DRAGON3,false);
  2170. script_set_constant("Option_Dragon4",OPTION_DRAGON4,false);
  2171. script_set_constant("Option_Dragon5",OPTION_DRAGON5,false);
  2172. script_set_constant("Option_Hanbok",OPTION_HANBOK,false);
  2173. script_set_constant("Option_Oktoberfest",OPTION_OKTOBERFEST,false);
  2174. /* status option compounds */
  2175. script_set_constant("Option_Dragon",OPTION_DRAGON,false);
  2176. script_set_constant("Option_Costume",OPTION_COSTUME,false);
  2177. /* sc_start flags */
  2178. script_set_constant("SCSTART_NONE",SCSTART_NONE,false);
  2179. script_set_constant("SCSTART_NOAVOID",SCSTART_NOAVOID,false);
  2180. script_set_constant("SCSTART_NOTICKDEF",SCSTART_NOTICKDEF,false);
  2181. script_set_constant("SCSTART_LOADED",SCSTART_LOADED,false);
  2182. script_set_constant("SCSTART_NORATEDEF",SCSTART_NORATEDEF,false);
  2183. script_set_constant("SCSTART_NOICON",SCSTART_NOICON,false);
  2184. }
  2185. /*==========================================
  2186. * Display emplacement line of script
  2187. *------------------------------------------*/
  2188. static const char* script_print_line(StringBuf* buf, const char* p, const char* mark, int line)
  2189. {
  2190. int i;
  2191. if( p == NULL || !p[0] ) return NULL;
  2192. if( line < 0 )
  2193. StringBuf_Printf(buf, "*% 5d : ", -line);
  2194. else
  2195. StringBuf_Printf(buf, " % 5d : ", line);
  2196. for(i=0;p[i] && p[i] != '\n';i++){
  2197. if(p + i != mark)
  2198. StringBuf_Printf(buf, "%c", p[i]);
  2199. else
  2200. StringBuf_Printf(buf, "\'%c\'", p[i]);
  2201. }
  2202. StringBuf_AppendStr(buf, "\n");
  2203. return p+i+(p[i] == '\n' ? 1 : 0);
  2204. }
  2205. 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) {
  2206. // Find the line where the error occurred
  2207. int j;
  2208. int line = start_line;
  2209. const char *p;
  2210. const char *linestart[5] = { NULL, NULL, NULL, NULL, NULL };
  2211. for(p=src;p && *p;line++){
  2212. const char *lineend=strchr(p,'\n');
  2213. if(lineend==NULL || error_pos_cur<lineend){
  2214. break;
  2215. }
  2216. for( j = 0; j < 4; j++ ) {
  2217. linestart[j] = linestart[j+1];
  2218. }
  2219. linestart[4] = p;
  2220. p=lineend+1;
  2221. }
  2222. StringBuf_Printf(buf, "script error on %s line %d\n", file, line);
  2223. StringBuf_Printf(buf, " %s\n", error_msg_cur);
  2224. for(j = 0; j < 5; j++ ) {
  2225. script_print_line(buf, linestart[j], NULL, line + j - 5);
  2226. }
  2227. p = script_print_line(buf, p, error_pos_cur, -line);
  2228. for(j = 0; j < 5; j++) {
  2229. p = script_print_line(buf, p, NULL, line + j + 1 );
  2230. }
  2231. }
  2232. void script_error(const char* src, const char* file, int start_line, const char* error_msg_cur, const char* error_pos_cur) {
  2233. StringBuf buf;
  2234. StringBuf_Init(&buf);
  2235. StringBuf_AppendStr(&buf, "\a\n");
  2236. script_errorwarning_sub(&buf, src, file, start_line, error_msg_cur, error_pos_cur);
  2237. ShowError("%s", StringBuf_Value(&buf));
  2238. StringBuf_Destroy(&buf);
  2239. }
  2240. void script_warning(const char* src, const char* file, int start_line, const char* error_msg_cur, const char* error_pos_cur) {
  2241. StringBuf buf;
  2242. StringBuf_Init(&buf);
  2243. script_errorwarning_sub(&buf, src, file, start_line, error_msg_cur, error_pos_cur);
  2244. ShowWarning("%s", StringBuf_Value(&buf));
  2245. StringBuf_Destroy(&buf);
  2246. }
  2247. /*==========================================
  2248. * Analysis of the script
  2249. *------------------------------------------*/
  2250. struct script_code* parse_script(const char *src,const char *file,int line,int options)
  2251. {
  2252. const char *p,*tmpp;
  2253. int i;
  2254. struct script_code* code = NULL;
  2255. static int first=1;
  2256. char end;
  2257. bool unresolved_names = false;
  2258. parser_current_src = src;
  2259. parser_current_file = file;
  2260. parser_current_line = line;
  2261. if( src == NULL )
  2262. return NULL;// empty script
  2263. memset(&syntax,0,sizeof(syntax));
  2264. if(first){
  2265. add_buildin_func();
  2266. read_constdb();
  2267. script_hardcoded_constants();
  2268. first=0;
  2269. }
  2270. script_buf=(unsigned char *)aMalloc(SCRIPT_BLOCK_SIZE*sizeof(unsigned char));
  2271. script_pos=0;
  2272. script_size=SCRIPT_BLOCK_SIZE;
  2273. parse_nextline(true, NULL);
  2274. // who called parse_script is responsible for clearing the database after using it, but just in case... lets clear it here
  2275. if( options&SCRIPT_USE_LABEL_DB )
  2276. db_clear(scriptlabel_db);
  2277. parse_options = options;
  2278. if( setjmp( error_jump ) != 0 ) {
  2279. //Restore program state when script has problems. [from jA]
  2280. int j;
  2281. const int size = ARRAYLENGTH(syntax.curly);
  2282. if( error_report )
  2283. script_error(src,file,line,error_msg,error_pos);
  2284. aFree( error_msg );
  2285. aFree( script_buf );
  2286. script_pos = 0;
  2287. script_size = 0;
  2288. script_buf = NULL;
  2289. for(j=LABEL_START;j<str_num;j++)
  2290. if(str_data[j].type == C_NOP) str_data[j].type = C_NAME;
  2291. for(j=0; j<size; j++)
  2292. linkdb_final(&syntax.curly[j].case_label);
  2293. return NULL;
  2294. }
  2295. parse_syntax_for_flag=0;
  2296. p=src;
  2297. p=skip_space(p);
  2298. if( options&SCRIPT_IGNORE_EXTERNAL_BRACKETS )
  2299. {// does not require brackets around the script
  2300. if( *p == '\0' && !(options&SCRIPT_RETURN_EMPTY_SCRIPT) )
  2301. {// empty script and can return NULL
  2302. aFree( script_buf );
  2303. script_pos = 0;
  2304. script_size = 0;
  2305. script_buf = NULL;
  2306. return NULL;
  2307. }
  2308. end = '\0';
  2309. }
  2310. else
  2311. {// requires brackets around the script
  2312. if( *p != '{' )
  2313. disp_error_message("not found '{'",p);
  2314. p = skip_space(p+1);
  2315. if( *p == '}' && !(options&SCRIPT_RETURN_EMPTY_SCRIPT) )
  2316. {// empty script and can return NULL
  2317. aFree( script_buf );
  2318. script_pos = 0;
  2319. script_size = 0;
  2320. script_buf = NULL;
  2321. return NULL;
  2322. }
  2323. end = '}';
  2324. }
  2325. // clear references of labels, variables and internal functions
  2326. for(i=LABEL_START;i<str_num;i++){
  2327. if(
  2328. str_data[i].type==C_POS || str_data[i].type==C_NAME ||
  2329. str_data[i].type==C_USERFUNC || str_data[i].type == C_USERFUNC_POS
  2330. ){
  2331. str_data[i].type=C_NOP;
  2332. str_data[i].backpatch=-1;
  2333. str_data[i].label=-1;
  2334. }
  2335. }
  2336. while( syntax.curly_count != 0 || *p != end )
  2337. {
  2338. if( *p == '\0' )
  2339. disp_error_message("unexpected end of script",p);
  2340. // Special handling only label
  2341. tmpp=skip_space(skip_word(p));
  2342. if(*tmpp==':' && !(!strncasecmp(p,"default:",8) && p + 7 == tmpp)){
  2343. i=add_word(p);
  2344. set_label(i,script_pos,p);
  2345. if( parse_options&SCRIPT_USE_LABEL_DB )
  2346. strdb_iput(scriptlabel_db, get_str(i), script_pos);
  2347. p=tmpp+1;
  2348. p=skip_space(p);
  2349. continue;
  2350. }
  2351. // All other lumped
  2352. p=parse_line(p);
  2353. p=skip_space(p);
  2354. parse_nextline(false, p);
  2355. }
  2356. add_scriptc(C_NOP);
  2357. // trim code to size
  2358. script_size = script_pos;
  2359. RECREATE(script_buf,unsigned char,script_pos);
  2360. // default unknown references to variables
  2361. for(i=LABEL_START;i<str_num;i++){
  2362. if(str_data[i].type==C_NOP){
  2363. int j;
  2364. str_data[i].type=C_NAME;
  2365. str_data[i].label=i;
  2366. for(j=str_data[i].backpatch;j>=0 && j!=0x00ffffff;){
  2367. int next=GETVALUE(script_buf,j);
  2368. SETVALUE(script_buf,j,i);
  2369. j=next;
  2370. }
  2371. }
  2372. else if( str_data[i].type == C_USERFUNC )
  2373. {// 'function name;' without follow-up code
  2374. ShowError("parse_script: function '%s' declared but not defined.\n", str_buf+str_data[i].str);
  2375. unresolved_names = true;
  2376. }
  2377. }
  2378. if( unresolved_names )
  2379. {
  2380. disp_error_message("parse_script: unresolved function references", p);
  2381. }
  2382. #ifdef DEBUG_DISP
  2383. for(i=0;i<script_pos;i++){
  2384. if((i&15)==0) ShowMessage("%04x : ",i);
  2385. ShowMessage("%02x ",script_buf[i]);
  2386. if((i&15)==15) ShowMessage("\n");
  2387. }
  2388. ShowMessage("\n");
  2389. #endif
  2390. #ifdef DEBUG_DISASM
  2391. {
  2392. int i = 0,j;
  2393. while(i < script_pos) {
  2394. c_op op = get_com(script_buf,&i);
  2395. ShowMessage("%06x %s", i, script_op2name(op));
  2396. j = i;
  2397. switch(op) {
  2398. case C_INT:
  2399. ShowMessage(" %d", get_num(script_buf,&i));
  2400. break;
  2401. case C_POS:
  2402. ShowMessage(" 0x%06x", *(int*)(script_buf+i)&0xffffff);
  2403. i += 3;
  2404. break;
  2405. case C_NAME:
  2406. j = (*(int*)(script_buf+i)&0xffffff);
  2407. ShowMessage(" %s", ( j == 0xffffff ) ? "?? unknown ??" : get_str(j));
  2408. i += 3;
  2409. break;
  2410. case C_STR:
  2411. j = strlen(script_buf + i);
  2412. ShowMessage(" %s", script_buf + i);
  2413. i += j+1;
  2414. break;
  2415. }
  2416. ShowMessage(CL_CLL"\n");
  2417. }
  2418. }
  2419. #endif
  2420. CREATE(code,struct script_code,1);
  2421. code->script_buf = script_buf;
  2422. code->script_size = script_size;
  2423. code->script_vars = idb_alloc(DB_OPT_RELEASE_DATA);
  2424. return code;
  2425. }
  2426. /// Returns the player attached to this script, identified by the rid.
  2427. /// If there is no player attached, the script is terminated.
  2428. TBL_PC *script_rid2sd(struct script_state *st)
  2429. {
  2430. TBL_PC *sd=map_id2sd(st->rid);
  2431. if(!sd){
  2432. ShowError("script_rid2sd: fatal error ! player not attached!\n");
  2433. script_reportfunc(st);
  2434. script_reportsrc(st);
  2435. st->state = END;
  2436. }
  2437. return sd;
  2438. }
  2439. /**
  2440. * Dereferences a variable/constant, replacing it with a copy of the value.
  2441. * @param st Script state
  2442. * @param data Variable/constant
  2443. * @param sd If NULL, will try to use sd from st->rid (for player's variables)
  2444. */
  2445. void get_val_(struct script_state* st, struct script_data* data, struct map_session_data *sd)
  2446. {
  2447. const char* name;
  2448. char prefix;
  2449. char postfix;
  2450. if( !data_isreference(data) )
  2451. return;// not a variable/constant
  2452. name = reference_getname(data);
  2453. prefix = name[0];
  2454. postfix = name[strlen(name) - 1];
  2455. //##TODO use reference_tovariable(data) when it's confirmed that it works [FlavioJS]
  2456. if( !reference_toconstant(data) && not_server_variable(prefix) )
  2457. {
  2458. if (sd == NULL)
  2459. sd = script_rid2sd(st);
  2460. if( sd == NULL )
  2461. {// needs player attached
  2462. if( postfix == '$' )
  2463. {// string variable
  2464. ShowWarning("script:get_val: cannot access player variable '%s', defaulting to \"\"\n", name);
  2465. data->type = C_CONSTSTR;
  2466. data->u.str = "";
  2467. }
  2468. else
  2469. {// integer variable
  2470. ShowWarning("script:get_val: cannot access player variable '%s', defaulting to 0\n", name);
  2471. data->type = C_INT;
  2472. data->u.num = 0;
  2473. }
  2474. return;
  2475. }
  2476. }
  2477. if( postfix == '$' )
  2478. {// string variable
  2479. switch( prefix )
  2480. {
  2481. case '@':
  2482. data->u.str = pc_readregstr(sd, data->u.num);
  2483. break;
  2484. case '$':
  2485. data->u.str = mapreg_readregstr(data->u.num);
  2486. break;
  2487. case '#':
  2488. if( name[1] == '#' )
  2489. data->u.str = pc_readaccountreg2str(sd, name);// global
  2490. else
  2491. data->u.str = pc_readaccountregstr(sd, name);// local
  2492. break;
  2493. case '.':
  2494. {
  2495. struct DBMap* n =
  2496. data->ref ? *data->ref:
  2497. name[1] == '@' ? st->stack->var_function:// instance/scope variable
  2498. st->script->script_vars;// npc variable
  2499. if( n )
  2500. data->u.str = (char*)idb_get(n,reference_getuid(data));
  2501. else
  2502. data->u.str = NULL;
  2503. }
  2504. break;
  2505. case '\'':
  2506. {
  2507. int instance_id = script_instancegetid(st);
  2508. if( instance_id )
  2509. data->u.str = (char*)idb_get(instance_data[instance_id].vars,reference_getuid(data));
  2510. else {
  2511. ShowWarning("script:get_val: cannot access instance variable '%s', defaulting to \"\"\n", name);
  2512. data->u.str = NULL;
  2513. }
  2514. }
  2515. break;
  2516. default:
  2517. data->u.str = pc_readglobalreg_str(sd, name);
  2518. break;
  2519. }
  2520. if( data->u.str == NULL || data->u.str[0] == '\0' )
  2521. {// empty string
  2522. data->type = C_CONSTSTR;
  2523. data->u.str = "";
  2524. }
  2525. else
  2526. {// duplicate string
  2527. data->type = C_STR;
  2528. data->u.str = aStrdup(data->u.str);
  2529. }
  2530. }
  2531. else
  2532. {// integer variable
  2533. data->type = C_INT;
  2534. if( reference_toconstant(data) )
  2535. {
  2536. data->u.num = reference_getconstant(data);
  2537. }
  2538. else if( reference_toparam(data) )
  2539. {
  2540. data->u.num = pc_readparam(sd, reference_getparamtype(data));
  2541. }
  2542. else
  2543. switch( prefix )
  2544. {
  2545. case '@':
  2546. data->u.num = pc_readreg(sd, data->u.num);
  2547. break;
  2548. case '$':
  2549. data->u.num = mapreg_readreg(data->u.num);
  2550. break;
  2551. case '#':
  2552. if( name[1] == '#' )
  2553. data->u.num = pc_readaccountreg2(sd, name);// global
  2554. else
  2555. data->u.num = pc_readaccountreg(sd, name);// local
  2556. break;
  2557. case '.':
  2558. {
  2559. struct DBMap* n =
  2560. data->ref ? *data->ref:
  2561. name[1] == '@' ? st->stack->var_function:// instance/scope variable
  2562. st->script->script_vars;// npc variable
  2563. if( n )
  2564. data->u.num = (int)idb_iget(n,reference_getuid(data));
  2565. else
  2566. data->u.num = 0;
  2567. }
  2568. break;
  2569. case '\'':
  2570. {
  2571. int instance_id = script_instancegetid(st);
  2572. if( instance_id )
  2573. data->u.num = (int)idb_iget(instance_data[instance_id].vars,reference_getuid(data));
  2574. else {
  2575. ShowWarning("script:get_val: cannot access instance variable '%s', defaulting to 0\n", name);
  2576. data->u.num = 0;
  2577. }
  2578. }
  2579. break;
  2580. default:
  2581. data->u.num = pc_readglobalreg(sd, name);
  2582. break;
  2583. }
  2584. }
  2585. return;
  2586. }
  2587. void get_val(struct script_state* st, struct script_data* data)
  2588. {
  2589. get_val_(st,data,NULL);
  2590. }
  2591. struct script_data* push_val2(struct script_stack* stack, enum c_op type, int val, struct DBMap** ref);
  2592. /// Retrieves the value of a reference identified by uid (variable, constant, param)
  2593. /// The value is left in the top of the stack and needs to be removed manually.
  2594. void* get_val2(struct script_state* st, int uid, struct DBMap** ref)
  2595. {
  2596. struct script_data* data;
  2597. push_val2(st->stack, C_NAME, uid, ref);
  2598. data = script_getdatatop(st, -1);
  2599. get_val(st, data);
  2600. return (data->type == C_INT ? (void*)__64BPRTSIZE(data->u.num) : (void*)__64BPRTSIZE(data->u.str));
  2601. }
  2602. /*==========================================
  2603. * Stores the value of a script variable
  2604. * Return value is 0 on fail, 1 on success.
  2605. *------------------------------------------*/
  2606. static int set_reg(struct script_state* st, TBL_PC* sd, int num, const char* name, const void* value, struct DBMap** ref)
  2607. {
  2608. char prefix = name[0];
  2609. if( is_string_variable(name) ) {// string variable
  2610. const char* str = (const char*)value;
  2611. switch (prefix) {
  2612. case '@':
  2613. return pc_setregstr(sd, num, str);
  2614. case '$':
  2615. return mapreg_setregstr(num, str);
  2616. case '#':
  2617. return (name[1] == '#') ?
  2618. pc_setaccountreg2str(sd, name, str) :
  2619. pc_setaccountregstr(sd, name, str);
  2620. case '.':
  2621. {
  2622. struct DBMap* n;
  2623. n = (ref) ? *ref : (name[1] == '@') ? st->stack->var_function : st->script->script_vars;
  2624. if( n ) {
  2625. idb_remove(n, num);
  2626. if (str[0]) idb_put(n, num, aStrdup(str));
  2627. }
  2628. }
  2629. return 1;
  2630. case '\'':
  2631. {
  2632. int instance_id = script_instancegetid(st);
  2633. if( instance_id ) {
  2634. idb_remove(instance_data[instance_id].vars, num);
  2635. if( str[0] ) idb_put(instance_data[instance_id].vars, num, aStrdup(str));
  2636. }
  2637. return 1;
  2638. }
  2639. default:
  2640. return pc_setglobalreg_str(sd, name, str);
  2641. }
  2642. } else {// integer variable
  2643. int val = (int)__64BPRTSIZE(value);
  2644. if(str_data[num&0x00ffffff].type == C_PARAM)
  2645. {
  2646. if( pc_setparam(sd, str_data[num&0x00ffffff].val, val) == 0 )
  2647. {
  2648. if( st != NULL )
  2649. {
  2650. ShowError("script:set_reg: failed to set param '%s' to %d.\n", name, val);
  2651. script_reportsrc(st);
  2652. st->state = END;
  2653. }
  2654. return 0;
  2655. }
  2656. return 1;
  2657. }
  2658. switch (prefix) {
  2659. case '@':
  2660. return pc_setreg(sd, num, val);
  2661. case '$':
  2662. return mapreg_setreg(num, val);
  2663. case '#':
  2664. return (name[1] == '#') ?
  2665. pc_setaccountreg2(sd, name, val) :
  2666. pc_setaccountreg(sd, name, val);
  2667. case '.':
  2668. {
  2669. struct DBMap* n;
  2670. n = (ref) ? *ref : (name[1] == '@') ? st->stack->var_function : st->script->script_vars;
  2671. if( n ) {
  2672. idb_remove(n, num);
  2673. if( val != 0 )
  2674. idb_iput(n, num, val);
  2675. }
  2676. }
  2677. return 1;
  2678. case '\'':
  2679. {
  2680. int instance_id = script_instancegetid(st);
  2681. if( instance_id ) {
  2682. idb_remove(instance_data[instance_id].vars, num);
  2683. if( val != 0 )
  2684. idb_iput(instance_data[instance_id].vars, num, val);
  2685. }
  2686. return 1;
  2687. }
  2688. default:
  2689. return pc_setglobalreg(sd, name, val);
  2690. }
  2691. }
  2692. }
  2693. int set_var(TBL_PC* sd, char* name, void* val)
  2694. {
  2695. return set_reg(NULL, sd, reference_uid(add_str(name),0), name, val, NULL);
  2696. }
  2697. void setd_sub(struct script_state *st, TBL_PC *sd, const char *varname, int elem, void *value, struct DBMap **ref)
  2698. {
  2699. set_reg(st, sd, reference_uid(add_str(varname),elem), varname, value, ref);
  2700. }
  2701. /**
  2702. * Converts the data to a string
  2703. * @param st
  2704. * @param data
  2705. * @param sd
  2706. */
  2707. const char* conv_str_(struct script_state* st, struct script_data* data, struct map_session_data *sd)
  2708. {
  2709. char* p;
  2710. get_val_(st, data, sd);
  2711. if( data_isstring(data) )
  2712. {// nothing to convert
  2713. }
  2714. else if( data_isint(data) )
  2715. {// int -> string
  2716. CREATE(p, char, ITEM_NAME_LENGTH);
  2717. snprintf(p, ITEM_NAME_LENGTH, "%d", data->u.num);
  2718. p[ITEM_NAME_LENGTH-1] = '\0';
  2719. data->type = C_STR;
  2720. data->u.str = p;
  2721. }
  2722. else if( data_isreference(data) )
  2723. {// reference -> string
  2724. //##TODO when does this happen (check get_val) [FlavioJS]
  2725. data->type = C_CONSTSTR;
  2726. data->u.str = reference_getname(data);
  2727. }
  2728. else
  2729. {// unsupported data type
  2730. ShowError("script:conv_str: cannot convert to string, defaulting to \"\"\n");
  2731. script_reportdata(data);
  2732. script_reportsrc(st);
  2733. data->type = C_CONSTSTR;
  2734. data->u.str = "";
  2735. }
  2736. return data->u.str;
  2737. }
  2738. const char* conv_str(struct script_state* st, struct script_data* data)
  2739. {
  2740. return conv_str_(st, data, NULL);
  2741. }
  2742. /**
  2743. * Converts the data to an int
  2744. * @param st
  2745. * @param data
  2746. * @param sd
  2747. */
  2748. int conv_num_(struct script_state* st, struct script_data* data, struct map_session_data *sd)
  2749. {
  2750. get_val_(st, data, sd);
  2751. if( data_isint(data) )
  2752. {// nothing to convert
  2753. }
  2754. else if( data_isstring(data) )
  2755. {// string -> int
  2756. // the result does not overflow or underflow, it is capped instead
  2757. // ex: 999999999999 is capped to INT_MAX (2147483647)
  2758. char* p = data->u.str;
  2759. long num;
  2760. errno = 0;
  2761. num = strtol(data->u.str, NULL, 10);// change radix to 0 to support octal numbers "o377" and hex numbers "0xFF"
  2762. if( errno == ERANGE
  2763. #if LONG_MAX > INT_MAX
  2764. || num < INT_MIN || num > INT_MAX
  2765. #endif
  2766. )
  2767. {
  2768. if( num <= INT_MIN )
  2769. {
  2770. num = INT_MIN;
  2771. ShowError("script:conv_num: underflow detected, capping to %ld\n", num);
  2772. }
  2773. else//if( num >= INT_MAX )
  2774. {
  2775. num = INT_MAX;
  2776. ShowError("script:conv_num: overflow detected, capping to %ld\n", num);
  2777. }
  2778. script_reportdata(data);
  2779. script_reportsrc(st);
  2780. }
  2781. if( data->type == C_STR )
  2782. aFree(p);
  2783. data->type = C_INT;
  2784. data->u.num = (int)num;
  2785. }
  2786. #if 0
  2787. // FIXME this function is being used to retrieve the position of labels and
  2788. // probably other stuff [FlavioJS]
  2789. else
  2790. {// unsupported data type
  2791. ShowError("script:conv_num: cannot convert to number, defaulting to 0\n");
  2792. script_reportdata(data);
  2793. script_reportsrc(st);
  2794. data->type = C_INT;
  2795. data->u.num = 0;
  2796. }
  2797. #endif
  2798. return data->u.num;
  2799. }
  2800. int conv_num(struct script_state* st, struct script_data* data)
  2801. {
  2802. return conv_num_(st, data, NULL);
  2803. }
  2804. //
  2805. // Stack operations
  2806. //
  2807. /// Increases the size of the stack
  2808. void stack_expand(struct script_stack* stack)
  2809. {
  2810. stack->sp_max += 64;
  2811. stack->stack_data = (struct script_data*)aRealloc(stack->stack_data,
  2812. stack->sp_max * sizeof(stack->stack_data[0]) );
  2813. memset(stack->stack_data + (stack->sp_max - 64), 0,
  2814. 64 * sizeof(stack->stack_data[0]) );
  2815. }
  2816. /// Pushes a value into the stack
  2817. #define push_val(stack,type,val) push_val2(stack, type, val, NULL)
  2818. /// Pushes a value into the stack (with reference)
  2819. struct script_data* push_val2(struct script_stack* stack, enum c_op type, int val, struct DBMap** ref)
  2820. {
  2821. if( stack->sp >= stack->sp_max )
  2822. stack_expand(stack);
  2823. stack->stack_data[stack->sp].type = type;
  2824. stack->stack_data[stack->sp].u.num = val;
  2825. stack->stack_data[stack->sp].ref = ref;
  2826. stack->sp++;
  2827. return &stack->stack_data[stack->sp-1];
  2828. }
  2829. /// Pushes a string into the stack
  2830. struct script_data* push_str(struct script_stack* stack, enum c_op type, char* str)
  2831. {
  2832. if( stack->sp >= stack->sp_max )
  2833. stack_expand(stack);
  2834. stack->stack_data[stack->sp].type = type;
  2835. stack->stack_data[stack->sp].u.str = str;
  2836. stack->stack_data[stack->sp].ref = NULL;
  2837. stack->sp++;
  2838. return &stack->stack_data[stack->sp-1];
  2839. }
  2840. /// Pushes a retinfo into the stack
  2841. struct script_data* push_retinfo(struct script_stack* stack, struct script_retinfo* ri, DBMap **ref)
  2842. {
  2843. if( stack->sp >= stack->sp_max )
  2844. stack_expand(stack);
  2845. stack->stack_data[stack->sp].type = C_RETINFO;
  2846. stack->stack_data[stack->sp].u.ri = ri;
  2847. stack->stack_data[stack->sp].ref = ref;
  2848. stack->sp++;
  2849. return &stack->stack_data[stack->sp-1];
  2850. }
  2851. /// Pushes a copy of the target position into the stack
  2852. struct script_data* push_copy(struct script_stack* stack, int pos)
  2853. {
  2854. switch( stack->stack_data[pos].type )
  2855. {
  2856. case C_CONSTSTR:
  2857. return push_str(stack, C_CONSTSTR, stack->stack_data[pos].u.str);
  2858. break;
  2859. case C_STR:
  2860. return push_str(stack, C_STR, aStrdup(stack->stack_data[pos].u.str));
  2861. break;
  2862. case C_RETINFO:
  2863. ShowFatalError("script:push_copy: can't create copies of C_RETINFO. Exiting...\n");
  2864. exit(1);
  2865. break;
  2866. default:
  2867. return push_val2(
  2868. stack,stack->stack_data[pos].type,
  2869. stack->stack_data[pos].u.num,
  2870. stack->stack_data[pos].ref
  2871. );
  2872. break;
  2873. }
  2874. }
  2875. /// Removes the values in indexes [start,end] from the stack.
  2876. /// Adjusts all stack pointers.
  2877. void pop_stack(struct script_state* st, int start, int end)
  2878. {
  2879. struct script_stack* stack = st->stack;
  2880. struct script_data* data;
  2881. int i;
  2882. if( start < 0 )
  2883. start = 0;
  2884. if( end > stack->sp )
  2885. end = stack->sp;
  2886. if( start >= end )
  2887. return;// nothing to pop
  2888. // free stack elements
  2889. for( i = start; i < end; i++ )
  2890. {
  2891. data = &stack->stack_data[i];
  2892. if( data->type == C_STR )
  2893. aFree(data->u.str);
  2894. if( data->type == C_RETINFO )
  2895. {
  2896. struct script_retinfo* ri = data->u.ri;
  2897. if( ri->var_function )
  2898. script_free_vars(ri->var_function);
  2899. if( data->ref )
  2900. aFree(data->ref);
  2901. aFree(ri);
  2902. }
  2903. data->type = C_NOP;
  2904. }
  2905. // move the rest of the elements
  2906. if( stack->sp > end )
  2907. {
  2908. memmove(&stack->stack_data[start], &stack->stack_data[end], sizeof(stack->stack_data[0])*(stack->sp - end));
  2909. for( i = start + stack->sp - end; i < stack->sp; ++i )
  2910. stack->stack_data[i].type = C_NOP;
  2911. }
  2912. // adjust stack pointers
  2913. if( st->start > end ) st->start -= end - start;
  2914. else if( st->start > start ) st->start = start;
  2915. if( st->end > end ) st->end -= end - start;
  2916. else if( st->end > start ) st->end = start;
  2917. if( stack->defsp > end ) stack->defsp -= end - start;
  2918. else if( stack->defsp > start ) stack->defsp = start;
  2919. stack->sp -= end - start;
  2920. }
  2921. ///
  2922. ///
  2923. ///
  2924. /*==========================================
  2925. * Release script dependent variable, dependent variable of function
  2926. *------------------------------------------*/
  2927. void script_free_vars(struct DBMap* storage)
  2928. {
  2929. if( storage )
  2930. {// destroy the storage construct containing the variables
  2931. db_destroy(storage);
  2932. }
  2933. }
  2934. void script_free_code(struct script_code* code)
  2935. {
  2936. nullpo_retv(code);
  2937. script_free_vars( code->script_vars );
  2938. aFree( code->script_buf );
  2939. aFree( code );
  2940. }
  2941. /// Creates a new script state.
  2942. ///
  2943. /// @param script Script code
  2944. /// @param pos Position in the code
  2945. /// @param rid Who is running the script (attached player)
  2946. /// @param oid Where the code is being run (npc 'object')
  2947. /// @return Script state
  2948. struct script_state* script_alloc_state(struct script_code* script, int pos, int rid, int oid)
  2949. {
  2950. struct script_state* st;
  2951. CREATE(st, struct script_state, 1);
  2952. st->stack = (struct script_stack*)aMalloc(sizeof(struct script_stack));
  2953. st->stack->sp = 0;
  2954. st->stack->sp_max = 64;
  2955. CREATE(st->stack->stack_data, struct script_data, st->stack->sp_max);
  2956. st->stack->defsp = st->stack->sp;
  2957. st->stack->var_function = idb_alloc(DB_OPT_RELEASE_DATA);
  2958. st->state = RUN;
  2959. st->script = script;
  2960. //st->scriptroot = script;
  2961. st->pos = pos;
  2962. st->rid = rid;
  2963. st->oid = oid;
  2964. st->sleep.timer = INVALID_TIMER;
  2965. st->npc_item_flag = battle_config.item_enabled_npc;
  2966. return st;
  2967. }
  2968. /// Frees a script state.
  2969. ///
  2970. /// @param st Script state
  2971. void script_free_state(struct script_state* st)
  2972. {
  2973. if(st->bk_st)
  2974. {// backup was not restored
  2975. 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);
  2976. }
  2977. if( st->sleep.timer != INVALID_TIMER )
  2978. delete_timer(st->sleep.timer, run_script_timer);
  2979. script_free_vars(st->stack->var_function);
  2980. pop_stack(st, 0, st->stack->sp);
  2981. aFree(st->stack->stack_data);
  2982. aFree(st->stack);
  2983. st->stack = NULL;
  2984. st->pos = -1;
  2985. aFree(st);
  2986. }
  2987. //
  2988. // Main execution unit
  2989. //
  2990. /*==========================================
  2991. * Read command
  2992. *------------------------------------------*/
  2993. c_op get_com(unsigned char *script,int *pos)
  2994. {
  2995. int i = 0, j = 0;
  2996. if(script[*pos]>=0x80){
  2997. return C_INT;
  2998. }
  2999. while(script[*pos]>=0x40){
  3000. i=script[(*pos)++]<<j;
  3001. j+=6;
  3002. }
  3003. return (c_op)(i+(script[(*pos)++]<<j));
  3004. }
  3005. /*==========================================
  3006. * Income figures
  3007. *------------------------------------------*/
  3008. int get_num(unsigned char *script,int *pos)
  3009. {
  3010. int i,j;
  3011. i=0; j=0;
  3012. while(script[*pos]>=0xc0){
  3013. i+=(script[(*pos)++]&0x7f)<<j;
  3014. j+=6;
  3015. }
  3016. return i+((script[(*pos)++]&0x7f)<<j);
  3017. }
  3018. /*==========================================
  3019. * Remove the value from the stack
  3020. *------------------------------------------*/
  3021. int pop_val(struct script_state* st)
  3022. {
  3023. if(st->stack->sp<=0)
  3024. return 0;
  3025. st->stack->sp--;
  3026. get_val(st,&(st->stack->stack_data[st->stack->sp]));
  3027. if(st->stack->stack_data[st->stack->sp].type==C_INT)
  3028. return st->stack->stack_data[st->stack->sp].u.num;
  3029. return 0;
  3030. }
  3031. /// Ternary operators
  3032. /// test ? if_true : if_false
  3033. void op_3(struct script_state* st, int op)
  3034. {
  3035. struct script_data* data;
  3036. int flag = 0;
  3037. data = script_getdatatop(st, -3);
  3038. get_val(st, data);
  3039. if( data_isstring(data) )
  3040. flag = data->u.str[0];// "" -> false
  3041. else if( data_isint(data) )
  3042. flag = data->u.num;// 0 -> false
  3043. else
  3044. {
  3045. ShowError("script:op_3: invalid data for the ternary operator test\n");
  3046. script_reportdata(data);
  3047. script_reportsrc(st);
  3048. script_removetop(st, -3, 0);
  3049. script_pushnil(st);
  3050. return;
  3051. }
  3052. if( flag )
  3053. script_pushcopytop(st, -2);
  3054. else
  3055. script_pushcopytop(st, -1);
  3056. script_removetop(st, -4, -1);
  3057. }
  3058. /// Binary string operators
  3059. /// s1 EQ s2 -> i
  3060. /// s1 NE s2 -> i
  3061. /// s1 GT s2 -> i
  3062. /// s1 GE s2 -> i
  3063. /// s1 LT s2 -> i
  3064. /// s1 LE s2 -> i
  3065. /// s1 ADD s2 -> s
  3066. void op_2str(struct script_state* st, int op, const char* s1, const char* s2)
  3067. {
  3068. int a = 0;
  3069. switch(op){
  3070. case C_EQ: a = (strcmp(s1,s2) == 0); break;
  3071. case C_NE: a = (strcmp(s1,s2) != 0); break;
  3072. case C_GT: a = (strcmp(s1,s2) > 0); break;
  3073. case C_GE: a = (strcmp(s1,s2) >= 0); break;
  3074. case C_LT: a = (strcmp(s1,s2) < 0); break;
  3075. case C_LE: a = (strcmp(s1,s2) <= 0); break;
  3076. case C_ADD:
  3077. {
  3078. char* buf = (char *)aMalloc((strlen(s1)+strlen(s2)+1)*sizeof(char));
  3079. strcpy(buf, s1);
  3080. strcat(buf, s2);
  3081. script_pushstr(st, buf);
  3082. return;
  3083. }
  3084. default:
  3085. ShowError("script:op2_str: unexpected string operator %s\n", script_op2name(op));
  3086. script_reportsrc(st);
  3087. script_pushnil(st);
  3088. st->state = END;
  3089. return;
  3090. }
  3091. script_pushint(st,a);
  3092. }
  3093. /// Binary number operators
  3094. /// i OP i -> i
  3095. void op_2num(struct script_state* st, int op, int i1, int i2)
  3096. {
  3097. int ret;
  3098. double ret_double;
  3099. switch( op )
  3100. {
  3101. case C_AND: ret = i1 & i2; break;
  3102. case C_OR: ret = i1 | i2; break;
  3103. case C_XOR: ret = i1 ^ i2; break;
  3104. case C_LAND: ret = (i1 && i2); break;
  3105. case C_LOR: ret = (i1 || i2); break;
  3106. case C_EQ: ret = (i1 == i2); break;
  3107. case C_NE: ret = (i1 != i2); break;
  3108. case C_GT: ret = (i1 > i2); break;
  3109. case C_GE: ret = (i1 >= i2); break;
  3110. case C_LT: ret = (i1 < i2); break;
  3111. case C_LE: ret = (i1 <= i2); break;
  3112. case C_R_SHIFT: ret = i1>>i2; break;
  3113. case C_L_SHIFT: ret = i1<<i2; break;
  3114. case C_DIV:
  3115. case C_MOD:
  3116. if( i2 == 0 )
  3117. {
  3118. ShowError("script:op_2num: division by zero detected op=%s i1=%d i2=%d\n", script_op2name(op), i1, i2);
  3119. script_reportsrc(st);
  3120. script_pushnil(st);
  3121. st->state = END;
  3122. return;
  3123. }
  3124. else if( op == C_DIV )
  3125. ret = i1 / i2;
  3126. else//if( op == C_MOD )
  3127. ret = i1 % i2;
  3128. break;
  3129. default:
  3130. switch( op )
  3131. {// operators that can overflow/underflow
  3132. case C_ADD: ret = i1 + i2; ret_double = (double)i1 + (double)i2; break;
  3133. case C_SUB: ret = i1 - i2; ret_double = (double)i1 - (double)i2; break;
  3134. case C_MUL: ret = i1 * i2; ret_double = (double)i1 * (double)i2; break;
  3135. default:
  3136. ShowError("script:op_2num: unexpected number operator %s i1=%d i2=%d\n", script_op2name(op), i1, i2);
  3137. script_reportsrc(st);
  3138. script_pushnil(st);
  3139. return;
  3140. }
  3141. if( ret_double < (double)INT_MIN )
  3142. {
  3143. ShowWarning("script:op_2num: underflow detected op=%s i1=%d i2=%d\n", script_op2name(op), i1, i2);
  3144. script_reportsrc(st);
  3145. ret = INT_MIN;
  3146. }
  3147. else if( ret_double > (double)INT_MAX )
  3148. {
  3149. ShowWarning("script:op_2num: overflow detected op=%s i1=%d i2=%d\n", script_op2name(op), i1, i2);
  3150. script_reportsrc(st);
  3151. ret = INT_MAX;
  3152. }
  3153. }
  3154. script_pushint(st, ret);
  3155. }
  3156. /// Binary operators
  3157. void op_2(struct script_state *st, int op)
  3158. {
  3159. struct script_data* left, leftref;
  3160. struct script_data* right;
  3161. leftref.type = C_NOP;
  3162. left = script_getdatatop(st, -2);
  3163. right = script_getdatatop(st, -1);
  3164. if (st->op2ref) {
  3165. if (data_isreference(left)) {
  3166. leftref = *left;
  3167. }
  3168. st->op2ref = 0;
  3169. }
  3170. get_val(st, left);
  3171. get_val(st, right);
  3172. // automatic conversions
  3173. switch( op )
  3174. {
  3175. case C_ADD:
  3176. if( data_isint(left) && data_isstring(right) )
  3177. {// convert int-string to string-string
  3178. conv_str(st, left);
  3179. }
  3180. else if( data_isstring(left) && data_isint(right) )
  3181. {// convert string-int to string-string
  3182. conv_str(st, right);
  3183. }
  3184. break;
  3185. }
  3186. if( data_isstring(left) && data_isstring(right) )
  3187. {// ss => op_2str
  3188. op_2str(st, op, left->u.str, right->u.str);
  3189. script_removetop(st, leftref.type == C_NOP ? -3 : -2, -1);// pop the two values before the top one
  3190. if (leftref.type != C_NOP)
  3191. {
  3192. if (left->type == C_STR) // don't free C_CONSTSTR
  3193. aFree(left->u.str);
  3194. *left = leftref;
  3195. }
  3196. }
  3197. else if( data_isint(left) && data_isint(right) )
  3198. {// ii => op_2num
  3199. int i1 = left->u.num;
  3200. int i2 = right->u.num;
  3201. script_removetop(st, leftref.type == C_NOP ? -2 : -1, 0);
  3202. op_2num(st, op, i1, i2);
  3203. if (leftref.type != C_NOP)
  3204. *left = leftref;
  3205. }
  3206. else
  3207. {// invalid argument
  3208. ShowError("script:op_2: invalid data for operator %s\n", script_op2name(op));
  3209. script_reportdata(left);
  3210. script_reportdata(right);
  3211. script_reportsrc(st);
  3212. script_removetop(st, -2, 0);
  3213. script_pushnil(st);
  3214. st->state = END;
  3215. }
  3216. }
  3217. /// Unary operators
  3218. /// NEG i -> i
  3219. /// NOT i -> i
  3220. /// LNOT i -> i
  3221. void op_1(struct script_state* st, int op)
  3222. {
  3223. struct script_data* data;
  3224. int i1;
  3225. data = script_getdatatop(st, -1);
  3226. get_val(st, data);
  3227. if( !data_isint(data) )
  3228. {// not a number
  3229. ShowError("script:op_1: argument is not a number (op=%s)\n", script_op2name(op));
  3230. script_reportdata(data);
  3231. script_reportsrc(st);
  3232. script_pushnil(st);
  3233. st->state = END;
  3234. return;
  3235. }
  3236. i1 = data->u.num;
  3237. script_removetop(st, -1, 0);
  3238. switch( op )
  3239. {
  3240. case C_NEG: i1 = -i1; break;
  3241. case C_NOT: i1 = ~i1; break;
  3242. case C_LNOT: i1 = !i1; break;
  3243. default:
  3244. ShowError("script:op_1: unexpected operator %s i1=%d\n", script_op2name(op), i1);
  3245. script_reportsrc(st);
  3246. script_pushnil(st);
  3247. st->state = END;
  3248. return;
  3249. }
  3250. script_pushint(st, i1);
  3251. }
  3252. /// Checks the type of all arguments passed to a built-in function.
  3253. ///
  3254. /// @param st Script state whose stack arguments should be inspected.
  3255. /// @param func Built-in function for which the arguments are intended.
  3256. static void script_check_buildin_argtype(struct script_state* st, int func)
  3257. {
  3258. int idx, invalid = 0;
  3259. for( idx = 2; script_hasdata(st, idx); idx++ )
  3260. {
  3261. struct script_data* data = script_getdata(st, idx);
  3262. script_function* sf = &buildin_func[str_data[func].val];
  3263. char type = sf->arg[idx-2];
  3264. if( type == '?' || type == '*' )
  3265. {// optional argument or unknown number of optional parameters ( no types are after this )
  3266. break;
  3267. }
  3268. else if( type == 0 )
  3269. {// more arguments than necessary ( should not happen, as it is checked before )
  3270. ShowWarning("Found more arguments than necessary. unexpected arg type %s\n",script_op2name(data->type));
  3271. invalid++;
  3272. break;
  3273. }
  3274. else
  3275. {
  3276. const char* name = NULL;
  3277. if( data_isreference(data) )
  3278. {// get name for variables to determine the type they refer to
  3279. name = reference_getname(data);
  3280. }
  3281. switch( type )
  3282. {
  3283. case 'v':
  3284. if( !data_isstring(data) && !data_isint(data) && !data_isreference(data) )
  3285. {// variant
  3286. ShowWarning("Unexpected type for argument %d. Expected string, number or variable.\n", idx-1);
  3287. script_reportdata(data);
  3288. invalid++;
  3289. }
  3290. break;
  3291. case 's':
  3292. if( !data_isstring(data) && !( data_isreference(data) && is_string_variable(name) ) )
  3293. {// string
  3294. ShowWarning("Unexpected type for argument %d. Expected string.\n", idx-1);
  3295. script_reportdata(data);
  3296. invalid++;
  3297. }
  3298. break;
  3299. case 'i':
  3300. if( !data_isint(data) && !( data_isreference(data) && ( reference_toparam(data) || reference_toconstant(data) || !is_string_variable(name) ) ) )
  3301. {// int ( params and constants are always int )
  3302. ShowWarning("Unexpected type for argument %d. Expected number.\n", idx-1);
  3303. script_reportdata(data);
  3304. invalid++;
  3305. }
  3306. break;
  3307. case 'r':
  3308. if( !data_isreference(data) )
  3309. {// variables
  3310. ShowWarning("Unexpected type for argument %d. Expected variable, got %s.\n", idx-1,script_op2name(data->type));
  3311. script_reportdata(data);
  3312. invalid++;
  3313. }
  3314. break;
  3315. case 'l':
  3316. if( !data_islabel(data) && !data_isfunclabel(data) )
  3317. {// label
  3318. ShowWarning("Unexpected type for argument %d. Expected label, got %s\n", idx-1,script_op2name(data->type));
  3319. script_reportdata(data);
  3320. invalid++;
  3321. }
  3322. break;
  3323. }
  3324. }
  3325. }
  3326. if(invalid)
  3327. {
  3328. ShowDebug("Function: %s\n", get_str(func));
  3329. script_reportsrc(st);
  3330. }
  3331. }
  3332. /// Executes a buildin command.
  3333. /// Stack: C_NAME(<command>) C_ARG <arg0> <arg1> ... <argN>
  3334. int run_func(struct script_state *st)
  3335. {
  3336. struct script_data* data;
  3337. int i,start_sp,end_sp,func;
  3338. end_sp = st->stack->sp;// position after the last argument
  3339. for( i = end_sp-1; i > 0 ; --i )
  3340. if( st->stack->stack_data[i].type == C_ARG )
  3341. break;
  3342. if( i == 0 ) {
  3343. ShowError("script:run_func: C_ARG not found. please report this!!!\n");
  3344. st->state = END;
  3345. script_reportsrc(st);
  3346. return 1;
  3347. }
  3348. start_sp = i-1;// C_NAME of the command
  3349. st->start = start_sp;
  3350. st->end = end_sp;
  3351. data = &st->stack->stack_data[st->start];
  3352. if( data->type == C_NAME && str_data[data->u.num].type == C_FUNC ) {
  3353. func = data->u.num;
  3354. st->funcname = reference_getname(data);
  3355. } else {
  3356. ShowError("script:run_func: not a buildin command.\n");
  3357. script_reportdata(data);
  3358. script_reportsrc(st);
  3359. st->state = END;
  3360. return 1;
  3361. }
  3362. if( script_config.warn_func_mismatch_argtypes ) {
  3363. script_check_buildin_argtype(st, func);
  3364. }
  3365. if(str_data[func].func) {
  3366. if (str_data[func].func(st)) //Report error
  3367. script_reportsrc(st);
  3368. } else {
  3369. 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));
  3370. script_reportsrc(st);
  3371. st->state = END;
  3372. }
  3373. // Stack's datum are used when re-running functions [Eoe]
  3374. if( st->state == RERUNLINE )
  3375. return 0;
  3376. pop_stack(st, st->start, st->end);
  3377. if( st->state == RETFUNC ) {// return from a user-defined function
  3378. struct script_retinfo* ri;
  3379. int olddefsp = st->stack->defsp;
  3380. int nargs;
  3381. pop_stack(st, st->stack->defsp, st->start);// pop distractions from the stack
  3382. if( st->stack->defsp < 1 || st->stack->stack_data[st->stack->defsp-1].type != C_RETINFO )
  3383. {
  3384. ShowWarning("script:run_func: return without callfunc or callsub!\n");
  3385. script_reportsrc(st);
  3386. st->state = END;
  3387. return 1;
  3388. }
  3389. script_free_vars( st->stack->var_function );
  3390. ri = st->stack->stack_data[st->stack->defsp-1].u.ri;
  3391. nargs = ri->nargs;
  3392. st->pos = ri->pos;
  3393. st->script = ri->script;
  3394. st->stack->var_function = ri->var_function;
  3395. st->stack->defsp = ri->defsp;
  3396. memset(ri, 0, sizeof(struct script_retinfo));
  3397. pop_stack(st, olddefsp-nargs-1, olddefsp);// pop arguments and retinfo
  3398. st->state = GOTO;
  3399. }
  3400. return 0;
  3401. }
  3402. /*==========================================
  3403. * script execution
  3404. *------------------------------------------*/
  3405. void run_script(struct script_code *rootscript, int pos, int rid, int oid)
  3406. {
  3407. struct script_state *st;
  3408. if( rootscript == NULL || pos < 0 )
  3409. return;
  3410. // TODO In jAthena, this function can take over the pending script in the player. [FlavioJS]
  3411. // It is unclear how that can be triggered, so it needs the be traced/checked in more detail.
  3412. // NOTE At the time of this change, this function wasn't capable of taking over the script state because st->scriptroot was never set.
  3413. st = script_alloc_state(rootscript, pos, rid, oid);
  3414. run_script_main(st);
  3415. }
  3416. void script_stop_sleeptimers(int id)
  3417. {
  3418. for(;;)
  3419. {
  3420. struct script_state* st = (struct script_state*)linkdb_erase(&sleep_db,(void*)__64BPRTSIZE(id));
  3421. if( st == NULL )
  3422. break; // no more sleep timers
  3423. script_free_state(st);
  3424. }
  3425. }
  3426. /*==========================================
  3427. * Delete the specified node from sleep_db
  3428. *------------------------------------------*/
  3429. struct linkdb_node* script_erase_sleepdb(struct linkdb_node *n)
  3430. {
  3431. struct linkdb_node *retnode;
  3432. if( n == NULL)
  3433. return NULL;
  3434. if( n->prev == NULL )
  3435. sleep_db = n->next;
  3436. else
  3437. n->prev->next = n->next;
  3438. if( n->next )
  3439. n->next->prev = n->prev;
  3440. retnode = n->next;
  3441. aFree( n );
  3442. return retnode; // The following; return retnode
  3443. }
  3444. /*==========================================
  3445. * Timer function for sleep
  3446. *------------------------------------------*/
  3447. int run_script_timer(int tid, unsigned int tick, int id, intptr_t data)
  3448. {
  3449. struct script_state *st = (struct script_state *)data;
  3450. struct linkdb_node *node = (struct linkdb_node *)sleep_db;
  3451. TBL_PC *sd = map_id2sd(st->rid);
  3452. if((sd && sd->status.char_id != id) || (st->rid && !sd))
  3453. { //Character mismatch. Cancel execution.
  3454. st->rid = 0;
  3455. st->state = END;
  3456. }
  3457. while( node && st->sleep.timer != INVALID_TIMER ) {
  3458. if( (int)__64BPRTSIZE(node->key) == st->oid && ((struct script_state *)node->data)->sleep.timer == st->sleep.timer ) {
  3459. script_erase_sleepdb(node);
  3460. st->sleep.timer = INVALID_TIMER;
  3461. break;
  3462. }
  3463. node = node->next;
  3464. }
  3465. if(st->state != RERUNLINE)
  3466. st->sleep.tick = 0;
  3467. run_script_main(st);
  3468. return 0;
  3469. }
  3470. /// Detaches script state from possibly attached character and restores it's previous script if any.
  3471. ///
  3472. /// @param st Script state to detach.
  3473. /// @param dequeue_event Whether to schedule any queued events, when there was no previous script.
  3474. static void script_detach_state(struct script_state* st, bool dequeue_event)
  3475. {
  3476. struct map_session_data* sd;
  3477. if(st->rid && (sd = map_id2sd(st->rid))!=NULL) {
  3478. sd->st = st->bk_st;
  3479. sd->npc_id = st->bk_npcid;
  3480. sd->state.disable_atcommand_on_npc = 0;
  3481. if(st->bk_st) {
  3482. //Remove tag for removal.
  3483. st->bk_st = NULL;
  3484. st->bk_npcid = 0;
  3485. } else if(dequeue_event) {
  3486. #ifdef SECURE_NPCTIMEOUT
  3487. /**
  3488. * We're done with this NPC session, so we cancel the timer (if existent) and move on
  3489. **/
  3490. if( sd->npc_idle_timer != INVALID_TIMER ) {
  3491. delete_timer(sd->npc_idle_timer,npc_rr_secure_timeout_timer);
  3492. sd->npc_idle_timer = INVALID_TIMER;
  3493. }
  3494. #endif
  3495. npc_event_dequeue(sd);
  3496. }
  3497. }
  3498. else if(st->bk_st)
  3499. {// rid was set to 0, before detaching the script state
  3500. 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);
  3501. script_reportsrc(st->bk_st);
  3502. script_free_state(st->bk_st);
  3503. st->bk_st = NULL;
  3504. }
  3505. }
  3506. /// Attaches script state to possibly attached character and backups it's previous script, if any.
  3507. ///
  3508. /// @param st Script state to attach.
  3509. static void script_attach_state(struct script_state* st)
  3510. {
  3511. struct map_session_data* sd;
  3512. if(st->rid && (sd = map_id2sd(st->rid))!=NULL)
  3513. {
  3514. if(st!=sd->st)
  3515. {
  3516. if(st->bk_st)
  3517. {// there is already a backup
  3518. 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);
  3519. }
  3520. st->bk_st = sd->st;
  3521. st->bk_npcid = sd->npc_id;
  3522. }
  3523. sd->st = st;
  3524. sd->npc_id = st->oid;
  3525. sd->npc_item_flag = st->npc_item_flag; // load default.
  3526. sd->state.disable_atcommand_on_npc = (!pc_has_permission(sd, PC_PERM_ENABLE_COMMAND));
  3527. #ifdef SECURE_NPCTIMEOUT
  3528. if( sd->npc_idle_timer == INVALID_TIMER )
  3529. sd->npc_idle_timer = add_timer(gettick() + (SECURE_NPCTIMEOUT_INTERVAL*1000),npc_rr_secure_timeout_timer,sd->bl.id,0);
  3530. sd->npc_idle_tick = gettick();
  3531. #endif
  3532. }
  3533. }
  3534. /*==========================================
  3535. * The main part of the script execution
  3536. *------------------------------------------*/
  3537. void run_script_main(struct script_state *st)
  3538. {
  3539. int cmdcount = script_config.check_cmdcount;
  3540. int gotocount = script_config.check_gotocount;
  3541. TBL_PC *sd;
  3542. struct script_stack *stack=st->stack;
  3543. script_attach_state(st);
  3544. if(st->state == RERUNLINE) {
  3545. run_func(st);
  3546. if(st->state == GOTO)
  3547. st->state = RUN;
  3548. } else if(st->state != END)
  3549. st->state = RUN;
  3550. while(st->state == RUN)
  3551. {
  3552. enum c_op c = get_com(st->script->script_buf,&st->pos);
  3553. switch(c){
  3554. case C_EOL:
  3555. if( stack->defsp > stack->sp )
  3556. ShowError("script:run_script_main: unexpected stack position (defsp=%d sp=%d). please report this!!!\n", stack->defsp, stack->sp);
  3557. else
  3558. pop_stack(st, stack->defsp, stack->sp);// pop unused stack data. (unused return value)
  3559. break;
  3560. case C_INT:
  3561. push_val(stack,C_INT,get_num(st->script->script_buf,&st->pos));
  3562. break;
  3563. case C_POS:
  3564. case C_NAME:
  3565. push_val(stack,c,GETVALUE(st->script->script_buf,st->pos));
  3566. st->pos+=3;
  3567. break;
  3568. case C_ARG:
  3569. push_val(stack,c,0);
  3570. break;
  3571. case C_STR:
  3572. push_str(stack,C_CONSTSTR,(char*)(st->script->script_buf+st->pos));
  3573. while(st->script->script_buf[st->pos++]);
  3574. break;
  3575. case C_FUNC:
  3576. run_func(st);
  3577. if(st->state==GOTO){
  3578. st->state = RUN;
  3579. if( !st->freeloop && gotocount>0 && (--gotocount)<=0 ){
  3580. ShowError("run_script: infinity loop !\n");
  3581. script_reportsrc(st);
  3582. st->state=END;
  3583. }
  3584. }
  3585. break;
  3586. case C_REF:
  3587. st->op2ref = 1;
  3588. break;
  3589. case C_NEG:
  3590. case C_NOT:
  3591. case C_LNOT:
  3592. op_1(st ,c);
  3593. break;
  3594. case C_ADD:
  3595. case C_SUB:
  3596. case C_MUL:
  3597. case C_DIV:
  3598. case C_MOD:
  3599. case C_EQ:
  3600. case C_NE:
  3601. case C_GT:
  3602. case C_GE:
  3603. case C_LT:
  3604. case C_LE:
  3605. case C_AND:
  3606. case C_OR:
  3607. case C_XOR:
  3608. case C_LAND:
  3609. case C_LOR:
  3610. case C_R_SHIFT:
  3611. case C_L_SHIFT:
  3612. op_2(st, c);
  3613. break;
  3614. case C_OP3:
  3615. op_3(st, c);
  3616. break;
  3617. case C_NOP:
  3618. st->state=END;
  3619. break;
  3620. default:
  3621. ShowError("unknown command : %d @ %d\n",c,st->pos);
  3622. st->state=END;
  3623. break;
  3624. }
  3625. if( !st->freeloop && cmdcount>0 && (--cmdcount)<=0 ){
  3626. ShowError("run_script: infinity loop !\n");
  3627. script_reportsrc(st);
  3628. st->state=END;
  3629. }
  3630. }
  3631. if(st->sleep.tick > 0) {
  3632. //Restore previous script
  3633. script_detach_state(st, false);
  3634. //Delay execution
  3635. sd = map_id2sd(st->rid); // Get sd since script might have attached someone while running. [Inkfish]
  3636. st->sleep.charid = sd?sd->status.char_id:0;
  3637. st->sleep.timer = add_timer(gettick()+st->sleep.tick,
  3638. run_script_timer, st->sleep.charid, (intptr_t)st);
  3639. linkdb_insert(&sleep_db, (void*)__64BPRTSIZE(st->oid), st);
  3640. }
  3641. else if(st->state != END && st->rid){
  3642. //Resume later (st is already attached to player).
  3643. if(st->bk_st) {
  3644. ShowWarning("Unable to restore stack! Double continuation!\n");
  3645. //Report BOTH scripts to see if that can help somehow.
  3646. ShowDebug("Previous script (lost):\n");
  3647. script_reportsrc(st->bk_st);
  3648. ShowDebug("Current script:\n");
  3649. script_reportsrc(st);
  3650. script_free_state(st->bk_st);
  3651. st->bk_st = NULL;
  3652. }
  3653. } else {
  3654. //Dispose of script.
  3655. if ((sd = map_id2sd(st->rid))!=NULL)
  3656. { //Restore previous stack and save char.
  3657. if(sd->state.using_fake_npc){
  3658. clif_clearunit_single(sd->npc_id, CLR_OUTSIGHT, sd->fd);
  3659. sd->state.using_fake_npc = 0;
  3660. }
  3661. //Restore previous script if any.
  3662. script_detach_state(st, true);
  3663. if (sd->state.reg_dirty&2)
  3664. intif_saveregistry(sd,2);
  3665. if (sd->state.reg_dirty&1)
  3666. intif_saveregistry(sd,1);
  3667. }
  3668. script_free_state(st);
  3669. }
  3670. }
  3671. int script_config_read(char *cfgName)
  3672. {
  3673. int i;
  3674. char line[1024],w1[1024],w2[1024];
  3675. FILE *fp;
  3676. fp=fopen(cfgName,"r");
  3677. if(fp==NULL){
  3678. ShowError("File not found: %s\n", cfgName);
  3679. return 1;
  3680. }
  3681. while(fgets(line, sizeof(line), fp))
  3682. {
  3683. if(line[0] == '/' && line[1] == '/')
  3684. continue;
  3685. i=sscanf(line,"%1023[^:]: %1023[^\r\n]",w1,w2);
  3686. if(i!=2)
  3687. continue;
  3688. if(strcmpi(w1,"warn_func_mismatch_paramnum")==0) {
  3689. script_config.warn_func_mismatch_paramnum = config_switch(w2);
  3690. }
  3691. else if(strcmpi(w1,"check_cmdcount")==0) {
  3692. script_config.check_cmdcount = config_switch(w2);
  3693. }
  3694. else if(strcmpi(w1,"check_gotocount")==0) {
  3695. script_config.check_gotocount = config_switch(w2);
  3696. }
  3697. else if(strcmpi(w1,"input_min_value")==0) {
  3698. script_config.input_min_value = config_switch(w2);
  3699. }
  3700. else if(strcmpi(w1,"input_max_value")==0) {
  3701. script_config.input_max_value = config_switch(w2);
  3702. }
  3703. else if(strcmpi(w1,"warn_func_mismatch_argtypes")==0) {
  3704. script_config.warn_func_mismatch_argtypes = config_switch(w2);
  3705. }
  3706. else if(strcmpi(w1,"import")==0){
  3707. script_config_read(w2);
  3708. }
  3709. else {
  3710. ShowWarning("Unknown setting '%s' in file %s\n", w1, cfgName);
  3711. }
  3712. }
  3713. fclose(fp);
  3714. return 0;
  3715. }
  3716. /**
  3717. * @see DBApply
  3718. */
  3719. static int db_script_free_code_sub(DBKey key, DBData *data, va_list ap)
  3720. {
  3721. struct script_code *code = db_data2ptr(data);
  3722. if (code)
  3723. script_free_code(code);
  3724. return 0;
  3725. }
  3726. void script_run_autobonus(const char *autobonus, struct map_session_data *sd, unsigned int pos)
  3727. {
  3728. struct script_code *script = (struct script_code *)strdb_get(autobonus_db, autobonus);
  3729. if( script )
  3730. {
  3731. int j;
  3732. ARR_FIND( 0, EQI_MAX, j, sd->equip_index[j] >= 0 && sd->status.inventory[sd->equip_index[j]].equip == pos );
  3733. if( j < EQI_MAX ) {
  3734. //Single item autobonus
  3735. current_equip_item_index = sd->equip_index[j];
  3736. current_equip_combo_pos = 0;
  3737. } else {
  3738. //Combo autobonus
  3739. current_equip_item_index = -1;
  3740. current_equip_combo_pos = pos;
  3741. }
  3742. run_script(script,0,sd->bl.id,0);
  3743. }
  3744. }
  3745. void script_add_autobonus(const char *autobonus)
  3746. {
  3747. if( strdb_get(autobonus_db, autobonus) == NULL )
  3748. {
  3749. struct script_code *script = parse_script(autobonus, "autobonus", 0, 0);
  3750. if( script )
  3751. strdb_put(autobonus_db, autobonus, script);
  3752. }
  3753. }
  3754. /// resets a temporary character array variable to given value
  3755. void script_cleararray_pc(struct map_session_data* sd, const char* varname, void* value)
  3756. {
  3757. int key;
  3758. uint8 idx;
  3759. if( not_array_variable(varname[0]) || !not_server_variable(varname[0]) )
  3760. {
  3761. ShowError("script_cleararray_pc: Variable '%s' has invalid scope (char_id=%d).\n", varname, sd->status.char_id);
  3762. return;
  3763. }
  3764. key = add_str(varname);
  3765. if( is_string_variable(varname) )
  3766. {
  3767. for( idx = 0; idx < SCRIPT_MAX_ARRAYSIZE; idx++ )
  3768. {
  3769. pc_setregstr(sd, reference_uid(key, idx), (const char*)value);
  3770. }
  3771. }
  3772. else
  3773. {
  3774. for( idx = 0; idx < SCRIPT_MAX_ARRAYSIZE; idx++ )
  3775. {
  3776. pc_setreg(sd, reference_uid(key, idx), (int)__64BPRTSIZE(value));
  3777. }
  3778. }
  3779. }
  3780. /// sets a temporary character array variable element idx to given value
  3781. /// @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.
  3782. void script_setarray_pc(struct map_session_data* sd, const char* varname, uint8 idx, void* value, int* refcache)
  3783. {
  3784. int key;
  3785. if( not_array_variable(varname[0]) || !not_server_variable(varname[0]) )
  3786. {
  3787. ShowError("script_setarray_pc: Variable '%s' has invalid scope (char_id=%d).\n", varname, sd->status.char_id);
  3788. return;
  3789. }
  3790. if( idx >= SCRIPT_MAX_ARRAYSIZE )
  3791. {
  3792. ShowError("script_setarray_pc: Variable '%s' has invalid index '%d' (char_id=%d).\n", varname, (int)idx, sd->status.char_id);
  3793. return;
  3794. }
  3795. key = ( refcache && refcache[0] ) ? refcache[0] : add_str(varname);
  3796. if( is_string_variable(varname) )
  3797. {
  3798. pc_setregstr(sd, reference_uid(key, idx), (const char*)value);
  3799. }
  3800. else
  3801. {
  3802. pc_setreg(sd, reference_uid(key, idx), (int)__64BPRTSIZE(value));
  3803. }
  3804. if( refcache )
  3805. {// save to avoid repeated add_str calls
  3806. refcache[0] = key;
  3807. }
  3808. }
  3809. #ifdef BETA_THREAD_TEST
  3810. int buildin_query_sql_sub(struct script_state* st, Sql* handle);
  3811. /* used to receive items the queryThread has already processed */
  3812. int queryThread_timer(int tid, unsigned int tick, int id, intptr_t data) {
  3813. int i, cursor = 0;
  3814. bool allOk = true;
  3815. EnterSpinLock(&queryThreadLock);
  3816. for( i = 0; i < queryThreadData.count; i++ ) {
  3817. struct queryThreadEntry *entry = queryThreadData.entry[i];
  3818. if( !entry->ok ) {
  3819. allOk = false;
  3820. continue;
  3821. }
  3822. run_script_main(entry->st);
  3823. entry->st = NULL;/* empty entries */
  3824. aFree(entry);
  3825. queryThreadData.entry[i] = NULL;
  3826. }
  3827. if( allOk ) {
  3828. /* cancel the repeating timer -- it'll re-create itself when necessary, dont need to remain looping */
  3829. delete_timer(queryThreadData.timer, queryThread_timer);
  3830. queryThreadData.timer = INVALID_TIMER;
  3831. }
  3832. /* now lets clear the mess. */
  3833. for( i = 0; i < queryThreadData.count; i++ ) {
  3834. struct queryThreadEntry *entry = queryThreadData.entry[i];
  3835. if( entry == NULL )
  3836. continue;/* entry on hold */
  3837. /* move */
  3838. memmove(&queryThreadData.entry[cursor], &queryThreadData.entry[i], sizeof(struct queryThreadEntry*));
  3839. cursor++;
  3840. }
  3841. queryThreadData.count = cursor;
  3842. LeaveSpinLock(&queryThreadLock);
  3843. return 0;
  3844. }
  3845. void queryThread_add(struct script_state *st, bool type) {
  3846. int idx = 0;
  3847. struct queryThreadEntry* entry = NULL;
  3848. EnterSpinLock(&queryThreadLock);
  3849. if( queryThreadData.count++ != 0 )
  3850. RECREATE(queryThreadData.entry, struct queryThreadEntry* , queryThreadData.count);
  3851. idx = queryThreadData.count-1;
  3852. CREATE(queryThreadData.entry[idx],struct queryThreadEntry,1);
  3853. entry = queryThreadData.entry[idx];
  3854. entry->st = st;
  3855. entry->ok = false;
  3856. entry->type = type;
  3857. if( queryThreadData.timer == INVALID_TIMER ) { /* start the receiver timer */
  3858. queryThreadData.timer = add_timer_interval(gettick() + 100, queryThread_timer, 0, 0, 100);
  3859. }
  3860. LeaveSpinLock(&queryThreadLock);
  3861. /* unlock the queryThread */
  3862. racond_signal(queryThreadCond);
  3863. }
  3864. /* adds a new log to the queue */
  3865. void queryThread_log(char * entry, int length) {
  3866. int idx = logThreadData.count;
  3867. EnterSpinLock(&queryThreadLock);
  3868. if( logThreadData.count++ != 0 )
  3869. RECREATE(logThreadData.entry, char* , logThreadData.count);
  3870. CREATE(logThreadData.entry[idx], char, length + 1 );
  3871. safestrncpy(logThreadData.entry[idx], entry, length + 1 );
  3872. LeaveSpinLock(&queryThreadLock);
  3873. /* unlock the queryThread */
  3874. racond_signal(queryThreadCond);
  3875. }
  3876. /* queryThread_main */
  3877. static void *queryThread_main(void *x) {
  3878. Sql *queryThread_handle = Sql_Malloc();
  3879. int i;
  3880. if ( SQL_ERROR == Sql_Connect(queryThread_handle, map_server_id, map_server_pw, map_server_ip, map_server_port, map_server_db) ){
  3881. ShowError("Couldn't connect with uname='%s',passwd='%s',host='%s',port='%d',database='%s'\n",
  3882. map_server_id, map_server_pw, map_server_ip, map_server_port, map_server_db);
  3883. Sql_ShowDebug(queryThread_handle);
  3884. Sql_Free(queryThread_handle);
  3885. exit(EXIT_FAILURE);
  3886. }
  3887. if( strlen(default_codepage) > 0 )
  3888. if ( SQL_ERROR == Sql_SetEncoding(queryThread_handle, default_codepage) )
  3889. Sql_ShowDebug(queryThread_handle);
  3890. if( log_config.sql_logs ) {
  3891. logmysql_handle = Sql_Malloc();
  3892. if ( SQL_ERROR == Sql_Connect(logmysql_handle, log_db_id, log_db_pw, log_db_ip, log_db_port, log_db_db) ){
  3893. ShowError("Couldn't connect with uname='%s',passwd='%s',host='%s',port='%d',database='%s'\n",
  3894. log_db_id, log_db_pw, log_db_ip, log_db_port, log_db_db);
  3895. Sql_ShowDebug(logmysql_handle);
  3896. Sql_Free(logmysql_handle);
  3897. exit(EXIT_FAILURE);
  3898. }
  3899. if( strlen(default_codepage) > 0 )
  3900. if ( SQL_ERROR == Sql_SetEncoding(logmysql_handle, default_codepage) )
  3901. Sql_ShowDebug(logmysql_handle);
  3902. }
  3903. while( 1 ) {
  3904. if(queryThreadTerminate > 0)
  3905. break;
  3906. EnterSpinLock(&queryThreadLock);
  3907. /* mess with queryThreadData within the lock */
  3908. for( i = 0; i < queryThreadData.count; i++ ) {
  3909. struct queryThreadEntry *entry = queryThreadData.entry[i];
  3910. if( entry->ok )
  3911. continue;
  3912. else if ( !entry->st || !entry->st->stack ) {
  3913. entry->ok = true;/* dispose */
  3914. continue;
  3915. }
  3916. buildin_query_sql_sub(entry->st, entry->type ? logmysql_handle : queryThread_handle);
  3917. entry->ok = true;/* we're done with this */
  3918. }
  3919. /* also check for any logs in need to be sent */
  3920. if( log_config.sql_logs ) {
  3921. for( i = 0; i < logThreadData.count; i++ ) {
  3922. if( SQL_ERROR == Sql_Query(logmysql_handle, logThreadData.entry[i]) )
  3923. Sql_ShowDebug(logmysql_handle);
  3924. aFree(logThreadData.entry[i]);
  3925. }
  3926. logThreadData.count = 0;
  3927. }
  3928. LeaveSpinLock(&queryThreadLock);
  3929. ramutex_lock( queryThreadMutex );
  3930. racond_wait( queryThreadCond, queryThreadMutex, -1 );
  3931. ramutex_unlock( queryThreadMutex );
  3932. }
  3933. Sql_Free(queryThread_handle);
  3934. if( log_config.sql_logs ) {
  3935. Sql_Free(logmysql_handle);
  3936. }
  3937. return NULL;
  3938. }
  3939. #endif
  3940. /*==========================================
  3941. * Destructor
  3942. *------------------------------------------*/
  3943. void do_final_script() {
  3944. int i;
  3945. #ifdef DEBUG_HASH
  3946. if (battle_config.etc_log)
  3947. {
  3948. FILE *fp = fopen("hash_dump.txt","wt");
  3949. if(fp) {
  3950. int count[SCRIPT_HASH_SIZE];
  3951. int count2[SCRIPT_HASH_SIZE]; // number of buckets with a certain number of items
  3952. int n=0;
  3953. int min=INT_MAX,max=0,zero=0;
  3954. double mean=0.0f;
  3955. double median=0.0f;
  3956. ShowNotice("Dumping script str hash information to hash_dump.txt\n");
  3957. memset(count, 0, sizeof(count));
  3958. fprintf(fp,"num : hash : data_name\n");
  3959. fprintf(fp,"---------------------------------------------------------------\n");
  3960. for(i=LABEL_START; i<str_num; i++) {
  3961. unsigned int h = calc_hash(get_str(i));
  3962. fprintf(fp,"%04d : %4u : %s\n",i,h, get_str(i));
  3963. ++count[h];
  3964. }
  3965. fprintf(fp,"--------------------\n\n");
  3966. memset(count2, 0, sizeof(count2));
  3967. for(i=0; i<SCRIPT_HASH_SIZE; i++) {
  3968. fprintf(fp," hash %3d = %d\n",i,count[i]);
  3969. if(min > count[i])
  3970. min = count[i]; // minimun count of collision
  3971. if(max < count[i])
  3972. max = count[i]; // maximun count of collision
  3973. if(count[i] == 0)
  3974. zero++;
  3975. ++count2[count[i]];
  3976. }
  3977. fprintf(fp,"\n--------------------\n items : buckets\n--------------------\n");
  3978. for( i=min; i <= max; ++i ){
  3979. fprintf(fp," %5d : %7d\n",i,count2[i]);
  3980. mean += 1.0f*i*count2[i]/SCRIPT_HASH_SIZE; // Note: this will always result in <nr labels>/<nr buckets>
  3981. }
  3982. for( i=min; i <= max; ++i ){
  3983. n += count2[i];
  3984. if( n*2 >= SCRIPT_HASH_SIZE )
  3985. {
  3986. if( SCRIPT_HASH_SIZE%2 == 0 && SCRIPT_HASH_SIZE/2 == n )
  3987. median = (i+i+1)/2.0f;
  3988. else
  3989. median = i;
  3990. break;
  3991. }
  3992. }
  3993. fprintf(fp,"--------------------\n min = %d, max = %d, zero = %d\n mean = %lf, median = %lf\n",min,max,zero,mean,median);
  3994. fclose(fp);
  3995. }
  3996. }
  3997. #endif
  3998. mapreg_final();
  3999. db_destroy(scriptlabel_db);
  4000. userfunc_db->destroy(userfunc_db, db_script_free_code_sub);
  4001. autobonus_db->destroy(autobonus_db, db_script_free_code_sub);
  4002. if(sleep_db) {
  4003. struct linkdb_node *n = (struct linkdb_node *)sleep_db;
  4004. while(n) {
  4005. struct script_state *st = (struct script_state *)n->data;
  4006. script_free_state(st);
  4007. n = n->next;
  4008. }
  4009. linkdb_final(&sleep_db);
  4010. }
  4011. if (str_data)
  4012. aFree(str_data);
  4013. if (str_buf)
  4014. aFree(str_buf);
  4015. for( i = 0; i < atcmd_binding_count; i++ ) {
  4016. aFree(atcmd_binding[i]);
  4017. }
  4018. if( atcmd_binding_count != 0 )
  4019. aFree(atcmd_binding);
  4020. #ifdef BETA_THREAD_TEST
  4021. /* QueryThread */
  4022. InterlockedIncrement(&queryThreadTerminate);
  4023. racond_signal(queryThreadCond);
  4024. rathread_wait(queryThread, NULL);
  4025. // Destroy cond var and mutex.
  4026. racond_destroy( queryThreadCond );
  4027. ramutex_destroy( queryThreadMutex );
  4028. /* Clear missing vars */
  4029. for( i = 0; i < queryThreadData.count; i++ ) {
  4030. aFree(queryThreadData.entry[i]);
  4031. }
  4032. aFree(queryThreadData.entry);
  4033. for( i = 0; i < logThreadData.count; i++ ) {
  4034. aFree(logThreadData.entry[i]);
  4035. }
  4036. aFree(logThreadData.entry);
  4037. #endif
  4038. }
  4039. /*==========================================
  4040. * Initialization
  4041. *------------------------------------------*/
  4042. void do_init_script(void) {
  4043. userfunc_db=strdb_alloc(DB_OPT_DUP_KEY,0);
  4044. scriptlabel_db=strdb_alloc(DB_OPT_DUP_KEY,50);
  4045. autobonus_db = strdb_alloc(DB_OPT_DUP_KEY,0);
  4046. mapreg_init();
  4047. #ifdef BETA_THREAD_TEST
  4048. CREATE(queryThreadData.entry, struct queryThreadEntry*, 1);
  4049. queryThreadData.count = 0;
  4050. CREATE(logThreadData.entry, char *, 1);
  4051. logThreadData.count = 0;
  4052. /* QueryThread Start */
  4053. InitializeSpinLock(&queryThreadLock);
  4054. queryThreadData.timer = INVALID_TIMER;
  4055. queryThreadTerminate = 0;
  4056. queryThreadMutex = ramutex_create();
  4057. queryThreadCond = racond_create();
  4058. queryThread = rathread_create(queryThread_main, NULL);
  4059. if(queryThread == NULL){
  4060. ShowFatalError("do_init_script: cannot spawn Query Thread.\n");
  4061. exit(EXIT_FAILURE);
  4062. }
  4063. add_timer_func_list(queryThread_timer, "queryThread_timer");
  4064. #endif
  4065. }
  4066. void script_reload(void) {
  4067. int i;
  4068. #ifdef BETA_THREAD_TEST
  4069. /* we're reloading so any queries undergoing should be...exterminated. */
  4070. EnterSpinLock(&queryThreadLock);
  4071. for( i = 0; i < queryThreadData.count; i++ ) {
  4072. aFree(queryThreadData.entry[i]);
  4073. }
  4074. queryThreadData.count = 0;
  4075. if( queryThreadData.timer != INVALID_TIMER ) {
  4076. delete_timer(queryThreadData.timer, queryThread_timer);
  4077. queryThreadData.timer = INVALID_TIMER;
  4078. }
  4079. LeaveSpinLock(&queryThreadLock);
  4080. #endif
  4081. userfunc_db->clear(userfunc_db, db_script_free_code_sub);
  4082. db_clear(scriptlabel_db);
  4083. // @commands (script based)
  4084. // Clear bindings
  4085. for( i = 0; i < atcmd_binding_count; i++ ) {
  4086. aFree(atcmd_binding[i]);
  4087. }
  4088. if( atcmd_binding_count != 0 )
  4089. aFree(atcmd_binding);
  4090. atcmd_binding_count = 0;
  4091. if(sleep_db) {
  4092. struct linkdb_node *n = (struct linkdb_node *)sleep_db;
  4093. while(n) {
  4094. struct script_state *st = (struct script_state *)n->data;
  4095. script_free_state(st);
  4096. n = n->next;
  4097. }
  4098. linkdb_final(&sleep_db);
  4099. }
  4100. mapreg_reload();
  4101. }
  4102. //-----------------------------------------------------------------------------
  4103. // buildin functions
  4104. //
  4105. #define BUILDIN_DEF(x,args) { buildin_ ## x , #x , args }
  4106. #define BUILDIN_DEF2(x,x2,args) { buildin_ ## x , x2 , args }
  4107. #define BUILDIN_FUNC(x) int buildin_ ## x (struct script_state* st)
  4108. /////////////////////////////////////////////////////////////////////
  4109. // NPC interaction
  4110. //
  4111. /// Appends a message to the npc dialog.
  4112. /// If a dialog doesn't exist yet, one is created.
  4113. ///
  4114. /// mes "<message>";
  4115. BUILDIN_FUNC(mes)
  4116. {
  4117. TBL_PC* sd = script_rid2sd(st);
  4118. if( sd == NULL )
  4119. return 0;
  4120. if( !script_hasdata(st, 3) )
  4121. {// only a single line detected in the script
  4122. clif_scriptmes(sd, st->oid, script_getstr(st, 2));
  4123. }
  4124. else
  4125. {// parse multiple lines as they exist
  4126. int i;
  4127. for( i = 2; script_hasdata(st, i); i++ )
  4128. {
  4129. // send the message to the client
  4130. clif_scriptmes(sd, st->oid, script_getstr(st, i));
  4131. }
  4132. }
  4133. st->mes_active = 1; // Invoking character has a NPC dialog box open.
  4134. return SCRIPT_CMD_SUCCESS;
  4135. }
  4136. /// Displays the button 'next' in the npc dialog.
  4137. /// The dialog text is cleared and the script continues when the button is pressed.
  4138. ///
  4139. /// next;
  4140. BUILDIN_FUNC(next)
  4141. {
  4142. TBL_PC* sd;
  4143. sd = script_rid2sd(st);
  4144. if( sd == NULL )
  4145. return 0;
  4146. #ifdef SECURE_NPCTIMEOUT
  4147. sd->npc_idle_type = NPCT_WAIT;
  4148. #endif
  4149. st->state = STOP;
  4150. clif_scriptnext(sd, st->oid);
  4151. return SCRIPT_CMD_SUCCESS;
  4152. }
  4153. /// Ends the script and displays the button 'close' on the npc dialog.
  4154. /// The dialog is closed when the button is pressed.
  4155. ///
  4156. /// close;
  4157. BUILDIN_FUNC(close)
  4158. {
  4159. TBL_PC* sd;
  4160. sd = script_rid2sd(st);
  4161. if( sd == NULL )
  4162. return 0;
  4163. if( !st->mes_active ) {
  4164. TBL_NPC* nd = map_id2nd(st->oid);
  4165. st->state = END; // Keep backwards compatibility.
  4166. ShowWarning("Incorrect use of 'close' command! (source:%s / path:%s)\n",nd?nd->name:"Unknown",nd?nd->path:"Unknown");
  4167. } else {
  4168. st->state = CLOSE;
  4169. st->mes_active = 0;
  4170. }
  4171. clif_scriptclose(sd, st->oid);
  4172. return SCRIPT_CMD_SUCCESS;
  4173. }
  4174. /// Displays the button 'close' on the npc dialog.
  4175. /// The dialog is closed and the script continues when the button is pressed.
  4176. ///
  4177. /// close2;
  4178. BUILDIN_FUNC(close2)
  4179. {
  4180. TBL_PC* sd;
  4181. sd = script_rid2sd(st);
  4182. if( sd == NULL )
  4183. return 0;
  4184. st->state = STOP;
  4185. if( st->mes_active )
  4186. st->mes_active = 0;
  4187. clif_scriptclose(sd, st->oid);
  4188. return SCRIPT_CMD_SUCCESS;
  4189. }
  4190. /// Counts the number of valid and total number of options in 'str'
  4191. /// If max_count > 0 the counting stops when that valid option is reached
  4192. /// total is incremented for each option (NULL is supported)
  4193. static int menu_countoptions(const char* str, int max_count, int* total)
  4194. {
  4195. int count = 0;
  4196. int bogus_total;
  4197. if( total == NULL )
  4198. total = &bogus_total;
  4199. ++(*total);
  4200. // initial empty options
  4201. while( *str == ':' )
  4202. {
  4203. ++str;
  4204. ++(*total);
  4205. }
  4206. // count menu options
  4207. while( *str != '\0' )
  4208. {
  4209. ++count;
  4210. --max_count;
  4211. if( max_count == 0 )
  4212. break;
  4213. while( *str != ':' && *str != '\0' )
  4214. ++str;
  4215. while( *str == ':' )
  4216. {
  4217. ++str;
  4218. ++(*total);
  4219. }
  4220. }
  4221. return count;
  4222. }
  4223. /// Displays a menu with options and goes to the target label.
  4224. /// The script is stopped if cancel is pressed.
  4225. /// Options with no text are not displayed in the client.
  4226. ///
  4227. /// Options can be grouped together, separated by the character ':' in the text:
  4228. /// ex: menu "A:B:C",L_target;
  4229. /// All these options go to the specified target label.
  4230. ///
  4231. /// The index of the selected option is put in the variable @menu.
  4232. /// Indexes start with 1 and are consistent with grouped and empty options.
  4233. /// ex: menu "A::B",-,"",L_Impossible,"C",-;
  4234. /// // displays "A", "B" and "C", corresponding to indexes 1, 3 and 5
  4235. ///
  4236. /// NOTE: the client closes the npc dialog when cancel is pressed
  4237. ///
  4238. /// menu "<option_text>",<target_label>{,"<option_text>",<target_label>,...};
  4239. BUILDIN_FUNC(menu)
  4240. {
  4241. int i;
  4242. const char* text;
  4243. TBL_PC* sd;
  4244. sd = script_rid2sd(st);
  4245. if( sd == NULL )
  4246. return 0;
  4247. #ifdef SECURE_NPCTIMEOUT
  4248. sd->npc_idle_type = NPCT_MENU;
  4249. #endif
  4250. // TODO detect multiple scripts waiting for input at the same time, and what to do when that happens
  4251. if( sd->state.menu_or_input == 0 )
  4252. {
  4253. struct StringBuf buf;
  4254. if( script_lastdata(st) % 2 == 0 )
  4255. {// argument count is not even (1st argument is at index 2)
  4256. ShowError("script:menu: illegal number of arguments (%d).\n", (script_lastdata(st) - 1));
  4257. st->state = END;
  4258. return SCRIPT_CMD_FAILURE;
  4259. }
  4260. StringBuf_Init(&buf);
  4261. sd->npc_menu = 0;
  4262. for( i = 2; i < script_lastdata(st); i += 2 )
  4263. {
  4264. struct script_data* data;
  4265. // menu options
  4266. text = script_getstr(st, i);
  4267. // target label
  4268. data = script_getdata(st, i+1);
  4269. if( !data_islabel(data) )
  4270. {// not a label
  4271. StringBuf_Destroy(&buf);
  4272. ShowError("script:menu: argument #%d (from 1) is not a label or label not found.\n", i);
  4273. script_reportdata(data);
  4274. st->state = END;
  4275. return SCRIPT_CMD_FAILURE;
  4276. }
  4277. // append option(s)
  4278. if( text[0] == '\0' )
  4279. continue;// empty string, ignore
  4280. if( sd->npc_menu > 0 )
  4281. StringBuf_AppendStr(&buf, ":");
  4282. StringBuf_AppendStr(&buf, text);
  4283. sd->npc_menu += menu_countoptions(text, 0, NULL);
  4284. }
  4285. st->state = RERUNLINE;
  4286. sd->state.menu_or_input = 1;
  4287. /**
  4288. * menus beyond this length crash the client (see bugreport:6402)
  4289. **/
  4290. if( StringBuf_Length(&buf) >= 2047 ) {
  4291. struct npc_data * nd = map_id2nd(st->oid);
  4292. char* menu;
  4293. CREATE(menu, char, 2048);
  4294. safestrncpy(menu, StringBuf_Value(&buf), 2047);
  4295. ShowWarning("NPC Menu too long! (source:%s / length:%d)\n",nd?nd->name:"Unknown",StringBuf_Length(&buf));
  4296. clif_scriptmenu(sd, st->oid, menu);
  4297. aFree(menu);
  4298. } else
  4299. clif_scriptmenu(sd, st->oid, StringBuf_Value(&buf));
  4300. StringBuf_Destroy(&buf);
  4301. if( sd->npc_menu >= 0xff )
  4302. {// 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
  4303. ShowWarning("buildin_menu: Too many options specified (current=%d, max=254).\n", sd->npc_menu);
  4304. script_reportsrc(st);
  4305. }
  4306. }
  4307. else if( sd->npc_menu == 0xff )
  4308. {// Cancel was pressed
  4309. sd->state.menu_or_input = 0;
  4310. st->state = END;
  4311. }
  4312. else
  4313. {// goto target label
  4314. int menu = 0;
  4315. sd->state.menu_or_input = 0;
  4316. if( sd->npc_menu <= 0 )
  4317. {
  4318. ShowDebug("script:menu: unexpected selection (%d)\n", sd->npc_menu);
  4319. st->state = END;
  4320. return SCRIPT_CMD_FAILURE;
  4321. }
  4322. // get target label
  4323. for( i = 2; i < script_lastdata(st); i += 2 )
  4324. {
  4325. text = script_getstr(st, i);
  4326. sd->npc_menu -= menu_countoptions(text, sd->npc_menu, &menu);
  4327. if( sd->npc_menu <= 0 )
  4328. break;// entry found
  4329. }
  4330. if( sd->npc_menu > 0 )
  4331. {// Invalid selection
  4332. ShowDebug("script:menu: selection is out of range (%d pairs are missing?) - please report this\n", sd->npc_menu);
  4333. st->state = END;
  4334. return SCRIPT_CMD_FAILURE;
  4335. }
  4336. if( !data_islabel(script_getdata(st, i + 1)) )
  4337. {// TODO remove this temporary crash-prevention code (fallback for multiple scripts requesting user input)
  4338. ShowError("script:menu: unexpected data in label argument\n");
  4339. script_reportdata(script_getdata(st, i + 1));
  4340. st->state = END;
  4341. return SCRIPT_CMD_FAILURE;
  4342. }
  4343. pc_setreg(sd, add_str("@menu"), menu);
  4344. st->pos = script_getnum(st, i + 1);
  4345. st->state = GOTO;
  4346. }
  4347. return SCRIPT_CMD_SUCCESS;
  4348. }
  4349. /// Displays a menu with options and returns the selected option.
  4350. /// Behaves like 'menu' without the target labels.
  4351. ///
  4352. /// select(<option_text>{,<option_text>,...}) -> <selected_option>
  4353. ///
  4354. /// @see menu
  4355. BUILDIN_FUNC(select)
  4356. {
  4357. int i;
  4358. const char* text;
  4359. TBL_PC* sd;
  4360. sd = script_rid2sd(st);
  4361. if( sd == NULL )
  4362. return 0;
  4363. #ifdef SECURE_NPCTIMEOUT
  4364. sd->npc_idle_type = NPCT_MENU;
  4365. #endif
  4366. if( sd->state.menu_or_input == 0 ) {
  4367. struct StringBuf buf;
  4368. StringBuf_Init(&buf);
  4369. sd->npc_menu = 0;
  4370. for( i = 2; i <= script_lastdata(st); ++i ) {
  4371. text = script_getstr(st, i);
  4372. if( sd->npc_menu > 0 )
  4373. StringBuf_AppendStr(&buf, ":");
  4374. StringBuf_AppendStr(&buf, text);
  4375. sd->npc_menu += menu_countoptions(text, 0, NULL);
  4376. }
  4377. st->state = RERUNLINE;
  4378. sd->state.menu_or_input = 1;
  4379. /**
  4380. * menus beyond this length crash the client (see bugreport:6402)
  4381. **/
  4382. if( StringBuf_Length(&buf) >= 2047 ) {
  4383. struct npc_data * nd = map_id2nd(st->oid);
  4384. char* menu;
  4385. CREATE(menu, char, 2048);
  4386. safestrncpy(menu, StringBuf_Value(&buf), 2047);
  4387. ShowWarning("NPC Menu too long! (source:%s / length:%d)\n",nd?nd->name:"Unknown",StringBuf_Length(&buf));
  4388. clif_scriptmenu(sd, st->oid, menu);
  4389. aFree(menu);
  4390. } else
  4391. clif_scriptmenu(sd, st->oid, StringBuf_Value(&buf));
  4392. StringBuf_Destroy(&buf);
  4393. if( sd->npc_menu >= 0xff ) {
  4394. ShowWarning("buildin_select: Too many options specified (current=%d, max=254).\n", sd->npc_menu);
  4395. script_reportsrc(st);
  4396. }
  4397. } else if( sd->npc_menu == 0xff ) {// Cancel was pressed
  4398. sd->state.menu_or_input = 0;
  4399. st->state = END;
  4400. } else {// return selected option
  4401. int menu = 0;
  4402. sd->state.menu_or_input = 0;
  4403. for( i = 2; i <= script_lastdata(st); ++i ) {
  4404. text = script_getstr(st, i);
  4405. sd->npc_menu -= menu_countoptions(text, sd->npc_menu, &menu);
  4406. if( sd->npc_menu <= 0 )
  4407. break;// entry found
  4408. }
  4409. pc_setreg(sd, add_str("@menu"), menu);
  4410. script_pushint(st, menu);
  4411. st->state = RUN;
  4412. }
  4413. return SCRIPT_CMD_SUCCESS;
  4414. }
  4415. /// Displays a menu with options and returns the selected option.
  4416. /// Behaves like 'menu' without the target labels, except when cancel is
  4417. /// pressed.
  4418. /// When cancel is pressed, the script continues and 255 is returned.
  4419. ///
  4420. /// prompt(<option_text>{,<option_text>,...}) -> <selected_option>
  4421. ///
  4422. /// @see menu
  4423. BUILDIN_FUNC(prompt)
  4424. {
  4425. int i;
  4426. const char *text;
  4427. TBL_PC* sd;
  4428. sd = script_rid2sd(st);
  4429. if( sd == NULL )
  4430. return 0;
  4431. #ifdef SECURE_NPCTIMEOUT
  4432. sd->npc_idle_type = NPCT_MENU;
  4433. #endif
  4434. if( sd->state.menu_or_input == 0 )
  4435. {
  4436. struct StringBuf buf;
  4437. StringBuf_Init(&buf);
  4438. sd->npc_menu = 0;
  4439. for( i = 2; i <= script_lastdata(st); ++i )
  4440. {
  4441. text = script_getstr(st, i);
  4442. if( sd->npc_menu > 0 )
  4443. StringBuf_AppendStr(&buf, ":");
  4444. StringBuf_AppendStr(&buf, text);
  4445. sd->npc_menu += menu_countoptions(text, 0, NULL);
  4446. }
  4447. st->state = RERUNLINE;
  4448. sd->state.menu_or_input = 1;
  4449. /**
  4450. * menus beyond this length crash the client (see bugreport:6402)
  4451. **/
  4452. if( StringBuf_Length(&buf) >= 2047 ) {
  4453. struct npc_data * nd = map_id2nd(st->oid);
  4454. char* menu;
  4455. CREATE(menu, char, 2048);
  4456. safestrncpy(menu, StringBuf_Value(&buf), 2047);
  4457. ShowWarning("NPC Menu too long! (source:%s / length:%d)\n",nd?nd->name:"Unknown",StringBuf_Length(&buf));
  4458. clif_scriptmenu(sd, st->oid, menu);
  4459. aFree(menu);
  4460. } else
  4461. clif_scriptmenu(sd, st->oid, StringBuf_Value(&buf));
  4462. StringBuf_Destroy(&buf);
  4463. if( sd->npc_menu >= 0xff )
  4464. {
  4465. ShowWarning("buildin_prompt: Too many options specified (current=%d, max=254).\n", sd->npc_menu);
  4466. script_reportsrc(st);
  4467. }
  4468. }
  4469. else if( sd->npc_menu == 0xff )
  4470. {// Cancel was pressed
  4471. sd->state.menu_or_input = 0;
  4472. pc_setreg(sd, add_str("@menu"), 0xff);
  4473. script_pushint(st, 0xff);
  4474. st->state = RUN;
  4475. }
  4476. else
  4477. {// return selected option
  4478. int menu = 0;
  4479. sd->state.menu_or_input = 0;
  4480. for( i = 2; i <= script_lastdata(st); ++i )
  4481. {
  4482. text = script_getstr(st, i);
  4483. sd->npc_menu -= menu_countoptions(text, sd->npc_menu, &menu);
  4484. if( sd->npc_menu <= 0 )
  4485. break;// entry found
  4486. }
  4487. pc_setreg(sd, add_str("@menu"), menu);
  4488. script_pushint(st, menu);
  4489. st->state = RUN;
  4490. }
  4491. return SCRIPT_CMD_SUCCESS;
  4492. }
  4493. /////////////////////////////////////////////////////////////////////
  4494. // ...
  4495. //
  4496. /// Jumps to the target script label.
  4497. ///
  4498. /// goto <label>;
  4499. BUILDIN_FUNC(goto)
  4500. {
  4501. if( !data_islabel(script_getdata(st,2)) )
  4502. {
  4503. ShowError("script:goto: not a label\n");
  4504. script_reportdata(script_getdata(st,2));
  4505. st->state = END;
  4506. return 1;
  4507. }
  4508. st->pos = script_getnum(st,2);
  4509. st->state = GOTO;
  4510. return SCRIPT_CMD_SUCCESS;
  4511. }
  4512. /*==========================================
  4513. * user-defined function call
  4514. *------------------------------------------*/
  4515. BUILDIN_FUNC(callfunc)
  4516. {
  4517. int i, j;
  4518. struct script_retinfo* ri;
  4519. struct script_code* scr;
  4520. const char* str = script_getstr(st,2);
  4521. DBMap **ref = NULL;
  4522. scr = (struct script_code*)strdb_get(userfunc_db, str);
  4523. if( !scr )
  4524. {
  4525. ShowError("script:callfunc: function not found! [%s]\n", str);
  4526. st->state = END;
  4527. return 1;
  4528. }
  4529. for( i = st->start+3, j = 0; i < st->end; i++, j++ )
  4530. {
  4531. struct script_data* data = push_copy(st->stack,i);
  4532. if( data_isreference(data) && !data->ref )
  4533. {
  4534. const char* name = reference_getname(data);
  4535. if( name[0] == '.' ) {
  4536. if(!ref){
  4537. ref = (struct DBMap**)aCalloc(sizeof(struct DBMap*), 1);
  4538. ref[0] = (name[1] == '@' ? st->stack->var_function : st->script->script_vars);
  4539. }
  4540. data->ref = ref;
  4541. }
  4542. }
  4543. }
  4544. CREATE(ri, struct script_retinfo, 1);
  4545. ri->script = st->script;// script code
  4546. ri->var_function = st->stack->var_function;// scope variables
  4547. ri->pos = st->pos;// script location
  4548. ri->nargs = j;// argument count
  4549. ri->defsp = st->stack->defsp;// default stack pointer
  4550. push_retinfo(st->stack, ri, ref);
  4551. st->pos = 0;
  4552. st->script = scr;
  4553. st->stack->defsp = st->stack->sp;
  4554. st->state = GOTO;
  4555. st->stack->var_function = idb_alloc(DB_OPT_RELEASE_DATA);
  4556. return SCRIPT_CMD_SUCCESS;
  4557. }
  4558. /*==========================================
  4559. * subroutine call
  4560. *------------------------------------------*/
  4561. BUILDIN_FUNC(callsub)
  4562. {
  4563. int i,j;
  4564. struct script_retinfo* ri;
  4565. int pos = script_getnum(st,2);
  4566. DBMap **ref = NULL;
  4567. if( !data_islabel(script_getdata(st,2)) && !data_isfunclabel(script_getdata(st,2)) )
  4568. {
  4569. ShowError("script:callsub: argument is not a label\n");
  4570. script_reportdata(script_getdata(st,2));
  4571. st->state = END;
  4572. return 1;
  4573. }
  4574. for( i = st->start+3, j = 0; i < st->end; i++, j++ )
  4575. {
  4576. struct script_data* data = push_copy(st->stack,i);
  4577. if( data_isreference(data) && !data->ref )
  4578. {
  4579. const char* name = reference_getname(data);
  4580. if( name[0] == '.' && name[1] == '@' ) {
  4581. if ( !ref ) {
  4582. ref = (struct DBMap**)aCalloc(sizeof(struct DBMap*), 1);
  4583. ref[0] = st->stack->var_function;
  4584. }
  4585. data->ref = ref;
  4586. }
  4587. }
  4588. }
  4589. CREATE(ri, struct script_retinfo, 1);
  4590. ri->script = st->script;// script code
  4591. ri->var_function = st->stack->var_function;// scope variables
  4592. ri->pos = st->pos;// script location
  4593. ri->nargs = j;// argument count
  4594. ri->defsp = st->stack->defsp;// default stack pointer
  4595. push_retinfo(st->stack, ri, ref);
  4596. st->pos = pos;
  4597. st->stack->defsp = st->stack->sp;
  4598. st->state = GOTO;
  4599. st->stack->var_function = idb_alloc(DB_OPT_RELEASE_DATA);
  4600. return SCRIPT_CMD_SUCCESS;
  4601. }
  4602. /// Retrieves an argument provided to callfunc/callsub.
  4603. /// If the argument doesn't exist
  4604. ///
  4605. /// getarg(<index>{,<default_value>}) -> <value>
  4606. BUILDIN_FUNC(getarg)
  4607. {
  4608. struct script_retinfo* ri;
  4609. int idx;
  4610. if( st->stack->defsp < 1 || st->stack->stack_data[st->stack->defsp - 1].type != C_RETINFO )
  4611. {
  4612. ShowError("script:getarg: no callfunc or callsub!\n");
  4613. st->state = END;
  4614. return 1;
  4615. }
  4616. ri = st->stack->stack_data[st->stack->defsp - 1].u.ri;
  4617. idx = script_getnum(st,2);
  4618. if( idx >= 0 && idx < ri->nargs )
  4619. push_copy(st->stack, st->stack->defsp - 1 - ri->nargs + idx);
  4620. else if( script_hasdata(st,3) )
  4621. script_pushcopy(st, 3);
  4622. else
  4623. {
  4624. ShowError("script:getarg: index (idx=%d) out of range (nargs=%d) and no default value found\n", idx, ri->nargs);
  4625. st->state = END;
  4626. return 1;
  4627. }
  4628. return SCRIPT_CMD_SUCCESS;
  4629. }
  4630. /// Returns from the current function, optionaly returning a value from the functions.
  4631. /// Don't use outside script functions.
  4632. ///
  4633. /// return;
  4634. /// return <value>;
  4635. BUILDIN_FUNC(return)
  4636. {
  4637. if( script_hasdata(st,2) )
  4638. {// return value
  4639. struct script_data* data;
  4640. script_pushcopy(st, 2);
  4641. data = script_getdatatop(st, -1);
  4642. if( data_isreference(data) )
  4643. {
  4644. const char* name = reference_getname(data);
  4645. if( name[0] == '.' && name[1] == '@' )
  4646. {// scope variable
  4647. if( !data->ref || data->ref == (DBMap**)&st->stack->var_function )
  4648. get_val(st, data);// current scope, convert to value
  4649. }
  4650. else if( name[0] == '.' && !data->ref )
  4651. {// script variable, link to current script
  4652. data->ref = &st->script->script_vars;
  4653. }
  4654. }
  4655. }
  4656. else
  4657. {// no return value
  4658. script_pushnil(st);
  4659. }
  4660. st->state = RETFUNC;
  4661. return SCRIPT_CMD_SUCCESS;
  4662. }
  4663. /// Returns a random number from 0 to <range>-1.
  4664. /// Or returns a random number from <min> to <max>.
  4665. /// If <min> is greater than <max>, their numbers are switched.
  4666. /// rand(<range>) -> <int>
  4667. /// rand(<min>,<max>) -> <int>
  4668. BUILDIN_FUNC(rand)
  4669. {
  4670. int range;
  4671. int min;
  4672. if( script_hasdata(st,3) )
  4673. {// min,max
  4674. int max = script_getnum(st,3);
  4675. min = script_getnum(st,2);
  4676. if( max < min )
  4677. swap(min, max);
  4678. range = max - min + 1;
  4679. }
  4680. else
  4681. {// range
  4682. min = 0;
  4683. range = script_getnum(st,2);
  4684. }
  4685. if( range <= 1 )
  4686. script_pushint(st, min);
  4687. else
  4688. script_pushint(st, rnd()%range + min);
  4689. return SCRIPT_CMD_SUCCESS;
  4690. }
  4691. /*==========================================
  4692. * Warp sd to str,x,y or Random or SavePoint/Save
  4693. *------------------------------------------*/
  4694. BUILDIN_FUNC(warp)
  4695. {
  4696. int ret;
  4697. int x,y;
  4698. const char* str;
  4699. TBL_PC* sd;
  4700. sd = script_rid2sd(st);
  4701. if( sd == NULL )
  4702. return 0;
  4703. str = script_getstr(st,2);
  4704. x = script_getnum(st,3);
  4705. y = script_getnum(st,4);
  4706. if(strcmp(str,"Random")==0)
  4707. ret = pc_randomwarp(sd,CLR_TELEPORT);
  4708. else if(strcmp(str,"SavePoint")==0 || strcmp(str,"Save")==0)
  4709. ret = pc_setpos(sd,sd->status.save_point.map,sd->status.save_point.x,sd->status.save_point.y,CLR_TELEPORT);
  4710. else
  4711. ret = pc_setpos(sd,mapindex_name2id(str),x,y,CLR_OUTSIGHT);
  4712. if( ret ) {
  4713. ShowError("buildin_warp: moving player '%s' to \"%s\",%d,%d failed.\n", sd->status.name, str, x, y);
  4714. return 1;
  4715. }
  4716. return SCRIPT_CMD_SUCCESS;
  4717. }
  4718. /*==========================================
  4719. * Warp a specified area
  4720. *------------------------------------------*/
  4721. static int buildin_areawarp_sub(struct block_list *bl,va_list ap)
  4722. {
  4723. int x2,y2,x3,y3;
  4724. unsigned int index;
  4725. index = va_arg(ap,unsigned int);
  4726. x2 = va_arg(ap,int);
  4727. y2 = va_arg(ap,int);
  4728. x3 = va_arg(ap,int);
  4729. y3 = va_arg(ap,int);
  4730. if(index == 0)
  4731. pc_randomwarp((TBL_PC *)bl,CLR_TELEPORT);
  4732. else if(x3 && y3) {
  4733. int max, tx, ty, j = 0;
  4734. // choose a suitable max number of attempts
  4735. if( (max = (y3-y2+1)*(x3-x2+1)*3) > 1000 )
  4736. max = 1000;
  4737. // find a suitable map cell
  4738. do {
  4739. tx = rnd()%(x3-x2+1)+x2;
  4740. ty = rnd()%(y3-y2+1)+y2;
  4741. j++;
  4742. } while( map_getcell(index,tx,ty,CELL_CHKNOPASS) && j < max );
  4743. pc_setpos((TBL_PC *)bl,index,tx,ty,CLR_OUTSIGHT);
  4744. }
  4745. else
  4746. pc_setpos((TBL_PC *)bl,index,x2,y2,CLR_OUTSIGHT);
  4747. return 0;
  4748. }
  4749. BUILDIN_FUNC(areawarp)
  4750. {
  4751. int16 m, x0,y0,x1,y1, x2,y2,x3=0,y3=0;
  4752. unsigned int index;
  4753. const char *str;
  4754. const char *mapname;
  4755. mapname = script_getstr(st,2);
  4756. x0 = script_getnum(st,3);
  4757. y0 = script_getnum(st,4);
  4758. x1 = script_getnum(st,5);
  4759. y1 = script_getnum(st,6);
  4760. str = script_getstr(st,7);
  4761. x2 = script_getnum(st,8);
  4762. y2 = script_getnum(st,9);
  4763. if( script_hasdata(st,10) && script_hasdata(st,11) ) { // Warp area to area
  4764. if( (x3 = script_getnum(st,10)) < 0 || (y3 = script_getnum(st,11)) < 0 ){
  4765. x3 = 0;
  4766. y3 = 0;
  4767. } else if( x3 && y3 ) {
  4768. // normalize x3/y3 coordinates
  4769. if( x3 < x2 ) swap(x3,x2);
  4770. if( y3 < y2 ) swap(y3,y2);
  4771. }
  4772. }
  4773. if( (m = map_mapname2mapid(mapname)) < 0 )
  4774. return 0;
  4775. if( strcmp(str,"Random") == 0 )
  4776. index = 0;
  4777. else if( !(index=mapindex_name2id(str)) )
  4778. return 0;
  4779. map_foreachinarea(buildin_areawarp_sub, m,x0,y0,x1,y1, BL_PC, index,x2,y2,x3,y3);
  4780. return SCRIPT_CMD_SUCCESS;
  4781. }
  4782. /*==========================================
  4783. * areapercentheal <map>,<x1>,<y1>,<x2>,<y2>,<hp>,<sp>
  4784. *------------------------------------------*/
  4785. static int buildin_areapercentheal_sub(struct block_list *bl,va_list ap)
  4786. {
  4787. int hp, sp;
  4788. hp = va_arg(ap, int);
  4789. sp = va_arg(ap, int);
  4790. pc_percentheal((TBL_PC *)bl,hp,sp);
  4791. return 0;
  4792. }
  4793. BUILDIN_FUNC(areapercentheal)
  4794. {
  4795. int hp,sp,m;
  4796. const char *mapname;
  4797. int x0,y0,x1,y1;
  4798. mapname=script_getstr(st,2);
  4799. x0=script_getnum(st,3);
  4800. y0=script_getnum(st,4);
  4801. x1=script_getnum(st,5);
  4802. y1=script_getnum(st,6);
  4803. hp=script_getnum(st,7);
  4804. sp=script_getnum(st,8);
  4805. if( (m=map_mapname2mapid(mapname))< 0)
  4806. return 0;
  4807. map_foreachinarea(buildin_areapercentheal_sub,m,x0,y0,x1,y1,BL_PC,hp,sp);
  4808. return SCRIPT_CMD_SUCCESS;
  4809. }
  4810. /*==========================================
  4811. * warpchar [LuzZza]
  4812. * Useful for warp one player from
  4813. * another player npc-session.
  4814. * Using: warpchar "mapname",x,y,Char_ID;
  4815. *------------------------------------------*/
  4816. BUILDIN_FUNC(warpchar)
  4817. {
  4818. int x,y,a;
  4819. const char *str;
  4820. TBL_PC *sd;
  4821. str=script_getstr(st,2);
  4822. x=script_getnum(st,3);
  4823. y=script_getnum(st,4);
  4824. a=script_getnum(st,5);
  4825. sd = map_charid2sd(a);
  4826. if( sd == NULL )
  4827. return 0;
  4828. if(strcmp(str, "Random") == 0)
  4829. pc_randomwarp(sd, CLR_TELEPORT);
  4830. else
  4831. if(strcmp(str, "SavePoint") == 0)
  4832. pc_setpos(sd, sd->status.save_point.map,sd->status.save_point.x, sd->status.save_point.y, CLR_TELEPORT);
  4833. else
  4834. pc_setpos(sd, mapindex_name2id(str), x, y, CLR_TELEPORT);
  4835. return SCRIPT_CMD_SUCCESS;
  4836. }
  4837. /*==========================================
  4838. * Warpparty - [Fredzilla] [Paradox924X]
  4839. * Syntax: warpparty "to_mapname",x,y,Party_ID,{"from_mapname"};
  4840. * If 'from_mapname' is specified, only the party members on that map will be warped
  4841. *------------------------------------------*/
  4842. BUILDIN_FUNC(warpparty)
  4843. {
  4844. TBL_PC *sd = NULL;
  4845. TBL_PC *pl_sd;
  4846. struct party_data* p;
  4847. int type;
  4848. int mapindex;
  4849. int i;
  4850. const char* str = script_getstr(st,2);
  4851. int x = script_getnum(st,3);
  4852. int y = script_getnum(st,4);
  4853. int p_id = script_getnum(st,5);
  4854. const char* str2 = NULL;
  4855. if ( script_hasdata(st,6) )
  4856. str2 = script_getstr(st,6);
  4857. p = party_search(p_id);
  4858. if(!p)
  4859. return 0;
  4860. type = ( strcmp(str,"Random")==0 ) ? 0
  4861. : ( strcmp(str,"SavePointAll")==0 ) ? 1
  4862. : ( strcmp(str,"SavePoint")==0 ) ? 2
  4863. : ( strcmp(str,"Leader")==0 ) ? 3
  4864. : 4;
  4865. switch (type)
  4866. {
  4867. case 3:
  4868. for(i = 0; i < MAX_PARTY && !p->party.member[i].leader; i++);
  4869. if (i == MAX_PARTY || !p->data[i].sd) //Leader not found / not online
  4870. return 0;
  4871. pl_sd = p->data[i].sd;
  4872. mapindex = pl_sd->mapindex;
  4873. x = pl_sd->bl.x;
  4874. y = pl_sd->bl.y;
  4875. break;
  4876. case 4:
  4877. mapindex = mapindex_name2id(str);
  4878. break;
  4879. case 2:
  4880. //"SavePoint" uses save point of the currently attached player
  4881. if (( sd = script_rid2sd(st) ) == NULL )
  4882. return 0;
  4883. default:
  4884. mapindex = 0;
  4885. break;
  4886. }
  4887. for (i = 0; i < MAX_PARTY; i++)
  4888. {
  4889. if( !(pl_sd = p->data[i].sd) || pl_sd->status.party_id != p_id )
  4890. continue;
  4891. if( str2 && strcmp(str2, map[pl_sd->bl.m].name) != 0 )
  4892. continue;
  4893. if( pc_isdead(pl_sd) )
  4894. continue;
  4895. switch( type )
  4896. {
  4897. case 0: // Random
  4898. if(!map[pl_sd->bl.m].flag.nowarp)
  4899. pc_randomwarp(pl_sd,CLR_TELEPORT);
  4900. break;
  4901. case 1: // SavePointAll
  4902. if(!map[pl_sd->bl.m].flag.noreturn)
  4903. pc_setpos(pl_sd,pl_sd->status.save_point.map,pl_sd->status.save_point.x,pl_sd->status.save_point.y,CLR_TELEPORT);
  4904. break;
  4905. case 2: // SavePoint
  4906. if(!map[pl_sd->bl.m].flag.noreturn)
  4907. pc_setpos(pl_sd,sd->status.save_point.map,sd->status.save_point.x,sd->status.save_point.y,CLR_TELEPORT);
  4908. break;
  4909. case 3: // Leader
  4910. case 4: // m,x,y
  4911. if(!map[pl_sd->bl.m].flag.noreturn && !map[pl_sd->bl.m].flag.nowarp)
  4912. pc_setpos(pl_sd,mapindex,x,y,CLR_TELEPORT);
  4913. break;
  4914. }
  4915. }
  4916. return SCRIPT_CMD_SUCCESS;
  4917. }
  4918. /*==========================================
  4919. * Warpguild - [Fredzilla]
  4920. * Syntax: warpguild "mapname",x,y,Guild_ID;
  4921. *------------------------------------------*/
  4922. BUILDIN_FUNC(warpguild)
  4923. {
  4924. TBL_PC *sd = NULL;
  4925. TBL_PC *pl_sd;
  4926. struct guild* g;
  4927. struct s_mapiterator* iter;
  4928. int type;
  4929. const char* str = script_getstr(st,2);
  4930. int x = script_getnum(st,3);
  4931. int y = script_getnum(st,4);
  4932. int gid = script_getnum(st,5);
  4933. g = guild_search(gid);
  4934. if( g == NULL )
  4935. return 0;
  4936. type = ( strcmp(str,"Random")==0 ) ? 0
  4937. : ( strcmp(str,"SavePointAll")==0 ) ? 1
  4938. : ( strcmp(str,"SavePoint")==0 ) ? 2
  4939. : 3;
  4940. if( type == 2 && ( sd = script_rid2sd(st) ) == NULL )
  4941. {// "SavePoint" uses save point of the currently attached player
  4942. return 0;
  4943. }
  4944. iter = mapit_getallusers();
  4945. for( pl_sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); pl_sd = (TBL_PC*)mapit_next(iter) )
  4946. {
  4947. if( pl_sd->status.guild_id != gid )
  4948. continue;
  4949. switch( type )
  4950. {
  4951. case 0: // Random
  4952. if(!map[pl_sd->bl.m].flag.nowarp)
  4953. pc_randomwarp(pl_sd,CLR_TELEPORT);
  4954. break;
  4955. case 1: // SavePointAll
  4956. if(!map[pl_sd->bl.m].flag.noreturn)
  4957. pc_setpos(pl_sd,pl_sd->status.save_point.map,pl_sd->status.save_point.x,pl_sd->status.save_point.y,CLR_TELEPORT);
  4958. break;
  4959. case 2: // SavePoint
  4960. if(!map[pl_sd->bl.m].flag.noreturn)
  4961. pc_setpos(pl_sd,sd->status.save_point.map,sd->status.save_point.x,sd->status.save_point.y,CLR_TELEPORT);
  4962. break;
  4963. case 3: // m,x,y
  4964. if(!map[pl_sd->bl.m].flag.noreturn && !map[pl_sd->bl.m].flag.nowarp)
  4965. pc_setpos(pl_sd,mapindex_name2id(str),x,y,CLR_TELEPORT);
  4966. break;
  4967. }
  4968. }
  4969. mapit_free(iter);
  4970. return SCRIPT_CMD_SUCCESS;
  4971. }
  4972. /*==========================================
  4973. * Force Heal a player (hp and sp)
  4974. *------------------------------------------*/
  4975. BUILDIN_FUNC(heal)
  4976. {
  4977. TBL_PC *sd;
  4978. int hp,sp;
  4979. sd = script_rid2sd(st);
  4980. if (!sd) return 0;
  4981. hp=script_getnum(st,2);
  4982. sp=script_getnum(st,3);
  4983. status_heal(&sd->bl, hp, sp, 1);
  4984. return SCRIPT_CMD_SUCCESS;
  4985. }
  4986. /*==========================================
  4987. * Heal a player by item (get vit bonus etc)
  4988. *------------------------------------------*/
  4989. BUILDIN_FUNC(itemheal)
  4990. {
  4991. TBL_PC *sd;
  4992. int hp,sp;
  4993. hp=script_getnum(st,2);
  4994. sp=script_getnum(st,3);
  4995. if(potion_flag==1) {
  4996. potion_hp = hp;
  4997. potion_sp = sp;
  4998. return 0;
  4999. }
  5000. sd = script_rid2sd(st);
  5001. if (!sd) return 0;
  5002. pc_itemheal(sd,sd->itemid,hp,sp);
  5003. return SCRIPT_CMD_SUCCESS;
  5004. }
  5005. /*==========================================
  5006. *
  5007. *------------------------------------------*/
  5008. BUILDIN_FUNC(percentheal)
  5009. {
  5010. int hp,sp;
  5011. TBL_PC* sd;
  5012. hp=script_getnum(st,2);
  5013. sp=script_getnum(st,3);
  5014. if(potion_flag==1) {
  5015. potion_per_hp = hp;
  5016. potion_per_sp = sp;
  5017. return 0;
  5018. }
  5019. sd = script_rid2sd(st);
  5020. if( sd == NULL )
  5021. return 0;
  5022. #ifdef RENEWAL
  5023. if( sd->sc.data[SC_EXTREMITYFIST2] )
  5024. sp = 0;
  5025. #endif
  5026. pc_percentheal(sd,hp,sp);
  5027. return SCRIPT_CMD_SUCCESS;
  5028. }
  5029. /*==========================================
  5030. *
  5031. *------------------------------------------*/
  5032. BUILDIN_FUNC(jobchange)
  5033. {
  5034. int job, upper=-1;
  5035. job=script_getnum(st,2);
  5036. if( script_hasdata(st,3) )
  5037. upper=script_getnum(st,3);
  5038. if (pcdb_checkid(job))
  5039. {
  5040. TBL_PC* sd;
  5041. sd = script_rid2sd(st);
  5042. if( sd == NULL )
  5043. return 0;
  5044. pc_jobchange(sd, job, upper);
  5045. }
  5046. return SCRIPT_CMD_SUCCESS;
  5047. }
  5048. /*==========================================
  5049. *
  5050. *------------------------------------------*/
  5051. BUILDIN_FUNC(jobname)
  5052. {
  5053. int class_=script_getnum(st,2);
  5054. script_pushconststr(st, (char*)job_name(class_));
  5055. return SCRIPT_CMD_SUCCESS;
  5056. }
  5057. /// Get input from the player.
  5058. /// For numeric inputs the value is capped to the range [min,max]. Returns 1 if
  5059. /// the value was higher than 'max', -1 if lower than 'min' and 0 otherwise.
  5060. /// For string inputs it returns 1 if the string was longer than 'max', -1 is
  5061. /// shorter than 'min' and 0 otherwise.
  5062. ///
  5063. /// input(<var>{,<min>{,<max>}}) -> <int>
  5064. BUILDIN_FUNC(input)
  5065. {
  5066. TBL_PC* sd;
  5067. struct script_data* data;
  5068. int uid;
  5069. const char* name;
  5070. int min;
  5071. int max;
  5072. sd = script_rid2sd(st);
  5073. if( sd == NULL )
  5074. return 0;
  5075. data = script_getdata(st,2);
  5076. if( !data_isreference(data) ){
  5077. ShowError("script:input: not a variable\n");
  5078. script_reportdata(data);
  5079. st->state = END;
  5080. return 1;
  5081. }
  5082. uid = reference_getuid(data);
  5083. name = reference_getname(data);
  5084. min = (script_hasdata(st,3) ? script_getnum(st,3) : script_config.input_min_value);
  5085. max = (script_hasdata(st,4) ? script_getnum(st,4) : script_config.input_max_value);
  5086. #ifdef SECURE_NPCTIMEOUT
  5087. sd->npc_idle_type = NPCT_WAIT;
  5088. #endif
  5089. if( !sd->state.menu_or_input )
  5090. { // first invocation, display npc input box
  5091. sd->state.menu_or_input = 1;
  5092. st->state = RERUNLINE;
  5093. if( is_string_variable(name) )
  5094. clif_scriptinputstr(sd,st->oid);
  5095. else
  5096. clif_scriptinput(sd,st->oid);
  5097. }
  5098. else
  5099. { // take received text/value and store it in the designated variable
  5100. sd->state.menu_or_input = 0;
  5101. if( is_string_variable(name) )
  5102. {
  5103. int len = (int)strlen(sd->npc_str);
  5104. set_reg(st, sd, uid, name, (void*)sd->npc_str, script_getref(st,2));
  5105. script_pushint(st, (len > max ? 1 : len < min ? -1 : 0));
  5106. }
  5107. else
  5108. {
  5109. int amount = sd->npc_amount;
  5110. set_reg(st, sd, uid, name, (void*)__64BPRTSIZE(cap_value(amount,min,max)), script_getref(st,2));
  5111. script_pushint(st, (amount > max ? 1 : amount < min ? -1 : 0));
  5112. }
  5113. st->state = RUN;
  5114. }
  5115. return SCRIPT_CMD_SUCCESS;
  5116. }
  5117. // declare the copyarray method here for future reference
  5118. BUILDIN_FUNC(copyarray);
  5119. /// Sets the value of a variable.
  5120. /// The value is converted to the type of the variable.
  5121. ///
  5122. /// set(<variable>,<value>{,<charid>})
  5123. BUILDIN_FUNC(set)
  5124. {
  5125. TBL_PC* sd = NULL;
  5126. struct script_data* data;
  5127. //struct script_data* datavalue;
  5128. int num;
  5129. const char* name;
  5130. char prefix;
  5131. data = script_getdata(st,2);
  5132. //datavalue = script_getdata(st,3);
  5133. if( !data_isreference(data) )
  5134. {
  5135. ShowError("script:set: not a variable\n");
  5136. script_reportdata(script_getdata(st,2));
  5137. st->state = END;
  5138. return 1;
  5139. }
  5140. num = reference_getuid(data);
  5141. name = reference_getname(data);
  5142. prefix = *name;
  5143. if (not_server_variable(prefix) && !script_charid2sd(4,sd)) {
  5144. ShowError("buildin_set: No player attached for player variable '%s'\n", name);
  5145. return SCRIPT_CMD_FAILURE;
  5146. }
  5147. #if 0
  5148. if( data_isreference(datavalue) )
  5149. {// the value being referenced is a variable
  5150. const char* namevalue = reference_getname(datavalue);
  5151. if( !not_array_variable(*namevalue) )
  5152. {// array variable being copied into another array variable
  5153. if( sd == NULL && not_server_variable(*namevalue) && !(sd = script_rid2sd(st)) )
  5154. {// player must be attached in order to copy a player variable
  5155. ShowError("script:set: no player attached for player variable '%s'\n", namevalue);
  5156. return 0;
  5157. }
  5158. if( is_string_variable(namevalue) != is_string_variable(name) )
  5159. {// non-matching array value types
  5160. ShowWarning("script:set: two array variables do not match in type.\n");
  5161. return 0;
  5162. }
  5163. // push the maximum number of array values to the stack
  5164. push_val(st->stack, C_INT, SCRIPT_MAX_ARRAYSIZE);
  5165. // call the copy array method directly
  5166. return buildin_copyarray(st);
  5167. }
  5168. }
  5169. #endif
  5170. if( is_string_variable(name) )
  5171. set_reg(st,sd,num,name,(void*)script_getstr(st,3),script_getref(st,2));
  5172. else
  5173. set_reg(st,sd,num,name,(void*)__64BPRTSIZE(script_getnum(st,3)),script_getref(st,2));
  5174. // return a copy of the variable reference
  5175. script_pushcopy(st,2);
  5176. return SCRIPT_CMD_SUCCESS;
  5177. }
  5178. /////////////////////////////////////////////////////////////////////
  5179. /// Array variables
  5180. ///
  5181. /// Returns the size of the specified array
  5182. static int32 getarraysize(struct script_state* st, int32 id, int32 idx, int isstring, struct DBMap** ref)
  5183. {
  5184. int32 ret = idx;
  5185. if( isstring )
  5186. {
  5187. for( ; idx < SCRIPT_MAX_ARRAYSIZE; ++idx )
  5188. {
  5189. char* str = (char*)get_val2(st, reference_uid(id, idx), ref);
  5190. if( str && *str )
  5191. ret = idx + 1;
  5192. script_removetop(st, -1, 0);
  5193. }
  5194. }
  5195. else
  5196. {
  5197. for( ; idx < SCRIPT_MAX_ARRAYSIZE; ++idx )
  5198. {
  5199. int32 num = (int32)__64BPRTSIZE(get_val2(st, reference_uid(id, idx), ref));
  5200. if( num )
  5201. ret = idx + 1;
  5202. script_removetop(st, -1, 0);
  5203. }
  5204. }
  5205. return ret;
  5206. }
  5207. /// Sets values of an array, from the starting index.
  5208. /// ex: setarray arr[1],1,2,3;
  5209. ///
  5210. /// setarray <array variable>,<value1>{,<value2>...};
  5211. BUILDIN_FUNC(setarray)
  5212. {
  5213. struct script_data* data;
  5214. const char* name;
  5215. int32 start;
  5216. int32 end;
  5217. int32 id;
  5218. int32 i;
  5219. TBL_PC* sd = NULL;
  5220. data = script_getdata(st, 2);
  5221. if( !data_isreference(data) )
  5222. {
  5223. ShowError("script:setarray: not a variable\n");
  5224. script_reportdata(data);
  5225. st->state = END;
  5226. return 1;// not a variable
  5227. }
  5228. id = reference_getid(data);
  5229. start = reference_getindex(data);
  5230. name = reference_getname(data);
  5231. if( not_array_variable(*name) )
  5232. {
  5233. ShowError("script:setarray: illegal scope\n");
  5234. script_reportdata(data);
  5235. st->state = END;
  5236. return 1;// not supported
  5237. }
  5238. if( not_server_variable(*name) )
  5239. {
  5240. sd = script_rid2sd(st);
  5241. if( sd == NULL )
  5242. return 0;// no player attached
  5243. }
  5244. end = start + script_lastdata(st) - 2;
  5245. if( end > SCRIPT_MAX_ARRAYSIZE )
  5246. end = SCRIPT_MAX_ARRAYSIZE;
  5247. if( is_string_variable(name) )
  5248. {// string array
  5249. for( i = 3; start < end; ++start, ++i )
  5250. set_reg(st, sd, reference_uid(id, start), name, (void*)script_getstr(st,i), reference_getref(data));
  5251. }
  5252. else
  5253. {// int array
  5254. for( i = 3; start < end; ++start, ++i )
  5255. set_reg(st, sd, reference_uid(id, start), name, (void*)__64BPRTSIZE(script_getnum(st,i)), reference_getref(data));
  5256. }
  5257. return SCRIPT_CMD_SUCCESS;
  5258. }
  5259. /// Sets count values of an array, from the starting index.
  5260. /// ex: cleararray arr[0],0,1;
  5261. ///
  5262. /// cleararray <array variable>,<value>,<count>;
  5263. BUILDIN_FUNC(cleararray)
  5264. {
  5265. struct script_data* data;
  5266. const char* name;
  5267. int32 start;
  5268. int32 end;
  5269. int32 id;
  5270. void* v;
  5271. TBL_PC* sd = NULL;
  5272. data = script_getdata(st, 2);
  5273. if( !data_isreference(data) )
  5274. {
  5275. ShowError("script:cleararray: not a variable\n");
  5276. script_reportdata(data);
  5277. st->state = END;
  5278. return 1;// not a variable
  5279. }
  5280. id = reference_getid(data);
  5281. start = reference_getindex(data);
  5282. name = reference_getname(data);
  5283. if( not_array_variable(*name) )
  5284. {
  5285. ShowError("script:cleararray: illegal scope\n");
  5286. script_reportdata(data);
  5287. st->state = END;
  5288. return 1;// not supported
  5289. }
  5290. if( not_server_variable(*name) )
  5291. {
  5292. sd = script_rid2sd(st);
  5293. if( sd == NULL )
  5294. return 0;// no player attached
  5295. }
  5296. if( is_string_variable(name) )
  5297. v = (void*)script_getstr(st, 3);
  5298. else
  5299. v = (void*)__64BPRTSIZE(script_getnum(st, 3));
  5300. end = start + script_getnum(st, 4);
  5301. if( end > SCRIPT_MAX_ARRAYSIZE )
  5302. end = SCRIPT_MAX_ARRAYSIZE;
  5303. for( ; start < end; ++start )
  5304. set_reg(st, sd, reference_uid(id, start), name, v, script_getref(st,2));
  5305. return SCRIPT_CMD_SUCCESS;
  5306. }
  5307. /// Copies data from one array to another.
  5308. /// ex: copyarray arr[0],arr[2],2;
  5309. ///
  5310. /// copyarray <destination array variable>,<source array variable>,<count>;
  5311. BUILDIN_FUNC(copyarray)
  5312. {
  5313. struct script_data* data1;
  5314. struct script_data* data2;
  5315. const char* name1;
  5316. const char* name2;
  5317. int32 idx1;
  5318. int32 idx2;
  5319. int32 id1;
  5320. int32 id2;
  5321. void* v;
  5322. int32 i;
  5323. int32 count;
  5324. TBL_PC* sd = NULL;
  5325. data1 = script_getdata(st, 2);
  5326. data2 = script_getdata(st, 3);
  5327. if( !data_isreference(data1) || !data_isreference(data2) )
  5328. {
  5329. ShowError("script:copyarray: not a variable\n");
  5330. script_reportdata(data1);
  5331. script_reportdata(data2);
  5332. st->state = END;
  5333. return 1;// not a variable
  5334. }
  5335. id1 = reference_getid(data1);
  5336. id2 = reference_getid(data2);
  5337. idx1 = reference_getindex(data1);
  5338. idx2 = reference_getindex(data2);
  5339. name1 = reference_getname(data1);
  5340. name2 = reference_getname(data2);
  5341. if( not_array_variable(*name1) || not_array_variable(*name2) )
  5342. {
  5343. ShowError("script:copyarray: illegal scope\n");
  5344. script_reportdata(data1);
  5345. script_reportdata(data2);
  5346. st->state = END;
  5347. return 1;// not supported
  5348. }
  5349. if( is_string_variable(name1) != is_string_variable(name2) )
  5350. {
  5351. ShowError("script:copyarray: type mismatch\n");
  5352. script_reportdata(data1);
  5353. script_reportdata(data2);
  5354. st->state = END;
  5355. return 1;// data type mismatch
  5356. }
  5357. if( not_server_variable(*name1) || not_server_variable(*name2) )
  5358. {
  5359. sd = script_rid2sd(st);
  5360. if( sd == NULL )
  5361. return 0;// no player attached
  5362. }
  5363. count = script_getnum(st, 4);
  5364. if( count > SCRIPT_MAX_ARRAYSIZE - idx1 )
  5365. count = SCRIPT_MAX_ARRAYSIZE - idx1;
  5366. if( count <= 0 || (id1 == id2 && idx1 == idx2) )
  5367. return 0;// nothing to copy
  5368. if( id1 == id2 && idx1 > idx2 )
  5369. {// destination might be overlapping the source - copy in reverse order
  5370. for( i = count - 1; i >= 0; --i )
  5371. {
  5372. v = get_val2(st, reference_uid(id2, idx2 + i), reference_getref(data2));
  5373. set_reg(st, sd, reference_uid(id1, idx1 + i), name1, v, reference_getref(data1));
  5374. script_removetop(st, -1, 0);
  5375. }
  5376. }
  5377. else
  5378. {// normal copy
  5379. for( i = 0; i < count; ++i )
  5380. {
  5381. if( idx2 + i < SCRIPT_MAX_ARRAYSIZE )
  5382. {
  5383. v = get_val2(st, reference_uid(id2, idx2 + i), reference_getref(data2));
  5384. set_reg(st, sd, reference_uid(id1, idx1 + i), name1, v, reference_getref(data1));
  5385. script_removetop(st, -1, 0);
  5386. }
  5387. else// out of range - assume ""/0
  5388. set_reg(st, sd, reference_uid(id1, idx1 + i), name1, (is_string_variable(name1)?(void*)"":(void*)0), reference_getref(data1));
  5389. }
  5390. }
  5391. return SCRIPT_CMD_SUCCESS;
  5392. }
  5393. /// Returns the size of the array.
  5394. /// Assumes that everything before the starting index exists.
  5395. /// ex: getarraysize(arr[3])
  5396. ///
  5397. /// getarraysize(<array variable>) -> <int>
  5398. BUILDIN_FUNC(getarraysize)
  5399. {
  5400. struct script_data* data;
  5401. const char* name;
  5402. data = script_getdata(st, 2);
  5403. if( !data_isreference(data) )
  5404. {
  5405. ShowError("script:getarraysize: not a variable\n");
  5406. script_reportdata(data);
  5407. script_pushnil(st);
  5408. st->state = END;
  5409. return 1;// not a variable
  5410. }
  5411. name = reference_getname(data);
  5412. if( not_array_variable(*name) )
  5413. {
  5414. ShowError("script:getarraysize: illegal scope\n");
  5415. script_reportdata(data);
  5416. script_pushnil(st);
  5417. st->state = END;
  5418. return 1;// not supported
  5419. }
  5420. script_pushint(st, getarraysize(st, reference_getid(data), reference_getindex(data), is_string_variable(name), reference_getref(data)));
  5421. return SCRIPT_CMD_SUCCESS;
  5422. }
  5423. /// Deletes count or all the elements in an array, from the starting index.
  5424. /// ex: deletearray arr[4],2;
  5425. ///
  5426. /// deletearray <array variable>;
  5427. /// deletearray <array variable>,<count>;
  5428. BUILDIN_FUNC(deletearray)
  5429. {
  5430. struct script_data* data;
  5431. const char* name;
  5432. int start;
  5433. int end;
  5434. int id;
  5435. TBL_PC *sd = NULL;
  5436. data = script_getdata(st, 2);
  5437. if( !data_isreference(data) )
  5438. {
  5439. ShowError("script:deletearray: not a variable\n");
  5440. script_reportdata(data);
  5441. st->state = END;
  5442. return 1;// not a variable
  5443. }
  5444. id = reference_getid(data);
  5445. start = reference_getindex(data);
  5446. name = reference_getname(data);
  5447. if( not_array_variable(*name) )
  5448. {
  5449. ShowError("script:deletearray: illegal scope\n");
  5450. script_reportdata(data);
  5451. st->state = END;
  5452. return 1;// not supported
  5453. }
  5454. if( not_server_variable(*name) )
  5455. {
  5456. sd = script_rid2sd(st);
  5457. if( sd == NULL )
  5458. return 0;// no player attached
  5459. }
  5460. end = SCRIPT_MAX_ARRAYSIZE;
  5461. if( start >= end )
  5462. return 0;// nothing to free
  5463. if( script_hasdata(st,3) )
  5464. {
  5465. int count = script_getnum(st, 3);
  5466. if( count > end - start )
  5467. count = end - start;
  5468. if( count <= 0 )
  5469. return 0;// nothing to free
  5470. // move rest of the elements backward
  5471. for( ; start + count < end; ++start )
  5472. {
  5473. void* v = get_val2(st, reference_uid(id, start + count), reference_getref(data));
  5474. set_reg(st, sd, reference_uid(id, start), name, v, reference_getref(data));
  5475. script_removetop(st, -1, 0);
  5476. }
  5477. }
  5478. // clear the rest of the array
  5479. if( is_string_variable(name) )
  5480. {
  5481. for( ; start < end; ++start )
  5482. set_reg(st, sd, reference_uid(id, start), name, (void *)"", reference_getref(data));
  5483. }
  5484. else
  5485. {
  5486. for( ; start < end; ++start )
  5487. set_reg(st, sd, reference_uid(id, start), name, (void*)0, reference_getref(data));
  5488. }
  5489. return SCRIPT_CMD_SUCCESS;
  5490. }
  5491. /// Returns a reference to the target index of the array variable.
  5492. /// Equivalent to var[index].
  5493. ///
  5494. /// getelementofarray(<array variable>,<index>) -> <variable reference>
  5495. BUILDIN_FUNC(getelementofarray)
  5496. {
  5497. struct script_data* data;
  5498. const char* name;
  5499. int32 id;
  5500. int i;
  5501. data = script_getdata(st, 2);
  5502. if( !data_isreference(data) )
  5503. {
  5504. ShowError("script:getelementofarray: not a variable\n");
  5505. script_reportdata(data);
  5506. script_pushnil(st);
  5507. st->state = END;
  5508. return 1;// not a variable
  5509. }
  5510. id = reference_getid(data);
  5511. name = reference_getname(data);
  5512. if( not_array_variable(*name) )
  5513. {
  5514. ShowError("script:getelementofarray: illegal scope\n");
  5515. script_reportdata(data);
  5516. script_pushnil(st);
  5517. st->state = END;
  5518. return 1;// not supported
  5519. }
  5520. i = script_getnum(st, 3);
  5521. if( i < 0 || i >= SCRIPT_MAX_ARRAYSIZE )
  5522. {
  5523. ShowWarning("script:getelementofarray: index out of range (%d)\n", i);
  5524. script_reportdata(data);
  5525. script_pushnil(st);
  5526. st->state = END;
  5527. return 1;// out of range
  5528. }
  5529. push_val2(st->stack, C_NAME, reference_uid(id, i), reference_getref(data));
  5530. return SCRIPT_CMD_SUCCESS;
  5531. }
  5532. /////////////////////////////////////////////////////////////////////
  5533. /// ...
  5534. ///
  5535. /*==========================================
  5536. *
  5537. *------------------------------------------*/
  5538. BUILDIN_FUNC(setlook)
  5539. {
  5540. int type,val;
  5541. TBL_PC* sd;
  5542. type=script_getnum(st,2);
  5543. val=script_getnum(st,3);
  5544. sd = script_rid2sd(st);
  5545. if( sd == NULL )
  5546. return 0;
  5547. pc_changelook(sd,type,val);
  5548. return SCRIPT_CMD_SUCCESS;
  5549. }
  5550. BUILDIN_FUNC(changelook)
  5551. { // As setlook but only client side
  5552. int type,val;
  5553. TBL_PC* sd;
  5554. type=script_getnum(st,2);
  5555. val=script_getnum(st,3);
  5556. sd = script_rid2sd(st);
  5557. if( sd == NULL )
  5558. return 0;
  5559. clif_changelook(&sd->bl,type,val);
  5560. return SCRIPT_CMD_SUCCESS;
  5561. }
  5562. /*==========================================
  5563. *
  5564. *------------------------------------------*/
  5565. BUILDIN_FUNC(cutin)
  5566. {
  5567. TBL_PC* sd;
  5568. sd = script_rid2sd(st);
  5569. if( sd == NULL )
  5570. return 0;
  5571. clif_cutin(sd,script_getstr(st,2),script_getnum(st,3));
  5572. return SCRIPT_CMD_SUCCESS;
  5573. }
  5574. /*==========================================
  5575. *
  5576. *------------------------------------------*/
  5577. BUILDIN_FUNC(viewpoint)
  5578. {
  5579. int type,x,y,id,color;
  5580. TBL_PC* sd;
  5581. type=script_getnum(st,2);
  5582. x=script_getnum(st,3);
  5583. y=script_getnum(st,4);
  5584. id=script_getnum(st,5);
  5585. color=script_getnum(st,6);
  5586. sd = script_rid2sd(st);
  5587. if( sd == NULL )
  5588. return 0;
  5589. clif_viewpoint(sd,st->oid,type,x,y,id,color);
  5590. return SCRIPT_CMD_SUCCESS;
  5591. }
  5592. /// Returns number of items in inventory/cart/storage
  5593. /// countitem <nameID>{,<accountID>});
  5594. /// countitem2 <nameID>,<Identified>,<Refine>,<Attribute>,<Card0>,<Card1>,<Card2>,<Card3>{,<accountID>}) [Lupus]
  5595. /// cartcountitem <nameID>{,<accountID>});
  5596. /// cartcountitem2 <nameID>,<Identified>,<Refine>,<Attribute>,<Card0>,<Card1>,<Card2>,<Card3>{,<accountID>})
  5597. /// storagecountitem <nameID>{,<accountID>});
  5598. /// storagecountitem2 <nameID>,<Identified>,<Refine>,<Attribute>,<Card0>,<Card1>,<Card2>,<Card3>{,<accountID>})
  5599. BUILDIN_FUNC(countitem)
  5600. {
  5601. int i = 0, count = 0, aid = 3;
  5602. struct item_data* id = NULL;
  5603. struct script_data* data;
  5604. char *command = (char *)script_getfuncname(st);
  5605. uint8 loc = 0;
  5606. uint16 size;
  5607. struct item *items;
  5608. TBL_PC *sd = NULL;
  5609. if( command[strlen(command)-1] == '2' ) {
  5610. i = 1;
  5611. aid = 10;
  5612. }
  5613. if( script_hasdata(st,aid) ) {
  5614. if( !(sd = map_id2sd( (aid = script_getnum(st, aid)) )) ) {
  5615. ShowError("buildin_%s: player not found (AID=%d).\n", command, aid);
  5616. st->state = END;
  5617. return SCRIPT_CMD_FAILURE;
  5618. }
  5619. }
  5620. else {
  5621. if( !(sd = script_rid2sd(st)) )
  5622. return SCRIPT_CMD_SUCCESS;
  5623. }
  5624. if( !strncmp(command, "cart", 4) ) {
  5625. loc = 1;
  5626. size = MAX_CART;
  5627. items = sd->status.cart;
  5628. }
  5629. else if( !strncmp(command, "storage", 7) ) {
  5630. loc = 2;
  5631. size = MAX_STORAGE;
  5632. items = sd->status.storage.items;
  5633. }
  5634. //TODO: 3 - Guild Storage
  5635. else {
  5636. size = MAX_INVENTORY;
  5637. items = sd->status.inventory;
  5638. }
  5639. if( loc == 1 && !pc_iscarton(sd) ) {
  5640. ShowError("buildin_%s: Player doesn't have cart (CID:%d).\n", command, sd->status.char_id);
  5641. script_pushint(st,-1);
  5642. return SCRIPT_CMD_SUCCESS;
  5643. }
  5644. data = script_getdata(st, 2);
  5645. get_val(st, data); // Convert into value in case of a variable
  5646. if( data_isstring(data) ) // item name
  5647. id = itemdb_searchname(conv_str(st, data));
  5648. else // item id
  5649. id = itemdb_exists(conv_num(st, data));
  5650. if( id == NULL ) {
  5651. ShowError("buildin_%s: Invalid item '%s'.\n", command, script_getstr(st,2)); // returns string, regardless of what it was
  5652. script_pushint(st,0);
  5653. return 1;
  5654. }
  5655. if( !i ) { // For count/cart/storagecountitem function
  5656. unsigned short nameid = id->nameid;
  5657. for( i = 0; i < size; i++ )
  5658. if( &items[i] && items[i].nameid == nameid )
  5659. count += items[i].amount;
  5660. }
  5661. else { // For count/cart/storagecountitem2 function
  5662. unsigned short nameid;
  5663. int iden, ref, attr, c1, c2, c3, c4;
  5664. nameid = id->nameid;
  5665. iden = script_getnum(st,3);
  5666. ref = script_getnum(st,4);
  5667. attr = script_getnum(st,5);
  5668. c1 = script_getnum(st,6);
  5669. c2 = script_getnum(st,7);
  5670. c3 = script_getnum(st,8);
  5671. c4 = script_getnum(st,9);
  5672. for( i = 0; i < size; i++ )
  5673. if( &items[i] && items[i].nameid > 0 && items[i].nameid == nameid &&
  5674. items[i].amount > 0 && items[i].identify == iden &&
  5675. items[i].refine == ref && items[i].attribute == attr &&
  5676. items[i].card[0] == c1 && items[i].card[1] == c2 &&
  5677. items[i].card[2] == c3 && items[i].card[3] == c4 )
  5678. {
  5679. count += items[i].amount;
  5680. }
  5681. }
  5682. script_pushint(st, count);
  5683. return SCRIPT_CMD_SUCCESS;
  5684. }
  5685. int checkweight_sub(TBL_PC *sd, int nbargs, unsigned short *eitemid, int32 *eamount)
  5686. {
  5687. struct item_data* id = NULL;
  5688. unsigned short nameid;
  5689. int amount;
  5690. uint32 weight=0;
  5691. uint16 amount2=0,slots,i;
  5692. slots = pc_inventoryblank(sd); //nb of empty slot
  5693. for(i=0; i<nbargs; i++) {
  5694. if(!eitemid[i])
  5695. continue;
  5696. id = itemdb_exists(eitemid[i]);
  5697. if( id == NULL ) {
  5698. ShowError("checkweight_sub: Invalid item '%hu'.\n", eitemid[i]);
  5699. return 0;
  5700. }
  5701. nameid = id->nameid;
  5702. amount = eamount[i];
  5703. if( amount < 1 ) {
  5704. ShowError("checkweight_sub: Invalid amount '%d'.\n", eamount[i]);
  5705. return 0;
  5706. }
  5707. weight += (id->weight)*amount; //total weight for all chk
  5708. if( weight + sd->weight > sd->max_weight ) // too heavy
  5709. return 0;
  5710. switch( pc_checkadditem(sd, nameid, amount) ) {
  5711. case CHKADDITEM_EXIST: // item is already in inventory, but there is still space for the requested amount
  5712. break;
  5713. case CHKADDITEM_NEW:
  5714. if( itemdb_isstackable(nameid) )
  5715. amount2++; // stackable
  5716. else
  5717. amount2 += amount; // non-stackable
  5718. if( slots < amount2)
  5719. return 0;
  5720. break;
  5721. case CHKADDITEM_OVERAMOUNT:
  5722. return 0;
  5723. }
  5724. }
  5725. return 1;
  5726. }
  5727. /*==========================================
  5728. * Check if item with this amount can fit in inventory
  5729. * Checking : weight, stack amount >32k, slots amount >(MAX_INVENTORY)
  5730. * Return
  5731. * 0 : fail
  5732. * 1 : success (npc side only)
  5733. *------------------------------------------*/
  5734. BUILDIN_FUNC(checkweight)
  5735. {
  5736. struct map_session_data* sd;
  5737. struct item_data* id = NULL;
  5738. unsigned short nameid[SCRIPT_MAX_ARRAYSIZE];
  5739. int32 amount[SCRIPT_MAX_ARRAYSIZE];
  5740. uint16 nbargs,i,j=0;
  5741. if( ( sd = script_rid2sd(st) ) == NULL )
  5742. return 0;
  5743. nbargs = script_lastdata(st)+1;
  5744. if(nbargs%2) {
  5745. ShowError("buildin_checkweight: Invalid nb of args should be a multiple of 2.\n");
  5746. script_pushint(st,0);
  5747. return 1;
  5748. }
  5749. for(i=2; i<nbargs; i=i+2) {
  5750. struct script_data* data = script_getdata(st,i);
  5751. get_val(st, data); // Convert into value in case of a variable
  5752. if( data_isstring(data) ) // item name
  5753. id = itemdb_searchname(conv_str(st, data));
  5754. else // item id
  5755. id = itemdb_exists(conv_num(st, data));
  5756. if( id == NULL ) {
  5757. ShowError("buildin_checkweight: Invalid item '%s'.\n", script_getstr(st,i)); // returns string, regardless of what it was
  5758. script_pushint(st,0);
  5759. return 1;
  5760. }
  5761. nameid[j] = id->nameid;
  5762. amount[j] = script_getnum(st,i+1);
  5763. j++;
  5764. }
  5765. script_pushint(st,checkweight_sub(sd,(nbargs-2)/2,nameid,amount));
  5766. return SCRIPT_CMD_SUCCESS;
  5767. }
  5768. BUILDIN_FUNC(checkweight2){
  5769. //variable sub checkweight
  5770. unsigned short nameid[SCRIPT_MAX_ARRAYSIZE];
  5771. int32 amount[SCRIPT_MAX_ARRAYSIZE], i;
  5772. //variable for array parsing
  5773. struct script_data* data_it;
  5774. struct script_data* data_nb;
  5775. const char* name_it;
  5776. const char* name_nb;
  5777. int32 id_it, id_nb;
  5778. int32 idx_it, idx_nb;
  5779. int nb_it, nb_nb; //array size
  5780. TBL_PC *sd = script_rid2sd(st);
  5781. nullpo_retr(1,sd);
  5782. data_it = script_getdata(st, 2);
  5783. data_nb = script_getdata(st, 3);
  5784. if( !data_isreference(data_it) || !data_isreference(data_nb)) {
  5785. ShowError("buildin_checkweight2: parameter not a variable\n");
  5786. script_pushint(st,0);
  5787. return 1;// not a variable
  5788. }
  5789. id_it = reference_getid(data_it);
  5790. id_nb = reference_getid(data_nb);
  5791. idx_it = reference_getindex(data_it);
  5792. idx_nb = reference_getindex(data_nb);
  5793. name_it = reference_getname(data_it);
  5794. name_nb = reference_getname(data_nb);
  5795. if( not_array_variable(*name_it) || not_array_variable(*name_nb)) {
  5796. ShowError("buildin_checkweight2: illegal scope\n");
  5797. script_pushint(st,0);
  5798. return 1;// not supported
  5799. }
  5800. if(is_string_variable(name_it) || is_string_variable(name_nb)) {
  5801. ShowError("buildin_checkweight2: illegal type, need int\n");
  5802. script_pushint(st,0);
  5803. return 1;// not supported
  5804. }
  5805. nb_it = getarraysize(st, id_it, idx_it, 0, reference_getref(data_it));
  5806. nb_nb = getarraysize(st, id_nb, idx_nb, 0, reference_getref(data_nb));
  5807. if(nb_it != nb_nb) {
  5808. ShowError("buildin_checkweight2: Size mistmatch: nb_it=%d, nb_nb=%d\n",nb_it,nb_nb);
  5809. script_pushint(st,0);
  5810. return 1;
  5811. }
  5812. for(i=0; i<nb_it; i++) {
  5813. nameid[i] = (int32)__64BPRTSIZE(get_val2(st,reference_uid(id_it,idx_it+i),reference_getref(data_it)));
  5814. script_removetop(st, -1, 0);
  5815. amount[i] = (int32)__64BPRTSIZE(get_val2(st,reference_uid(id_nb,idx_nb+i),reference_getref(data_nb)));
  5816. script_removetop(st, -1, 0);
  5817. } //end loop DO NOT break it prematurly we need to depop all stack
  5818. script_pushint(st,checkweight_sub(sd,nb_it,nameid,amount)); //push result of sub to script
  5819. return SCRIPT_CMD_SUCCESS;
  5820. }
  5821. /*==========================================
  5822. * getitem <item id>,<amount>{,<account ID>};
  5823. * getitem "<item name>",<amount>{,<account ID>};
  5824. *
  5825. * getitembound <item id>,<amount>,<type>{,<account ID>};
  5826. * getitembound "<item id>",<amount>,<type>{,<account ID>};
  5827. * Type:
  5828. * 0 - No bound
  5829. * 1 - Account Bound
  5830. * 2 - Guild Bound
  5831. * 3 - Party Bound
  5832. * 4 - Character Bound
  5833. *------------------------------------------*/
  5834. BUILDIN_FUNC(getitem)
  5835. {
  5836. int amount, get_count, i;
  5837. unsigned short nameid;
  5838. struct item it;
  5839. TBL_PC *sd;
  5840. struct script_data *data;
  5841. unsigned char flag = 0;
  5842. const char* command = script_getfuncname(st);
  5843. struct item_data *id = NULL;
  5844. data = script_getdata(st,2);
  5845. get_val(st,data);
  5846. if( data_isstring(data) ) {// "<item name>"
  5847. const char *name = conv_str(st,data);
  5848. id = itemdb_searchname(name);
  5849. if( id == NULL ){
  5850. ShowError("buildin_getitem: Nonexistant item %s requested.\n", name);
  5851. return SCRIPT_CMD_FAILURE; //No item created.
  5852. }
  5853. nameid = id->nameid;
  5854. } else if( data_isint(data) ) {// <item id>
  5855. nameid = conv_num(st,data);
  5856. if( !(id = itemdb_exists(nameid)) ){
  5857. ShowError("buildin_getitem: Nonexistant item %d requested.\n", nameid);
  5858. return SCRIPT_CMD_FAILURE; //No item created.
  5859. }
  5860. } else {
  5861. ShowError("buildin_getitem: invalid data type for argument #1 (%d).", data->type);
  5862. return SCRIPT_CMD_FAILURE;
  5863. }
  5864. // <amount>
  5865. if( (amount = script_getnum(st,3)) <= 0)
  5866. return SCRIPT_CMD_SUCCESS; //return if amount <=0, skip the useles iteration
  5867. memset(&it,0,sizeof(it));
  5868. it.nameid = nameid;
  5869. it.identify = 1;
  5870. it.bound = BOUND_NONE;
  5871. if( !strcmp(command,"getitembound") ) {
  5872. char bound = script_getnum(st,4);
  5873. if( bound < BOUND_NONE || bound >= BOUND_MAX ) {
  5874. ShowError("script_getitembound: Not a correct bound type! Type=%d\n",bound);
  5875. return SCRIPT_CMD_FAILURE;
  5876. }
  5877. if( script_hasdata(st,5) )
  5878. sd = map_id2sd(script_getnum(st,5));
  5879. else
  5880. sd = script_rid2sd(st); // Attached player
  5881. it.bound = bound;
  5882. } else if( script_hasdata(st,4) )
  5883. sd = map_id2sd(script_getnum(st,4)); // <Account ID>
  5884. else
  5885. sd = script_rid2sd(st); // Attached player
  5886. if( sd == NULL ) // no target
  5887. return SCRIPT_CMD_SUCCESS;
  5888. //Check if it's stackable.
  5889. if (!itemdb_isstackable2(id))
  5890. get_count = 1;
  5891. else
  5892. get_count = amount;
  5893. for (i = 0; i < amount; i += get_count)
  5894. {
  5895. // if not pet egg
  5896. if (!pet_create_egg(sd, nameid))
  5897. {
  5898. if ((flag = pc_additem(sd, &it, get_count, LOG_TYPE_SCRIPT)))
  5899. {
  5900. clif_additem(sd, 0, 0, flag);
  5901. if( pc_candrop(sd,&it) )
  5902. map_addflooritem(&it,get_count,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0);
  5903. }
  5904. }
  5905. }
  5906. return SCRIPT_CMD_SUCCESS;
  5907. }
  5908. /*==========================================
  5909. * getitem2 <item id>,<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>{,<account ID>};
  5910. * getitem2 "<item name>",<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>{,<account ID>};
  5911. *
  5912. * getitembound2 <item id>,<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>,<bound type>{,<account ID>};
  5913. * getitembound2 "<item name>",<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>,<bound type>{,<account ID>};
  5914. * Type:
  5915. * 0 - No bound
  5916. * 1 - Account Bound
  5917. * 2 - Guild Bound
  5918. * 3 - Party Bound
  5919. * 4 - Character Bound
  5920. *------------------------------------------*/
  5921. BUILDIN_FUNC(getitem2)
  5922. {
  5923. int amount;
  5924. unsigned short nameid;
  5925. int iden, ref, attr;
  5926. unsigned short c1, c2, c3, c4;
  5927. char bound = BOUND_NONE;
  5928. struct item_data *item_data = NULL;
  5929. struct item item_tmp;
  5930. TBL_PC *sd;
  5931. struct script_data *data;
  5932. const char* command = script_getfuncname(st);
  5933. if( !strcmp(command,"getitembound2") ) {
  5934. bound = script_getnum(st,11);
  5935. if( bound < BOUND_NONE || bound >= BOUND_MAX ) {
  5936. ShowError("script_getitembound2: Not a correct bound type! Type=%d\n",bound);
  5937. return SCRIPT_CMD_FAILURE;
  5938. }
  5939. if( script_hasdata(st,12) )
  5940. sd = map_id2sd(script_getnum(st,12));
  5941. else
  5942. sd = script_rid2sd(st); // Attached player
  5943. } else if( script_hasdata(st,11) )
  5944. sd = map_id2sd(script_getnum(st,11)); // <Account ID>
  5945. else
  5946. sd = script_rid2sd(st); // Attached player
  5947. if( sd == NULL ) // no target
  5948. return SCRIPT_CMD_SUCCESS;
  5949. data = script_getdata(st,2);
  5950. get_val(st,data);
  5951. if( data_isstring(data) ) {
  5952. const char *name = conv_str(st,data);
  5953. if( (item_data = itemdb_searchname(name)) == NULL ){
  5954. ShowError("buildin_getitem2: Nonexistant item %s requested (by conv_str).\n", name);
  5955. return SCRIPT_CMD_FAILURE; //No item created.
  5956. }
  5957. nameid = item_data->nameid;
  5958. } else {
  5959. nameid = conv_num(st,data);
  5960. if( (item_data = itemdb_exists(nameid)) == NULL ){
  5961. ShowError("buildin_getitem2: Nonexistant item %d requested (by conv_num).\n", nameid);
  5962. return SCRIPT_CMD_FAILURE; //No item created.
  5963. }
  5964. }
  5965. amount = script_getnum(st,3);
  5966. iden = script_getnum(st,4);
  5967. ref = script_getnum(st,5);
  5968. attr = script_getnum(st,6);
  5969. c1 = (unsigned short)script_getnum(st,7);
  5970. c2 = (unsigned short)script_getnum(st,8);
  5971. c3 = (unsigned short)script_getnum(st,9);
  5972. c4 = (unsigned short)script_getnum(st,10);
  5973. if( item_data ) {
  5974. int get_count = 0, i;
  5975. memset(&item_tmp,0,sizeof(item_tmp));
  5976. if( item_data->type == IT_WEAPON || item_data->type == IT_ARMOR || item_data->type == IT_SHADOWGEAR ) {
  5977. if(ref > MAX_REFINE)
  5978. ref = MAX_REFINE;
  5979. }
  5980. else if( item_data->type == IT_PETEGG ) {
  5981. iden = 1;
  5982. ref = 0;
  5983. }
  5984. else {
  5985. iden = 1;
  5986. ref = attr = 0;
  5987. }
  5988. item_tmp.nameid = nameid;
  5989. item_tmp.identify = iden;
  5990. item_tmp.refine = ref;
  5991. item_tmp.attribute = attr;
  5992. item_tmp.card[0] = c1;
  5993. item_tmp.card[1] = c2;
  5994. item_tmp.card[2] = c3;
  5995. item_tmp.card[3] = c4;
  5996. item_tmp.bound = bound;
  5997. //Check if it's stackable.
  5998. if (!itemdb_isstackable2(item_data))
  5999. get_count = 1;
  6000. else
  6001. get_count = amount;
  6002. for (i = 0; i < amount; i += get_count)
  6003. {
  6004. // if not pet egg
  6005. if (!pet_create_egg(sd, nameid))
  6006. {
  6007. unsigned char flag = 0;
  6008. if ((flag = pc_additem(sd, &item_tmp, get_count, LOG_TYPE_SCRIPT)))
  6009. {
  6010. clif_additem(sd, 0, 0, flag);
  6011. if( pc_candrop(sd,&item_tmp) )
  6012. map_addflooritem(&item_tmp,get_count,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0);
  6013. }
  6014. }
  6015. }
  6016. }
  6017. return SCRIPT_CMD_SUCCESS;
  6018. }
  6019. /** Gives rental item to player
  6020. * rentitem <item id>,<seconds>{,<account_id>}
  6021. * rentitem "<item name>",<seconds>{,<account_id>}
  6022. */
  6023. BUILDIN_FUNC(rentitem) {
  6024. struct map_session_data *sd;
  6025. struct script_data *data;
  6026. struct item it;
  6027. int seconds;
  6028. unsigned short nameid = 0;
  6029. unsigned char flag = 0;
  6030. data = script_getdata(st,2);
  6031. get_val(st,data);
  6032. if (!script_accid2sd(4,sd))
  6033. return SCRIPT_CMD_FAILURE;
  6034. if( data_isstring(data) )
  6035. {
  6036. const char *name = conv_str(st,data);
  6037. struct item_data *itd = itemdb_searchname(name);
  6038. if( itd == NULL )
  6039. {
  6040. ShowError("buildin_rentitem: Nonexistant item %s requested.\n", name);
  6041. return SCRIPT_CMD_FAILURE;
  6042. }
  6043. nameid = itd->nameid;
  6044. }
  6045. else if( data_isint(data) )
  6046. {
  6047. nameid = conv_num(st,data);
  6048. if( nameid == 0 || !itemdb_exists(nameid) )
  6049. {
  6050. ShowError("buildin_rentitem: Nonexistant item %hu requested.\n", nameid);
  6051. return SCRIPT_CMD_FAILURE;
  6052. }
  6053. }
  6054. else
  6055. {
  6056. ShowError("buildin_rentitem: invalid data type for argument #1 (%d).\n", data->type);
  6057. return SCRIPT_CMD_FAILURE;
  6058. }
  6059. seconds = script_getnum(st,3);
  6060. memset(&it, 0, sizeof(it));
  6061. it.nameid = nameid;
  6062. it.identify = 1;
  6063. it.expire_time = (unsigned int)(time(NULL) + seconds);
  6064. it.bound = BOUND_NONE;
  6065. if( (flag = pc_additem(sd, &it, 1, LOG_TYPE_SCRIPT)) )
  6066. {
  6067. clif_additem(sd, 0, 0, flag);
  6068. return SCRIPT_CMD_FAILURE;
  6069. }
  6070. return SCRIPT_CMD_SUCCESS;
  6071. }
  6072. /** Gives rental item to player with advanced option
  6073. * rentitem2 <item id>,<time>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>{,<account_id>};
  6074. * rentitem2 "<item name>",<time>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>{,<account_id>};
  6075. */
  6076. BUILDIN_FUNC(rentitem2) {
  6077. struct map_session_data *sd;
  6078. struct script_data *data;
  6079. struct item it;
  6080. struct item_data *id;
  6081. int seconds;
  6082. unsigned short nameid = 0;
  6083. unsigned char flag = 0;
  6084. int iden,ref,attr,c1,c2,c3,c4;
  6085. data = script_getdata(st,2);
  6086. get_val(st,data);
  6087. if (!script_accid2sd(11,sd))
  6088. return SCRIPT_CMD_FAILURE;
  6089. if( data_isstring(data) ) {
  6090. const char *name = conv_str(st,data);
  6091. id = itemdb_searchname(name);
  6092. if( id == NULL ) {
  6093. ShowError("buildin_rentitem2: Nonexistant item %s requested.\n", name);
  6094. return SCRIPT_CMD_FAILURE;
  6095. }
  6096. nameid = id->nameid;
  6097. }
  6098. else if( data_isint(data) ) {
  6099. nameid = conv_num(st,data);
  6100. if( !(id = itemdb_search(nameid))) {
  6101. ShowError("buildin_rentitem2: Nonexistant item %hu requested.\n", nameid);
  6102. return SCRIPT_CMD_FAILURE;
  6103. }
  6104. }
  6105. else {
  6106. ShowError("buildin_rentitem2: invalid data type for argument #1 (%d).\n", data->type);
  6107. return SCRIPT_CMD_FAILURE;
  6108. }
  6109. seconds = script_getnum(st,3);
  6110. iden = script_getnum(st,4);
  6111. ref = script_getnum(st,5);
  6112. attr = script_getnum(st,6);
  6113. if (id->type==IT_WEAPON || id->type==IT_ARMOR || id->type==IT_SHADOWGEAR) {
  6114. if(ref > MAX_REFINE) ref = MAX_REFINE;
  6115. }
  6116. else if (id->type==IT_PETEGG) {
  6117. iden = 1;
  6118. ref = 0;
  6119. }
  6120. else {
  6121. iden = 1;
  6122. ref = attr = 0;
  6123. }
  6124. c1 = (short)script_getnum(st,7);
  6125. c2 = (short)script_getnum(st,8);
  6126. c3 = (short)script_getnum(st,9);
  6127. c4 = (short)script_getnum(st,10);
  6128. memset(&it, 0, sizeof(it));
  6129. it.nameid = nameid;
  6130. it.identify = iden;
  6131. it.refine = ref;
  6132. it.attribute = attr;
  6133. it.card[0] = (short)c1;
  6134. it.card[1] = (short)c2;
  6135. it.card[2] = (short)c3;
  6136. it.card[3] = (short)c4;
  6137. it.expire_time = (unsigned int)(time(NULL) + seconds);
  6138. if( (flag = pc_additem(sd, &it, 1, LOG_TYPE_SCRIPT)) ) {
  6139. clif_additem(sd, 0, 0, flag);
  6140. return SCRIPT_CMD_FAILURE;
  6141. }
  6142. return SCRIPT_CMD_SUCCESS;
  6143. }
  6144. /*==========================================
  6145. * gets an item with someone's name inscribed [Skotlex]
  6146. * getinscribeditem item_num, character_name
  6147. * Returned Qty is always 1, only works on equip-able
  6148. * equipment
  6149. *------------------------------------------*/
  6150. BUILDIN_FUNC(getnameditem)
  6151. {
  6152. unsigned short nameid;
  6153. struct item item_tmp;
  6154. TBL_PC *sd, *tsd;
  6155. struct script_data *data;
  6156. sd = script_rid2sd(st);
  6157. if (sd == NULL)
  6158. { //Player not attached!
  6159. script_pushint(st,0);
  6160. return 0;
  6161. }
  6162. data=script_getdata(st,2);
  6163. get_val(st,data);
  6164. if( data_isstring(data) ){
  6165. const char *name=conv_str(st,data);
  6166. struct item_data *item_data = itemdb_searchname(name);
  6167. if( item_data == NULL)
  6168. { //Failed
  6169. script_pushint(st,0);
  6170. return 0;
  6171. }
  6172. nameid = item_data->nameid;
  6173. }else
  6174. nameid = conv_num(st,data);
  6175. if(!itemdb_exists(nameid)/* || itemdb_isstackable(nameid)*/)
  6176. { //Even though named stackable items "could" be risky, they are required for certain quests.
  6177. script_pushint(st,0);
  6178. return 0;
  6179. }
  6180. data=script_getdata(st,3);
  6181. get_val(st,data);
  6182. if( data_isstring(data) ) //Char Name
  6183. tsd=map_nick2sd(conv_str(st,data));
  6184. else //Char Id was given
  6185. tsd=map_charid2sd(conv_num(st,data));
  6186. if( tsd == NULL )
  6187. { //Failed
  6188. script_pushint(st,0);
  6189. return 0;
  6190. }
  6191. memset(&item_tmp,0,sizeof(item_tmp));
  6192. item_tmp.nameid=nameid;
  6193. item_tmp.amount=1;
  6194. item_tmp.identify=1;
  6195. item_tmp.card[0]=CARD0_CREATE; //we don't use 255! because for example SIGNED WEAPON shouldn't get TOP10 BS Fame bonus [Lupus]
  6196. item_tmp.card[2]=tsd->status.char_id;
  6197. item_tmp.card[3]=tsd->status.char_id >> 16;
  6198. if(pc_additem(sd,&item_tmp,1,LOG_TYPE_SCRIPT)) {
  6199. script_pushint(st,0);
  6200. return 0; //Failed to add item, we will not drop if they don't fit
  6201. }
  6202. script_pushint(st,1);
  6203. return SCRIPT_CMD_SUCCESS;
  6204. }
  6205. /*==========================================
  6206. * gets a random item ID from an item group [Skotlex]
  6207. * groupranditem <group_num>{,<sub_group>};
  6208. *------------------------------------------*/
  6209. BUILDIN_FUNC(grouprandomitem) {
  6210. struct s_item_group_entry *entry = NULL;
  6211. int sub_group = 1;
  6212. FETCH(3, sub_group);
  6213. entry = itemdb_get_randgroupitem(script_getnum(st,2),sub_group);
  6214. if (!entry) {
  6215. ShowError("buildin_grouprandomitem: Invalid item group with group_id '%d', sub_group '%d'.\n", script_getnum(st,2), sub_group);
  6216. script_pushint(st,UNKNOWN_ITEM_ID);
  6217. return SCRIPT_CMD_FAILURE;
  6218. }
  6219. script_pushint(st,entry->nameid);
  6220. return SCRIPT_CMD_SUCCESS;
  6221. }
  6222. /**
  6223. * makeitem <item id>,<amount>,"<map name>",<X>,<Y>;
  6224. * makeitem "<item name>",<amount>,"<map name>",<X>,<Y>;
  6225. */
  6226. BUILDIN_FUNC(makeitem) {
  6227. int16 nameid;
  6228. uint16 amount, flag = 0, x, y;
  6229. const char *mapname;
  6230. int m;
  6231. struct item item_tmp;
  6232. struct script_data *data;
  6233. data = script_getdata(st,2);
  6234. get_val(st,data);
  6235. if( data_isstring(data) ){
  6236. const char *name = conv_str(st,data);
  6237. struct item_data *item_data = itemdb_searchname(name);
  6238. if( item_data )
  6239. nameid = item_data->nameid;
  6240. else
  6241. nameid = UNKNOWN_ITEM_ID;
  6242. }
  6243. else
  6244. nameid = conv_num(st,data);
  6245. amount = script_getnum(st,3);
  6246. mapname = script_getstr(st,4);
  6247. x = script_getnum(st,5);
  6248. y = script_getnum(st,6);
  6249. if(strcmp(mapname,"this")==0) {
  6250. TBL_PC *sd;
  6251. sd = script_rid2sd(st);
  6252. if (!sd)
  6253. return SCRIPT_CMD_SUCCESS; //Failed...
  6254. m = sd->bl.m;
  6255. } else
  6256. m = map_mapname2mapid(mapname);
  6257. if(nameid<0) {
  6258. nameid = -nameid;
  6259. flag = 1;
  6260. }
  6261. if(nameid > 0) {
  6262. memset(&item_tmp,0,sizeof(item_tmp));
  6263. item_tmp.nameid = nameid;
  6264. if(!flag)
  6265. item_tmp.identify = 1;
  6266. else
  6267. item_tmp.identify = itemdb_isidentified(nameid);
  6268. map_addflooritem(&item_tmp,amount,m,x,y,0,0,0,4);
  6269. }
  6270. return SCRIPT_CMD_SUCCESS;
  6271. }
  6272. /**
  6273. * makeitem2 <item id>,<amount>,"<map name>",<X>,<Y>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>;
  6274. * makeitem2 "<item name>",<amount>,"<map name>",<X>,<Y>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>;
  6275. */
  6276. BUILDIN_FUNC(makeitem2) {
  6277. uint16 nameid, amount, x, y;
  6278. const char *mapname;
  6279. int m;
  6280. struct item item_tmp;
  6281. struct script_data *data;
  6282. struct item_data *id;
  6283. data = script_getdata(st,2);
  6284. get_val(st,data);
  6285. if( data_isstring(data) ){
  6286. const char *name = conv_str(st,data);
  6287. struct item_data *item_data = itemdb_searchname(name);
  6288. if( item_data )
  6289. nameid = item_data->nameid;
  6290. else
  6291. nameid = UNKNOWN_ITEM_ID;
  6292. }
  6293. else
  6294. nameid = conv_num(st,data);
  6295. amount = script_getnum(st,3);
  6296. mapname = script_getstr(st,4);
  6297. x = script_getnum(st,5);
  6298. y = script_getnum(st,6);
  6299. if (strcmp(mapname,"this")==0) {
  6300. TBL_PC *sd;
  6301. sd = script_rid2sd(st);
  6302. if (!sd)
  6303. return SCRIPT_CMD_SUCCESS; //Failed...
  6304. m = sd->bl.m;
  6305. }
  6306. else
  6307. m = map_mapname2mapid(mapname);
  6308. if ((id = itemdb_search(nameid))) {
  6309. char iden, ref, attr;
  6310. memset(&item_tmp,0,sizeof(item_tmp));
  6311. item_tmp.nameid = nameid;
  6312. iden = (char)script_getnum(st,7);
  6313. ref = (char)script_getnum(st,8);
  6314. attr = (char)script_getnum(st,9);
  6315. if (id->type==IT_WEAPON || id->type==IT_ARMOR || id->type==IT_SHADOWGEAR) {
  6316. if(ref > MAX_REFINE) ref = MAX_REFINE;
  6317. }
  6318. else if (id->type==IT_PETEGG) {
  6319. iden = 1;
  6320. ref = 0;
  6321. }
  6322. else {
  6323. iden = 1;
  6324. ref = attr = 0;
  6325. }
  6326. item_tmp.identify = iden;
  6327. item_tmp.refine = ref;
  6328. item_tmp.attribute = attr;
  6329. item_tmp.card[0] = script_getnum(st,10);
  6330. item_tmp.card[1] = script_getnum(st,11);
  6331. item_tmp.card[2] = script_getnum(st,12);
  6332. item_tmp.card[3] = script_getnum(st,13);
  6333. map_addflooritem(&item_tmp,amount,m,x,y,0,0,0,4);
  6334. }
  6335. else
  6336. return SCRIPT_CMD_FAILURE;
  6337. return SCRIPT_CMD_SUCCESS;
  6338. }
  6339. /// Counts / deletes the current item given by idx.
  6340. /// Used by buildin_delitem_search
  6341. /// Relies on all input data being already fully valid.
  6342. static void buildin_delitem_delete(struct map_session_data* sd, int idx, int* amount, uint8 loc, bool delete_items)
  6343. {
  6344. int delamount;
  6345. struct item *itm = NULL;
  6346. switch(loc) {
  6347. case 1: // cart
  6348. itm = &sd->status.cart[idx];
  6349. break;
  6350. case 2: // storage
  6351. itm = &sd->status.storage.items[idx];
  6352. break;
  6353. default: //inventory
  6354. itm = &sd->status.inventory[idx];
  6355. break;
  6356. }
  6357. delamount = ( amount[0] < itm->amount ) ? amount[0] : itm->amount;
  6358. if( delete_items )
  6359. {
  6360. if( itemdb_type(itm->nameid) == IT_PETEGG && itm->card[0] == CARD0_PET )
  6361. {// delete associated pet
  6362. intif_delete_petdata(MakeDWord(itm->card[1], itm->card[2]));
  6363. }
  6364. switch(loc) {
  6365. case 1:
  6366. pc_cart_delitem(sd,idx,delamount,0,LOG_TYPE_SCRIPT);
  6367. break;
  6368. case 2:
  6369. storage_delitem(sd,idx,delamount);
  6370. log_pick_pc(sd,LOG_TYPE_SCRIPT,-delamount,itm);
  6371. break;
  6372. default:
  6373. pc_delitem(sd, idx, delamount, 0, 0, LOG_TYPE_SCRIPT);
  6374. break;
  6375. }
  6376. }
  6377. amount[0]-= delamount;
  6378. }
  6379. /// Searches for item(s) and checks, if there is enough of them.
  6380. /// Used by delitem and delitem2
  6381. /// Relies on all input data being already fully valid.
  6382. /// @param exact_match will also match item attributes and cards, not just name id
  6383. /// @return true when all items could be deleted, false when there were not enough items to delete
  6384. static bool buildin_delitem_search(struct map_session_data* sd, struct item* it, bool exact_match, uint8 loc)
  6385. {
  6386. bool delete_items = false;
  6387. int i, amount, size;
  6388. struct item *items;
  6389. // prefer always non-equipped items
  6390. it->equip = 0;
  6391. // when searching for nameid only, prefer additionally
  6392. if( !exact_match )
  6393. {
  6394. // non-refined items
  6395. it->refine = 0;
  6396. // card-less items
  6397. memset(it->card, 0, sizeof(it->card));
  6398. }
  6399. switch(loc) {
  6400. case 1: // cart
  6401. size = MAX_CART;
  6402. items = sd->status.cart;
  6403. break;
  6404. case 2: // storage
  6405. size = MAX_STORAGE;
  6406. items = sd->status.storage.items;
  6407. break;
  6408. default: //inventory
  6409. size = MAX_INVENTORY;
  6410. items = sd->status.inventory;
  6411. break;
  6412. }
  6413. for(;;)
  6414. {
  6415. unsigned short important = 0;
  6416. amount = it->amount;
  6417. // 1st pass -- less important items / exact match
  6418. for( i = 0; amount && i < size; i++ )
  6419. {
  6420. struct item *itm = NULL;
  6421. if( !&items[i] || !(itm = &items[i])->nameid || itm->nameid != it->nameid )
  6422. {// wrong/invalid item
  6423. continue;
  6424. }
  6425. if( itm->equip != it->equip || itm->refine != it->refine )
  6426. {// not matching attributes
  6427. important++;
  6428. continue;
  6429. }
  6430. if( exact_match )
  6431. {
  6432. if( itm->identify != it->identify || itm->attribute != it->attribute || memcmp(itm->card, it->card, sizeof(itm->card)) )
  6433. {// not matching exact attributes
  6434. continue;
  6435. }
  6436. }
  6437. else
  6438. {
  6439. if( itemdb_type(itm->nameid) == IT_PETEGG )
  6440. {
  6441. if( itm->card[0] == CARD0_PET && CheckForCharServer() )
  6442. {// pet which cannot be deleted
  6443. continue;
  6444. }
  6445. }
  6446. else if( memcmp(itm->card, it->card, sizeof(itm->card)) )
  6447. {// named/carded item
  6448. important++;
  6449. continue;
  6450. }
  6451. }
  6452. // count / delete item
  6453. buildin_delitem_delete(sd, i, &amount, loc, delete_items);
  6454. }
  6455. // 2nd pass -- any matching item
  6456. if( amount == 0 || important == 0 )
  6457. {// either everything was already consumed or no items were skipped
  6458. ;
  6459. }
  6460. else for( i = 0; amount && i < size; i++ )
  6461. {
  6462. struct item *itm = NULL;
  6463. if( !&items[i] || !(itm = &items[i])->nameid || itm->nameid != it->nameid )
  6464. {// wrong/invalid item
  6465. continue;
  6466. }
  6467. if( itemdb_type(itm->nameid) == IT_PETEGG && itm->card[0] == CARD0_PET && CheckForCharServer() )
  6468. {// pet which cannot be deleted
  6469. continue;
  6470. }
  6471. if( exact_match )
  6472. {
  6473. if( itm->refine != it->refine || itm->identify != it->identify || itm->attribute != it->attribute || memcmp(itm->card, it->card, sizeof(itm->card)) )
  6474. {// not matching attributes
  6475. continue;
  6476. }
  6477. }
  6478. // count / delete item
  6479. buildin_delitem_delete(sd, i, &amount, loc, delete_items);
  6480. }
  6481. if( amount )
  6482. {// not enough items
  6483. return false;
  6484. }
  6485. else if( delete_items )
  6486. {// we are done with the work
  6487. return true;
  6488. }
  6489. else
  6490. {// get rid of the items now
  6491. delete_items = true;
  6492. }
  6493. }
  6494. }
  6495. /// Deletes items from the target/attached player.
  6496. /// Prioritizes ordinary items.
  6497. ///
  6498. /// delitem <item id>,<amount>{,<account id>}
  6499. /// delitem "<item name>",<amount>{,<account id>}
  6500. /// cartdelitem <item id>,<amount>{,<account id>}
  6501. /// cartdelitem "<item name>",<amount>{,<account id>}
  6502. /// storagedelitem <item id>,<amount>{,<account id>}
  6503. /// storagedelitem "<item name>",<amount>{,<account id>}
  6504. BUILDIN_FUNC(delitem)
  6505. {
  6506. TBL_PC *sd;
  6507. struct item it;
  6508. struct script_data *data;
  6509. uint8 loc = 0;
  6510. char* command = (char*)script_getfuncname(st);
  6511. if(!strncmp(command, "cart", 4))
  6512. loc = 1;
  6513. else if(!strncmp(command, "storage", 7))
  6514. loc = 2;
  6515. //TODO: 3 - Guild Storage
  6516. if( script_hasdata(st,4) )
  6517. {
  6518. uint32 account_id = script_getnum(st,4);
  6519. sd = map_id2sd(account_id); // <account id>
  6520. if( sd == NULL )
  6521. {
  6522. ShowError("buildin_%s: player not found (AID=%d).\n", command, account_id);
  6523. st->state = END;
  6524. return 1;
  6525. }
  6526. }
  6527. else
  6528. {
  6529. sd = script_rid2sd(st);// attached player
  6530. if( sd == NULL )
  6531. return 0;
  6532. }
  6533. if (loc == 1 && !pc_iscarton(sd)) {
  6534. ShowError("buildin_cartdelitem: player doesn't have cart (CID=%d).\n", sd->status.char_id);
  6535. return SCRIPT_CMD_FAILURE;
  6536. }
  6537. data = script_getdata(st,2);
  6538. get_val(st,data);
  6539. if( data_isstring(data) )
  6540. {
  6541. const char* item_name = conv_str(st,data);
  6542. struct item_data* id = itemdb_searchname(item_name);
  6543. if( id == NULL )
  6544. {
  6545. ShowError("buildin_%s: unknown item \"%s\".\n", command, item_name);
  6546. st->state = END;
  6547. return 1;
  6548. }
  6549. it.nameid = id->nameid;// "<item name>"
  6550. }
  6551. else
  6552. {
  6553. it.nameid = conv_num(st,data);// <item id>
  6554. if( !itemdb_exists( it.nameid ) )
  6555. {
  6556. ShowError("buildin_%s: unknown item \"%hu\".\n", command, it.nameid);
  6557. st->state = END;
  6558. return 1;
  6559. }
  6560. }
  6561. it.amount=script_getnum(st,3);
  6562. if( it.amount <= 0 )
  6563. return 0;// nothing to do
  6564. if( buildin_delitem_search(sd, &it, false, loc) )
  6565. {// success
  6566. return SCRIPT_CMD_SUCCESS;
  6567. }
  6568. ShowError("buildin_%s: failed to delete %d items (AID=%d item_id=%hu).\n", command, it.amount, sd->status.account_id, it.nameid);
  6569. st->state = END;
  6570. st->mes_active = 0;
  6571. clif_scriptclose(sd, st->oid);
  6572. return SCRIPT_CMD_FAILURE;
  6573. }
  6574. /// Deletes items from the target/attached player.
  6575. ///
  6576. /// delitem2 <item id>,<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>{,<account ID>}
  6577. /// delitem2 "<Item name>",<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>{,<account ID>}
  6578. /// cartdelitem2 <item id>,<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>{,<account ID>}
  6579. /// cartdelitem2 "<Item name>",<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>{,<account ID>}
  6580. /// storagedelitem2 <item id>,<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>{,<account ID>}
  6581. /// storagedelitem2 "<Item name>",<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>{,<account ID>}
  6582. BUILDIN_FUNC(delitem2)
  6583. {
  6584. TBL_PC *sd;
  6585. struct item it;
  6586. struct script_data *data;
  6587. uint8 loc = 0;
  6588. char* command = (char*)script_getfuncname(st);
  6589. if(!strncmp(command, "cart", 4))
  6590. loc = 1;
  6591. else if(!strncmp(command, "storage", 7))
  6592. loc = 2;
  6593. //TODO: 3 - Guild Storage
  6594. if( script_hasdata(st,11) )
  6595. {
  6596. uint32 account_id = script_getnum(st,11);
  6597. sd = map_id2sd(account_id); // <account id>
  6598. if( sd == NULL )
  6599. {
  6600. ShowError("buildin_%s: player not found (AID=%d).\n", command, account_id);
  6601. st->state = END;
  6602. return 1;
  6603. }
  6604. }
  6605. else
  6606. {
  6607. sd = script_rid2sd(st);// attached player
  6608. if( sd == NULL )
  6609. return 0;
  6610. }
  6611. if (loc == 1 && !pc_iscarton(sd)) {
  6612. ShowError("buildin_cartdelitem: player doesn't have cart (CID=%d).\n", sd->status.char_id);
  6613. script_pushint(st,-1);
  6614. return SCRIPT_CMD_FAILURE;
  6615. }
  6616. data = script_getdata(st,2);
  6617. get_val(st,data);
  6618. if( data_isstring(data) )
  6619. {
  6620. const char* item_name = conv_str(st,data);
  6621. struct item_data* id = itemdb_searchname(item_name);
  6622. if( id == NULL )
  6623. {
  6624. ShowError("buildin_%s: unknown item \"%s\".\n", command, item_name);
  6625. st->state = END;
  6626. return 1;
  6627. }
  6628. it.nameid = id->nameid;// "<item name>"
  6629. }
  6630. else
  6631. {
  6632. it.nameid = conv_num(st,data);// <item id>
  6633. if( !itemdb_exists( it.nameid ) )
  6634. {
  6635. ShowError("buildin_%s: unknown item \"%hu\".\n", command, it.nameid);
  6636. st->state = END;
  6637. return 1;
  6638. }
  6639. }
  6640. it.amount=script_getnum(st,3);
  6641. it.identify=script_getnum(st,4);
  6642. it.refine=script_getnum(st,5);
  6643. it.attribute=script_getnum(st,6);
  6644. it.card[0]=(short)script_getnum(st,7);
  6645. it.card[1]=(short)script_getnum(st,8);
  6646. it.card[2]=(short)script_getnum(st,9);
  6647. it.card[3]=(short)script_getnum(st,10);
  6648. if( it.amount <= 0 )
  6649. return 0;// nothing to do
  6650. if( buildin_delitem_search(sd, &it, true, loc) )
  6651. {// success
  6652. return SCRIPT_CMD_SUCCESS;
  6653. }
  6654. ShowError("buildin_%s: failed to delete %d items (AID=%d item_id=%hu).\n", command, it.amount, sd->status.account_id, it.nameid);
  6655. st->state = END;
  6656. st->mes_active = 0;
  6657. clif_scriptclose(sd, st->oid);
  6658. return SCRIPT_CMD_FAILURE;
  6659. }
  6660. /*==========================================
  6661. * Enables/Disables use of items while in an NPC [Skotlex]
  6662. *------------------------------------------*/
  6663. BUILDIN_FUNC(enableitemuse)
  6664. {
  6665. TBL_PC *sd;
  6666. sd=script_rid2sd(st);
  6667. if (sd)
  6668. st->npc_item_flag = sd->npc_item_flag = 1;
  6669. return SCRIPT_CMD_SUCCESS;
  6670. }
  6671. BUILDIN_FUNC(disableitemuse)
  6672. {
  6673. TBL_PC *sd;
  6674. sd=script_rid2sd(st);
  6675. if (sd)
  6676. st->npc_item_flag = sd->npc_item_flag = 0;
  6677. return SCRIPT_CMD_SUCCESS;
  6678. }
  6679. /*==========================================
  6680. * Returns a character's specified stat.
  6681. * Check pc_readparam for available options.
  6682. * readparam <param>{,"<nick>"}
  6683. *------------------------------------------*/
  6684. BUILDIN_FUNC(readparam)
  6685. {
  6686. int type;
  6687. TBL_PC *sd;
  6688. type = script_getnum(st,2);
  6689. if (!script_nick2sd(3,sd)) {
  6690. script_pushint(st,-1);
  6691. return SCRIPT_CMD_FAILURE;
  6692. }
  6693. script_pushint(st,pc_readparam(sd,type));
  6694. return SCRIPT_CMD_SUCCESS;
  6695. }
  6696. /*==========================================
  6697. * Return charid identification
  6698. * return by @num :
  6699. * 0 : char_id
  6700. * 1 : party_id
  6701. * 2 : guild_id
  6702. * 3 : account_id
  6703. * 4 : bg_id
  6704. *------------------------------------------*/
  6705. BUILDIN_FUNC(getcharid)
  6706. {
  6707. int num;
  6708. TBL_PC *sd;
  6709. num = script_getnum(st,2);
  6710. if( script_hasdata(st,3) )
  6711. sd=map_nick2sd(script_getstr(st,3));
  6712. else
  6713. sd=script_rid2sd(st);
  6714. if(sd==NULL){
  6715. script_pushint(st,0); //return 0, according docs
  6716. return 0;
  6717. }
  6718. switch( num ) {
  6719. case 0: script_pushint(st,sd->status.char_id); break;
  6720. case 1: script_pushint(st,sd->status.party_id); break;
  6721. case 2: script_pushint(st,sd->status.guild_id); break;
  6722. case 3: script_pushint(st,sd->status.account_id); break;
  6723. case 4: script_pushint(st,sd->bg_id); break;
  6724. default:
  6725. ShowError("buildin_getcharid: invalid parameter (%d).\n", num);
  6726. script_pushint(st,0);
  6727. break;
  6728. }
  6729. return SCRIPT_CMD_SUCCESS;
  6730. }
  6731. /*==========================================
  6732. * returns the GID of an NPC
  6733. *------------------------------------------*/
  6734. BUILDIN_FUNC(getnpcid)
  6735. {
  6736. int num = script_getnum(st,2);
  6737. struct npc_data* nd = NULL;
  6738. if( script_hasdata(st,3) )
  6739. {// unique npc name
  6740. if( ( nd = npc_name2id(script_getstr(st,3)) ) == NULL )
  6741. {
  6742. ShowError("buildin_getnpcid: No such NPC '%s'.\n", script_getstr(st,3));
  6743. script_pushint(st,0);
  6744. return 1;
  6745. }
  6746. }
  6747. switch (num) {
  6748. case 0:
  6749. script_pushint(st,nd ? nd->bl.id : st->oid);
  6750. break;
  6751. default:
  6752. ShowError("buildin_getnpcid: invalid parameter (%d).\n", num);
  6753. script_pushint(st,0);
  6754. return 1;
  6755. }
  6756. return SCRIPT_CMD_SUCCESS;
  6757. }
  6758. /*==========================================
  6759. * Return the name of the party_id
  6760. * null if not found
  6761. *------------------------------------------*/
  6762. BUILDIN_FUNC(getpartyname)
  6763. {
  6764. int party_id;
  6765. struct party_data* p;
  6766. party_id = script_getnum(st,2);
  6767. if( ( p = party_search(party_id) ) != NULL )
  6768. {
  6769. script_pushstrcopy(st,p->party.name);
  6770. }
  6771. else
  6772. {
  6773. script_pushconststr(st,"null");
  6774. }
  6775. return SCRIPT_CMD_SUCCESS;
  6776. }
  6777. /*==========================================
  6778. * Get the information of the members of a party by type
  6779. * @party_id, @type
  6780. * return by @type :
  6781. * - : nom des membres
  6782. * 1 : char_id des membres
  6783. * 2 : account_id des membres
  6784. *------------------------------------------*/
  6785. BUILDIN_FUNC(getpartymember)
  6786. {
  6787. struct party_data *p;
  6788. unsigned char j = 0;
  6789. p = party_search(script_getnum(st,2));
  6790. if (p != NULL) {
  6791. int type = 0;
  6792. unsigned char i;
  6793. if (script_hasdata(st,3))
  6794. type = script_getnum(st,3);
  6795. for (i = 0; i < MAX_PARTY; i++) {
  6796. if (p->party.member[i].account_id) {
  6797. switch (type) {
  6798. case 2:
  6799. mapreg_setreg(reference_uid(add_str("$@partymemberaid"), j),p->party.member[i].account_id);
  6800. break;
  6801. case 1:
  6802. mapreg_setreg(reference_uid(add_str("$@partymembercid"), j),p->party.member[i].char_id);
  6803. break;
  6804. default:
  6805. mapreg_setregstr(reference_uid(add_str("$@partymembername$"), j),p->party.member[i].name);
  6806. break;
  6807. }
  6808. j++;
  6809. }
  6810. }
  6811. }
  6812. mapreg_setreg(add_str("$@partymembercount"),j);
  6813. return SCRIPT_CMD_SUCCESS;
  6814. }
  6815. /*==========================================
  6816. * Retrieves party leader. if flag is specified,
  6817. * return some of the leader data. Otherwise, return name.
  6818. *------------------------------------------*/
  6819. BUILDIN_FUNC(getpartyleader)
  6820. {
  6821. int party_id, type = 0, i=0;
  6822. struct party_data *p;
  6823. party_id=script_getnum(st,2);
  6824. if( script_hasdata(st,3) )
  6825. type=script_getnum(st,3);
  6826. p=party_search(party_id);
  6827. if (p) //Search leader
  6828. for(i = 0; i < MAX_PARTY && !p->party.member[i].leader; i++);
  6829. if (!p || i == MAX_PARTY) { //leader not found
  6830. if (type)
  6831. script_pushint(st,-1);
  6832. else
  6833. script_pushconststr(st,"null");
  6834. return 0;
  6835. }
  6836. switch (type) {
  6837. case 1: script_pushint(st,p->party.member[i].account_id); break;
  6838. case 2: script_pushint(st,p->party.member[i].char_id); break;
  6839. case 3: script_pushint(st,p->party.member[i].class_); break;
  6840. case 4: script_pushstrcopy(st,mapindex_id2name(p->party.member[i].map)); break;
  6841. case 5: script_pushint(st,p->party.member[i].lv); break;
  6842. default: script_pushstrcopy(st,p->party.member[i].name); break;
  6843. }
  6844. return SCRIPT_CMD_SUCCESS;
  6845. }
  6846. /*==========================================
  6847. * Return the name of the @guild_id
  6848. * null if not found
  6849. *------------------------------------------*/
  6850. BUILDIN_FUNC(getguildname)
  6851. {
  6852. int guild_id;
  6853. struct guild* g;
  6854. guild_id = script_getnum(st,2);
  6855. if( ( g = guild_search(guild_id) ) != NULL )
  6856. script_pushstrcopy(st,g->name);
  6857. else
  6858. script_pushconststr(st,"null");
  6859. return SCRIPT_CMD_SUCCESS;
  6860. }
  6861. /*==========================================
  6862. * Return the name of the guild master of @guild_id
  6863. * null if not found
  6864. *------------------------------------------*/
  6865. BUILDIN_FUNC(getguildmaster)
  6866. {
  6867. int guild_id;
  6868. struct guild* g;
  6869. guild_id = script_getnum(st,2);
  6870. if( ( g = guild_search(guild_id) ) != NULL )
  6871. script_pushstrcopy(st,g->member[0].name);
  6872. else
  6873. script_pushconststr(st,"null");
  6874. return SCRIPT_CMD_SUCCESS;
  6875. }
  6876. BUILDIN_FUNC(getguildmasterid)
  6877. {
  6878. int guild_id;
  6879. struct guild* g;
  6880. guild_id = script_getnum(st,2);
  6881. if( ( g = guild_search(guild_id) ) != NULL )
  6882. script_pushint(st,g->member[0].char_id);
  6883. else
  6884. script_pushint(st,0);
  6885. return SCRIPT_CMD_SUCCESS;
  6886. }
  6887. /*==========================================
  6888. * Get char string information by type :
  6889. * Return by @type :
  6890. * 0 : char_name
  6891. * 1 : party_name or ""
  6892. * 2 : guild_name or ""
  6893. * 3 : map_name
  6894. * - : ""
  6895. * strcharinfo(<type>{,<char_id>})
  6896. *------------------------------------------*/
  6897. BUILDIN_FUNC(strcharinfo)
  6898. {
  6899. TBL_PC *sd;
  6900. int num;
  6901. struct guild* g;
  6902. struct party_data* p;
  6903. if (!script_charid2sd(3,sd)) {
  6904. script_pushconststr(st,"");
  6905. return SCRIPT_CMD_FAILURE;
  6906. }
  6907. num=script_getnum(st,2);
  6908. switch(num){
  6909. case 0:
  6910. script_pushstrcopy(st,sd->status.name);
  6911. break;
  6912. case 1:
  6913. if( ( p = party_search(sd->status.party_id) ) != NULL ) {
  6914. script_pushstrcopy(st,p->party.name);
  6915. } else {
  6916. script_pushconststr(st,"");
  6917. }
  6918. break;
  6919. case 2:
  6920. if( ( g = sd->guild ) != NULL ) {
  6921. script_pushstrcopy(st,g->name);
  6922. } else {
  6923. script_pushconststr(st,"");
  6924. }
  6925. break;
  6926. case 3:
  6927. script_pushconststr(st,map[sd->bl.m].name);
  6928. break;
  6929. default:
  6930. ShowWarning("buildin_strcharinfo: unknown parameter.\n");
  6931. script_pushconststr(st,"");
  6932. break;
  6933. }
  6934. return SCRIPT_CMD_SUCCESS;
  6935. }
  6936. /*==========================================
  6937. * Get npc string information by type
  6938. * return by @type:
  6939. * 0 : name
  6940. * 1 : str#
  6941. * 2 : #str
  6942. * 3 : ::str
  6943. * 4 : map name
  6944. *------------------------------------------*/
  6945. BUILDIN_FUNC(strnpcinfo)
  6946. {
  6947. TBL_NPC* nd;
  6948. int num;
  6949. char *buf,*name=NULL;
  6950. nd = map_id2nd(st->oid);
  6951. if (!nd) {
  6952. script_pushconststr(st, "");
  6953. return 0;
  6954. }
  6955. num = script_getnum(st,2);
  6956. switch(num){
  6957. case 0: // display name
  6958. name = aStrdup(nd->name);
  6959. break;
  6960. case 1: // visible part of display name
  6961. if((buf = strchr(nd->name,'#')) != NULL)
  6962. {
  6963. name = aStrdup(nd->name);
  6964. name[buf - nd->name] = 0;
  6965. } else // Return the name, there is no '#' present
  6966. name = aStrdup(nd->name);
  6967. break;
  6968. case 2: // # fragment
  6969. if((buf = strchr(nd->name,'#')) != NULL)
  6970. name = aStrdup(buf+1);
  6971. break;
  6972. case 3: // unique name
  6973. name = aStrdup(nd->exname);
  6974. break;
  6975. case 4: // map name
  6976. name = aStrdup(map[nd->bl.m].name);
  6977. break;
  6978. }
  6979. if(name)
  6980. script_pushstr(st, name);
  6981. else
  6982. script_pushconststr(st, "");
  6983. return SCRIPT_CMD_SUCCESS;
  6984. }
  6985. /// aegis->athena slot position conversion table
  6986. /// Index is used by EQI_ in db/const.txt
  6987. static unsigned int equip[] = {
  6988. EQP_HEAD_TOP,
  6989. EQP_ARMOR,
  6990. EQP_HAND_L,
  6991. EQP_HAND_R,
  6992. EQP_GARMENT,
  6993. EQP_SHOES,
  6994. EQP_ACC_L,
  6995. EQP_ACC_R,
  6996. EQP_HEAD_MID,
  6997. EQP_HEAD_LOW,
  6998. EQP_COSTUME_HEAD_LOW,
  6999. EQP_COSTUME_HEAD_MID,
  7000. EQP_COSTUME_HEAD_TOP,
  7001. EQP_COSTUME_GARMENT,
  7002. EQP_AMMO,
  7003. EQP_SHADOW_ARMOR,
  7004. EQP_SHADOW_WEAPON,
  7005. EQP_SHADOW_SHIELD,
  7006. EQP_SHADOW_SHOES,
  7007. EQP_SHADOW_ACC_R,
  7008. EQP_SHADOW_ACC_L
  7009. };
  7010. /**
  7011. * getequipid(<equipment slot>{,<char_id>})
  7012. **/
  7013. BUILDIN_FUNC(getequipid)
  7014. {
  7015. int i, num;
  7016. TBL_PC* sd;
  7017. struct item_data* item;
  7018. if (!script_charid2sd(3, sd)) {
  7019. script_pushint(st,-1);
  7020. return SCRIPT_CMD_FAILURE;
  7021. }
  7022. num = script_getnum(st,2) - 1;
  7023. if( num < 0 || num >= ARRAYLENGTH(equip) )
  7024. {
  7025. script_pushint(st,-1);
  7026. return SCRIPT_CMD_FAILURE;
  7027. }
  7028. // get inventory position of item
  7029. i = pc_checkequip(sd,equip[num]);
  7030. if( i < 0 )
  7031. {
  7032. script_pushint(st,-1);
  7033. return SCRIPT_CMD_FAILURE;
  7034. }
  7035. item = sd->inventory_data[i];
  7036. if( item != 0 )
  7037. script_pushint(st,item->nameid);
  7038. else
  7039. script_pushint(st,0);
  7040. return SCRIPT_CMD_SUCCESS;
  7041. }
  7042. /*==========================================
  7043. * getequipuniqueid(<equipment slot>{,<char_id>})
  7044. *------------------------------------------*/
  7045. BUILDIN_FUNC(getequipuniqueid)
  7046. {
  7047. int i, num;
  7048. TBL_PC* sd;
  7049. struct item* item;
  7050. if (!script_charid2sd(3, sd)) {
  7051. script_pushconststr(st, "");
  7052. return SCRIPT_CMD_FAILURE;
  7053. }
  7054. num = script_getnum(st,2) - 1;
  7055. if (num < 0 || num >= ARRAYLENGTH(equip)) {
  7056. script_pushconststr(st, "");
  7057. return SCRIPT_CMD_FAILURE;
  7058. }
  7059. // get inventory position of item
  7060. i = pc_checkequip(sd,equip[num]);
  7061. if (i < 0) {
  7062. script_pushconststr(st, "");
  7063. return SCRIPT_CMD_FAILURE;
  7064. }
  7065. item = &sd->status.inventory[i];
  7066. if (item != 0) {
  7067. char buf[256];
  7068. memset(buf, 0, sizeof(buf));
  7069. snprintf(buf, sizeof(buf)-1, "%llu", (unsigned long long)item->unique_id);
  7070. script_pushstr(st, buf);
  7071. } else
  7072. script_pushconststr(st, "");
  7073. return SCRIPT_CMD_SUCCESS;
  7074. }
  7075. /*==========================================
  7076. * Get the equipement name at pos
  7077. * return item jname or ""
  7078. * getequipname(<equipment slot>{,<char_id>})
  7079. *------------------------------------------*/
  7080. BUILDIN_FUNC(getequipname)
  7081. {
  7082. int i, num;
  7083. TBL_PC* sd;
  7084. struct item_data* item;
  7085. if (!script_charid2sd(3, sd)) {
  7086. script_pushconststr(st, "");
  7087. return SCRIPT_CMD_FAILURE;
  7088. }
  7089. num = script_getnum(st,2) - 1;
  7090. if( num < 0 || num >= ARRAYLENGTH(equip) )
  7091. {
  7092. script_pushconststr(st,"");
  7093. return 0;
  7094. }
  7095. // get inventory position of item
  7096. i = pc_checkequip(sd,equip[num]);
  7097. if( i < 0 )
  7098. {
  7099. script_pushconststr(st,"");
  7100. return 0;
  7101. }
  7102. item = sd->inventory_data[i];
  7103. if( item != 0 )
  7104. script_pushstrcopy(st,item->jname);
  7105. else
  7106. script_pushconststr(st,"");
  7107. return SCRIPT_CMD_SUCCESS;
  7108. }
  7109. /*==========================================
  7110. * getbrokenid [Valaris]
  7111. * getbrokenid(<number>{,<char_id>})
  7112. *------------------------------------------*/
  7113. BUILDIN_FUNC(getbrokenid)
  7114. {
  7115. int i,num,id=0,brokencounter=0;
  7116. TBL_PC *sd;
  7117. if (!script_charid2sd(3, sd)) {
  7118. script_pushint(st,0);
  7119. return SCRIPT_CMD_FAILURE;
  7120. }
  7121. num=script_getnum(st,2);
  7122. for(i=0; i<MAX_INVENTORY; i++) {
  7123. if(sd->status.inventory[i].attribute){
  7124. brokencounter++;
  7125. if(num==brokencounter){
  7126. id=sd->status.inventory[i].nameid;
  7127. break;
  7128. }
  7129. }
  7130. }
  7131. script_pushint(st,id);
  7132. return SCRIPT_CMD_SUCCESS;
  7133. }
  7134. /*==========================================
  7135. * repair [Valaris]
  7136. * repair <num>{,<char_id>};
  7137. *------------------------------------------*/
  7138. BUILDIN_FUNC(repair)
  7139. {
  7140. int i,num;
  7141. int repaircounter=0;
  7142. TBL_PC *sd;
  7143. if (!script_charid2sd(3,sd))
  7144. return SCRIPT_CMD_FAILURE;
  7145. num=script_getnum(st,2);
  7146. for(i=0; i<MAX_INVENTORY; i++) {
  7147. if(sd->status.inventory[i].attribute){
  7148. repaircounter++;
  7149. if(num==repaircounter){
  7150. sd->status.inventory[i].attribute=0;
  7151. clif_equiplist(sd);
  7152. clif_produceeffect(sd, 0, sd->status.inventory[i].nameid);
  7153. clif_misceffect(&sd->bl, 3);
  7154. break;
  7155. }
  7156. }
  7157. }
  7158. return SCRIPT_CMD_SUCCESS;
  7159. }
  7160. /*==========================================
  7161. * repairall {<char_id>}
  7162. *------------------------------------------*/
  7163. BUILDIN_FUNC(repairall)
  7164. {
  7165. int i, repaircounter = 0;
  7166. TBL_PC *sd;
  7167. if (!script_charid2sd(2,sd))
  7168. return SCRIPT_CMD_FAILURE;
  7169. for(i = 0; i < MAX_INVENTORY; i++)
  7170. {
  7171. if(sd->status.inventory[i].nameid && sd->status.inventory[i].attribute)
  7172. {
  7173. sd->status.inventory[i].attribute = 0;
  7174. clif_produceeffect(sd,0,sd->status.inventory[i].nameid);
  7175. repaircounter++;
  7176. }
  7177. }
  7178. if(repaircounter)
  7179. {
  7180. clif_misceffect(&sd->bl, 3);
  7181. clif_equiplist(sd);
  7182. }
  7183. return SCRIPT_CMD_SUCCESS;
  7184. }
  7185. /*==========================================
  7186. * Chk if player have something equiped at pos
  7187. * getequipisequiped <pos>{,<char_id>}
  7188. *------------------------------------------*/
  7189. BUILDIN_FUNC(getequipisequiped)
  7190. {
  7191. int i = -1,num;
  7192. TBL_PC *sd;
  7193. num = script_getnum(st,2);
  7194. if (!script_charid2sd(3,sd))
  7195. return SCRIPT_CMD_FAILURE;
  7196. if (num > 0 && num <= ARRAYLENGTH(equip))
  7197. i=pc_checkequip(sd,equip[num-1]);
  7198. if(i >= 0)
  7199. script_pushint(st,1);
  7200. else
  7201. script_pushint(st,0);
  7202. return SCRIPT_CMD_SUCCESS;
  7203. }
  7204. /*==========================================
  7205. * Chk if the player have something equiped at pos
  7206. * if so chk if this item ain't marked not refinable or rental
  7207. * return (npc)
  7208. * 1 : true
  7209. * 0 : false
  7210. * getequipisenableref(<equipment slot>{,<char_id>})
  7211. *------------------------------------------*/
  7212. BUILDIN_FUNC(getequipisenableref)
  7213. {
  7214. int i = -1,num;
  7215. TBL_PC *sd;
  7216. num = script_getnum(st,2);
  7217. if (!script_charid2sd(3, sd)) {
  7218. script_pushint(st,0);
  7219. return SCRIPT_CMD_FAILURE;
  7220. }
  7221. if( num > 0 && num <= ARRAYLENGTH(equip) )
  7222. i = pc_checkequip(sd,equip[num-1]);
  7223. if( i >= 0 && sd->inventory_data[i] && !sd->inventory_data[i]->flag.no_refine && !sd->status.inventory[i].expire_time )
  7224. script_pushint(st,1);
  7225. else
  7226. script_pushint(st,0);
  7227. return SCRIPT_CMD_SUCCESS;
  7228. }
  7229. /*==========================================
  7230. * Get the item refined value at pos
  7231. * return (npc)
  7232. * x : refine amount
  7233. * 0 : false (not refined)
  7234. * getequiprefinerycnt(<equipment slot>{,<char_id>})
  7235. *------------------------------------------*/
  7236. BUILDIN_FUNC(getequiprefinerycnt)
  7237. {
  7238. int i = -1,num;
  7239. TBL_PC *sd;
  7240. num = script_getnum(st,2);
  7241. if (!script_charid2sd(3, sd)) {
  7242. script_pushint(st,0);
  7243. return SCRIPT_CMD_FAILURE;
  7244. }
  7245. if (num > 0 && num <= ARRAYLENGTH(equip))
  7246. i=pc_checkequip(sd,equip[num-1]);
  7247. if(i >= 0)
  7248. script_pushint(st,sd->status.inventory[i].refine);
  7249. else
  7250. script_pushint(st,0);
  7251. return SCRIPT_CMD_SUCCESS;
  7252. }
  7253. /*==========================================
  7254. * Get the weapon level value at pos
  7255. * (pos should normally only be EQI_HAND_L or EQI_HAND_R)
  7256. * return (npc)
  7257. * x : weapon level
  7258. * 0 : false
  7259. * getequipweaponlv(<equipment slot>{,<char_id>})
  7260. *------------------------------------------*/
  7261. BUILDIN_FUNC(getequipweaponlv)
  7262. {
  7263. int i = -1,num;
  7264. TBL_PC *sd;
  7265. num = script_getnum(st,2);
  7266. if (!script_charid2sd(3, sd)) {
  7267. script_pushint(st,0);
  7268. return SCRIPT_CMD_FAILURE;
  7269. }
  7270. if (num > 0 && num <= ARRAYLENGTH(equip))
  7271. i=pc_checkequip(sd,equip[num-1]);
  7272. if(i >= 0 && sd->inventory_data[i])
  7273. script_pushint(st,sd->inventory_data[i]->wlv);
  7274. else
  7275. script_pushint(st,0);
  7276. return SCRIPT_CMD_SUCCESS;
  7277. }
  7278. /*==========================================
  7279. * Get the item refine chance (from refine.txt) for item at pos
  7280. * return (npc)
  7281. * x : refine chance
  7282. * 0 : false (max refine level or unequip..)
  7283. * getequippercentrefinery(<equipment slot>{,<char_id>})
  7284. *------------------------------------------*/
  7285. BUILDIN_FUNC(getequippercentrefinery)
  7286. {
  7287. int i = -1,num;
  7288. TBL_PC *sd;
  7289. num = script_getnum(st,2);
  7290. if (!script_charid2sd(3, sd)) {
  7291. script_pushint(st,0);
  7292. return SCRIPT_CMD_FAILURE;
  7293. }
  7294. if (num > 0 && num <= ARRAYLENGTH(equip))
  7295. i = pc_checkequip(sd,equip[num-1]);
  7296. if(i >= 0 && sd->status.inventory[i].nameid && sd->status.inventory[i].refine < MAX_REFINE)
  7297. script_pushint(st,status_get_refine_chance((enum refine_type)itemdb_wlv(sd->status.inventory[i].nameid), (int)sd->status.inventory[i].refine));
  7298. else
  7299. script_pushint(st,0);
  7300. return SCRIPT_CMD_SUCCESS;
  7301. }
  7302. /*==========================================
  7303. * Refine +1 item at pos and log and display refine
  7304. * successrefitem <equipment slot>{,<count>{,<char_id>}}
  7305. *------------------------------------------*/
  7306. BUILDIN_FUNC(successrefitem) {
  7307. short i = -1, up = 1;
  7308. int pos;
  7309. TBL_PC *sd;
  7310. pos = script_getnum(st,2);
  7311. if (!script_charid2sd(4, sd)) {
  7312. script_pushint(st, -1);
  7313. return SCRIPT_CMD_FAILURE;
  7314. }
  7315. if (script_hasdata(st, 3))
  7316. up = script_getnum(st, 3);
  7317. if (pos > 0 && pos <= ARRAYLENGTH(equip))
  7318. i = pc_checkequip(sd,equip[pos-1]);
  7319. if (i >= 0) {
  7320. unsigned int ep = sd->status.inventory[i].equip;
  7321. //Logs items, got from (N)PC scripts [Lupus]
  7322. log_pick_pc(sd, LOG_TYPE_SCRIPT, -1, &sd->status.inventory[i]);
  7323. if (sd->status.inventory[i].refine >= MAX_REFINE) {
  7324. script_pushint(st, MAX_REFINE);
  7325. return SCRIPT_CMD_SUCCESS;
  7326. }
  7327. sd->status.inventory[i].refine += up;
  7328. sd->status.inventory[i].refine = cap_value( sd->status.inventory[i].refine, 0, MAX_REFINE);
  7329. pc_unequipitem(sd,i,2); // status calc will happen in pc_equipitem() below
  7330. clif_refine(sd->fd,0,i,sd->status.inventory[i].refine);
  7331. clif_delitem(sd,i,1,3);
  7332. //Logs items, got from (N)PC scripts [Lupus]
  7333. log_pick_pc(sd, LOG_TYPE_SCRIPT, 1, &sd->status.inventory[i]);
  7334. clif_additem(sd,i,1,0);
  7335. pc_equipitem(sd,i,ep);
  7336. clif_misceffect(&sd->bl,3);
  7337. if (sd->status.inventory[i].refine == MAX_REFINE &&
  7338. sd->status.inventory[i].card[0] == CARD0_FORGE &&
  7339. sd->status.char_id == (int)MakeDWord(sd->status.inventory[i].card[2],sd->status.inventory[i].card[3]))
  7340. { // Fame point system [DracoRPG]
  7341. switch (sd->inventory_data[i]->wlv){
  7342. case 1:
  7343. pc_addfame(sd, battle_config.fame_refine_lv1); // Success to refine to +10 a lv1 weapon you forged = +1 fame point
  7344. break;
  7345. case 2:
  7346. pc_addfame(sd, battle_config.fame_refine_lv2); // Success to refine to +10 a lv2 weapon you forged = +25 fame point
  7347. break;
  7348. case 3:
  7349. pc_addfame(sd, battle_config.fame_refine_lv3); // Success to refine to +10 a lv3 weapon you forged = +1000 fame point
  7350. break;
  7351. }
  7352. }
  7353. script_pushint(st, sd->status.inventory[i].refine);
  7354. return SCRIPT_CMD_SUCCESS;
  7355. }
  7356. ShowError("buildin_successrefitem: No item equipped at pos %d (CID=%d/AID=%d).\n", pos, sd->status.char_id, sd->status.account_id);
  7357. script_pushint(st, -1);
  7358. return SCRIPT_CMD_FAILURE;
  7359. }
  7360. /*==========================================
  7361. * Show a failed Refine +1 attempt
  7362. * failedrefitem <equipment slot>{,<char_id>}
  7363. *------------------------------------------*/
  7364. BUILDIN_FUNC(failedrefitem) {
  7365. short i = -1;
  7366. int pos;
  7367. TBL_PC *sd;
  7368. pos = script_getnum(st,2);
  7369. if (!script_charid2sd(3, sd)) {
  7370. script_pushint(st, 0);
  7371. return SCRIPT_CMD_FAILURE;
  7372. }
  7373. if (pos > 0 && pos <= ARRAYLENGTH(equip))
  7374. i = pc_checkequip(sd,equip[pos-1]);
  7375. if (i >= 0) {
  7376. sd->status.inventory[i].refine = 0;
  7377. pc_unequipitem(sd,i,3); //recalculate bonus
  7378. clif_refine(sd->fd,1,i,sd->status.inventory[i].refine); //notify client of failure
  7379. pc_delitem(sd,i,1,0,2,LOG_TYPE_SCRIPT);
  7380. clif_misceffect(&sd->bl,2); // display failure effect
  7381. script_pushint(st, 1);
  7382. return SCRIPT_CMD_SUCCESS;
  7383. }
  7384. ShowError("buildin_failedrefitem: No item equipped at pos %d (CID=%d/AID=%d).\n", pos, sd->status.char_id, sd->status.account_id);
  7385. script_pushint(st, 0);
  7386. return SCRIPT_CMD_FAILURE;
  7387. }
  7388. /*==========================================
  7389. * Downgrades an Equipment Part by -1 . [Masao]
  7390. * downrefitem <equipment slot>{,<count>{,<char_id>}}
  7391. *------------------------------------------*/
  7392. BUILDIN_FUNC(downrefitem) {
  7393. short i = -1, down = 1;
  7394. int pos;
  7395. TBL_PC *sd;
  7396. if (!script_charid2sd(4, sd)) {
  7397. script_pushint(st, -1);
  7398. return SCRIPT_CMD_FAILURE;
  7399. }
  7400. pos = script_getnum(st,2);
  7401. if (script_hasdata(st, 3))
  7402. down = script_getnum(st, 3);
  7403. if (pos > 0 && pos <= ARRAYLENGTH(equip))
  7404. i = pc_checkequip(sd,equip[pos-1]);
  7405. if (i >= 0) {
  7406. unsigned int ep = sd->status.inventory[i].equip;
  7407. //Logs items, got from (N)PC scripts [Lupus]
  7408. log_pick_pc(sd, LOG_TYPE_SCRIPT, -1, &sd->status.inventory[i]);
  7409. pc_unequipitem(sd,i,2); // status calc will happen in pc_equipitem() below
  7410. sd->status.inventory[i].refine -= down;
  7411. sd->status.inventory[i].refine = cap_value( sd->status.inventory[i].refine, 0, MAX_REFINE);
  7412. clif_refine(sd->fd,2,i,sd->status.inventory[i].refine);
  7413. clif_delitem(sd,i,1,3);
  7414. //Logs items, got from (N)PC scripts [Lupus]
  7415. log_pick_pc(sd, LOG_TYPE_SCRIPT, 1, &sd->status.inventory[i]);
  7416. clif_additem(sd,i,1,0);
  7417. pc_equipitem(sd,i,ep);
  7418. clif_misceffect(&sd->bl,2);
  7419. script_pushint(st, sd->status.inventory[i].refine);
  7420. return SCRIPT_CMD_SUCCESS;
  7421. }
  7422. ShowError("buildin_downrefitem: No item equipped at pos %d (CID=%d/AID=%d).\n", pos, sd->status.char_id, sd->status.account_id);
  7423. script_pushint(st, -1);
  7424. return SCRIPT_CMD_FAILURE;
  7425. }
  7426. /**
  7427. * Delete the item equipped at pos.
  7428. * delequip <equipment slot>{,<char_id>};
  7429. **/
  7430. BUILDIN_FUNC(delequip) {
  7431. short i = -1;
  7432. int pos;
  7433. int8 ret;
  7434. TBL_PC *sd;
  7435. pos = script_getnum(st,2);
  7436. if (!script_charid2sd(3,sd)) {
  7437. st->state = END;
  7438. return SCRIPT_CMD_FAILURE;
  7439. }
  7440. if (pos > 0 && pos <= ARRAYLENGTH(equip))
  7441. i = pc_checkequip(sd,equip[pos-1]);
  7442. if (i >= 0) {
  7443. pc_unequipitem(sd,i,3); //recalculate bonus
  7444. ret = !(pc_delitem(sd,i,1,0,2,LOG_TYPE_SCRIPT));
  7445. }
  7446. else {
  7447. ShowError("buildin_delequip: No item equipped at pos %d (CID=%d/AID=%d).\n", pos, sd->status.char_id, sd->status.account_id);
  7448. st->state = END;
  7449. return SCRIPT_CMD_FAILURE;
  7450. }
  7451. script_pushint(st,ret);
  7452. return SCRIPT_CMD_SUCCESS;
  7453. }
  7454. /**
  7455. * Break the item equipped at pos.
  7456. * breakequip <equipment slot>{,<char_id>};
  7457. **/
  7458. BUILDIN_FUNC(breakequip) {
  7459. short i = -1;
  7460. int pos;
  7461. TBL_PC *sd;
  7462. pos = script_getnum(st,2);
  7463. if (!script_charid2sd(3,sd))
  7464. return SCRIPT_CMD_FAILURE;
  7465. if (pos > 0 && pos <= ARRAYLENGTH(equip))
  7466. i = pc_checkequip(sd,equip[pos-1]);
  7467. if (i >= 0) {
  7468. sd->status.inventory[i].attribute = 1;
  7469. pc_unequipitem(sd,i,3);
  7470. clif_equiplist(sd);
  7471. script_pushint(st,1);
  7472. return SCRIPT_CMD_SUCCESS;
  7473. }
  7474. ShowError("buildin_breakequip: No item equipped at pos %d (CID=%d/AID=%d).\n", pos, sd->status.char_id, sd->status.account_id);
  7475. script_pushint(st,0);
  7476. return SCRIPT_CMD_FAILURE;
  7477. }
  7478. /**
  7479. * statusup <stat>{,<char_id>};
  7480. **/
  7481. BUILDIN_FUNC(statusup)
  7482. {
  7483. int type;
  7484. TBL_PC *sd;
  7485. type = script_getnum(st,2);
  7486. if (!script_charid2sd(3, sd))
  7487. return SCRIPT_CMD_FAILURE;
  7488. pc_statusup(sd, type, 1);
  7489. return SCRIPT_CMD_SUCCESS;
  7490. }
  7491. /**
  7492. * statusup2 <stat>,<amount>{,<char_id>};
  7493. **/
  7494. BUILDIN_FUNC(statusup2)
  7495. {
  7496. int type,val;
  7497. TBL_PC *sd;
  7498. type=script_getnum(st,2);
  7499. val=script_getnum(st,3);
  7500. if (!script_charid2sd(4, sd))
  7501. return SCRIPT_CMD_FAILURE;
  7502. pc_statusup2(sd,type,val);
  7503. return SCRIPT_CMD_SUCCESS;
  7504. }
  7505. /// See 'doc/item_bonus.txt'
  7506. ///
  7507. /// bonus <bonus type>,<val1>;
  7508. /// bonus2 <bonus type>,<val1>,<val2>;
  7509. /// bonus3 <bonus type>,<val1>,<val2>,<val3>;
  7510. /// bonus4 <bonus type>,<val1>,<val2>,<val3>,<val4>;
  7511. /// bonus5 <bonus type>,<val1>,<val2>,<val3>,<val4>,<val5>;
  7512. BUILDIN_FUNC(bonus)
  7513. {
  7514. int type;
  7515. int val1;
  7516. int val2 = 0;
  7517. int val3 = 0;
  7518. int val4 = 0;
  7519. int val5 = 0;
  7520. TBL_PC* sd;
  7521. struct script_data *data;
  7522. sd = script_rid2sd(st);
  7523. if( sd == NULL )
  7524. return 0; // no player attached
  7525. type = script_getnum(st,2);
  7526. switch( type ) {
  7527. case SP_AUTOSPELL:
  7528. case SP_AUTOSPELL_WHENHIT:
  7529. case SP_AUTOSPELL_ONSKILL:
  7530. case SP_SKILL_ATK:
  7531. case SP_SKILL_HEAL:
  7532. case SP_SKILL_HEAL2:
  7533. case SP_ADD_SKILL_BLOW:
  7534. case SP_CASTRATE:
  7535. case SP_ADDEFF_ONSKILL:
  7536. case SP_SKILL_USE_SP_RATE:
  7537. case SP_SKILL_COOLDOWN:
  7538. case SP_SKILL_FIXEDCAST:
  7539. case SP_SKILL_VARIABLECAST:
  7540. case SP_VARCASTRATE:
  7541. case SP_FIXCASTRATE:
  7542. case SP_SKILL_USE_SP:
  7543. case SP_SUB_SKILL:
  7544. // these bonuses support skill names
  7545. data = script_getdata(st, 3);
  7546. get_val(st, data); // Convert into value in case of a variable
  7547. val1 = ( data_isstring(data) ? skill_name2id(script_getstr(st,3)) : script_getnum(st,3) );
  7548. break;
  7549. default:
  7550. val1 = script_getnum(st,3);
  7551. break;
  7552. }
  7553. switch( script_lastdata(st)-2 ) {
  7554. case 1:
  7555. pc_bonus(sd, type, val1);
  7556. break;
  7557. case 2:
  7558. val2 = script_getnum(st,4);
  7559. pc_bonus2(sd, type, val1, val2);
  7560. break;
  7561. case 3:
  7562. val2 = script_getnum(st,4);
  7563. val3 = script_getnum(st,5);
  7564. pc_bonus3(sd, type, val1, val2, val3);
  7565. break;
  7566. case 4:
  7567. data = script_getdata(st, 4);
  7568. get_val(st, data); // Convert into value in case of a variable
  7569. if( type == SP_AUTOSPELL_ONSKILL && data_isstring(data) )
  7570. val2 = skill_name2id(script_getstr(st,4)); // 2nd value can be skill name
  7571. else
  7572. val2 = script_getnum(st,4);
  7573. val3 = script_getnum(st,5);
  7574. val4 = script_getnum(st,6);
  7575. pc_bonus4(sd, type, val1, val2, val3, val4);
  7576. break;
  7577. case 5:
  7578. data = script_getdata(st, 4);
  7579. get_val(st, data); // Convert into value in case of a variable
  7580. if( type == SP_AUTOSPELL_ONSKILL && data_isstring(data) )
  7581. val2 = skill_name2id(script_getstr(st,4)); // 2nd value can be skill name
  7582. else
  7583. val2 = script_getnum(st,4);
  7584. val3 = script_getnum(st,5);
  7585. val4 = script_getnum(st,6);
  7586. val5 = script_getnum(st,7);
  7587. pc_bonus5(sd, type, val1, val2, val3, val4, val5);
  7588. break;
  7589. default:
  7590. ShowDebug("buildin_bonus: unexpected number of arguments (%d)\n", (script_lastdata(st) - 1));
  7591. break;
  7592. }
  7593. return SCRIPT_CMD_SUCCESS;
  7594. }
  7595. BUILDIN_FUNC(autobonus)
  7596. {
  7597. unsigned int dur, pos;
  7598. short rate;
  7599. short atk_type = 0;
  7600. TBL_PC* sd;
  7601. const char *bonus_script, *other_script = NULL;
  7602. sd = script_rid2sd(st);
  7603. if( sd == NULL )
  7604. return 0; // no player attached
  7605. if (current_equip_combo_pos)
  7606. pos = current_equip_combo_pos;
  7607. else
  7608. pos = sd->status.inventory[current_equip_item_index].equip;
  7609. if((sd->state.autobonus&pos) == pos)
  7610. return 0;
  7611. rate = script_getnum(st,3);
  7612. dur = script_getnum(st,4);
  7613. bonus_script = script_getstr(st,2);
  7614. if( !rate || !dur || !pos || !bonus_script )
  7615. return 0;
  7616. if( script_hasdata(st,5) )
  7617. atk_type = script_getnum(st,5);
  7618. if( script_hasdata(st,6) )
  7619. other_script = script_getstr(st,6);
  7620. if( pc_addautobonus(sd->autobonus,ARRAYLENGTH(sd->autobonus),
  7621. bonus_script,rate,dur,atk_type,other_script,pos,false) )
  7622. {
  7623. script_add_autobonus(bonus_script);
  7624. if( other_script )
  7625. script_add_autobonus(other_script);
  7626. }
  7627. return SCRIPT_CMD_SUCCESS;
  7628. }
  7629. BUILDIN_FUNC(autobonus2)
  7630. {
  7631. unsigned int dur, pos;
  7632. short rate;
  7633. short atk_type = 0;
  7634. TBL_PC* sd;
  7635. const char *bonus_script, *other_script = NULL;
  7636. sd = script_rid2sd(st);
  7637. if( sd == NULL )
  7638. return 0; // no player attached
  7639. if (current_equip_combo_pos)
  7640. pos = current_equip_combo_pos;
  7641. else
  7642. pos = sd->status.inventory[current_equip_item_index].equip;
  7643. if((sd->state.autobonus&pos) == pos)
  7644. return 0;
  7645. rate = script_getnum(st,3);
  7646. dur = script_getnum(st,4);
  7647. bonus_script = script_getstr(st,2);
  7648. if( !rate || !dur || !pos || !bonus_script )
  7649. return 0;
  7650. if( script_hasdata(st,5) )
  7651. atk_type = script_getnum(st,5);
  7652. if( script_hasdata(st,6) )
  7653. other_script = script_getstr(st,6);
  7654. if( pc_addautobonus(sd->autobonus2,ARRAYLENGTH(sd->autobonus2),
  7655. bonus_script,rate,dur,atk_type,other_script,pos,false) )
  7656. {
  7657. script_add_autobonus(bonus_script);
  7658. if( other_script )
  7659. script_add_autobonus(other_script);
  7660. }
  7661. return SCRIPT_CMD_SUCCESS;
  7662. }
  7663. BUILDIN_FUNC(autobonus3)
  7664. {
  7665. unsigned int dur, pos;
  7666. short rate,atk_type;
  7667. TBL_PC* sd;
  7668. const char *bonus_script, *other_script = NULL;
  7669. struct script_data *data;
  7670. sd = script_rid2sd(st);
  7671. if( sd == NULL )
  7672. return 0; // no player attached
  7673. if (current_equip_combo_pos)
  7674. pos = current_equip_combo_pos;
  7675. else
  7676. pos = sd->status.inventory[current_equip_item_index].equip;
  7677. if((sd->state.autobonus&pos) == pos)
  7678. return 0;
  7679. rate = script_getnum(st,3);
  7680. dur = script_getnum(st,4);
  7681. data = script_getdata(st, 5);
  7682. get_val(st, data); // Convert into value in case of a variable
  7683. atk_type = ( data_isstring(data) ? skill_name2id(script_getstr(st,5)) : script_getnum(st,5) );
  7684. bonus_script = script_getstr(st,2);
  7685. if( !rate || !dur || !pos || !atk_type || !bonus_script )
  7686. return 0;
  7687. if( script_hasdata(st,6) )
  7688. other_script = script_getstr(st,6);
  7689. if( pc_addautobonus(sd->autobonus3,ARRAYLENGTH(sd->autobonus3),
  7690. bonus_script,rate,dur,atk_type,other_script,pos,true) )
  7691. {
  7692. script_add_autobonus(bonus_script);
  7693. if( other_script )
  7694. script_add_autobonus(other_script);
  7695. }
  7696. return SCRIPT_CMD_SUCCESS;
  7697. }
  7698. /// Changes the level of a player skill.
  7699. /// <flag> defaults to 1
  7700. /// <flag>=0 : set the level of the skill
  7701. /// <flag>=1 : set the temporary level of the skill
  7702. /// <flag>=2 : add to the level of the skill
  7703. ///
  7704. /// skill <skill id>,<level>,<flag>
  7705. /// skill <skill id>,<level>
  7706. /// skill "<skill name>",<level>,<flag>
  7707. /// skill "<skill name>",<level>
  7708. BUILDIN_FUNC(skill)
  7709. {
  7710. int id;
  7711. int level;
  7712. int flag = ADDSKILL_TEMP;
  7713. TBL_PC* sd;
  7714. struct script_data *data;
  7715. const char* command = script_getfuncname(st);
  7716. sd = script_rid2sd(st);
  7717. if( sd == NULL )
  7718. return 0;// no player attached, report source
  7719. if (strcmpi(command, "addtoskill") == 0)
  7720. flag = ADDSKILL_TEMP_ADDLEVEL;
  7721. data = script_getdata(st, 2);
  7722. get_val(st, data); // Convert into value in case of a variable
  7723. id = ( data_isstring(data) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  7724. level = script_getnum(st,3);
  7725. if( script_hasdata(st,4) )
  7726. flag = script_getnum(st,4);
  7727. pc_skill(sd, id, level, (enum e_addskill_type)flag);
  7728. return SCRIPT_CMD_SUCCESS;
  7729. }
  7730. /// Increases the level of a guild skill.
  7731. ///
  7732. /// guildskill <skill id>,<amount>;
  7733. /// guildskill "<skill name>",<amount>;
  7734. BUILDIN_FUNC(guildskill)
  7735. {
  7736. int id;
  7737. int level;
  7738. TBL_PC* sd;
  7739. int i;
  7740. struct script_data *data;
  7741. sd = script_rid2sd(st);
  7742. if( sd == NULL )
  7743. return 0;// no player attached, report source
  7744. data = script_getdata(st, 2);
  7745. get_val(st, data); // Convert into value in case of a variable
  7746. id = ( data_isstring(data) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  7747. level = script_getnum(st,3);
  7748. for( i=0; i < level; i++ )
  7749. guild_skillup(sd, id);
  7750. return SCRIPT_CMD_SUCCESS;
  7751. }
  7752. /// Returns the level of the player skill.
  7753. ///
  7754. /// getskilllv(<skill id>) -> <level>
  7755. /// getskilllv("<skill name>") -> <level>
  7756. BUILDIN_FUNC(getskilllv)
  7757. {
  7758. int id;
  7759. TBL_PC* sd;
  7760. struct script_data *data;
  7761. sd = script_rid2sd(st);
  7762. if( sd == NULL )
  7763. return 0;// no player attached, report source
  7764. data = script_getdata(st, 2);
  7765. get_val(st, data); // Convert into value in case of a variable
  7766. id = ( data_isstring(data) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  7767. script_pushint(st, pc_checkskill(sd,id));
  7768. return SCRIPT_CMD_SUCCESS;
  7769. }
  7770. /// Returns the level of the guild skill.
  7771. ///
  7772. /// getgdskilllv(<guild id>,<skill id>) -> <level>
  7773. /// getgdskilllv(<guild id>,"<skill name>") -> <level>
  7774. BUILDIN_FUNC(getgdskilllv)
  7775. {
  7776. int guild_id;
  7777. uint16 skill_id;
  7778. struct guild* g;
  7779. struct script_data *data;
  7780. guild_id = script_getnum(st,2);
  7781. data = script_getdata(st, 3);
  7782. get_val(st, data); // Convert into value in case of a variable
  7783. skill_id = ( data_isstring(data) ? skill_name2id(script_getstr(st,3)) : script_getnum(st,3) );
  7784. g = guild_search(guild_id);
  7785. if( g == NULL )
  7786. script_pushint(st, -1);
  7787. else
  7788. script_pushint(st, guild_checkskill(g,skill_id));
  7789. return SCRIPT_CMD_SUCCESS;
  7790. }
  7791. /// Returns the 'basic_skill_check' setting.
  7792. /// This config determines if the server checks the skill level of NV_BASIC
  7793. /// before allowing the basic actions.
  7794. ///
  7795. /// basicskillcheck() -> <bool>
  7796. BUILDIN_FUNC(basicskillcheck)
  7797. {
  7798. script_pushint(st, battle_config.basic_skill_check);
  7799. return SCRIPT_CMD_SUCCESS;
  7800. }
  7801. /// Returns the GM level of the player.
  7802. ///
  7803. /// getgmlevel({<char_id>}) -> <level>
  7804. BUILDIN_FUNC(getgmlevel)
  7805. {
  7806. TBL_PC* sd;
  7807. if (!script_charid2sd(2,sd))
  7808. return SCRIPT_CMD_FAILURE;
  7809. script_pushint(st, pc_get_group_level(sd));
  7810. return SCRIPT_CMD_SUCCESS;
  7811. }
  7812. /// Returns the group ID of the player.
  7813. ///
  7814. /// getgroupid({<char_id>}) -> <int>
  7815. BUILDIN_FUNC(getgroupid)
  7816. {
  7817. TBL_PC* sd;
  7818. if (!script_charid2sd(2,sd))
  7819. return SCRIPT_CMD_FAILURE;
  7820. script_pushint(st, pc_get_group_id(sd));
  7821. return SCRIPT_CMD_SUCCESS;
  7822. }
  7823. /// Terminates the execution of this script instance.
  7824. ///
  7825. /// end
  7826. BUILDIN_FUNC(end)
  7827. {
  7828. TBL_PC* sd;
  7829. sd = map_id2sd(st->rid);
  7830. st->state = END;
  7831. if( st->mes_active )
  7832. st->mes_active = 0;
  7833. if( sd )
  7834. clif_scriptclose(sd, st->oid); // If a menu/select/prompt is active, close it.
  7835. return SCRIPT_CMD_SUCCESS;
  7836. }
  7837. /// Checks if the player has that effect state (option).
  7838. ///
  7839. /// checkoption(<option>{,<char_id>}) -> <bool>
  7840. BUILDIN_FUNC(checkoption)
  7841. {
  7842. int option;
  7843. TBL_PC* sd;
  7844. if (!script_charid2sd(3,sd))
  7845. return SCRIPT_CMD_FAILURE;
  7846. option = script_getnum(st,2);
  7847. if( sd->sc.option&option )
  7848. script_pushint(st, 1);
  7849. else
  7850. script_pushint(st, 0);
  7851. return SCRIPT_CMD_SUCCESS;
  7852. }
  7853. /// Checks if the player is in that body state (opt1).
  7854. ///
  7855. /// checkoption1(<opt1>{,<char_id>}) -> <bool>
  7856. BUILDIN_FUNC(checkoption1)
  7857. {
  7858. int opt1;
  7859. TBL_PC* sd;
  7860. if (!script_charid2sd(3,sd))
  7861. return SCRIPT_CMD_FAILURE;
  7862. opt1 = script_getnum(st,2);
  7863. if( sd->sc.opt1 == opt1 )
  7864. script_pushint(st, 1);
  7865. else
  7866. script_pushint(st, 0);
  7867. return SCRIPT_CMD_SUCCESS;
  7868. }
  7869. /// Checks if the player has that health state (opt2).
  7870. ///
  7871. /// checkoption2(<opt2>{,<char_id>}) -> <bool>
  7872. BUILDIN_FUNC(checkoption2)
  7873. {
  7874. int opt2;
  7875. TBL_PC* sd;
  7876. if (!script_charid2sd(3,sd))
  7877. return SCRIPT_CMD_FAILURE;
  7878. opt2 = script_getnum(st,2);
  7879. if( sd->sc.opt2&opt2 )
  7880. script_pushint(st, 1);
  7881. else
  7882. script_pushint(st, 0);
  7883. return SCRIPT_CMD_SUCCESS;
  7884. }
  7885. /// Changes the effect state (option) of the player.
  7886. /// <flag> defaults to 1
  7887. /// <flag>=0 : removes the option
  7888. /// <flag>=other : adds the option
  7889. ///
  7890. /// setoption <option>{,<flag>{,<char_id>}};
  7891. BUILDIN_FUNC(setoption)
  7892. {
  7893. int option;
  7894. int flag = 1;
  7895. TBL_PC* sd;
  7896. if (!script_charid2sd(4,sd))
  7897. return SCRIPT_CMD_FAILURE;
  7898. option = script_getnum(st,2);
  7899. if( script_hasdata(st,3) )
  7900. flag = script_getnum(st,3);
  7901. else if( !option ){// Request to remove everything.
  7902. flag = 0;
  7903. option = OPTION_FALCON|OPTION_RIDING;
  7904. #ifndef NEW_CARTS
  7905. option |= OPTION_CART;
  7906. #endif
  7907. }
  7908. if( flag ){// Add option
  7909. if( option&OPTION_WEDDING && !battle_config.wedding_modifydisplay )
  7910. option &= ~OPTION_WEDDING;// Do not show the wedding sprites
  7911. pc_setoption(sd, sd->sc.option|option);
  7912. } else// Remove option
  7913. pc_setoption(sd, sd->sc.option&~option);
  7914. return SCRIPT_CMD_SUCCESS;
  7915. }
  7916. /// Returns if the player has a cart.
  7917. ///
  7918. /// checkcart({char_id}) -> <bool>
  7919. ///
  7920. /// @author Valaris
  7921. BUILDIN_FUNC(checkcart)
  7922. {
  7923. TBL_PC* sd;
  7924. if (!script_charid2sd(2,sd))
  7925. return SCRIPT_CMD_FAILURE;
  7926. if( pc_iscarton(sd) )
  7927. script_pushint(st, 1);
  7928. else
  7929. script_pushint(st, 0);
  7930. return SCRIPT_CMD_SUCCESS;
  7931. }
  7932. /// Sets the cart of the player.
  7933. /// <type> defaults to 1
  7934. /// <type>=0 : removes the cart
  7935. /// <type>=1 : Normal cart
  7936. /// <type>=2 : Wooden cart
  7937. /// <type>=3 : Covered cart with flowers and ferns
  7938. /// <type>=4 : Wooden cart with a Panda doll on the back
  7939. /// <type>=5 : Normal cart with bigger wheels, a roof and a banner on the back
  7940. ///
  7941. /// setcart {<type>{,<char_id>}};
  7942. BUILDIN_FUNC(setcart)
  7943. {
  7944. int type = 1;
  7945. TBL_PC* sd;
  7946. if (!script_charid2sd(3,sd))
  7947. return SCRIPT_CMD_FAILURE;
  7948. if( script_hasdata(st,2) )
  7949. type = script_getnum(st,2);
  7950. pc_setcart(sd, type);
  7951. return SCRIPT_CMD_SUCCESS;
  7952. }
  7953. /// Returns if the player has a falcon.
  7954. ///
  7955. /// checkfalcon({char_id}) -> <bool>
  7956. ///
  7957. /// @author Valaris
  7958. BUILDIN_FUNC(checkfalcon)
  7959. {
  7960. TBL_PC* sd;
  7961. if (!script_charid2sd(2,sd))
  7962. return SCRIPT_CMD_FAILURE;
  7963. if( pc_isfalcon(sd) )
  7964. script_pushint(st, 1);
  7965. else
  7966. script_pushint(st, 0);
  7967. return SCRIPT_CMD_SUCCESS;
  7968. }
  7969. /// Sets if the player has a falcon or not.
  7970. /// <flag> defaults to 1
  7971. ///
  7972. /// setfalcon {<flag>{,<char_id>}};
  7973. BUILDIN_FUNC(setfalcon)
  7974. {
  7975. int flag = 1;
  7976. TBL_PC* sd;
  7977. if (!script_charid2sd(3,sd))
  7978. return SCRIPT_CMD_FAILURE;
  7979. if( script_hasdata(st,2) )
  7980. flag = script_getnum(st,2);
  7981. pc_setfalcon(sd, flag);
  7982. return SCRIPT_CMD_SUCCESS;
  7983. }
  7984. /// Returns if the player is riding.
  7985. ///
  7986. /// checkriding({char_id}) -> <bool>
  7987. ///
  7988. /// @author Valaris
  7989. BUILDIN_FUNC(checkriding)
  7990. {
  7991. TBL_PC* sd;
  7992. if (!script_charid2sd(2,sd))
  7993. return SCRIPT_CMD_FAILURE;
  7994. if( pc_isriding(sd) )
  7995. script_pushint(st, 1);
  7996. else
  7997. script_pushint(st, 0);
  7998. return SCRIPT_CMD_SUCCESS;
  7999. }
  8000. /// Sets if the player is riding.
  8001. /// <flag> defaults to 1
  8002. ///
  8003. /// setriding {<flag>{,<char_id>}};
  8004. BUILDIN_FUNC(setriding)
  8005. {
  8006. int flag = 1;
  8007. TBL_PC* sd;
  8008. if (!script_charid2sd(3,sd))
  8009. return SCRIPT_CMD_FAILURE;
  8010. if( script_hasdata(st,2) )
  8011. flag = script_getnum(st,2);
  8012. pc_setriding(sd, flag);
  8013. return SCRIPT_CMD_SUCCESS;
  8014. }
  8015. /// Returns if the player has a warg.
  8016. ///
  8017. /// checkwug({char_id}) -> <bool>
  8018. ///
  8019. BUILDIN_FUNC(checkwug)
  8020. {
  8021. TBL_PC* sd;
  8022. if (!script_charid2sd(2,sd))
  8023. return SCRIPT_CMD_FAILURE;
  8024. if( pc_iswug(sd) || pc_isridingwug(sd) )
  8025. script_pushint(st, 1);
  8026. else
  8027. script_pushint(st, 0);
  8028. return SCRIPT_CMD_SUCCESS;
  8029. }
  8030. /// Returns if the player is wearing MADO Gear.
  8031. ///
  8032. /// checkmadogear({<char_id>}) -> <bool>
  8033. ///
  8034. BUILDIN_FUNC(checkmadogear)
  8035. {
  8036. TBL_PC* sd;
  8037. if (!script_charid2sd(2,sd))
  8038. return SCRIPT_CMD_FAILURE;
  8039. if( pc_ismadogear(sd) )
  8040. script_pushint(st, 1);
  8041. else
  8042. script_pushint(st, 0);
  8043. return SCRIPT_CMD_SUCCESS;
  8044. }
  8045. /// Sets if the player is riding MADO Gear.
  8046. /// <flag> defaults to 1
  8047. ///
  8048. /// setmadogear {<flag>{,<char_id>}};
  8049. BUILDIN_FUNC(setmadogear)
  8050. {
  8051. int flag = 1;
  8052. TBL_PC* sd;
  8053. if (!script_charid2sd(3,sd))
  8054. return SCRIPT_CMD_FAILURE;
  8055. if( script_hasdata(st,2) )
  8056. flag = script_getnum(st,2);
  8057. pc_setmadogear(sd, flag);
  8058. return SCRIPT_CMD_SUCCESS;
  8059. }
  8060. /// Sets the save point of the player.
  8061. ///
  8062. /// save "<map name>",<x>,<y>{,<char_id>}
  8063. /// savepoint "<map name>",<x>,<y>{,<char_id>}
  8064. BUILDIN_FUNC(savepoint)
  8065. {
  8066. int x;
  8067. int y;
  8068. short map_idx;
  8069. const char* str;
  8070. TBL_PC* sd;
  8071. if (!script_charid2sd(5,sd))
  8072. return SCRIPT_CMD_FAILURE;// no player attached, report source
  8073. str = script_getstr(st, 2);
  8074. x = script_getnum(st,3);
  8075. y = script_getnum(st,4);
  8076. map_idx = mapindex_name2id(str);
  8077. if( map_idx )
  8078. pc_setsavepoint(sd, map_idx, x, y);
  8079. return SCRIPT_CMD_SUCCESS;
  8080. }
  8081. /*==========================================
  8082. * GetTimeTick(0: System Tick, 1: Time Second Tick)
  8083. *------------------------------------------*/
  8084. BUILDIN_FUNC(gettimetick) /* Asgard Version */
  8085. {
  8086. int type;
  8087. time_t timer;
  8088. struct tm *t;
  8089. type=script_getnum(st,2);
  8090. switch(type){
  8091. case 2:
  8092. //type 2:(Get the number of seconds elapsed since 00:00 hours, Jan 1, 1970 UTC
  8093. // from the system clock.)
  8094. script_pushint(st,(int)time(NULL));
  8095. break;
  8096. case 1:
  8097. //type 1:(Second Ticks: 0-86399, 00:00:00-23:59:59)
  8098. time(&timer);
  8099. t=localtime(&timer);
  8100. script_pushint(st,((t->tm_hour)*3600+(t->tm_min)*60+t->tm_sec));
  8101. break;
  8102. case 0:
  8103. default:
  8104. //type 0:(System Ticks)
  8105. script_pushint(st,gettick());
  8106. break;
  8107. }
  8108. return SCRIPT_CMD_SUCCESS;
  8109. }
  8110. /*==========================================
  8111. * GetTime(Type);
  8112. * 1: Sec 2: Min 3: Hour
  8113. * 4: WeekDay 5: MonthDay 6: Month
  8114. * 7: Year
  8115. *------------------------------------------*/
  8116. BUILDIN_FUNC(gettime) /* Asgard Version */
  8117. {
  8118. int type;
  8119. time_t timer;
  8120. struct tm *t;
  8121. type=script_getnum(st,2);
  8122. time(&timer);
  8123. t=localtime(&timer);
  8124. switch(type){
  8125. case 1://Sec(0~59)
  8126. script_pushint(st,t->tm_sec);
  8127. break;
  8128. case 2://Min(0~59)
  8129. script_pushint(st,t->tm_min);
  8130. break;
  8131. case 3://Hour(0~23)
  8132. script_pushint(st,t->tm_hour);
  8133. break;
  8134. case 4://WeekDay(0~6)
  8135. script_pushint(st,t->tm_wday);
  8136. break;
  8137. case 5://MonthDay(01~31)
  8138. script_pushint(st,t->tm_mday);
  8139. break;
  8140. case 6://Month(01~12)
  8141. script_pushint(st,t->tm_mon+1);
  8142. break;
  8143. case 7://Year(20xx)
  8144. script_pushint(st,t->tm_year+1900);
  8145. break;
  8146. case 8://Year Day(01~366)
  8147. script_pushint(st,t->tm_yday+1);
  8148. break;
  8149. default://(format error)
  8150. script_pushint(st,-1);
  8151. break;
  8152. }
  8153. return SCRIPT_CMD_SUCCESS;
  8154. }
  8155. /*==========================================
  8156. * GetTimeStr("TimeFMT", Length);
  8157. *------------------------------------------*/
  8158. BUILDIN_FUNC(gettimestr)
  8159. {
  8160. char *tmpstr;
  8161. const char *fmtstr;
  8162. int maxlen;
  8163. time_t now = time(NULL);
  8164. fmtstr=script_getstr(st,2);
  8165. maxlen=script_getnum(st,3);
  8166. tmpstr=(char *)aMalloc((maxlen+1)*sizeof(char));
  8167. strftime(tmpstr,maxlen,fmtstr,localtime(&now));
  8168. tmpstr[maxlen]='\0';
  8169. script_pushstr(st,tmpstr);
  8170. return SCRIPT_CMD_SUCCESS;
  8171. }
  8172. /*==========================================
  8173. * Open player storage
  8174. *------------------------------------------*/
  8175. BUILDIN_FUNC(openstorage)
  8176. {
  8177. TBL_PC* sd;
  8178. sd = script_rid2sd(st);
  8179. if( sd == NULL )
  8180. return 0;
  8181. storage_storageopen(sd);
  8182. return SCRIPT_CMD_SUCCESS;
  8183. }
  8184. BUILDIN_FUNC(guildopenstorage)
  8185. {
  8186. TBL_PC* sd;
  8187. int ret;
  8188. sd = script_rid2sd(st);
  8189. if( sd == NULL )
  8190. return 0;
  8191. ret = gstorage_storageopen(sd);
  8192. script_pushint(st,ret);
  8193. return SCRIPT_CMD_SUCCESS;
  8194. }
  8195. /*==========================================
  8196. * Make player use a skill trought item usage
  8197. *------------------------------------------*/
  8198. /// itemskill <skill id>,<level>
  8199. /// itemskill "<skill name>",<level>
  8200. BUILDIN_FUNC(itemskill)
  8201. {
  8202. int id;
  8203. int lv;
  8204. TBL_PC* sd;
  8205. struct script_data *data;
  8206. sd = script_rid2sd(st);
  8207. if( sd == NULL || sd->ud.skilltimer != INVALID_TIMER )
  8208. return 0;
  8209. data = script_getdata(st, 2);
  8210. get_val(st, data); // Convert into value in case of a variable
  8211. id = ( data_isstring(data) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  8212. lv = script_getnum(st,3);
  8213. sd->skillitem=id;
  8214. sd->skillitemlv=lv;
  8215. clif_item_skill(sd,id,lv);
  8216. return SCRIPT_CMD_SUCCESS;
  8217. }
  8218. /*==========================================
  8219. * Attempt to create an item
  8220. *------------------------------------------*/
  8221. BUILDIN_FUNC(produce)
  8222. {
  8223. int trigger;
  8224. TBL_PC* sd;
  8225. sd = script_rid2sd(st);
  8226. if( sd == NULL )
  8227. return 0;
  8228. trigger=script_getnum(st,2);
  8229. clif_skill_produce_mix_list(sd, -1, trigger);
  8230. return SCRIPT_CMD_SUCCESS;
  8231. }
  8232. /*==========================================
  8233. *
  8234. *------------------------------------------*/
  8235. BUILDIN_FUNC(cooking)
  8236. {
  8237. int trigger;
  8238. TBL_PC* sd;
  8239. sd = script_rid2sd(st);
  8240. if( sd == NULL )
  8241. return 0;
  8242. trigger=script_getnum(st,2);
  8243. clif_cooking_list(sd, trigger, AM_PHARMACY, 1, 1);
  8244. return SCRIPT_CMD_SUCCESS;
  8245. }
  8246. /*==========================================
  8247. * Create a pet
  8248. *------------------------------------------*/
  8249. BUILDIN_FUNC(makepet)
  8250. {
  8251. TBL_PC* sd;
  8252. int id,pet_id;
  8253. id=script_getnum(st,2);
  8254. sd = script_rid2sd(st);
  8255. if( sd == NULL )
  8256. return 0;
  8257. pet_id = search_petDB_index(id, PET_CLASS);
  8258. if (pet_id < 0)
  8259. pet_id = search_petDB_index(id, PET_EGG);
  8260. if (pet_id >= 0 && sd) {
  8261. sd->catch_target_class = pet_db[pet_id].class_;
  8262. intif_create_pet(
  8263. sd->status.account_id, sd->status.char_id,
  8264. (short)pet_db[pet_id].class_, (short)mob_db(pet_db[pet_id].class_)->lv,
  8265. (short)pet_db[pet_id].EggID, 0, (short)pet_db[pet_id].intimate,
  8266. 100, 0, 1, pet_db[pet_id].jname);
  8267. }
  8268. return SCRIPT_CMD_SUCCESS;
  8269. }
  8270. /**
  8271. * Give player exp base,job * quest_exp_rate/100
  8272. * getexp <base xp>,<job xp>{,<char_id>};
  8273. **/
  8274. BUILDIN_FUNC(getexp)
  8275. {
  8276. TBL_PC* sd;
  8277. int base=0,job=0;
  8278. double bonus;
  8279. if (!script_charid2sd(4,sd))
  8280. return SCRIPT_CMD_FAILURE;
  8281. base=script_getnum(st,2);
  8282. job =script_getnum(st,3);
  8283. if(base<0 || job<0)
  8284. return 0;
  8285. // bonus for npc-given exp
  8286. bonus = battle_config.quest_exp_rate / 100.;
  8287. base = (int) cap_value(base * bonus, 0, INT_MAX);
  8288. job = (int) cap_value(job * bonus, 0, INT_MAX);
  8289. pc_gainexp(sd, NULL, base, job, true);
  8290. return SCRIPT_CMD_SUCCESS;
  8291. }
  8292. /*==========================================
  8293. * Gain guild exp [Celest]
  8294. *------------------------------------------*/
  8295. BUILDIN_FUNC(guildgetexp)
  8296. {
  8297. TBL_PC* sd;
  8298. int exp;
  8299. sd = script_rid2sd(st);
  8300. if( sd == NULL )
  8301. return 0;
  8302. exp = script_getnum(st,2);
  8303. if(exp < 0)
  8304. return 0;
  8305. if(sd && sd->status.guild_id > 0)
  8306. guild_getexp (sd, exp);
  8307. return SCRIPT_CMD_SUCCESS;
  8308. }
  8309. /*==========================================
  8310. * Changes the guild master of a guild [Skotlex]
  8311. *------------------------------------------*/
  8312. BUILDIN_FUNC(guildchangegm)
  8313. {
  8314. TBL_PC *sd;
  8315. int guild_id;
  8316. const char *name;
  8317. guild_id = script_getnum(st,2);
  8318. name = script_getstr(st,3);
  8319. sd=map_nick2sd(name);
  8320. if (!sd)
  8321. script_pushint(st,0);
  8322. else
  8323. script_pushint(st,guild_gm_change(guild_id, sd));
  8324. return SCRIPT_CMD_SUCCESS;
  8325. }
  8326. /*==========================================
  8327. * Spawn a monster:
  8328. * *monster "<map name>",<x>,<y>,"<name to show>",<mob id>,<amount>{,"<event label>",<size>,<ai>};
  8329. *------------------------------------------*/
  8330. BUILDIN_FUNC(monster)
  8331. {
  8332. const char* mapn = script_getstr(st,2);
  8333. int x = script_getnum(st,3);
  8334. int y = script_getnum(st,4);
  8335. const char* str = script_getstr(st,5);
  8336. int class_ = script_getnum(st,6);
  8337. int amount = script_getnum(st,7);
  8338. const char* event = "";
  8339. unsigned int size = SZ_SMALL;
  8340. unsigned int ai = AI_NONE;
  8341. struct map_session_data* sd;
  8342. int16 m;
  8343. int i;
  8344. if (script_hasdata(st, 8)) {
  8345. event = script_getstr(st, 8);
  8346. check_event(st, event);
  8347. }
  8348. if (script_hasdata(st, 9)) {
  8349. size = script_getnum(st, 9);
  8350. if (size > 3) {
  8351. ShowWarning("buildin_monster: Attempted to spawn non-existing size %d for monster class %d\n", size, class_);
  8352. return 1;
  8353. }
  8354. }
  8355. if (script_hasdata(st, 10)) {
  8356. ai = script_getnum(st, 10);
  8357. if (ai >= AI_MAX) {
  8358. ShowWarning("buildin_monster: Attempted to spawn non-existing ai %d for monster class %d\n", ai, class_);
  8359. return 1;
  8360. }
  8361. }
  8362. if (class_ >= 0 && !mobdb_checkid(class_)) {
  8363. ShowWarning("buildin_monster: Attempted to spawn non-existing monster class %d\n", class_);
  8364. return 1;
  8365. }
  8366. sd = map_id2sd(st->rid);
  8367. if (sd && strcmp(mapn, "this") == 0)
  8368. m = sd->bl.m;
  8369. else
  8370. m = map_mapname2mapid(mapn);
  8371. for(i = 0; i < amount; i++) { //not optimised
  8372. int mobid = mob_once_spawn(sd, m, x, y, str, class_, 1, event, size, ai);
  8373. if (mobid)
  8374. mapreg_setreg(reference_uid(add_str("$@mobid"), i), mobid);
  8375. }
  8376. return SCRIPT_CMD_SUCCESS;
  8377. }
  8378. /*==========================================
  8379. * Request List of Monster Drops
  8380. *------------------------------------------*/
  8381. BUILDIN_FUNC(getmobdrops)
  8382. {
  8383. int class_ = script_getnum(st,2);
  8384. int i, j = 0;
  8385. struct mob_db *mob;
  8386. if( !mobdb_checkid(class_) )
  8387. {
  8388. script_pushint(st, 0);
  8389. return 0;
  8390. }
  8391. mob = mob_db(class_);
  8392. for( i = 0; i < MAX_MOB_DROP; i++ )
  8393. {
  8394. if( mob->dropitem[i].nameid < 1 )
  8395. continue;
  8396. if( itemdb_exists(mob->dropitem[i].nameid) == NULL )
  8397. continue;
  8398. mapreg_setreg(reference_uid(add_str("$@MobDrop_item"), j), mob->dropitem[i].nameid);
  8399. mapreg_setreg(reference_uid(add_str("$@MobDrop_rate"), j), mob->dropitem[i].p);
  8400. j++;
  8401. }
  8402. mapreg_setreg(add_str("$@MobDrop_count"), j);
  8403. script_pushint(st, 1);
  8404. return SCRIPT_CMD_SUCCESS;
  8405. }
  8406. /*==========================================
  8407. * Spawn a monster in a random location
  8408. * in x0,x1,y0,y1 area.
  8409. *------------------------------------------*/
  8410. BUILDIN_FUNC(areamonster)
  8411. {
  8412. const char* mapn = script_getstr(st,2);
  8413. int x0 = script_getnum(st,3);
  8414. int y0 = script_getnum(st,4);
  8415. int x1 = script_getnum(st,5);
  8416. int y1 = script_getnum(st,6);
  8417. const char* str = script_getstr(st,7);
  8418. int class_ = script_getnum(st,8);
  8419. int amount = script_getnum(st,9);
  8420. const char* event = "";
  8421. unsigned int size = SZ_SMALL;
  8422. unsigned int ai = AI_NONE;
  8423. struct map_session_data* sd;
  8424. int16 m;
  8425. int i;
  8426. if (script_hasdata(st,10)) {
  8427. event = script_getstr(st, 10);
  8428. check_event(st, event);
  8429. }
  8430. if (script_hasdata(st, 11)) {
  8431. size = script_getnum(st, 11);
  8432. if (size > 3) {
  8433. ShowWarning("buildin_monster: Attempted to spawn non-existing size %d for monster class %d\n", size, class_);
  8434. return 1;
  8435. }
  8436. }
  8437. if (script_hasdata(st, 12)) {
  8438. ai = script_getnum(st, 12);
  8439. if (ai >= AI_MAX) {
  8440. ShowWarning("buildin_monster: Attempted to spawn non-existing ai %d for monster class %d\n", ai, class_);
  8441. return 1;
  8442. }
  8443. }
  8444. sd = map_id2sd(st->rid);
  8445. if (sd && strcmp(mapn, "this") == 0)
  8446. m = sd->bl.m;
  8447. else
  8448. m = map_mapname2mapid(mapn);
  8449. for(i = 0; i < amount; i++) { //not optimised
  8450. int mobid = mob_once_spawn_area(sd, m, x0, y0, x1, y1, str, class_, 1, event, size, ai);
  8451. if (mobid)
  8452. mapreg_setreg(reference_uid(add_str("$@mobid"), i), mobid);
  8453. }
  8454. return SCRIPT_CMD_SUCCESS;
  8455. }
  8456. /*==========================================
  8457. * KillMonster subcheck, verify if mob to kill ain't got an even to handle, could be force kill by allflag
  8458. *------------------------------------------*/
  8459. static int buildin_killmonster_sub_strip(struct block_list *bl,va_list ap)
  8460. { //same fix but with killmonster instead - stripping events from mobs.
  8461. TBL_MOB* md = (TBL_MOB*)bl;
  8462. char *event=va_arg(ap,char *);
  8463. int allflag=va_arg(ap,int);
  8464. md->state.npc_killmonster = 1;
  8465. if(!allflag){
  8466. if(strcmp(event,md->npc_event)==0)
  8467. status_kill(bl);
  8468. }else{
  8469. if(!md->spawn)
  8470. status_kill(bl);
  8471. }
  8472. md->state.npc_killmonster = 0;
  8473. return 0;
  8474. }
  8475. static int buildin_killmonster_sub(struct block_list *bl,va_list ap)
  8476. {
  8477. TBL_MOB* md = (TBL_MOB*)bl;
  8478. char *event=va_arg(ap,char *);
  8479. int allflag=va_arg(ap,int);
  8480. if(!allflag){
  8481. if(strcmp(event,md->npc_event)==0)
  8482. status_kill(bl);
  8483. }else{
  8484. if(!md->spawn)
  8485. status_kill(bl);
  8486. }
  8487. return 0;
  8488. }
  8489. BUILDIN_FUNC(killmonster)
  8490. {
  8491. const char *mapname,*event;
  8492. int16 m,allflag=0;
  8493. mapname=script_getstr(st,2);
  8494. event=script_getstr(st,3);
  8495. if(strcmp(event,"All")==0)
  8496. allflag = 1;
  8497. else
  8498. check_event(st, event);
  8499. if( (m=map_mapname2mapid(mapname))<0 )
  8500. return 0;
  8501. if( script_hasdata(st,4) ) {
  8502. if ( script_getnum(st,4) == 1 ) {
  8503. map_foreachinmap(buildin_killmonster_sub, m, BL_MOB, event ,allflag);
  8504. return 0;
  8505. }
  8506. }
  8507. map_freeblock_lock();
  8508. map_foreachinmap(buildin_killmonster_sub_strip, m, BL_MOB, event ,allflag);
  8509. map_freeblock_unlock();
  8510. return SCRIPT_CMD_SUCCESS;
  8511. }
  8512. static int buildin_killmonsterall_sub_strip(struct block_list *bl,va_list ap)
  8513. { //Strips the event from the mob if it's killed the old method.
  8514. struct mob_data *md;
  8515. md = BL_CAST(BL_MOB, bl);
  8516. if (md->npc_event[0])
  8517. md->npc_event[0] = 0;
  8518. status_kill(bl);
  8519. return 0;
  8520. }
  8521. static int buildin_killmonsterall_sub(struct block_list *bl,va_list ap)
  8522. {
  8523. status_kill(bl);
  8524. return 0;
  8525. }
  8526. BUILDIN_FUNC(killmonsterall)
  8527. {
  8528. const char *mapname;
  8529. int16 m;
  8530. mapname=script_getstr(st,2);
  8531. if( (m = map_mapname2mapid(mapname))<0 )
  8532. return 0;
  8533. if( script_hasdata(st,3) ) {
  8534. if ( script_getnum(st,3) == 1 ) {
  8535. map_foreachinmap(buildin_killmonsterall_sub,m,BL_MOB);
  8536. return 0;
  8537. }
  8538. }
  8539. map_foreachinmap(buildin_killmonsterall_sub_strip,m,BL_MOB);
  8540. return SCRIPT_CMD_SUCCESS;
  8541. }
  8542. /*==========================================
  8543. * Creates a clone of a player.
  8544. * clone map, x, y, event, char_id, master_id, mode, flag, duration
  8545. *------------------------------------------*/
  8546. BUILDIN_FUNC(clone)
  8547. {
  8548. TBL_PC *sd, *msd=NULL;
  8549. uint32 char_id;
  8550. int master_id=0,x,y, mode = 0, flag = 0, m;
  8551. unsigned int duration = 0;
  8552. const char *mapname,*event;
  8553. mapname=script_getstr(st,2);
  8554. x=script_getnum(st,3);
  8555. y=script_getnum(st,4);
  8556. event=script_getstr(st,5);
  8557. char_id=script_getnum(st,6);
  8558. if( script_hasdata(st,7) )
  8559. master_id=script_getnum(st,7);
  8560. if( script_hasdata(st,8) )
  8561. mode=script_getnum(st,8);
  8562. if( script_hasdata(st,9) )
  8563. flag=script_getnum(st,9);
  8564. if( script_hasdata(st,10) )
  8565. duration=script_getnum(st,10);
  8566. check_event(st, event);
  8567. m = map_mapname2mapid(mapname);
  8568. if (m < 0) return 0;
  8569. sd = map_charid2sd(char_id);
  8570. if (master_id) {
  8571. msd = map_charid2sd(master_id);
  8572. if (msd)
  8573. master_id = msd->bl.id;
  8574. else
  8575. master_id = 0;
  8576. }
  8577. if (sd) //Return ID of newly crafted clone.
  8578. script_pushint(st,mob_clone_spawn(sd, m, x, y, event, master_id, mode, flag, 1000*duration));
  8579. else //Failed to create clone.
  8580. script_pushint(st,0);
  8581. return SCRIPT_CMD_SUCCESS;
  8582. }
  8583. /*==========================================
  8584. *------------------------------------------*/
  8585. BUILDIN_FUNC(doevent)
  8586. {
  8587. const char* event = script_getstr(st,2);
  8588. struct map_session_data* sd;
  8589. if( ( sd = script_rid2sd(st) ) == NULL )
  8590. {
  8591. return 0;
  8592. }
  8593. check_event(st, event);
  8594. npc_event(sd, event, 0);
  8595. return SCRIPT_CMD_SUCCESS;
  8596. }
  8597. /*==========================================
  8598. *------------------------------------------*/
  8599. BUILDIN_FUNC(donpcevent)
  8600. {
  8601. const char* event = script_getstr(st,2);
  8602. check_event(st, event);
  8603. if( !npc_event_do(event) ) {
  8604. struct npc_data * nd = map_id2nd(st->oid);
  8605. ShowDebug("NPCEvent '%s' not found! (source: %s)\n",event,nd?nd->name:"Unknown");
  8606. script_pushint(st, 0);
  8607. } else
  8608. script_pushint(st, 1);
  8609. return SCRIPT_CMD_SUCCESS;
  8610. }
  8611. /// for Aegis compatibility
  8612. /// basically a specialized 'donpcevent', with the event specified as two arguments instead of one
  8613. BUILDIN_FUNC(cmdothernpc) // Added by RoVeRT
  8614. {
  8615. const char* npc = script_getstr(st,2);
  8616. const char* command = script_getstr(st,3);
  8617. char event[EVENT_NAME_LENGTH];
  8618. snprintf(event, sizeof(event), "%s::OnCommand%s", npc, command);
  8619. check_event(st, event);
  8620. npc_event_do(event);
  8621. return SCRIPT_CMD_SUCCESS;
  8622. }
  8623. /*==========================================
  8624. *------------------------------------------*/
  8625. BUILDIN_FUNC(addtimer)
  8626. {
  8627. int tick = script_getnum(st,2);
  8628. const char* event = script_getstr(st, 3);
  8629. TBL_PC* sd;
  8630. check_event(st, event);
  8631. sd = script_rid2sd(st);
  8632. if( sd == NULL )
  8633. return 0;
  8634. if (!pc_addeventtimer(sd,tick,event)) {
  8635. ShowWarning("buildin_addtimer: Event timer is full, can't add new event timer. (cid:%d timer:%s)\n",sd->status.char_id,event);
  8636. return SCRIPT_CMD_FAILURE;
  8637. }
  8638. return SCRIPT_CMD_SUCCESS;
  8639. }
  8640. /*==========================================
  8641. *------------------------------------------*/
  8642. BUILDIN_FUNC(deltimer)
  8643. {
  8644. const char *event;
  8645. TBL_PC* sd;
  8646. event=script_getstr(st, 2);
  8647. sd = script_rid2sd(st);
  8648. if( sd == NULL )
  8649. return 0;
  8650. check_event(st, event);
  8651. pc_deleventtimer(sd,event);
  8652. return SCRIPT_CMD_SUCCESS;
  8653. }
  8654. /*==========================================
  8655. *------------------------------------------*/
  8656. BUILDIN_FUNC(addtimercount)
  8657. {
  8658. const char *event;
  8659. int tick;
  8660. TBL_PC* sd;
  8661. tick=script_getnum(st,2);
  8662. event=script_getstr(st,3);
  8663. sd = script_rid2sd(st);
  8664. if( sd == NULL )
  8665. return 0;
  8666. check_event(st, event);
  8667. pc_addeventtimercount(sd,event,tick);
  8668. return SCRIPT_CMD_SUCCESS;
  8669. }
  8670. /*==========================================
  8671. *------------------------------------------*/
  8672. BUILDIN_FUNC(initnpctimer)
  8673. {
  8674. struct npc_data *nd;
  8675. int flag = 0;
  8676. if( script_hasdata(st,3) )
  8677. { //Two arguments: NPC name and attach flag.
  8678. nd = npc_name2id(script_getstr(st, 2));
  8679. flag = script_getnum(st,3);
  8680. }
  8681. else if( script_hasdata(st,2) )
  8682. { //Check if argument is numeric (flag) or string (npc name)
  8683. struct script_data *data;
  8684. data = script_getdata(st,2);
  8685. get_val(st,data);
  8686. if( data_isstring(data) ) //NPC name
  8687. nd = npc_name2id(conv_str(st, data));
  8688. else if( data_isint(data) ) //Flag
  8689. {
  8690. nd = (struct npc_data *)map_id2bl(st->oid);
  8691. flag = conv_num(st,data);
  8692. }
  8693. else
  8694. {
  8695. ShowError("initnpctimer: invalid argument type #1 (needs be int or string)).\n");
  8696. return 1;
  8697. }
  8698. }
  8699. else
  8700. nd = (struct npc_data *)map_id2bl(st->oid);
  8701. if( !nd )
  8702. return 0;
  8703. if( flag ) //Attach
  8704. {
  8705. TBL_PC* sd = script_rid2sd(st);
  8706. if( sd == NULL )
  8707. return 0;
  8708. nd->u.scr.rid = sd->bl.id;
  8709. }
  8710. nd->u.scr.timertick = 0;
  8711. npc_settimerevent_tick(nd,0);
  8712. npc_timerevent_start(nd, st->rid);
  8713. return SCRIPT_CMD_SUCCESS;
  8714. }
  8715. /*==========================================
  8716. *------------------------------------------*/
  8717. BUILDIN_FUNC(startnpctimer)
  8718. {
  8719. struct npc_data *nd;
  8720. int flag = 0;
  8721. if( script_hasdata(st,3) )
  8722. { //Two arguments: NPC name and attach flag.
  8723. nd = npc_name2id(script_getstr(st, 2));
  8724. flag = script_getnum(st,3);
  8725. }
  8726. else if( script_hasdata(st,2) )
  8727. { //Check if argument is numeric (flag) or string (npc name)
  8728. struct script_data *data;
  8729. data = script_getdata(st,2);
  8730. get_val(st,data);
  8731. if( data_isstring(data) ) //NPC name
  8732. nd = npc_name2id(conv_str(st, data));
  8733. else if( data_isint(data) ) //Flag
  8734. {
  8735. nd = (struct npc_data *)map_id2bl(st->oid);
  8736. flag = conv_num(st,data);
  8737. }
  8738. else
  8739. {
  8740. ShowError("initnpctimer: invalid argument type #1 (needs be int or string)).\n");
  8741. return 1;
  8742. }
  8743. }
  8744. else
  8745. nd=(struct npc_data *)map_id2bl(st->oid);
  8746. if( !nd )
  8747. return 0;
  8748. if( flag ) //Attach
  8749. {
  8750. TBL_PC* sd = script_rid2sd(st);
  8751. if( sd == NULL )
  8752. return 0;
  8753. nd->u.scr.rid = sd->bl.id;
  8754. }
  8755. npc_timerevent_start(nd, st->rid);
  8756. return SCRIPT_CMD_SUCCESS;
  8757. }
  8758. /*==========================================
  8759. *------------------------------------------*/
  8760. BUILDIN_FUNC(stopnpctimer)
  8761. {
  8762. struct npc_data *nd;
  8763. int flag = 0;
  8764. if( script_hasdata(st,3) )
  8765. { //Two arguments: NPC name and attach flag.
  8766. nd = npc_name2id(script_getstr(st, 2));
  8767. flag = script_getnum(st,3);
  8768. }
  8769. else if( script_hasdata(st,2) )
  8770. { //Check if argument is numeric (flag) or string (npc name)
  8771. struct script_data *data;
  8772. data = script_getdata(st,2);
  8773. get_val(st,data);
  8774. if( data_isstring(data) ) //NPC name
  8775. nd = npc_name2id(conv_str(st, data));
  8776. else if( data_isint(data) ) //Flag
  8777. {
  8778. nd = (struct npc_data *)map_id2bl(st->oid);
  8779. flag = conv_num(st,data);
  8780. }
  8781. else
  8782. {
  8783. ShowError("initnpctimer: invalid argument type #1 (needs be int or string)).\n");
  8784. return 1;
  8785. }
  8786. }
  8787. else
  8788. nd=(struct npc_data *)map_id2bl(st->oid);
  8789. if( !nd )
  8790. return 0;
  8791. if( flag ) //Detach
  8792. nd->u.scr.rid = 0;
  8793. npc_timerevent_stop(nd);
  8794. return SCRIPT_CMD_SUCCESS;
  8795. }
  8796. /*==========================================
  8797. *------------------------------------------*/
  8798. BUILDIN_FUNC(getnpctimer)
  8799. {
  8800. struct npc_data *nd;
  8801. TBL_PC *sd;
  8802. int type = script_getnum(st,2);
  8803. int val = 0;
  8804. if( script_hasdata(st,3) )
  8805. nd = npc_name2id(script_getstr(st,3));
  8806. else
  8807. nd = (struct npc_data *)map_id2bl(st->oid);
  8808. if( !nd || nd->bl.type != BL_NPC )
  8809. {
  8810. script_pushint(st,0);
  8811. ShowError("getnpctimer: Invalid NPC.\n");
  8812. return 1;
  8813. }
  8814. switch( type )
  8815. {
  8816. case 0: val = npc_gettimerevent_tick(nd); break;
  8817. case 1:
  8818. if( nd->u.scr.rid )
  8819. {
  8820. sd = map_id2sd(nd->u.scr.rid);
  8821. if( !sd )
  8822. {
  8823. ShowError("buildin_getnpctimer: Attached player not found!\n");
  8824. break;
  8825. }
  8826. val = (sd->npc_timer_id != INVALID_TIMER);
  8827. }
  8828. else
  8829. val = (nd->u.scr.timerid != INVALID_TIMER);
  8830. break;
  8831. case 2: val = nd->u.scr.timeramount; break;
  8832. }
  8833. script_pushint(st,val);
  8834. return SCRIPT_CMD_SUCCESS;
  8835. }
  8836. /*==========================================
  8837. *------------------------------------------*/
  8838. BUILDIN_FUNC(setnpctimer)
  8839. {
  8840. int tick;
  8841. struct npc_data *nd;
  8842. tick = script_getnum(st,2);
  8843. if( script_hasdata(st,3) )
  8844. nd = npc_name2id(script_getstr(st,3));
  8845. else
  8846. nd = (struct npc_data *)map_id2bl(st->oid);
  8847. if( !nd || nd->bl.type != BL_NPC )
  8848. {
  8849. script_pushint(st,1);
  8850. ShowError("setnpctimer: Invalid NPC.\n");
  8851. return 1;
  8852. }
  8853. npc_settimerevent_tick(nd,tick);
  8854. script_pushint(st,0);
  8855. return SCRIPT_CMD_SUCCESS;
  8856. }
  8857. /*==========================================
  8858. * attaches the player rid to the timer [Celest]
  8859. *------------------------------------------*/
  8860. BUILDIN_FUNC(attachnpctimer)
  8861. {
  8862. TBL_PC *sd;
  8863. struct npc_data *nd = (struct npc_data *)map_id2bl(st->oid);
  8864. if( !nd || nd->bl.type != BL_NPC )
  8865. {
  8866. script_pushint(st,1);
  8867. ShowError("setnpctimer: Invalid NPC.\n");
  8868. return 1;
  8869. }
  8870. if( script_hasdata(st,2) )
  8871. sd = map_nick2sd(script_getstr(st,2));
  8872. else
  8873. sd = script_rid2sd(st);
  8874. if( !sd )
  8875. {
  8876. script_pushint(st,1);
  8877. ShowWarning("attachnpctimer: Invalid player.\n");
  8878. return 1;
  8879. }
  8880. nd->u.scr.rid = sd->bl.id;
  8881. script_pushint(st,0);
  8882. return SCRIPT_CMD_SUCCESS;
  8883. }
  8884. /*==========================================
  8885. * detaches a player rid from the timer [Celest]
  8886. *------------------------------------------*/
  8887. BUILDIN_FUNC(detachnpctimer)
  8888. {
  8889. struct npc_data *nd;
  8890. if( script_hasdata(st,2) )
  8891. nd = npc_name2id(script_getstr(st,2));
  8892. else
  8893. nd = (struct npc_data *)map_id2bl(st->oid);
  8894. if( !nd || nd->bl.type != BL_NPC )
  8895. {
  8896. script_pushint(st,1);
  8897. ShowError("detachnpctimer: Invalid NPC.\n");
  8898. return 1;
  8899. }
  8900. nd->u.scr.rid = 0;
  8901. script_pushint(st,0);
  8902. return SCRIPT_CMD_SUCCESS;
  8903. }
  8904. /*==========================================
  8905. * To avoid "player not attached" script errors, this function is provided,
  8906. * it checks if there is a player attached to the current script. [Skotlex]
  8907. * If no, returns 0, if yes, returns the account_id of the attached player.
  8908. *------------------------------------------*/
  8909. BUILDIN_FUNC(playerattached)
  8910. {
  8911. if(st->rid == 0 || map_id2sd(st->rid) == NULL)
  8912. script_pushint(st,0);
  8913. else
  8914. script_pushint(st,st->rid);
  8915. return SCRIPT_CMD_SUCCESS;
  8916. }
  8917. /*==========================================
  8918. *------------------------------------------*/
  8919. BUILDIN_FUNC(announce)
  8920. {
  8921. const char *mes = script_getstr(st,2);
  8922. int flag = script_getnum(st,3);
  8923. const char *fontColor = script_hasdata(st,4) ? script_getstr(st,4) : NULL;
  8924. int fontType = script_hasdata(st,5) ? script_getnum(st,5) : 0x190; // default fontType (FW_NORMAL)
  8925. int fontSize = script_hasdata(st,6) ? script_getnum(st,6) : 12; // default fontSize
  8926. int fontAlign = script_hasdata(st,7) ? script_getnum(st,7) : 0; // default fontAlign
  8927. int fontY = script_hasdata(st,8) ? script_getnum(st,8) : 0; // default fontY
  8928. if (flag&(BC_TARGET_MASK|BC_SOURCE_MASK)) // Broadcast source or broadcast region defined
  8929. {
  8930. send_target target;
  8931. 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
  8932. if (bl == NULL)
  8933. return 0;
  8934. switch (flag&BC_TARGET_MASK) {
  8935. case BC_MAP: target = ALL_SAMEMAP; break;
  8936. case BC_AREA: target = AREA; break;
  8937. case BC_SELF: target = SELF; break;
  8938. default: target = ALL_CLIENT; break; // BC_ALL
  8939. }
  8940. if (fontColor)
  8941. clif_broadcast2(bl, mes, (int)strlen(mes)+1, strtol(fontColor, (char **)NULL, 0), fontType, fontSize, fontAlign, fontY, target);
  8942. else
  8943. clif_broadcast(bl, mes, (int)strlen(mes)+1, flag&BC_COLOR_MASK, target);
  8944. }
  8945. else
  8946. {
  8947. if (fontColor)
  8948. intif_broadcast2(mes, (int)strlen(mes)+1, strtol(fontColor, (char **)NULL, 0), fontType, fontSize, fontAlign, fontY);
  8949. else
  8950. intif_broadcast(mes, (int)strlen(mes)+1, flag&BC_COLOR_MASK);
  8951. }
  8952. return SCRIPT_CMD_SUCCESS;
  8953. }
  8954. /*==========================================
  8955. *------------------------------------------*/
  8956. static int buildin_announce_sub(struct block_list *bl, va_list ap)
  8957. {
  8958. char *mes = va_arg(ap, char *);
  8959. int len = va_arg(ap, int);
  8960. int type = va_arg(ap, int);
  8961. char *fontColor = va_arg(ap, char *);
  8962. short fontType = (short)va_arg(ap, int);
  8963. short fontSize = (short)va_arg(ap, int);
  8964. short fontAlign = (short)va_arg(ap, int);
  8965. short fontY = (short)va_arg(ap, int);
  8966. if (fontColor)
  8967. clif_broadcast2(bl, mes, len, strtol(fontColor, (char **)NULL, 0), fontType, fontSize, fontAlign, fontY, SELF);
  8968. else
  8969. clif_broadcast(bl, mes, len, type, SELF);
  8970. return 0;
  8971. }
  8972. BUILDIN_FUNC(mapannounce)
  8973. {
  8974. const char *mapname = script_getstr(st,2);
  8975. const char *mes = script_getstr(st,3);
  8976. int flag = script_getnum(st,4);
  8977. const char *fontColor = script_hasdata(st,5) ? script_getstr(st,5) : NULL;
  8978. int fontType = script_hasdata(st,6) ? script_getnum(st,6) : 0x190; // default fontType (FW_NORMAL)
  8979. int fontSize = script_hasdata(st,7) ? script_getnum(st,7) : 12; // default fontSize
  8980. int fontAlign = script_hasdata(st,8) ? script_getnum(st,8) : 0; // default fontAlign
  8981. int fontY = script_hasdata(st,9) ? script_getnum(st,9) : 0; // default fontY
  8982. int16 m;
  8983. if ((m = map_mapname2mapid(mapname)) < 0)
  8984. return 0;
  8985. map_foreachinmap(buildin_announce_sub, m, BL_PC,
  8986. mes, strlen(mes)+1, flag&BC_COLOR_MASK, fontColor, fontType, fontSize, fontAlign, fontY);
  8987. return SCRIPT_CMD_SUCCESS;
  8988. }
  8989. /*==========================================
  8990. *------------------------------------------*/
  8991. BUILDIN_FUNC(areaannounce)
  8992. {
  8993. const char *mapname = script_getstr(st,2);
  8994. int x0 = script_getnum(st,3);
  8995. int y0 = script_getnum(st,4);
  8996. int x1 = script_getnum(st,5);
  8997. int y1 = script_getnum(st,6);
  8998. const char *mes = script_getstr(st,7);
  8999. int flag = script_getnum(st,8);
  9000. const char *fontColor = script_hasdata(st,9) ? script_getstr(st,9) : NULL;
  9001. int fontType = script_hasdata(st,10) ? script_getnum(st,10) : 0x190; // default fontType (FW_NORMAL)
  9002. int fontSize = script_hasdata(st,11) ? script_getnum(st,11) : 12; // default fontSize
  9003. int fontAlign = script_hasdata(st,12) ? script_getnum(st,12) : 0; // default fontAlign
  9004. int fontY = script_hasdata(st,13) ? script_getnum(st,13) : 0; // default fontY
  9005. int16 m;
  9006. if ((m = map_mapname2mapid(mapname)) < 0)
  9007. return 0;
  9008. map_foreachinarea(buildin_announce_sub, m, x0, y0, x1, y1, BL_PC,
  9009. mes, strlen(mes)+1, flag&BC_COLOR_MASK, fontColor, fontType, fontSize, fontAlign, fontY);
  9010. return SCRIPT_CMD_SUCCESS;
  9011. }
  9012. /*==========================================
  9013. *------------------------------------------*/
  9014. BUILDIN_FUNC(getusers)
  9015. {
  9016. int flag, val = 0;
  9017. struct map_session_data* sd;
  9018. struct block_list* bl = NULL;
  9019. flag = script_getnum(st,2);
  9020. switch(flag&0x07)
  9021. {
  9022. case 0:
  9023. if(flag&0x8)
  9024. {// npc
  9025. bl = map_id2bl(st->oid);
  9026. }
  9027. else if((sd = script_rid2sd(st))!=NULL)
  9028. {// pc
  9029. bl = &sd->bl;
  9030. }
  9031. if(bl)
  9032. {
  9033. val = map[bl->m].users;
  9034. }
  9035. break;
  9036. case 1:
  9037. val = map_getusers();
  9038. break;
  9039. default:
  9040. ShowWarning("buildin_getusers: Unknown type %d.\n", flag);
  9041. script_pushint(st,0);
  9042. return 1;
  9043. }
  9044. script_pushint(st,val);
  9045. return SCRIPT_CMD_SUCCESS;
  9046. }
  9047. /*==========================================
  9048. * getmapguildusers("mapname",guild ID) Returns the number guild members present on a map [Reddozen]
  9049. *------------------------------------------*/
  9050. BUILDIN_FUNC(getmapguildusers)
  9051. {
  9052. const char *str;
  9053. int16 m;
  9054. int gid;
  9055. int c=0;
  9056. struct guild *g = NULL;
  9057. str=script_getstr(st,2);
  9058. gid=script_getnum(st,3);
  9059. if ((m = map_mapname2mapid(str)) < 0) { // map id on this server (m == -1 if not in actual map-server)
  9060. script_pushint(st,-1);
  9061. return 0;
  9062. }
  9063. g = guild_search(gid);
  9064. if (g){
  9065. unsigned short i;
  9066. for(i = 0; i < g->max_member; i++)
  9067. {
  9068. if (g->member[i].sd && g->member[i].sd->bl.m == m)
  9069. c++;
  9070. }
  9071. }
  9072. script_pushint(st,c);
  9073. return SCRIPT_CMD_SUCCESS;
  9074. }
  9075. /*==========================================
  9076. *------------------------------------------*/
  9077. BUILDIN_FUNC(getmapusers)
  9078. {
  9079. const char *str;
  9080. int16 m;
  9081. str=script_getstr(st,2);
  9082. if( (m=map_mapname2mapid(str))< 0){
  9083. script_pushint(st,-1);
  9084. return 0;
  9085. }
  9086. script_pushint(st,map[m].users);
  9087. return SCRIPT_CMD_SUCCESS;
  9088. }
  9089. /*==========================================
  9090. *------------------------------------------*/
  9091. static int buildin_getareausers_sub(struct block_list *bl,va_list ap)
  9092. {
  9093. int *users=va_arg(ap,int *);
  9094. (*users)++;
  9095. return 0;
  9096. }
  9097. BUILDIN_FUNC(getareausers)
  9098. {
  9099. const char *str;
  9100. int16 m,x0,y0,x1,y1,users=0; //doubt we can have more then 32k users on
  9101. str=script_getstr(st,2);
  9102. x0=script_getnum(st,3);
  9103. y0=script_getnum(st,4);
  9104. x1=script_getnum(st,5);
  9105. y1=script_getnum(st,6);
  9106. if( (m=map_mapname2mapid(str))< 0){
  9107. script_pushint(st,-1);
  9108. return 0;
  9109. }
  9110. map_foreachinarea(buildin_getareausers_sub,
  9111. m,x0,y0,x1,y1,BL_PC,&users);
  9112. script_pushint(st,users);
  9113. return SCRIPT_CMD_SUCCESS;
  9114. }
  9115. /*==========================================
  9116. *------------------------------------------*/
  9117. static int buildin_getareadropitem_sub(struct block_list *bl,va_list ap)
  9118. {
  9119. int nameid=va_arg(ap,int);
  9120. int *amount=va_arg(ap,int *);
  9121. struct flooritem_data *drop=(struct flooritem_data *)bl;
  9122. if(drop->item.nameid==nameid)
  9123. (*amount)+=drop->item.amount;
  9124. return 0;
  9125. }
  9126. BUILDIN_FUNC(getareadropitem)
  9127. {
  9128. const char *str;
  9129. int16 m,x0,y0,x1,y1;
  9130. int nameid,amount=0;
  9131. struct script_data *data;
  9132. str=script_getstr(st,2);
  9133. x0=script_getnum(st,3);
  9134. y0=script_getnum(st,4);
  9135. x1=script_getnum(st,5);
  9136. y1=script_getnum(st,6);
  9137. data=script_getdata(st,7);
  9138. get_val(st,data);
  9139. if( data_isstring(data) ){
  9140. const char *name=conv_str(st,data);
  9141. struct item_data *item_data = itemdb_searchname(name);
  9142. nameid=UNKNOWN_ITEM_ID;
  9143. if( item_data )
  9144. nameid=item_data->nameid;
  9145. }else
  9146. nameid=conv_num(st,data);
  9147. if( (m=map_mapname2mapid(str))< 0){
  9148. script_pushint(st,-1);
  9149. return 0;
  9150. }
  9151. map_foreachinarea(buildin_getareadropitem_sub,
  9152. m,x0,y0,x1,y1,BL_ITEM,nameid,&amount);
  9153. script_pushint(st,amount);
  9154. return SCRIPT_CMD_SUCCESS;
  9155. }
  9156. /*==========================================
  9157. *------------------------------------------*/
  9158. BUILDIN_FUNC(enablenpc)
  9159. {
  9160. const char *str;
  9161. str=script_getstr(st,2);
  9162. npc_enable(str,1);
  9163. return SCRIPT_CMD_SUCCESS;
  9164. }
  9165. /*==========================================
  9166. *------------------------------------------*/
  9167. BUILDIN_FUNC(disablenpc)
  9168. {
  9169. const char *str;
  9170. str=script_getstr(st,2);
  9171. npc_enable(str,0);
  9172. return SCRIPT_CMD_SUCCESS;
  9173. }
  9174. /*==========================================
  9175. *------------------------------------------*/
  9176. BUILDIN_FUNC(hideoffnpc)
  9177. {
  9178. const char *str;
  9179. str=script_getstr(st,2);
  9180. npc_enable(str,2);
  9181. return SCRIPT_CMD_SUCCESS;
  9182. }
  9183. /*==========================================
  9184. *------------------------------------------*/
  9185. BUILDIN_FUNC(hideonnpc)
  9186. {
  9187. const char *str;
  9188. str=script_getstr(st,2);
  9189. npc_enable(str,4);
  9190. return SCRIPT_CMD_SUCCESS;
  9191. }
  9192. /* Starts a status effect on the target unit or on the attached player.
  9193. *
  9194. * sc_start <effect_id>,<duration>,<val1>{,<rate>,<flag>,{<unit_id>}};
  9195. * sc_start2 <effect_id>,<duration>,<val1>,<val2>{,<rate,<flag>,{<unit_id>}};
  9196. * sc_start4 <effect_id>,<duration>,<val1>,<val2>,<val3>,<val4>{,<rate,<flag>,{<unit_id>}};
  9197. * <flag>: enum e_status_change_start_flags
  9198. */
  9199. BUILDIN_FUNC(sc_start)
  9200. {
  9201. TBL_NPC * nd = map_id2nd(st->oid);
  9202. struct block_list* bl;
  9203. enum sc_type type;
  9204. int tick, val1, val2, val3, val4=0, rate, flag;
  9205. char start_type;
  9206. const char* command = script_getfuncname(st);
  9207. if(strstr(command, "4"))
  9208. start_type = 4;
  9209. else if(strstr(command, "2"))
  9210. start_type = 2;
  9211. else
  9212. start_type = 1;
  9213. type = (sc_type)script_getnum(st,2);
  9214. tick = script_getnum(st,3);
  9215. val1 = script_getnum(st,4);
  9216. //If from NPC we make default flag 1 to be unavoidable
  9217. if(nd && nd->bl.id == fake_nd->bl.id)
  9218. flag = script_hasdata(st,5+start_type)?script_getnum(st,5+start_type):SCSTART_NOTICKDEF;
  9219. else
  9220. flag = script_hasdata(st,5+start_type)?script_getnum(st,5+start_type):SCSTART_NOAVOID;
  9221. rate = script_hasdata(st,4+start_type)?min(script_getnum(st,4+start_type),10000):10000;
  9222. if(script_hasdata(st,(6+start_type)))
  9223. bl = map_id2bl(script_getnum(st,(6+start_type)));
  9224. else
  9225. bl = map_id2bl(st->rid);
  9226. if(tick == 0 && val1 > 0 && type > SC_NONE && type < SC_MAX && status_sc2skill(type) != 0)
  9227. {// When there isn't a duration specified, try to get it from the skill_db
  9228. tick = skill_get_time(status_sc2skill(type), val1);
  9229. }
  9230. if(potion_flag == 1 && potion_target) { //skill.c set the flags before running the script, this is a potion-pitched effect.
  9231. bl = map_id2bl(potion_target);
  9232. tick /= 2;// Thrown potions only last half.
  9233. val4 = 1;// Mark that this was a thrown sc_effect
  9234. }
  9235. if(!bl)
  9236. return 0;
  9237. switch(start_type) {
  9238. case 1:
  9239. status_change_start(bl, bl, type, rate, val1, 0, 0, val4, tick, flag);
  9240. break;
  9241. case 2:
  9242. val2 = script_getnum(st,5);
  9243. status_change_start(bl, bl, type, rate, val1, val2, 0, val4, tick, flag);
  9244. break;
  9245. case 4:
  9246. val2 = script_getnum(st,5);
  9247. val3 = script_getnum(st,6);
  9248. val4 = script_getnum(st,7);
  9249. status_change_start(bl, bl, type, rate, val1, val2, val3, val4, tick, flag);
  9250. break;
  9251. }
  9252. return SCRIPT_CMD_SUCCESS;
  9253. }
  9254. /// Ends one or all status effects on the target unit or on the attached player.
  9255. ///
  9256. /// sc_end <effect_id>{,<unit_id>};
  9257. BUILDIN_FUNC(sc_end)
  9258. {
  9259. struct block_list* bl;
  9260. int type;
  9261. type = script_getnum(st, 2);
  9262. if (script_hasdata(st, 3))
  9263. bl = map_id2bl(script_getnum(st, 3));
  9264. else
  9265. bl = map_id2bl(st->rid);
  9266. if (potion_flag == 1 && potion_target) //##TODO how does this work [FlavioJS]
  9267. bl = map_id2bl(potion_target);
  9268. if (!bl)
  9269. return 0;
  9270. if (type >= 0 && type < SC_MAX) {
  9271. struct status_change *sc = status_get_sc(bl);
  9272. struct status_change_entry *sce = sc ? sc->data[type] : NULL;
  9273. if (!sce)
  9274. return 0;
  9275. switch (type) {
  9276. case SC_WEIGHT50:
  9277. case SC_WEIGHT90:
  9278. case SC_NOCHAT:
  9279. case SC_PUSH_CART:
  9280. case SC_ALL_RIDING:
  9281. case SC_STYLE_CHANGE:
  9282. case SC_MONSTER_TRANSFORM:
  9283. case SC_MTF_ASPD:
  9284. case SC_MTF_RANGEATK:
  9285. case SC_MTF_MATK:
  9286. case SC_MTF_MLEATKED:
  9287. case SC_MTF_CRIDAMAGE:
  9288. case SC_MTF_ASPD2:
  9289. case SC_MTF_RANGEATK2:
  9290. case SC_MTF_MATK2:
  9291. case SC_MTF_MHP:
  9292. case SC_MTF_MSP:
  9293. case SC_MTF_PUMPKIN:
  9294. case SC_MTF_HITFLEE:
  9295. return 0;
  9296. default:
  9297. break;
  9298. }
  9299. //This should help status_change_end force disabling the SC in case it has no limit.
  9300. sce->val1 = sce->val2 = sce->val3 = sce->val4 = 0;
  9301. status_change_end(bl, (sc_type)type, INVALID_TIMER);
  9302. } else
  9303. status_change_clear(bl, 3); // remove all effects
  9304. return SCRIPT_CMD_SUCCESS;
  9305. }
  9306. /*==========================================
  9307. * @FIXME atm will return reduced tick, 0 immune, 1 no tick
  9308. *------------------------------------------*/
  9309. BUILDIN_FUNC(getscrate)
  9310. {
  9311. struct block_list *bl;
  9312. int type,rate;
  9313. type=script_getnum(st,2);
  9314. rate=script_getnum(st,3);
  9315. if( script_hasdata(st,4) ) //get for the bl assigned
  9316. bl = map_id2bl(script_getnum(st,4));
  9317. else
  9318. bl = map_id2bl(st->rid);
  9319. if (bl)
  9320. rate = status_get_sc_def(NULL,bl, (sc_type)type, 10000, 10000, SCSTART_NONE);
  9321. script_pushint(st,rate);
  9322. return SCRIPT_CMD_SUCCESS;
  9323. }
  9324. /**
  9325. * getstatus(<effect type>{,<type>{,<char_id>}});
  9326. **/
  9327. BUILDIN_FUNC(getstatus)
  9328. {
  9329. int id, type;
  9330. struct map_session_data* sd;
  9331. if (!script_charid2sd(4,sd))
  9332. return SCRIPT_CMD_FAILURE;
  9333. id = script_getnum(st, 2);
  9334. type = script_hasdata(st, 3) ? script_getnum(st, 3) : 0;
  9335. if( id <= SC_NONE || id >= SC_MAX )
  9336. {// invalid status type given
  9337. ShowWarning("script.c:getstatus: Invalid status type given (%d).\n", id);
  9338. return 0;
  9339. }
  9340. if( sd->sc.count == 0 || !sd->sc.data[id] )
  9341. {// no status is active
  9342. script_pushint(st, 0);
  9343. return 0;
  9344. }
  9345. switch( type )
  9346. {
  9347. case 1: script_pushint(st, sd->sc.data[id]->val1); break;
  9348. case 2: script_pushint(st, sd->sc.data[id]->val2); break;
  9349. case 3: script_pushint(st, sd->sc.data[id]->val3); break;
  9350. case 4: script_pushint(st, sd->sc.data[id]->val4); break;
  9351. case 5:
  9352. {
  9353. struct TimerData* timer = (struct TimerData*)get_timer(sd->sc.data[id]->timer);
  9354. if( timer )
  9355. {// return the amount of time remaining
  9356. script_pushint(st, timer->tick - gettick());
  9357. }
  9358. }
  9359. break;
  9360. default: script_pushint(st, 1); break;
  9361. }
  9362. return SCRIPT_CMD_SUCCESS;
  9363. }
  9364. /*==========================================
  9365. *
  9366. *------------------------------------------*/
  9367. BUILDIN_FUNC(debugmes)
  9368. {
  9369. const char *str;
  9370. str=script_getstr(st,2);
  9371. ShowDebug("script debug : %d %d : %s\n",st->rid,st->oid,str);
  9372. return SCRIPT_CMD_SUCCESS;
  9373. }
  9374. /*==========================================
  9375. *------------------------------------------*/
  9376. BUILDIN_FUNC(catchpet)
  9377. {
  9378. int pet_id;
  9379. TBL_PC *sd;
  9380. pet_id= script_getnum(st,2);
  9381. sd=script_rid2sd(st);
  9382. if( sd == NULL )
  9383. return 0;
  9384. pet_catch_process1(sd,pet_id);
  9385. return SCRIPT_CMD_SUCCESS;
  9386. }
  9387. /*==========================================
  9388. * [orn]
  9389. *------------------------------------------*/
  9390. BUILDIN_FUNC(homunculus_evolution)
  9391. {
  9392. TBL_PC *sd;
  9393. sd=script_rid2sd(st);
  9394. if( sd == NULL )
  9395. return 0;
  9396. if(hom_is_active(sd->hd))
  9397. {
  9398. if (sd->hd->homunculus.intimacy >= battle_config.homunculus_evo_intimacy_need)
  9399. hom_evolution(sd->hd);
  9400. else
  9401. clif_emotion(&sd->hd->bl, E_SWT);
  9402. }
  9403. return SCRIPT_CMD_SUCCESS;
  9404. }
  9405. /*==========================================
  9406. * Checks for vaporized morph state
  9407. * and deletes ITEMID_STRANGE_EMBRYO.
  9408. *------------------------------------------*/
  9409. BUILDIN_FUNC(homunculus_mutate)
  9410. {
  9411. int homun_id;
  9412. TBL_PC *sd;
  9413. sd = script_rid2sd(st);
  9414. if( sd == NULL || sd->hd == NULL )
  9415. return 0;
  9416. if(script_hasdata(st,2))
  9417. homun_id = script_getnum(st,2);
  9418. else
  9419. homun_id = 6048 + (rnd() % 4);
  9420. if( sd->hd->homunculus.vaporize == HOM_ST_MORPH ) {
  9421. int m_class = hom_class2mapid(sd->hd->homunculus.class_);
  9422. int m_id = hom_class2mapid(homun_id);
  9423. short i = pc_search_inventory(sd, ITEMID_STRANGE_EMBRYO);
  9424. if ( m_class != -1 && m_id != -1 && m_class&HOM_EVO && m_id&HOM_S && sd->hd->homunculus.level >= 99 && i >= 0 ) {
  9425. sd->hd->homunculus.vaporize = HOM_ST_REST; // Remove morph state.
  9426. hom_call(sd); // Respawn homunculus.
  9427. hom_mutate(sd->hd, homun_id);
  9428. pc_delitem(sd, i, 1, 0, 0, LOG_TYPE_SCRIPT);
  9429. script_pushint(st, 1);
  9430. return 0;
  9431. } else
  9432. clif_emotion(&sd->bl, E_SWT);
  9433. } else
  9434. clif_emotion(&sd->bl, E_SWT);
  9435. script_pushint(st, 0);
  9436. return SCRIPT_CMD_SUCCESS;
  9437. }
  9438. /*==========================================
  9439. * Puts homunculus into morph state
  9440. * and gives ITEMID_STRANGE_EMBRYO.
  9441. *------------------------------------------*/
  9442. BUILDIN_FUNC(morphembryo)
  9443. {
  9444. struct item item_tmp;
  9445. TBL_PC *sd;
  9446. sd = script_rid2sd(st);
  9447. if( sd == NULL || sd->hd == NULL )
  9448. return 0;
  9449. if( hom_is_active(sd->hd) ) {
  9450. int m_class = hom_class2mapid(sd->hd->homunculus.class_);
  9451. if ( m_class != -1 && m_class&HOM_EVO && sd->hd->homunculus.level >= 99 ) {
  9452. char i;
  9453. memset(&item_tmp, 0, sizeof(item_tmp));
  9454. item_tmp.nameid = ITEMID_STRANGE_EMBRYO;
  9455. item_tmp.identify = 1;
  9456. if( (i = pc_additem(sd, &item_tmp, 1, LOG_TYPE_SCRIPT)) ) {
  9457. clif_additem(sd, 0, 0, i);
  9458. clif_emotion(&sd->bl, E_SWT); // Fail to avoid item drop exploit.
  9459. } else {
  9460. hom_vaporize(sd, HOM_ST_MORPH);
  9461. script_pushint(st, 1);
  9462. return 0;
  9463. }
  9464. } else
  9465. clif_emotion(&sd->hd->bl, E_SWT);
  9466. } else
  9467. clif_emotion(&sd->bl, E_SWT);
  9468. script_pushint(st, 0);
  9469. return SCRIPT_CMD_SUCCESS;
  9470. }
  9471. // [Zephyrus]
  9472. BUILDIN_FUNC(homunculus_shuffle)
  9473. {
  9474. TBL_PC *sd;
  9475. sd=script_rid2sd(st);
  9476. if( sd == NULL )
  9477. return 0;
  9478. if(hom_is_active(sd->hd))
  9479. hom_shuffle(sd->hd);
  9480. return SCRIPT_CMD_SUCCESS;
  9481. }
  9482. /*==========================================
  9483. * Check for homunculus state.
  9484. * Return: -1 = No homunculus
  9485. * 0 = Homunculus is active
  9486. * 1 = Homunculus is vaporized (rest)
  9487. * 2 = Homunculus is in morph state
  9488. *------------------------------------------*/
  9489. BUILDIN_FUNC(checkhomcall)
  9490. {
  9491. TBL_PC *sd = script_rid2sd(st);
  9492. TBL_HOM *hd;
  9493. if( sd == NULL )
  9494. return 0;
  9495. hd = sd->hd;
  9496. if( !hd )
  9497. script_pushint(st, -1);
  9498. else
  9499. script_pushint(st, hd->homunculus.vaporize);
  9500. return SCRIPT_CMD_SUCCESS;
  9501. }
  9502. //These two functions bring the eA MAPID_* class functionality to scripts.
  9503. BUILDIN_FUNC(eaclass)
  9504. {
  9505. int class_;
  9506. if( script_hasdata(st,2) )
  9507. class_ = script_getnum(st,2);
  9508. else {
  9509. TBL_PC *sd;
  9510. sd=script_rid2sd(st);
  9511. if (!sd) {
  9512. script_pushint(st,-1);
  9513. return 0;
  9514. }
  9515. class_ = sd->status.class_;
  9516. }
  9517. script_pushint(st,pc_jobid2mapid(class_));
  9518. return SCRIPT_CMD_SUCCESS;
  9519. }
  9520. BUILDIN_FUNC(roclass)
  9521. {
  9522. int class_ =script_getnum(st,2);
  9523. int sex;
  9524. if( script_hasdata(st,3) )
  9525. sex = script_getnum(st,3);
  9526. else {
  9527. TBL_PC *sd;
  9528. if (st->rid && (sd=script_rid2sd(st)))
  9529. sex = sd->status.sex;
  9530. else
  9531. sex = 1; //Just use male when not found.
  9532. }
  9533. script_pushint(st,pc_mapid2jobid(class_, sex));
  9534. return SCRIPT_CMD_SUCCESS;
  9535. }
  9536. /*==========================================
  9537. * Tells client to open a hatching window, used for pet incubator
  9538. *------------------------------------------*/
  9539. BUILDIN_FUNC(birthpet)
  9540. {
  9541. TBL_PC *sd;
  9542. sd=script_rid2sd(st);
  9543. if( sd == NULL )
  9544. return 0;
  9545. if( sd->status.pet_id )
  9546. {// do not send egg list, when you already have a pet
  9547. return 0;
  9548. }
  9549. clif_sendegg(sd);
  9550. return SCRIPT_CMD_SUCCESS;
  9551. }
  9552. /**
  9553. * resetlvl <action type>{,<char_id>};
  9554. * @param action_type:
  9555. * 1 : make like after rebirth
  9556. * 2 : blvl,jlvl=1, skillpoint=0
  9557. * 3 : don't reset skill, blvl=1
  9558. * 4 : jlvl=0
  9559. * @author AppleGirl
  9560. **/
  9561. BUILDIN_FUNC(resetlvl)
  9562. {
  9563. TBL_PC *sd;
  9564. int type=script_getnum(st,2);
  9565. if (!script_charid2sd(3,sd))
  9566. return SCRIPT_CMD_FAILURE;
  9567. pc_resetlvl(sd,type);
  9568. return SCRIPT_CMD_SUCCESS;
  9569. }
  9570. /**
  9571. * Reset a player status point
  9572. * resetstatus({<char_id>});
  9573. **/
  9574. BUILDIN_FUNC(resetstatus)
  9575. {
  9576. TBL_PC *sd;
  9577. if (!script_charid2sd(2,sd))
  9578. return SCRIPT_CMD_FAILURE;
  9579. pc_resetstate(sd);
  9580. return SCRIPT_CMD_SUCCESS;
  9581. }
  9582. /**
  9583. * Reset player's skill
  9584. * resetskill({<char_id>});
  9585. **/
  9586. BUILDIN_FUNC(resetskill)
  9587. {
  9588. TBL_PC *sd;
  9589. if (!script_charid2sd(2,sd))
  9590. return SCRIPT_CMD_FAILURE;
  9591. pc_resetskill(sd,1);
  9592. return SCRIPT_CMD_SUCCESS;
  9593. }
  9594. /**
  9595. * Counts total amount of skill points.
  9596. * skillpointcount({<char_id>})
  9597. **/
  9598. BUILDIN_FUNC(skillpointcount)
  9599. {
  9600. TBL_PC *sd;
  9601. if (!script_charid2sd(2,sd))
  9602. return SCRIPT_CMD_FAILURE;
  9603. script_pushint(st,sd->status.skill_point + pc_resetskill(sd,2));
  9604. return SCRIPT_CMD_SUCCESS;
  9605. }
  9606. /*==========================================
  9607. *
  9608. *------------------------------------------*/
  9609. BUILDIN_FUNC(changebase)
  9610. {
  9611. TBL_PC *sd=NULL;
  9612. int vclass;
  9613. if( script_hasdata(st,3) )
  9614. sd=map_id2sd(script_getnum(st,3));
  9615. else
  9616. sd=script_rid2sd(st);
  9617. if(sd == NULL)
  9618. return 0;
  9619. vclass = script_getnum(st,2);
  9620. if(vclass == JOB_WEDDING)
  9621. {
  9622. if (!battle_config.wedding_modifydisplay || //Do not show the wedding sprites
  9623. sd->class_&JOBL_BABY //Baby classes screw up when showing wedding sprites. [Skotlex] They don't seem to anymore.
  9624. )
  9625. return 0;
  9626. }
  9627. if(!sd->disguise && vclass != sd->vd.class_) {
  9628. status_set_viewdata(&sd->bl, vclass);
  9629. //Updated client view. Base, Weapon and Cloth Colors.
  9630. clif_changelook(&sd->bl,LOOK_BASE,sd->vd.class_);
  9631. clif_changelook(&sd->bl,LOOK_WEAPON,sd->status.weapon);
  9632. if (sd->vd.cloth_color)
  9633. clif_changelook(&sd->bl,LOOK_CLOTHES_COLOR,sd->vd.cloth_color);
  9634. clif_skillinfoblock(sd);
  9635. }
  9636. return SCRIPT_CMD_SUCCESS;
  9637. }
  9638. /**
  9639. * Change account sex and unequip all item and request for a changesex to char-serv
  9640. * changesex({<char_id>});
  9641. */
  9642. BUILDIN_FUNC(changesex)
  9643. {
  9644. int i;
  9645. TBL_PC *sd = NULL;
  9646. if (!script_charid2sd(2,sd))
  9647. return SCRIPT_CMD_FAILURE;
  9648. pc_resetskill(sd,4);
  9649. // to avoid any problem with equipment and invalid sex, equipment is unequiped.
  9650. for(i = 0; i < EQI_MAX; i++) {
  9651. if (sd->equip_index[i] >= 0)
  9652. pc_unequipitem(sd, sd->equip_index[i], 3);
  9653. }
  9654. chrif_changesex(sd, true);
  9655. return SCRIPT_CMD_SUCCESS;
  9656. }
  9657. /**
  9658. * Change character's sex and unequip all item and request for a changesex to char-serv
  9659. * changecharsex({<char_id>});
  9660. */
  9661. BUILDIN_FUNC(changecharsex)
  9662. {
  9663. #if PACKETVER >= 20141016
  9664. int i;
  9665. TBL_PC *sd = NULL;
  9666. if (!script_charid2sd(2,sd))
  9667. return SCRIPT_CMD_FAILURE;
  9668. pc_resetskill(sd,4);
  9669. // to avoid any problem with equipment and invalid sex, equipment is unequiped.
  9670. for (i = 0; i < EQI_MAX; i++) {
  9671. if (sd->equip_index[i] >= 0)
  9672. pc_unequipitem(sd, sd->equip_index[i], 3);
  9673. }
  9674. chrif_changesex(sd, false);
  9675. return SCRIPT_CMD_SUCCESS;
  9676. #else
  9677. return SCRIPT_CMD_FAILURE;
  9678. #endif
  9679. }
  9680. /*==========================================
  9681. * Works like 'announce' but outputs in the common chat window
  9682. *------------------------------------------*/
  9683. BUILDIN_FUNC(globalmes)
  9684. {
  9685. struct block_list *bl = map_id2bl(st->oid);
  9686. struct npc_data *nd = (struct npc_data *)bl;
  9687. const char *name=NULL,*mes;
  9688. mes=script_getstr(st,2);
  9689. if(mes==NULL) return 0;
  9690. if(script_hasdata(st,3)){ // npc name to display
  9691. name=script_getstr(st,3);
  9692. } else {
  9693. name=nd->name; //use current npc name
  9694. }
  9695. npc_globalmessage(name,mes); // broadcast to all players connected
  9696. return SCRIPT_CMD_SUCCESS;
  9697. }
  9698. /////////////////////////////////////////////////////////////////////
  9699. // NPC waiting room (chat room)
  9700. //
  9701. /// Creates a waiting room (chat room) for this npc.
  9702. ///
  9703. /// waitingroom "<title>",<limit>{,"<event>"{,<trigger>{,<zeny>{,<minlvl>{,<maxlvl>}}}}};
  9704. BUILDIN_FUNC(waitingroom)
  9705. {
  9706. struct npc_data* nd;
  9707. const char* title = script_getstr(st, 2);
  9708. int limit = script_getnum(st, 3);
  9709. const char* ev = script_hasdata(st,4) ? script_getstr(st,4) : "";
  9710. int trigger = script_hasdata(st,5) ? script_getnum(st,5) : limit;
  9711. int zeny = script_hasdata(st,6) ? script_getnum(st,6) : 0;
  9712. int minLvl = script_hasdata(st,7) ? script_getnum(st,7) : 1;
  9713. int maxLvl = script_hasdata(st,8) ? script_getnum(st,8) : MAX_LEVEL;
  9714. nd = (struct npc_data *)map_id2bl(st->oid);
  9715. if( nd != NULL )
  9716. chat_createnpcchat(nd, title, limit, 1, trigger, ev, zeny, minLvl, maxLvl);
  9717. return SCRIPT_CMD_SUCCESS;
  9718. }
  9719. /// Removes the waiting room of the current or target npc.
  9720. ///
  9721. /// delwaitingroom "<npc_name>";
  9722. /// delwaitingroom;
  9723. BUILDIN_FUNC(delwaitingroom)
  9724. {
  9725. struct npc_data* nd;
  9726. if( script_hasdata(st,2) )
  9727. nd = npc_name2id(script_getstr(st, 2));
  9728. else
  9729. nd = (struct npc_data *)map_id2bl(st->oid);
  9730. if( nd != NULL )
  9731. chat_deletenpcchat(nd);
  9732. return SCRIPT_CMD_SUCCESS;
  9733. }
  9734. /// Kicks all the players from the waiting room of the current or target npc.
  9735. ///
  9736. /// kickwaitingroomall "<npc_name>";
  9737. /// kickwaitingroomall;
  9738. BUILDIN_FUNC(waitingroomkickall)
  9739. {
  9740. struct npc_data* nd;
  9741. struct chat_data* cd;
  9742. if( script_hasdata(st,2) )
  9743. nd = npc_name2id(script_getstr(st,2));
  9744. else
  9745. nd = (struct npc_data *)map_id2bl(st->oid);
  9746. if( nd != NULL && (cd=(struct chat_data *)map_id2bl(nd->chat_id)) != NULL )
  9747. chat_npckickall(cd);
  9748. return SCRIPT_CMD_SUCCESS;
  9749. }
  9750. /// Enables the waiting room event of the current or target npc.
  9751. ///
  9752. /// enablewaitingroomevent "<npc_name>";
  9753. /// enablewaitingroomevent;
  9754. BUILDIN_FUNC(enablewaitingroomevent)
  9755. {
  9756. struct npc_data* nd;
  9757. struct chat_data* cd;
  9758. if( script_hasdata(st,2) )
  9759. nd = npc_name2id(script_getstr(st, 2));
  9760. else
  9761. nd = (struct npc_data *)map_id2bl(st->oid);
  9762. if( nd != NULL && (cd=(struct chat_data *)map_id2bl(nd->chat_id)) != NULL )
  9763. chat_enableevent(cd);
  9764. return SCRIPT_CMD_SUCCESS;
  9765. }
  9766. /// Disables the waiting room event of the current or target npc.
  9767. ///
  9768. /// disablewaitingroomevent "<npc_name>";
  9769. /// disablewaitingroomevent;
  9770. BUILDIN_FUNC(disablewaitingroomevent)
  9771. {
  9772. struct npc_data *nd;
  9773. struct chat_data *cd;
  9774. if( script_hasdata(st,2) )
  9775. nd = npc_name2id(script_getstr(st, 2));
  9776. else
  9777. nd = (struct npc_data *)map_id2bl(st->oid);
  9778. if( nd != NULL && (cd=(struct chat_data *)map_id2bl(nd->chat_id)) != NULL )
  9779. chat_disableevent(cd);
  9780. return SCRIPT_CMD_SUCCESS;
  9781. }
  9782. /// Returns info on the waiting room of the current or target npc.
  9783. /// Returns -1 if the type unknown
  9784. /// <type>=0 : current number of users
  9785. /// <type>=1 : maximum number of users allowed
  9786. /// <type>=2 : the number of users that trigger the event
  9787. /// <type>=3 : if the trigger is disabled
  9788. /// <type>=4 : the title of the waiting room
  9789. /// <type>=5 : the password of the waiting room
  9790. /// <type>=16 : the name of the waiting room event
  9791. /// <type>=32 : if the waiting room is full
  9792. /// <type>=33 : if there are enough users to trigger the event
  9793. ///
  9794. /// getwaitingroomstate(<type>,"<npc_name>") -> <info>
  9795. /// getwaitingroomstate(<type>) -> <info>
  9796. BUILDIN_FUNC(getwaitingroomstate)
  9797. {
  9798. struct npc_data *nd;
  9799. struct chat_data *cd;
  9800. int type;
  9801. type = script_getnum(st,2);
  9802. if( script_hasdata(st,3) )
  9803. nd = npc_name2id(script_getstr(st, 3));
  9804. else
  9805. nd = (struct npc_data *)map_id2bl(st->oid);
  9806. if( nd == NULL || (cd=(struct chat_data *)map_id2bl(nd->chat_id)) == NULL )
  9807. {
  9808. script_pushint(st, -1);
  9809. return 0;
  9810. }
  9811. switch(type)
  9812. {
  9813. case 0: script_pushint(st, cd->users); break;
  9814. case 1: script_pushint(st, cd->limit); break;
  9815. case 2: script_pushint(st, cd->trigger&0x7f); break;
  9816. case 3: script_pushint(st, ((cd->trigger&0x80)!=0)); break;
  9817. case 4: script_pushstrcopy(st, cd->title); break;
  9818. case 5: script_pushstrcopy(st, cd->pass); break;
  9819. case 16: script_pushstrcopy(st, cd->npc_event);break;
  9820. case 32: script_pushint(st, (cd->users >= cd->limit)); break;
  9821. case 33: script_pushint(st, (cd->users >= cd->trigger)); break;
  9822. default: script_pushint(st, -1); break;
  9823. }
  9824. return SCRIPT_CMD_SUCCESS;
  9825. }
  9826. /// Warps the trigger or target amount of players to the target map and position.
  9827. /// Players are automatically removed from the waiting room.
  9828. /// Those waiting the longest will get warped first.
  9829. /// The target map can be "Random" for a random position in the current map,
  9830. /// and "SavePoint" for the savepoint map+position.
  9831. /// The map flag noteleport of the current map is only considered when teleporting to the savepoint.
  9832. ///
  9833. /// The id's of the teleported players are put into the array $@warpwaitingpc[]
  9834. /// The total number of teleported players is put into $@warpwaitingpcnum
  9835. ///
  9836. /// warpwaitingpc "<map name>",<x>,<y>,<number of players>;
  9837. /// warpwaitingpc "<map name>",<x>,<y>;
  9838. BUILDIN_FUNC(warpwaitingpc)
  9839. {
  9840. int x;
  9841. int y;
  9842. int i;
  9843. int n;
  9844. const char* map_name;
  9845. struct npc_data* nd;
  9846. struct chat_data* cd;
  9847. nd = (struct npc_data *)map_id2bl(st->oid);
  9848. if( nd == NULL || (cd=(struct chat_data *)map_id2bl(nd->chat_id)) == NULL )
  9849. return 0;
  9850. map_name = script_getstr(st,2);
  9851. x = script_getnum(st,3);
  9852. y = script_getnum(st,4);
  9853. n = cd->trigger&0x7f;
  9854. if( script_hasdata(st,5) )
  9855. n = script_getnum(st,5);
  9856. for( i = 0; i < n && cd->users > 0; i++ )
  9857. {
  9858. TBL_PC* sd = cd->usersd[0];
  9859. if( strcmp(map_name,"SavePoint") == 0 && map[sd->bl.m].flag.noteleport )
  9860. {// can't teleport on this map
  9861. break;
  9862. }
  9863. if( cd->zeny )
  9864. {// fee set
  9865. if( (uint32)sd->status.zeny < cd->zeny )
  9866. {// no zeny to cover set fee
  9867. break;
  9868. }
  9869. pc_payzeny(sd, cd->zeny, LOG_TYPE_NPC, NULL);
  9870. }
  9871. mapreg_setreg(reference_uid(add_str("$@warpwaitingpc"), i), sd->bl.id);
  9872. if( strcmp(map_name,"Random") == 0 )
  9873. pc_randomwarp(sd,CLR_TELEPORT);
  9874. else if( strcmp(map_name,"SavePoint") == 0 )
  9875. pc_setpos(sd, sd->status.save_point.map, sd->status.save_point.x, sd->status.save_point.y, CLR_TELEPORT);
  9876. else
  9877. pc_setpos(sd, mapindex_name2id(map_name), x, y, CLR_OUTSIGHT);
  9878. }
  9879. mapreg_setreg(add_str("$@warpwaitingpcnum"), i);
  9880. return SCRIPT_CMD_SUCCESS;
  9881. }
  9882. /////////////////////////////////////////////////////////////////////
  9883. // ...
  9884. //
  9885. /// Detaches a character from a script.
  9886. ///
  9887. /// @param st Script state to detach the character from.
  9888. static void script_detach_rid(struct script_state* st)
  9889. {
  9890. if(st->rid)
  9891. {
  9892. script_detach_state(st, false);
  9893. st->rid = 0;
  9894. }
  9895. }
  9896. /*=========================================================================
  9897. * Attaches a set of RIDs to the current script. [digitalhamster]
  9898. * addrid(<type>{,<flag>{,<parameters>}});
  9899. * <type>:
  9900. * 0 : All players in the server.
  9901. * 1 : All players in the map of the invoking player, or the invoking NPC if no player is attached.
  9902. * 2 : Party members of a specified party ID.
  9903. * [ Parameters: <party id> ]
  9904. * 3 : Guild members of a specified guild ID.
  9905. * [ Parameters: <guild id> ]
  9906. * 4 : All players in a specified area of the map of the invoking player (or NPC).
  9907. * [ Parameters: <x0>,<y0>,<x1>,<y1> ]
  9908. * Account ID: The specified account ID.
  9909. * <flag>:
  9910. * 0 : Players are always attached. (default)
  9911. * 1 : Players currently running another script will not be attached.
  9912. *-------------------------------------------------------------------------*/
  9913. static int buildin_addrid_sub(struct block_list *bl,va_list ap)
  9914. {
  9915. int forceflag;
  9916. struct map_session_data *sd = (TBL_PC *)bl;
  9917. struct script_state* st;
  9918. st = va_arg(ap,struct script_state*);
  9919. forceflag = va_arg(ap,int);
  9920. if(!forceflag || !sd->st)
  9921. if(sd->status.account_id != st->rid)
  9922. run_script(st->script,st->pos,sd->status.account_id,st->oid);
  9923. return 0;
  9924. }
  9925. BUILDIN_FUNC(addrid)
  9926. {
  9927. struct s_mapiterator* iter;
  9928. struct block_list *bl;
  9929. TBL_PC *sd;
  9930. if(st->rid < 1) {
  9931. st->state = END;
  9932. bl = map_id2bl(st->oid);
  9933. } else
  9934. bl = map_id2bl(st->rid); //if run without rid it'd error,also oid if npc, else rid for map
  9935. iter = mapit_getallusers();
  9936. switch(script_getnum(st,2)) {
  9937. case 0:
  9938. for( sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); sd = (TBL_PC*)mapit_next(iter)) {
  9939. if(!script_getnum(st,3) || !sd->st)
  9940. if(sd->status.account_id != st->rid) //attached player already runs.
  9941. run_script(st->script,st->pos,sd->status.account_id,st->oid);
  9942. }
  9943. break;
  9944. case 1:
  9945. for( sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); sd = (TBL_PC*)mapit_next(iter)) {
  9946. if(!script_getnum(st,3) || !sd->st)
  9947. if((sd->bl.m == bl->m) && (sd->status.account_id != st->rid))
  9948. run_script(st->script,st->pos,sd->status.account_id,st->oid);
  9949. }
  9950. break;
  9951. case 2:
  9952. if(script_getnum(st,4) == 0) {
  9953. script_pushint(st,0);
  9954. return 0;
  9955. }
  9956. for( sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); sd = (TBL_PC*)mapit_next(iter)) {
  9957. if(!script_getnum(st,3) || !sd->st)
  9958. if((sd->status.account_id != st->rid) && (sd->status.party_id == script_getnum(st,4))) //attached player already runs.
  9959. run_script(st->script,st->pos,sd->status.account_id,st->oid);
  9960. }
  9961. break;
  9962. case 3:
  9963. if(script_getnum(st,4) == 0) {
  9964. script_pushint(st,0);
  9965. return 0;
  9966. }
  9967. for( sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); sd = (TBL_PC*)mapit_next(iter)) {
  9968. if(!script_getnum(st,3) || !sd->st)
  9969. if((sd->status.account_id != st->rid) && (sd->status.guild_id == script_getnum(st,4))) //attached player already runs.
  9970. run_script(st->script,st->pos,sd->status.account_id,st->oid);
  9971. }
  9972. break;
  9973. case 4:
  9974. map_foreachinarea(buildin_addrid_sub,
  9975. bl->m,script_getnum(st,4),script_getnum(st,5),script_getnum(st,6),script_getnum(st,7),BL_PC,
  9976. st,script_getnum(st,3));//4-x0 , 5-y0 , 6-x1, 7-y1
  9977. break;
  9978. default:
  9979. if((map_id2sd(script_getnum(st,2))) == NULL) { // Player not found.
  9980. script_pushint(st,0);
  9981. return 0;
  9982. }
  9983. if(!script_getnum(st,3) || !map_id2sd(script_getnum(st,2))->st) {
  9984. run_script(st->script,st->pos,script_getnum(st,2),st->oid);
  9985. script_pushint(st,1);
  9986. }
  9987. return 0;
  9988. }
  9989. mapit_free(iter);
  9990. script_pushint(st,1);
  9991. return SCRIPT_CMD_SUCCESS;
  9992. }
  9993. /*==========================================
  9994. * Attach sd char id to script and detach current one if any
  9995. *------------------------------------------*/
  9996. BUILDIN_FUNC(attachrid)
  9997. {
  9998. int rid = script_getnum(st,2);
  9999. if (map_id2sd(rid) != NULL) {
  10000. script_detach_rid(st);
  10001. st->rid = rid;
  10002. script_attach_state(st);
  10003. script_pushint(st,1);
  10004. } else
  10005. script_pushint(st,0);
  10006. return SCRIPT_CMD_SUCCESS;
  10007. }
  10008. /*==========================================
  10009. * Detach script to rid
  10010. *------------------------------------------*/
  10011. BUILDIN_FUNC(detachrid)
  10012. {
  10013. script_detach_rid(st);
  10014. return SCRIPT_CMD_SUCCESS;
  10015. }
  10016. /*==========================================
  10017. * Chk if account connected, (and charid from account if specified)
  10018. *------------------------------------------*/
  10019. BUILDIN_FUNC(isloggedin)
  10020. {
  10021. TBL_PC* sd = map_id2sd(script_getnum(st,2));
  10022. if (script_hasdata(st,3) && sd &&
  10023. sd->status.char_id != script_getnum(st,3))
  10024. sd = NULL;
  10025. push_val(st->stack,C_INT,sd!=NULL);
  10026. return SCRIPT_CMD_SUCCESS;
  10027. }
  10028. /*==========================================
  10029. *
  10030. *------------------------------------------*/
  10031. BUILDIN_FUNC(setmapflagnosave)
  10032. {
  10033. int16 m,x,y;
  10034. unsigned short mapindex;
  10035. const char *str,*str2;
  10036. str=script_getstr(st,2);
  10037. str2=script_getstr(st,3);
  10038. x=script_getnum(st,4);
  10039. y=script_getnum(st,5);
  10040. m = map_mapname2mapid(str);
  10041. mapindex = mapindex_name2id(str2);
  10042. if(m >= 0 && mapindex) {
  10043. map[m].flag.nosave=1;
  10044. map[m].save.map=mapindex;
  10045. map[m].save.x=x;
  10046. map[m].save.y=y;
  10047. }
  10048. return SCRIPT_CMD_SUCCESS;
  10049. }
  10050. BUILDIN_FUNC(getmapflag)
  10051. {
  10052. int16 m,i;
  10053. const char *str;
  10054. str=script_getstr(st,2);
  10055. i=script_getnum(st,3);
  10056. m = map_mapname2mapid(str);
  10057. if(m >= 0) {
  10058. switch(i) {
  10059. case MF_NOMEMO: script_pushint(st,map[m].flag.nomemo); break;
  10060. case MF_NOTELEPORT: script_pushint(st,map[m].flag.noteleport); break;
  10061. case MF_NOSAVE: script_pushint(st,map[m].flag.nosave); break;
  10062. case MF_NOBRANCH: script_pushint(st,map[m].flag.nobranch); break;
  10063. case MF_NOPENALTY: script_pushint(st,map[m].flag.noexppenalty); break;
  10064. case MF_NOZENYPENALTY: script_pushint(st,map[m].flag.nozenypenalty); break;
  10065. case MF_PVP: script_pushint(st,map[m].flag.pvp); break;
  10066. case MF_PVP_NOPARTY: script_pushint(st,map[m].flag.pvp_noparty); break;
  10067. case MF_PVP_NOGUILD: script_pushint(st,map[m].flag.pvp_noguild); break;
  10068. case MF_GVG: script_pushint(st,map[m].flag.gvg); break;
  10069. case MF_GVG_NOPARTY: script_pushint(st,map[m].flag.gvg_noparty); break;
  10070. case MF_NOTRADE: script_pushint(st,map[m].flag.notrade); break;
  10071. case MF_NOSKILL: script_pushint(st,map[m].flag.noskill); break;
  10072. case MF_NOWARP: script_pushint(st,map[m].flag.nowarp); break;
  10073. case MF_PARTYLOCK: script_pushint(st,map[m].flag.partylock); break;
  10074. case MF_NOICEWALL: script_pushint(st,map[m].flag.noicewall); break;
  10075. case MF_SNOW: script_pushint(st,map[m].flag.snow); break;
  10076. case MF_FOG: script_pushint(st,map[m].flag.fog); break;
  10077. case MF_SAKURA: script_pushint(st,map[m].flag.sakura); break;
  10078. case MF_LEAVES: script_pushint(st,map[m].flag.leaves); break;
  10079. case MF_NOGO: script_pushint(st,map[m].flag.nogo); break;
  10080. case MF_CLOUDS: script_pushint(st,map[m].flag.clouds); break;
  10081. case MF_CLOUDS2: script_pushint(st,map[m].flag.clouds2); break;
  10082. case MF_FIREWORKS: script_pushint(st,map[m].flag.fireworks); break;
  10083. case MF_GVG_CASTLE: script_pushint(st,map[m].flag.gvg_castle); break;
  10084. case MF_GVG_DUNGEON: script_pushint(st,map[m].flag.gvg_dungeon); break;
  10085. case MF_NIGHTENABLED: script_pushint(st,map[m].flag.nightenabled); break;
  10086. case MF_NOBASEEXP: script_pushint(st,map[m].flag.nobaseexp); break;
  10087. case MF_NOJOBEXP: script_pushint(st,map[m].flag.nojobexp); break;
  10088. case MF_NOMOBLOOT: script_pushint(st,map[m].flag.nomobloot); break;
  10089. case MF_NOMVPLOOT: script_pushint(st,map[m].flag.nomvploot); break;
  10090. case MF_NORETURN: script_pushint(st,map[m].flag.noreturn); break;
  10091. case MF_NOWARPTO: script_pushint(st,map[m].flag.nowarpto); break;
  10092. case MF_NIGHTMAREDROP: script_pushint(st,map[m].flag.pvp_nightmaredrop); break;
  10093. case MF_RESTRICTED: script_pushint(st,map[m].flag.restricted); break;
  10094. case MF_NOCOMMAND: script_pushint(st,map[m].nocommand); break;
  10095. case MF_NODROP: script_pushint(st,map[m].flag.nodrop); break;
  10096. case MF_JEXP: script_pushint(st,map[m].adjust.jexp); break;
  10097. case MF_BEXP: script_pushint(st,map[m].adjust.bexp); break;
  10098. case MF_NOVENDING: script_pushint(st,map[m].flag.novending); break;
  10099. case MF_LOADEVENT: script_pushint(st,map[m].flag.loadevent); break;
  10100. case MF_NOCHAT: script_pushint(st,map[m].flag.nochat); break;
  10101. case MF_NOEXPPENALTY: script_pushint(st,map[m].flag.noexppenalty ); break;
  10102. case MF_GUILDLOCK: script_pushint(st,map[m].flag.guildlock); break;
  10103. case MF_TOWN: script_pushint(st,map[m].flag.town); break;
  10104. case MF_AUTOTRADE: script_pushint(st,map[m].flag.autotrade); break;
  10105. case MF_ALLOWKS: script_pushint(st,map[m].flag.allowks); break;
  10106. case MF_MONSTER_NOTELEPORT: script_pushint(st,map[m].flag.monster_noteleport); break;
  10107. case MF_PVP_NOCALCRANK: script_pushint(st,map[m].flag.pvp_nocalcrank); break;
  10108. case MF_BATTLEGROUND: script_pushint(st,map[m].flag.battleground); break;
  10109. case MF_RESET: script_pushint(st,map[m].flag.reset); break;
  10110. case MF_CHANNELAUTOJOIN: script_pushint(st,map[m].flag.chmautojoin); break;
  10111. case MF_NOUSECART: script_pushint(st,map[m].flag.nousecart); break;
  10112. case MF_NOITEMCONSUMPTION: script_pushint(st,map[m].flag.noitemconsumption); break;
  10113. case MF_SUMSTARTMIRACLE: script_pushint(st,map[m].flag.nosumstarmiracle); break;
  10114. case MF_NOMINEEFFECT: script_pushint(st,map[m].flag.nomineeffect); break;
  10115. case MF_NOLOCKON: script_pushint(st,map[m].flag.nolockon); break;
  10116. case MF_NOTOMB: script_pushint(st,map[m].flag.notomb); break;
  10117. #ifdef ADJUST_SKILL_DAMAGE
  10118. case MF_SKILL_DAMAGE:
  10119. {
  10120. int ret_val=0, type=0;
  10121. FETCH(4,type);
  10122. switch (type) {
  10123. case 1: ret_val = map[m].adjust.damage.pc; break;
  10124. case 2: ret_val = map[m].adjust.damage.mob; break;
  10125. case 3: ret_val = map[m].adjust.damage.boss; break;
  10126. case 4: ret_val = map[m].adjust.damage.other; break;
  10127. case 5: ret_val = map[m].adjust.damage.caster; break;
  10128. default: ret_val = map[m].flag.skill_damage; break;
  10129. }
  10130. script_pushint(st,ret_val); break;
  10131. } break;
  10132. #endif
  10133. }
  10134. }
  10135. return SCRIPT_CMD_SUCCESS;
  10136. }
  10137. /* pvp timer handling */
  10138. static int script_mapflag_pvp_sub(struct block_list *bl,va_list ap) {
  10139. TBL_PC* sd = (TBL_PC*)bl;
  10140. if (sd->pvp_timer == INVALID_TIMER) {
  10141. sd->pvp_timer = add_timer(gettick() + 200, pc_calc_pvprank_timer, sd->bl.id, 0);
  10142. sd->pvp_rank = 0;
  10143. sd->pvp_lastusers = 0;
  10144. sd->pvp_point = 5;
  10145. sd->pvp_won = 0;
  10146. sd->pvp_lost = 0;
  10147. }
  10148. clif_map_property(sd, MAPPROPERTY_FREEPVPZONE);
  10149. clif_maptypeproperty2(&sd->bl,SELF);
  10150. return 0;
  10151. }
  10152. BUILDIN_FUNC(setmapflag)
  10153. {
  10154. int16 m,i;
  10155. const char *str;
  10156. int val=0;
  10157. str=script_getstr(st,2);
  10158. i=script_getnum(st,3);
  10159. FETCH(4,val);
  10160. m = map_mapname2mapid(str);
  10161. if(m >= 0) {
  10162. switch(i) {
  10163. case MF_NOMEMO: map[m].flag.nomemo = 1; break;
  10164. case MF_NOTELEPORT: map[m].flag.noteleport = 1; break;
  10165. case MF_NOSAVE: map[m].flag.nosave = 1; break;
  10166. case MF_NOBRANCH: map[m].flag.nobranch = 1; break;
  10167. case MF_NOPENALTY: map[m].flag.noexppenalty = 1; map[m].flag.nozenypenalty = 1; break;
  10168. case MF_NOZENYPENALTY: map[m].flag.nozenypenalty = 1; break;
  10169. case MF_PVP:
  10170. map[m].flag.pvp = 1;
  10171. if( !battle_config.pk_mode ) {
  10172. map_foreachinmap(script_mapflag_pvp_sub,m,BL_PC);
  10173. }
  10174. break;
  10175. case MF_PVP_NOPARTY: map[m].flag.pvp_noparty = 1; break;
  10176. case MF_PVP_NOGUILD: map[m].flag.pvp_noguild = 1; break;
  10177. case MF_GVG: {
  10178. struct block_list bl;
  10179. map[m].flag.gvg = 1;
  10180. clif_map_property_mapall(m, MAPPROPERTY_AGITZONE);
  10181. bl.type = BL_NUL;
  10182. bl.m = m;
  10183. clif_maptypeproperty2(&bl,ALL_SAMEMAP);
  10184. }
  10185. break;
  10186. case MF_GVG_NOPARTY: map[m].flag.gvg_noparty = 1; break;
  10187. case MF_NOTRADE: map[m].flag.notrade = 1; break;
  10188. case MF_NOSKILL: map[m].flag.noskill = 1; break;
  10189. case MF_NOWARP: map[m].flag.nowarp = 1; break;
  10190. case MF_PARTYLOCK: map[m].flag.partylock = 1; break;
  10191. case MF_NOICEWALL: map[m].flag.noicewall = 1; break;
  10192. case MF_SNOW: map[m].flag.snow = 1; break;
  10193. case MF_FOG: map[m].flag.fog = 1; break;
  10194. case MF_SAKURA: map[m].flag.sakura = 1; break;
  10195. case MF_LEAVES: map[m].flag.leaves = 1; break;
  10196. case MF_NOGO: map[m].flag.nogo = 1; break;
  10197. case MF_CLOUDS: map[m].flag.clouds = 1; break;
  10198. case MF_CLOUDS2: map[m].flag.clouds2 = 1; break;
  10199. case MF_FIREWORKS: map[m].flag.fireworks = 1; break;
  10200. case MF_GVG_CASTLE: map[m].flag.gvg_castle = 1; break;
  10201. case MF_GVG_DUNGEON: map[m].flag.gvg_dungeon = 1; break;
  10202. case MF_NIGHTENABLED: map[m].flag.nightenabled = 1; break;
  10203. case MF_NOBASEEXP: map[m].flag.nobaseexp = 1; break;
  10204. case MF_NOJOBEXP: map[m].flag.nojobexp = 1; break;
  10205. case MF_NOMOBLOOT: map[m].flag.nomobloot = 1; break;
  10206. case MF_NOMVPLOOT: map[m].flag.nomvploot = 1; break;
  10207. case MF_NORETURN: map[m].flag.noreturn = 1; break;
  10208. case MF_NOWARPTO: map[m].flag.nowarpto = 1; break;
  10209. case MF_NIGHTMAREDROP: map[m].flag.pvp_nightmaredrop = 1; break;
  10210. case MF_RESTRICTED:
  10211. map[m].zone |= 1<<(val+1);
  10212. map[m].flag.restricted=1;
  10213. break;
  10214. case MF_NOCOMMAND: map[m].nocommand = (val <= 0) ? 100 : val; break;
  10215. case MF_NODROP: map[m].flag.nodrop = 1; break;
  10216. case MF_JEXP: map[m].adjust.jexp = (val <= 0) ? 100 : val; break;
  10217. case MF_BEXP: map[m].adjust.bexp = (val <= 0) ? 100 : val; break;
  10218. case MF_NOVENDING: map[m].flag.novending = 1; break;
  10219. case MF_LOADEVENT: map[m].flag.loadevent = 1; break;
  10220. case MF_NOCHAT: map[m].flag.nochat = 1; break;
  10221. case MF_NOEXPPENALTY: map[m].flag.noexppenalty = 1; break;
  10222. case MF_GUILDLOCK: map[m].flag.guildlock = 1; break;
  10223. case MF_TOWN: map[m].flag.town = 1; break;
  10224. case MF_AUTOTRADE: map[m].flag.autotrade = 1; break;
  10225. case MF_ALLOWKS: map[m].flag.allowks = 1; break;
  10226. case MF_MONSTER_NOTELEPORT: map[m].flag.monster_noteleport = 1; break;
  10227. case MF_PVP_NOCALCRANK: map[m].flag.pvp_nocalcrank = 1; break;
  10228. case MF_BATTLEGROUND: map[m].flag.battleground = (val <= 0 || val > 2) ? 1 : val; break;
  10229. case MF_RESET: map[m].flag.reset = 1; break;
  10230. case MF_CHANNELAUTOJOIN: map[m].flag.chmautojoin = 1 ; break;
  10231. case MF_NOUSECART: map[m].flag.nousecart = 1 ; break;
  10232. case MF_NOITEMCONSUMPTION: map[m].flag.noitemconsumption = 1 ; break;
  10233. case MF_SUMSTARTMIRACLE: map[m].flag.nosumstarmiracle = 1 ; break;
  10234. case MF_NOMINEEFFECT: map[m].flag.nomineeffect = 1 ; break;
  10235. case MF_NOLOCKON: map[m].flag.nolockon = 1 ; break;
  10236. case MF_NOTOMB: map[m].flag.notomb = 1; break;
  10237. #ifdef ADJUST_SKILL_DAMAGE
  10238. case MF_SKILL_DAMAGE:
  10239. {
  10240. int type=0;
  10241. FETCH(5,type);
  10242. switch (type) {
  10243. case 1: map[m].adjust.damage.pc = val; break;
  10244. case 2: map[m].adjust.damage.mob = val; break;
  10245. case 3: map[m].adjust.damage.boss = val; break;
  10246. case 4: map[m].adjust.damage.other = val; break;
  10247. case 5: map[m].adjust.damage.caster = val; break;
  10248. }
  10249. map[m].flag.skill_damage = 1;
  10250. } break;
  10251. #endif
  10252. }
  10253. }
  10254. return SCRIPT_CMD_SUCCESS;
  10255. }
  10256. BUILDIN_FUNC(removemapflag)
  10257. {
  10258. int16 m,i;
  10259. const char *str;
  10260. int val=0;
  10261. str=script_getstr(st,2);
  10262. i=script_getnum(st,3);
  10263. FETCH(4,val);
  10264. m = map_mapname2mapid(str);
  10265. if(m >= 0) {
  10266. switch(i) {
  10267. case MF_NOMEMO: map[m].flag.nomemo = 0; break;
  10268. case MF_NOTELEPORT: map[m].flag.noteleport = 0; break;
  10269. case MF_NOSAVE: map[m].flag.nosave = 0; break;
  10270. case MF_NOBRANCH: map[m].flag.nobranch = 0; break;
  10271. case MF_NOPENALTY: map[m].flag.noexppenalty = 0; map[m].flag.nozenypenalty = 0; break;
  10272. case MF_NOZENYPENALTY: map[m].flag.nozenypenalty = 0; break;
  10273. case MF_PVP: {
  10274. struct block_list bl;
  10275. bl.type = BL_NUL;
  10276. bl.m = m;
  10277. map[m].flag.pvp = 0;
  10278. clif_map_property_mapall(m, MAPPROPERTY_NOTHING);
  10279. clif_maptypeproperty2(&bl,ALL_SAMEMAP);
  10280. }
  10281. break;
  10282. case MF_PVP_NOPARTY: map[m].flag.pvp_noparty = 0; break;
  10283. case MF_PVP_NOGUILD: map[m].flag.pvp_noguild = 0; break;
  10284. case MF_GVG: {
  10285. struct block_list bl;
  10286. bl.type = BL_NUL;
  10287. bl.m = m;
  10288. map[m].flag.gvg = 0;
  10289. clif_map_property_mapall(m, MAPPROPERTY_NOTHING);
  10290. clif_maptypeproperty2(&bl,ALL_SAMEMAP);
  10291. }
  10292. break;
  10293. case MF_GVG_NOPARTY: map[m].flag.gvg_noparty = 0; break;
  10294. case MF_NOTRADE: map[m].flag.notrade = 0; break;
  10295. case MF_NOSKILL: map[m].flag.noskill = 0; break;
  10296. case MF_NOWARP: map[m].flag.nowarp = 0; break;
  10297. case MF_PARTYLOCK: map[m].flag.partylock = 0; break;
  10298. case MF_NOICEWALL: map[m].flag.noicewall = 0; break;
  10299. case MF_SNOW: map[m].flag.snow = 0; break;
  10300. case MF_FOG: map[m].flag.fog = 0; break;
  10301. case MF_SAKURA: map[m].flag.sakura = 0; break;
  10302. case MF_LEAVES: map[m].flag.leaves = 0; break;
  10303. case MF_NOGO: map[m].flag.nogo = 0; break;
  10304. case MF_CLOUDS: map[m].flag.clouds = 0; break;
  10305. case MF_CLOUDS2: map[m].flag.clouds2 = 0; break;
  10306. case MF_FIREWORKS: map[m].flag.fireworks = 0; break;
  10307. case MF_GVG_CASTLE: map[m].flag.gvg_castle = 0; break;
  10308. case MF_GVG_DUNGEON: map[m].flag.gvg_dungeon = 0; break;
  10309. case MF_NIGHTENABLED: map[m].flag.nightenabled = 0; break;
  10310. case MF_NOBASEEXP: map[m].flag.nobaseexp = 0; break;
  10311. case MF_NOJOBEXP: map[m].flag.nojobexp = 0; break;
  10312. case MF_NOMOBLOOT: map[m].flag.nomobloot = 0; break;
  10313. case MF_NOMVPLOOT: map[m].flag.nomvploot = 0; break;
  10314. case MF_NORETURN: map[m].flag.noreturn = 0; break;
  10315. case MF_NOWARPTO: map[m].flag.nowarpto = 0; break;
  10316. case MF_NIGHTMAREDROP: map[m].flag.pvp_nightmaredrop = 0; break;
  10317. case MF_RESTRICTED:
  10318. map[m].zone ^= 1<<(val+1);
  10319. if (map[m].zone == 0){
  10320. map[m].flag.restricted=0;
  10321. }
  10322. break;
  10323. case MF_NOCOMMAND: map[m].nocommand = 0; break;
  10324. case MF_NODROP: map[m].flag.nodrop = 0; break;
  10325. case MF_JEXP: map[m].adjust.jexp = 0; break;
  10326. case MF_BEXP: map[m].adjust.bexp = 0; break;
  10327. case MF_NOVENDING: map[m].flag.novending = 0; break;
  10328. case MF_LOADEVENT: map[m].flag.loadevent = 0; break;
  10329. case MF_NOCHAT: map[m].flag.nochat = 0; break;
  10330. case MF_NOEXPPENALTY: map[m].flag.noexppenalty = 0; break;
  10331. case MF_GUILDLOCK: map[m].flag.guildlock = 0; break;
  10332. case MF_TOWN: map[m].flag.town = 0; break;
  10333. case MF_AUTOTRADE: map[m].flag.autotrade = 0; break;
  10334. case MF_ALLOWKS: map[m].flag.allowks = 0; break;
  10335. case MF_MONSTER_NOTELEPORT: map[m].flag.monster_noteleport = 0; break;
  10336. case MF_PVP_NOCALCRANK: map[m].flag.pvp_nocalcrank = 0; break;
  10337. case MF_BATTLEGROUND: map[m].flag.battleground = 0; break;
  10338. case MF_RESET: map[m].flag.reset = 0; break;
  10339. case MF_CHANNELAUTOJOIN: map[m].flag.chmautojoin = 0 ; break;
  10340. case MF_NOUSECART: map[m].flag.nousecart = 0 ; break;
  10341. case MF_NOITEMCONSUMPTION: map[m].flag.noitemconsumption = 0 ; break;
  10342. case MF_SUMSTARTMIRACLE: map[m].flag.nosumstarmiracle = 0 ; break;
  10343. case MF_NOMINEEFFECT: map[m].flag.nomineeffect = 0 ; break;
  10344. case MF_NOLOCKON: map[m].flag.nolockon = 0 ; break;
  10345. case MF_NOTOMB: map[m].flag.notomb = 0; break;
  10346. #ifdef ADJUST_SKILL_DAMAGE
  10347. case MF_SKILL_DAMAGE:
  10348. {
  10349. map[m].flag.skill_damage = 0;
  10350. memset(&map[m].adjust.damage, 0, sizeof(map[m].adjust.damage));
  10351. if (map[m].skill_damage.count)
  10352. map_skill_damage_free(&map[m]);
  10353. } break;
  10354. #endif
  10355. }
  10356. }
  10357. return SCRIPT_CMD_SUCCESS;
  10358. }
  10359. BUILDIN_FUNC(pvpon)
  10360. {
  10361. int16 m;
  10362. const char *str;
  10363. TBL_PC* sd = NULL;
  10364. struct s_mapiterator* iter;
  10365. struct block_list bl;
  10366. str = script_getstr(st,2);
  10367. m = map_mapname2mapid(str);
  10368. if( m < 0 || map[m].flag.pvp )
  10369. return 0; // nothing to do
  10370. map[m].flag.pvp = 1;
  10371. clif_map_property_mapall(m, MAPPROPERTY_FREEPVPZONE);
  10372. bl.type = BL_NUL;
  10373. bl.m = m;
  10374. clif_maptypeproperty2(&bl,ALL_SAMEMAP);
  10375. if(battle_config.pk_mode) // disable ranking functions if pk_mode is on [Valaris]
  10376. return 0;
  10377. iter = mapit_getallusers();
  10378. for( sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); sd = (TBL_PC*)mapit_next(iter) )
  10379. {
  10380. if( sd->bl.m != m || sd->pvp_timer != INVALID_TIMER )
  10381. continue; // not applicable
  10382. sd->pvp_timer = add_timer(gettick()+200,pc_calc_pvprank_timer,sd->bl.id,0);
  10383. sd->pvp_rank = 0;
  10384. sd->pvp_lastusers = 0;
  10385. sd->pvp_point = 5;
  10386. sd->pvp_won = 0;
  10387. sd->pvp_lost = 0;
  10388. }
  10389. mapit_free(iter);
  10390. return SCRIPT_CMD_SUCCESS;
  10391. }
  10392. static int buildin_pvpoff_sub(struct block_list *bl,va_list ap)
  10393. {
  10394. TBL_PC* sd = (TBL_PC*)bl;
  10395. clif_pvpset(sd, 0, 0, 2);
  10396. if (sd->pvp_timer != INVALID_TIMER) {
  10397. delete_timer(sd->pvp_timer, pc_calc_pvprank_timer);
  10398. sd->pvp_timer = INVALID_TIMER;
  10399. }
  10400. return 0;
  10401. }
  10402. BUILDIN_FUNC(pvpoff)
  10403. {
  10404. int16 m;
  10405. const char *str;
  10406. struct block_list bl;
  10407. str=script_getstr(st,2);
  10408. m = map_mapname2mapid(str);
  10409. if(m < 0 || !map[m].flag.pvp)
  10410. return 0; //fixed Lupus
  10411. map[m].flag.pvp = 0;
  10412. clif_map_property_mapall(m, MAPPROPERTY_NOTHING);
  10413. bl.type = BL_NUL;
  10414. bl.m = m;
  10415. clif_maptypeproperty2(&bl,ALL_SAMEMAP);
  10416. if(battle_config.pk_mode) // disable ranking options if pk_mode is on [Valaris]
  10417. return 0;
  10418. map_foreachinmap(buildin_pvpoff_sub, m, BL_PC);
  10419. return SCRIPT_CMD_SUCCESS;
  10420. }
  10421. BUILDIN_FUNC(gvgon)
  10422. {
  10423. int16 m;
  10424. const char *str;
  10425. struct block_list bl;
  10426. str=script_getstr(st,2);
  10427. m = map_mapname2mapid(str);
  10428. if(m >= 0 && !map[m].flag.gvg) {
  10429. map[m].flag.gvg = 1;
  10430. clif_map_property_mapall(m, MAPPROPERTY_AGITZONE);
  10431. bl.type = BL_NUL;
  10432. bl.m = m;
  10433. clif_maptypeproperty2(&bl,ALL_SAMEMAP);
  10434. }
  10435. return SCRIPT_CMD_SUCCESS;
  10436. }
  10437. BUILDIN_FUNC(gvgoff)
  10438. {
  10439. int16 m;
  10440. const char *str;
  10441. str=script_getstr(st,2);
  10442. m = map_mapname2mapid(str);
  10443. if(m >= 0 && map[m].flag.gvg) {
  10444. struct block_list bl;
  10445. map[m].flag.gvg = 0;
  10446. clif_map_property_mapall(m, MAPPROPERTY_NOTHING);
  10447. bl.type = BL_NUL;
  10448. bl.m = m;
  10449. clif_maptypeproperty2(&bl,ALL_SAMEMAP);
  10450. }
  10451. return SCRIPT_CMD_SUCCESS;
  10452. }
  10453. /*==========================================
  10454. * Shows an emoticon on top of the player/npc
  10455. * emotion emotion#, <target: 0 - NPC, 1 - PC>, <NPC/PC name>
  10456. *------------------------------------------*/
  10457. //Optional second parameter added by [Skotlex]
  10458. BUILDIN_FUNC(emotion)
  10459. {
  10460. int type;
  10461. int player=0;
  10462. type=script_getnum(st,2);
  10463. if(type < 0 || type > 100)
  10464. return 0;
  10465. if( script_hasdata(st,3) )
  10466. player=script_getnum(st,3);
  10467. if (player) {
  10468. TBL_PC *sd = NULL;
  10469. if( script_hasdata(st,4) )
  10470. sd = map_nick2sd(script_getstr(st,4));
  10471. else
  10472. sd = script_rid2sd(st);
  10473. if (sd)
  10474. clif_emotion(&sd->bl,type);
  10475. } else
  10476. if( script_hasdata(st,4) )
  10477. {
  10478. TBL_NPC *nd = npc_name2id(script_getstr(st,4));
  10479. if(nd)
  10480. clif_emotion(&nd->bl,type);
  10481. }
  10482. else
  10483. clif_emotion(map_id2bl(st->oid),type);
  10484. return SCRIPT_CMD_SUCCESS;
  10485. }
  10486. static int buildin_maprespawnguildid_sub_pc(struct map_session_data* sd, va_list ap)
  10487. {
  10488. int16 m=va_arg(ap,int);
  10489. int g_id=va_arg(ap,int);
  10490. int flag=va_arg(ap,int);
  10491. if(!sd || sd->bl.m != m)
  10492. return 0;
  10493. if(
  10494. (sd->status.guild_id == g_id && flag&1) || //Warp out owners
  10495. (sd->status.guild_id != g_id && flag&2) || //Warp out outsiders
  10496. (sd->status.guild_id == 0 && flag&2) // Warp out players not in guild
  10497. )
  10498. pc_setpos(sd,sd->status.save_point.map,sd->status.save_point.x,sd->status.save_point.y,CLR_TELEPORT);
  10499. return 1;
  10500. }
  10501. static int buildin_maprespawnguildid_sub_mob(struct block_list *bl,va_list ap)
  10502. {
  10503. struct mob_data *md=(struct mob_data *)bl;
  10504. if(!md->guardian_data && md->mob_id != MOBID_EMPERIUM)
  10505. status_kill(bl);
  10506. return 0;
  10507. }
  10508. /*
  10509. * Function to kick guild members out of a map and to their save points.
  10510. * m : mapid
  10511. * g_id : owner guild id
  10512. * flag & 1 : Warp out owners
  10513. * flag & 2 : Warp out outsiders
  10514. * flag & 4 : reserved for mobs
  10515. * */
  10516. BUILDIN_FUNC(maprespawnguildid)
  10517. {
  10518. const char *mapname=script_getstr(st,2);
  10519. int g_id=script_getnum(st,3);
  10520. int flag=script_getnum(st,4);
  10521. int16 m=map_mapname2mapid(mapname);
  10522. if(m == -1)
  10523. return 0;
  10524. //Catch ALL players (in case some are 'between maps' on execution time)
  10525. map_foreachpc(buildin_maprespawnguildid_sub_pc,m,g_id,flag);
  10526. if (flag&4) //Remove script mobs.
  10527. map_foreachinmap(buildin_maprespawnguildid_sub_mob,m,BL_MOB);
  10528. return SCRIPT_CMD_SUCCESS;
  10529. }
  10530. BUILDIN_FUNC(agitstart)
  10531. {
  10532. if(agit_flag==1) return SCRIPT_CMD_SUCCESS; // Agit already Start.
  10533. agit_flag=1;
  10534. guild_agit_start();
  10535. return SCRIPT_CMD_SUCCESS;
  10536. }
  10537. BUILDIN_FUNC(agitend)
  10538. {
  10539. if(agit_flag==0) return SCRIPT_CMD_SUCCESS; // Agit already End.
  10540. agit_flag=0;
  10541. guild_agit_end();
  10542. return SCRIPT_CMD_SUCCESS;
  10543. }
  10544. BUILDIN_FUNC(agitstart2)
  10545. {
  10546. if(agit2_flag==1) return SCRIPT_CMD_SUCCESS; // Agit2 already Start.
  10547. agit2_flag=1;
  10548. guild_agit2_start();
  10549. return SCRIPT_CMD_SUCCESS;
  10550. }
  10551. BUILDIN_FUNC(agitend2)
  10552. {
  10553. if(agit2_flag==0) return SCRIPT_CMD_SUCCESS; // Agit2 already End.
  10554. agit2_flag=0;
  10555. guild_agit2_end();
  10556. return SCRIPT_CMD_SUCCESS;
  10557. }
  10558. /*==========================================
  10559. * Returns whether woe is on or off. // choice script
  10560. *------------------------------------------*/
  10561. BUILDIN_FUNC(agitcheck)
  10562. {
  10563. script_pushint(st,agit_flag);
  10564. return SCRIPT_CMD_SUCCESS;
  10565. }
  10566. /*==========================================
  10567. * Returns whether woese is on or off. // choice script
  10568. *------------------------------------------*/
  10569. BUILDIN_FUNC(agitcheck2)
  10570. {
  10571. script_pushint(st,agit2_flag);
  10572. return SCRIPT_CMD_SUCCESS;
  10573. }
  10574. /// Sets the guild_id of this npc.
  10575. ///
  10576. /// flagemblem <guild_id>;
  10577. BUILDIN_FUNC(flagemblem)
  10578. {
  10579. TBL_NPC* nd;
  10580. int g_id = script_getnum(st,2);
  10581. if(g_id < 0) return 0;
  10582. nd = (TBL_NPC*)map_id2nd(st->oid);
  10583. if( nd == NULL ) {
  10584. ShowError("script:flagemblem: npc %d not found\n", st->oid);
  10585. } else if( nd->subtype != NPCTYPE_SCRIPT ) {
  10586. ShowError("script:flagemblem: unexpected subtype %d for npc %d '%s'\n", nd->subtype, st->oid, nd->exname);
  10587. } else {
  10588. bool changed = ( nd->u.scr.guild_id != g_id )?true:false;
  10589. nd->u.scr.guild_id = g_id;
  10590. clif_guild_emblem_area(&nd->bl);
  10591. /* guild flag caching */
  10592. if( g_id ) /* adding a id */
  10593. guild_flag_add(nd);
  10594. else if( changed ) /* removing a flag */
  10595. guild_flag_remove(nd);
  10596. }
  10597. return SCRIPT_CMD_SUCCESS;
  10598. }
  10599. BUILDIN_FUNC(getcastlename)
  10600. {
  10601. const char* mapname = mapindex_getmapname(script_getstr(st,2),NULL);
  10602. struct guild_castle* gc = guild_mapname2gc(mapname);
  10603. const char* name = (gc) ? gc->castle_name : "";
  10604. script_pushstrcopy(st,name);
  10605. return SCRIPT_CMD_SUCCESS;
  10606. }
  10607. BUILDIN_FUNC(getcastledata)
  10608. {
  10609. const char *mapname = mapindex_getmapname(script_getstr(st,2),NULL);
  10610. int index = script_getnum(st,3);
  10611. struct guild_castle *gc = guild_mapname2gc(mapname);
  10612. if (gc == NULL) {
  10613. script_pushint(st,0);
  10614. ShowWarning("buildin_setcastledata: guild castle for map '%s' not found\n", mapname);
  10615. return 1;
  10616. }
  10617. switch (index) {
  10618. case 1:
  10619. script_pushint(st,gc->guild_id); break;
  10620. case 2:
  10621. script_pushint(st,gc->economy); break;
  10622. case 3:
  10623. script_pushint(st,gc->defense); break;
  10624. case 4:
  10625. script_pushint(st,gc->triggerE); break;
  10626. case 5:
  10627. script_pushint(st,gc->triggerD); break;
  10628. case 6:
  10629. script_pushint(st,gc->nextTime); break;
  10630. case 7:
  10631. script_pushint(st,gc->payTime); break;
  10632. case 8:
  10633. script_pushint(st,gc->createTime); break;
  10634. case 9:
  10635. script_pushint(st,gc->visibleC); break;
  10636. default:
  10637. if (index > 9 && index <= 9+MAX_GUARDIANS) {
  10638. script_pushint(st,gc->guardian[index-10].visible);
  10639. break;
  10640. }
  10641. script_pushint(st,0);
  10642. ShowWarning("buildin_setcastledata: index = '%d' is out of allowed range\n", index);
  10643. return 1;
  10644. }
  10645. return SCRIPT_CMD_SUCCESS;
  10646. }
  10647. BUILDIN_FUNC(setcastledata)
  10648. {
  10649. const char *mapname = mapindex_getmapname(script_getstr(st,2),NULL);
  10650. int index = script_getnum(st,3);
  10651. int value = script_getnum(st,4);
  10652. struct guild_castle *gc = guild_mapname2gc(mapname);
  10653. if (gc == NULL) {
  10654. ShowWarning("buildin_setcastledata: guild castle for map '%s' not found\n", mapname);
  10655. return 1;
  10656. }
  10657. if (index <= 0 || index > 9+MAX_GUARDIANS) {
  10658. ShowWarning("buildin_setcastledata: index = '%d' is out of allowed range\n", index);
  10659. return 1;
  10660. }
  10661. guild_castledatasave(gc->castle_id, index, value);
  10662. return SCRIPT_CMD_SUCCESS;
  10663. }
  10664. /* =====================================================================
  10665. * ---------------------------------------------------------------------*/
  10666. BUILDIN_FUNC(requestguildinfo)
  10667. {
  10668. int guild_id=script_getnum(st,2);
  10669. const char *event=NULL;
  10670. if( script_hasdata(st,3) ){
  10671. event=script_getstr(st,3);
  10672. check_event(st, event);
  10673. }
  10674. if(guild_id>0)
  10675. guild_npc_request_info(guild_id,event);
  10676. return SCRIPT_CMD_SUCCESS;
  10677. }
  10678. /// Returns the number of cards that have been compounded onto the specified equipped item.
  10679. /// getequipcardcnt(<equipment slot>);
  10680. BUILDIN_FUNC(getequipcardcnt)
  10681. {
  10682. int i=-1,j,num;
  10683. TBL_PC *sd;
  10684. int count;
  10685. num=script_getnum(st,2);
  10686. sd=script_rid2sd(st);
  10687. if (num > 0 && num <= ARRAYLENGTH(equip))
  10688. i=pc_checkequip(sd,equip[num-1]);
  10689. if (i < 0 || !sd->inventory_data[i]) {
  10690. script_pushint(st,0);
  10691. return 0;
  10692. }
  10693. if(itemdb_isspecial(sd->status.inventory[i].card[0]))
  10694. {
  10695. script_pushint(st,0);
  10696. return 0;
  10697. }
  10698. count = 0;
  10699. for( j = 0; j < sd->inventory_data[i]->slot; j++ )
  10700. if( sd->status.inventory[i].card[j] && itemdb_type(sd->status.inventory[i].card[j]) == IT_CARD )
  10701. count++;
  10702. script_pushint(st,count);
  10703. return SCRIPT_CMD_SUCCESS;
  10704. }
  10705. /// Removes all cards from the item found in the specified equipment slot of the invoking character,
  10706. /// and give them to the character. If any cards were removed in this manner, it will also show a success effect.
  10707. /// successremovecards <slot>;
  10708. BUILDIN_FUNC(successremovecards) {
  10709. int i=-1,c,cardflag=0;
  10710. TBL_PC* sd = script_rid2sd(st);
  10711. int num = script_getnum(st,2);
  10712. if (num > 0 && num <= ARRAYLENGTH(equip))
  10713. i=pc_checkequip(sd,equip[num-1]);
  10714. if (i < 0 || !sd->inventory_data[i]) {
  10715. return 0;
  10716. }
  10717. if(itemdb_isspecial(sd->status.inventory[i].card[0]))
  10718. return 0;
  10719. for( c = sd->inventory_data[i]->slot - 1; c >= 0; --c ) {
  10720. if( sd->status.inventory[i].card[c] && itemdb_type(sd->status.inventory[i].card[c]) == IT_CARD ) {// extract this card from the item
  10721. unsigned char flag = 0;
  10722. struct item item_tmp;
  10723. memset(&item_tmp,0,sizeof(item_tmp));
  10724. cardflag = 1;
  10725. item_tmp.nameid = sd->status.inventory[i].card[c];
  10726. item_tmp.identify = 1;
  10727. if((flag=pc_additem(sd,&item_tmp,1,LOG_TYPE_SCRIPT))){ // get back the cart in inventory
  10728. clif_additem(sd,0,0,flag);
  10729. map_addflooritem(&item_tmp,1,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0);
  10730. }
  10731. }
  10732. }
  10733. if(cardflag == 1) {//if card was remove remplace item with no card
  10734. unsigned char flag = 0, j;
  10735. struct item item_tmp;
  10736. memset(&item_tmp,0,sizeof(item_tmp));
  10737. item_tmp.nameid = sd->status.inventory[i].nameid;
  10738. item_tmp.identify = 1;
  10739. item_tmp.refine = sd->status.inventory[i].refine;
  10740. item_tmp.attribute = sd->status.inventory[i].attribute;
  10741. item_tmp.expire_time = sd->status.inventory[i].expire_time;
  10742. item_tmp.bound = sd->status.inventory[i].bound;
  10743. for (j = sd->inventory_data[i]->slot; j < MAX_SLOTS; j++)
  10744. item_tmp.card[j]=sd->status.inventory[i].card[j];
  10745. pc_delitem(sd,i,1,0,3,LOG_TYPE_SCRIPT);
  10746. if((flag=pc_additem(sd,&item_tmp,1,LOG_TYPE_SCRIPT))){ //chk if can be spawn in inventory otherwise put on floor
  10747. clif_additem(sd,0,0,flag);
  10748. map_addflooritem(&item_tmp,1,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0);
  10749. }
  10750. clif_misceffect(&sd->bl,3);
  10751. }
  10752. return SCRIPT_CMD_SUCCESS;
  10753. }
  10754. /// Removes all cards from the item found in the specified equipment slot of the invoking character.
  10755. /// failedremovecards <slot>, <type>;
  10756. /// <type>=0 : will destroy both the item and the cards.
  10757. /// <type>=1 : will keep the item, but destroy the cards.
  10758. /// <type>=2 : will keep the cards, but destroy the item.
  10759. /// <type>=? : will just display the failure effect.
  10760. BUILDIN_FUNC(failedremovecards) {
  10761. int i=-1,c,cardflag=0;
  10762. TBL_PC* sd = script_rid2sd(st);
  10763. int num = script_getnum(st,2);
  10764. int typefail = script_getnum(st,3);
  10765. if (num > 0 && num <= ARRAYLENGTH(equip))
  10766. i=pc_checkequip(sd,equip[num-1]);
  10767. if (i < 0 || !sd->inventory_data[i])
  10768. return 0;
  10769. if(itemdb_isspecial(sd->status.inventory[i].card[0]))
  10770. return 0;
  10771. for( c = sd->inventory_data[i]->slot - 1; c >= 0; --c ) {
  10772. if( sd->status.inventory[i].card[c] && itemdb_type(sd->status.inventory[i].card[c]) == IT_CARD ) {
  10773. cardflag = 1;
  10774. if(typefail == 2) {// add cards to inventory, clear
  10775. unsigned char flag = 0;
  10776. struct item item_tmp;
  10777. memset(&item_tmp,0,sizeof(item_tmp));
  10778. item_tmp.nameid = sd->status.inventory[i].card[c];
  10779. item_tmp.identify = 1;
  10780. if((flag=pc_additem(sd,&item_tmp,1,LOG_TYPE_SCRIPT))){
  10781. clif_additem(sd,0,0,flag);
  10782. map_addflooritem(&item_tmp,1,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0);
  10783. }
  10784. }
  10785. }
  10786. }
  10787. if(cardflag == 1) {
  10788. if(typefail == 0 || typefail == 2){ // destroy the item
  10789. pc_delitem(sd,i,1,0,2,LOG_TYPE_SCRIPT);
  10790. }
  10791. if(typefail == 1){ // destroy the card
  10792. unsigned char flag = 0, j;
  10793. struct item item_tmp;
  10794. memset(&item_tmp,0,sizeof(item_tmp));
  10795. item_tmp.nameid = sd->status.inventory[i].nameid;
  10796. item_tmp.identify = 1;
  10797. item_tmp.refine = sd->status.inventory[i].refine;
  10798. item_tmp.attribute = sd->status.inventory[i].attribute;
  10799. item_tmp.expire_time = sd->status.inventory[i].expire_time;
  10800. item_tmp.bound = sd->status.inventory[i].bound;
  10801. for (j = sd->inventory_data[i]->slot; j < MAX_SLOTS; j++)
  10802. item_tmp.card[j]=sd->status.inventory[i].card[j];
  10803. pc_delitem(sd,i,1,0,2,LOG_TYPE_SCRIPT);
  10804. if((flag=pc_additem(sd,&item_tmp,1,LOG_TYPE_SCRIPT))){
  10805. clif_additem(sd,0,0,flag);
  10806. map_addflooritem(&item_tmp,1,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0);
  10807. }
  10808. }
  10809. clif_misceffect(&sd->bl,2);
  10810. }
  10811. return SCRIPT_CMD_SUCCESS;
  10812. }
  10813. /* ================================================================
  10814. * mapwarp "<from map>","<to map>",<x>,<y>,<type>,<ID for Type>;
  10815. * type: 0=everyone, 1=guild, 2=party; [Reddozen]
  10816. * improved by [Lance]
  10817. * ================================================================*/
  10818. BUILDIN_FUNC(mapwarp) // Added by RoVeRT
  10819. {
  10820. int x,y,m,check_val=0,check_ID=0,i=0;
  10821. struct guild *g = NULL;
  10822. struct party_data *p = NULL;
  10823. const char *str;
  10824. const char *mapname;
  10825. unsigned int index;
  10826. mapname=script_getstr(st,2);
  10827. str=script_getstr(st,3);
  10828. x=script_getnum(st,4);
  10829. y=script_getnum(st,5);
  10830. if(script_hasdata(st,7)){
  10831. check_val=script_getnum(st,6);
  10832. check_ID=script_getnum(st,7);
  10833. }
  10834. if((m=map_mapname2mapid(mapname))< 0)
  10835. return 0;
  10836. if(!(index=mapindex_name2id(str)))
  10837. return 0;
  10838. switch(check_val){
  10839. case 1:
  10840. g = guild_search(check_ID);
  10841. if (g){
  10842. for( i=0; i < g->max_member; i++)
  10843. {
  10844. if(g->member[i].sd && g->member[i].sd->bl.m==m){
  10845. pc_setpos(g->member[i].sd,index,x,y,CLR_TELEPORT);
  10846. }
  10847. }
  10848. }
  10849. break;
  10850. case 2:
  10851. p = party_search(check_ID);
  10852. if(p){
  10853. for(i=0;i<MAX_PARTY; i++){
  10854. if(p->data[i].sd && p->data[i].sd->bl.m == m){
  10855. pc_setpos(p->data[i].sd,index,x,y,CLR_TELEPORT);
  10856. }
  10857. }
  10858. }
  10859. break;
  10860. default:
  10861. map_foreachinmap(buildin_areawarp_sub,m,BL_PC,index,x,y,0,0);
  10862. break;
  10863. }
  10864. return SCRIPT_CMD_SUCCESS;
  10865. }
  10866. static int buildin_mobcount_sub(struct block_list *bl,va_list ap) // Added by RoVeRT
  10867. {
  10868. char *event=va_arg(ap,char *);
  10869. struct mob_data *md = ((struct mob_data *)bl);
  10870. if( md->status.hp > 0 && (!event || strcmp(event,md->npc_event) == 0) )
  10871. return 1;
  10872. return SCRIPT_CMD_SUCCESS;
  10873. }
  10874. BUILDIN_FUNC(mobcount) // Added by RoVeRT
  10875. {
  10876. const char *mapname,*event;
  10877. int16 m;
  10878. mapname=script_getstr(st,2);
  10879. event=script_getstr(st,3);
  10880. if( strcmp(event, "all") == 0 )
  10881. event = NULL;
  10882. else
  10883. check_event(st, event);
  10884. if( strcmp(mapname, "this") == 0 ) {
  10885. struct map_session_data *sd = script_rid2sd(st);
  10886. if( sd )
  10887. m = sd->bl.m;
  10888. else {
  10889. script_pushint(st,-1);
  10890. return 0;
  10891. }
  10892. }
  10893. else if( (m = map_mapname2mapid(mapname)) < 0 ) {
  10894. script_pushint(st,-1);
  10895. return 0;
  10896. }
  10897. script_pushint(st,map_foreachinmap(buildin_mobcount_sub, m, BL_MOB, event));
  10898. return SCRIPT_CMD_SUCCESS;
  10899. }
  10900. BUILDIN_FUNC(marriage)
  10901. {
  10902. const char *partner=script_getstr(st,2);
  10903. TBL_PC *sd=script_rid2sd(st);
  10904. TBL_PC *p_sd=map_nick2sd(partner);
  10905. if(!sd || !p_sd || !pc_marriage(sd,p_sd)){
  10906. script_pushint(st,0);
  10907. return 0;
  10908. }
  10909. script_pushint(st,1);
  10910. return SCRIPT_CMD_SUCCESS;
  10911. }
  10912. BUILDIN_FUNC(wedding_effect)
  10913. {
  10914. TBL_PC *sd=script_rid2sd(st);
  10915. struct block_list *bl;
  10916. if(sd==NULL) {
  10917. bl=map_id2bl(st->oid);
  10918. } else
  10919. bl=&sd->bl;
  10920. clif_wedding_effect(bl);
  10921. return SCRIPT_CMD_SUCCESS;
  10922. }
  10923. /**
  10924. * divorce({<char_id>})
  10925. **/
  10926. BUILDIN_FUNC(divorce)
  10927. {
  10928. TBL_PC *sd;
  10929. if (!script_charid2sd(2,sd) || !pc_divorce(sd)) {
  10930. script_pushint(st,0);
  10931. return SCRIPT_CMD_FAILURE;
  10932. }
  10933. script_pushint(st,1);
  10934. return SCRIPT_CMD_SUCCESS;
  10935. }
  10936. /**
  10937. * ispartneron({<char_id>})
  10938. **/
  10939. BUILDIN_FUNC(ispartneron)
  10940. {
  10941. TBL_PC *sd;
  10942. if (!script_charid2sd(2,sd) || !pc_ismarried(sd) ||
  10943. map_charid2sd(sd->status.partner_id) == NULL)
  10944. {
  10945. script_pushint(st,0);
  10946. return SCRIPT_CMD_FAILURE;
  10947. }
  10948. script_pushint(st,1);
  10949. return SCRIPT_CMD_SUCCESS;
  10950. }
  10951. /**
  10952. * getpartnerid({<char_id>})
  10953. **/
  10954. BUILDIN_FUNC(getpartnerid)
  10955. {
  10956. TBL_PC *sd;
  10957. if (!script_charid2sd(2,sd)) {
  10958. script_pushint(st,0);
  10959. return SCRIPT_CMD_FAILURE;
  10960. }
  10961. script_pushint(st,sd->status.partner_id);
  10962. return SCRIPT_CMD_SUCCESS;
  10963. }
  10964. /**
  10965. * getchildid({<char_id>})
  10966. **/
  10967. BUILDIN_FUNC(getchildid)
  10968. {
  10969. TBL_PC *sd;
  10970. if (!script_charid2sd(2,sd)) {
  10971. script_pushint(st,0);
  10972. return SCRIPT_CMD_FAILURE;
  10973. }
  10974. script_pushint(st,sd->status.child);
  10975. return SCRIPT_CMD_SUCCESS;
  10976. }
  10977. /**
  10978. * getmotherid({<char_id>})
  10979. **/
  10980. BUILDIN_FUNC(getmotherid)
  10981. {
  10982. TBL_PC *sd;
  10983. if (!script_charid2sd(2,sd)) {
  10984. script_pushint(st,0);
  10985. return SCRIPT_CMD_FAILURE;
  10986. }
  10987. script_pushint(st,sd->status.mother);
  10988. return SCRIPT_CMD_SUCCESS;
  10989. }
  10990. /**
  10991. * getfatherid({<char_id>})
  10992. **/
  10993. BUILDIN_FUNC(getfatherid)
  10994. {
  10995. TBL_PC *sd;
  10996. if (!script_charid2sd(2,sd)) {
  10997. script_pushint(st,0);
  10998. return SCRIPT_CMD_FAILURE;
  10999. }
  11000. script_pushint(st,sd->status.father);
  11001. return SCRIPT_CMD_SUCCESS;
  11002. }
  11003. BUILDIN_FUNC(warppartner)
  11004. {
  11005. int x,y;
  11006. unsigned short mapindex;
  11007. const char *str;
  11008. TBL_PC *sd=script_rid2sd(st);
  11009. TBL_PC *p_sd=NULL;
  11010. if(sd==NULL || !pc_ismarried(sd) ||
  11011. (p_sd=map_charid2sd(sd->status.partner_id)) == NULL) {
  11012. script_pushint(st,0);
  11013. return 0;
  11014. }
  11015. str=script_getstr(st,2);
  11016. x=script_getnum(st,3);
  11017. y=script_getnum(st,4);
  11018. mapindex = mapindex_name2id(str);
  11019. if (mapindex) {
  11020. pc_setpos(p_sd,mapindex,x,y,CLR_OUTSIGHT);
  11021. script_pushint(st,1);
  11022. } else
  11023. script_pushint(st,0);
  11024. return SCRIPT_CMD_SUCCESS;
  11025. }
  11026. /*================================================
  11027. * Script for Displaying MOB Information [Valaris]
  11028. *------------------------------------------------*/
  11029. BUILDIN_FUNC(strmobinfo)
  11030. {
  11031. int num=script_getnum(st,2);
  11032. int class_=script_getnum(st,3);
  11033. if(!mobdb_checkid(class_))
  11034. {
  11035. if (num < 3) //requested a string
  11036. script_pushconststr(st,"");
  11037. else
  11038. script_pushint(st,0);
  11039. return 0;
  11040. }
  11041. switch (num) {
  11042. case 1: script_pushstrcopy(st,mob_db(class_)->name); break;
  11043. case 2: script_pushstrcopy(st,mob_db(class_)->jname); break;
  11044. case 3: script_pushint(st,mob_db(class_)->lv); break;
  11045. case 4: script_pushint(st,mob_db(class_)->status.max_hp); break;
  11046. case 5: script_pushint(st,mob_db(class_)->status.max_sp); break;
  11047. case 6: script_pushint(st,mob_db(class_)->base_exp); break;
  11048. case 7: script_pushint(st,mob_db(class_)->job_exp); break;
  11049. default:
  11050. script_pushint(st,0);
  11051. break;
  11052. }
  11053. return SCRIPT_CMD_SUCCESS;
  11054. }
  11055. /*==========================================
  11056. * Summon guardians [Valaris]
  11057. * guardian("<map name>",<x>,<y>,"<name to show>",<mob id>{,"<event label>"}{,<guardian index>}) -> <id>
  11058. *------------------------------------------*/
  11059. BUILDIN_FUNC(guardian)
  11060. {
  11061. int class_=0,x=0,y=0,guardian=0;
  11062. const char *str,*mapname,*evt="";
  11063. bool has_index = false;
  11064. mapname =script_getstr(st,2);
  11065. x =script_getnum(st,3);
  11066. y =script_getnum(st,4);
  11067. str =script_getstr(st,5);
  11068. class_=script_getnum(st,6);
  11069. if( script_hasdata(st,8) )
  11070. {// "<event label>",<guardian index>
  11071. evt=script_getstr(st,7);
  11072. guardian=script_getnum(st,8);
  11073. has_index = true;
  11074. } else if( script_hasdata(st,7) ){
  11075. struct script_data *data = script_getdata(st,7);
  11076. get_val(st,data);
  11077. if( data_isstring(data) )
  11078. {// "<event label>"
  11079. evt=script_getstr(st,7);
  11080. } else if( data_isint(data) )
  11081. {// <guardian index>
  11082. guardian=script_getnum(st,7);
  11083. has_index = true;
  11084. } else {
  11085. ShowError("script:guardian: invalid data type for argument #6 (from 1)\n");
  11086. script_reportdata(data);
  11087. return 1;
  11088. }
  11089. }
  11090. check_event(st, evt);
  11091. script_pushint(st, mob_spawn_guardian(mapname,x,y,str,class_,evt,guardian,has_index));
  11092. return SCRIPT_CMD_SUCCESS;
  11093. }
  11094. /*==========================================
  11095. * Invisible Walls [Zephyrus]
  11096. *------------------------------------------*/
  11097. BUILDIN_FUNC(setwall)
  11098. {
  11099. const char *mapname, *name;
  11100. int x, y, m, size, dir;
  11101. bool shootable;
  11102. mapname = script_getstr(st,2);
  11103. x = script_getnum(st,3);
  11104. y = script_getnum(st,4);
  11105. size = script_getnum(st,5);
  11106. dir = script_getnum(st,6);
  11107. shootable = script_getnum(st,7);
  11108. name = script_getstr(st,8);
  11109. if( (m = map_mapname2mapid(mapname)) < 0 )
  11110. return 0; // Invalid Map
  11111. map_iwall_set(m, x, y, size, dir, shootable, name);
  11112. return SCRIPT_CMD_SUCCESS;
  11113. }
  11114. BUILDIN_FUNC(delwall)
  11115. {
  11116. const char *name = script_getstr(st,2);
  11117. map_iwall_remove(name);
  11118. return SCRIPT_CMD_SUCCESS;
  11119. }
  11120. /// Retrieves various information about the specified guardian.
  11121. ///
  11122. /// guardianinfo("<map_name>", <index>, <type>) -> <value>
  11123. /// type: 0 - whether it is deployed or not
  11124. /// 1 - maximum hp
  11125. /// 2 - current hp
  11126. ///
  11127. BUILDIN_FUNC(guardianinfo)
  11128. {
  11129. const char* mapname = mapindex_getmapname(script_getstr(st,2),NULL);
  11130. int id = script_getnum(st,3);
  11131. int type = script_getnum(st,4);
  11132. struct guild_castle* gc = guild_mapname2gc(mapname);
  11133. struct mob_data* gd;
  11134. if( gc == NULL || id < 0 || id >= MAX_GUARDIANS )
  11135. {
  11136. script_pushint(st,-1);
  11137. return 0;
  11138. }
  11139. if( type == 0 )
  11140. script_pushint(st, gc->guardian[id].visible);
  11141. else
  11142. if( !gc->guardian[id].visible )
  11143. script_pushint(st,-1);
  11144. else
  11145. if( (gd = map_id2md(gc->guardian[id].id)) == NULL )
  11146. script_pushint(st,-1);
  11147. else
  11148. {
  11149. if ( type == 1 ) script_pushint(st,gd->status.max_hp);
  11150. else if( type == 2 ) script_pushint(st,gd->status.hp);
  11151. else
  11152. script_pushint(st,-1);
  11153. }
  11154. return SCRIPT_CMD_SUCCESS;
  11155. }
  11156. /*==========================================
  11157. * Get the item name by item_id or null
  11158. *------------------------------------------*/
  11159. BUILDIN_FUNC(getitemname)
  11160. {
  11161. unsigned short item_id = 0;
  11162. struct item_data *i_data;
  11163. char *item_name;
  11164. struct script_data *data;
  11165. data=script_getdata(st,2);
  11166. get_val(st,data);
  11167. if( data_isstring(data) ){
  11168. const char *name=conv_str(st,data);
  11169. struct item_data *item_data = itemdb_searchname(name);
  11170. if( item_data )
  11171. item_id=item_data->nameid;
  11172. }else
  11173. item_id=conv_num(st,data);
  11174. i_data = itemdb_exists(item_id);
  11175. if (i_data == NULL)
  11176. {
  11177. script_pushconststr(st,"null");
  11178. return 0;
  11179. }
  11180. item_name=(char *)aMalloc(ITEM_NAME_LENGTH*sizeof(char));
  11181. memcpy(item_name, i_data->jname, ITEM_NAME_LENGTH);
  11182. script_pushstr(st,item_name);
  11183. return SCRIPT_CMD_SUCCESS;
  11184. }
  11185. /*==========================================
  11186. * Returns number of slots an item has. [Skotlex]
  11187. *------------------------------------------*/
  11188. BUILDIN_FUNC(getitemslots)
  11189. {
  11190. unsigned short item_id;
  11191. struct item_data *i_data;
  11192. item_id=script_getnum(st,2);
  11193. i_data = itemdb_exists(item_id);
  11194. if (i_data)
  11195. script_pushint(st,i_data->slot);
  11196. else
  11197. script_pushint(st,-1);
  11198. return SCRIPT_CMD_SUCCESS;
  11199. }
  11200. // TODO: add matk here if needed/once we get rid of RENEWAL
  11201. /*==========================================
  11202. * Returns some values of an item [Lupus]
  11203. * Price, Weight, etc...
  11204. getiteminfo(itemID,n), where n
  11205. 0 value_buy;
  11206. 1 value_sell;
  11207. 2 type;
  11208. 3 maxchance = Max drop chance of this item e.g. 1 = 0.01% , etc..
  11209. if = 0, then monsters don't drop it at all (rare or a quest item)
  11210. if = -1, then this item is sold in NPC shops only
  11211. 4 sex;
  11212. 5 equip;
  11213. 6 weight;
  11214. 7 atk;
  11215. 8 def;
  11216. 9 range;
  11217. 10 slot;
  11218. 11 look;
  11219. 12 elv;
  11220. 13 wlv;
  11221. 14 view id
  11222. 15 eLvmax
  11223. 16 matk (renewal)
  11224. *------------------------------------------*/
  11225. BUILDIN_FUNC(getiteminfo)
  11226. {
  11227. unsigned short item_id,n;
  11228. struct item_data *i_data;
  11229. item_id = script_getnum(st,2);
  11230. n = script_getnum(st,3);
  11231. i_data = itemdb_exists(item_id);
  11232. if (i_data && n <= 16) {
  11233. int *item_arr = (int*)&i_data->value_buy;
  11234. #ifndef RENEWAL
  11235. if (n == 16)
  11236. script_pushint(st,0);
  11237. else
  11238. #endif
  11239. script_pushint(st,item_arr[n]);
  11240. } else
  11241. script_pushint(st,-1);
  11242. return SCRIPT_CMD_SUCCESS;
  11243. }
  11244. /*==========================================
  11245. * Set some values of an item [Lupus]
  11246. * Price, Weight, etc...
  11247. setiteminfo(itemID,n,Value), where n
  11248. 0 value_buy;
  11249. 1 value_sell;
  11250. 2 type;
  11251. 3 maxchance = Max drop chance of this item e.g. 1 = 0.01% , etc..
  11252. if = 0, then monsters don't drop it at all (rare or a quest item)
  11253. if = -1, then this item is sold in NPC shops only
  11254. 4 sex;
  11255. 5 equip;
  11256. 6 weight;
  11257. 7 atk;
  11258. 8 def;
  11259. 9 range;
  11260. 10 slot;
  11261. 11 look;
  11262. 12 elv;
  11263. 13 wlv;
  11264. 14 view id
  11265. * Returns Value or -1 if the wrong field's been set
  11266. *------------------------------------------*/
  11267. BUILDIN_FUNC(setiteminfo)
  11268. {
  11269. unsigned short item_id;
  11270. int n,value;
  11271. struct item_data *i_data;
  11272. item_id = script_getnum(st,2);
  11273. n = script_getnum(st,3);
  11274. value = script_getnum(st,4);
  11275. i_data = itemdb_exists(item_id);
  11276. if (i_data && n>=0 && n<=14) {
  11277. int *item_arr = (int*)&i_data->value_buy;
  11278. item_arr[n] = value;
  11279. script_pushint(st,value);
  11280. } else
  11281. script_pushint(st,-1);
  11282. return SCRIPT_CMD_SUCCESS;
  11283. }
  11284. /*==========================================
  11285. * Returns value from equipped item slot n [Lupus]
  11286. getequipcardid(num,slot)
  11287. where
  11288. num = eqip position slot
  11289. slot = 0,1,2,3 (Card Slot N)
  11290. This func returns CARD ID, 255,254,-255 (for card 0, if the item is produced)
  11291. it's useful when you want to check item cards or if it's signed
  11292. Useful for such quests as "Sign this refined item with players name" etc
  11293. Hat[0] +4 -> Player's Hat[0] +4
  11294. *------------------------------------------*/
  11295. BUILDIN_FUNC(getequipcardid)
  11296. {
  11297. int i=-1,num,slot;
  11298. TBL_PC *sd;
  11299. num=script_getnum(st,2);
  11300. slot=script_getnum(st,3);
  11301. sd=script_rid2sd(st);
  11302. if (num > 0 && num <= ARRAYLENGTH(equip))
  11303. i=pc_checkequip(sd,equip[num-1]);
  11304. if(i >= 0 && slot>=0 && slot<4)
  11305. script_pushint(st,sd->status.inventory[i].card[slot]);
  11306. else
  11307. script_pushint(st,0);
  11308. return SCRIPT_CMD_SUCCESS;
  11309. }
  11310. /*==========================================
  11311. * petskillbonus [Valaris] //Rewritten by [Skotlex]
  11312. *------------------------------------------*/
  11313. BUILDIN_FUNC(petskillbonus)
  11314. {
  11315. struct pet_data *pd;
  11316. TBL_PC *sd = script_rid2sd(st);
  11317. if(sd == NULL || sd->pd == NULL)
  11318. return SCRIPT_CMD_FAILURE;
  11319. pd = sd->pd;
  11320. if (pd->bonus)
  11321. { //Clear previous bonus
  11322. if (pd->bonus->timer != INVALID_TIMER)
  11323. delete_timer(pd->bonus->timer, pet_skill_bonus_timer);
  11324. } else //init
  11325. pd->bonus = (struct pet_bonus *) aMalloc(sizeof(struct pet_bonus));
  11326. pd->bonus->type = script_getnum(st,2);
  11327. pd->bonus->val = script_getnum(st,3);
  11328. pd->bonus->duration = script_getnum(st,4);
  11329. pd->bonus->delay = script_getnum(st,5);
  11330. if (pd->state.skillbonus == 1)
  11331. pd->state.skillbonus = 0; // waiting state
  11332. // wait for timer to start
  11333. if (battle_config.pet_equip_required && pd->pet.equip == 0)
  11334. pd->bonus->timer = INVALID_TIMER;
  11335. else
  11336. pd->bonus->timer = add_timer(gettick()+pd->bonus->delay*1000, pet_skill_bonus_timer, sd->bl.id, 0);
  11337. return SCRIPT_CMD_SUCCESS;
  11338. }
  11339. /*==========================================
  11340. * pet looting [Valaris] //Rewritten by [Skotlex]
  11341. *------------------------------------------*/
  11342. BUILDIN_FUNC(petloot)
  11343. {
  11344. int max;
  11345. struct pet_data *pd;
  11346. TBL_PC *sd=script_rid2sd(st);
  11347. if(sd==NULL || sd->pd==NULL)
  11348. return 0;
  11349. max=script_getnum(st,2);
  11350. if(max < 1)
  11351. max = 1; //Let'em loot at least 1 item.
  11352. else if (max > MAX_PETLOOT_SIZE)
  11353. max = MAX_PETLOOT_SIZE;
  11354. pd = sd->pd;
  11355. if (pd->loot != NULL)
  11356. { //Release whatever was there already and reallocate memory
  11357. pet_lootitem_drop(pd, pd->master);
  11358. aFree(pd->loot->item);
  11359. }
  11360. else
  11361. pd->loot = (struct pet_loot *)aMalloc(sizeof(struct pet_loot));
  11362. pd->loot->item = (struct item *)aCalloc(max,sizeof(struct item));
  11363. pd->loot->max=max;
  11364. pd->loot->count = 0;
  11365. pd->loot->weight = 0;
  11366. return SCRIPT_CMD_SUCCESS;
  11367. }
  11368. /*==========================================
  11369. * Set arrays with info of all sd inventory :
  11370. * @inventorylist_id, @inventorylist_amount, @inventorylist_equip,
  11371. * @inventorylist_refine, @inventorylist_identify, @inventorylist_attribute,
  11372. * @inventorylist_card(0..3), @inventorylist_expire
  11373. * @inventorylist_count = scalar
  11374. *------------------------------------------*/
  11375. BUILDIN_FUNC(getinventorylist)
  11376. {
  11377. TBL_PC *sd;
  11378. char card_var[NAME_LENGTH];
  11379. int i,j=0,k;
  11380. if (!script_charid2sd(2,sd))
  11381. return SCRIPT_CMD_FAILURE;
  11382. for(i=0;i<MAX_INVENTORY;i++){
  11383. if(sd->status.inventory[i].nameid > 0 && sd->status.inventory[i].amount > 0){
  11384. pc_setreg(sd,reference_uid(add_str("@inventorylist_id"), j),sd->status.inventory[i].nameid);
  11385. pc_setreg(sd,reference_uid(add_str("@inventorylist_amount"), j),sd->status.inventory[i].amount);
  11386. pc_setreg(sd,reference_uid(add_str("@inventorylist_equip"), j),sd->status.inventory[i].equip);
  11387. pc_setreg(sd,reference_uid(add_str("@inventorylist_refine"), j),sd->status.inventory[i].refine);
  11388. pc_setreg(sd,reference_uid(add_str("@inventorylist_identify"), j),sd->status.inventory[i].identify);
  11389. pc_setreg(sd,reference_uid(add_str("@inventorylist_attribute"), j),sd->status.inventory[i].attribute);
  11390. for (k = 0; k < MAX_SLOTS; k++)
  11391. {
  11392. sprintf(card_var, "@inventorylist_card%d",k+1);
  11393. pc_setreg(sd,reference_uid(add_str(card_var), j),sd->status.inventory[i].card[k]);
  11394. }
  11395. pc_setreg(sd,reference_uid(add_str("@inventorylist_expire"), j),sd->status.inventory[i].expire_time);
  11396. pc_setreg(sd,reference_uid(add_str("@inventorylist_bound"), j),sd->status.inventory[i].bound);
  11397. j++;
  11398. }
  11399. }
  11400. pc_setreg(sd,add_str("@inventorylist_count"),j);
  11401. return SCRIPT_CMD_SUCCESS;
  11402. }
  11403. /**
  11404. * getskilllist ({<char_id>});
  11405. **/
  11406. BUILDIN_FUNC(getskilllist)
  11407. {
  11408. TBL_PC *sd;
  11409. int i,j=0;
  11410. if (!script_charid2sd(2,sd))
  11411. return SCRIPT_CMD_FAILURE;
  11412. for(i=0;i<MAX_SKILL;i++){
  11413. if(sd->status.skill[i].id > 0 && sd->status.skill[i].lv > 0){
  11414. pc_setreg(sd,reference_uid(add_str("@skilllist_id"), j),sd->status.skill[i].id);
  11415. pc_setreg(sd,reference_uid(add_str("@skilllist_lv"), j),sd->status.skill[i].lv);
  11416. pc_setreg(sd,reference_uid(add_str("@skilllist_flag"), j),sd->status.skill[i].flag);
  11417. j++;
  11418. }
  11419. }
  11420. pc_setreg(sd,add_str("@skilllist_count"),j);
  11421. return SCRIPT_CMD_SUCCESS;
  11422. }
  11423. /**
  11424. * clearitem ({<char_id>});
  11425. **/
  11426. BUILDIN_FUNC(clearitem)
  11427. {
  11428. TBL_PC *sd;
  11429. int i;
  11430. if (!script_charid2sd(2,sd))
  11431. return SCRIPT_CMD_FAILURE;
  11432. for (i=0; i<MAX_INVENTORY; i++) {
  11433. if (sd->status.inventory[i].amount) {
  11434. pc_delitem(sd, i, sd->status.inventory[i].amount, 0, 0, LOG_TYPE_SCRIPT);
  11435. }
  11436. }
  11437. return SCRIPT_CMD_SUCCESS;
  11438. }
  11439. /**
  11440. * Disguise Player (returns Mob/NPC ID if success, 0 on fail)
  11441. * disguise <Monster ID>{,<char_id>};
  11442. **/
  11443. BUILDIN_FUNC(disguise)
  11444. {
  11445. int id;
  11446. TBL_PC* sd;
  11447. if (!script_charid2sd(3,sd))
  11448. return SCRIPT_CMD_FAILURE;
  11449. id = script_getnum(st,2);
  11450. if (mobdb_checkid(id) || npcdb_checkid(id)) {
  11451. pc_disguise(sd, id);
  11452. script_pushint(st,id);
  11453. } else
  11454. script_pushint(st,0);
  11455. return SCRIPT_CMD_SUCCESS;
  11456. }
  11457. /**
  11458. * Undisguise Player (returns 1 if success, 0 on fail)
  11459. * undisguise {<char_id>};
  11460. **/
  11461. BUILDIN_FUNC(undisguise)
  11462. {
  11463. TBL_PC* sd;
  11464. if (!script_charid2sd(2,sd))
  11465. return SCRIPT_CMD_FAILURE;
  11466. if (sd->disguise) {
  11467. pc_disguise(sd, 0);
  11468. script_pushint(st,0);
  11469. } else {
  11470. script_pushint(st,1);
  11471. }
  11472. return SCRIPT_CMD_SUCCESS;
  11473. }
  11474. /*==========================================
  11475. * Transform a bl to another _class,
  11476. * @type unused
  11477. *------------------------------------------*/
  11478. BUILDIN_FUNC(classchange)
  11479. {
  11480. int _class,type;
  11481. struct block_list *bl=map_id2bl(st->oid);
  11482. if(bl==NULL) return 0;
  11483. _class=script_getnum(st,2);
  11484. type=script_getnum(st,3);
  11485. clif_class_change(bl,_class,type);
  11486. return SCRIPT_CMD_SUCCESS;
  11487. }
  11488. /*==========================================
  11489. * Display an effect
  11490. *------------------------------------------*/
  11491. BUILDIN_FUNC(misceffect)
  11492. {
  11493. int type;
  11494. type=script_getnum(st,2);
  11495. if(st->oid && st->oid != fake_nd->bl.id) {
  11496. struct block_list *bl = map_id2bl(st->oid);
  11497. if (bl)
  11498. clif_specialeffect(bl,type,AREA);
  11499. } else{
  11500. TBL_PC *sd=script_rid2sd(st);
  11501. if(sd)
  11502. clif_specialeffect(&sd->bl,type,AREA);
  11503. }
  11504. return SCRIPT_CMD_SUCCESS;
  11505. }
  11506. /*==========================================
  11507. * Play a BGM on a single client [Rikter/Yommy]
  11508. *------------------------------------------*/
  11509. BUILDIN_FUNC(playBGM)
  11510. {
  11511. struct map_session_data* sd;
  11512. if( ( sd = script_rid2sd(st) ) != NULL ) {
  11513. clif_playBGM(sd, script_getstr(st,2));
  11514. }
  11515. return SCRIPT_CMD_SUCCESS;
  11516. }
  11517. static int playBGM_sub(struct block_list* bl,va_list ap)
  11518. {
  11519. const char* name = va_arg(ap,const char*);
  11520. clif_playBGM(BL_CAST(BL_PC, bl), name);
  11521. return 0;
  11522. }
  11523. static int playBGM_foreachpc_sub(struct map_session_data* sd, va_list args)
  11524. {
  11525. const char* name = va_arg(args, const char*);
  11526. clif_playBGM(sd, name);
  11527. return 0;
  11528. }
  11529. /*==========================================
  11530. * Play a BGM on multiple client [Rikter/Yommy]
  11531. *------------------------------------------*/
  11532. BUILDIN_FUNC(playBGMall)
  11533. {
  11534. const char* name;
  11535. name = script_getstr(st,2);
  11536. if( script_hasdata(st,7) ) {// specified part of map
  11537. const char* mapname = script_getstr(st,3);
  11538. int x0 = script_getnum(st,4);
  11539. int y0 = script_getnum(st,5);
  11540. int x1 = script_getnum(st,6);
  11541. int y1 = script_getnum(st,7);
  11542. map_foreachinarea(playBGM_sub, map_mapname2mapid(mapname), x0, y0, x1, y1, BL_PC, name);
  11543. }
  11544. else if( script_hasdata(st,3) ) {// entire map
  11545. const char* mapname = script_getstr(st,3);
  11546. map_foreachinmap(playBGM_sub, map_mapname2mapid(mapname), BL_PC, name);
  11547. }
  11548. else {// entire server
  11549. map_foreachpc(&playBGM_foreachpc_sub, name);
  11550. }
  11551. return SCRIPT_CMD_SUCCESS;
  11552. }
  11553. /*==========================================
  11554. * Play a .wav sound for sd
  11555. *------------------------------------------*/
  11556. BUILDIN_FUNC(soundeffect)
  11557. {
  11558. TBL_PC* sd = script_rid2sd(st);
  11559. const char* name = script_getstr(st,2);
  11560. int type = script_getnum(st,3);
  11561. if(sd) {
  11562. clif_soundeffect(sd,&sd->bl,name,type);
  11563. }
  11564. return SCRIPT_CMD_SUCCESS;
  11565. }
  11566. int soundeffect_sub(struct block_list* bl,va_list ap)
  11567. {
  11568. char* name = va_arg(ap,char*);
  11569. int type = va_arg(ap,int);
  11570. clif_soundeffect((TBL_PC *)bl, bl, name, type);
  11571. return 0;
  11572. }
  11573. /*==========================================
  11574. * Play a sound effect (.wav) on multiple clients
  11575. * soundeffectall "<filepath>",<type>{,"<map name>"}{,<x0>,<y0>,<x1>,<y1>};
  11576. *------------------------------------------*/
  11577. BUILDIN_FUNC(soundeffectall)
  11578. {
  11579. struct block_list* bl;
  11580. const char* name;
  11581. int type;
  11582. bl = (st->rid) ? &(script_rid2sd(st)->bl) : map_id2bl(st->oid);
  11583. if (!bl)
  11584. return 0;
  11585. name = script_getstr(st,2);
  11586. type = script_getnum(st,3);
  11587. //FIXME: enumerating map squares (map_foreach) is slower than enumerating the list of online players (map_foreachpc?) [ultramage]
  11588. if(!script_hasdata(st,4))
  11589. { // area around
  11590. clif_soundeffectall(bl, name, type, AREA);
  11591. }
  11592. else
  11593. if(!script_hasdata(st,5))
  11594. { // entire map
  11595. const char* mapname = script_getstr(st,4);
  11596. map_foreachinmap(soundeffect_sub, map_mapname2mapid(mapname), BL_PC, name, type);
  11597. }
  11598. else
  11599. if(script_hasdata(st,8))
  11600. { // specified part of map
  11601. const char* mapname = script_getstr(st,4);
  11602. int x0 = script_getnum(st,5);
  11603. int y0 = script_getnum(st,6);
  11604. int x1 = script_getnum(st,7);
  11605. int y1 = script_getnum(st,8);
  11606. map_foreachinarea(soundeffect_sub, map_mapname2mapid(mapname), x0, y0, x1, y1, BL_PC, name, type);
  11607. }
  11608. else
  11609. {
  11610. ShowError("buildin_soundeffectall: insufficient arguments for specific area broadcast.\n");
  11611. }
  11612. return SCRIPT_CMD_SUCCESS;
  11613. }
  11614. /*==========================================
  11615. * pet status recovery [Valaris] / Rewritten by [Skotlex]
  11616. *------------------------------------------*/
  11617. BUILDIN_FUNC(petrecovery)
  11618. {
  11619. struct pet_data *pd;
  11620. TBL_PC *sd=script_rid2sd(st);
  11621. int sc;
  11622. if(sd == NULL || sd->pd == NULL)
  11623. return SCRIPT_CMD_FAILURE;
  11624. sc = script_getnum(st,2);
  11625. if (sc <= SC_NONE || sc >= SC_MAX) {
  11626. ShowError("buildin_petrecovery: Invalid SC type: %d\n", sc);
  11627. return SCRIPT_CMD_FAILURE;
  11628. }
  11629. pd = sd->pd;
  11630. if (pd->recovery)
  11631. { //Halt previous bonus
  11632. if (pd->recovery->timer != INVALID_TIMER)
  11633. delete_timer(pd->recovery->timer, pet_recovery_timer);
  11634. } else //Init
  11635. pd->recovery = (struct pet_recovery *)aMalloc(sizeof(struct pet_recovery));
  11636. pd->recovery->type = (sc_type)sc;
  11637. pd->recovery->delay = script_getnum(st,3);
  11638. pd->recovery->timer = INVALID_TIMER;
  11639. return SCRIPT_CMD_SUCCESS;
  11640. }
  11641. /*==========================================
  11642. * pet healing [Valaris] //Rewritten by [Skotlex]
  11643. *------------------------------------------*/
  11644. BUILDIN_FUNC(petheal)
  11645. {
  11646. struct pet_data *pd;
  11647. TBL_PC *sd=script_rid2sd(st);
  11648. if(sd==NULL || sd->pd==NULL)
  11649. return 0;
  11650. pd=sd->pd;
  11651. if (pd->s_skill)
  11652. { //Clear previous skill
  11653. if (pd->s_skill->timer != INVALID_TIMER)
  11654. {
  11655. if (pd->s_skill->id)
  11656. delete_timer(pd->s_skill->timer, pet_skill_support_timer);
  11657. else
  11658. delete_timer(pd->s_skill->timer, pet_heal_timer);
  11659. }
  11660. } else //init memory
  11661. pd->s_skill = (struct pet_skill_support *) aMalloc(sizeof(struct pet_skill_support));
  11662. pd->s_skill->id=0; //This id identifies that it IS petheal rather than pet_skillsupport
  11663. //Use the lv as the amount to heal
  11664. pd->s_skill->lv=script_getnum(st,2);
  11665. pd->s_skill->delay=script_getnum(st,3);
  11666. pd->s_skill->hp=script_getnum(st,4);
  11667. pd->s_skill->sp=script_getnum(st,5);
  11668. //Use delay as initial offset to avoid skill/heal exploits
  11669. if (battle_config.pet_equip_required && pd->pet.equip == 0)
  11670. pd->s_skill->timer = INVALID_TIMER;
  11671. else
  11672. pd->s_skill->timer = add_timer(gettick()+pd->s_skill->delay*1000,pet_heal_timer,sd->bl.id,0);
  11673. return SCRIPT_CMD_SUCCESS;
  11674. }
  11675. /*==========================================
  11676. * pet attack skills [Valaris] //Rewritten by [Skotlex]
  11677. *------------------------------------------*/
  11678. /// petskillattack <skill id>,<level>,<rate>,<bonusrate>
  11679. /// petskillattack "<skill name>",<level>,<rate>,<bonusrate>
  11680. BUILDIN_FUNC(petskillattack)
  11681. {
  11682. struct pet_data *pd;
  11683. struct script_data *data;
  11684. TBL_PC *sd = script_rid2sd(st);
  11685. int id = 0;
  11686. if(sd == NULL || sd->pd == NULL)
  11687. return SCRIPT_CMD_FAILURE;
  11688. data = script_getdata(st, 2);
  11689. get_val(st, data);
  11690. id = (data_isstring(data) ? skill_name2id(script_getstr(st,2)) : skill_get_index(script_getnum(st,2)));
  11691. if (!id) {
  11692. ShowError("buildin_petskillattack: Invalid skill defined!\n");
  11693. return SCRIPT_CMD_FAILURE;
  11694. }
  11695. pd = sd->pd;
  11696. if (pd->a_skill == NULL)
  11697. pd->a_skill = (struct pet_skill_attack *)aMalloc(sizeof(struct pet_skill_attack));
  11698. pd->a_skill->id = id;
  11699. pd->a_skill->damage = 0;
  11700. pd->a_skill->lv = (unsigned short)min(script_getnum(st,3), skill_get_max(pd->a_skill->id));
  11701. pd->a_skill->div_ = 0;
  11702. pd->a_skill->rate = script_getnum(st,4);
  11703. pd->a_skill->bonusrate = script_getnum(st,5);
  11704. return SCRIPT_CMD_SUCCESS;
  11705. }
  11706. /*==========================================
  11707. * pet attack skills [Valaris]
  11708. *------------------------------------------*/
  11709. /// petskillattack2 <skill id>,<damage>,<div>,<rate>,<bonusrate>
  11710. /// petskillattack2 "<skill name>",<damage>,<div>,<rate>,<bonusrate>
  11711. BUILDIN_FUNC(petskillattack2)
  11712. {
  11713. struct pet_data *pd;
  11714. struct script_data *data;
  11715. TBL_PC *sd = script_rid2sd(st);
  11716. int id = 0;
  11717. if(sd == NULL || sd->pd == NULL)
  11718. return SCRIPT_CMD_FAILURE;
  11719. data = script_getdata(st, 2);
  11720. get_val(st, data);
  11721. id = (data_isstring(data) ? skill_name2id(script_getstr(st,2)) : skill_get_index(script_getnum(st,2)));
  11722. if (!id) {
  11723. ShowError("buildin_petskillattack2: Invalid skill defined!\n");
  11724. return SCRIPT_CMD_FAILURE;
  11725. }
  11726. pd = sd->pd;
  11727. if (pd->a_skill == NULL)
  11728. pd->a_skill = (struct pet_skill_attack *)aMalloc(sizeof(struct pet_skill_attack));
  11729. pd->a_skill->id = id;
  11730. pd->a_skill->damage = script_getnum(st,3); // Fixed damage
  11731. pd->a_skill->lv = (unsigned short)skill_get_max(pd->a_skill->id); // Adjust to max skill level
  11732. pd->a_skill->div_ = script_getnum(st,4);
  11733. pd->a_skill->rate = script_getnum(st,5);
  11734. pd->a_skill->bonusrate = script_getnum(st,6);
  11735. return SCRIPT_CMD_SUCCESS;
  11736. }
  11737. /*==========================================
  11738. * pet support skills [Skotlex]
  11739. *------------------------------------------*/
  11740. /// petskillsupport <skill id>,<level>,<delay>,<hp>,<sp>
  11741. /// petskillsupport "<skill name>",<level>,<delay>,<hp>,<sp>
  11742. BUILDIN_FUNC(petskillsupport)
  11743. {
  11744. struct pet_data *pd;
  11745. struct script_data *data;
  11746. TBL_PC *sd = script_rid2sd(st);
  11747. int id = 0;
  11748. if(sd == NULL || sd->pd == NULL)
  11749. return SCRIPT_CMD_FAILURE;
  11750. data = script_getdata(st, 2);
  11751. get_val(st, data);
  11752. id = (data_isstring(data) ? skill_name2id(script_getstr(st,2)) : skill_get_index(script_getnum(st,2)));
  11753. if (!id) {
  11754. ShowError("buildin_petskillsupport: Invalid skill defined!\n");
  11755. return SCRIPT_CMD_FAILURE;
  11756. }
  11757. pd = sd->pd;
  11758. if (pd->s_skill)
  11759. { //Clear previous skill
  11760. if (pd->s_skill->timer != INVALID_TIMER)
  11761. {
  11762. if (pd->s_skill->id)
  11763. delete_timer(pd->s_skill->timer, pet_skill_support_timer);
  11764. else
  11765. delete_timer(pd->s_skill->timer, pet_heal_timer);
  11766. }
  11767. } else //init memory
  11768. pd->s_skill = (struct pet_skill_support *) aMalloc(sizeof(struct pet_skill_support));
  11769. pd->s_skill->id = id;
  11770. pd->s_skill->lv = script_getnum(st,3);
  11771. pd->s_skill->delay = script_getnum(st,4);
  11772. pd->s_skill->hp = script_getnum(st,5);
  11773. pd->s_skill->sp = script_getnum(st,6);
  11774. //Use delay as initial offset to avoid skill/heal exploits
  11775. if (battle_config.pet_equip_required && pd->pet.equip == 0)
  11776. pd->s_skill->timer = INVALID_TIMER;
  11777. else
  11778. pd->s_skill->timer = add_timer(gettick()+pd->s_skill->delay*1000,pet_skill_support_timer,sd->bl.id,0);
  11779. return SCRIPT_CMD_SUCCESS;
  11780. }
  11781. /*==========================================
  11782. * Scripted skill effects [Celest]
  11783. *------------------------------------------*/
  11784. /// skilleffect <skill id>,<level>
  11785. /// skilleffect "<skill name>",<level>
  11786. BUILDIN_FUNC(skilleffect)
  11787. {
  11788. TBL_PC *sd;
  11789. uint16 skill_id, skill_lv;
  11790. struct script_data *data = script_getdata(st, 2);
  11791. get_val(st, data); // Convert into value in case of a variable
  11792. skill_id = ( data_isstring(data) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  11793. skill_lv = script_getnum(st,3);
  11794. sd = script_rid2sd(st);
  11795. /* Ensure we're standing because the following packet causes the client to virtually set the char to stand,
  11796. * which leaves the server thinking it still is sitting. */
  11797. if( pc_issit(sd) && pc_setstand(sd, false) ) {
  11798. skill_sit(sd, 0);
  11799. clif_standing(&sd->bl);
  11800. }
  11801. clif_skill_nodamage(&sd->bl,&sd->bl,skill_id,skill_lv,1);
  11802. return SCRIPT_CMD_SUCCESS;
  11803. }
  11804. /*==========================================
  11805. * NPC skill effects [Valaris]
  11806. *------------------------------------------*/
  11807. /// npcskilleffect <skill id>,<level>,<x>,<y>
  11808. /// npcskilleffect "<skill name>",<level>,<x>,<y>
  11809. BUILDIN_FUNC(npcskilleffect)
  11810. {
  11811. struct block_list *bl= map_id2bl(st->oid);
  11812. uint16 skill_id, skill_lv;
  11813. int x, y;
  11814. struct script_data *data = script_getdata(st, 2);
  11815. get_val(st, data); // Convert into value in case of a variable
  11816. skill_id=( data_isstring(data) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  11817. skill_lv=script_getnum(st,3);
  11818. x=script_getnum(st,4);
  11819. y=script_getnum(st,5);
  11820. if (bl)
  11821. clif_skill_poseffect(bl,skill_id,skill_lv,x,y,gettick());
  11822. return SCRIPT_CMD_SUCCESS;
  11823. }
  11824. /*==========================================
  11825. * Special effects [Valaris]
  11826. *------------------------------------------*/
  11827. BUILDIN_FUNC(specialeffect)
  11828. {
  11829. struct block_list *bl=map_id2bl(st->oid);
  11830. int type = script_getnum(st,2);
  11831. enum send_target target = script_hasdata(st,3) ? (send_target)script_getnum(st,3) : AREA;
  11832. if(bl==NULL)
  11833. return 0;
  11834. if( script_hasdata(st,4) )
  11835. {
  11836. TBL_NPC *nd = npc_name2id(script_getstr(st,4));
  11837. if(nd)
  11838. clif_specialeffect(&nd->bl, type, target);
  11839. }
  11840. else
  11841. {
  11842. if (target == SELF) {
  11843. TBL_PC *sd=script_rid2sd(st);
  11844. if (sd)
  11845. clif_specialeffect_single(bl,type,sd->fd);
  11846. } else {
  11847. clif_specialeffect(bl, type, target);
  11848. }
  11849. }
  11850. return SCRIPT_CMD_SUCCESS;
  11851. }
  11852. BUILDIN_FUNC(specialeffect2)
  11853. {
  11854. TBL_PC *sd=script_rid2sd(st);
  11855. int type = script_getnum(st,2);
  11856. enum send_target target = script_hasdata(st,3) ? (send_target)script_getnum(st,3) : AREA;
  11857. if( script_hasdata(st,4) )
  11858. sd = map_nick2sd(script_getstr(st,4));
  11859. if (sd)
  11860. clif_specialeffect(&sd->bl, type, target);
  11861. return SCRIPT_CMD_SUCCESS;
  11862. }
  11863. /**
  11864. * nude({<char_id>});
  11865. * @author [Valaris]
  11866. **/
  11867. BUILDIN_FUNC(nude)
  11868. {
  11869. TBL_PC *sd;
  11870. int i, calcflag = 0;
  11871. if (!script_charid2sd(2,sd))
  11872. return SCRIPT_CMD_FAILURE;
  11873. for( i = 0 ; i < EQI_MAX; i++ ) {
  11874. if( sd->equip_index[ i ] >= 0 ) {
  11875. if( !calcflag )
  11876. calcflag = 1;
  11877. pc_unequipitem( sd , sd->equip_index[ i ] , 2);
  11878. }
  11879. }
  11880. if( calcflag )
  11881. status_calc_pc(sd,SCO_NONE);
  11882. return SCRIPT_CMD_SUCCESS;
  11883. }
  11884. int atcommand_sub(struct script_state* st,int type) {
  11885. TBL_PC *sd, dummy_sd;
  11886. int fd;
  11887. const char *cmd;
  11888. cmd = script_getstr(st,2);
  11889. if (st->rid) {
  11890. sd = script_rid2sd(st);
  11891. fd = sd->fd;
  11892. } else { //Use a dummy character.
  11893. sd = &dummy_sd;
  11894. fd = 0;
  11895. memset(&dummy_sd, 0, sizeof(TBL_PC));
  11896. if (st->oid) {
  11897. struct block_list* bl = map_id2bl(st->oid);
  11898. memcpy(&dummy_sd.bl, bl, sizeof(struct block_list));
  11899. if (bl->type == BL_NPC)
  11900. safestrncpy(dummy_sd.status.name, ((TBL_NPC*)bl)->name, NAME_LENGTH);
  11901. sd->mapindex = (bl->m > 0) ? bl->m : mapindex_name2id(map_default.mapname);
  11902. }
  11903. // Init Group ID, Level, & permissions
  11904. sd->group_id = sd->group_level = 99;
  11905. sd->permissions |= PC_PERM_ALLPERMISSION;
  11906. }
  11907. if (!is_atcommand(fd, sd, cmd, type)) {
  11908. ShowWarning("buildin_atcommand: failed to execute command '%s'\n", cmd);
  11909. script_reportsrc(st);
  11910. return SCRIPT_CMD_FAILURE;
  11911. }
  11912. return SCRIPT_CMD_SUCCESS;
  11913. }
  11914. /*==========================================
  11915. * gmcommand [MouseJstr]
  11916. *------------------------------------------*/
  11917. BUILDIN_FUNC(atcommand) {
  11918. return atcommand_sub(st,0);
  11919. }
  11920. /** Displays a message for the player only (like system messages like "you got an apple" )
  11921. * dispbottom("<message>"{,<color>{,<char_id>}})
  11922. * @param message
  11923. * @param color Hex color default (Green)
  11924. */
  11925. BUILDIN_FUNC(dispbottom)
  11926. {
  11927. TBL_PC *sd;
  11928. int color = 0;
  11929. const char *message;
  11930. if (!script_charid2sd(4,sd))
  11931. return SCRIPT_CMD_FAILURE;
  11932. message = script_getstr(st,2);
  11933. if (script_hasdata(st,3))
  11934. color = script_getnum(st,3); // <color>
  11935. if(sd) {
  11936. if (script_hasdata(st,3))
  11937. clif_messagecolor2(sd, color, message); // [Napster]
  11938. else
  11939. clif_disp_onlyself(sd, message, (int)strlen(message));
  11940. }
  11941. return SCRIPT_CMD_SUCCESS;
  11942. }
  11943. /*===================================
  11944. * Heal portion of recovery command
  11945. *-----------------------------------*/
  11946. int recovery_sub(struct map_session_data* sd, int revive)
  11947. {
  11948. if(revive&(1|4) && pc_isdead(sd)) {
  11949. status_revive(&sd->bl, 100, 100);
  11950. clif_displaymessage(sd->fd,msg_txt(sd,16)); // You've been revived!
  11951. clif_specialeffect(&sd->bl, 77, AREA);
  11952. } else if(revive&(1|2) && !pc_isdead(sd)) {
  11953. status_percent_heal(&sd->bl, 100, 100);
  11954. clif_displaymessage(sd->fd,msg_txt(sd,680)); // You have been recovered!
  11955. }
  11956. return SCRIPT_CMD_SUCCESS;
  11957. }
  11958. /*=========================================================================
  11959. * Fully Recover a Character's HP/SP - [Capuche] & [Akinari]
  11960. * recovery <type>{,<option>,<revive_flag>{,<map name>}};
  11961. * <type> determines <option>:
  11962. * 0 : char_id
  11963. * 1 : party_id
  11964. * 2 : guild_id
  11965. * 3 : map_name
  11966. * 4 : all characters
  11967. * <revive_flag>:
  11968. * 1 : Revive and heal all players (default)
  11969. * 2 : Heal living players only
  11970. * 4 : Revive dead players only
  11971. * <map name>:
  11972. * for types 1-2 : map_name (null = all maps)
  11973. *-------------------------------------------------------------------------*/
  11974. BUILDIN_FUNC(recovery)
  11975. {
  11976. TBL_PC *sd;
  11977. int map_idx = 0, type = 0, revive = 1;
  11978. type = script_getnum(st,2);
  11979. if(script_hasdata(st,4))
  11980. revive = script_getnum(st,4);
  11981. switch(type) {
  11982. case 0:
  11983. if(script_hasdata(st,3))
  11984. sd=map_charid2sd(script_getnum(st,3));
  11985. else if((sd = script_rid2sd(st)) == NULL)
  11986. return 0; //If we don't have sd by now, bail out
  11987. recovery_sub(sd, revive);
  11988. break;
  11989. case 1:
  11990. {
  11991. struct party_data* p;
  11992. //When no party given, we use invoker party
  11993. int p_id = 0, i;
  11994. if(script_hasdata(st,5)) {//Bad maps shouldn't cause issues
  11995. map_idx = map_mapname2mapid(script_getstr(st,5));
  11996. if(map_idx < 1) { //But we'll check anyways
  11997. ShowDebug("recovery: bad map name given (%s)\n", script_getstr(st,5));
  11998. return 1;
  11999. }
  12000. }
  12001. if(script_hasdata(st,3))
  12002. p_id = script_getnum(st,3);
  12003. else if((sd = script_rid2sd(st)))
  12004. p_id = sd->status.party_id;
  12005. p = party_search(p_id);
  12006. if(p == NULL)
  12007. return 0;
  12008. for (i = 0; i < MAX_PARTY; i++) {
  12009. struct map_session_data* pl_sd;
  12010. if((!(pl_sd = p->data[i].sd) || pl_sd->status.party_id != p_id)
  12011. || (map_idx && pl_sd->bl.m != map_idx))
  12012. continue;
  12013. recovery_sub(pl_sd, revive);
  12014. }
  12015. break;
  12016. }
  12017. case 2:
  12018. {
  12019. struct guild* g;
  12020. //When no guild given, we use invoker guild
  12021. int g_id = 0, i;
  12022. if(script_hasdata(st,5)) {//Bad maps shouldn't cause issues
  12023. map_idx = map_mapname2mapid(script_getstr(st,5));
  12024. if(map_idx < 1) { //But we'll check anyways
  12025. ShowDebug("recovery: bad map name given (%s)\n", script_getstr(st,5));
  12026. return 1;
  12027. }
  12028. }
  12029. if(script_hasdata(st,3))
  12030. g_id = script_getnum(st,3);
  12031. else if((sd = script_rid2sd(st)))
  12032. g_id = sd->status.guild_id;
  12033. g = guild_search(g_id);
  12034. if(g == NULL)
  12035. return 0;
  12036. for (i = 0; i < MAX_GUILD; i++) {
  12037. struct map_session_data* pl_sd;
  12038. if((!(pl_sd = g->member[i].sd) || pl_sd->status.guild_id != g_id)
  12039. || (map_idx && pl_sd->bl.m != map_idx))
  12040. continue;
  12041. recovery_sub(pl_sd, revive);
  12042. }
  12043. break;
  12044. }
  12045. case 3:
  12046. if(script_hasdata(st,3))
  12047. map_idx = map_mapname2mapid(script_getstr(st,3));
  12048. else if((sd = script_rid2sd(st)))
  12049. map_idx = sd->bl.m;
  12050. if(map_idx < 1)
  12051. return 1; //No sd and no map given - return
  12052. case 4:
  12053. {
  12054. struct s_mapiterator *iter;
  12055. struct script_data *data = script_getdata(st, 3);
  12056. get_val(st, data); // Convert into value in case of a variable
  12057. if(script_hasdata(st,3) && !data_isstring(data))
  12058. revive = script_getnum(st,3); // recovery 4,<revive_flag>;
  12059. iter = mapit_getallusers();
  12060. for (sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); sd = (TBL_PC*)mapit_next(iter)) {
  12061. if(type == 3 && sd->bl.m != map_idx)
  12062. continue;
  12063. recovery_sub(sd, revive);
  12064. }
  12065. mapit_free(iter);
  12066. break;
  12067. }
  12068. default:
  12069. ShowWarning("script: buildin_recovery: Invalid type %d\n", type);
  12070. script_pushint(st,-1);
  12071. return 1;
  12072. }
  12073. script_pushint(st,1); //Successfully executed without errors
  12074. return SCRIPT_CMD_SUCCESS;
  12075. }
  12076. /*==========================================
  12077. * Get your pet info: getpetinfo(n)
  12078. * n -> 0:pet_id 1:pet_class 2:pet_name
  12079. * 3:friendly 4:hungry, 5: rename flag.6:level
  12080. *------------------------------------------*/
  12081. BUILDIN_FUNC(getpetinfo)
  12082. {
  12083. TBL_PC *sd=script_rid2sd(st);
  12084. TBL_PET *pd;
  12085. int type=script_getnum(st,2);
  12086. if(!sd || !sd->pd) {
  12087. if (type == 2)
  12088. script_pushconststr(st,"null");
  12089. else
  12090. script_pushint(st,0);
  12091. return 0;
  12092. }
  12093. pd = sd->pd;
  12094. switch(type){
  12095. case 0: script_pushint(st,pd->pet.pet_id); break;
  12096. case 1: script_pushint(st,pd->pet.class_); break;
  12097. case 2: script_pushstrcopy(st,pd->pet.name); break;
  12098. case 3: script_pushint(st,pd->pet.intimate); break;
  12099. case 4: script_pushint(st,pd->pet.hungry); break;
  12100. case 5: script_pushint(st,pd->pet.rename_flag); break;
  12101. case 6: script_pushint(st,(int)pd->pet.level); break;
  12102. default:
  12103. script_pushint(st,0);
  12104. break;
  12105. }
  12106. return SCRIPT_CMD_SUCCESS;
  12107. }
  12108. /*==========================================
  12109. * Get your homunculus info: gethominfo(n)
  12110. * n -> 0:hom_id 1:class 2:name
  12111. * 3:friendly 4:hungry, 5: rename flag.
  12112. * 6: level
  12113. *------------------------------------------*/
  12114. BUILDIN_FUNC(gethominfo)
  12115. {
  12116. TBL_PC *sd=script_rid2sd(st);
  12117. TBL_HOM *hd;
  12118. int type=script_getnum(st,2);
  12119. hd = sd?sd->hd:NULL;
  12120. if(!hd) {
  12121. if (type == 2)
  12122. script_pushconststr(st,"null");
  12123. else
  12124. script_pushint(st,0);
  12125. return 0;
  12126. }
  12127. switch(type){
  12128. case 0: script_pushint(st,hd->homunculus.hom_id); break;
  12129. case 1: script_pushint(st,hd->homunculus.class_); break;
  12130. case 2: script_pushstrcopy(st,hd->homunculus.name); break;
  12131. case 3: script_pushint(st,hd->homunculus.intimacy); break;
  12132. case 4: script_pushint(st,hd->homunculus.hunger); break;
  12133. case 5: script_pushint(st,hd->homunculus.rename_flag); break;
  12134. case 6: script_pushint(st,hd->homunculus.level); break;
  12135. default:
  12136. script_pushint(st,0);
  12137. break;
  12138. }
  12139. return SCRIPT_CMD_SUCCESS;
  12140. }
  12141. /// Retrieves information about character's mercenary
  12142. /// getmercinfo <type>[,<char id>];
  12143. BUILDIN_FUNC(getmercinfo)
  12144. {
  12145. int type;
  12146. struct map_session_data* sd;
  12147. struct mercenary_data* md;
  12148. type = script_getnum(st,2);
  12149. if( script_hasdata(st,3) )
  12150. {
  12151. int char_id = script_getnum(st,3);
  12152. if( ( sd = map_charid2sd(char_id) ) == NULL )
  12153. {
  12154. ShowError("buildin_getmercinfo: No such character (char_id=%d).\n", char_id);
  12155. script_pushnil(st);
  12156. return SCRIPT_CMD_FAILURE;
  12157. }
  12158. }
  12159. else
  12160. {
  12161. if( ( sd = script_rid2sd(st) ) == NULL )
  12162. {
  12163. script_pushnil(st);
  12164. return SCRIPT_CMD_SUCCESS;
  12165. }
  12166. }
  12167. md = ( sd->status.mer_id && sd->md ) ? sd->md : NULL;
  12168. switch( type )
  12169. {
  12170. case 0: script_pushint(st,md ? md->mercenary.mercenary_id : 0); break;
  12171. case 1: script_pushint(st,md ? md->mercenary.class_ : 0); break;
  12172. case 2:
  12173. if( md )
  12174. script_pushstrcopy(st,md->db->name);
  12175. else
  12176. script_pushconststr(st,"");
  12177. break;
  12178. case 3: script_pushint(st,md ? mercenary_get_faith(md) : 0); break;
  12179. case 4: script_pushint(st,md ? mercenary_get_calls(md) : 0); break;
  12180. case 5: script_pushint(st,md ? md->mercenary.kill_count : 0); break;
  12181. case 6: script_pushint(st,md ? mercenary_get_lifetime(md) : 0); break;
  12182. case 7: script_pushint(st,md ? md->db->lv : 0); break;
  12183. default:
  12184. ShowError("buildin_getmercinfo: Invalid type %d (char_id=%d).\n", type, sd->status.char_id);
  12185. script_pushnil(st);
  12186. return SCRIPT_CMD_FAILURE;
  12187. }
  12188. return SCRIPT_CMD_SUCCESS;
  12189. }
  12190. /*==========================================
  12191. * Shows wether your inventory(and equips) contain
  12192. selected card or not.
  12193. checkequipedcard(4001);
  12194. *------------------------------------------*/
  12195. BUILDIN_FUNC(checkequipedcard)
  12196. {
  12197. TBL_PC *sd=script_rid2sd(st);
  12198. if(sd){
  12199. int n,i,c=0;
  12200. c=script_getnum(st,2);
  12201. for(i=0;i<MAX_INVENTORY;i++){
  12202. if(sd->status.inventory[i].nameid > 0 && sd->status.inventory[i].amount && sd->inventory_data[i]){
  12203. if (itemdb_isspecial(sd->status.inventory[i].card[0]))
  12204. continue;
  12205. for(n=0;n<sd->inventory_data[i]->slot;n++){
  12206. if(sd->status.inventory[i].card[n]==c){
  12207. script_pushint(st,1);
  12208. return 0;
  12209. }
  12210. }
  12211. }
  12212. }
  12213. }
  12214. script_pushint(st,0);
  12215. return SCRIPT_CMD_SUCCESS;
  12216. }
  12217. BUILDIN_FUNC(jump_zero)
  12218. {
  12219. int sel;
  12220. sel=script_getnum(st,2);
  12221. if(!sel) {
  12222. int pos;
  12223. if( !data_islabel(script_getdata(st,3)) ){
  12224. ShowError("script: jump_zero: not label !\n");
  12225. st->state=END;
  12226. return 1;
  12227. }
  12228. pos=script_getnum(st,3);
  12229. st->pos=pos;
  12230. st->state=GOTO;
  12231. }
  12232. return SCRIPT_CMD_SUCCESS;
  12233. }
  12234. /*==========================================
  12235. * movenpc [MouseJstr]
  12236. *------------------------------------------*/
  12237. BUILDIN_FUNC(movenpc)
  12238. {
  12239. TBL_NPC *nd = NULL;
  12240. const char *npc;
  12241. int x,y;
  12242. npc = script_getstr(st,2);
  12243. x = script_getnum(st,3);
  12244. y = script_getnum(st,4);
  12245. if ((nd = npc_name2id(npc)) == NULL){
  12246. ShowError("script: movenpc: NPC with ID '%s' was not found!\n", npc );
  12247. return -1;
  12248. }
  12249. if (script_hasdata(st,5))
  12250. nd->ud.dir = script_getnum(st,5) % 8;
  12251. npc_movenpc(nd, x, y);
  12252. return SCRIPT_CMD_SUCCESS;
  12253. }
  12254. /*==========================================
  12255. * message [MouseJstr]
  12256. *------------------------------------------*/
  12257. BUILDIN_FUNC(message)
  12258. {
  12259. const char *msg,*player;
  12260. TBL_PC *pl_sd = NULL;
  12261. player = script_getstr(st,2);
  12262. msg = script_getstr(st,3);
  12263. if((pl_sd=map_nick2sd((char *) player)) == NULL)
  12264. return 0;
  12265. clif_displaymessage(pl_sd->fd, msg);
  12266. return SCRIPT_CMD_SUCCESS;
  12267. }
  12268. /*==========================================
  12269. * npctalk (sends message to surrounding area)
  12270. *------------------------------------------*/
  12271. BUILDIN_FUNC(npctalk)
  12272. {
  12273. const char* str;
  12274. struct npc_data* nd = (struct npc_data *)map_id2bl(st->oid);
  12275. str = script_getstr(st,2);
  12276. if(nd)
  12277. {
  12278. char name[NAME_LENGTH], message[256];
  12279. safestrncpy(name, nd->name, sizeof(name));
  12280. strtok(name, "#"); // discard extra name identifier if present
  12281. safesnprintf(message, sizeof(message), "%s : %s", name, str);
  12282. clif_disp_overhead(&nd->bl, message);
  12283. }
  12284. return SCRIPT_CMD_SUCCESS;
  12285. }
  12286. // change npc walkspeed [Valaris]
  12287. BUILDIN_FUNC(npcspeed)
  12288. {
  12289. struct npc_data* nd;
  12290. int speed;
  12291. speed = script_getnum(st,2);
  12292. nd =(struct npc_data *)map_id2bl(st->oid);
  12293. if( nd ) {
  12294. nd->speed = speed;
  12295. nd->ud.state.speed_changed = 1;
  12296. }
  12297. return SCRIPT_CMD_SUCCESS;
  12298. }
  12299. // make an npc walk to a position [Valaris]
  12300. BUILDIN_FUNC(npcwalkto)
  12301. {
  12302. struct npc_data *nd=(struct npc_data *)map_id2bl(st->oid);
  12303. int x=0,y=0;
  12304. x=script_getnum(st,2);
  12305. y=script_getnum(st,3);
  12306. if(nd) {
  12307. if (!nd->status.hp)
  12308. status_calc_npc(nd, SCO_FIRST);
  12309. else
  12310. status_calc_npc(nd, SCO_NONE);
  12311. unit_walktoxy(&nd->bl,x,y,0);
  12312. }
  12313. return SCRIPT_CMD_SUCCESS;
  12314. }
  12315. // stop an npc's movement [Valaris]
  12316. BUILDIN_FUNC(npcstop)
  12317. {
  12318. struct npc_data *nd=(struct npc_data *)map_id2bl(st->oid);
  12319. if(nd) {
  12320. unit_stop_walking(&nd->bl,1|4);
  12321. }
  12322. return SCRIPT_CMD_SUCCESS;
  12323. }
  12324. /**
  12325. * getlook(<type>{,<char_id>})
  12326. **/
  12327. BUILDIN_FUNC(getlook)
  12328. {
  12329. int type,val;
  12330. TBL_PC *sd;
  12331. if (!script_charid2sd(3,sd)) {
  12332. script_pushint(st,-1);
  12333. return SCRIPT_CMD_FAILURE;
  12334. }
  12335. type=script_getnum(st,2);
  12336. val=-1;
  12337. switch(type) {
  12338. case LOOK_HAIR: val=sd->status.hair; break; //1
  12339. case LOOK_WEAPON: val=sd->status.weapon; break; //2
  12340. case LOOK_HEAD_BOTTOM: val=sd->status.head_bottom; break; //3
  12341. case LOOK_HEAD_TOP: val=sd->status.head_top; break; //4
  12342. case LOOK_HEAD_MID: val=sd->status.head_mid; break; //5
  12343. case LOOK_HAIR_COLOR: val=sd->status.hair_color; break; //6
  12344. case LOOK_CLOTHES_COLOR: val=sd->status.clothes_color; break; //7
  12345. case LOOK_SHIELD: val=sd->status.shield; break; //8
  12346. case LOOK_SHOES: break; //9
  12347. case LOOK_ROBE: val=sd->status.robe; break; //12
  12348. }
  12349. script_pushint(st,val);
  12350. return SCRIPT_CMD_SUCCESS;
  12351. }
  12352. /**
  12353. * getsavepoint(<information type>{,<char_id>})
  12354. * @param type 0- map name, 1- x, 2- y
  12355. **/
  12356. BUILDIN_FUNC(getsavepoint)
  12357. {
  12358. TBL_PC* sd;
  12359. int type;
  12360. if (!script_charid2sd(3,sd)) {
  12361. script_pushint(st,0);
  12362. return 0;
  12363. }
  12364. type = script_getnum(st,2);
  12365. switch(type) {
  12366. case 0: script_pushstrcopy(st,mapindex_id2name(sd->status.save_point.map)); break;
  12367. case 1: script_pushint(st,sd->status.save_point.x); break;
  12368. case 2: script_pushint(st,sd->status.save_point.y); break;
  12369. default:
  12370. script_pushint(st,0);
  12371. break;
  12372. }
  12373. return SCRIPT_CMD_SUCCESS;
  12374. }
  12375. /*==========================================
  12376. * Get position for char/NPC/pet/hom/merc/elem objects. Added by Lorky
  12377. *
  12378. * int getMapXY(MapName$,MapX,MapY,type,[CharName$]);
  12379. * where type:
  12380. * MapName$ - String variable for output map name
  12381. * MapX - Integer variable for output coord X
  12382. * MapY - Integer variable for output coord Y
  12383. * type - type of object
  12384. * 0 - Character coord
  12385. * 1 - NPC coord
  12386. * 2 - Pet coord
  12387. * 3 - Mob coord (see 'getunitdata')
  12388. * 4 - Homun coord
  12389. * 5 - Mercenary coord
  12390. * 6 - Elemental coord
  12391. * CharName$ - Name object. If miss or "this" the current object
  12392. *
  12393. * Return:
  12394. * 0 - success
  12395. * -1 - some error, MapName$,MapX,MapY contains unknown value.
  12396. *------------------------------------------*/
  12397. BUILDIN_FUNC(getmapxy)
  12398. {
  12399. struct block_list *bl = NULL;
  12400. TBL_PC *sd=NULL;
  12401. int num;
  12402. const char *name;
  12403. char prefix;
  12404. int x,y,type;
  12405. char mapname[MAP_NAME_LENGTH];
  12406. if( !data_isreference(script_getdata(st,2)) ){
  12407. ShowWarning("script: buildin_getmapxy: not mapname variable\n");
  12408. script_pushint(st,-1);
  12409. return 1;
  12410. }
  12411. if( !data_isreference(script_getdata(st,3)) ){
  12412. ShowWarning("script: buildin_getmapxy: not mapx variable\n");
  12413. script_pushint(st,-1);
  12414. return 1;
  12415. }
  12416. if( !data_isreference(script_getdata(st,4)) ){
  12417. ShowWarning("script: buildin_getmapxy: not mapy variable\n");
  12418. script_pushint(st,-1);
  12419. return 1;
  12420. }
  12421. // Possible needly check function parameters on C_STR,C_INT,C_INT
  12422. type=script_getnum(st,5);
  12423. switch (type){
  12424. case 0: //Get Character Position
  12425. if( script_hasdata(st,6) )
  12426. sd=map_nick2sd(script_getstr(st,6));
  12427. else
  12428. sd=script_rid2sd(st);
  12429. if (sd)
  12430. bl = &sd->bl;
  12431. break;
  12432. case 1: //Get NPC Position
  12433. if( script_hasdata(st,6) )
  12434. {
  12435. struct npc_data *nd;
  12436. nd=npc_name2id(script_getstr(st,6));
  12437. if (nd)
  12438. bl = &nd->bl;
  12439. } else //In case the origin is not an npc?
  12440. bl=map_id2bl(st->oid);
  12441. break;
  12442. case 2: //Get Pet Position
  12443. if(script_hasdata(st,6))
  12444. sd=map_nick2sd(script_getstr(st,6));
  12445. else
  12446. sd=script_rid2sd(st);
  12447. if (sd && sd->pd)
  12448. bl = &sd->pd->bl;
  12449. break;
  12450. case 3: //Get Mob Position
  12451. break; //see 'getunitdata'
  12452. case 4: //Get Homun Position
  12453. if(script_hasdata(st,6))
  12454. sd=map_nick2sd(script_getstr(st,6));
  12455. else
  12456. sd=script_rid2sd(st);
  12457. if (sd && sd->hd)
  12458. bl = &sd->hd->bl;
  12459. break;
  12460. case 5: //Get Mercenary Position
  12461. if(script_hasdata(st,6))
  12462. sd=map_nick2sd(script_getstr(st,6));
  12463. else
  12464. sd=script_rid2sd(st);
  12465. if (sd && sd->md)
  12466. bl = &sd->md->bl;
  12467. break;
  12468. case 6: //Get Elemental Position
  12469. if(script_hasdata(st,6))
  12470. sd=map_nick2sd(script_getstr(st,6));
  12471. else
  12472. sd=script_rid2sd(st);
  12473. if (sd && sd->ed)
  12474. bl = &sd->ed->bl;
  12475. break;
  12476. default:
  12477. ShowWarning("script: buildin_getmapxy: Invalid type %d\n", type);
  12478. script_pushint(st,-1);
  12479. return 1;
  12480. }
  12481. if (!bl) { //No object found.
  12482. script_pushint(st,-1);
  12483. return 0;
  12484. }
  12485. x= bl->x;
  12486. y= bl->y;
  12487. safestrncpy(mapname, map[bl->m].name, MAP_NAME_LENGTH);
  12488. //Set MapName$
  12489. num=st->stack->stack_data[st->start+2].u.num;
  12490. name=get_str(num&0x00ffffff);
  12491. prefix=*name;
  12492. if(not_server_variable(prefix))
  12493. sd=script_rid2sd(st);
  12494. else
  12495. sd=NULL;
  12496. set_reg(st,sd,num,name,(void*)mapname,script_getref(st,2));
  12497. //Set MapX
  12498. num=st->stack->stack_data[st->start+3].u.num;
  12499. name=get_str(num&0x00ffffff);
  12500. prefix=*name;
  12501. if(not_server_variable(prefix))
  12502. sd=script_rid2sd(st);
  12503. else
  12504. sd=NULL;
  12505. set_reg(st,sd,num,name,(void*)__64BPRTSIZE(x),script_getref(st,3));
  12506. //Set MapY
  12507. num=st->stack->stack_data[st->start+4].u.num;
  12508. name=get_str(num&0x00ffffff);
  12509. prefix=*name;
  12510. if(not_server_variable(prefix))
  12511. sd=script_rid2sd(st);
  12512. else
  12513. sd=NULL;
  12514. set_reg(st,sd,num,name,(void*)__64BPRTSIZE(y),script_getref(st,4));
  12515. //Return Success value
  12516. script_pushint(st,0);
  12517. return SCRIPT_CMD_SUCCESS;
  12518. }
  12519. /// Returns the map name of given map ID.
  12520. ///
  12521. /// mapid2name <map ID>;
  12522. BUILDIN_FUNC(mapid2name)
  12523. {
  12524. uint16 m = script_getnum(st, 2);
  12525. if (m < 0 || m >= MAX_MAP_PER_SERVER) {
  12526. script_pushstr(st, "");
  12527. return SCRIPT_CMD_FAILURE;
  12528. }
  12529. script_pushstrcopy(st, map_mapid2mapname(m));
  12530. return SCRIPT_CMD_SUCCESS;
  12531. }
  12532. /*==========================================
  12533. * Allows player to write NPC logs (i.e. Bank NPC, etc) [Lupus]
  12534. *------------------------------------------*/
  12535. BUILDIN_FUNC(logmes)
  12536. {
  12537. const char *str;
  12538. TBL_PC* sd;
  12539. sd = script_rid2sd(st);
  12540. if( sd == NULL )
  12541. return 1;
  12542. str = script_getstr(st,2);
  12543. log_npc(sd,str);
  12544. return SCRIPT_CMD_SUCCESS;
  12545. }
  12546. BUILDIN_FUNC(summon)
  12547. {
  12548. int _class, timeout=0;
  12549. const char *str,*event="";
  12550. TBL_PC *sd;
  12551. struct mob_data *md;
  12552. int tick = gettick();
  12553. sd=script_rid2sd(st);
  12554. if (!sd) return 0;
  12555. str =script_getstr(st,2);
  12556. _class=script_getnum(st,3);
  12557. if( script_hasdata(st,4) )
  12558. timeout=script_getnum(st,4);
  12559. if( script_hasdata(st,5) ){
  12560. event=script_getstr(st,5);
  12561. check_event(st, event);
  12562. }
  12563. clif_skill_poseffect(&sd->bl,AM_CALLHOMUN,1,sd->bl.x,sd->bl.y,tick);
  12564. md = mob_once_spawn_sub(&sd->bl, sd->bl.m, sd->bl.x, sd->bl.y, str, _class, event, SZ_SMALL, AI_NONE);
  12565. if (md) {
  12566. md->master_id=sd->bl.id;
  12567. md->special_state.ai = AI_ATTACK;
  12568. if( md->deletetimer != INVALID_TIMER )
  12569. delete_timer(md->deletetimer, mob_timer_delete);
  12570. md->deletetimer = add_timer(tick+(timeout>0?timeout:60000),mob_timer_delete,md->bl.id,0);
  12571. mob_spawn (md); //Now it is ready for spawning.
  12572. clif_specialeffect(&md->bl,344,AREA);
  12573. sc_start4(NULL,&md->bl, SC_MODECHANGE, 100, 1, 0, MD_AGGRESSIVE, 0, 60000);
  12574. }
  12575. script_pushint(st, md->bl.id);
  12576. return SCRIPT_CMD_SUCCESS;
  12577. }
  12578. /*==========================================
  12579. * Checks whether it is daytime/nighttime
  12580. *------------------------------------------*/
  12581. BUILDIN_FUNC(isnight)
  12582. {
  12583. script_pushint(st,(night_flag == 1));
  12584. return SCRIPT_CMD_SUCCESS;
  12585. }
  12586. BUILDIN_FUNC(isday)
  12587. {
  12588. script_pushint(st,(night_flag == 0));
  12589. return SCRIPT_CMD_SUCCESS;
  12590. }
  12591. /*================================================
  12592. * Check how many items/cards in the list are
  12593. * equipped - used for 2/15's cards patch [celest]
  12594. *------------------------------------------------*/
  12595. BUILDIN_FUNC(isequippedcnt)
  12596. {
  12597. TBL_PC *sd;
  12598. int i, id = 1;
  12599. int ret = 0;
  12600. sd = script_rid2sd(st);
  12601. if (!sd) { //If the player is not attached it is a script error anyway... but better prevent the map server from crashing...
  12602. script_pushint(st,0);
  12603. return 0;
  12604. }
  12605. for (i=0; id!=0; i++) {
  12606. short j;
  12607. FETCH (i+2, id) else id = 0;
  12608. if (id <= 0)
  12609. continue;
  12610. for (j=0; j<EQI_MAX; j++) {
  12611. short index = sd->equip_index[j];
  12612. if(index < 0)
  12613. continue;
  12614. if (pc_is_same_equip_index((enum equip_index)j, sd->equip_index, index))
  12615. continue;
  12616. if(!sd->inventory_data[index])
  12617. continue;
  12618. if (itemdb_type(id) != IT_CARD) { //No card. Count amount in inventory.
  12619. if (sd->inventory_data[index]->nameid == id)
  12620. ret+= sd->status.inventory[index].amount;
  12621. } else { //Count cards.
  12622. short k;
  12623. if (itemdb_isspecial(sd->status.inventory[index].card[0]))
  12624. continue; //No cards
  12625. for(k=0; k<sd->inventory_data[index]->slot; k++) {
  12626. if (sd->status.inventory[index].card[k] == id)
  12627. ret++; //[Lupus]
  12628. }
  12629. }
  12630. }
  12631. }
  12632. script_pushint(st,ret);
  12633. return SCRIPT_CMD_SUCCESS;
  12634. }
  12635. /*================================================
  12636. * Check whether another card has been
  12637. * equipped - used for 2/15's cards patch [celest]
  12638. * -- Items checked cannot be reused in another
  12639. * card set to prevent exploits
  12640. *------------------------------------------------*/
  12641. BUILDIN_FUNC(isequipped)
  12642. {
  12643. TBL_PC *sd;
  12644. int i, id = 1;
  12645. int ret = -1;
  12646. //Original hash to reverse it when full check fails.
  12647. unsigned int setitem_hash = 0, setitem_hash2 = 0;
  12648. sd = script_rid2sd(st);
  12649. if (!sd) { //If the player is not attached it is a script error anyway... but better prevent the map server from crashing...
  12650. script_pushint(st,0);
  12651. return SCRIPT_CMD_SUCCESS;
  12652. }
  12653. setitem_hash = sd->bonus.setitem_hash;
  12654. setitem_hash2 = sd->bonus.setitem_hash2;
  12655. for (i=0; id!=0; i++) {
  12656. int flag = 0;
  12657. short j;
  12658. FETCH (i+2, id) else id = 0;
  12659. if (id <= 0)
  12660. continue;
  12661. for (j=0; j<EQI_MAX; j++) {
  12662. short index = sd->equip_index[j];
  12663. if(index < 0)
  12664. continue;
  12665. if (pc_is_same_equip_index((enum equip_index)i, sd->equip_index, index))
  12666. continue;
  12667. if(!sd->inventory_data[index])
  12668. continue;
  12669. if (itemdb_type(id) != IT_CARD) {
  12670. if (sd->inventory_data[index]->nameid != id)
  12671. continue;
  12672. flag = 1;
  12673. break;
  12674. } else { //Cards
  12675. short k;
  12676. if (sd->inventory_data[index]->slot == 0 ||
  12677. itemdb_isspecial(sd->status.inventory[index].card[0]))
  12678. continue;
  12679. for (k = 0; k < sd->inventory_data[index]->slot; k++)
  12680. { //New hash system which should support up to 4 slots on any equipment. [Skotlex]
  12681. unsigned int hash = 0;
  12682. if (sd->status.inventory[index].card[k] != id)
  12683. continue;
  12684. hash = 1<<((j<5?j:j-5)*4 + k);
  12685. // check if card is already used by another set
  12686. if ( ( j < 5 ? sd->bonus.setitem_hash : sd->bonus.setitem_hash2 ) & hash)
  12687. continue;
  12688. // We have found a match
  12689. flag = 1;
  12690. // Set hash so this card cannot be used by another
  12691. if (j<5)
  12692. sd->bonus.setitem_hash |= hash;
  12693. else
  12694. sd->bonus.setitem_hash2 |= hash;
  12695. break;
  12696. }
  12697. }
  12698. if (flag) break; //Card found
  12699. }
  12700. if (ret == -1)
  12701. ret = flag;
  12702. else
  12703. ret &= flag;
  12704. if (!ret) break;
  12705. }
  12706. if (!ret) {//When check fails, restore original hash values. [Skotlex]
  12707. sd->bonus.setitem_hash = setitem_hash;
  12708. sd->bonus.setitem_hash2 = setitem_hash2;
  12709. }
  12710. script_pushint(st,ret);
  12711. return SCRIPT_CMD_SUCCESS;
  12712. }
  12713. /*================================================
  12714. * Check how many given inserted cards in the CURRENT
  12715. * weapon - used for 2/15's cards patch [Lupus]
  12716. *------------------------------------------------*/
  12717. BUILDIN_FUNC(cardscnt)
  12718. {
  12719. TBL_PC *sd;
  12720. int i, k, id = 1;
  12721. int ret = 0;
  12722. int index;
  12723. sd = script_rid2sd(st);
  12724. for (i=0; id!=0; i++) {
  12725. FETCH (i+2, id) else id = 0;
  12726. if (id <= 0)
  12727. continue;
  12728. index = current_equip_item_index; //we get CURRENT WEAPON inventory index from status.c [Lupus]
  12729. if(index < 0) continue;
  12730. if(!sd->inventory_data[index])
  12731. continue;
  12732. if(itemdb_type(id) != IT_CARD) {
  12733. if (sd->inventory_data[index]->nameid == id)
  12734. ret+= sd->status.inventory[index].amount;
  12735. } else {
  12736. if (itemdb_isspecial(sd->status.inventory[index].card[0]))
  12737. continue;
  12738. for(k=0; k<sd->inventory_data[index]->slot; k++) {
  12739. if (sd->status.inventory[index].card[k] == id)
  12740. ret++;
  12741. }
  12742. }
  12743. }
  12744. script_pushint(st,ret);
  12745. // script_pushint(st,current_equip_item_index);
  12746. return SCRIPT_CMD_SUCCESS;
  12747. }
  12748. /*=======================================================
  12749. * Returns the refined number of the current item, or an
  12750. * item with inventory index specified
  12751. *-------------------------------------------------------*/
  12752. BUILDIN_FUNC(getrefine)
  12753. {
  12754. TBL_PC *sd;
  12755. if ((sd = script_rid2sd(st))!= NULL)
  12756. script_pushint(st,sd->status.inventory[current_equip_item_index].refine);
  12757. else
  12758. script_pushint(st,0);
  12759. return SCRIPT_CMD_SUCCESS;
  12760. }
  12761. /*=======================================================
  12762. * Day/Night controls
  12763. *-------------------------------------------------------*/
  12764. BUILDIN_FUNC(night)
  12765. {
  12766. if (night_flag != 1) map_night_timer(night_timer_tid, 0, 0, 1);
  12767. return SCRIPT_CMD_SUCCESS;
  12768. }
  12769. BUILDIN_FUNC(day)
  12770. {
  12771. if (night_flag != 0) map_day_timer(day_timer_tid, 0, 0, 1);
  12772. return SCRIPT_CMD_SUCCESS;
  12773. }
  12774. /**
  12775. * unequip <equipment slot>{,<char_id>};
  12776. * @author [Spectre]
  12777. **/
  12778. BUILDIN_FUNC(unequip) {
  12779. int pos;
  12780. TBL_PC *sd;
  12781. if (!script_charid2sd(3,sd))
  12782. return SCRIPT_CMD_FAILURE;
  12783. pos = script_getnum(st,2);
  12784. if (pos >= 1 && pos <= ARRAYLENGTH(equip)) {
  12785. short i = pc_checkequip(sd,equip[pos-1]);
  12786. if (i >= 0) {
  12787. pc_unequipitem(sd,i,1|2);
  12788. script_pushint(st, 1);
  12789. return SCRIPT_CMD_SUCCESS;
  12790. }
  12791. }
  12792. ShowError("buildin_unequip: No item equipped at pos %d (CID=%d/AID=%d).\n", pos, sd->status.char_id, sd->status.account_id);
  12793. script_pushint(st, 0);
  12794. return SCRIPT_CMD_FAILURE;
  12795. }
  12796. /**
  12797. * equip <item id>{,<char_id>};
  12798. **/
  12799. BUILDIN_FUNC(equip) {
  12800. unsigned short nameid = 0;
  12801. TBL_PC *sd;
  12802. struct item_data *item_data;
  12803. if (!script_charid2sd(3,sd))
  12804. return SCRIPT_CMD_FAILURE;
  12805. nameid = script_getnum(st,2);
  12806. if ((item_data = itemdb_exists(nameid))) {
  12807. int i;
  12808. ARR_FIND( 0, MAX_INVENTORY, i, sd->status.inventory[i].nameid == nameid );
  12809. if (i < MAX_INVENTORY) {
  12810. pc_equipitem(sd,i,item_data->equip);
  12811. script_pushint(st,1);
  12812. return SCRIPT_CMD_SUCCESS;
  12813. }
  12814. }
  12815. ShowError("buildin_equip: Item %hu cannot be equipped\n",nameid);
  12816. script_pushint(st,0);
  12817. return SCRIPT_CMD_FAILURE;
  12818. }
  12819. BUILDIN_FUNC(autoequip)
  12820. {
  12821. unsigned short nameid;
  12822. int flag;
  12823. struct item_data *item_data;
  12824. nameid=script_getnum(st,2);
  12825. flag=script_getnum(st,3);
  12826. if( ( item_data = itemdb_exists(nameid) ) == NULL )
  12827. {
  12828. ShowError("buildin_autoequip: Invalid item '%hu'.\n", nameid);
  12829. return SCRIPT_CMD_FAILURE;
  12830. }
  12831. if( !itemdb_isequip2(item_data) )
  12832. {
  12833. ShowError("buildin_autoequip: Item '%hu' cannot be equipped.\n", nameid);
  12834. return SCRIPT_CMD_FAILURE;
  12835. }
  12836. item_data->flag.autoequip = flag>0?1:0;
  12837. return SCRIPT_CMD_SUCCESS;
  12838. }
  12839. BUILDIN_FUNC(setbattleflag)
  12840. {
  12841. const char *flag, *value;
  12842. flag = script_getstr(st,2);
  12843. value = script_getstr(st,3); // HACK: Retrieve number as string (auto-converted) for battle_set_value
  12844. if (battle_set_value(flag, value) == 0)
  12845. ShowWarning("buildin_setbattleflag: unknown battle_config flag '%s'\n",flag);
  12846. else
  12847. ShowInfo("buildin_setbattleflag: battle_config flag '%s' is now set to '%s'.\n",flag,value);
  12848. return SCRIPT_CMD_SUCCESS;
  12849. }
  12850. BUILDIN_FUNC(getbattleflag)
  12851. {
  12852. const char *flag;
  12853. flag = script_getstr(st,2);
  12854. script_pushint(st,battle_get_value(flag));
  12855. return SCRIPT_CMD_SUCCESS;
  12856. }
  12857. //=======================================================
  12858. // strlen [Valaris]
  12859. //-------------------------------------------------------
  12860. BUILDIN_FUNC(getstrlen)
  12861. {
  12862. const char *str = script_getstr(st,2);
  12863. int len = (str) ? (int)strlen(str) : 0;
  12864. script_pushint(st,len);
  12865. return SCRIPT_CMD_SUCCESS;
  12866. }
  12867. //=======================================================
  12868. // isalpha [Valaris]
  12869. //-------------------------------------------------------
  12870. BUILDIN_FUNC(charisalpha)
  12871. {
  12872. const char *str=script_getstr(st,2);
  12873. int pos=script_getnum(st,3);
  12874. int val = ( str && pos >= 0 && (unsigned int)pos < strlen(str) ) ? ISALPHA( str[pos] ) != 0 : 0;
  12875. script_pushint(st,val);
  12876. return SCRIPT_CMD_SUCCESS;
  12877. }
  12878. //=======================================================
  12879. // charisupper <str>, <index>
  12880. //-------------------------------------------------------
  12881. BUILDIN_FUNC(charisupper)
  12882. {
  12883. const char *str = script_getstr(st,2);
  12884. int pos = script_getnum(st,3);
  12885. int val = ( str && pos >= 0 && (unsigned int)pos < strlen(str) ) ? ISUPPER( str[pos] ) : 0;
  12886. script_pushint(st,val);
  12887. return SCRIPT_CMD_SUCCESS;
  12888. }
  12889. //=======================================================
  12890. // charislower <str>, <index>
  12891. //-------------------------------------------------------
  12892. BUILDIN_FUNC(charislower)
  12893. {
  12894. const char *str = script_getstr(st,2);
  12895. int pos = script_getnum(st,3);
  12896. int val = ( str && pos >= 0 && (unsigned int)pos < strlen(str) ) ? ISLOWER( str[pos] ) : 0;
  12897. script_pushint(st,val);
  12898. return SCRIPT_CMD_SUCCESS;
  12899. }
  12900. //=======================================================
  12901. // charat <str>, <index>
  12902. //-------------------------------------------------------
  12903. BUILDIN_FUNC(charat) {
  12904. const char *str = script_getstr(st,2);
  12905. int pos = script_getnum(st,3);
  12906. if( pos >= 0 && (unsigned int)pos < strlen(str) ) {
  12907. char output[2];
  12908. output[0] = str[pos];
  12909. output[1] = '\0';
  12910. script_pushstrcopy(st, output);
  12911. } else
  12912. script_pushconststr(st, "");
  12913. return SCRIPT_CMD_SUCCESS;
  12914. }
  12915. //=======================================================
  12916. // setchar <string>, <char>, <index>
  12917. //-------------------------------------------------------
  12918. BUILDIN_FUNC(setchar)
  12919. {
  12920. const char *str = script_getstr(st,2);
  12921. const char *c = script_getstr(st,3);
  12922. int index = script_getnum(st,4);
  12923. char *output = aStrdup(str);
  12924. if(index >= 0 && index < strlen(output))
  12925. output[index] = *c;
  12926. script_pushstr(st, output);
  12927. return SCRIPT_CMD_SUCCESS;
  12928. }
  12929. //=======================================================
  12930. // insertchar <string>, <char>, <index>
  12931. //-------------------------------------------------------
  12932. BUILDIN_FUNC(insertchar)
  12933. {
  12934. const char *str = script_getstr(st,2);
  12935. const char *c = script_getstr(st,3);
  12936. int index = script_getnum(st,4);
  12937. char *output;
  12938. size_t len = strlen(str);
  12939. if(index < 0)
  12940. index = 0;
  12941. else if(index > len)
  12942. index = len;
  12943. output = (char*)aMalloc(len + 2);
  12944. memcpy(output, str, index);
  12945. output[index] = c[0];
  12946. memcpy(&output[index+1], &str[index], len - index);
  12947. output[len+1] = '\0';
  12948. script_pushstr(st, output);
  12949. return SCRIPT_CMD_SUCCESS;
  12950. }
  12951. //=======================================================
  12952. // delchar <string>, <index>
  12953. //-------------------------------------------------------
  12954. BUILDIN_FUNC(delchar)
  12955. {
  12956. const char *str = script_getstr(st,2);
  12957. int index = script_getnum(st,3);
  12958. char *output;
  12959. size_t len = strlen(str);
  12960. if(index < 0 || index > len) {
  12961. //return original
  12962. output = aStrdup(str);
  12963. script_pushstr(st, output);
  12964. return 0;
  12965. }
  12966. output = (char*)aMalloc(len);
  12967. memcpy(output, str, index);
  12968. memcpy(&output[index], &str[index+1], len - index);
  12969. script_pushstr(st, output);
  12970. return SCRIPT_CMD_SUCCESS;
  12971. }
  12972. //=======================================================
  12973. // strtoupper <str>
  12974. //-------------------------------------------------------
  12975. BUILDIN_FUNC(strtoupper)
  12976. {
  12977. const char *str = script_getstr(st,2);
  12978. char *output = aStrdup(str);
  12979. char *cursor = output;
  12980. while (*cursor != '\0') {
  12981. *cursor = TOUPPER(*cursor);
  12982. cursor++;
  12983. }
  12984. script_pushstr(st, output);
  12985. return SCRIPT_CMD_SUCCESS;
  12986. }
  12987. //=======================================================
  12988. // strtolower <str>
  12989. //-------------------------------------------------------
  12990. BUILDIN_FUNC(strtolower)
  12991. {
  12992. const char *str = script_getstr(st,2);
  12993. char *output = aStrdup(str);
  12994. char *cursor = output;
  12995. while (*cursor != '\0') {
  12996. *cursor = TOLOWER(*cursor);
  12997. cursor++;
  12998. }
  12999. script_pushstr(st, output);
  13000. return SCRIPT_CMD_SUCCESS;
  13001. }
  13002. //=======================================================
  13003. // substr <str>, <start>, <end>
  13004. //-------------------------------------------------------
  13005. BUILDIN_FUNC(substr)
  13006. {
  13007. const char *str = script_getstr(st,2);
  13008. char *output;
  13009. int start = script_getnum(st,3);
  13010. int end = script_getnum(st,4);
  13011. int len = 0;
  13012. if(start >= 0 && end < strlen(str) && start <= end) {
  13013. len = end - start + 1;
  13014. output = (char*)aMalloc(len + 1);
  13015. memcpy(output, &str[start], len);
  13016. } else
  13017. output = (char*)aMalloc(1);
  13018. output[len] = '\0';
  13019. script_pushstr(st, output);
  13020. return SCRIPT_CMD_SUCCESS;
  13021. }
  13022. //=======================================================
  13023. // explode <dest_string_array>, <str>, <delimiter>
  13024. // Note: delimiter is limited to 1 char
  13025. //-------------------------------------------------------
  13026. BUILDIN_FUNC(explode)
  13027. {
  13028. struct script_data* data = script_getdata(st, 2);
  13029. const char *str = script_getstr(st,3);
  13030. const char delimiter = script_getstr(st, 4)[0];
  13031. int32 id;
  13032. size_t len = strlen(str);
  13033. int i = 0, j = 0;
  13034. int start;
  13035. char *temp;
  13036. const char* name;
  13037. TBL_PC* sd = NULL;
  13038. temp = (char*)aMalloc(len + 1);
  13039. if( !data_isreference(data) ) {
  13040. ShowError("script:explode: not a variable\n");
  13041. script_reportdata(data);
  13042. st->state = END;
  13043. return 1;// not a variable
  13044. }
  13045. id = reference_getid(data);
  13046. start = reference_getindex(data);
  13047. name = reference_getname(data);
  13048. if( not_array_variable(*name) ) {
  13049. ShowError("script:explode: illegal scope\n");
  13050. script_reportdata(data);
  13051. st->state = END;
  13052. return 1;// not supported
  13053. }
  13054. if( !is_string_variable(name) ) {
  13055. ShowError("script:explode: not string array\n");
  13056. script_reportdata(data);
  13057. st->state = END;
  13058. return 1;// data type mismatch
  13059. }
  13060. if( not_server_variable(*name) ) {
  13061. sd = script_rid2sd(st);
  13062. if( sd == NULL )
  13063. return 0;// no player attached
  13064. }
  13065. while(str[i] != '\0') {
  13066. if(str[i] == delimiter && start < SCRIPT_MAX_ARRAYSIZE-1) { //break at delimiter but ignore after reaching last array index
  13067. temp[j] = '\0';
  13068. set_reg(st, sd, reference_uid(id, start++), name, (void*)temp, reference_getref(data));
  13069. j = 0;
  13070. ++i;
  13071. } else {
  13072. temp[j++] = str[i++];
  13073. }
  13074. }
  13075. //set last string
  13076. temp[j] = '\0';
  13077. set_reg(st, sd, reference_uid(id, start), name, (void*)temp, reference_getref(data));
  13078. aFree(temp);
  13079. return SCRIPT_CMD_SUCCESS;
  13080. }
  13081. //=======================================================
  13082. // implode <string_array>
  13083. // implode <string_array>, <glue>
  13084. //-------------------------------------------------------
  13085. BUILDIN_FUNC(implode)
  13086. {
  13087. struct script_data* data = script_getdata(st, 2);
  13088. const char *name;
  13089. int32 glue_len = 0, array_size, id;
  13090. char *output;
  13091. if( !data_isreference(data) ) {
  13092. ShowError("script:implode: not a variable\n");
  13093. script_reportdata(data);
  13094. st->state = END;
  13095. return 1;// not a variable
  13096. }
  13097. id = reference_getid(data);
  13098. name = reference_getname(data);
  13099. if( not_array_variable(*name) ) {
  13100. ShowError("script:implode: illegal scope\n");
  13101. script_reportdata(data);
  13102. st->state = END;
  13103. return 1;// not supported
  13104. }
  13105. if( !is_string_variable(name) ) {
  13106. ShowError("script:implode: not string array\n");
  13107. script_reportdata(data);
  13108. st->state = END;
  13109. return 1;// data type mismatch
  13110. }
  13111. if( not_server_variable(*name) ) {
  13112. TBL_PC* sd = script_rid2sd(st);
  13113. if( sd == NULL )
  13114. return 0;// no player attached
  13115. }
  13116. //count chars
  13117. array_size = getarraysize(st, id, reference_getindex(data), is_string_variable(name), reference_getref(data)) - 1;
  13118. if(array_size == -1) { //empty array check (AmsTaff)
  13119. ShowWarning("script:implode: array length = 0\n");
  13120. output = (char*)aMalloc(sizeof(char)*5);
  13121. sprintf(output,"%s","NULL");
  13122. } else {
  13123. const char *glue = NULL, *temp;
  13124. size_t len = 0;
  13125. int i, k = 0;
  13126. for(i = 0; i <= array_size; ++i) {
  13127. temp = (char*) get_val2(st, reference_uid(id, i), reference_getref(data));
  13128. len += strlen(temp);
  13129. script_removetop(st, -1, 0);
  13130. }
  13131. //allocate mem
  13132. if( script_hasdata(st,3) ) {
  13133. glue = script_getstr(st,3);
  13134. glue_len = strlen(glue);
  13135. len += glue_len * (array_size);
  13136. }
  13137. output = (char*)aMalloc(len + 1);
  13138. //build output
  13139. for(i = 0; i < array_size; ++i) {
  13140. temp = (char*) get_val2(st, reference_uid(id, i), reference_getref(data));
  13141. len = strlen(temp);
  13142. memcpy(&output[k], temp, len);
  13143. k += len;
  13144. if(glue_len != 0) {
  13145. memcpy(&output[k], glue, glue_len);
  13146. k += glue_len;
  13147. }
  13148. script_removetop(st, -1, 0);
  13149. }
  13150. temp = (char*) get_val2(st, reference_uid(id, array_size), reference_getref(data));
  13151. len = strlen(temp);
  13152. memcpy(&output[k], temp, len);
  13153. k += len;
  13154. script_removetop(st, -1, 0);
  13155. output[k] = '\0';
  13156. }
  13157. script_pushstr(st, output);
  13158. return SCRIPT_CMD_SUCCESS;
  13159. }
  13160. //=======================================================
  13161. // sprintf(<format>, ...);
  13162. // Implements C sprintf, except format %n. The resulting string is
  13163. // returned, instead of being saved in variable by reference.
  13164. //-------------------------------------------------------
  13165. BUILDIN_FUNC(sprintf)
  13166. {
  13167. unsigned int len, argc = 0, arg = 0, buf2_len = 0;
  13168. const char* format;
  13169. char* p;
  13170. char* q;
  13171. char* buf = NULL;
  13172. char* buf2 = NULL;
  13173. struct script_data* data;
  13174. StringBuf final_buf;
  13175. // Fetch init data
  13176. format = script_getstr(st, 2);
  13177. argc = script_lastdata(st)-2;
  13178. len = strlen(format);
  13179. // Skip parsing, where no parsing is required.
  13180. if(len == 0) {
  13181. script_pushconststr(st,"");
  13182. return 0;
  13183. }
  13184. // Pessimistic alloc
  13185. CREATE(buf, char, len+1);
  13186. // Need not be parsed, just solve stuff like %%.
  13187. if(argc == 0) {
  13188. memcpy(buf,format,len+1);
  13189. script_pushstrcopy(st, buf);
  13190. aFree(buf);
  13191. return 0;
  13192. }
  13193. safestrncpy(buf, format, len+1);
  13194. // Issue sprintf for each parameter
  13195. StringBuf_Init(&final_buf);
  13196. q = buf;
  13197. while((p = strchr(q, '%')) != NULL) {
  13198. if(p != q) {
  13199. len = p - q + 1;
  13200. if(buf2_len < len) {
  13201. RECREATE(buf2, char, len);
  13202. buf2_len = len;
  13203. }
  13204. safestrncpy(buf2, q, len);
  13205. StringBuf_AppendStr(&final_buf, buf2);
  13206. q = p;
  13207. }
  13208. p = q + 1;
  13209. if(*p == '%') { // %%
  13210. StringBuf_AppendStr(&final_buf, "%");
  13211. q+=2;
  13212. continue;
  13213. }
  13214. if(*p == 'n') { // %n
  13215. ShowWarning("buildin_sprintf: Format %%n not supported! Skipping...\n");
  13216. script_reportsrc(st);
  13217. q+=2;
  13218. continue;
  13219. }
  13220. if(arg >= argc) {
  13221. ShowError("buildin_sprintf: Not enough arguments passed!\n");
  13222. if(buf) aFree(buf);
  13223. if(buf2) aFree(buf2);
  13224. StringBuf_Destroy(&final_buf);
  13225. script_pushconststr(st,"");
  13226. return 1;
  13227. }
  13228. if((p = strchr(q+1, '%')) == NULL)
  13229. p = strchr(q, 0); // EOS
  13230. len = p - q + 1;
  13231. if(buf2_len < len) {
  13232. RECREATE(buf2, char, len);
  13233. buf2_len = len;
  13234. }
  13235. safestrncpy(buf2, q, len);
  13236. q = p;
  13237. // Note: This assumes the passed value being the correct
  13238. // type to the current format specifier. If not, the server
  13239. // probably crashes or returns anything else, than expected,
  13240. // but it would behave in normal code the same way so it's
  13241. // the scripter's responsibility.
  13242. data = script_getdata(st, arg+3);
  13243. if(data_isstring(data)) // String
  13244. StringBuf_Printf(&final_buf, buf2, script_getstr(st, arg+3));
  13245. else if(data_isint(data)) // Number
  13246. StringBuf_Printf(&final_buf, buf2, script_getnum(st, arg+3));
  13247. else if(data_isreference(data)) { // Variable
  13248. char* name = reference_getname(data);
  13249. if(name[strlen(name)-1]=='$') // var Str
  13250. StringBuf_Printf(&final_buf, buf2, script_getstr(st, arg+3));
  13251. else // var Int
  13252. StringBuf_Printf(&final_buf, buf2, script_getnum(st, arg+3));
  13253. } else { // Unsupported type
  13254. ShowError("buildin_sprintf: Unknown argument type!\n");
  13255. if(buf) aFree(buf);
  13256. if(buf2) aFree(buf2);
  13257. StringBuf_Destroy(&final_buf);
  13258. script_pushconststr(st,"");
  13259. return 1;
  13260. }
  13261. arg++;
  13262. }
  13263. // Append anything left
  13264. if(*q)
  13265. StringBuf_AppendStr(&final_buf, q);
  13266. // Passed more, than needed
  13267. if(arg < argc) {
  13268. ShowWarning("buildin_sprintf: Unused arguments passed.\n");
  13269. script_reportsrc(st);
  13270. }
  13271. script_pushstrcopy(st, StringBuf_Value(&final_buf));
  13272. if(buf) aFree(buf);
  13273. if(buf2) aFree(buf2);
  13274. StringBuf_Destroy(&final_buf);
  13275. return SCRIPT_CMD_SUCCESS;
  13276. }
  13277. //=======================================================
  13278. // sscanf(<str>, <format>, ...);
  13279. // Implements C sscanf.
  13280. //-------------------------------------------------------
  13281. BUILDIN_FUNC(sscanf){
  13282. unsigned int argc, arg = 0, len;
  13283. struct script_data* data;
  13284. struct map_session_data* sd = NULL;
  13285. const char* str;
  13286. const char* format;
  13287. const char* p;
  13288. const char* q;
  13289. char* buf = NULL;
  13290. char* buf_p;
  13291. char* ref_str = NULL;
  13292. int ref_int;
  13293. // Get data
  13294. str = script_getstr(st, 2);
  13295. format = script_getstr(st, 3);
  13296. argc = script_lastdata(st)-3;
  13297. len = strlen(format);
  13298. if (len != 0 && strlen(str) == 0) {
  13299. // If the source string is empty but the format string is not, we return -1
  13300. // according to the C specs. (if the format string is also empty, we shall
  13301. // continue and return 0: 0 conversions took place out of the 0 attempted.)
  13302. script_pushint(st, -1);
  13303. return SCRIPT_CMD_SUCCESS;
  13304. }
  13305. CREATE(buf, char, len*2+1);
  13306. // Issue sscanf for each parameter
  13307. *buf = 0;
  13308. q = format;
  13309. while((p = strchr(q, '%'))){
  13310. if(p!=q){
  13311. strncat(buf, q, (size_t)(p-q));
  13312. q = p;
  13313. }
  13314. p = q+1;
  13315. if(*p=='*' || *p=='%'){ // Skip
  13316. strncat(buf, q, 2);
  13317. q+=2;
  13318. continue;
  13319. }
  13320. if(arg>=argc){
  13321. ShowError("buildin_sscanf: Not enough arguments passed!\n");
  13322. script_pushint(st, -1);
  13323. if(buf) aFree(buf);
  13324. if(ref_str) aFree(ref_str);
  13325. return 1;
  13326. }
  13327. if((p = strchr(q+1, '%'))==NULL){
  13328. p = strchr(q, 0); // EOS
  13329. }
  13330. len = p-q;
  13331. strncat(buf, q, len);
  13332. q = p;
  13333. // Validate output
  13334. data = script_getdata(st, arg+4);
  13335. if(!data_isreference(data) || !reference_tovariable(data)){
  13336. ShowError("buildin_sscanf: Target argument is not a variable!\n");
  13337. script_pushint(st, -1);
  13338. if(buf) aFree(buf);
  13339. if(ref_str) aFree(ref_str);
  13340. return 1;
  13341. }
  13342. buf_p = reference_getname(data);
  13343. if(not_server_variable(*buf_p) && (sd = script_rid2sd(st))==NULL){
  13344. script_pushint(st, -1);
  13345. if(buf) aFree(buf);
  13346. if(ref_str) aFree(ref_str);
  13347. return 0;
  13348. }
  13349. // Save value if any
  13350. if(buf_p[strlen(buf_p)-1]=='$'){ // String
  13351. if(ref_str==NULL){
  13352. CREATE(ref_str, char, strlen(str)+1);
  13353. }
  13354. if(sscanf(str, buf, ref_str)==0){
  13355. break;
  13356. }
  13357. set_reg(st, sd, reference_uid( reference_getid(data), reference_getindex(data) ), buf_p, (void *)(ref_str), reference_getref(data));
  13358. } else { // Number
  13359. if(sscanf(str, buf, &ref_int)==0){
  13360. break;
  13361. }
  13362. set_reg(st, sd, reference_uid( reference_getid(data), reference_getindex(data) ), buf_p, (void *)__64BPRTSIZE(ref_int), reference_getref(data));
  13363. }
  13364. arg++;
  13365. // Disable used format (%... -> %*...)
  13366. buf_p = strchr(buf, 0);
  13367. memmove(buf_p-len+2, buf_p-len+1, len);
  13368. *(buf_p-len+1) = '*';
  13369. }
  13370. script_pushint(st, arg);
  13371. if(buf) aFree(buf);
  13372. if(ref_str) aFree(ref_str);
  13373. return SCRIPT_CMD_SUCCESS;
  13374. }
  13375. //=======================================================
  13376. // strpos(<haystack>, <needle>)
  13377. // strpos(<haystack>, <needle>, <offset>)
  13378. //
  13379. // Implements PHP style strpos. Adapted from code from
  13380. // http://www.daniweb.com/code/snippet313.html, Dave Sinkula
  13381. //-------------------------------------------------------
  13382. BUILDIN_FUNC(strpos) {
  13383. const char *haystack = script_getstr(st,2);
  13384. const char *needle = script_getstr(st,3);
  13385. int i;
  13386. size_t len;
  13387. if( script_hasdata(st,4) )
  13388. i = script_getnum(st,4);
  13389. else
  13390. i = 0;
  13391. if (needle[0] == '\0') {
  13392. script_pushint(st, -1);
  13393. return 0;
  13394. }
  13395. len = strlen(haystack);
  13396. for ( ; i < len; ++i ) {
  13397. if ( haystack[i] == *needle ) {
  13398. // matched starting char -- loop through remaining chars
  13399. const char *h, *n;
  13400. for ( h = &haystack[i], n = needle; *h && *n; ++h, ++n ) {
  13401. if ( *h != *n ) {
  13402. break;
  13403. }
  13404. }
  13405. if ( !*n ) { // matched all of 'needle' to null termination
  13406. script_pushint(st, i);
  13407. return 0;
  13408. }
  13409. }
  13410. }
  13411. script_pushint(st, -1);
  13412. return SCRIPT_CMD_SUCCESS;
  13413. }
  13414. //===============================================================
  13415. // replacestr <input>, <search>, <replace>{, <usecase>{, <count>}}
  13416. //
  13417. // Note: Finds all instances of <search> in <input> and replaces
  13418. // with <replace>. If specified will only replace as many
  13419. // instances as specified in <count>. By default will be case
  13420. // sensitive.
  13421. //---------------------------------------------------------------
  13422. BUILDIN_FUNC(replacestr)
  13423. {
  13424. const char *input = script_getstr(st, 2);
  13425. const char *find = script_getstr(st, 3);
  13426. const char *replace = script_getstr(st, 4);
  13427. size_t inputlen = strlen(input);
  13428. size_t findlen = strlen(find);
  13429. struct StringBuf output;
  13430. bool usecase = true;
  13431. int count = 0;
  13432. int numFinds = 0;
  13433. int i = 0, f = 0;
  13434. if(findlen == 0) {
  13435. ShowError("script:replacestr: Invalid search length.\n");
  13436. st->state = END;
  13437. return 1;
  13438. }
  13439. if(script_hasdata(st, 5)) {
  13440. struct script_data *data = script_getdata(st, 5);
  13441. get_val(st, data); // Convert into value in case of a variable
  13442. if( !data_isstring(data) )
  13443. usecase = script_getnum(st, 5) != 0;
  13444. else {
  13445. ShowError("script:replacestr: Invalid usecase value. Expected int got string\n");
  13446. st->state = END;
  13447. return 1;
  13448. }
  13449. }
  13450. if(script_hasdata(st, 6)) {
  13451. count = script_getnum(st, 6);
  13452. if(count == 0) {
  13453. ShowError("script:replacestr: Invalid count value. Expected int got string\n");
  13454. st->state = END;
  13455. return 1;
  13456. }
  13457. }
  13458. StringBuf_Init(&output);
  13459. for(; i < inputlen; i++) {
  13460. if(count && count == numFinds) { //found enough, stop looking
  13461. break;
  13462. }
  13463. for(f = 0; f <= findlen; f++) {
  13464. if(f == findlen) { //complete match
  13465. numFinds++;
  13466. StringBuf_AppendStr(&output, replace);
  13467. i += findlen - 1;
  13468. break;
  13469. } else {
  13470. if(usecase) {
  13471. if((i + f) > inputlen || input[i + f] != find[f]) {
  13472. StringBuf_Printf(&output, "%c", input[i]);
  13473. break;
  13474. }
  13475. } else {
  13476. if(((i + f) > inputlen || input[i + f] != find[f]) && TOUPPER(input[i+f]) != TOUPPER(find[f])) {
  13477. StringBuf_Printf(&output, "%c", input[i]);
  13478. break;
  13479. }
  13480. }
  13481. }
  13482. }
  13483. }
  13484. //append excess after enough found
  13485. if(i < inputlen)
  13486. StringBuf_AppendStr(&output, &(input[i]));
  13487. script_pushstrcopy(st, StringBuf_Value(&output));
  13488. StringBuf_Destroy(&output);
  13489. return SCRIPT_CMD_SUCCESS;
  13490. }
  13491. //========================================================
  13492. // countstr <input>, <search>{, <usecase>}
  13493. //
  13494. // Note: Counts the number of times <search> occurs in
  13495. // <input>. By default will be case sensitive.
  13496. //--------------------------------------------------------
  13497. BUILDIN_FUNC(countstr)
  13498. {
  13499. const char *input = script_getstr(st, 2);
  13500. const char *find = script_getstr(st, 3);
  13501. size_t inputlen = strlen(input);
  13502. size_t findlen = strlen(find);
  13503. bool usecase = true;
  13504. int numFinds = 0;
  13505. int i = 0, f = 0;
  13506. if(findlen == 0) {
  13507. ShowError("script:countstr: Invalid search length.\n");
  13508. st->state = END;
  13509. return 1;
  13510. }
  13511. if(script_hasdata(st, 4)) {
  13512. struct script_data *data;
  13513. data = script_getdata(st, 4);
  13514. get_val(st, data); // Convert into value in case of a variable
  13515. if( !data_isstring(data) )
  13516. usecase = script_getnum(st, 4) != 0;
  13517. else {
  13518. ShowError("script:countstr: Invalid usecase value. Expected int got string\n");
  13519. st->state = END;
  13520. return 1;
  13521. }
  13522. }
  13523. for(; i < inputlen; i++) {
  13524. for(f = 0; f <= findlen; f++) {
  13525. if(f == findlen) { //complete match
  13526. numFinds++;
  13527. i += findlen - 1;
  13528. break;
  13529. } else {
  13530. if(usecase) {
  13531. if((i + f) > inputlen || input[i + f] != find[f]) {
  13532. break;
  13533. }
  13534. } else {
  13535. if(((i + f) > inputlen || input[i + f] != find[f]) && TOUPPER(input[i+f]) != TOUPPER(find[f])) {
  13536. break;
  13537. }
  13538. }
  13539. }
  13540. }
  13541. }
  13542. script_pushint(st, numFinds);
  13543. return SCRIPT_CMD_SUCCESS;
  13544. }
  13545. /// Changes the display name and/or display class of the npc.
  13546. /// Returns 0 is successful, 1 if the npc does not exist.
  13547. ///
  13548. /// setnpcdisplay("<npc name>", "<new display name>", <new class id>, <new size>) -> <int>
  13549. /// setnpcdisplay("<npc name>", "<new display name>", <new class id>) -> <int>
  13550. /// setnpcdisplay("<npc name>", "<new display name>") -> <int>
  13551. /// setnpcdisplay("<npc name>", <new class id>) -> <int>
  13552. BUILDIN_FUNC(setnpcdisplay)
  13553. {
  13554. const char* name;
  13555. const char* newname = NULL;
  13556. int class_ = -1, size = -1;
  13557. struct script_data* data;
  13558. struct npc_data* nd;
  13559. name = script_getstr(st,2);
  13560. data = script_getdata(st,3);
  13561. if( script_hasdata(st,4) )
  13562. class_ = script_getnum(st,4);
  13563. if( script_hasdata(st,5) )
  13564. size = script_getnum(st,5);
  13565. get_val(st, data);
  13566. if( data_isstring(data) )
  13567. newname = conv_str(st,data);
  13568. else if( data_isint(data) )
  13569. class_ = conv_num(st,data);
  13570. else
  13571. {
  13572. ShowError("script:setnpcdisplay: expected string or number\n");
  13573. script_reportdata(data);
  13574. return 1;
  13575. }
  13576. nd = npc_name2id(name);
  13577. if( nd == NULL )
  13578. {// not found
  13579. script_pushint(st,1);
  13580. return 0;
  13581. }
  13582. // update npc
  13583. if( newname )
  13584. npc_setdisplayname(nd, newname);
  13585. if( size != -1 && size != (int)nd->size )
  13586. nd->size = size;
  13587. else
  13588. size = -1;
  13589. if( class_ != -1 && nd->class_ != class_ )
  13590. npc_setclass(nd, class_);
  13591. else if( size != -1 )
  13592. { // Required to update the visual size
  13593. clif_clearunit_area(&nd->bl, CLR_OUTSIGHT);
  13594. clif_spawn(&nd->bl);
  13595. }
  13596. script_pushint(st,0);
  13597. return SCRIPT_CMD_SUCCESS;
  13598. }
  13599. BUILDIN_FUNC(atoi)
  13600. {
  13601. const char *value;
  13602. value = script_getstr(st,2);
  13603. script_pushint(st,atoi(value));
  13604. return SCRIPT_CMD_SUCCESS;
  13605. }
  13606. BUILDIN_FUNC(axtoi)
  13607. {
  13608. const char *hex = script_getstr(st,2);
  13609. long value = strtol(hex, NULL, 16);
  13610. #if LONG_MAX > INT_MAX || LONG_MIN < INT_MIN
  13611. value = cap_value(value, INT_MIN, INT_MAX);
  13612. #endif
  13613. script_pushint(st, (int)value);
  13614. return SCRIPT_CMD_SUCCESS;
  13615. }
  13616. BUILDIN_FUNC(strtol)
  13617. {
  13618. const char *string = script_getstr(st, 2);
  13619. int base = script_getnum(st, 3);
  13620. long value = strtol(string, NULL, base);
  13621. #if LONG_MAX > INT_MAX || LONG_MIN < INT_MIN
  13622. value = cap_value(value, INT_MIN, INT_MAX);
  13623. #endif
  13624. script_pushint(st, (int)value);
  13625. return SCRIPT_CMD_SUCCESS;
  13626. }
  13627. // case-insensitive substring search [lordalfa]
  13628. BUILDIN_FUNC(compare)
  13629. {
  13630. const char *message;
  13631. const char *cmpstring;
  13632. message = script_getstr(st,2);
  13633. cmpstring = script_getstr(st,3);
  13634. script_pushint(st,(stristr(message,cmpstring) != NULL));
  13635. return SCRIPT_CMD_SUCCESS;
  13636. }
  13637. BUILDIN_FUNC(strcmp)
  13638. {
  13639. const char *str1;
  13640. const char *str2;
  13641. str1 = script_getstr(st,2);
  13642. str2 = script_getstr(st,3);
  13643. script_pushint(st,strcmp(str1, str2));
  13644. return SCRIPT_CMD_SUCCESS;
  13645. }
  13646. // [zBuffer] List of mathematics commands --->
  13647. BUILDIN_FUNC(sqrt)
  13648. {
  13649. double i, a;
  13650. i = script_getnum(st,2);
  13651. a = sqrt(i);
  13652. script_pushint(st,(int)a);
  13653. return SCRIPT_CMD_SUCCESS;
  13654. }
  13655. BUILDIN_FUNC(pow)
  13656. {
  13657. double i, a, b;
  13658. a = script_getnum(st,2);
  13659. b = script_getnum(st,3);
  13660. i = pow(a,b);
  13661. script_pushint(st,(int)i);
  13662. return SCRIPT_CMD_SUCCESS;
  13663. }
  13664. BUILDIN_FUNC(distance)
  13665. {
  13666. int x0, y0, x1, y1;
  13667. x0 = script_getnum(st,2);
  13668. y0 = script_getnum(st,3);
  13669. x1 = script_getnum(st,4);
  13670. y1 = script_getnum(st,5);
  13671. script_pushint(st,distance_xy(x0,y0,x1,y1));
  13672. return SCRIPT_CMD_SUCCESS;
  13673. }
  13674. // <--- [zBuffer] List of mathematics commands
  13675. BUILDIN_FUNC(md5)
  13676. {
  13677. const char *tmpstr;
  13678. char *md5str;
  13679. tmpstr = script_getstr(st,2);
  13680. md5str = (char *)aMalloc((32+1)*sizeof(char));
  13681. MD5_String(tmpstr, md5str);
  13682. script_pushstr(st, md5str);
  13683. return SCRIPT_CMD_SUCCESS;
  13684. }
  13685. // [zBuffer] List of dynamic var commands --->
  13686. /**
  13687. * setd "<variable name>",<value>{,<char_id>};
  13688. **/
  13689. BUILDIN_FUNC(setd)
  13690. {
  13691. TBL_PC *sd = NULL;
  13692. char varname[100];
  13693. const char *buffer;
  13694. int elem;
  13695. buffer = script_getstr(st, 2);
  13696. if(sscanf(buffer, "%99[^[][%d]", varname, &elem) < 2)
  13697. elem = 0;
  13698. if( not_server_variable(*varname) ) {
  13699. if (!script_charid2sd(4,sd))
  13700. return SCRIPT_CMD_FAILURE;
  13701. }
  13702. if( is_string_variable(varname) ) {
  13703. setd_sub(st, sd, varname, elem, (void *)script_getstr(st, 3), NULL);
  13704. } else {
  13705. setd_sub(st, sd, varname, elem, (void *)__64BPRTSIZE(script_getnum(st, 3)), NULL);
  13706. }
  13707. return SCRIPT_CMD_SUCCESS;
  13708. }
  13709. int buildin_query_sql_sub(struct script_state* st, Sql* handle)
  13710. {
  13711. int i, j;
  13712. TBL_PC* sd = NULL;
  13713. const char* query;
  13714. struct script_data* data;
  13715. const char* name;
  13716. int max_rows = SCRIPT_MAX_ARRAYSIZE; // maximum number of rows
  13717. int num_vars;
  13718. int num_cols;
  13719. // check target variables
  13720. for( i = 3; script_hasdata(st,i); ++i ) {
  13721. data = script_getdata(st, i);
  13722. if( data_isreference(data) ) { // it's a variable
  13723. name = reference_getname(data);
  13724. if( not_server_variable(*name) && sd == NULL ) { // requires a player
  13725. sd = script_rid2sd(st);
  13726. if( sd == NULL ) { // no player attached
  13727. script_reportdata(data);
  13728. st->state = END;
  13729. return 1;
  13730. }
  13731. }
  13732. if( not_array_variable(*name) )
  13733. max_rows = 1;// not an array, limit to one row
  13734. } else {
  13735. ShowError("script:query_sql: not a variable\n");
  13736. script_reportdata(data);
  13737. st->state = END;
  13738. return 1;
  13739. }
  13740. }
  13741. num_vars = i - 3;
  13742. // Execute the query
  13743. query = script_getstr(st,2);
  13744. if( SQL_ERROR == Sql_QueryStr(handle, query) ) {
  13745. Sql_ShowDebug(handle);
  13746. script_pushint(st, -1);
  13747. return 1;
  13748. }
  13749. if( Sql_NumRows(handle) == 0 ) { // No data received
  13750. Sql_FreeResult(handle);
  13751. script_pushint(st, -1);
  13752. return 0;
  13753. }
  13754. // Count the number of columns to store
  13755. num_cols = Sql_NumColumns(handle);
  13756. if( num_vars < num_cols ) {
  13757. ShowWarning("script:query_sql: Too many columns, discarding last %u columns.\n", (unsigned int)(num_cols-num_vars));
  13758. script_reportsrc(st);
  13759. } else if( num_vars > num_cols ) {
  13760. ShowWarning("script:query_sql: Too many variables (%u extra).\n", (unsigned int)(num_vars-num_cols));
  13761. script_reportsrc(st);
  13762. }
  13763. // Store data
  13764. for( i = 0; i < max_rows && SQL_SUCCESS == Sql_NextRow(handle); ++i ) {
  13765. for( j = 0; j < num_vars; ++j ) {
  13766. char* str = NULL;
  13767. if( j < num_cols )
  13768. Sql_GetData(handle, j, &str, NULL);
  13769. data = script_getdata(st, j+3);
  13770. name = reference_getname(data);
  13771. if( is_string_variable(name) )
  13772. setd_sub(st, sd, name, i, (void *)(str?str:""), reference_getref(data));
  13773. else
  13774. setd_sub(st, sd, name, i, (void *)__64BPRTSIZE((str?atoi(str):0)), reference_getref(data));
  13775. }
  13776. }
  13777. if( i == max_rows && max_rows < Sql_NumRows(handle) ) {
  13778. ShowWarning("script:query_sql: Only %d/%u rows have been stored.\n", max_rows, (unsigned int)Sql_NumRows(handle));
  13779. script_reportsrc(st);
  13780. }
  13781. // Free data
  13782. Sql_FreeResult(handle);
  13783. script_pushint(st, i);
  13784. return SCRIPT_CMD_SUCCESS;
  13785. }
  13786. BUILDIN_FUNC(query_sql) {
  13787. #ifdef BETA_THREAD_TEST
  13788. if( st->state != RERUNLINE ) {
  13789. queryThread_add(st,false);
  13790. st->state = RERUNLINE;/* will continue when the query is finished running. */
  13791. } else
  13792. st->state = RUN;
  13793. return 0;
  13794. #else
  13795. return buildin_query_sql_sub(st, qsmysql_handle);
  13796. #endif
  13797. }
  13798. BUILDIN_FUNC(query_logsql) {
  13799. if( !log_config.sql_logs ) {// logmysql_handle == NULL
  13800. ShowWarning("buildin_query_logsql: SQL logs are disabled, query '%s' will not be executed.\n", script_getstr(st,2));
  13801. script_pushint(st,-1);
  13802. return 1;
  13803. }
  13804. #ifdef BETA_THREAD_TEST
  13805. if( st->state != RERUNLINE ) {
  13806. queryThread_add(st,true);
  13807. st->state = RERUNLINE;/* will continue when the query is finished running. */
  13808. } else
  13809. st->state = RUN;
  13810. return 0;
  13811. #else
  13812. return buildin_query_sql_sub(st, logmysql_handle);
  13813. #endif
  13814. }
  13815. //Allows escaping of a given string.
  13816. BUILDIN_FUNC(escape_sql)
  13817. {
  13818. const char *str;
  13819. char *esc_str;
  13820. size_t len;
  13821. str = script_getstr(st,2);
  13822. len = strlen(str);
  13823. esc_str = (char*)aMalloc(len*2+1);
  13824. Sql_EscapeStringLen(mmysql_handle, esc_str, str, len);
  13825. script_pushstr(st, esc_str);
  13826. return SCRIPT_CMD_SUCCESS;
  13827. }
  13828. BUILDIN_FUNC(getd)
  13829. {
  13830. char varname[100];
  13831. const char *buffer;
  13832. int elem;
  13833. buffer = script_getstr(st, 2);
  13834. if(sscanf(buffer, "%99[^[][%d]", varname, &elem) < 2)
  13835. elem = 0;
  13836. // Push the 'pointer' so it's more flexible [Lance]
  13837. push_val(st->stack, C_NAME, reference_uid(add_str(varname), elem));
  13838. return SCRIPT_CMD_SUCCESS;
  13839. }
  13840. BUILDIN_FUNC(callshop)
  13841. {
  13842. TBL_PC *sd = NULL;
  13843. struct npc_data *nd;
  13844. const char *shopname;
  13845. int flag = 0;
  13846. sd = script_rid2sd(st);
  13847. if (!sd) {
  13848. script_pushint(st,0);
  13849. return 0;
  13850. }
  13851. shopname = script_getstr(st, 2);
  13852. if (script_hasdata(st,3))
  13853. flag = script_getnum(st,3);
  13854. nd = npc_name2id(shopname);
  13855. 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) ) {
  13856. ShowError("buildin_callshop: Shop [%s] not found (or NPC is not shop type)\n", shopname);
  13857. script_pushint(st,0);
  13858. return 1;
  13859. }
  13860. if (nd->subtype == NPCTYPE_SHOP || nd->subtype == NPCTYPE_ITEMSHOP || nd->subtype == NPCTYPE_POINTSHOP) {
  13861. // flag the user as using a valid script call for opening the shop (for floating NPCs)
  13862. sd->state.callshop = 1;
  13863. switch (flag) {
  13864. case 1: npc_buysellsel(sd,nd->bl.id,0); break; //Buy window
  13865. case 2: npc_buysellsel(sd,nd->bl.id,1); break; //Sell window
  13866. default: clif_npcbuysell(sd,nd->bl.id); break; //Show menu
  13867. }
  13868. }
  13869. #if PACKETVER >= 20131223
  13870. else if (nd->subtype == NPCTYPE_MARKETSHOP) {
  13871. unsigned short i;
  13872. for (i = 0; i < nd->u.shop.count; i++) {
  13873. if (nd->u.shop.shop_item[i].qty)
  13874. break;
  13875. }
  13876. if (i == nd->u.shop.count) {
  13877. clif_colormes(sd, color_table[COLOR_RED], msg_txt(sd, 534));
  13878. return false;
  13879. }
  13880. sd->npc_shopid = nd->bl.id;
  13881. clif_npc_market_open(sd, nd);
  13882. script_pushint(st,1);
  13883. return SCRIPT_CMD_SUCCESS;
  13884. }
  13885. #endif
  13886. else
  13887. clif_cashshop_show(sd, nd);
  13888. sd->npc_shopid = nd->bl.id;
  13889. script_pushint(st,1);
  13890. return SCRIPT_CMD_SUCCESS;
  13891. }
  13892. BUILDIN_FUNC(npcshopitem)
  13893. {
  13894. const char* npcname = script_getstr(st, 2);
  13895. struct npc_data* nd = npc_name2id(npcname);
  13896. int n, i;
  13897. int amount;
  13898. uint16 offs = 2;
  13899. if( !nd || ( nd->subtype != NPCTYPE_SHOP && nd->subtype != NPCTYPE_CASHSHOP && nd->subtype != NPCTYPE_ITEMSHOP && nd->subtype != NPCTYPE_POINTSHOP && nd->subtype != NPCTYPE_MARKETSHOP ) ) { // Not found.
  13900. script_pushint(st,0);
  13901. return 0;
  13902. }
  13903. #if PACKETVER >= 20131223
  13904. if (nd->subtype == NPCTYPE_MARKETSHOP) {
  13905. offs = 3;
  13906. npc_market_delfromsql_(nd->exname, 0, true);
  13907. }
  13908. #endif
  13909. // get the count of new entries
  13910. amount = (script_lastdata(st)-2)/offs;
  13911. // generate new shop item list
  13912. RECREATE(nd->u.shop.shop_item, struct npc_item_list, amount);
  13913. for (n = 0, i = 3; n < amount; n++, i+=offs) {
  13914. nd->u.shop.shop_item[n].nameid = script_getnum(st,i);
  13915. nd->u.shop.shop_item[n].value = script_getnum(st,i+1);
  13916. #if PACKETVER >= 20131223
  13917. if (nd->subtype == NPCTYPE_MARKETSHOP) {
  13918. nd->u.shop.shop_item[n].qty = script_getnum(st,i+2);
  13919. nd->u.shop.shop_item[n].flag = 1;
  13920. npc_market_tosql(nd->exname, &nd->u.shop.shop_item[n]);
  13921. }
  13922. #endif
  13923. }
  13924. nd->u.shop.count = n;
  13925. script_pushint(st,1);
  13926. return SCRIPT_CMD_SUCCESS;
  13927. }
  13928. BUILDIN_FUNC(npcshopadditem)
  13929. {
  13930. const char* npcname = script_getstr(st,2);
  13931. struct npc_data* nd = npc_name2id(npcname);
  13932. int n, i;
  13933. int amount;
  13934. uint16 offs = 2;
  13935. if (!nd || ( nd->subtype != NPCTYPE_SHOP && nd->subtype != NPCTYPE_CASHSHOP && nd->subtype != NPCTYPE_ITEMSHOP && nd->subtype != NPCTYPE_POINTSHOP && nd->subtype != NPCTYPE_MARKETSHOP)) { // Not found.
  13936. script_pushint(st,0);
  13937. return 0;
  13938. }
  13939. if (nd->subtype == NPCTYPE_MARKETSHOP)
  13940. offs = 3;
  13941. // get the count of new entries
  13942. amount = (script_lastdata(st)-2)/offs;
  13943. #if PACKETVER >= 20131223
  13944. if (nd->subtype == NPCTYPE_MARKETSHOP) {
  13945. for (n = 0, i = 3; n < amount; n++, i += offs) {
  13946. uint16 nameid = script_getnum(st,i), j;
  13947. // Check existing entries
  13948. ARR_FIND(0, nd->u.shop.count, j, nd->u.shop.shop_item[j].nameid == nameid);
  13949. if (j == nd->u.shop.count) {
  13950. RECREATE(nd->u.shop.shop_item, struct npc_item_list, nd->u.shop.count+1);
  13951. j = nd->u.shop.count;
  13952. nd->u.shop.shop_item[j].flag = 1;
  13953. nd->u.shop.count++;
  13954. }
  13955. nd->u.shop.shop_item[j].nameid = nameid;
  13956. nd->u.shop.shop_item[j].value = script_getnum(st,i+1);
  13957. nd->u.shop.shop_item[j].qty = script_getnum(st,i+2);
  13958. npc_market_tosql(nd->exname, &nd->u.shop.shop_item[j]);
  13959. }
  13960. script_pushint(st,1);
  13961. return SCRIPT_CMD_SUCCESS;
  13962. }
  13963. #endif
  13964. // append new items to existing shop item list
  13965. RECREATE(nd->u.shop.shop_item, struct npc_item_list, nd->u.shop.count+amount);
  13966. for (n = nd->u.shop.count, i = 3; n < nd->u.shop.count+amount; n++, i+=offs)
  13967. {
  13968. nd->u.shop.shop_item[n].nameid = script_getnum(st,i);
  13969. nd->u.shop.shop_item[n].value = script_getnum(st,i+1);
  13970. }
  13971. nd->u.shop.count = n;
  13972. script_pushint(st,1);
  13973. return SCRIPT_CMD_SUCCESS;
  13974. }
  13975. BUILDIN_FUNC(npcshopdelitem)
  13976. {
  13977. const char* npcname = script_getstr(st,2);
  13978. struct npc_data* nd = npc_name2id(npcname);
  13979. int n, i;
  13980. int amount;
  13981. int size;
  13982. if (!nd || ( nd->subtype != NPCTYPE_SHOP && nd->subtype != NPCTYPE_CASHSHOP && nd->subtype != NPCTYPE_ITEMSHOP && nd->subtype != NPCTYPE_POINTSHOP && nd->subtype != NPCTYPE_MARKETSHOP)) { // Not found.
  13983. script_pushint(st,0);
  13984. return 0;
  13985. }
  13986. amount = script_lastdata(st)-2;
  13987. size = nd->u.shop.count;
  13988. // remove specified items from the shop item list
  13989. for( i = 3; i < 3 + amount; i++ ) {
  13990. unsigned short nameid = script_getnum(st,i);
  13991. ARR_FIND( 0, size, n, nd->u.shop.shop_item[n].nameid == nameid );
  13992. if( n < size ) {
  13993. if (n+1 != size)
  13994. memmove(&nd->u.shop.shop_item[n], &nd->u.shop.shop_item[n+1], sizeof(nd->u.shop.shop_item[0])*(size-n));
  13995. #if PACKETVER >= 20131223
  13996. if (nd->subtype == NPCTYPE_MARKETSHOP)
  13997. npc_market_delfromsql_(nd->exname, nameid, false);
  13998. #endif
  13999. size--;
  14000. }
  14001. }
  14002. RECREATE(nd->u.shop.shop_item, struct npc_item_list, size);
  14003. nd->u.shop.count = size;
  14004. script_pushint(st,1);
  14005. return SCRIPT_CMD_SUCCESS;
  14006. }
  14007. /**
  14008. * Sets a script to attach to a shop npc.
  14009. * npcshopattach "<npc_name>";
  14010. **/
  14011. BUILDIN_FUNC(npcshopattach)
  14012. {
  14013. const char* npcname = script_getstr(st,2);
  14014. struct npc_data* nd = npc_name2id(npcname);
  14015. int flag = 1;
  14016. if( script_hasdata(st,3) )
  14017. flag = script_getnum(st,3);
  14018. if (!nd || ( nd->subtype != NPCTYPE_SHOP && nd->subtype != NPCTYPE_CASHSHOP && nd->subtype != NPCTYPE_ITEMSHOP && nd->subtype != NPCTYPE_POINTSHOP && nd->subtype != NPCTYPE_MARKETSHOP)) { // Not found.
  14019. script_pushint(st,0);
  14020. return 0;
  14021. }
  14022. if (flag)
  14023. nd->master_nd = ((struct npc_data *)map_id2bl(st->oid));
  14024. else
  14025. nd->master_nd = NULL;
  14026. script_pushint(st,1);
  14027. return SCRIPT_CMD_SUCCESS;
  14028. }
  14029. /*==========================================
  14030. * Returns some values of an item [Lupus]
  14031. * Price, Weight, etc...
  14032. setitemscript(itemID,"{new item bonus script}",[n]);
  14033. Where n:
  14034. 0 - script
  14035. 1 - Equip script
  14036. 2 - Unequip script
  14037. *------------------------------------------*/
  14038. BUILDIN_FUNC(setitemscript)
  14039. {
  14040. unsigned short item_id;
  14041. int n = 0;
  14042. const char *script;
  14043. struct item_data *i_data;
  14044. struct script_code **dstscript;
  14045. item_id = script_getnum(st,2);
  14046. script = script_getstr(st,3);
  14047. if( script_hasdata(st,4) )
  14048. n=script_getnum(st,4);
  14049. i_data = itemdb_exists(item_id);
  14050. if (!i_data || script==NULL || ( script[0] && script[0]!='{' )) {
  14051. script_pushint(st,0);
  14052. return 0;
  14053. }
  14054. switch (n) {
  14055. case 2:
  14056. dstscript = &i_data->unequip_script;
  14057. break;
  14058. case 1:
  14059. dstscript = &i_data->equip_script;
  14060. break;
  14061. default:
  14062. dstscript = &i_data->script;
  14063. break;
  14064. }
  14065. if(*dstscript)
  14066. script_free_code(*dstscript);
  14067. *dstscript = script[0] ? parse_script(script, "script_setitemscript", 0, 0) : NULL;
  14068. script_pushint(st,1);
  14069. return SCRIPT_CMD_SUCCESS;
  14070. }
  14071. /*=======================================================
  14072. * Add or Update a mob drop temporarily [Akinari]
  14073. * Original Idea By: [Lupus]
  14074. *
  14075. * addmonsterdrop <mob_id or name>,<item_id>,<rate>;
  14076. *
  14077. * If given an item the mob already drops, the rate
  14078. * is updated to the new rate. Rate cannot exceed 10000
  14079. * Returns 1 if succeeded (added/updated a mob drop)
  14080. *-------------------------------------------------------*/
  14081. BUILDIN_FUNC(addmonsterdrop)
  14082. {
  14083. struct mob_db *mob;
  14084. struct script_data *data;
  14085. unsigned short item_id;
  14086. int rate;
  14087. data = script_getdata(st, 2);
  14088. get_val(st, data); // Convert into value in case of a variable
  14089. if(data_isstring(data))
  14090. mob = mob_db(mobdb_searchname(script_getstr(st,2)));
  14091. else
  14092. mob = mob_db(script_getnum(st,2));
  14093. item_id=script_getnum(st,3);
  14094. rate=script_getnum(st,4);
  14095. if(!itemdb_exists(item_id)){
  14096. ShowError("addmonsterdrop: Nonexistant item %hu requested.\n", item_id );
  14097. return 1;
  14098. }
  14099. if(mob) { //We got a valid monster, check for available drop slot
  14100. unsigned char i, c = 0;
  14101. for(i = 0; i < MAX_MOB_DROP; i++) {
  14102. if(mob->dropitem[i].nameid) {
  14103. if(mob->dropitem[i].nameid == item_id) { //If it equals item_id we update that drop
  14104. c = i;
  14105. break;
  14106. }
  14107. continue;
  14108. }
  14109. if(!c) //Accept first available slot only
  14110. c = i;
  14111. }
  14112. if(c) { //Fill in the slot with the item and rate
  14113. mob->dropitem[c].nameid = item_id;
  14114. mob->dropitem[c].p = (rate > 10000)?10000:rate;
  14115. script_pushint(st,1);
  14116. } else //No place to put the new drop
  14117. script_pushint(st,0);
  14118. } else {
  14119. ShowWarning("addmonsterdrop: bad mob id given %d\n",script_getnum(st,2));
  14120. return 1;
  14121. }
  14122. return SCRIPT_CMD_SUCCESS;
  14123. }
  14124. /*=======================================================
  14125. * Delete a mob drop temporarily [Akinari]
  14126. * Original Idea By: [Lupus]
  14127. *
  14128. * delmonsterdrop <mob_id or name>,<item_id>;
  14129. *
  14130. * Returns 1 if succeeded (deleted a mob drop)
  14131. *-------------------------------------------------------*/
  14132. BUILDIN_FUNC(delmonsterdrop)
  14133. {
  14134. struct mob_db *mob;
  14135. struct script_data *data;
  14136. unsigned short item_id;
  14137. data = script_getdata(st, 2);
  14138. get_val(st, data); // Convert into value in case of a variable
  14139. if(data_isstring(data))
  14140. mob = mob_db(mobdb_searchname(script_getstr(st,2)));
  14141. else
  14142. mob = mob_db(script_getnum(st,2));
  14143. item_id=script_getnum(st,3);
  14144. if(!itemdb_exists(item_id)){
  14145. ShowError("delmonsterdrop: Nonexistant item %hu requested.\n", item_id );
  14146. return 1;
  14147. }
  14148. if(mob) { //We got a valid monster, check for item drop on monster
  14149. unsigned char i;
  14150. for(i = 0; i < MAX_MOB_DROP; i++) {
  14151. if(mob->dropitem[i].nameid == item_id) {
  14152. mob->dropitem[i].nameid = 0;
  14153. mob->dropitem[i].p = 0;
  14154. script_pushint(st,1);
  14155. return 0;
  14156. }
  14157. }
  14158. //No drop on that monster
  14159. script_pushint(st,0);
  14160. } else {
  14161. ShowWarning("delmonsterdrop: bad mob id given %d\n",script_getnum(st,2));
  14162. return 1;
  14163. }
  14164. return SCRIPT_CMD_SUCCESS;
  14165. }
  14166. /*==========================================
  14167. * Returns some values of a monster [Lupus]
  14168. * Name, Level, race, size, etc...
  14169. getmonsterinfo(monsterID,queryIndex);
  14170. *------------------------------------------*/
  14171. BUILDIN_FUNC(getmonsterinfo)
  14172. {
  14173. struct mob_db *mob;
  14174. int mob_id;
  14175. mob_id = script_getnum(st,2);
  14176. if (!mobdb_checkid(mob_id)) {
  14177. //ShowError("buildin_getmonsterinfo: Wrong Monster ID: %i\n", mob_id);
  14178. if ( !script_getnum(st,3) ) //requested a string
  14179. script_pushconststr(st,"null");
  14180. else
  14181. script_pushint(st,-1);
  14182. return 0;
  14183. }
  14184. mob = mob_db(mob_id);
  14185. switch ( script_getnum(st,3) ) {
  14186. case 0: script_pushstrcopy(st,mob->jname); break;
  14187. case 1: script_pushint(st,mob->lv); break;
  14188. case 2: script_pushint(st,mob->status.max_hp); break;
  14189. case 3: script_pushint(st,mob->base_exp); break;
  14190. case 4: script_pushint(st,mob->job_exp); break;
  14191. case 5: script_pushint(st,mob->status.rhw.atk); break;
  14192. case 6: script_pushint(st,mob->status.rhw.atk2); break;
  14193. case 7: script_pushint(st,mob->status.def); break;
  14194. case 8: script_pushint(st,mob->status.mdef); break;
  14195. case 9: script_pushint(st,mob->status.str); break;
  14196. case 10: script_pushint(st,mob->status.agi); break;
  14197. case 11: script_pushint(st,mob->status.vit); break;
  14198. case 12: script_pushint(st,mob->status.int_); break;
  14199. case 13: script_pushint(st,mob->status.dex); break;
  14200. case 14: script_pushint(st,mob->status.luk); break;
  14201. case 15: script_pushint(st,mob->status.rhw.range); break;
  14202. case 16: script_pushint(st,mob->range2); break;
  14203. case 17: script_pushint(st,mob->range3); break;
  14204. case 18: script_pushint(st,mob->status.size); break;
  14205. case 19: script_pushint(st,mob->status.race); break;
  14206. case 20: script_pushint(st,mob->status.def_ele); break;
  14207. case 21: script_pushint(st,mob->status.mode); break;
  14208. case 22: script_pushint(st,mob->mexp); break;
  14209. default: script_pushint(st,-1); //wrong Index
  14210. }
  14211. return SCRIPT_CMD_SUCCESS;
  14212. }
  14213. /**
  14214. * Check player's vending/buyingstore/autotrade state
  14215. * checkvending({"<Player Name>"})
  14216. * @author [Nab4]
  14217. */
  14218. BUILDIN_FUNC(checkvending) {
  14219. TBL_PC *sd = NULL;
  14220. if (script_hasdata(st,2)) {
  14221. if (!(sd = map_nick2sd(script_getstr(st,2)))) {
  14222. ShowError("buildin_checkvending: Player '%s' is not online!\n", script_getstr(st,2));
  14223. return SCRIPT_CMD_FAILURE;
  14224. }
  14225. }
  14226. else
  14227. sd = script_rid2sd(st);
  14228. if (!sd) {
  14229. script_pushint(st,0);
  14230. return SCRIPT_CMD_SUCCESS;
  14231. }
  14232. else {
  14233. int8 ret = 0;
  14234. if (sd->state.vending)
  14235. ret = 1;
  14236. else if (sd->state.buyingstore)
  14237. ret = 4;
  14238. if (sd->state.autotrade)
  14239. ret |= 2;
  14240. script_pushint(st, ret);
  14241. }
  14242. return SCRIPT_CMD_SUCCESS;
  14243. }
  14244. BUILDIN_FUNC(checkchatting) // check chatting [Marka]
  14245. {
  14246. TBL_PC *sd = NULL;
  14247. if(script_hasdata(st,2))
  14248. sd = map_nick2sd(script_getstr(st,2));
  14249. else
  14250. sd = script_rid2sd(st);
  14251. if(sd)
  14252. script_pushint(st,(sd->chatID != 0));
  14253. else
  14254. script_pushint(st,0);
  14255. return SCRIPT_CMD_SUCCESS;
  14256. }
  14257. BUILDIN_FUNC(checkidle)
  14258. {
  14259. TBL_PC *sd = NULL;
  14260. if (script_hasdata(st, 2))
  14261. sd = map_nick2sd(script_getstr(st, 2));
  14262. else
  14263. sd = script_rid2sd(st);
  14264. if (sd)
  14265. script_pushint(st, DIFF_TICK(last_tick, sd->idletime));
  14266. else
  14267. script_pushint(st, 0);
  14268. return SCRIPT_CMD_SUCCESS;
  14269. }
  14270. BUILDIN_FUNC(searchitem)
  14271. {
  14272. struct script_data* data = script_getdata(st, 2);
  14273. const char *itemname = script_getstr(st,3);
  14274. struct item_data *items[MAX_SEARCH];
  14275. int count;
  14276. char* name;
  14277. int32 start;
  14278. int32 id;
  14279. int32 i;
  14280. TBL_PC* sd = NULL;
  14281. if ((items[0] = itemdb_exists(atoi(itemname))))
  14282. count = 1;
  14283. else {
  14284. count = itemdb_searchname_array(items, ARRAYLENGTH(items), itemname);
  14285. if (count > MAX_SEARCH) count = MAX_SEARCH;
  14286. }
  14287. if (!count) {
  14288. script_pushint(st, 0);
  14289. return 0;
  14290. }
  14291. if( !data_isreference(data) )
  14292. {
  14293. ShowError("script:searchitem: not a variable\n");
  14294. script_reportdata(data);
  14295. st->state = END;
  14296. return 1;// not a variable
  14297. }
  14298. id = reference_getid(data);
  14299. start = reference_getindex(data);
  14300. name = reference_getname(data);
  14301. if( not_array_variable(*name) )
  14302. {
  14303. ShowError("script:searchitem: illegal scope\n");
  14304. script_reportdata(data);
  14305. st->state = END;
  14306. return 1;// not supported
  14307. }
  14308. if( not_server_variable(*name) )
  14309. {
  14310. sd = script_rid2sd(st);
  14311. if( sd == NULL )
  14312. return 0;// no player attached
  14313. }
  14314. if( is_string_variable(name) )
  14315. {// string array
  14316. ShowError("script:searchitem: not an integer array reference\n");
  14317. script_reportdata(data);
  14318. st->state = END;
  14319. return 1;// not supported
  14320. }
  14321. for( i = 0; i < count; ++start, ++i )
  14322. {// Set array
  14323. void* v = (void*)__64BPRTSIZE((int)items[i]->nameid);
  14324. set_reg(st, sd, reference_uid(id, start), name, v, reference_getref(data));
  14325. }
  14326. script_pushint(st, count);
  14327. return SCRIPT_CMD_SUCCESS;
  14328. }
  14329. // [zBuffer] List of player cont commands --->
  14330. BUILDIN_FUNC(rid2name)
  14331. {
  14332. struct block_list *bl = NULL;
  14333. int rid = script_getnum(st,2);
  14334. if((bl = map_id2bl(rid)))
  14335. {
  14336. switch(bl->type) {
  14337. case BL_MOB: script_pushstrcopy(st,((TBL_MOB*)bl)->name); break;
  14338. case BL_PC: script_pushstrcopy(st,((TBL_PC*)bl)->status.name); break;
  14339. case BL_NPC: script_pushstrcopy(st,((TBL_NPC*)bl)->exname); break;
  14340. case BL_PET: script_pushstrcopy(st,((TBL_PET*)bl)->pet.name); break;
  14341. case BL_HOM: script_pushstrcopy(st,((TBL_HOM*)bl)->homunculus.name); break;
  14342. case BL_MER: script_pushstrcopy(st,((TBL_MER*)bl)->db->name); break;
  14343. default:
  14344. ShowError("buildin_rid2name: BL type unknown.\n");
  14345. script_pushconststr(st,"");
  14346. break;
  14347. }
  14348. } else {
  14349. ShowError("buildin_rid2name: invalid RID\n");
  14350. script_pushconststr(st,"(null)");
  14351. }
  14352. return SCRIPT_CMD_SUCCESS;
  14353. }
  14354. BUILDIN_FUNC(pcblockmove)
  14355. {
  14356. int id, flag;
  14357. TBL_PC *sd = NULL;
  14358. id = script_getnum(st,2);
  14359. flag = script_getnum(st,3);
  14360. if(id)
  14361. sd = map_id2sd(id);
  14362. else
  14363. sd = script_rid2sd(st);
  14364. if(sd)
  14365. sd->state.blockedmove = flag > 0;
  14366. return SCRIPT_CMD_SUCCESS;
  14367. }
  14368. BUILDIN_FUNC(pcfollow)
  14369. {
  14370. int id, targetid;
  14371. TBL_PC *sd = NULL;
  14372. id = script_getnum(st,2);
  14373. targetid = script_getnum(st,3);
  14374. if(id)
  14375. sd = map_id2sd(id);
  14376. else
  14377. sd = script_rid2sd(st);
  14378. if(sd)
  14379. pc_follow(sd, targetid);
  14380. return SCRIPT_CMD_SUCCESS;
  14381. }
  14382. BUILDIN_FUNC(pcstopfollow)
  14383. {
  14384. int id;
  14385. TBL_PC *sd = NULL;
  14386. id = script_getnum(st,2);
  14387. if(id)
  14388. sd = map_id2sd(id);
  14389. else
  14390. sd = script_rid2sd(st);
  14391. if(sd)
  14392. pc_stop_following(sd);
  14393. return SCRIPT_CMD_SUCCESS;
  14394. }
  14395. // <--- [zBuffer] List of player cont commands
  14396. // [zBuffer] List of unit control commands --->
  14397. /// Gets the type of the given Game ID.
  14398. ///
  14399. /// getunittype <unit id>;
  14400. BUILDIN_FUNC(getunittype)
  14401. {
  14402. struct block_list* bl;
  14403. uint8 value = 0;
  14404. bl = map_id2bl(script_getnum(st, 2));
  14405. if (!bl) {
  14406. ShowWarning("buildin_getunittype: Error in finding object with given game ID %d!\n", script_getnum(st, 2));
  14407. script_pushint(st, -1);
  14408. return SCRIPT_CMD_FAILURE;
  14409. }
  14410. switch (bl->type) {
  14411. case BL_MOB: value = 0; break;
  14412. case BL_HOM: value = 1; break;
  14413. case BL_PET: value = 2; break;
  14414. case BL_MER: value = 3; break;
  14415. case BL_ELEM: value = 4; break;
  14416. case BL_NPC: value = 5; break;
  14417. }
  14418. script_pushint(st, value);
  14419. return SCRIPT_CMD_SUCCESS;
  14420. }
  14421. /// Gets specific live information of a bl.
  14422. ///
  14423. /// getunitdata <unit id>,<arrayname>;
  14424. BUILDIN_FUNC(getunitdata)
  14425. {
  14426. TBL_PC *sd = st->rid ? map_id2sd(st->rid) : NULL;
  14427. struct block_list* bl;
  14428. TBL_MOB* md = NULL;
  14429. TBL_HOM* hd = NULL;
  14430. TBL_MER* mc = NULL;
  14431. TBL_PET* pd = NULL;
  14432. TBL_ELEM* ed = NULL;
  14433. TBL_NPC* nd = NULL;
  14434. int num;
  14435. char* name;
  14436. struct script_data *data = script_getdata(st, 3);
  14437. if (!data_isreference(data)) {
  14438. ShowWarning("buildin_getunitdata: Error in argument! Please give a variable to store values in.\n");
  14439. return SCRIPT_CMD_FAILURE;
  14440. }
  14441. bl = map_id2bl(script_getnum(st, 2));
  14442. if (!bl) {
  14443. ShowWarning("buildin_getunitdata: Error in finding object with given game ID %d!\n", script_getnum(st, 2));
  14444. return SCRIPT_CMD_FAILURE;
  14445. }
  14446. switch (bl->type) {
  14447. case BL_MOB: md = map_id2md(bl->id); break;
  14448. case BL_HOM: hd = map_id2hd(bl->id); break;
  14449. case BL_PET: pd = map_id2pd(bl->id); break;
  14450. case BL_MER: mc = map_id2mc(bl->id); break;
  14451. case BL_ELEM: ed = map_id2ed(bl->id); break;
  14452. case BL_NPC: nd = map_id2nd(bl->id); break;
  14453. }
  14454. num = reference_getuid(data);
  14455. name = reference_getname(data);
  14456. #define getunitdata_sub(idx__,var__) setd_sub(st,sd,name,(idx__),(void *)__64BPRTSIZE((int)(var__)),data->ref)
  14457. switch(bl->type) {
  14458. case BL_MOB:
  14459. if (!md) {
  14460. ShowWarning("buildin_getunitdata: Error in finding object BL_MOB!\n");
  14461. return SCRIPT_CMD_FAILURE;
  14462. }
  14463. getunitdata_sub(0, md->status.size);
  14464. getunitdata_sub(1, md->level);
  14465. getunitdata_sub(2, md->status.hp);
  14466. getunitdata_sub(3, md->status.max_hp);
  14467. getunitdata_sub(4, md->master_id);
  14468. getunitdata_sub(5, md->bl.m);
  14469. getunitdata_sub(6, md->bl.x);
  14470. getunitdata_sub(7, md->bl.y);
  14471. getunitdata_sub(8, md->status.speed);
  14472. getunitdata_sub(9, md->status.mode);
  14473. getunitdata_sub(10,md->special_state.ai);
  14474. getunitdata_sub(11,md->sc.option);
  14475. getunitdata_sub(12,md->vd->sex);
  14476. getunitdata_sub(13,md->vd->class_);
  14477. getunitdata_sub(14,md->vd->hair_style);
  14478. getunitdata_sub(15,md->vd->hair_color);
  14479. getunitdata_sub(16,md->vd->head_bottom);
  14480. getunitdata_sub(17,md->vd->head_mid);
  14481. getunitdata_sub(18,md->vd->head_top);
  14482. getunitdata_sub(19,md->vd->cloth_color);
  14483. getunitdata_sub(20,md->vd->shield);
  14484. getunitdata_sub(21,md->vd->weapon);
  14485. getunitdata_sub(22,md->ud.dir);
  14486. getunitdata_sub(23,md->status.str);
  14487. getunitdata_sub(24,md->status.agi);
  14488. getunitdata_sub(25,md->status.vit);
  14489. getunitdata_sub(26,md->status.int_);
  14490. getunitdata_sub(27,md->status.dex);
  14491. getunitdata_sub(28,md->status.luk);
  14492. getunitdata_sub(29,md->state.copy_master_mode);
  14493. getunitdata_sub(30,md->ud.immune_attack);
  14494. break;
  14495. case BL_HOM:
  14496. if (!hd) {
  14497. ShowWarning("buildin_getunitdata: Error in finding object BL_HOM!\n");
  14498. return SCRIPT_CMD_FAILURE;
  14499. }
  14500. getunitdata_sub(0, hd->base_status.size);
  14501. getunitdata_sub(1, hd->homunculus.level);
  14502. getunitdata_sub(2, hd->homunculus.hp);
  14503. getunitdata_sub(3, hd->homunculus.max_hp);
  14504. getunitdata_sub(4, hd->homunculus.sp);
  14505. getunitdata_sub(5, hd->homunculus.max_sp);
  14506. getunitdata_sub(6, hd->homunculus.char_id);
  14507. getunitdata_sub(7, hd->bl.m);
  14508. getunitdata_sub(8, hd->bl.x);
  14509. getunitdata_sub(9, hd->bl.y);
  14510. getunitdata_sub(10,hd->homunculus.hunger);
  14511. getunitdata_sub(11,hd->homunculus.intimacy);
  14512. getunitdata_sub(12,hd->base_status.speed);
  14513. getunitdata_sub(13,hd->ud.dir);
  14514. getunitdata_sub(14,hd->ud.canmove_tick);
  14515. getunitdata_sub(15,hd->base_status.str);
  14516. getunitdata_sub(16,hd->base_status.agi);
  14517. getunitdata_sub(17,hd->base_status.vit);
  14518. getunitdata_sub(18,hd->base_status.int_);
  14519. getunitdata_sub(19,hd->base_status.dex);
  14520. getunitdata_sub(20,hd->base_status.luk);
  14521. getunitdata_sub(21,hd->ud.immune_attack);
  14522. break;
  14523. case BL_PET:
  14524. if (!pd) {
  14525. ShowWarning("buildin_getunitdata: Error in finding object BL_PET!\n");
  14526. return SCRIPT_CMD_FAILURE;
  14527. }
  14528. getunitdata_sub(0, pd->status.size);
  14529. getunitdata_sub(1, pd->pet.level);
  14530. getunitdata_sub(2, pd->status.hp);
  14531. getunitdata_sub(3, pd->status.max_hp);
  14532. getunitdata_sub(4, pd->pet.account_id);
  14533. getunitdata_sub(5, pd->bl.m);
  14534. getunitdata_sub(6, pd->bl.x);
  14535. getunitdata_sub(7, pd->bl.y);
  14536. getunitdata_sub(8, pd->pet.hungry);
  14537. getunitdata_sub(9, pd->pet.intimate);
  14538. getunitdata_sub(10,pd->status.speed);
  14539. getunitdata_sub(11,pd->ud.dir);
  14540. getunitdata_sub(12,pd->ud.canmove_tick);
  14541. getunitdata_sub(13,pd->status.str);
  14542. getunitdata_sub(14,pd->status.agi);
  14543. getunitdata_sub(15,pd->status.vit);
  14544. getunitdata_sub(16,pd->status.int_);
  14545. getunitdata_sub(17,pd->status.dex);
  14546. getunitdata_sub(18,pd->status.luk);
  14547. getunitdata_sub(19,pd->ud.immune_attack);
  14548. break;
  14549. case BL_MER:
  14550. if (!mc) {
  14551. ShowWarning("buildin_getunitdata: Error in finding object BL_MER!\n");
  14552. return SCRIPT_CMD_FAILURE;
  14553. }
  14554. getunitdata_sub(0, mc->base_status.size);
  14555. getunitdata_sub(1, mc->base_status.hp);
  14556. getunitdata_sub(2, mc->base_status.max_hp);
  14557. getunitdata_sub(3, mc->mercenary.char_id);
  14558. getunitdata_sub(4, mc->bl.m);
  14559. getunitdata_sub(5, mc->bl.x);
  14560. getunitdata_sub(6, mc->bl.y);
  14561. getunitdata_sub(7, mc->mercenary.kill_count);
  14562. getunitdata_sub(8, mc->mercenary.life_time);
  14563. getunitdata_sub(9, mc->base_status.speed);
  14564. getunitdata_sub(10,mc->ud.dir);
  14565. getunitdata_sub(11,mc->ud.canmove_tick);
  14566. getunitdata_sub(12,mc->base_status.str);
  14567. getunitdata_sub(13,mc->base_status.agi);
  14568. getunitdata_sub(14,mc->base_status.vit);
  14569. getunitdata_sub(15,mc->base_status.int_);
  14570. getunitdata_sub(16,mc->base_status.dex);
  14571. getunitdata_sub(17,mc->base_status.luk);
  14572. getunitdata_sub(18,mc->ud.immune_attack);
  14573. break;
  14574. case BL_ELEM:
  14575. if (!ed) {
  14576. ShowWarning("buildin_getunitdata: Error in finding object BL_ELEM!\n");
  14577. return SCRIPT_CMD_FAILURE;
  14578. }
  14579. getunitdata_sub(0, ed->base_status.size);
  14580. getunitdata_sub(1, ed->elemental.hp);
  14581. getunitdata_sub(2, ed->elemental.max_hp);
  14582. getunitdata_sub(3, ed->elemental.sp);
  14583. getunitdata_sub(4, ed->elemental.max_sp);
  14584. getunitdata_sub(5, ed->elemental.char_id);
  14585. getunitdata_sub(6, ed->bl.m);
  14586. getunitdata_sub(7, ed->bl.x);
  14587. getunitdata_sub(8, ed->bl.y);
  14588. getunitdata_sub(9, ed->elemental.life_time);
  14589. getunitdata_sub(10,ed->elemental.mode);
  14590. getunitdata_sub(11,ed->base_status.speed);
  14591. getunitdata_sub(12,ed->ud.dir);
  14592. getunitdata_sub(13,ed->ud.canmove_tick);
  14593. getunitdata_sub(14,ed->base_status.str);
  14594. getunitdata_sub(15,ed->base_status.agi);
  14595. getunitdata_sub(16,ed->base_status.vit);
  14596. getunitdata_sub(17,ed->base_status.int_);
  14597. getunitdata_sub(18,ed->base_status.dex);
  14598. getunitdata_sub(19,ed->base_status.luk);
  14599. getunitdata_sub(20,ed->ud.immune_attack);
  14600. break;
  14601. case BL_NPC:
  14602. if (!nd) {
  14603. ShowWarning("buildin_getunitdata: Error in finding object BL_NPC!\n");
  14604. return SCRIPT_CMD_FAILURE;
  14605. }
  14606. getunitdata_sub(0, nd->class_);
  14607. getunitdata_sub(1, nd->level);
  14608. getunitdata_sub(2, nd->status.hp);
  14609. getunitdata_sub(3, nd->status.max_hp);
  14610. getunitdata_sub(4, nd->bl.m);
  14611. getunitdata_sub(5, nd->bl.x);
  14612. getunitdata_sub(6, nd->bl.y);
  14613. getunitdata_sub(7, nd->ud.dir);
  14614. getunitdata_sub(8, nd->status.str);
  14615. getunitdata_sub(9, nd->status.agi);
  14616. getunitdata_sub(10,nd->status.vit);
  14617. getunitdata_sub(11,nd->status.int_);
  14618. getunitdata_sub(12,nd->status.dex);
  14619. getunitdata_sub(13,nd->status.luk);
  14620. break;
  14621. default:
  14622. ShowWarning("buildin_getunitdata: Unknown object type!\n");
  14623. return SCRIPT_CMD_FAILURE;
  14624. }
  14625. #undef getunitdata_sub
  14626. return SCRIPT_CMD_SUCCESS;
  14627. }
  14628. /// Changes the live data of a bl.
  14629. ///
  14630. /// setunitdata <unit id>,<type>,<value>;
  14631. BUILDIN_FUNC(setunitdata)
  14632. {
  14633. struct block_list* bl = NULL;
  14634. struct script_data* data;
  14635. const char *mapname = NULL;
  14636. TBL_MOB* md = NULL;
  14637. TBL_HOM* hd = NULL;
  14638. TBL_MER* mc = NULL;
  14639. TBL_PET* pd = NULL;
  14640. TBL_ELEM* ed = NULL;
  14641. TBL_NPC* nd = NULL;
  14642. int type, value = 0;
  14643. bl = map_id2bl(script_getnum(st, 2));
  14644. if (!bl) {
  14645. ShowWarning("buildin_setunitdata: Error in finding object with given game ID %d!\n", script_getnum(st, 2));
  14646. return SCRIPT_CMD_FAILURE;
  14647. }
  14648. switch (bl->type) {
  14649. case BL_MOB: md = map_id2md(bl->id); break;
  14650. case BL_HOM: hd = map_id2hd(bl->id); break;
  14651. case BL_PET: pd = map_id2pd(bl->id); break;
  14652. case BL_MER: mc = map_id2mc(bl->id); break;
  14653. case BL_ELEM: ed = map_id2ed(bl->id); break;
  14654. case BL_NPC:
  14655. nd = map_id2nd(bl->id);
  14656. if (!nd->status.hp)
  14657. status_calc_npc(nd, SCO_FIRST);
  14658. else
  14659. status_calc_npc(nd, SCO_NONE);
  14660. break;
  14661. default:
  14662. ShowError("buildin_setunitdata: Invalid object!");
  14663. return SCRIPT_CMD_FAILURE;
  14664. }
  14665. type = script_getnum(st, 3);
  14666. data = script_getdata(st, 4);
  14667. get_val(st, data);
  14668. if (type == 5 && data_isstring(data))
  14669. mapname = conv_str(st, data);
  14670. else if (data_isint(data))
  14671. value = conv_num(st, data);
  14672. else {
  14673. ShowError("buildin_setunitdata: Invalid data type for argument #3 (%d).", data->type);
  14674. return SCRIPT_CMD_FAILURE;
  14675. }
  14676. switch (bl->type) {
  14677. case BL_MOB:
  14678. if (!md) {
  14679. ShowWarning("buildin_setunitdata: Error in finding object BL_MOB!\n");
  14680. return SCRIPT_CMD_FAILURE;
  14681. }
  14682. switch (type) {
  14683. case 0: md->status.size = (unsigned char)value; break;
  14684. case 1: md->level = (unsigned short)value; break;
  14685. case 2: status_set_hp(bl, (unsigned int)value, 0); break;
  14686. case 3: status_set_maxhp(bl, (unsigned int)value, 0); break;
  14687. case 4: md->master_id = value; break;
  14688. case 5: if (mapname) value = map_mapname2mapid(mapname); unit_warp(bl, (short)value, 0, 0, 3); break;
  14689. case 6: if (!unit_walktoxy(bl, (short)value, md->bl.y, 2)) unit_movepos(bl, (short)value, md->bl.y, 0, 0); break;
  14690. case 7: if (!unit_walktoxy(bl, md->bl.x, (short)value, 2)) unit_movepos(bl, md->bl.x, (short)value, 0, 0); break;
  14691. case 8: md->status.speed = (unsigned short)value; status_calc_bl(bl, SCB_ALL); break;
  14692. case 9: md->status.mode = (enum e_mode)value; status_calc_bl(bl, SCB_MODE); break;
  14693. case 10: md->special_state.ai = (enum mob_ai)value; break;
  14694. case 11: md->sc.option = (unsigned short)value; break;
  14695. case 12: md->vd->sex = (char)value; break;
  14696. case 13: status_set_viewdata(bl, (unsigned short)value); break;
  14697. case 14: clif_changelook(bl, LOOK_HAIR, (unsigned short)value); break;
  14698. case 15: clif_changelook(bl, LOOK_HAIR_COLOR, (unsigned short)value); break;
  14699. case 16: clif_changelook(bl, LOOK_HEAD_BOTTOM, (unsigned short)value); break;
  14700. case 17: clif_changelook(bl, LOOK_HEAD_MID, (unsigned short)value); break;
  14701. case 18: clif_changelook(bl, LOOK_HEAD_TOP, (unsigned short)value); break;
  14702. case 19: clif_changelook(bl, LOOK_CLOTHES_COLOR, (unsigned short)value); break;
  14703. case 20: clif_changelook(bl, LOOK_SHIELD, (unsigned short)value); break;
  14704. case 21: clif_changelook(bl, LOOK_WEAPON, (unsigned short)value); break;
  14705. case 22: unit_setdir(bl, (uint8)value); break;
  14706. case 23: md->status.str = (unsigned short)value; status_calc_bl(bl, SCB_ALL); break;
  14707. case 24: md->status.agi = (unsigned short)value; status_calc_bl(bl, SCB_ALL); break;
  14708. case 25: md->status.vit = (unsigned short)value; status_calc_bl(bl, SCB_ALL); break;
  14709. case 26: md->status.int_ = (unsigned short)value; status_calc_bl(bl, SCB_ALL); break;
  14710. case 27: md->status.dex = (unsigned short)value; status_calc_bl(bl, SCB_ALL); break;
  14711. case 28: md->status.luk = (unsigned short)value; status_calc_bl(bl, SCB_ALL); break;
  14712. case 29: md->state.copy_master_mode = value > 0 ? 1 : 0; status_calc_bl(bl, SCB_MODE); break;
  14713. case 30: md->ud.immune_attack = (bool)value > 0 ? 1 : 0; break;
  14714. default:
  14715. ShowError("buildin_setunitdata: Unknown data identifier %d for BL_MOB.\n", type);
  14716. return SCRIPT_CMD_FAILURE;
  14717. }
  14718. break;
  14719. case BL_HOM:
  14720. if (!hd) {
  14721. ShowWarning("buildin_setunitdata: Error in finding object BL_HOM!\n");
  14722. return SCRIPT_CMD_FAILURE;
  14723. }
  14724. switch (type) {
  14725. case 0: hd->base_status.size = (unsigned char)value; break;
  14726. case 1: hd->homunculus.level = (unsigned short)value; break;
  14727. case 2: status_set_hp(bl, (unsigned int)value, 0); break;
  14728. case 3: status_set_maxhp(bl, (unsigned int)value, 0); break;
  14729. case 4: status_set_sp(bl, (unsigned int)value, 0); break;
  14730. case 5: status_set_maxsp(bl, (unsigned int)value, 0); break;
  14731. case 6: hd->homunculus.char_id = (uint32)value; break;
  14732. case 7: if (mapname) value = map_mapname2mapid(mapname); unit_warp(bl, (short)value, 0, 0, 3); break;
  14733. case 8: if (!unit_walktoxy(bl, (short)value, hd->bl.y, 2)) unit_movepos(bl, (short)value, hd->bl.y, 0, 0); break;
  14734. case 9: if (!unit_walktoxy(bl, hd->bl.x, (short)value, 2)) unit_movepos(bl, hd->bl.x, (short)value, 0, 0); break;
  14735. case 10: hd->homunculus.hunger = (short)value; clif_send_homdata(map_charid2sd(hd->homunculus.char_id), SP_HUNGRY, hd->homunculus.hunger); break;
  14736. case 11: hom_increase_intimacy(hd, (unsigned int)value); clif_send_homdata(map_charid2sd(hd->homunculus.char_id), SP_INTIMATE, hd->homunculus.intimacy / 100); break;
  14737. case 12: hd->base_status.speed = (unsigned short)value; status_calc_bl(bl, SCB_ALL); break;
  14738. case 13: unit_setdir(bl, (uint8)value); break;
  14739. case 14: hd->ud.canmove_tick = value > 0 ? 1 : 0; break;
  14740. case 15: hd->base_status.str = (unsigned short)value; status_calc_bl(bl, SCB_ALL); break;
  14741. case 16: hd->base_status.agi = (unsigned short)value; status_calc_bl(bl, SCB_ALL); break;
  14742. case 17: hd->base_status.vit = (unsigned short)value; status_calc_bl(bl, SCB_ALL); break;
  14743. case 18: hd->base_status.int_ = (unsigned short)value; status_calc_bl(bl, SCB_ALL); break;
  14744. case 19: hd->base_status.dex = (unsigned short)value; status_calc_bl(bl, SCB_ALL); break;
  14745. case 20: hd->base_status.luk = (unsigned short)value; status_calc_bl(bl, SCB_ALL); break;
  14746. case 21: hd->ud.immune_attack = (bool)value > 0 ? 1 : 0; break;
  14747. default:
  14748. ShowError("buildin_setunitdata: Unknown data identifier %d for BL_HOM.\n", type);
  14749. return SCRIPT_CMD_FAILURE;
  14750. }
  14751. break;
  14752. case BL_PET:
  14753. if (!pd) {
  14754. ShowWarning("buildin_setunitdata: Error in finding object BL_PET!\n");
  14755. return SCRIPT_CMD_FAILURE;
  14756. }
  14757. switch (type) {
  14758. case 0: pd->status.size = (unsigned char)value; break;
  14759. case 1: pd->pet.level = (unsigned short)value; break;
  14760. case 2: status_set_hp(bl, (unsigned int)value, 0); break;
  14761. case 3: status_set_maxhp(bl, (unsigned int)value, 0); break;
  14762. case 4: pd->pet.account_id = (unsigned int)value; break;
  14763. case 5: if (mapname) value = map_mapname2mapid(mapname); unit_warp(bl, (short)value, 0, 0, 3); break;
  14764. case 6: if (!unit_walktoxy(bl, (short)value, pd->bl.y, 2)) unit_movepos(bl, (short)value, md->bl.y, 0, 0); break;
  14765. case 7: if (!unit_walktoxy(bl, pd->bl.x, (short)value, 2)) unit_movepos(bl, pd->bl.x, (short)value, 0, 0); break;
  14766. case 8: pd->pet.hungry = (short)value; clif_send_petdata(map_id2sd(pd->pet.account_id), pd, 2, pd->pet.hungry); break;
  14767. case 9: pet_set_intimate(pd, (unsigned int)value); clif_send_petdata(map_id2sd(pd->pet.account_id), pd, 1, pd->pet.intimate); break;
  14768. case 10: pd->status.speed = (unsigned short)value; status_calc_bl(bl, SCB_ALL); break;
  14769. case 11: unit_setdir(bl, (uint8)value); break;
  14770. case 12: pd->ud.canmove_tick = value > 0 ? 1 : 0; break;
  14771. case 13: pd->status.str = (unsigned short)value; status_calc_bl(bl, SCB_ALL); break;
  14772. case 14: pd->status.agi = (unsigned short)value; status_calc_bl(bl, SCB_ALL); break;
  14773. case 15: pd->status.vit = (unsigned short)value; status_calc_bl(bl, SCB_ALL); break;
  14774. case 16: pd->status.int_ = (unsigned short)value; status_calc_bl(bl, SCB_ALL); break;
  14775. case 17: pd->status.dex = (unsigned short)value; status_calc_bl(bl, SCB_ALL); break;
  14776. case 18: pd->status.luk = (unsigned short)value; status_calc_bl(bl, SCB_ALL); break;
  14777. case 19: pd->ud.immune_attack = (bool)value > 0 ? 1 : 0; break;
  14778. default:
  14779. ShowError("buildin_setunitdata: Unknown data identifier %d for BL_PET.\n", type);
  14780. return SCRIPT_CMD_FAILURE;
  14781. }
  14782. break;
  14783. case BL_MER:
  14784. if (!mc) {
  14785. ShowWarning("buildin_setunitdata: Error in finding object BL_MER!\n");
  14786. return SCRIPT_CMD_FAILURE;
  14787. }
  14788. switch (type) {
  14789. case 0: mc->base_status.size = (unsigned char)value; break;
  14790. case 1: status_set_hp(bl, (unsigned int)value, 0); break;
  14791. case 2: status_set_maxhp(bl, (unsigned int)value, 0); break;
  14792. case 3: mc->mercenary.char_id = (uint32)value; break;
  14793. case 4: if (mapname) value = map_mapname2mapid(mapname); unit_warp(bl, (short)value, 0, 0, 3); break;
  14794. case 5: if (!unit_walktoxy(bl, (short)value, mc->bl.y, 2)) unit_movepos(bl, (short)value, mc->bl.y, 0, 0); break;
  14795. case 6: if (!unit_walktoxy(bl, mc->bl.x, (short)value, 2)) unit_movepos(bl, mc->bl.x, (short)value, 0, 0); break;
  14796. case 7: mc->mercenary.kill_count = (unsigned int)value; break;
  14797. case 8: mc->mercenary.life_time = (unsigned int)value; break;
  14798. case 9: mc->base_status.speed = (unsigned short)value; status_calc_bl(bl, SCB_ALL); break;
  14799. case 10: unit_setdir(bl, (uint8)value); break;
  14800. case 11: mc->ud.canmove_tick = value > 0 ? 1 : 0; break;
  14801. case 12: mc->base_status.str = (unsigned short)value; status_calc_bl(bl, SCB_ALL); break;
  14802. case 13: mc->base_status.agi = (unsigned short)value; status_calc_bl(bl, SCB_ALL); break;
  14803. case 14: mc->base_status.vit = (unsigned short)value; status_calc_bl(bl, SCB_ALL); break;
  14804. case 15: mc->base_status.int_ = (unsigned short)value; status_calc_bl(bl, SCB_ALL); break;
  14805. case 16: mc->base_status.dex = (unsigned short)value; status_calc_bl(bl, SCB_ALL); break;
  14806. case 17: mc->base_status.luk = (unsigned short)value; status_calc_bl(bl, SCB_ALL); break;
  14807. case 18: mc->ud.immune_attack = (bool)value > 0 ? 1 : 0; break;
  14808. default:
  14809. ShowError("buildin_setunitdata: Unknown data identifier %d for BL_MER.\n", type);
  14810. return SCRIPT_CMD_FAILURE;
  14811. }
  14812. break;
  14813. case BL_ELEM:
  14814. if (!ed) {
  14815. ShowWarning("buildin_setunitdata: Error in finding object BL_ELEM!\n");
  14816. return SCRIPT_CMD_FAILURE;
  14817. }
  14818. switch (type) {
  14819. case 0: ed->base_status.size = (unsigned char)value; break;
  14820. case 1: status_set_hp(bl, (unsigned int)value, 0); break;
  14821. case 2: status_set_maxhp(bl, (unsigned int)value, 0); break;
  14822. case 3: status_set_sp(bl, (unsigned int)value, 0); break;
  14823. case 4: status_set_maxsp(bl, (unsigned int)value, 0); break;
  14824. case 5: ed->elemental.char_id = (uint32)value; break;
  14825. case 6: if (mapname) value = map_mapname2mapid(mapname); unit_warp(bl, (short)value, 0, 0, 3); break;
  14826. case 7: if (!unit_walktoxy(bl, (short)value, ed->bl.y, 2)) unit_movepos(bl, (short)value, ed->bl.y, 0, 0); break;
  14827. case 8: if (!unit_walktoxy(bl, ed->bl.x, (short)value, 2)) unit_movepos(bl, ed->bl.x, (short)value, 0, 0); break;
  14828. case 9: ed->elemental.life_time = (unsigned int)value; break;
  14829. case 10: ed->elemental.mode = (unsigned int)value; status_calc_bl(bl, SCB_MODE); break;
  14830. case 11: ed->base_status.speed = (unsigned short)value; status_calc_bl(bl, SCB_ALL); break;
  14831. case 12: unit_setdir(bl, (uint8)value); break;
  14832. case 13: ed->ud.canmove_tick = value > 0 ? 1 : 0; break;
  14833. case 14: ed->base_status.str = (unsigned short)value; status_calc_bl(bl, SCB_ALL); break;
  14834. case 15: ed->base_status.agi = (unsigned short)value; status_calc_bl(bl, SCB_ALL); break;
  14835. case 16: ed->base_status.vit = (unsigned short)value; status_calc_bl(bl, SCB_ALL); break;
  14836. case 17: ed->base_status.int_ = (unsigned short)value; status_calc_bl(bl, SCB_ALL); break;
  14837. case 18: ed->base_status.dex = (unsigned short)value; status_calc_bl(bl, SCB_ALL); break;
  14838. case 19: ed->base_status.luk = (unsigned short)value; status_calc_bl(bl, SCB_ALL); break;
  14839. case 20: ed->ud.immune_attack = (bool)value > 0 ? 1 : 0; break;
  14840. default:
  14841. ShowError("buildin_setunitdata: Unknown data identifier %d for BL_ELEM.\n", type);
  14842. return SCRIPT_CMD_FAILURE;
  14843. }
  14844. break;
  14845. case BL_NPC:
  14846. if (!nd) {
  14847. ShowWarning("buildin_setunitdata: Error in finding object BL_NPC!\n");
  14848. return SCRIPT_CMD_FAILURE;
  14849. }
  14850. switch (type) {
  14851. case 0: status_set_viewdata(bl, (unsigned short)value); break;
  14852. case 1: nd->level = (unsigned int)value; break;
  14853. case 2: status_set_hp(bl, (unsigned int)value, 0); break;
  14854. case 3: status_set_maxhp(bl, (unsigned int)value, 0); break;
  14855. case 4: if (mapname) value = map_mapname2mapid(mapname); unit_warp(bl, (short)value, 0, 0, 3); break;
  14856. case 5: if (!unit_walktoxy(bl, (short)value, nd->bl.y, 2)) unit_movepos(bl, (short)value, nd->bl.x, 0, 0); break;
  14857. case 6: if (!unit_walktoxy(bl, nd->bl.x, (short)value, 2)) unit_movepos(bl, nd->bl.x, (short)value, 0, 0); break;
  14858. case 7: unit_setdir(bl, (uint8)value); break;
  14859. case 8: nd->params.str = (unsigned short)value; status_calc_bl(bl, SCB_ALL); break;
  14860. case 9: nd->params.agi = (unsigned short)value; status_calc_bl(bl, SCB_ALL); break;
  14861. case 10: nd->params.vit = (unsigned short)value; status_calc_bl(bl, SCB_ALL); break;
  14862. case 11: nd->params.int_ = (unsigned short)value; status_calc_bl(bl, SCB_ALL); break;
  14863. case 12: nd->params.dex = (unsigned short)value; status_calc_bl(bl, SCB_ALL); break;
  14864. case 13: nd->params.luk = (unsigned short)value; status_calc_bl(bl, SCB_ALL); break;
  14865. default:
  14866. ShowError("buildin_setunitdata: Unknown data identifier %d for BL_NPC.\n", type);
  14867. return SCRIPT_CMD_FAILURE;
  14868. }
  14869. break;
  14870. default:
  14871. ShowWarning("buildin_setunitdata: Unknown object type!\n");
  14872. return SCRIPT_CMD_FAILURE;
  14873. }
  14874. return SCRIPT_CMD_SUCCESS;
  14875. }
  14876. /// Gets the name of a bl.
  14877. /// Supported types are [MOB|HOM|PET|NPC].
  14878. /// MER and ELEM don't support custom names.
  14879. ///
  14880. /// getunitname <unit id>;
  14881. BUILDIN_FUNC(getunitname)
  14882. {
  14883. struct block_list* bl = NULL;
  14884. bl = map_id2bl(script_getnum(st, 2));
  14885. if (!bl) {
  14886. ShowWarning("buildin_getunitname: Error in finding object with given game ID %d!\n", script_getnum(st, 2));
  14887. return SCRIPT_CMD_FAILURE;
  14888. }
  14889. script_pushstrcopy(st, status_get_name(bl));
  14890. return SCRIPT_CMD_SUCCESS;
  14891. }
  14892. /// Changes the name of a bl.
  14893. /// Supported types are [MOB|HOM|PET].
  14894. /// For NPC see 'setnpcdisplay', MER and ELEM don't support custom names.
  14895. ///
  14896. /// setunitname <unit id>,<name>;
  14897. BUILDIN_FUNC(setunitname)
  14898. {
  14899. struct block_list* bl = NULL;
  14900. TBL_MOB* md = NULL;
  14901. TBL_HOM* hd = NULL;
  14902. TBL_PET* pd = NULL;
  14903. bl = map_id2bl(script_getnum(st, 2));
  14904. if (!bl) {
  14905. ShowWarning("buildin_setunitname: Error in finding object with given game ID %d!\n", script_getnum(st, 2));
  14906. return SCRIPT_CMD_FAILURE;
  14907. }
  14908. switch (bl->type) {
  14909. case BL_MOB: md = map_id2md(bl->id); break;
  14910. case BL_HOM: hd = map_id2hd(bl->id); break;
  14911. case BL_PET: pd = map_id2pd(bl->id); break;
  14912. }
  14913. switch (bl->type) {
  14914. case BL_MOB:
  14915. if (!md) {
  14916. ShowWarning("buildin_setunitname: Error in finding object BL_MOB!\n");
  14917. return SCRIPT_CMD_FAILURE;
  14918. }
  14919. safestrncpy(md->name, script_getstr(st, 3), NAME_LENGTH);
  14920. break;
  14921. case BL_HOM:
  14922. if (!hd) {
  14923. ShowWarning("buildin_setunitname: Error in finding object BL_HOM!\n");
  14924. return SCRIPT_CMD_FAILURE;
  14925. }
  14926. safestrncpy(hd->homunculus.name, script_getstr(st, 3), NAME_LENGTH);
  14927. break;
  14928. case BL_PET:
  14929. if (!pd) {
  14930. ShowWarning("buildin_setunitname: Error in finding object BL_PET!\n");
  14931. return SCRIPT_CMD_FAILURE;
  14932. }
  14933. safestrncpy(pd->pet.name, script_getstr(st, 3), NAME_LENGTH);
  14934. break;
  14935. default:
  14936. ShowWarning("buildin_setunitname: Unknown object type!\n");
  14937. return SCRIPT_CMD_FAILURE;
  14938. }
  14939. clif_charnameack(0, bl); // Send update to client.
  14940. return SCRIPT_CMD_SUCCESS;
  14941. }
  14942. /// Makes the unit walk to target position or map.
  14943. /// Returns if it was successful.
  14944. ///
  14945. /// unitwalk(<unit_id>,<x>,<y>{,<event_label>}) -> <bool>
  14946. /// unitwalkto(<unit_id>,<target_id>{,<event_label>}) -> <bool>
  14947. BUILDIN_FUNC(unitwalk)
  14948. {
  14949. struct block_list* bl;
  14950. struct unit_data *ud = NULL;
  14951. const char *cmd = script_getfuncname(st), *done_label = "";
  14952. uint8 off = 5;
  14953. bl = map_id2bl(script_getnum(st,2));
  14954. if (!bl) {
  14955. ShowError("buildin_unitwalk: Invalid unit with ID '%d'.\n", script_getnum(st,2));
  14956. script_pushint(st, 0);
  14957. return SCRIPT_CMD_FAILURE;
  14958. }
  14959. ud = unit_bl2ud(bl);
  14960. if (strcmp(cmd,"unitwalk")) {
  14961. int x = script_getnum(st,3);
  14962. int y = script_getnum(st,4);
  14963. if (script_pushint(st, unit_can_reach_pos(bl,x,y,0)))
  14964. add_timer(gettick()+50, unit_delay_walktoxy_timer, bl->id, (x<<16)|(y&0xFFFF)); // Need timer to avoid mismatches
  14965. } else {
  14966. struct block_list* tbl = map_id2bl(script_getnum(st,3));
  14967. if (!tbl) {
  14968. ShowError("buildin_unitwalk: Bad target destination.\n");
  14969. script_pushint(st, 0);
  14970. return SCRIPT_CMD_FAILURE;
  14971. } else if (script_pushint(st, unit_can_reach_bl(bl, tbl, distance_bl(bl, tbl)+1, 0, NULL, NULL)))
  14972. add_timer(gettick()+50, unit_delay_walktobl_timer, bl->id, tbl->id); // Need timer to avoid mismatches
  14973. off = 3;
  14974. }
  14975. if (ud && script_hasdata(st, off)) {
  14976. done_label = script_getstr(st, off);
  14977. check_event(st, done_label);
  14978. safestrncpy(ud->walk_done_event, done_label, sizeof(ud->walk_done_event));
  14979. }
  14980. return SCRIPT_CMD_SUCCESS;
  14981. }
  14982. /// Kills the unit.
  14983. ///
  14984. /// unitkill <unit_id>;
  14985. BUILDIN_FUNC(unitkill)
  14986. {
  14987. struct block_list* bl = map_id2bl(script_getnum(st,2));
  14988. if (bl != NULL)
  14989. status_kill(bl);
  14990. return SCRIPT_CMD_SUCCESS;
  14991. }
  14992. /// Warps the unit to the target position in the target map.
  14993. /// Returns if it was successful.
  14994. ///
  14995. /// unitwarp(<unit_id>,"<map name>",<x>,<y>) -> <bool>
  14996. BUILDIN_FUNC(unitwarp)
  14997. {
  14998. int unit_id;
  14999. int map_idx;
  15000. short x;
  15001. short y;
  15002. struct block_list* bl;
  15003. const char *mapname;
  15004. unit_id = script_getnum(st,2);
  15005. mapname = script_getstr(st, 3);
  15006. x = (short)script_getnum(st,4);
  15007. y = (short)script_getnum(st,5);
  15008. if (!unit_id) //Warp the script's runner
  15009. bl = map_id2bl(st->rid);
  15010. else
  15011. bl = map_id2bl(unit_id);
  15012. if (!strcmp(mapname,"this"))
  15013. map_idx = bl?bl->m:-1;
  15014. else
  15015. map_idx = map_mapname2mapid(mapname);
  15016. if (map_idx >= 0 && bl != NULL)
  15017. script_pushint(st, unit_warp(bl,map_idx,x,y,CLR_OUTSIGHT));
  15018. else
  15019. script_pushint(st, 0);
  15020. return SCRIPT_CMD_SUCCESS;
  15021. }
  15022. /// Makes the unit attack the target.
  15023. /// If the unit is a player and <action type> is not 0, it does a continuous
  15024. /// attack instead of a single attack.
  15025. /// Returns if the request was successful.
  15026. ///
  15027. /// unitattack(<unit_id>,"<target name>"{,<action type>}) -> <bool>
  15028. /// unitattack(<unit_id>,<target_id>{,<action type>}) -> <bool>
  15029. BUILDIN_FUNC(unitattack)
  15030. {
  15031. struct block_list* unit_bl;
  15032. struct block_list* target_bl = NULL;
  15033. struct script_data* data;
  15034. int actiontype = 0;
  15035. unit_bl = map_id2bl(script_getnum(st,2));
  15036. if (!unit_bl) {
  15037. script_pushint(st, 0);
  15038. return SCRIPT_CMD_FAILURE;
  15039. }
  15040. data = script_getdata(st, 3);
  15041. get_val(st, data);
  15042. if (data_isstring(data)) {
  15043. TBL_PC* sd = map_nick2sd(conv_str(st, data));
  15044. if( sd != NULL )
  15045. target_bl = &sd->bl;
  15046. } else
  15047. target_bl = map_id2bl(conv_num(st, data));
  15048. if (!target_bl) {
  15049. script_pushint(st, 0);
  15050. return SCRIPT_CMD_FAILURE;
  15051. }
  15052. if (script_hasdata(st,4))
  15053. actiontype = script_getnum(st,4);
  15054. switch(unit_bl->type) {
  15055. case BL_PC:
  15056. clif_parse_ActionRequest_sub(((TBL_PC *)unit_bl), actiontype > 0 ? 0x07 : 0x00, target_bl->id, gettick());
  15057. script_pushint(st, 1);
  15058. return SCRIPT_CMD_SUCCESS;
  15059. case BL_MOB:
  15060. ((TBL_MOB *)unit_bl)->target_id = target_bl->id;
  15061. break;
  15062. case BL_PET:
  15063. ((TBL_PET *)unit_bl)->target_id = target_bl->id;
  15064. break;
  15065. default:
  15066. ShowError("buildin_unitattack: Unsupported source unit type %d.\n", unit_bl->type);
  15067. script_pushint(st, 0);
  15068. return SCRIPT_CMD_FAILURE;
  15069. }
  15070. script_pushint(st, unit_walktobl(unit_bl, target_bl, 65025, 2));
  15071. return SCRIPT_CMD_SUCCESS;
  15072. }
  15073. /// Makes the unit stop attacking.
  15074. ///
  15075. /// unitstopattack <unit_id>;
  15076. BUILDIN_FUNC(unitstopattack)
  15077. {
  15078. int unit_id;
  15079. struct block_list* bl;
  15080. unit_id = script_getnum(st,2);
  15081. bl = map_id2bl(unit_id);
  15082. if (bl != NULL) {
  15083. unit_stop_attack(bl);
  15084. if (bl->type == BL_MOB)
  15085. ((TBL_MOB*)bl)->target_id = 0;
  15086. }
  15087. return SCRIPT_CMD_SUCCESS;
  15088. }
  15089. /// Makes the unit stop walking.
  15090. ///
  15091. /// unitstopwalk <unit_id>;
  15092. BUILDIN_FUNC(unitstopwalk)
  15093. {
  15094. int unit_id;
  15095. struct block_list* bl;
  15096. unit_id = script_getnum(st,2);
  15097. bl = map_id2bl(unit_id);
  15098. if (bl != NULL) {
  15099. unit_stop_walking(bl,4);
  15100. if (bl->type == BL_MOB)
  15101. ((TBL_MOB*)bl)->target_id = 0;
  15102. }
  15103. return SCRIPT_CMD_SUCCESS;
  15104. }
  15105. /// Makes the unit say the given message.
  15106. ///
  15107. /// unittalk <unit_id>,"<message>";
  15108. BUILDIN_FUNC(unittalk)
  15109. {
  15110. int unit_id;
  15111. const char* message;
  15112. struct block_list* bl;
  15113. unit_id = script_getnum(st,2);
  15114. message = script_getstr(st, 3);
  15115. bl = map_id2bl(unit_id);
  15116. if (bl != NULL) {
  15117. struct StringBuf sbuf;
  15118. StringBuf_Init(&sbuf);
  15119. StringBuf_Printf(&sbuf, "%s : %s", status_get_name(bl), message);
  15120. clif_disp_overhead(bl, StringBuf_Value(&sbuf));
  15121. StringBuf_Destroy(&sbuf);
  15122. }
  15123. return SCRIPT_CMD_SUCCESS;
  15124. }
  15125. /// Makes the unit do an emotion.
  15126. ///
  15127. /// unitemote <unit_id>,<emotion>;
  15128. ///
  15129. /// @see e_* in db/const.txt
  15130. BUILDIN_FUNC(unitemote)
  15131. {
  15132. int unit_id;
  15133. int emotion;
  15134. struct block_list* bl;
  15135. unit_id = script_getnum(st,2);
  15136. emotion = script_getnum(st,3);
  15137. bl = map_id2bl(unit_id);
  15138. if (bl != NULL)
  15139. clif_emotion(bl, emotion);
  15140. return SCRIPT_CMD_SUCCESS;
  15141. }
  15142. /// Makes the unit cast the skill on the target or self if no target is specified.
  15143. ///
  15144. /// unitskilluseid <unit_id>,<skill_id>,<skill_lv>{,<target_id>,<casttime>};
  15145. /// unitskilluseid <unit_id>,"<skill name>",<skill_lv>{,<target_id>,<casttime>};
  15146. BUILDIN_FUNC(unitskilluseid)
  15147. {
  15148. int unit_id, target_id, casttime;
  15149. uint16 skill_id, skill_lv;
  15150. struct block_list* bl;
  15151. struct script_data *data;
  15152. unit_id = script_getnum(st,2);
  15153. data = script_getdata(st, 3);
  15154. get_val(st, data); // Convert into value in case of a variable
  15155. skill_id = ( data_isstring(data) ? skill_name2id(script_getstr(st,3)) : script_getnum(st,3) );
  15156. skill_lv = script_getnum(st,4);
  15157. target_id = ( script_hasdata(st,5) ? script_getnum(st,5) : unit_id );
  15158. casttime = ( script_hasdata(st,6) ? script_getnum(st,6) : 0 );
  15159. bl = map_id2bl(unit_id);
  15160. if (bl != NULL) {
  15161. if (bl->type == BL_NPC) {
  15162. if (!((TBL_NPC*)bl)->status.hp)
  15163. status_calc_npc(((TBL_NPC*)bl), SCO_FIRST);
  15164. else
  15165. status_calc_npc(((TBL_NPC*)bl), SCO_NONE);
  15166. }
  15167. unit_skilluse_id2(bl, target_id, skill_id, skill_lv, (casttime * 1000) + skill_castfix(bl, skill_id, skill_lv), skill_get_castcancel(skill_id));
  15168. }
  15169. return SCRIPT_CMD_SUCCESS;
  15170. }
  15171. /// Makes the unit cast the skill on the target position.
  15172. ///
  15173. /// unitskillusepos <unit_id>,<skill_id>,<skill_lv>,<target_x>,<target_y>{,<casttime>};
  15174. /// unitskillusepos <unit_id>,"<skill name>",<skill_lv>,<target_x>,<target_y>{,<casttime>};
  15175. BUILDIN_FUNC(unitskillusepos)
  15176. {
  15177. int unit_id, skill_x, skill_y, casttime;
  15178. uint16 skill_id, skill_lv;
  15179. struct block_list* bl;
  15180. struct script_data *data;
  15181. unit_id = script_getnum(st,2);
  15182. data = script_getdata(st, 3);
  15183. get_val(st, data); // Convert into value in case of a variable
  15184. skill_id = ( data_isstring(data) ? skill_name2id(script_getstr(st,3)) : script_getnum(st,3) );
  15185. skill_lv = script_getnum(st,4);
  15186. skill_x = script_getnum(st,5);
  15187. skill_y = script_getnum(st,6);
  15188. casttime = ( script_hasdata(st,7) ? script_getnum(st,7) : 0 );
  15189. bl = map_id2bl(unit_id);
  15190. if (bl != NULL) {
  15191. if (bl->type == BL_NPC) {
  15192. if (!((TBL_NPC*)bl)->status.hp)
  15193. status_calc_npc(((TBL_NPC*)bl), true);
  15194. else
  15195. status_calc_npc(((TBL_NPC*)bl), false);
  15196. }
  15197. 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));
  15198. }
  15199. return SCRIPT_CMD_SUCCESS;
  15200. }
  15201. // <--- [zBuffer] List of unit control commands
  15202. /// Pauses the execution of the script, detaching the player
  15203. ///
  15204. /// sleep <mili seconds>;
  15205. BUILDIN_FUNC(sleep)
  15206. {
  15207. int ticks;
  15208. ticks = script_getnum(st,2);
  15209. // detach the player
  15210. script_detach_rid(st);
  15211. if( ticks <= 0 )
  15212. {// do nothing
  15213. }
  15214. else if( st->sleep.tick == 0 )
  15215. {// sleep for the target amount of time
  15216. st->state = RERUNLINE;
  15217. st->sleep.tick = ticks;
  15218. }
  15219. else
  15220. {// sleep time is over
  15221. st->state = RUN;
  15222. st->sleep.tick = 0;
  15223. }
  15224. return SCRIPT_CMD_SUCCESS;
  15225. }
  15226. /// Pauses the execution of the script, keeping the player attached
  15227. /// Returns if a player is still attached
  15228. ///
  15229. /// sleep2(<mili secconds>) -> <bool>
  15230. BUILDIN_FUNC(sleep2)
  15231. {
  15232. int ticks;
  15233. ticks = script_getnum(st,2);
  15234. if( ticks <= 0 )
  15235. {// do nothing
  15236. script_pushint(st, (map_id2sd(st->rid)!=NULL));
  15237. }
  15238. else if( !st->sleep.tick )
  15239. {// sleep for the target amount of time
  15240. st->state = RERUNLINE;
  15241. st->sleep.tick = ticks;
  15242. }
  15243. else
  15244. {// sleep time is over
  15245. st->state = RUN;
  15246. st->sleep.tick = 0;
  15247. script_pushint(st, (map_id2sd(st->rid)!=NULL));
  15248. }
  15249. return SCRIPT_CMD_SUCCESS;
  15250. }
  15251. /// Awakes all the sleep timers of the target npc
  15252. ///
  15253. /// awake "<npc name>";
  15254. BUILDIN_FUNC(awake)
  15255. {
  15256. struct npc_data* nd;
  15257. struct linkdb_node *node = (struct linkdb_node *)sleep_db;
  15258. nd = npc_name2id(script_getstr(st, 2));
  15259. if( nd == NULL ) {
  15260. ShowError("awake: NPC \"%s\" not found\n", script_getstr(st, 2));
  15261. return 1;
  15262. }
  15263. while( node )
  15264. {
  15265. if( (int)__64BPRTSIZE(node->key) == nd->bl.id )
  15266. {// sleep timer for the npc
  15267. struct script_state* tst = (struct script_state*)node->data;
  15268. TBL_PC* sd = map_id2sd(tst->rid);
  15269. if( tst->sleep.timer == INVALID_TIMER )
  15270. {// already awake ???
  15271. node = node->next;
  15272. continue;
  15273. }
  15274. if( (sd && sd->status.char_id != tst->sleep.charid) || (tst->rid && !sd))
  15275. {// char not online anymore / another char of the same account is online - Cancel execution
  15276. tst->state = END;
  15277. tst->rid = 0;
  15278. }
  15279. delete_timer(tst->sleep.timer, run_script_timer);
  15280. node = script_erase_sleepdb(node);
  15281. tst->sleep.timer = INVALID_TIMER;
  15282. if(tst->state != RERUNLINE)
  15283. tst->sleep.tick = 0;
  15284. run_script_main(tst);
  15285. }
  15286. else
  15287. {
  15288. node = node->next;
  15289. }
  15290. }
  15291. return SCRIPT_CMD_SUCCESS;
  15292. }
  15293. /// Returns a reference to a variable of the target NPC.
  15294. /// Returns 0 if an error occurs.
  15295. ///
  15296. /// getvariableofnpc(<variable>, "<npc name>") -> <reference>
  15297. BUILDIN_FUNC(getvariableofnpc)
  15298. {
  15299. struct script_data* data;
  15300. const char* name;
  15301. struct npc_data* nd;
  15302. data = script_getdata(st,2);
  15303. if( !data_isreference(data) )
  15304. {// Not a reference (aka varaible name)
  15305. ShowError("script:getvariableofnpc: not a variable\n");
  15306. script_reportdata(data);
  15307. script_pushnil(st);
  15308. st->state = END;
  15309. return 1;
  15310. }
  15311. name = reference_getname(data);
  15312. if( *name != '.' || name[1] == '@' )
  15313. {// not a npc variable
  15314. ShowError("script:getvariableofnpc: invalid scope (not npc variable)\n");
  15315. script_reportdata(data);
  15316. script_pushnil(st);
  15317. st->state = END;
  15318. return 1;
  15319. }
  15320. nd = npc_name2id(script_getstr(st,3));
  15321. if( nd == NULL || nd->subtype != NPCTYPE_SCRIPT || nd->u.scr.script == NULL )
  15322. {// NPC not found or has no script
  15323. ShowError("script:getvariableofnpc: can't find npc %s\n", script_getstr(st,3));
  15324. script_pushnil(st);
  15325. st->state = END;
  15326. return 1;
  15327. }
  15328. push_val2(st->stack, C_NAME, reference_getuid(data), &nd->u.scr.script->script_vars );
  15329. return SCRIPT_CMD_SUCCESS;
  15330. }
  15331. /// Opens a warp portal.
  15332. /// Has no "portal opening" effect/sound, it opens the portal immediately.
  15333. ///
  15334. /// warpportal <source x>,<source y>,"<target map>",<target x>,<target y>;
  15335. ///
  15336. /// @author blackhole89
  15337. BUILDIN_FUNC(warpportal)
  15338. {
  15339. int spx;
  15340. int spy;
  15341. unsigned short mapindex;
  15342. int tpx;
  15343. int tpy;
  15344. struct skill_unit_group* group;
  15345. struct block_list* bl;
  15346. bl = map_id2bl(st->oid);
  15347. if( bl == NULL ) {
  15348. ShowError("buildin_warpportal: NPC is needed\n");
  15349. return SCRIPT_CMD_FAILURE;
  15350. }
  15351. spx = script_getnum(st,2);
  15352. spy = script_getnum(st,3);
  15353. mapindex = mapindex_name2id(script_getstr(st, 4));
  15354. tpx = script_getnum(st,5);
  15355. tpy = script_getnum(st,6);
  15356. if( mapindex == 0 ) {
  15357. ShowError("buildin_warpportal: Target map not found %s.\n", script_getstr(st, 4));
  15358. return SCRIPT_CMD_FAILURE;
  15359. }
  15360. group = skill_unitsetting(bl, AL_WARP, 4, spx, spy, 0);
  15361. if( group == NULL )
  15362. return SCRIPT_CMD_FAILURE;// failed
  15363. group->val1 = (group->val1<<16)|(short)0;
  15364. group->val2 = (tpx<<16) | tpy;
  15365. group->val3 = mapindex;
  15366. return SCRIPT_CMD_SUCCESS;
  15367. }
  15368. /**
  15369. * openmail({<char_id>});
  15370. **/
  15371. BUILDIN_FUNC(openmail)
  15372. {
  15373. TBL_PC* sd;
  15374. if (!script_charid2sd(2,sd))
  15375. return SCRIPT_CMD_FAILURE;
  15376. mail_openmail(sd);
  15377. return SCRIPT_CMD_SUCCESS;
  15378. }
  15379. /**
  15380. * openauction({<char_id>});
  15381. **/
  15382. BUILDIN_FUNC(openauction)
  15383. {
  15384. TBL_PC* sd;
  15385. if (!script_charid2sd(2,sd))
  15386. return SCRIPT_CMD_FAILURE;
  15387. if( !battle_config.feature_auction ) {
  15388. clif_colormes(sd, color_table[COLOR_RED], msg_txt(sd, 517));
  15389. return 0;
  15390. }
  15391. clif_Auction_openwindow(sd);
  15392. return SCRIPT_CMD_SUCCESS;
  15393. }
  15394. /// Retrieves the value of the specified flag of the specified cell.
  15395. ///
  15396. /// checkcell("<map name>",<x>,<y>,<type>) -> <bool>
  15397. ///
  15398. /// @see cell_chk* constants in const.txt for the types
  15399. BUILDIN_FUNC(checkcell)
  15400. {
  15401. int16 m = map_mapname2mapid(script_getstr(st,2));
  15402. int16 x = script_getnum(st,3);
  15403. int16 y = script_getnum(st,4);
  15404. cell_chk type = (cell_chk)script_getnum(st,5);
  15405. script_pushint(st, map_getcell(m, x, y, type));
  15406. return SCRIPT_CMD_SUCCESS;
  15407. }
  15408. /// Modifies flags of cells in the specified area.
  15409. ///
  15410. /// setcell "<map name>",<x1>,<y1>,<x2>,<y2>,<type>,<flag>;
  15411. ///
  15412. /// @see cell_* constants in const.txt for the types
  15413. BUILDIN_FUNC(setcell)
  15414. {
  15415. int16 m = map_mapname2mapid(script_getstr(st,2));
  15416. int16 x1 = script_getnum(st,3);
  15417. int16 y1 = script_getnum(st,4);
  15418. int16 x2 = script_getnum(st,5);
  15419. int16 y2 = script_getnum(st,6);
  15420. cell_t type = (cell_t)script_getnum(st,7);
  15421. bool flag = (bool)script_getnum(st,8);
  15422. int x,y;
  15423. if( x1 > x2 ) swap(x1,x2);
  15424. if( y1 > y2 ) swap(y1,y2);
  15425. for( y = y1; y <= y2; ++y )
  15426. for( x = x1; x <= x2; ++x )
  15427. map_setcell(m, x, y, type, flag);
  15428. return SCRIPT_CMD_SUCCESS;
  15429. }
  15430. /*==========================================
  15431. * Mercenary Commands
  15432. *------------------------------------------*/
  15433. BUILDIN_FUNC(mercenary_create)
  15434. {
  15435. struct map_session_data *sd;
  15436. int class_, contract_time;
  15437. if( (sd = script_rid2sd(st)) == NULL || sd->md || sd->status.mer_id != 0 )
  15438. return 0;
  15439. class_ = script_getnum(st,2);
  15440. if( !mercenary_class(class_) )
  15441. return 0;
  15442. contract_time = script_getnum(st,3);
  15443. mercenary_create(sd, class_, contract_time);
  15444. return SCRIPT_CMD_SUCCESS;
  15445. }
  15446. BUILDIN_FUNC(mercenary_heal)
  15447. {
  15448. struct map_session_data *sd = script_rid2sd(st);
  15449. int hp, sp;
  15450. if( sd == NULL || sd->md == NULL )
  15451. return 0;
  15452. hp = script_getnum(st,2);
  15453. sp = script_getnum(st,3);
  15454. status_heal(&sd->md->bl, hp, sp, 0);
  15455. return SCRIPT_CMD_SUCCESS;
  15456. }
  15457. BUILDIN_FUNC(mercenary_sc_start)
  15458. {
  15459. struct map_session_data *sd = script_rid2sd(st);
  15460. enum sc_type type;
  15461. int tick, val1;
  15462. if( sd == NULL || sd->md == NULL )
  15463. return 0;
  15464. type = (sc_type)script_getnum(st,2);
  15465. tick = script_getnum(st,3);
  15466. val1 = script_getnum(st,4);
  15467. status_change_start(NULL, &sd->md->bl, type, 10000, val1, 0, 0, 0, tick, SCSTART_NOTICKDEF);
  15468. return SCRIPT_CMD_SUCCESS;
  15469. }
  15470. BUILDIN_FUNC(mercenary_get_calls)
  15471. {
  15472. struct map_session_data *sd = script_rid2sd(st);
  15473. int guild;
  15474. if( sd == NULL )
  15475. return 0;
  15476. guild = script_getnum(st,2);
  15477. switch( guild )
  15478. {
  15479. case ARCH_MERC_GUILD:
  15480. script_pushint(st,sd->status.arch_calls);
  15481. break;
  15482. case SPEAR_MERC_GUILD:
  15483. script_pushint(st,sd->status.spear_calls);
  15484. break;
  15485. case SWORD_MERC_GUILD:
  15486. script_pushint(st,sd->status.sword_calls);
  15487. break;
  15488. default:
  15489. script_pushint(st,0);
  15490. break;
  15491. }
  15492. return SCRIPT_CMD_SUCCESS;
  15493. }
  15494. BUILDIN_FUNC(mercenary_set_calls)
  15495. {
  15496. struct map_session_data *sd = script_rid2sd(st);
  15497. int guild, value, *calls;
  15498. if( sd == NULL )
  15499. return 0;
  15500. guild = script_getnum(st,2);
  15501. value = script_getnum(st,3);
  15502. switch( guild )
  15503. {
  15504. case ARCH_MERC_GUILD:
  15505. calls = &sd->status.arch_calls;
  15506. break;
  15507. case SPEAR_MERC_GUILD:
  15508. calls = &sd->status.spear_calls;
  15509. break;
  15510. case SWORD_MERC_GUILD:
  15511. calls = &sd->status.sword_calls;
  15512. break;
  15513. default:
  15514. return 0; // Invalid Guild
  15515. }
  15516. *calls += value;
  15517. *calls = cap_value(*calls, 0, INT_MAX);
  15518. return SCRIPT_CMD_SUCCESS;
  15519. }
  15520. BUILDIN_FUNC(mercenary_get_faith)
  15521. {
  15522. struct map_session_data *sd = script_rid2sd(st);
  15523. int guild;
  15524. if( sd == NULL )
  15525. return 0;
  15526. guild = script_getnum(st,2);
  15527. switch( guild )
  15528. {
  15529. case ARCH_MERC_GUILD:
  15530. script_pushint(st,sd->status.arch_faith);
  15531. break;
  15532. case SPEAR_MERC_GUILD:
  15533. script_pushint(st,sd->status.spear_faith);
  15534. break;
  15535. case SWORD_MERC_GUILD:
  15536. script_pushint(st,sd->status.sword_faith);
  15537. break;
  15538. default:
  15539. script_pushint(st,0);
  15540. break;
  15541. }
  15542. return SCRIPT_CMD_SUCCESS;
  15543. }
  15544. BUILDIN_FUNC(mercenary_set_faith)
  15545. {
  15546. struct map_session_data *sd = script_rid2sd(st);
  15547. int guild, value, *calls;
  15548. if( sd == NULL )
  15549. return 0;
  15550. guild = script_getnum(st,2);
  15551. value = script_getnum(st,3);
  15552. switch( guild )
  15553. {
  15554. case ARCH_MERC_GUILD:
  15555. calls = &sd->status.arch_faith;
  15556. break;
  15557. case SPEAR_MERC_GUILD:
  15558. calls = &sd->status.spear_faith;
  15559. break;
  15560. case SWORD_MERC_GUILD:
  15561. calls = &sd->status.sword_faith;
  15562. break;
  15563. default:
  15564. return 0; // Invalid Guild
  15565. }
  15566. *calls += value;
  15567. *calls = cap_value(*calls, 0, INT_MAX);
  15568. if( mercenary_get_guild(sd->md) == guild )
  15569. clif_mercenary_updatestatus(sd,SP_MERCFAITH);
  15570. return SCRIPT_CMD_SUCCESS;
  15571. }
  15572. /*------------------------------------------
  15573. * Book Reading
  15574. *------------------------------------------*/
  15575. BUILDIN_FUNC(readbook)
  15576. {
  15577. struct map_session_data *sd;
  15578. int book_id, page;
  15579. if( (sd = script_rid2sd(st)) == NULL )
  15580. return 0;
  15581. book_id = script_getnum(st,2);
  15582. page = script_getnum(st,3);
  15583. clif_readbook(sd->fd, book_id, page);
  15584. return SCRIPT_CMD_SUCCESS;
  15585. }
  15586. /******************
  15587. Questlog script commands
  15588. *******************/
  15589. /**
  15590. * questinfo <Quest ID>,<Icon>{,<Map Mark Color>{,<Job Class>}};
  15591. **/
  15592. BUILDIN_FUNC(questinfo)
  15593. {
  15594. TBL_NPC* nd = map_id2nd(st->oid);
  15595. int quest_id, icon;
  15596. struct questinfo qi;
  15597. if( nd == NULL || nd->bl.m == -1 )
  15598. return true;
  15599. quest_id = script_getnum(st, 2);
  15600. icon = script_getnum(st, 3);
  15601. #if PACKETVER >= 20120410
  15602. if(icon < 0 || (icon > 8 && icon != 9999) || icon == 7)
  15603. icon = 9999; // Default to nothing if icon id is invalid.
  15604. #else
  15605. if(icon < 0 || icon > 7)
  15606. icon = 0;
  15607. else
  15608. icon = icon + 1;
  15609. #endif
  15610. qi.quest_id = quest_id;
  15611. qi.icon = (unsigned char)icon;
  15612. qi.nd = nd;
  15613. if( script_hasdata(st, 4) ) {
  15614. int color = script_getnum(st, 4);
  15615. if( color < 0 || color > 3 ) {
  15616. ShowWarning("buildin_questinfo: invalid color '%d', changing to 0\n",color);
  15617. script_reportfunc(st);
  15618. color = 0;
  15619. }
  15620. qi.color = (unsigned char)color;
  15621. }
  15622. qi.hasJob = false;
  15623. if(script_hasdata(st, 5)) {
  15624. int job = script_getnum(st, 5);
  15625. if (!pcdb_checkid(job))
  15626. ShowError("buildin_questinfo: Nonexistant Job Class.\n");
  15627. else {
  15628. qi.hasJob = true;
  15629. qi.job = (unsigned short)job;
  15630. }
  15631. }
  15632. map_add_questinfo(nd->bl.m,&qi);
  15633. return true;
  15634. }
  15635. /**
  15636. * setquest <ID>{,<char_id>};
  15637. **/
  15638. BUILDIN_FUNC(setquest)
  15639. {
  15640. struct map_session_data *sd;
  15641. unsigned short i;
  15642. int quest_id;
  15643. quest_id = script_getnum(st, 2);
  15644. if (!script_charid2sd(3,sd))
  15645. return SCRIPT_CMD_FAILURE;
  15646. quest_add(sd, quest_id);
  15647. // If questinfo is set, remove quest bubble once quest is set.
  15648. for(i = 0; i < map[sd->bl.m].qi_count; i++) {
  15649. struct questinfo *qi = &map[sd->bl.m].qi_data[i];
  15650. if( qi->quest_id == quest_id ) {
  15651. #if PACKETVER >= 20120410
  15652. clif_quest_show_event(sd, &qi->nd->bl, 9999, 0);
  15653. #else
  15654. clif_quest_show_event(sd, &qi->nd->bl, 0, 0);
  15655. #endif
  15656. }
  15657. }
  15658. return SCRIPT_CMD_SUCCESS;
  15659. }
  15660. /**
  15661. * erasequest <ID>{,<char_id>};
  15662. **/
  15663. BUILDIN_FUNC(erasequest)
  15664. {
  15665. struct map_session_data *sd;
  15666. if (!script_charid2sd(3,sd))
  15667. return SCRIPT_CMD_FAILURE;
  15668. quest_delete(sd, script_getnum(st, 2));
  15669. return SCRIPT_CMD_SUCCESS;
  15670. }
  15671. /**
  15672. * completequest <ID>{,<char_id>};
  15673. **/
  15674. BUILDIN_FUNC(completequest)
  15675. {
  15676. struct map_session_data *sd;
  15677. if (!script_charid2sd(3,sd))
  15678. return SCRIPT_CMD_FAILURE;
  15679. quest_update_status(sd, script_getnum(st, 2), Q_COMPLETE);
  15680. return SCRIPT_CMD_SUCCESS;
  15681. }
  15682. /**
  15683. * changequest <ID>,<ID2>{,<char_id>};
  15684. **/
  15685. BUILDIN_FUNC(changequest)
  15686. {
  15687. struct map_session_data *sd;
  15688. if (!script_charid2sd(4,sd))
  15689. return SCRIPT_CMD_FAILURE;
  15690. quest_change(sd, script_getnum(st, 2),script_getnum(st, 3));
  15691. return SCRIPT_CMD_SUCCESS;
  15692. }
  15693. /**
  15694. * checkquest(<ID>{,PLAYTIME|HUNTING{,<char_id>}})
  15695. **/
  15696. BUILDIN_FUNC(checkquest)
  15697. {
  15698. struct map_session_data *sd;
  15699. enum quest_check_type type = HAVEQUEST;
  15700. if( script_hasdata(st, 3) )
  15701. type = (enum quest_check_type)script_getnum(st, 3);
  15702. if (!script_charid2sd(4,sd))
  15703. return SCRIPT_CMD_FAILURE;
  15704. script_pushint(st, quest_check(sd, script_getnum(st, 2), type));
  15705. return SCRIPT_CMD_SUCCESS;
  15706. }
  15707. /**
  15708. * isbegin_quest(<ID>{,<char_id>})
  15709. **/
  15710. BUILDIN_FUNC(isbegin_quest)
  15711. {
  15712. struct map_session_data *sd;
  15713. int i;
  15714. if (!script_charid2sd(3,sd))
  15715. return SCRIPT_CMD_FAILURE;
  15716. i = quest_check(sd, script_getnum(st, 2), (enum quest_check_type) HAVEQUEST);
  15717. script_pushint(st, i + (i < 1));
  15718. return SCRIPT_CMD_SUCCESS;
  15719. }
  15720. /**
  15721. * showevent <icon>{,<mark color>{,<char_id>}}
  15722. **/
  15723. BUILDIN_FUNC(showevent)
  15724. {
  15725. TBL_PC *sd;
  15726. struct npc_data *nd = map_id2nd(st->oid);
  15727. int icon, color = 0;
  15728. if (!script_charid2sd(4,sd))
  15729. return SCRIPT_CMD_FAILURE;
  15730. if( sd == NULL || nd == NULL )
  15731. return 0;
  15732. icon = script_getnum(st, 2);
  15733. if( script_hasdata(st, 3) ) {
  15734. color = script_getnum(st, 3);
  15735. if( color < 0 || color > 3 ) {
  15736. ShowWarning("buildin_showevent: invalid color '%d', changing to 0\n",color);
  15737. script_reportfunc(st);
  15738. color = 0;
  15739. }
  15740. }
  15741. #if PACKETVER >= 20120410
  15742. if(icon < 0 || (icon > 8 && icon != 9999) || icon == 7)
  15743. icon = 9999; // Default to nothing if icon id is invalid.
  15744. #else
  15745. if(icon < 0 || icon > 7)
  15746. icon = 0;
  15747. else
  15748. icon = icon + 1;
  15749. #endif
  15750. clif_quest_show_event(sd, &nd->bl, icon, color);
  15751. return SCRIPT_CMD_SUCCESS;
  15752. }
  15753. /*==========================================
  15754. * BattleGround System
  15755. *------------------------------------------*/
  15756. BUILDIN_FUNC(waitingroom2bg)
  15757. {
  15758. struct npc_data *nd;
  15759. struct chat_data *cd;
  15760. const char *map_name, *ev = "", *dev = "";
  15761. int x, y, mapindex = 0, bg_id, n;
  15762. unsigned char i;
  15763. if( script_hasdata(st,7) )
  15764. nd = npc_name2id(script_getstr(st,7));
  15765. else
  15766. nd = (struct npc_data *)map_id2bl(st->oid);
  15767. if( nd == NULL || (cd = (struct chat_data *)map_id2bl(nd->chat_id)) == NULL )
  15768. {
  15769. script_pushint(st,0);
  15770. return 0;
  15771. }
  15772. map_name = script_getstr(st,2);
  15773. if( strcmp(map_name,"-") != 0 )
  15774. {
  15775. mapindex = mapindex_name2id(map_name);
  15776. if( mapindex == 0 )
  15777. { // Invalid Map
  15778. script_pushint(st,0);
  15779. return 0;
  15780. }
  15781. }
  15782. x = script_getnum(st,3);
  15783. y = script_getnum(st,4);
  15784. ev = script_getstr(st,5); // Logout Event
  15785. dev = script_getstr(st,6); // Die Event
  15786. if( (bg_id = bg_create(mapindex, x, y, ev, dev)) == 0 )
  15787. { // Creation failed
  15788. script_pushint(st,0);
  15789. return 0;
  15790. }
  15791. n = cd->users;
  15792. for( i = 0; i < n && i < MAX_BG_MEMBERS; i++ )
  15793. {
  15794. struct map_session_data *sd;
  15795. if( (sd = cd->usersd[i]) != NULL && bg_team_join(bg_id, sd) )
  15796. mapreg_setreg(reference_uid(add_str("$@arenamembers"), i), sd->bl.id);
  15797. else
  15798. mapreg_setreg(reference_uid(add_str("$@arenamembers"), i), 0);
  15799. }
  15800. mapreg_setreg(add_str("$@arenamembersnum"), i);
  15801. script_pushint(st,bg_id);
  15802. return SCRIPT_CMD_SUCCESS;
  15803. }
  15804. BUILDIN_FUNC(waitingroom2bg_single)
  15805. {
  15806. const char* map_name;
  15807. struct npc_data *nd;
  15808. struct chat_data *cd;
  15809. struct map_session_data *sd;
  15810. int x, y, mapindex, bg_id;
  15811. bg_id = script_getnum(st,2);
  15812. map_name = script_getstr(st,3);
  15813. if( (mapindex = mapindex_name2id(map_name)) == 0 )
  15814. return 0; // Invalid Map
  15815. x = script_getnum(st,4);
  15816. y = script_getnum(st,5);
  15817. nd = npc_name2id(script_getstr(st,6));
  15818. if( nd == NULL || (cd = (struct chat_data *)map_id2bl(nd->chat_id)) == NULL || cd->users <= 0 )
  15819. return 0;
  15820. if( (sd = cd->usersd[0]) == NULL )
  15821. return 0;
  15822. if( bg_team_join(bg_id, sd) )
  15823. {
  15824. pc_setpos(sd, mapindex, x, y, CLR_TELEPORT);
  15825. script_pushint(st,1);
  15826. }
  15827. else
  15828. script_pushint(st,0);
  15829. return SCRIPT_CMD_SUCCESS;
  15830. }
  15831. BUILDIN_FUNC(bg_team_setxy)
  15832. {
  15833. struct battleground_data *bg;
  15834. int bg_id;
  15835. bg_id = script_getnum(st,2);
  15836. if( (bg = bg_team_search(bg_id)) == NULL )
  15837. return 0;
  15838. bg->x = script_getnum(st,3);
  15839. bg->y = script_getnum(st,4);
  15840. return SCRIPT_CMD_SUCCESS;
  15841. }
  15842. BUILDIN_FUNC(bg_warp)
  15843. {
  15844. int x, y, mapindex, bg_id;
  15845. const char* map_name;
  15846. bg_id = script_getnum(st,2);
  15847. map_name = script_getstr(st,3);
  15848. if( (mapindex = mapindex_name2id(map_name)) == 0 )
  15849. return 0; // Invalid Map
  15850. x = script_getnum(st,4);
  15851. y = script_getnum(st,5);
  15852. bg_team_warp(bg_id, mapindex, x, y);
  15853. return SCRIPT_CMD_SUCCESS;
  15854. }
  15855. BUILDIN_FUNC(bg_monster)
  15856. {
  15857. int class_ = 0, x = 0, y = 0, bg_id = 0;
  15858. const char *str,*mapname, *evt="";
  15859. bg_id = script_getnum(st,2);
  15860. mapname = script_getstr(st,3);
  15861. x = script_getnum(st,4);
  15862. y = script_getnum(st,5);
  15863. str = script_getstr(st,6);
  15864. class_ = script_getnum(st,7);
  15865. if( script_hasdata(st,8) ) evt = script_getstr(st,8);
  15866. check_event(st, evt);
  15867. script_pushint(st, mob_spawn_bg(mapname,x,y,str,class_,evt,bg_id));
  15868. return SCRIPT_CMD_SUCCESS;
  15869. }
  15870. BUILDIN_FUNC(bg_monster_set_team)
  15871. {
  15872. struct mob_data *md;
  15873. struct block_list *mbl;
  15874. int id = script_getnum(st,2),
  15875. bg_id = script_getnum(st,3);
  15876. if( id == 0 || (mbl = map_id2bl(id)) == NULL || mbl->type != BL_MOB )
  15877. return 0;
  15878. md = (TBL_MOB *)mbl;
  15879. md->bg_id = bg_id;
  15880. mob_stop_attack(md);
  15881. mob_stop_walking(md, 0);
  15882. md->target_id = md->attacked_id = 0;
  15883. clif_charnameack(0, &md->bl);
  15884. return SCRIPT_CMD_SUCCESS;
  15885. }
  15886. BUILDIN_FUNC(bg_leave)
  15887. {
  15888. struct map_session_data *sd = script_rid2sd(st);
  15889. if( sd == NULL || !sd->bg_id )
  15890. return 0;
  15891. bg_team_leave(sd,0);
  15892. return SCRIPT_CMD_SUCCESS;
  15893. }
  15894. BUILDIN_FUNC(bg_destroy)
  15895. {
  15896. int bg_id = script_getnum(st,2);
  15897. bg_team_delete(bg_id);
  15898. return SCRIPT_CMD_SUCCESS;
  15899. }
  15900. BUILDIN_FUNC(bg_getareausers)
  15901. {
  15902. const char *str;
  15903. int16 m, x0, y0, x1, y1;
  15904. int bg_id;
  15905. int i = 0, c = 0;
  15906. struct battleground_data *bg = NULL;
  15907. bg_id = script_getnum(st,2);
  15908. str = script_getstr(st,3);
  15909. if( (bg = bg_team_search(bg_id)) == NULL || (m = map_mapname2mapid(str)) < 0 )
  15910. {
  15911. script_pushint(st,0);
  15912. return 0;
  15913. }
  15914. x0 = script_getnum(st,4);
  15915. y0 = script_getnum(st,5);
  15916. x1 = script_getnum(st,6);
  15917. y1 = script_getnum(st,7);
  15918. for( i = 0; i < MAX_BG_MEMBERS; i++ )
  15919. {
  15920. struct map_session_data *sd;
  15921. if( (sd = bg->members[i].sd) == NULL )
  15922. continue;
  15923. if( sd->bl.m != m || sd->bl.x < x0 || sd->bl.y < y0 || sd->bl.x > x1 || sd->bl.y > y1 )
  15924. continue;
  15925. c++;
  15926. }
  15927. script_pushint(st,c);
  15928. return SCRIPT_CMD_SUCCESS;
  15929. }
  15930. BUILDIN_FUNC(bg_updatescore)
  15931. {
  15932. const char *str;
  15933. int16 m;
  15934. str = script_getstr(st,2);
  15935. if( (m = map_mapname2mapid(str)) < 0 )
  15936. return 0;
  15937. map[m].bgscore_lion = script_getnum(st,3);
  15938. map[m].bgscore_eagle = script_getnum(st,4);
  15939. clif_bg_updatescore(m);
  15940. return SCRIPT_CMD_SUCCESS;
  15941. }
  15942. BUILDIN_FUNC(bg_get_data)
  15943. {
  15944. struct battleground_data *bg;
  15945. int bg_id = script_getnum(st,2),
  15946. type = script_getnum(st,3);
  15947. if( (bg = bg_team_search(bg_id)) == NULL )
  15948. {
  15949. script_pushint(st,0);
  15950. return 0;
  15951. }
  15952. switch( type )
  15953. {
  15954. case 0: script_pushint(st, bg->count); break;
  15955. default:
  15956. ShowError("script:bg_get_data: unknown data identifier %d\n", type);
  15957. break;
  15958. }
  15959. return SCRIPT_CMD_SUCCESS;
  15960. }
  15961. /*==========================================
  15962. * Instancing System
  15963. *------------------------------------------*/
  15964. //Returns an Instance ID
  15965. //Checks NPC first, then if player is attached we check party
  15966. int script_instancegetid(struct script_state* st)
  15967. {
  15968. short instance_id = 0;
  15969. struct npc_data *nd;
  15970. if( (nd = map_id2nd(st->oid)) && nd->instance_id > 0 )
  15971. instance_id = nd->instance_id;
  15972. else {
  15973. struct map_session_data *sd;
  15974. struct party_data *p;
  15975. if( (sd = script_rid2sd(st)) != NULL && sd->status.party_id && (p = party_search(sd->status.party_id)) != NULL && p->instance_id )
  15976. instance_id = p->instance_id;
  15977. }
  15978. return instance_id;
  15979. }
  15980. /*==========================================
  15981. * Creates the instance
  15982. * Returns Instance ID if created successfully
  15983. *------------------------------------------*/
  15984. BUILDIN_FUNC(instance_create)
  15985. {
  15986. struct map_session_data *sd;
  15987. if((sd = script_rid2sd(st)) == NULL)
  15988. return -1;
  15989. script_pushint(st,instance_create(sd->status.party_id, script_getstr(st, 2)));
  15990. return SCRIPT_CMD_SUCCESS;
  15991. }
  15992. /*==========================================
  15993. * Destroys an instance (unofficial)
  15994. * Officially instances are only destroyed by timeout
  15995. *
  15996. * instance_destroy {<instance_id>};
  15997. *------------------------------------------*/
  15998. BUILDIN_FUNC(instance_destroy)
  15999. {
  16000. short instance_id;
  16001. if( script_hasdata(st,2) )
  16002. instance_id = script_getnum(st,2);
  16003. else
  16004. instance_id = script_instancegetid(st);
  16005. if( instance_id <= 0 || instance_id >= MAX_MAP_PER_SERVER ) {
  16006. ShowError("script:instance_destroy: Trying to destroy invalid instance %d.\n", instance_id);
  16007. return 0;
  16008. }
  16009. instance_destroy(instance_id);
  16010. return SCRIPT_CMD_SUCCESS;
  16011. }
  16012. /*==========================================
  16013. * Warps player to instance
  16014. * Results:
  16015. * 0: Success
  16016. * 1: Character not in party
  16017. * 2: Party doesn't have instance
  16018. * 3: Other errors (instance not in DB, instance doesn't match with party, etc.)
  16019. *------------------------------------------*/
  16020. BUILDIN_FUNC(instance_enter)
  16021. {
  16022. struct map_session_data *sd;
  16023. if((sd = script_rid2sd(st)) != NULL){
  16024. if (script_hasdata(st,3) && script_hasdata(st,4))
  16025. script_pushint(st,instance_enter_position(sd,script_getstr(st, 2),script_getnum(st, 3),script_getnum(st, 4)));
  16026. else
  16027. script_pushint(st,instance_enter(sd,script_getstr(st, 2)));
  16028. }
  16029. else
  16030. return 1;
  16031. return SCRIPT_CMD_SUCCESS;
  16032. }
  16033. /*==========================================
  16034. * Returns the name of a duplicated NPC
  16035. *
  16036. * instance_npcname <npc_name>{,<instance_id>};
  16037. * <npc_name> is the full name of an NPC.
  16038. *------------------------------------------*/
  16039. BUILDIN_FUNC(instance_npcname)
  16040. {
  16041. const char *str;
  16042. short instance_id = 0;
  16043. struct npc_data *nd;
  16044. str = script_getstr(st,2);
  16045. if( script_hasdata(st,3) )
  16046. instance_id = script_getnum(st,3);
  16047. else
  16048. instance_id = script_instancegetid(st);
  16049. if( instance_id && (nd = npc_name2id(str)) != NULL ) {
  16050. static char npcname[NAME_LENGTH];
  16051. snprintf(npcname, sizeof(npcname), "dup_%d_%d", instance_id, nd->bl.id);
  16052. script_pushconststr(st,npcname);
  16053. } else {
  16054. ShowError("script:instance_npcname: invalid instance NPC (instance_id: %d, NPC name: \"%s\".)\n", instance_id, str);
  16055. st->state = END;
  16056. return 1;
  16057. }
  16058. return SCRIPT_CMD_SUCCESS;
  16059. }
  16060. /*==========================================
  16061. * Returns the name of a duplicated map
  16062. *
  16063. * instance_mapname <map_name>{,<instance_id>};
  16064. *------------------------------------------*/
  16065. BUILDIN_FUNC(instance_mapname)
  16066. {
  16067. const char *str;
  16068. int16 m;
  16069. short instance_id = 0;
  16070. str = script_getstr(st,2);
  16071. if( script_hasdata(st,3) )
  16072. instance_id = script_getnum(st,3);
  16073. else
  16074. instance_id = script_instancegetid(st);
  16075. // Check that instance mapname is a valid map
  16076. if(!instance_id || (m = instance_mapname2mapid(str,instance_id)) < 0)
  16077. script_pushconststr(st, "");
  16078. else
  16079. script_pushconststr(st, map[m].name);
  16080. return SCRIPT_CMD_SUCCESS;
  16081. }
  16082. /*==========================================
  16083. * Returns an Instance ID
  16084. *------------------------------------------*/
  16085. BUILDIN_FUNC(instance_id)
  16086. {
  16087. short instance_id;
  16088. instance_id = script_instancegetid(st);
  16089. if(!instance_id) {
  16090. //ShowError("script:instance_id: No instance attached to NPC or player");
  16091. script_pushint(st, 0);
  16092. return SCRIPT_CMD_SUCCESS;
  16093. }
  16094. script_pushint(st, instance_id);
  16095. return SCRIPT_CMD_SUCCESS;
  16096. }
  16097. /*==========================================
  16098. * Warps all players inside an instance
  16099. *
  16100. * instance_warpall <map_name>,<x>,<y>{,<instance_id>};
  16101. *------------------------------------------*/
  16102. BUILDIN_FUNC(instance_warpall)
  16103. {
  16104. struct party_data *p;
  16105. int16 m, i;
  16106. short instance_id;
  16107. const char *mapn;
  16108. int x, y;
  16109. mapn = script_getstr(st,2);
  16110. x = script_getnum(st,3);
  16111. y = script_getnum(st,4);
  16112. if( script_hasdata(st,5) )
  16113. instance_id = script_getnum(st,5);
  16114. else
  16115. instance_id = script_instancegetid(st);
  16116. if( !instance_id || (m = map_mapname2mapid(mapn)) < 0 || (m = instance_mapname2mapid(map[m].name,instance_id)) < 0)
  16117. return 0;
  16118. if( !(p = party_search(instance_data[instance_id].party_id)) )
  16119. return 0;
  16120. for( i = 0; i < MAX_PARTY; i++ ) {
  16121. struct map_session_data *pl_sd;
  16122. 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);
  16123. }
  16124. return SCRIPT_CMD_SUCCESS;
  16125. }
  16126. /*==========================================
  16127. * Broadcasts to all maps inside an instance
  16128. *
  16129. * instance_announce <instance id>,"<text>",<flag>{,<fontColor>{,<fontType>{,<fontSize>{,<fontAlign>{,<fontY>}}}}};
  16130. * Using -1 for <instance id> will auto-detect the id.
  16131. *------------------------------------------*/
  16132. BUILDIN_FUNC(instance_announce) {
  16133. int instance_id = script_getnum(st,2);
  16134. const char *mes = script_getstr(st,3);
  16135. int flag = script_getnum(st,4);
  16136. const char *fontColor = script_hasdata(st,5) ? script_getstr(st,5) : NULL;
  16137. int fontType = script_hasdata(st,6) ? script_getnum(st,6) : 0x190; // default fontType (FW_NORMAL)
  16138. int fontSize = script_hasdata(st,7) ? script_getnum(st,7) : 12; // default fontSize
  16139. int fontAlign = script_hasdata(st,8) ? script_getnum(st,8) : 0; // default fontAlign
  16140. int fontY = script_hasdata(st,9) ? script_getnum(st,9) : 0; // default fontY
  16141. int i;
  16142. if( instance_id == -1 ) {
  16143. instance_id = script_instancegetid(st);
  16144. }
  16145. if( !instance_id && &instance_data[instance_id] != NULL)
  16146. return true;
  16147. for( i = 0; i < instance_data[instance_id].cnt_map; i++ )
  16148. map_foreachinmap(buildin_announce_sub, instance_data[instance_id].map[i].m, BL_PC,
  16149. mes, strlen(mes)+1, flag&BC_COLOR_MASK, fontColor, fontType, fontSize, fontAlign, fontY);
  16150. return SCRIPT_CMD_SUCCESS;
  16151. }
  16152. /*==========================================
  16153. * instance_check_party [malufett]
  16154. * Values:
  16155. * party_id : Party ID of the invoking character. [Required Parameter]
  16156. * amount : Amount of needed Partymembers for the Instance. [Optional Parameter]
  16157. * min : Minimum Level needed to join the Instance. [Optional Parameter]
  16158. * max : Maxium Level allowed to join the Instance. [Optional Parameter]
  16159. * Example: instance_check_party (getcharid(1){,amount}{,min}{,max});
  16160. * Example 2: instance_check_party (getcharid(1),1,1,99);
  16161. *------------------------------------------*/
  16162. BUILDIN_FUNC(instance_check_party)
  16163. {
  16164. int amount, min, max, i, party_id, c = 0;
  16165. struct party_data *p = NULL;
  16166. amount = script_hasdata(st,3) ? script_getnum(st,3) : 1; // Amount of needed Partymembers for the Instance.
  16167. min = script_hasdata(st,4) ? script_getnum(st,4) : 1; // Minimum Level needed to join the Instance.
  16168. max = script_hasdata(st,5) ? script_getnum(st,5) : MAX_LEVEL; // Maxium Level allowed to join the Instance.
  16169. if( min < 1 || min > MAX_LEVEL) {
  16170. ShowError("script:check_party: Invalid min level, %d\n", min);
  16171. return 1;
  16172. } else if( max < 1 || max > MAX_LEVEL) {
  16173. ShowError("script:check_party: Invalid max level, %d\n", max);
  16174. return 1;
  16175. }
  16176. if( script_hasdata(st,2) )
  16177. party_id = script_getnum(st,2);
  16178. else return 0;
  16179. if( !(p = party_search(party_id)) ) {
  16180. script_pushint(st, 0); // Returns false if party does not exist.
  16181. return 0;
  16182. }
  16183. for( i = 0; i < MAX_PARTY; i++ ) {
  16184. struct map_session_data *pl_sd;
  16185. if( (pl_sd = p->data[i].sd) )
  16186. if(map_id2bl(pl_sd->bl.id)) {
  16187. if(pl_sd->status.base_level < min) {
  16188. script_pushint(st, 0);
  16189. return 0;
  16190. } else if(pl_sd->status.base_level > max) {
  16191. script_pushint(st, 0);
  16192. return 0;
  16193. }
  16194. c++;
  16195. }
  16196. }
  16197. if(c < amount)
  16198. script_pushint(st, 0); // Not enough Members in the Party to join Instance.
  16199. else
  16200. script_pushint(st, 1);
  16201. return SCRIPT_CMD_SUCCESS;
  16202. }
  16203. /*==========================================
  16204. * Custom Fonts
  16205. *------------------------------------------*/
  16206. BUILDIN_FUNC(setfont)
  16207. {
  16208. struct map_session_data *sd = script_rid2sd(st);
  16209. int font = script_getnum(st,2);
  16210. if( sd == NULL )
  16211. return 0;
  16212. if( sd->status.font != font )
  16213. sd->status.font = font;
  16214. else
  16215. sd->status.font = 0;
  16216. clif_font(sd);
  16217. return SCRIPT_CMD_SUCCESS;
  16218. }
  16219. static int buildin_mobuseskill_sub(struct block_list *bl,va_list ap)
  16220. {
  16221. TBL_MOB* md = (TBL_MOB*)bl;
  16222. struct block_list *tbl;
  16223. int mobid = va_arg(ap,int);
  16224. uint16 skill_id = va_arg(ap,int);
  16225. uint16 skill_lv = va_arg(ap,int);
  16226. int casttime = va_arg(ap,int);
  16227. int cancel = va_arg(ap,int);
  16228. int emotion = va_arg(ap,int);
  16229. int target = va_arg(ap,int);
  16230. if( md->mob_id != mobid )
  16231. return 0;
  16232. // 0:self, 1:target, 2:master, default:random
  16233. switch( target ) {
  16234. case 0: tbl = map_id2bl(md->bl.id); break;
  16235. case 1: tbl = map_id2bl(md->target_id); break;
  16236. case 2: tbl = map_id2bl(md->master_id); break;
  16237. default:tbl = battle_getenemy(&md->bl, DEFAULT_ENEMY_TYPE(md),skill_get_range2(&md->bl, skill_id, skill_lv)); break;
  16238. }
  16239. if( !tbl )
  16240. return 0;
  16241. if( md->ud.skilltimer != INVALID_TIMER ) // Cancel the casting skill.
  16242. unit_skillcastcancel(bl,0);
  16243. if( skill_get_casttype(skill_id) == CAST_GROUND )
  16244. unit_skilluse_pos2(&md->bl, tbl->x, tbl->y, skill_id, skill_lv, casttime, cancel);
  16245. else
  16246. unit_skilluse_id2(&md->bl, tbl->id, skill_id, skill_lv, casttime, cancel);
  16247. clif_emotion(&md->bl, emotion);
  16248. return SCRIPT_CMD_SUCCESS;
  16249. }
  16250. /*==========================================
  16251. * areamobuseskill "Map Name",<x>,<y>,<range>,<Mob ID>,"Skill Name"/<Skill ID>,<Skill Lv>,<Cast Time>,<Cancelable>,<Emotion>,<Target Type>;
  16252. *------------------------------------------*/
  16253. BUILDIN_FUNC(areamobuseskill)
  16254. {
  16255. struct block_list center;
  16256. struct script_data *data;
  16257. int16 m;
  16258. int range,mobid,skill_id,skill_lv,casttime,emotion,target,cancel;
  16259. if( (m = map_mapname2mapid(script_getstr(st,2))) < 0 ) {
  16260. ShowError("areamobuseskill: invalid map name.\n");
  16261. return 0;
  16262. }
  16263. center.m = m;
  16264. center.x = script_getnum(st,3);
  16265. center.y = script_getnum(st,4);
  16266. range = script_getnum(st,5);
  16267. mobid = script_getnum(st,6);
  16268. data = script_getdata(st, 7);
  16269. get_val(st, data); // Convert into value in case of a variable
  16270. skill_id = ( data_isstring(data) ? skill_name2id(script_getstr(st,7)) : script_getnum(st,7) );
  16271. if( (skill_lv = script_getnum(st,8)) > battle_config.mob_max_skilllvl )
  16272. skill_lv = battle_config.mob_max_skilllvl;
  16273. casttime = script_getnum(st,9);
  16274. cancel = script_getnum(st,10);
  16275. emotion = script_getnum(st,11);
  16276. target = script_getnum(st,12);
  16277. map_foreachinrange(buildin_mobuseskill_sub, &center, range, BL_MOB, mobid, skill_id, skill_lv, casttime, cancel, emotion, target);
  16278. return SCRIPT_CMD_SUCCESS;
  16279. }
  16280. BUILDIN_FUNC(progressbar)
  16281. {
  16282. struct map_session_data * sd = script_rid2sd(st);
  16283. const char * color;
  16284. unsigned int second;
  16285. if( !st || !sd )
  16286. return 0;
  16287. st->state = STOP;
  16288. color = script_getstr(st,2);
  16289. second = script_getnum(st,3);
  16290. sd->progressbar.npc_id = st->oid;
  16291. sd->progressbar.timeout = gettick() + second*1000;
  16292. clif_progressbar(sd, strtol(color, (char **)NULL, 0), second);
  16293. return SCRIPT_CMD_SUCCESS;
  16294. }
  16295. BUILDIN_FUNC(pushpc)
  16296. {
  16297. uint8 dir;
  16298. int cells, dx, dy;
  16299. struct map_session_data* sd;
  16300. if((sd = script_rid2sd(st))==NULL)
  16301. {
  16302. return 0;
  16303. }
  16304. dir = script_getnum(st,2);
  16305. cells = script_getnum(st,3);
  16306. if(dir>7)
  16307. {
  16308. ShowWarning("buildin_pushpc: Invalid direction %d specified.\n", dir);
  16309. script_reportsrc(st);
  16310. dir%= 8; // trim spin-over
  16311. }
  16312. if(!cells)
  16313. {// zero distance
  16314. return 0;
  16315. }
  16316. else if(cells<0)
  16317. {// pushing backwards
  16318. dir = (dir+4)%8; // turn around
  16319. cells = -cells;
  16320. }
  16321. dx = dirx[dir];
  16322. dy = diry[dir];
  16323. unit_blown(&sd->bl, dx, dy, cells, 0);
  16324. return SCRIPT_CMD_SUCCESS;
  16325. }
  16326. /// Invokes buying store preparation window
  16327. /// buyingstore <slots>;
  16328. BUILDIN_FUNC(buyingstore)
  16329. {
  16330. struct map_session_data* sd;
  16331. if( ( sd = script_rid2sd(st) ) == NULL )
  16332. {
  16333. return 0;
  16334. }
  16335. if( npc_isnear(&sd->bl) ) {
  16336. char output[150];
  16337. sprintf(output, msg_txt(sd,662), battle_config.min_npc_vendchat_distance);
  16338. clif_displaymessage(sd->fd, output);
  16339. return 0;
  16340. }
  16341. buyingstore_setup(sd, script_getnum(st,2));
  16342. return SCRIPT_CMD_SUCCESS;
  16343. }
  16344. /// Invokes search store info window
  16345. /// searchstores <uses>,<effect>;
  16346. BUILDIN_FUNC(searchstores)
  16347. {
  16348. unsigned short effect;
  16349. unsigned int uses;
  16350. struct map_session_data* sd;
  16351. if( ( sd = script_rid2sd(st) ) == NULL )
  16352. {
  16353. return 0;
  16354. }
  16355. uses = script_getnum(st,2);
  16356. effect = script_getnum(st,3);
  16357. if( !uses )
  16358. {
  16359. ShowError("buildin_searchstores: Amount of uses cannot be zero.\n");
  16360. return 1;
  16361. }
  16362. if( effect > 1 )
  16363. {
  16364. ShowError("buildin_searchstores: Invalid effect id %hu, specified.\n", effect);
  16365. return 1;
  16366. }
  16367. searchstore_open(sd, uses, effect);
  16368. return SCRIPT_CMD_SUCCESS;
  16369. }
  16370. /// Displays a number as large digital clock.
  16371. /// showdigit <value>[,<type>];
  16372. BUILDIN_FUNC(showdigit)
  16373. {
  16374. unsigned int type = 0;
  16375. int value;
  16376. struct map_session_data* sd;
  16377. if( ( sd = script_rid2sd(st) ) == NULL )
  16378. {
  16379. return 0;
  16380. }
  16381. value = script_getnum(st,2);
  16382. if( script_hasdata(st,3) )
  16383. {
  16384. type = script_getnum(st,3);
  16385. if( type > 3 )
  16386. {
  16387. ShowError("buildin_showdigit: Invalid type %u.\n", type);
  16388. return 1;
  16389. }
  16390. }
  16391. clif_showdigit(sd, (unsigned char)type, value);
  16392. return SCRIPT_CMD_SUCCESS;
  16393. }
  16394. /**
  16395. * Rune Knight
  16396. * makerune <% success bonus>{,<char_id>};
  16397. **/
  16398. BUILDIN_FUNC(makerune) {
  16399. TBL_PC* sd;
  16400. if (!script_charid2sd(3,sd))
  16401. return SCRIPT_CMD_FAILURE;
  16402. clif_skill_produce_mix_list(sd,RK_RUNEMASTERY,24);
  16403. sd->itemid = script_getnum(st,2);
  16404. return SCRIPT_CMD_SUCCESS;
  16405. }
  16406. /**
  16407. * checkdragon({<char_id>}) returns 1 if mounting a dragon or 0 otherwise.
  16408. **/
  16409. BUILDIN_FUNC(checkdragon) {
  16410. TBL_PC* sd;
  16411. if (!script_charid2sd(2,sd))
  16412. return SCRIPT_CMD_FAILURE;
  16413. if( pc_isridingdragon(sd) )
  16414. script_pushint(st,1);
  16415. else
  16416. script_pushint(st,0);
  16417. return SCRIPT_CMD_SUCCESS;
  16418. }
  16419. /**
  16420. * setdragon({optional Color{,<char_id>}}) returns 1 on success or 0 otherwise
  16421. * - Toggles the dragon on a RK if he can mount;
  16422. * @param Color - when not provided uses the green dragon;
  16423. * - 1 : Green Dragon
  16424. * - 2 : Brown Dragon
  16425. * - 3 : Gray Dragon
  16426. * - 4 : Blue Dragon
  16427. * - 5 : Red Dragon
  16428. **/
  16429. BUILDIN_FUNC(setdragon) {
  16430. TBL_PC* sd;
  16431. int color = script_hasdata(st,2) ? script_getnum(st,2) : 0;
  16432. if (!script_charid2sd(3,sd))
  16433. return SCRIPT_CMD_FAILURE;
  16434. if( !pc_checkskill(sd,RK_DRAGONTRAINING) || (sd->class_&MAPID_THIRDMASK) != MAPID_RUNE_KNIGHT )
  16435. script_pushint(st,0);//Doesn't have the skill or it's not a Rune Knight
  16436. else if ( pc_isridingdragon(sd) ) {//Is mounted; release
  16437. pc_setoption(sd, sd->sc.option&~OPTION_DRAGON);
  16438. script_pushint(st,1);
  16439. } else {//Not mounted; Mount now.
  16440. unsigned int option = OPTION_DRAGON1;
  16441. if( color ) {
  16442. option = ( color == 1 ? OPTION_DRAGON1 :
  16443. color == 2 ? OPTION_DRAGON2 :
  16444. color == 3 ? OPTION_DRAGON3 :
  16445. color == 4 ? OPTION_DRAGON4 :
  16446. color == 5 ? OPTION_DRAGON5 : 0);
  16447. if( !option ) {
  16448. ShowWarning("script_setdragon: Unknown Color %d used; changing to green (1)\n",color);
  16449. option = OPTION_DRAGON1;
  16450. }
  16451. }
  16452. pc_setoption(sd, sd->sc.option|option);
  16453. script_pushint(st,1);
  16454. }
  16455. return SCRIPT_CMD_SUCCESS;
  16456. }
  16457. /**
  16458. * ismounting({<char_id>}) returns 1 if mounting a new mount or 0 otherwise
  16459. **/
  16460. BUILDIN_FUNC(ismounting) {
  16461. TBL_PC* sd;
  16462. if (!script_charid2sd(2,sd))
  16463. return SCRIPT_CMD_FAILURE;
  16464. if( &sd->sc && sd->sc.data[SC_ALL_RIDING] )
  16465. script_pushint(st,1);
  16466. else
  16467. script_pushint(st,0);
  16468. return SCRIPT_CMD_SUCCESS;
  16469. }
  16470. /**
  16471. * setmounting({<char_id>}) returns 1 on success or 0 otherwise
  16472. * - Toggles new mounts on a player when he can mount
  16473. * - Will fail if the player is mounting a non-new mount, e.g. dragon, peco, wug, etc.
  16474. * - Will unmount the player is he is already mounting
  16475. **/
  16476. BUILDIN_FUNC(setmounting) {
  16477. TBL_PC* sd;
  16478. if (!script_charid2sd(2,sd))
  16479. return SCRIPT_CMD_FAILURE;
  16480. if( &sd->sc && sd->sc.option&(OPTION_WUGRIDER|OPTION_RIDING|OPTION_DRAGON|OPTION_MADOGEAR) ) {
  16481. clif_msgtable(sd->fd, NEED_REINS_OF_MOUNT);
  16482. script_pushint(st,0); //can't mount with one of these
  16483. } else {
  16484. if( &sd->sc && sd->sc.data[SC_ALL_RIDING] )
  16485. status_change_end(&sd->bl, SC_ALL_RIDING, INVALID_TIMER); //release mount
  16486. else
  16487. sc_start(NULL, &sd->bl, SC_ALL_RIDING, 10000, 1, INVALID_TIMER); //mount
  16488. script_pushint(st,1);//in both cases, return 1.
  16489. }
  16490. return SCRIPT_CMD_SUCCESS;
  16491. }
  16492. /**
  16493. * Retrieves quantity of arguments provided to callfunc/callsub.
  16494. * getargcount() -> amount of arguments received in a function
  16495. **/
  16496. BUILDIN_FUNC(getargcount) {
  16497. struct script_retinfo* ri;
  16498. if( st->stack->defsp < 1 || st->stack->stack_data[st->stack->defsp - 1].type != C_RETINFO ) {
  16499. ShowError("script:getargcount: used out of function or callsub label!\n");
  16500. st->state = END;
  16501. return 1;
  16502. }
  16503. ri = st->stack->stack_data[st->stack->defsp - 1].u.ri;
  16504. script_pushint(st, ri->nargs);
  16505. return SCRIPT_CMD_SUCCESS;
  16506. }
  16507. /**
  16508. * getcharip(<account ID>/<character ID>/<character name>)
  16509. **/
  16510. BUILDIN_FUNC(getcharip)
  16511. {
  16512. struct map_session_data* sd = NULL;
  16513. /* check if a character name is specified */
  16514. if( script_hasdata(st, 2) )
  16515. {
  16516. struct script_data *data;
  16517. data = script_getdata(st, 2);
  16518. get_val(st, data); // Convert into value in case of a variable
  16519. if (data_isstring(data))
  16520. sd = map_nick2sd(script_getstr(st, 2));
  16521. else if (data_isint(data) || script_getnum(st, 2))
  16522. {
  16523. int id = 0;
  16524. id = script_getnum(st, 2);
  16525. sd = (map_id2sd(id) ? map_id2sd(id) : map_charid2sd(id));
  16526. }
  16527. }
  16528. else
  16529. sd = script_rid2sd(st);
  16530. /* check for sd and IP */
  16531. if (!sd || !session[sd->fd]->client_addr)
  16532. {
  16533. script_pushconststr(st, "");
  16534. return 0;
  16535. }
  16536. /* return the client ip_addr converted for output */
  16537. if (sd && sd->fd && session[sd->fd])
  16538. {
  16539. /* initiliaze */
  16540. const char *ip_addr = NULL;
  16541. uint32 ip;
  16542. /* set ip, ip_addr and convert to ip and push str */
  16543. ip = session[sd->fd]->client_addr;
  16544. ip_addr = ip2str(ip, NULL);
  16545. script_pushstrcopy(st, ip_addr);
  16546. }
  16547. return SCRIPT_CMD_SUCCESS;
  16548. }
  16549. /**
  16550. * is_function(<function name>) -> 1 if function exists, 0 otherwise
  16551. **/
  16552. BUILDIN_FUNC(is_function) {
  16553. const char* str = script_getstr(st,2);
  16554. if( strdb_exists(userfunc_db, str) )
  16555. script_pushint(st,1);
  16556. else
  16557. script_pushint(st,0);
  16558. return SCRIPT_CMD_SUCCESS;
  16559. }
  16560. /**
  16561. * get_revision() -> retrieves the current svn revision (if available)
  16562. **/
  16563. BUILDIN_FUNC(get_revision) {
  16564. const char *svn = get_svn_revision();
  16565. if ( svn[0] != UNKNOWN_VERSION )
  16566. script_pushint(st,atoi(svn));
  16567. else
  16568. script_pushint(st,-1); //unknown
  16569. return SCRIPT_CMD_SUCCESS;
  16570. }
  16571. /* get_hash() -> retrieves the current git hash (if available)*/
  16572. BUILDIN_FUNC(get_githash) {
  16573. const char* git = get_git_hash();
  16574. char buf[CHAT_SIZE_MAX];
  16575. safestrncpy(buf,git,strlen(git)+1);
  16576. if ( git[0] != UNKNOWN_VERSION )
  16577. script_pushstr(st,buf);
  16578. else
  16579. script_pushstr(st,"Unknown"); //unknown
  16580. return SCRIPT_CMD_SUCCESS;
  16581. }
  16582. /**
  16583. * freeloop(<toggle>) -> toggles this script instance's looping-check ability
  16584. **/
  16585. BUILDIN_FUNC(freeloop) {
  16586. if( script_hasdata(st,2) ) {
  16587. if( script_getnum(st,2) )
  16588. st->freeloop = 1;
  16589. else
  16590. st->freeloop = 0;
  16591. }
  16592. script_pushint(st, st->freeloop);
  16593. return SCRIPT_CMD_SUCCESS;
  16594. }
  16595. /**
  16596. * @commands (script based)
  16597. **/
  16598. BUILDIN_FUNC(bindatcmd) {
  16599. const char* atcmd;
  16600. const char* eventName;
  16601. int i, level = 0, level2 = 0;
  16602. bool create = false;
  16603. atcmd = script_getstr(st,2);
  16604. eventName = script_getstr(st,3);
  16605. if( *atcmd == atcommand_symbol || *atcmd == charcommand_symbol )
  16606. atcmd++;
  16607. if( script_hasdata(st,4) ) level = script_getnum(st,4);
  16608. if( script_hasdata(st,5) ) level2 = script_getnum(st,5);
  16609. if( atcmd_binding_count == 0 ) {
  16610. CREATE(atcmd_binding,struct atcmd_binding_data*,1);
  16611. create = true;
  16612. } else {
  16613. ARR_FIND(0, atcmd_binding_count, i, strcmp(atcmd_binding[i]->command,atcmd) == 0);
  16614. if( i < atcmd_binding_count ) {/* update existent entry */
  16615. safestrncpy(atcmd_binding[i]->npc_event, eventName, 50);
  16616. atcmd_binding[i]->level = level;
  16617. atcmd_binding[i]->level2 = level2;
  16618. } else
  16619. create = true;
  16620. }
  16621. if( create ) {
  16622. i = atcmd_binding_count;
  16623. if( atcmd_binding_count++ != 0 )
  16624. RECREATE(atcmd_binding,struct atcmd_binding_data*,atcmd_binding_count);
  16625. CREATE(atcmd_binding[i],struct atcmd_binding_data,1);
  16626. safestrncpy(atcmd_binding[i]->command, atcmd, 50);
  16627. safestrncpy(atcmd_binding[i]->npc_event, eventName, 50);
  16628. atcmd_binding[i]->level = level;
  16629. atcmd_binding[i]->level2 = level2;
  16630. }
  16631. return SCRIPT_CMD_SUCCESS;
  16632. }
  16633. BUILDIN_FUNC(unbindatcmd) {
  16634. const char* atcmd;
  16635. int i = 0;
  16636. atcmd = script_getstr(st, 2);
  16637. if( *atcmd == atcommand_symbol || *atcmd == charcommand_symbol )
  16638. atcmd++;
  16639. if( atcmd_binding_count == 0 ) {
  16640. script_pushint(st, 0);
  16641. return 0;
  16642. }
  16643. ARR_FIND(0, atcmd_binding_count, i, strcmp(atcmd_binding[i]->command, atcmd) == 0);
  16644. if( i < atcmd_binding_count ) {
  16645. int cursor = 0;
  16646. aFree(atcmd_binding[i]);
  16647. atcmd_binding[i] = NULL;
  16648. /* compact the list now that we freed a slot somewhere */
  16649. for( i = 0, cursor = 0; i < atcmd_binding_count; i++ ) {
  16650. if( atcmd_binding[i] == NULL )
  16651. continue;
  16652. if( cursor != i ) {
  16653. memmove(&atcmd_binding[cursor], &atcmd_binding[i], sizeof(struct atcmd_binding_data*));
  16654. }
  16655. cursor++;
  16656. }
  16657. if( (atcmd_binding_count = cursor) == 0 )
  16658. aFree(atcmd_binding);
  16659. script_pushint(st, 1);
  16660. } else
  16661. script_pushint(st, 0);/* not found */
  16662. return SCRIPT_CMD_SUCCESS;
  16663. }
  16664. BUILDIN_FUNC(useatcmd) {
  16665. return atcommand_sub(st,3);
  16666. }
  16667. BUILDIN_FUNC(checkre)
  16668. {
  16669. int num;
  16670. num=script_getnum(st,2);
  16671. switch(num){
  16672. case 0:
  16673. #ifdef RENEWAL
  16674. script_pushint(st, 1);
  16675. #else
  16676. script_pushint(st, 0);
  16677. #endif
  16678. break;
  16679. case 1:
  16680. #ifdef RENEWAL_CAST
  16681. script_pushint(st, 1);
  16682. #else
  16683. script_pushint(st, 0);
  16684. #endif
  16685. break;
  16686. case 2:
  16687. #ifdef RENEWAL_DROP
  16688. script_pushint(st, 1);
  16689. #else
  16690. script_pushint(st, 0);
  16691. #endif
  16692. break;
  16693. case 3:
  16694. #ifdef RENEWAL_EXP
  16695. script_pushint(st, 1);
  16696. #else
  16697. script_pushint(st, 0);
  16698. #endif
  16699. break;
  16700. case 4:
  16701. #ifdef RENEWAL_LVDMG
  16702. script_pushint(st, 1);
  16703. #else
  16704. script_pushint(st, 0);
  16705. #endif
  16706. break;
  16707. case 5:
  16708. #ifdef RENEWAL_ASPD
  16709. script_pushint(st, 1);
  16710. #else
  16711. script_pushint(st, 0);
  16712. #endif
  16713. break;
  16714. default:
  16715. ShowWarning("buildin_checkre: unknown parameter.\n");
  16716. break;
  16717. }
  16718. return SCRIPT_CMD_SUCCESS;
  16719. }
  16720. /* getrandgroupitem <group_id>{,<quantity>{,<sub_group>}} */
  16721. BUILDIN_FUNC(getrandgroupitem) {
  16722. TBL_PC* sd;
  16723. int i, get_count = 0;
  16724. uint16 group = script_getnum(st,2), qty = 0;
  16725. uint8 sub_group = 1;
  16726. struct item item_tmp;
  16727. struct s_item_group_entry *entry = NULL;
  16728. if (!(sd = script_rid2sd(st)))
  16729. return SCRIPT_CMD_SUCCESS;
  16730. if (!group) {
  16731. ShowError("buildin_getrandgroupitem: Invalid group id (%d)!\n",script_getnum(st,2));
  16732. return SCRIPT_CMD_FAILURE;
  16733. }
  16734. FETCH(3, qty);
  16735. FETCH(4, sub_group);
  16736. entry = itemdb_get_randgroupitem(group,sub_group);
  16737. if (!entry)
  16738. return SCRIPT_CMD_FAILURE; //ensure valid itemid
  16739. memset(&item_tmp,0,sizeof(item_tmp));
  16740. item_tmp.nameid = entry->nameid;
  16741. item_tmp.identify = itemdb_isidentified(entry->nameid);
  16742. if (!qty)
  16743. qty = entry->amount;
  16744. //Check if it's stackable.
  16745. if (!itemdb_isstackable(entry->nameid)) {
  16746. item_tmp.amount = 1;
  16747. get_count = qty;
  16748. }
  16749. else {
  16750. item_tmp.amount = qty;
  16751. get_count = 1;
  16752. }
  16753. for (i = 0; i < get_count; i++) {
  16754. // if not pet egg
  16755. if (!pet_create_egg(sd, entry->nameid)) {
  16756. unsigned char flag = 0;
  16757. if ((flag = pc_additem(sd,&item_tmp,item_tmp.amount,LOG_TYPE_SCRIPT))) {
  16758. clif_additem(sd,0,0,flag);
  16759. if (pc_candrop(sd,&item_tmp))
  16760. map_addflooritem(&item_tmp,item_tmp.amount,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0);
  16761. }
  16762. }
  16763. }
  16764. return SCRIPT_CMD_SUCCESS;
  16765. }
  16766. /* getgroupitem <group_id>;
  16767. * Gives item(s) to the attached player based on item group contents
  16768. */
  16769. BUILDIN_FUNC(getgroupitem) {
  16770. TBL_PC *sd;
  16771. int group_id = script_getnum(st,2);
  16772. if (!(sd = script_rid2sd(st)))
  16773. return SCRIPT_CMD_SUCCESS;
  16774. if (itemdb_pc_get_itemgroup(group_id,sd)) {
  16775. ShowError("getgroupitem: Invalid group id '%d' specified.\n",group_id);
  16776. return SCRIPT_CMD_FAILURE;
  16777. }
  16778. return SCRIPT_CMD_SUCCESS;
  16779. }
  16780. /* cleanmap <map_name>;
  16781. * cleanarea <map_name>, <x0>, <y0>, <x1>, <y1>; */
  16782. static int atcommand_cleanfloor_sub(struct block_list *bl, va_list ap)
  16783. {
  16784. nullpo_ret(bl);
  16785. map_clearflooritem(bl);
  16786. return SCRIPT_CMD_SUCCESS;
  16787. }
  16788. BUILDIN_FUNC(cleanmap)
  16789. {
  16790. const char *mapname;
  16791. int16 m;
  16792. int16 x0 = 0, y0 = 0, x1 = 0, y1 = 0;
  16793. mapname = script_getstr(st, 2);
  16794. m = map_mapname2mapid(mapname);
  16795. if (!m)
  16796. return 1;
  16797. if ((script_lastdata(st) - 2) < 4) {
  16798. map_foreachinmap(atcommand_cleanfloor_sub, m, BL_ITEM);
  16799. } else {
  16800. x0 = script_getnum(st, 3);
  16801. y0 = script_getnum(st, 4);
  16802. x1 = script_getnum(st, 5);
  16803. y1 = script_getnum(st, 6);
  16804. if (x0 > 0 && y0 > 0 && x1 > 0 && y1 > 0) {
  16805. map_foreachinarea(atcommand_cleanfloor_sub, m, x0, y0, x1, y1, BL_ITEM);
  16806. } else {
  16807. ShowError("cleanarea: invalid coordinate defined!\n");
  16808. return 1;
  16809. }
  16810. }
  16811. return SCRIPT_CMD_SUCCESS;
  16812. }
  16813. /* Cast a skill on the attached player.
  16814. * npcskill <skill id>, <skill lvl>, <stat point>, <NPC level>;
  16815. * npcskill "<skill name>", <skill lvl>, <stat point>, <NPC level>; */
  16816. BUILDIN_FUNC(npcskill)
  16817. {
  16818. uint16 skill_id;
  16819. unsigned short skill_level;
  16820. unsigned int stat_point;
  16821. unsigned int npc_level;
  16822. struct npc_data *nd;
  16823. struct map_session_data *sd;
  16824. struct script_data *data;
  16825. data = script_getdata(st, 2);
  16826. get_val(st, data); // Convert into value in case of a variable
  16827. skill_id = data_isstring(data) ? skill_name2id(script_getstr(st, 2)) : script_getnum(st, 2);
  16828. skill_level = script_getnum(st, 3);
  16829. stat_point = script_getnum(st, 4);
  16830. npc_level = script_getnum(st, 5);
  16831. sd = script_rid2sd(st);
  16832. nd = (struct npc_data *)map_id2bl(sd->npc_id);
  16833. if (stat_point > battle_config.max_third_parameter) {
  16834. ShowError("npcskill: stat point exceeded maximum of %d.\n",battle_config.max_third_parameter );
  16835. return 1;
  16836. }
  16837. if (npc_level > MAX_LEVEL) {
  16838. ShowError("npcskill: level exceeded maximum of %d.\n", MAX_LEVEL);
  16839. return 1;
  16840. }
  16841. if (sd == NULL || nd == NULL) { //ain't possible, but I don't trust people.
  16842. return 1;
  16843. }
  16844. nd->level = npc_level;
  16845. nd->stat_point = stat_point;
  16846. if (!nd->status.hp)
  16847. status_calc_npc(nd, SCO_FIRST);
  16848. else
  16849. status_calc_npc(nd, SCO_NONE);
  16850. if (skill_get_inf(skill_id)&INF_GROUND_SKILL)
  16851. unit_skilluse_pos(&nd->bl, sd->bl.x, sd->bl.y, skill_id, skill_level);
  16852. else
  16853. unit_skilluse_id(&nd->bl, sd->bl.id, skill_id, skill_level);
  16854. return SCRIPT_CMD_SUCCESS;
  16855. }
  16856. /* Consumes an item.
  16857. * consumeitem <item id>{,<char_id>};
  16858. * consumeitem "<item name>"{,<char_id>};
  16859. * @param item: Item ID or name
  16860. */
  16861. BUILDIN_FUNC(consumeitem)
  16862. {
  16863. TBL_PC *sd;
  16864. struct script_data *data;
  16865. struct item_data *item_data;
  16866. if (!script_charid2sd(3, sd))
  16867. return SCRIPT_CMD_FAILURE;
  16868. data = script_getdata( st, 2 );
  16869. get_val( st, data );
  16870. if( data_isstring( data ) ){
  16871. const char *name = conv_str( st, data );
  16872. if( ( item_data = itemdb_searchname( name ) ) == NULL ){
  16873. ShowError( "buildin_consumeitem: Nonexistant item %s requested.\n", name );
  16874. return SCRIPT_CMD_FAILURE;
  16875. }
  16876. }else if( data_isint( data ) ){
  16877. unsigned short nameid = conv_num( st, data );
  16878. if( ( item_data = itemdb_exists( nameid ) ) == NULL ){
  16879. ShowError("buildin_consumeitem: Nonexistant item %hu requested.\n", nameid );
  16880. return SCRIPT_CMD_FAILURE;
  16881. }
  16882. }else{
  16883. ShowError("buildin_consumeitem: invalid data type for argument #1 (%d).\n", data->type );
  16884. return SCRIPT_CMD_FAILURE;
  16885. }
  16886. run_script( item_data->script, 0, sd->bl.id, 0 );
  16887. return SCRIPT_CMD_SUCCESS;
  16888. }
  16889. /** Makes a player sit/stand.
  16890. * sit {"<character name>"};
  16891. * stand {"<character name>"};
  16892. * Note: Use readparam(Sitting) which returns 1 or 0 (sitting or standing).
  16893. * @param name: Player name that will be invoked
  16894. */
  16895. BUILDIN_FUNC(sit)
  16896. {
  16897. TBL_PC *sd;
  16898. if( script_hasdata(st, 2) )
  16899. sd = map_nick2sd(script_getstr(st, 2));
  16900. else
  16901. sd = script_rid2sd(st);
  16902. if( sd == NULL)
  16903. return SCRIPT_CMD_FAILURE;
  16904. if( !pc_issit(sd) ) {
  16905. pc_setsit(sd);
  16906. skill_sit(sd, 1);
  16907. clif_sitting(&sd->bl);
  16908. }
  16909. return SCRIPT_CMD_SUCCESS;
  16910. }
  16911. /** Makes player to stand
  16912. * @param name: Player name that will be set
  16913. */
  16914. BUILDIN_FUNC(stand)
  16915. {
  16916. TBL_PC *sd;
  16917. if( script_hasdata(st, 2) )
  16918. sd = map_nick2sd(script_getstr(st, 2));
  16919. else
  16920. sd = script_rid2sd(st);
  16921. if( sd == NULL)
  16922. return SCRIPT_CMD_FAILURE;
  16923. if( pc_issit(sd) && pc_setstand(sd, false)) {
  16924. skill_sit(sd, 0);
  16925. clif_standing(&sd->bl);
  16926. }
  16927. return SCRIPT_CMD_SUCCESS;
  16928. }
  16929. /** Creates an array of bounded item IDs
  16930. * countbound {<type>{,<char_id>}};
  16931. * @param type: 0 - All bound items; 1 - Account Bound; 2 - Guild Bound; 3 - Party Bound
  16932. * @return amt: Amount of items found
  16933. */
  16934. BUILDIN_FUNC(countbound)
  16935. {
  16936. int i, type, j = 0, k = 0;
  16937. TBL_PC *sd;
  16938. if (!script_charid2sd(3,sd))
  16939. return SCRIPT_CMD_FAILURE;
  16940. type = script_getnum(st,2);
  16941. for( i = 0; i < MAX_INVENTORY; i ++ ) {
  16942. if( sd->status.inventory[i].nameid > 0 && (
  16943. (!type && sd->status.inventory[i].bound) || (type && sd->status.inventory[i].bound == type)
  16944. ))
  16945. {
  16946. pc_setreg(sd,reference_uid(add_str("@bound_items"), k),sd->status.inventory[i].nameid);
  16947. k++;
  16948. j += sd->status.inventory[i].amount;
  16949. }
  16950. }
  16951. script_pushint(st,j);
  16952. return SCRIPT_CMD_SUCCESS;
  16953. }
  16954. /** Creates new party
  16955. * party_create "<party name>"{,<char id>{,<item share>{,<item share type>}}};
  16956. * @param party_name: String of party name that will be created
  16957. * @param char_id: Chara that will be leader of this new party. If no char_id specified, the invoker will be party leader
  16958. * @param item_share: 0-Each Take. 1-Party Share
  16959. * @param item_share_type: 0-Each Take. 1-Even Share
  16960. * @return val: Result value
  16961. * -3 - party name is exist
  16962. * -2 - player is in party already
  16963. * -1 - player is not found
  16964. * 0 - unknown error
  16965. * 1 - success, will return party id $@party_create_id
  16966. */
  16967. BUILDIN_FUNC(party_create)
  16968. {
  16969. char party_name[NAME_LENGTH];
  16970. int item1 = 0, item2 = 0;
  16971. TBL_PC *sd;
  16972. if( (!script_hasdata(st,3) && !(sd = script_rid2sd(st))) || (script_hasdata(st,3) && !(sd = map_charid2sd(script_getnum(st,3)))) ) {
  16973. script_pushint(st,-1);
  16974. return SCRIPT_CMD_FAILURE;
  16975. }
  16976. if( sd->status.party_id ) {
  16977. script_pushint(st,-2);
  16978. return SCRIPT_CMD_FAILURE;
  16979. }
  16980. safestrncpy(party_name,script_getstr(st,2),NAME_LENGTH);
  16981. trim(party_name);
  16982. if( party_searchname(party_name) ) {
  16983. script_pushint(st,-3);
  16984. return SCRIPT_CMD_FAILURE;
  16985. }
  16986. if( script_getnum(st,4) )
  16987. item1 = 1;
  16988. if( script_getnum(st,5) )
  16989. item2 = 1;
  16990. party_create_byscript = 1;
  16991. script_pushint(st,party_create(sd,party_name,item1,item2));
  16992. return SCRIPT_CMD_SUCCESS;
  16993. }
  16994. /** Adds player to specified party
  16995. * party_addmember <party id>,<char id>;
  16996. * @param party_id: The party that will be entered by player
  16997. * @param char_id: Char id of player that will be joined to the party
  16998. * @return val: Result value
  16999. * -4 - party is full
  17000. * -3 - party is not found
  17001. * -2 - player is in party already
  17002. * -1 - player is not found
  17003. * 0 - unknown error
  17004. * 1 - success
  17005. */
  17006. BUILDIN_FUNC(party_addmember)
  17007. {
  17008. int party_id = script_getnum(st,2);
  17009. TBL_PC *sd;
  17010. struct party_data *party;
  17011. if( !(sd = map_charid2sd(script_getnum(st,3))) ) {
  17012. script_pushint(st,-1);
  17013. return SCRIPT_CMD_FAILURE;
  17014. }
  17015. if( sd->status.party_id ) {
  17016. script_pushint(st,-2);
  17017. return SCRIPT_CMD_FAILURE;
  17018. }
  17019. if( !(party = party_search(party_id)) ) {
  17020. script_pushint(st,-3);
  17021. return SCRIPT_CMD_FAILURE;
  17022. }
  17023. if( party->party.count >= MAX_PARTY ) {
  17024. script_pushint(st,-4);
  17025. return SCRIPT_CMD_FAILURE;
  17026. }
  17027. sd->party_invite = party_id;
  17028. script_pushint(st,party_add_member(party_id,sd));
  17029. return SCRIPT_CMD_SUCCESS;
  17030. }
  17031. /** Removes player from his/her party. If party_id and char_id is empty remove the invoker from his/her party
  17032. * party_delmember {<char id>,<party_id>};
  17033. * @param: char_id
  17034. * @param: party_id
  17035. * @return val: Result value
  17036. * -3 - player is not in party
  17037. * -2 - party is not found
  17038. * -1 - player is not found
  17039. * 0 - unknown error
  17040. * 1 - success
  17041. */
  17042. BUILDIN_FUNC(party_delmember)
  17043. {
  17044. TBL_PC *sd = NULL;
  17045. if( !script_hasdata(st,2) && !script_hasdata(st,3) && !(sd = script_rid2sd(st)) ) {
  17046. script_pushint(st,-1);
  17047. return SCRIPT_CMD_FAILURE;
  17048. }
  17049. if( sd || (script_getnum(st,2) && (sd = map_charid2sd(script_getnum(st,2)))) )
  17050. script_pushint(st,party_removemember2(sd,0,0));
  17051. else
  17052. script_pushint(st,party_removemember2(NULL,script_getnum(st,2),script_getnum(st,3)));
  17053. return SCRIPT_CMD_SUCCESS;
  17054. }
  17055. /** Changes party leader of specified party (even the leader is offline)
  17056. * party_changeleader <party id>,<char id>;
  17057. * @param party_id: ID of party
  17058. * @param char_id: Char ID of new leader
  17059. * @return val: Result value
  17060. * -4 - player is party leader already
  17061. * -3 - player is not in this party
  17062. * -2 - player is not found
  17063. * -1 - party is not found
  17064. * 0 - unknown error
  17065. * 1 - success */
  17066. BUILDIN_FUNC(party_changeleader)
  17067. {
  17068. int i, party_id = script_getnum(st,2);
  17069. TBL_PC *sd = NULL;
  17070. TBL_PC *tsd = NULL;
  17071. struct party_data *party = NULL;
  17072. if( !(party = party_search(party_id)) ) {
  17073. script_pushint(st,-1);
  17074. return SCRIPT_CMD_FAILURE;
  17075. }
  17076. if( !(tsd = map_charid2sd(script_getnum(st,3))) ) {
  17077. script_pushint(st,-2);
  17078. return SCRIPT_CMD_FAILURE;
  17079. }
  17080. if( tsd->status.party_id != party_id ) {
  17081. script_pushint(st,-3);
  17082. return SCRIPT_CMD_FAILURE;
  17083. }
  17084. ARR_FIND(0,MAX_PARTY,i,party->party.member[i].leader);
  17085. if( i >= MAX_PARTY ) { //this is should impossible!
  17086. script_pushint(st,0);
  17087. return SCRIPT_CMD_FAILURE;
  17088. }
  17089. if( party->data[i].sd == tsd ) {
  17090. script_pushint(st,-4);
  17091. return SCRIPT_CMD_FAILURE;
  17092. }
  17093. script_pushint(st,party_changeleader(sd,tsd,party));
  17094. return SCRIPT_CMD_SUCCESS;
  17095. }
  17096. /** Changes party option
  17097. * party_changeoption <party id>,<option>,<flag>;
  17098. * @param party_id: ID of party that will be changed
  17099. * @param option: Type of option
  17100. * @return val: -1 - party is not found, 0 - invalid option, 1 - success
  17101. */
  17102. BUILDIN_FUNC(party_changeoption)
  17103. {
  17104. struct party_data *party;
  17105. if( !(party = party_search(script_getnum(st,2))) ) {
  17106. script_pushint(st,-1);
  17107. return SCRIPT_CMD_FAILURE;
  17108. }
  17109. script_pushint(st,party_setoption(party,script_getnum(st,3),script_getnum(st,4)));
  17110. return SCRIPT_CMD_SUCCESS;
  17111. }
  17112. /** Destroys party with party id.
  17113. * party_destroy <party id>;
  17114. * @param party_id: ID of party that will be destroyed
  17115. */
  17116. BUILDIN_FUNC(party_destroy)
  17117. {
  17118. int i;
  17119. struct party_data *party;
  17120. if( !(party = party_search(script_getnum(st,2))) ) {
  17121. script_pushint(st,0);
  17122. return SCRIPT_CMD_FAILURE;
  17123. }
  17124. ARR_FIND(0,MAX_PARTY,i,party->party.member[i].leader);
  17125. if( i >= MAX_PARTY || !party->data[i].sd ) { //leader not online
  17126. int j;
  17127. for( j = 0; j < MAX_PARTY; j++ ) {
  17128. TBL_PC *sd = party->data[j].sd;
  17129. if(sd)
  17130. party_member_withdraw(party->party.party_id,sd->status.account_id,sd->status.char_id);
  17131. else if( party->party.member[j].char_id )
  17132. intif_party_leave(party->party.party_id,party->party.member[j].account_id,party->party.member[j].char_id);
  17133. }
  17134. party_broken(party->party.party_id);
  17135. script_pushint(st,1);
  17136. }
  17137. else //leader leave = party broken
  17138. script_pushint(st,party_leave(party->data[i].sd));
  17139. return SCRIPT_CMD_SUCCESS;
  17140. }
  17141. /** Checks if a player's client version meets a required version or date.
  17142. * @param type: 0 - check by version number; 1 - check by date
  17143. * @param data: Input
  17144. */
  17145. BUILDIN_FUNC(is_clientver) {
  17146. TBL_PC *sd = NULL;
  17147. int type = script_getnum(st,2);
  17148. int data = script_getnum(st,3);
  17149. int ret = 0;
  17150. if (script_hasdata(st,4))
  17151. sd = map_charid2sd(script_getnum(st,4));
  17152. else
  17153. sd = script_rid2sd(st);
  17154. if (sd == NULL) {
  17155. script_pushint(st,0);
  17156. return SCRIPT_CMD_FAILURE;
  17157. }
  17158. switch(type){
  17159. case 0:
  17160. ret = (sd->packet_ver >= data)?1:0;
  17161. break;
  17162. case 1:
  17163. ret = (sd->packet_ver >= date2version(data))?1:0;
  17164. break;
  17165. }
  17166. script_pushint(st,ret);
  17167. return SCRIPT_CMD_SUCCESS;
  17168. }
  17169. /** Returns various information about a player's VIP status. Need to enable VIP system
  17170. * vip_status <type>,{"<character name>"};
  17171. * @param type: Info type, 1: VIP status, 2: Expired date, 3: Remaining time
  17172. * @param name: Character name (Optional)
  17173. */
  17174. BUILDIN_FUNC(vip_status) {
  17175. #ifdef VIP_ENABLE
  17176. TBL_PC *sd;
  17177. char vip_str[26];
  17178. time_t now = time(NULL);
  17179. int type = script_getnum(st, 2);
  17180. if (script_hasdata(st, 3))
  17181. sd = map_nick2sd(script_getstr(st, 3));
  17182. else
  17183. sd = script_rid2sd(st);
  17184. if (sd == NULL)
  17185. return SCRIPT_CMD_FAILURE;
  17186. switch(type) {
  17187. case 1: // Get VIP status.
  17188. script_pushint(st, pc_isvip(sd));
  17189. break;
  17190. case 2: // Get VIP expire date.
  17191. if (pc_isvip(sd)) {
  17192. time_t viptime = sd->vip.time;
  17193. strftime(vip_str, 24, "%Y-%m-%d %H:%M", localtime(&viptime));
  17194. vip_str[25] = '\0';
  17195. script_pushstrcopy(st, vip_str);
  17196. } else
  17197. script_pushint(st, 0);
  17198. break;
  17199. case 3: // Get remaining time.
  17200. if (pc_isvip(sd)) {
  17201. time_t viptime_remain = sd->vip.time - now;
  17202. int year=0,month=0,day=0,hour=0,min=0,sec=0;
  17203. split_time((int)viptime_remain,&year,&month,&day,&hour,&min,&sec);
  17204. safesnprintf(vip_str,sizeof(vip_str),"%d-%d-%d %d:%d",year,month,day,hour,min);
  17205. script_pushstrcopy(st, vip_str);
  17206. } else
  17207. script_pushint(st, 0);
  17208. break;
  17209. }
  17210. #else
  17211. script_pushint(st, 0);
  17212. #endif
  17213. return SCRIPT_CMD_SUCCESS;
  17214. }
  17215. /** Adds or removes VIP time in minutes. Need to enable VIP system
  17216. * vip_time <time in mn>,{"<character name>"};
  17217. * @param time: VIP duration in minutes. If time < 0 remove time, else add time.
  17218. * @param name: Character name (optional)
  17219. */
  17220. BUILDIN_FUNC(vip_time) {
  17221. #ifdef VIP_ENABLE //would be a pain for scripting npc otherwise
  17222. TBL_PC *sd;
  17223. int viptime = script_getnum(st, 2) * 60; // Convert since it's given in minutes.
  17224. if (script_hasdata(st, 3))
  17225. sd = map_nick2sd(script_getstr(st, 3));
  17226. else
  17227. sd = script_rid2sd(st);
  17228. if (sd == NULL)
  17229. return SCRIPT_CMD_FAILURE;
  17230. chrif_req_login_operation(sd->status.account_id, sd->status.name, CHRIF_OP_LOGIN_VIP, viptime, 7, 0);
  17231. #endif
  17232. return SCRIPT_CMD_SUCCESS;
  17233. }
  17234. /** Turns a player into a monster and grants SC attribute effect. [malufett/Hercules]
  17235. * montransform <monster name/ID>, <duration>, <sc type>, <val1>, <val2>, <val3>, <val4>;
  17236. * @param monster: Monster ID or name
  17237. * @param duration: Transform duration in millisecond (ms)
  17238. * @param sc_type: Type of SC that will be affected during the transformation
  17239. * @param val1: Value for SC
  17240. * @param val2: Value for SC
  17241. * @param val3: Value for SC
  17242. * @param val4: Value for SC
  17243. */
  17244. BUILDIN_FUNC(montransform) {
  17245. TBL_PC *sd;
  17246. enum sc_type type;
  17247. int tick, mob_id, val1, val2, val3, val4;
  17248. struct script_data *data;
  17249. val1 = val2 = val3 = val4 = 0;
  17250. if( (sd = script_rid2sd(st)) == NULL )
  17251. return 1;
  17252. data = script_getdata(st, 2);
  17253. get_val(st, data); // Convert into value in case of a variable
  17254. if( data_isstring(data) )
  17255. mob_id = mobdb_searchname(script_getstr(st, 2));
  17256. else
  17257. mob_id = mobdb_checkid(script_getnum(st, 2));
  17258. tick = script_getnum(st, 3);
  17259. if (script_hasdata(st, 4))
  17260. type = (sc_type)script_getnum(st, 4);
  17261. else
  17262. type = SC_NONE;
  17263. if (mob_id == 0) {
  17264. if( data_isstring(data) )
  17265. ShowWarning("buildin_montransform: Attempted to use non-existing monster '%s'.\n", script_getstr(st, 2));
  17266. else
  17267. ShowWarning("buildin_montransform: Attempted to use non-existing monster of ID '%d'.\n", script_getnum(st, 2));
  17268. return SCRIPT_CMD_FAILURE;
  17269. }
  17270. if (mob_id == MOBID_EMPERIUM) {
  17271. ShowWarning("buildin_montransform: Monster 'Emperium' cannot be used.\n");
  17272. return SCRIPT_CMD_FAILURE;
  17273. }
  17274. if (!(type >= SC_NONE && type < SC_MAX)) {
  17275. ShowWarning("buildin_montransform: Unsupported status change id %d\n", type);
  17276. return SCRIPT_CMD_FAILURE;
  17277. }
  17278. if (script_hasdata(st, 5))
  17279. val1 = script_getnum(st, 5);
  17280. if (script_hasdata(st, 6))
  17281. val2 = script_getnum(st, 6);
  17282. if (script_hasdata(st, 7))
  17283. val3 = script_getnum(st, 7);
  17284. if (script_hasdata(st, 8))
  17285. val4 = script_getnum(st, 8);
  17286. if (tick != 0) {
  17287. if (battle_config.mon_trans_disable_in_gvg && map_flag_gvg2(sd->bl.m)) {
  17288. clif_displaymessage(sd->fd, msg_txt(sd,731)); // Transforming into monster is not allowed in Guild Wars.
  17289. return SCRIPT_CMD_FAILURE;
  17290. }
  17291. if (sd->disguise){
  17292. clif_displaymessage(sd->fd, msg_txt(sd,729)); // Cannot transform into monster while in disguise.
  17293. return SCRIPT_CMD_FAILURE;
  17294. }
  17295. status_change_end(&sd->bl, SC_MONSTER_TRANSFORM, INVALID_TIMER); // Clear previous
  17296. sc_start2(NULL, &sd->bl, SC_MONSTER_TRANSFORM, 100, mob_id, type, tick);
  17297. if (type != SC_NONE)
  17298. sc_start4(NULL, &sd->bl, type, 100, val1, val2, val3, val4, tick);
  17299. }
  17300. return SCRIPT_CMD_SUCCESS;
  17301. }
  17302. /**
  17303. * Attach script to player for certain duration
  17304. * bonus_script "<script code>",<duration>{,<flag>{,<type>{,<status_icon>{,<char_id>}}}};
  17305. * @param "script code"
  17306. * @param duration
  17307. * @param flag
  17308. * @param icon
  17309. * @param char_id
  17310. * @author [Cydh]
  17311. **/
  17312. BUILDIN_FUNC(bonus_script) {
  17313. uint16 flag = 0;
  17314. int16 icon = SI_BLANK;
  17315. uint32 dur;
  17316. uint8 type = 0;
  17317. TBL_PC* sd;
  17318. const char *script_str = NULL;
  17319. struct s_bonus_script_entry *entry = NULL;
  17320. if (script_hasdata(st,7)) {
  17321. if (!(sd = map_charid2sd(script_getnum(st,7)))) {
  17322. ShowError("buildin_bonus_script: Player CID=%d is not found.\n", script_getnum(st,7));
  17323. return SCRIPT_CMD_FAILURE;
  17324. }
  17325. }
  17326. else
  17327. sd = script_rid2sd(st);
  17328. if (sd == NULL)
  17329. return SCRIPT_CMD_FAILURE;
  17330. script_str = script_getstr(st,2);
  17331. dur = 1000 * abs(script_getnum(st,3));
  17332. FETCH(4, flag);
  17333. FETCH(5, type);
  17334. FETCH(6, icon);
  17335. // No Script string, No Duration!
  17336. if (script_str[0] == '\0' || !dur) {
  17337. ShowError("buildin_bonus_script: Invalid! Script: \"%s\". Duration: %d\n", script_str, dur);
  17338. return SCRIPT_CMD_FAILURE;
  17339. }
  17340. if (strlen(script_str) >= MAX_BONUS_SCRIPT_LENGTH) {
  17341. ShowError("buildin_bonus_script: Script string to long: \"%s\".\n", script_str);
  17342. return SCRIPT_CMD_FAILURE;
  17343. }
  17344. if (icon <= SI_BLANK || icon >= SI_MAX)
  17345. icon = SI_BLANK;
  17346. if ((entry = pc_bonus_script_add(sd, script_str, dur, (enum si_type)icon, flag, type))) {
  17347. linkdb_insert(&sd->bonus_script.head, (void *)((intptr_t)entry), entry);
  17348. status_calc_pc(sd,SCO_NONE);
  17349. }
  17350. return SCRIPT_CMD_SUCCESS;
  17351. }
  17352. /**
  17353. * Removes all bonus script from player
  17354. * bonus_script_clear {<flag>,{<char_id>}};
  17355. * @param flag 0 - Except permanent bonus, 1 - With permanent bonus
  17356. * @param char_id Clear script from this character
  17357. * @author [Cydh]
  17358. */
  17359. BUILDIN_FUNC(bonus_script_clear) {
  17360. TBL_PC* sd;
  17361. bool flag = false;
  17362. if (script_hasdata(st,2))
  17363. flag = script_getnum(st,2);
  17364. if (script_hasdata(st,3))
  17365. sd = map_charid2sd(script_getnum(st,3));
  17366. else
  17367. sd = script_rid2sd(st);
  17368. if (sd == NULL)
  17369. return SCRIPT_CMD_FAILURE;
  17370. pc_bonus_script_clear(sd,(flag ? BSF_PERMANENT : BSF_REM_ALL));
  17371. return SCRIPT_CMD_SUCCESS;
  17372. }
  17373. /** Allows player to use atcommand while talking with NPC
  17374. * enable_command;
  17375. * @author [Cydh], [Kichi]
  17376. */
  17377. BUILDIN_FUNC(enable_command) {
  17378. TBL_PC* sd = script_rid2sd(st);
  17379. if (!sd)
  17380. return SCRIPT_CMD_FAILURE;
  17381. sd->state.disable_atcommand_on_npc = 0;
  17382. return SCRIPT_CMD_SUCCESS;
  17383. }
  17384. /** Prevents player to use atcommand while talking with NPC
  17385. * disable_command;
  17386. * @author [Cydh], [Kichi]
  17387. */
  17388. BUILDIN_FUNC(disable_command) {
  17389. TBL_PC* sd = script_rid2sd(st);
  17390. if (!sd)
  17391. return SCRIPT_CMD_FAILURE;
  17392. sd->state.disable_atcommand_on_npc = 1;
  17393. return SCRIPT_CMD_SUCCESS;
  17394. }
  17395. /** Get the information of the members of a guild by type.
  17396. * getguildmember <guild_id>{,<type>};
  17397. * @param guild_id: ID of guild
  17398. * @param type: Type of option (optional)
  17399. */
  17400. BUILDIN_FUNC(getguildmember)
  17401. {
  17402. unsigned char j = 0;
  17403. struct guild *g = NULL;
  17404. g = guild_search(script_getnum(st,2));
  17405. if (g) {
  17406. unsigned char i, type = 0;
  17407. if (script_hasdata(st,3))
  17408. type = (unsigned char)script_getnum(st,3);
  17409. for (i = 0; i < MAX_GUILD; i++) {
  17410. if (g->member[i].account_id) {
  17411. switch (type) {
  17412. case 2:
  17413. mapreg_setreg(reference_uid(add_str("$@guildmemberaid"), j),g->member[i].account_id);
  17414. break;
  17415. case 1:
  17416. mapreg_setreg(reference_uid(add_str("$@guildmembercid"), j), g->member[i].char_id);
  17417. break;
  17418. default:
  17419. mapreg_setregstr(reference_uid(add_str("$@guildmembername$"), j), g->member[i].name);
  17420. break;
  17421. }
  17422. j++;
  17423. }
  17424. }
  17425. }
  17426. mapreg_setreg(add_str("$@guildmembercount"), j);
  17427. return SCRIPT_CMD_SUCCESS;
  17428. }
  17429. /** Adds spirit ball to player for 'duration' in milisecond
  17430. * addspiritball <count>,<duration>{,<char_id>};
  17431. * @param count How many spirit ball will be added
  17432. * @param duration How long spiritball is active until it disappears
  17433. * @param char_id Target player (Optional)
  17434. * @author [Cydh]
  17435. */
  17436. BUILDIN_FUNC(addspiritball) {
  17437. uint8 i, count = script_getnum(st,2);
  17438. uint16 duration = script_getnum(st,3);
  17439. struct map_session_data *sd = NULL;
  17440. if (count == 0)
  17441. return SCRIPT_CMD_SUCCESS;
  17442. if (script_hasdata(st,4)) {
  17443. if (!script_isstring(st,4))
  17444. sd = map_charid2sd(script_getnum(st,4));
  17445. else
  17446. sd = map_nick2sd(script_getstr(st,4));
  17447. }
  17448. else
  17449. sd = script_rid2sd(st);
  17450. if (!sd)
  17451. return SCRIPT_CMD_FAILURE;
  17452. for (i = 0; i < count; i++)
  17453. pc_addspiritball(sd,duration,10);
  17454. return SCRIPT_CMD_SUCCESS;
  17455. }
  17456. /** Deletes the spirit ball(s) from player
  17457. * delspiritball <count>{,<char_id>};
  17458. * @param count How many spirit ball will be deleted
  17459. * @param char_id Target player (Optional)
  17460. * @author [Cydh]
  17461. */
  17462. BUILDIN_FUNC(delspiritball) {
  17463. uint8 count = script_getnum(st,2);
  17464. struct map_session_data *sd = NULL;
  17465. if (count == 0)
  17466. count = 1;
  17467. if (script_hasdata(st,3)) {
  17468. if (!script_isstring(st,3))
  17469. sd = map_charid2sd(script_getnum(st,3));
  17470. else
  17471. sd = map_nick2sd(script_getstr(st,3));
  17472. }
  17473. else
  17474. sd = script_rid2sd(st);
  17475. if (!sd)
  17476. return SCRIPT_CMD_FAILURE;
  17477. pc_delspiritball(sd,count,0);
  17478. return SCRIPT_CMD_SUCCESS;
  17479. }
  17480. /** Counts the spirit ball that player has
  17481. * countspiritball {<char_id>};
  17482. * @param char_id Target player (Optional)
  17483. * @author [Cydh]
  17484. */
  17485. BUILDIN_FUNC(countspiritball) {
  17486. struct map_session_data *sd;
  17487. if (script_hasdata(st,2)) {
  17488. if (!script_isstring(st,2))
  17489. sd = map_charid2sd(script_getnum(st,2));
  17490. else
  17491. sd = map_nick2sd(script_getstr(st,2));
  17492. }
  17493. else
  17494. sd = script_rid2sd(st);
  17495. if (!sd)
  17496. return SCRIPT_CMD_FAILURE;
  17497. script_pushint(st,sd->spiritball);
  17498. return SCRIPT_CMD_SUCCESS;
  17499. }
  17500. /** Merges separated stackable items because of guid
  17501. * mergeitem {<item_id>,{<char_id>}};
  17502. * mergeitem {"<item name>",{<char_id>}};
  17503. * @param item Item ID/Name for merging specific item (Optional)
  17504. * @author [Cydh]
  17505. */
  17506. BUILDIN_FUNC(mergeitem) {
  17507. struct map_session_data *sd;
  17508. struct item *items = NULL;
  17509. uint16 i, count = 0;
  17510. int nameid = 0;
  17511. if (!script_charid2sd(3, sd))
  17512. return SCRIPT_CMD_FAILURE;
  17513. if (script_hasdata(st, 2)) {
  17514. struct script_data *data = script_getdata(st, 2);
  17515. struct item_data *id;
  17516. get_val(st, data);
  17517. if (data_isstring(data)) {// "<item name>"
  17518. const char *name = conv_str(st,data);
  17519. if (!(id = itemdb_searchname(name))) {
  17520. ShowError("buildin_mergeitem: Nonexistant item %s requested.\n", name);
  17521. script_pushint(st, count);
  17522. return SCRIPT_CMD_FAILURE;
  17523. }
  17524. nameid = id->nameid;
  17525. }
  17526. else if (data_isint(data)) {// <item id>
  17527. nameid = conv_num(st,data);
  17528. if (!(id = itemdb_exists(nameid))) {
  17529. ShowError("buildin_mergeitem: Nonexistant item %d requested.\n", nameid);
  17530. script_pushint(st, count);
  17531. return SCRIPT_CMD_FAILURE;
  17532. }
  17533. }
  17534. }
  17535. for (i = 0; i < MAX_INVENTORY; i++) {
  17536. struct item *it = &sd->status.inventory[i];
  17537. if (!it || !it->unique_id || it->expire_time || !itemdb_isstackable(it->nameid))
  17538. continue;
  17539. if ((!nameid || (nameid == it->nameid))) {
  17540. uint8 k;
  17541. if (!count) {
  17542. CREATE(items, struct item, 1);
  17543. memcpy(&items[count++], it, sizeof(struct item));
  17544. pc_delitem(sd, i, it->amount, 0, 0, LOG_TYPE_NPC);
  17545. continue;
  17546. }
  17547. for (k = 0; k < count; k++) {
  17548. // Find Match
  17549. if (&items[k] && items[k].nameid == it->nameid && items[k].bound == it->bound && memcmp(items[k].card, it->card, sizeof(it->card)) == 0) {
  17550. items[k].amount += it->amount;
  17551. pc_delitem(sd, i, it->amount, 0, 0, LOG_TYPE_NPC);
  17552. break;
  17553. }
  17554. }
  17555. if (k >= count) {
  17556. // New entry
  17557. RECREATE(items, struct item, count+1);
  17558. memcpy(&items[count++], it, sizeof(struct item));
  17559. pc_delitem(sd, i, it->amount, 0, 0, LOG_TYPE_NPC);
  17560. }
  17561. }
  17562. }
  17563. if (!items) // Nothing todo here
  17564. return SCRIPT_CMD_SUCCESS;
  17565. // Retrieve the items
  17566. for (i = 0; i < count; i++) {
  17567. uint8 flag = 0;
  17568. if (!&items[i])
  17569. continue;
  17570. items[i].id = 0;
  17571. items[i].unique_id = 0;
  17572. if ((flag = pc_additem(sd, &items[i], items[i].amount, LOG_TYPE_NPC)))
  17573. clif_additem(sd, i, items[i].amount, flag);
  17574. }
  17575. aFree(items);
  17576. script_pushint(st, count);
  17577. return SCRIPT_CMD_SUCCESS;
  17578. }
  17579. /**
  17580. * Update an entry from NPC shop.
  17581. * npcshopupdate "<name>",<item_id>,<price>{,<stock>}
  17582. **/
  17583. BUILDIN_FUNC(npcshopupdate) {
  17584. const char* npcname = script_getstr(st, 2);
  17585. struct npc_data* nd = npc_name2id(npcname);
  17586. uint16 nameid = script_getnum(st, 3);
  17587. int price = script_getnum(st, 4);
  17588. #if PACKETVER >= 20131223
  17589. uint16 stock = script_hasdata(st,5) ? script_getnum(st,5) : 0;
  17590. #endif
  17591. int i;
  17592. if( !nd || ( nd->subtype != NPCTYPE_SHOP && nd->subtype != NPCTYPE_CASHSHOP && nd->subtype != NPCTYPE_ITEMSHOP && nd->subtype != NPCTYPE_POINTSHOP && nd->subtype != NPCTYPE_MARKETSHOP ) ) { // Not found.
  17593. script_pushint(st,0);
  17594. return SCRIPT_CMD_FAILURE;
  17595. }
  17596. if (!nd->u.shop.count) {
  17597. ShowError("buildin_npcshopupdate: Attempt to update empty shop from '%s'.\n", nd->exname);
  17598. script_pushint(st,0);
  17599. return SCRIPT_CMD_FAILURE;
  17600. }
  17601. for (i = 0; i < nd->u.shop.count; i++) {
  17602. if (nd->u.shop.shop_item[i].nameid == nameid) {
  17603. if (price != 0)
  17604. nd->u.shop.shop_item[i].value = price;
  17605. #if PACKETVER >= 20131223
  17606. if (nd->subtype == NPCTYPE_MARKETSHOP) {
  17607. nd->u.shop.shop_item[i].qty = stock;
  17608. npc_market_tosql(nd->exname, &nd->u.shop.shop_item[i]);
  17609. }
  17610. #endif
  17611. }
  17612. }
  17613. script_pushint(st,1);
  17614. return SCRIPT_CMD_SUCCESS;
  17615. }
  17616. /**
  17617. * Get rid from running script.
  17618. * getattachedrid();
  17619. **/
  17620. BUILDIN_FUNC(getattachedrid) {
  17621. script_pushint(st, st->rid);
  17622. return SCRIPT_CMD_SUCCESS;
  17623. }
  17624. /**
  17625. * Get variable from a Player
  17626. * getvar <variable>,<char_id>;
  17627. */
  17628. BUILDIN_FUNC(getvar) {
  17629. int char_id = script_getnum(st, 3);
  17630. struct map_session_data *sd = map_charid2sd(char_id);
  17631. struct script_data *data = NULL;
  17632. const char *name = NULL;
  17633. if (!sd) {
  17634. ShowError("buildin_getvar: No player found with char id '%d'.\n", char_id);
  17635. return SCRIPT_CMD_FAILURE;
  17636. }
  17637. data = script_getdata(st, 2);
  17638. if (!data_isreference(data)) {
  17639. ShowError("buildin_getvar: Not a variable\n");
  17640. script_reportdata(data);
  17641. script_pushnil(st);
  17642. st->state = END;
  17643. return SCRIPT_CMD_FAILURE;
  17644. }
  17645. name = reference_getname(data);
  17646. if (name[0] == '.' || name[0] == '$' || name[0] == '\'') { // Not a PC variable
  17647. ShowError("buildin_getvar: Invalid scope (not PC variable)\n");
  17648. script_reportdata(data);
  17649. script_pushnil(st);
  17650. st->state = END;
  17651. return SCRIPT_CMD_FAILURE;
  17652. }
  17653. get_val_(st, data, sd);
  17654. if (data_isint(data))
  17655. script_pushint(st, conv_num_(st, data, sd));
  17656. else
  17657. script_pushstrcopy(st, conv_str_(st, data, sd));
  17658. push_val2(st->stack, C_NAME, reference_getuid(data), reference_getref(data));
  17659. return SCRIPT_CMD_SUCCESS;
  17660. }
  17661. /**
  17662. * Display script message
  17663. * showscript "<message>"{,<GID>};
  17664. **/
  17665. BUILDIN_FUNC(showscript) {
  17666. struct block_list *bl = NULL;
  17667. const char *msg = script_getstr(st,2);
  17668. int id = 0;
  17669. if (script_hasdata(st,3)) {
  17670. id = script_getnum(st,3);
  17671. bl = map_id2bl(id);
  17672. }
  17673. else {
  17674. bl = st->rid ? map_id2bl(st->rid) : map_id2bl(st->oid);
  17675. }
  17676. if (!bl) {
  17677. ShowError("buildin_showscript: Script not attached. (id=%, rid=%d, oid=%d)\n", id, st->rid, st->oid);
  17678. script_pushint(st,0);
  17679. return SCRIPT_CMD_FAILURE;
  17680. }
  17681. clif_showscript(bl, msg);
  17682. script_pushint(st,1);
  17683. return SCRIPT_CMD_SUCCESS;
  17684. }
  17685. #include "../custom/script.inc"
  17686. // declarations that were supposed to be exported from npc_chat.c
  17687. #ifdef PCRE_SUPPORT
  17688. BUILDIN_FUNC(defpattern);
  17689. BUILDIN_FUNC(activatepset);
  17690. BUILDIN_FUNC(deactivatepset);
  17691. BUILDIN_FUNC(deletepset);
  17692. #endif
  17693. /** Regular expression matching
  17694. * preg_match(<pattern>,<string>{,<offset>})
  17695. */
  17696. BUILDIN_FUNC(preg_match) {
  17697. #ifdef PCRE_SUPPORT
  17698. pcre *re;
  17699. pcre_extra *pcreExtra;
  17700. const char *error;
  17701. int erroffset, r, offset = 0;
  17702. int subStrVec[30];
  17703. const char* pattern = script_getstr(st,2);
  17704. const char* subject = script_getstr(st,3);
  17705. if (script_hasdata(st,4))
  17706. offset = script_getnum(st,4);
  17707. re = pcre_compile(pattern, 0, &error, &erroffset, NULL);
  17708. pcreExtra = pcre_study(re, 0, &error);
  17709. r = pcre_exec(re, pcreExtra, subject, (int)strlen(subject), offset, 0, subStrVec, 30);
  17710. pcre_free(re);
  17711. if (pcreExtra != NULL)
  17712. pcre_free(pcreExtra);
  17713. if (r < 0)
  17714. script_pushint(st,0);
  17715. else
  17716. script_pushint(st,(r > 0) ? r : 30 / 3);
  17717. return SCRIPT_CMD_SUCCESS;
  17718. #else
  17719. ShowDebug("script:preg_match: cannot run without PCRE library enabled.\n");
  17720. script_pushint(st,0);
  17721. return SCRIPT_CMD_SUCCESS;
  17722. #endif
  17723. }
  17724. /// script command definitions
  17725. /// for an explanation on args, see add_buildin_func
  17726. struct script_function buildin_func[] = {
  17727. // NPC interaction
  17728. BUILDIN_DEF(mes,"s*"),
  17729. BUILDIN_DEF(next,""),
  17730. BUILDIN_DEF(close,""),
  17731. BUILDIN_DEF(close2,""),
  17732. BUILDIN_DEF(menu,"sl*"),
  17733. BUILDIN_DEF(select,"s*"), //for future jA script compatibility
  17734. BUILDIN_DEF(prompt,"s*"),
  17735. //
  17736. BUILDIN_DEF(goto,"l"),
  17737. BUILDIN_DEF(callsub,"l*"),
  17738. BUILDIN_DEF(callfunc,"s*"),
  17739. BUILDIN_DEF(return,"?"),
  17740. BUILDIN_DEF(getarg,"i?"),
  17741. BUILDIN_DEF(jobchange,"i?"),
  17742. BUILDIN_DEF(jobname,"i"),
  17743. BUILDIN_DEF(input,"r??"),
  17744. BUILDIN_DEF(warp,"sii"),
  17745. BUILDIN_DEF(areawarp,"siiiisii??"),
  17746. BUILDIN_DEF(warpchar,"siii"), // [LuzZza]
  17747. BUILDIN_DEF(warpparty,"siii?"), // [Fredzilla] [Paradox924X]
  17748. BUILDIN_DEF(warpguild,"siii"), // [Fredzilla]
  17749. BUILDIN_DEF(setlook,"ii"),
  17750. BUILDIN_DEF(changelook,"ii"), // Simulates but don't Store it
  17751. BUILDIN_DEF(set,"rv?"),
  17752. BUILDIN_DEF(setarray,"rv*"),
  17753. BUILDIN_DEF(cleararray,"rvi"),
  17754. BUILDIN_DEF(copyarray,"rri"),
  17755. BUILDIN_DEF(getarraysize,"r"),
  17756. BUILDIN_DEF(deletearray,"r?"),
  17757. BUILDIN_DEF(getelementofarray,"ri"),
  17758. BUILDIN_DEF(getitem,"vi?"),
  17759. BUILDIN_DEF(rentitem,"vi?"),
  17760. BUILDIN_DEF(rentitem2,"viiiiiiii?"),
  17761. BUILDIN_DEF(getitem2,"viiiiiiii?"),
  17762. BUILDIN_DEF(getnameditem,"vv"),
  17763. BUILDIN_DEF2(grouprandomitem,"groupranditem","i?"),
  17764. BUILDIN_DEF(makeitem,"visii"),
  17765. BUILDIN_DEF(makeitem2,"visiiiiiiiii"),
  17766. BUILDIN_DEF(delitem,"vi?"),
  17767. BUILDIN_DEF2(delitem,"storagedelitem","vi?"),
  17768. BUILDIN_DEF2(delitem,"cartdelitem","vi?"),
  17769. BUILDIN_DEF(delitem2,"viiiiiiii?"),
  17770. BUILDIN_DEF2(delitem2,"storagedelitem2","viiiiiiii?"),
  17771. BUILDIN_DEF2(delitem2,"cartdelitem2","viiiiiiii?"),
  17772. BUILDIN_DEF2(enableitemuse,"enable_items",""),
  17773. BUILDIN_DEF2(disableitemuse,"disable_items",""),
  17774. BUILDIN_DEF(cutin,"si"),
  17775. BUILDIN_DEF(viewpoint,"iiiii"),
  17776. BUILDIN_DEF(heal,"ii"),
  17777. BUILDIN_DEF(itemheal,"ii"),
  17778. BUILDIN_DEF(percentheal,"ii"),
  17779. BUILDIN_DEF(rand,"i?"),
  17780. BUILDIN_DEF(countitem,"v?"),
  17781. BUILDIN_DEF2(countitem,"storagecountitem","v?"),
  17782. BUILDIN_DEF2(countitem,"cartcountitem","v?"),
  17783. BUILDIN_DEF2(countitem,"countitem2","viiiiiii?"),
  17784. BUILDIN_DEF2(countitem,"storagecountitem2","viiiiiii?"),
  17785. BUILDIN_DEF2(countitem,"cartcountitem2","viiiiiii?"),
  17786. BUILDIN_DEF(checkweight,"vi*"),
  17787. BUILDIN_DEF(checkweight2,"rr"),
  17788. BUILDIN_DEF(readparam,"i?"),
  17789. BUILDIN_DEF(getcharid,"i?"),
  17790. BUILDIN_DEF(getnpcid,"i?"),
  17791. BUILDIN_DEF(getpartyname,"i"),
  17792. BUILDIN_DEF(getpartymember,"i?"),
  17793. BUILDIN_DEF(getpartyleader,"i?"),
  17794. BUILDIN_DEF(getguildname,"i"),
  17795. BUILDIN_DEF(getguildmaster,"i"),
  17796. BUILDIN_DEF(getguildmasterid,"i"),
  17797. BUILDIN_DEF(strcharinfo,"i?"),
  17798. BUILDIN_DEF(strnpcinfo,"i"),
  17799. BUILDIN_DEF(getequipid,"i?"),
  17800. BUILDIN_DEF(getequipuniqueid,"i?"),
  17801. BUILDIN_DEF(getequipname,"i?"),
  17802. BUILDIN_DEF(getbrokenid,"i?"), // [Valaris]
  17803. BUILDIN_DEF(repair,"i?"), // [Valaris]
  17804. BUILDIN_DEF(repairall,"?"),
  17805. BUILDIN_DEF(getequipisequiped,"i?"),
  17806. BUILDIN_DEF(getequipisenableref,"i?"),
  17807. BUILDIN_DEF(getequiprefinerycnt,"i?"),
  17808. BUILDIN_DEF(getequipweaponlv,"i?"),
  17809. BUILDIN_DEF(getequippercentrefinery,"i?"),
  17810. BUILDIN_DEF(successrefitem,"i??"),
  17811. BUILDIN_DEF(failedrefitem,"i?"),
  17812. BUILDIN_DEF(downrefitem,"i??"),
  17813. BUILDIN_DEF(statusup,"i?"),
  17814. BUILDIN_DEF(statusup2,"ii?"),
  17815. BUILDIN_DEF(bonus,"iv"),
  17816. BUILDIN_DEF2(bonus,"bonus2","ivi"),
  17817. BUILDIN_DEF2(bonus,"bonus3","ivii"),
  17818. BUILDIN_DEF2(bonus,"bonus4","ivvii"),
  17819. BUILDIN_DEF2(bonus,"bonus5","ivviii"),
  17820. BUILDIN_DEF(autobonus,"sii??"),
  17821. BUILDIN_DEF(autobonus2,"sii??"),
  17822. BUILDIN_DEF(autobonus3,"siiv?"),
  17823. BUILDIN_DEF(skill,"vi?"),
  17824. BUILDIN_DEF2(skill,"addtoskill","vi?"), // [Valaris]
  17825. BUILDIN_DEF(guildskill,"vi"),
  17826. BUILDIN_DEF(getskilllv,"v"),
  17827. BUILDIN_DEF(getgdskilllv,"iv"),
  17828. BUILDIN_DEF(basicskillcheck,""),
  17829. BUILDIN_DEF(getgmlevel,"?"),
  17830. BUILDIN_DEF(getgroupid,"?"),
  17831. BUILDIN_DEF(end,""),
  17832. BUILDIN_DEF(checkoption,"i?"),
  17833. BUILDIN_DEF(setoption,"i??"),
  17834. BUILDIN_DEF(setcart,"??"),
  17835. BUILDIN_DEF(checkcart,"?"),
  17836. BUILDIN_DEF(setfalcon,"??"),
  17837. BUILDIN_DEF(checkfalcon,"?"),
  17838. BUILDIN_DEF(setriding,"??"),
  17839. BUILDIN_DEF(checkriding,"?"),
  17840. BUILDIN_DEF(checkwug,"?"),
  17841. BUILDIN_DEF(checkmadogear,"?"),
  17842. BUILDIN_DEF(setmadogear,"??"),
  17843. BUILDIN_DEF2(savepoint,"save","sii?"),
  17844. BUILDIN_DEF(savepoint,"sii?"),
  17845. BUILDIN_DEF(gettimetick,"i"),
  17846. BUILDIN_DEF(gettime,"i"),
  17847. BUILDIN_DEF(gettimestr,"si"),
  17848. BUILDIN_DEF(openstorage,""),
  17849. BUILDIN_DEF(guildopenstorage,""),
  17850. BUILDIN_DEF(itemskill,"vi"),
  17851. BUILDIN_DEF(produce,"i"),
  17852. BUILDIN_DEF(cooking,"i"),
  17853. BUILDIN_DEF(monster,"siisii???"),
  17854. BUILDIN_DEF(getmobdrops,"i"),
  17855. BUILDIN_DEF(areamonster,"siiiisii???"),
  17856. BUILDIN_DEF(killmonster,"ss?"),
  17857. BUILDIN_DEF(killmonsterall,"s?"),
  17858. BUILDIN_DEF(clone,"siisi????"),
  17859. BUILDIN_DEF(doevent,"s"),
  17860. BUILDIN_DEF(donpcevent,"s"),
  17861. BUILDIN_DEF(cmdothernpc,"ss"),
  17862. BUILDIN_DEF(addtimer,"is"),
  17863. BUILDIN_DEF(deltimer,"s"),
  17864. BUILDIN_DEF(addtimercount,"is"),
  17865. BUILDIN_DEF(initnpctimer,"??"),
  17866. BUILDIN_DEF(stopnpctimer,"??"),
  17867. BUILDIN_DEF(startnpctimer,"??"),
  17868. BUILDIN_DEF(setnpctimer,"i?"),
  17869. BUILDIN_DEF(getnpctimer,"i?"),
  17870. BUILDIN_DEF(attachnpctimer,"?"), // attached the player id to the npc timer [Celest]
  17871. BUILDIN_DEF(detachnpctimer,"?"), // detached the player id from the npc timer [Celest]
  17872. BUILDIN_DEF(playerattached,""), // returns id of the current attached player. [Skotlex]
  17873. BUILDIN_DEF(announce,"si?????"),
  17874. BUILDIN_DEF(mapannounce,"ssi?????"),
  17875. BUILDIN_DEF(areaannounce,"siiiisi?????"),
  17876. BUILDIN_DEF(getusers,"i"),
  17877. BUILDIN_DEF(getmapguildusers,"si"),
  17878. BUILDIN_DEF(getmapusers,"s"),
  17879. BUILDIN_DEF(getareausers,"siiii"),
  17880. BUILDIN_DEF(getareadropitem,"siiiiv"),
  17881. BUILDIN_DEF(enablenpc,"s"),
  17882. BUILDIN_DEF(disablenpc,"s"),
  17883. BUILDIN_DEF(hideoffnpc,"s"),
  17884. BUILDIN_DEF(hideonnpc,"s"),
  17885. BUILDIN_DEF(sc_start,"iii???"),
  17886. BUILDIN_DEF2(sc_start,"sc_start2","iiii???"),
  17887. BUILDIN_DEF2(sc_start,"sc_start4","iiiiii???"),
  17888. BUILDIN_DEF(sc_end,"i?"),
  17889. BUILDIN_DEF(getstatus, "i??"),
  17890. BUILDIN_DEF(getscrate,"ii?"),
  17891. BUILDIN_DEF(debugmes,"s"),
  17892. BUILDIN_DEF2(catchpet,"pet","i"),
  17893. BUILDIN_DEF2(birthpet,"bpet",""),
  17894. BUILDIN_DEF(resetlvl,"i?"),
  17895. BUILDIN_DEF(resetstatus,"?"),
  17896. BUILDIN_DEF(resetskill,"?"),
  17897. BUILDIN_DEF(skillpointcount,"?"),
  17898. BUILDIN_DEF(changebase,"i?"),
  17899. BUILDIN_DEF(changesex,"?"),
  17900. BUILDIN_DEF(changecharsex,"?"),
  17901. BUILDIN_DEF(waitingroom,"si?????"),
  17902. BUILDIN_DEF(delwaitingroom,"?"),
  17903. BUILDIN_DEF2(waitingroomkickall,"kickwaitingroomall","?"),
  17904. BUILDIN_DEF(enablewaitingroomevent,"?"),
  17905. BUILDIN_DEF(disablewaitingroomevent,"?"),
  17906. BUILDIN_DEF2(enablewaitingroomevent,"enablearena",""), // Added by RoVeRT
  17907. BUILDIN_DEF2(disablewaitingroomevent,"disablearena",""), // Added by RoVeRT
  17908. BUILDIN_DEF(getwaitingroomstate,"i?"),
  17909. BUILDIN_DEF(warpwaitingpc,"sii?"),
  17910. BUILDIN_DEF(attachrid,"i"),
  17911. BUILDIN_DEF(addrid,"i?????"),
  17912. BUILDIN_DEF(detachrid,""),
  17913. BUILDIN_DEF(isloggedin,"i?"),
  17914. BUILDIN_DEF(setmapflagnosave,"ssii"),
  17915. BUILDIN_DEF(getmapflag,"si?"),
  17916. BUILDIN_DEF(setmapflag,"si??"),
  17917. BUILDIN_DEF(removemapflag,"si?"),
  17918. BUILDIN_DEF(pvpon,"s"),
  17919. BUILDIN_DEF(pvpoff,"s"),
  17920. BUILDIN_DEF(gvgon,"s"),
  17921. BUILDIN_DEF(gvgoff,"s"),
  17922. BUILDIN_DEF(emotion,"i??"),
  17923. BUILDIN_DEF(maprespawnguildid,"sii"),
  17924. BUILDIN_DEF(agitstart,""), // <Agit>
  17925. BUILDIN_DEF(agitend,""),
  17926. BUILDIN_DEF(agitcheck,""), // <Agitcheck>
  17927. BUILDIN_DEF(flagemblem,"i"), // Flag Emblem
  17928. BUILDIN_DEF(getcastlename,"s"),
  17929. BUILDIN_DEF(getcastledata,"si"),
  17930. BUILDIN_DEF(setcastledata,"sii"),
  17931. BUILDIN_DEF(requestguildinfo,"i?"),
  17932. BUILDIN_DEF(getequipcardcnt,"i"),
  17933. BUILDIN_DEF(successremovecards,"i"),
  17934. BUILDIN_DEF(failedremovecards,"ii"),
  17935. BUILDIN_DEF(marriage,"s"),
  17936. BUILDIN_DEF2(wedding_effect,"wedding",""),
  17937. BUILDIN_DEF(divorce,"?"),
  17938. BUILDIN_DEF(ispartneron,"?"),
  17939. BUILDIN_DEF(getpartnerid,"?"),
  17940. BUILDIN_DEF(getchildid,"?"),
  17941. BUILDIN_DEF(getmotherid,"?"),
  17942. BUILDIN_DEF(getfatherid,"?"),
  17943. BUILDIN_DEF(warppartner,"sii"),
  17944. BUILDIN_DEF(getitemname,"v"),
  17945. BUILDIN_DEF(getitemslots,"i"),
  17946. BUILDIN_DEF(makepet,"i"),
  17947. BUILDIN_DEF(getexp,"ii?"),
  17948. BUILDIN_DEF(getinventorylist,"?"),
  17949. BUILDIN_DEF(getskilllist,"?"),
  17950. BUILDIN_DEF(clearitem,"?"),
  17951. BUILDIN_DEF(classchange,"ii"),
  17952. BUILDIN_DEF(misceffect,"i"),
  17953. BUILDIN_DEF(playBGM,"s"),
  17954. BUILDIN_DEF(playBGMall,"s?????"),
  17955. BUILDIN_DEF(soundeffect,"si"),
  17956. BUILDIN_DEF(soundeffectall,"si?????"), // SoundEffectAll [Codemaster]
  17957. BUILDIN_DEF(strmobinfo,"ii"), // display mob data [Valaris]
  17958. BUILDIN_DEF(guardian,"siisi??"), // summon guardians
  17959. BUILDIN_DEF(guardianinfo,"sii"), // display guardian data [Valaris]
  17960. BUILDIN_DEF(petskillbonus,"iiii"), // [Valaris]
  17961. BUILDIN_DEF(petrecovery,"ii"), // [Valaris]
  17962. BUILDIN_DEF(petloot,"i"), // [Valaris]
  17963. BUILDIN_DEF(petheal,"iiii"), // [Valaris]
  17964. BUILDIN_DEF(petskillattack,"viii"), // [Skotlex]
  17965. BUILDIN_DEF(petskillattack2,"viiii"), // [Valaris]
  17966. BUILDIN_DEF(petskillsupport,"viiii"), // [Skotlex]
  17967. BUILDIN_DEF(skilleffect,"vi"), // skill effect [Celest]
  17968. BUILDIN_DEF(npcskilleffect,"viii"), // npc skill effect [Valaris]
  17969. BUILDIN_DEF(specialeffect,"i??"), // npc skill effect [Valaris]
  17970. BUILDIN_DEF(specialeffect2,"i??"), // skill effect on players[Valaris]
  17971. BUILDIN_DEF(nude,"?"), // nude command [Valaris]
  17972. BUILDIN_DEF(mapwarp,"ssii??"), // Added by RoVeRT
  17973. BUILDIN_DEF(atcommand,"s"), // [MouseJstr]
  17974. BUILDIN_DEF2(atcommand,"charcommand","s"), // [MouseJstr]
  17975. BUILDIN_DEF(movenpc,"sii?"), // [MouseJstr]
  17976. BUILDIN_DEF(message,"ss"), // [MouseJstr]
  17977. BUILDIN_DEF(npctalk,"s"), // [Valaris]
  17978. BUILDIN_DEF(mobcount,"ss"),
  17979. BUILDIN_DEF(getlook,"i?"),
  17980. BUILDIN_DEF(getsavepoint,"i?"),
  17981. BUILDIN_DEF(npcspeed,"i"), // [Valaris]
  17982. BUILDIN_DEF(npcwalkto,"ii"), // [Valaris]
  17983. BUILDIN_DEF(npcstop,""), // [Valaris]
  17984. BUILDIN_DEF(getmapxy,"rrri?"), //by Lorky [Lupus]
  17985. BUILDIN_DEF(mapid2name,"i"),
  17986. BUILDIN_DEF(checkoption1,"i?"),
  17987. BUILDIN_DEF(checkoption2,"i?"),
  17988. BUILDIN_DEF(guildgetexp,"i"),
  17989. BUILDIN_DEF(guildchangegm,"is"),
  17990. BUILDIN_DEF(logmes,"s"), //this command actls as MES but rints info into LOG file either SQL/TXT [Lupus]
  17991. BUILDIN_DEF(summon,"si??"), // summons a slave monster [Celest]
  17992. BUILDIN_DEF(isnight,""), // check whether it is night time [Celest]
  17993. BUILDIN_DEF(isday,""), // check whether it is day time [Celest]
  17994. BUILDIN_DEF(isequipped,"i*"), // check whether another item/card has been equipped [Celest]
  17995. BUILDIN_DEF(isequippedcnt,"i*"), // check how many items/cards are being equipped [Celest]
  17996. BUILDIN_DEF(cardscnt,"i*"), // check how many items/cards are being equipped in the same arm [Lupus]
  17997. BUILDIN_DEF(getrefine,""), // returns the refined number of the current item, or an item with index specified [celest]
  17998. BUILDIN_DEF(night,""), // sets the server to night time
  17999. BUILDIN_DEF(day,""), // sets the server to day time
  18000. #ifdef PCRE_SUPPORT
  18001. BUILDIN_DEF(defpattern,"iss"), // Define pattern to listen for [MouseJstr]
  18002. BUILDIN_DEF(activatepset,"i"), // Activate a pattern set [MouseJstr]
  18003. BUILDIN_DEF(deactivatepset,"i"), // Deactive a pattern set [MouseJstr]
  18004. BUILDIN_DEF(deletepset,"i"), // Delete a pattern set [MouseJstr]
  18005. #endif
  18006. BUILDIN_DEF(preg_match,"ss?"),
  18007. BUILDIN_DEF(dispbottom,"s??"), //added from jA [Lupus]
  18008. BUILDIN_DEF(recovery,"i???"),
  18009. BUILDIN_DEF(getpetinfo,"i"),
  18010. BUILDIN_DEF(gethominfo,"i"),
  18011. BUILDIN_DEF(getmercinfo,"i?"),
  18012. BUILDIN_DEF(checkequipedcard,"i"),
  18013. BUILDIN_DEF(jump_zero,"il"), //for future jA script compatibility
  18014. BUILDIN_DEF(globalmes,"s?"), //end jA addition
  18015. BUILDIN_DEF(unequip,"i?"), // unequip command [Spectre]
  18016. BUILDIN_DEF(getstrlen,"s"), //strlen [Valaris]
  18017. BUILDIN_DEF(charisalpha,"si"), //isalpha [Valaris]
  18018. BUILDIN_DEF(charat,"si"),
  18019. BUILDIN_DEF(setchar,"ssi"),
  18020. BUILDIN_DEF(insertchar,"ssi"),
  18021. BUILDIN_DEF(delchar,"si"),
  18022. BUILDIN_DEF(strtoupper,"s"),
  18023. BUILDIN_DEF(strtolower,"s"),
  18024. BUILDIN_DEF(charisupper, "si"),
  18025. BUILDIN_DEF(charislower, "si"),
  18026. BUILDIN_DEF(substr,"sii"),
  18027. BUILDIN_DEF(explode, "rss"),
  18028. BUILDIN_DEF(implode, "r?"),
  18029. BUILDIN_DEF(sprintf,"s*"), // [Mirei]
  18030. BUILDIN_DEF(sscanf,"ss*"), // [Mirei]
  18031. BUILDIN_DEF(strpos,"ss?"),
  18032. BUILDIN_DEF(replacestr,"sss??"),
  18033. BUILDIN_DEF(countstr,"ss?"),
  18034. BUILDIN_DEF(setnpcdisplay,"sv??"),
  18035. BUILDIN_DEF(compare,"ss"), // Lordalfa - To bring strstr to scripting Engine.
  18036. BUILDIN_DEF(strcmp,"ss"),
  18037. BUILDIN_DEF(getiteminfo,"ii"), //[Lupus] returns Items Buy / sell Price, etc info
  18038. BUILDIN_DEF(setiteminfo,"iii"), //[Lupus] set Items Buy / sell Price, etc info
  18039. BUILDIN_DEF(getequipcardid,"ii"), //[Lupus] returns CARD ID or other info from CARD slot N of equipped item
  18040. // [zBuffer] List of mathematics commands --->
  18041. BUILDIN_DEF(sqrt,"i"),
  18042. BUILDIN_DEF(pow,"ii"),
  18043. BUILDIN_DEF(distance,"iiii"),
  18044. // <--- [zBuffer] List of mathematics commands
  18045. BUILDIN_DEF(md5,"s"),
  18046. // [zBuffer] List of dynamic var commands --->
  18047. BUILDIN_DEF(getd,"s"),
  18048. BUILDIN_DEF(setd,"sv?"),
  18049. BUILDIN_DEF(callshop,"s?"), // [Skotlex]
  18050. BUILDIN_DEF(npcshopitem,"sii*"), // [Lance]
  18051. BUILDIN_DEF(npcshopadditem,"sii*"),
  18052. BUILDIN_DEF(npcshopdelitem,"si*"),
  18053. BUILDIN_DEF(npcshopattach,"s?"),
  18054. BUILDIN_DEF(equip,"i?"),
  18055. BUILDIN_DEF(autoequip,"ii"),
  18056. BUILDIN_DEF(setbattleflag,"si"),
  18057. BUILDIN_DEF(getbattleflag,"s"),
  18058. BUILDIN_DEF(setitemscript,"is?"), //Set NEW item bonus script. Lupus
  18059. BUILDIN_DEF(disguise,"i?"), //disguise player. Lupus
  18060. BUILDIN_DEF(undisguise,"?"), //undisguise player. Lupus
  18061. BUILDIN_DEF(getmonsterinfo,"ii"), //Lupus
  18062. BUILDIN_DEF(addmonsterdrop,"vii"), //Akinari [Lupus]
  18063. BUILDIN_DEF(delmonsterdrop,"vi"), //Akinari [Lupus]
  18064. BUILDIN_DEF(axtoi,"s"),
  18065. BUILDIN_DEF(query_sql,"s*"),
  18066. BUILDIN_DEF(query_logsql,"s*"),
  18067. BUILDIN_DEF(escape_sql,"v"),
  18068. BUILDIN_DEF(atoi,"s"),
  18069. BUILDIN_DEF(strtol,"si"),
  18070. // [zBuffer] List of player cont commands --->
  18071. BUILDIN_DEF(rid2name,"i"),
  18072. BUILDIN_DEF(pcfollow,"ii"),
  18073. BUILDIN_DEF(pcstopfollow,"i"),
  18074. BUILDIN_DEF(pcblockmove,"ii"),
  18075. // <--- [zBuffer] List of player cont commands
  18076. // [zBuffer] List of mob control commands --->
  18077. BUILDIN_DEF(getunittype,"i"),
  18078. BUILDIN_DEF(getunitname,"i"),
  18079. BUILDIN_DEF(setunitname,"is"),
  18080. BUILDIN_DEF(getunitdata,"i*"),
  18081. BUILDIN_DEF(setunitdata,"iii"),
  18082. BUILDIN_DEF(unitwalk,"ii??"),
  18083. BUILDIN_DEF2(unitwalk,"unitwalkto","ii?"),
  18084. BUILDIN_DEF(unitkill,"i"),
  18085. BUILDIN_DEF(unitwarp,"isii"),
  18086. BUILDIN_DEF(unitattack,"iv?"),
  18087. BUILDIN_DEF(unitstopattack,"i"),
  18088. BUILDIN_DEF(unitstopwalk,"i"),
  18089. BUILDIN_DEF(unittalk,"is"),
  18090. BUILDIN_DEF(unitemote,"ii"),
  18091. BUILDIN_DEF(unitskilluseid,"ivi??"), // originally by Qamera [Celest]
  18092. BUILDIN_DEF(unitskillusepos,"iviii?"), // [Celest]
  18093. // <--- [zBuffer] List of mob control commands
  18094. BUILDIN_DEF(sleep,"i"),
  18095. BUILDIN_DEF(sleep2,"i"),
  18096. BUILDIN_DEF(awake,"s"),
  18097. BUILDIN_DEF(getvariableofnpc,"rs"),
  18098. BUILDIN_DEF(warpportal,"iisii"),
  18099. BUILDIN_DEF2(homunculus_evolution,"homevolution",""), //[orn]
  18100. BUILDIN_DEF2(homunculus_mutate,"hommutate","?"),
  18101. BUILDIN_DEF(morphembryo,""),
  18102. BUILDIN_DEF2(homunculus_shuffle,"homshuffle",""), //[Zephyrus]
  18103. BUILDIN_DEF(checkhomcall,""),
  18104. BUILDIN_DEF(eaclass,"?"), //[Skotlex]
  18105. BUILDIN_DEF(roclass,"i?"), //[Skotlex]
  18106. BUILDIN_DEF(checkvending,"?"),
  18107. BUILDIN_DEF(checkchatting,"?"),
  18108. BUILDIN_DEF(checkidle,"?"),
  18109. BUILDIN_DEF(openmail,"?"),
  18110. BUILDIN_DEF(openauction,"?"),
  18111. BUILDIN_DEF(checkcell,"siii"),
  18112. BUILDIN_DEF(setcell,"siiiiii"),
  18113. BUILDIN_DEF(setwall,"siiiiis"),
  18114. BUILDIN_DEF(delwall,"s"),
  18115. BUILDIN_DEF(searchitem,"rs"),
  18116. BUILDIN_DEF(mercenary_create,"ii"),
  18117. BUILDIN_DEF(mercenary_heal,"ii"),
  18118. BUILDIN_DEF(mercenary_sc_start,"iii"),
  18119. BUILDIN_DEF(mercenary_get_calls,"i"),
  18120. BUILDIN_DEF(mercenary_get_faith,"i"),
  18121. BUILDIN_DEF(mercenary_set_calls,"ii"),
  18122. BUILDIN_DEF(mercenary_set_faith,"ii"),
  18123. BUILDIN_DEF(readbook,"ii"),
  18124. BUILDIN_DEF(setfont,"i"),
  18125. BUILDIN_DEF(areamobuseskill,"siiiiviiiii"),
  18126. BUILDIN_DEF(progressbar,"si"),
  18127. BUILDIN_DEF(pushpc,"ii"),
  18128. BUILDIN_DEF(buyingstore,"i"),
  18129. BUILDIN_DEF(searchstores,"ii"),
  18130. BUILDIN_DEF(showdigit,"i?"),
  18131. // WoE SE
  18132. BUILDIN_DEF(agitstart2,""),
  18133. BUILDIN_DEF(agitend2,""),
  18134. BUILDIN_DEF(agitcheck2,""),
  18135. // BattleGround
  18136. BUILDIN_DEF(waitingroom2bg,"siiss?"),
  18137. BUILDIN_DEF(waitingroom2bg_single,"isiis"),
  18138. BUILDIN_DEF(bg_team_setxy,"iii"),
  18139. BUILDIN_DEF(bg_warp,"isii"),
  18140. BUILDIN_DEF(bg_monster,"isiisi?"),
  18141. BUILDIN_DEF(bg_monster_set_team,"ii"),
  18142. BUILDIN_DEF(bg_leave,""),
  18143. BUILDIN_DEF(bg_destroy,"i"),
  18144. BUILDIN_DEF(areapercentheal,"siiiiii"),
  18145. BUILDIN_DEF(bg_get_data,"ii"),
  18146. BUILDIN_DEF(bg_getareausers,"isiiii"),
  18147. BUILDIN_DEF(bg_updatescore,"sii"),
  18148. // Instancing
  18149. BUILDIN_DEF(instance_create,"s"),
  18150. BUILDIN_DEF(instance_destroy,"?"),
  18151. BUILDIN_DEF(instance_id,""),
  18152. BUILDIN_DEF(instance_enter,"s??"),
  18153. BUILDIN_DEF(instance_npcname,"s?"),
  18154. BUILDIN_DEF(instance_mapname,"s?"),
  18155. BUILDIN_DEF(instance_warpall,"sii?"),
  18156. BUILDIN_DEF(instance_announce,"isi?????"),
  18157. BUILDIN_DEF(instance_check_party,"i???"),
  18158. /**
  18159. * 3rd-related
  18160. **/
  18161. BUILDIN_DEF(makerune,"i?"),
  18162. BUILDIN_DEF(checkdragon,"?"),//[Ind]
  18163. BUILDIN_DEF(setdragon,"??"),//[Ind]
  18164. BUILDIN_DEF(ismounting,"?"),//[Ind]
  18165. BUILDIN_DEF(setmounting,"?"),//[Ind]
  18166. BUILDIN_DEF(checkre,"i"),
  18167. /**
  18168. * rAthena and beyond!
  18169. **/
  18170. BUILDIN_DEF(getargcount,""),
  18171. BUILDIN_DEF(getcharip,"?"),
  18172. BUILDIN_DEF(is_function,"s"),
  18173. BUILDIN_DEF(get_revision,""),
  18174. BUILDIN_DEF(get_githash,""),
  18175. BUILDIN_DEF(freeloop,"?"),
  18176. BUILDIN_DEF(getrandgroupitem,"i??"),
  18177. BUILDIN_DEF(cleanmap,"s"),
  18178. BUILDIN_DEF2(cleanmap,"cleanarea","siiii"),
  18179. BUILDIN_DEF(npcskill,"viii"),
  18180. BUILDIN_DEF(consumeitem,"v?"),
  18181. BUILDIN_DEF(delequip,"i?"),
  18182. BUILDIN_DEF(breakequip,"i?"),
  18183. BUILDIN_DEF(sit,"?"),
  18184. BUILDIN_DEF(stand,"?"),
  18185. //@commands (script based)
  18186. BUILDIN_DEF(bindatcmd, "ss??"),
  18187. BUILDIN_DEF(unbindatcmd, "s"),
  18188. BUILDIN_DEF(useatcmd, "s"),
  18189. //Quest Log System [Inkfish]
  18190. BUILDIN_DEF(questinfo, "ii??"),
  18191. BUILDIN_DEF(setquest, "i?"),
  18192. BUILDIN_DEF(erasequest, "i?"),
  18193. BUILDIN_DEF(completequest, "i?"),
  18194. BUILDIN_DEF(checkquest, "i??"),
  18195. BUILDIN_DEF(isbegin_quest,"i?"),
  18196. BUILDIN_DEF(changequest, "ii?"),
  18197. BUILDIN_DEF(showevent, "i??"),
  18198. //Bound items [Xantara] & [Akinari]
  18199. BUILDIN_DEF2(getitem,"getitembound","vii?"),
  18200. BUILDIN_DEF2(getitem2,"getitembound2","viiiiiiiii?"),
  18201. BUILDIN_DEF(countbound, "??"),
  18202. // Party related
  18203. BUILDIN_DEF(party_create,"s???"),
  18204. BUILDIN_DEF(party_addmember,"ii"),
  18205. BUILDIN_DEF(party_delmember,"??"),
  18206. BUILDIN_DEF(party_changeleader,"ii"),
  18207. BUILDIN_DEF(party_changeoption,"iii"),
  18208. BUILDIN_DEF(party_destroy,"i"),
  18209. BUILDIN_DEF(is_clientver,"ii?"),
  18210. BUILDIN_DEF2(montransform, "transform", "vi?????"), // Monster Transform [malufett/Hercules]
  18211. BUILDIN_DEF(vip_status,"i?"),
  18212. BUILDIN_DEF(vip_time,"i?"),
  18213. BUILDIN_DEF(bonus_script,"si????"),
  18214. BUILDIN_DEF(bonus_script_clear,"??"),
  18215. BUILDIN_DEF(getgroupitem,"i"),
  18216. BUILDIN_DEF(enable_command,""),
  18217. BUILDIN_DEF(disable_command,""),
  18218. BUILDIN_DEF(getguildmember,"i?"),
  18219. BUILDIN_DEF(addspiritball,"ii?"),
  18220. BUILDIN_DEF(delspiritball,"i?"),
  18221. BUILDIN_DEF(countspiritball,"?"),
  18222. BUILDIN_DEF(mergeitem,"??"),
  18223. BUILDIN_DEF(npcshopupdate,"sii?"),
  18224. BUILDIN_DEF(getattachedrid,""),
  18225. BUILDIN_DEF(getvar,"vi"),
  18226. BUILDIN_DEF(showscript,"s?"),
  18227. #include "../custom/script_def.inc"
  18228. {NULL,NULL,NULL},
  18229. };