script.c 608 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112131131311413115131161311713118131191312013121131221312313124131251312613127131281312913130131311313213133131341313513136131371313813139131401314113142131431314413145131461314713148131491315013151131521315313154131551315613157131581315913160131611316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208132091321013211132121321313214132151321613217132181321913220132211322213223132241322513226132271322813229132301323113232132331323413235132361323713238132391324013241132421324313244132451324613247132481324913250132511325213253132541325513256132571325813259132601326113262132631326413265132661326713268132691327013271132721327313274132751327613277132781327913280132811328213283132841328513286132871328813289132901329113292132931329413295132961329713298132991330013301133021330313304133051330613307133081330913310133111331213313133141331513316133171331813319133201332113322133231332413325133261332713328133291333013331133321333313334133351333613337133381333913340133411334213343133441334513346133471334813349133501335113352133531335413355133561335713358133591336013361133621336313364133651336613367133681336913370133711337213373133741337513376133771337813379133801338113382133831338413385133861338713388133891339013391133921339313394133951339613397133981339913400134011340213403134041340513406134071340813409134101341113412134131341413415134161341713418134191342013421134221342313424134251342613427134281342913430134311343213433134341343513436134371343813439134401344113442134431344413445134461344713448134491345013451134521345313454134551345613457134581345913460134611346213463134641346513466134671346813469134701347113472134731347413475134761347713478134791348013481134821348313484134851348613487134881348913490134911349213493134941349513496134971349813499135001350113502135031350413505135061350713508135091351013511135121351313514135151351613517135181351913520135211352213523135241352513526135271352813529135301353113532135331353413535135361353713538135391354013541135421354313544135451354613547135481354913550135511355213553135541355513556135571355813559135601356113562135631356413565135661356713568135691357013571135721357313574135751357613577135781357913580135811358213583135841358513586135871358813589135901359113592135931359413595135961359713598135991360013601136021360313604136051360613607136081360913610136111361213613136141361513616136171361813619136201362113622136231362413625136261362713628136291363013631136321363313634136351363613637136381363913640136411364213643136441364513646136471364813649136501365113652136531365413655136561365713658136591366013661136621366313664136651366613667136681366913670136711367213673136741367513676136771367813679136801368113682136831368413685136861368713688136891369013691136921369313694136951369613697136981369913700137011370213703137041370513706137071370813709137101371113712137131371413715137161371713718137191372013721137221372313724137251372613727137281372913730137311373213733137341373513736137371373813739137401374113742137431374413745137461374713748137491375013751137521375313754137551375613757137581375913760137611376213763137641376513766137671376813769137701377113772137731377413775137761377713778137791378013781137821378313784137851378613787137881378913790137911379213793137941379513796137971379813799138001380113802138031380413805138061380713808138091381013811138121381313814138151381613817138181381913820138211382213823138241382513826138271382813829138301383113832138331383413835138361383713838138391384013841138421384313844138451384613847138481384913850138511385213853138541385513856138571385813859138601386113862138631386413865138661386713868138691387013871138721387313874138751387613877138781387913880138811388213883138841388513886138871388813889138901389113892138931389413895138961389713898138991390013901139021390313904139051390613907139081390913910139111391213913139141391513916139171391813919139201392113922139231392413925139261392713928139291393013931139321393313934139351393613937139381393913940139411394213943139441394513946139471394813949139501395113952139531395413955139561395713958139591396013961139621396313964139651396613967139681396913970139711397213973139741397513976139771397813979139801398113982139831398413985139861398713988139891399013991139921399313994139951399613997139981399914000140011400214003140041400514006140071400814009140101401114012140131401414015140161401714018140191402014021140221402314024140251402614027140281402914030140311403214033140341403514036140371403814039140401404114042140431404414045140461404714048140491405014051140521405314054140551405614057140581405914060140611406214063140641406514066140671406814069140701407114072140731407414075140761407714078140791408014081140821408314084140851408614087140881408914090140911409214093140941409514096140971409814099141001410114102141031410414105141061410714108141091411014111141121411314114141151411614117141181411914120141211412214123141241412514126141271412814129141301413114132141331413414135141361413714138141391414014141141421414314144141451414614147141481414914150141511415214153141541415514156141571415814159141601416114162141631416414165141661416714168141691417014171141721417314174141751417614177141781417914180141811418214183141841418514186141871418814189141901419114192141931419414195141961419714198141991420014201142021420314204142051420614207142081420914210142111421214213142141421514216142171421814219142201422114222142231422414225142261422714228142291423014231142321423314234142351423614237142381423914240142411424214243142441424514246142471424814249142501425114252142531425414255142561425714258142591426014261142621426314264142651426614267142681426914270142711427214273142741427514276142771427814279142801428114282142831428414285142861428714288142891429014291142921429314294142951429614297142981429914300143011430214303143041430514306143071430814309143101431114312143131431414315143161431714318143191432014321143221432314324143251432614327143281432914330143311433214333143341433514336143371433814339143401434114342143431434414345143461434714348143491435014351143521435314354143551435614357143581435914360143611436214363143641436514366143671436814369143701437114372143731437414375143761437714378143791438014381143821438314384143851438614387143881438914390143911439214393143941439514396143971439814399144001440114402144031440414405144061440714408144091441014411144121441314414144151441614417144181441914420144211442214423144241442514426144271442814429144301443114432144331443414435144361443714438144391444014441144421444314444144451444614447144481444914450144511445214453144541445514456144571445814459144601446114462144631446414465144661446714468144691447014471144721447314474144751447614477144781447914480144811448214483144841448514486144871448814489144901449114492144931449414495144961449714498144991450014501145021450314504145051450614507145081450914510145111451214513145141451514516145171451814519145201452114522145231452414525145261452714528145291453014531145321453314534145351453614537145381453914540145411454214543145441454514546145471454814549145501455114552145531455414555145561455714558145591456014561145621456314564145651456614567145681456914570145711457214573145741457514576145771457814579145801458114582145831458414585145861458714588145891459014591145921459314594145951459614597145981459914600146011460214603146041460514606146071460814609146101461114612146131461414615146161461714618146191462014621146221462314624146251462614627146281462914630146311463214633146341463514636146371463814639146401464114642146431464414645146461464714648146491465014651146521465314654146551465614657146581465914660146611466214663146641466514666146671466814669146701467114672146731467414675146761467714678146791468014681146821468314684146851468614687146881468914690146911469214693146941469514696146971469814699147001470114702147031470414705147061470714708147091471014711147121471314714147151471614717147181471914720147211472214723147241472514726147271472814729147301473114732147331473414735147361473714738147391474014741147421474314744147451474614747147481474914750147511475214753147541475514756147571475814759147601476114762147631476414765147661476714768147691477014771147721477314774147751477614777147781477914780147811478214783147841478514786147871478814789147901479114792147931479414795147961479714798147991480014801148021480314804148051480614807148081480914810148111481214813148141481514816148171481814819148201482114822148231482414825148261482714828148291483014831148321483314834148351483614837148381483914840148411484214843148441484514846148471484814849148501485114852148531485414855148561485714858148591486014861148621486314864148651486614867148681486914870148711487214873148741487514876148771487814879148801488114882148831488414885148861488714888148891489014891148921489314894148951489614897148981489914900149011490214903149041490514906149071490814909149101491114912149131491414915149161491714918149191492014921149221492314924149251492614927149281492914930149311493214933149341493514936149371493814939149401494114942149431494414945149461494714948149491495014951149521495314954149551495614957149581495914960149611496214963149641496514966149671496814969149701497114972149731497414975149761497714978149791498014981149821498314984149851498614987149881498914990149911499214993149941499514996149971499814999150001500115002150031500415005150061500715008150091501015011150121501315014150151501615017150181501915020150211502215023150241502515026150271502815029150301503115032150331503415035150361503715038150391504015041150421504315044150451504615047150481504915050150511505215053150541505515056150571505815059150601506115062150631506415065150661506715068150691507015071150721507315074150751507615077150781507915080150811508215083150841508515086150871508815089150901509115092150931509415095150961509715098150991510015101151021510315104151051510615107151081510915110151111511215113151141511515116151171511815119151201512115122151231512415125151261512715128151291513015131151321513315134151351513615137151381513915140151411514215143151441514515146151471514815149151501515115152151531515415155151561515715158151591516015161151621516315164151651516615167151681516915170151711517215173151741517515176151771517815179151801518115182151831518415185151861518715188151891519015191151921519315194151951519615197151981519915200152011520215203152041520515206152071520815209152101521115212152131521415215152161521715218152191522015221152221522315224152251522615227152281522915230152311523215233152341523515236152371523815239152401524115242152431524415245152461524715248152491525015251152521525315254152551525615257152581525915260152611526215263152641526515266152671526815269152701527115272152731527415275152761527715278152791528015281152821528315284152851528615287152881528915290152911529215293152941529515296152971529815299153001530115302153031530415305153061530715308153091531015311153121531315314153151531615317153181531915320153211532215323153241532515326153271532815329153301533115332153331533415335153361533715338153391534015341153421534315344153451534615347153481534915350153511535215353153541535515356153571535815359153601536115362153631536415365153661536715368153691537015371153721537315374153751537615377153781537915380153811538215383153841538515386153871538815389153901539115392153931539415395153961539715398153991540015401154021540315404154051540615407154081540915410154111541215413154141541515416154171541815419154201542115422154231542415425154261542715428154291543015431154321543315434154351543615437154381543915440154411544215443154441544515446154471544815449154501545115452154531545415455154561545715458154591546015461154621546315464154651546615467154681546915470154711547215473154741547515476154771547815479154801548115482154831548415485154861548715488154891549015491154921549315494154951549615497154981549915500155011550215503155041550515506155071550815509155101551115512155131551415515155161551715518155191552015521155221552315524155251552615527155281552915530155311553215533155341553515536155371553815539155401554115542155431554415545155461554715548155491555015551155521555315554155551555615557155581555915560155611556215563155641556515566155671556815569155701557115572155731557415575155761557715578155791558015581155821558315584155851558615587155881558915590155911559215593155941559515596155971559815599156001560115602156031560415605156061560715608156091561015611156121561315614156151561615617156181561915620156211562215623156241562515626156271562815629156301563115632156331563415635156361563715638156391564015641156421564315644156451564615647156481564915650156511565215653156541565515656156571565815659156601566115662156631566415665156661566715668156691567015671156721567315674156751567615677156781567915680156811568215683156841568515686156871568815689156901569115692156931569415695156961569715698156991570015701157021570315704157051570615707157081570915710157111571215713157141571515716157171571815719157201572115722157231572415725157261572715728157291573015731157321573315734157351573615737157381573915740157411574215743157441574515746157471574815749157501575115752157531575415755157561575715758157591576015761157621576315764157651576615767157681576915770157711577215773157741577515776157771577815779157801578115782157831578415785157861578715788157891579015791157921579315794157951579615797157981579915800158011580215803158041580515806158071580815809158101581115812158131581415815158161581715818158191582015821158221582315824158251582615827158281582915830158311583215833158341583515836158371583815839158401584115842158431584415845158461584715848158491585015851158521585315854158551585615857158581585915860158611586215863158641586515866158671586815869158701587115872158731587415875158761587715878158791588015881158821588315884158851588615887158881588915890158911589215893158941589515896158971589815899159001590115902159031590415905159061590715908159091591015911159121591315914159151591615917159181591915920159211592215923159241592515926159271592815929159301593115932159331593415935159361593715938159391594015941159421594315944159451594615947159481594915950159511595215953159541595515956159571595815959159601596115962159631596415965159661596715968159691597015971159721597315974159751597615977159781597915980159811598215983159841598515986159871598815989159901599115992159931599415995159961599715998159991600016001160021600316004160051600616007160081600916010160111601216013160141601516016160171601816019160201602116022160231602416025160261602716028160291603016031160321603316034160351603616037160381603916040160411604216043160441604516046160471604816049160501605116052160531605416055160561605716058160591606016061160621606316064160651606616067160681606916070160711607216073160741607516076160771607816079160801608116082160831608416085160861608716088160891609016091160921609316094160951609616097160981609916100161011610216103161041610516106161071610816109161101611116112161131611416115161161611716118161191612016121161221612316124161251612616127161281612916130161311613216133161341613516136161371613816139161401614116142161431614416145161461614716148161491615016151161521615316154161551615616157161581615916160161611616216163161641616516166161671616816169161701617116172161731617416175161761617716178161791618016181161821618316184161851618616187161881618916190161911619216193161941619516196161971619816199162001620116202162031620416205162061620716208162091621016211162121621316214162151621616217162181621916220162211622216223162241622516226162271622816229162301623116232162331623416235162361623716238162391624016241162421624316244162451624616247162481624916250162511625216253162541625516256162571625816259162601626116262162631626416265162661626716268162691627016271162721627316274162751627616277162781627916280162811628216283162841628516286162871628816289162901629116292162931629416295162961629716298162991630016301163021630316304163051630616307163081630916310163111631216313163141631516316163171631816319163201632116322163231632416325163261632716328163291633016331163321633316334163351633616337163381633916340163411634216343163441634516346163471634816349163501635116352163531635416355163561635716358163591636016361163621636316364163651636616367163681636916370163711637216373163741637516376163771637816379163801638116382163831638416385163861638716388163891639016391163921639316394163951639616397163981639916400164011640216403164041640516406164071640816409164101641116412164131641416415164161641716418164191642016421164221642316424164251642616427164281642916430164311643216433164341643516436164371643816439164401644116442164431644416445164461644716448164491645016451164521645316454164551645616457164581645916460164611646216463164641646516466164671646816469164701647116472164731647416475164761647716478164791648016481164821648316484164851648616487164881648916490164911649216493164941649516496164971649816499165001650116502165031650416505165061650716508165091651016511165121651316514165151651616517165181651916520165211652216523165241652516526165271652816529165301653116532165331653416535165361653716538165391654016541165421654316544165451654616547165481654916550165511655216553165541655516556165571655816559165601656116562165631656416565165661656716568165691657016571165721657316574165751657616577165781657916580165811658216583165841658516586165871658816589165901659116592165931659416595165961659716598165991660016601166021660316604166051660616607166081660916610166111661216613166141661516616166171661816619166201662116622166231662416625166261662716628166291663016631166321663316634166351663616637166381663916640166411664216643166441664516646166471664816649166501665116652166531665416655166561665716658166591666016661166621666316664166651666616667166681666916670166711667216673166741667516676166771667816679166801668116682166831668416685166861668716688166891669016691166921669316694166951669616697166981669916700167011670216703167041670516706167071670816709167101671116712167131671416715167161671716718167191672016721167221672316724167251672616727167281672916730167311673216733167341673516736167371673816739167401674116742167431674416745167461674716748167491675016751167521675316754167551675616757167581675916760167611676216763167641676516766167671676816769167701677116772167731677416775167761677716778167791678016781167821678316784167851678616787167881678916790167911679216793167941679516796167971679816799168001680116802168031680416805168061680716808168091681016811168121681316814168151681616817168181681916820168211682216823168241682516826168271682816829168301683116832168331683416835168361683716838168391684016841168421684316844168451684616847168481684916850168511685216853168541685516856168571685816859168601686116862168631686416865168661686716868168691687016871168721687316874168751687616877168781687916880168811688216883168841688516886168871688816889168901689116892168931689416895168961689716898168991690016901169021690316904169051690616907169081690916910169111691216913169141691516916169171691816919169201692116922169231692416925169261692716928169291693016931169321693316934169351693616937169381693916940169411694216943169441694516946169471694816949169501695116952169531695416955169561695716958169591696016961169621696316964169651696616967169681696916970169711697216973169741697516976169771697816979169801698116982169831698416985169861698716988169891699016991169921699316994169951699616997169981699917000170011700217003170041700517006170071700817009170101701117012170131701417015170161701717018170191702017021170221702317024170251702617027170281702917030170311703217033170341703517036170371703817039170401704117042170431704417045170461704717048170491705017051170521705317054170551705617057170581705917060170611706217063170641706517066170671706817069170701707117072170731707417075170761707717078170791708017081170821708317084170851708617087170881708917090170911709217093170941709517096170971709817099171001710117102171031710417105171061710717108171091711017111171121711317114171151711617117171181711917120171211712217123171241712517126171271712817129171301713117132171331713417135171361713717138171391714017141171421714317144171451714617147171481714917150171511715217153171541715517156171571715817159171601716117162171631716417165171661716717168171691717017171171721717317174171751717617177171781717917180171811718217183171841718517186171871718817189171901719117192171931719417195171961719717198171991720017201172021720317204172051720617207172081720917210172111721217213172141721517216172171721817219172201722117222172231722417225172261722717228172291723017231172321723317234172351723617237172381723917240172411724217243172441724517246172471724817249172501725117252172531725417255172561725717258172591726017261172621726317264172651726617267172681726917270172711727217273172741727517276172771727817279172801728117282172831728417285172861728717288172891729017291172921729317294172951729617297172981729917300173011730217303173041730517306173071730817309173101731117312173131731417315173161731717318173191732017321173221732317324173251732617327173281732917330173311733217333173341733517336173371733817339173401734117342173431734417345173461734717348173491735017351173521735317354173551735617357173581735917360173611736217363173641736517366173671736817369173701737117372173731737417375173761737717378173791738017381173821738317384173851738617387173881738917390173911739217393173941739517396173971739817399174001740117402174031740417405174061740717408174091741017411174121741317414174151741617417174181741917420174211742217423174241742517426174271742817429174301743117432174331743417435174361743717438174391744017441174421744317444174451744617447174481744917450174511745217453174541745517456174571745817459174601746117462174631746417465174661746717468174691747017471174721747317474174751747617477174781747917480174811748217483174841748517486174871748817489174901749117492174931749417495174961749717498174991750017501175021750317504175051750617507175081750917510175111751217513175141751517516175171751817519175201752117522175231752417525175261752717528175291753017531175321753317534175351753617537175381753917540175411754217543175441754517546175471754817549175501755117552175531755417555175561755717558175591756017561175621756317564175651756617567175681756917570175711757217573175741757517576175771757817579175801758117582175831758417585175861758717588175891759017591175921759317594175951759617597175981759917600176011760217603176041760517606176071760817609176101761117612176131761417615176161761717618176191762017621176221762317624176251762617627176281762917630176311763217633176341763517636176371763817639176401764117642176431764417645176461764717648176491765017651176521765317654176551765617657176581765917660176611766217663176641766517666176671766817669176701767117672176731767417675176761767717678176791768017681176821768317684176851768617687176881768917690176911769217693176941769517696176971769817699177001770117702177031770417705177061770717708177091771017711177121771317714177151771617717177181771917720177211772217723177241772517726177271772817729177301773117732177331773417735177361773717738177391774017741177421774317744177451774617747177481774917750177511775217753177541775517756177571775817759177601776117762177631776417765177661776717768177691777017771177721777317774177751777617777177781777917780177811778217783177841778517786177871778817789177901779117792177931779417795177961779717798177991780017801178021780317804178051780617807178081780917810178111781217813178141781517816178171781817819178201782117822178231782417825178261782717828178291783017831178321783317834178351783617837178381783917840178411784217843178441784517846178471784817849178501785117852178531785417855178561785717858178591786017861178621786317864178651786617867178681786917870178711787217873178741787517876178771787817879178801788117882178831788417885178861788717888178891789017891178921789317894178951789617897178981789917900179011790217903179041790517906179071790817909179101791117912179131791417915179161791717918179191792017921179221792317924179251792617927179281792917930179311793217933179341793517936179371793817939179401794117942179431794417945179461794717948179491795017951179521795317954179551795617957179581795917960179611796217963179641796517966179671796817969179701797117972179731797417975179761797717978179791798017981179821798317984179851798617987179881798917990179911799217993179941799517996179971799817999180001800118002180031800418005180061800718008180091801018011180121801318014180151801618017180181801918020180211802218023180241802518026180271802818029180301803118032180331803418035180361803718038180391804018041180421804318044180451804618047180481804918050180511805218053180541805518056180571805818059180601806118062180631806418065180661806718068180691807018071180721807318074180751807618077180781807918080180811808218083180841808518086180871808818089180901809118092180931809418095180961809718098180991810018101181021810318104181051810618107181081810918110181111811218113181141811518116181171811818119181201812118122181231812418125181261812718128181291813018131181321813318134181351813618137181381813918140181411814218143181441814518146181471814818149181501815118152181531815418155181561815718158181591816018161181621816318164181651816618167181681816918170181711817218173181741817518176181771817818179181801818118182181831818418185181861818718188181891819018191181921819318194181951819618197181981819918200182011820218203182041820518206182071820818209182101821118212182131821418215182161821718218182191822018221182221822318224182251822618227182281822918230182311823218233182341823518236182371823818239182401824118242182431824418245182461824718248182491825018251182521825318254182551825618257182581825918260182611826218263182641826518266182671826818269182701827118272182731827418275182761827718278182791828018281182821828318284182851828618287182881828918290182911829218293182941829518296182971829818299183001830118302183031830418305183061830718308183091831018311183121831318314183151831618317183181831918320183211832218323183241832518326183271832818329183301833118332183331833418335183361833718338183391834018341183421834318344183451834618347183481834918350183511835218353183541835518356183571835818359183601836118362183631836418365183661836718368183691837018371183721837318374183751837618377183781837918380183811838218383183841838518386183871838818389183901839118392183931839418395183961839718398183991840018401184021840318404184051840618407184081840918410184111841218413184141841518416184171841818419184201842118422184231842418425184261842718428184291843018431184321843318434184351843618437184381843918440184411844218443184441844518446184471844818449184501845118452184531845418455184561845718458184591846018461184621846318464184651846618467184681846918470184711847218473184741847518476184771847818479184801848118482184831848418485184861848718488184891849018491184921849318494184951849618497184981849918500185011850218503185041850518506185071850818509185101851118512185131851418515185161851718518185191852018521185221852318524185251852618527185281852918530185311853218533185341853518536185371853818539185401854118542185431854418545185461854718548185491855018551185521855318554185551855618557185581855918560185611856218563185641856518566185671856818569185701857118572185731857418575185761857718578185791858018581185821858318584185851858618587185881858918590185911859218593185941859518596185971859818599186001860118602186031860418605186061860718608186091861018611186121861318614186151861618617186181861918620186211862218623186241862518626186271862818629186301863118632186331863418635186361863718638186391864018641186421864318644186451864618647186481864918650186511865218653186541865518656186571865818659186601866118662186631866418665186661866718668186691867018671186721867318674186751867618677186781867918680186811868218683186841868518686186871868818689186901869118692186931869418695186961869718698186991870018701187021870318704187051870618707187081870918710187111871218713187141871518716187171871818719187201872118722187231872418725187261872718728187291873018731187321873318734187351873618737187381873918740187411874218743187441874518746187471874818749187501875118752187531875418755187561875718758187591876018761187621876318764187651876618767187681876918770187711877218773187741877518776187771877818779187801878118782187831878418785187861878718788187891879018791187921879318794187951879618797187981879918800188011880218803188041880518806188071880818809188101881118812188131881418815188161881718818188191882018821188221882318824188251882618827188281882918830188311883218833188341883518836188371883818839188401884118842188431884418845188461884718848188491885018851188521885318854188551885618857188581885918860188611886218863188641886518866188671886818869188701887118872188731887418875188761887718878188791888018881188821888318884188851888618887188881888918890188911889218893188941889518896188971889818899189001890118902189031890418905189061890718908189091891018911189121891318914189151891618917189181891918920189211892218923189241892518926189271892818929189301893118932189331893418935189361893718938189391894018941189421894318944189451894618947189481894918950189511895218953189541895518956189571895818959189601896118962189631896418965189661896718968189691897018971189721897318974189751897618977189781897918980189811898218983189841898518986189871898818989189901899118992189931899418995189961899718998189991900019001190021900319004190051900619007190081900919010190111901219013190141901519016190171901819019190201902119022190231902419025190261902719028190291903019031190321903319034190351903619037190381903919040190411904219043190441904519046190471904819049190501905119052190531905419055190561905719058190591906019061190621906319064190651906619067190681906919070190711907219073190741907519076190771907819079190801908119082190831908419085190861908719088190891909019091190921909319094190951909619097190981909919100191011910219103191041910519106191071910819109191101911119112191131911419115191161911719118191191912019121191221912319124191251912619127191281912919130191311913219133191341913519136191371913819139191401914119142191431914419145191461914719148191491915019151191521915319154191551915619157191581915919160191611916219163191641916519166191671916819169191701917119172191731917419175191761917719178191791918019181191821918319184191851918619187191881918919190191911919219193191941919519196191971919819199192001920119202192031920419205192061920719208192091921019211192121921319214192151921619217192181921919220192211922219223192241922519226192271922819229192301923119232192331923419235192361923719238192391924019241192421924319244192451924619247192481924919250192511925219253192541925519256192571925819259192601926119262192631926419265192661926719268192691927019271192721927319274192751927619277192781927919280192811928219283192841928519286192871928819289192901929119292192931929419295192961929719298192991930019301193021930319304193051930619307193081930919310193111931219313193141931519316193171931819319193201932119322193231932419325193261932719328193291933019331193321933319334193351933619337193381933919340193411934219343193441934519346193471934819349193501935119352193531935419355193561935719358193591936019361193621936319364193651936619367193681936919370193711937219373193741937519376193771937819379193801938119382193831938419385193861938719388193891939019391193921939319394193951939619397193981939919400194011940219403194041940519406194071940819409194101941119412194131941419415194161941719418194191942019421194221942319424194251942619427194281942919430194311943219433194341943519436194371943819439194401944119442194431944419445194461944719448194491945019451194521945319454194551945619457194581945919460194611946219463194641946519466194671946819469194701947119472194731947419475194761947719478194791948019481194821948319484194851948619487194881948919490194911949219493194941949519496194971949819499195001950119502195031950419505195061950719508195091951019511195121951319514195151951619517195181951919520195211952219523195241952519526195271952819529195301953119532195331953419535195361953719538195391954019541195421954319544195451954619547195481954919550195511955219553195541955519556195571955819559195601956119562195631956419565195661956719568195691957019571195721957319574195751957619577195781957919580195811958219583195841958519586195871958819589195901959119592195931959419595195961959719598195991960019601196021960319604196051960619607196081960919610196111961219613196141961519616196171961819619196201962119622196231962419625196261962719628196291963019631196321963319634196351963619637196381963919640196411964219643196441964519646196471964819649196501965119652196531965419655196561965719658196591966019661196621966319664196651966619667196681966919670196711967219673196741967519676196771967819679196801968119682196831968419685196861968719688196891969019691196921969319694196951969619697196981969919700197011970219703197041970519706197071970819709197101971119712197131971419715197161971719718197191972019721197221972319724197251972619727197281972919730197311973219733197341973519736197371973819739197401974119742197431974419745197461974719748197491975019751197521975319754197551975619757197581975919760197611976219763197641976519766197671976819769197701977119772197731977419775197761977719778197791978019781197821978319784197851978619787197881978919790197911979219793197941979519796197971979819799198001980119802198031980419805198061980719808198091981019811198121981319814198151981619817198181981919820198211982219823198241982519826198271982819829198301983119832198331983419835198361983719838198391984019841198421984319844198451984619847198481984919850198511985219853198541985519856198571985819859198601986119862198631986419865198661986719868198691987019871198721987319874198751987619877198781987919880198811988219883198841988519886198871988819889198901989119892198931989419895198961989719898198991990019901199021990319904199051990619907199081990919910199111991219913199141991519916199171991819919199201992119922199231992419925199261992719928199291993019931199321993319934199351993619937199381993919940199411994219943199441994519946199471994819949199501995119952199531995419955199561995719958199591996019961199621996319964199651996619967199681996919970199711997219973199741997519976199771997819979199801998119982199831998419985199861998719988199891999019991199921999319994199951999619997199981999920000200012000220003200042000520006200072000820009200102001120012200132001420015200162001720018200192002020021200222002320024200252002620027200282002920030200312003220033200342003520036200372003820039200402004120042200432004420045200462004720048200492005020051200522005320054200552005620057200582005920060200612006220063200642006520066200672006820069200702007120072200732007420075200762007720078200792008020081200822008320084200852008620087200882008920090200912009220093200942009520096200972009820099201002010120102201032010420105201062010720108201092011020111201122011320114201152011620117201182011920120201212012220123201242012520126201272012820129201302013120132201332013420135201362013720138201392014020141201422014320144201452014620147201482014920150201512015220153201542015520156201572015820159201602016120162201632016420165201662016720168201692017020171201722017320174201752017620177201782017920180201812018220183201842018520186201872018820189201902019120192201932019420195201962019720198201992020020201202022020320204202052020620207202082020920210202112021220213202142021520216202172021820219202202022120222202232022420225202262022720228202292023020231202322023320234202352023620237202382023920240202412024220243202442024520246202472024820249202502025120252202532025420255202562025720258202592026020261202622026320264202652026620267202682026920270202712027220273202742027520276202772027820279202802028120282202832028420285202862028720288202892029020291202922029320294202952029620297202982029920300203012030220303203042030520306203072030820309203102031120312203132031420315203162031720318203192032020321203222032320324203252032620327203282032920330203312033220333203342033520336203372033820339203402034120342203432034420345203462034720348203492035020351203522035320354203552035620357203582035920360203612036220363203642036520366203672036820369203702037120372203732037420375203762037720378203792038020381203822038320384203852038620387203882038920390203912039220393203942039520396203972039820399204002040120402204032040420405204062040720408204092041020411204122041320414204152041620417204182041920420204212042220423204242042520426204272042820429204302043120432204332043420435204362043720438204392044020441204422044320444204452044620447204482044920450204512045220453204542045520456204572045820459204602046120462204632046420465204662046720468204692047020471204722047320474204752047620477204782047920480204812048220483204842048520486204872048820489204902049120492204932049420495204962049720498204992050020501205022050320504205052050620507205082050920510205112051220513205142051520516205172051820519205202052120522205232052420525205262052720528205292053020531205322053320534205352053620537205382053920540205412054220543205442054520546205472054820549205502055120552205532055420555205562055720558205592056020561205622056320564205652056620567205682056920570205712057220573205742057520576205772057820579205802058120582205832058420585205862058720588205892059020591205922059320594205952059620597205982059920600206012060220603206042060520606206072060820609206102061120612206132061420615206162061720618206192062020621206222062320624206252062620627206282062920630206312063220633206342063520636206372063820639206402064120642206432064420645206462064720648206492065020651206522065320654206552065620657206582065920660206612066220663206642066520666206672066820669206702067120672206732067420675206762067720678206792068020681206822068320684206852068620687206882068920690206912069220693206942069520696206972069820699207002070120702207032070420705207062070720708207092071020711207122071320714207152071620717207182071920720207212072220723207242072520726207272072820729207302073120732207332073420735207362073720738207392074020741207422074320744207452074620747207482074920750207512075220753207542075520756207572075820759207602076120762207632076420765207662076720768207692077020771207722077320774207752077620777207782077920780207812078220783207842078520786207872078820789207902079120792207932079420795207962079720798207992080020801208022080320804208052080620807208082080920810208112081220813208142081520816208172081820819208202082120822208232082420825208262082720828208292083020831208322083320834208352083620837208382083920840208412084220843208442084520846208472084820849208502085120852208532085420855208562085720858208592086020861208622086320864208652086620867208682086920870208712087220873208742087520876208772087820879208802088120882208832088420885208862088720888208892089020891208922089320894208952089620897208982089920900209012090220903209042090520906209072090820909209102091120912209132091420915209162091720918209192092020921209222092320924209252092620927209282092920930209312093220933209342093520936209372093820939209402094120942209432094420945209462094720948209492095020951209522095320954209552095620957209582095920960209612096220963209642096520966209672096820969209702097120972209732097420975209762097720978209792098020981209822098320984209852098620987209882098920990209912099220993209942099520996209972099820999210002100121002210032100421005210062100721008210092101021011210122101321014210152101621017210182101921020210212102221023210242102521026210272102821029210302103121032210332103421035210362103721038210392104021041210422104321044210452104621047210482104921050210512105221053210542105521056210572105821059210602106121062210632106421065210662106721068210692107021071210722107321074210752107621077210782107921080210812108221083210842108521086210872108821089210902109121092210932109421095210962109721098210992110021101211022110321104211052110621107211082110921110211112111221113211142111521116211172111821119211202112121122211232112421125211262112721128211292113021131211322113321134211352113621137211382113921140211412114221143211442114521146211472114821149211502115121152211532115421155211562115721158211592116021161211622116321164211652116621167211682116921170211712117221173211742117521176211772117821179211802118121182211832118421185211862118721188211892119021191211922119321194211952119621197211982119921200212012120221203212042120521206212072120821209212102121121212212132121421215212162121721218212192122021221212222122321224212252122621227212282122921230212312123221233212342123521236212372123821239212402124121242212432124421245212462124721248212492125021251212522125321254212552125621257212582125921260212612126221263212642126521266212672126821269212702127121272212732127421275212762127721278212792128021281212822128321284212852128621287212882128921290212912129221293212942129521296212972129821299213002130121302213032130421305213062130721308213092131021311213122131321314213152131621317213182131921320213212132221323213242132521326213272132821329213302133121332213332133421335213362133721338213392134021341213422134321344213452134621347213482134921350213512135221353213542135521356213572135821359213602136121362213632136421365213662136721368213692137021371213722137321374213752137621377213782137921380213812138221383213842138521386213872138821389213902139121392213932139421395213962139721398213992140021401214022140321404214052140621407214082140921410214112141221413214142141521416214172141821419214202142121422214232142421425214262142721428214292143021431214322143321434214352143621437214382143921440214412144221443214442144521446214472144821449214502145121452214532145421455214562145721458214592146021461214622146321464214652146621467214682146921470214712147221473214742147521476214772147821479214802148121482214832148421485214862148721488214892149021491214922149321494214952149621497214982149921500215012150221503215042150521506215072150821509215102151121512215132151421515215162151721518215192152021521215222152321524215252152621527215282152921530215312153221533215342153521536215372153821539215402154121542215432154421545215462154721548215492155021551215522155321554215552155621557215582155921560215612156221563215642156521566215672156821569215702157121572215732157421575215762157721578215792158021581215822158321584215852158621587215882158921590215912159221593215942159521596215972159821599216002160121602216032160421605216062160721608216092161021611216122161321614216152161621617216182161921620216212162221623216242162521626216272162821629216302163121632216332163421635216362163721638216392164021641216422164321644216452164621647216482164921650216512165221653216542165521656216572165821659216602166121662216632166421665216662166721668216692167021671216722167321674216752167621677216782167921680216812168221683216842168521686216872168821689216902169121692216932169421695216962169721698216992170021701217022170321704217052170621707217082170921710217112171221713217142171521716217172171821719217202172121722217232172421725217262172721728217292173021731217322173321734217352173621737217382173921740217412174221743217442174521746217472174821749217502175121752217532175421755217562175721758217592176021761217622176321764217652176621767217682176921770217712177221773217742177521776217772177821779217802178121782217832178421785217862178721788217892179021791217922179321794217952179621797217982179921800218012180221803218042180521806218072180821809218102181121812218132181421815218162181721818218192182021821218222182321824218252182621827218282182921830218312183221833218342183521836218372183821839218402184121842218432184421845218462184721848218492185021851218522185321854218552185621857218582185921860218612186221863218642186521866218672186821869218702187121872218732187421875218762187721878218792188021881218822188321884218852188621887218882188921890218912189221893218942189521896218972189821899219002190121902219032190421905219062190721908219092191021911219122191321914219152191621917219182191921920219212192221923219242192521926219272192821929219302193121932219332193421935219362193721938219392194021941219422194321944219452194621947219482194921950219512195221953219542195521956219572195821959219602196121962219632196421965219662196721968219692197021971219722197321974219752197621977219782197921980219812198221983219842198521986219872198821989219902199121992219932199421995219962199721998219992200022001220022200322004220052200622007220082200922010220112201222013220142201522016220172201822019220202202122022220232202422025220262202722028220292203022031220322203322034220352203622037220382203922040220412204222043220442204522046220472204822049220502205122052220532205422055220562205722058220592206022061220622206322064220652206622067220682206922070220712207222073220742207522076220772207822079220802208122082220832208422085220862208722088220892209022091220922209322094220952209622097220982209922100221012210222103221042210522106221072210822109221102211122112221132211422115221162211722118221192212022121221222212322124221252212622127221282212922130221312213222133221342213522136221372213822139221402214122142221432214422145221462214722148221492215022151221522215322154221552215622157221582215922160221612216222163221642216522166221672216822169221702217122172221732217422175221762217722178221792218022181221822218322184221852218622187221882218922190221912219222193221942219522196221972219822199222002220122202222032220422205222062220722208222092221022211222122221322214222152221622217222182221922220222212222222223222242222522226222272222822229222302223122232222332223422235222362223722238222392224022241222422224322244222452224622247222482224922250222512225222253222542225522256222572225822259222602226122262222632226422265222662226722268222692227022271222722227322274222752227622277222782227922280222812228222283222842228522286222872228822289222902229122292222932229422295222962229722298222992230022301223022230322304223052230622307223082230922310223112231222313223142231522316223172231822319223202232122322223232232422325223262232722328223292233022331223322233322334223352233622337223382233922340223412234222343223442234522346223472234822349223502235122352223532235422355223562235722358223592236022361223622236322364223652236622367223682236922370223712237222373223742237522376223772237822379223802238122382223832238422385223862238722388223892239022391223922239322394223952239622397223982239922400224012240222403224042240522406224072240822409224102241122412224132241422415224162241722418224192242022421224222242322424224252242622427224282242922430224312243222433224342243522436224372243822439224402244122442224432244422445224462244722448224492245022451224522245322454224552245622457224582245922460224612246222463224642246522466224672246822469224702247122472224732247422475224762247722478224792248022481224822248322484224852248622487224882248922490224912249222493224942249522496224972249822499225002250122502225032250422505225062250722508225092251022511225122251322514225152251622517225182251922520225212252222523225242252522526225272252822529225302253122532225332253422535225362253722538225392254022541225422254322544225452254622547225482254922550225512255222553225542255522556225572255822559225602256122562225632256422565225662256722568225692257022571225722257322574225752257622577225782257922580225812258222583225842258522586225872258822589225902259122592225932259422595225962259722598225992260022601226022260322604226052260622607226082260922610226112261222613226142261522616226172261822619226202262122622226232262422625226262262722628226292263022631226322263322634226352263622637226382263922640226412264222643226442264522646226472264822649226502265122652226532265422655226562265722658226592266022661226622266322664226652266622667226682266922670226712267222673226742267522676226772267822679226802268122682226832268422685226862268722688226892269022691226922269322694226952269622697226982269922700227012270222703227042270522706227072270822709227102271122712227132271422715227162271722718227192272022721227222272322724227252272622727227282272922730227312273222733227342273522736227372273822739227402274122742227432274422745227462274722748227492275022751227522275322754227552275622757227582275922760227612276222763227642276522766227672276822769227702277122772227732277422775227762277722778227792278022781227822278322784227852278622787227882278922790227912279222793227942279522796227972279822799
  1. // Copyright (c) Athena Dev Teams - Licensed under GNU GPL
  2. // For more information, see LICENCE in the main folder
  3. //#define DEBUG_DISP
  4. //#define DEBUG_DISASM
  5. //#define DEBUG_RUN
  6. //#define DEBUG_HASH
  7. //#define DEBUG_DUMP_STACK
  8. #ifdef PCRE_SUPPORT
  9. #include "../../3rdparty/pcre/include/pcre.h" // preg_match
  10. #endif
  11. #include "../common/cbasetypes.h"
  12. #include "../common/malloc.h"
  13. #include "../common/md5calc.h"
  14. #include "../common/nullpo.h"
  15. #include "../common/random.h"
  16. #include "../common/showmsg.h"
  17. #include "../common/socket.h"
  18. #include "../common/strlib.h"
  19. #include "../common/timer.h"
  20. #include "../common/utils.h"
  21. #ifdef BETA_THREAD_TEST
  22. #include "../common/atomic.h"
  23. #include "../common/spinlock.h"
  24. #include "../common/thread.h"
  25. #include "../common/mutex.h"
  26. #endif
  27. #include "map.h"
  28. #include "path.h"
  29. #include "clan.h"
  30. #include "clif.h"
  31. #include "chrif.h"
  32. #include "itemdb.h"
  33. #include "pc.h"
  34. #include "storage.h"
  35. #include "pet.h"
  36. #include "mapreg.h"
  37. #include "homunculus.h"
  38. #include "instance.h"
  39. #include "mercenary.h"
  40. #include "intif.h"
  41. #include "chat.h"
  42. #include "battleground.h"
  43. #include "party.h"
  44. #include "mail.h"
  45. #include "quest.h"
  46. #include "elemental.h"
  47. #include <math.h>
  48. #ifndef WIN32
  49. #endif
  50. #include <setjmp.h>
  51. #include <errno.h>
  52. TBL_PC *script_rid2sd(struct script_state *st);
  53. /**
  54. * Get `sd` from a account id in `loc` param instead of attached rid
  55. * @param st Script
  56. * @param loc Location to look account id in script parameter
  57. * @param sd Variable that will be assigned
  58. * @return True if `sd` is assigned, false otherwise
  59. **/
  60. static bool script_accid2sd_(struct script_state *st, uint8 loc, struct map_session_data **sd, const char *func) {
  61. if (script_hasdata(st, loc)) {
  62. int id_ = script_getnum(st, loc);
  63. if (!(*sd = map_id2sd(id_)))
  64. ShowError("%s: Player with account id '%d' is not found.\n", func, id_);
  65. }
  66. else
  67. *sd = script_rid2sd(st);
  68. return (*sd) ? true : false;
  69. }
  70. /**
  71. * Get `sd` from a char id in `loc` param instead of attached rid
  72. * @param st Script
  73. * @param loc Location to look char id in script parameter
  74. * @param sd Variable that will be assigned
  75. * @return True if `sd` is assigned, false otherwise
  76. **/
  77. static bool script_charid2sd_(struct script_state *st, uint8 loc, struct map_session_data **sd, const char *func) {
  78. if (script_hasdata(st, loc)) {
  79. int id_ = script_getnum(st, loc);
  80. if (!(*sd = map_charid2sd(id_)))
  81. ShowError("%s: Player with char id '%d' is not found.\n", func, id_);
  82. }
  83. else
  84. *sd = script_rid2sd(st);
  85. return (*sd) ? true : false;
  86. }
  87. /**
  88. * Get `sd` from a nick in `loc` param instead of attached rid
  89. * @param st Script
  90. * @param loc Location to look nick in script parameter
  91. * @param sd Variable that will be assigned
  92. * @return True if `sd` is assigned, false otherwise
  93. **/
  94. static bool script_nick2sd_(struct script_state *st, uint8 loc, struct map_session_data **sd, const char *func) {
  95. if (script_hasdata(st, loc)) {
  96. const char *name_ = script_getstr(st, loc);
  97. if (!(*sd = map_nick2sd(name_)))
  98. ShowError("%s: Player with nick '%s' is not found.\n", func, name_);
  99. }
  100. else
  101. *sd = script_rid2sd(st);
  102. return (*sd) ? true : false;
  103. }
  104. /**
  105. * Get `bl` from an ID in `loc` param, if ID is 0 will return the `bl` of the script's activator.
  106. * @param st Script
  107. * @param loc Location to look for ID in script parameter
  108. * @param bl Variable that will be assigned
  109. * @return True if `bl` is assigned, false otherwise
  110. **/
  111. static bool script_rid2bl_(struct script_state *st, uint8 loc, struct block_list **bl, const char *func) {
  112. int unit_id;
  113. if ( !script_hasdata(st, loc) || ( unit_id = script_getnum(st, loc) ) == 0)
  114. unit_id = st->rid;
  115. *bl = map_id2bl(unit_id);
  116. if ( *bl )
  117. return true;
  118. else {
  119. ShowError("%s: Unit with ID '%d' is not found.\n", func, unit_id);
  120. return false;
  121. }
  122. }
  123. #define script_accid2sd(loc,sd) script_accid2sd_(st,(loc),&(sd),__FUNCTION__)
  124. #define script_charid2sd(loc,sd) script_charid2sd_(st,(loc),&(sd),__FUNCTION__)
  125. #define script_nick2sd(loc,sd) script_nick2sd_(st,(loc),&(sd),__FUNCTION__)
  126. #define script_rid2bl(loc,bl) script_rid2bl_(st,(loc),&(bl),__FUNCTION__)
  127. /// temporary buffer for passing around compiled bytecode
  128. /// @see add_scriptb, set_label, parse_script
  129. static unsigned char* script_buf = NULL;
  130. static int script_pos = 0, script_size = 0;
  131. static inline int GETVALUE(const unsigned char* buf, int i)
  132. {
  133. return (int)MakeDWord(MakeWord(buf[i], buf[i+1]), MakeWord(buf[i+2], 0));
  134. }
  135. static inline void SETVALUE(unsigned char* buf, int i, int n)
  136. {
  137. buf[i] = GetByte(n, 0);
  138. buf[i+1] = GetByte(n, 1);
  139. buf[i+2] = GetByte(n, 2);
  140. }
  141. // String buffer structures.
  142. // str_data stores string information
  143. static struct str_data_struct {
  144. enum c_op type;
  145. int str;
  146. int backpatch;
  147. int label;
  148. int (*func)(struct script_state *st);
  149. int val;
  150. int next;
  151. } *str_data = NULL;
  152. static int str_data_size = 0; // size of the data
  153. static int str_num = LABEL_START; // next id to be assigned
  154. // str_buf holds the strings themselves
  155. static char *str_buf;
  156. static int str_size = 0; // size of the buffer
  157. static int str_pos = 0; // next position to be assigned
  158. // Using a prime number for SCRIPT_HASH_SIZE should give better distributions
  159. #define SCRIPT_HASH_SIZE 1021
  160. int str_hash[SCRIPT_HASH_SIZE];
  161. // Specifies which string hashing method to use
  162. //#define SCRIPT_HASH_DJB2
  163. //#define SCRIPT_HASH_SDBM
  164. #define SCRIPT_HASH_ELF
  165. static DBMap* scriptlabel_db = NULL; // const char* label_name -> int script_pos
  166. static DBMap* userfunc_db = NULL; // const char* func_name -> struct script_code*
  167. static int parse_options = 0;
  168. DBMap* script_get_label_db(void) { return scriptlabel_db; }
  169. DBMap* script_get_userfunc_db(void) { return userfunc_db; }
  170. // important buildin function references for usage in scripts
  171. static int buildin_set_ref = 0;
  172. static int buildin_callsub_ref = 0;
  173. static int buildin_callfunc_ref = 0;
  174. static int buildin_getelementofarray_ref = 0;
  175. // Caches compiled autoscript item code.
  176. // Note: This is not cleared when reloading itemdb.
  177. static DBMap* autobonus_db = NULL; // char* script -> char* bytecode
  178. struct Script_Config script_config = {
  179. 1, // warn_func_mismatch_argtypes
  180. 1, 65535, 2048, //warn_func_mismatch_paramnum/check_cmdcount/check_gotocount
  181. 0, INT_MAX, // input_min_value/input_max_value
  182. // NOTE: None of these event labels should be longer than <NAME_LENGTH> characters
  183. "OnPCDieEvent", //die_event_name
  184. "OnPCKillEvent", //kill_pc_event_name
  185. "OnNPCKillEvent", //kill_mob_event_name
  186. "OnPCLoginEvent", //login_event_name
  187. "OnPCLogoutEvent", //logout_event_name
  188. "OnPCLoadMapEvent", //loadmap_event_name
  189. "OnPCBaseLvUpEvent", //baselvup_event_name
  190. "OnPCJobLvUpEvent", //joblvup_event_name
  191. "OnPCStatCalcEvent", //stat_calc_event_name
  192. "OnTouch_", //ontouch_name (runs on first visible char to enter area, picks another char if the first char leaves)
  193. "OnTouch", //ontouch2_name (run whenever a char walks into the OnTouch area)
  194. "OnWhisperGlobal", //onwhisper_event_name (is executed when a player sends a whisper message to the NPC)
  195. };
  196. static jmp_buf error_jump;
  197. static char* error_msg;
  198. static const char* error_pos;
  199. static int error_report; // if the error should produce output
  200. // Used by disp_warning_message
  201. static const char* parser_current_src;
  202. static const char* parser_current_file;
  203. static int parser_current_line;
  204. // for advanced scripting support ( nested if, switch, while, for, do-while, function, etc )
  205. // [Eoe / jA 1080, 1081, 1094, 1164]
  206. enum curly_type {
  207. TYPE_NULL = 0,
  208. TYPE_IF,
  209. TYPE_SWITCH,
  210. TYPE_WHILE,
  211. TYPE_FOR,
  212. TYPE_DO,
  213. TYPE_USERFUNC,
  214. TYPE_ARGLIST // function argument list
  215. };
  216. enum e_arglist
  217. {
  218. ARGLIST_UNDEFINED = 0,
  219. ARGLIST_NO_PAREN = 1,
  220. ARGLIST_PAREN = 2,
  221. };
  222. static struct {
  223. struct {
  224. enum curly_type type;
  225. int index;
  226. int count;
  227. int flag;
  228. struct linkdb_node *case_label;
  229. } curly[256]; // Information right parenthesis
  230. int curly_count; // The number of right brackets
  231. int index; // Number of the syntax used in the script
  232. } syntax;
  233. const char* parse_curly_close(const char* p);
  234. const char* parse_syntax_close(const char* p);
  235. const char* parse_syntax_close_sub(const char* p,int* flag);
  236. const char* parse_syntax(const char* p);
  237. static int parse_syntax_for_flag = 0;
  238. extern short current_equip_item_index; //for New CARDS Scripts. It contains Inventory Index of the EQUIP_SCRIPT caller item. [Lupus]
  239. extern unsigned int current_equip_combo_pos;
  240. int potion_flag=0; //For use on Alchemist improved potions/Potion Pitcher. [Skotlex]
  241. int potion_hp=0, potion_per_hp=0, potion_sp=0, potion_per_sp=0;
  242. int potion_target = 0;
  243. unsigned int *generic_ui_array = NULL;
  244. unsigned int generic_ui_array_size = 0;
  245. c_op get_com(unsigned char *script,int *pos);
  246. int get_num(unsigned char *script,int *pos);
  247. typedef struct script_function {
  248. int (*func)(struct script_state *st);
  249. const char *name;
  250. const char *arg;
  251. } script_function;
  252. extern script_function buildin_func[];
  253. #ifdef BETA_THREAD_TEST
  254. /**
  255. * MySQL Query Slave
  256. **/
  257. static SPIN_LOCK queryThreadLock;
  258. static rAthread queryThread = NULL;
  259. static ramutex queryThreadMutex = NULL;
  260. static racond queryThreadCond = NULL;
  261. static volatile int32 queryThreadTerminate = 0;
  262. struct queryThreadEntry {
  263. bool ok;
  264. bool type; /* main db or log db? */
  265. struct script_state *st;
  266. };
  267. /* Ladies and Gentleman the Manager! */
  268. struct {
  269. struct queryThreadEntry **entry;/* array of structs */
  270. int count;
  271. int timer;/* used to receive processed entries */
  272. } queryThreadData;
  273. #endif
  274. /*==========================================
  275. * (Only those needed) local declaration prototype
  276. *------------------------------------------*/
  277. const char* parse_subexpr(const char* p,int limit);
  278. int run_func(struct script_state *st);
  279. unsigned short script_instancegetid(struct script_state *st);
  280. enum {
  281. MF_NOMEMO, //0
  282. MF_NOTELEPORT,
  283. MF_NOSAVE,
  284. MF_NOBRANCH,
  285. MF_NOPENALTY,
  286. MF_NOZENYPENALTY,
  287. MF_PVP,
  288. MF_PVP_NOPARTY,
  289. MF_PVP_NOGUILD,
  290. MF_GVG,
  291. MF_GVG_NOPARTY, //10
  292. MF_NOTRADE,
  293. MF_NOSKILL,
  294. MF_NOWARP,
  295. MF_PARTYLOCK,
  296. MF_NOICEWALL,
  297. MF_SNOW,
  298. MF_FOG,
  299. MF_SAKURA,
  300. MF_LEAVES,
  301. /**
  302. * No longer available, keeping here just in case it's back someday. [Ind]
  303. **/
  304. //MF_RAIN, //20
  305. // 21 free
  306. MF_NOGO = 22,
  307. MF_CLOUDS,
  308. MF_CLOUDS2,
  309. MF_FIREWORKS,
  310. MF_GVG_CASTLE,
  311. MF_GVG_DUNGEON,
  312. MF_NIGHTENABLED,
  313. MF_NOBASEEXP,
  314. MF_NOJOBEXP, //30
  315. MF_NOMOBLOOT,
  316. MF_NOMVPLOOT,
  317. MF_NORETURN,
  318. MF_NOWARPTO,
  319. MF_NIGHTMAREDROP,
  320. MF_RESTRICTED,
  321. MF_NOCOMMAND,
  322. MF_NODROP,
  323. MF_JEXP,
  324. MF_BEXP, //40
  325. MF_NOVENDING,
  326. MF_LOADEVENT,
  327. MF_NOCHAT,
  328. MF_NOEXPPENALTY,
  329. MF_GUILDLOCK,
  330. MF_TOWN,
  331. MF_AUTOTRADE,
  332. MF_ALLOWKS,
  333. MF_MONSTER_NOTELEPORT,
  334. MF_PVP_NOCALCRANK, //50
  335. MF_BATTLEGROUND,
  336. MF_RESET,
  337. MF_CHANNELAUTOJOIN,
  338. MF_NOUSECART,
  339. MF_NOITEMCONSUMPTION,
  340. MF_SUMSTARTMIRACLE,
  341. MF_NOMINEEFFECT,
  342. MF_NOLOCKON,
  343. MF_NOTOMB,
  344. MF_SKILL_DAMAGE, //60
  345. MF_NOCOSTUME,
  346. MF_GVG_TE_CASTLE,
  347. MF_GVG_TE,
  348. MF_HIDEMOBHPBAR,
  349. };
  350. const char* script_op2name(int op)
  351. {
  352. #define RETURN_OP_NAME(type) case type: return #type
  353. switch( op )
  354. {
  355. RETURN_OP_NAME(C_NOP);
  356. RETURN_OP_NAME(C_POS);
  357. RETURN_OP_NAME(C_INT);
  358. RETURN_OP_NAME(C_PARAM);
  359. RETURN_OP_NAME(C_FUNC);
  360. RETURN_OP_NAME(C_STR);
  361. RETURN_OP_NAME(C_CONSTSTR);
  362. RETURN_OP_NAME(C_ARG);
  363. RETURN_OP_NAME(C_NAME);
  364. RETURN_OP_NAME(C_EOL);
  365. RETURN_OP_NAME(C_RETINFO);
  366. RETURN_OP_NAME(C_USERFUNC);
  367. RETURN_OP_NAME(C_USERFUNC_POS);
  368. RETURN_OP_NAME(C_REF);
  369. // operators
  370. RETURN_OP_NAME(C_OP3);
  371. RETURN_OP_NAME(C_LOR);
  372. RETURN_OP_NAME(C_LAND);
  373. RETURN_OP_NAME(C_LE);
  374. RETURN_OP_NAME(C_LT);
  375. RETURN_OP_NAME(C_GE);
  376. RETURN_OP_NAME(C_GT);
  377. RETURN_OP_NAME(C_EQ);
  378. RETURN_OP_NAME(C_NE);
  379. RETURN_OP_NAME(C_XOR);
  380. RETURN_OP_NAME(C_OR);
  381. RETURN_OP_NAME(C_AND);
  382. RETURN_OP_NAME(C_ADD);
  383. RETURN_OP_NAME(C_SUB);
  384. RETURN_OP_NAME(C_MUL);
  385. RETURN_OP_NAME(C_DIV);
  386. RETURN_OP_NAME(C_MOD);
  387. RETURN_OP_NAME(C_NEG);
  388. RETURN_OP_NAME(C_LNOT);
  389. RETURN_OP_NAME(C_NOT);
  390. RETURN_OP_NAME(C_R_SHIFT);
  391. RETURN_OP_NAME(C_L_SHIFT);
  392. RETURN_OP_NAME(C_ADD_POST);
  393. RETURN_OP_NAME(C_SUB_POST);
  394. RETURN_OP_NAME(C_ADD_PRE);
  395. RETURN_OP_NAME(C_SUB_PRE);
  396. default:
  397. ShowDebug("script_op2name: unexpected op=%d\n", op);
  398. return "???";
  399. }
  400. #undef RETURN_OP_NAME
  401. }
  402. #ifdef DEBUG_DUMP_STACK
  403. static void script_dump_stack(struct script_state* st)
  404. {
  405. int i;
  406. ShowMessage("\tstart = %d\n", st->start);
  407. ShowMessage("\tend = %d\n", st->end);
  408. ShowMessage("\tdefsp = %d\n", st->stack->defsp);
  409. ShowMessage("\tsp = %d\n", st->stack->sp);
  410. for( i = 0; i < st->stack->sp; ++i )
  411. {
  412. struct script_data* data = &st->stack->stack_data[i];
  413. ShowMessage("\t[%d] %s", i, script_op2name(data->type));
  414. switch( data->type )
  415. {
  416. case C_INT:
  417. case C_POS:
  418. ShowMessage(" %d\n", data->u.num);
  419. break;
  420. case C_STR:
  421. case C_CONSTSTR:
  422. ShowMessage(" \"%s\"\n", data->u.str);
  423. break;
  424. case C_NAME:
  425. 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));
  426. break;
  427. case C_RETINFO:
  428. {
  429. struct script_retinfo* ri = data->u.ri;
  430. ShowMessage(" %p {scope.vars=%p, scope.arrays=%p, script=%p, pos=%d, nargs=%d, defsp=%d}\n", ri, ri->scope.vars, ri->scope.arrays, ri->script, ri->pos, ri->nargs, ri->defsp);
  431. }
  432. break;
  433. default:
  434. ShowMessage("\n");
  435. break;
  436. }
  437. }
  438. }
  439. #endif
  440. /// Reports on the console the src of a script error.
  441. static void script_reportsrc(struct script_state *st)
  442. {
  443. struct block_list* bl;
  444. if( st->oid == 0 )
  445. return; //Can't report source.
  446. bl = map_id2bl(st->oid);
  447. if( bl == NULL )
  448. return;
  449. switch( bl->type ) {
  450. case BL_NPC:
  451. if( bl->m >= 0 )
  452. ShowDebug("Source (NPC): %s at %s (%d,%d)\n", ((struct npc_data *)bl)->name, map[bl->m].name, bl->x, bl->y);
  453. else
  454. ShowDebug("Source (NPC): %s (invisible/not on a map)\n", ((struct npc_data *)bl)->name);
  455. break;
  456. default:
  457. if( bl->m >= 0 )
  458. 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);
  459. else
  460. ShowDebug("Source (Non-NPC type %d): name %s (invisible/not on a map)\n", bl->type, status_get_name(bl));
  461. break;
  462. }
  463. }
  464. /// Reports on the console information about the script data.
  465. static void script_reportdata(struct script_data* data)
  466. {
  467. if( data == NULL )
  468. return;
  469. switch( data->type ) {
  470. case C_NOP:// no value
  471. ShowDebug("Data: nothing (nil)\n");
  472. break;
  473. case C_INT:// number
  474. ShowDebug("Data: number value=%"PRId64"\n", data->u.num);
  475. break;
  476. case C_STR:
  477. case C_CONSTSTR:// string
  478. if( data->u.str ) {
  479. ShowDebug("Data: string value=\"%s\"\n", data->u.str);
  480. } else {
  481. ShowDebug("Data: string value=NULL\n");
  482. }
  483. break;
  484. case C_NAME:// reference
  485. if( reference_tovariable(data) ) {// variable
  486. const char* name = reference_getname(data);
  487. ShowDebug("Data: variable name='%s' index=%d\n", name, reference_getindex(data));
  488. } else if( reference_toconstant(data) ) {// constant
  489. ShowDebug("Data: constant name='%s' value=%d\n", reference_getname(data), reference_getconstant(data));
  490. } else if( reference_toparam(data) ) {// param
  491. ShowDebug("Data: param name='%s' type=%d\n", reference_getname(data), reference_getparamtype(data));
  492. } else {// ???
  493. ShowDebug("Data: reference name='%s' type=%s\n", reference_getname(data), script_op2name(data->type));
  494. ShowDebug("Please report this!!! - script->str_data.type=%s\n", script_op2name(str_data[reference_getid(data)].type));
  495. }
  496. break;
  497. case C_POS:// label
  498. ShowDebug("Data: label pos=%"PRId64"\n", data->u.num);
  499. break;
  500. default:
  501. ShowDebug("Data: %s\n", script_op2name(data->type));
  502. break;
  503. }
  504. }
  505. /// Reports on the console information about the current built-in function.
  506. static void script_reportfunc(struct script_state* st)
  507. {
  508. int params, id;
  509. struct script_data* data;
  510. if( !script_hasdata(st,0) )
  511. {// no stack
  512. return;
  513. }
  514. data = script_getdata(st,0);
  515. if( !data_isreference(data) || str_data[reference_getid(data)].type != C_FUNC )
  516. {// script currently not executing a built-in function or corrupt stack
  517. return;
  518. }
  519. id = reference_getid(data);
  520. params = script_lastdata(st)-1;
  521. if( params > 0 )
  522. {
  523. int i;
  524. ShowDebug("Function: %s (%d parameter%s):\n", get_str(id), params, ( params == 1 ) ? "" : "s");
  525. for( i = 2; i <= script_lastdata(st); i++ )
  526. {
  527. script_reportdata(script_getdata(st,i));
  528. }
  529. }
  530. else
  531. {
  532. ShowDebug("Function: %s (no parameters)\n", get_str(id));
  533. }
  534. }
  535. /*==========================================
  536. * Output error message
  537. *------------------------------------------*/
  538. static void disp_error_message2(const char *mes,const char *pos,int report)
  539. {
  540. error_msg = aStrdup(mes);
  541. error_pos = pos;
  542. error_report = report;
  543. longjmp( error_jump, 1 );
  544. }
  545. #define disp_error_message(mes,pos) disp_error_message2(mes,pos,1)
  546. static void disp_warning_message(const char *mes, const char *pos) {
  547. script_warning(parser_current_src,parser_current_file,parser_current_line,mes,pos);
  548. }
  549. /// Checks event parameter validity
  550. static void check_event(struct script_state *st, const char *evt)
  551. {
  552. if( evt && evt[0] && !stristr(evt, "::On") )
  553. {
  554. ShowWarning("NPC event parameter deprecated! Please use 'NPCNAME::OnEVENT' instead of '%s'.\n", evt);
  555. script_reportsrc(st);
  556. }
  557. }
  558. /*==========================================
  559. * Hashes the input string
  560. *------------------------------------------*/
  561. static unsigned int calc_hash(const char* p)
  562. {
  563. unsigned int h;
  564. #if defined(SCRIPT_HASH_DJB2)
  565. h = 5381;
  566. while( *p ) // hash*33 + c
  567. h = ( h << 5 ) + h + ((unsigned char)TOLOWER(*p++));
  568. #elif defined(SCRIPT_HASH_SDBM)
  569. h = 0;
  570. while( *p ) // hash*65599 + c
  571. h = ( h << 6 ) + ( h << 16 ) - h + ((unsigned char)TOLOWER(*p++));
  572. #elif defined(SCRIPT_HASH_ELF) // UNIX ELF hash
  573. h = 0;
  574. while( *p ){
  575. unsigned int g;
  576. h = ( h << 4 ) + ((unsigned char)TOLOWER(*p++));
  577. g = h & 0xF0000000;
  578. if( g )
  579. {
  580. h ^= g >> 24;
  581. h &= ~g;
  582. }
  583. }
  584. #else // athena hash
  585. h = 0;
  586. while( *p )
  587. h = ( h << 1 ) + ( h >> 3 ) + ( h >> 5 ) + ( h >> 8 ) + (unsigned char)TOLOWER(*p++);
  588. #endif
  589. return h % SCRIPT_HASH_SIZE;
  590. }
  591. /*==========================================
  592. * str_data manipulation functions
  593. *------------------------------------------*/
  594. /// Looks up string using the provided id.
  595. const char* get_str(int id)
  596. {
  597. Assert( id >= LABEL_START && id < str_size );
  598. return str_buf+str_data[id].str;
  599. }
  600. /// Returns the uid of the string, or -1.
  601. static int search_str(const char* p)
  602. {
  603. int i;
  604. for( i = str_hash[calc_hash(p)]; i != 0; i = str_data[i].next )
  605. if( strcasecmp(get_str(i),p) == 0 )
  606. return i;
  607. return -1;
  608. }
  609. /// Stores a copy of the string and returns its id.
  610. /// If an identical string is already present, returns its id instead.
  611. int add_str(const char* p)
  612. {
  613. int h;
  614. int len;
  615. h = calc_hash(p);
  616. if( str_hash[h] == 0 )
  617. {// empty bucket, add new node here
  618. str_hash[h] = str_num;
  619. }
  620. else
  621. {// scan for end of list, or occurence of identical string
  622. int i;
  623. for( i = str_hash[h]; ; i = str_data[i].next )
  624. {
  625. if( strcasecmp(get_str(i),p) == 0 )
  626. return i; // string already in list
  627. if( str_data[i].next == 0 )
  628. break; // reached the end
  629. }
  630. // append node to end of list
  631. str_data[i].next = str_num;
  632. }
  633. // grow list if neccessary
  634. if( str_num >= str_data_size )
  635. {
  636. str_data_size += 128;
  637. RECREATE(str_data,struct str_data_struct,str_data_size);
  638. memset(str_data + (str_data_size - 128), '\0', 128);
  639. }
  640. len=(int)strlen(p);
  641. // grow string buffer if neccessary
  642. while( str_pos+len+1 >= str_size )
  643. {
  644. str_size += 256;
  645. RECREATE(str_buf,char,str_size);
  646. memset(str_buf + (str_size - 256), '\0', 256);
  647. }
  648. safestrncpy(str_buf+str_pos, p, len+1);
  649. str_data[str_num].type = C_NOP;
  650. str_data[str_num].str = str_pos;
  651. str_data[str_num].next = 0;
  652. str_data[str_num].func = NULL;
  653. str_data[str_num].backpatch = -1;
  654. str_data[str_num].label = -1;
  655. str_pos += len+1;
  656. return str_num++;
  657. }
  658. /// Appends 1 byte to the script buffer.
  659. static void add_scriptb(int a)
  660. {
  661. if( script_pos+1 >= script_size )
  662. {
  663. script_size += SCRIPT_BLOCK_SIZE;
  664. RECREATE(script_buf,unsigned char,script_size);
  665. }
  666. script_buf[script_pos++] = (uint8)(a);
  667. }
  668. /// Appends a c_op value to the script buffer.
  669. /// The value is variable-length encoded into 8-bit blocks.
  670. /// The encoding scheme is ( 01?????? )* 00??????, LSB first.
  671. /// All blocks but the last hold 7 bits of data, topmost bit is always 1 (carries).
  672. static void add_scriptc(int a)
  673. {
  674. while( a >= 0x40 )
  675. {
  676. add_scriptb((a&0x3f)|0x40);
  677. a = (a - 0x40) >> 6;
  678. }
  679. add_scriptb(a);
  680. }
  681. /// Appends an integer value to the script buffer.
  682. /// The value is variable-length encoded into 8-bit blocks.
  683. /// The encoding scheme is ( 11?????? )* 10??????, LSB first.
  684. /// All blocks but the last hold 7 bits of data, topmost bit is always 1 (carries).
  685. static void add_scripti(int a)
  686. {
  687. while( a >= 0x40 )
  688. {
  689. add_scriptb((a&0x3f)|0xc0);
  690. a = (a - 0x40) >> 6;
  691. }
  692. add_scriptb(a|0x80);
  693. }
  694. /// Appends a str_data object (label/function/variable/integer) to the script buffer.
  695. ///
  696. /// @param l The id of the str_data entry
  697. // Maximum up to 16M
  698. static void add_scriptl(int l)
  699. {
  700. int backpatch = str_data[l].backpatch;
  701. switch(str_data[l].type){
  702. case C_POS:
  703. case C_USERFUNC_POS:
  704. add_scriptc(C_POS);
  705. add_scriptb(str_data[l].label);
  706. add_scriptb(str_data[l].label>>8);
  707. add_scriptb(str_data[l].label>>16);
  708. break;
  709. case C_NOP:
  710. case C_USERFUNC:
  711. // Embedded data backpatch there is a possibility of label
  712. add_scriptc(C_NAME);
  713. str_data[l].backpatch = script_pos;
  714. add_scriptb(backpatch);
  715. add_scriptb(backpatch>>8);
  716. add_scriptb(backpatch>>16);
  717. break;
  718. case C_INT:
  719. add_scripti(abs(str_data[l].val));
  720. if( str_data[l].val < 0 ) //Notice that this is negative, from jA (Rayce)
  721. add_scriptc(C_NEG);
  722. break;
  723. default: // assume C_NAME
  724. add_scriptc(C_NAME);
  725. add_scriptb(l);
  726. add_scriptb(l>>8);
  727. add_scriptb(l>>16);
  728. break;
  729. }
  730. }
  731. /*==========================================
  732. * Resolve the label
  733. *------------------------------------------*/
  734. void set_label(int l,int pos, const char* script_pos_cur)
  735. {
  736. int i;
  737. if(str_data[l].type==C_INT || str_data[l].type==C_PARAM || str_data[l].type==C_FUNC)
  738. { //Prevent overwriting constants values, parameters and built-in functions [Skotlex]
  739. disp_error_message("set_label: invalid label name",script_pos_cur);
  740. return;
  741. }
  742. if(str_data[l].label!=-1){
  743. disp_error_message("set_label: dup label ",script_pos_cur);
  744. return;
  745. }
  746. str_data[l].type=(str_data[l].type == C_USERFUNC ? C_USERFUNC_POS : C_POS);
  747. str_data[l].label=pos;
  748. for(i=str_data[l].backpatch;i>=0 && i!=0x00ffffff;){
  749. int next=GETVALUE(script_buf,i);
  750. script_buf[i-1]=(str_data[l].type == C_USERFUNC ? C_USERFUNC_POS : C_POS);
  751. SETVALUE(script_buf,i,pos);
  752. i=next;
  753. }
  754. }
  755. /// Skips spaces and/or comments.
  756. const char* skip_space(const char* p)
  757. {
  758. if( p == NULL )
  759. return NULL;
  760. for(;;)
  761. {
  762. while( ISSPACE(*p) )
  763. ++p;
  764. if( *p == '/' && p[1] == '/' )
  765. {// line comment
  766. while(*p && *p!='\n')
  767. ++p;
  768. }
  769. else if( *p == '/' && p[1] == '*' )
  770. {// block comment
  771. p += 2;
  772. for(;;)
  773. {
  774. if( *p == '\0' ) {
  775. disp_warning_message("script:script->skip_space: end of file while parsing block comment. expected "CL_BOLD"*/"CL_NORM, p);
  776. return p;
  777. }
  778. if( *p == '*' && p[1] == '/' )
  779. {// end of block comment
  780. p += 2;
  781. break;
  782. }
  783. ++p;
  784. }
  785. }
  786. else
  787. break;
  788. }
  789. return p;
  790. }
  791. /// Skips a word.
  792. /// A word consists of undercores and/or alphanumeric characters,
  793. /// and valid variable prefixes/postfixes.
  794. static const char* skip_word(const char* p)
  795. {
  796. // prefix
  797. switch( *p ) {
  798. case '@':// temporary char variable
  799. ++p; break;
  800. case '#':// account variable
  801. p += ( p[1] == '#' ? 2 : 1 ); break;
  802. case '\'':// instance variable
  803. ++p; break;
  804. case '.':// npc variable
  805. p += ( p[1] == '@' ? 2 : 1 ); break;
  806. case '$':// global variable
  807. p += ( p[1] == '@' ? 2 : 1 ); break;
  808. }
  809. while( ISALNUM(*p) || *p == '_' )
  810. ++p;
  811. // postfix
  812. if( *p == '$' )// string
  813. p++;
  814. return p;
  815. }
  816. /// Adds a word to str_data.
  817. /// @see skip_word
  818. /// @see add_str
  819. static int add_word(const char* p)
  820. {
  821. char* word;
  822. int len;
  823. int i;
  824. // Check for a word
  825. len = skip_word(p) - p;
  826. if( len == 0 )
  827. disp_error_message("script:add_word: invalid word. A word consists of undercores and/or alphanumeric characters, and valid variable prefixes/postfixes.", p);
  828. // Duplicate the word
  829. word = (char*)aMalloc(len+1);
  830. memcpy(word, p, len);
  831. word[len] = 0;
  832. // add the word
  833. i = add_str(word);
  834. aFree(word);
  835. return i;
  836. }
  837. /// Parses a function call.
  838. /// The argument list can have parenthesis or not.
  839. /// The number of arguments is checked.
  840. static
  841. const char* parse_callfunc(const char* p, int require_paren, int is_custom)
  842. {
  843. const char* p2;
  844. const char* arg=NULL;
  845. int func;
  846. func = add_word(p);
  847. if( str_data[func].type == C_FUNC ){
  848. // buildin function
  849. add_scriptl(func);
  850. add_scriptc(C_ARG);
  851. arg = buildin_func[str_data[func].val].arg;
  852. } else if( str_data[func].type == C_USERFUNC || str_data[func].type == C_USERFUNC_POS ){
  853. // script defined function
  854. add_scriptl(buildin_callsub_ref);
  855. add_scriptc(C_ARG);
  856. add_scriptl(func);
  857. arg = buildin_func[str_data[buildin_callsub_ref].val].arg;
  858. if( *arg == 0 )
  859. disp_error_message("parse_callfunc: callsub has no arguments, please review its definition",p);
  860. if( *arg != '*' )
  861. ++arg; // count func as argument
  862. } else {
  863. #ifdef SCRIPT_CALLFUNC_CHECK
  864. const char* name = get_str(func);
  865. if( !is_custom && strdb_get(userfunc_db, name) == NULL ) {
  866. #endif
  867. disp_error_message("parse_line: expect command, missing function name or calling undeclared function",p);
  868. #ifdef SCRIPT_CALLFUNC_CHECK
  869. } else {;
  870. add_scriptl(buildin_callfunc_ref);
  871. add_scriptc(C_ARG);
  872. add_scriptc(C_STR);
  873. while( *name ) add_scriptb(*name ++);
  874. add_scriptb(0);
  875. arg = buildin_func[str_data[buildin_callfunc_ref].val].arg;
  876. if( *arg != '*' ) ++ arg;
  877. }
  878. #endif
  879. }
  880. p = skip_word(p);
  881. p = skip_space(p);
  882. syntax.curly[syntax.curly_count].type = TYPE_ARGLIST;
  883. syntax.curly[syntax.curly_count].count = 0;
  884. if( *p == ';' )
  885. {// <func name> ';'
  886. syntax.curly[syntax.curly_count].flag = ARGLIST_NO_PAREN;
  887. } else if( *p == '(' && *(p2=skip_space(p+1)) == ')' )
  888. {// <func name> '(' ')'
  889. syntax.curly[syntax.curly_count].flag = ARGLIST_PAREN;
  890. p = p2;
  891. /*
  892. } else if( 0 && require_paren && *p != '(' )
  893. {// <func name>
  894. syntax.curly[syntax.curly_count].flag = ARGLIST_NO_PAREN;
  895. */
  896. } else
  897. {// <func name> <arg list>
  898. if( require_paren ){
  899. if( *p != '(' )
  900. disp_error_message("need '('",p);
  901. ++p; // skip '('
  902. syntax.curly[syntax.curly_count].flag = ARGLIST_PAREN;
  903. } else if( *p == '(' ){
  904. syntax.curly[syntax.curly_count].flag = ARGLIST_UNDEFINED;
  905. } else {
  906. syntax.curly[syntax.curly_count].flag = ARGLIST_NO_PAREN;
  907. }
  908. ++syntax.curly_count;
  909. while( *arg ) {
  910. p2=parse_subexpr(p,-1);
  911. if( p == p2 )
  912. break; // not an argument
  913. if( *arg != '*' )
  914. ++arg; // next argument
  915. p=skip_space(p2);
  916. if( *arg == 0 || *p != ',' )
  917. break; // no more arguments
  918. ++p; // skip comma
  919. }
  920. --syntax.curly_count;
  921. }
  922. if( *arg && *arg != '?' && *arg != '*' )
  923. disp_error_message2("parse_callfunc: not enough arguments, expected ','", p, script_config.warn_func_mismatch_paramnum);
  924. if( syntax.curly[syntax.curly_count].type != TYPE_ARGLIST )
  925. disp_error_message("parse_callfunc: DEBUG last curly is not an argument list",p);
  926. if( syntax.curly[syntax.curly_count].flag == ARGLIST_PAREN ){
  927. if( *p != ')' )
  928. disp_error_message("parse_callfunc: expected ')' to close argument list",p);
  929. ++p;
  930. }
  931. add_scriptc(C_FUNC);
  932. return p;
  933. }
  934. /// Processes end of logical script line.
  935. /// @param first When true, only fix up scheduling data is initialized
  936. /// @param p Script position for error reporting in set_label
  937. static void parse_nextline(bool first, const char* p)
  938. {
  939. if( !first )
  940. {
  941. add_scriptc(C_EOL); // mark end of line for stack cleanup
  942. set_label(LABEL_NEXTLINE, script_pos, p); // fix up '-' labels
  943. }
  944. // initialize data for new '-' label fix up scheduling
  945. str_data[LABEL_NEXTLINE].type = C_NOP;
  946. str_data[LABEL_NEXTLINE].backpatch = -1;
  947. str_data[LABEL_NEXTLINE].label = -1;
  948. }
  949. /**
  950. * Pushes a variable into stack, processing its array index if needed.
  951. * @see parse_variable
  952. */
  953. void parse_variable_sub_push(int word, const char *p2)
  954. {
  955. if( p2 ) { // Process the variable index
  956. const char *p3 = NULL;
  957. // Push the getelementofarray method into the stack
  958. add_scriptl(buildin_getelementofarray_ref);
  959. add_scriptc(C_ARG);
  960. add_scriptl(word);
  961. // Process the sub-expression for this assignment
  962. p3 = parse_subexpr(p2 + 1, 1);
  963. p3 = skip_space(p3);
  964. if( *p3 != ']' ) // Closing parenthesis is required for this script
  965. disp_error_message("Missing closing ']' parenthesis for the variable assignment.", p3);
  966. // Push the closing function stack operator onto the stack
  967. add_scriptc(C_FUNC);
  968. p3++;
  969. } else // No array index, simply push the variable or value onto the stack
  970. add_scriptl(word);
  971. }
  972. /// Parse a variable assignment using the direct equals operator
  973. /// @param p script position where the function should run from
  974. /// @return NULL if not a variable assignment, the new position otherwise
  975. const char* parse_variable(const char* p) {
  976. int word;
  977. c_op type = C_NOP;
  978. const char *p2 = NULL;
  979. const char *var = p;
  980. if ((p[0] == '+' && p[1] == '+' && (type = C_ADD_PRE)) // pre ++
  981. || (p[0] == '-' && p[1] == '-' && (type = C_SUB_PRE))) // pre --
  982. var = p = skip_space(&p[2]);
  983. // skip the variable where applicable
  984. p = skip_word(p);
  985. p = skip_space(p);
  986. if( p == NULL ) {// end of the line or invalid buffer
  987. return NULL;
  988. }
  989. if( *p == '[' ) {// array variable so process the array as appropriate
  990. int i,j;
  991. for( p2 = p, i = 0, j = 1; p; ++ i ) {
  992. if( *p ++ == ']' && --(j) == 0 ) break;
  993. if( *p == '[' ) ++ j;
  994. }
  995. if( !(p = skip_space(p)) ) {// end of line or invalid characters remaining
  996. disp_error_message("Missing right expression or closing bracket for variable.", p);
  997. }
  998. }
  999. if( type == C_NOP &&
  1000. !( ( p[0] == '=' && p[1] != '=' && (type = C_EQ) ) // =
  1001. || ( p[0] == '+' && p[1] == '=' && (type = C_ADD) ) // +=
  1002. || ( p[0] == '-' && p[1] == '=' && (type = C_SUB) ) // -=
  1003. || ( p[0] == '^' && p[1] == '=' && (type = C_XOR) ) // ^=
  1004. || ( p[0] == '|' && p[1] == '=' && (type = C_OR ) ) // |=
  1005. || ( p[0] == '&' && p[1] == '=' && (type = C_AND) ) // &=
  1006. || ( p[0] == '*' && p[1] == '=' && (type = C_MUL) ) // *=
  1007. || ( p[0] == '/' && p[1] == '=' && (type = C_DIV) ) // /=
  1008. || ( p[0] == '%' && p[1] == '=' && (type = C_MOD) ) // %=
  1009. || ( p[0] == '~' && p[1] == '=' && (type = C_NOT) ) // ~=
  1010. || ( p[0] == '+' && p[1] == '+' && (type = C_ADD_POST) ) // post ++
  1011. || ( p[0] == '-' && p[1] == '-' && (type = C_SUB_POST) ) // post --
  1012. || ( p[0] == '<' && p[1] == '<' && p[2] == '=' && (type = C_L_SHIFT) ) // <<=
  1013. || ( p[0] == '>' && p[1] == '>' && p[2] == '=' && (type = C_R_SHIFT) ) // >>=
  1014. ) )
  1015. {// failed to find a matching operator combination so invalid
  1016. return NULL;
  1017. }
  1018. switch( type ) {
  1019. case C_ADD_PRE: // pre ++
  1020. case C_SUB_PRE: // pre --
  1021. // Nothing more to skip
  1022. break;
  1023. case C_EQ: {// incremental modifier
  1024. p = skip_space( &p[1] );
  1025. }
  1026. break;
  1027. case C_L_SHIFT:
  1028. case C_R_SHIFT: {// left or right shift modifier
  1029. p = skip_space( &p[3] );
  1030. }
  1031. break;
  1032. default: {// normal incremental command
  1033. p = skip_space( &p[2] );
  1034. }
  1035. }
  1036. if( p == NULL ) {// end of line or invalid buffer
  1037. return NULL;
  1038. }
  1039. // push the set function onto the stack
  1040. add_scriptl(buildin_set_ref);
  1041. add_scriptc(C_ARG);
  1042. // always append parenthesis to avoid errors
  1043. syntax.curly[syntax.curly_count].type = TYPE_ARGLIST;
  1044. syntax.curly[syntax.curly_count].count = 0;
  1045. syntax.curly[syntax.curly_count].flag = ARGLIST_PAREN;
  1046. // increment the total curly count for the position in the script
  1047. ++ syntax.curly_count;
  1048. // parse the variable currently being modified
  1049. word = add_word(var);
  1050. if( str_data[word].type == C_FUNC || str_data[word].type == C_USERFUNC || str_data[word].type == C_USERFUNC_POS ) // cannot assign a variable which exists as a function or label
  1051. disp_error_message("Cannot modify a variable which has the same name as a function or label.", p);
  1052. parse_variable_sub_push(word, p2); // Push variable onto the stack
  1053. if( type != C_EQ )
  1054. parse_variable_sub_push(word, p2); // Push variable onto the stack once again (first argument of setr)
  1055. if( type == C_ADD_POST || type == C_SUB_POST ) { // post ++ / --
  1056. add_scripti(1);
  1057. add_scriptc(type == C_ADD_POST ? C_ADD : C_SUB);
  1058. parse_variable_sub_push(word, p2); // Push variable onto the stack (third argument of setr)
  1059. } else if( type == C_ADD_PRE || type == C_SUB_PRE ) { // pre ++ / --
  1060. add_scripti(1);
  1061. add_scriptc(type == C_ADD_PRE ? C_ADD : C_SUB);
  1062. } else { // Process the value as an expression
  1063. p = parse_subexpr(p, -1);
  1064. if( type != C_EQ )
  1065. {// push the type of modifier onto the stack
  1066. add_scriptc(type);
  1067. }
  1068. }
  1069. // decrement the curly count for the position within the script
  1070. -- syntax.curly_count;
  1071. // close the script by appending the function operator
  1072. add_scriptc(C_FUNC);
  1073. // push the buffer from the method
  1074. return p;
  1075. }
  1076. /*
  1077. * Checks whether the gives string is a number literal
  1078. *
  1079. * Mainly necessary to differentiate between number literals and NPC name
  1080. * constants, since several of those start with a digit.
  1081. *
  1082. * All this does is to check if the string begins with an optional + or - sign,
  1083. * followed by a hexadecimal or decimal number literal literal and is NOT
  1084. * followed by a underscore or letter.
  1085. *
  1086. * @author : Hercules.ws
  1087. * @param p Pointer to the string to check
  1088. * @return Whether the string is a number literal
  1089. */
  1090. bool is_number(const char *p) {
  1091. const char *np;
  1092. if (!p)
  1093. return false;
  1094. if (*p == '-' || *p == '+')
  1095. p++;
  1096. np = p;
  1097. if (*p == '0' && p[1] == 'x') {
  1098. p+=2;
  1099. np = p;
  1100. // Hexadecimal
  1101. while (ISXDIGIT(*np))
  1102. np++;
  1103. } else {
  1104. // Decimal
  1105. while (ISDIGIT(*np))
  1106. np++;
  1107. }
  1108. if (p != np && *np != '_' && !ISALPHA(*np)) // At least one digit, and next isn't a letter or _
  1109. return true;
  1110. return false;
  1111. }
  1112. /*==========================================
  1113. * Analysis section
  1114. *------------------------------------------*/
  1115. const char* parse_simpleexpr(const char *p)
  1116. {
  1117. long long i;
  1118. p=skip_space(p);
  1119. if(*p==';' || *p==',')
  1120. disp_error_message("parse_simpleexpr: unexpected end of expression",p);
  1121. if(*p=='('){
  1122. if( (i=syntax.curly_count-1) >= 0 && syntax.curly[i].type == TYPE_ARGLIST )
  1123. ++syntax.curly[i].count;
  1124. p=parse_subexpr(p+1,-1);
  1125. p=skip_space(p);
  1126. if( (i=syntax.curly_count-1) >= 0 && syntax.curly[i].type == TYPE_ARGLIST &&
  1127. syntax.curly[i].flag == ARGLIST_UNDEFINED && --syntax.curly[i].count == 0
  1128. ){
  1129. if( *p == ',' ){
  1130. syntax.curly[i].flag = ARGLIST_PAREN;
  1131. return p;
  1132. } else
  1133. syntax.curly[i].flag = ARGLIST_NO_PAREN;
  1134. }
  1135. if( *p != ')' )
  1136. disp_error_message("parse_simpleexpr: unmatched ')'",p);
  1137. ++p;
  1138. } else if(is_number(p)) {
  1139. char *np;
  1140. while(*p == '0' && ISDIGIT(p[1])) p++;
  1141. i=strtoll(p,&np,0);
  1142. if( i < INT_MIN ) {
  1143. i = INT_MIN;
  1144. disp_warning_message("parse_simpleexpr: underflow detected, capping value to INT_MIN",p);
  1145. } else if( i > INT_MAX ) {
  1146. i = INT_MAX;
  1147. disp_warning_message("parse_simpleexpr: overflow detected, capping value to INT_MAX",p);
  1148. }
  1149. add_scripti((int)i);
  1150. p=np;
  1151. } else if(*p=='"'){
  1152. add_scriptc(C_STR);
  1153. p++;
  1154. while( *p && *p != '"' ){
  1155. if( (unsigned char)p[-1] <= 0x7e && *p == '\\' )
  1156. {
  1157. char buf[8];
  1158. size_t len = skip_escaped_c(p) - p;
  1159. size_t n = sv_unescape_c(buf, p, len);
  1160. if( n != 1 )
  1161. ShowDebug("parse_simpleexpr: unexpected length %d after unescape (\"%.*s\" -> %.*s)\n", (int)n, (int)len, p, (int)n, buf);
  1162. p += len;
  1163. add_scriptb(*buf);
  1164. continue;
  1165. }
  1166. else if( *p == '\n' )
  1167. disp_error_message("parse_simpleexpr: unexpected newline @ string",p);
  1168. add_scriptb(*p++);
  1169. }
  1170. if(!*p)
  1171. disp_error_message("parse_simpleexpr: unexpected eof @ string",p);
  1172. add_scriptb(0);
  1173. p++; //'"'
  1174. } else {
  1175. int l;
  1176. const char* pv;
  1177. // label , register , function etc
  1178. if(skip_word(p)==p)
  1179. disp_error_message("parse_simpleexpr: unexpected character",p);
  1180. l=add_word(p);
  1181. if( str_data[l].type == C_FUNC || str_data[l].type == C_USERFUNC || str_data[l].type == C_USERFUNC_POS)
  1182. return parse_callfunc(p,1,0);
  1183. #ifdef SCRIPT_CALLFUNC_CHECK
  1184. else {
  1185. const char* name = get_str(l);
  1186. if( strdb_get(userfunc_db,name) != NULL ) {
  1187. return parse_callfunc(p,1,1);
  1188. }
  1189. }
  1190. #endif
  1191. if( (pv = parse_variable(p)) )
  1192. {// successfully processed a variable assignment
  1193. return pv;
  1194. }
  1195. p=skip_word(p);
  1196. if( *p == '[' ){
  1197. // array(name[i] => getelementofarray(name,i) )
  1198. add_scriptl(buildin_getelementofarray_ref);
  1199. add_scriptc(C_ARG);
  1200. add_scriptl(l);
  1201. p=parse_subexpr(p+1,-1);
  1202. p=skip_space(p);
  1203. if( *p != ']' )
  1204. disp_error_message("parse_simpleexpr: unmatched ']'",p);
  1205. ++p;
  1206. add_scriptc(C_FUNC);
  1207. }else
  1208. add_scriptl(l);
  1209. }
  1210. return p;
  1211. }
  1212. /*==========================================
  1213. * Analysis of the expression
  1214. *------------------------------------------*/
  1215. const char* parse_subexpr(const char* p,int limit)
  1216. {
  1217. int op,opl,len;
  1218. p=skip_space(p);
  1219. if( *p == '-' ){
  1220. const char* tmpp = skip_space(p+1);
  1221. if( *tmpp == ';' || *tmpp == ',' ){
  1222. add_scriptl(LABEL_NEXTLINE);
  1223. p++;
  1224. return p;
  1225. }
  1226. }
  1227. if( (op = C_ADD_PRE, p[0] == '+' && p[1] == '+') || (op = C_SUB_PRE, p[0] == '-' && p[1] == '-') ) // Pre ++ -- operators
  1228. p = parse_variable(p);
  1229. else if( (op = C_NEG, *p == '-') || (op = C_LNOT, *p == '!') || (op = C_NOT, *p == '~') ) { // Unary - ! ~ operators
  1230. p = parse_subexpr(p + 1, 11);
  1231. add_scriptc(op);
  1232. } else
  1233. p = parse_simpleexpr(p);
  1234. p = skip_space(p);
  1235. while((
  1236. (op=C_OP3,opl=0,len=1,*p=='?') ||
  1237. (op=C_ADD,opl=9,len=1,*p=='+') ||
  1238. (op=C_SUB,opl=9,len=1,*p=='-') ||
  1239. (op=C_MUL,opl=10,len=1,*p=='*') ||
  1240. (op=C_DIV,opl=10,len=1,*p=='/') ||
  1241. (op=C_MOD,opl=10,len=1,*p=='%') ||
  1242. (op=C_LAND,opl=2,len=2,*p=='&' && p[1]=='&') ||
  1243. (op=C_AND,opl=5,len=1,*p=='&') ||
  1244. (op=C_LOR,opl=1,len=2,*p=='|' && p[1]=='|') ||
  1245. (op=C_OR,opl=3,len=1,*p=='|') ||
  1246. (op=C_XOR,opl=4,len=1,*p=='^') ||
  1247. (op=C_EQ,opl=6,len=2,*p=='=' && p[1]=='=') ||
  1248. (op=C_NE,opl=6,len=2,*p=='!' && p[1]=='=') ||
  1249. (op=C_R_SHIFT,opl=8,len=2,*p=='>' && p[1]=='>') ||
  1250. (op=C_GE,opl=7,len=2,*p=='>' && p[1]=='=') ||
  1251. (op=C_GT,opl=7,len=1,*p=='>') ||
  1252. (op=C_L_SHIFT,opl=8,len=2,*p=='<' && p[1]=='<') ||
  1253. (op=C_LE,opl=7,len=2,*p=='<' && p[1]=='=') ||
  1254. (op=C_LT,opl=7,len=1,*p=='<')) && opl>limit){
  1255. p+=len;
  1256. if(op == C_OP3) {
  1257. p=parse_subexpr(p,-1);
  1258. p=skip_space(p);
  1259. if( *(p++) != ':')
  1260. disp_error_message("parse_subexpr: expected ':'", p-1);
  1261. p=parse_subexpr(p,-1);
  1262. } else {
  1263. p=parse_subexpr(p,opl);
  1264. }
  1265. add_scriptc(op);
  1266. p=skip_space(p);
  1267. }
  1268. return p; /* return first untreated operator */
  1269. }
  1270. /*==========================================
  1271. * Evaluation of the expression
  1272. *------------------------------------------*/
  1273. const char* parse_expr(const char *p)
  1274. {
  1275. switch(*p){
  1276. case ')': case ';': case ':': case '[': case ']':
  1277. case '}':
  1278. disp_error_message("parse_expr: unexpected character",p);
  1279. }
  1280. p=parse_subexpr(p,-1);
  1281. return p;
  1282. }
  1283. /*==========================================
  1284. * Analysis of the line
  1285. *------------------------------------------*/
  1286. const char* parse_line(const char* p)
  1287. {
  1288. const char* p2;
  1289. p=skip_space(p);
  1290. if(*p==';') {
  1291. //Close decision for if(); for(); while();
  1292. p = parse_syntax_close(p + 1);
  1293. return p;
  1294. }
  1295. if(*p==')' && parse_syntax_for_flag)
  1296. return p+1;
  1297. p = skip_space(p);
  1298. if(p[0] == '{') {
  1299. syntax.curly[syntax.curly_count].type = TYPE_NULL;
  1300. syntax.curly[syntax.curly_count].count = -1;
  1301. syntax.curly[syntax.curly_count].index = -1;
  1302. syntax.curly_count++;
  1303. return p + 1;
  1304. } else if(p[0] == '}') {
  1305. return parse_curly_close(p);
  1306. }
  1307. // Syntax-related processing
  1308. p2 = parse_syntax(p);
  1309. if(p2 != NULL)
  1310. return p2;
  1311. // attempt to process a variable assignment
  1312. p2 = parse_variable(p);
  1313. if( p2 != NULL )
  1314. {// variable assignment processed so leave the method
  1315. return parse_syntax_close(p2 + 1);
  1316. }
  1317. p = parse_callfunc(p,0,0);
  1318. p = skip_space(p);
  1319. if(parse_syntax_for_flag) {
  1320. if( *p != ')' )
  1321. disp_error_message("parse_line: expected ')'",p);
  1322. } else {
  1323. if( *p != ';' )
  1324. disp_error_message("parse_line: expected ';'",p);
  1325. }
  1326. //Binding decision for if(), for(), while()
  1327. p = parse_syntax_close(p+1);
  1328. return p;
  1329. }
  1330. // { ... } Closing process
  1331. const char* parse_curly_close(const char* p)
  1332. {
  1333. if(syntax.curly_count <= 0) {
  1334. disp_error_message("parse_curly_close: unexpected string",p);
  1335. return p + 1;
  1336. } else if(syntax.curly[syntax.curly_count-1].type == TYPE_NULL) {
  1337. syntax.curly_count--;
  1338. //Close decision if, for , while
  1339. p = parse_syntax_close(p + 1);
  1340. return p;
  1341. } else if(syntax.curly[syntax.curly_count-1].type == TYPE_SWITCH) {
  1342. //Closing switch()
  1343. int pos = syntax.curly_count-1;
  1344. char label[256];
  1345. int l;
  1346. // Remove temporary variables
  1347. sprintf(label,"set $@__SW%x_VAL,0;",syntax.curly[pos].index);
  1348. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1349. parse_line(label);
  1350. syntax.curly_count--;
  1351. // Go to the end pointer unconditionally
  1352. sprintf(label,"goto __SW%x_FIN;",syntax.curly[pos].index);
  1353. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1354. parse_line(label);
  1355. syntax.curly_count--;
  1356. // You are here labeled
  1357. sprintf(label,"__SW%x_%x",syntax.curly[pos].index,syntax.curly[pos].count);
  1358. l=add_str(label);
  1359. set_label(l,script_pos, p);
  1360. if(syntax.curly[pos].flag) {
  1361. //Exists default
  1362. sprintf(label,"goto __SW%x_DEF;",syntax.curly[pos].index);
  1363. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1364. parse_line(label);
  1365. syntax.curly_count--;
  1366. }
  1367. // Label end
  1368. sprintf(label,"__SW%x_FIN",syntax.curly[pos].index);
  1369. l=add_str(label);
  1370. set_label(l,script_pos, p);
  1371. linkdb_final(&syntax.curly[pos].case_label); // free the list of case label
  1372. syntax.curly_count--;
  1373. //Closing decision if, for , while
  1374. p = parse_syntax_close(p + 1);
  1375. return p;
  1376. } else {
  1377. disp_error_message("parse_curly_close: unexpected string",p);
  1378. return p + 1;
  1379. }
  1380. }
  1381. // Syntax-related processing
  1382. // break, case, continue, default, do, for, function,
  1383. // if, switch, while ? will handle this internally.
  1384. const char* parse_syntax(const char* p)
  1385. {
  1386. const char *p2 = skip_word(p);
  1387. switch(*p) {
  1388. case 'B':
  1389. case 'b':
  1390. if(p2 - p == 5 && !strncasecmp(p,"break",5)) {
  1391. // break Processing
  1392. char label[256];
  1393. int pos = syntax.curly_count - 1;
  1394. while(pos >= 0) {
  1395. if(syntax.curly[pos].type == TYPE_DO) {
  1396. sprintf(label,"goto __DO%x_FIN;",syntax.curly[pos].index);
  1397. break;
  1398. } else if(syntax.curly[pos].type == TYPE_FOR) {
  1399. sprintf(label,"goto __FR%x_FIN;",syntax.curly[pos].index);
  1400. break;
  1401. } else if(syntax.curly[pos].type == TYPE_WHILE) {
  1402. sprintf(label,"goto __WL%x_FIN;",syntax.curly[pos].index);
  1403. break;
  1404. } else if(syntax.curly[pos].type == TYPE_SWITCH) {
  1405. sprintf(label,"goto __SW%x_FIN;",syntax.curly[pos].index);
  1406. break;
  1407. }
  1408. pos--;
  1409. }
  1410. if(pos < 0) {
  1411. disp_error_message("parse_syntax: unexpected 'break'",p);
  1412. } else {
  1413. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1414. parse_line(label);
  1415. syntax.curly_count--;
  1416. }
  1417. p = skip_space(p2);
  1418. if(*p != ';')
  1419. disp_error_message("parse_syntax: expected ';'",p);
  1420. // Closing decision if, for , while
  1421. p = parse_syntax_close(p + 1);
  1422. return p;
  1423. }
  1424. break;
  1425. case 'c':
  1426. case 'C':
  1427. if(p2 - p == 4 && !strncasecmp(p,"case",4)) {
  1428. //Processing case
  1429. int pos = syntax.curly_count-1;
  1430. if(pos < 0 || syntax.curly[pos].type != TYPE_SWITCH) {
  1431. disp_error_message("parse_syntax: unexpected 'case' ",p);
  1432. return p+1;
  1433. } else {
  1434. char label[256];
  1435. int l,v;
  1436. char *np;
  1437. if(syntax.curly[pos].count != 1) {
  1438. //Jump for FALLTHRU
  1439. sprintf(label,"goto __SW%x_%xJ;",syntax.curly[pos].index,syntax.curly[pos].count);
  1440. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1441. parse_line(label);
  1442. syntax.curly_count--;
  1443. // You are here labeled
  1444. sprintf(label,"__SW%x_%x",syntax.curly[pos].index,syntax.curly[pos].count);
  1445. l=add_str(label);
  1446. set_label(l,script_pos, p);
  1447. }
  1448. //Decision statement switch
  1449. p = skip_space(p2);
  1450. if(p == p2) {
  1451. disp_error_message("parse_syntax: expected a space ' '",p);
  1452. }
  1453. // check whether case label is integer or not
  1454. if(is_number(p)) {
  1455. //Numeric value
  1456. v = (int)strtol(p,&np,0);
  1457. if((*p == '-' || *p == '+') && ISDIGIT(p[1])) // pre-skip because '-' can not skip_word
  1458. p++;
  1459. p = skip_word(p);
  1460. if(np != p)
  1461. disp_error_message("parse_syntax: 'case' label is not an integer",np);
  1462. } else {
  1463. //Check for constants
  1464. p2 = skip_word(p);
  1465. v = (int)(size_t) (p2-p); // length of word at p2
  1466. memcpy(label,p,v);
  1467. label[v]='\0';
  1468. if( !script_get_constant(label, &v) )
  1469. disp_error_message("parse_syntax: 'case' label is not an integer",p);
  1470. p = skip_word(p);
  1471. }
  1472. p = skip_space(p);
  1473. if(*p != ':')
  1474. disp_error_message("parse_syntax: expect ':'",p);
  1475. sprintf(label,"if(%d != $@__SW%x_VAL) goto __SW%x_%x;",
  1476. v,syntax.curly[pos].index,syntax.curly[pos].index,syntax.curly[pos].count+1);
  1477. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1478. // Bad I do not parse twice
  1479. p2 = parse_line(label);
  1480. parse_line(p2);
  1481. syntax.curly_count--;
  1482. if(syntax.curly[pos].count != 1) {
  1483. // Label after the completion of FALLTHRU
  1484. sprintf(label,"__SW%x_%xJ",syntax.curly[pos].index,syntax.curly[pos].count);
  1485. l=add_str(label);
  1486. set_label(l,script_pos,p);
  1487. }
  1488. // check duplication of case label [Rayce]
  1489. if(linkdb_search(&syntax.curly[pos].case_label, (void*)__64BPRTSIZE(v)) != NULL)
  1490. disp_error_message("parse_syntax: dup 'case'",p);
  1491. linkdb_insert(&syntax.curly[pos].case_label, (void*)__64BPRTSIZE(v), (void*)1);
  1492. sprintf(label,"set $@__SW%x_VAL,0;",syntax.curly[pos].index);
  1493. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1494. parse_line(label);
  1495. syntax.curly_count--;
  1496. syntax.curly[pos].count++;
  1497. }
  1498. return p + 1;
  1499. } else if(p2 - p == 8 && !strncasecmp(p,"continue",8)) {
  1500. // Processing continue
  1501. char label[256];
  1502. int pos = syntax.curly_count - 1;
  1503. while(pos >= 0) {
  1504. if(syntax.curly[pos].type == TYPE_DO) {
  1505. sprintf(label,"goto __DO%x_NXT;",syntax.curly[pos].index);
  1506. syntax.curly[pos].flag = 1; //Flag put the link for continue
  1507. break;
  1508. } else if(syntax.curly[pos].type == TYPE_FOR) {
  1509. sprintf(label,"goto __FR%x_NXT;",syntax.curly[pos].index);
  1510. break;
  1511. } else if(syntax.curly[pos].type == TYPE_WHILE) {
  1512. sprintf(label,"goto __WL%x_NXT;",syntax.curly[pos].index);
  1513. break;
  1514. }
  1515. pos--;
  1516. }
  1517. if(pos < 0) {
  1518. disp_error_message("parse_syntax: unexpected 'continue'",p);
  1519. } else {
  1520. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1521. parse_line(label);
  1522. syntax.curly_count--;
  1523. }
  1524. p = skip_space(p2);
  1525. if(*p != ';')
  1526. disp_error_message("parse_syntax: expected ';'",p);
  1527. //Closing decision if, for , while
  1528. p = parse_syntax_close(p + 1);
  1529. return p;
  1530. }
  1531. break;
  1532. case 'd':
  1533. case 'D':
  1534. if(p2 - p == 7 && !strncasecmp(p,"default",7)) {
  1535. // Switch - default processing
  1536. int pos = syntax.curly_count-1;
  1537. if(pos < 0 || syntax.curly[pos].type != TYPE_SWITCH) {
  1538. disp_error_message("parse_syntax: unexpected 'default'",p);
  1539. } else if(syntax.curly[pos].flag) {
  1540. disp_error_message("parse_syntax: dup 'default'",p);
  1541. } else {
  1542. char label[256];
  1543. int l;
  1544. // Put the label location
  1545. p = skip_space(p2);
  1546. if(*p != ':') {
  1547. disp_error_message("parse_syntax: expected ':'",p);
  1548. }
  1549. sprintf(label,"__SW%x_%x",syntax.curly[pos].index,syntax.curly[pos].count);
  1550. l=add_str(label);
  1551. set_label(l,script_pos,p);
  1552. // Skip to the next link w/o condition
  1553. sprintf(label,"goto __SW%x_%x;",syntax.curly[pos].index,syntax.curly[pos].count+1);
  1554. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1555. parse_line(label);
  1556. syntax.curly_count--;
  1557. // The default label
  1558. sprintf(label,"__SW%x_DEF",syntax.curly[pos].index);
  1559. l=add_str(label);
  1560. set_label(l,script_pos,p);
  1561. syntax.curly[syntax.curly_count - 1].flag = 1;
  1562. syntax.curly[pos].count++;
  1563. }
  1564. return p + 1;
  1565. } else if(p2 - p == 2 && !strncasecmp(p,"do",2)) {
  1566. int l;
  1567. char label[256];
  1568. p=skip_space(p2);
  1569. syntax.curly[syntax.curly_count].type = TYPE_DO;
  1570. syntax.curly[syntax.curly_count].count = 1;
  1571. syntax.curly[syntax.curly_count].index = syntax.index++;
  1572. syntax.curly[syntax.curly_count].flag = 0;
  1573. // Label of the (do) form here
  1574. sprintf(label,"__DO%x_BGN",syntax.curly[syntax.curly_count].index);
  1575. l=add_str(label);
  1576. set_label(l,script_pos,p);
  1577. syntax.curly_count++;
  1578. return p;
  1579. }
  1580. break;
  1581. case 'f':
  1582. case 'F':
  1583. if(p2 - p == 3 && !strncasecmp(p,"for",3)) {
  1584. int l;
  1585. char label[256];
  1586. int pos = syntax.curly_count;
  1587. syntax.curly[syntax.curly_count].type = TYPE_FOR;
  1588. syntax.curly[syntax.curly_count].count = 1;
  1589. syntax.curly[syntax.curly_count].index = syntax.index++;
  1590. syntax.curly[syntax.curly_count].flag = 0;
  1591. syntax.curly_count++;
  1592. p=skip_space(p2);
  1593. if(*p != '(')
  1594. disp_error_message("parse_syntax: expected '('",p);
  1595. p++;
  1596. // Execute the initialization statement
  1597. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1598. p=parse_line(p);
  1599. syntax.curly_count--;
  1600. // Form the start of label decision
  1601. sprintf(label,"__FR%x_J",syntax.curly[pos].index);
  1602. l=add_str(label);
  1603. set_label(l,script_pos,p);
  1604. p=skip_space(p);
  1605. if(*p == ';') {
  1606. // For (; Because the pattern of always true ;)
  1607. ;
  1608. } else {
  1609. // Skip to the end point if the condition is false
  1610. sprintf(label,"__FR%x_FIN",syntax.curly[pos].index);
  1611. add_scriptl(add_str("jump_zero"));
  1612. add_scriptc(C_ARG);
  1613. p=parse_expr(p);
  1614. p=skip_space(p);
  1615. add_scriptl(add_str(label));
  1616. add_scriptc(C_FUNC);
  1617. }
  1618. if(*p != ';')
  1619. disp_error_message("parse_syntax: expected ';'",p);
  1620. p++;
  1621. // Skip to the beginning of the loop
  1622. sprintf(label,"goto __FR%x_BGN;",syntax.curly[pos].index);
  1623. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1624. parse_line(label);
  1625. syntax.curly_count--;
  1626. // Labels to form the next loop
  1627. sprintf(label,"__FR%x_NXT",syntax.curly[pos].index);
  1628. l=add_str(label);
  1629. set_label(l,script_pos,p);
  1630. // Process the next time you enter the loop
  1631. // A ')' last for; flag to be treated as'
  1632. parse_syntax_for_flag = 1;
  1633. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1634. p=parse_line(p);
  1635. syntax.curly_count--;
  1636. parse_syntax_for_flag = 0;
  1637. // Skip to the determination process conditions
  1638. sprintf(label,"goto __FR%x_J;",syntax.curly[pos].index);
  1639. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1640. parse_line(label);
  1641. syntax.curly_count--;
  1642. // Loop start labeling
  1643. sprintf(label,"__FR%x_BGN",syntax.curly[pos].index);
  1644. l=add_str(label);
  1645. set_label(l,script_pos,p);
  1646. return p;
  1647. }
  1648. else if( p2 - p == 8 && strncasecmp(p,"function",8) == 0 )
  1649. {// internal script function
  1650. const char *func_name;
  1651. func_name = skip_space(p2);
  1652. p = skip_word(func_name);
  1653. if( p == func_name )
  1654. disp_error_message("parse_syntax:function: function name is missing or invalid", p);
  1655. p2 = skip_space(p);
  1656. if( *p2 == ';' )
  1657. {// function <name> ;
  1658. // function declaration - just register the name
  1659. int l;
  1660. l = add_word(func_name);
  1661. if( str_data[l].type == C_NOP )// register only, if the name was not used by something else
  1662. str_data[l].type = C_USERFUNC;
  1663. else if( str_data[l].type == C_USERFUNC )
  1664. ; // already registered
  1665. else
  1666. disp_error_message("parse_syntax:function: function name is invalid", func_name);
  1667. // Close condition of if, for, while
  1668. p = parse_syntax_close(p2 + 1);
  1669. return p;
  1670. }
  1671. else if(*p2 == '{')
  1672. {// function <name> <line/block of code>
  1673. char label[256];
  1674. int l;
  1675. syntax.curly[syntax.curly_count].type = TYPE_USERFUNC;
  1676. syntax.curly[syntax.curly_count].count = 1;
  1677. syntax.curly[syntax.curly_count].index = syntax.index++;
  1678. syntax.curly[syntax.curly_count].flag = 0;
  1679. ++syntax.curly_count;
  1680. // Jump over the function code
  1681. sprintf(label, "goto __FN%x_FIN;", syntax.curly[syntax.curly_count-1].index);
  1682. syntax.curly[syntax.curly_count].type = TYPE_NULL;
  1683. ++syntax.curly_count;
  1684. parse_line(label);
  1685. --syntax.curly_count;
  1686. // Set the position of the function (label)
  1687. l=add_word(func_name);
  1688. if( str_data[l].type == C_NOP || str_data[l].type == C_USERFUNC )// register only, if the name was not used by something else
  1689. {
  1690. str_data[l].type = C_USERFUNC;
  1691. set_label(l, script_pos, p);
  1692. if( parse_options&SCRIPT_USE_LABEL_DB )
  1693. strdb_iput(scriptlabel_db, get_str(l), script_pos);
  1694. }
  1695. else
  1696. disp_error_message("parse_syntax:function: function name is invalid", func_name);
  1697. return skip_space(p);
  1698. }
  1699. else
  1700. {
  1701. disp_error_message("expect ';' or '{' at function syntax",p);
  1702. }
  1703. }
  1704. break;
  1705. case 'i':
  1706. case 'I':
  1707. if(p2 - p == 2 && !strncasecmp(p,"if",2)) {
  1708. // If process
  1709. char label[256];
  1710. p=skip_space(p2);
  1711. if(*p != '(') { //Prevent if this {} non-c syntax. from Rayce (jA)
  1712. disp_error_message("need '('",p);
  1713. }
  1714. syntax.curly[syntax.curly_count].type = TYPE_IF;
  1715. syntax.curly[syntax.curly_count].count = 1;
  1716. syntax.curly[syntax.curly_count].index = syntax.index++;
  1717. syntax.curly[syntax.curly_count].flag = 0;
  1718. sprintf(label,"__IF%x_%x",syntax.curly[syntax.curly_count].index,syntax.curly[syntax.curly_count].count);
  1719. syntax.curly_count++;
  1720. add_scriptl(add_str("jump_zero"));
  1721. add_scriptc(C_ARG);
  1722. p=parse_expr(p);
  1723. p=skip_space(p);
  1724. add_scriptl(add_str(label));
  1725. add_scriptc(C_FUNC);
  1726. return p;
  1727. }
  1728. break;
  1729. case 's':
  1730. case 'S':
  1731. if(p2 - p == 6 && !strncasecmp(p,"switch",6)) {
  1732. // Processing of switch ()
  1733. char label[256];
  1734. p=skip_space(p2);
  1735. if(*p != '(') {
  1736. disp_error_message("need '('",p);
  1737. }
  1738. syntax.curly[syntax.curly_count].type = TYPE_SWITCH;
  1739. syntax.curly[syntax.curly_count].count = 1;
  1740. syntax.curly[syntax.curly_count].index = syntax.index++;
  1741. syntax.curly[syntax.curly_count].flag = 0;
  1742. sprintf(label,"$@__SW%x_VAL",syntax.curly[syntax.curly_count].index);
  1743. syntax.curly_count++;
  1744. add_scriptl(add_str("set"));
  1745. add_scriptc(C_ARG);
  1746. add_scriptl(add_str(label));
  1747. p=parse_expr(p);
  1748. p=skip_space(p);
  1749. if(*p != '{') {
  1750. disp_error_message("parse_syntax: expected '{'",p);
  1751. }
  1752. add_scriptc(C_FUNC);
  1753. return p + 1;
  1754. }
  1755. break;
  1756. case 'w':
  1757. case 'W':
  1758. if(p2 - p == 5 && !strncasecmp(p,"while",5)) {
  1759. int l;
  1760. char label[256];
  1761. p=skip_space(p2);
  1762. if(*p != '(') {
  1763. disp_error_message("need '('",p);
  1764. }
  1765. syntax.curly[syntax.curly_count].type = TYPE_WHILE;
  1766. syntax.curly[syntax.curly_count].count = 1;
  1767. syntax.curly[syntax.curly_count].index = syntax.index++;
  1768. syntax.curly[syntax.curly_count].flag = 0;
  1769. // Form the start of label decision
  1770. sprintf(label,"__WL%x_NXT",syntax.curly[syntax.curly_count].index);
  1771. l=add_str(label);
  1772. set_label(l,script_pos,p);
  1773. // Skip to the end point if the condition is false
  1774. sprintf(label,"__WL%x_FIN",syntax.curly[syntax.curly_count].index);
  1775. syntax.curly_count++;
  1776. add_scriptl(add_str("jump_zero"));
  1777. add_scriptc(C_ARG);
  1778. p=parse_expr(p);
  1779. p=skip_space(p);
  1780. add_scriptl(add_str(label));
  1781. add_scriptc(C_FUNC);
  1782. return p;
  1783. }
  1784. break;
  1785. }
  1786. return NULL;
  1787. }
  1788. const char* parse_syntax_close(const char *p) {
  1789. // If (...) for (...) hoge (); as to make sure closed closed once again
  1790. int flag;
  1791. do {
  1792. p = parse_syntax_close_sub(p,&flag);
  1793. } while(flag);
  1794. return p;
  1795. }
  1796. // Close judgment if, for, while, of do
  1797. // flag == 1 : closed
  1798. // flag == 0 : not closed
  1799. const char* parse_syntax_close_sub(const char* p,int* flag)
  1800. {
  1801. char label[256];
  1802. int pos = syntax.curly_count - 1;
  1803. int l;
  1804. *flag = 1;
  1805. if(syntax.curly_count <= 0) {
  1806. *flag = 0;
  1807. return p;
  1808. } else if(syntax.curly[pos].type == TYPE_IF) {
  1809. const char *bp = p;
  1810. const char *p2;
  1811. // if-block and else-block end is a new line
  1812. parse_nextline(false, p);
  1813. // Skip to the last location if
  1814. sprintf(label,"goto __IF%x_FIN;",syntax.curly[pos].index);
  1815. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1816. parse_line(label);
  1817. syntax.curly_count--;
  1818. // Put the label of the location
  1819. sprintf(label,"__IF%x_%x",syntax.curly[pos].index,syntax.curly[pos].count);
  1820. l=add_str(label);
  1821. set_label(l,script_pos,p);
  1822. syntax.curly[pos].count++;
  1823. p = skip_space(p);
  1824. p2 = skip_word(p);
  1825. if(!syntax.curly[pos].flag && p2 - p == 4 && !strncasecmp(p,"else",4)) {
  1826. // else or else - if
  1827. p = skip_space(p2);
  1828. p2 = skip_word(p);
  1829. if(p2 - p == 2 && !strncasecmp(p,"if",2)) {
  1830. // else - if
  1831. p=skip_space(p2);
  1832. if(*p != '(') {
  1833. disp_error_message("need '('",p);
  1834. }
  1835. sprintf(label,"__IF%x_%x",syntax.curly[pos].index,syntax.curly[pos].count);
  1836. add_scriptl(add_str("jump_zero"));
  1837. add_scriptc(C_ARG);
  1838. p=parse_expr(p);
  1839. p=skip_space(p);
  1840. add_scriptl(add_str(label));
  1841. add_scriptc(C_FUNC);
  1842. *flag = 0;
  1843. return p;
  1844. } else {
  1845. // else
  1846. if(!syntax.curly[pos].flag) {
  1847. syntax.curly[pos].flag = 1;
  1848. *flag = 0;
  1849. return p;
  1850. }
  1851. }
  1852. }
  1853. // Close if
  1854. syntax.curly_count--;
  1855. // Put the label of the final location
  1856. sprintf(label,"__IF%x_FIN",syntax.curly[pos].index);
  1857. l=add_str(label);
  1858. set_label(l,script_pos,p);
  1859. if(syntax.curly[pos].flag == 1) {
  1860. // Because the position of the pointer is the same if not else for this
  1861. return bp;
  1862. }
  1863. return p;
  1864. } else if(syntax.curly[pos].type == TYPE_DO) {
  1865. int l2;
  1866. char label2[256];
  1867. const char *p2;
  1868. if(syntax.curly[pos].flag) {
  1869. // (Come here continue) to form the label here
  1870. sprintf(label2,"__DO%x_NXT",syntax.curly[pos].index);
  1871. l2=add_str(label2);
  1872. set_label(l2,script_pos,p);
  1873. }
  1874. // Skip to the end point if the condition is false
  1875. p = skip_space(p);
  1876. p2 = skip_word(p);
  1877. if(p2 - p != 5 || strncasecmp(p,"while",5))
  1878. disp_error_message("parse_syntax: expected 'while'",p);
  1879. p = skip_space(p2);
  1880. if(*p != '(') {
  1881. disp_error_message("need '('",p);
  1882. }
  1883. // do-block end is a new line
  1884. parse_nextline(false, p);
  1885. sprintf(label2,"__DO%x_FIN",syntax.curly[pos].index);
  1886. add_scriptl(add_str("jump_zero"));
  1887. add_scriptc(C_ARG);
  1888. p=parse_expr(p);
  1889. p=skip_space(p);
  1890. add_scriptl(add_str(label2));
  1891. add_scriptc(C_FUNC);
  1892. // Skip to the starting point
  1893. sprintf(label2,"goto __DO%x_BGN;",syntax.curly[pos].index);
  1894. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1895. parse_line(label2);
  1896. syntax.curly_count--;
  1897. // Form label of the end point conditions
  1898. sprintf(label2,"__DO%x_FIN",syntax.curly[pos].index);
  1899. l2=add_str(label2);
  1900. set_label(l2,script_pos,p);
  1901. p = skip_space(p);
  1902. if(*p != ';') {
  1903. disp_error_message("parse_syntax: expected ';'",p);
  1904. return p+1;
  1905. }
  1906. p++;
  1907. syntax.curly_count--;
  1908. return p;
  1909. } else if(syntax.curly[pos].type == TYPE_FOR) {
  1910. // for-block end is a new line
  1911. parse_nextline(false, p);
  1912. // Skip to the next loop
  1913. sprintf(label,"goto __FR%x_NXT;",syntax.curly[pos].index);
  1914. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1915. parse_line(label);
  1916. syntax.curly_count--;
  1917. // End for labeling
  1918. sprintf(label,"__FR%x_FIN",syntax.curly[pos].index);
  1919. l=add_str(label);
  1920. set_label(l,script_pos,p);
  1921. syntax.curly_count--;
  1922. return p;
  1923. } else if(syntax.curly[pos].type == TYPE_WHILE) {
  1924. // while-block end is a new line
  1925. parse_nextline(false, p);
  1926. // Skip to the decision while
  1927. sprintf(label,"goto __WL%x_NXT;",syntax.curly[pos].index);
  1928. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1929. parse_line(label);
  1930. syntax.curly_count--;
  1931. // End while labeling
  1932. sprintf(label,"__WL%x_FIN",syntax.curly[pos].index);
  1933. l=add_str(label);
  1934. set_label(l,script_pos,p);
  1935. syntax.curly_count--;
  1936. return p;
  1937. } else if(syntax.curly[syntax.curly_count-1].type == TYPE_USERFUNC) {
  1938. int pos2 = syntax.curly_count-1;
  1939. char label2[256];
  1940. int l2;
  1941. // Back
  1942. sprintf(label2,"return;");
  1943. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1944. parse_line(label2);
  1945. syntax.curly_count--;
  1946. // Put the label of the location
  1947. sprintf(label2,"__FN%x_FIN",syntax.curly[pos2].index);
  1948. l2=add_str(label2);
  1949. set_label(l2,script_pos,p);
  1950. syntax.curly_count--;
  1951. return p;
  1952. } else {
  1953. *flag = 0;
  1954. return p;
  1955. }
  1956. }
  1957. /*==========================================
  1958. * Added built-in functions
  1959. *------------------------------------------*/
  1960. static void add_buildin_func(void)
  1961. {
  1962. int i;
  1963. for( i = 0; buildin_func[i].func; i++ )
  1964. {
  1965. // arg must follow the pattern: (v|s|i|r|l)*\?*\*?
  1966. // 'v' - value (either string or int or reference)
  1967. // 's' - string
  1968. // 'i' - int
  1969. // 'r' - reference (of a variable)
  1970. // 'l' - label
  1971. // '?' - one optional parameter
  1972. // '*' - unknown number of optional parameters
  1973. const char* p = buildin_func[i].arg;
  1974. while( *p == 'v' || *p == 's' || *p == 'i' || *p == 'r' || *p == 'l' ) ++p;
  1975. while( *p == '?' ) ++p;
  1976. if( *p == '*' ) ++p;
  1977. if( *p != 0){
  1978. ShowWarning("add_buildin_func: ignoring function \"%s\" with invalid arg \"%s\".\n", buildin_func[i].name, buildin_func[i].arg);
  1979. } else if( *skip_word(buildin_func[i].name) != 0 ){
  1980. ShowWarning("add_buildin_func: ignoring function with invalid name \"%s\" (must be a word).\n", buildin_func[i].name);
  1981. } else {
  1982. int n = add_str(buildin_func[i].name);
  1983. str_data[n].type = C_FUNC;
  1984. str_data[n].val = i;
  1985. str_data[n].func = buildin_func[i].func;
  1986. if (!strcmp(buildin_func[i].name, "setr")) buildin_set_ref = n;
  1987. else if (!strcmp(buildin_func[i].name, "callsub")) buildin_callsub_ref = n;
  1988. else if (!strcmp(buildin_func[i].name, "callfunc")) buildin_callfunc_ref = n;
  1989. else if( !strcmp(buildin_func[i].name, "getelementofarray") ) buildin_getelementofarray_ref = n;
  1990. }
  1991. }
  1992. }
  1993. /// Retrieves the value of a constant.
  1994. bool script_get_constant(const char* name, int* value)
  1995. {
  1996. int n = search_str(name);
  1997. if( n == -1 || str_data[n].type != C_INT )
  1998. {// not found or not a constant
  1999. return false;
  2000. }
  2001. value[0] = str_data[n].val;
  2002. return true;
  2003. }
  2004. /// Creates new constant or parameter with given value.
  2005. void script_set_constant(const char* name, int value, bool isparameter)
  2006. {
  2007. int n = add_str(name);
  2008. if( str_data[n].type == C_NOP )
  2009. {// new
  2010. str_data[n].type = isparameter ? C_PARAM : C_INT;
  2011. str_data[n].val = value;
  2012. }
  2013. else if( str_data[n].type == C_PARAM || str_data[n].type == C_INT )
  2014. {// existing parameter or constant
  2015. 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);
  2016. }
  2017. else
  2018. {// existing name
  2019. ShowError("script_set_constant: Invalid name for %s '%s' (already defined as %s).\n", isparameter ? "parameter" : "constant", name, script_op2name(str_data[n].type));
  2020. }
  2021. }
  2022. /*==========================================
  2023. * Reading constant databases
  2024. * const.txt
  2025. *------------------------------------------*/
  2026. static void read_constdb(void)
  2027. {
  2028. FILE *fp;
  2029. char line[1024],name[1024],val[1024];
  2030. int type;
  2031. int entries=0, skipped=0, linenum=0;
  2032. sprintf(line, "%s/const.txt", db_path);
  2033. fp=fopen(line, "r");
  2034. if(fp==NULL){
  2035. ShowError("can't read %s\n", line);
  2036. return ;
  2037. }
  2038. while(fgets(line, sizeof(line), fp))
  2039. {
  2040. linenum++;
  2041. if( line[0] == '\0' || line[0] == '\n' || line[0] == '\r') //ignore empty line
  2042. continue;
  2043. if(line[0]=='/' && line[1]=='/') //ignore commented line
  2044. continue;
  2045. type=0;
  2046. if(sscanf(line,"%1023[A-Za-z0-9/_],%1023[A-Za-z0-9/_-],%11d",name,val,&type)>=2 ||
  2047. sscanf(line,"%1023[A-Za-z0-9/_] %1023[A-Za-z0-9/_-] %11d",name,val,&type)>=2){
  2048. entries++;
  2049. script_set_constant(name, (int)strtol(val, NULL, 0), (bool)type);
  2050. }
  2051. else {
  2052. skipped++;
  2053. ShowWarning("Skipping line '"CL_WHITE"%d"CL_RESET"', invalide constant definition\n",linenum);
  2054. }
  2055. }
  2056. fclose(fp);
  2057. ShowStatus("Done reading '"CL_WHITE"%d"CL_RESET"' entries in '"CL_WHITE"%s/const.txt"CL_RESET"'.\n", entries, db_path);
  2058. if(skipped){
  2059. ShowWarning("Skipped '"CL_WHITE"%d"CL_RESET"', entries\n",skipped);
  2060. }
  2061. }
  2062. /**
  2063. * Sets source-end constants for NPC scripts to access.
  2064. **/
  2065. void script_hardcoded_constants(void) {
  2066. #include "script_constants.h"
  2067. }
  2068. /*==========================================
  2069. * Display emplacement line of script
  2070. *------------------------------------------*/
  2071. static const char* script_print_line(StringBuf* buf, const char* p, const char* mark, int line)
  2072. {
  2073. int i;
  2074. if( p == NULL || !p[0] ) return NULL;
  2075. if( line < 0 )
  2076. StringBuf_Printf(buf, "*% 5d : ", -line);
  2077. else
  2078. StringBuf_Printf(buf, " % 5d : ", line);
  2079. for(i=0;p[i] && p[i] != '\n';i++){
  2080. if(p + i != mark)
  2081. StringBuf_Printf(buf, "%c", p[i]);
  2082. else
  2083. StringBuf_Printf(buf, "\'%c\'", p[i]);
  2084. }
  2085. StringBuf_AppendStr(buf, "\n");
  2086. return p+i+(p[i] == '\n' ? 1 : 0);
  2087. }
  2088. 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) {
  2089. // Find the line where the error occurred
  2090. int j;
  2091. int line = start_line;
  2092. const char *p;
  2093. const char *linestart[5] = { NULL, NULL, NULL, NULL, NULL };
  2094. for(p=src;p && *p;line++){
  2095. const char *lineend=strchr(p,'\n');
  2096. if(lineend==NULL || error_pos_cur<lineend){
  2097. break;
  2098. }
  2099. for( j = 0; j < 4; j++ ) {
  2100. linestart[j] = linestart[j+1];
  2101. }
  2102. linestart[4] = p;
  2103. p=lineend+1;
  2104. }
  2105. StringBuf_Printf(buf, "script error on %s line %d\n", file, line);
  2106. StringBuf_Printf(buf, " %s\n", error_msg_cur);
  2107. for(j = 0; j < 5; j++ ) {
  2108. script_print_line(buf, linestart[j], NULL, line + j - 5);
  2109. }
  2110. p = script_print_line(buf, p, error_pos_cur, -line);
  2111. for(j = 0; j < 5; j++) {
  2112. p = script_print_line(buf, p, NULL, line + j + 1 );
  2113. }
  2114. }
  2115. void script_error(const char* src, const char* file, int start_line, const char* error_msg_cur, const char* error_pos_cur) {
  2116. StringBuf buf;
  2117. StringBuf_Init(&buf);
  2118. StringBuf_AppendStr(&buf, "\a\n");
  2119. script_errorwarning_sub(&buf, src, file, start_line, error_msg_cur, error_pos_cur);
  2120. ShowError("%s", StringBuf_Value(&buf));
  2121. StringBuf_Destroy(&buf);
  2122. }
  2123. void script_warning(const char* src, const char* file, int start_line, const char* error_msg_cur, const char* error_pos_cur) {
  2124. StringBuf buf;
  2125. StringBuf_Init(&buf);
  2126. script_errorwarning_sub(&buf, src, file, start_line, error_msg_cur, error_pos_cur);
  2127. ShowWarning("%s", StringBuf_Value(&buf));
  2128. StringBuf_Destroy(&buf);
  2129. }
  2130. /*==========================================
  2131. * Analysis of the script
  2132. *------------------------------------------*/
  2133. struct script_code* parse_script(const char *src,const char *file,int line,int options)
  2134. {
  2135. const char *p,*tmpp;
  2136. int i;
  2137. struct script_code* code = NULL;
  2138. static int first=1;
  2139. char end;
  2140. bool unresolved_names = false;
  2141. parser_current_src = src;
  2142. parser_current_file = file;
  2143. parser_current_line = line;
  2144. if( src == NULL )
  2145. return NULL;// empty script
  2146. memset(&syntax,0,sizeof(syntax));
  2147. if(first){
  2148. add_buildin_func();
  2149. read_constdb();
  2150. script_hardcoded_constants();
  2151. first=0;
  2152. }
  2153. script_buf=(unsigned char *)aMalloc(SCRIPT_BLOCK_SIZE*sizeof(unsigned char));
  2154. script_pos=0;
  2155. script_size=SCRIPT_BLOCK_SIZE;
  2156. parse_nextline(true, NULL);
  2157. // who called parse_script is responsible for clearing the database after using it, but just in case... lets clear it here
  2158. if( options&SCRIPT_USE_LABEL_DB )
  2159. db_clear(scriptlabel_db);
  2160. parse_options = options;
  2161. if( setjmp( error_jump ) != 0 ) {
  2162. //Restore program state when script has problems. [from jA]
  2163. int j;
  2164. const int size = ARRAYLENGTH(syntax.curly);
  2165. if( error_report )
  2166. script_error(src,file,line,error_msg,error_pos);
  2167. aFree( error_msg );
  2168. aFree( script_buf );
  2169. script_pos = 0;
  2170. script_size = 0;
  2171. script_buf = NULL;
  2172. for(j=LABEL_START;j<str_num;j++)
  2173. if(str_data[j].type == C_NOP) str_data[j].type = C_NAME;
  2174. for(j=0; j<size; j++)
  2175. linkdb_final(&syntax.curly[j].case_label);
  2176. return NULL;
  2177. }
  2178. parse_syntax_for_flag=0;
  2179. p=src;
  2180. p=skip_space(p);
  2181. if( options&SCRIPT_IGNORE_EXTERNAL_BRACKETS )
  2182. {// does not require brackets around the script
  2183. if( *p == '\0' && !(options&SCRIPT_RETURN_EMPTY_SCRIPT) )
  2184. {// empty script and can return NULL
  2185. aFree( script_buf );
  2186. script_pos = 0;
  2187. script_size = 0;
  2188. script_buf = NULL;
  2189. return NULL;
  2190. }
  2191. end = '\0';
  2192. }
  2193. else
  2194. {// requires brackets around the script
  2195. if( *p != '{' )
  2196. disp_error_message("not found '{'",p);
  2197. p = skip_space(p+1);
  2198. if( *p == '}' && !(options&SCRIPT_RETURN_EMPTY_SCRIPT) )
  2199. {// empty script and can return NULL
  2200. aFree( script_buf );
  2201. script_pos = 0;
  2202. script_size = 0;
  2203. script_buf = NULL;
  2204. return NULL;
  2205. }
  2206. end = '}';
  2207. }
  2208. // clear references of labels, variables and internal functions
  2209. for(i=LABEL_START;i<str_num;i++){
  2210. if(
  2211. str_data[i].type==C_POS || str_data[i].type==C_NAME ||
  2212. str_data[i].type==C_USERFUNC || str_data[i].type == C_USERFUNC_POS
  2213. ){
  2214. str_data[i].type=C_NOP;
  2215. str_data[i].backpatch=-1;
  2216. str_data[i].label=-1;
  2217. }
  2218. }
  2219. while( syntax.curly_count != 0 || *p != end )
  2220. {
  2221. if( *p == '\0' )
  2222. disp_error_message("unexpected end of script",p);
  2223. // Special handling only label
  2224. tmpp=skip_space(skip_word(p));
  2225. if(*tmpp==':' && !(!strncasecmp(p,"default:",8) && p + 7 == tmpp)){
  2226. i=add_word(p);
  2227. set_label(i,script_pos,p);
  2228. if( parse_options&SCRIPT_USE_LABEL_DB )
  2229. strdb_iput(scriptlabel_db, get_str(i), script_pos);
  2230. p=tmpp+1;
  2231. p=skip_space(p);
  2232. continue;
  2233. }
  2234. // All other lumped
  2235. p=parse_line(p);
  2236. p=skip_space(p);
  2237. parse_nextline(false, p);
  2238. }
  2239. add_scriptc(C_NOP);
  2240. // trim code to size
  2241. script_size = script_pos;
  2242. RECREATE(script_buf,unsigned char,script_pos);
  2243. // default unknown references to variables
  2244. for(i=LABEL_START;i<str_num;i++){
  2245. if(str_data[i].type==C_NOP){
  2246. int j;
  2247. str_data[i].type=C_NAME;
  2248. str_data[i].label=i;
  2249. for(j=str_data[i].backpatch;j>=0 && j!=0x00ffffff;){
  2250. int next=GETVALUE(script_buf,j);
  2251. SETVALUE(script_buf,j,i);
  2252. j=next;
  2253. }
  2254. }
  2255. else if( str_data[i].type == C_USERFUNC )
  2256. {// 'function name;' without follow-up code
  2257. ShowError("parse_script: function '%s' declared but not defined.\n", str_buf+str_data[i].str);
  2258. unresolved_names = true;
  2259. }
  2260. }
  2261. if( unresolved_names )
  2262. {
  2263. disp_error_message("parse_script: unresolved function references", p);
  2264. }
  2265. #ifdef DEBUG_DISP
  2266. for(i=0;i<script_pos;i++){
  2267. if((i&15)==0) ShowMessage("%04x : ",i);
  2268. ShowMessage("%02x ",script_buf[i]);
  2269. if((i&15)==15) ShowMessage("\n");
  2270. }
  2271. ShowMessage("\n");
  2272. #endif
  2273. #ifdef DEBUG_DISASM
  2274. {
  2275. int i = 0,j;
  2276. while(i < script_pos) {
  2277. c_op op = get_com(script_buf,&i);
  2278. ShowMessage("%06x %s", i, script_op2name(op));
  2279. j = i;
  2280. switch(op) {
  2281. case C_INT:
  2282. ShowMessage(" %d", get_num(script_buf,&i));
  2283. break;
  2284. case C_POS:
  2285. ShowMessage(" 0x%06x", *(int*)(script_buf+i)&0xffffff);
  2286. i += 3;
  2287. break;
  2288. case C_NAME:
  2289. j = (*(int*)(script_buf+i)&0xffffff);
  2290. ShowMessage(" %s", ( j == 0xffffff ) ? "?? unknown ??" : get_str(j));
  2291. i += 3;
  2292. break;
  2293. case C_STR:
  2294. j = strlen(script_buf + i);
  2295. ShowMessage(" %s", script_buf + i);
  2296. i += j+1;
  2297. break;
  2298. }
  2299. ShowMessage(CL_CLL"\n");
  2300. }
  2301. }
  2302. #endif
  2303. CREATE(code,struct script_code,1);
  2304. code->script_buf = script_buf;
  2305. code->script_size = script_size;
  2306. code->local.vars = NULL;
  2307. code->local.arrays = NULL;
  2308. return code;
  2309. }
  2310. /// Returns the player attached to this script, identified by the rid.
  2311. /// If there is no player attached, the script is terminated.
  2312. TBL_PC *script_rid2sd(struct script_state *st)
  2313. {
  2314. TBL_PC *sd=map_id2sd(st->rid);
  2315. if(!sd){
  2316. ShowError("script_rid2sd: fatal error ! player not attached!\n");
  2317. script_reportfunc(st);
  2318. script_reportsrc(st);
  2319. st->state = END;
  2320. }
  2321. return sd;
  2322. }
  2323. /**
  2324. * Dereferences a variable/constant, replacing it with a copy of the value.
  2325. * @param st Script state
  2326. * @param data Variable/constant
  2327. * @param sd If NULL, will try to use sd from st->rid (for player's variables)
  2328. */
  2329. void get_val_(struct script_state* st, struct script_data* data, struct map_session_data *sd)
  2330. {
  2331. const char* name;
  2332. char prefix;
  2333. char postfix;
  2334. if( !data_isreference(data) )
  2335. return;// not a variable/constant
  2336. name = reference_getname(data);
  2337. prefix = name[0];
  2338. postfix = name[strlen(name) - 1];
  2339. //##TODO use reference_tovariable(data) when it's confirmed that it works [FlavioJS]
  2340. if( !reference_toconstant(data) && not_server_variable(prefix) ) {
  2341. if (sd == NULL)
  2342. sd = script_rid2sd(st);
  2343. if( sd == NULL ) {// needs player attached
  2344. if( postfix == '$' ) {// string variable
  2345. ShowWarning("script:get_val: cannot access player variable '%s', defaulting to \"\"\n", name);
  2346. data->type = C_CONSTSTR;
  2347. data->u.str = "";
  2348. } else {// integer variable
  2349. ShowWarning("script:get_val: cannot access player variable '%s', defaulting to 0\n", name);
  2350. data->type = C_INT;
  2351. data->u.num = 0;
  2352. }
  2353. return;
  2354. }
  2355. }
  2356. if( postfix == '$' ) {// string variable
  2357. switch( prefix ) {
  2358. case '@':
  2359. data->u.str = pc_readregstr(sd, data->u.num);
  2360. break;
  2361. case '$':
  2362. data->u.str = mapreg_readregstr(data->u.num);
  2363. break;
  2364. case '#':
  2365. if( name[1] == '#' )
  2366. data->u.str = pc_readaccountreg2str(sd, data->u.num);// global
  2367. else
  2368. data->u.str = pc_readaccountregstr(sd, data->u.num);// local
  2369. break;
  2370. case '.':
  2371. {
  2372. struct DBMap* n = data->ref ?
  2373. data->ref->vars : name[1] == '@' ?
  2374. st->stack->scope.vars : // instance/scope variable
  2375. st->script->local.vars; // npc variable
  2376. if( n )
  2377. data->u.str = (char*)i64db_get(n,reference_getuid(data));
  2378. else
  2379. data->u.str = NULL;
  2380. }
  2381. break;
  2382. case '\'':
  2383. {
  2384. unsigned short instance_id = script_instancegetid(st);
  2385. if( instance_id )
  2386. data->u.str = (char*)i64db_get(instance_data[instance_id].regs.vars,reference_getuid(data));
  2387. else {
  2388. ShowWarning("script:get_val: cannot access instance variable '%s', defaulting to \"\"\n", name);
  2389. data->u.str = NULL;
  2390. }
  2391. }
  2392. break;
  2393. default:
  2394. data->u.str = pc_readglobalreg_str(sd, data->u.num);
  2395. break;
  2396. }
  2397. if( data->u.str == NULL || data->u.str[0] == '\0' ) {// empty string
  2398. data->type = C_CONSTSTR;
  2399. data->u.str = "";
  2400. } else {// duplicate string
  2401. data->type = C_STR;
  2402. data->u.str = aStrdup(data->u.str);
  2403. }
  2404. } else {// integer variable
  2405. data->type = C_INT;
  2406. if( reference_toconstant(data) ) {
  2407. data->u.num = reference_getconstant(data);
  2408. } else if( reference_toparam(data) ) {
  2409. data->u.num = pc_readparam(sd, reference_getparamtype(data));
  2410. } else
  2411. switch( prefix ) {
  2412. case '@':
  2413. data->u.num = pc_readreg(sd, data->u.num);
  2414. break;
  2415. case '$':
  2416. data->u.num = mapreg_readreg(data->u.num);
  2417. break;
  2418. case '#':
  2419. if( name[1] == '#' )
  2420. data->u.num = pc_readaccountreg2(sd, data->u.num);// global
  2421. else
  2422. data->u.num = pc_readaccountreg(sd, data->u.num);// local
  2423. break;
  2424. case '.':
  2425. {
  2426. struct DBMap* n = data->ref ?
  2427. data->ref->vars : name[1] == '@' ?
  2428. st->stack->scope.vars : // instance/scope variable
  2429. st->script->local.vars; // npc variable
  2430. if( n )
  2431. data->u.num = (int)i64db_iget(n,reference_getuid(data));
  2432. else
  2433. data->u.num = 0;
  2434. }
  2435. break;
  2436. case '\'':
  2437. {
  2438. unsigned short instance_id = script_instancegetid(st);
  2439. if( instance_id )
  2440. data->u.num = (int)i64db_iget(instance_data[instance_id].regs.vars,reference_getuid(data));
  2441. else {
  2442. ShowWarning("script:get_val: cannot access instance variable '%s', defaulting to 0\n", name);
  2443. data->u.num = 0;
  2444. }
  2445. }
  2446. break;
  2447. default:
  2448. data->u.num = pc_readglobalreg(sd, data->u.num);
  2449. break;
  2450. }
  2451. }
  2452. data->ref = NULL;
  2453. return;
  2454. }
  2455. void get_val(struct script_state* st, struct script_data* data)
  2456. {
  2457. get_val_(st,data,NULL);
  2458. }
  2459. struct script_data* push_val2(struct script_stack* stack, enum c_op type, int64 val, struct reg_db* ref);
  2460. /// Retrieves the value of a reference identified by uid (variable, constant, param)
  2461. /// The value is left in the top of the stack and needs to be removed manually.
  2462. /// @param st[in]: script state.
  2463. /// @param uid[in]: reference identifier.
  2464. /// @param ref[in]: the container to look up the reference into.
  2465. /// @return: the retrieved value of the reference.
  2466. void* get_val2(struct script_state* st, int64 uid, struct reg_db *ref)
  2467. {
  2468. struct script_data* data;
  2469. push_val2(st->stack, C_NAME, uid, ref);
  2470. data = script_getdatatop(st, -1);
  2471. get_val(st, data);
  2472. //! TODO: Support data->u.num as int64 instead cast it to int? (in get_val, it was casted to int).
  2473. return (data->type == C_INT ? (void*)__64BPRTSIZE((int)data->u.num) : (void*)__64BPRTSIZE(data->u.str));
  2474. }
  2475. /**
  2476. * Because, currently, array members with key 0 are indifferenciable from normal variables, we should ensure its actually in
  2477. * Will be gone as soon as undefined var feature is implemented
  2478. **/
  2479. void script_array_ensure_zero(struct script_state *st, struct map_session_data *sd, int64 uid, struct reg_db *ref)
  2480. {
  2481. const char *name = get_str(script_getvarid(uid));
  2482. // is here st can be null pointer and st->rid is wrong?
  2483. struct reg_db *src = script_array_src(st, sd ? sd : st->rid ? map_id2sd(st->rid) : NULL, name, ref);
  2484. bool insert = false;
  2485. if (sd && !st) {
  2486. // when sd comes, st isn't available
  2487. insert = true;
  2488. } else {
  2489. if( is_string_variable(name) ) {
  2490. char* str = (char*)get_val2(st, uid, ref);
  2491. if( str && *str )
  2492. insert = true;
  2493. script_removetop(st, -1, 0);
  2494. } else {
  2495. int32 num = (int32)__64BPRTSIZE(get_val2(st, uid, ref));
  2496. if( num )
  2497. insert = true;
  2498. script_removetop(st, -1, 0);
  2499. }
  2500. }
  2501. if (src && src->arrays) {
  2502. struct script_array *sa = idb_get(src->arrays, script_getvarid(uid));
  2503. if (sa) {
  2504. unsigned int i;
  2505. ARR_FIND(0, sa->size, i, sa->members[i] == 0);
  2506. if( i != sa->size ) {
  2507. if( !insert )
  2508. script_array_remove_member(src,sa,i);
  2509. return;
  2510. }
  2511. script_array_add_member(sa,0);
  2512. } else if (insert) {
  2513. script_array_update(src,reference_uid(script_getvarid(uid), 0),false);
  2514. }
  2515. }
  2516. }
  2517. /**
  2518. * Returns array size by ID
  2519. **/
  2520. unsigned int script_array_size(struct script_state *st, struct map_session_data *sd, const char *name, struct reg_db *ref)
  2521. {
  2522. struct script_array *sa = NULL;
  2523. struct reg_db *src = script_array_src(st, sd, name, ref);
  2524. if (src && src->arrays)
  2525. sa = idb_get(src->arrays, search_str(name));
  2526. return sa ? sa->size : 0;
  2527. }
  2528. /**
  2529. * Returns array's highest key (for that awful getarraysize implementation that doesn't really gets the array size)
  2530. **/
  2531. unsigned int script_array_highest_key(struct script_state *st, struct map_session_data *sd, const char *name, struct reg_db *ref)
  2532. {
  2533. struct script_array *sa = NULL;
  2534. struct reg_db *src = script_array_src(st, sd, name, ref);
  2535. if (src && src->arrays) {
  2536. int key = add_word(name);
  2537. script_array_ensure_zero(st,sd,reference_uid(key, 0), ref);
  2538. if( ( sa = idb_get(src->arrays, key) ) ) {
  2539. unsigned int i, highest_key = 0;
  2540. for(i = 0; i < sa->size; i++) {
  2541. if( sa->members[i] > highest_key )
  2542. highest_key = sa->members[i];
  2543. }
  2544. return sa->size ? highest_key + 1 : 0;
  2545. }
  2546. }
  2547. return SCRIPT_CMD_SUCCESS;
  2548. }
  2549. int script_free_array_db(DBKey key, DBData *data, va_list ap)
  2550. {
  2551. struct script_array *sa = db_data2ptr(data);
  2552. aFree(sa->members);
  2553. ers_free(array_ers, sa);
  2554. return SCRIPT_CMD_SUCCESS;
  2555. }
  2556. /**
  2557. * Clears script_array and removes it from script->array_db
  2558. **/
  2559. void script_array_delete(struct reg_db *src, struct script_array *sa)
  2560. {
  2561. aFree(sa->members);
  2562. idb_remove(src->arrays, sa->id);
  2563. ers_free(array_ers, sa);
  2564. }
  2565. /**
  2566. * Removes a member from a script_array list
  2567. *
  2568. * @param idx the index of the member in script_array struct list, not of the actual array member
  2569. **/
  2570. void script_array_remove_member(struct reg_db *src, struct script_array *sa, unsigned int idx)
  2571. {
  2572. unsigned int i, cursor;
  2573. // it's the only member left, no need to do anything other than delete the array data
  2574. if( sa->size == 1 ) {
  2575. script_array_delete(src,sa);
  2576. return;
  2577. }
  2578. sa->members[idx] = UINT_MAX;
  2579. for(i = 0, cursor = 0; i < sa->size; i++) {
  2580. if( sa->members[i] == UINT_MAX )
  2581. continue;
  2582. if( i != cursor )
  2583. sa->members[cursor] = sa->members[i];
  2584. cursor++;
  2585. }
  2586. sa->size = cursor;
  2587. }
  2588. /**
  2589. * Appends a new array index to the list in script_array
  2590. *
  2591. * @param idx the index of the array member being inserted
  2592. **/
  2593. void script_array_add_member(struct script_array *sa, unsigned int idx)
  2594. {
  2595. RECREATE(sa->members, unsigned int, ++sa->size);
  2596. sa->members[sa->size - 1] = idx;
  2597. }
  2598. /**
  2599. * Obtains the source of the array database for this type and scenario
  2600. * Initializes such database when not yet initialized.
  2601. **/
  2602. struct reg_db *script_array_src(struct script_state *st, struct map_session_data *sd, const char *name, struct reg_db *ref)
  2603. {
  2604. struct reg_db *src = NULL;
  2605. switch( name[0] ) {
  2606. // from player
  2607. default: // char reg
  2608. case '@': // temp char reg
  2609. case '#': // account reg
  2610. src = &sd->regs;
  2611. break;
  2612. case '$': // map reg
  2613. src = &regs;
  2614. break;
  2615. case '.': // npc/script
  2616. if (ref)
  2617. src = ref;
  2618. else
  2619. src = (name[1] == '@') ? &st->stack->scope : &st->script->local;
  2620. break;
  2621. case '\'': // instance
  2622. {
  2623. unsigned short instance_id = script_instancegetid(st);
  2624. if( instance_id ) {
  2625. src = &instance_data[instance_id].regs;
  2626. }
  2627. break;
  2628. }
  2629. }
  2630. if( src ) {
  2631. if( !src->arrays )
  2632. src->arrays = idb_alloc(DB_OPT_BASE);
  2633. return src;
  2634. }
  2635. return NULL;
  2636. }
  2637. /**
  2638. * Processes a array member modification, and update data accordingly
  2639. *
  2640. * @param src[in,out] Variable source database. If the array database doesn't exist, it is created.
  2641. * @param num[in] Variable ID
  2642. * @param empty[in] Whether the modified member is empty (needs to be removed)
  2643. **/
  2644. void script_array_update(struct reg_db *src, int64 num, bool empty)
  2645. {
  2646. struct script_array *sa = NULL;
  2647. int id = script_getvarid(num);
  2648. unsigned int index = script_getvaridx(num);
  2649. if (!src->arrays) {
  2650. src->arrays = idb_alloc(DB_OPT_BASE);
  2651. } else {
  2652. sa = idb_get(src->arrays, id);
  2653. }
  2654. if( sa ) {
  2655. unsigned int i;
  2656. // search
  2657. for(i = 0; i < sa->size; i++) {
  2658. if( sa->members[i] == index )
  2659. break;
  2660. }
  2661. // if existent
  2662. if( i != sa->size ) {
  2663. // if empty, we gotta remove it
  2664. if( empty ) {
  2665. script_array_remove_member(src, sa, i);
  2666. }
  2667. } else if( !empty ) { /* new entry */
  2668. script_array_add_member(sa,index);
  2669. // we do nothing if its empty, no point in modifying array data for a new empty member
  2670. }
  2671. } else if ( !empty ) { // we only move to create if not empty
  2672. sa = ers_alloc(array_ers, struct script_array);
  2673. sa->id = id;
  2674. sa->members = NULL;
  2675. sa->size = 0;
  2676. script_array_add_member(sa,index);
  2677. idb_put(src->arrays, id, sa);
  2678. }
  2679. }
  2680. /**
  2681. * Stores the value of a script variable
  2682. *
  2683. * @param st: current script state.
  2684. * @param sd: current character data.
  2685. * @param num: variable identifier.
  2686. * @param name: variable name.
  2687. * @param value: new value.
  2688. * @param ref: variable container, in case of a npc/scope variable reference outside the current scope.
  2689. * @return: 0 failure, 1 success.
  2690. *
  2691. * TODO: return values are screwed up, have been for some time (reaad: years), e.g. some functions return 1 failure and success.
  2692. *------------------------------------------*/
  2693. int set_reg(struct script_state* st, TBL_PC* sd, int64 num, const char* name, const void* value, struct reg_db *ref)
  2694. {
  2695. char prefix = name[0];
  2696. if( is_string_variable(name) ) {// string variable
  2697. const char *str = (const char*)value;
  2698. switch (prefix) {
  2699. case '@':
  2700. pc_setregstr(sd, num, str);
  2701. return 1;
  2702. case '$':
  2703. return mapreg_setregstr(num, str);
  2704. case '#':
  2705. return (name[1] == '#') ?
  2706. pc_setaccountreg2str(sd, num, str) :
  2707. pc_setaccountregstr(sd, num, str);
  2708. case '.':
  2709. {
  2710. struct reg_db *n = (ref) ? ref : (name[1] == '@') ? &st->stack->scope : &st->script->local;
  2711. if( n ) {
  2712. if (str[0]) {
  2713. i64db_put(n->vars, num, aStrdup(str));
  2714. if( script_getvaridx(num) )
  2715. script_array_update(n, num, false);
  2716. } else {
  2717. i64db_remove(n->vars, num);
  2718. if( script_getvaridx(num) )
  2719. script_array_update(n, num, true);
  2720. }
  2721. }
  2722. }
  2723. return 1;
  2724. case '\'':
  2725. {
  2726. unsigned short instance_id = script_instancegetid(st);
  2727. if( instance_id ) {
  2728. if( str[0] ) {
  2729. i64db_put(instance_data[instance_id].regs.vars, num, aStrdup(str));
  2730. if( script_getvaridx(num) )
  2731. script_array_update(&instance_data[instance_id].regs, num, false);
  2732. } else {
  2733. i64db_remove(instance_data[instance_id].regs.vars, num);
  2734. if (script_getvaridx(num))
  2735. script_array_update(&instance_data[instance_id].regs, num, true);
  2736. }
  2737. } else {
  2738. ShowError("script_set_reg: cannot write instance variable '%s', NPC not in a instance!\n", name);
  2739. script_reportsrc(st);
  2740. }
  2741. return 1;
  2742. }
  2743. default:
  2744. return pc_setglobalreg_str(sd, num, str);
  2745. }
  2746. } else {// integer variable
  2747. int val = (int)__64BPRTSIZE(value);
  2748. if(str_data[script_getvarid(num)].type == C_PARAM) {
  2749. if( pc_setparam(sd, str_data[script_getvarid(num)].val, val) == 0 ) {
  2750. if( st != NULL ) {
  2751. ShowError("script_set_reg: failed to set param '%s' to %d.\n", name, val);
  2752. script_reportsrc(st);
  2753. st->state = END;
  2754. }
  2755. return 0;
  2756. }
  2757. return 1;
  2758. }
  2759. switch (prefix) {
  2760. case '@':
  2761. pc_setreg(sd, num, val);
  2762. return 1;
  2763. case '$':
  2764. return mapreg_setreg(num, val);
  2765. case '#':
  2766. return (name[1] == '#') ?
  2767. pc_setaccountreg2(sd, num, val) :
  2768. pc_setaccountreg(sd, num, val);
  2769. case '.':
  2770. {
  2771. struct reg_db *n = (ref) ? ref : (name[1] == '@') ? &st->stack->scope : &st->script->local;
  2772. if( n ) {
  2773. if( val != 0 ) {
  2774. i64db_iput(n->vars, num, val);
  2775. if( script_getvaridx(num) )
  2776. script_array_update(n, num, false);
  2777. } else {
  2778. i64db_remove(n->vars, num);
  2779. if( script_getvaridx(num) )
  2780. script_array_update(n, num, true);
  2781. }
  2782. }
  2783. }
  2784. return 1;
  2785. case '\'':
  2786. {
  2787. unsigned short instance_id = script_instancegetid(st);
  2788. if( instance_id ) {
  2789. if( val != 0 ) {
  2790. i64db_iput(instance_data[instance_id].regs.vars, num, val);
  2791. if( script_getvaridx(num) )
  2792. script_array_update(&instance_data[instance_id].regs, num, false);
  2793. } else {
  2794. i64db_remove(instance_data[instance_id].regs.vars, num);
  2795. if (script_getvaridx(num))
  2796. script_array_update(&instance_data[instance_id].regs, num, true);
  2797. }
  2798. } else {
  2799. ShowError("script_set_reg: cannot write instance variable '%s', NPC not in a instance!\n", name);
  2800. script_reportsrc(st);
  2801. }
  2802. return 1;
  2803. }
  2804. default:
  2805. return pc_setglobalreg(sd, num, val);
  2806. }
  2807. }
  2808. }
  2809. int set_var(struct map_session_data* sd, char* name, void* val)
  2810. {
  2811. return set_reg(NULL, sd, reference_uid(add_str(name),0), name, val, NULL);
  2812. }
  2813. void setd_sub(struct script_state *st, TBL_PC *sd, const char *varname, int elem, void *value, struct reg_db *ref)
  2814. {
  2815. set_reg(st, sd, reference_uid(add_str(varname),elem), varname, value, ref);
  2816. }
  2817. /**
  2818. * Converts the data to a string
  2819. * @param st
  2820. * @param data
  2821. * @param sd
  2822. */
  2823. const char* conv_str_(struct script_state* st, struct script_data* data, struct map_session_data *sd)
  2824. {
  2825. char* p;
  2826. get_val_(st, data, sd);
  2827. if( data_isstring(data) )
  2828. {// nothing to convert
  2829. }
  2830. else if( data_isint(data) )
  2831. {// int -> string
  2832. CREATE(p, char, ITEM_NAME_LENGTH);
  2833. snprintf(p, ITEM_NAME_LENGTH, "%"PRId64"", data->u.num);
  2834. p[ITEM_NAME_LENGTH-1] = '\0';
  2835. data->type = C_STR;
  2836. data->u.str = p;
  2837. }
  2838. else if( data_isreference(data) )
  2839. {// reference -> string
  2840. //##TODO when does this happen (check get_val) [FlavioJS]
  2841. data->type = C_CONSTSTR;
  2842. data->u.str = reference_getname(data);
  2843. }
  2844. else
  2845. {// unsupported data type
  2846. ShowError("script:conv_str: cannot convert to string, defaulting to \"\"\n");
  2847. script_reportdata(data);
  2848. script_reportsrc(st);
  2849. data->type = C_CONSTSTR;
  2850. data->u.str = "";
  2851. }
  2852. return data->u.str;
  2853. }
  2854. const char* conv_str(struct script_state* st, struct script_data* data)
  2855. {
  2856. return conv_str_(st, data, NULL);
  2857. }
  2858. /**
  2859. * Converts the data to an int
  2860. * @param st
  2861. * @param data
  2862. * @param sd
  2863. */
  2864. int conv_num_(struct script_state* st, struct script_data* data, struct map_session_data *sd)
  2865. {
  2866. get_val_(st, data, sd);
  2867. if( data_isint(data) )
  2868. {// nothing to convert
  2869. }
  2870. else if( data_isstring(data) )
  2871. {// string -> int
  2872. // the result does not overflow or underflow, it is capped instead
  2873. // ex: 999999999999 is capped to INT_MAX (2147483647)
  2874. char* p = data->u.str;
  2875. long num;
  2876. errno = 0;
  2877. num = strtol(data->u.str, NULL, 10);// change radix to 0 to support octal numbers "o377" and hex numbers "0xFF"
  2878. if( errno == ERANGE
  2879. #if LONG_MAX > INT_MAX
  2880. || num < INT_MIN || num > INT_MAX
  2881. #endif
  2882. )
  2883. {
  2884. if( num <= INT_MIN )
  2885. {
  2886. num = INT_MIN;
  2887. ShowError("script:conv_num: underflow detected, capping to %ld\n", num);
  2888. }
  2889. else//if( num >= INT_MAX )
  2890. {
  2891. num = INT_MAX;
  2892. ShowError("script:conv_num: overflow detected, capping to %ld\n", num);
  2893. }
  2894. script_reportdata(data);
  2895. script_reportsrc(st);
  2896. }
  2897. if( data->type == C_STR )
  2898. aFree(p);
  2899. data->type = C_INT;
  2900. data->u.num = (int)num;
  2901. }
  2902. #if 0
  2903. // FIXME this function is being used to retrieve the position of labels and
  2904. // probably other stuff [FlavioJS]
  2905. else
  2906. {// unsupported data type
  2907. ShowError("script:conv_num: cannot convert to number, defaulting to 0\n");
  2908. script_reportdata(data);
  2909. script_reportsrc(st);
  2910. data->type = C_INT;
  2911. data->u.num = 0;
  2912. }
  2913. #endif
  2914. return (int)data->u.num;
  2915. }
  2916. int conv_num(struct script_state* st, struct script_data* data)
  2917. {
  2918. return conv_num_(st, data, NULL);
  2919. }
  2920. //
  2921. // Stack operations
  2922. //
  2923. /// Increases the size of the stack
  2924. void stack_expand(struct script_stack* stack)
  2925. {
  2926. stack->sp_max += 64;
  2927. stack->stack_data = (struct script_data*)aRealloc(stack->stack_data,
  2928. stack->sp_max * sizeof(stack->stack_data[0]) );
  2929. memset(stack->stack_data + (stack->sp_max - 64), 0,
  2930. 64 * sizeof(stack->stack_data[0]) );
  2931. }
  2932. /// Pushes a value into the stack
  2933. #define push_val(stack,type,val) push_val2(stack, type, val, NULL)
  2934. /// Pushes a value into the stack (with reference)
  2935. struct script_data* push_val2(struct script_stack* stack, enum c_op type, int64 val, struct reg_db *ref)
  2936. {
  2937. if( stack->sp >= stack->sp_max )
  2938. stack_expand(stack);
  2939. stack->stack_data[stack->sp].type = type;
  2940. stack->stack_data[stack->sp].u.num = val;
  2941. stack->stack_data[stack->sp].ref = ref;
  2942. stack->sp++;
  2943. return &stack->stack_data[stack->sp-1];
  2944. }
  2945. /// Pushes a string into the stack
  2946. struct script_data* push_str(struct script_stack* stack, enum c_op type, char* str)
  2947. {
  2948. if( stack->sp >= stack->sp_max )
  2949. stack_expand(stack);
  2950. stack->stack_data[stack->sp].type = type;
  2951. stack->stack_data[stack->sp].u.str = str;
  2952. stack->stack_data[stack->sp].ref = NULL;
  2953. stack->sp++;
  2954. return &stack->stack_data[stack->sp-1];
  2955. }
  2956. /// Pushes a retinfo into the stack
  2957. struct script_data* push_retinfo(struct script_stack* stack, struct script_retinfo* ri, struct reg_db *ref)
  2958. {
  2959. if( stack->sp >= stack->sp_max )
  2960. stack_expand(stack);
  2961. stack->stack_data[stack->sp].type = C_RETINFO;
  2962. stack->stack_data[stack->sp].u.ri = ri;
  2963. stack->stack_data[stack->sp].ref = ref;
  2964. stack->sp++;
  2965. return &stack->stack_data[stack->sp-1];
  2966. }
  2967. /// Pushes a copy of the target position into the stack
  2968. struct script_data* push_copy(struct script_stack* stack, int pos)
  2969. {
  2970. switch( stack->stack_data[pos].type ) {
  2971. case C_CONSTSTR:
  2972. return push_str(stack, C_CONSTSTR, stack->stack_data[pos].u.str);
  2973. break;
  2974. case C_STR:
  2975. return push_str(stack, C_STR, aStrdup(stack->stack_data[pos].u.str));
  2976. break;
  2977. case C_RETINFO:
  2978. ShowFatalError("script:push_copy: can't create copies of C_RETINFO. Exiting...\n");
  2979. exit(1);
  2980. break;
  2981. default:
  2982. return push_val2(
  2983. stack,stack->stack_data[pos].type,
  2984. stack->stack_data[pos].u.num,
  2985. stack->stack_data[pos].ref
  2986. );
  2987. break;
  2988. }
  2989. }
  2990. /// Removes the values in indexes [start,end] from the stack.
  2991. /// Adjusts all stack pointers.
  2992. void pop_stack(struct script_state* st, int start, int end)
  2993. {
  2994. struct script_stack* stack = st->stack;
  2995. struct script_data* data;
  2996. int i;
  2997. if( start < 0 )
  2998. start = 0;
  2999. if( end > stack->sp )
  3000. end = stack->sp;
  3001. if( start >= end )
  3002. return;// nothing to pop
  3003. // free stack elements
  3004. for( i = start; i < end; i++ )
  3005. {
  3006. data = &stack->stack_data[i];
  3007. if( data->type == C_STR )
  3008. aFree(data->u.str);
  3009. if( data->type == C_RETINFO ) {
  3010. struct script_retinfo* ri = data->u.ri;
  3011. if (ri->scope.vars) {
  3012. script_free_vars(ri->scope.vars);
  3013. ri->scope.vars = NULL;
  3014. }
  3015. if (ri->scope.arrays) {
  3016. ri->scope.arrays->destroy(ri->scope.arrays, script_free_array_db);
  3017. ri->scope.arrays = NULL;
  3018. }
  3019. if( data->ref )
  3020. aFree(data->ref);
  3021. aFree(ri);
  3022. }
  3023. data->type = C_NOP;
  3024. }
  3025. // move the rest of the elements
  3026. if( stack->sp > end )
  3027. {
  3028. memmove(&stack->stack_data[start], &stack->stack_data[end], sizeof(stack->stack_data[0])*(stack->sp - end));
  3029. for( i = start + stack->sp - end; i < stack->sp; ++i )
  3030. stack->stack_data[i].type = C_NOP;
  3031. }
  3032. // adjust stack pointers
  3033. if( st->start > end ) st->start -= end - start;
  3034. else if( st->start > start ) st->start = start;
  3035. if( st->end > end ) st->end -= end - start;
  3036. else if( st->end > start ) st->end = start;
  3037. if( stack->defsp > end ) stack->defsp -= end - start;
  3038. else if( stack->defsp > start ) stack->defsp = start;
  3039. stack->sp -= end - start;
  3040. }
  3041. ///
  3042. ///
  3043. ///
  3044. /*==========================================
  3045. * Release script dependent variable, dependent variable of function
  3046. *------------------------------------------*/
  3047. void script_free_vars(struct DBMap* storage)
  3048. {
  3049. if( storage ) {
  3050. // destroy the storage construct containing the variables
  3051. db_destroy(storage);
  3052. }
  3053. }
  3054. void script_free_code(struct script_code* code)
  3055. {
  3056. nullpo_retv(code);
  3057. if (code->instances)
  3058. script_stop_instances(code);
  3059. script_free_vars(code->local.vars);
  3060. if (code->local.arrays)
  3061. code->local.arrays->destroy(code->local.arrays, script_free_array_db);
  3062. aFree(code->script_buf);
  3063. aFree(code);
  3064. }
  3065. /// Creates a new script state.
  3066. ///
  3067. /// @param script Script code
  3068. /// @param pos Position in the code
  3069. /// @param rid Who is running the script (attached player)
  3070. /// @param oid Where the code is being run (npc 'object')
  3071. /// @return Script state
  3072. struct script_state* script_alloc_state(struct script_code* rootscript, int pos, int rid, int oid)
  3073. {
  3074. struct script_state* st;
  3075. st = ers_alloc(st_ers, struct script_state);
  3076. st->stack = ers_alloc(stack_ers, struct script_stack);
  3077. st->stack->sp = 0;
  3078. st->stack->sp_max = 64;
  3079. CREATE(st->stack->stack_data, struct script_data, st->stack->sp_max);
  3080. st->stack->defsp = st->stack->sp;
  3081. st->stack->scope.vars = i64db_alloc(DB_OPT_RELEASE_DATA);
  3082. st->stack->scope.arrays = NULL;
  3083. st->state = RUN;
  3084. st->script = rootscript;
  3085. st->pos = pos;
  3086. st->rid = rid;
  3087. st->oid = oid;
  3088. st->sleep.timer = INVALID_TIMER;
  3089. st->npc_item_flag = battle_config.item_enabled_npc;
  3090. if( st->script->instances != USHRT_MAX )
  3091. st->script->instances++;
  3092. else {
  3093. struct npc_data *nd = map_id2nd(oid);
  3094. ShowError("Over 65k instances of '%s' script are being run!\n",nd ? nd->name : "unknown");
  3095. }
  3096. if (!st->script->local.vars)
  3097. st->script->local.vars = i64db_alloc(DB_OPT_RELEASE_DATA);
  3098. st->id = next_id++;
  3099. active_scripts++;
  3100. idb_put(st_db, st->id, st);
  3101. return st;
  3102. }
  3103. /// Frees a script state.
  3104. ///
  3105. /// @param st Script state
  3106. void script_free_state(struct script_state* st)
  3107. {
  3108. if (idb_exists(st_db, st->id)) {
  3109. struct map_session_data *sd = st->rid ? map_id2sd(st->rid) : NULL;
  3110. if (st->bk_st) // backup was not restored
  3111. 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);
  3112. if (sd && sd->st == st) { // Current script is aborted.
  3113. if(sd->state.using_fake_npc) {
  3114. clif_clearunit_single(sd->npc_id, CLR_OUTSIGHT, sd->fd);
  3115. sd->state.using_fake_npc = 0;
  3116. }
  3117. sd->st = NULL;
  3118. sd->npc_id = 0;
  3119. }
  3120. if (st->sleep.timer != INVALID_TIMER)
  3121. delete_timer(st->sleep.timer, run_script_timer);
  3122. if (st->stack) {
  3123. script_free_vars(st->stack->scope.vars);
  3124. if (st->stack->scope.arrays)
  3125. st->stack->scope.arrays->destroy(st->stack->scope.arrays, script_free_array_db);
  3126. pop_stack(st, 0, st->stack->sp);
  3127. aFree(st->stack->stack_data);
  3128. ers_free(stack_ers, st->stack);
  3129. st->stack = NULL;
  3130. }
  3131. if (st->script && st->script->instances != USHRT_MAX && --st->script->instances == 0) {
  3132. if (st->script->local.vars && !db_size(st->script->local.vars)) {
  3133. script_free_vars(st->script->local.vars);
  3134. st->script->local.vars = NULL;
  3135. }
  3136. if (st->script->local.arrays && !db_size(st->script->local.arrays)) {
  3137. st->script->local.arrays->destroy(st->script->local.arrays, script_free_array_db);
  3138. st->script->local.arrays = NULL;
  3139. }
  3140. }
  3141. st->pos = -1;
  3142. idb_remove(st_db, st->id);
  3143. ers_free(st_ers, st);
  3144. if (--active_scripts == 0)
  3145. next_id = 0;
  3146. }
  3147. }
  3148. //
  3149. // Main execution unit
  3150. //
  3151. /*==========================================
  3152. * Read command
  3153. *------------------------------------------*/
  3154. c_op get_com(unsigned char *script,int *pos)
  3155. {
  3156. int i = 0, j = 0;
  3157. if(script[*pos]>=0x80){
  3158. return C_INT;
  3159. }
  3160. while(script[*pos]>=0x40){
  3161. i=script[(*pos)++]<<j;
  3162. j+=6;
  3163. }
  3164. return (c_op)(i+(script[(*pos)++]<<j));
  3165. }
  3166. /*==========================================
  3167. * Income figures
  3168. *------------------------------------------*/
  3169. int get_num(unsigned char *script,int *pos)
  3170. {
  3171. int i,j;
  3172. i=0; j=0;
  3173. while(script[*pos]>=0xc0){
  3174. i+=(script[(*pos)++]&0x7f)<<j;
  3175. j+=6;
  3176. }
  3177. return i+((script[(*pos)++]&0x7f)<<j);
  3178. }
  3179. /// Ternary operators
  3180. /// test ? if_true : if_false
  3181. void op_3(struct script_state* st, int op)
  3182. {
  3183. struct script_data* data;
  3184. int flag = 0;
  3185. data = script_getdatatop(st, -3);
  3186. get_val(st, data);
  3187. if( data_isstring(data) )
  3188. flag = data->u.str[0];// "" -> false
  3189. else if( data_isint(data) )
  3190. flag = data->u.num == 0 ? 0 : 1;// 0 -> false
  3191. else
  3192. {
  3193. ShowError("script:op_3: invalid data for the ternary operator test\n");
  3194. script_reportdata(data);
  3195. script_reportsrc(st);
  3196. script_removetop(st, -3, 0);
  3197. script_pushnil(st);
  3198. return;
  3199. }
  3200. if( flag )
  3201. script_pushcopytop(st, -2);
  3202. else
  3203. script_pushcopytop(st, -1);
  3204. script_removetop(st, -4, -1);
  3205. }
  3206. /// Binary string operators
  3207. /// s1 EQ s2 -> i
  3208. /// s1 NE s2 -> i
  3209. /// s1 GT s2 -> i
  3210. /// s1 GE s2 -> i
  3211. /// s1 LT s2 -> i
  3212. /// s1 LE s2 -> i
  3213. /// s1 ADD s2 -> s
  3214. void op_2str(struct script_state* st, int op, const char* s1, const char* s2)
  3215. {
  3216. int a = 0;
  3217. switch(op){
  3218. case C_EQ: a = (strcmp(s1,s2) == 0); break;
  3219. case C_NE: a = (strcmp(s1,s2) != 0); break;
  3220. case C_GT: a = (strcmp(s1,s2) > 0); break;
  3221. case C_GE: a = (strcmp(s1,s2) >= 0); break;
  3222. case C_LT: a = (strcmp(s1,s2) < 0); break;
  3223. case C_LE: a = (strcmp(s1,s2) <= 0); break;
  3224. case C_ADD:
  3225. {
  3226. char* buf = (char *)aMalloc((strlen(s1)+strlen(s2)+1)*sizeof(char));
  3227. strcpy(buf, s1);
  3228. strcat(buf, s2);
  3229. script_pushstr(st, buf);
  3230. return;
  3231. }
  3232. default:
  3233. ShowError("script:op2_str: unexpected string operator %s\n", script_op2name(op));
  3234. script_reportsrc(st);
  3235. script_pushnil(st);
  3236. st->state = END;
  3237. return;
  3238. }
  3239. script_pushint(st,a);
  3240. }
  3241. /// Binary number operators
  3242. /// i OP i -> i
  3243. void op_2num(struct script_state* st, int op, int i1, int i2)
  3244. {
  3245. int ret;
  3246. double ret_double;
  3247. switch( op ) {
  3248. case C_AND: ret = i1 & i2; break;
  3249. case C_OR: ret = i1 | i2; break;
  3250. case C_XOR: ret = i1 ^ i2; break;
  3251. case C_LAND: ret = (i1 && i2); break;
  3252. case C_LOR: ret = (i1 || i2); break;
  3253. case C_EQ: ret = (i1 == i2); break;
  3254. case C_NE: ret = (i1 != i2); break;
  3255. case C_GT: ret = (i1 > i2); break;
  3256. case C_GE: ret = (i1 >= i2); break;
  3257. case C_LT: ret = (i1 < i2); break;
  3258. case C_LE: ret = (i1 <= i2); break;
  3259. case C_R_SHIFT: ret = i1>>i2; break;
  3260. case C_L_SHIFT: ret = i1<<i2; break;
  3261. case C_DIV:
  3262. case C_MOD:
  3263. if( i2 == 0 )
  3264. {
  3265. ShowError("script:op_2num: division by zero detected op=%s i1=%d i2=%d\n", script_op2name(op), i1, i2);
  3266. script_reportsrc(st);
  3267. script_pushnil(st);
  3268. st->state = END;
  3269. return;
  3270. }
  3271. else if( op == C_DIV )
  3272. ret = i1 / i2;
  3273. else//if( op == C_MOD )
  3274. ret = i1 % i2;
  3275. break;
  3276. default:
  3277. switch( op ) {// operators that can overflow/underflow
  3278. case C_ADD: ret = i1 + i2; ret_double = (double)i1 + (double)i2; break;
  3279. case C_SUB: ret = i1 - i2; ret_double = (double)i1 - (double)i2; break;
  3280. case C_MUL: ret = i1 * i2; ret_double = (double)i1 * (double)i2; break;
  3281. default:
  3282. ShowError("script:op_2num: unexpected number operator %s i1=%d i2=%d\n", script_op2name(op), i1, i2);
  3283. script_reportsrc(st);
  3284. script_pushnil(st);
  3285. return;
  3286. }
  3287. if( ret_double < (double)INT_MIN ) {
  3288. ShowWarning("script:op_2num: underflow detected op=%s i1=%d i2=%d\n", script_op2name(op), i1, i2);
  3289. script_reportsrc(st);
  3290. ret = INT_MIN;
  3291. }
  3292. else if( ret_double > (double)INT_MAX ) {
  3293. ShowWarning("script:op_2num: overflow detected op=%s i1=%d i2=%d\n", script_op2name(op), i1, i2);
  3294. script_reportsrc(st);
  3295. ret = INT_MAX;
  3296. }
  3297. }
  3298. script_pushint(st, ret);
  3299. }
  3300. /// Binary operators
  3301. void op_2(struct script_state *st, int op)
  3302. {
  3303. struct script_data* left, leftref;
  3304. struct script_data* right;
  3305. leftref.type = C_NOP;
  3306. left = script_getdatatop(st, -2);
  3307. right = script_getdatatop(st, -1);
  3308. if (st->op2ref) {
  3309. if (data_isreference(left)) {
  3310. leftref = *left;
  3311. }
  3312. st->op2ref = 0;
  3313. }
  3314. get_val(st, left);
  3315. get_val(st, right);
  3316. // automatic conversions
  3317. switch( op )
  3318. {
  3319. case C_ADD:
  3320. if( data_isint(left) && data_isstring(right) )
  3321. {// convert int-string to string-string
  3322. conv_str(st, left);
  3323. }
  3324. else if( data_isstring(left) && data_isint(right) )
  3325. {// convert string-int to string-string
  3326. conv_str(st, right);
  3327. }
  3328. break;
  3329. }
  3330. if( data_isstring(left) && data_isstring(right) )
  3331. {// ss => op_2str
  3332. op_2str(st, op, left->u.str, right->u.str);
  3333. script_removetop(st, leftref.type == C_NOP ? -3 : -2, -1);// pop the two values before the top one
  3334. if (leftref.type != C_NOP)
  3335. {
  3336. if (left->type == C_STR) // don't free C_CONSTSTR
  3337. aFree(left->u.str);
  3338. *left = leftref;
  3339. }
  3340. }
  3341. else if( data_isint(left) && data_isint(right) )
  3342. {// ii => op_2num
  3343. int i1 = (int)left->u.num;
  3344. int i2 = (int)right->u.num;
  3345. script_removetop(st, leftref.type == C_NOP ? -2 : -1, 0);
  3346. op_2num(st, op, i1, i2);
  3347. if (leftref.type != C_NOP)
  3348. *left = leftref;
  3349. }
  3350. else
  3351. {// invalid argument
  3352. ShowError("script:op_2: invalid data for operator %s\n", script_op2name(op));
  3353. script_reportdata(left);
  3354. script_reportdata(right);
  3355. script_reportsrc(st);
  3356. script_removetop(st, -2, 0);
  3357. script_pushnil(st);
  3358. st->state = END;
  3359. }
  3360. }
  3361. /// Unary operators
  3362. /// NEG i -> i
  3363. /// NOT i -> i
  3364. /// LNOT i -> i
  3365. void op_1(struct script_state* st, int op)
  3366. {
  3367. struct script_data* data;
  3368. int i1;
  3369. data = script_getdatatop(st, -1);
  3370. get_val(st, data);
  3371. if( !data_isint(data) )
  3372. {// not a number
  3373. ShowError("script:op_1: argument is not a number (op=%s)\n", script_op2name(op));
  3374. script_reportdata(data);
  3375. script_reportsrc(st);
  3376. script_pushnil(st);
  3377. st->state = END;
  3378. return;
  3379. }
  3380. i1 = (int)data->u.num;
  3381. script_removetop(st, -1, 0);
  3382. switch( op )
  3383. {
  3384. case C_NEG: i1 = -i1; break;
  3385. case C_NOT: i1 = ~i1; break;
  3386. case C_LNOT: i1 = !i1; break;
  3387. default:
  3388. ShowError("script:op_1: unexpected operator %s i1=%d\n", script_op2name(op), i1);
  3389. script_reportsrc(st);
  3390. script_pushnil(st);
  3391. st->state = END;
  3392. return;
  3393. }
  3394. script_pushint(st, i1);
  3395. }
  3396. /// Checks the type of all arguments passed to a built-in function.
  3397. ///
  3398. /// @param st Script state whose stack arguments should be inspected.
  3399. /// @param func Built-in function for which the arguments are intended.
  3400. static void script_check_buildin_argtype(struct script_state* st, int func)
  3401. {
  3402. int idx, invalid = 0;
  3403. for( idx = 2; script_hasdata(st, idx); idx++ )
  3404. {
  3405. struct script_data* data = script_getdata(st, idx);
  3406. script_function* sf = &buildin_func[str_data[func].val];
  3407. char type = sf->arg[idx-2];
  3408. if( type == '?' || type == '*' )
  3409. {// optional argument or unknown number of optional parameters ( no types are after this )
  3410. break;
  3411. }
  3412. else if( type == 0 )
  3413. {// more arguments than necessary ( should not happen, as it is checked before )
  3414. ShowWarning("Found more arguments than necessary. unexpected arg type %s\n",script_op2name(data->type));
  3415. invalid++;
  3416. break;
  3417. }
  3418. else
  3419. {
  3420. const char* name = NULL;
  3421. if( data_isreference(data) )
  3422. {// get name for variables to determine the type they refer to
  3423. name = reference_getname(data);
  3424. }
  3425. switch( type )
  3426. {
  3427. case 'v':
  3428. if( !data_isstring(data) && !data_isint(data) && !data_isreference(data) )
  3429. {// variant
  3430. ShowWarning("Unexpected type for argument %d. Expected string, number or variable.\n", idx-1);
  3431. script_reportdata(data);
  3432. invalid++;
  3433. }
  3434. break;
  3435. case 's':
  3436. if( !data_isstring(data) && !( data_isreference(data) && is_string_variable(name) ) )
  3437. {// string
  3438. ShowWarning("Unexpected type for argument %d. Expected string.\n", idx-1);
  3439. script_reportdata(data);
  3440. invalid++;
  3441. }
  3442. break;
  3443. case 'i':
  3444. if( !data_isint(data) && !( data_isreference(data) && ( reference_toparam(data) || reference_toconstant(data) || !is_string_variable(name) ) ) )
  3445. {// int ( params and constants are always int )
  3446. ShowWarning("Unexpected type for argument %d. Expected number.\n", idx-1);
  3447. script_reportdata(data);
  3448. invalid++;
  3449. }
  3450. break;
  3451. case 'r':
  3452. if( !data_isreference(data) )
  3453. {// variables
  3454. ShowWarning("Unexpected type for argument %d. Expected variable, got %s.\n", idx-1,script_op2name(data->type));
  3455. script_reportdata(data);
  3456. invalid++;
  3457. }
  3458. break;
  3459. case 'l':
  3460. if( !data_islabel(data) && !data_isfunclabel(data) )
  3461. {// label
  3462. ShowWarning("Unexpected type for argument %d. Expected label, got %s\n", idx-1,script_op2name(data->type));
  3463. script_reportdata(data);
  3464. invalid++;
  3465. }
  3466. break;
  3467. }
  3468. }
  3469. }
  3470. if(invalid)
  3471. {
  3472. ShowDebug("Function: %s\n", get_str(func));
  3473. script_reportsrc(st);
  3474. }
  3475. }
  3476. /// Executes a buildin command.
  3477. /// Stack: C_NAME(<command>) C_ARG <arg0> <arg1> ... <argN>
  3478. int run_func(struct script_state *st)
  3479. {
  3480. struct script_data* data;
  3481. int i,start_sp,end_sp,func;
  3482. end_sp = st->stack->sp;// position after the last argument
  3483. for( i = end_sp-1; i > 0 ; --i )
  3484. if( st->stack->stack_data[i].type == C_ARG )
  3485. break;
  3486. if( i == 0 ) {
  3487. ShowError("script:run_func: C_ARG not found. please report this!!!\n");
  3488. st->state = END;
  3489. script_reportsrc(st);
  3490. return 1;
  3491. }
  3492. start_sp = i-1;// C_NAME of the command
  3493. st->start = start_sp;
  3494. st->end = end_sp;
  3495. data = &st->stack->stack_data[st->start];
  3496. if( data->type == C_NAME && str_data[data->u.num].type == C_FUNC ) {
  3497. func = (int)data->u.num;
  3498. st->funcname = reference_getname(data);
  3499. } else {
  3500. ShowError("script:run_func: not a buildin command.\n");
  3501. script_reportdata(data);
  3502. script_reportsrc(st);
  3503. st->state = END;
  3504. return 1;
  3505. }
  3506. if( script_config.warn_func_mismatch_argtypes ) {
  3507. script_check_buildin_argtype(st, func);
  3508. }
  3509. if(str_data[func].func) {
  3510. if (str_data[func].func(st) == SCRIPT_CMD_FAILURE) //Report error
  3511. script_reportsrc(st);
  3512. } else {
  3513. 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));
  3514. script_reportsrc(st);
  3515. st->state = END;
  3516. }
  3517. // Stack's datum are used when re-running functions [Eoe]
  3518. if( st->state == RERUNLINE )
  3519. return 0;
  3520. pop_stack(st, st->start, st->end);
  3521. if( st->state == RETFUNC ) {// return from a user-defined function
  3522. struct script_retinfo* ri;
  3523. int olddefsp = st->stack->defsp;
  3524. int nargs;
  3525. pop_stack(st, st->stack->defsp, st->start);// pop distractions from the stack
  3526. if( st->stack->defsp < 1 || st->stack->stack_data[st->stack->defsp-1].type != C_RETINFO )
  3527. {
  3528. ShowWarning("script:run_func: return without callfunc or callsub!\n");
  3529. script_reportsrc(st);
  3530. st->state = END;
  3531. return 1;
  3532. }
  3533. script_free_vars(st->stack->scope.vars);
  3534. st->stack->scope.arrays->destroy(st->stack->scope.arrays, script_free_array_db);
  3535. ri = st->stack->stack_data[st->stack->defsp-1].u.ri;
  3536. nargs = ri->nargs;
  3537. st->pos = ri->pos;
  3538. st->script = ri->script;
  3539. st->stack->scope.vars = ri->scope.vars;
  3540. st->stack->scope.arrays = ri->scope.arrays;
  3541. st->stack->defsp = ri->defsp;
  3542. memset(ri, 0, sizeof(struct script_retinfo));
  3543. pop_stack(st, olddefsp-nargs-1, olddefsp);// pop arguments and retinfo
  3544. st->state = GOTO;
  3545. }
  3546. return 0;
  3547. }
  3548. /*==========================================
  3549. * script execution
  3550. *------------------------------------------*/
  3551. void run_script(struct script_code *rootscript, int pos, int rid, int oid)
  3552. {
  3553. struct script_state *st;
  3554. if( rootscript == NULL || pos < 0 )
  3555. return;
  3556. // TODO In jAthena, this function can take over the pending script in the player. [FlavioJS]
  3557. // It is unclear how that can be triggered, so it needs the be traced/checked in more detail.
  3558. // NOTE At the time of this change, this function wasn't capable of taking over the script state because st->scriptroot was never set.
  3559. st = script_alloc_state(rootscript, pos, rid, oid);
  3560. run_script_main(st);
  3561. }
  3562. void script_stop_instances(struct script_code *code) {
  3563. DBIterator *iter;
  3564. struct script_state* st;
  3565. if( !active_scripts )
  3566. return; // Don't even bother.
  3567. iter = db_iterator(st_db);
  3568. for( st = dbi_first(iter); dbi_exists(iter); st = dbi_next(iter) ) {
  3569. if( st->script == code )
  3570. script_free_state(st);
  3571. }
  3572. dbi_destroy(iter);
  3573. }
  3574. /*==========================================
  3575. * Timer function for sleep
  3576. *------------------------------------------*/
  3577. int run_script_timer(int tid, unsigned int tick, int id, intptr_t data)
  3578. {
  3579. struct script_state *st = (struct script_state *)data;
  3580. // If it was a player before going to sleep and there is still a unit attached to the script
  3581. if( id != 0 && st->rid ){
  3582. struct map_session_data *sd = map_id2sd(st->rid);
  3583. // Attached player is offline or another unit type - should not happen
  3584. if( !sd ){
  3585. ShowWarning( "Script sleep timer called by an offline character or non player unit.\n" );
  3586. script_reportsrc(st);
  3587. st->rid = 0;
  3588. st->state = END;
  3589. // Character mismatch. Cancel execution.
  3590. }else if( sd->status.char_id != id ){
  3591. ShowWarning( "Script sleep timer detected a character mismatch CID %d != %d\n", sd->status.char_id, id );
  3592. script_reportsrc(st);
  3593. st->rid = 0;
  3594. st->state = END;
  3595. }
  3596. }
  3597. st->sleep.timer = INVALID_TIMER;
  3598. if(st->state != RERUNLINE)
  3599. st->sleep.tick = 0;
  3600. run_script_main(st);
  3601. return 0;
  3602. }
  3603. /// Detaches script state from possibly attached character and restores it's previous script if any.
  3604. ///
  3605. /// @param st Script state to detach.
  3606. /// @param dequeue_event Whether to schedule any queued events, when there was no previous script.
  3607. static void script_detach_state(struct script_state* st, bool dequeue_event)
  3608. {
  3609. struct map_session_data* sd;
  3610. if(st->rid && (sd = map_id2sd(st->rid))!=NULL) {
  3611. sd->st = st->bk_st;
  3612. sd->npc_id = st->bk_npcid;
  3613. sd->state.disable_atcommand_on_npc = 0;
  3614. if(st->bk_st) {
  3615. //Remove tag for removal.
  3616. st->bk_st = NULL;
  3617. st->bk_npcid = 0;
  3618. } else if(dequeue_event) {
  3619. #ifdef SECURE_NPCTIMEOUT
  3620. /**
  3621. * We're done with this NPC session, so we cancel the timer (if existent) and move on
  3622. **/
  3623. if( sd->npc_idle_timer != INVALID_TIMER ) {
  3624. delete_timer(sd->npc_idle_timer,npc_rr_secure_timeout_timer);
  3625. sd->npc_idle_timer = INVALID_TIMER;
  3626. }
  3627. #endif
  3628. npc_event_dequeue(sd);
  3629. }
  3630. }
  3631. else if(st->bk_st)
  3632. {// rid was set to 0, before detaching the script state
  3633. 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);
  3634. script_reportsrc(st->bk_st);
  3635. script_free_state(st->bk_st);
  3636. st->bk_st = NULL;
  3637. }
  3638. }
  3639. /// Attaches script state to possibly attached character and backups it's previous script, if any.
  3640. ///
  3641. /// @param st Script state to attach.
  3642. static void script_attach_state(struct script_state* st)
  3643. {
  3644. struct map_session_data* sd;
  3645. if(st->rid && (sd = map_id2sd(st->rid))!=NULL)
  3646. {
  3647. if(st!=sd->st)
  3648. {
  3649. if(st->bk_st)
  3650. {// there is already a backup
  3651. ShowDebug("script_attach_state: Previous script state lost (rid=%d, oid=%d, state=%d, bk_npcid=%d).\n", st->bk_st->rid, st->bk_st->oid, st->bk_st->state, st->bk_npcid);
  3652. }
  3653. st->bk_st = sd->st;
  3654. st->bk_npcid = sd->npc_id;
  3655. }
  3656. sd->st = st;
  3657. sd->npc_id = st->oid;
  3658. sd->npc_item_flag = st->npc_item_flag; // load default.
  3659. sd->state.disable_atcommand_on_npc = (!pc_has_permission(sd, PC_PERM_ENABLE_COMMAND));
  3660. #ifdef SECURE_NPCTIMEOUT
  3661. if( sd->npc_idle_timer == INVALID_TIMER )
  3662. sd->npc_idle_timer = add_timer(gettick() + (SECURE_NPCTIMEOUT_INTERVAL*1000),npc_rr_secure_timeout_timer,sd->bl.id,0);
  3663. sd->npc_idle_tick = gettick();
  3664. #endif
  3665. }
  3666. }
  3667. /*==========================================
  3668. * The main part of the script execution
  3669. *------------------------------------------*/
  3670. void run_script_main(struct script_state *st)
  3671. {
  3672. int cmdcount = script_config.check_cmdcount;
  3673. int gotocount = script_config.check_gotocount;
  3674. TBL_PC *sd;
  3675. struct script_stack *stack = st->stack;
  3676. script_attach_state(st);
  3677. if(st->state == RERUNLINE) {
  3678. run_func(st);
  3679. if(st->state == GOTO)
  3680. st->state = RUN;
  3681. } else if(st->state != END)
  3682. st->state = RUN;
  3683. while(st->state == RUN) {
  3684. enum c_op c = get_com(st->script->script_buf,&st->pos);
  3685. switch(c){
  3686. case C_EOL:
  3687. if( stack->defsp > stack->sp )
  3688. ShowError("script:run_script_main: unexpected stack position (defsp=%d sp=%d). please report this!!!\n", stack->defsp, stack->sp);
  3689. else
  3690. pop_stack(st, stack->defsp, stack->sp);// pop unused stack data. (unused return value)
  3691. break;
  3692. case C_INT:
  3693. push_val(stack,C_INT,get_num(st->script->script_buf,&st->pos));
  3694. break;
  3695. case C_POS:
  3696. case C_NAME:
  3697. push_val(stack,c,GETVALUE(st->script->script_buf,st->pos));
  3698. st->pos+=3;
  3699. break;
  3700. case C_ARG:
  3701. push_val(stack,c,0);
  3702. break;
  3703. case C_STR:
  3704. push_str(stack,C_CONSTSTR,(char*)(st->script->script_buf+st->pos));
  3705. while(st->script->script_buf[st->pos++]);
  3706. break;
  3707. case C_FUNC:
  3708. run_func(st);
  3709. if(st->state==GOTO){
  3710. st->state = RUN;
  3711. if( !st->freeloop && gotocount>0 && (--gotocount)<=0 ){
  3712. ShowError("script:run_script_main: infinity loop !\n");
  3713. script_reportsrc(st);
  3714. st->state=END;
  3715. }
  3716. }
  3717. break;
  3718. case C_REF:
  3719. st->op2ref = 1;
  3720. break;
  3721. case C_NEG:
  3722. case C_NOT:
  3723. case C_LNOT:
  3724. op_1(st ,c);
  3725. break;
  3726. case C_ADD:
  3727. case C_SUB:
  3728. case C_MUL:
  3729. case C_DIV:
  3730. case C_MOD:
  3731. case C_EQ:
  3732. case C_NE:
  3733. case C_GT:
  3734. case C_GE:
  3735. case C_LT:
  3736. case C_LE:
  3737. case C_AND:
  3738. case C_OR:
  3739. case C_XOR:
  3740. case C_LAND:
  3741. case C_LOR:
  3742. case C_R_SHIFT:
  3743. case C_L_SHIFT:
  3744. op_2(st, c);
  3745. break;
  3746. case C_OP3:
  3747. op_3(st, c);
  3748. break;
  3749. case C_NOP:
  3750. st->state=END;
  3751. break;
  3752. default:
  3753. ShowError("script:run_script_main:unknown command : %d @ %d\n",c,st->pos);
  3754. st->state=END;
  3755. break;
  3756. }
  3757. if( !st->freeloop && cmdcount>0 && (--cmdcount)<=0 ){
  3758. ShowError("script:run_script_main: infinity loop !\n");
  3759. script_reportsrc(st);
  3760. st->state=END;
  3761. }
  3762. }
  3763. if(st->sleep.tick > 0) {
  3764. //Restore previous script
  3765. script_detach_state(st, false);
  3766. //Delay execution
  3767. sd = map_id2sd(st->rid); // Get sd since script might have attached someone while running. [Inkfish]
  3768. st->sleep.charid = sd?sd->status.char_id:0;
  3769. st->sleep.timer = add_timer(gettick()+st->sleep.tick,
  3770. run_script_timer, st->sleep.charid, (intptr_t)st);
  3771. } else if(st->state != END && st->rid) {
  3772. //Resume later (st is already attached to player).
  3773. if(st->bk_st) {
  3774. ShowWarning("Unable to restore stack! Double continuation!\n");
  3775. //Report BOTH scripts to see if that can help somehow.
  3776. ShowDebug("Previous script (lost):\n");
  3777. script_reportsrc(st->bk_st);
  3778. ShowDebug("Current script:\n");
  3779. script_reportsrc(st);
  3780. script_free_state(st->bk_st);
  3781. st->bk_st = NULL;
  3782. }
  3783. } else {
  3784. //Dispose of script.
  3785. if ((sd = map_id2sd(st->rid))!=NULL)
  3786. { //Restore previous stack and save char.
  3787. if(sd->state.using_fake_npc){
  3788. clif_clearunit_single(sd->npc_id, CLR_OUTSIGHT, sd->fd);
  3789. sd->state.using_fake_npc = 0;
  3790. }
  3791. //Restore previous script if any.
  3792. script_detach_state(st, true);
  3793. if (sd->vars_dirty)
  3794. intif_saveregistry(sd);
  3795. }
  3796. script_free_state(st);
  3797. }
  3798. }
  3799. int script_config_read(char *cfgName)
  3800. {
  3801. int i;
  3802. char line[1024],w1[1024],w2[1024];
  3803. FILE *fp;
  3804. fp=fopen(cfgName,"r");
  3805. if(fp==NULL){
  3806. ShowError("File not found: %s\n", cfgName);
  3807. return 1;
  3808. }
  3809. while(fgets(line, sizeof(line), fp))
  3810. {
  3811. if(line[0] == '/' && line[1] == '/')
  3812. continue;
  3813. i=sscanf(line,"%1023[^:]: %1023[^\r\n]",w1,w2);
  3814. if(i!=2)
  3815. continue;
  3816. if(strcmpi(w1,"warn_func_mismatch_paramnum")==0) {
  3817. script_config.warn_func_mismatch_paramnum = config_switch(w2);
  3818. }
  3819. else if(strcmpi(w1,"check_cmdcount")==0) {
  3820. script_config.check_cmdcount = config_switch(w2);
  3821. }
  3822. else if(strcmpi(w1,"check_gotocount")==0) {
  3823. script_config.check_gotocount = config_switch(w2);
  3824. }
  3825. else if(strcmpi(w1,"input_min_value")==0) {
  3826. script_config.input_min_value = config_switch(w2);
  3827. }
  3828. else if(strcmpi(w1,"input_max_value")==0) {
  3829. script_config.input_max_value = config_switch(w2);
  3830. }
  3831. else if(strcmpi(w1,"warn_func_mismatch_argtypes")==0) {
  3832. script_config.warn_func_mismatch_argtypes = config_switch(w2);
  3833. }
  3834. else if(strcmpi(w1,"import")==0){
  3835. script_config_read(w2);
  3836. }
  3837. else {
  3838. ShowWarning("Unknown setting '%s' in file %s\n", w1, cfgName);
  3839. }
  3840. }
  3841. fclose(fp);
  3842. return 0;
  3843. }
  3844. /**
  3845. * @see DBApply
  3846. */
  3847. static int db_script_free_code_sub(DBKey key, DBData *data, va_list ap)
  3848. {
  3849. struct script_code *code = db_data2ptr(data);
  3850. if (code)
  3851. script_free_code(code);
  3852. return 0;
  3853. }
  3854. void script_run_autobonus(const char *autobonus, struct map_session_data *sd, unsigned int pos)
  3855. {
  3856. struct script_code *script = (struct script_code *)strdb_get(autobonus_db, autobonus);
  3857. if( script )
  3858. {
  3859. int j;
  3860. ARR_FIND( 0, EQI_MAX, j, sd->equip_index[j] >= 0 && sd->inventory.u.items_inventory[sd->equip_index[j]].equip == pos );
  3861. if( j < EQI_MAX ) {
  3862. //Single item autobonus
  3863. current_equip_item_index = sd->equip_index[j];
  3864. current_equip_combo_pos = 0;
  3865. } else {
  3866. //Combo autobonus
  3867. current_equip_item_index = -1;
  3868. current_equip_combo_pos = pos;
  3869. }
  3870. run_script(script,0,sd->bl.id,0);
  3871. }
  3872. }
  3873. void script_add_autobonus(const char *autobonus)
  3874. {
  3875. if( strdb_get(autobonus_db, autobonus) == NULL )
  3876. {
  3877. struct script_code *script = parse_script(autobonus, "autobonus", 0, 0);
  3878. if( script )
  3879. strdb_put(autobonus_db, autobonus, script);
  3880. }
  3881. }
  3882. /// resets a temporary character array variable to given value
  3883. void script_cleararray_pc(struct map_session_data* sd, const char* varname, void* value)
  3884. {
  3885. struct script_array *sa = NULL;
  3886. struct reg_db *src = NULL;
  3887. unsigned int i, *list = NULL, size = 0;
  3888. int key;
  3889. key = add_str(varname);
  3890. if( !(src = script_array_src(NULL,sd,varname,NULL) ) )
  3891. return;
  3892. if( value )
  3893. script_array_ensure_zero(NULL,sd,reference_uid(key,0), NULL);
  3894. if( !(sa = idb_get(src->arrays, key)) ) /* non-existent array, nothing to empty */
  3895. return;
  3896. size = sa->size;
  3897. list = script_array_cpy_list(sa);
  3898. for(i = 0; i < size; i++) {
  3899. set_reg(NULL,sd,reference_uid(key, list[i]),varname,value,NULL);
  3900. }
  3901. }
  3902. /// sets a temporary character array variable element idx to given value
  3903. /// @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.
  3904. void script_setarray_pc(struct map_session_data* sd, const char* varname, uint32 idx, void* value, int* refcache)
  3905. {
  3906. int key;
  3907. if( idx >= SCRIPT_MAX_ARRAYSIZE ) {
  3908. ShowError("script_setarray_pc: Variable '%s' has invalid index '%u' (char_id=%d).\n", varname, idx, sd->status.char_id);
  3909. return;
  3910. }
  3911. key = ( refcache && refcache[0] ) ? refcache[0] : add_str(varname);
  3912. set_reg(NULL,sd,reference_uid(key, idx),varname,value,NULL);
  3913. if( refcache ) {
  3914. // save to avoid repeated script->add_str calls
  3915. refcache[0] = key;
  3916. }
  3917. }
  3918. /**
  3919. * Clears persistent variables from memory
  3920. **/
  3921. int script_reg_destroy(DBKey key, DBData *data, va_list ap)
  3922. {
  3923. struct script_reg_state *src;
  3924. if( data->type != DB_DATA_PTR ) // got no need for those!
  3925. return 0;
  3926. src = db_data2ptr(data);
  3927. if( src->type ) {
  3928. struct script_reg_str *p = (struct script_reg_str *)src;
  3929. if( p->value )
  3930. aFree(p->value);
  3931. ers_free(str_reg_ers,p);
  3932. } else {
  3933. ers_free(num_reg_ers,(struct script_reg_num*)src);
  3934. }
  3935. return 0;
  3936. }
  3937. /**
  3938. * Clears a single persistent variable
  3939. **/
  3940. void script_reg_destroy_single(struct map_session_data *sd, int64 reg, struct script_reg_state *data)
  3941. {
  3942. i64db_remove(sd->regs.vars, reg);
  3943. if( data->type ) {
  3944. struct script_reg_str *p = (struct script_reg_str*)data;
  3945. if( p->value )
  3946. aFree(p->value);
  3947. ers_free(str_reg_ers,p);
  3948. } else {
  3949. ers_free(num_reg_ers,(struct script_reg_num*)data);
  3950. }
  3951. }
  3952. unsigned int *script_array_cpy_list(struct script_array *sa)
  3953. {
  3954. if( sa->size > generic_ui_array_size )
  3955. script_generic_ui_array_expand(sa->size);
  3956. memcpy(generic_ui_array, sa->members, sizeof(unsigned int)*sa->size);
  3957. return generic_ui_array;
  3958. }
  3959. void script_generic_ui_array_expand (unsigned int plus)
  3960. {
  3961. generic_ui_array_size += plus + 100;
  3962. RECREATE(generic_ui_array, unsigned int, generic_ui_array_size);
  3963. }
  3964. int buildin_query_sql_sub(struct script_state *st, Sql *handle);
  3965. #ifdef BETA_THREAD_TEST
  3966. /* used to receive items the queryThread has already processed */
  3967. int queryThread_timer(int tid, unsigned int tick, int id, intptr_t data) {
  3968. int i, cursor = 0;
  3969. bool allOk = true;
  3970. EnterSpinLock(&queryThreadLock);
  3971. for( i = 0; i < queryThreadData.count; i++ ) {
  3972. struct queryThreadEntry *entry = queryThreadData.entry[i];
  3973. if( !entry->ok ) {
  3974. allOk = false;
  3975. continue;
  3976. }
  3977. run_script_main(entry->st);
  3978. entry->st = NULL;/* empty entries */
  3979. aFree(entry);
  3980. queryThreadData.entry[i] = NULL;
  3981. }
  3982. if( allOk ) {
  3983. /* cancel the repeating timer -- it'll re-create itself when necessary, dont need to remain looping */
  3984. delete_timer(queryThreadData.timer, queryThread_timer);
  3985. queryThreadData.timer = INVALID_TIMER;
  3986. }
  3987. /* now lets clear the mess. */
  3988. for( i = 0; i < queryThreadData.count; i++ ) {
  3989. struct queryThreadEntry *entry = queryThreadData.entry[i];
  3990. if( entry == NULL )
  3991. continue;/* entry on hold */
  3992. /* move */
  3993. memmove(&queryThreadData.entry[cursor], &queryThreadData.entry[i], sizeof(struct queryThreadEntry*));
  3994. cursor++;
  3995. }
  3996. queryThreadData.count = cursor;
  3997. LeaveSpinLock(&queryThreadLock);
  3998. return 0;
  3999. }
  4000. void queryThread_add(struct script_state *st, bool type) {
  4001. int idx = 0;
  4002. struct queryThreadEntry* entry = NULL;
  4003. EnterSpinLock(&queryThreadLock);
  4004. if( queryThreadData.count++ != 0 )
  4005. RECREATE(queryThreadData.entry, struct queryThreadEntry* , queryThreadData.count);
  4006. idx = queryThreadData.count-1;
  4007. CREATE(queryThreadData.entry[idx],struct queryThreadEntry,1);
  4008. entry = queryThreadData.entry[idx];
  4009. entry->st = st;
  4010. entry->ok = false;
  4011. entry->type = type;
  4012. if( queryThreadData.timer == INVALID_TIMER ) { /* start the receiver timer */
  4013. queryThreadData.timer = add_timer_interval(gettick() + 100, queryThread_timer, 0, 0, 100);
  4014. }
  4015. LeaveSpinLock(&queryThreadLock);
  4016. /* unlock the queryThread */
  4017. racond_signal(queryThreadCond);
  4018. }
  4019. /* adds a new log to the queue */
  4020. void queryThread_log(char * entry, int length) {
  4021. int idx = logThreadData.count;
  4022. EnterSpinLock(&queryThreadLock);
  4023. if( logThreadData.count++ != 0 )
  4024. RECREATE(logThreadData.entry, char* , logThreadData.count);
  4025. CREATE(logThreadData.entry[idx], char, length + 1 );
  4026. safestrncpy(logThreadData.entry[idx], entry, length + 1 );
  4027. LeaveSpinLock(&queryThreadLock);
  4028. /* unlock the queryThread */
  4029. racond_signal(queryThreadCond);
  4030. }
  4031. /* queryThread_main */
  4032. static void *queryThread_main(void *x) {
  4033. Sql *queryThread_handle = Sql_Malloc();
  4034. int i;
  4035. if ( SQL_ERROR == Sql_Connect(queryThread_handle, map_server_id, map_server_pw, map_server_ip, map_server_port, map_server_db) ){
  4036. ShowError("Couldn't connect with uname='%s',passwd='%s',host='%s',port='%d',database='%s'\n",
  4037. map_server_id, map_server_pw, map_server_ip, map_server_port, map_server_db);
  4038. Sql_ShowDebug(queryThread_handle);
  4039. Sql_Free(queryThread_handle);
  4040. exit(EXIT_FAILURE);
  4041. }
  4042. if( strlen(default_codepage) > 0 )
  4043. if ( SQL_ERROR == Sql_SetEncoding(queryThread_handle, default_codepage) )
  4044. Sql_ShowDebug(queryThread_handle);
  4045. if( log_config.sql_logs ) {
  4046. logmysql_handle = Sql_Malloc();
  4047. if ( SQL_ERROR == Sql_Connect(logmysql_handle, log_db_id, log_db_pw, log_db_ip, log_db_port, log_db_db) ){
  4048. ShowError("Couldn't connect with uname='%s',passwd='%s',host='%s',port='%d',database='%s'\n",
  4049. log_db_id, log_db_pw, log_db_ip, log_db_port, log_db_db);
  4050. Sql_ShowDebug(logmysql_handle);
  4051. Sql_Free(logmysql_handle);
  4052. exit(EXIT_FAILURE);
  4053. }
  4054. if( strlen(default_codepage) > 0 )
  4055. if ( SQL_ERROR == Sql_SetEncoding(logmysql_handle, default_codepage) )
  4056. Sql_ShowDebug(logmysql_handle);
  4057. }
  4058. while( 1 ) {
  4059. if(queryThreadTerminate > 0)
  4060. break;
  4061. EnterSpinLock(&queryThreadLock);
  4062. /* mess with queryThreadData within the lock */
  4063. for( i = 0; i < queryThreadData.count; i++ ) {
  4064. struct queryThreadEntry *entry = queryThreadData.entry[i];
  4065. if( entry->ok )
  4066. continue;
  4067. else if ( !entry->st || !entry->st->stack ) {
  4068. entry->ok = true;/* dispose */
  4069. continue;
  4070. }
  4071. buildin_query_sql_sub(entry->st, entry->type ? logmysql_handle : queryThread_handle);
  4072. entry->ok = true;/* we're done with this */
  4073. }
  4074. /* also check for any logs in need to be sent */
  4075. if( log_config.sql_logs ) {
  4076. for( i = 0; i < logThreadData.count; i++ ) {
  4077. if( SQL_ERROR == Sql_Query(logmysql_handle, logThreadData.entry[i]) )
  4078. Sql_ShowDebug(logmysql_handle);
  4079. aFree(logThreadData.entry[i]);
  4080. }
  4081. logThreadData.count = 0;
  4082. }
  4083. LeaveSpinLock(&queryThreadLock);
  4084. ramutex_lock( queryThreadMutex );
  4085. racond_wait( queryThreadCond, queryThreadMutex, -1 );
  4086. ramutex_unlock( queryThreadMutex );
  4087. }
  4088. Sql_Free(queryThread_handle);
  4089. if( log_config.sql_logs ) {
  4090. Sql_Free(logmysql_handle);
  4091. }
  4092. return NULL;
  4093. }
  4094. #endif
  4095. /*==========================================
  4096. * Destructor
  4097. *------------------------------------------*/
  4098. void do_final_script() {
  4099. int i;
  4100. DBIterator *iter;
  4101. struct script_state *st;
  4102. #ifdef DEBUG_HASH
  4103. if (battle_config.etc_log)
  4104. {
  4105. FILE *fp = fopen("hash_dump.txt","wt");
  4106. if(fp) {
  4107. int count[SCRIPT_HASH_SIZE];
  4108. int count2[SCRIPT_HASH_SIZE]; // number of buckets with a certain number of items
  4109. int n=0;
  4110. int min=INT_MAX,max=0,zero=0;
  4111. double mean=0.0f;
  4112. double median=0.0f;
  4113. ShowNotice("Dumping script str hash information to hash_dump.txt\n");
  4114. memset(count, 0, sizeof(count));
  4115. fprintf(fp,"num : hash : data_name\n");
  4116. fprintf(fp,"---------------------------------------------------------------\n");
  4117. for(i=LABEL_START; i<str_num; i++) {
  4118. unsigned int h = calc_hash(get_str(i));
  4119. fprintf(fp,"%04d : %4u : %s\n",i,h, get_str(i));
  4120. ++count[h];
  4121. }
  4122. fprintf(fp,"--------------------\n\n");
  4123. memset(count2, 0, sizeof(count2));
  4124. for(i=0; i<SCRIPT_HASH_SIZE; i++) {
  4125. fprintf(fp," hash %3d = %d\n",i,count[i]);
  4126. if(min > count[i])
  4127. min = count[i]; // minimun count of collision
  4128. if(max < count[i])
  4129. max = count[i]; // maximun count of collision
  4130. if(count[i] == 0)
  4131. zero++;
  4132. ++count2[count[i]];
  4133. }
  4134. fprintf(fp,"\n--------------------\n items : buckets\n--------------------\n");
  4135. for( i=min; i <= max; ++i ){
  4136. fprintf(fp," %5d : %7d\n",i,count2[i]);
  4137. mean += 1.0f*i*count2[i]/SCRIPT_HASH_SIZE; // Note: this will always result in <nr labels>/<nr buckets>
  4138. }
  4139. for( i=min; i <= max; ++i ){
  4140. n += count2[i];
  4141. if( n*2 >= SCRIPT_HASH_SIZE )
  4142. {
  4143. if( SCRIPT_HASH_SIZE%2 == 0 && SCRIPT_HASH_SIZE/2 == n )
  4144. median = (i+i+1)/2.0f;
  4145. else
  4146. median = i;
  4147. break;
  4148. }
  4149. }
  4150. fprintf(fp,"--------------------\n min = %d, max = %d, zero = %d\n mean = %lf, median = %lf\n",min,max,zero,mean,median);
  4151. fclose(fp);
  4152. }
  4153. }
  4154. #endif
  4155. mapreg_final();
  4156. db_destroy(scriptlabel_db);
  4157. userfunc_db->destroy(userfunc_db, db_script_free_code_sub);
  4158. autobonus_db->destroy(autobonus_db, db_script_free_code_sub);
  4159. ers_destroy(array_ers);
  4160. if (generic_ui_array)
  4161. aFree(generic_ui_array);
  4162. iter = db_iterator(st_db);
  4163. for(st = dbi_first(iter); dbi_exists(iter); st = dbi_next(iter))
  4164. script_free_state(st);
  4165. dbi_destroy(iter);
  4166. if (str_data)
  4167. aFree(str_data);
  4168. if (str_buf)
  4169. aFree(str_buf);
  4170. for( i = 0; i < atcmd_binding_count; i++ ) {
  4171. aFree(atcmd_binding[i]);
  4172. }
  4173. if( atcmd_binding_count != 0 )
  4174. aFree(atcmd_binding);
  4175. ers_destroy(st_ers);
  4176. ers_destroy(stack_ers);
  4177. db_destroy(st_db);
  4178. #ifdef BETA_THREAD_TEST
  4179. /* QueryThread */
  4180. InterlockedIncrement(&queryThreadTerminate);
  4181. racond_signal(queryThreadCond);
  4182. rathread_wait(queryThread, NULL);
  4183. // Destroy cond var and mutex.
  4184. racond_destroy( queryThreadCond );
  4185. ramutex_destroy( queryThreadMutex );
  4186. /* Clear missing vars */
  4187. for( i = 0; i < queryThreadData.count; i++ ) {
  4188. aFree(queryThreadData.entry[i]);
  4189. }
  4190. aFree(queryThreadData.entry);
  4191. for( i = 0; i < logThreadData.count; i++ ) {
  4192. aFree(logThreadData.entry[i]);
  4193. }
  4194. aFree(logThreadData.entry);
  4195. #endif
  4196. }
  4197. /*==========================================
  4198. * Initialization
  4199. *------------------------------------------*/
  4200. void do_init_script(void) {
  4201. st_db = idb_alloc(DB_OPT_BASE);
  4202. userfunc_db = strdb_alloc(DB_OPT_DUP_KEY,0);
  4203. scriptlabel_db = strdb_alloc(DB_OPT_DUP_KEY,50);
  4204. autobonus_db = strdb_alloc(DB_OPT_DUP_KEY,0);
  4205. st_ers = ers_new(sizeof(struct script_state), "script.c::st_ers", ERS_CACHE_OPTIONS);
  4206. stack_ers = ers_new(sizeof(struct script_stack), "script.c::script_stack", ERS_OPT_FLEX_CHUNK);
  4207. array_ers = ers_new(sizeof(struct script_array), "script.c:array_ers", ERS_OPT_CLEAN|ERS_OPT_CLEAR); // TODO: This value is not yet in the enum
  4208. ers_chunk_size(st_ers, 10);
  4209. ers_chunk_size(stack_ers, 10);
  4210. active_scripts = 0;
  4211. next_id = 0;
  4212. mapreg_init();
  4213. #ifdef BETA_THREAD_TEST
  4214. CREATE(queryThreadData.entry, struct queryThreadEntry*, 1);
  4215. queryThreadData.count = 0;
  4216. CREATE(logThreadData.entry, char *, 1);
  4217. logThreadData.count = 0;
  4218. /* QueryThread Start */
  4219. InitializeSpinLock(&queryThreadLock);
  4220. queryThreadData.timer = INVALID_TIMER;
  4221. queryThreadTerminate = 0;
  4222. queryThreadMutex = ramutex_create();
  4223. queryThreadCond = racond_create();
  4224. queryThread = rathread_create(queryThread_main, NULL);
  4225. if(queryThread == NULL){
  4226. ShowFatalError("do_init_script: cannot spawn Query Thread.\n");
  4227. exit(EXIT_FAILURE);
  4228. }
  4229. add_timer_func_list(queryThread_timer, "queryThread_timer");
  4230. #endif
  4231. }
  4232. void script_reload(void) {
  4233. int i;
  4234. DBIterator *iter;
  4235. struct script_state *st;
  4236. #ifdef BETA_THREAD_TEST
  4237. /* we're reloading so any queries undergoing should be...exterminated. */
  4238. EnterSpinLock(&queryThreadLock);
  4239. for( i = 0; i < queryThreadData.count; i++ ) {
  4240. aFree(queryThreadData.entry[i]);
  4241. }
  4242. queryThreadData.count = 0;
  4243. if( queryThreadData.timer != INVALID_TIMER ) {
  4244. delete_timer(queryThreadData.timer, queryThread_timer);
  4245. queryThreadData.timer = INVALID_TIMER;
  4246. }
  4247. LeaveSpinLock(&queryThreadLock);
  4248. #endif
  4249. userfunc_db->clear(userfunc_db, db_script_free_code_sub);
  4250. db_clear(scriptlabel_db);
  4251. // @commands (script based)
  4252. // Clear bindings
  4253. for( i = 0; i < atcmd_binding_count; i++ ) {
  4254. aFree(atcmd_binding[i]);
  4255. }
  4256. if( atcmd_binding_count != 0 )
  4257. aFree(atcmd_binding);
  4258. atcmd_binding_count = 0;
  4259. iter = db_iterator(st_db);
  4260. for(st = dbi_first(iter); dbi_exists(iter); st = dbi_next(iter))
  4261. script_free_state(st);
  4262. dbi_destroy(iter);
  4263. db_clear(st_db);
  4264. mapreg_reload();
  4265. }
  4266. //-----------------------------------------------------------------------------
  4267. // buildin functions
  4268. //
  4269. #define BUILDIN_DEF(x,args) { buildin_ ## x , #x , args }
  4270. #define BUILDIN_DEF2(x,x2,args) { buildin_ ## x , x2 , args }
  4271. #define BUILDIN_FUNC(x) int buildin_ ## x (struct script_state* st)
  4272. /////////////////////////////////////////////////////////////////////
  4273. // NPC interaction
  4274. //
  4275. /// Appends a message to the npc dialog.
  4276. /// If a dialog doesn't exist yet, one is created.
  4277. ///
  4278. /// mes "<message>";
  4279. BUILDIN_FUNC(mes)
  4280. {
  4281. TBL_PC* sd = script_rid2sd(st);
  4282. if( sd == NULL )
  4283. return SCRIPT_CMD_SUCCESS;
  4284. if( !script_hasdata(st, 3) )
  4285. {// only a single line detected in the script
  4286. clif_scriptmes(sd, st->oid, script_getstr(st, 2));
  4287. }
  4288. else
  4289. {// parse multiple lines as they exist
  4290. int i;
  4291. for( i = 2; script_hasdata(st, i); i++ )
  4292. {
  4293. // send the message to the client
  4294. clif_scriptmes(sd, st->oid, script_getstr(st, i));
  4295. }
  4296. }
  4297. st->mes_active = 1; // Invoking character has a NPC dialog box open.
  4298. return SCRIPT_CMD_SUCCESS;
  4299. }
  4300. /// Displays the button 'next' in the npc dialog.
  4301. /// The dialog text is cleared and the script continues when the button is pressed.
  4302. ///
  4303. /// next;
  4304. BUILDIN_FUNC(next)
  4305. {
  4306. TBL_PC* sd;
  4307. sd = script_rid2sd(st);
  4308. if( sd == NULL )
  4309. return SCRIPT_CMD_SUCCESS;
  4310. #ifdef SECURE_NPCTIMEOUT
  4311. sd->npc_idle_type = NPCT_WAIT;
  4312. #endif
  4313. st->state = STOP;
  4314. clif_scriptnext(sd, st->oid);
  4315. return SCRIPT_CMD_SUCCESS;
  4316. }
  4317. /// Ends the script and displays the button 'close' on the npc dialog.
  4318. /// The dialog is closed when the button is pressed.
  4319. ///
  4320. /// close;
  4321. BUILDIN_FUNC(close)
  4322. {
  4323. TBL_PC* sd;
  4324. sd = script_rid2sd(st);
  4325. if( sd == NULL )
  4326. return SCRIPT_CMD_SUCCESS;
  4327. if( !st->mes_active ) {
  4328. TBL_NPC* nd = map_id2nd(st->oid);
  4329. st->state = END; // Keep backwards compatibility.
  4330. ShowWarning("Incorrect use of 'close' command! (source:%s / path:%s)\n",nd?nd->name:"Unknown",nd?nd->path:"Unknown");
  4331. } else {
  4332. st->state = CLOSE;
  4333. st->mes_active = 0;
  4334. }
  4335. clif_scriptclose(sd, st->oid);
  4336. return SCRIPT_CMD_SUCCESS;
  4337. }
  4338. /// Displays the button 'close' on the npc dialog.
  4339. /// The dialog is closed and the script continues when the button is pressed.
  4340. ///
  4341. /// close2;
  4342. BUILDIN_FUNC(close2)
  4343. {
  4344. TBL_PC* sd;
  4345. sd = script_rid2sd(st);
  4346. if( sd == NULL )
  4347. return SCRIPT_CMD_SUCCESS;
  4348. st->state = STOP;
  4349. if( st->mes_active )
  4350. st->mes_active = 0;
  4351. clif_scriptclose(sd, st->oid);
  4352. return SCRIPT_CMD_SUCCESS;
  4353. }
  4354. /// Counts the number of valid and total number of options in 'str'
  4355. /// If max_count > 0 the counting stops when that valid option is reached
  4356. /// total is incremented for each option (NULL is supported)
  4357. static int menu_countoptions(const char* str, int max_count, int* total)
  4358. {
  4359. int count = 0;
  4360. int bogus_total;
  4361. if( total == NULL )
  4362. total = &bogus_total;
  4363. ++(*total);
  4364. // initial empty options
  4365. while( *str == ':' )
  4366. {
  4367. ++str;
  4368. ++(*total);
  4369. }
  4370. // count menu options
  4371. while( *str != '\0' )
  4372. {
  4373. ++count;
  4374. --max_count;
  4375. if( max_count == 0 )
  4376. break;
  4377. while( *str != ':' && *str != '\0' )
  4378. ++str;
  4379. while( *str == ':' )
  4380. {
  4381. ++str;
  4382. ++(*total);
  4383. }
  4384. }
  4385. return count;
  4386. }
  4387. /// Displays a menu with options and goes to the target label.
  4388. /// The script is stopped if cancel is pressed.
  4389. /// Options with no text are not displayed in the client.
  4390. ///
  4391. /// Options can be grouped together, separated by the character ':' in the text:
  4392. /// ex: menu "A:B:C",L_target;
  4393. /// All these options go to the specified target label.
  4394. ///
  4395. /// The index of the selected option is put in the variable @menu.
  4396. /// Indexes start with 1 and are consistent with grouped and empty options.
  4397. /// ex: menu "A::B",-,"",L_Impossible,"C",-;
  4398. /// // displays "A", "B" and "C", corresponding to indexes 1, 3 and 5
  4399. ///
  4400. /// NOTE: the client closes the npc dialog when cancel is pressed
  4401. ///
  4402. /// menu "<option_text>",<target_label>{,"<option_text>",<target_label>,...};
  4403. BUILDIN_FUNC(menu)
  4404. {
  4405. int i;
  4406. const char* text;
  4407. TBL_PC* sd;
  4408. sd = script_rid2sd(st);
  4409. if( sd == NULL )
  4410. return SCRIPT_CMD_SUCCESS;
  4411. #ifdef SECURE_NPCTIMEOUT
  4412. sd->npc_idle_type = NPCT_MENU;
  4413. #endif
  4414. // TODO detect multiple scripts waiting for input at the same time, and what to do when that happens
  4415. if( sd->state.menu_or_input == 0 )
  4416. {
  4417. struct StringBuf buf;
  4418. if( script_lastdata(st) % 2 == 0 )
  4419. {// argument count is not even (1st argument is at index 2)
  4420. ShowError("buildin_menu: Illegal number of arguments (%d).\n", (script_lastdata(st) - 1));
  4421. st->state = END;
  4422. return SCRIPT_CMD_FAILURE;
  4423. }
  4424. StringBuf_Init(&buf);
  4425. sd->npc_menu = 0;
  4426. for( i = 2; i < script_lastdata(st); i += 2 )
  4427. {
  4428. struct script_data* data;
  4429. // menu options
  4430. text = script_getstr(st, i);
  4431. // target label
  4432. data = script_getdata(st, i+1);
  4433. if( !data_islabel(data) )
  4434. {// not a label
  4435. StringBuf_Destroy(&buf);
  4436. ShowError("buildin_menu: Argument #%d (from 1) is not a label or label not found.\n", i);
  4437. script_reportdata(data);
  4438. st->state = END;
  4439. return SCRIPT_CMD_FAILURE;
  4440. }
  4441. // append option(s)
  4442. if( text[0] == '\0' )
  4443. continue;// empty string, ignore
  4444. if( sd->npc_menu > 0 )
  4445. StringBuf_AppendStr(&buf, ":");
  4446. StringBuf_AppendStr(&buf, text);
  4447. sd->npc_menu += menu_countoptions(text, 0, NULL);
  4448. }
  4449. st->state = RERUNLINE;
  4450. sd->state.menu_or_input = 1;
  4451. /**
  4452. * menus beyond this length crash the client (see bugreport:6402)
  4453. **/
  4454. if( StringBuf_Length(&buf) >= 2047 ) {
  4455. struct npc_data * nd = map_id2nd(st->oid);
  4456. char* menu;
  4457. CREATE(menu, char, 2048);
  4458. safestrncpy(menu, StringBuf_Value(&buf), 2047);
  4459. ShowWarning("buildin_menu: NPC Menu too long! (source:%s / length:%d)\n",nd?nd->name:"Unknown",StringBuf_Length(&buf));
  4460. clif_scriptmenu(sd, st->oid, menu);
  4461. aFree(menu);
  4462. } else
  4463. clif_scriptmenu(sd, st->oid, StringBuf_Value(&buf));
  4464. StringBuf_Destroy(&buf);
  4465. if( sd->npc_menu >= 0xff )
  4466. {// 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
  4467. ShowWarning("buildin_menu: Too many options specified (current=%d, max=254).\n", sd->npc_menu);
  4468. script_reportsrc(st);
  4469. }
  4470. }
  4471. else if( sd->npc_menu == 0xff )
  4472. {// Cancel was pressed
  4473. sd->state.menu_or_input = 0;
  4474. st->state = END;
  4475. }
  4476. else
  4477. {// goto target label
  4478. int menu = 0;
  4479. sd->state.menu_or_input = 0;
  4480. if( sd->npc_menu <= 0 )
  4481. {
  4482. ShowDebug("buildin_menu: Unexpected selection (%d)\n", sd->npc_menu);
  4483. st->state = END;
  4484. return SCRIPT_CMD_FAILURE;
  4485. }
  4486. // get target label
  4487. for( i = 2; i < script_lastdata(st); i += 2 )
  4488. {
  4489. text = script_getstr(st, i);
  4490. sd->npc_menu -= menu_countoptions(text, sd->npc_menu, &menu);
  4491. if( sd->npc_menu <= 0 )
  4492. break;// entry found
  4493. }
  4494. if( sd->npc_menu > 0 )
  4495. {// Invalid selection
  4496. ShowDebug("buildin_menu: Selection is out of range (%d pairs are missing?) - please report this\n", sd->npc_menu);
  4497. st->state = END;
  4498. return SCRIPT_CMD_FAILURE;
  4499. }
  4500. if( !data_islabel(script_getdata(st, i + 1)) )
  4501. {// TODO remove this temporary crash-prevention code (fallback for multiple scripts requesting user input)
  4502. ShowError("buildin_menu: Unexpected data in label argument\n");
  4503. script_reportdata(script_getdata(st, i + 1));
  4504. st->state = END;
  4505. return SCRIPT_CMD_FAILURE;
  4506. }
  4507. pc_setreg(sd, add_str("@menu"), menu);
  4508. st->pos = script_getnum(st, i + 1);
  4509. st->state = GOTO;
  4510. }
  4511. return SCRIPT_CMD_SUCCESS;
  4512. }
  4513. /// Displays a menu with options and returns the selected option.
  4514. /// Behaves like 'menu' without the target labels.
  4515. ///
  4516. /// select(<option_text>{,<option_text>,...}) -> <selected_option>
  4517. ///
  4518. /// @see menu
  4519. BUILDIN_FUNC(select)
  4520. {
  4521. int i;
  4522. const char* text;
  4523. TBL_PC* sd;
  4524. sd = script_rid2sd(st);
  4525. if( sd == NULL )
  4526. return SCRIPT_CMD_SUCCESS;
  4527. #ifdef SECURE_NPCTIMEOUT
  4528. sd->npc_idle_type = NPCT_MENU;
  4529. #endif
  4530. if( sd->state.menu_or_input == 0 ) {
  4531. struct StringBuf buf;
  4532. StringBuf_Init(&buf);
  4533. sd->npc_menu = 0;
  4534. for( i = 2; i <= script_lastdata(st); ++i ) {
  4535. text = script_getstr(st, i);
  4536. if( sd->npc_menu > 0 )
  4537. StringBuf_AppendStr(&buf, ":");
  4538. StringBuf_AppendStr(&buf, text);
  4539. sd->npc_menu += menu_countoptions(text, 0, NULL);
  4540. }
  4541. st->state = RERUNLINE;
  4542. sd->state.menu_or_input = 1;
  4543. /**
  4544. * menus beyond this length crash the client (see bugreport:6402)
  4545. **/
  4546. if( StringBuf_Length(&buf) >= 2047 ) {
  4547. struct npc_data * nd = map_id2nd(st->oid);
  4548. char* menu;
  4549. CREATE(menu, char, 2048);
  4550. safestrncpy(menu, StringBuf_Value(&buf), 2047);
  4551. ShowWarning("buildin_select: NPC Menu too long! (source:%s / length:%d)\n",nd?nd->name:"Unknown",StringBuf_Length(&buf));
  4552. clif_scriptmenu(sd, st->oid, menu);
  4553. aFree(menu);
  4554. } else
  4555. clif_scriptmenu(sd, st->oid, StringBuf_Value(&buf));
  4556. StringBuf_Destroy(&buf);
  4557. if( sd->npc_menu >= 0xff ) {
  4558. ShowWarning("buildin_select: Too many options specified (current=%d, max=254).\n", sd->npc_menu);
  4559. script_reportsrc(st);
  4560. }
  4561. } else if( sd->npc_menu == 0xff ) {// Cancel was pressed
  4562. sd->state.menu_or_input = 0;
  4563. st->state = END;
  4564. } else {// return selected option
  4565. int menu = 0;
  4566. sd->state.menu_or_input = 0;
  4567. for( i = 2; i <= script_lastdata(st); ++i ) {
  4568. text = script_getstr(st, i);
  4569. sd->npc_menu -= menu_countoptions(text, sd->npc_menu, &menu);
  4570. if( sd->npc_menu <= 0 )
  4571. break;// entry found
  4572. }
  4573. pc_setreg(sd, add_str("@menu"), menu);
  4574. script_pushint(st, menu);
  4575. st->state = RUN;
  4576. }
  4577. return SCRIPT_CMD_SUCCESS;
  4578. }
  4579. /// Displays a menu with options and returns the selected option.
  4580. /// Behaves like 'menu' without the target labels, except when cancel is
  4581. /// pressed.
  4582. /// When cancel is pressed, the script continues and 255 is returned.
  4583. ///
  4584. /// prompt(<option_text>{,<option_text>,...}) -> <selected_option>
  4585. ///
  4586. /// @see menu
  4587. BUILDIN_FUNC(prompt)
  4588. {
  4589. int i;
  4590. const char *text;
  4591. TBL_PC* sd;
  4592. sd = script_rid2sd(st);
  4593. if( sd == NULL )
  4594. return SCRIPT_CMD_SUCCESS;
  4595. #ifdef SECURE_NPCTIMEOUT
  4596. sd->npc_idle_type = NPCT_MENU;
  4597. #endif
  4598. if( sd->state.menu_or_input == 0 )
  4599. {
  4600. struct StringBuf buf;
  4601. StringBuf_Init(&buf);
  4602. sd->npc_menu = 0;
  4603. for( i = 2; i <= script_lastdata(st); ++i )
  4604. {
  4605. text = script_getstr(st, i);
  4606. if( sd->npc_menu > 0 )
  4607. StringBuf_AppendStr(&buf, ":");
  4608. StringBuf_AppendStr(&buf, text);
  4609. sd->npc_menu += menu_countoptions(text, 0, NULL);
  4610. }
  4611. st->state = RERUNLINE;
  4612. sd->state.menu_or_input = 1;
  4613. /**
  4614. * menus beyond this length crash the client (see bugreport:6402)
  4615. **/
  4616. if( StringBuf_Length(&buf) >= 2047 ) {
  4617. struct npc_data * nd = map_id2nd(st->oid);
  4618. char* menu;
  4619. CREATE(menu, char, 2048);
  4620. safestrncpy(menu, StringBuf_Value(&buf), 2047);
  4621. ShowWarning("buildin_prompt: NPC Menu too long! (source:%s / length:%d)\n",nd?nd->name:"Unknown",StringBuf_Length(&buf));
  4622. clif_scriptmenu(sd, st->oid, menu);
  4623. aFree(menu);
  4624. } else
  4625. clif_scriptmenu(sd, st->oid, StringBuf_Value(&buf));
  4626. StringBuf_Destroy(&buf);
  4627. if( sd->npc_menu >= 0xff )
  4628. {
  4629. ShowWarning("buildin_prompt: Too many options specified (current=%d, max=254).\n", sd->npc_menu);
  4630. script_reportsrc(st);
  4631. }
  4632. }
  4633. else if( sd->npc_menu == 0xff )
  4634. {// Cancel was pressed
  4635. sd->state.menu_or_input = 0;
  4636. pc_setreg(sd, add_str("@menu"), 0xff);
  4637. script_pushint(st, 0xff);
  4638. st->state = RUN;
  4639. }
  4640. else
  4641. {// return selected option
  4642. int menu = 0;
  4643. sd->state.menu_or_input = 0;
  4644. for( i = 2; i <= script_lastdata(st); ++i )
  4645. {
  4646. text = script_getstr(st, i);
  4647. sd->npc_menu -= menu_countoptions(text, sd->npc_menu, &menu);
  4648. if( sd->npc_menu <= 0 )
  4649. break;// entry found
  4650. }
  4651. pc_setreg(sd, add_str("@menu"), menu);
  4652. script_pushint(st, menu);
  4653. st->state = RUN;
  4654. }
  4655. return SCRIPT_CMD_SUCCESS;
  4656. }
  4657. /////////////////////////////////////////////////////////////////////
  4658. // ...
  4659. //
  4660. /// Jumps to the target script label.
  4661. ///
  4662. /// goto <label>;
  4663. BUILDIN_FUNC(goto)
  4664. {
  4665. if( !data_islabel(script_getdata(st,2)) )
  4666. {
  4667. ShowError("buildin_goto: Not a label\n");
  4668. script_reportdata(script_getdata(st,2));
  4669. st->state = END;
  4670. return SCRIPT_CMD_FAILURE;
  4671. }
  4672. st->pos = script_getnum(st,2);
  4673. st->state = GOTO;
  4674. return SCRIPT_CMD_SUCCESS;
  4675. }
  4676. /*==========================================
  4677. * user-defined function call
  4678. *------------------------------------------*/
  4679. BUILDIN_FUNC(callfunc)
  4680. {
  4681. int i, j;
  4682. struct script_retinfo* ri;
  4683. struct script_code* scr;
  4684. const char* str = script_getstr(st,2);
  4685. struct reg_db *ref = NULL;
  4686. scr = (struct script_code*)strdb_get(userfunc_db, str);
  4687. if(!scr) {
  4688. ShowError("buildin_callfunc: Function not found! [%s]\n", str);
  4689. st->state = END;
  4690. return SCRIPT_CMD_FAILURE;
  4691. }
  4692. ref = (struct reg_db *)aCalloc(sizeof(struct reg_db), 2);
  4693. ref[0].vars = st->stack->scope.vars;
  4694. if (!st->stack->scope.arrays)
  4695. st->stack->scope.arrays = idb_alloc(DB_OPT_BASE); // TODO: Can this happen? when?
  4696. ref[0].arrays = st->stack->scope.arrays;
  4697. ref[1].vars = st->script->local.vars;
  4698. if (!st->script->local.arrays)
  4699. st->script->local.arrays = idb_alloc(DB_OPT_BASE); // TODO: Can this happen? when?
  4700. ref[1].arrays = st->script->local.arrays;
  4701. for(i = st->start+3, j = 0; i < st->end; i++, j++) {
  4702. struct script_data* data = push_copy(st->stack,i);
  4703. if (data_isreference(data) && !data->ref) {
  4704. const char* name = reference_getname(data);
  4705. if (name[0] == '.')
  4706. data->ref = (name[1] == '@' ? &ref[0] : &ref[1]);
  4707. }
  4708. }
  4709. CREATE(ri, struct script_retinfo, 1);
  4710. ri->script = st->script; // script code
  4711. ri->scope.vars = st->stack->scope.vars; // scope variables
  4712. ri->scope.arrays = st->stack->scope.arrays; // scope arrays
  4713. ri->pos = st->pos; // script location
  4714. ri->nargs = j; // argument count
  4715. ri->defsp = st->stack->defsp; // default stack pointer
  4716. push_retinfo(st->stack, ri, ref);
  4717. st->pos = 0;
  4718. st->script = scr;
  4719. st->stack->defsp = st->stack->sp;
  4720. st->state = GOTO;
  4721. st->stack->scope.vars = i64db_alloc(DB_OPT_RELEASE_DATA);
  4722. st->stack->scope.arrays = idb_alloc(DB_OPT_BASE);
  4723. if (!st->script->local.vars)
  4724. st->script->local.vars = i64db_alloc(DB_OPT_RELEASE_DATA);
  4725. return SCRIPT_CMD_SUCCESS;
  4726. }
  4727. /*==========================================
  4728. * subroutine call
  4729. *------------------------------------------*/
  4730. BUILDIN_FUNC(callsub)
  4731. {
  4732. int i,j;
  4733. struct script_retinfo* ri;
  4734. int pos = script_getnum(st,2);
  4735. struct reg_db *ref = NULL;
  4736. if( !data_islabel(script_getdata(st,2)) && !data_isfunclabel(script_getdata(st,2)) ) {
  4737. ShowError("buildin_callsub: Argument is not a label\n");
  4738. script_reportdata(script_getdata(st,2));
  4739. st->state = END;
  4740. return SCRIPT_CMD_FAILURE;
  4741. }
  4742. ref = (struct reg_db *)aCalloc(sizeof(struct reg_db), 1);
  4743. ref[0].vars = st->stack->scope.vars;
  4744. if (!st->stack->scope.arrays)
  4745. st->stack->scope.arrays = idb_alloc(DB_OPT_BASE); // TODO: Can this happen? when?
  4746. ref[0].arrays = st->stack->scope.arrays;
  4747. for(i = st->start+3, j = 0; i < st->end; i++, j++) {
  4748. struct script_data* data = push_copy(st->stack,i);
  4749. if (data_isreference(data) && !data->ref) {
  4750. const char* name = reference_getname(data);
  4751. if (name[0] == '.' && name[1] == '@')
  4752. data->ref = &ref[0];
  4753. }
  4754. }
  4755. CREATE(ri, struct script_retinfo, 1);
  4756. ri->script = st->script; // script code
  4757. ri->scope.vars = st->stack->scope.vars; // scope variables
  4758. ri->scope.arrays = st->stack->scope.arrays; // scope arrays
  4759. ri->pos = st->pos; // script location
  4760. ri->nargs = j; // argument count
  4761. ri->defsp = st->stack->defsp; // default stack pointer
  4762. push_retinfo(st->stack, ri, ref);
  4763. st->pos = pos;
  4764. st->stack->defsp = st->stack->sp;
  4765. st->state = GOTO;
  4766. st->stack->scope.vars = i64db_alloc(DB_OPT_RELEASE_DATA);
  4767. st->stack->scope.arrays = idb_alloc(DB_OPT_BASE);
  4768. return SCRIPT_CMD_SUCCESS;
  4769. }
  4770. /// Retrieves an argument provided to callfunc/callsub.
  4771. /// If the argument doesn't exist
  4772. ///
  4773. /// getarg(<index>{,<default_value>}) -> <value>
  4774. BUILDIN_FUNC(getarg)
  4775. {
  4776. struct script_retinfo* ri;
  4777. int idx;
  4778. if( st->stack->defsp < 1 || st->stack->stack_data[st->stack->defsp - 1].type != C_RETINFO )
  4779. {
  4780. ShowError("buildin_getarg: No callfunc or callsub!\n");
  4781. st->state = END;
  4782. return SCRIPT_CMD_FAILURE;
  4783. }
  4784. ri = st->stack->stack_data[st->stack->defsp - 1].u.ri;
  4785. idx = script_getnum(st,2);
  4786. if( idx >= 0 && idx < ri->nargs )
  4787. push_copy(st->stack, st->stack->defsp - 1 - ri->nargs + idx);
  4788. else if( script_hasdata(st,3) )
  4789. script_pushcopy(st, 3);
  4790. else
  4791. {
  4792. ShowError("buildin_getarg: Index (idx=%d) out of range (nargs=%d) and no default value found\n", idx, ri->nargs);
  4793. st->state = END;
  4794. return SCRIPT_CMD_FAILURE;
  4795. }
  4796. return SCRIPT_CMD_SUCCESS;
  4797. }
  4798. /// Returns from the current function, optionaly returning a value from the functions.
  4799. /// Don't use outside script functions.
  4800. ///
  4801. /// return;
  4802. /// return <value>;
  4803. BUILDIN_FUNC(return)
  4804. {
  4805. if( script_hasdata(st,2) )
  4806. {// return value
  4807. struct script_data* data;
  4808. script_pushcopy(st, 2);
  4809. data = script_getdatatop(st, -1);
  4810. if( data_isreference(data) ) {
  4811. const char* name = reference_getname(data);
  4812. if( name[0] == '.' && name[1] == '@' ) { // scope variable
  4813. if( !data->ref || data->ref->vars == st->stack->scope.vars )
  4814. get_val(st, data); // current scope, convert to value
  4815. if( data->ref && data->ref->vars == st->stack->stack_data[st->stack->defsp-1].u.ri->scope.vars )
  4816. data->ref = NULL; // Reference to the parent scope, remove reference pointer
  4817. } else if( name[0] == '.' && !data->ref ) { // script variable, link to current script
  4818. data->ref = (struct reg_db *)aCalloc(sizeof(struct reg_db), 1);
  4819. data->ref->vars = st->script->local.vars;
  4820. if (!st->script->local.arrays)
  4821. st->script->local.arrays = idb_alloc(DB_OPT_BASE);
  4822. data->ref->arrays = st->script->local.arrays;
  4823. }
  4824. }
  4825. }
  4826. else
  4827. {// no return value
  4828. script_pushnil(st);
  4829. }
  4830. st->state = RETFUNC;
  4831. return SCRIPT_CMD_SUCCESS;
  4832. }
  4833. /// Returns a random number from 0 to <range>-1.
  4834. /// Or returns a random number from <min> to <max>.
  4835. /// If <min> is greater than <max>, their numbers are switched.
  4836. /// rand(<range>) -> <int>
  4837. /// rand(<min>,<max>) -> <int>
  4838. BUILDIN_FUNC(rand)
  4839. {
  4840. int range;
  4841. int min;
  4842. if( script_hasdata(st,3) )
  4843. {// min,max
  4844. int max = script_getnum(st,3);
  4845. min = script_getnum(st,2);
  4846. if( max < min )
  4847. swap(min, max);
  4848. range = max - min + 1;
  4849. }
  4850. else
  4851. {// range
  4852. min = 0;
  4853. range = script_getnum(st,2);
  4854. }
  4855. if( range <= 1 )
  4856. script_pushint(st, min);
  4857. else
  4858. script_pushint(st, rnd()%range + min);
  4859. return SCRIPT_CMD_SUCCESS;
  4860. }
  4861. /*==========================================
  4862. * Warp sd to str,x,y or Random or SavePoint/Save
  4863. *------------------------------------------*/
  4864. BUILDIN_FUNC(warp)
  4865. {
  4866. int ret;
  4867. int x,y;
  4868. const char* str;
  4869. struct map_session_data* sd;
  4870. if(!script_charid2sd(5, sd))
  4871. return SCRIPT_CMD_SUCCESS;
  4872. str = script_getstr(st,2);
  4873. x = script_getnum(st,3);
  4874. y = script_getnum(st,4);
  4875. if(strcmp(str,"Random")==0)
  4876. ret = pc_randomwarp(sd,CLR_TELEPORT);
  4877. else if(strcmp(str,"SavePoint")==0 || strcmp(str,"Save")==0)
  4878. ret = pc_setpos(sd,sd->status.save_point.map,sd->status.save_point.x,sd->status.save_point.y,CLR_TELEPORT);
  4879. else
  4880. ret = pc_setpos(sd,mapindex_name2id(str),x,y,CLR_OUTSIGHT);
  4881. if( ret ) {
  4882. ShowError("buildin_warp: moving player '%s' to \"%s\",%d,%d failed.\n", sd->status.name, str, x, y);
  4883. return SCRIPT_CMD_FAILURE;
  4884. }
  4885. return SCRIPT_CMD_SUCCESS;
  4886. }
  4887. /*==========================================
  4888. * Warp a specified area
  4889. *------------------------------------------*/
  4890. static int buildin_areawarp_sub(struct block_list *bl,va_list ap)
  4891. {
  4892. int x2,y2,x3,y3;
  4893. unsigned int index;
  4894. index = va_arg(ap,unsigned int);
  4895. x2 = va_arg(ap,int);
  4896. y2 = va_arg(ap,int);
  4897. x3 = va_arg(ap,int);
  4898. y3 = va_arg(ap,int);
  4899. if(index == 0)
  4900. pc_randomwarp((TBL_PC *)bl,CLR_TELEPORT);
  4901. else if(x3 && y3) {
  4902. int max, tx, ty, j = 0;
  4903. int16 m;
  4904. m = map_mapindex2mapid(index);
  4905. // choose a suitable max number of attempts
  4906. if( (max = (y3-y2+1)*(x3-x2+1)*3) > 1000 )
  4907. max = 1000;
  4908. // find a suitable map cell
  4909. do {
  4910. tx = rnd()%(x3-x2+1)+x2;
  4911. ty = rnd()%(y3-y2+1)+y2;
  4912. j++;
  4913. } while( map_getcell(m,tx,ty,CELL_CHKNOPASS) && j < max );
  4914. pc_setpos((TBL_PC *)bl,index,tx,ty,CLR_OUTSIGHT);
  4915. }
  4916. else
  4917. pc_setpos((TBL_PC *)bl,index,x2,y2,CLR_OUTSIGHT);
  4918. return 0;
  4919. }
  4920. BUILDIN_FUNC(areawarp)
  4921. {
  4922. int16 m, x0,y0,x1,y1, x2,y2,x3=0,y3=0;
  4923. unsigned int index;
  4924. const char *str;
  4925. const char *mapname;
  4926. mapname = script_getstr(st,2);
  4927. x0 = script_getnum(st,3);
  4928. y0 = script_getnum(st,4);
  4929. x1 = script_getnum(st,5);
  4930. y1 = script_getnum(st,6);
  4931. str = script_getstr(st,7);
  4932. x2 = script_getnum(st,8);
  4933. y2 = script_getnum(st,9);
  4934. if( script_hasdata(st,10) && script_hasdata(st,11) ) { // Warp area to area
  4935. if( (x3 = script_getnum(st,10)) < 0 || (y3 = script_getnum(st,11)) < 0 ){
  4936. x3 = 0;
  4937. y3 = 0;
  4938. } else if( x3 && y3 ) {
  4939. // normalize x3/y3 coordinates
  4940. if( x3 < x2 ) swap(x3,x2);
  4941. if( y3 < y2 ) swap(y3,y2);
  4942. }
  4943. }
  4944. if( (m = map_mapname2mapid(mapname)) < 0 )
  4945. return SCRIPT_CMD_FAILURE;
  4946. if( strcmp(str,"Random") == 0 )
  4947. index = 0;
  4948. else if( !(index=mapindex_name2id(str)) )
  4949. return SCRIPT_CMD_FAILURE;
  4950. map_foreachinarea(buildin_areawarp_sub, m,x0,y0,x1,y1, BL_PC, index,x2,y2,x3,y3);
  4951. return SCRIPT_CMD_SUCCESS;
  4952. }
  4953. /*==========================================
  4954. * areapercentheal <map>,<x1>,<y1>,<x2>,<y2>,<hp>,<sp>
  4955. *------------------------------------------*/
  4956. static int buildin_areapercentheal_sub(struct block_list *bl,va_list ap)
  4957. {
  4958. int hp, sp;
  4959. hp = va_arg(ap, int);
  4960. sp = va_arg(ap, int);
  4961. pc_percentheal((TBL_PC *)bl,hp,sp);
  4962. return 0;
  4963. }
  4964. BUILDIN_FUNC(areapercentheal)
  4965. {
  4966. int hp,sp,m;
  4967. const char *mapname;
  4968. int x0,y0,x1,y1;
  4969. mapname=script_getstr(st,2);
  4970. x0=script_getnum(st,3);
  4971. y0=script_getnum(st,4);
  4972. x1=script_getnum(st,5);
  4973. y1=script_getnum(st,6);
  4974. hp=script_getnum(st,7);
  4975. sp=script_getnum(st,8);
  4976. if( (m=map_mapname2mapid(mapname))< 0)
  4977. return SCRIPT_CMD_FAILURE;
  4978. map_foreachinarea(buildin_areapercentheal_sub,m,x0,y0,x1,y1,BL_PC,hp,sp);
  4979. return SCRIPT_CMD_SUCCESS;
  4980. }
  4981. /*==========================================
  4982. * Warpparty - [Fredzilla] [Paradox924X]
  4983. * Syntax: warpparty "to_mapname",x,y,Party_ID,{"from_mapname"};
  4984. * If 'from_mapname' is specified, only the party members on that map will be warped
  4985. *------------------------------------------*/
  4986. BUILDIN_FUNC(warpparty)
  4987. {
  4988. TBL_PC *sd = NULL;
  4989. TBL_PC *pl_sd;
  4990. struct party_data* p;
  4991. int type;
  4992. int mapindex = 0, m = -1;
  4993. int i;
  4994. const char* str = script_getstr(st,2);
  4995. int x = script_getnum(st,3);
  4996. int y = script_getnum(st,4);
  4997. int p_id = script_getnum(st,5);
  4998. const char* str2 = NULL;
  4999. if ( script_hasdata(st,6) )
  5000. str2 = script_getstr(st,6);
  5001. p = party_search(p_id);
  5002. if(!p)
  5003. return SCRIPT_CMD_SUCCESS;
  5004. type = ( strcmp(str,"Random")==0 ) ? 0
  5005. : ( strcmp(str,"SavePointAll")==0 ) ? 1
  5006. : ( strcmp(str,"SavePoint")==0 ) ? 2
  5007. : ( strcmp(str,"Leader")==0 ) ? 3
  5008. : 4;
  5009. switch (type)
  5010. {
  5011. case 3:
  5012. for(i = 0; i < MAX_PARTY && !p->party.member[i].leader; i++);
  5013. if (i == MAX_PARTY || !p->data[i].sd) //Leader not found / not online
  5014. return SCRIPT_CMD_FAILURE;
  5015. pl_sd = p->data[i].sd;
  5016. mapindex = pl_sd->mapindex;
  5017. m = map_mapindex2mapid(mapindex);
  5018. x = pl_sd->bl.x;
  5019. y = pl_sd->bl.y;
  5020. break;
  5021. case 4:
  5022. mapindex = mapindex_name2id(str);
  5023. if (!mapindex) {// Invalid map
  5024. return SCRIPT_CMD_FAILURE;
  5025. }
  5026. m = map_mapindex2mapid(mapindex);
  5027. break;
  5028. case 2:
  5029. //"SavePoint" uses save point of the currently attached player
  5030. if (( sd = script_rid2sd(st) ) == NULL )
  5031. return SCRIPT_CMD_SUCCESS;
  5032. break;
  5033. }
  5034. for (i = 0; i < MAX_PARTY; i++)
  5035. {
  5036. if( !(pl_sd = p->data[i].sd) || pl_sd->status.party_id != p_id )
  5037. continue;
  5038. if( str2 && strcmp(str2, map[pl_sd->bl.m].name) != 0 )
  5039. continue;
  5040. if( pc_isdead(pl_sd) )
  5041. continue;
  5042. switch( type )
  5043. {
  5044. case 0: // Random
  5045. if(!map[pl_sd->bl.m].flag.nowarp)
  5046. pc_randomwarp(pl_sd,CLR_TELEPORT);
  5047. break;
  5048. case 1: // SavePointAll
  5049. if(!map[pl_sd->bl.m].flag.noreturn)
  5050. pc_setpos(pl_sd,pl_sd->status.save_point.map,pl_sd->status.save_point.x,pl_sd->status.save_point.y,CLR_TELEPORT);
  5051. break;
  5052. case 2: // SavePoint
  5053. if(!map[pl_sd->bl.m].flag.noreturn)
  5054. pc_setpos(pl_sd,sd->status.save_point.map,sd->status.save_point.x,sd->status.save_point.y,CLR_TELEPORT);
  5055. break;
  5056. case 3: // Leader
  5057. case 4: // m,x,y
  5058. if(!map[pl_sd->bl.m].flag.noreturn && !map[pl_sd->bl.m].flag.nowarp && pc_job_can_entermap((enum e_job)pl_sd->status.class_, m, pl_sd->group_level))
  5059. pc_setpos(pl_sd,mapindex,x,y,CLR_TELEPORT);
  5060. break;
  5061. }
  5062. }
  5063. return SCRIPT_CMD_SUCCESS;
  5064. }
  5065. /*==========================================
  5066. * Warpguild - [Fredzilla]
  5067. * Syntax: warpguild "mapname",x,y,Guild_ID;
  5068. *------------------------------------------*/
  5069. BUILDIN_FUNC(warpguild)
  5070. {
  5071. TBL_PC *sd = NULL;
  5072. TBL_PC *pl_sd;
  5073. struct guild* g;
  5074. struct s_mapiterator* iter;
  5075. int type, mapindex = 0, m = -1;
  5076. const char* str = script_getstr(st,2);
  5077. int x = script_getnum(st,3);
  5078. int y = script_getnum(st,4);
  5079. int gid = script_getnum(st,5);
  5080. g = guild_search(gid);
  5081. if( g == NULL )
  5082. return SCRIPT_CMD_SUCCESS;
  5083. type = ( strcmp(str,"Random")==0 ) ? 0
  5084. : ( strcmp(str,"SavePointAll")==0 ) ? 1
  5085. : ( strcmp(str,"SavePoint")==0 ) ? 2
  5086. : 3;
  5087. if( type == 2 && ( sd = script_rid2sd(st) ) == NULL )
  5088. {// "SavePoint" uses save point of the currently attached player
  5089. return SCRIPT_CMD_SUCCESS;
  5090. }
  5091. switch (type) {
  5092. case 3:
  5093. mapindex = mapindex_name2id(str);
  5094. if (!mapindex)
  5095. return SCRIPT_CMD_FAILURE;
  5096. m = map_mapindex2mapid(mapindex);
  5097. break;
  5098. }
  5099. iter = mapit_getallusers();
  5100. for( pl_sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); pl_sd = (TBL_PC*)mapit_next(iter) )
  5101. {
  5102. if( pl_sd->status.guild_id != gid )
  5103. continue;
  5104. switch( type )
  5105. {
  5106. case 0: // Random
  5107. if(!map[pl_sd->bl.m].flag.nowarp)
  5108. pc_randomwarp(pl_sd,CLR_TELEPORT);
  5109. break;
  5110. case 1: // SavePointAll
  5111. if(!map[pl_sd->bl.m].flag.noreturn)
  5112. pc_setpos(pl_sd,pl_sd->status.save_point.map,pl_sd->status.save_point.x,pl_sd->status.save_point.y,CLR_TELEPORT);
  5113. break;
  5114. case 2: // SavePoint
  5115. if(!map[pl_sd->bl.m].flag.noreturn)
  5116. pc_setpos(pl_sd,sd->status.save_point.map,sd->status.save_point.x,sd->status.save_point.y,CLR_TELEPORT);
  5117. break;
  5118. case 3: // m,x,y
  5119. if(!map[pl_sd->bl.m].flag.noreturn && !map[pl_sd->bl.m].flag.nowarp && pc_job_can_entermap((enum e_job)pl_sd->status.class_, m, pl_sd->group_level))
  5120. pc_setpos(pl_sd,mapindex,x,y,CLR_TELEPORT);
  5121. break;
  5122. }
  5123. }
  5124. mapit_free(iter);
  5125. return SCRIPT_CMD_SUCCESS;
  5126. }
  5127. /*==========================================
  5128. * Force Heal a player (hp and sp)
  5129. *------------------------------------------*/
  5130. BUILDIN_FUNC(heal)
  5131. {
  5132. TBL_PC *sd;
  5133. int hp,sp;
  5134. if (!script_charid2sd(4,sd))
  5135. return SCRIPT_CMD_SUCCESS;
  5136. hp=script_getnum(st,2);
  5137. sp=script_getnum(st,3);
  5138. status_heal(&sd->bl, hp, sp, 1);
  5139. return SCRIPT_CMD_SUCCESS;
  5140. }
  5141. /*==========================================
  5142. * Heal a player by item (get vit bonus etc)
  5143. *------------------------------------------*/
  5144. BUILDIN_FUNC(itemheal)
  5145. {
  5146. TBL_PC *sd;
  5147. int hp,sp;
  5148. hp=script_getnum(st,2);
  5149. sp=script_getnum(st,3);
  5150. if(potion_flag==1) {
  5151. potion_hp = hp;
  5152. potion_sp = sp;
  5153. return SCRIPT_CMD_SUCCESS;
  5154. }
  5155. if (!script_charid2sd(4,sd))
  5156. return SCRIPT_CMD_SUCCESS;
  5157. pc_itemheal(sd,sd->itemid,hp,sp);
  5158. return SCRIPT_CMD_SUCCESS;
  5159. }
  5160. /*==========================================
  5161. *
  5162. *------------------------------------------*/
  5163. BUILDIN_FUNC(percentheal)
  5164. {
  5165. int hp,sp;
  5166. TBL_PC* sd;
  5167. hp=script_getnum(st,2);
  5168. sp=script_getnum(st,3);
  5169. if(potion_flag==1) {
  5170. potion_per_hp = hp;
  5171. potion_per_sp = sp;
  5172. return SCRIPT_CMD_SUCCESS;
  5173. }
  5174. if (!script_charid2sd(4,sd))
  5175. return SCRIPT_CMD_SUCCESS;
  5176. #ifdef RENEWAL
  5177. if( sd->sc.data[SC_EXTREMITYFIST2] )
  5178. sp = 0;
  5179. #endif
  5180. if (sd->sc.data[SC_NORECOVER_STATE]) {
  5181. hp = 0;
  5182. sp = 0;
  5183. }
  5184. if (sd->sc.data[SC_BITESCAR])
  5185. hp = 0;
  5186. pc_percentheal(sd,hp,sp);
  5187. return SCRIPT_CMD_SUCCESS;
  5188. }
  5189. /*==========================================
  5190. *
  5191. *------------------------------------------*/
  5192. BUILDIN_FUNC(jobchange)
  5193. {
  5194. int job, upper=-1;
  5195. job=script_getnum(st,2);
  5196. if( script_hasdata(st,3) )
  5197. upper=script_getnum(st,3);
  5198. if (pcdb_checkid(job))
  5199. {
  5200. TBL_PC* sd;
  5201. if (!script_charid2sd(4,sd))
  5202. return SCRIPT_CMD_SUCCESS;
  5203. pc_jobchange(sd, job, upper);
  5204. }
  5205. return SCRIPT_CMD_SUCCESS;
  5206. }
  5207. /*==========================================
  5208. *
  5209. *------------------------------------------*/
  5210. BUILDIN_FUNC(jobname)
  5211. {
  5212. int class_=script_getnum(st,2);
  5213. script_pushconststr(st, (char*)job_name(class_));
  5214. return SCRIPT_CMD_SUCCESS;
  5215. }
  5216. /// Get input from the player.
  5217. /// For numeric inputs the value is capped to the range [min,max]. Returns 1 if
  5218. /// the value was higher than 'max', -1 if lower than 'min' and 0 otherwise.
  5219. /// For string inputs it returns 1 if the string was longer than 'max', -1 is
  5220. /// shorter than 'min' and 0 otherwise.
  5221. ///
  5222. /// input(<var>{,<min>{,<max>}}) -> <int>
  5223. BUILDIN_FUNC(input)
  5224. {
  5225. TBL_PC* sd;
  5226. struct script_data* data;
  5227. int64 uid;
  5228. const char* name;
  5229. int min;
  5230. int max;
  5231. sd = script_rid2sd(st);
  5232. if( sd == NULL )
  5233. return SCRIPT_CMD_SUCCESS;
  5234. data = script_getdata(st,2);
  5235. if( !data_isreference(data) ){
  5236. ShowError("script:input: not a variable\n");
  5237. script_reportdata(data);
  5238. st->state = END;
  5239. return SCRIPT_CMD_FAILURE;
  5240. }
  5241. uid = reference_getuid(data);
  5242. name = reference_getname(data);
  5243. min = (script_hasdata(st,3) ? script_getnum(st,3) : script_config.input_min_value);
  5244. max = (script_hasdata(st,4) ? script_getnum(st,4) : script_config.input_max_value);
  5245. #ifdef SECURE_NPCTIMEOUT
  5246. sd->npc_idle_type = NPCT_WAIT;
  5247. #endif
  5248. if( !sd->state.menu_or_input )
  5249. { // first invocation, display npc input box
  5250. sd->state.menu_or_input = 1;
  5251. st->state = RERUNLINE;
  5252. if( is_string_variable(name) )
  5253. clif_scriptinputstr(sd,st->oid);
  5254. else
  5255. clif_scriptinput(sd,st->oid);
  5256. }
  5257. else
  5258. { // take received text/value and store it in the designated variable
  5259. sd->state.menu_or_input = 0;
  5260. if( is_string_variable(name) )
  5261. {
  5262. int len = (int)strlen(sd->npc_str);
  5263. set_reg(st, sd, uid, name, (void*)sd->npc_str, script_getref(st,2));
  5264. script_pushint(st, (len > max ? 1 : len < min ? -1 : 0));
  5265. }
  5266. else
  5267. {
  5268. int amount = sd->npc_amount;
  5269. set_reg(st, sd, uid, name, (void*)__64BPRTSIZE(cap_value(amount,min,max)), script_getref(st,2));
  5270. script_pushint(st, (amount > max ? 1 : amount < min ? -1 : 0));
  5271. }
  5272. st->state = RUN;
  5273. }
  5274. return SCRIPT_CMD_SUCCESS;
  5275. }
  5276. // declare the copyarray method here for future reference
  5277. BUILDIN_FUNC(copyarray);
  5278. /// Sets the value of a variable.
  5279. /// The value is converted to the type of the variable.
  5280. ///
  5281. /// set(<variable>,<value>{,<charid>})
  5282. BUILDIN_FUNC(setr)
  5283. {
  5284. TBL_PC* sd = NULL;
  5285. struct script_data* data;
  5286. //struct script_data* datavalue;
  5287. int64 num;
  5288. uint8 pos = 4;
  5289. const char* name, *command = script_getfuncname(st);
  5290. char prefix;
  5291. data = script_getdata(st,2);
  5292. //datavalue = script_getdata(st,3);
  5293. if( !data_isreference(data) )
  5294. {
  5295. ShowError("script:set: not a variable\n");
  5296. script_reportdata(script_getdata(st,2));
  5297. st->state = END;
  5298. return SCRIPT_CMD_FAILURE;
  5299. }
  5300. num = reference_getuid(data);
  5301. name = reference_getname(data);
  5302. prefix = *name;
  5303. if (!strcmp(command, "setr"))
  5304. pos = 5;
  5305. if (not_server_variable(prefix) && !script_charid2sd(pos,sd)) {
  5306. ShowError("buildin_set: No player attached for player variable '%s'\n", name);
  5307. return SCRIPT_CMD_FAILURE;
  5308. }
  5309. #if 0
  5310. if( data_isreference(datavalue) )
  5311. {// the value being referenced is a variable
  5312. const char* namevalue = reference_getname(datavalue);
  5313. if( !not_array_variable(*namevalue) )
  5314. {// array variable being copied into another array variable
  5315. if( sd == NULL && not_server_variable(*namevalue) && !(sd = script_rid2sd(st)) )
  5316. {// player must be attached in order to copy a player variable
  5317. ShowError("script:set: no player attached for player variable '%s'\n", namevalue);
  5318. return SCRIPT_CMD_SUCCESS;
  5319. }
  5320. if( is_string_variable(namevalue) != is_string_variable(name) )
  5321. {// non-matching array value types
  5322. ShowWarning("script:set: two array variables do not match in type.\n");
  5323. return SCRIPT_CMD_SUCCESS;
  5324. }
  5325. // push the maximum number of array values to the stack
  5326. push_val(st->stack, C_INT, SCRIPT_MAX_ARRAYSIZE);
  5327. // call the copy array method directly
  5328. return buildin_copyarray(st);
  5329. }
  5330. }
  5331. #endif
  5332. if( !strcmp(command, "setr") && script_hasdata(st, 4) ) { // Optional argument used by post-increment/post-decrement constructs to return the previous value
  5333. if( is_string_variable(name) )
  5334. script_pushstrcopy(st,script_getstr(st, 4));
  5335. else
  5336. script_pushint(st,script_getnum(st, 4));
  5337. } else // Return a copy of the variable reference
  5338. script_pushcopy(st, 2);
  5339. if( is_string_variable(name) )
  5340. set_reg(st,sd,num,name,(void*)script_getstr(st,3),script_getref(st,2));
  5341. else
  5342. set_reg(st,sd,num,name,(void*)__64BPRTSIZE(script_getnum(st,3)),script_getref(st,2));
  5343. return SCRIPT_CMD_SUCCESS;
  5344. }
  5345. /////////////////////////////////////////////////////////////////////
  5346. /// Array variables
  5347. ///
  5348. /// Sets values of an array, from the starting index.
  5349. /// ex: setarray arr[1],1,2,3;
  5350. ///
  5351. /// setarray <array variable>,<value1>{,<value2>...};
  5352. BUILDIN_FUNC(setarray)
  5353. {
  5354. struct script_data* data;
  5355. const char* name;
  5356. uint32 start;
  5357. uint32 end;
  5358. int32 id;
  5359. int32 i;
  5360. TBL_PC* sd = NULL;
  5361. data = script_getdata(st, 2);
  5362. if( !data_isreference(data) )
  5363. {
  5364. ShowError("script:setarray: not a variable\n");
  5365. script_reportdata(data);
  5366. st->state = END;
  5367. return SCRIPT_CMD_FAILURE;// not a variable
  5368. }
  5369. id = reference_getid(data);
  5370. start = reference_getindex(data);
  5371. name = reference_getname(data);
  5372. if( not_server_variable(*name) )
  5373. {
  5374. sd = script_rid2sd(st);
  5375. if( sd == NULL )
  5376. return SCRIPT_CMD_SUCCESS;// no player attached
  5377. }
  5378. end = start + script_lastdata(st) - 2;
  5379. if( end > SCRIPT_MAX_ARRAYSIZE )
  5380. end = SCRIPT_MAX_ARRAYSIZE;
  5381. if( is_string_variable(name) )
  5382. {// string array
  5383. for( i = 3; start < end; ++start, ++i )
  5384. set_reg(st, sd, reference_uid(id, start), name, (void*)script_getstr(st,i), reference_getref(data));
  5385. }
  5386. else
  5387. {// int array
  5388. for( i = 3; start < end; ++start, ++i )
  5389. set_reg(st, sd, reference_uid(id, start), name, (void*)__64BPRTSIZE(script_getnum(st,i)), reference_getref(data));
  5390. }
  5391. return SCRIPT_CMD_SUCCESS;
  5392. }
  5393. /// Sets count values of an array, from the starting index.
  5394. /// ex: cleararray arr[0],0,1;
  5395. ///
  5396. /// cleararray <array variable>,<value>,<count>;
  5397. BUILDIN_FUNC(cleararray)
  5398. {
  5399. struct script_data* data;
  5400. const char* name;
  5401. uint32 start;
  5402. uint32 end;
  5403. int32 id;
  5404. void* v;
  5405. TBL_PC* sd = NULL;
  5406. data = script_getdata(st, 2);
  5407. if( !data_isreference(data) )
  5408. {
  5409. ShowError("script:cleararray: not a variable\n");
  5410. script_reportdata(data);
  5411. st->state = END;
  5412. return SCRIPT_CMD_FAILURE;// not a variable
  5413. }
  5414. id = reference_getid(data);
  5415. start = reference_getindex(data);
  5416. name = reference_getname(data);
  5417. if( not_server_variable(*name) )
  5418. {
  5419. sd = script_rid2sd(st);
  5420. if( sd == NULL )
  5421. return SCRIPT_CMD_SUCCESS;// no player attached
  5422. }
  5423. if( is_string_variable(name) )
  5424. v = (void*)script_getstr(st, 3);
  5425. else
  5426. v = (void*)__64BPRTSIZE(script_getnum(st, 3));
  5427. end = start + script_getnum(st, 4);
  5428. if( end > SCRIPT_MAX_ARRAYSIZE )
  5429. end = SCRIPT_MAX_ARRAYSIZE;
  5430. for( ; start < end; ++start )
  5431. set_reg(st, sd, reference_uid(id, start), name, v, script_getref(st,2));
  5432. return SCRIPT_CMD_SUCCESS;
  5433. }
  5434. /// Copies data from one array to another.
  5435. /// ex: copyarray arr[0],arr[2],2;
  5436. ///
  5437. /// copyarray <destination array variable>,<source array variable>,<count>;
  5438. BUILDIN_FUNC(copyarray)
  5439. {
  5440. struct script_data* data1;
  5441. struct script_data* data2;
  5442. const char* name1;
  5443. const char* name2;
  5444. int32 idx1;
  5445. int32 idx2;
  5446. int32 id1;
  5447. int32 id2;
  5448. void* v;
  5449. int32 i;
  5450. uint32 count;
  5451. TBL_PC* sd = NULL;
  5452. data1 = script_getdata(st, 2);
  5453. data2 = script_getdata(st, 3);
  5454. if( !data_isreference(data1) || !data_isreference(data2) )
  5455. {
  5456. ShowError("script:copyarray: not a variable\n");
  5457. script_reportdata(data1);
  5458. script_reportdata(data2);
  5459. st->state = END;
  5460. return SCRIPT_CMD_FAILURE;// not a variable
  5461. }
  5462. id1 = reference_getid(data1);
  5463. id2 = reference_getid(data2);
  5464. idx1 = reference_getindex(data1);
  5465. idx2 = reference_getindex(data2);
  5466. name1 = reference_getname(data1);
  5467. name2 = reference_getname(data2);
  5468. if( is_string_variable(name1) != is_string_variable(name2) )
  5469. {
  5470. ShowError("script:copyarray: type mismatch\n");
  5471. script_reportdata(data1);
  5472. script_reportdata(data2);
  5473. st->state = END;
  5474. return SCRIPT_CMD_FAILURE;// data type mismatch
  5475. }
  5476. if( not_server_variable(*name1) || not_server_variable(*name2) )
  5477. {
  5478. sd = script_rid2sd(st);
  5479. if( sd == NULL )
  5480. return SCRIPT_CMD_SUCCESS;// no player attached
  5481. }
  5482. count = script_getnum(st, 4);
  5483. if( count > SCRIPT_MAX_ARRAYSIZE - idx1 )
  5484. count = SCRIPT_MAX_ARRAYSIZE - idx1;
  5485. if( count <= 0 || (id1 == id2 && idx1 == idx2) )
  5486. return SCRIPT_CMD_SUCCESS;// nothing to copy
  5487. if( id1 == id2 && idx1 > idx2 )
  5488. {// destination might be overlapping the source - copy in reverse order
  5489. for( i = count - 1; i >= 0; --i )
  5490. {
  5491. v = get_val2(st, reference_uid(id2, idx2 + i), reference_getref(data2));
  5492. set_reg(st, sd, reference_uid(id1, idx1 + i), name1, v, reference_getref(data1));
  5493. script_removetop(st, -1, 0);
  5494. }
  5495. }
  5496. else
  5497. {// normal copy
  5498. for( i = 0; i < count; ++i )
  5499. {
  5500. if( idx2 + i < SCRIPT_MAX_ARRAYSIZE )
  5501. {
  5502. v = get_val2(st, reference_uid(id2, idx2 + i), reference_getref(data2));
  5503. set_reg(st, sd, reference_uid(id1, idx1 + i), name1, v, reference_getref(data1));
  5504. script_removetop(st, -1, 0);
  5505. }
  5506. else// out of range - assume ""/0
  5507. set_reg(st, sd, reference_uid(id1, idx1 + i), name1, (is_string_variable(name1)?(void*)"":(void*)0), reference_getref(data1));
  5508. }
  5509. }
  5510. return SCRIPT_CMD_SUCCESS;
  5511. }
  5512. /// Returns the size of the array.
  5513. /// Assumes that everything before the starting index exists.
  5514. /// ex: getarraysize(arr[3])
  5515. ///
  5516. /// getarraysize(<array variable>) -> <int>
  5517. BUILDIN_FUNC(getarraysize)
  5518. {
  5519. struct script_data* data;
  5520. const char* name;
  5521. struct map_session_data* sd = NULL;
  5522. data = script_getdata(st, 2);
  5523. if( !data_isreference(data) )
  5524. {
  5525. ShowError("script:getarraysize: not a variable\n");
  5526. script_reportdata(data);
  5527. script_pushnil(st);
  5528. st->state = END;
  5529. return SCRIPT_CMD_FAILURE;// not a variable
  5530. }
  5531. name = reference_getname(data);
  5532. if( not_server_variable(*name) ){
  5533. sd = script_rid2sd(st);
  5534. if (sd == NULL)
  5535. return SCRIPT_CMD_SUCCESS;// no player attached
  5536. }
  5537. script_pushint(st, script_array_highest_key(st, sd, reference_getname(data), reference_getref(data)));
  5538. return SCRIPT_CMD_SUCCESS;
  5539. }
  5540. int script_array_index_cmp(const void *a, const void *b) {
  5541. return ( *(unsigned int*)a - *(unsigned int*)b );
  5542. }
  5543. /// Deletes count or all the elements in an array, from the starting index.
  5544. /// ex: deletearray arr[4],2;
  5545. ///
  5546. /// deletearray <array variable>;
  5547. /// deletearray <array variable>,<count>;
  5548. BUILDIN_FUNC(deletearray)
  5549. {
  5550. struct script_data* data;
  5551. const char* name;
  5552. unsigned int start, end, i;
  5553. int id;
  5554. TBL_PC *sd = NULL;
  5555. struct script_array *sa = NULL;
  5556. struct reg_db *src = NULL;
  5557. void *value;
  5558. data = script_getdata(st, 2);
  5559. if( !data_isreference(data) ) {
  5560. ShowError("script:deletearray: not a variable\n");
  5561. script_reportdata(data);
  5562. st->state = END;
  5563. return SCRIPT_CMD_FAILURE;// not a variable
  5564. }
  5565. id = reference_getid(data);
  5566. start = reference_getindex(data);
  5567. name = reference_getname(data);
  5568. if( not_server_variable(*name) ) {
  5569. sd = script_rid2sd(st);
  5570. if( sd == NULL )
  5571. return SCRIPT_CMD_SUCCESS;// no player attached
  5572. }
  5573. if (!(src = script_array_src(st, sd, name, reference_getref(data)))) {
  5574. ShowError("script:deletearray: not a array\n");
  5575. script_reportdata(data);
  5576. st->state = END;
  5577. return SCRIPT_CMD_FAILURE;// not a variable
  5578. }
  5579. script_array_ensure_zero(st, NULL, data->u.num, reference_getref(data));
  5580. if ( !(sa = idb_get(src->arrays, id)) ) { // non-existent array, nothing to empty
  5581. return SCRIPT_CMD_SUCCESS;// not a variable
  5582. }
  5583. end = script_array_highest_key(st, sd, name, reference_getref(data));
  5584. if( start >= end )
  5585. return SCRIPT_CMD_SUCCESS;// nothing to free
  5586. if( is_string_variable(name) )
  5587. value = (void *)"";
  5588. else
  5589. value = (void *)0;
  5590. if( script_hasdata(st,3) ) {
  5591. unsigned int count = script_getnum(st, 3);
  5592. if( count > end - start )
  5593. count = end - start;
  5594. if( count <= 0 )
  5595. return SCRIPT_CMD_SUCCESS;// nothing to free
  5596. if( end - start < sa->size ) {
  5597. // Better to iterate directly on the array, no speed-up from using sa
  5598. for( ; start + count < end; ++start ) {
  5599. // Compact and overwrite
  5600. void* v = get_val2(st, reference_uid(id, start + count), reference_getref(data));
  5601. set_reg(st, sd, reference_uid(id, start), name, v, reference_getref(data));
  5602. script_removetop(st, -1, 0);
  5603. }
  5604. for( ; start < end; start++ ) {
  5605. // Clean up any leftovers that weren't overwritten
  5606. set_reg(st, sd, reference_uid(id, start), name, value, reference_getref(data));
  5607. }
  5608. } else {
  5609. // using sa to speed up
  5610. unsigned int *list = NULL, size = 0;
  5611. list = script_array_cpy_list(sa);
  5612. size = sa->size;
  5613. qsort(list, size, sizeof(unsigned int), script_array_index_cmp);
  5614. ARR_FIND(0, size, i, list[i] >= start);
  5615. for( ; i < size && list[i] < start + count; i++ ) {
  5616. // Clear any entries between start and start+count, if they exist
  5617. set_reg(st, sd, reference_uid(id, list[i]), name, value, reference_getref(data));
  5618. }
  5619. for( ; i < size && list[i] < end; i++ ) {
  5620. // Move back count positions any entries between start+count to fill the gaps
  5621. void* v = get_val2(st, reference_uid(id, list[i]), reference_getref(data));
  5622. set_reg(st, sd, reference_uid(id, list[i]-count), name, v, reference_getref(data));
  5623. script_removetop(st, -1, 0);
  5624. // Clear their originals
  5625. set_reg(st, sd, reference_uid(id, list[i]), name, value, reference_getref(data));
  5626. }
  5627. }
  5628. } else {
  5629. unsigned int *list = NULL, size = 0;
  5630. list = script_array_cpy_list(sa);
  5631. size = sa->size;
  5632. for(i = 0; i < size; i++) {
  5633. if( list[i] >= start ) // Less expensive than sorting it, most likely
  5634. set_reg(st, sd, reference_uid(id, list[i]), name, value, reference_getref(data));
  5635. }
  5636. }
  5637. return SCRIPT_CMD_SUCCESS;
  5638. }
  5639. /// Returns a reference to the target index of the array variable.
  5640. /// Equivalent to var[index].
  5641. ///
  5642. /// getelementofarray(<array variable>,<index>) -> <variable reference>
  5643. BUILDIN_FUNC(getelementofarray)
  5644. {
  5645. struct script_data* data;
  5646. int32 id;
  5647. int64 i;
  5648. data = script_getdata(st, 2);
  5649. if( !data_isreference(data) )
  5650. {
  5651. ShowError("script:getelementofarray: not a variable\n");
  5652. script_reportdata(data);
  5653. script_pushnil(st);
  5654. st->state = END;
  5655. return SCRIPT_CMD_SUCCESS;// not a variable
  5656. }
  5657. id = reference_getid(data);
  5658. i = script_getnum(st, 3);
  5659. if (i < 0 || i >= SCRIPT_MAX_ARRAYSIZE) {
  5660. ShowWarning("script:getelementofarray: index out of range (%"PRId64")\n", i);
  5661. script_reportdata(data);
  5662. script_pushnil(st);
  5663. st->state = END;
  5664. return SCRIPT_CMD_FAILURE;// out of range
  5665. }
  5666. push_val2(st->stack, C_NAME, reference_uid(id, i), reference_getref(data));
  5667. return SCRIPT_CMD_SUCCESS;
  5668. }
  5669. /////////////////////////////////////////////////////////////////////
  5670. /// ...
  5671. ///
  5672. /*==========================================
  5673. *
  5674. *------------------------------------------*/
  5675. BUILDIN_FUNC(setlook)
  5676. {
  5677. int type,val;
  5678. TBL_PC* sd;
  5679. type = script_getnum(st,2);
  5680. val = script_getnum(st,3);
  5681. if (!script_charid2sd(4,sd))
  5682. return SCRIPT_CMD_SUCCESS;
  5683. pc_changelook(sd,type,val);
  5684. return SCRIPT_CMD_SUCCESS;
  5685. }
  5686. BUILDIN_FUNC(changelook)
  5687. { // As setlook but only client side
  5688. int type,val;
  5689. TBL_PC* sd;
  5690. type = script_getnum(st,2);
  5691. val = script_getnum(st,3);
  5692. if (!script_charid2sd(4,sd))
  5693. return SCRIPT_CMD_SUCCESS;
  5694. clif_changelook(&sd->bl,type,val);
  5695. return SCRIPT_CMD_SUCCESS;
  5696. }
  5697. /*==========================================
  5698. *
  5699. *------------------------------------------*/
  5700. BUILDIN_FUNC(cutin)
  5701. {
  5702. TBL_PC* sd;
  5703. sd = script_rid2sd(st);
  5704. if( sd == NULL )
  5705. return SCRIPT_CMD_SUCCESS;
  5706. clif_cutin(sd,script_getstr(st,2),script_getnum(st,3));
  5707. return SCRIPT_CMD_SUCCESS;
  5708. }
  5709. /*==========================================
  5710. *
  5711. *------------------------------------------*/
  5712. BUILDIN_FUNC(viewpoint)
  5713. {
  5714. int type,x,y,id,color;
  5715. TBL_PC* sd;
  5716. type=script_getnum(st,2);
  5717. x=script_getnum(st,3);
  5718. y=script_getnum(st,4);
  5719. id=script_getnum(st,5);
  5720. color=script_getnum(st,6);
  5721. sd = script_rid2sd(st);
  5722. if( sd == NULL )
  5723. return SCRIPT_CMD_SUCCESS;
  5724. clif_viewpoint(sd,st->oid,type,x,y,id,color);
  5725. return SCRIPT_CMD_SUCCESS;
  5726. }
  5727. /// Returns number of items in inventory/cart/storage
  5728. /// countitem <nameID>{,<accountID>});
  5729. /// countitem2 <nameID>,<Identified>,<Refine>,<Attribute>,<Card0>,<Card1>,<Card2>,<Card3>{,<accountID>}) [Lupus]
  5730. /// cartcountitem <nameID>{,<accountID>});
  5731. /// cartcountitem2 <nameID>,<Identified>,<Refine>,<Attribute>,<Card0>,<Card1>,<Card2>,<Card3>{,<accountID>})
  5732. /// storagecountitem <nameID>{,<accountID>});
  5733. /// storagecountitem2 <nameID>,<Identified>,<Refine>,<Attribute>,<Card0>,<Card1>,<Card2>,<Card3>{,<accountID>})
  5734. /// guildstoragecountitem <nameID>{,<accountID>});
  5735. /// guildstoragecountitem2 <nameID>,<Identified>,<Refine>,<Attribute>,<Card0>,<Card1>,<Card2>,<Card3>{,<accountID>})
  5736. BUILDIN_FUNC(countitem)
  5737. {
  5738. int i = 0, aid = 3;
  5739. struct item_data* id = NULL;
  5740. struct script_data* data;
  5741. char *command = (char *)script_getfuncname(st);
  5742. uint8 loc = 0;
  5743. uint16 size, count = 0;
  5744. struct item *items;
  5745. TBL_PC *sd = NULL;
  5746. struct s_storage *gstor = NULL;
  5747. if( command[strlen(command)-1] == '2' ) {
  5748. i = 1;
  5749. aid = 10;
  5750. }
  5751. if( script_hasdata(st,aid) ) {
  5752. if( !(sd = map_id2sd( (aid = script_getnum(st, aid)) )) ) {
  5753. ShowError("buildin_%s: player not found (AID=%d).\n", command, aid);
  5754. st->state = END;
  5755. return SCRIPT_CMD_FAILURE;
  5756. }
  5757. }
  5758. else {
  5759. if( !(sd = script_rid2sd(st)) )
  5760. return SCRIPT_CMD_SUCCESS;
  5761. }
  5762. if( !strncmp(command, "cart", 4) ) {
  5763. loc = TABLE_CART;
  5764. size = MAX_CART;
  5765. items = sd->cart.u.items_cart;
  5766. }
  5767. else if( !strncmp(command, "storage", 7) ) {
  5768. loc = TABLE_STORAGE;
  5769. size = MAX_STORAGE;
  5770. items = sd->storage.u.items_storage;
  5771. }
  5772. else if( !strncmp(command, "guildstorage", 12) ) {
  5773. gstor = guild2storage2(sd->status.guild_id);
  5774. if (gstor && !sd->state.storage_flag) {
  5775. loc = TABLE_GUILD_STORAGE;
  5776. size = MAX_GUILD_STORAGE;
  5777. items = gstor->u.items_guild;
  5778. } else {
  5779. script_pushint(st, -1);
  5780. return SCRIPT_CMD_SUCCESS;
  5781. }
  5782. }
  5783. else {
  5784. size = MAX_INVENTORY;
  5785. items = sd->inventory.u.items_inventory;
  5786. }
  5787. if( loc == TABLE_CART && !pc_iscarton(sd) ) {
  5788. ShowError("buildin_%s: Player doesn't have cart (CID:%d).\n", command, sd->status.char_id);
  5789. script_pushint(st,-1);
  5790. return SCRIPT_CMD_SUCCESS;
  5791. }
  5792. data = script_getdata(st, 2);
  5793. get_val(st, data); // Convert into value in case of a variable
  5794. if( data_isstring(data) ) // item name
  5795. id = itemdb_searchname(conv_str(st, data));
  5796. else // item id
  5797. id = itemdb_exists(conv_num(st, data));
  5798. if( id == NULL ) {
  5799. ShowError("buildin_%s: Invalid item '%s'.\n", command, script_getstr(st,2)); // returns string, regardless of what it was
  5800. script_pushint(st,0);
  5801. return SCRIPT_CMD_FAILURE;
  5802. }
  5803. if (loc == TABLE_GUILD_STORAGE)
  5804. gstor->lock = true;
  5805. if( !i ) { // For count/cart/storagecountitem function
  5806. unsigned short nameid = id->nameid;
  5807. for( i = 0; i < size; i++ )
  5808. if( &items[i] && items[i].nameid == nameid )
  5809. count += items[i].amount;
  5810. }
  5811. else { // For count/cart/storagecountitem2 function
  5812. unsigned short nameid;
  5813. int iden, ref, attr, c1, c2, c3, c4;
  5814. nameid = id->nameid;
  5815. iden = script_getnum(st,3);
  5816. ref = script_getnum(st,4);
  5817. attr = script_getnum(st,5);
  5818. c1 = script_getnum(st,6);
  5819. c2 = script_getnum(st,7);
  5820. c3 = script_getnum(st,8);
  5821. c4 = script_getnum(st,9);
  5822. for( i = 0; i < size; i++ )
  5823. if( &items[i] && items[i].nameid > 0 && items[i].nameid == nameid &&
  5824. items[i].amount > 0 && items[i].identify == iden &&
  5825. items[i].refine == ref && items[i].attribute == attr &&
  5826. items[i].card[0] == c1 && items[i].card[1] == c2 &&
  5827. items[i].card[2] == c3 && items[i].card[3] == c4 )
  5828. {
  5829. count += items[i].amount;
  5830. }
  5831. }
  5832. if (loc == TABLE_GUILD_STORAGE) {
  5833. storage_guild_storageclose(sd);
  5834. gstor->lock = false;
  5835. }
  5836. script_pushint(st, count);
  5837. return SCRIPT_CMD_SUCCESS;
  5838. }
  5839. /*==========================================
  5840. * Check if item with this amount can fit in inventory
  5841. * Checking : weight, stack amount >(MAX_AMOUNT), slots amount >(MAX_INVENTORY)
  5842. * Return
  5843. * 0 : fail
  5844. * 1 : success (npc side only)
  5845. *------------------------------------------*/
  5846. BUILDIN_FUNC(checkweight)
  5847. {
  5848. int slots = 0;
  5849. unsigned short amount2 = 0;
  5850. unsigned int weight = 0, i, nbargs;
  5851. struct item_data* id = NULL;
  5852. struct map_session_data* sd;
  5853. if( ( sd = script_rid2sd(st) ) == NULL )
  5854. return SCRIPT_CMD_SUCCESS;
  5855. nbargs = script_lastdata(st)+1;
  5856. if(nbargs%2) {
  5857. ShowError("buildin_checkweight: Invalid nb of args should be a multiple of 2.\n");
  5858. script_pushint(st,0);
  5859. return SCRIPT_CMD_FAILURE;
  5860. }
  5861. slots = pc_inventoryblank(sd); //nb of empty slot
  5862. for (i = 2; i < nbargs; i += 2) {
  5863. unsigned short nameid, amount;
  5864. struct script_data* data = script_getdata(st,i);
  5865. get_val(st, data); // Convert into value in case of a variable
  5866. if( data_isstring(data) ) // item name
  5867. id = itemdb_searchname(conv_str(st, data));
  5868. else // item id
  5869. id = itemdb_exists(conv_num(st, data));
  5870. if( id == NULL ) {
  5871. ShowError("buildin_checkweight: Invalid item '%s'.\n", script_getstr(st,i)); // returns string, regardless of what it was
  5872. script_pushint(st,0);
  5873. return SCRIPT_CMD_FAILURE;
  5874. }
  5875. nameid = id->nameid;
  5876. amount = script_getnum(st,i+1);
  5877. if( amount < 1 ) {
  5878. ShowError("buildin_checkweight: Invalid amount '%d'.\n", amount);
  5879. script_pushint(st,0);
  5880. return SCRIPT_CMD_FAILURE;
  5881. }
  5882. weight += itemdb_weight(nameid)*amount; //total weight for all chk
  5883. if( weight + sd->weight > sd->max_weight )
  5884. {// too heavy
  5885. script_pushint(st,0);
  5886. return SCRIPT_CMD_SUCCESS;
  5887. }
  5888. switch( pc_checkadditem(sd, nameid, amount) ) {
  5889. case CHKADDITEM_EXIST:
  5890. // item is already in inventory, but there is still space for the requested amount
  5891. break;
  5892. case CHKADDITEM_NEW:
  5893. if( itemdb_isstackable(nameid) ) {
  5894. // stackable
  5895. amount2++;
  5896. if( slots < amount2 ) {
  5897. script_pushint(st,0);
  5898. return SCRIPT_CMD_SUCCESS;
  5899. }
  5900. } else {
  5901. // non-stackable
  5902. amount2 += amount;
  5903. if( slots < amount2) {
  5904. script_pushint(st,0);
  5905. return SCRIPT_CMD_SUCCESS;
  5906. }
  5907. }
  5908. break;
  5909. case CHKADDITEM_OVERAMOUNT:
  5910. script_pushint(st,0);
  5911. return SCRIPT_CMD_SUCCESS;
  5912. }
  5913. }
  5914. script_pushint(st,1);
  5915. return SCRIPT_CMD_SUCCESS;
  5916. }
  5917. BUILDIN_FUNC(checkweight2)
  5918. {
  5919. //variable sub checkweight
  5920. int i = 0, slots = 0, weight = 0;
  5921. short fail = 0;
  5922. unsigned short amount2 = 0;
  5923. //variable for array parsing
  5924. struct script_data* data_it;
  5925. struct script_data* data_nb;
  5926. const char* name_it;
  5927. const char* name_nb;
  5928. int32 id_it, id_nb;
  5929. int32 idx_it, idx_nb;
  5930. int nb_it, nb_nb; //array size
  5931. TBL_PC *sd = script_rid2sd(st);
  5932. nullpo_retr(1,sd);
  5933. data_it = script_getdata(st, 2);
  5934. data_nb = script_getdata(st, 3);
  5935. if( !data_isreference(data_it) || !data_isreference(data_nb)) {
  5936. ShowError("buildin_checkweight2: parameter not a variable\n");
  5937. script_pushint(st,0);
  5938. return SCRIPT_CMD_FAILURE;// not a variable
  5939. }
  5940. id_it = reference_getid(data_it);
  5941. id_nb = reference_getid(data_nb);
  5942. idx_it = reference_getindex(data_it);
  5943. idx_nb = reference_getindex(data_nb);
  5944. name_it = reference_getname(data_it);
  5945. name_nb = reference_getname(data_nb);
  5946. if(is_string_variable(name_it) || is_string_variable(name_nb)) {
  5947. ShowError("buildin_checkweight2: illegal type, need int\n");
  5948. script_pushint(st,0);
  5949. return SCRIPT_CMD_FAILURE;// not supported
  5950. }
  5951. nb_it = script_array_highest_key(st, sd, reference_getname(data_it), reference_getref(data_it));
  5952. nb_nb = script_array_highest_key(st, sd, reference_getname(data_nb), reference_getref(data_nb));
  5953. if(nb_it != nb_nb) {
  5954. ShowError("buildin_checkweight2: Size mistmatch: nb_it=%d, nb_nb=%d\n",nb_it,nb_nb);
  5955. fail = 1;
  5956. }
  5957. slots = pc_inventoryblank(sd);
  5958. for(i=0; i<nb_it; i++) {
  5959. unsigned short amount, nameid = (int32)__64BPRTSIZE(get_val2(st,reference_uid(id_it,idx_it+i),reference_getref(data_it)));
  5960. script_removetop(st, -1, 0);
  5961. amount = (int32)__64BPRTSIZE(get_val2(st,reference_uid(id_nb,idx_nb+i),reference_getref(data_nb)));
  5962. script_removetop(st, -1, 0);
  5963. if(fail)
  5964. continue; //cpntonie to depop rest
  5965. if(itemdb_exists(nameid) == NULL ) {
  5966. ShowError("buildin_checkweight2: Invalid item '%d'.\n", nameid);
  5967. fail=1;
  5968. continue;
  5969. }
  5970. if(amount < 0 ) {
  5971. ShowError("buildin_checkweight2: Invalid amount '%d'.\n", amount);
  5972. fail = 1;
  5973. continue;
  5974. }
  5975. weight += itemdb_weight(nameid)*amount;
  5976. if( weight + sd->weight > sd->max_weight ) {
  5977. fail = 1;
  5978. continue;
  5979. }
  5980. switch( pc_checkadditem(sd, nameid, amount) ) {
  5981. case CHKADDITEM_EXIST:
  5982. // item is already in inventory, but there is still space for the requested amount
  5983. break;
  5984. case CHKADDITEM_NEW:
  5985. if( itemdb_isstackable(nameid) ) {// stackable
  5986. amount2++;
  5987. if( slots < amount2 )
  5988. fail = 1;
  5989. } else {// non-stackable
  5990. amount2 += amount;
  5991. if( slots < amount2 ) {
  5992. fail = 1;
  5993. }
  5994. }
  5995. break;
  5996. case CHKADDITEM_OVERAMOUNT:
  5997. fail = 1;
  5998. } //end switch
  5999. } //end loop DO NOT break it prematurly we need to depop all stack
  6000. fail ? script_pushint(st,0) : script_pushint(st,1);
  6001. return SCRIPT_CMD_SUCCESS;
  6002. }
  6003. /*==========================================
  6004. * getitem <item id>,<amount>{,<account ID>};
  6005. * getitem "<item name>",<amount>{,<account ID>};
  6006. *
  6007. * getitembound <item id>,<amount>,<type>{,<account ID>};
  6008. * getitembound "<item id>",<amount>,<type>{,<account ID>};
  6009. * Type:
  6010. * 0 - No bound
  6011. * 1 - Account Bound
  6012. * 2 - Guild Bound
  6013. * 3 - Party Bound
  6014. * 4 - Character Bound
  6015. *------------------------------------------*/
  6016. BUILDIN_FUNC(getitem)
  6017. {
  6018. int get_count, i;
  6019. unsigned short nameid, amount;
  6020. struct item it;
  6021. TBL_PC *sd;
  6022. struct script_data *data;
  6023. unsigned char flag = 0;
  6024. const char* command = script_getfuncname(st);
  6025. struct item_data *id = NULL;
  6026. data = script_getdata(st,2);
  6027. get_val(st,data);
  6028. if( data_isstring(data) ) {// "<item name>"
  6029. const char *name = conv_str(st,data);
  6030. id = itemdb_searchname(name);
  6031. if( id == NULL ){
  6032. ShowError("buildin_getitem: Nonexistant item %s requested.\n", name);
  6033. return SCRIPT_CMD_FAILURE; //No item created.
  6034. }
  6035. nameid = id->nameid;
  6036. } else if( data_isint(data) ) {// <item id>
  6037. nameid = conv_num(st,data);
  6038. if( !(id = itemdb_exists(nameid)) ){
  6039. ShowError("buildin_getitem: Nonexistant item %d requested.\n", nameid);
  6040. return SCRIPT_CMD_FAILURE; //No item created.
  6041. }
  6042. } else {
  6043. ShowError("buildin_getitem: invalid data type for argument #1 (%d).", data->type);
  6044. return SCRIPT_CMD_FAILURE;
  6045. }
  6046. // <amount>
  6047. if( (amount = script_getnum(st,3)) <= 0)
  6048. return SCRIPT_CMD_SUCCESS; //return if amount <=0, skip the useles iteration
  6049. memset(&it,0,sizeof(it));
  6050. it.nameid = nameid;
  6051. it.identify = 1;
  6052. it.bound = BOUND_NONE;
  6053. if( !strcmp(command,"getitembound") ) {
  6054. char bound = script_getnum(st,4);
  6055. if( bound < BOUND_NONE || bound >= BOUND_MAX ) {
  6056. ShowError("script_getitembound: Not a correct bound type! Type=%d\n",bound);
  6057. return SCRIPT_CMD_FAILURE;
  6058. }
  6059. if( script_hasdata(st,5) )
  6060. sd = map_id2sd(script_getnum(st,5));
  6061. else
  6062. sd = script_rid2sd(st); // Attached player
  6063. it.bound = bound;
  6064. } else if( script_hasdata(st,4) )
  6065. sd = map_id2sd(script_getnum(st,4)); // <Account ID>
  6066. else
  6067. sd = script_rid2sd(st); // Attached player
  6068. if( sd == NULL ) // no target
  6069. return SCRIPT_CMD_SUCCESS;
  6070. //Check if it's stackable.
  6071. if (!itemdb_isstackable2(id))
  6072. get_count = 1;
  6073. else
  6074. get_count = amount;
  6075. for (i = 0; i < amount; i += get_count)
  6076. {
  6077. // if not pet egg
  6078. if (!pet_create_egg(sd, nameid))
  6079. {
  6080. if ((flag = pc_additem(sd, &it, get_count, LOG_TYPE_SCRIPT)))
  6081. {
  6082. clif_additem(sd, 0, 0, flag);
  6083. if( pc_candrop(sd,&it) )
  6084. map_addflooritem(&it,get_count,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0,0);
  6085. }
  6086. }
  6087. }
  6088. return SCRIPT_CMD_SUCCESS;
  6089. }
  6090. /*==========================================
  6091. * getitem2 <item id>,<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>{,<account ID>};
  6092. * getitem2 "<item name>",<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>{,<account ID>};
  6093. *
  6094. * getitembound2 <item id>,<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>,<bound type>{,<account ID>};
  6095. * getitembound2 "<item name>",<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>,<bound type>{,<account ID>};
  6096. * Type:
  6097. * 0 - No bound
  6098. * 1 - Account Bound
  6099. * 2 - Guild Bound
  6100. * 3 - Party Bound
  6101. * 4 - Character Bound
  6102. *------------------------------------------*/
  6103. BUILDIN_FUNC(getitem2)
  6104. {
  6105. unsigned short nameid, amount;
  6106. int iden, ref, attr;
  6107. unsigned short c1, c2, c3, c4;
  6108. char bound = BOUND_NONE;
  6109. struct item_data *item_data = NULL;
  6110. struct item item_tmp;
  6111. TBL_PC *sd;
  6112. struct script_data *data;
  6113. const char* command = script_getfuncname(st);
  6114. if( !strcmp(command,"getitembound2") ) {
  6115. bound = script_getnum(st,11);
  6116. if( bound < BOUND_NONE || bound >= BOUND_MAX ) {
  6117. ShowError("script_getitembound2: Not a correct bound type! Type=%d\n",bound);
  6118. return SCRIPT_CMD_FAILURE;
  6119. }
  6120. if( script_hasdata(st,12) )
  6121. sd = map_id2sd(script_getnum(st,12));
  6122. else
  6123. sd = script_rid2sd(st); // Attached player
  6124. } else if( script_hasdata(st,11) )
  6125. sd = map_id2sd(script_getnum(st,11)); // <Account ID>
  6126. else
  6127. sd = script_rid2sd(st); // Attached player
  6128. if( sd == NULL ) // no target
  6129. return SCRIPT_CMD_SUCCESS;
  6130. data = script_getdata(st,2);
  6131. get_val(st,data);
  6132. if( data_isstring(data) ) {
  6133. const char *name = conv_str(st,data);
  6134. if( (item_data = itemdb_searchname(name)) == NULL ){
  6135. ShowError("buildin_getitem2: Nonexistant item %s requested (by conv_str).\n", name);
  6136. return SCRIPT_CMD_FAILURE; //No item created.
  6137. }
  6138. nameid = item_data->nameid;
  6139. } else {
  6140. nameid = conv_num(st,data);
  6141. if( (item_data = itemdb_exists(nameid)) == NULL ){
  6142. ShowError("buildin_getitem2: Nonexistant item %d requested (by conv_num).\n", nameid);
  6143. return SCRIPT_CMD_FAILURE; //No item created.
  6144. }
  6145. }
  6146. amount = script_getnum(st,3);
  6147. iden = script_getnum(st,4);
  6148. ref = script_getnum(st,5);
  6149. attr = script_getnum(st,6);
  6150. c1 = (unsigned short)script_getnum(st,7);
  6151. c2 = (unsigned short)script_getnum(st,8);
  6152. c3 = (unsigned short)script_getnum(st,9);
  6153. c4 = (unsigned short)script_getnum(st,10);
  6154. if( item_data ) {
  6155. int get_count = 0, i;
  6156. memset(&item_tmp,0,sizeof(item_tmp));
  6157. if( item_data->type == IT_WEAPON || item_data->type == IT_ARMOR || item_data->type == IT_SHADOWGEAR ) {
  6158. if(ref > MAX_REFINE)
  6159. ref = MAX_REFINE;
  6160. }
  6161. else if( item_data->type == IT_PETEGG ) {
  6162. iden = 1;
  6163. ref = 0;
  6164. }
  6165. else {
  6166. iden = 1;
  6167. ref = attr = 0;
  6168. }
  6169. item_tmp.nameid = nameid;
  6170. item_tmp.identify = iden;
  6171. item_tmp.refine = ref;
  6172. item_tmp.attribute = attr;
  6173. item_tmp.card[0] = c1;
  6174. item_tmp.card[1] = c2;
  6175. item_tmp.card[2] = c3;
  6176. item_tmp.card[3] = c4;
  6177. item_tmp.bound = bound;
  6178. //Check if it's stackable.
  6179. if (!itemdb_isstackable2(item_data))
  6180. get_count = 1;
  6181. else
  6182. get_count = amount;
  6183. for (i = 0; i < amount; i += get_count)
  6184. {
  6185. // if not pet egg
  6186. if (!pet_create_egg(sd, nameid))
  6187. {
  6188. unsigned char flag = 0;
  6189. if ((flag = pc_additem(sd, &item_tmp, get_count, LOG_TYPE_SCRIPT)))
  6190. {
  6191. clif_additem(sd, 0, 0, flag);
  6192. if( pc_candrop(sd,&item_tmp) )
  6193. map_addflooritem(&item_tmp,get_count,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0,0);
  6194. }
  6195. }
  6196. }
  6197. }
  6198. return SCRIPT_CMD_SUCCESS;
  6199. }
  6200. /** Gives rental item to player
  6201. * rentitem <item id>,<seconds>{,<account_id>}
  6202. * rentitem "<item name>",<seconds>{,<account_id>}
  6203. */
  6204. BUILDIN_FUNC(rentitem) {
  6205. struct map_session_data *sd;
  6206. struct script_data *data;
  6207. struct item it;
  6208. int seconds;
  6209. unsigned short nameid = 0;
  6210. unsigned char flag = 0;
  6211. data = script_getdata(st,2);
  6212. get_val(st,data);
  6213. if (!script_accid2sd(4,sd))
  6214. return SCRIPT_CMD_FAILURE;
  6215. if( data_isstring(data) )
  6216. {
  6217. const char *name = conv_str(st,data);
  6218. struct item_data *itd = itemdb_searchname(name);
  6219. if( itd == NULL )
  6220. {
  6221. ShowError("buildin_rentitem: Nonexistant item %s requested.\n", name);
  6222. return SCRIPT_CMD_FAILURE;
  6223. }
  6224. nameid = itd->nameid;
  6225. }
  6226. else if( data_isint(data) )
  6227. {
  6228. nameid = conv_num(st,data);
  6229. if( nameid == 0 || !itemdb_exists(nameid) )
  6230. {
  6231. ShowError("buildin_rentitem: Nonexistant item %hu requested.\n", nameid);
  6232. return SCRIPT_CMD_FAILURE;
  6233. }
  6234. }
  6235. else
  6236. {
  6237. ShowError("buildin_rentitem: invalid data type for argument #1 (%d).\n", data->type);
  6238. return SCRIPT_CMD_FAILURE;
  6239. }
  6240. seconds = script_getnum(st,3);
  6241. memset(&it, 0, sizeof(it));
  6242. it.nameid = nameid;
  6243. it.identify = 1;
  6244. it.expire_time = (unsigned int)(time(NULL) + seconds);
  6245. it.bound = BOUND_NONE;
  6246. if( (flag = pc_additem(sd, &it, 1, LOG_TYPE_SCRIPT)) )
  6247. {
  6248. clif_additem(sd, 0, 0, flag);
  6249. return SCRIPT_CMD_FAILURE;
  6250. }
  6251. return SCRIPT_CMD_SUCCESS;
  6252. }
  6253. /** Gives rental item to player with advanced option
  6254. * rentitem2 <item id>,<time>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>{,<account_id>};
  6255. * rentitem2 "<item name>",<time>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>{,<account_id>};
  6256. */
  6257. BUILDIN_FUNC(rentitem2) {
  6258. struct map_session_data *sd;
  6259. struct script_data *data;
  6260. struct item it;
  6261. struct item_data *id;
  6262. int seconds;
  6263. unsigned short nameid = 0;
  6264. unsigned char flag = 0;
  6265. int iden,ref,attr,c1,c2,c3,c4;
  6266. data = script_getdata(st,2);
  6267. get_val(st,data);
  6268. if (!script_accid2sd(11,sd))
  6269. return SCRIPT_CMD_FAILURE;
  6270. if( data_isstring(data) ) {
  6271. const char *name = conv_str(st,data);
  6272. id = itemdb_searchname(name);
  6273. if( id == NULL ) {
  6274. ShowError("buildin_rentitem2: Nonexistant item %s requested.\n", name);
  6275. return SCRIPT_CMD_FAILURE;
  6276. }
  6277. nameid = id->nameid;
  6278. }
  6279. else if( data_isint(data) ) {
  6280. nameid = conv_num(st,data);
  6281. if( !(id = itemdb_search(nameid))) {
  6282. ShowError("buildin_rentitem2: Nonexistant item %hu requested.\n", nameid);
  6283. return SCRIPT_CMD_FAILURE;
  6284. }
  6285. }
  6286. else {
  6287. ShowError("buildin_rentitem2: invalid data type for argument #1 (%d).\n", data->type);
  6288. return SCRIPT_CMD_FAILURE;
  6289. }
  6290. seconds = script_getnum(st,3);
  6291. iden = script_getnum(st,4);
  6292. ref = script_getnum(st,5);
  6293. attr = script_getnum(st,6);
  6294. if (id->type==IT_WEAPON || id->type==IT_ARMOR || id->type==IT_SHADOWGEAR) {
  6295. if(ref > MAX_REFINE) ref = MAX_REFINE;
  6296. }
  6297. else if (id->type==IT_PETEGG) {
  6298. iden = 1;
  6299. ref = 0;
  6300. }
  6301. else {
  6302. iden = 1;
  6303. ref = attr = 0;
  6304. }
  6305. c1 = (short)script_getnum(st,7);
  6306. c2 = (short)script_getnum(st,8);
  6307. c3 = (short)script_getnum(st,9);
  6308. c4 = (short)script_getnum(st,10);
  6309. memset(&it, 0, sizeof(it));
  6310. it.nameid = nameid;
  6311. it.identify = iden;
  6312. it.refine = ref;
  6313. it.attribute = attr;
  6314. it.card[0] = (short)c1;
  6315. it.card[1] = (short)c2;
  6316. it.card[2] = (short)c3;
  6317. it.card[3] = (short)c4;
  6318. it.expire_time = (unsigned int)(time(NULL) + seconds);
  6319. if( (flag = pc_additem(sd, &it, 1, LOG_TYPE_SCRIPT)) ) {
  6320. clif_additem(sd, 0, 0, flag);
  6321. return SCRIPT_CMD_FAILURE;
  6322. }
  6323. return SCRIPT_CMD_SUCCESS;
  6324. }
  6325. /*==========================================
  6326. * gets an item with someone's name inscribed [Skotlex]
  6327. * getinscribeditem item_num, character_name
  6328. * Returned Qty is always 1, only works on equip-able
  6329. * equipment
  6330. *------------------------------------------*/
  6331. BUILDIN_FUNC(getnameditem)
  6332. {
  6333. unsigned short nameid;
  6334. struct item item_tmp;
  6335. TBL_PC *sd, *tsd;
  6336. struct script_data *data;
  6337. sd = script_rid2sd(st);
  6338. if (sd == NULL)
  6339. { //Player not attached!
  6340. script_pushint(st,0);
  6341. return SCRIPT_CMD_SUCCESS;
  6342. }
  6343. data=script_getdata(st,2);
  6344. get_val(st,data);
  6345. if( data_isstring(data) ){
  6346. const char *name=conv_str(st,data);
  6347. struct item_data *item_data = itemdb_searchname(name);
  6348. if( item_data == NULL)
  6349. { //Failed
  6350. script_pushint(st,0);
  6351. return SCRIPT_CMD_SUCCESS;
  6352. }
  6353. nameid = item_data->nameid;
  6354. }else
  6355. nameid = conv_num(st,data);
  6356. if(!itemdb_exists(nameid)/* || itemdb_isstackable(nameid)*/)
  6357. { //Even though named stackable items "could" be risky, they are required for certain quests.
  6358. script_pushint(st,0);
  6359. return SCRIPT_CMD_SUCCESS;
  6360. }
  6361. data=script_getdata(st,3);
  6362. get_val(st,data);
  6363. if( data_isstring(data) ) //Char Name
  6364. tsd=map_nick2sd(conv_str(st,data));
  6365. else //Char Id was given
  6366. tsd=map_charid2sd(conv_num(st,data));
  6367. if( tsd == NULL )
  6368. { //Failed
  6369. script_pushint(st,0);
  6370. return SCRIPT_CMD_SUCCESS;
  6371. }
  6372. memset(&item_tmp,0,sizeof(item_tmp));
  6373. item_tmp.nameid=nameid;
  6374. item_tmp.amount=1;
  6375. item_tmp.identify=1;
  6376. item_tmp.card[0]=CARD0_CREATE; //we don't use 255! because for example SIGNED WEAPON shouldn't get TOP10 BS Fame bonus [Lupus]
  6377. item_tmp.card[2]=tsd->status.char_id;
  6378. item_tmp.card[3]=tsd->status.char_id >> 16;
  6379. if(pc_additem(sd,&item_tmp,1,LOG_TYPE_SCRIPT)) {
  6380. script_pushint(st,0);
  6381. return SCRIPT_CMD_SUCCESS; //Failed to add item, we will not drop if they don't fit
  6382. }
  6383. script_pushint(st,1);
  6384. return SCRIPT_CMD_SUCCESS;
  6385. }
  6386. /*==========================================
  6387. * gets a random item ID from an item group [Skotlex]
  6388. * groupranditem <group_num>{,<sub_group>};
  6389. *------------------------------------------*/
  6390. BUILDIN_FUNC(grouprandomitem) {
  6391. struct s_item_group_entry *entry = NULL;
  6392. int sub_group = 1;
  6393. FETCH(3, sub_group);
  6394. entry = itemdb_get_randgroupitem(script_getnum(st,2),sub_group);
  6395. if (!entry) {
  6396. ShowError("buildin_grouprandomitem: Invalid item group with group_id '%d', sub_group '%d'.\n", script_getnum(st,2), sub_group);
  6397. script_pushint(st,UNKNOWN_ITEM_ID);
  6398. return SCRIPT_CMD_FAILURE;
  6399. }
  6400. script_pushint(st,entry->nameid);
  6401. return SCRIPT_CMD_SUCCESS;
  6402. }
  6403. /**
  6404. * makeitem <item id>,<amount>,"<map name>",<X>,<Y>;
  6405. * makeitem "<item name>",<amount>,"<map name>",<X>,<Y>;
  6406. */
  6407. BUILDIN_FUNC(makeitem) {
  6408. uint16 nameid, amount, flag = 0, x, y;
  6409. const char *mapname;
  6410. int m;
  6411. struct item item_tmp;
  6412. struct script_data *data;
  6413. data = script_getdata(st,2);
  6414. get_val(st,data);
  6415. if( data_isstring(data) ){
  6416. const char *name = conv_str(st,data);
  6417. struct item_data *item_data = itemdb_searchname(name);
  6418. if( item_data )
  6419. nameid = item_data->nameid;
  6420. else
  6421. nameid = UNKNOWN_ITEM_ID;
  6422. }
  6423. else
  6424. nameid = conv_num(st,data);
  6425. amount = script_getnum(st,3);
  6426. mapname = script_getstr(st,4);
  6427. x = script_getnum(st,5);
  6428. y = script_getnum(st,6);
  6429. if(strcmp(mapname,"this")==0) {
  6430. TBL_PC *sd;
  6431. sd = script_rid2sd(st);
  6432. if (!sd)
  6433. return SCRIPT_CMD_SUCCESS; //Failed...
  6434. m = sd->bl.m;
  6435. } else
  6436. m = map_mapname2mapid(mapname);
  6437. if(nameid<0) {
  6438. nameid = -nameid;
  6439. flag = 1;
  6440. }
  6441. if(nameid > 0) {
  6442. memset(&item_tmp,0,sizeof(item_tmp));
  6443. item_tmp.nameid = nameid;
  6444. if(!flag)
  6445. item_tmp.identify = 1;
  6446. else
  6447. item_tmp.identify = itemdb_isidentified(nameid);
  6448. map_addflooritem(&item_tmp,amount,m,x,y,0,0,0,4,0);
  6449. }
  6450. return SCRIPT_CMD_SUCCESS;
  6451. }
  6452. /**
  6453. * makeitem2 <item id>,<amount>,"<map name>",<X>,<Y>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>;
  6454. * makeitem2 "<item name>",<amount>,"<map name>",<X>,<Y>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>;
  6455. */
  6456. BUILDIN_FUNC(makeitem2) {
  6457. uint16 nameid, amount, x, y;
  6458. const char *mapname;
  6459. int m;
  6460. struct item item_tmp;
  6461. struct script_data *data;
  6462. struct item_data *id;
  6463. data = script_getdata(st,2);
  6464. get_val(st,data);
  6465. if( data_isstring(data) ){
  6466. const char *name = conv_str(st,data);
  6467. struct item_data *item_data = itemdb_searchname(name);
  6468. if( item_data )
  6469. nameid = item_data->nameid;
  6470. else
  6471. nameid = UNKNOWN_ITEM_ID;
  6472. }
  6473. else
  6474. nameid = conv_num(st,data);
  6475. amount = script_getnum(st,3);
  6476. mapname = script_getstr(st,4);
  6477. x = script_getnum(st,5);
  6478. y = script_getnum(st,6);
  6479. if (strcmp(mapname,"this")==0) {
  6480. TBL_PC *sd;
  6481. sd = script_rid2sd(st);
  6482. if (!sd)
  6483. return SCRIPT_CMD_SUCCESS; //Failed...
  6484. m = sd->bl.m;
  6485. }
  6486. else
  6487. m = map_mapname2mapid(mapname);
  6488. if ((id = itemdb_search(nameid))) {
  6489. char iden, ref, attr;
  6490. memset(&item_tmp,0,sizeof(item_tmp));
  6491. item_tmp.nameid = nameid;
  6492. iden = (char)script_getnum(st,7);
  6493. ref = (char)script_getnum(st,8);
  6494. attr = (char)script_getnum(st,9);
  6495. if (id->type==IT_WEAPON || id->type==IT_ARMOR || id->type==IT_SHADOWGEAR) {
  6496. if(ref > MAX_REFINE) ref = MAX_REFINE;
  6497. }
  6498. else if (id->type==IT_PETEGG) {
  6499. iden = 1;
  6500. ref = 0;
  6501. }
  6502. else {
  6503. iden = 1;
  6504. ref = attr = 0;
  6505. }
  6506. item_tmp.identify = iden;
  6507. item_tmp.refine = ref;
  6508. item_tmp.attribute = attr;
  6509. item_tmp.card[0] = script_getnum(st,10);
  6510. item_tmp.card[1] = script_getnum(st,11);
  6511. item_tmp.card[2] = script_getnum(st,12);
  6512. item_tmp.card[3] = script_getnum(st,13);
  6513. map_addflooritem(&item_tmp,amount,m,x,y,0,0,0,4,0);
  6514. }
  6515. else
  6516. return SCRIPT_CMD_FAILURE;
  6517. return SCRIPT_CMD_SUCCESS;
  6518. }
  6519. /// Counts / deletes the current item given by idx.
  6520. /// Used by buildin_delitem_search
  6521. /// Relies on all input data being already fully valid.
  6522. static void buildin_delitem_delete(struct map_session_data* sd, int idx, int* amount, uint8 loc, bool delete_items)
  6523. {
  6524. int delamount;
  6525. struct item *itm = NULL;
  6526. struct s_storage *gstor = NULL;
  6527. switch(loc) {
  6528. case TABLE_CART:
  6529. itm = &sd->cart.u.items_cart[idx];
  6530. break;
  6531. case TABLE_STORAGE:
  6532. itm = &sd->storage.u.items_storage[idx];
  6533. break;
  6534. case TABLE_GUILD_STORAGE:
  6535. {
  6536. gstor = guild2storage2(sd->status.guild_id);
  6537. itm = &gstor->u.items_guild[idx];
  6538. }
  6539. break;
  6540. default: // TABLE_INVENTORY
  6541. itm = &sd->inventory.u.items_inventory[idx];
  6542. break;
  6543. }
  6544. delamount = ( amount[0] < itm->amount ) ? amount[0] : itm->amount;
  6545. if( delete_items )
  6546. {
  6547. if( itemdb_type(itm->nameid) == IT_PETEGG && itm->card[0] == CARD0_PET )
  6548. {// delete associated pet
  6549. intif_delete_petdata(MakeDWord(itm->card[1], itm->card[2]));
  6550. }
  6551. switch(loc) {
  6552. case TABLE_CART:
  6553. pc_cart_delitem(sd,idx,delamount,0,LOG_TYPE_SCRIPT);
  6554. break;
  6555. case TABLE_STORAGE:
  6556. storage_delitem(sd,&sd->storage,idx,delamount);
  6557. log_pick_pc(sd,LOG_TYPE_SCRIPT,-delamount,itm);
  6558. break;
  6559. case TABLE_GUILD_STORAGE:
  6560. gstor->lock = true;
  6561. storage_guild_delitem(sd, gstor, idx, delamount);
  6562. log_pick_pc(sd, LOG_TYPE_SCRIPT, -delamount, itm);
  6563. storage_guild_storageclose(sd);
  6564. gstor->lock = false;
  6565. break;
  6566. default: // TABLE_INVENTORY
  6567. pc_delitem(sd, idx, delamount, 0, 0, LOG_TYPE_SCRIPT);
  6568. break;
  6569. }
  6570. }
  6571. amount[0]-= delamount;
  6572. }
  6573. /// Searches for item(s) and checks, if there is enough of them.
  6574. /// Used by delitem and delitem2
  6575. /// Relies on all input data being already fully valid.
  6576. /// @param exact_match will also match item attributes and cards, not just name id
  6577. /// @return true when all items could be deleted, false when there were not enough items to delete
  6578. static bool buildin_delitem_search(struct map_session_data* sd, struct item* it, bool exact_match, uint8 loc)
  6579. {
  6580. bool delete_items = false;
  6581. int i, amount, size;
  6582. struct item *items;
  6583. // prefer always non-equipped items
  6584. it->equip = 0;
  6585. // when searching for nameid only, prefer additionally
  6586. if( !exact_match )
  6587. {
  6588. // non-refined items
  6589. it->refine = 0;
  6590. // card-less items
  6591. memset(it->card, 0, sizeof(it->card));
  6592. }
  6593. switch(loc) {
  6594. case TABLE_CART:
  6595. size = MAX_CART;
  6596. items = sd->cart.u.items_cart;
  6597. break;
  6598. case TABLE_STORAGE:
  6599. size = MAX_STORAGE;
  6600. items = sd->storage.u.items_storage;
  6601. break;
  6602. case TABLE_GUILD_STORAGE:
  6603. {
  6604. struct s_storage *gstor = guild2storage2(sd->status.guild_id);
  6605. size = MAX_GUILD_STORAGE;
  6606. items = gstor->u.items_guild;
  6607. }
  6608. break;
  6609. default: // TABLE_INVENTORY
  6610. size = MAX_INVENTORY;
  6611. items = sd->inventory.u.items_inventory;
  6612. break;
  6613. }
  6614. for(;;)
  6615. {
  6616. unsigned short important = 0;
  6617. amount = it->amount;
  6618. // 1st pass -- less important items / exact match
  6619. for( i = 0; amount && i < size; i++ )
  6620. {
  6621. struct item *itm = NULL;
  6622. if( !&items[i] || !(itm = &items[i])->nameid || itm->nameid != it->nameid )
  6623. {// wrong/invalid item
  6624. continue;
  6625. }
  6626. if( itm->equip != it->equip || itm->refine != it->refine )
  6627. {// not matching attributes
  6628. important++;
  6629. continue;
  6630. }
  6631. if( exact_match )
  6632. {
  6633. if( itm->identify != it->identify || itm->attribute != it->attribute || memcmp(itm->card, it->card, sizeof(itm->card)) )
  6634. {// not matching exact attributes
  6635. continue;
  6636. }
  6637. }
  6638. else
  6639. {
  6640. if( itemdb_type(itm->nameid) == IT_PETEGG )
  6641. {
  6642. if( itm->card[0] == CARD0_PET && CheckForCharServer() )
  6643. {// pet which cannot be deleted
  6644. continue;
  6645. }
  6646. }
  6647. else if( memcmp(itm->card, it->card, sizeof(itm->card)) )
  6648. {// named/carded item
  6649. important++;
  6650. continue;
  6651. }
  6652. }
  6653. // count / delete item
  6654. buildin_delitem_delete(sd, i, &amount, loc, delete_items);
  6655. }
  6656. // 2nd pass -- any matching item
  6657. if( amount == 0 || important == 0 )
  6658. {// either everything was already consumed or no items were skipped
  6659. ;
  6660. }
  6661. else for( i = 0; amount && i < size; i++ )
  6662. {
  6663. struct item *itm = NULL;
  6664. if( !&items[i] || !(itm = &items[i])->nameid || itm->nameid != it->nameid )
  6665. {// wrong/invalid item
  6666. continue;
  6667. }
  6668. if( itemdb_type(itm->nameid) == IT_PETEGG && itm->card[0] == CARD0_PET && CheckForCharServer() )
  6669. {// pet which cannot be deleted
  6670. continue;
  6671. }
  6672. if( exact_match )
  6673. {
  6674. if( itm->refine != it->refine || itm->identify != it->identify || itm->attribute != it->attribute || memcmp(itm->card, it->card, sizeof(itm->card)) )
  6675. {// not matching attributes
  6676. continue;
  6677. }
  6678. }
  6679. // count / delete item
  6680. buildin_delitem_delete(sd, i, &amount, loc, delete_items);
  6681. }
  6682. if( amount )
  6683. {// not enough items
  6684. return false;
  6685. }
  6686. else if( delete_items )
  6687. {// we are done with the work
  6688. return true;
  6689. }
  6690. else
  6691. {// get rid of the items now
  6692. delete_items = true;
  6693. }
  6694. }
  6695. }
  6696. /// Deletes items from the target/attached player.
  6697. /// Prioritizes ordinary items.
  6698. ///
  6699. /// delitem <item id>,<amount>{,<account id>}
  6700. /// delitem "<item name>",<amount>{,<account id>}
  6701. /// cartdelitem <item id>,<amount>{,<account id>}
  6702. /// cartdelitem "<item name>",<amount>{,<account id>}
  6703. /// storagedelitem <item id>,<amount>{,<account id>}
  6704. /// storagedelitem "<item name>",<amount>{,<account id>}
  6705. /// guildstoragedelitem <item id>,<amount>{,<account id>}
  6706. /// guildstoragedelitem "<item name>",<amount>{,<account id>}
  6707. BUILDIN_FUNC(delitem)
  6708. {
  6709. TBL_PC *sd;
  6710. struct item it;
  6711. struct script_data *data;
  6712. uint8 loc = 0;
  6713. char* command = (char*)script_getfuncname(st);
  6714. if(!strncmp(command, "cart", 4))
  6715. loc = TABLE_CART;
  6716. else if(!strncmp(command, "storage", 7))
  6717. loc = TABLE_STORAGE;
  6718. else if(!strncmp(command, "guildstorage", 12))
  6719. loc = TABLE_GUILD_STORAGE;
  6720. if( script_hasdata(st,4) )
  6721. {
  6722. uint32 account_id = script_getnum(st,4);
  6723. sd = map_id2sd(account_id); // <account id>
  6724. if( sd == NULL )
  6725. {
  6726. ShowError("buildin_%s: player not found (AID=%d).\n", command, account_id);
  6727. st->state = END;
  6728. return SCRIPT_CMD_FAILURE;
  6729. }
  6730. }
  6731. else
  6732. {
  6733. sd = script_rid2sd(st);// attached player
  6734. if( sd == NULL )
  6735. return SCRIPT_CMD_SUCCESS;
  6736. }
  6737. if (loc == TABLE_CART && !pc_iscarton(sd)) {
  6738. ShowError("buildin_cartdelitem: player doesn't have cart (CID=%d).\n", sd->status.char_id);
  6739. script_pushint(st, -1);
  6740. return SCRIPT_CMD_FAILURE;
  6741. }
  6742. if (loc == TABLE_GUILD_STORAGE) {
  6743. struct s_storage *gstor = guild2storage2(sd->status.guild_id);
  6744. if (gstor == NULL || sd->state.storage_flag) {
  6745. script_pushint(st, -1);
  6746. return SCRIPT_CMD_FAILURE;
  6747. }
  6748. }
  6749. data = script_getdata(st,2);
  6750. get_val(st,data);
  6751. if( data_isstring(data) )
  6752. {
  6753. const char* item_name = conv_str(st,data);
  6754. struct item_data* id = itemdb_searchname(item_name);
  6755. if( id == NULL )
  6756. {
  6757. ShowError("buildin_%s: unknown item \"%s\".\n", command, item_name);
  6758. st->state = END;
  6759. return SCRIPT_CMD_FAILURE;
  6760. }
  6761. it.nameid = id->nameid;// "<item name>"
  6762. }
  6763. else
  6764. {
  6765. it.nameid = conv_num(st,data);// <item id>
  6766. if( !itemdb_exists( it.nameid ) )
  6767. {
  6768. ShowError("buildin_%s: unknown item \"%hu\".\n", command, it.nameid);
  6769. st->state = END;
  6770. return SCRIPT_CMD_FAILURE;
  6771. }
  6772. }
  6773. it.amount=script_getnum(st,3);
  6774. if( it.amount <= 0 )
  6775. return SCRIPT_CMD_SUCCESS;// nothing to do
  6776. if( buildin_delitem_search(sd, &it, false, loc) )
  6777. {// success
  6778. return SCRIPT_CMD_SUCCESS;
  6779. }
  6780. ShowError("buildin_%s: failed to delete %d items (AID=%d item_id=%hu).\n", command, it.amount, sd->status.account_id, it.nameid);
  6781. st->state = END;
  6782. st->mes_active = 0;
  6783. clif_scriptclose(sd, st->oid);
  6784. return SCRIPT_CMD_FAILURE;
  6785. }
  6786. /// Deletes items from the target/attached player.
  6787. ///
  6788. /// delitem2 <item id>,<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>{,<account ID>}
  6789. /// delitem2 "<Item name>",<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>{,<account ID>}
  6790. /// cartdelitem2 <item id>,<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>{,<account ID>}
  6791. /// cartdelitem2 "<Item name>",<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>{,<account ID>}
  6792. /// storagedelitem2 <item id>,<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>{,<account ID>}
  6793. /// storagedelitem2 "<Item name>",<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>{,<account ID>}
  6794. /// guildstoragedelitem2 <item id>,<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>{,<account ID>}
  6795. /// guildstoragedelitem2 "<Item name>",<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>{,<account ID>}
  6796. BUILDIN_FUNC(delitem2)
  6797. {
  6798. TBL_PC *sd;
  6799. struct item it;
  6800. struct script_data *data;
  6801. uint8 loc = 0;
  6802. char* command = (char*)script_getfuncname(st);
  6803. if(!strncmp(command, "cart", 4))
  6804. loc = TABLE_CART;
  6805. else if(!strncmp(command, "storage", 7))
  6806. loc = TABLE_STORAGE;
  6807. else if(!strncmp(command, "guildstorage", 12))
  6808. loc = TABLE_GUILD_STORAGE;
  6809. if( script_hasdata(st,11) )
  6810. {
  6811. uint32 account_id = script_getnum(st,11);
  6812. sd = map_id2sd(account_id); // <account id>
  6813. if( sd == NULL )
  6814. {
  6815. ShowError("buildin_%s: player not found (AID=%d).\n", command, account_id);
  6816. st->state = END;
  6817. return SCRIPT_CMD_FAILURE;
  6818. }
  6819. }
  6820. else
  6821. {
  6822. sd = script_rid2sd(st);// attached player
  6823. if( sd == NULL )
  6824. return SCRIPT_CMD_SUCCESS;
  6825. }
  6826. if (loc == TABLE_CART && !pc_iscarton(sd)) {
  6827. ShowError("buildin_cartdelitem: player doesn't have cart (CID=%d).\n", sd->status.char_id);
  6828. script_pushint(st,-1);
  6829. return SCRIPT_CMD_FAILURE;
  6830. }
  6831. if (loc == TABLE_GUILD_STORAGE) {
  6832. struct s_storage *gstor = guild2storage2(sd->status.guild_id);
  6833. if (gstor == NULL || sd->state.storage_flag) {
  6834. script_pushint(st, -1);
  6835. return SCRIPT_CMD_FAILURE;
  6836. }
  6837. }
  6838. data = script_getdata(st,2);
  6839. get_val(st,data);
  6840. if( data_isstring(data) )
  6841. {
  6842. const char* item_name = conv_str(st,data);
  6843. struct item_data* id = itemdb_searchname(item_name);
  6844. if( id == NULL )
  6845. {
  6846. ShowError("buildin_%s: unknown item \"%s\".\n", command, item_name);
  6847. st->state = END;
  6848. return SCRIPT_CMD_FAILURE;
  6849. }
  6850. it.nameid = id->nameid;// "<item name>"
  6851. }
  6852. else
  6853. {
  6854. it.nameid = conv_num(st,data);// <item id>
  6855. if( !itemdb_exists( it.nameid ) )
  6856. {
  6857. ShowError("buildin_%s: unknown item \"%hu\".\n", command, it.nameid);
  6858. st->state = END;
  6859. return SCRIPT_CMD_FAILURE;
  6860. }
  6861. }
  6862. it.amount=script_getnum(st,3);
  6863. it.identify=script_getnum(st,4);
  6864. it.refine=script_getnum(st,5);
  6865. it.attribute=script_getnum(st,6);
  6866. it.card[0]=(short)script_getnum(st,7);
  6867. it.card[1]=(short)script_getnum(st,8);
  6868. it.card[2]=(short)script_getnum(st,9);
  6869. it.card[3]=(short)script_getnum(st,10);
  6870. if( it.amount <= 0 )
  6871. return SCRIPT_CMD_SUCCESS;// nothing to do
  6872. if( buildin_delitem_search(sd, &it, true, loc) )
  6873. {// success
  6874. return SCRIPT_CMD_SUCCESS;
  6875. }
  6876. ShowError("buildin_%s: failed to delete %d items (AID=%d item_id=%hu).\n", command, it.amount, sd->status.account_id, it.nameid);
  6877. st->state = END;
  6878. st->mes_active = 0;
  6879. clif_scriptclose(sd, st->oid);
  6880. return SCRIPT_CMD_FAILURE;
  6881. }
  6882. /*==========================================
  6883. * Enables/Disables use of items while in an NPC [Skotlex]
  6884. *------------------------------------------*/
  6885. BUILDIN_FUNC(enableitemuse)
  6886. {
  6887. TBL_PC *sd;
  6888. sd=script_rid2sd(st);
  6889. if (sd)
  6890. st->npc_item_flag = sd->npc_item_flag = 1;
  6891. return SCRIPT_CMD_SUCCESS;
  6892. }
  6893. BUILDIN_FUNC(disableitemuse)
  6894. {
  6895. TBL_PC *sd;
  6896. sd=script_rid2sd(st);
  6897. if (sd)
  6898. st->npc_item_flag = sd->npc_item_flag = 0;
  6899. return SCRIPT_CMD_SUCCESS;
  6900. }
  6901. /*==========================================
  6902. * Returns a character's specified stat.
  6903. * Check pc_readparam for available options.
  6904. * readparam <param>{,"<nick>"}
  6905. * readparam <param>{,<char_id>}
  6906. *------------------------------------------*/
  6907. BUILDIN_FUNC(readparam)
  6908. {
  6909. int value;
  6910. struct script_data *data = script_getdata(st, 2);
  6911. TBL_PC *sd;
  6912. if( script_hasdata(st, 3) ){
  6913. if( script_isint(st, 3) ){
  6914. script_charid2sd(3, sd);
  6915. }else{
  6916. script_nick2sd(3, sd);
  6917. }
  6918. }else{
  6919. sd = script_rid2sd(st);
  6920. }
  6921. if( !sd ){
  6922. script_pushint(st, -1);
  6923. return SCRIPT_CMD_FAILURE;
  6924. }
  6925. // If you use a parameter, return the value behind it
  6926. if( reference_toparam(data) ){
  6927. get_val_(st, data, sd);
  6928. value = (int)data->u.num;
  6929. }else{
  6930. value = pc_readparam(sd,script_getnum(st, 2));
  6931. }
  6932. script_pushint(st,value);
  6933. return SCRIPT_CMD_SUCCESS;
  6934. }
  6935. /*==========================================
  6936. * Return charid identification
  6937. * return by @num :
  6938. * 0 : char_id
  6939. * 1 : party_id
  6940. * 2 : guild_id
  6941. * 3 : account_id
  6942. * 4 : bg_id
  6943. * 5 : clan_id
  6944. *------------------------------------------*/
  6945. BUILDIN_FUNC(getcharid)
  6946. {
  6947. int num;
  6948. TBL_PC *sd;
  6949. num = script_getnum(st,2);
  6950. if( script_hasdata(st,3) )
  6951. sd=map_nick2sd(script_getstr(st,3));
  6952. else
  6953. sd=script_rid2sd(st);
  6954. if(sd==NULL){
  6955. script_pushint(st,0); //return 0, according docs
  6956. return SCRIPT_CMD_SUCCESS;
  6957. }
  6958. switch( num ) {
  6959. case 0: script_pushint(st,sd->status.char_id); break;
  6960. case 1: script_pushint(st,sd->status.party_id); break;
  6961. case 2: script_pushint(st,sd->status.guild_id); break;
  6962. case 3: script_pushint(st,sd->status.account_id); break;
  6963. case 4: script_pushint(st,sd->bg_id); break;
  6964. case 5: script_pushint(st,sd->status.clan_id); break;
  6965. default:
  6966. ShowError("buildin_getcharid: invalid parameter (%d).\n", num);
  6967. script_pushint(st,0);
  6968. break;
  6969. }
  6970. return SCRIPT_CMD_SUCCESS;
  6971. }
  6972. /*==========================================
  6973. * returns the GID of an NPC
  6974. *------------------------------------------*/
  6975. BUILDIN_FUNC(getnpcid)
  6976. {
  6977. int num = script_getnum(st,2);
  6978. struct npc_data* nd = NULL;
  6979. if( script_hasdata(st,3) )
  6980. {// unique npc name
  6981. if( ( nd = npc_name2id(script_getstr(st,3)) ) == NULL )
  6982. {
  6983. ShowError("buildin_getnpcid: No such NPC '%s'.\n", script_getstr(st,3));
  6984. script_pushint(st,0);
  6985. return SCRIPT_CMD_FAILURE;
  6986. }
  6987. }
  6988. switch (num) {
  6989. case 0:
  6990. script_pushint(st,nd ? nd->bl.id : st->oid);
  6991. break;
  6992. default:
  6993. ShowError("buildin_getnpcid: invalid parameter (%d).\n", num);
  6994. script_pushint(st,0);
  6995. return SCRIPT_CMD_FAILURE;
  6996. }
  6997. return SCRIPT_CMD_SUCCESS;
  6998. }
  6999. /*==========================================
  7000. * Return the name of the party_id
  7001. * null if not found
  7002. *------------------------------------------*/
  7003. BUILDIN_FUNC(getpartyname)
  7004. {
  7005. int party_id;
  7006. struct party_data* p;
  7007. party_id = script_getnum(st,2);
  7008. if( ( p = party_search(party_id) ) != NULL )
  7009. {
  7010. script_pushstrcopy(st,p->party.name);
  7011. }
  7012. else
  7013. {
  7014. script_pushconststr(st,"null");
  7015. }
  7016. return SCRIPT_CMD_SUCCESS;
  7017. }
  7018. /*==========================================
  7019. * Get the information of the members of a party by type
  7020. * @party_id, @type
  7021. * return by @type :
  7022. * - : nom des membres
  7023. * 1 : char_id des membres
  7024. * 2 : account_id des membres
  7025. *------------------------------------------*/
  7026. BUILDIN_FUNC(getpartymember)
  7027. {
  7028. struct party_data *p;
  7029. uint8 j = 0;
  7030. p = party_search(script_getnum(st,2));
  7031. if (p != NULL) {
  7032. uint8 i, type = 0;
  7033. struct script_data *data = NULL;
  7034. char *varname = NULL;
  7035. if (script_hasdata(st,3))
  7036. type = script_getnum(st,3);
  7037. if (script_hasdata(st,4)) {
  7038. data = script_getdata(st, 4);
  7039. if (!data_isreference(data)) {
  7040. ShowError("buildin_getpartymember: Error in argument! Please give a variable to store values in.\n");
  7041. return SCRIPT_CMD_FAILURE;
  7042. }
  7043. varname = reference_getname(data);
  7044. if (type <= 0 && varname[strlen(varname)-1] != '$') {
  7045. ShowError("buildin_getpartymember: The array %s is not string type.\n", varname);
  7046. return SCRIPT_CMD_FAILURE;
  7047. }
  7048. }
  7049. for (i = 0; i < MAX_PARTY; i++) {
  7050. if (p->party.member[i].account_id) {
  7051. switch (type) {
  7052. case 2:
  7053. if (data)
  7054. setd_sub(st, NULL, varname, j, (void *)__64BPRTSIZE(p->party.member[i].account_id), data->ref);
  7055. else
  7056. mapreg_setreg(reference_uid(add_str("$@partymemberaid"), j),p->party.member[i].account_id);
  7057. break;
  7058. case 1:
  7059. if (data)
  7060. setd_sub(st, NULL, varname, j, (void *)__64BPRTSIZE(p->party.member[i].char_id), data->ref);
  7061. else
  7062. mapreg_setreg(reference_uid(add_str("$@partymembercid"), j),p->party.member[i].char_id);
  7063. break;
  7064. default:
  7065. if (data)
  7066. setd_sub(st, NULL, varname, j, (void *)__64BPRTSIZE(p->party.member[i].name), data->ref);
  7067. else
  7068. mapreg_setregstr(reference_uid(add_str("$@partymembername$"), j),p->party.member[i].name);
  7069. break;
  7070. }
  7071. j++;
  7072. }
  7073. }
  7074. }
  7075. mapreg_setreg(add_str("$@partymembercount"),j);
  7076. script_pushint(st, j);
  7077. return SCRIPT_CMD_SUCCESS;
  7078. }
  7079. /*==========================================
  7080. * Retrieves party leader. if flag is specified,
  7081. * return some of the leader data. Otherwise, return name.
  7082. *------------------------------------------*/
  7083. BUILDIN_FUNC(getpartyleader)
  7084. {
  7085. int party_id, type = 0, i=0;
  7086. struct party_data *p;
  7087. party_id=script_getnum(st,2);
  7088. if( script_hasdata(st,3) )
  7089. type=script_getnum(st,3);
  7090. p=party_search(party_id);
  7091. if (p) //Search leader
  7092. for(i = 0; i < MAX_PARTY && !p->party.member[i].leader; i++);
  7093. if (!p || i == MAX_PARTY) { //leader not found
  7094. if (type)
  7095. script_pushint(st,-1);
  7096. else
  7097. script_pushconststr(st,"null");
  7098. return SCRIPT_CMD_SUCCESS;
  7099. }
  7100. switch (type) {
  7101. case 1: script_pushint(st,p->party.member[i].account_id); break;
  7102. case 2: script_pushint(st,p->party.member[i].char_id); break;
  7103. case 3: script_pushint(st,p->party.member[i].class_); break;
  7104. case 4: script_pushstrcopy(st,mapindex_id2name(p->party.member[i].map)); break;
  7105. case 5: script_pushint(st,p->party.member[i].lv); break;
  7106. default: script_pushstrcopy(st,p->party.member[i].name); break;
  7107. }
  7108. return SCRIPT_CMD_SUCCESS;
  7109. }
  7110. /*==========================================
  7111. * Return the name of the @guild_id
  7112. * null if not found
  7113. *------------------------------------------*/
  7114. BUILDIN_FUNC(getguildname)
  7115. {
  7116. int guild_id;
  7117. struct guild* g;
  7118. guild_id = script_getnum(st,2);
  7119. if( ( g = guild_search(guild_id) ) != NULL )
  7120. script_pushstrcopy(st,g->name);
  7121. else
  7122. script_pushconststr(st,"null");
  7123. return SCRIPT_CMD_SUCCESS;
  7124. }
  7125. /*==========================================
  7126. * Return the name of the guild master of @guild_id
  7127. * null if not found
  7128. *------------------------------------------*/
  7129. BUILDIN_FUNC(getguildmaster)
  7130. {
  7131. int guild_id;
  7132. struct guild* g;
  7133. guild_id = script_getnum(st,2);
  7134. if( ( g = guild_search(guild_id) ) != NULL )
  7135. script_pushstrcopy(st,g->member[0].name);
  7136. else
  7137. script_pushconststr(st,"null");
  7138. return SCRIPT_CMD_SUCCESS;
  7139. }
  7140. BUILDIN_FUNC(getguildmasterid)
  7141. {
  7142. int guild_id;
  7143. struct guild* g;
  7144. guild_id = script_getnum(st,2);
  7145. if( ( g = guild_search(guild_id) ) != NULL )
  7146. script_pushint(st,g->member[0].char_id);
  7147. else
  7148. script_pushint(st,0);
  7149. return SCRIPT_CMD_SUCCESS;
  7150. }
  7151. /*==========================================
  7152. * Get char string information by type :
  7153. * Return by @type :
  7154. * 0 : char_name
  7155. * 1 : party_name or ""
  7156. * 2 : guild_name or ""
  7157. * 3 : map_name
  7158. * - : ""
  7159. * strcharinfo(<type>{,<char_id>})
  7160. *------------------------------------------*/
  7161. BUILDIN_FUNC(strcharinfo)
  7162. {
  7163. TBL_PC *sd;
  7164. int num;
  7165. struct guild* g;
  7166. struct party_data* p;
  7167. if (!script_charid2sd(3,sd)) {
  7168. script_pushconststr(st,"");
  7169. return SCRIPT_CMD_FAILURE;
  7170. }
  7171. num=script_getnum(st,2);
  7172. switch(num){
  7173. case 0:
  7174. script_pushstrcopy(st,sd->status.name);
  7175. break;
  7176. case 1:
  7177. if( ( p = party_search(sd->status.party_id) ) != NULL ) {
  7178. script_pushstrcopy(st,p->party.name);
  7179. } else {
  7180. script_pushconststr(st,"");
  7181. }
  7182. break;
  7183. case 2:
  7184. if( ( g = sd->guild ) != NULL ) {
  7185. script_pushstrcopy(st,g->name);
  7186. } else {
  7187. script_pushconststr(st,"");
  7188. }
  7189. break;
  7190. case 3:
  7191. script_pushconststr(st,map[sd->bl.m].name);
  7192. break;
  7193. default:
  7194. ShowWarning("buildin_strcharinfo: unknown parameter.\n");
  7195. script_pushconststr(st,"");
  7196. break;
  7197. }
  7198. return SCRIPT_CMD_SUCCESS;
  7199. }
  7200. /*==========================================
  7201. * Get npc string information by type
  7202. * return by @type:
  7203. * 0 : name
  7204. * 1 : str#
  7205. * 2 : #str
  7206. * 3 : ::str
  7207. * 4 : map name
  7208. *------------------------------------------*/
  7209. BUILDIN_FUNC(strnpcinfo)
  7210. {
  7211. TBL_NPC* nd;
  7212. int num;
  7213. char *buf,*name=NULL;
  7214. nd = map_id2nd(st->oid);
  7215. if (!nd) {
  7216. script_pushconststr(st, "");
  7217. return SCRIPT_CMD_SUCCESS;
  7218. }
  7219. num = script_getnum(st,2);
  7220. switch(num){
  7221. case 0: // display name
  7222. name = aStrdup(nd->name);
  7223. break;
  7224. case 1: // visible part of display name
  7225. if((buf = strchr(nd->name,'#')) != NULL)
  7226. {
  7227. name = aStrdup(nd->name);
  7228. name[buf - nd->name] = 0;
  7229. } else // Return the name, there is no '#' present
  7230. name = aStrdup(nd->name);
  7231. break;
  7232. case 2: // # fragment
  7233. if((buf = strchr(nd->name,'#')) != NULL)
  7234. name = aStrdup(buf+1);
  7235. break;
  7236. case 3: // unique name
  7237. name = aStrdup(nd->exname);
  7238. break;
  7239. case 4: // map name
  7240. name = aStrdup(map[nd->bl.m].name);
  7241. break;
  7242. }
  7243. if(name)
  7244. script_pushstr(st, name);
  7245. else
  7246. script_pushconststr(st, "");
  7247. return SCRIPT_CMD_SUCCESS;
  7248. }
  7249. /**
  7250. * getequipid(<equipment slot>{,<char_id>})
  7251. **/
  7252. BUILDIN_FUNC(getequipid)
  7253. {
  7254. int i, num;
  7255. TBL_PC* sd;
  7256. struct item_data* item;
  7257. if (!script_charid2sd(3, sd)) {
  7258. script_pushint(st,-1);
  7259. return SCRIPT_CMD_FAILURE;
  7260. }
  7261. num = script_getnum(st,2);
  7262. if( !equip_index_check(num) )
  7263. {
  7264. script_pushint(st,-1);
  7265. return SCRIPT_CMD_FAILURE;
  7266. }
  7267. // get inventory position of item
  7268. i = pc_checkequip(sd,equip_bitmask[num]);
  7269. if( i < 0 )
  7270. {
  7271. script_pushint(st,-1);
  7272. return SCRIPT_CMD_SUCCESS;
  7273. }
  7274. item = sd->inventory_data[i];
  7275. if( item != 0 )
  7276. script_pushint(st,item->nameid);
  7277. else
  7278. script_pushint(st,0);
  7279. return SCRIPT_CMD_SUCCESS;
  7280. }
  7281. /*==========================================
  7282. * getequipuniqueid(<equipment slot>{,<char_id>})
  7283. *------------------------------------------*/
  7284. BUILDIN_FUNC(getequipuniqueid)
  7285. {
  7286. int i, num;
  7287. TBL_PC* sd;
  7288. struct item* item;
  7289. if (!script_charid2sd(3, sd)) {
  7290. script_pushconststr(st, "");
  7291. return SCRIPT_CMD_FAILURE;
  7292. }
  7293. num = script_getnum(st,2);
  7294. if ( !equip_index_check(num) ) {
  7295. script_pushconststr(st, "");
  7296. return SCRIPT_CMD_FAILURE;
  7297. }
  7298. // get inventory position of item
  7299. i = pc_checkequip(sd,equip_bitmask[num]);
  7300. if (i < 0) {
  7301. script_pushconststr(st, "");
  7302. return SCRIPT_CMD_FAILURE;
  7303. }
  7304. item = &sd->inventory.u.items_inventory[i];
  7305. if (item != 0) {
  7306. int maxlen = 256;
  7307. char *buf = (char *)aMalloc(maxlen*sizeof(char));
  7308. memset(buf, 0, maxlen);
  7309. snprintf(buf, maxlen-1, "%llu", (unsigned long long)item->unique_id);
  7310. script_pushstr(st, buf);
  7311. } else
  7312. script_pushconststr(st, "");
  7313. return SCRIPT_CMD_SUCCESS;
  7314. }
  7315. /*==========================================
  7316. * Get the equipement name at pos
  7317. * return item jname or ""
  7318. * getequipname(<equipment slot>{,<char_id>})
  7319. *------------------------------------------*/
  7320. BUILDIN_FUNC(getequipname)
  7321. {
  7322. int i, num;
  7323. TBL_PC* sd;
  7324. struct item_data* item;
  7325. if (!script_charid2sd(3, sd)) {
  7326. script_pushconststr(st, "");
  7327. return SCRIPT_CMD_FAILURE;
  7328. }
  7329. num = script_getnum(st,2);
  7330. if( !equip_index_check(num) )
  7331. {
  7332. script_pushconststr(st,"");
  7333. return SCRIPT_CMD_SUCCESS;
  7334. }
  7335. // get inventory position of item
  7336. i = pc_checkequip(sd,equip_bitmask[num]);
  7337. if( i < 0 )
  7338. {
  7339. script_pushconststr(st,"");
  7340. return SCRIPT_CMD_SUCCESS;
  7341. }
  7342. item = sd->inventory_data[i];
  7343. if( item != 0 )
  7344. script_pushstrcopy(st,item->jname);
  7345. else
  7346. script_pushconststr(st,"");
  7347. return SCRIPT_CMD_SUCCESS;
  7348. }
  7349. /*==========================================
  7350. * getbrokenid [Valaris]
  7351. * getbrokenid(<number>{,<char_id>})
  7352. *------------------------------------------*/
  7353. BUILDIN_FUNC(getbrokenid)
  7354. {
  7355. int i,num,id = 0,brokencounter = 0;
  7356. TBL_PC *sd;
  7357. if (!script_charid2sd(3, sd)) {
  7358. script_pushint(st,0);
  7359. return SCRIPT_CMD_FAILURE;
  7360. }
  7361. num = script_getnum(st,2);
  7362. for(i = 0; i < MAX_INVENTORY; i++) {
  7363. if(sd->inventory.u.items_inventory[i].attribute) {
  7364. brokencounter++;
  7365. if(num == brokencounter){
  7366. id = sd->inventory.u.items_inventory[i].nameid;
  7367. break;
  7368. }
  7369. }
  7370. }
  7371. script_pushint(st,id);
  7372. return SCRIPT_CMD_SUCCESS;
  7373. }
  7374. /*==========================================
  7375. * repair [Valaris]
  7376. * repair <num>{,<char_id>};
  7377. *------------------------------------------*/
  7378. BUILDIN_FUNC(repair)
  7379. {
  7380. int i,num;
  7381. int repaircounter = 0;
  7382. TBL_PC *sd;
  7383. if (!script_charid2sd(3,sd))
  7384. return SCRIPT_CMD_FAILURE;
  7385. num = script_getnum(st,2);
  7386. for(i = 0; i < MAX_INVENTORY; i++) {
  7387. if(sd->inventory.u.items_inventory[i].attribute) {
  7388. repaircounter++;
  7389. if(num == repaircounter) {
  7390. sd->inventory.u.items_inventory[i].attribute = 0;
  7391. clif_equiplist(sd);
  7392. clif_produceeffect(sd, 0, sd->inventory.u.items_inventory[i].nameid);
  7393. clif_misceffect(&sd->bl, 3);
  7394. break;
  7395. }
  7396. }
  7397. }
  7398. return SCRIPT_CMD_SUCCESS;
  7399. }
  7400. /*==========================================
  7401. * repairall {<char_id>}
  7402. *------------------------------------------*/
  7403. BUILDIN_FUNC(repairall)
  7404. {
  7405. int i, repaircounter = 0;
  7406. TBL_PC *sd;
  7407. if (!script_charid2sd(2,sd))
  7408. return SCRIPT_CMD_FAILURE;
  7409. for(i = 0; i < MAX_INVENTORY; i++)
  7410. {
  7411. if(sd->inventory.u.items_inventory[i].nameid && sd->inventory.u.items_inventory[i].attribute)
  7412. {
  7413. sd->inventory.u.items_inventory[i].attribute = 0;
  7414. clif_produceeffect(sd,0,sd->inventory.u.items_inventory[i].nameid);
  7415. repaircounter++;
  7416. }
  7417. }
  7418. if(repaircounter)
  7419. {
  7420. clif_misceffect(&sd->bl, 3);
  7421. clif_equiplist(sd);
  7422. }
  7423. return SCRIPT_CMD_SUCCESS;
  7424. }
  7425. /*==========================================
  7426. * Chk if player have something equiped at pos
  7427. * getequipisequiped <pos>{,<char_id>}
  7428. *------------------------------------------*/
  7429. BUILDIN_FUNC(getequipisequiped)
  7430. {
  7431. int i = -1,num;
  7432. TBL_PC *sd;
  7433. num = script_getnum(st,2);
  7434. if (!script_charid2sd(3,sd))
  7435. return SCRIPT_CMD_FAILURE;
  7436. if ( equip_index_check(num) )
  7437. i=pc_checkequip(sd,equip_bitmask[num]);
  7438. if(i >= 0)
  7439. script_pushint(st,1);
  7440. else
  7441. script_pushint(st,0);
  7442. return SCRIPT_CMD_SUCCESS;
  7443. }
  7444. /*==========================================
  7445. * Chk if the player have something equiped at pos
  7446. * if so chk if this item ain't marked not refinable or rental
  7447. * return (npc)
  7448. * 1 : true
  7449. * 0 : false
  7450. * getequipisenableref(<equipment slot>{,<char_id>})
  7451. *------------------------------------------*/
  7452. BUILDIN_FUNC(getequipisenableref)
  7453. {
  7454. int i = -1,num;
  7455. TBL_PC *sd;
  7456. num = script_getnum(st,2);
  7457. if (!script_charid2sd(3, sd)) {
  7458. script_pushint(st,0);
  7459. return SCRIPT_CMD_FAILURE;
  7460. }
  7461. if( equip_index_check(num) )
  7462. i = pc_checkequip(sd,equip_bitmask[num]);
  7463. if( i >= 0 && sd->inventory_data[i] && !sd->inventory_data[i]->flag.no_refine && !sd->inventory.u.items_inventory[i].expire_time )
  7464. script_pushint(st,1);
  7465. else
  7466. script_pushint(st,0);
  7467. return SCRIPT_CMD_SUCCESS;
  7468. }
  7469. /*==========================================
  7470. * Get the item refined value at pos
  7471. * return (npc)
  7472. * x : refine amount
  7473. * 0 : false (not refined)
  7474. * getequiprefinerycnt(<equipment slot>{,<char_id>})
  7475. *------------------------------------------*/
  7476. BUILDIN_FUNC(getequiprefinerycnt)
  7477. {
  7478. int i = -1,num;
  7479. TBL_PC *sd;
  7480. num = script_getnum(st,2);
  7481. if (!script_charid2sd(3, sd)) {
  7482. script_pushint(st,0);
  7483. return SCRIPT_CMD_FAILURE;
  7484. }
  7485. if (equip_index_check(num))
  7486. i=pc_checkequip(sd,equip_bitmask[num]);
  7487. if(i >= 0)
  7488. script_pushint(st,sd->inventory.u.items_inventory[i].refine);
  7489. else
  7490. script_pushint(st,0);
  7491. return SCRIPT_CMD_SUCCESS;
  7492. }
  7493. /*==========================================
  7494. * Get the weapon level value at pos
  7495. * (pos should normally only be EQI_HAND_L or EQI_HAND_R)
  7496. * return (npc)
  7497. * x : weapon level
  7498. * 0 : false
  7499. * getequipweaponlv(<equipment slot>{,<char_id>})
  7500. *------------------------------------------*/
  7501. BUILDIN_FUNC(getequipweaponlv)
  7502. {
  7503. int i = -1, num;
  7504. TBL_PC *sd;
  7505. num = script_getnum(st, 2);
  7506. if (!script_charid2sd(3, sd)) {
  7507. script_pushint(st, 0);
  7508. return SCRIPT_CMD_FAILURE;
  7509. }
  7510. if (num == -1)
  7511. i = current_equip_item_index;
  7512. else if (equip_index_check(num))
  7513. i = pc_checkequip(sd, equip_bitmask[num]);
  7514. if (i >= 0 && sd->inventory_data[i])
  7515. script_pushint(st, sd->inventory_data[i]->wlv);
  7516. else
  7517. script_pushint(st, 0);
  7518. return SCRIPT_CMD_SUCCESS;
  7519. }
  7520. /*==========================================
  7521. * Get the item refine chance (from refine.txt) for item at pos
  7522. * return (npc)
  7523. * x : refine chance
  7524. * 0 : false (max refine level or unequip..)
  7525. * getequippercentrefinery(<equipment slot>{,<char_id>})
  7526. *------------------------------------------*/
  7527. BUILDIN_FUNC(getequippercentrefinery)
  7528. {
  7529. int i = -1,num;
  7530. TBL_PC *sd;
  7531. num = script_getnum(st,2);
  7532. if (!script_charid2sd(3, sd)) {
  7533. script_pushint(st,0);
  7534. return SCRIPT_CMD_FAILURE;
  7535. }
  7536. if (equip_index_check(num))
  7537. i = pc_checkequip(sd,equip_bitmask[num]);
  7538. if(i >= 0 && sd->inventory.u.items_inventory[i].nameid && sd->inventory.u.items_inventory[i].refine < MAX_REFINE)
  7539. script_pushint(st,status_get_refine_chance((enum refine_type)itemdb_wlv(sd->inventory.u.items_inventory[i].nameid), (int)sd->inventory.u.items_inventory[i].refine));
  7540. else
  7541. script_pushint(st,0);
  7542. return SCRIPT_CMD_SUCCESS;
  7543. }
  7544. /*==========================================
  7545. * Refine +1 item at pos and log and display refine
  7546. * successrefitem <equipment slot>{,<count>{,<char_id>}}
  7547. *------------------------------------------*/
  7548. BUILDIN_FUNC(successrefitem) {
  7549. short i = -1, up = 1;
  7550. int pos;
  7551. TBL_PC *sd;
  7552. pos = script_getnum(st,2);
  7553. if (!script_charid2sd(4, sd)) {
  7554. script_pushint(st, -1);
  7555. return SCRIPT_CMD_FAILURE;
  7556. }
  7557. if (script_hasdata(st, 3))
  7558. up = script_getnum(st, 3);
  7559. if (equip_index_check(pos))
  7560. i = pc_checkequip(sd,equip_bitmask[pos]);
  7561. if (i >= 0) {
  7562. unsigned int ep = sd->inventory.u.items_inventory[i].equip;
  7563. //Logs items, got from (N)PC scripts [Lupus]
  7564. log_pick_pc(sd, LOG_TYPE_SCRIPT, -1, &sd->inventory.u.items_inventory[i]);
  7565. if (sd->inventory.u.items_inventory[i].refine >= MAX_REFINE) {
  7566. script_pushint(st, MAX_REFINE);
  7567. return SCRIPT_CMD_SUCCESS;
  7568. }
  7569. sd->inventory.u.items_inventory[i].refine += up;
  7570. sd->inventory.u.items_inventory[i].refine = cap_value( sd->inventory.u.items_inventory[i].refine, 0, MAX_REFINE);
  7571. pc_unequipitem(sd,i,2); // status calc will happen in pc_equipitem() below
  7572. clif_refine(sd->fd,0,i,sd->inventory.u.items_inventory[i].refine);
  7573. clif_delitem(sd,i,1,3);
  7574. //Logs items, got from (N)PC scripts [Lupus]
  7575. log_pick_pc(sd, LOG_TYPE_SCRIPT, 1, &sd->inventory.u.items_inventory[i]);
  7576. clif_additem(sd,i,1,0);
  7577. pc_equipitem(sd,i,ep);
  7578. clif_misceffect(&sd->bl,3);
  7579. if (sd->inventory.u.items_inventory[i].refine == MAX_REFINE &&
  7580. sd->inventory.u.items_inventory[i].card[0] == CARD0_FORGE &&
  7581. sd->status.char_id == (int)MakeDWord(sd->inventory.u.items_inventory[i].card[2],sd->inventory.u.items_inventory[i].card[3]))
  7582. { // Fame point system [DracoRPG]
  7583. switch (sd->inventory_data[i]->wlv){
  7584. case 1:
  7585. pc_addfame(sd, battle_config.fame_refine_lv1); // Success to refine to +10 a lv1 weapon you forged = +1 fame point
  7586. break;
  7587. case 2:
  7588. pc_addfame(sd, battle_config.fame_refine_lv2); // Success to refine to +10 a lv2 weapon you forged = +25 fame point
  7589. break;
  7590. case 3:
  7591. pc_addfame(sd, battle_config.fame_refine_lv3); // Success to refine to +10 a lv3 weapon you forged = +1000 fame point
  7592. break;
  7593. }
  7594. }
  7595. script_pushint(st, sd->inventory.u.items_inventory[i].refine);
  7596. return SCRIPT_CMD_SUCCESS;
  7597. }
  7598. ShowError("buildin_successrefitem: No item equipped at pos %d (CID=%d/AID=%d).\n", pos, sd->status.char_id, sd->status.account_id);
  7599. script_pushint(st, -1);
  7600. return SCRIPT_CMD_FAILURE;
  7601. }
  7602. /*==========================================
  7603. * Show a failed Refine +1 attempt
  7604. * failedrefitem <equipment slot>{,<char_id>}
  7605. *------------------------------------------*/
  7606. BUILDIN_FUNC(failedrefitem) {
  7607. short i = -1;
  7608. int pos;
  7609. TBL_PC *sd;
  7610. pos = script_getnum(st,2);
  7611. if (!script_charid2sd(3, sd)) {
  7612. script_pushint(st, 0);
  7613. return SCRIPT_CMD_FAILURE;
  7614. }
  7615. if (equip_index_check(pos))
  7616. i = pc_checkequip(sd,equip_bitmask[pos]);
  7617. if (i >= 0) {
  7618. sd->inventory.u.items_inventory[i].refine = 0;
  7619. pc_unequipitem(sd,i,3); //recalculate bonus
  7620. clif_refine(sd->fd,1,i,sd->inventory.u.items_inventory[i].refine); //notify client of failure
  7621. pc_delitem(sd,i,1,0,2,LOG_TYPE_SCRIPT);
  7622. clif_misceffect(&sd->bl,2); // display failure effect
  7623. script_pushint(st, 1);
  7624. return SCRIPT_CMD_SUCCESS;
  7625. }
  7626. ShowError("buildin_failedrefitem: No item equipped at pos %d (CID=%d/AID=%d).\n", pos, sd->status.char_id, sd->status.account_id);
  7627. script_pushint(st, 0);
  7628. return SCRIPT_CMD_FAILURE;
  7629. }
  7630. /*==========================================
  7631. * Downgrades an Equipment Part by -1 . [Masao]
  7632. * downrefitem <equipment slot>{,<count>{,<char_id>}}
  7633. *------------------------------------------*/
  7634. BUILDIN_FUNC(downrefitem) {
  7635. short i = -1, down = 1;
  7636. int pos;
  7637. TBL_PC *sd;
  7638. if (!script_charid2sd(4, sd)) {
  7639. script_pushint(st, -1);
  7640. return SCRIPT_CMD_FAILURE;
  7641. }
  7642. pos = script_getnum(st,2);
  7643. if (script_hasdata(st, 3))
  7644. down = script_getnum(st, 3);
  7645. if (equip_index_check(pos))
  7646. i = pc_checkequip(sd,equip_bitmask[pos]);
  7647. if (i >= 0) {
  7648. unsigned int ep = sd->inventory.u.items_inventory[i].equip;
  7649. //Logs items, got from (N)PC scripts [Lupus]
  7650. log_pick_pc(sd, LOG_TYPE_SCRIPT, -1, &sd->inventory.u.items_inventory[i]);
  7651. pc_unequipitem(sd,i,2); // status calc will happen in pc_equipitem() below
  7652. sd->inventory.u.items_inventory[i].refine -= down;
  7653. sd->inventory.u.items_inventory[i].refine = cap_value( sd->inventory.u.items_inventory[i].refine, 0, MAX_REFINE);
  7654. clif_refine(sd->fd,2,i,sd->inventory.u.items_inventory[i].refine);
  7655. clif_delitem(sd,i,1,3);
  7656. //Logs items, got from (N)PC scripts [Lupus]
  7657. log_pick_pc(sd, LOG_TYPE_SCRIPT, 1, &sd->inventory.u.items_inventory[i]);
  7658. clif_additem(sd,i,1,0);
  7659. pc_equipitem(sd,i,ep);
  7660. clif_misceffect(&sd->bl,2);
  7661. script_pushint(st, sd->inventory.u.items_inventory[i].refine);
  7662. return SCRIPT_CMD_SUCCESS;
  7663. }
  7664. ShowError("buildin_downrefitem: No item equipped at pos %d (CID=%d/AID=%d).\n", pos, sd->status.char_id, sd->status.account_id);
  7665. script_pushint(st, -1);
  7666. return SCRIPT_CMD_FAILURE;
  7667. }
  7668. /**
  7669. * Delete the item equipped at pos.
  7670. * delequip <equipment slot>{,<char_id>};
  7671. **/
  7672. BUILDIN_FUNC(delequip) {
  7673. short i = -1;
  7674. int pos;
  7675. int8 ret;
  7676. TBL_PC *sd;
  7677. pos = script_getnum(st,2);
  7678. if (!script_charid2sd(3,sd)) {
  7679. st->state = END;
  7680. return SCRIPT_CMD_FAILURE;
  7681. }
  7682. if (equip_index_check(pos))
  7683. i = pc_checkequip(sd,equip_bitmask[pos]);
  7684. if (i >= 0) {
  7685. pc_unequipitem(sd,i,3); //recalculate bonus
  7686. ret = !(pc_delitem(sd,i,1,0,2,LOG_TYPE_SCRIPT));
  7687. }
  7688. else {
  7689. ShowError("buildin_delequip: No item equipped at pos %d (CID=%d/AID=%d).\n", pos, sd->status.char_id, sd->status.account_id);
  7690. st->state = END;
  7691. return SCRIPT_CMD_FAILURE;
  7692. }
  7693. script_pushint(st,ret);
  7694. return SCRIPT_CMD_SUCCESS;
  7695. }
  7696. /**
  7697. * Break the item equipped at pos.
  7698. * breakequip <equipment slot>{,<char_id>};
  7699. **/
  7700. BUILDIN_FUNC(breakequip) {
  7701. short i = -1;
  7702. int pos;
  7703. TBL_PC *sd;
  7704. pos = script_getnum(st,2);
  7705. if (!script_charid2sd(3,sd))
  7706. return SCRIPT_CMD_FAILURE;
  7707. if (equip_index_check(pos))
  7708. i = pc_checkequip(sd,equip_bitmask[pos]);
  7709. if (i >= 0) {
  7710. sd->inventory.u.items_inventory[i].attribute = 1;
  7711. pc_unequipitem(sd,i,3);
  7712. clif_equiplist(sd);
  7713. script_pushint(st,1);
  7714. return SCRIPT_CMD_SUCCESS;
  7715. }
  7716. ShowError("buildin_breakequip: No item equipped at pos %d (CID=%d/AID=%d).\n", pos, sd->status.char_id, sd->status.account_id);
  7717. script_pushint(st,0);
  7718. return SCRIPT_CMD_FAILURE;
  7719. }
  7720. /**
  7721. * statusup <stat>{,<char_id>};
  7722. **/
  7723. BUILDIN_FUNC(statusup)
  7724. {
  7725. int type;
  7726. TBL_PC *sd;
  7727. type = script_getnum(st,2);
  7728. if (!script_charid2sd(3, sd))
  7729. return SCRIPT_CMD_FAILURE;
  7730. pc_statusup(sd, type, 1);
  7731. return SCRIPT_CMD_SUCCESS;
  7732. }
  7733. /**
  7734. * statusup2 <stat>,<amount>{,<char_id>};
  7735. **/
  7736. BUILDIN_FUNC(statusup2)
  7737. {
  7738. int type,val;
  7739. TBL_PC *sd;
  7740. type=script_getnum(st,2);
  7741. val=script_getnum(st,3);
  7742. if (!script_charid2sd(4, sd))
  7743. return SCRIPT_CMD_FAILURE;
  7744. pc_statusup2(sd,type,val);
  7745. return SCRIPT_CMD_SUCCESS;
  7746. }
  7747. /// See 'doc/item_bonus.txt'
  7748. ///
  7749. /// bonus <bonus type>,<val1>;
  7750. /// bonus2 <bonus type>,<val1>,<val2>;
  7751. /// bonus3 <bonus type>,<val1>,<val2>,<val3>;
  7752. /// bonus4 <bonus type>,<val1>,<val2>,<val3>,<val4>;
  7753. /// bonus5 <bonus type>,<val1>,<val2>,<val3>,<val4>,<val5>;
  7754. BUILDIN_FUNC(bonus)
  7755. {
  7756. int type;
  7757. int val1;
  7758. int val2 = 0;
  7759. int val3 = 0;
  7760. int val4 = 0;
  7761. int val5 = 0;
  7762. TBL_PC* sd;
  7763. struct script_data *data;
  7764. sd = script_rid2sd(st);
  7765. if( sd == NULL )
  7766. return SCRIPT_CMD_SUCCESS; // no player attached
  7767. type = script_getnum(st,2);
  7768. switch( type ) {
  7769. case SP_AUTOSPELL:
  7770. case SP_AUTOSPELL_WHENHIT:
  7771. case SP_AUTOSPELL_ONSKILL:
  7772. case SP_SKILL_ATK:
  7773. case SP_SKILL_HEAL:
  7774. case SP_SKILL_HEAL2:
  7775. case SP_ADD_SKILL_BLOW:
  7776. case SP_CASTRATE:
  7777. case SP_ADDEFF_ONSKILL:
  7778. case SP_SKILL_USE_SP_RATE:
  7779. case SP_SKILL_COOLDOWN:
  7780. case SP_SKILL_FIXEDCAST:
  7781. case SP_SKILL_VARIABLECAST:
  7782. case SP_VARCASTRATE:
  7783. case SP_FIXCASTRATE:
  7784. case SP_SKILL_USE_SP:
  7785. case SP_SUB_SKILL:
  7786. // these bonuses support skill names
  7787. data = script_getdata(st, 3);
  7788. get_val(st, data); // Convert into value in case of a variable
  7789. val1 = ( data_isstring(data) ? skill_name2id(script_getstr(st,3)) : script_getnum(st,3) );
  7790. break;
  7791. default:
  7792. val1 = script_getnum(st,3);
  7793. break;
  7794. }
  7795. switch( script_lastdata(st)-2 ) {
  7796. case 1:
  7797. pc_bonus(sd, type, val1);
  7798. break;
  7799. case 2:
  7800. val2 = script_getnum(st,4);
  7801. pc_bonus2(sd, type, val1, val2);
  7802. break;
  7803. case 3:
  7804. val2 = script_getnum(st,4);
  7805. val3 = script_getnum(st,5);
  7806. pc_bonus3(sd, type, val1, val2, val3);
  7807. break;
  7808. case 4:
  7809. data = script_getdata(st, 4);
  7810. get_val(st, data); // Convert into value in case of a variable
  7811. if( type == SP_AUTOSPELL_ONSKILL && data_isstring(data) )
  7812. val2 = skill_name2id(script_getstr(st,4)); // 2nd value can be skill name
  7813. else
  7814. val2 = script_getnum(st,4);
  7815. val3 = script_getnum(st,5);
  7816. val4 = script_getnum(st,6);
  7817. pc_bonus4(sd, type, val1, val2, val3, val4);
  7818. break;
  7819. case 5:
  7820. data = script_getdata(st, 4);
  7821. get_val(st, data); // Convert into value in case of a variable
  7822. if( type == SP_AUTOSPELL_ONSKILL && data_isstring(data) )
  7823. val2 = skill_name2id(script_getstr(st,4)); // 2nd value can be skill name
  7824. else
  7825. val2 = script_getnum(st,4);
  7826. val3 = script_getnum(st,5);
  7827. val4 = script_getnum(st,6);
  7828. val5 = script_getnum(st,7);
  7829. pc_bonus5(sd, type, val1, val2, val3, val4, val5);
  7830. break;
  7831. default:
  7832. ShowDebug("buildin_bonus: unexpected number of arguments (%d)\n", (script_lastdata(st) - 1));
  7833. break;
  7834. }
  7835. return SCRIPT_CMD_SUCCESS;
  7836. }
  7837. BUILDIN_FUNC(autobonus)
  7838. {
  7839. unsigned int dur, pos;
  7840. short rate;
  7841. short atk_type = 0;
  7842. TBL_PC* sd;
  7843. const char *bonus_script, *other_script = NULL;
  7844. sd = script_rid2sd(st);
  7845. if( sd == NULL )
  7846. return SCRIPT_CMD_SUCCESS; // no player attached
  7847. if (current_equip_combo_pos)
  7848. pos = current_equip_combo_pos;
  7849. else
  7850. pos = sd->inventory.u.items_inventory[current_equip_item_index].equip;
  7851. if((sd->state.autobonus&pos) == pos)
  7852. return SCRIPT_CMD_SUCCESS;
  7853. rate = script_getnum(st,3);
  7854. dur = script_getnum(st,4);
  7855. bonus_script = script_getstr(st,2);
  7856. if( !rate || !dur || !pos || !bonus_script )
  7857. return SCRIPT_CMD_SUCCESS;
  7858. if( script_hasdata(st,5) )
  7859. atk_type = script_getnum(st,5);
  7860. if( script_hasdata(st,6) )
  7861. other_script = script_getstr(st,6);
  7862. if( pc_addautobonus(sd->autobonus,ARRAYLENGTH(sd->autobonus),
  7863. bonus_script,rate,dur,atk_type,other_script,pos,false) )
  7864. {
  7865. script_add_autobonus(bonus_script);
  7866. if( other_script )
  7867. script_add_autobonus(other_script);
  7868. }
  7869. return SCRIPT_CMD_SUCCESS;
  7870. }
  7871. BUILDIN_FUNC(autobonus2)
  7872. {
  7873. unsigned int dur, pos;
  7874. short rate;
  7875. short atk_type = 0;
  7876. TBL_PC* sd;
  7877. const char *bonus_script, *other_script = NULL;
  7878. sd = script_rid2sd(st);
  7879. if( sd == NULL )
  7880. return SCRIPT_CMD_SUCCESS; // no player attached
  7881. if (current_equip_combo_pos)
  7882. pos = current_equip_combo_pos;
  7883. else
  7884. pos = sd->inventory.u.items_inventory[current_equip_item_index].equip;
  7885. if((sd->state.autobonus&pos) == pos)
  7886. return SCRIPT_CMD_SUCCESS;
  7887. rate = script_getnum(st,3);
  7888. dur = script_getnum(st,4);
  7889. bonus_script = script_getstr(st,2);
  7890. if( !rate || !dur || !pos || !bonus_script )
  7891. return SCRIPT_CMD_SUCCESS;
  7892. if( script_hasdata(st,5) )
  7893. atk_type = script_getnum(st,5);
  7894. if( script_hasdata(st,6) )
  7895. other_script = script_getstr(st,6);
  7896. if( pc_addautobonus(sd->autobonus2,ARRAYLENGTH(sd->autobonus2),
  7897. bonus_script,rate,dur,atk_type,other_script,pos,false) )
  7898. {
  7899. script_add_autobonus(bonus_script);
  7900. if( other_script )
  7901. script_add_autobonus(other_script);
  7902. }
  7903. return SCRIPT_CMD_SUCCESS;
  7904. }
  7905. BUILDIN_FUNC(autobonus3)
  7906. {
  7907. unsigned int dur, pos;
  7908. short rate,atk_type;
  7909. TBL_PC* sd;
  7910. const char *bonus_script, *other_script = NULL;
  7911. struct script_data *data;
  7912. sd = script_rid2sd(st);
  7913. if( sd == NULL )
  7914. return SCRIPT_CMD_SUCCESS; // no player attached
  7915. if (current_equip_combo_pos)
  7916. pos = current_equip_combo_pos;
  7917. else
  7918. pos = sd->inventory.u.items_inventory[current_equip_item_index].equip;
  7919. if((sd->state.autobonus&pos) == pos)
  7920. return SCRIPT_CMD_SUCCESS;
  7921. rate = script_getnum(st,3);
  7922. dur = script_getnum(st,4);
  7923. data = script_getdata(st, 5);
  7924. get_val(st, data); // Convert into value in case of a variable
  7925. atk_type = ( data_isstring(data) ? skill_name2id(script_getstr(st,5)) : script_getnum(st,5) );
  7926. bonus_script = script_getstr(st,2);
  7927. if( !rate || !dur || !pos || !atk_type || !bonus_script )
  7928. return SCRIPT_CMD_SUCCESS;
  7929. if( script_hasdata(st,6) )
  7930. other_script = script_getstr(st,6);
  7931. if( pc_addautobonus(sd->autobonus3,ARRAYLENGTH(sd->autobonus3),
  7932. bonus_script,rate,dur,atk_type,other_script,pos,true) )
  7933. {
  7934. script_add_autobonus(bonus_script);
  7935. if( other_script )
  7936. script_add_autobonus(other_script);
  7937. }
  7938. return SCRIPT_CMD_SUCCESS;
  7939. }
  7940. /// Changes the level of a player skill.
  7941. /// <flag> defaults to 1
  7942. /// <flag>=0 : set the level of the skill
  7943. /// <flag>=1 : set the temporary level of the skill
  7944. /// <flag>=2 : add to the level of the skill
  7945. ///
  7946. /// skill <skill id>,<level>,<flag>
  7947. /// skill <skill id>,<level>
  7948. /// skill "<skill name>",<level>,<flag>
  7949. /// skill "<skill name>",<level>
  7950. BUILDIN_FUNC(skill)
  7951. {
  7952. int id;
  7953. int level;
  7954. int flag = ADDSKILL_TEMP;
  7955. TBL_PC* sd;
  7956. struct script_data *data;
  7957. const char* command = script_getfuncname(st);
  7958. sd = script_rid2sd(st);
  7959. if( sd == NULL )
  7960. return SCRIPT_CMD_SUCCESS;// no player attached, report source
  7961. if (strcmpi(command, "addtoskill") == 0)
  7962. flag = ADDSKILL_TEMP_ADDLEVEL;
  7963. data = script_getdata(st, 2);
  7964. get_val(st, data); // Convert into value in case of a variable
  7965. id = ( data_isstring(data) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  7966. level = script_getnum(st,3);
  7967. if( script_hasdata(st,4) )
  7968. flag = script_getnum(st,4);
  7969. pc_skill(sd, id, level, (enum e_addskill_type)flag);
  7970. return SCRIPT_CMD_SUCCESS;
  7971. }
  7972. /// Increases the level of a guild skill.
  7973. ///
  7974. /// guildskill <skill id>,<amount>;
  7975. /// guildskill "<skill name>",<amount>;
  7976. BUILDIN_FUNC(guildskill)
  7977. {
  7978. int id;
  7979. int level;
  7980. TBL_PC* sd;
  7981. int i;
  7982. struct script_data *data;
  7983. sd = script_rid2sd(st);
  7984. if( sd == NULL )
  7985. return SCRIPT_CMD_SUCCESS;// no player attached, report source
  7986. data = script_getdata(st, 2);
  7987. get_val(st, data); // Convert into value in case of a variable
  7988. id = ( data_isstring(data) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  7989. level = script_getnum(st,3);
  7990. for( i=0; i < level; i++ )
  7991. guild_skillup(sd, id);
  7992. return SCRIPT_CMD_SUCCESS;
  7993. }
  7994. /// Returns the level of the player skill.
  7995. ///
  7996. /// getskilllv(<skill id>) -> <level>
  7997. /// getskilllv("<skill name>") -> <level>
  7998. BUILDIN_FUNC(getskilllv)
  7999. {
  8000. int id;
  8001. TBL_PC* sd;
  8002. struct script_data *data;
  8003. sd = script_rid2sd(st);
  8004. if( sd == NULL )
  8005. return SCRIPT_CMD_SUCCESS;// no player attached, report source
  8006. data = script_getdata(st, 2);
  8007. get_val(st, data); // Convert into value in case of a variable
  8008. id = ( data_isstring(data) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  8009. script_pushint(st, pc_checkskill(sd,id));
  8010. return SCRIPT_CMD_SUCCESS;
  8011. }
  8012. /// Returns the level of the guild skill.
  8013. ///
  8014. /// getgdskilllv(<guild id>,<skill id>) -> <level>
  8015. /// getgdskilllv(<guild id>,"<skill name>") -> <level>
  8016. BUILDIN_FUNC(getgdskilllv)
  8017. {
  8018. int guild_id;
  8019. uint16 skill_id;
  8020. struct guild* g;
  8021. struct script_data *data;
  8022. guild_id = script_getnum(st,2);
  8023. data = script_getdata(st, 3);
  8024. get_val(st, data); // Convert into value in case of a variable
  8025. skill_id = ( data_isstring(data) ? skill_name2id(script_getstr(st,3)) : script_getnum(st,3) );
  8026. g = guild_search(guild_id);
  8027. if( g == NULL )
  8028. script_pushint(st, -1);
  8029. else
  8030. script_pushint(st, guild_checkskill(g,skill_id));
  8031. return SCRIPT_CMD_SUCCESS;
  8032. }
  8033. /// Returns the 'basic_skill_check' setting.
  8034. /// This config determines if the server checks the skill level of NV_BASIC
  8035. /// before allowing the basic actions.
  8036. ///
  8037. /// basicskillcheck() -> <bool>
  8038. BUILDIN_FUNC(basicskillcheck)
  8039. {
  8040. script_pushint(st, battle_config.basic_skill_check);
  8041. return SCRIPT_CMD_SUCCESS;
  8042. }
  8043. /// Returns the GM level of the player.
  8044. ///
  8045. /// getgmlevel({<char_id>}) -> <level>
  8046. BUILDIN_FUNC(getgmlevel)
  8047. {
  8048. TBL_PC* sd;
  8049. if (!script_charid2sd(2,sd))
  8050. return SCRIPT_CMD_FAILURE;
  8051. script_pushint(st, pc_get_group_level(sd));
  8052. return SCRIPT_CMD_SUCCESS;
  8053. }
  8054. /// Returns the group ID of the player.
  8055. ///
  8056. /// getgroupid({<char_id>}) -> <int>
  8057. BUILDIN_FUNC(getgroupid)
  8058. {
  8059. TBL_PC* sd;
  8060. if (!script_charid2sd(2,sd))
  8061. return SCRIPT_CMD_FAILURE;
  8062. script_pushint(st, pc_get_group_id(sd));
  8063. return SCRIPT_CMD_SUCCESS;
  8064. }
  8065. /// Terminates the execution of this script instance.
  8066. ///
  8067. /// end
  8068. BUILDIN_FUNC(end)
  8069. {
  8070. TBL_PC* sd;
  8071. sd = map_id2sd(st->rid);
  8072. st->state = END;
  8073. if (st->stack->defsp >= 1 && st->stack->stack_data[st->stack->defsp-1].type == C_RETINFO) {
  8074. int i;
  8075. for(i = 0; i < st->stack->sp; i++) {
  8076. if (st->stack->stack_data[i].type == C_RETINFO) { // Grab the first, aka the original
  8077. struct script_retinfo *ri = st->stack->stack_data[i].u.ri;
  8078. st->script = ri->script;
  8079. break;
  8080. }
  8081. }
  8082. }
  8083. if( st->mes_active )
  8084. st->mes_active = 0;
  8085. if (sd){
  8086. if (sd->state.callshop == 0)
  8087. clif_scriptclose(sd, st->oid); // If a menu/select/prompt is active, close it.
  8088. else
  8089. sd->state.callshop = 0;
  8090. }
  8091. return SCRIPT_CMD_SUCCESS;
  8092. }
  8093. /// Checks if the player has that effect state (option).
  8094. ///
  8095. /// checkoption(<option>{,<char_id>}) -> <bool>
  8096. BUILDIN_FUNC(checkoption)
  8097. {
  8098. int option;
  8099. TBL_PC* sd;
  8100. if (!script_charid2sd(3,sd))
  8101. return SCRIPT_CMD_FAILURE;
  8102. option = script_getnum(st,2);
  8103. if( sd->sc.option&option )
  8104. script_pushint(st, 1);
  8105. else
  8106. script_pushint(st, 0);
  8107. return SCRIPT_CMD_SUCCESS;
  8108. }
  8109. /// Checks if the player is in that body state (opt1).
  8110. ///
  8111. /// checkoption1(<opt1>{,<char_id>}) -> <bool>
  8112. BUILDIN_FUNC(checkoption1)
  8113. {
  8114. int opt1;
  8115. TBL_PC* sd;
  8116. if (!script_charid2sd(3,sd))
  8117. return SCRIPT_CMD_FAILURE;
  8118. opt1 = script_getnum(st,2);
  8119. if( sd->sc.opt1 == opt1 )
  8120. script_pushint(st, 1);
  8121. else
  8122. script_pushint(st, 0);
  8123. return SCRIPT_CMD_SUCCESS;
  8124. }
  8125. /// Checks if the player has that health state (opt2).
  8126. ///
  8127. /// checkoption2(<opt2>{,<char_id>}) -> <bool>
  8128. BUILDIN_FUNC(checkoption2)
  8129. {
  8130. int opt2;
  8131. TBL_PC* sd;
  8132. if (!script_charid2sd(3,sd))
  8133. return SCRIPT_CMD_FAILURE;
  8134. opt2 = script_getnum(st,2);
  8135. if( sd->sc.opt2&opt2 )
  8136. script_pushint(st, 1);
  8137. else
  8138. script_pushint(st, 0);
  8139. return SCRIPT_CMD_SUCCESS;
  8140. }
  8141. /// Changes the effect state (option) of the player.
  8142. /// <flag> defaults to 1
  8143. /// <flag>=0 : removes the option
  8144. /// <flag>=other : adds the option
  8145. ///
  8146. /// setoption <option>{,<flag>{,<char_id>}};
  8147. BUILDIN_FUNC(setoption)
  8148. {
  8149. int option;
  8150. int flag = 1;
  8151. TBL_PC* sd;
  8152. if (!script_charid2sd(4,sd))
  8153. return SCRIPT_CMD_FAILURE;
  8154. option = script_getnum(st,2);
  8155. if( script_hasdata(st,3) )
  8156. flag = script_getnum(st,3);
  8157. else if( !option ){// Request to remove everything.
  8158. flag = 0;
  8159. option = OPTION_FALCON|OPTION_RIDING;
  8160. #ifndef NEW_CARTS
  8161. option |= OPTION_CART;
  8162. #endif
  8163. }
  8164. if( flag ){// Add option
  8165. if( option&OPTION_WEDDING && !battle_config.wedding_modifydisplay )
  8166. option &= ~OPTION_WEDDING;// Do not show the wedding sprites
  8167. pc_setoption(sd, sd->sc.option|option);
  8168. } else// Remove option
  8169. pc_setoption(sd, sd->sc.option&~option);
  8170. return SCRIPT_CMD_SUCCESS;
  8171. }
  8172. /// Returns if the player has a cart.
  8173. ///
  8174. /// checkcart({char_id}) -> <bool>
  8175. ///
  8176. /// @author Valaris
  8177. BUILDIN_FUNC(checkcart)
  8178. {
  8179. TBL_PC* sd;
  8180. if (!script_charid2sd(2,sd))
  8181. return SCRIPT_CMD_FAILURE;
  8182. if( pc_iscarton(sd) )
  8183. script_pushint(st, 1);
  8184. else
  8185. script_pushint(st, 0);
  8186. return SCRIPT_CMD_SUCCESS;
  8187. }
  8188. /// Sets the cart of the player.
  8189. /// <type> defaults to 1
  8190. /// <type>=0 : removes the cart
  8191. /// <type>=1 : Normal cart
  8192. /// <type>=2 : Wooden cart
  8193. /// <type>=3 : Covered cart with flowers and ferns
  8194. /// <type>=4 : Wooden cart with a Panda doll on the back
  8195. /// <type>=5 : Normal cart with bigger wheels, a roof and a banner on the back
  8196. ///
  8197. /// setcart {<type>{,<char_id>}};
  8198. BUILDIN_FUNC(setcart)
  8199. {
  8200. int type = 1;
  8201. TBL_PC* sd;
  8202. if (!script_charid2sd(3,sd))
  8203. return SCRIPT_CMD_FAILURE;
  8204. if( script_hasdata(st,2) )
  8205. type = script_getnum(st,2);
  8206. pc_setcart(sd, type);
  8207. return SCRIPT_CMD_SUCCESS;
  8208. }
  8209. /// Returns if the player has a falcon.
  8210. ///
  8211. /// checkfalcon({char_id}) -> <bool>
  8212. ///
  8213. /// @author Valaris
  8214. BUILDIN_FUNC(checkfalcon)
  8215. {
  8216. TBL_PC* sd;
  8217. if (!script_charid2sd(2,sd))
  8218. return SCRIPT_CMD_FAILURE;
  8219. if( pc_isfalcon(sd) )
  8220. script_pushint(st, 1);
  8221. else
  8222. script_pushint(st, 0);
  8223. return SCRIPT_CMD_SUCCESS;
  8224. }
  8225. /// Sets if the player has a falcon or not.
  8226. /// <flag> defaults to 1
  8227. ///
  8228. /// setfalcon {<flag>{,<char_id>}};
  8229. BUILDIN_FUNC(setfalcon)
  8230. {
  8231. int flag = 1;
  8232. TBL_PC* sd;
  8233. if (!script_charid2sd(3,sd))
  8234. return SCRIPT_CMD_FAILURE;
  8235. if( script_hasdata(st,2) )
  8236. flag = script_getnum(st,2);
  8237. pc_setfalcon(sd, flag);
  8238. return SCRIPT_CMD_SUCCESS;
  8239. }
  8240. /// Returns if the player is riding.
  8241. ///
  8242. /// checkriding({char_id}) -> <bool>
  8243. ///
  8244. /// @author Valaris
  8245. BUILDIN_FUNC(checkriding)
  8246. {
  8247. TBL_PC* sd;
  8248. if (!script_charid2sd(2,sd))
  8249. return SCRIPT_CMD_FAILURE;
  8250. if( pc_isriding(sd) )
  8251. script_pushint(st, 1);
  8252. else
  8253. script_pushint(st, 0);
  8254. return SCRIPT_CMD_SUCCESS;
  8255. }
  8256. /// Sets if the player is riding.
  8257. /// <flag> defaults to 1
  8258. ///
  8259. /// setriding {<flag>{,<char_id>}};
  8260. BUILDIN_FUNC(setriding)
  8261. {
  8262. int flag = 1;
  8263. TBL_PC* sd;
  8264. if (!script_charid2sd(3,sd))
  8265. return SCRIPT_CMD_FAILURE;
  8266. if( script_hasdata(st,2) )
  8267. flag = script_getnum(st,2);
  8268. pc_setriding(sd, flag);
  8269. return SCRIPT_CMD_SUCCESS;
  8270. }
  8271. /// Returns if the player has a warg.
  8272. ///
  8273. /// checkwug({char_id}) -> <bool>
  8274. ///
  8275. BUILDIN_FUNC(checkwug)
  8276. {
  8277. TBL_PC* sd;
  8278. if (!script_charid2sd(2,sd))
  8279. return SCRIPT_CMD_FAILURE;
  8280. if( pc_iswug(sd) || pc_isridingwug(sd) )
  8281. script_pushint(st, 1);
  8282. else
  8283. script_pushint(st, 0);
  8284. return SCRIPT_CMD_SUCCESS;
  8285. }
  8286. /// Returns if the player is wearing MADO Gear.
  8287. ///
  8288. /// checkmadogear({<char_id>}) -> <bool>
  8289. ///
  8290. BUILDIN_FUNC(checkmadogear)
  8291. {
  8292. TBL_PC* sd;
  8293. if (!script_charid2sd(2,sd))
  8294. return SCRIPT_CMD_FAILURE;
  8295. if( pc_ismadogear(sd) )
  8296. script_pushint(st, 1);
  8297. else
  8298. script_pushint(st, 0);
  8299. return SCRIPT_CMD_SUCCESS;
  8300. }
  8301. /// Sets if the player is riding MADO Gear.
  8302. /// <flag> defaults to 1
  8303. ///
  8304. /// setmadogear {<flag>{,<char_id>}};
  8305. BUILDIN_FUNC(setmadogear)
  8306. {
  8307. int flag = 1;
  8308. TBL_PC* sd;
  8309. if (!script_charid2sd(3,sd))
  8310. return SCRIPT_CMD_FAILURE;
  8311. if( script_hasdata(st,2) )
  8312. flag = script_getnum(st,2);
  8313. pc_setmadogear(sd, flag);
  8314. return SCRIPT_CMD_SUCCESS;
  8315. }
  8316. /// Sets the save point of the player.
  8317. ///
  8318. /// save "<map name>",<x>,<y>{,{<range x>,<range y>,}<char_id>}
  8319. /// savepoint "<map name>",<x>,<y>{,{<range x>,<range y>,}<char_id>}
  8320. BUILDIN_FUNC(savepoint)
  8321. {
  8322. int x, y, m, cid_pos = 5;
  8323. unsigned short map_idx;
  8324. const char* str;
  8325. TBL_PC* sd;
  8326. if (script_lastdata(st) > 5)
  8327. cid_pos = 7;
  8328. if (!script_charid2sd(cid_pos,sd))
  8329. return SCRIPT_CMD_FAILURE;// no player attached, report source
  8330. str = script_getstr(st, 2);
  8331. map_idx = mapindex_name2id(str);
  8332. if( !map_idx )
  8333. return SCRIPT_CMD_FAILURE;
  8334. x = script_getnum(st,3);
  8335. y = script_getnum(st,4);
  8336. m = map_mapindex2mapid(map_idx);
  8337. if (cid_pos == 7) {
  8338. int dx = script_getnum(st,5), dy = script_getnum(st,6),
  8339. x1 = x + dx, y1 = y + dy,
  8340. x0 = x - dx, y0 = y - dy;
  8341. uint8 n = 10;
  8342. do {
  8343. x = x0 + rnd()%(x1-x0+1);
  8344. y = y0 + rnd()%(y1-y0+1);
  8345. } while (m != -1 && (--n) > 0 && !map_getcell(m, x, y, CELL_CHKPASS));
  8346. }
  8347. // Check for valid coordinates if map in local map-server
  8348. if (m != -1 && !map_getcell(m, x, y, CELL_CHKPASS)) {
  8349. ShowError("buildin_savepoint: Invalid coordinates %d,%d at map %s.\n", x, y, str);
  8350. return SCRIPT_CMD_FAILURE;
  8351. }
  8352. pc_setsavepoint(sd, map_idx, x, y);
  8353. return SCRIPT_CMD_SUCCESS;
  8354. }
  8355. /*==========================================
  8356. * GetTimeTick(0: System Tick, 1: Time Second Tick)
  8357. *------------------------------------------*/
  8358. BUILDIN_FUNC(gettimetick) /* Asgard Version */
  8359. {
  8360. int type;
  8361. time_t timer;
  8362. struct tm *t;
  8363. type=script_getnum(st,2);
  8364. switch(type){
  8365. case 2:
  8366. //type 2:(Get the number of seconds elapsed since 00:00 hours, Jan 1, 1970 UTC
  8367. // from the system clock.)
  8368. script_pushint(st,(int)time(NULL));
  8369. break;
  8370. case 1:
  8371. //type 1:(Second Ticks: 0-86399, 00:00:00-23:59:59)
  8372. time(&timer);
  8373. t=localtime(&timer);
  8374. script_pushint(st,((t->tm_hour)*3600+(t->tm_min)*60+t->tm_sec));
  8375. break;
  8376. case 0:
  8377. default:
  8378. //type 0:(System Ticks)
  8379. script_pushint(st,gettick());
  8380. break;
  8381. }
  8382. return SCRIPT_CMD_SUCCESS;
  8383. }
  8384. /*==========================================
  8385. * GetTime(Type);
  8386. * 1: Sec 2: Min 3: Hour
  8387. * 4: WeekDay 5: MonthDay 6: Month
  8388. * 7: Year
  8389. *------------------------------------------*/
  8390. BUILDIN_FUNC(gettime) /* Asgard Version */
  8391. {
  8392. int type;
  8393. time_t timer;
  8394. struct tm *t;
  8395. type=script_getnum(st,2);
  8396. time(&timer);
  8397. t=localtime(&timer);
  8398. switch(type){
  8399. case 1://Sec(0~59)
  8400. script_pushint(st,t->tm_sec);
  8401. break;
  8402. case 2://Min(0~59)
  8403. script_pushint(st,t->tm_min);
  8404. break;
  8405. case 3://Hour(0~23)
  8406. script_pushint(st,t->tm_hour);
  8407. break;
  8408. case 4://WeekDay(0~6)
  8409. script_pushint(st,t->tm_wday);
  8410. break;
  8411. case 5://MonthDay(01~31)
  8412. script_pushint(st,t->tm_mday);
  8413. break;
  8414. case 6://Month(01~12)
  8415. script_pushint(st,t->tm_mon+1);
  8416. break;
  8417. case 7://Year(20xx)
  8418. script_pushint(st,t->tm_year+1900);
  8419. break;
  8420. case 8://Year Day(01~366)
  8421. script_pushint(st,t->tm_yday+1);
  8422. break;
  8423. default://(format error)
  8424. script_pushint(st,-1);
  8425. break;
  8426. }
  8427. return SCRIPT_CMD_SUCCESS;
  8428. }
  8429. /*==========================================
  8430. * GetTimeStr("TimeFMT", Length);
  8431. *------------------------------------------*/
  8432. BUILDIN_FUNC(gettimestr)
  8433. {
  8434. char *tmpstr;
  8435. const char *fmtstr;
  8436. int maxlen;
  8437. time_t now = time(NULL);
  8438. fmtstr=script_getstr(st,2);
  8439. maxlen=script_getnum(st,3);
  8440. tmpstr=(char *)aMalloc((maxlen+1)*sizeof(char));
  8441. strftime(tmpstr,maxlen,fmtstr,localtime(&now));
  8442. tmpstr[maxlen]='\0';
  8443. script_pushstr(st,tmpstr);
  8444. return SCRIPT_CMD_SUCCESS;
  8445. }
  8446. /*==========================================
  8447. * Open player storage
  8448. *------------------------------------------*/
  8449. BUILDIN_FUNC(openstorage)
  8450. {
  8451. TBL_PC* sd;
  8452. sd = script_rid2sd(st);
  8453. if( sd == NULL )
  8454. return SCRIPT_CMD_SUCCESS;
  8455. storage_storageopen(sd);
  8456. return SCRIPT_CMD_SUCCESS;
  8457. }
  8458. BUILDIN_FUNC(guildopenstorage)
  8459. {
  8460. TBL_PC* sd;
  8461. int ret;
  8462. sd = script_rid2sd(st);
  8463. if( sd == NULL )
  8464. return SCRIPT_CMD_SUCCESS;
  8465. ret = storage_guild_storageopen(sd);
  8466. script_pushint(st,ret);
  8467. return SCRIPT_CMD_SUCCESS;
  8468. }
  8469. /*==========================================
  8470. * Make player use a skill trought item usage
  8471. *------------------------------------------*/
  8472. /// itemskill <skill id>,<level>
  8473. /// itemskill "<skill name>",<level>
  8474. BUILDIN_FUNC(itemskill)
  8475. {
  8476. int id;
  8477. int lv;
  8478. bool keep_requirement;
  8479. TBL_PC* sd;
  8480. struct script_data *data;
  8481. sd = script_rid2sd(st);
  8482. if( sd == NULL || sd->ud.skilltimer != INVALID_TIMER )
  8483. return SCRIPT_CMD_SUCCESS;
  8484. data = script_getdata(st, 2);
  8485. get_val(st, data); // Convert into value in case of a variable
  8486. id = ( data_isstring(data) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  8487. lv = script_getnum(st,3);
  8488. if (script_hasdata(st, 4)) {
  8489. keep_requirement = (script_getnum(st, 4) != 0);
  8490. } else {
  8491. keep_requirement = false;
  8492. }
  8493. sd->skillitem=id;
  8494. sd->skillitemlv=lv;
  8495. sd->skillitem_keep_requirement = keep_requirement;
  8496. clif_item_skill(sd,id,lv);
  8497. return SCRIPT_CMD_SUCCESS;
  8498. }
  8499. /*==========================================
  8500. * Attempt to create an item
  8501. *------------------------------------------*/
  8502. BUILDIN_FUNC(produce)
  8503. {
  8504. int trigger;
  8505. TBL_PC* sd;
  8506. sd = script_rid2sd(st);
  8507. if( sd == NULL )
  8508. return SCRIPT_CMD_SUCCESS;
  8509. trigger=script_getnum(st,2);
  8510. clif_skill_produce_mix_list(sd, -1, trigger);
  8511. return SCRIPT_CMD_SUCCESS;
  8512. }
  8513. /*==========================================
  8514. *
  8515. *------------------------------------------*/
  8516. BUILDIN_FUNC(cooking)
  8517. {
  8518. int trigger;
  8519. TBL_PC* sd;
  8520. sd = script_rid2sd(st);
  8521. if( sd == NULL )
  8522. return SCRIPT_CMD_SUCCESS;
  8523. trigger=script_getnum(st,2);
  8524. clif_cooking_list(sd, trigger, AM_PHARMACY, 1, 1);
  8525. return SCRIPT_CMD_SUCCESS;
  8526. }
  8527. /*==========================================
  8528. * Create a pet
  8529. *------------------------------------------*/
  8530. BUILDIN_FUNC(makepet)
  8531. {
  8532. TBL_PC* sd;
  8533. int id,pet_id;
  8534. id=script_getnum(st,2);
  8535. sd = script_rid2sd(st);
  8536. if( sd == NULL )
  8537. return SCRIPT_CMD_SUCCESS;
  8538. pet_id = search_petDB_index(id, PET_CLASS);
  8539. if (pet_id < 0)
  8540. pet_id = search_petDB_index(id, PET_EGG);
  8541. if (pet_id >= 0 && sd) {
  8542. sd->catch_target_class = pet_db[pet_id].class_;
  8543. intif_create_pet(
  8544. sd->status.account_id, sd->status.char_id,
  8545. (short)pet_db[pet_id].class_, (short)mob_db(pet_db[pet_id].class_)->lv,
  8546. (short)pet_db[pet_id].EggID, 0, (short)pet_db[pet_id].intimate,
  8547. 100, 0, 1, pet_db[pet_id].jname);
  8548. }
  8549. return SCRIPT_CMD_SUCCESS;
  8550. }
  8551. /**
  8552. * Give player exp base,job * quest_exp_rate/100
  8553. * getexp <base xp>,<job xp>{,<char_id>};
  8554. **/
  8555. BUILDIN_FUNC(getexp)
  8556. {
  8557. TBL_PC* sd;
  8558. int base=0,job=0;
  8559. double bonus;
  8560. if (!script_charid2sd(4,sd))
  8561. return SCRIPT_CMD_FAILURE;
  8562. base=script_getnum(st,2);
  8563. job =script_getnum(st,3);
  8564. if(base<0 || job<0)
  8565. return SCRIPT_CMD_SUCCESS;
  8566. // bonus for npc-given exp
  8567. bonus = battle_config.quest_exp_rate / 100.;
  8568. if (base)
  8569. base = (int) cap_value(base * bonus, 0, INT_MAX);
  8570. if (job)
  8571. job = (int) cap_value(job * bonus, 0, INT_MAX);
  8572. pc_gainexp(sd, NULL, base, job, 1);
  8573. return SCRIPT_CMD_SUCCESS;
  8574. }
  8575. /*==========================================
  8576. * Gain guild exp [Celest]
  8577. *------------------------------------------*/
  8578. BUILDIN_FUNC(guildgetexp)
  8579. {
  8580. TBL_PC* sd;
  8581. int exp;
  8582. sd = script_rid2sd(st);
  8583. if( sd == NULL )
  8584. return SCRIPT_CMD_SUCCESS;
  8585. exp = script_getnum(st,2);
  8586. if(exp < 0)
  8587. return SCRIPT_CMD_SUCCESS;
  8588. if(sd && sd->status.guild_id > 0)
  8589. guild_getexp (sd, exp);
  8590. return SCRIPT_CMD_SUCCESS;
  8591. }
  8592. /*==========================================
  8593. * Changes the guild master of a guild [Skotlex]
  8594. *------------------------------------------*/
  8595. BUILDIN_FUNC(guildchangegm)
  8596. {
  8597. TBL_PC *sd;
  8598. int guild_id;
  8599. const char *name;
  8600. guild_id = script_getnum(st,2);
  8601. name = script_getstr(st,3);
  8602. sd=map_nick2sd(name);
  8603. if (!sd)
  8604. script_pushint(st,0);
  8605. else
  8606. script_pushint(st,guild_gm_change(guild_id, sd));
  8607. return SCRIPT_CMD_SUCCESS;
  8608. }
  8609. /*==========================================
  8610. * Spawn a monster:
  8611. * *monster "<map name>",<x>,<y>,"<name to show>",<mob id>,<amount>{,"<event label>",<size>,<ai>};
  8612. *------------------------------------------*/
  8613. BUILDIN_FUNC(monster)
  8614. {
  8615. const char* mapn = script_getstr(st,2);
  8616. int x = script_getnum(st,3);
  8617. int y = script_getnum(st,4);
  8618. const char* str = script_getstr(st,5);
  8619. int class_ = script_getnum(st,6);
  8620. int amount = script_getnum(st,7);
  8621. const char* event = "";
  8622. unsigned int size = SZ_SMALL;
  8623. unsigned int ai = AI_NONE;
  8624. struct map_session_data* sd;
  8625. int16 m;
  8626. int i;
  8627. if (script_hasdata(st, 8)) {
  8628. event = script_getstr(st, 8);
  8629. check_event(st, event);
  8630. }
  8631. if (script_hasdata(st, 9)) {
  8632. size = script_getnum(st, 9);
  8633. if (size > 3) {
  8634. ShowWarning("buildin_monster: Attempted to spawn non-existing size %d for monster class %d\n", size, class_);
  8635. return SCRIPT_CMD_FAILURE;
  8636. }
  8637. }
  8638. if (script_hasdata(st, 10)) {
  8639. ai = script_getnum(st, 10);
  8640. if (ai >= AI_MAX) {
  8641. ShowWarning("buildin_monster: Attempted to spawn non-existing ai %d for monster class %d\n", ai, class_);
  8642. return SCRIPT_CMD_FAILURE;
  8643. }
  8644. }
  8645. if (class_ >= 0 && !mobdb_checkid(class_)) {
  8646. ShowWarning("buildin_monster: Attempted to spawn non-existing monster class %d\n", class_);
  8647. return SCRIPT_CMD_FAILURE;
  8648. }
  8649. sd = map_id2sd(st->rid);
  8650. if (sd && strcmp(mapn, "this") == 0)
  8651. m = sd->bl.m;
  8652. else
  8653. m = map_mapname2mapid(mapn);
  8654. for(i = 0; i < amount; i++) { //not optimised
  8655. int mobid = mob_once_spawn(sd, m, x, y, str, class_, 1, event, size, ai);
  8656. if (mobid)
  8657. mapreg_setreg(reference_uid(add_str("$@mobid"), i), mobid);
  8658. }
  8659. return SCRIPT_CMD_SUCCESS;
  8660. }
  8661. /*==========================================
  8662. * Request List of Monster Drops
  8663. *------------------------------------------*/
  8664. BUILDIN_FUNC(getmobdrops)
  8665. {
  8666. int class_ = script_getnum(st,2);
  8667. int i, j = 0;
  8668. struct mob_db *mob;
  8669. if( !mobdb_checkid(class_) )
  8670. {
  8671. script_pushint(st, 0);
  8672. return SCRIPT_CMD_SUCCESS;
  8673. }
  8674. mob = mob_db(class_);
  8675. for( i = 0; i < MAX_MOB_DROP; i++ )
  8676. {
  8677. if( mob->dropitem[i].nameid < 1 )
  8678. continue;
  8679. if( itemdb_exists(mob->dropitem[i].nameid) == NULL )
  8680. continue;
  8681. mapreg_setreg(reference_uid(add_str("$@MobDrop_item"), j), mob->dropitem[i].nameid);
  8682. mapreg_setreg(reference_uid(add_str("$@MobDrop_rate"), j), mob->dropitem[i].p);
  8683. j++;
  8684. }
  8685. mapreg_setreg(add_str("$@MobDrop_count"), j);
  8686. script_pushint(st, 1);
  8687. return SCRIPT_CMD_SUCCESS;
  8688. }
  8689. /*==========================================
  8690. * Spawn a monster in a random location
  8691. * in x0,x1,y0,y1 area.
  8692. *------------------------------------------*/
  8693. BUILDIN_FUNC(areamonster)
  8694. {
  8695. const char* mapn = script_getstr(st,2);
  8696. int x0 = script_getnum(st,3);
  8697. int y0 = script_getnum(st,4);
  8698. int x1 = script_getnum(st,5);
  8699. int y1 = script_getnum(st,6);
  8700. const char* str = script_getstr(st,7);
  8701. int class_ = script_getnum(st,8);
  8702. int amount = script_getnum(st,9);
  8703. const char* event = "";
  8704. unsigned int size = SZ_SMALL;
  8705. unsigned int ai = AI_NONE;
  8706. struct map_session_data* sd;
  8707. int16 m;
  8708. int i;
  8709. if (script_hasdata(st,10)) {
  8710. event = script_getstr(st, 10);
  8711. check_event(st, event);
  8712. }
  8713. if (script_hasdata(st, 11)) {
  8714. size = script_getnum(st, 11);
  8715. if (size > 3) {
  8716. ShowWarning("buildin_monster: Attempted to spawn non-existing size %d for monster class %d\n", size, class_);
  8717. return SCRIPT_CMD_FAILURE;
  8718. }
  8719. }
  8720. if (script_hasdata(st, 12)) {
  8721. ai = script_getnum(st, 12);
  8722. if (ai >= AI_MAX) {
  8723. ShowWarning("buildin_monster: Attempted to spawn non-existing ai %d for monster class %d\n", ai, class_);
  8724. return SCRIPT_CMD_FAILURE;
  8725. }
  8726. }
  8727. sd = map_id2sd(st->rid);
  8728. if (sd && strcmp(mapn, "this") == 0)
  8729. m = sd->bl.m;
  8730. else
  8731. m = map_mapname2mapid(mapn);
  8732. for(i = 0; i < amount; i++) { //not optimised
  8733. int mobid = mob_once_spawn_area(sd, m, x0, y0, x1, y1, str, class_, 1, event, size, ai);
  8734. if (mobid)
  8735. mapreg_setreg(reference_uid(add_str("$@mobid"), i), mobid);
  8736. }
  8737. return SCRIPT_CMD_SUCCESS;
  8738. }
  8739. /*==========================================
  8740. * KillMonster subcheck, verify if mob to kill ain't got an even to handle, could be force kill by allflag
  8741. *------------------------------------------*/
  8742. static int buildin_killmonster_sub_strip(struct block_list *bl,va_list ap)
  8743. { //same fix but with killmonster instead - stripping events from mobs.
  8744. TBL_MOB* md = (TBL_MOB*)bl;
  8745. char *event=va_arg(ap,char *);
  8746. int allflag=va_arg(ap,int);
  8747. md->state.npc_killmonster = 1;
  8748. if(!allflag){
  8749. if(strcmp(event,md->npc_event)==0)
  8750. status_kill(bl);
  8751. }else{
  8752. if(!md->spawn)
  8753. status_kill(bl);
  8754. }
  8755. md->state.npc_killmonster = 0;
  8756. return SCRIPT_CMD_SUCCESS;
  8757. }
  8758. static int buildin_killmonster_sub(struct block_list *bl,va_list ap)
  8759. {
  8760. TBL_MOB* md = (TBL_MOB*)bl;
  8761. char *event=va_arg(ap,char *);
  8762. int allflag=va_arg(ap,int);
  8763. if(!allflag){
  8764. if(strcmp(event,md->npc_event)==0)
  8765. status_kill(bl);
  8766. }else{
  8767. if(!md->spawn)
  8768. status_kill(bl);
  8769. }
  8770. return SCRIPT_CMD_SUCCESS;
  8771. }
  8772. BUILDIN_FUNC(killmonster)
  8773. {
  8774. const char *mapname,*event;
  8775. int16 m,allflag=0;
  8776. mapname=script_getstr(st,2);
  8777. event=script_getstr(st,3);
  8778. if(strcmp(event,"All")==0)
  8779. allflag = 1;
  8780. else
  8781. check_event(st, event);
  8782. if( (m=map_mapname2mapid(mapname))<0 )
  8783. return SCRIPT_CMD_SUCCESS;
  8784. if( script_hasdata(st,4) ) {
  8785. if ( script_getnum(st,4) == 1 ) {
  8786. map_foreachinmap(buildin_killmonster_sub, m, BL_MOB, event ,allflag);
  8787. return SCRIPT_CMD_SUCCESS;
  8788. }
  8789. }
  8790. map_freeblock_lock();
  8791. map_foreachinmap(buildin_killmonster_sub_strip, m, BL_MOB, event ,allflag);
  8792. map_freeblock_unlock();
  8793. return SCRIPT_CMD_SUCCESS;
  8794. }
  8795. static int buildin_killmonsterall_sub_strip(struct block_list *bl,va_list ap)
  8796. { //Strips the event from the mob if it's killed the old method.
  8797. struct mob_data *md;
  8798. md = BL_CAST(BL_MOB, bl);
  8799. if (md->npc_event[0])
  8800. md->npc_event[0] = 0;
  8801. status_kill(bl);
  8802. return 0;
  8803. }
  8804. static int buildin_killmonsterall_sub(struct block_list *bl,va_list ap)
  8805. {
  8806. status_kill(bl);
  8807. return 0;
  8808. }
  8809. BUILDIN_FUNC(killmonsterall)
  8810. {
  8811. const char *mapname;
  8812. int16 m;
  8813. mapname=script_getstr(st,2);
  8814. if( (m = map_mapname2mapid(mapname))<0 )
  8815. return SCRIPT_CMD_SUCCESS;
  8816. if( script_hasdata(st,3) ) {
  8817. if ( script_getnum(st,3) == 1 ) {
  8818. map_foreachinmap(buildin_killmonsterall_sub,m,BL_MOB);
  8819. return SCRIPT_CMD_SUCCESS;
  8820. }
  8821. }
  8822. map_foreachinmap(buildin_killmonsterall_sub_strip,m,BL_MOB);
  8823. return SCRIPT_CMD_SUCCESS;
  8824. }
  8825. /*==========================================
  8826. * Creates a clone of a player.
  8827. * clone map, x, y, event, char_id, master_id, mode, flag, duration
  8828. *------------------------------------------*/
  8829. BUILDIN_FUNC(clone)
  8830. {
  8831. TBL_PC *sd, *msd=NULL;
  8832. uint32 char_id, master_id = 0, x, y, flag = 0, m;
  8833. enum e_mode mode = 0;
  8834. unsigned int duration = 0;
  8835. const char *mapname,*event;
  8836. mapname=script_getstr(st,2);
  8837. x=script_getnum(st,3);
  8838. y=script_getnum(st,4);
  8839. event=script_getstr(st,5);
  8840. char_id=script_getnum(st,6);
  8841. if( script_hasdata(st,7) )
  8842. master_id=script_getnum(st,7);
  8843. if( script_hasdata(st,8) )
  8844. mode=script_getnum(st,8);
  8845. if( script_hasdata(st,9) )
  8846. flag=script_getnum(st,9);
  8847. if( script_hasdata(st,10) )
  8848. duration=script_getnum(st,10);
  8849. check_event(st, event);
  8850. m = map_mapname2mapid(mapname);
  8851. if (m < 0)
  8852. return SCRIPT_CMD_SUCCESS;
  8853. sd = map_charid2sd(char_id);
  8854. if (master_id) {
  8855. msd = map_charid2sd(master_id);
  8856. if (msd)
  8857. master_id = msd->bl.id;
  8858. else
  8859. master_id = 0;
  8860. }
  8861. if (sd) //Return ID of newly crafted clone.
  8862. script_pushint(st,mob_clone_spawn(sd, m, x, y, event, master_id, mode, flag, 1000*duration));
  8863. else //Failed to create clone.
  8864. script_pushint(st,0);
  8865. return SCRIPT_CMD_SUCCESS;
  8866. }
  8867. /*==========================================
  8868. *------------------------------------------*/
  8869. BUILDIN_FUNC(doevent)
  8870. {
  8871. const char* event = script_getstr(st,2);
  8872. struct map_session_data* sd;
  8873. if( ( sd = script_rid2sd(st) ) == NULL )
  8874. {
  8875. return SCRIPT_CMD_SUCCESS;
  8876. }
  8877. check_event(st, event);
  8878. npc_event(sd, event, 0);
  8879. return SCRIPT_CMD_SUCCESS;
  8880. }
  8881. /*==========================================
  8882. *------------------------------------------*/
  8883. BUILDIN_FUNC(donpcevent)
  8884. {
  8885. const char* event = script_getstr(st,2);
  8886. check_event(st, event);
  8887. if( !npc_event_do(event) ) {
  8888. struct npc_data * nd = map_id2nd(st->oid);
  8889. ShowDebug("NPCEvent '%s' not found! (source: %s)\n",event,nd?nd->name:"Unknown");
  8890. script_pushint(st, 0);
  8891. } else
  8892. script_pushint(st, 1);
  8893. return SCRIPT_CMD_SUCCESS;
  8894. }
  8895. /// for Aegis compatibility
  8896. /// basically a specialized 'donpcevent', with the event specified as two arguments instead of one
  8897. BUILDIN_FUNC(cmdothernpc) // Added by RoVeRT
  8898. {
  8899. const char* npc = script_getstr(st,2);
  8900. const char* command = script_getstr(st,3);
  8901. char event[EVENT_NAME_LENGTH];
  8902. snprintf(event, sizeof(event), "%s::OnCommand%s", npc, command);
  8903. check_event(st, event);
  8904. npc_event_do(event);
  8905. return SCRIPT_CMD_SUCCESS;
  8906. }
  8907. /*==========================================
  8908. *------------------------------------------*/
  8909. BUILDIN_FUNC(addtimer)
  8910. {
  8911. int tick = script_getnum(st,2);
  8912. const char* event = script_getstr(st, 3);
  8913. TBL_PC* sd;
  8914. check_event(st, event);
  8915. sd = script_rid2sd(st);
  8916. if( sd == NULL )
  8917. return SCRIPT_CMD_SUCCESS;
  8918. if (!pc_addeventtimer(sd,tick,event)) {
  8919. ShowWarning("buildin_addtimer: Event timer is full, can't add new event timer. (cid:%d timer:%s)\n",sd->status.char_id,event);
  8920. return SCRIPT_CMD_FAILURE;
  8921. }
  8922. return SCRIPT_CMD_SUCCESS;
  8923. }
  8924. /*==========================================
  8925. *------------------------------------------*/
  8926. BUILDIN_FUNC(deltimer)
  8927. {
  8928. const char *event;
  8929. TBL_PC* sd;
  8930. event=script_getstr(st, 2);
  8931. sd = script_rid2sd(st);
  8932. if( sd == NULL )
  8933. return SCRIPT_CMD_SUCCESS;
  8934. check_event(st, event);
  8935. pc_deleventtimer(sd,event);
  8936. return SCRIPT_CMD_SUCCESS;
  8937. }
  8938. /*==========================================
  8939. *------------------------------------------*/
  8940. BUILDIN_FUNC(addtimercount)
  8941. {
  8942. const char *event;
  8943. int tick;
  8944. TBL_PC* sd;
  8945. tick=script_getnum(st,2);
  8946. event=script_getstr(st,3);
  8947. sd = script_rid2sd(st);
  8948. if( sd == NULL )
  8949. return SCRIPT_CMD_SUCCESS;
  8950. check_event(st, event);
  8951. pc_addeventtimercount(sd,event,tick);
  8952. return SCRIPT_CMD_SUCCESS;
  8953. }
  8954. /*==========================================
  8955. *------------------------------------------*/
  8956. BUILDIN_FUNC(initnpctimer)
  8957. {
  8958. struct npc_data *nd;
  8959. int flag = 0;
  8960. if( script_hasdata(st,3) )
  8961. { //Two arguments: NPC name and attach flag.
  8962. nd = npc_name2id(script_getstr(st, 2));
  8963. flag = script_getnum(st,3);
  8964. }
  8965. else if( script_hasdata(st,2) )
  8966. { //Check if argument is numeric (flag) or string (npc name)
  8967. struct script_data *data;
  8968. data = script_getdata(st,2);
  8969. get_val(st,data);
  8970. if( data_isstring(data) ) //NPC name
  8971. nd = npc_name2id(conv_str(st, data));
  8972. else if( data_isint(data) ) //Flag
  8973. {
  8974. nd = (struct npc_data *)map_id2bl(st->oid);
  8975. flag = conv_num(st,data);
  8976. }
  8977. else
  8978. {
  8979. ShowError("initnpctimer: invalid argument type #1 (needs be int or string)).\n");
  8980. return SCRIPT_CMD_FAILURE;
  8981. }
  8982. }
  8983. else
  8984. nd = (struct npc_data *)map_id2bl(st->oid);
  8985. if( !nd )
  8986. return SCRIPT_CMD_SUCCESS;
  8987. if( flag ) //Attach
  8988. {
  8989. TBL_PC* sd = script_rid2sd(st);
  8990. if( sd == NULL )
  8991. return SCRIPT_CMD_SUCCESS;
  8992. nd->u.scr.rid = sd->bl.id;
  8993. }
  8994. nd->u.scr.timertick = 0;
  8995. npc_settimerevent_tick(nd,0);
  8996. npc_timerevent_start(nd, st->rid);
  8997. return SCRIPT_CMD_SUCCESS;
  8998. }
  8999. /*==========================================
  9000. *------------------------------------------*/
  9001. BUILDIN_FUNC(startnpctimer)
  9002. {
  9003. struct npc_data *nd;
  9004. int flag = 0;
  9005. if( script_hasdata(st,3) )
  9006. { //Two arguments: NPC name and attach flag.
  9007. nd = npc_name2id(script_getstr(st, 2));
  9008. flag = script_getnum(st,3);
  9009. }
  9010. else if( script_hasdata(st,2) )
  9011. { //Check if argument is numeric (flag) or string (npc name)
  9012. struct script_data *data;
  9013. data = script_getdata(st,2);
  9014. get_val(st,data);
  9015. if( data_isstring(data) ) //NPC name
  9016. nd = npc_name2id(conv_str(st, data));
  9017. else if( data_isint(data) ) //Flag
  9018. {
  9019. nd = (struct npc_data *)map_id2bl(st->oid);
  9020. flag = conv_num(st,data);
  9021. }
  9022. else
  9023. {
  9024. ShowError("initnpctimer: invalid argument type #1 (needs be int or string)).\n");
  9025. return SCRIPT_CMD_FAILURE;
  9026. }
  9027. }
  9028. else
  9029. nd=(struct npc_data *)map_id2bl(st->oid);
  9030. if( !nd )
  9031. return SCRIPT_CMD_SUCCESS;
  9032. if( flag ) //Attach
  9033. {
  9034. TBL_PC* sd = script_rid2sd(st);
  9035. if( sd == NULL )
  9036. return SCRIPT_CMD_SUCCESS;
  9037. nd->u.scr.rid = sd->bl.id;
  9038. }
  9039. npc_timerevent_start(nd, st->rid);
  9040. return SCRIPT_CMD_SUCCESS;
  9041. }
  9042. /*==========================================
  9043. *------------------------------------------*/
  9044. BUILDIN_FUNC(stopnpctimer)
  9045. {
  9046. struct npc_data *nd;
  9047. int flag = 0;
  9048. if( script_hasdata(st,3) )
  9049. { //Two arguments: NPC name and attach flag.
  9050. nd = npc_name2id(script_getstr(st, 2));
  9051. flag = script_getnum(st,3);
  9052. }
  9053. else if( script_hasdata(st,2) )
  9054. { //Check if argument is numeric (flag) or string (npc name)
  9055. struct script_data *data;
  9056. data = script_getdata(st,2);
  9057. get_val(st,data);
  9058. if( data_isstring(data) ) //NPC name
  9059. nd = npc_name2id(conv_str(st, data));
  9060. else if( data_isint(data) ) //Flag
  9061. {
  9062. nd = (struct npc_data *)map_id2bl(st->oid);
  9063. flag = conv_num(st,data);
  9064. }
  9065. else
  9066. {
  9067. ShowError("initnpctimer: invalid argument type #1 (needs be int or string)).\n");
  9068. return SCRIPT_CMD_FAILURE;
  9069. }
  9070. }
  9071. else
  9072. nd=(struct npc_data *)map_id2bl(st->oid);
  9073. if( !nd )
  9074. return SCRIPT_CMD_SUCCESS;
  9075. if( flag ) //Detach
  9076. nd->u.scr.rid = 0;
  9077. npc_timerevent_stop(nd);
  9078. return SCRIPT_CMD_SUCCESS;
  9079. }
  9080. /*==========================================
  9081. *------------------------------------------*/
  9082. BUILDIN_FUNC(getnpctimer)
  9083. {
  9084. struct npc_data *nd;
  9085. TBL_PC *sd;
  9086. int type = script_getnum(st,2);
  9087. int val = 0;
  9088. if( script_hasdata(st,3) )
  9089. nd = npc_name2id(script_getstr(st,3));
  9090. else
  9091. nd = (struct npc_data *)map_id2bl(st->oid);
  9092. if( !nd || nd->bl.type != BL_NPC )
  9093. {
  9094. script_pushint(st,0);
  9095. ShowError("getnpctimer: Invalid NPC.\n");
  9096. return SCRIPT_CMD_FAILURE;
  9097. }
  9098. switch( type )
  9099. {
  9100. case 0: val = npc_gettimerevent_tick(nd); break;
  9101. case 1:
  9102. if( nd->u.scr.rid )
  9103. {
  9104. sd = map_id2sd(nd->u.scr.rid);
  9105. if( !sd )
  9106. {
  9107. ShowError("buildin_getnpctimer: Attached player not found!\n");
  9108. break;
  9109. }
  9110. val = (sd->npc_timer_id != INVALID_TIMER);
  9111. }
  9112. else
  9113. val = (nd->u.scr.timerid != INVALID_TIMER);
  9114. break;
  9115. case 2: val = nd->u.scr.timeramount; break;
  9116. }
  9117. script_pushint(st,val);
  9118. return SCRIPT_CMD_SUCCESS;
  9119. }
  9120. /*==========================================
  9121. *------------------------------------------*/
  9122. BUILDIN_FUNC(setnpctimer)
  9123. {
  9124. int tick;
  9125. struct npc_data *nd;
  9126. tick = script_getnum(st,2);
  9127. if( script_hasdata(st,3) )
  9128. nd = npc_name2id(script_getstr(st,3));
  9129. else
  9130. nd = (struct npc_data *)map_id2bl(st->oid);
  9131. if( !nd || nd->bl.type != BL_NPC )
  9132. {
  9133. script_pushint(st,1);
  9134. ShowError("setnpctimer: Invalid NPC.\n");
  9135. return SCRIPT_CMD_FAILURE;
  9136. }
  9137. npc_settimerevent_tick(nd,tick);
  9138. script_pushint(st,0);
  9139. return SCRIPT_CMD_SUCCESS;
  9140. }
  9141. /*==========================================
  9142. * attaches the player rid to the timer [Celest]
  9143. *------------------------------------------*/
  9144. BUILDIN_FUNC(attachnpctimer)
  9145. {
  9146. TBL_PC *sd;
  9147. struct npc_data *nd = (struct npc_data *)map_id2bl(st->oid);
  9148. if( !nd || nd->bl.type != BL_NPC )
  9149. {
  9150. script_pushint(st,1);
  9151. ShowError("setnpctimer: Invalid NPC.\n");
  9152. return SCRIPT_CMD_FAILURE;
  9153. }
  9154. if( script_hasdata(st,2) )
  9155. sd = map_nick2sd(script_getstr(st,2));
  9156. else
  9157. sd = script_rid2sd(st);
  9158. if( !sd )
  9159. {
  9160. script_pushint(st,1);
  9161. ShowWarning("attachnpctimer: Invalid player.\n");
  9162. return SCRIPT_CMD_FAILURE;
  9163. }
  9164. nd->u.scr.rid = sd->bl.id;
  9165. script_pushint(st,0);
  9166. return SCRIPT_CMD_SUCCESS;
  9167. }
  9168. /*==========================================
  9169. * detaches a player rid from the timer [Celest]
  9170. *------------------------------------------*/
  9171. BUILDIN_FUNC(detachnpctimer)
  9172. {
  9173. struct npc_data *nd;
  9174. if( script_hasdata(st,2) )
  9175. nd = npc_name2id(script_getstr(st,2));
  9176. else
  9177. nd = (struct npc_data *)map_id2bl(st->oid);
  9178. if( !nd || nd->bl.type != BL_NPC )
  9179. {
  9180. script_pushint(st,1);
  9181. ShowError("detachnpctimer: Invalid NPC.\n");
  9182. return SCRIPT_CMD_FAILURE;
  9183. }
  9184. nd->u.scr.rid = 0;
  9185. script_pushint(st,0);
  9186. return SCRIPT_CMD_SUCCESS;
  9187. }
  9188. /*==========================================
  9189. * To avoid "player not attached" script errors, this function is provided,
  9190. * it checks if there is a player attached to the current script. [Skotlex]
  9191. * If no, returns 0, if yes, returns the account_id of the attached player.
  9192. *------------------------------------------*/
  9193. BUILDIN_FUNC(playerattached)
  9194. {
  9195. if(st->rid == 0 || map_id2sd(st->rid) == NULL)
  9196. script_pushint(st,0);
  9197. else
  9198. script_pushint(st,st->rid);
  9199. return SCRIPT_CMD_SUCCESS;
  9200. }
  9201. /*==========================================
  9202. *------------------------------------------*/
  9203. BUILDIN_FUNC(announce)
  9204. {
  9205. const char *mes = script_getstr(st,2);
  9206. int flag = script_getnum(st,3);
  9207. const char *fontColor = script_hasdata(st,4) ? script_getstr(st,4) : NULL;
  9208. int fontType = script_hasdata(st,5) ? script_getnum(st,5) : FW_NORMAL; // default fontType
  9209. int fontSize = script_hasdata(st,6) ? script_getnum(st,6) : 12; // default fontSize
  9210. int fontAlign = script_hasdata(st,7) ? script_getnum(st,7) : 0; // default fontAlign
  9211. int fontY = script_hasdata(st,8) ? script_getnum(st,8) : 0; // default fontY
  9212. if (flag&(BC_TARGET_MASK|BC_SOURCE_MASK)) // Broadcast source or broadcast region defined
  9213. {
  9214. send_target target;
  9215. 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
  9216. if (bl == NULL)
  9217. return SCRIPT_CMD_SUCCESS;
  9218. switch (flag&BC_TARGET_MASK) {
  9219. case BC_MAP: target = ALL_SAMEMAP; break;
  9220. case BC_AREA: target = AREA; break;
  9221. case BC_SELF: target = SELF; break;
  9222. default: target = ALL_CLIENT; break; // BC_ALL
  9223. }
  9224. if (fontColor)
  9225. clif_broadcast2(bl, mes, (int)strlen(mes)+1, strtol(fontColor, (char **)NULL, 0), fontType, fontSize, fontAlign, fontY, target);
  9226. else
  9227. clif_broadcast(bl, mes, (int)strlen(mes)+1, flag&BC_COLOR_MASK, target);
  9228. }
  9229. else
  9230. {
  9231. if (fontColor)
  9232. intif_broadcast2(mes, (int)strlen(mes)+1, strtol(fontColor, (char **)NULL, 0), fontType, fontSize, fontAlign, fontY);
  9233. else
  9234. intif_broadcast(mes, (int)strlen(mes)+1, flag&BC_COLOR_MASK);
  9235. }
  9236. return SCRIPT_CMD_SUCCESS;
  9237. }
  9238. /*==========================================
  9239. *------------------------------------------*/
  9240. static int buildin_announce_sub(struct block_list *bl, va_list ap)
  9241. {
  9242. char *mes = va_arg(ap, char *);
  9243. int len = va_arg(ap, int);
  9244. int type = va_arg(ap, int);
  9245. char *fontColor = va_arg(ap, char *);
  9246. short fontType = (short)va_arg(ap, int);
  9247. short fontSize = (short)va_arg(ap, int);
  9248. short fontAlign = (short)va_arg(ap, int);
  9249. short fontY = (short)va_arg(ap, int);
  9250. if (fontColor)
  9251. clif_broadcast2(bl, mes, len, strtol(fontColor, (char **)NULL, 0), fontType, fontSize, fontAlign, fontY, SELF);
  9252. else
  9253. clif_broadcast(bl, mes, len, type, SELF);
  9254. return SCRIPT_CMD_SUCCESS;
  9255. }
  9256. BUILDIN_FUNC(mapannounce)
  9257. {
  9258. const char *mapname = script_getstr(st,2);
  9259. const char *mes = script_getstr(st,3);
  9260. int flag = script_getnum(st,4);
  9261. const char *fontColor = script_hasdata(st,5) ? script_getstr(st,5) : NULL;
  9262. int fontType = script_hasdata(st,6) ? script_getnum(st,6) : FW_NORMAL; // default fontType
  9263. int fontSize = script_hasdata(st,7) ? script_getnum(st,7) : 12; // default fontSize
  9264. int fontAlign = script_hasdata(st,8) ? script_getnum(st,8) : 0; // default fontAlign
  9265. int fontY = script_hasdata(st,9) ? script_getnum(st,9) : 0; // default fontY
  9266. int16 m;
  9267. if ((m = map_mapname2mapid(mapname)) < 0)
  9268. return SCRIPT_CMD_SUCCESS;
  9269. map_foreachinmap(buildin_announce_sub, m, BL_PC,
  9270. mes, strlen(mes)+1, flag&BC_COLOR_MASK, fontColor, fontType, fontSize, fontAlign, fontY);
  9271. return SCRIPT_CMD_SUCCESS;
  9272. }
  9273. /*==========================================
  9274. *------------------------------------------*/
  9275. BUILDIN_FUNC(areaannounce)
  9276. {
  9277. const char *mapname = script_getstr(st,2);
  9278. int x0 = script_getnum(st,3);
  9279. int y0 = script_getnum(st,4);
  9280. int x1 = script_getnum(st,5);
  9281. int y1 = script_getnum(st,6);
  9282. const char *mes = script_getstr(st,7);
  9283. int flag = script_getnum(st,8);
  9284. const char *fontColor = script_hasdata(st,9) ? script_getstr(st,9) : NULL;
  9285. int fontType = script_hasdata(st,10) ? script_getnum(st,10) : FW_NORMAL; // default fontType
  9286. int fontSize = script_hasdata(st,11) ? script_getnum(st,11) : 12; // default fontSize
  9287. int fontAlign = script_hasdata(st,12) ? script_getnum(st,12) : 0; // default fontAlign
  9288. int fontY = script_hasdata(st,13) ? script_getnum(st,13) : 0; // default fontY
  9289. int16 m;
  9290. if ((m = map_mapname2mapid(mapname)) < 0)
  9291. return SCRIPT_CMD_SUCCESS;
  9292. map_foreachinarea(buildin_announce_sub, m, x0, y0, x1, y1, BL_PC,
  9293. mes, strlen(mes)+1, flag&BC_COLOR_MASK, fontColor, fontType, fontSize, fontAlign, fontY);
  9294. return SCRIPT_CMD_SUCCESS;
  9295. }
  9296. /*==========================================
  9297. *------------------------------------------*/
  9298. BUILDIN_FUNC(getusers)
  9299. {
  9300. int flag, val = 0;
  9301. struct map_session_data* sd;
  9302. struct block_list* bl = NULL;
  9303. flag = script_getnum(st,2);
  9304. switch(flag&0x07)
  9305. {
  9306. case 0:
  9307. if(flag&0x8)
  9308. {// npc
  9309. bl = map_id2bl(st->oid);
  9310. }
  9311. else if((sd = script_rid2sd(st))!=NULL)
  9312. {// pc
  9313. bl = &sd->bl;
  9314. }
  9315. if(bl)
  9316. {
  9317. val = map[bl->m].users;
  9318. }
  9319. break;
  9320. case 1:
  9321. val = map_getusers();
  9322. break;
  9323. default:
  9324. ShowWarning("buildin_getusers: Unknown type %d.\n", flag);
  9325. script_pushint(st,0);
  9326. return SCRIPT_CMD_FAILURE;
  9327. }
  9328. script_pushint(st,val);
  9329. return SCRIPT_CMD_SUCCESS;
  9330. }
  9331. /*==========================================
  9332. * getmapguildusers("mapname",guild ID) Returns the number guild members present on a map [Reddozen]
  9333. *------------------------------------------*/
  9334. BUILDIN_FUNC(getmapguildusers)
  9335. {
  9336. const char *str;
  9337. int16 m;
  9338. int gid;
  9339. int c=0;
  9340. struct guild *g = NULL;
  9341. str=script_getstr(st,2);
  9342. gid=script_getnum(st,3);
  9343. if ((m = map_mapname2mapid(str)) < 0) { // map id on this server (m == -1 if not in actual map-server)
  9344. script_pushint(st,-1);
  9345. return SCRIPT_CMD_SUCCESS;
  9346. }
  9347. g = guild_search(gid);
  9348. if (g){
  9349. unsigned short i;
  9350. for(i = 0; i < g->max_member; i++)
  9351. {
  9352. if (g->member[i].sd && g->member[i].sd->bl.m == m)
  9353. c++;
  9354. }
  9355. }
  9356. script_pushint(st,c);
  9357. return SCRIPT_CMD_SUCCESS;
  9358. }
  9359. /*==========================================
  9360. *------------------------------------------*/
  9361. BUILDIN_FUNC(getmapusers)
  9362. {
  9363. const char *str;
  9364. int16 m;
  9365. str=script_getstr(st,2);
  9366. if( (m=map_mapname2mapid(str))< 0){
  9367. script_pushint(st,-1);
  9368. return SCRIPT_CMD_SUCCESS;
  9369. }
  9370. script_pushint(st,map[m].users);
  9371. return SCRIPT_CMD_SUCCESS;
  9372. }
  9373. /*==========================================
  9374. *------------------------------------------*/
  9375. static int buildin_getareausers_sub(struct block_list *bl,va_list ap)
  9376. {
  9377. int *users=va_arg(ap,int *);
  9378. (*users)++;
  9379. return SCRIPT_CMD_SUCCESS;
  9380. }
  9381. BUILDIN_FUNC(getareausers)
  9382. {
  9383. const char *str;
  9384. int16 m,x0,y0,x1,y1,users=0; //doubt we can have more then 32k users on
  9385. str=script_getstr(st,2);
  9386. x0=script_getnum(st,3);
  9387. y0=script_getnum(st,4);
  9388. x1=script_getnum(st,5);
  9389. y1=script_getnum(st,6);
  9390. if( (m=map_mapname2mapid(str))< 0){
  9391. script_pushint(st,-1);
  9392. return SCRIPT_CMD_SUCCESS;
  9393. }
  9394. map_foreachinarea(buildin_getareausers_sub,
  9395. m,x0,y0,x1,y1,BL_PC,&users);
  9396. script_pushint(st,users);
  9397. return SCRIPT_CMD_SUCCESS;
  9398. }
  9399. /*==========================================
  9400. *------------------------------------------*/
  9401. static int buildin_getareadropitem_sub(struct block_list *bl,va_list ap)
  9402. {
  9403. unsigned short nameid = (unsigned short)va_arg(ap, int);
  9404. unsigned short *amount = (unsigned short *)va_arg(ap, int *);
  9405. struct flooritem_data *drop=(struct flooritem_data *)bl;
  9406. if(drop->item.nameid==nameid)
  9407. (*amount)+=drop->item.amount;
  9408. return SCRIPT_CMD_SUCCESS;
  9409. }
  9410. BUILDIN_FUNC(getareadropitem)
  9411. {
  9412. const char *str;
  9413. int16 m,x0,y0,x1,y1;
  9414. unsigned short nameid, amount = 0;
  9415. struct script_data *data;
  9416. str=script_getstr(st,2);
  9417. x0=script_getnum(st,3);
  9418. y0=script_getnum(st,4);
  9419. x1=script_getnum(st,5);
  9420. y1=script_getnum(st,6);
  9421. data=script_getdata(st,7);
  9422. get_val(st,data);
  9423. if( data_isstring(data) ){
  9424. const char *name=conv_str(st,data);
  9425. struct item_data *item_data = itemdb_searchname(name);
  9426. nameid=UNKNOWN_ITEM_ID;
  9427. if( item_data )
  9428. nameid=item_data->nameid;
  9429. }else
  9430. nameid=conv_num(st,data);
  9431. if( (m=map_mapname2mapid(str))< 0){
  9432. script_pushint(st,-1);
  9433. return SCRIPT_CMD_SUCCESS;
  9434. }
  9435. map_foreachinarea(buildin_getareadropitem_sub,
  9436. m,x0,y0,x1,y1,BL_ITEM,nameid,&amount);
  9437. script_pushint(st,amount);
  9438. return SCRIPT_CMD_SUCCESS;
  9439. }
  9440. /*==========================================
  9441. *------------------------------------------*/
  9442. BUILDIN_FUNC(enablenpc)
  9443. {
  9444. const char *str;
  9445. str=script_getstr(st,2);
  9446. npc_enable(str,1);
  9447. return SCRIPT_CMD_SUCCESS;
  9448. }
  9449. /*==========================================
  9450. *------------------------------------------*/
  9451. BUILDIN_FUNC(disablenpc)
  9452. {
  9453. const char *str;
  9454. str=script_getstr(st,2);
  9455. npc_enable(str,0);
  9456. return SCRIPT_CMD_SUCCESS;
  9457. }
  9458. /*==========================================
  9459. *------------------------------------------*/
  9460. BUILDIN_FUNC(hideoffnpc)
  9461. {
  9462. const char *str;
  9463. str=script_getstr(st,2);
  9464. npc_enable(str,2);
  9465. return SCRIPT_CMD_SUCCESS;
  9466. }
  9467. /*==========================================
  9468. *------------------------------------------*/
  9469. BUILDIN_FUNC(hideonnpc)
  9470. {
  9471. const char *str;
  9472. str=script_getstr(st,2);
  9473. npc_enable(str,4);
  9474. return SCRIPT_CMD_SUCCESS;
  9475. }
  9476. /* Starts a status effect on the target unit or on the attached player.
  9477. *
  9478. * sc_start <effect_id>,<duration>,<val1>{,<rate>,<flag>,{<unit_id>}};
  9479. * sc_start2 <effect_id>,<duration>,<val1>,<val2>{,<rate,<flag>,{<unit_id>}};
  9480. * sc_start4 <effect_id>,<duration>,<val1>,<val2>,<val3>,<val4>{,<rate,<flag>,{<unit_id>}};
  9481. * <flag>: enum e_status_change_start_flags
  9482. */
  9483. BUILDIN_FUNC(sc_start)
  9484. {
  9485. TBL_NPC * nd = map_id2nd(st->oid);
  9486. struct block_list* bl;
  9487. enum sc_type type;
  9488. int tick, val1, val2, val3, val4=0, rate, flag;
  9489. char start_type;
  9490. const char* command = script_getfuncname(st);
  9491. if(strstr(command, "4"))
  9492. start_type = 4;
  9493. else if(strstr(command, "2"))
  9494. start_type = 2;
  9495. else
  9496. start_type = 1;
  9497. type = (sc_type)script_getnum(st,2);
  9498. tick = script_getnum(st,3);
  9499. val1 = script_getnum(st,4);
  9500. //If from NPC we make default flag 1 to be unavoidable
  9501. if(nd && nd->bl.id == fake_nd->bl.id)
  9502. flag = script_hasdata(st,5+start_type)?script_getnum(st,5+start_type):SCSTART_NOTICKDEF;
  9503. else
  9504. flag = script_hasdata(st,5+start_type)?script_getnum(st,5+start_type):SCSTART_NOAVOID;
  9505. rate = script_hasdata(st,4+start_type)?min(script_getnum(st,4+start_type),10000):10000;
  9506. if(script_hasdata(st,(6+start_type)))
  9507. bl = map_id2bl(script_getnum(st,(6+start_type)));
  9508. else
  9509. bl = map_id2bl(st->rid);
  9510. if(tick == 0 && val1 > 0 && type > SC_NONE && type < SC_MAX && status_sc2skill(type) != 0)
  9511. {// When there isn't a duration specified, try to get it from the skill_db
  9512. tick = skill_get_time(status_sc2skill(type), val1);
  9513. }
  9514. if(potion_flag == 1 && potion_target) { //skill.c set the flags before running the script, this is a potion-pitched effect.
  9515. bl = map_id2bl(potion_target);
  9516. tick /= 2;// Thrown potions only last half.
  9517. val4 = 1;// Mark that this was a thrown sc_effect
  9518. }
  9519. if(!bl)
  9520. return SCRIPT_CMD_SUCCESS;
  9521. switch(start_type) {
  9522. case 1:
  9523. status_change_start(bl, bl, type, rate, val1, 0, 0, val4, tick, flag);
  9524. break;
  9525. case 2:
  9526. val2 = script_getnum(st,5);
  9527. status_change_start(bl, bl, type, rate, val1, val2, 0, val4, tick, flag);
  9528. break;
  9529. case 4:
  9530. val2 = script_getnum(st,5);
  9531. val3 = script_getnum(st,6);
  9532. val4 = script_getnum(st,7);
  9533. status_change_start(bl, bl, type, rate, val1, val2, val3, val4, tick, flag);
  9534. break;
  9535. }
  9536. return SCRIPT_CMD_SUCCESS;
  9537. }
  9538. /// Ends one or all status effects on the target unit or on the attached player.
  9539. ///
  9540. /// sc_end <effect_id>{,<unit_id>};
  9541. BUILDIN_FUNC(sc_end)
  9542. {
  9543. struct block_list* bl;
  9544. int type;
  9545. type = script_getnum(st, 2);
  9546. if (script_hasdata(st, 3))
  9547. bl = map_id2bl(script_getnum(st, 3));
  9548. else
  9549. bl = map_id2bl(st->rid);
  9550. if (potion_flag == 1 && potion_target) //##TODO how does this work [FlavioJS]
  9551. bl = map_id2bl(potion_target);
  9552. if (!bl)
  9553. return SCRIPT_CMD_SUCCESS;
  9554. if (type >= 0 && type < SC_MAX) {
  9555. struct status_change *sc = status_get_sc(bl);
  9556. struct status_change_entry *sce = sc ? sc->data[type] : NULL;
  9557. if (!sce)
  9558. return SCRIPT_CMD_SUCCESS;
  9559. switch (type) {
  9560. case SC_WEIGHT50:
  9561. case SC_WEIGHT90:
  9562. case SC_NOCHAT:
  9563. case SC_PUSH_CART:
  9564. case SC_ALL_RIDING:
  9565. case SC_STYLE_CHANGE:
  9566. case SC_MONSTER_TRANSFORM:
  9567. case SC_ACTIVE_MONSTER_TRANSFORM:
  9568. case SC_MTF_ASPD:
  9569. case SC_MTF_RANGEATK:
  9570. case SC_MTF_MATK:
  9571. case SC_MTF_MLEATKED:
  9572. case SC_MTF_CRIDAMAGE:
  9573. case SC_MTF_ASPD2:
  9574. case SC_MTF_RANGEATK2:
  9575. case SC_MTF_MATK2:
  9576. case SC_MTF_MHP:
  9577. case SC_MTF_MSP:
  9578. case SC_MTF_PUMPKIN:
  9579. case SC_MTF_HITFLEE:
  9580. case SC_ATTHASTE_CASH:
  9581. case SC_REUSE_LIMIT_A: case SC_REUSE_LIMIT_B: case SC_REUSE_LIMIT_C:
  9582. case SC_REUSE_LIMIT_D: case SC_REUSE_LIMIT_E: case SC_REUSE_LIMIT_F:
  9583. case SC_REUSE_LIMIT_G: case SC_REUSE_LIMIT_H: case SC_REUSE_LIMIT_MTF:
  9584. case SC_REUSE_LIMIT_ASPD_POTION: case SC_REUSE_MILLENNIUMSHIELD: case SC_REUSE_CRUSHSTRIKE:
  9585. case SC_REUSE_STORMBLAST: case SC_ALL_RIDING_REUSE_LIMIT: case SC_REUSE_REFRESH:
  9586. case SC_REUSE_LIMIT_ECL: case SC_REUSE_LIMIT_RECALL:
  9587. return SCRIPT_CMD_SUCCESS;
  9588. default:
  9589. break;
  9590. }
  9591. //This should help status_change_end force disabling the SC in case it has no limit.
  9592. sce->val1 = sce->val2 = sce->val3 = sce->val4 = 0;
  9593. status_change_end(bl, (sc_type)type, INVALID_TIMER);
  9594. } else
  9595. status_change_clear(bl, 3); // remove all effects
  9596. return SCRIPT_CMD_SUCCESS;
  9597. }
  9598. /*==========================================
  9599. * @FIXME atm will return reduced tick, 0 immune, 1 no tick
  9600. *------------------------------------------*/
  9601. BUILDIN_FUNC(getscrate)
  9602. {
  9603. struct block_list *bl;
  9604. int type,rate;
  9605. type=script_getnum(st,2);
  9606. rate=script_getnum(st,3);
  9607. if( script_hasdata(st,4) ) //get for the bl assigned
  9608. bl = map_id2bl(script_getnum(st,4));
  9609. else
  9610. bl = map_id2bl(st->rid);
  9611. if (bl)
  9612. rate = status_get_sc_def(NULL,bl, (sc_type)type, 10000, 10000, SCSTART_NONE);
  9613. script_pushint(st,rate);
  9614. return SCRIPT_CMD_SUCCESS;
  9615. }
  9616. /**
  9617. * getstatus(<effect type>{,<type>{,<char_id>}});
  9618. **/
  9619. BUILDIN_FUNC(getstatus)
  9620. {
  9621. int id, type;
  9622. struct map_session_data* sd;
  9623. if (!script_charid2sd(4,sd))
  9624. return SCRIPT_CMD_FAILURE;
  9625. id = script_getnum(st, 2);
  9626. type = script_hasdata(st, 3) ? script_getnum(st, 3) : 0;
  9627. if( id <= SC_NONE || id >= SC_MAX )
  9628. {// invalid status type given
  9629. ShowWarning("script.c:getstatus: Invalid status type given (%d).\n", id);
  9630. return SCRIPT_CMD_SUCCESS;
  9631. }
  9632. if( sd->sc.count == 0 || !sd->sc.data[id] )
  9633. {// no status is active
  9634. script_pushint(st, 0);
  9635. return SCRIPT_CMD_SUCCESS;
  9636. }
  9637. switch( type )
  9638. {
  9639. case 1: script_pushint(st, sd->sc.data[id]->val1); break;
  9640. case 2: script_pushint(st, sd->sc.data[id]->val2); break;
  9641. case 3: script_pushint(st, sd->sc.data[id]->val3); break;
  9642. case 4: script_pushint(st, sd->sc.data[id]->val4); break;
  9643. case 5:
  9644. {
  9645. struct TimerData* timer = (struct TimerData*)get_timer(sd->sc.data[id]->timer);
  9646. if( timer )
  9647. {// return the amount of time remaining
  9648. script_pushint(st, timer->tick - gettick());
  9649. } else {
  9650. script_pushint(st, -1);
  9651. }
  9652. }
  9653. break;
  9654. default: script_pushint(st, 1); break;
  9655. }
  9656. return SCRIPT_CMD_SUCCESS;
  9657. }
  9658. /*==========================================
  9659. *
  9660. *------------------------------------------*/
  9661. BUILDIN_FUNC(debugmes)
  9662. {
  9663. const char *str;
  9664. str=script_getstr(st,2);
  9665. ShowDebug("script debug : %d %d : %s\n",st->rid,st->oid,str);
  9666. return SCRIPT_CMD_SUCCESS;
  9667. }
  9668. /*==========================================
  9669. *------------------------------------------*/
  9670. BUILDIN_FUNC(catchpet)
  9671. {
  9672. int pet_id;
  9673. TBL_PC *sd;
  9674. pet_id= script_getnum(st,2);
  9675. sd=script_rid2sd(st);
  9676. if( sd == NULL )
  9677. return SCRIPT_CMD_SUCCESS;
  9678. pet_catch_process1(sd,pet_id);
  9679. return SCRIPT_CMD_SUCCESS;
  9680. }
  9681. /*==========================================
  9682. * [orn]
  9683. *------------------------------------------*/
  9684. BUILDIN_FUNC(homunculus_evolution)
  9685. {
  9686. TBL_PC *sd;
  9687. sd=script_rid2sd(st);
  9688. if( sd == NULL )
  9689. return SCRIPT_CMD_SUCCESS;
  9690. if(hom_is_active(sd->hd))
  9691. {
  9692. if (sd->hd->homunculus.intimacy >= battle_config.homunculus_evo_intimacy_need)
  9693. hom_evolution(sd->hd);
  9694. else
  9695. clif_emotion(&sd->hd->bl, E_SWT);
  9696. }
  9697. return SCRIPT_CMD_SUCCESS;
  9698. }
  9699. /*==========================================
  9700. * Checks for vaporized morph state
  9701. * and deletes ITEMID_STRANGE_EMBRYO.
  9702. *------------------------------------------*/
  9703. BUILDIN_FUNC(homunculus_mutate)
  9704. {
  9705. int homun_id;
  9706. TBL_PC *sd;
  9707. sd = script_rid2sd(st);
  9708. if( sd == NULL || sd->hd == NULL )
  9709. return SCRIPT_CMD_SUCCESS;
  9710. if(script_hasdata(st,2))
  9711. homun_id = script_getnum(st,2);
  9712. else
  9713. homun_id = 6048 + (rnd() % 4);
  9714. if( sd->hd->homunculus.vaporize == HOM_ST_MORPH ) {
  9715. int m_class = hom_class2mapid(sd->hd->homunculus.class_);
  9716. int m_id = hom_class2mapid(homun_id);
  9717. short i = pc_search_inventory(sd, ITEMID_STRANGE_EMBRYO);
  9718. if ( m_class != -1 && m_id != -1 && m_class&HOM_EVO && m_id&HOM_S && sd->hd->homunculus.level >= 99 && i >= 0 ) {
  9719. sd->hd->homunculus.vaporize = HOM_ST_REST; // Remove morph state.
  9720. hom_call(sd); // Respawn homunculus.
  9721. hom_mutate(sd->hd, homun_id);
  9722. pc_delitem(sd, i, 1, 0, 0, LOG_TYPE_SCRIPT);
  9723. script_pushint(st, 1);
  9724. return SCRIPT_CMD_SUCCESS;
  9725. } else
  9726. clif_emotion(&sd->bl, E_SWT);
  9727. } else
  9728. clif_emotion(&sd->bl, E_SWT);
  9729. script_pushint(st, 0);
  9730. return SCRIPT_CMD_SUCCESS;
  9731. }
  9732. /*==========================================
  9733. * Puts homunculus into morph state
  9734. * and gives ITEMID_STRANGE_EMBRYO.
  9735. *------------------------------------------*/
  9736. BUILDIN_FUNC(morphembryo)
  9737. {
  9738. struct item item_tmp;
  9739. TBL_PC *sd;
  9740. sd = script_rid2sd(st);
  9741. if( sd == NULL || sd->hd == NULL )
  9742. return SCRIPT_CMD_SUCCESS;
  9743. if( hom_is_active(sd->hd) ) {
  9744. int m_class = hom_class2mapid(sd->hd->homunculus.class_);
  9745. if ( m_class != -1 && m_class&HOM_EVO && sd->hd->homunculus.level >= 99 ) {
  9746. char i;
  9747. memset(&item_tmp, 0, sizeof(item_tmp));
  9748. item_tmp.nameid = ITEMID_STRANGE_EMBRYO;
  9749. item_tmp.identify = 1;
  9750. if( (i = pc_additem(sd, &item_tmp, 1, LOG_TYPE_SCRIPT)) ) {
  9751. clif_additem(sd, 0, 0, i);
  9752. clif_emotion(&sd->bl, E_SWT); // Fail to avoid item drop exploit.
  9753. } else {
  9754. hom_vaporize(sd, HOM_ST_MORPH);
  9755. script_pushint(st, 1);
  9756. return SCRIPT_CMD_SUCCESS;
  9757. }
  9758. } else
  9759. clif_emotion(&sd->hd->bl, E_SWT);
  9760. } else
  9761. clif_emotion(&sd->bl, E_SWT);
  9762. script_pushint(st, 0);
  9763. return SCRIPT_CMD_SUCCESS;
  9764. }
  9765. // [Zephyrus]
  9766. BUILDIN_FUNC(homunculus_shuffle)
  9767. {
  9768. TBL_PC *sd;
  9769. sd=script_rid2sd(st);
  9770. if( sd == NULL )
  9771. return SCRIPT_CMD_SUCCESS;
  9772. if(hom_is_active(sd->hd))
  9773. hom_shuffle(sd->hd);
  9774. return SCRIPT_CMD_SUCCESS;
  9775. }
  9776. /*==========================================
  9777. * Check for homunculus state.
  9778. * Return: -1 = No homunculus
  9779. * 0 = Homunculus is active
  9780. * 1 = Homunculus is vaporized (rest)
  9781. * 2 = Homunculus is in morph state
  9782. *------------------------------------------*/
  9783. BUILDIN_FUNC(checkhomcall)
  9784. {
  9785. TBL_PC *sd = script_rid2sd(st);
  9786. TBL_HOM *hd;
  9787. if( sd == NULL )
  9788. return SCRIPT_CMD_SUCCESS;
  9789. hd = sd->hd;
  9790. if( !hd )
  9791. script_pushint(st, -1);
  9792. else
  9793. script_pushint(st, hd->homunculus.vaporize);
  9794. return SCRIPT_CMD_SUCCESS;
  9795. }
  9796. //These two functions bring the eA MAPID_* class functionality to scripts.
  9797. BUILDIN_FUNC(eaclass)
  9798. {
  9799. int class_;
  9800. if( script_hasdata(st,2) )
  9801. class_ = script_getnum(st,2);
  9802. else {
  9803. TBL_PC *sd;
  9804. if (!script_charid2sd(3,sd)) {
  9805. script_pushint(st,-1);
  9806. return SCRIPT_CMD_SUCCESS;
  9807. }
  9808. class_ = sd->status.class_;
  9809. }
  9810. script_pushint(st,pc_jobid2mapid(class_));
  9811. return SCRIPT_CMD_SUCCESS;
  9812. }
  9813. BUILDIN_FUNC(roclass)
  9814. {
  9815. int class_ =script_getnum(st,2);
  9816. int sex;
  9817. if( script_hasdata(st,3) )
  9818. sex = script_getnum(st,3);
  9819. else {
  9820. TBL_PC *sd;
  9821. if (st->rid && (sd=script_rid2sd(st)))
  9822. sex = sd->status.sex;
  9823. else
  9824. sex = 1; //Just use male when not found.
  9825. }
  9826. script_pushint(st,pc_mapid2jobid(class_, sex));
  9827. return SCRIPT_CMD_SUCCESS;
  9828. }
  9829. /*==========================================
  9830. * Tells client to open a hatching window, used for pet incubator
  9831. *------------------------------------------*/
  9832. BUILDIN_FUNC(birthpet)
  9833. {
  9834. TBL_PC *sd;
  9835. sd=script_rid2sd(st);
  9836. if( sd == NULL )
  9837. return SCRIPT_CMD_SUCCESS;
  9838. if( sd->status.pet_id )
  9839. {// do not send egg list, when you already have a pet
  9840. return SCRIPT_CMD_SUCCESS;
  9841. }
  9842. clif_sendegg(sd);
  9843. return SCRIPT_CMD_SUCCESS;
  9844. }
  9845. /**
  9846. * resetlvl <action type>{,<char_id>};
  9847. * @param action_type:
  9848. * 1 : make like after rebirth
  9849. * 2 : blvl,jlvl=1, skillpoint=0
  9850. * 3 : don't reset skill, blvl=1
  9851. * 4 : jlvl=0
  9852. * @author AppleGirl
  9853. **/
  9854. BUILDIN_FUNC(resetlvl)
  9855. {
  9856. TBL_PC *sd;
  9857. int type=script_getnum(st,2);
  9858. if (!script_charid2sd(3,sd))
  9859. return SCRIPT_CMD_FAILURE;
  9860. pc_resetlvl(sd,type);
  9861. return SCRIPT_CMD_SUCCESS;
  9862. }
  9863. /**
  9864. * Reset a player status point
  9865. * resetstatus({<char_id>});
  9866. **/
  9867. BUILDIN_FUNC(resetstatus)
  9868. {
  9869. TBL_PC *sd;
  9870. if (!script_charid2sd(2,sd))
  9871. return SCRIPT_CMD_FAILURE;
  9872. pc_resetstate(sd);
  9873. return SCRIPT_CMD_SUCCESS;
  9874. }
  9875. /**
  9876. * Reset player's skill
  9877. * resetskill({<char_id>});
  9878. **/
  9879. BUILDIN_FUNC(resetskill)
  9880. {
  9881. TBL_PC *sd;
  9882. if (!script_charid2sd(2,sd))
  9883. return SCRIPT_CMD_FAILURE;
  9884. pc_resetskill(sd,1);
  9885. return SCRIPT_CMD_SUCCESS;
  9886. }
  9887. /**
  9888. * Counts total amount of skill points.
  9889. * skillpointcount({<char_id>})
  9890. **/
  9891. BUILDIN_FUNC(skillpointcount)
  9892. {
  9893. TBL_PC *sd;
  9894. if (!script_charid2sd(2,sd))
  9895. return SCRIPT_CMD_FAILURE;
  9896. script_pushint(st,sd->status.skill_point + pc_resetskill(sd,2));
  9897. return SCRIPT_CMD_SUCCESS;
  9898. }
  9899. /*==========================================
  9900. *
  9901. *------------------------------------------*/
  9902. BUILDIN_FUNC(changebase)
  9903. {
  9904. TBL_PC *sd=NULL;
  9905. int vclass;
  9906. if( script_hasdata(st,3) )
  9907. sd=map_id2sd(script_getnum(st,3));
  9908. else
  9909. sd=script_rid2sd(st);
  9910. if(sd == NULL)
  9911. return SCRIPT_CMD_SUCCESS;
  9912. vclass = script_getnum(st,2);
  9913. if(vclass == JOB_WEDDING)
  9914. {
  9915. if (!battle_config.wedding_modifydisplay || //Do not show the wedding sprites
  9916. sd->class_&JOBL_BABY //Baby classes screw up when showing wedding sprites. [Skotlex] They don't seem to anymore.
  9917. )
  9918. return SCRIPT_CMD_SUCCESS;
  9919. }
  9920. if(!sd->disguise && vclass != sd->vd.class_) {
  9921. status_set_viewdata(&sd->bl, vclass);
  9922. //Updated client view. Base, Weapon and Cloth Colors.
  9923. clif_changelook(&sd->bl,LOOK_BASE,sd->vd.class_);
  9924. clif_changelook(&sd->bl,LOOK_WEAPON,sd->status.weapon);
  9925. if (sd->vd.cloth_color)
  9926. clif_changelook(&sd->bl,LOOK_CLOTHES_COLOR,sd->vd.cloth_color);
  9927. if (sd->vd.body_style)
  9928. clif_changelook(&sd->bl,LOOK_BODY2,sd->vd.body_style);
  9929. clif_skillinfoblock(sd);
  9930. }
  9931. return SCRIPT_CMD_SUCCESS;
  9932. }
  9933. /**
  9934. * Change account sex and unequip all item and request for a changesex to char-serv
  9935. * changesex({<char_id>});
  9936. */
  9937. BUILDIN_FUNC(changesex)
  9938. {
  9939. int i;
  9940. TBL_PC *sd = NULL;
  9941. if (!script_charid2sd(2,sd))
  9942. return SCRIPT_CMD_FAILURE;
  9943. pc_resetskill(sd,4);
  9944. // to avoid any problem with equipment and invalid sex, equipment is unequiped.
  9945. for(i = 0; i < EQI_MAX; i++) {
  9946. if (sd->equip_index[i] >= 0)
  9947. pc_unequipitem(sd, sd->equip_index[i], 3);
  9948. }
  9949. chrif_changesex(sd, true);
  9950. return SCRIPT_CMD_SUCCESS;
  9951. }
  9952. /**
  9953. * Change character's sex and unequip all item and request for a changesex to char-serv
  9954. * changecharsex({<char_id>});
  9955. */
  9956. BUILDIN_FUNC(changecharsex)
  9957. {
  9958. #if PACKETVER >= 20141016
  9959. int i;
  9960. TBL_PC *sd = NULL;
  9961. if (!script_charid2sd(2,sd))
  9962. return SCRIPT_CMD_FAILURE;
  9963. pc_resetskill(sd,4);
  9964. // to avoid any problem with equipment and invalid sex, equipment is unequiped.
  9965. for (i = 0; i < EQI_MAX; i++) {
  9966. if (sd->equip_index[i] >= 0)
  9967. pc_unequipitem(sd, sd->equip_index[i], 3);
  9968. }
  9969. chrif_changesex(sd, false);
  9970. return SCRIPT_CMD_SUCCESS;
  9971. #else
  9972. return SCRIPT_CMD_FAILURE;
  9973. #endif
  9974. }
  9975. /*==========================================
  9976. * Works like 'announce' but outputs in the common chat window
  9977. *------------------------------------------*/
  9978. BUILDIN_FUNC(globalmes)
  9979. {
  9980. struct block_list *bl = map_id2bl(st->oid);
  9981. struct npc_data *nd = (struct npc_data *)bl;
  9982. const char *name=NULL,*mes;
  9983. mes=script_getstr(st,2);
  9984. if(mes==NULL)
  9985. return SCRIPT_CMD_SUCCESS;
  9986. if(script_hasdata(st,3)){ // npc name to display
  9987. name=script_getstr(st,3);
  9988. } else {
  9989. name=nd->name; //use current npc name
  9990. }
  9991. npc_globalmessage(name,mes); // broadcast to all players connected
  9992. return SCRIPT_CMD_SUCCESS;
  9993. }
  9994. /////////////////////////////////////////////////////////////////////
  9995. // NPC waiting room (chat room)
  9996. //
  9997. /// Creates a waiting room (chat room) for this npc.
  9998. ///
  9999. /// waitingroom "<title>",<limit>{,"<event>"{,<trigger>{,<zeny>{,<minlvl>{,<maxlvl>}}}}};
  10000. BUILDIN_FUNC(waitingroom)
  10001. {
  10002. struct npc_data* nd;
  10003. const char* title = script_getstr(st, 2);
  10004. int limit = script_getnum(st, 3);
  10005. const char* ev = script_hasdata(st,4) ? script_getstr(st,4) : "";
  10006. int trigger = script_hasdata(st,5) ? script_getnum(st,5) : limit;
  10007. int zeny = script_hasdata(st,6) ? script_getnum(st,6) : 0;
  10008. int minLvl = script_hasdata(st,7) ? script_getnum(st,7) : 1;
  10009. int maxLvl = script_hasdata(st,8) ? script_getnum(st,8) : MAX_LEVEL;
  10010. nd = (struct npc_data *)map_id2bl(st->oid);
  10011. if( nd != NULL )
  10012. chat_createnpcchat(nd, title, limit, 1, trigger, ev, zeny, minLvl, maxLvl);
  10013. return SCRIPT_CMD_SUCCESS;
  10014. }
  10015. /// Removes the waiting room of the current or target npc.
  10016. ///
  10017. /// delwaitingroom "<npc_name>";
  10018. /// delwaitingroom;
  10019. BUILDIN_FUNC(delwaitingroom)
  10020. {
  10021. struct npc_data* nd;
  10022. if( script_hasdata(st,2) )
  10023. nd = npc_name2id(script_getstr(st, 2));
  10024. else
  10025. nd = (struct npc_data *)map_id2bl(st->oid);
  10026. if( nd != NULL )
  10027. chat_deletenpcchat(nd);
  10028. return SCRIPT_CMD_SUCCESS;
  10029. }
  10030. /// Kick the specified player from the waiting room of the target npc.
  10031. ///
  10032. /// waitingroomkick "<npc_name>", <kickusername>;
  10033. BUILDIN_FUNC(waitingroomkick)
  10034. {
  10035. struct npc_data* nd;
  10036. struct chat_data* cd;
  10037. const char* kickusername;
  10038. nd = npc_name2id(script_getstr(st,2));
  10039. kickusername = script_getstr(st,3);
  10040. if( nd != NULL && (cd=(struct chat_data *)map_id2bl(nd->chat_id)) != NULL )
  10041. chat_npckickchat(cd, kickusername);
  10042. return SCRIPT_CMD_SUCCESS;
  10043. }
  10044. /// Get Users in waiting room and stores gids in .@waitingroom_users[]
  10045. /// Num users stored in .@waitingroom_usercount
  10046. ///
  10047. /// getwaitingroomusers "<npc_name>";
  10048. BUILDIN_FUNC(getwaitingroomusers)
  10049. {
  10050. struct npc_data* nd;
  10051. struct chat_data* cd;
  10052. int i, j=0;
  10053. if( script_hasdata(st,2) )
  10054. nd = npc_name2id(script_getstr(st, 2));
  10055. else
  10056. nd = (struct npc_data *)map_id2bl(st->oid);
  10057. if( nd != NULL && (cd=(struct chat_data *)map_id2bl(nd->chat_id)) != NULL ) {
  10058. for(i = 0; i < cd->users; ++i) {
  10059. setd_sub(st, NULL, ".@waitingroom_users", j, (void *)__64BPRTSIZE(cd->usersd[i]->status.account_id), NULL);
  10060. j++;
  10061. }
  10062. setd_sub(st, NULL, ".@waitingroom_usercount", 0, (void *)__64BPRTSIZE(j), NULL);
  10063. }
  10064. return SCRIPT_CMD_SUCCESS;
  10065. }
  10066. /// Kicks all the players from the waiting room of the current or target npc.
  10067. ///
  10068. /// kickwaitingroomall "<npc_name>";
  10069. /// kickwaitingroomall;
  10070. BUILDIN_FUNC(waitingroomkickall)
  10071. {
  10072. struct npc_data* nd;
  10073. struct chat_data* cd;
  10074. if( script_hasdata(st,2) )
  10075. nd = npc_name2id(script_getstr(st,2));
  10076. else
  10077. nd = (struct npc_data *)map_id2bl(st->oid);
  10078. if( nd != NULL && (cd=(struct chat_data *)map_id2bl(nd->chat_id)) != NULL )
  10079. chat_npckickall(cd);
  10080. return SCRIPT_CMD_SUCCESS;
  10081. }
  10082. /// Enables the waiting room event of the current or target npc.
  10083. ///
  10084. /// enablewaitingroomevent "<npc_name>";
  10085. /// enablewaitingroomevent;
  10086. BUILDIN_FUNC(enablewaitingroomevent)
  10087. {
  10088. struct npc_data* nd;
  10089. struct chat_data* cd;
  10090. if( script_hasdata(st,2) )
  10091. nd = npc_name2id(script_getstr(st, 2));
  10092. else
  10093. nd = (struct npc_data *)map_id2bl(st->oid);
  10094. if( nd != NULL && (cd=(struct chat_data *)map_id2bl(nd->chat_id)) != NULL )
  10095. chat_enableevent(cd);
  10096. return SCRIPT_CMD_SUCCESS;
  10097. }
  10098. /// Disables the waiting room event of the current or target npc.
  10099. ///
  10100. /// disablewaitingroomevent "<npc_name>";
  10101. /// disablewaitingroomevent;
  10102. BUILDIN_FUNC(disablewaitingroomevent)
  10103. {
  10104. struct npc_data *nd;
  10105. struct chat_data *cd;
  10106. if( script_hasdata(st,2) )
  10107. nd = npc_name2id(script_getstr(st, 2));
  10108. else
  10109. nd = (struct npc_data *)map_id2bl(st->oid);
  10110. if( nd != NULL && (cd=(struct chat_data *)map_id2bl(nd->chat_id)) != NULL )
  10111. chat_disableevent(cd);
  10112. return SCRIPT_CMD_SUCCESS;
  10113. }
  10114. /// Returns info on the waiting room of the current or target npc.
  10115. /// Returns -1 if the type unknown
  10116. /// <type>=0 : current number of users
  10117. /// <type>=1 : maximum number of users allowed
  10118. /// <type>=2 : the number of users that trigger the event
  10119. /// <type>=3 : if the trigger is disabled
  10120. /// <type>=4 : the title of the waiting room
  10121. /// <type>=5 : the password of the waiting room
  10122. /// <type>=16 : the name of the waiting room event
  10123. /// <type>=32 : if the waiting room is full
  10124. /// <type>=33 : if there are enough users to trigger the event
  10125. ///
  10126. /// getwaitingroomstate(<type>,"<npc_name>") -> <info>
  10127. /// getwaitingroomstate(<type>) -> <info>
  10128. BUILDIN_FUNC(getwaitingroomstate)
  10129. {
  10130. struct npc_data *nd;
  10131. struct chat_data *cd;
  10132. int type;
  10133. type = script_getnum(st,2);
  10134. if( script_hasdata(st,3) )
  10135. nd = npc_name2id(script_getstr(st, 3));
  10136. else
  10137. nd = (struct npc_data *)map_id2bl(st->oid);
  10138. if( nd == NULL || (cd=(struct chat_data *)map_id2bl(nd->chat_id)) == NULL )
  10139. {
  10140. script_pushint(st, -1);
  10141. return SCRIPT_CMD_SUCCESS;
  10142. }
  10143. switch(type)
  10144. {
  10145. case 0: script_pushint(st, cd->users); break;
  10146. case 1: script_pushint(st, cd->limit); break;
  10147. case 2: script_pushint(st, cd->trigger&0x7f); break;
  10148. case 3: script_pushint(st, ((cd->trigger&0x80)!=0)); break;
  10149. case 4: script_pushstrcopy(st, cd->title); break;
  10150. case 5: script_pushstrcopy(st, cd->pass); break;
  10151. case 16: script_pushstrcopy(st, cd->npc_event);break;
  10152. case 32: script_pushint(st, (cd->users >= cd->limit)); break;
  10153. case 33: script_pushint(st, (cd->users >= cd->trigger)); break;
  10154. default: script_pushint(st, -1); break;
  10155. }
  10156. return SCRIPT_CMD_SUCCESS;
  10157. }
  10158. /// Warps the trigger or target amount of players to the target map and position.
  10159. /// Players are automatically removed from the waiting room.
  10160. /// Those waiting the longest will get warped first.
  10161. /// The target map can be "Random" for a random position in the current map,
  10162. /// and "SavePoint" for the savepoint map+position.
  10163. /// The map flag noteleport of the current map is only considered when teleporting to the savepoint.
  10164. ///
  10165. /// The id's of the teleported players are put into the array $@warpwaitingpc[]
  10166. /// The total number of teleported players is put into $@warpwaitingpcnum
  10167. ///
  10168. /// warpwaitingpc "<map name>",<x>,<y>,<number of players>;
  10169. /// warpwaitingpc "<map name>",<x>,<y>;
  10170. BUILDIN_FUNC(warpwaitingpc)
  10171. {
  10172. int x;
  10173. int y;
  10174. int i;
  10175. int n;
  10176. const char* map_name;
  10177. struct npc_data* nd;
  10178. struct chat_data* cd;
  10179. nd = (struct npc_data *)map_id2bl(st->oid);
  10180. if( nd == NULL || (cd=(struct chat_data *)map_id2bl(nd->chat_id)) == NULL )
  10181. return SCRIPT_CMD_SUCCESS;
  10182. map_name = script_getstr(st,2);
  10183. x = script_getnum(st,3);
  10184. y = script_getnum(st,4);
  10185. n = cd->trigger&0x7f;
  10186. if( script_hasdata(st,5) )
  10187. n = script_getnum(st,5);
  10188. for( i = 0; i < n && cd->users > 0; i++ )
  10189. {
  10190. TBL_PC* sd = cd->usersd[0];
  10191. if( strcmp(map_name,"SavePoint") == 0 && map[sd->bl.m].flag.noteleport )
  10192. {// can't teleport on this map
  10193. break;
  10194. }
  10195. if( cd->zeny )
  10196. {// fee set
  10197. if( (uint32)sd->status.zeny < cd->zeny )
  10198. {// no zeny to cover set fee
  10199. break;
  10200. }
  10201. pc_payzeny(sd, cd->zeny, LOG_TYPE_NPC, NULL);
  10202. }
  10203. mapreg_setreg(reference_uid(add_str("$@warpwaitingpc"), i), sd->bl.id);
  10204. if( strcmp(map_name,"Random") == 0 )
  10205. pc_randomwarp(sd,CLR_TELEPORT);
  10206. else if( strcmp(map_name,"SavePoint") == 0 )
  10207. pc_setpos(sd, sd->status.save_point.map, sd->status.save_point.x, sd->status.save_point.y, CLR_TELEPORT);
  10208. else
  10209. pc_setpos(sd, mapindex_name2id(map_name), x, y, CLR_OUTSIGHT);
  10210. }
  10211. mapreg_setreg(add_str("$@warpwaitingpcnum"), i);
  10212. return SCRIPT_CMD_SUCCESS;
  10213. }
  10214. /////////////////////////////////////////////////////////////////////
  10215. // ...
  10216. //
  10217. /// Detaches a character from a script.
  10218. ///
  10219. /// @param st Script state to detach the character from.
  10220. static void script_detach_rid(struct script_state* st)
  10221. {
  10222. if(st->rid)
  10223. {
  10224. script_detach_state(st, false);
  10225. st->rid = 0;
  10226. }
  10227. }
  10228. /*=========================================================================
  10229. * Attaches a set of RIDs to the current script. [digitalhamster]
  10230. * addrid(<type>{,<flag>{,<parameters>}});
  10231. * <type>:
  10232. * 0 : All players in the server.
  10233. * 1 : All players in the map of the invoking player, or the invoking NPC if no player is attached.
  10234. * 2 : Party members of a specified party ID.
  10235. * [ Parameters: <party id> ]
  10236. * 3 : Guild members of a specified guild ID.
  10237. * [ Parameters: <guild id> ]
  10238. * 4 : All players in a specified area of the map of the invoking player (or NPC).
  10239. * [ Parameters: <x0>,<y0>,<x1>,<y1> ]
  10240. * 5 : All players in the map.
  10241. * [ Parameters: "<map name>" ]
  10242. * Account ID: The specified account ID.
  10243. * <flag>:
  10244. * 0 : Players are always attached. (default)
  10245. * 1 : Players currently running another script will not be attached.
  10246. *-------------------------------------------------------------------------*/
  10247. static int buildin_addrid_sub(struct block_list *bl,va_list ap)
  10248. {
  10249. int forceflag;
  10250. struct map_session_data *sd = (TBL_PC *)bl;
  10251. struct script_state* st;
  10252. st = va_arg(ap,struct script_state*);
  10253. forceflag = va_arg(ap,int);
  10254. if(!forceflag || !sd->st)
  10255. if(sd->status.account_id != st->rid)
  10256. run_script(st->script,st->pos,sd->status.account_id,st->oid);
  10257. return 0;
  10258. }
  10259. BUILDIN_FUNC(addrid)
  10260. {
  10261. struct s_mapiterator* iter;
  10262. struct block_list *bl;
  10263. TBL_PC *sd;
  10264. if(st->rid < 1) {
  10265. st->state = END;
  10266. bl = map_id2bl(st->oid);
  10267. } else
  10268. bl = map_id2bl(st->rid); //if run without rid it'd error,also oid if npc, else rid for map
  10269. iter = mapit_getallusers();
  10270. switch(script_getnum(st,2)) {
  10271. case 0:
  10272. for( sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); sd = (TBL_PC*)mapit_next(iter)) {
  10273. if(!script_getnum(st,3) || !sd->st)
  10274. if(sd->status.account_id != st->rid) //attached player already runs.
  10275. run_script(st->script,st->pos,sd->status.account_id,st->oid);
  10276. }
  10277. break;
  10278. case 1:
  10279. for( sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); sd = (TBL_PC*)mapit_next(iter)) {
  10280. if(!script_getnum(st,3) || !sd->st)
  10281. if((sd->bl.m == bl->m) && (sd->status.account_id != st->rid))
  10282. run_script(st->script,st->pos,sd->status.account_id,st->oid);
  10283. }
  10284. break;
  10285. case 2:
  10286. if(script_getnum(st,4) == 0) {
  10287. script_pushint(st,0);
  10288. return SCRIPT_CMD_SUCCESS;
  10289. }
  10290. for( sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); sd = (TBL_PC*)mapit_next(iter)) {
  10291. if(!script_getnum(st,3) || !sd->st)
  10292. if((sd->status.account_id != st->rid) && (sd->status.party_id == script_getnum(st,4))) //attached player already runs.
  10293. run_script(st->script,st->pos,sd->status.account_id,st->oid);
  10294. }
  10295. break;
  10296. case 3:
  10297. if(script_getnum(st,4) == 0) {
  10298. script_pushint(st,0);
  10299. return SCRIPT_CMD_SUCCESS;
  10300. }
  10301. for( sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); sd = (TBL_PC*)mapit_next(iter)) {
  10302. if(!script_getnum(st,3) || !sd->st)
  10303. if((sd->status.account_id != st->rid) && (sd->status.guild_id == script_getnum(st,4))) //attached player already runs.
  10304. run_script(st->script,st->pos,sd->status.account_id,st->oid);
  10305. }
  10306. break;
  10307. case 4:
  10308. map_foreachinarea(buildin_addrid_sub,
  10309. bl->m,script_getnum(st,4),script_getnum(st,5),script_getnum(st,6),script_getnum(st,7),BL_PC,
  10310. st,script_getnum(st,3));//4-x0 , 5-y0 , 6-x1, 7-y1
  10311. break;
  10312. case 5:
  10313. if (script_getstr(st, 4) == NULL) {
  10314. script_pushint(st, 0);
  10315. return SCRIPT_CMD_FAILURE;
  10316. }
  10317. if (map_mapname2mapid(script_getstr(st, 4)) < 0) {
  10318. script_pushint(st, 0);
  10319. return SCRIPT_CMD_FAILURE;
  10320. }
  10321. map_foreachinmap(buildin_addrid_sub, map_mapname2mapid(script_getstr(st, 4)), BL_PC, st, script_getnum(st, 3));
  10322. break;
  10323. default:
  10324. if((map_id2sd(script_getnum(st,2))) == NULL) { // Player not found.
  10325. script_pushint(st,0);
  10326. return SCRIPT_CMD_SUCCESS;
  10327. }
  10328. if(!script_getnum(st,3) || !map_id2sd(script_getnum(st,2))->st) {
  10329. run_script(st->script,st->pos,script_getnum(st,2),st->oid);
  10330. script_pushint(st,1);
  10331. }
  10332. return SCRIPT_CMD_SUCCESS;
  10333. }
  10334. mapit_free(iter);
  10335. script_pushint(st,1);
  10336. return SCRIPT_CMD_SUCCESS;
  10337. }
  10338. /*==========================================
  10339. * Attach sd char id to script and detach current one if any
  10340. *------------------------------------------*/
  10341. BUILDIN_FUNC(attachrid)
  10342. {
  10343. int rid = script_getnum(st,2);
  10344. if (map_id2sd(rid) != NULL) {
  10345. script_detach_rid(st);
  10346. st->rid = rid;
  10347. script_attach_state(st);
  10348. script_pushint(st,1);
  10349. } else
  10350. script_pushint(st,0);
  10351. return SCRIPT_CMD_SUCCESS;
  10352. }
  10353. /*==========================================
  10354. * Detach script to rid
  10355. *------------------------------------------*/
  10356. BUILDIN_FUNC(detachrid)
  10357. {
  10358. script_detach_rid(st);
  10359. return SCRIPT_CMD_SUCCESS;
  10360. }
  10361. /*==========================================
  10362. * Chk if account connected, (and charid from account if specified)
  10363. *------------------------------------------*/
  10364. BUILDIN_FUNC(isloggedin)
  10365. {
  10366. TBL_PC* sd = map_id2sd(script_getnum(st,2));
  10367. if (script_hasdata(st,3) && sd &&
  10368. sd->status.char_id != script_getnum(st,3))
  10369. sd = NULL;
  10370. push_val(st->stack,C_INT,sd!=NULL);
  10371. return SCRIPT_CMD_SUCCESS;
  10372. }
  10373. /*==========================================
  10374. *
  10375. *------------------------------------------*/
  10376. BUILDIN_FUNC(setmapflagnosave)
  10377. {
  10378. int16 m,x,y;
  10379. unsigned short mapindex;
  10380. const char *str,*str2;
  10381. str=script_getstr(st,2);
  10382. str2=script_getstr(st,3);
  10383. x=script_getnum(st,4);
  10384. y=script_getnum(st,5);
  10385. m = map_mapname2mapid(str);
  10386. mapindex = mapindex_name2id(str2);
  10387. if(m >= 0 && mapindex) {
  10388. map[m].flag.nosave=1;
  10389. map[m].save.map=mapindex;
  10390. map[m].save.x=x;
  10391. map[m].save.y=y;
  10392. }
  10393. return SCRIPT_CMD_SUCCESS;
  10394. }
  10395. BUILDIN_FUNC(getmapflag)
  10396. {
  10397. int16 m,i;
  10398. const char *str;
  10399. str=script_getstr(st,2);
  10400. i=script_getnum(st,3);
  10401. m = map_mapname2mapid(str);
  10402. if(m >= 0) {
  10403. switch(i) {
  10404. case MF_NOMEMO: script_pushint(st,map[m].flag.nomemo); break;
  10405. case MF_NOTELEPORT: script_pushint(st,map[m].flag.noteleport); break;
  10406. case MF_NOSAVE: script_pushint(st,map[m].flag.nosave); break;
  10407. case MF_NOBRANCH: script_pushint(st,map[m].flag.nobranch); break;
  10408. case MF_NOPENALTY: script_pushint(st,map[m].flag.noexppenalty); break;
  10409. case MF_NOZENYPENALTY: script_pushint(st,map[m].flag.nozenypenalty); break;
  10410. case MF_PVP: script_pushint(st,map[m].flag.pvp); break;
  10411. case MF_PVP_NOPARTY: script_pushint(st,map[m].flag.pvp_noparty); break;
  10412. case MF_PVP_NOGUILD: script_pushint(st,map[m].flag.pvp_noguild); break;
  10413. case MF_GVG: script_pushint(st,map[m].flag.gvg); break;
  10414. case MF_GVG_NOPARTY: script_pushint(st,map[m].flag.gvg_noparty); break;
  10415. case MF_NOTRADE: script_pushint(st,map[m].flag.notrade); break;
  10416. case MF_NOSKILL: script_pushint(st,map[m].flag.noskill); break;
  10417. case MF_NOWARP: script_pushint(st,map[m].flag.nowarp); break;
  10418. case MF_PARTYLOCK: script_pushint(st,map[m].flag.partylock); break;
  10419. case MF_NOICEWALL: script_pushint(st,map[m].flag.noicewall); break;
  10420. case MF_SNOW: script_pushint(st,map[m].flag.snow); break;
  10421. case MF_FOG: script_pushint(st,map[m].flag.fog); break;
  10422. case MF_SAKURA: script_pushint(st,map[m].flag.sakura); break;
  10423. case MF_LEAVES: script_pushint(st,map[m].flag.leaves); break;
  10424. case MF_NOGO: script_pushint(st,map[m].flag.nogo); break;
  10425. case MF_CLOUDS: script_pushint(st,map[m].flag.clouds); break;
  10426. case MF_CLOUDS2: script_pushint(st,map[m].flag.clouds2); break;
  10427. case MF_FIREWORKS: script_pushint(st,map[m].flag.fireworks); break;
  10428. case MF_GVG_CASTLE: script_pushint(st,map[m].flag.gvg_castle); break;
  10429. case MF_GVG_DUNGEON: script_pushint(st,map[m].flag.gvg_dungeon); break;
  10430. case MF_NIGHTENABLED: script_pushint(st,map[m].flag.nightenabled); break;
  10431. case MF_NOBASEEXP: script_pushint(st,map[m].flag.nobaseexp); break;
  10432. case MF_NOJOBEXP: script_pushint(st,map[m].flag.nojobexp); break;
  10433. case MF_NOMOBLOOT: script_pushint(st,map[m].flag.nomobloot); break;
  10434. case MF_NOMVPLOOT: script_pushint(st,map[m].flag.nomvploot); break;
  10435. case MF_NORETURN: script_pushint(st,map[m].flag.noreturn); break;
  10436. case MF_NOWARPTO: script_pushint(st,map[m].flag.nowarpto); break;
  10437. case MF_NIGHTMAREDROP: script_pushint(st,map[m].flag.pvp_nightmaredrop); break;
  10438. case MF_RESTRICTED: script_pushint(st,map[m].flag.restricted); break;
  10439. case MF_NOCOMMAND: script_pushint(st,map[m].nocommand); break;
  10440. case MF_NODROP: script_pushint(st,map[m].flag.nodrop); break;
  10441. case MF_JEXP: script_pushint(st,map[m].adjust.jexp); break;
  10442. case MF_BEXP: script_pushint(st,map[m].adjust.bexp); break;
  10443. case MF_NOVENDING: script_pushint(st,map[m].flag.novending); break;
  10444. case MF_LOADEVENT: script_pushint(st,map[m].flag.loadevent); break;
  10445. case MF_NOCHAT: script_pushint(st,map[m].flag.nochat); break;
  10446. case MF_NOEXPPENALTY: script_pushint(st,map[m].flag.noexppenalty ); break;
  10447. case MF_GUILDLOCK: script_pushint(st,map[m].flag.guildlock); break;
  10448. case MF_TOWN: script_pushint(st,map[m].flag.town); break;
  10449. case MF_AUTOTRADE: script_pushint(st,map[m].flag.autotrade); break;
  10450. case MF_ALLOWKS: script_pushint(st,map[m].flag.allowks); break;
  10451. case MF_MONSTER_NOTELEPORT: script_pushint(st,map[m].flag.monster_noteleport); break;
  10452. case MF_PVP_NOCALCRANK: script_pushint(st,map[m].flag.pvp_nocalcrank); break;
  10453. case MF_BATTLEGROUND: script_pushint(st,map[m].flag.battleground); break;
  10454. case MF_RESET: script_pushint(st,map[m].flag.reset); break;
  10455. case MF_CHANNELAUTOJOIN: script_pushint(st,map[m].flag.chmautojoin); break;
  10456. case MF_NOUSECART: script_pushint(st,map[m].flag.nousecart); break;
  10457. case MF_NOITEMCONSUMPTION: script_pushint(st,map[m].flag.noitemconsumption); break;
  10458. case MF_SUMSTARTMIRACLE: script_pushint(st,map[m].flag.nosumstarmiracle); break;
  10459. case MF_NOMINEEFFECT: script_pushint(st,map[m].flag.nomineeffect); break;
  10460. case MF_NOLOCKON: script_pushint(st,map[m].flag.nolockon); break;
  10461. case MF_NOTOMB: script_pushint(st,map[m].flag.notomb); break;
  10462. case MF_NOCOSTUME: script_pushint(st,map[m].flag.nocostume); break;
  10463. case MF_GVG_TE_CASTLE: script_pushint(st,map[m].flag.gvg_te_castle); break;
  10464. case MF_GVG_TE: script_pushint(st,map[m].flag.gvg_te); break;
  10465. case MF_HIDEMOBHPBAR: script_pushint(st,map[m].flag.hidemobhpbar); break;
  10466. #ifdef ADJUST_SKILL_DAMAGE
  10467. case MF_SKILL_DAMAGE:
  10468. {
  10469. int ret_val=0, type=0;
  10470. FETCH(4,type);
  10471. switch (type) {
  10472. case 1: ret_val = map[m].adjust.damage.pc; break;
  10473. case 2: ret_val = map[m].adjust.damage.mob; break;
  10474. case 3: ret_val = map[m].adjust.damage.boss; break;
  10475. case 4: ret_val = map[m].adjust.damage.other; break;
  10476. case 5: ret_val = map[m].adjust.damage.caster; break;
  10477. default: ret_val = map[m].flag.skill_damage; break;
  10478. }
  10479. script_pushint(st,ret_val); break;
  10480. } break;
  10481. #endif
  10482. }
  10483. }
  10484. return SCRIPT_CMD_SUCCESS;
  10485. }
  10486. /* pvp timer handling */
  10487. static int script_mapflag_pvp_sub(struct block_list *bl,va_list ap) {
  10488. TBL_PC* sd = (TBL_PC*)bl;
  10489. if (sd->pvp_timer == INVALID_TIMER) {
  10490. sd->pvp_timer = add_timer(gettick() + 200, pc_calc_pvprank_timer, sd->bl.id, 0);
  10491. sd->pvp_rank = 0;
  10492. sd->pvp_lastusers = 0;
  10493. sd->pvp_point = 5;
  10494. sd->pvp_won = 0;
  10495. sd->pvp_lost = 0;
  10496. }
  10497. clif_map_property(&sd->bl, MAPPROPERTY_FREEPVPZONE,SELF);
  10498. return 0;
  10499. }
  10500. BUILDIN_FUNC(setmapflag)
  10501. {
  10502. int16 m,i;
  10503. const char *str;
  10504. int val=0;
  10505. str=script_getstr(st,2);
  10506. i=script_getnum(st,3);
  10507. FETCH(4,val);
  10508. m = map_mapname2mapid(str);
  10509. if(m >= 0) {
  10510. switch(i) {
  10511. case MF_NOMEMO: map[m].flag.nomemo = 1; break;
  10512. case MF_NOTELEPORT: map[m].flag.noteleport = 1; break;
  10513. case MF_NOSAVE: map[m].flag.nosave = 1; break;
  10514. case MF_NOBRANCH: map[m].flag.nobranch = 1; break;
  10515. case MF_NOPENALTY: map[m].flag.noexppenalty = 1; map[m].flag.nozenypenalty = 1; break;
  10516. case MF_NOZENYPENALTY: map[m].flag.nozenypenalty = 1; break;
  10517. case MF_PVP:
  10518. map[m].flag.pvp = 1;
  10519. if( !battle_config.pk_mode ) {
  10520. map_foreachinmap(script_mapflag_pvp_sub,m,BL_PC);
  10521. }
  10522. break;
  10523. case MF_PVP_NOPARTY: map[m].flag.pvp_noparty = 1; break;
  10524. case MF_PVP_NOGUILD: map[m].flag.pvp_noguild = 1; break;
  10525. case MF_GVG:
  10526. map[m].flag.gvg = 1;
  10527. clif_map_property_mapall(m, MAPPROPERTY_AGITZONE);
  10528. break;
  10529. case MF_GVG_NOPARTY: map[m].flag.gvg_noparty = 1; break;
  10530. case MF_NOTRADE: map[m].flag.notrade = 1; break;
  10531. case MF_NOSKILL: map[m].flag.noskill = 1; break;
  10532. case MF_NOWARP: map[m].flag.nowarp = 1; break;
  10533. case MF_PARTYLOCK: map[m].flag.partylock = 1; break;
  10534. case MF_NOICEWALL: map[m].flag.noicewall = 1; break;
  10535. case MF_SNOW: map[m].flag.snow = 1; break;
  10536. case MF_FOG: map[m].flag.fog = 1; break;
  10537. case MF_SAKURA: map[m].flag.sakura = 1; break;
  10538. case MF_LEAVES: map[m].flag.leaves = 1; break;
  10539. case MF_NOGO: map[m].flag.nogo = 1; break;
  10540. case MF_CLOUDS: map[m].flag.clouds = 1; break;
  10541. case MF_CLOUDS2: map[m].flag.clouds2 = 1; break;
  10542. case MF_FIREWORKS: map[m].flag.fireworks = 1; break;
  10543. case MF_GVG_CASTLE: map[m].flag.gvg_castle = 1; break;
  10544. case MF_GVG_DUNGEON: map[m].flag.gvg_dungeon = 1; break;
  10545. case MF_NIGHTENABLED: map[m].flag.nightenabled = 1; break;
  10546. case MF_NOBASEEXP: map[m].flag.nobaseexp = 1; break;
  10547. case MF_NOJOBEXP: map[m].flag.nojobexp = 1; break;
  10548. case MF_NOMOBLOOT: map[m].flag.nomobloot = 1; break;
  10549. case MF_NOMVPLOOT: map[m].flag.nomvploot = 1; break;
  10550. case MF_NORETURN: map[m].flag.noreturn = 1; break;
  10551. case MF_NOWARPTO: map[m].flag.nowarpto = 1; break;
  10552. case MF_NIGHTMAREDROP: map[m].flag.pvp_nightmaredrop = 1; break;
  10553. case MF_RESTRICTED:
  10554. map[m].zone |= 1<<(val+1);
  10555. map[m].flag.restricted=1;
  10556. break;
  10557. case MF_NOCOMMAND: map[m].nocommand = (val <= 0) ? 100 : val; break;
  10558. case MF_NODROP: map[m].flag.nodrop = 1; break;
  10559. case MF_JEXP: map[m].adjust.jexp = (val <= 0) ? 100 : val; break;
  10560. case MF_BEXP: map[m].adjust.bexp = (val <= 0) ? 100 : val; break;
  10561. case MF_NOVENDING: map[m].flag.novending = 1; break;
  10562. case MF_LOADEVENT: map[m].flag.loadevent = 1; break;
  10563. case MF_NOCHAT: map[m].flag.nochat = 1; break;
  10564. case MF_NOEXPPENALTY: map[m].flag.noexppenalty = 1; break;
  10565. case MF_GUILDLOCK: map[m].flag.guildlock = 1; break;
  10566. case MF_TOWN: map[m].flag.town = 1; break;
  10567. case MF_AUTOTRADE: map[m].flag.autotrade = 1; break;
  10568. case MF_ALLOWKS: map[m].flag.allowks = 1; break;
  10569. case MF_MONSTER_NOTELEPORT: map[m].flag.monster_noteleport = 1; break;
  10570. case MF_PVP_NOCALCRANK: map[m].flag.pvp_nocalcrank = 1; break;
  10571. case MF_BATTLEGROUND: map[m].flag.battleground = (val <= 0 || val > 2) ? 1 : val; break;
  10572. case MF_RESET: map[m].flag.reset = 1; break;
  10573. case MF_CHANNELAUTOJOIN: map[m].flag.chmautojoin = 1 ; break;
  10574. case MF_NOUSECART: map[m].flag.nousecart = 1 ; break;
  10575. case MF_NOITEMCONSUMPTION: map[m].flag.noitemconsumption = 1 ; break;
  10576. case MF_SUMSTARTMIRACLE: map[m].flag.nosumstarmiracle = 1 ; break;
  10577. case MF_NOMINEEFFECT: map[m].flag.nomineeffect = 1 ; break;
  10578. case MF_NOLOCKON: map[m].flag.nolockon = 1 ; break;
  10579. case MF_NOTOMB: map[m].flag.notomb = 1; break;
  10580. case MF_NOCOSTUME: map[m].flag.nocostume = 1; break;
  10581. case MF_GVG_TE_CASTLE: map[m].flag.gvg_te_castle = 1; break;
  10582. case MF_GVG_TE:
  10583. map[m].flag.gvg_te = 1;
  10584. clif_map_property_mapall(m, MAPPROPERTY_AGITZONE);
  10585. break;
  10586. case MF_HIDEMOBHPBAR: map[m].flag.hidemobhpbar = 1; break;
  10587. #ifdef ADJUST_SKILL_DAMAGE
  10588. case MF_SKILL_DAMAGE:
  10589. {
  10590. int type=0;
  10591. FETCH(5,type);
  10592. switch (type) {
  10593. case 1: map[m].adjust.damage.pc = val; break;
  10594. case 2: map[m].adjust.damage.mob = val; break;
  10595. case 3: map[m].adjust.damage.boss = val; break;
  10596. case 4: map[m].adjust.damage.other = val; break;
  10597. case 5: map[m].adjust.damage.caster = val; break;
  10598. }
  10599. map[m].flag.skill_damage = 1;
  10600. } break;
  10601. #endif
  10602. }
  10603. }
  10604. return SCRIPT_CMD_SUCCESS;
  10605. }
  10606. BUILDIN_FUNC(removemapflag)
  10607. {
  10608. int16 m,i;
  10609. const char *str;
  10610. int val=0;
  10611. str=script_getstr(st,2);
  10612. i=script_getnum(st,3);
  10613. FETCH(4,val);
  10614. m = map_mapname2mapid(str);
  10615. if(m >= 0) {
  10616. switch(i) {
  10617. case MF_NOMEMO: map[m].flag.nomemo = 0; break;
  10618. case MF_NOTELEPORT: map[m].flag.noteleport = 0; break;
  10619. case MF_NOSAVE: map[m].flag.nosave = 0; break;
  10620. case MF_NOBRANCH: map[m].flag.nobranch = 0; break;
  10621. case MF_NOPENALTY: map[m].flag.noexppenalty = 0; map[m].flag.nozenypenalty = 0; break;
  10622. case MF_NOZENYPENALTY: map[m].flag.nozenypenalty = 0; break;
  10623. case MF_PVP:
  10624. map[m].flag.pvp = 0;
  10625. clif_map_property_mapall(m, MAPPROPERTY_NOTHING);
  10626. break;
  10627. case MF_PVP_NOPARTY: map[m].flag.pvp_noparty = 0; break;
  10628. case MF_PVP_NOGUILD: map[m].flag.pvp_noguild = 0; break;
  10629. case MF_GVG:
  10630. map[m].flag.gvg = 0;
  10631. clif_map_property_mapall(m, MAPPROPERTY_NOTHING);
  10632. break;
  10633. case MF_GVG_NOPARTY: map[m].flag.gvg_noparty = 0; break;
  10634. case MF_NOTRADE: map[m].flag.notrade = 0; break;
  10635. case MF_NOSKILL: map[m].flag.noskill = 0; break;
  10636. case MF_NOWARP: map[m].flag.nowarp = 0; break;
  10637. case MF_PARTYLOCK: map[m].flag.partylock = 0; break;
  10638. case MF_NOICEWALL: map[m].flag.noicewall = 0; break;
  10639. case MF_SNOW: map[m].flag.snow = 0; break;
  10640. case MF_FOG: map[m].flag.fog = 0; break;
  10641. case MF_SAKURA: map[m].flag.sakura = 0; break;
  10642. case MF_LEAVES: map[m].flag.leaves = 0; break;
  10643. case MF_NOGO: map[m].flag.nogo = 0; break;
  10644. case MF_CLOUDS: map[m].flag.clouds = 0; break;
  10645. case MF_CLOUDS2: map[m].flag.clouds2 = 0; break;
  10646. case MF_FIREWORKS: map[m].flag.fireworks = 0; break;
  10647. case MF_GVG_CASTLE: map[m].flag.gvg_castle = 0; break;
  10648. case MF_GVG_DUNGEON: map[m].flag.gvg_dungeon = 0; break;
  10649. case MF_NIGHTENABLED: map[m].flag.nightenabled = 0; break;
  10650. case MF_NOBASEEXP: map[m].flag.nobaseexp = 0; break;
  10651. case MF_NOJOBEXP: map[m].flag.nojobexp = 0; break;
  10652. case MF_NOMOBLOOT: map[m].flag.nomobloot = 0; break;
  10653. case MF_NOMVPLOOT: map[m].flag.nomvploot = 0; break;
  10654. case MF_NORETURN: map[m].flag.noreturn = 0; break;
  10655. case MF_NOWARPTO: map[m].flag.nowarpto = 0; break;
  10656. case MF_NIGHTMAREDROP: map[m].flag.pvp_nightmaredrop = 0; break;
  10657. case MF_RESTRICTED:
  10658. map[m].zone ^= 1<<(val+1);
  10659. if (map[m].zone == 0){
  10660. map[m].flag.restricted=0;
  10661. }
  10662. break;
  10663. case MF_NOCOMMAND: map[m].nocommand = 0; break;
  10664. case MF_NODROP: map[m].flag.nodrop = 0; break;
  10665. case MF_JEXP: map[m].adjust.jexp = 0; break;
  10666. case MF_BEXP: map[m].adjust.bexp = 0; break;
  10667. case MF_NOVENDING: map[m].flag.novending = 0; break;
  10668. case MF_LOADEVENT: map[m].flag.loadevent = 0; break;
  10669. case MF_NOCHAT: map[m].flag.nochat = 0; break;
  10670. case MF_NOEXPPENALTY: map[m].flag.noexppenalty = 0; break;
  10671. case MF_GUILDLOCK: map[m].flag.guildlock = 0; break;
  10672. case MF_TOWN: map[m].flag.town = 0; break;
  10673. case MF_AUTOTRADE: map[m].flag.autotrade = 0; break;
  10674. case MF_ALLOWKS: map[m].flag.allowks = 0; break;
  10675. case MF_MONSTER_NOTELEPORT: map[m].flag.monster_noteleport = 0; break;
  10676. case MF_PVP_NOCALCRANK: map[m].flag.pvp_nocalcrank = 0; break;
  10677. case MF_BATTLEGROUND: map[m].flag.battleground = 0; break;
  10678. case MF_RESET: map[m].flag.reset = 0; break;
  10679. case MF_CHANNELAUTOJOIN: map[m].flag.chmautojoin = 0 ; break;
  10680. case MF_NOUSECART: map[m].flag.nousecart = 0 ; break;
  10681. case MF_NOITEMCONSUMPTION: map[m].flag.noitemconsumption = 0 ; break;
  10682. case MF_SUMSTARTMIRACLE: map[m].flag.nosumstarmiracle = 0 ; break;
  10683. case MF_NOMINEEFFECT: map[m].flag.nomineeffect = 0 ; break;
  10684. case MF_NOLOCKON: map[m].flag.nolockon = 0 ; break;
  10685. case MF_NOTOMB: map[m].flag.notomb = 0; break;
  10686. case MF_NOCOSTUME: map[m].flag.nocostume = 0; break;
  10687. case MF_GVG_TE_CASTLE: map[m].flag.gvg_te_castle = 0; break;
  10688. case MF_GVG_TE:
  10689. map[m].flag.gvg_te = 0;
  10690. clif_map_property_mapall(m, MAPPROPERTY_NOTHING);
  10691. break;
  10692. case MF_HIDEMOBHPBAR: map[m].flag.hidemobhpbar = 0; break;
  10693. #ifdef ADJUST_SKILL_DAMAGE
  10694. case MF_SKILL_DAMAGE:
  10695. {
  10696. map[m].flag.skill_damage = 0;
  10697. memset(&map[m].adjust.damage, 0, sizeof(map[m].adjust.damage));
  10698. if (map[m].skill_damage.count)
  10699. map_skill_damage_free(&map[m]);
  10700. } break;
  10701. #endif
  10702. }
  10703. }
  10704. return SCRIPT_CMD_SUCCESS;
  10705. }
  10706. BUILDIN_FUNC(pvpon)
  10707. {
  10708. int16 m;
  10709. const char *str;
  10710. TBL_PC* sd = NULL;
  10711. struct s_mapiterator* iter;
  10712. str = script_getstr(st,2);
  10713. m = map_mapname2mapid(str);
  10714. if( m < 0 || map[m].flag.pvp )
  10715. return SCRIPT_CMD_SUCCESS; // nothing to do
  10716. map[m].flag.pvp = 1;
  10717. clif_map_property_mapall(m, MAPPROPERTY_FREEPVPZONE);
  10718. if(battle_config.pk_mode) // disable ranking functions if pk_mode is on [Valaris]
  10719. return SCRIPT_CMD_SUCCESS;
  10720. iter = mapit_getallusers();
  10721. for( sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); sd = (TBL_PC*)mapit_next(iter) )
  10722. {
  10723. if( sd->bl.m != m || sd->pvp_timer != INVALID_TIMER )
  10724. continue; // not applicable
  10725. sd->pvp_timer = add_timer(gettick()+200,pc_calc_pvprank_timer,sd->bl.id,0);
  10726. sd->pvp_rank = 0;
  10727. sd->pvp_lastusers = 0;
  10728. sd->pvp_point = 5;
  10729. sd->pvp_won = 0;
  10730. sd->pvp_lost = 0;
  10731. }
  10732. mapit_free(iter);
  10733. return SCRIPT_CMD_SUCCESS;
  10734. }
  10735. static int buildin_pvpoff_sub(struct block_list *bl,va_list ap)
  10736. {
  10737. TBL_PC* sd = (TBL_PC*)bl;
  10738. clif_pvpset(sd, 0, 0, 2);
  10739. if (sd->pvp_timer != INVALID_TIMER) {
  10740. delete_timer(sd->pvp_timer, pc_calc_pvprank_timer);
  10741. sd->pvp_timer = INVALID_TIMER;
  10742. }
  10743. return 0;
  10744. }
  10745. BUILDIN_FUNC(pvpoff)
  10746. {
  10747. int16 m;
  10748. const char *str;
  10749. str=script_getstr(st,2);
  10750. m = map_mapname2mapid(str);
  10751. if(m < 0 || !map[m].flag.pvp)
  10752. return SCRIPT_CMD_SUCCESS; //fixed Lupus
  10753. map[m].flag.pvp = 0;
  10754. clif_map_property_mapall(m, MAPPROPERTY_NOTHING);
  10755. if(battle_config.pk_mode) // disable ranking options if pk_mode is on [Valaris]
  10756. return SCRIPT_CMD_SUCCESS;
  10757. map_foreachinmap(buildin_pvpoff_sub, m, BL_PC);
  10758. return SCRIPT_CMD_SUCCESS;
  10759. }
  10760. BUILDIN_FUNC(gvgon)
  10761. {
  10762. int16 m;
  10763. const char *str;
  10764. str=script_getstr(st,2);
  10765. m = map_mapname2mapid(str);
  10766. if(m >= 0 && !map[m].flag.gvg) {
  10767. map[m].flag.gvg = 1;
  10768. clif_map_property_mapall(m, MAPPROPERTY_AGITZONE);
  10769. }
  10770. return SCRIPT_CMD_SUCCESS;
  10771. }
  10772. BUILDIN_FUNC(gvgoff)
  10773. {
  10774. int16 m;
  10775. const char *str;
  10776. str=script_getstr(st,2);
  10777. m = map_mapname2mapid(str);
  10778. if(m >= 0 && map[m].flag.gvg) {
  10779. map[m].flag.gvg = 0;
  10780. clif_map_property_mapall(m, MAPPROPERTY_NOTHING);
  10781. }
  10782. return SCRIPT_CMD_SUCCESS;
  10783. }
  10784. BUILDIN_FUNC(gvgon3)
  10785. {
  10786. int16 m;
  10787. const char *str;
  10788. str = script_getstr(st,2);
  10789. m = map_mapname2mapid(str);
  10790. if (m >= 0 && !map[m].flag.gvg_te) {
  10791. map[m].flag.gvg_te = 1;
  10792. clif_map_property_mapall(m, MAPPROPERTY_AGITZONE);
  10793. }
  10794. return SCRIPT_CMD_SUCCESS;
  10795. }
  10796. BUILDIN_FUNC(gvgoff3)
  10797. {
  10798. int16 m;
  10799. const char *str;
  10800. str = script_getstr(st,2);
  10801. m = map_mapname2mapid(str);
  10802. if (m >= 0 && map[m].flag.gvg_te) {
  10803. map[m].flag.gvg_te = 0;
  10804. clif_map_property_mapall(m, MAPPROPERTY_NOTHING);
  10805. }
  10806. return SCRIPT_CMD_SUCCESS;
  10807. }
  10808. /*==========================================
  10809. * Shows an emoticon on top of the player/npc
  10810. * emotion emotion#, <target: 0 - NPC, 1 - PC>, <NPC/PC name>
  10811. *------------------------------------------*/
  10812. //Optional second parameter added by [Skotlex]
  10813. BUILDIN_FUNC(emotion)
  10814. {
  10815. int type;
  10816. int player=0;
  10817. type=script_getnum(st,2);
  10818. if(type < 0 || type > 100)
  10819. return SCRIPT_CMD_SUCCESS;
  10820. if( script_hasdata(st,3) )
  10821. player=script_getnum(st,3);
  10822. if (player) {
  10823. TBL_PC *sd = NULL;
  10824. if( script_hasdata(st,4) )
  10825. sd = map_nick2sd(script_getstr(st,4));
  10826. else
  10827. sd = script_rid2sd(st);
  10828. if (sd)
  10829. clif_emotion(&sd->bl,type);
  10830. } else
  10831. if( script_hasdata(st,4) )
  10832. {
  10833. TBL_NPC *nd = npc_name2id(script_getstr(st,4));
  10834. if(nd)
  10835. clif_emotion(&nd->bl,type);
  10836. }
  10837. else
  10838. clif_emotion(map_id2bl(st->oid),type);
  10839. return SCRIPT_CMD_SUCCESS;
  10840. }
  10841. static int buildin_maprespawnguildid_sub_pc(struct map_session_data* sd, va_list ap)
  10842. {
  10843. int16 m=va_arg(ap,int);
  10844. int g_id=va_arg(ap,int);
  10845. int flag=va_arg(ap,int);
  10846. if(!sd || sd->bl.m != m)
  10847. return 0;
  10848. if(
  10849. (sd->status.guild_id == g_id && flag&1) || //Warp out owners
  10850. (sd->status.guild_id != g_id && flag&2) || //Warp out outsiders
  10851. (sd->status.guild_id == 0 && flag&2) // Warp out players not in guild
  10852. )
  10853. pc_setpos(sd,sd->status.save_point.map,sd->status.save_point.x,sd->status.save_point.y,CLR_TELEPORT);
  10854. return 1;
  10855. }
  10856. static int buildin_maprespawnguildid_sub_mob(struct block_list *bl,va_list ap)
  10857. {
  10858. struct mob_data *md=(struct mob_data *)bl;
  10859. if(!md->guardian_data && md->mob_id != MOBID_EMPERIUM && ( !mob_is_clone(md->mob_id) || battle_config.guild_maprespawn_clones ))
  10860. status_kill(bl);
  10861. return 1;
  10862. }
  10863. /*
  10864. * Function to kick guild members out of a map and to their save points.
  10865. * m : mapid
  10866. * g_id : owner guild id
  10867. * flag & 1 : Warp out owners
  10868. * flag & 2 : Warp out outsiders
  10869. * flag & 4 : reserved for mobs
  10870. * */
  10871. BUILDIN_FUNC(maprespawnguildid)
  10872. {
  10873. const char *mapname=script_getstr(st,2);
  10874. int g_id=script_getnum(st,3);
  10875. int flag=script_getnum(st,4);
  10876. int16 m=map_mapname2mapid(mapname);
  10877. if(m == -1)
  10878. return SCRIPT_CMD_SUCCESS;
  10879. //Catch ALL players (in case some are 'between maps' on execution time)
  10880. map_foreachpc(buildin_maprespawnguildid_sub_pc,m,g_id,flag);
  10881. if (flag&4) //Remove script mobs.
  10882. map_foreachinmap(buildin_maprespawnguildid_sub_mob,m,BL_MOB);
  10883. return SCRIPT_CMD_SUCCESS;
  10884. }
  10885. /// Siege commands
  10886. /**
  10887. * Start WoE:FE
  10888. * agitstart();
  10889. */
  10890. BUILDIN_FUNC(agitstart)
  10891. {
  10892. if (agit_flag)
  10893. return SCRIPT_CMD_SUCCESS;// Agit already Started.
  10894. agit_flag = true;
  10895. guild_agit_start();
  10896. return SCRIPT_CMD_SUCCESS;
  10897. }
  10898. /**
  10899. * End WoE:FE
  10900. * agitend();
  10901. */
  10902. BUILDIN_FUNC(agitend)
  10903. {
  10904. if (!agit_flag)
  10905. return SCRIPT_CMD_SUCCESS;// Agit already Ended.
  10906. agit_flag = false;
  10907. guild_agit_end();
  10908. return SCRIPT_CMD_SUCCESS;
  10909. }
  10910. /**
  10911. * Start WoE:SE
  10912. * agitstart2();
  10913. */
  10914. BUILDIN_FUNC(agitstart2)
  10915. {
  10916. if (agit2_flag)
  10917. return SCRIPT_CMD_SUCCESS;// Agit2 already Started.
  10918. agit2_flag = true;
  10919. guild_agit2_start();
  10920. return SCRIPT_CMD_SUCCESS;
  10921. }
  10922. /**
  10923. * End WoE:SE
  10924. * agitend();
  10925. */
  10926. BUILDIN_FUNC(agitend2)
  10927. {
  10928. if (!agit2_flag)
  10929. return SCRIPT_CMD_SUCCESS;// Agit2 already Ended.
  10930. agit2_flag = false;
  10931. guild_agit2_end();
  10932. return SCRIPT_CMD_SUCCESS;
  10933. }
  10934. /**
  10935. * Start WoE:TE
  10936. * agitstart3();
  10937. */
  10938. BUILDIN_FUNC(agitstart3)
  10939. {
  10940. if (agit3_flag)
  10941. return SCRIPT_CMD_SUCCESS;// AgitTE already Started.
  10942. agit3_flag = true;
  10943. guild_agit3_start();
  10944. return SCRIPT_CMD_SUCCESS;
  10945. }
  10946. /**
  10947. * End WoE:TE
  10948. * agitend3();
  10949. */
  10950. BUILDIN_FUNC(agitend3)
  10951. {
  10952. if (!agit3_flag)
  10953. return SCRIPT_CMD_SUCCESS;// AgitTE already Ended.
  10954. agit3_flag = false;
  10955. guild_agit3_end();
  10956. return SCRIPT_CMD_SUCCESS;
  10957. }
  10958. /**
  10959. * Returns whether WoE:FE is on or off.
  10960. * agitcheck();
  10961. */
  10962. BUILDIN_FUNC(agitcheck)
  10963. {
  10964. script_pushint(st, agit_flag);
  10965. return SCRIPT_CMD_SUCCESS;
  10966. }
  10967. /**
  10968. * Returns whether WoE:SE is on or off.
  10969. * agitcheck2();
  10970. */
  10971. BUILDIN_FUNC(agitcheck2)
  10972. {
  10973. script_pushint(st, agit2_flag);
  10974. return SCRIPT_CMD_SUCCESS;
  10975. }
  10976. /**
  10977. * Returns whether WoE:TE is on or off.
  10978. * agitcheck3();
  10979. */
  10980. BUILDIN_FUNC(agitcheck3)
  10981. {
  10982. script_pushint(st, agit3_flag);
  10983. return SCRIPT_CMD_SUCCESS;
  10984. }
  10985. /// Sets the guild_id of this npc.
  10986. ///
  10987. /// flagemblem <guild_id>;
  10988. BUILDIN_FUNC(flagemblem)
  10989. {
  10990. TBL_NPC* nd;
  10991. int g_id = script_getnum(st,2);
  10992. if(g_id < 0)
  10993. return SCRIPT_CMD_SUCCESS;
  10994. nd = (TBL_NPC*)map_id2nd(st->oid);
  10995. if( nd == NULL ) {
  10996. ShowError("script:flagemblem: npc %d not found\n", st->oid);
  10997. } else if( nd->subtype != NPCTYPE_SCRIPT ) {
  10998. ShowError("script:flagemblem: unexpected subtype %d for npc %d '%s'\n", nd->subtype, st->oid, nd->exname);
  10999. } else {
  11000. bool changed = ( nd->u.scr.guild_id != g_id )?true:false;
  11001. nd->u.scr.guild_id = g_id;
  11002. clif_guild_emblem_area(&nd->bl);
  11003. /* guild flag caching */
  11004. if( g_id ) /* adding a id */
  11005. guild_flag_add(nd);
  11006. else if( changed ) /* removing a flag */
  11007. guild_flag_remove(nd);
  11008. }
  11009. return SCRIPT_CMD_SUCCESS;
  11010. }
  11011. BUILDIN_FUNC(getcastlename)
  11012. {
  11013. const char* mapname = mapindex_getmapname(script_getstr(st,2),NULL);
  11014. struct guild_castle* gc = guild_mapname2gc(mapname);
  11015. const char* name = (gc) ? gc->castle_name : "";
  11016. script_pushstrcopy(st,name);
  11017. return SCRIPT_CMD_SUCCESS;
  11018. }
  11019. BUILDIN_FUNC(getcastledata)
  11020. {
  11021. const char *mapname = mapindex_getmapname(script_getstr(st,2),NULL);
  11022. int index = script_getnum(st,3);
  11023. struct guild_castle *gc = guild_mapname2gc(mapname);
  11024. if (gc == NULL) {
  11025. script_pushint(st,0);
  11026. ShowWarning("buildin_getcastledata: guild castle for map '%s' not found\n", mapname);
  11027. return SCRIPT_CMD_FAILURE;
  11028. }
  11029. switch (index) {
  11030. case 1:
  11031. script_pushint(st,gc->guild_id); break;
  11032. case 2:
  11033. script_pushint(st,gc->economy); break;
  11034. case 3:
  11035. script_pushint(st,gc->defense); break;
  11036. case 4:
  11037. script_pushint(st,gc->triggerE); break;
  11038. case 5:
  11039. script_pushint(st,gc->triggerD); break;
  11040. case 6:
  11041. script_pushint(st,gc->nextTime); break;
  11042. case 7:
  11043. script_pushint(st,gc->payTime); break;
  11044. case 8:
  11045. script_pushint(st,gc->createTime); break;
  11046. case 9:
  11047. script_pushint(st,gc->visibleC); break;
  11048. default:
  11049. if (index > 9 && index <= 9+MAX_GUARDIANS) {
  11050. script_pushint(st,gc->guardian[index-10].visible);
  11051. break;
  11052. }
  11053. script_pushint(st,0);
  11054. ShowWarning("buildin_getcastledata: index = '%d' is out of allowed range\n", index);
  11055. return SCRIPT_CMD_FAILURE;
  11056. }
  11057. return SCRIPT_CMD_SUCCESS;
  11058. }
  11059. BUILDIN_FUNC(setcastledata)
  11060. {
  11061. const char *mapname = mapindex_getmapname(script_getstr(st,2),NULL);
  11062. int index = script_getnum(st,3);
  11063. int value = script_getnum(st,4);
  11064. struct guild_castle *gc = guild_mapname2gc(mapname);
  11065. if (gc == NULL) {
  11066. ShowWarning("buildin_setcastledata: guild castle for map '%s' not found\n", mapname);
  11067. return SCRIPT_CMD_FAILURE;
  11068. }
  11069. if (index <= 0 || index > 9+MAX_GUARDIANS) {
  11070. ShowWarning("buildin_setcastledata: index = '%d' is out of allowed range\n", index);
  11071. return SCRIPT_CMD_FAILURE;
  11072. }
  11073. guild_castledatasave(gc->castle_id, index, value);
  11074. return SCRIPT_CMD_SUCCESS;
  11075. }
  11076. /* =====================================================================
  11077. * ---------------------------------------------------------------------*/
  11078. BUILDIN_FUNC(requestguildinfo)
  11079. {
  11080. int guild_id=script_getnum(st,2);
  11081. const char *event=NULL;
  11082. if( script_hasdata(st,3) ){
  11083. event=script_getstr(st,3);
  11084. check_event(st, event);
  11085. }
  11086. if(guild_id>0)
  11087. guild_npc_request_info(guild_id,event);
  11088. return SCRIPT_CMD_SUCCESS;
  11089. }
  11090. /// Returns the number of cards that have been compounded onto the specified equipped item.
  11091. /// getequipcardcnt(<equipment slot>);
  11092. BUILDIN_FUNC(getequipcardcnt)
  11093. {
  11094. int i=-1,j,num;
  11095. TBL_PC *sd;
  11096. int count;
  11097. num=script_getnum(st,2);
  11098. sd=script_rid2sd(st);
  11099. if (equip_index_check(num))
  11100. i=pc_checkequip(sd,equip_bitmask[num]);
  11101. if (i < 0 || !sd->inventory_data[i]) {
  11102. script_pushint(st,0);
  11103. return SCRIPT_CMD_SUCCESS;
  11104. }
  11105. if(itemdb_isspecial(sd->inventory.u.items_inventory[i].card[0]))
  11106. {
  11107. script_pushint(st,0);
  11108. return SCRIPT_CMD_SUCCESS;
  11109. }
  11110. count = 0;
  11111. for( j = 0; j < sd->inventory_data[i]->slot; j++ )
  11112. if( sd->inventory.u.items_inventory[i].card[j] && itemdb_type(sd->inventory.u.items_inventory[i].card[j]) == IT_CARD )
  11113. count++;
  11114. script_pushint(st,count);
  11115. return SCRIPT_CMD_SUCCESS;
  11116. }
  11117. /// Removes all cards from the item found in the specified equipment slot of the invoking character,
  11118. /// and give them to the character. If any cards were removed in this manner, it will also show a success effect.
  11119. /// successremovecards <slot>;
  11120. BUILDIN_FUNC(successremovecards) {
  11121. int i=-1,c,cardflag=0;
  11122. TBL_PC* sd = script_rid2sd(st);
  11123. int num = script_getnum(st,2);
  11124. if (equip_index_check(num))
  11125. i=pc_checkequip(sd,equip_bitmask[num]);
  11126. if (i < 0 || !sd->inventory_data[i]) {
  11127. return SCRIPT_CMD_SUCCESS;
  11128. }
  11129. if(itemdb_isspecial(sd->inventory.u.items_inventory[i].card[0]))
  11130. return SCRIPT_CMD_SUCCESS;
  11131. for( c = sd->inventory_data[i]->slot - 1; c >= 0; --c ) {
  11132. if( sd->inventory.u.items_inventory[i].card[c] && itemdb_type(sd->inventory.u.items_inventory[i].card[c]) == IT_CARD ) {// extract this card from the item
  11133. unsigned char flag = 0;
  11134. struct item item_tmp;
  11135. memset(&item_tmp,0,sizeof(item_tmp));
  11136. cardflag = 1;
  11137. item_tmp.nameid = sd->inventory.u.items_inventory[i].card[c];
  11138. item_tmp.identify = 1;
  11139. if((flag=pc_additem(sd,&item_tmp,1,LOG_TYPE_SCRIPT))){ // get back the cart in inventory
  11140. clif_additem(sd,0,0,flag);
  11141. map_addflooritem(&item_tmp,1,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0,0);
  11142. }
  11143. }
  11144. }
  11145. if(cardflag == 1) {//if card was remove remplace item with no card
  11146. unsigned char flag = 0, j;
  11147. struct item item_tmp;
  11148. memset(&item_tmp,0,sizeof(item_tmp));
  11149. item_tmp.nameid = sd->inventory.u.items_inventory[i].nameid;
  11150. item_tmp.identify = 1;
  11151. item_tmp.refine = sd->inventory.u.items_inventory[i].refine;
  11152. item_tmp.attribute = sd->inventory.u.items_inventory[i].attribute;
  11153. item_tmp.expire_time = sd->inventory.u.items_inventory[i].expire_time;
  11154. item_tmp.bound = sd->inventory.u.items_inventory[i].bound;
  11155. for (j = sd->inventory_data[i]->slot; j < MAX_SLOTS; j++)
  11156. item_tmp.card[j]=sd->inventory.u.items_inventory[i].card[j];
  11157. for (j = 0; j < MAX_ITEM_RDM_OPT; j++){
  11158. item_tmp.option[j].id=sd->inventory.u.items_inventory[i].option[j].id;
  11159. item_tmp.option[j].value=sd->inventory.u.items_inventory[i].option[j].value;
  11160. item_tmp.option[j].param=sd->inventory.u.items_inventory[i].option[j].param;
  11161. }
  11162. pc_delitem(sd,i,1,0,3,LOG_TYPE_SCRIPT);
  11163. if((flag=pc_additem(sd,&item_tmp,1,LOG_TYPE_SCRIPT))){ //chk if can be spawn in inventory otherwise put on floor
  11164. clif_additem(sd,0,0,flag);
  11165. map_addflooritem(&item_tmp,1,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0,0);
  11166. }
  11167. clif_misceffect(&sd->bl,3);
  11168. }
  11169. return SCRIPT_CMD_SUCCESS;
  11170. }
  11171. /// Removes all cards from the item found in the specified equipment slot of the invoking character.
  11172. /// failedremovecards <slot>, <type>;
  11173. /// <type>=0 : will destroy both the item and the cards.
  11174. /// <type>=1 : will keep the item, but destroy the cards.
  11175. /// <type>=2 : will keep the cards, but destroy the item.
  11176. /// <type>=? : will just display the failure effect.
  11177. BUILDIN_FUNC(failedremovecards) {
  11178. int i=-1,c,cardflag=0;
  11179. TBL_PC* sd = script_rid2sd(st);
  11180. int num = script_getnum(st,2);
  11181. int typefail = script_getnum(st,3);
  11182. if (equip_index_check(num))
  11183. i=pc_checkequip(sd,equip_bitmask[num]);
  11184. if (i < 0 || !sd->inventory_data[i])
  11185. return SCRIPT_CMD_SUCCESS;
  11186. if(itemdb_isspecial(sd->inventory.u.items_inventory[i].card[0]))
  11187. return SCRIPT_CMD_SUCCESS;
  11188. for( c = sd->inventory_data[i]->slot - 1; c >= 0; --c ) {
  11189. if( sd->inventory.u.items_inventory[i].card[c] && itemdb_type(sd->inventory.u.items_inventory[i].card[c]) == IT_CARD ) {
  11190. cardflag = 1;
  11191. if(typefail == 2) {// add cards to inventory, clear
  11192. unsigned char flag = 0;
  11193. struct item item_tmp;
  11194. memset(&item_tmp,0,sizeof(item_tmp));
  11195. item_tmp.nameid = sd->inventory.u.items_inventory[i].card[c];
  11196. item_tmp.identify = 1;
  11197. if((flag=pc_additem(sd,&item_tmp,1,LOG_TYPE_SCRIPT))){
  11198. clif_additem(sd,0,0,flag);
  11199. map_addflooritem(&item_tmp,1,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0,0);
  11200. }
  11201. }
  11202. }
  11203. }
  11204. if(cardflag == 1) {
  11205. if(typefail == 0 || typefail == 2){ // destroy the item
  11206. pc_delitem(sd,i,1,0,2,LOG_TYPE_SCRIPT);
  11207. }else if(typefail == 1){ // destroy the card
  11208. unsigned char flag = 0, j;
  11209. struct item item_tmp;
  11210. memset(&item_tmp,0,sizeof(item_tmp));
  11211. item_tmp.nameid = sd->inventory.u.items_inventory[i].nameid;
  11212. item_tmp.identify = 1;
  11213. item_tmp.refine = sd->inventory.u.items_inventory[i].refine;
  11214. item_tmp.attribute = sd->inventory.u.items_inventory[i].attribute;
  11215. item_tmp.expire_time = sd->inventory.u.items_inventory[i].expire_time;
  11216. item_tmp.bound = sd->inventory.u.items_inventory[i].bound;
  11217. for (j = sd->inventory_data[i]->slot; j < MAX_SLOTS; j++)
  11218. item_tmp.card[j]=sd->inventory.u.items_inventory[i].card[j];
  11219. for (j = 0; j < MAX_ITEM_RDM_OPT; j++){
  11220. item_tmp.option[j].id=sd->inventory.u.items_inventory[i].option[j].id;
  11221. item_tmp.option[j].value=sd->inventory.u.items_inventory[i].option[j].value;
  11222. item_tmp.option[j].param=sd->inventory.u.items_inventory[i].option[j].param;
  11223. }
  11224. pc_delitem(sd,i,1,0,2,LOG_TYPE_SCRIPT);
  11225. if((flag=pc_additem(sd,&item_tmp,1,LOG_TYPE_SCRIPT))){
  11226. clif_additem(sd,0,0,flag);
  11227. map_addflooritem(&item_tmp,1,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0,0);
  11228. }
  11229. }
  11230. clif_misceffect(&sd->bl,2);
  11231. }
  11232. return SCRIPT_CMD_SUCCESS;
  11233. }
  11234. /* ================================================================
  11235. * mapwarp "<from map>","<to map>",<x>,<y>,<type>,<ID for Type>;
  11236. * type: 0=everyone, 1=guild, 2=party; [Reddozen]
  11237. * improved by [Lance]
  11238. * ================================================================*/
  11239. BUILDIN_FUNC(mapwarp) // Added by RoVeRT
  11240. {
  11241. int x,y,m,check_val=0,check_ID=0,i=0;
  11242. struct guild *g = NULL;
  11243. struct party_data *p = NULL;
  11244. const char *str;
  11245. const char *mapname;
  11246. unsigned int index;
  11247. mapname=script_getstr(st,2);
  11248. str=script_getstr(st,3);
  11249. x=script_getnum(st,4);
  11250. y=script_getnum(st,5);
  11251. if(script_hasdata(st,7)){
  11252. check_val=script_getnum(st,6);
  11253. check_ID=script_getnum(st,7);
  11254. }
  11255. if((m=map_mapname2mapid(mapname))< 0)
  11256. return SCRIPT_CMD_SUCCESS;
  11257. if(!(index=mapindex_name2id(str)))
  11258. return SCRIPT_CMD_SUCCESS;
  11259. switch(check_val){
  11260. case 1:
  11261. g = guild_search(check_ID);
  11262. if (g){
  11263. for( i=0; i < g->max_member; i++)
  11264. {
  11265. if(g->member[i].sd && g->member[i].sd->bl.m==m){
  11266. pc_setpos(g->member[i].sd,index,x,y,CLR_TELEPORT);
  11267. }
  11268. }
  11269. }
  11270. break;
  11271. case 2:
  11272. p = party_search(check_ID);
  11273. if(p){
  11274. for(i=0;i<MAX_PARTY; i++){
  11275. if(p->data[i].sd && p->data[i].sd->bl.m == m){
  11276. pc_setpos(p->data[i].sd,index,x,y,CLR_TELEPORT);
  11277. }
  11278. }
  11279. }
  11280. break;
  11281. default:
  11282. map_foreachinmap(buildin_areawarp_sub,m,BL_PC,index,x,y,0,0);
  11283. break;
  11284. }
  11285. return SCRIPT_CMD_SUCCESS;
  11286. }
  11287. static int buildin_mobcount_sub(struct block_list *bl,va_list ap) // Added by RoVeRT
  11288. {
  11289. char *event=va_arg(ap,char *);
  11290. struct mob_data *md = ((struct mob_data *)bl);
  11291. if( md->status.hp > 0 && (!event || strcmp(event,md->npc_event) == 0) )
  11292. return 1;
  11293. return SCRIPT_CMD_SUCCESS;
  11294. }
  11295. BUILDIN_FUNC(mobcount) // Added by RoVeRT
  11296. {
  11297. const char *mapname,*event;
  11298. int16 m;
  11299. mapname=script_getstr(st,2);
  11300. event=script_getstr(st,3);
  11301. if( strcmp(event, "all") == 0 )
  11302. event = NULL;
  11303. else
  11304. check_event(st, event);
  11305. if( strcmp(mapname, "this") == 0 ) {
  11306. struct map_session_data *sd = script_rid2sd(st);
  11307. if( sd )
  11308. m = sd->bl.m;
  11309. else {
  11310. script_pushint(st,-1);
  11311. return SCRIPT_CMD_SUCCESS;
  11312. }
  11313. }
  11314. else if( (m = map_mapname2mapid(mapname)) < 0 ) {
  11315. script_pushint(st,-1);
  11316. return SCRIPT_CMD_SUCCESS;
  11317. }
  11318. script_pushint(st,map_foreachinmap(buildin_mobcount_sub, m, BL_MOB, event));
  11319. return SCRIPT_CMD_SUCCESS;
  11320. }
  11321. BUILDIN_FUNC(marriage)
  11322. {
  11323. const char *partner=script_getstr(st,2);
  11324. TBL_PC *sd=script_rid2sd(st);
  11325. TBL_PC *p_sd=map_nick2sd(partner);
  11326. if(!sd || !p_sd || !pc_marriage(sd,p_sd)){
  11327. script_pushint(st,0);
  11328. return SCRIPT_CMD_SUCCESS;
  11329. }
  11330. script_pushint(st,1);
  11331. return SCRIPT_CMD_SUCCESS;
  11332. }
  11333. BUILDIN_FUNC(wedding_effect)
  11334. {
  11335. TBL_PC *sd=script_rid2sd(st);
  11336. struct block_list *bl;
  11337. if(sd==NULL) {
  11338. bl=map_id2bl(st->oid);
  11339. } else
  11340. bl=&sd->bl;
  11341. clif_wedding_effect(bl);
  11342. return SCRIPT_CMD_SUCCESS;
  11343. }
  11344. /**
  11345. * divorce({<char_id>})
  11346. **/
  11347. BUILDIN_FUNC(divorce)
  11348. {
  11349. TBL_PC *sd;
  11350. if (!script_charid2sd(2,sd) || !pc_divorce(sd)) {
  11351. script_pushint(st,0);
  11352. return SCRIPT_CMD_FAILURE;
  11353. }
  11354. script_pushint(st,1);
  11355. return SCRIPT_CMD_SUCCESS;
  11356. }
  11357. /**
  11358. * ispartneron({<char_id>})
  11359. **/
  11360. BUILDIN_FUNC(ispartneron)
  11361. {
  11362. TBL_PC *sd;
  11363. if (!script_charid2sd(2,sd) || !pc_ismarried(sd) ||
  11364. map_charid2sd(sd->status.partner_id) == NULL)
  11365. {
  11366. script_pushint(st,0);
  11367. return SCRIPT_CMD_FAILURE;
  11368. }
  11369. script_pushint(st,1);
  11370. return SCRIPT_CMD_SUCCESS;
  11371. }
  11372. /**
  11373. * getpartnerid({<char_id>})
  11374. **/
  11375. BUILDIN_FUNC(getpartnerid)
  11376. {
  11377. TBL_PC *sd;
  11378. if (!script_charid2sd(2,sd)) {
  11379. script_pushint(st,0);
  11380. return SCRIPT_CMD_FAILURE;
  11381. }
  11382. script_pushint(st,sd->status.partner_id);
  11383. return SCRIPT_CMD_SUCCESS;
  11384. }
  11385. /**
  11386. * getchildid({<char_id>})
  11387. **/
  11388. BUILDIN_FUNC(getchildid)
  11389. {
  11390. TBL_PC *sd;
  11391. if (!script_charid2sd(2,sd)) {
  11392. script_pushint(st,0);
  11393. return SCRIPT_CMD_FAILURE;
  11394. }
  11395. script_pushint(st,sd->status.child);
  11396. return SCRIPT_CMD_SUCCESS;
  11397. }
  11398. /**
  11399. * getmotherid({<char_id>})
  11400. **/
  11401. BUILDIN_FUNC(getmotherid)
  11402. {
  11403. TBL_PC *sd;
  11404. if (!script_charid2sd(2,sd)) {
  11405. script_pushint(st,0);
  11406. return SCRIPT_CMD_FAILURE;
  11407. }
  11408. script_pushint(st,sd->status.mother);
  11409. return SCRIPT_CMD_SUCCESS;
  11410. }
  11411. /**
  11412. * getfatherid({<char_id>})
  11413. **/
  11414. BUILDIN_FUNC(getfatherid)
  11415. {
  11416. TBL_PC *sd;
  11417. if (!script_charid2sd(2,sd)) {
  11418. script_pushint(st,0);
  11419. return SCRIPT_CMD_FAILURE;
  11420. }
  11421. script_pushint(st,sd->status.father);
  11422. return SCRIPT_CMD_SUCCESS;
  11423. }
  11424. BUILDIN_FUNC(warppartner)
  11425. {
  11426. int x,y;
  11427. unsigned short mapindex;
  11428. const char *str;
  11429. TBL_PC *sd=script_rid2sd(st);
  11430. TBL_PC *p_sd=NULL;
  11431. if(sd==NULL || !pc_ismarried(sd) ||
  11432. (p_sd=map_charid2sd(sd->status.partner_id)) == NULL) {
  11433. script_pushint(st,0);
  11434. return SCRIPT_CMD_SUCCESS;
  11435. }
  11436. str=script_getstr(st,2);
  11437. x=script_getnum(st,3);
  11438. y=script_getnum(st,4);
  11439. mapindex = mapindex_name2id(str);
  11440. if (mapindex) {
  11441. pc_setpos(p_sd,mapindex,x,y,CLR_OUTSIGHT);
  11442. script_pushint(st,1);
  11443. } else
  11444. script_pushint(st,0);
  11445. return SCRIPT_CMD_SUCCESS;
  11446. }
  11447. /*================================================
  11448. * Script for Displaying MOB Information [Valaris]
  11449. *------------------------------------------------*/
  11450. BUILDIN_FUNC(strmobinfo)
  11451. {
  11452. int num=script_getnum(st,2);
  11453. int class_=script_getnum(st,3);
  11454. if(!mobdb_checkid(class_))
  11455. {
  11456. if (num < 3) //requested a string
  11457. script_pushconststr(st,"");
  11458. else
  11459. script_pushint(st,0);
  11460. return SCRIPT_CMD_SUCCESS;
  11461. }
  11462. switch (num) {
  11463. case 1: script_pushstrcopy(st,mob_db(class_)->name); break;
  11464. case 2: script_pushstrcopy(st,mob_db(class_)->jname); break;
  11465. case 3: script_pushint(st,mob_db(class_)->lv); break;
  11466. case 4: script_pushint(st,mob_db(class_)->status.max_hp); break;
  11467. case 5: script_pushint(st,mob_db(class_)->status.max_sp); break;
  11468. case 6: script_pushint(st,mob_db(class_)->base_exp); break;
  11469. case 7: script_pushint(st,mob_db(class_)->job_exp); break;
  11470. default:
  11471. script_pushint(st,0);
  11472. break;
  11473. }
  11474. return SCRIPT_CMD_SUCCESS;
  11475. }
  11476. /*==========================================
  11477. * Summon guardians [Valaris]
  11478. * guardian("<map name>",<x>,<y>,"<name to show>",<mob id>{,"<event label>"}{,<guardian index>}) -> <id>
  11479. *------------------------------------------*/
  11480. BUILDIN_FUNC(guardian)
  11481. {
  11482. int class_=0,x=0,y=0,guardian=0;
  11483. const char *str,*mapname,*evt="";
  11484. bool has_index = false;
  11485. mapname =script_getstr(st,2);
  11486. x =script_getnum(st,3);
  11487. y =script_getnum(st,4);
  11488. str =script_getstr(st,5);
  11489. class_=script_getnum(st,6);
  11490. if( script_hasdata(st,8) )
  11491. {// "<event label>",<guardian index>
  11492. evt=script_getstr(st,7);
  11493. guardian=script_getnum(st,8);
  11494. has_index = true;
  11495. } else if( script_hasdata(st,7) ){
  11496. struct script_data *data = script_getdata(st,7);
  11497. get_val(st,data);
  11498. if( data_isstring(data) )
  11499. {// "<event label>"
  11500. evt=script_getstr(st,7);
  11501. } else if( data_isint(data) )
  11502. {// <guardian index>
  11503. guardian=script_getnum(st,7);
  11504. has_index = true;
  11505. } else {
  11506. ShowError("script:guardian: invalid data type for argument #6 (from 1)\n");
  11507. script_reportdata(data);
  11508. return SCRIPT_CMD_FAILURE;
  11509. }
  11510. }
  11511. check_event(st, evt);
  11512. script_pushint(st, mob_spawn_guardian(mapname,x,y,str,class_,evt,guardian,has_index));
  11513. return SCRIPT_CMD_SUCCESS;
  11514. }
  11515. /*==========================================
  11516. * Invisible Walls [Zephyrus]
  11517. *------------------------------------------*/
  11518. BUILDIN_FUNC(setwall)
  11519. {
  11520. const char *mapname, *name;
  11521. int x, y, m, size, dir;
  11522. bool shootable;
  11523. mapname = script_getstr(st,2);
  11524. x = script_getnum(st,3);
  11525. y = script_getnum(st,4);
  11526. size = script_getnum(st,5);
  11527. dir = script_getnum(st,6);
  11528. shootable = script_getnum(st,7);
  11529. name = script_getstr(st,8);
  11530. if( (m = map_mapname2mapid(mapname)) < 0 )
  11531. return SCRIPT_CMD_SUCCESS; // Invalid Map
  11532. map_iwall_set(m, x, y, size, dir, shootable, name);
  11533. return SCRIPT_CMD_SUCCESS;
  11534. }
  11535. BUILDIN_FUNC(delwall)
  11536. {
  11537. const char *name = script_getstr(st,2);
  11538. map_iwall_remove(name);
  11539. return SCRIPT_CMD_SUCCESS;
  11540. }
  11541. /// Retrieves various information about the specified guardian.
  11542. ///
  11543. /// guardianinfo("<map_name>", <index>, <type>) -> <value>
  11544. /// type: 0 - whether it is deployed or not
  11545. /// 1 - maximum hp
  11546. /// 2 - current hp
  11547. ///
  11548. BUILDIN_FUNC(guardianinfo)
  11549. {
  11550. const char* mapname = mapindex_getmapname(script_getstr(st,2),NULL);
  11551. int id = script_getnum(st,3);
  11552. int type = script_getnum(st,4);
  11553. struct guild_castle* gc = guild_mapname2gc(mapname);
  11554. struct mob_data* gd;
  11555. if( gc == NULL || id < 0 || id >= MAX_GUARDIANS )
  11556. {
  11557. script_pushint(st,-1);
  11558. return SCRIPT_CMD_SUCCESS;
  11559. }
  11560. if( type == 0 )
  11561. script_pushint(st, gc->guardian[id].visible);
  11562. else
  11563. if( !gc->guardian[id].visible )
  11564. script_pushint(st,-1);
  11565. else
  11566. if( (gd = map_id2md(gc->guardian[id].id)) == NULL )
  11567. script_pushint(st,-1);
  11568. else
  11569. {
  11570. if ( type == 1 ) script_pushint(st,gd->status.max_hp);
  11571. else if( type == 2 ) script_pushint(st,gd->status.hp);
  11572. else
  11573. script_pushint(st,-1);
  11574. }
  11575. return SCRIPT_CMD_SUCCESS;
  11576. }
  11577. /*==========================================
  11578. * Get the item name by item_id or null
  11579. *------------------------------------------*/
  11580. BUILDIN_FUNC(getitemname)
  11581. {
  11582. unsigned short item_id = 0;
  11583. struct item_data *i_data;
  11584. char *item_name;
  11585. struct script_data *data;
  11586. data=script_getdata(st,2);
  11587. get_val(st,data);
  11588. if( data_isstring(data) ){
  11589. const char *name=conv_str(st,data);
  11590. struct item_data *item_data = itemdb_searchname(name);
  11591. if( item_data )
  11592. item_id=item_data->nameid;
  11593. }else
  11594. item_id=conv_num(st,data);
  11595. i_data = itemdb_exists(item_id);
  11596. if (i_data == NULL)
  11597. {
  11598. script_pushconststr(st,"null");
  11599. return SCRIPT_CMD_SUCCESS;
  11600. }
  11601. item_name=(char *)aMalloc(ITEM_NAME_LENGTH*sizeof(char));
  11602. memcpy(item_name, i_data->jname, ITEM_NAME_LENGTH);
  11603. script_pushstr(st,item_name);
  11604. return SCRIPT_CMD_SUCCESS;
  11605. }
  11606. /*==========================================
  11607. * Returns number of slots an item has. [Skotlex]
  11608. *------------------------------------------*/
  11609. BUILDIN_FUNC(getitemslots)
  11610. {
  11611. unsigned short item_id;
  11612. struct item_data *i_data;
  11613. item_id=script_getnum(st,2);
  11614. i_data = itemdb_exists(item_id);
  11615. if (i_data)
  11616. script_pushint(st,i_data->slot);
  11617. else
  11618. script_pushint(st,-1);
  11619. return SCRIPT_CMD_SUCCESS;
  11620. }
  11621. // TODO: add matk here if needed/once we get rid of RENEWAL
  11622. /*==========================================
  11623. * Returns some values of an item [Lupus]
  11624. * Price, Weight, etc...
  11625. getiteminfo(itemID,n), where n
  11626. 0 value_buy;
  11627. 1 value_sell;
  11628. 2 type;
  11629. 3 maxchance = Max drop chance of this item e.g. 1 = 0.01% , etc..
  11630. if = 0, then monsters don't drop it at all (rare or a quest item)
  11631. if = -1, then this item is sold in NPC shops only
  11632. 4 sex;
  11633. 5 equip;
  11634. 6 weight;
  11635. 7 atk;
  11636. 8 def;
  11637. 9 range;
  11638. 10 slot;
  11639. 11 look;
  11640. 12 elv;
  11641. 13 wlv;
  11642. 14 view id
  11643. 15 eLvmax
  11644. 16 matk (renewal)
  11645. *------------------------------------------*/
  11646. BUILDIN_FUNC(getiteminfo)
  11647. {
  11648. unsigned short item_id,n;
  11649. struct item_data *i_data;
  11650. item_id = script_getnum(st,2);
  11651. n = script_getnum(st,3);
  11652. i_data = itemdb_exists(item_id);
  11653. if (i_data && n <= 16) {
  11654. int *item_arr = (int*)&i_data->value_buy;
  11655. #ifndef RENEWAL
  11656. if (n == 16)
  11657. script_pushint(st,0);
  11658. else
  11659. #endif
  11660. script_pushint(st,item_arr[n]);
  11661. } else
  11662. script_pushint(st,-1);
  11663. return SCRIPT_CMD_SUCCESS;
  11664. }
  11665. /*==========================================
  11666. * Set some values of an item [Lupus]
  11667. * Price, Weight, etc...
  11668. setiteminfo(itemID,n,Value), where n
  11669. 0 value_buy;
  11670. 1 value_sell;
  11671. 2 type;
  11672. 3 maxchance = Max drop chance of this item e.g. 1 = 0.01% , etc..
  11673. if = 0, then monsters don't drop it at all (rare or a quest item)
  11674. if = -1, then this item is sold in NPC shops only
  11675. 4 sex;
  11676. 5 equip;
  11677. 6 weight;
  11678. 7 atk;
  11679. 8 def;
  11680. 9 range;
  11681. 10 slot;
  11682. 11 look;
  11683. 12 elv;
  11684. 13 wlv;
  11685. 14 view id
  11686. * Returns Value or -1 if the wrong field's been set
  11687. *------------------------------------------*/
  11688. BUILDIN_FUNC(setiteminfo)
  11689. {
  11690. unsigned short item_id;
  11691. int n,value;
  11692. struct item_data *i_data;
  11693. item_id = script_getnum(st,2);
  11694. n = script_getnum(st,3);
  11695. value = script_getnum(st,4);
  11696. i_data = itemdb_exists(item_id);
  11697. if (i_data && n>=0 && n<=14) {
  11698. int *item_arr = (int*)&i_data->value_buy;
  11699. item_arr[n] = value;
  11700. script_pushint(st,value);
  11701. } else
  11702. script_pushint(st,-1);
  11703. return SCRIPT_CMD_SUCCESS;
  11704. }
  11705. /*==========================================
  11706. * Returns value from equipped item slot n [Lupus]
  11707. getequipcardid(num,slot)
  11708. where
  11709. num = eqip position slot
  11710. slot = 0,1,2,3 (Card Slot N)
  11711. This func returns CARD ID, 255,254,-255 (for card 0, if the item is produced)
  11712. it's useful when you want to check item cards or if it's signed
  11713. Useful for such quests as "Sign this refined item with players name" etc
  11714. Hat[0] +4 -> Player's Hat[0] +4
  11715. *------------------------------------------*/
  11716. BUILDIN_FUNC(getequipcardid)
  11717. {
  11718. int i=-1,num,slot;
  11719. TBL_PC *sd;
  11720. num=script_getnum(st,2);
  11721. slot=script_getnum(st,3);
  11722. sd=script_rid2sd(st);
  11723. if (equip_index_check(num))
  11724. i=pc_checkequip(sd,equip_bitmask[num]);
  11725. if(i >= 0 && slot>=0 && slot<4)
  11726. script_pushint(st,sd->inventory.u.items_inventory[i].card[slot]);
  11727. else
  11728. script_pushint(st,0);
  11729. return SCRIPT_CMD_SUCCESS;
  11730. }
  11731. /*==========================================
  11732. * petskillbonus [Valaris] //Rewritten by [Skotlex]
  11733. *------------------------------------------*/
  11734. BUILDIN_FUNC(petskillbonus)
  11735. {
  11736. struct pet_data *pd;
  11737. TBL_PC *sd = script_rid2sd(st);
  11738. if(sd == NULL || sd->pd == NULL)
  11739. return SCRIPT_CMD_FAILURE;
  11740. pd = sd->pd;
  11741. if (pd->bonus)
  11742. { //Clear previous bonus
  11743. if (pd->bonus->timer != INVALID_TIMER)
  11744. delete_timer(pd->bonus->timer, pet_skill_bonus_timer);
  11745. } else //init
  11746. pd->bonus = (struct pet_bonus *) aMalloc(sizeof(struct pet_bonus));
  11747. pd->bonus->type = script_getnum(st,2);
  11748. pd->bonus->val = script_getnum(st,3);
  11749. pd->bonus->duration = script_getnum(st,4);
  11750. pd->bonus->delay = script_getnum(st,5);
  11751. if (pd->state.skillbonus == 1)
  11752. pd->state.skillbonus = 0; // waiting state
  11753. // wait for timer to start
  11754. if (battle_config.pet_equip_required && pd->pet.equip == 0)
  11755. pd->bonus->timer = INVALID_TIMER;
  11756. else
  11757. pd->bonus->timer = add_timer(gettick()+pd->bonus->delay*1000, pet_skill_bonus_timer, sd->bl.id, 0);
  11758. return SCRIPT_CMD_SUCCESS;
  11759. }
  11760. /*==========================================
  11761. * pet looting [Valaris] //Rewritten by [Skotlex]
  11762. *------------------------------------------*/
  11763. BUILDIN_FUNC(petloot)
  11764. {
  11765. int max;
  11766. struct pet_data *pd;
  11767. TBL_PC *sd=script_rid2sd(st);
  11768. if(sd==NULL || sd->pd==NULL)
  11769. return SCRIPT_CMD_SUCCESS;
  11770. max=script_getnum(st,2);
  11771. if(max < 1)
  11772. max = 1; //Let'em loot at least 1 item.
  11773. else if (max > MAX_PETLOOT_SIZE)
  11774. max = MAX_PETLOOT_SIZE;
  11775. pd = sd->pd;
  11776. if (pd->loot != NULL)
  11777. { //Release whatever was there already and reallocate memory
  11778. pet_lootitem_drop(pd, pd->master);
  11779. aFree(pd->loot->item);
  11780. }
  11781. else
  11782. pd->loot = (struct pet_loot *)aMalloc(sizeof(struct pet_loot));
  11783. pd->loot->item = (struct item *)aCalloc(max,sizeof(struct item));
  11784. pd->loot->max=max;
  11785. pd->loot->count = 0;
  11786. pd->loot->weight = 0;
  11787. return SCRIPT_CMD_SUCCESS;
  11788. }
  11789. /*==========================================
  11790. * Set arrays with info of all sd inventory :
  11791. * @inventorylist_id, @inventorylist_amount, @inventorylist_equip,
  11792. * @inventorylist_refine, @inventorylist_identify, @inventorylist_attribute,
  11793. * @inventorylist_card(0..3), @inventorylist_expire
  11794. * @inventorylist_count = scalar
  11795. *------------------------------------------*/
  11796. BUILDIN_FUNC(getinventorylist)
  11797. {
  11798. TBL_PC *sd;
  11799. char card_var[NAME_LENGTH];
  11800. int i,j=0,k;
  11801. if (!script_charid2sd(2,sd))
  11802. return SCRIPT_CMD_FAILURE;
  11803. for(i=0;i<MAX_INVENTORY;i++){
  11804. if(sd->inventory.u.items_inventory[i].nameid > 0 && sd->inventory.u.items_inventory[i].amount > 0){
  11805. pc_setreg(sd,reference_uid(add_str("@inventorylist_id"), j),sd->inventory.u.items_inventory[i].nameid);
  11806. pc_setreg(sd,reference_uid(add_str("@inventorylist_amount"), j),sd->inventory.u.items_inventory[i].amount);
  11807. pc_setreg(sd,reference_uid(add_str("@inventorylist_equip"), j),sd->inventory.u.items_inventory[i].equip);
  11808. pc_setreg(sd,reference_uid(add_str("@inventorylist_refine"), j),sd->inventory.u.items_inventory[i].refine);
  11809. pc_setreg(sd,reference_uid(add_str("@inventorylist_identify"), j),sd->inventory.u.items_inventory[i].identify);
  11810. pc_setreg(sd,reference_uid(add_str("@inventorylist_attribute"), j),sd->inventory.u.items_inventory[i].attribute);
  11811. for (k = 0; k < MAX_SLOTS; k++)
  11812. {
  11813. sprintf(card_var, "@inventorylist_card%d",k+1);
  11814. pc_setreg(sd,reference_uid(add_str(card_var), j),sd->inventory.u.items_inventory[i].card[k]);
  11815. }
  11816. pc_setreg(sd,reference_uid(add_str("@inventorylist_expire"), j),sd->inventory.u.items_inventory[i].expire_time);
  11817. pc_setreg(sd,reference_uid(add_str("@inventorylist_bound"), j),sd->inventory.u.items_inventory[i].bound);
  11818. j++;
  11819. }
  11820. }
  11821. pc_setreg(sd,add_str("@inventorylist_count"),j);
  11822. return SCRIPT_CMD_SUCCESS;
  11823. }
  11824. /**
  11825. * getskilllist ({<char_id>});
  11826. **/
  11827. BUILDIN_FUNC(getskilllist)
  11828. {
  11829. TBL_PC *sd;
  11830. int i,j=0;
  11831. if (!script_charid2sd(2,sd))
  11832. return SCRIPT_CMD_FAILURE;
  11833. for(i=0;i<MAX_SKILL;i++){
  11834. if(sd->status.skill[i].id > 0 && sd->status.skill[i].lv > 0){
  11835. pc_setreg(sd,reference_uid(add_str("@skilllist_id"), j),sd->status.skill[i].id);
  11836. pc_setreg(sd,reference_uid(add_str("@skilllist_lv"), j),sd->status.skill[i].lv);
  11837. pc_setreg(sd,reference_uid(add_str("@skilllist_flag"), j),sd->status.skill[i].flag);
  11838. j++;
  11839. }
  11840. }
  11841. pc_setreg(sd,add_str("@skilllist_count"),j);
  11842. return SCRIPT_CMD_SUCCESS;
  11843. }
  11844. /**
  11845. * clearitem ({<char_id>});
  11846. **/
  11847. BUILDIN_FUNC(clearitem)
  11848. {
  11849. TBL_PC *sd;
  11850. int i;
  11851. if (!script_charid2sd(2,sd))
  11852. return SCRIPT_CMD_FAILURE;
  11853. for (i=0; i<MAX_INVENTORY; i++) {
  11854. if (sd->inventory.u.items_inventory[i].amount) {
  11855. pc_delitem(sd, i, sd->inventory.u.items_inventory[i].amount, 0, 0, LOG_TYPE_SCRIPT);
  11856. }
  11857. }
  11858. return SCRIPT_CMD_SUCCESS;
  11859. }
  11860. /**
  11861. * Disguise Player (returns Mob/NPC ID if success, 0 on fail)
  11862. * disguise <Monster ID>{,<char_id>};
  11863. **/
  11864. BUILDIN_FUNC(disguise)
  11865. {
  11866. int id;
  11867. TBL_PC* sd;
  11868. if (!script_charid2sd(3,sd))
  11869. return SCRIPT_CMD_FAILURE;
  11870. id = script_getnum(st,2);
  11871. if (mobdb_checkid(id) || npcdb_checkid(id)) {
  11872. pc_disguise(sd, id);
  11873. script_pushint(st,id);
  11874. } else
  11875. script_pushint(st,0);
  11876. return SCRIPT_CMD_SUCCESS;
  11877. }
  11878. /**
  11879. * Undisguise Player (returns 1 if success, 0 on fail)
  11880. * undisguise {<char_id>};
  11881. **/
  11882. BUILDIN_FUNC(undisguise)
  11883. {
  11884. TBL_PC* sd;
  11885. if (!script_charid2sd(2,sd))
  11886. return SCRIPT_CMD_FAILURE;
  11887. if (sd->disguise) {
  11888. pc_disguise(sd, 0);
  11889. script_pushint(st,0);
  11890. } else {
  11891. script_pushint(st,1);
  11892. }
  11893. return SCRIPT_CMD_SUCCESS;
  11894. }
  11895. /*==========================================
  11896. * Transform a bl to another _class,
  11897. * @type unused
  11898. *------------------------------------------*/
  11899. BUILDIN_FUNC(classchange)
  11900. {
  11901. int _class,type;
  11902. struct block_list *bl=map_id2bl(st->oid);
  11903. if (bl==NULL)
  11904. return SCRIPT_CMD_FAILURE;
  11905. _class=script_getnum(st,2);
  11906. type=script_getnum(st,3);
  11907. clif_class_change(bl,_class,type);
  11908. return SCRIPT_CMD_SUCCESS;
  11909. }
  11910. /*==========================================
  11911. * Display an effect
  11912. *------------------------------------------*/
  11913. BUILDIN_FUNC(misceffect)
  11914. {
  11915. int type;
  11916. type=script_getnum(st,2);
  11917. if(st->oid && st->oid != fake_nd->bl.id) {
  11918. struct block_list *bl = map_id2bl(st->oid);
  11919. if (bl)
  11920. clif_specialeffect(bl,type,AREA);
  11921. } else{
  11922. TBL_PC *sd=script_rid2sd(st);
  11923. if(sd)
  11924. clif_specialeffect(&sd->bl,type,AREA);
  11925. }
  11926. return SCRIPT_CMD_SUCCESS;
  11927. }
  11928. /*==========================================
  11929. * Play a BGM on a single client [Rikter/Yommy]
  11930. *------------------------------------------*/
  11931. BUILDIN_FUNC(playBGM)
  11932. {
  11933. struct map_session_data* sd;
  11934. if( ( sd = script_rid2sd(st) ) != NULL ) {
  11935. clif_playBGM(sd, script_getstr(st,2));
  11936. }
  11937. return SCRIPT_CMD_SUCCESS;
  11938. }
  11939. static int playBGM_sub(struct block_list* bl,va_list ap)
  11940. {
  11941. const char* name = va_arg(ap,const char*);
  11942. clif_playBGM(BL_CAST(BL_PC, bl), name);
  11943. return 0;
  11944. }
  11945. static int playBGM_foreachpc_sub(struct map_session_data* sd, va_list args)
  11946. {
  11947. const char* name = va_arg(args, const char*);
  11948. clif_playBGM(sd, name);
  11949. return 0;
  11950. }
  11951. /*==========================================
  11952. * Play a BGM on multiple client [Rikter/Yommy]
  11953. *------------------------------------------*/
  11954. BUILDIN_FUNC(playBGMall)
  11955. {
  11956. const char* name;
  11957. name = script_getstr(st,2);
  11958. if( script_hasdata(st,7) ) {// specified part of map
  11959. const char* mapname = script_getstr(st,3);
  11960. int x0 = script_getnum(st,4);
  11961. int y0 = script_getnum(st,5);
  11962. int x1 = script_getnum(st,6);
  11963. int y1 = script_getnum(st,7);
  11964. map_foreachinarea(playBGM_sub, map_mapname2mapid(mapname), x0, y0, x1, y1, BL_PC, name);
  11965. }
  11966. else if( script_hasdata(st,3) ) {// entire map
  11967. const char* mapname = script_getstr(st,3);
  11968. map_foreachinmap(playBGM_sub, map_mapname2mapid(mapname), BL_PC, name);
  11969. }
  11970. else {// entire server
  11971. map_foreachpc(&playBGM_foreachpc_sub, name);
  11972. }
  11973. return SCRIPT_CMD_SUCCESS;
  11974. }
  11975. /*==========================================
  11976. * Play a .wav sound for sd
  11977. *------------------------------------------*/
  11978. BUILDIN_FUNC(soundeffect)
  11979. {
  11980. TBL_PC* sd = script_rid2sd(st);
  11981. const char* name = script_getstr(st,2);
  11982. int type = script_getnum(st,3);
  11983. if(sd) {
  11984. clif_soundeffect(sd,&sd->bl,name,type);
  11985. }
  11986. return SCRIPT_CMD_SUCCESS;
  11987. }
  11988. int soundeffect_sub(struct block_list* bl,va_list ap)
  11989. {
  11990. char* name = va_arg(ap,char*);
  11991. int type = va_arg(ap,int);
  11992. clif_soundeffect((TBL_PC *)bl, bl, name, type);
  11993. return 0;
  11994. }
  11995. /*==========================================
  11996. * Play a sound effect (.wav) on multiple clients
  11997. * soundeffectall "<filepath>",<type>{,"<map name>"}{,<x0>,<y0>,<x1>,<y1>};
  11998. *------------------------------------------*/
  11999. BUILDIN_FUNC(soundeffectall)
  12000. {
  12001. struct block_list* bl;
  12002. const char* name;
  12003. int type;
  12004. bl = (st->rid) ? &(script_rid2sd(st)->bl) : map_id2bl(st->oid);
  12005. if (!bl)
  12006. return SCRIPT_CMD_SUCCESS;
  12007. name = script_getstr(st,2);
  12008. type = script_getnum(st,3);
  12009. //FIXME: enumerating map squares (map_foreach) is slower than enumerating the list of online players (map_foreachpc?) [ultramage]
  12010. if(!script_hasdata(st,4))
  12011. { // area around
  12012. clif_soundeffectall(bl, name, type, AREA);
  12013. }
  12014. else
  12015. if(!script_hasdata(st,5))
  12016. { // entire map
  12017. const char* mapname = script_getstr(st,4);
  12018. map_foreachinmap(soundeffect_sub, map_mapname2mapid(mapname), BL_PC, name, type);
  12019. }
  12020. else
  12021. if(script_hasdata(st,8))
  12022. { // specified part of map
  12023. const char* mapname = script_getstr(st,4);
  12024. int x0 = script_getnum(st,5);
  12025. int y0 = script_getnum(st,6);
  12026. int x1 = script_getnum(st,7);
  12027. int y1 = script_getnum(st,8);
  12028. map_foreachinarea(soundeffect_sub, map_mapname2mapid(mapname), x0, y0, x1, y1, BL_PC, name, type);
  12029. }
  12030. else
  12031. {
  12032. ShowError("buildin_soundeffectall: insufficient arguments for specific area broadcast.\n");
  12033. }
  12034. return SCRIPT_CMD_SUCCESS;
  12035. }
  12036. /*==========================================
  12037. * pet status recovery [Valaris] / Rewritten by [Skotlex]
  12038. *------------------------------------------*/
  12039. BUILDIN_FUNC(petrecovery)
  12040. {
  12041. struct pet_data *pd;
  12042. TBL_PC *sd=script_rid2sd(st);
  12043. int sc;
  12044. if(sd == NULL || sd->pd == NULL)
  12045. return SCRIPT_CMD_FAILURE;
  12046. sc = script_getnum(st,2);
  12047. if (sc <= SC_NONE || sc >= SC_MAX) {
  12048. ShowError("buildin_petrecovery: Invalid SC type: %d\n", sc);
  12049. return SCRIPT_CMD_FAILURE;
  12050. }
  12051. pd = sd->pd;
  12052. if (pd->recovery)
  12053. { //Halt previous bonus
  12054. if (pd->recovery->timer != INVALID_TIMER)
  12055. delete_timer(pd->recovery->timer, pet_recovery_timer);
  12056. } else //Init
  12057. pd->recovery = (struct pet_recovery *)aMalloc(sizeof(struct pet_recovery));
  12058. pd->recovery->type = (sc_type)sc;
  12059. pd->recovery->delay = script_getnum(st,3);
  12060. pd->recovery->timer = INVALID_TIMER;
  12061. return SCRIPT_CMD_SUCCESS;
  12062. }
  12063. /*==========================================
  12064. * pet attack skills [Valaris] //Rewritten by [Skotlex]
  12065. *------------------------------------------*/
  12066. /// petskillattack <skill id>,<level>,<rate>,<bonusrate>
  12067. /// petskillattack "<skill name>",<level>,<rate>,<bonusrate>
  12068. BUILDIN_FUNC(petskillattack)
  12069. {
  12070. struct pet_data *pd;
  12071. struct script_data *data;
  12072. TBL_PC *sd = script_rid2sd(st);
  12073. int id = 0;
  12074. if(sd == NULL || sd->pd == NULL)
  12075. return SCRIPT_CMD_FAILURE;
  12076. data = script_getdata(st, 2);
  12077. get_val(st, data);
  12078. id = (data_isstring(data) ? skill_name2id(script_getstr(st,2)) : skill_get_index(script_getnum(st,2)));
  12079. if (!id) {
  12080. ShowError("buildin_petskillattack: Invalid skill defined!\n");
  12081. return SCRIPT_CMD_FAILURE;
  12082. }
  12083. pd = sd->pd;
  12084. if (pd->a_skill == NULL)
  12085. pd->a_skill = (struct pet_skill_attack *)aMalloc(sizeof(struct pet_skill_attack));
  12086. pd->a_skill->id = id;
  12087. pd->a_skill->damage = 0;
  12088. pd->a_skill->lv = (unsigned short)min(script_getnum(st,3), skill_get_max(pd->a_skill->id));
  12089. pd->a_skill->div_ = 0;
  12090. pd->a_skill->rate = script_getnum(st,4);
  12091. pd->a_skill->bonusrate = script_getnum(st,5);
  12092. return SCRIPT_CMD_SUCCESS;
  12093. }
  12094. /*==========================================
  12095. * pet attack skills [Valaris]
  12096. *------------------------------------------*/
  12097. /// petskillattack2 <skill id>,<damage>,<div>,<rate>,<bonusrate>
  12098. /// petskillattack2 "<skill name>",<damage>,<div>,<rate>,<bonusrate>
  12099. BUILDIN_FUNC(petskillattack2)
  12100. {
  12101. struct pet_data *pd;
  12102. struct script_data *data;
  12103. TBL_PC *sd = script_rid2sd(st);
  12104. int id = 0;
  12105. if(sd == NULL || sd->pd == NULL)
  12106. return SCRIPT_CMD_FAILURE;
  12107. data = script_getdata(st, 2);
  12108. get_val(st, data);
  12109. id = (data_isstring(data) ? skill_name2id(script_getstr(st,2)) : skill_get_index(script_getnum(st,2)));
  12110. if (!id) {
  12111. ShowError("buildin_petskillattack2: Invalid skill defined!\n");
  12112. return SCRIPT_CMD_FAILURE;
  12113. }
  12114. pd = sd->pd;
  12115. if (pd->a_skill == NULL)
  12116. pd->a_skill = (struct pet_skill_attack *)aMalloc(sizeof(struct pet_skill_attack));
  12117. pd->a_skill->id = id;
  12118. pd->a_skill->damage = script_getnum(st,3); // Fixed damage
  12119. pd->a_skill->lv = (unsigned short)skill_get_max(pd->a_skill->id); // Adjust to max skill level
  12120. pd->a_skill->div_ = script_getnum(st,4);
  12121. pd->a_skill->rate = script_getnum(st,5);
  12122. pd->a_skill->bonusrate = script_getnum(st,6);
  12123. return SCRIPT_CMD_SUCCESS;
  12124. }
  12125. /*==========================================
  12126. * pet support skills [Skotlex]
  12127. *------------------------------------------*/
  12128. /// petskillsupport <skill id>,<level>,<delay>,<hp>,<sp>
  12129. /// petskillsupport "<skill name>",<level>,<delay>,<hp>,<sp>
  12130. BUILDIN_FUNC(petskillsupport)
  12131. {
  12132. struct pet_data *pd;
  12133. struct script_data *data;
  12134. TBL_PC *sd = script_rid2sd(st);
  12135. int id = 0;
  12136. if(sd == NULL || sd->pd == NULL)
  12137. return SCRIPT_CMD_FAILURE;
  12138. data = script_getdata(st, 2);
  12139. get_val(st, data);
  12140. id = (data_isstring(data) ? skill_name2id(script_getstr(st,2)) : skill_get_index(script_getnum(st,2)));
  12141. if (!id) {
  12142. ShowError("buildin_petskillsupport: Invalid skill defined!\n");
  12143. return SCRIPT_CMD_FAILURE;
  12144. }
  12145. pd = sd->pd;
  12146. if (pd->s_skill)
  12147. { //Clear previous skill
  12148. if (pd->s_skill->timer != INVALID_TIMER)
  12149. {
  12150. if (pd->s_skill->id)
  12151. delete_timer(pd->s_skill->timer, pet_skill_support_timer);
  12152. else
  12153. delete_timer(pd->s_skill->timer, pet_heal_timer);
  12154. }
  12155. } else //init memory
  12156. pd->s_skill = (struct pet_skill_support *) aMalloc(sizeof(struct pet_skill_support));
  12157. pd->s_skill->id = id;
  12158. pd->s_skill->lv = script_getnum(st,3);
  12159. pd->s_skill->delay = script_getnum(st,4);
  12160. pd->s_skill->hp = script_getnum(st,5);
  12161. pd->s_skill->sp = script_getnum(st,6);
  12162. //Use delay as initial offset to avoid skill/heal exploits
  12163. if (battle_config.pet_equip_required && pd->pet.equip == 0)
  12164. pd->s_skill->timer = INVALID_TIMER;
  12165. else
  12166. pd->s_skill->timer = add_timer(gettick()+pd->s_skill->delay*1000,pet_skill_support_timer,sd->bl.id,0);
  12167. return SCRIPT_CMD_SUCCESS;
  12168. }
  12169. /*==========================================
  12170. * Scripted skill effects [Celest]
  12171. *------------------------------------------*/
  12172. /// skilleffect <skill id>,<level>
  12173. /// skilleffect "<skill name>",<level>
  12174. BUILDIN_FUNC(skilleffect)
  12175. {
  12176. TBL_PC *sd;
  12177. uint16 skill_id, skill_lv;
  12178. struct script_data *data = script_getdata(st, 2);
  12179. get_val(st, data); // Convert into value in case of a variable
  12180. skill_id = ( data_isstring(data) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  12181. skill_lv = script_getnum(st,3);
  12182. sd = script_rid2sd(st);
  12183. /* Ensure we're standing because the following packet causes the client to virtually set the char to stand,
  12184. * which leaves the server thinking it still is sitting. */
  12185. if( pc_issit(sd) && pc_setstand(sd, false) ) {
  12186. skill_sit(sd, 0);
  12187. clif_standing(&sd->bl);
  12188. }
  12189. clif_skill_nodamage(&sd->bl,&sd->bl,skill_id,skill_lv,1);
  12190. return SCRIPT_CMD_SUCCESS;
  12191. }
  12192. /*==========================================
  12193. * NPC skill effects [Valaris]
  12194. *------------------------------------------*/
  12195. /// npcskilleffect <skill id>,<level>,<x>,<y>
  12196. /// npcskilleffect "<skill name>",<level>,<x>,<y>
  12197. BUILDIN_FUNC(npcskilleffect)
  12198. {
  12199. struct block_list *bl= map_id2bl(st->oid);
  12200. uint16 skill_id, skill_lv;
  12201. int x, y;
  12202. struct script_data *data = script_getdata(st, 2);
  12203. get_val(st, data); // Convert into value in case of a variable
  12204. skill_id=( data_isstring(data) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  12205. skill_lv=script_getnum(st,3);
  12206. x=script_getnum(st,4);
  12207. y=script_getnum(st,5);
  12208. if (bl)
  12209. clif_skill_poseffect(bl,skill_id,skill_lv,x,y,gettick());
  12210. return SCRIPT_CMD_SUCCESS;
  12211. }
  12212. /*==========================================
  12213. * Special effects [Valaris]
  12214. *------------------------------------------*/
  12215. BUILDIN_FUNC(specialeffect)
  12216. {
  12217. struct block_list *bl=map_id2bl(st->oid);
  12218. int type = script_getnum(st,2);
  12219. enum send_target target = script_hasdata(st,3) ? (send_target)script_getnum(st,3) : AREA;
  12220. if(bl==NULL)
  12221. return SCRIPT_CMD_SUCCESS;
  12222. if( script_hasdata(st,4) )
  12223. {
  12224. TBL_NPC *nd = npc_name2id(script_getstr(st,4));
  12225. if(nd)
  12226. clif_specialeffect(&nd->bl, type, target);
  12227. }
  12228. else
  12229. {
  12230. if (target == SELF) {
  12231. TBL_PC *sd=script_rid2sd(st);
  12232. if (sd)
  12233. clif_specialeffect_single(bl,type,sd->fd);
  12234. } else {
  12235. clif_specialeffect(bl, type, target);
  12236. }
  12237. }
  12238. return SCRIPT_CMD_SUCCESS;
  12239. }
  12240. BUILDIN_FUNC(specialeffect2)
  12241. {
  12242. TBL_PC *sd=script_rid2sd(st);
  12243. int type = script_getnum(st,2);
  12244. enum send_target target = script_hasdata(st,3) ? (send_target)script_getnum(st,3) : AREA;
  12245. if( script_hasdata(st,4) )
  12246. sd = map_nick2sd(script_getstr(st,4));
  12247. if (sd)
  12248. clif_specialeffect(&sd->bl, type, target);
  12249. return SCRIPT_CMD_SUCCESS;
  12250. }
  12251. /**
  12252. * nude({<char_id>});
  12253. * @author [Valaris]
  12254. **/
  12255. BUILDIN_FUNC(nude)
  12256. {
  12257. TBL_PC *sd;
  12258. int i, calcflag = 0;
  12259. if (!script_charid2sd(2,sd))
  12260. return SCRIPT_CMD_FAILURE;
  12261. for( i = 0 ; i < EQI_MAX; i++ ) {
  12262. if( sd->equip_index[ i ] >= 0 ) {
  12263. if( !calcflag )
  12264. calcflag = 1;
  12265. pc_unequipitem( sd , sd->equip_index[ i ] , 2);
  12266. }
  12267. }
  12268. if( calcflag )
  12269. status_calc_pc(sd,SCO_NONE);
  12270. return SCRIPT_CMD_SUCCESS;
  12271. }
  12272. int atcommand_sub(struct script_state* st,int type) {
  12273. TBL_PC *sd, dummy_sd;
  12274. int fd;
  12275. const char *cmd;
  12276. cmd = script_getstr(st,2);
  12277. if (st->rid) {
  12278. sd = script_rid2sd(st);
  12279. fd = sd->fd;
  12280. } else { //Use a dummy character.
  12281. sd = &dummy_sd;
  12282. fd = 0;
  12283. memset(&dummy_sd, 0, sizeof(TBL_PC));
  12284. if (st->oid) {
  12285. struct block_list* bl = map_id2bl(st->oid);
  12286. memcpy(&dummy_sd.bl, bl, sizeof(struct block_list));
  12287. if (bl->type == BL_NPC)
  12288. safestrncpy(dummy_sd.status.name, ((TBL_NPC*)bl)->name, NAME_LENGTH);
  12289. sd->mapindex = (bl->m > 0) ? bl->m : mapindex_name2id(map_default.mapname);
  12290. }
  12291. // Init Group ID, Level, & permissions
  12292. sd->group_id = sd->group_level = 99;
  12293. sd->permissions |= PC_PERM_ALLPERMISSION;
  12294. }
  12295. if (!is_atcommand(fd, sd, cmd, type)) {
  12296. ShowWarning("buildin_atcommand: failed to execute command '%s'\n", cmd);
  12297. script_reportsrc(st);
  12298. return SCRIPT_CMD_FAILURE;
  12299. }
  12300. return SCRIPT_CMD_SUCCESS;
  12301. }
  12302. /*==========================================
  12303. * gmcommand [MouseJstr]
  12304. *------------------------------------------*/
  12305. BUILDIN_FUNC(atcommand) {
  12306. return atcommand_sub(st,0);
  12307. }
  12308. /** Displays a message for the player only (like system messages like "you got an apple" )
  12309. * dispbottom("<message>"{,<color>{,<char_id>}})
  12310. * @param message
  12311. * @param color Hex color default (Green)
  12312. */
  12313. BUILDIN_FUNC(dispbottom)
  12314. {
  12315. TBL_PC *sd;
  12316. int color = 0;
  12317. const char *message;
  12318. if (!script_charid2sd(4,sd))
  12319. return SCRIPT_CMD_FAILURE;
  12320. message = script_getstr(st,2);
  12321. if (script_hasdata(st,3))
  12322. color = script_getnum(st,3); // <color>
  12323. if(sd) {
  12324. if (script_hasdata(st,3))
  12325. clif_messagecolor2(sd, color, message); // [Napster]
  12326. else
  12327. clif_disp_onlyself(sd, message, (int)strlen(message));
  12328. }
  12329. return SCRIPT_CMD_SUCCESS;
  12330. }
  12331. /*===================================
  12332. * Heal portion of recovery command
  12333. *-----------------------------------*/
  12334. int recovery_sub(struct map_session_data* sd, int revive)
  12335. {
  12336. if(revive&(1|4) && pc_isdead(sd)) {
  12337. status_revive(&sd->bl, 100, 100);
  12338. clif_displaymessage(sd->fd,msg_txt(sd,16)); // You've been revived!
  12339. clif_specialeffect(&sd->bl, 77, AREA);
  12340. } else if(revive&(1|2) && !pc_isdead(sd)) {
  12341. status_percent_heal(&sd->bl, 100, 100);
  12342. clif_displaymessage(sd->fd,msg_txt(sd,680)); // You have been recovered!
  12343. }
  12344. return SCRIPT_CMD_SUCCESS;
  12345. }
  12346. /*=========================================================================
  12347. * Fully Recover a Character's HP/SP - [Capuche] & [Akinari]
  12348. * recovery <type>{,<option>,<revive_flag>{,<map name>}};
  12349. * <type> determines <option>:
  12350. * 0 : char_id
  12351. * 1 : party_id
  12352. * 2 : guild_id
  12353. * 3 : map_name
  12354. * 4 : all characters
  12355. * <revive_flag>:
  12356. * 1 : Revive and heal all players (default)
  12357. * 2 : Heal living players only
  12358. * 4 : Revive dead players only
  12359. * <map name>:
  12360. * for types 1-2 : map_name (null = all maps)
  12361. *-------------------------------------------------------------------------*/
  12362. BUILDIN_FUNC(recovery)
  12363. {
  12364. TBL_PC *sd;
  12365. int map_idx = 0, type = 0, revive = 1;
  12366. type = script_getnum(st,2);
  12367. if(script_hasdata(st,4))
  12368. revive = script_getnum(st,4);
  12369. switch(type) {
  12370. case 0:
  12371. if(script_hasdata(st,3))
  12372. sd=map_charid2sd(script_getnum(st,3));
  12373. else if((sd = script_rid2sd(st)) == NULL)
  12374. return SCRIPT_CMD_SUCCESS; //If we don't have sd by now, bail out
  12375. recovery_sub(sd, revive);
  12376. break;
  12377. case 1:
  12378. {
  12379. struct party_data* p;
  12380. //When no party given, we use invoker party
  12381. int p_id = 0, i;
  12382. if(script_hasdata(st,5)) {//Bad maps shouldn't cause issues
  12383. map_idx = map_mapname2mapid(script_getstr(st,5));
  12384. if(map_idx < 1) { //But we'll check anyways
  12385. ShowDebug("recovery: bad map name given (%s)\n", script_getstr(st,5));
  12386. return SCRIPT_CMD_FAILURE;
  12387. }
  12388. }
  12389. if(script_hasdata(st,3))
  12390. p_id = script_getnum(st,3);
  12391. else if((sd = script_rid2sd(st)))
  12392. p_id = sd->status.party_id;
  12393. p = party_search(p_id);
  12394. if(p == NULL)
  12395. return SCRIPT_CMD_SUCCESS;
  12396. for (i = 0; i < MAX_PARTY; i++) {
  12397. struct map_session_data* pl_sd;
  12398. if((!(pl_sd = p->data[i].sd) || pl_sd->status.party_id != p_id)
  12399. || (map_idx && pl_sd->bl.m != map_idx))
  12400. continue;
  12401. recovery_sub(pl_sd, revive);
  12402. }
  12403. break;
  12404. }
  12405. case 2:
  12406. {
  12407. struct guild* g;
  12408. //When no guild given, we use invoker guild
  12409. int g_id = 0, i;
  12410. if(script_hasdata(st,5)) {//Bad maps shouldn't cause issues
  12411. map_idx = map_mapname2mapid(script_getstr(st,5));
  12412. if(map_idx < 1) { //But we'll check anyways
  12413. ShowDebug("recovery: bad map name given (%s)\n", script_getstr(st,5));
  12414. return SCRIPT_CMD_FAILURE;
  12415. }
  12416. }
  12417. if(script_hasdata(st,3))
  12418. g_id = script_getnum(st,3);
  12419. else if((sd = script_rid2sd(st)))
  12420. g_id = sd->status.guild_id;
  12421. g = guild_search(g_id);
  12422. if(g == NULL)
  12423. return SCRIPT_CMD_SUCCESS;
  12424. for (i = 0; i < MAX_GUILD; i++) {
  12425. struct map_session_data* pl_sd;
  12426. if((!(pl_sd = g->member[i].sd) || pl_sd->status.guild_id != g_id)
  12427. || (map_idx && pl_sd->bl.m != map_idx))
  12428. continue;
  12429. recovery_sub(pl_sd, revive);
  12430. }
  12431. break;
  12432. }
  12433. case 3:
  12434. if(script_hasdata(st,3))
  12435. map_idx = map_mapname2mapid(script_getstr(st,3));
  12436. else if((sd = script_rid2sd(st)))
  12437. map_idx = sd->bl.m;
  12438. if(map_idx < 1)
  12439. return SCRIPT_CMD_FAILURE; //No sd and no map given - return
  12440. case 4:
  12441. {
  12442. struct s_mapiterator *iter;
  12443. struct script_data *data = script_getdata(st, 3);
  12444. get_val(st, data); // Convert into value in case of a variable
  12445. if(script_hasdata(st,3) && !data_isstring(data))
  12446. revive = script_getnum(st,3); // recovery 4,<revive_flag>;
  12447. iter = mapit_getallusers();
  12448. for (sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); sd = (TBL_PC*)mapit_next(iter)) {
  12449. if(type == 3 && sd->bl.m != map_idx)
  12450. continue;
  12451. recovery_sub(sd, revive);
  12452. }
  12453. mapit_free(iter);
  12454. break;
  12455. }
  12456. default:
  12457. ShowWarning("script: buildin_recovery: Invalid type %d\n", type);
  12458. script_pushint(st,-1);
  12459. return SCRIPT_CMD_FAILURE;
  12460. }
  12461. script_pushint(st,1); //Successfully executed without errors
  12462. return SCRIPT_CMD_SUCCESS;
  12463. }
  12464. /*==========================================
  12465. * Get your pet info: getpetinfo <type>{,<char_id>}
  12466. * n -> 0:pet_id 1:pet_class 2:pet_name
  12467. * 3:friendly 4:hungry, 5: rename flag.6:level,
  12468. * 7:game id
  12469. *------------------------------------------*/
  12470. BUILDIN_FUNC(getpetinfo)
  12471. {
  12472. TBL_PC *sd=script_rid2sd(st);
  12473. TBL_PET *pd;
  12474. int type = script_getnum(st,2);
  12475. if (!script_charid2sd(3, sd) || !(pd = sd->pd)) {
  12476. if (type == 2)
  12477. script_pushconststr(st,"null");
  12478. else
  12479. script_pushint(st,0);
  12480. return SCRIPT_CMD_SUCCESS;
  12481. }
  12482. switch(type){
  12483. case PETINFO_ID: script_pushint(st,pd->pet.pet_id); break;
  12484. case PETINFO_CLASS: script_pushint(st,pd->pet.class_); break;
  12485. case PETINFO_NAME: script_pushstrcopy(st,pd->pet.name); break;
  12486. case PETINFO_INTIMATE: script_pushint(st,pd->pet.intimate); break;
  12487. case PETINFO_HUNGRY: script_pushint(st,pd->pet.hungry); break;
  12488. case PETINFO_RENAMED: script_pushint(st,pd->pet.rename_flag); break;
  12489. case PETINFO_LEVEL: script_pushint(st,(int)pd->pet.level); break;
  12490. case PETINFO_BLOCKID: script_pushint(st,pd->bl.id); break;
  12491. default:
  12492. script_pushint(st,0);
  12493. break;
  12494. }
  12495. return SCRIPT_CMD_SUCCESS;
  12496. }
  12497. /*==========================================
  12498. * Get your homunculus info: gethominfo <type>{,<char_id>};
  12499. * type -> 0:hom_id 1:class 2:name
  12500. * 3:friendly 4:hungry, 5: rename flag.
  12501. * 6: level, 7: game id
  12502. *------------------------------------------*/
  12503. BUILDIN_FUNC(gethominfo)
  12504. {
  12505. TBL_PC *sd=script_rid2sd(st);
  12506. TBL_HOM *hd;
  12507. int type=script_getnum(st,2);
  12508. if (!script_charid2sd(3, sd) || !(hd = sd->hd)) {
  12509. if (type == 2)
  12510. script_pushconststr(st,"null");
  12511. else
  12512. script_pushint(st,0);
  12513. return SCRIPT_CMD_SUCCESS;
  12514. }
  12515. switch(type){
  12516. case 0: script_pushint(st,hd->homunculus.hom_id); break;
  12517. case 1: script_pushint(st,hd->homunculus.class_); break;
  12518. case 2: script_pushstrcopy(st,hd->homunculus.name); break;
  12519. case 3: script_pushint(st,hd->homunculus.intimacy); break;
  12520. case 4: script_pushint(st,hd->homunculus.hunger); break;
  12521. case 5: script_pushint(st,hd->homunculus.rename_flag); break;
  12522. case 6: script_pushint(st,hd->homunculus.level); break;
  12523. case 7: script_pushint(st,hd->bl.id); break;
  12524. default:
  12525. script_pushint(st,0);
  12526. break;
  12527. }
  12528. return SCRIPT_CMD_SUCCESS;
  12529. }
  12530. /// Retrieves information about character's mercenary
  12531. /// getmercinfo <type>[,<char id>];
  12532. BUILDIN_FUNC(getmercinfo)
  12533. {
  12534. int type;
  12535. struct map_session_data* sd;
  12536. struct mercenary_data* md;
  12537. type = script_getnum(st,2);
  12538. if( script_hasdata(st,3) )
  12539. {
  12540. uint32 char_id = script_getnum(st,3);
  12541. if( ( sd = map_charid2sd(char_id) ) == NULL )
  12542. {
  12543. ShowError("buildin_getmercinfo: No such character (char_id=%d).\n", char_id);
  12544. script_pushnil(st);
  12545. return SCRIPT_CMD_FAILURE;
  12546. }
  12547. }
  12548. else
  12549. {
  12550. if( ( sd = script_rid2sd(st) ) == NULL )
  12551. {
  12552. script_pushnil(st);
  12553. return SCRIPT_CMD_SUCCESS;
  12554. }
  12555. }
  12556. md = ( sd->status.mer_id && sd->md ) ? sd->md : NULL;
  12557. switch( type )
  12558. {
  12559. case 0: script_pushint(st,md ? md->mercenary.mercenary_id : 0); break;
  12560. case 1: script_pushint(st,md ? md->mercenary.class_ : 0); break;
  12561. case 2:
  12562. if( md )
  12563. script_pushstrcopy(st,md->db->name);
  12564. else
  12565. script_pushconststr(st,"");
  12566. break;
  12567. case 3: script_pushint(st,md ? mercenary_get_faith(md) : 0); break;
  12568. case 4: script_pushint(st,md ? mercenary_get_calls(md) : 0); break;
  12569. case 5: script_pushint(st,md ? md->mercenary.kill_count : 0); break;
  12570. case 6: script_pushint(st,md ? mercenary_get_lifetime(md) : 0); break;
  12571. case 7: script_pushint(st,md ? md->db->lv : 0); break;
  12572. case 8: script_pushint(st,md ? md->bl.id : 0); break;
  12573. default:
  12574. ShowError("buildin_getmercinfo: Invalid type %d (char_id=%d).\n", type, sd->status.char_id);
  12575. script_pushnil(st);
  12576. return SCRIPT_CMD_FAILURE;
  12577. }
  12578. return SCRIPT_CMD_SUCCESS;
  12579. }
  12580. /*==========================================
  12581. * Shows wether your inventory(and equips) contain
  12582. selected card or not.
  12583. checkequipedcard(4001);
  12584. *------------------------------------------*/
  12585. BUILDIN_FUNC(checkequipedcard)
  12586. {
  12587. TBL_PC *sd=script_rid2sd(st);
  12588. if(sd){
  12589. int n,i,c=0;
  12590. c=script_getnum(st,2);
  12591. for(i=0;i<MAX_INVENTORY;i++){
  12592. if(sd->inventory.u.items_inventory[i].nameid > 0 && sd->inventory.u.items_inventory[i].amount && sd->inventory_data[i]){
  12593. if (itemdb_isspecial(sd->inventory.u.items_inventory[i].card[0]))
  12594. continue;
  12595. for(n=0;n<sd->inventory_data[i]->slot;n++){
  12596. if(sd->inventory.u.items_inventory[i].card[n] == c) {
  12597. script_pushint(st,1);
  12598. return SCRIPT_CMD_SUCCESS;
  12599. }
  12600. }
  12601. }
  12602. }
  12603. }
  12604. script_pushint(st,0);
  12605. return SCRIPT_CMD_SUCCESS;
  12606. }
  12607. BUILDIN_FUNC(jump_zero)
  12608. {
  12609. int sel;
  12610. sel=script_getnum(st,2);
  12611. if(!sel) {
  12612. int pos;
  12613. if( !data_islabel(script_getdata(st,3)) ){
  12614. ShowError("script: jump_zero: not label !\n");
  12615. st->state=END;
  12616. return SCRIPT_CMD_FAILURE;
  12617. }
  12618. pos=script_getnum(st,3);
  12619. st->pos=pos;
  12620. st->state=GOTO;
  12621. }
  12622. return SCRIPT_CMD_SUCCESS;
  12623. }
  12624. /*==========================================
  12625. * movenpc [MouseJstr]
  12626. *------------------------------------------*/
  12627. BUILDIN_FUNC(movenpc)
  12628. {
  12629. TBL_NPC *nd = NULL;
  12630. const char *npc;
  12631. int x,y;
  12632. npc = script_getstr(st,2);
  12633. x = script_getnum(st,3);
  12634. y = script_getnum(st,4);
  12635. if ((nd = npc_name2id(npc)) == NULL){
  12636. ShowError("script: movenpc: NPC with ID '%s' was not found!\n", npc );
  12637. return -1;
  12638. }
  12639. if (script_hasdata(st,5))
  12640. nd->ud.dir = script_getnum(st,5) % 8;
  12641. npc_movenpc(nd, x, y);
  12642. return SCRIPT_CMD_SUCCESS;
  12643. }
  12644. /*==========================================
  12645. * message [MouseJstr]
  12646. *------------------------------------------*/
  12647. BUILDIN_FUNC(message)
  12648. {
  12649. const char *msg,*player;
  12650. TBL_PC *pl_sd = NULL;
  12651. player = script_getstr(st,2);
  12652. msg = script_getstr(st,3);
  12653. if((pl_sd=map_nick2sd((char *) player)) == NULL)
  12654. return SCRIPT_CMD_SUCCESS;
  12655. clif_displaymessage(pl_sd->fd, msg);
  12656. return SCRIPT_CMD_SUCCESS;
  12657. }
  12658. /*==========================================
  12659. * npctalk (sends message to surrounding area)
  12660. *------------------------------------------*/
  12661. BUILDIN_FUNC(npctalk)
  12662. {
  12663. struct npc_data* nd = NULL;
  12664. const char* str = script_getstr(st,2);
  12665. if (script_hasdata(st, 3))
  12666. nd = npc_name2id(script_getstr(st, 3));
  12667. else
  12668. nd = (struct npc_data *)map_id2bl(st->oid);
  12669. if (nd != NULL) {
  12670. char message[256];
  12671. safesnprintf(message, sizeof(message), "%s", str);
  12672. clif_disp_overhead(&nd->bl, message);
  12673. }
  12674. return SCRIPT_CMD_SUCCESS;
  12675. }
  12676. // change npc walkspeed [Valaris]
  12677. BUILDIN_FUNC(npcspeed)
  12678. {
  12679. struct npc_data* nd;
  12680. int speed;
  12681. speed = script_getnum(st,2);
  12682. nd =(struct npc_data *)map_id2bl(st->oid);
  12683. if( nd ) {
  12684. nd->speed = speed;
  12685. nd->ud.state.speed_changed = 1;
  12686. }
  12687. return SCRIPT_CMD_SUCCESS;
  12688. }
  12689. // make an npc walk to a position [Valaris]
  12690. BUILDIN_FUNC(npcwalkto)
  12691. {
  12692. struct npc_data *nd=(struct npc_data *)map_id2bl(st->oid);
  12693. int x=0,y=0;
  12694. x=script_getnum(st,2);
  12695. y=script_getnum(st,3);
  12696. if(nd) {
  12697. if (!nd->status.hp)
  12698. status_calc_npc(nd, SCO_FIRST);
  12699. else
  12700. status_calc_npc(nd, SCO_NONE);
  12701. unit_walktoxy(&nd->bl,x,y,0);
  12702. }
  12703. return SCRIPT_CMD_SUCCESS;
  12704. }
  12705. // stop an npc's movement [Valaris]
  12706. BUILDIN_FUNC(npcstop)
  12707. {
  12708. struct npc_data *nd=(struct npc_data *)map_id2bl(st->oid);
  12709. if(nd) {
  12710. unit_stop_walking(&nd->bl,1|4);
  12711. }
  12712. return SCRIPT_CMD_SUCCESS;
  12713. }
  12714. /**
  12715. * getlook(<type>{,<char_id>})
  12716. **/
  12717. BUILDIN_FUNC(getlook)
  12718. {
  12719. int type,val;
  12720. TBL_PC *sd;
  12721. if (!script_charid2sd(3,sd)) {
  12722. script_pushint(st,-1);
  12723. return SCRIPT_CMD_FAILURE;
  12724. }
  12725. type=script_getnum(st,2);
  12726. val=-1;
  12727. switch(type) {
  12728. // TODO: implement LOOK_BASE as stated in script doc
  12729. case LOOK_HAIR: val=sd->status.hair; break; //1
  12730. case LOOK_WEAPON: val=sd->status.weapon; break; //2
  12731. case LOOK_HEAD_BOTTOM: val=sd->status.head_bottom; break; //3
  12732. case LOOK_HEAD_TOP: val=sd->status.head_top; break; //4
  12733. case LOOK_HEAD_MID: val=sd->status.head_mid; break; //5
  12734. case LOOK_HAIR_COLOR: val=sd->status.hair_color; break; //6
  12735. case LOOK_CLOTHES_COLOR: val=sd->status.clothes_color; break; //7
  12736. case LOOK_SHIELD: val=sd->status.shield; break; //8
  12737. case LOOK_SHOES: break; //9
  12738. case LOOK_ROBE: val=sd->status.robe; break; //12
  12739. case LOOK_BODY2: val=sd->status.body; break; //13
  12740. }
  12741. script_pushint(st,val);
  12742. return SCRIPT_CMD_SUCCESS;
  12743. }
  12744. /**
  12745. * getsavepoint(<information type>{,<char_id>})
  12746. * @param type 0- map name, 1- x, 2- y
  12747. **/
  12748. BUILDIN_FUNC(getsavepoint)
  12749. {
  12750. TBL_PC* sd;
  12751. int type;
  12752. if (!script_charid2sd(3,sd)) {
  12753. script_pushint(st,0);
  12754. return SCRIPT_CMD_SUCCESS;
  12755. }
  12756. type = script_getnum(st,2);
  12757. switch(type) {
  12758. case 0: script_pushstrcopy(st,mapindex_id2name(sd->status.save_point.map)); break;
  12759. case 1: script_pushint(st,sd->status.save_point.x); break;
  12760. case 2: script_pushint(st,sd->status.save_point.y); break;
  12761. default:
  12762. script_pushint(st,0);
  12763. break;
  12764. }
  12765. return SCRIPT_CMD_SUCCESS;
  12766. }
  12767. /**
  12768. * Get position for BL objects.
  12769. * getmapxy(<map name>,<x>,<y>,<type>{,<char name>});
  12770. * @param mapname: String variable for output map name
  12771. * @param x: Integer variable for output coord X
  12772. * @param y: Integer variable for output coord Y
  12773. * @param type: Type of object
  12774. * UNITTYPE_PC - Character coord
  12775. * UNITTYPE_NPC - NPC coord
  12776. * UNITTYPE_PET - Pet coord
  12777. * UNITTYPE_HOM - Homun coord
  12778. * UNITTYPE_MER - Mercenary coord
  12779. * UNITTYPE_ELEM - Elemental coord
  12780. * @param charname: Name object. If empty or "this" use the current object
  12781. * @return 0 - success; -1 - some error, MapName$,MapX,MapY contains unknown value.
  12782. */
  12783. BUILDIN_FUNC(getmapxy)
  12784. {
  12785. struct block_list *bl = NULL;
  12786. TBL_PC *sd=NULL;
  12787. int64 num;
  12788. const char *name;
  12789. char prefix;
  12790. int x,y,type;
  12791. char mapname[MAP_NAME_LENGTH];
  12792. if( !data_isreference(script_getdata(st,2)) ) {
  12793. ShowWarning("script: buildin_getmapxy: mapname value is not a variable.\n");
  12794. script_pushint(st,-1);
  12795. return SCRIPT_CMD_FAILURE;
  12796. }
  12797. if( !data_isreference(script_getdata(st,3)) ) {
  12798. ShowWarning("script: buildin_getmapxy: mapx value is not a variable.\n");
  12799. script_pushint(st,-1);
  12800. return SCRIPT_CMD_FAILURE;
  12801. }
  12802. if( !data_isreference(script_getdata(st,4)) ) {
  12803. ShowWarning("script: buildin_getmapxy: mapy value is not a variable.\n");
  12804. script_pushint(st,-1);
  12805. return SCRIPT_CMD_FAILURE;
  12806. }
  12807. if( !is_string_variable(reference_getname(script_getdata(st, 2))) ) {
  12808. ShowWarning("script: buildin_getmapxy: %s is not a string variable.\n",reference_getname(script_getdata(st, 2)));
  12809. script_pushint(st,-1);
  12810. return SCRIPT_CMD_FAILURE;
  12811. }
  12812. if( is_string_variable(reference_getname(script_getdata(st, 3))) ) {
  12813. ShowWarning("script: buildin_getmapxy: %s is a string variable, should be an INT.\n",reference_getname(script_getdata(st, 3)));
  12814. script_pushint(st,-1);
  12815. return SCRIPT_CMD_FAILURE;
  12816. }
  12817. if( is_string_variable(reference_getname(script_getdata(st, 4))) ) {
  12818. ShowWarning("script: buildin_getmapxy: %s is a string variable, should be an INT.\n",reference_getname(script_getdata(st, 4)));
  12819. script_pushint(st,-1);
  12820. return SCRIPT_CMD_FAILURE;
  12821. }
  12822. // Possible needly check function parameters on C_STR,C_INT,C_INT
  12823. type=script_getnum(st,5);
  12824. switch (type) {
  12825. case UNITTYPE_PC: //Get Character Position
  12826. if( script_hasdata(st,6) )
  12827. sd=map_nick2sd(script_getstr(st,6));
  12828. else
  12829. sd=script_rid2sd(st);
  12830. if (sd)
  12831. bl = &sd->bl;
  12832. break;
  12833. case UNITTYPE_NPC: //Get NPC Position
  12834. if( script_hasdata(st,6) )
  12835. {
  12836. struct npc_data *nd;
  12837. nd=npc_name2id(script_getstr(st,6));
  12838. if (nd)
  12839. bl = &nd->bl;
  12840. } else //In case the origin is not an npc?
  12841. bl=map_id2bl(st->oid);
  12842. break;
  12843. case UNITTYPE_PET: //Get Pet Position
  12844. if(script_hasdata(st,6))
  12845. sd=map_nick2sd(script_getstr(st,6));
  12846. else
  12847. sd=script_rid2sd(st);
  12848. if (sd && sd->pd)
  12849. bl = &sd->pd->bl;
  12850. break;
  12851. case UNITTYPE_HOM: //Get Homun Position
  12852. if(script_hasdata(st,6))
  12853. sd=map_nick2sd(script_getstr(st,6));
  12854. else
  12855. sd=script_rid2sd(st);
  12856. if (sd && sd->hd)
  12857. bl = &sd->hd->bl;
  12858. break;
  12859. case UNITTYPE_MER: //Get Mercenary Position
  12860. if(script_hasdata(st,6))
  12861. sd=map_nick2sd(script_getstr(st,6));
  12862. else
  12863. sd=script_rid2sd(st);
  12864. if (sd && sd->md)
  12865. bl = &sd->md->bl;
  12866. break;
  12867. case UNITTYPE_ELEM: //Get Elemental Position
  12868. if(script_hasdata(st,6))
  12869. sd=map_nick2sd(script_getstr(st,6));
  12870. else
  12871. sd=script_rid2sd(st);
  12872. if (sd && sd->ed)
  12873. bl = &sd->ed->bl;
  12874. break;
  12875. default:
  12876. ShowWarning("script: buildin_getmapxy: Invalid type %d.\n", type);
  12877. script_pushint(st,-1);
  12878. return SCRIPT_CMD_FAILURE;
  12879. }
  12880. if (!bl) { //No object found.
  12881. script_pushint(st,-1);
  12882. return SCRIPT_CMD_SUCCESS;
  12883. }
  12884. x= bl->x;
  12885. y= bl->y;
  12886. safestrncpy(mapname, map[bl->m].name, MAP_NAME_LENGTH);
  12887. //Set MapName$
  12888. num=st->stack->stack_data[st->start+2].u.num;
  12889. name=get_str(script_getvarid(num));
  12890. prefix=*name;
  12891. if(not_server_variable(prefix))
  12892. sd=script_rid2sd(st);
  12893. else
  12894. sd=NULL;
  12895. set_reg(st,sd,num,name,(void*)mapname,script_getref(st,2));
  12896. //Set MapX
  12897. num=st->stack->stack_data[st->start+3].u.num;
  12898. name=get_str(script_getvarid(num));
  12899. prefix=*name;
  12900. if(not_server_variable(prefix))
  12901. sd=script_rid2sd(st);
  12902. else
  12903. sd=NULL;
  12904. set_reg(st,sd,num,name,(void*)__64BPRTSIZE(x),script_getref(st,3));
  12905. //Set MapY
  12906. num=st->stack->stack_data[st->start+4].u.num;
  12907. name=get_str(script_getvarid(num));
  12908. prefix=*name;
  12909. if(not_server_variable(prefix))
  12910. sd=script_rid2sd(st);
  12911. else
  12912. sd=NULL;
  12913. set_reg(st,sd,num,name,(void*)__64BPRTSIZE(y),script_getref(st,4));
  12914. //Return Success value
  12915. script_pushint(st,0);
  12916. return SCRIPT_CMD_SUCCESS;
  12917. }
  12918. /// Returns the map name of given map ID.
  12919. ///
  12920. /// mapid2name <map ID>;
  12921. BUILDIN_FUNC(mapid2name)
  12922. {
  12923. uint16 m = script_getnum(st, 2);
  12924. if (m < 0 || m >= MAX_MAP_PER_SERVER) {
  12925. script_pushstr(st, "");
  12926. return SCRIPT_CMD_FAILURE;
  12927. }
  12928. script_pushstrcopy(st, map_mapid2mapname(m));
  12929. return SCRIPT_CMD_SUCCESS;
  12930. }
  12931. /*==========================================
  12932. * Allows player to write NPC logs (i.e. Bank NPC, etc) [Lupus]
  12933. *------------------------------------------*/
  12934. BUILDIN_FUNC(logmes)
  12935. {
  12936. const char *str;
  12937. TBL_PC* sd;
  12938. sd = script_rid2sd(st);
  12939. if( sd == NULL )
  12940. return SCRIPT_CMD_FAILURE;
  12941. str = script_getstr(st,2);
  12942. log_npc(sd,str);
  12943. return SCRIPT_CMD_SUCCESS;
  12944. }
  12945. BUILDIN_FUNC(summon)
  12946. {
  12947. int _class, timeout=0;
  12948. const char *str,*event="";
  12949. TBL_PC *sd;
  12950. struct mob_data *md;
  12951. int tick = gettick();
  12952. sd=script_rid2sd(st);
  12953. if (!sd)
  12954. return SCRIPT_CMD_SUCCESS;
  12955. str =script_getstr(st,2);
  12956. _class=script_getnum(st,3);
  12957. if( script_hasdata(st,4) )
  12958. timeout=script_getnum(st,4);
  12959. if( script_hasdata(st,5) ){
  12960. event=script_getstr(st,5);
  12961. check_event(st, event);
  12962. }
  12963. clif_skill_poseffect(&sd->bl,AM_CALLHOMUN,1,sd->bl.x,sd->bl.y,tick);
  12964. md = mob_once_spawn_sub(&sd->bl, sd->bl.m, sd->bl.x, sd->bl.y, str, _class, event, SZ_SMALL, AI_NONE);
  12965. if (md) {
  12966. md->master_id=sd->bl.id;
  12967. md->special_state.ai = AI_ATTACK;
  12968. if( md->deletetimer != INVALID_TIMER )
  12969. delete_timer(md->deletetimer, mob_timer_delete);
  12970. md->deletetimer = add_timer(tick+(timeout>0?timeout:60000),mob_timer_delete,md->bl.id,0);
  12971. mob_spawn (md); //Now it is ready for spawning.
  12972. clif_specialeffect(&md->bl,344,AREA);
  12973. sc_start4(NULL,&md->bl, SC_MODECHANGE, 100, 1, 0, MD_AGGRESSIVE, 0, 60000);
  12974. }
  12975. script_pushint(st, md->bl.id);
  12976. return SCRIPT_CMD_SUCCESS;
  12977. }
  12978. /*==========================================
  12979. * Checks whether it is daytime/nighttime
  12980. *------------------------------------------*/
  12981. BUILDIN_FUNC(isnight)
  12982. {
  12983. script_pushint(st,(night_flag == 1));
  12984. return SCRIPT_CMD_SUCCESS;
  12985. }
  12986. BUILDIN_FUNC(isday)
  12987. {
  12988. script_pushint(st,(night_flag == 0));
  12989. return SCRIPT_CMD_SUCCESS;
  12990. }
  12991. /*================================================
  12992. * Check how many items/cards in the list are
  12993. * equipped - used for 2/15's cards patch [celest]
  12994. *------------------------------------------------*/
  12995. BUILDIN_FUNC(isequippedcnt)
  12996. {
  12997. TBL_PC *sd;
  12998. int i, id = 1;
  12999. int ret = 0;
  13000. sd = script_rid2sd(st);
  13001. if (!sd) { //If the player is not attached it is a script error anyway... but better prevent the map server from crashing...
  13002. script_pushint(st,0);
  13003. return SCRIPT_CMD_SUCCESS;
  13004. }
  13005. for (i=0; id!=0; i++) {
  13006. short j;
  13007. FETCH (i+2, id) else id = 0;
  13008. if (id <= 0)
  13009. continue;
  13010. for (j=0; j<EQI_MAX; j++) {
  13011. short index = sd->equip_index[j];
  13012. if(index < 0)
  13013. continue;
  13014. if (pc_is_same_equip_index((enum equip_index)j, sd->equip_index, index))
  13015. continue;
  13016. if(!sd->inventory_data[index])
  13017. continue;
  13018. if (itemdb_type(id) != IT_CARD) { //No card. Count amount in inventory.
  13019. if (sd->inventory_data[index]->nameid == id)
  13020. ret+= sd->inventory.u.items_inventory[index].amount;
  13021. } else { //Count cards.
  13022. short k;
  13023. if (itemdb_isspecial(sd->inventory.u.items_inventory[index].card[0]))
  13024. continue; //No cards
  13025. for(k=0; k<sd->inventory_data[index]->slot; k++) {
  13026. if (sd->inventory.u.items_inventory[index].card[k] == id)
  13027. ret++; //[Lupus]
  13028. }
  13029. }
  13030. }
  13031. }
  13032. script_pushint(st,ret);
  13033. return SCRIPT_CMD_SUCCESS;
  13034. }
  13035. /*================================================
  13036. * Check whether another card has been
  13037. * equipped - used for 2/15's cards patch [celest]
  13038. * -- Items checked cannot be reused in another
  13039. * card set to prevent exploits
  13040. *------------------------------------------------*/
  13041. BUILDIN_FUNC(isequipped)
  13042. {
  13043. TBL_PC *sd;
  13044. int i, id = 1;
  13045. int ret = -1;
  13046. //Original hash to reverse it when full check fails.
  13047. unsigned int setitem_hash = 0, setitem_hash2 = 0;
  13048. sd = script_rid2sd(st);
  13049. if (!sd) { //If the player is not attached it is a script error anyway... but better prevent the map server from crashing...
  13050. script_pushint(st,0);
  13051. return SCRIPT_CMD_SUCCESS;
  13052. }
  13053. setitem_hash = sd->bonus.setitem_hash;
  13054. setitem_hash2 = sd->bonus.setitem_hash2;
  13055. for (i=0; id!=0; i++) {
  13056. int flag = 0;
  13057. short j;
  13058. FETCH (i+2, id) else id = 0;
  13059. if (id <= 0)
  13060. continue;
  13061. for (j=0; j<EQI_MAX; j++) {
  13062. short index = sd->equip_index[j];
  13063. if(index < 0)
  13064. continue;
  13065. if (pc_is_same_equip_index((enum equip_index)i, sd->equip_index, index))
  13066. continue;
  13067. if(!sd->inventory_data[index])
  13068. continue;
  13069. if (itemdb_type(id) != IT_CARD) {
  13070. if (sd->inventory_data[index]->nameid != id)
  13071. continue;
  13072. flag = 1;
  13073. break;
  13074. } else { //Cards
  13075. short k;
  13076. if (sd->inventory_data[index]->slot == 0 ||
  13077. itemdb_isspecial(sd->inventory.u.items_inventory[index].card[0]))
  13078. continue;
  13079. for (k = 0; k < sd->inventory_data[index]->slot; k++)
  13080. { //New hash system which should support up to 4 slots on any equipment. [Skotlex]
  13081. unsigned int hash = 0;
  13082. if (sd->inventory.u.items_inventory[index].card[k] != id)
  13083. continue;
  13084. hash = 1<<((j<5?j:j-5)*4 + k);
  13085. // check if card is already used by another set
  13086. if ( ( j < 5 ? sd->bonus.setitem_hash : sd->bonus.setitem_hash2 ) & hash)
  13087. continue;
  13088. // We have found a match
  13089. flag = 1;
  13090. // Set hash so this card cannot be used by another
  13091. if (j<5)
  13092. sd->bonus.setitem_hash |= hash;
  13093. else
  13094. sd->bonus.setitem_hash2 |= hash;
  13095. break;
  13096. }
  13097. }
  13098. if (flag) break; //Card found
  13099. }
  13100. if (ret == -1)
  13101. ret = flag;
  13102. else
  13103. ret &= flag;
  13104. if (!ret) break;
  13105. }
  13106. if (!ret) {//When check fails, restore original hash values. [Skotlex]
  13107. sd->bonus.setitem_hash = setitem_hash;
  13108. sd->bonus.setitem_hash2 = setitem_hash2;
  13109. }
  13110. script_pushint(st,ret);
  13111. return SCRIPT_CMD_SUCCESS;
  13112. }
  13113. /*================================================
  13114. * Check how many given inserted cards in the CURRENT
  13115. * weapon - used for 2/15's cards patch [Lupus]
  13116. *------------------------------------------------*/
  13117. BUILDIN_FUNC(cardscnt)
  13118. {
  13119. TBL_PC *sd;
  13120. int i, k, id = 1;
  13121. int ret = 0;
  13122. int index;
  13123. sd = script_rid2sd(st);
  13124. for (i=0; id!=0; i++) {
  13125. FETCH (i+2, id) else id = 0;
  13126. if (id <= 0)
  13127. continue;
  13128. index = current_equip_item_index; //we get CURRENT WEAPON inventory index from status.c [Lupus]
  13129. if(index < 0) continue;
  13130. if(!sd->inventory_data[index])
  13131. continue;
  13132. if(itemdb_type(id) != IT_CARD) {
  13133. if (sd->inventory_data[index]->nameid == id)
  13134. ret+= sd->inventory.u.items_inventory[index].amount;
  13135. } else {
  13136. if (itemdb_isspecial(sd->inventory.u.items_inventory[index].card[0]))
  13137. continue;
  13138. for(k=0; k<sd->inventory_data[index]->slot; k++) {
  13139. if (sd->inventory.u.items_inventory[index].card[k] == id)
  13140. ret++;
  13141. }
  13142. }
  13143. }
  13144. script_pushint(st,ret);
  13145. // script_pushint(st,current_equip_item_index);
  13146. return SCRIPT_CMD_SUCCESS;
  13147. }
  13148. /*=======================================================
  13149. * Returns the refined number of the current item, or an
  13150. * item with inventory index specified
  13151. *-------------------------------------------------------*/
  13152. BUILDIN_FUNC(getrefine)
  13153. {
  13154. TBL_PC *sd;
  13155. if ((sd = script_rid2sd(st))!= NULL)
  13156. script_pushint(st,sd->inventory.u.items_inventory[current_equip_item_index].refine);
  13157. else
  13158. script_pushint(st,0);
  13159. return SCRIPT_CMD_SUCCESS;
  13160. }
  13161. /*=======================================================
  13162. * Day/Night controls
  13163. *-------------------------------------------------------*/
  13164. BUILDIN_FUNC(night)
  13165. {
  13166. if (night_flag != 1) map_night_timer(night_timer_tid, 0, 0, 1);
  13167. return SCRIPT_CMD_SUCCESS;
  13168. }
  13169. BUILDIN_FUNC(day)
  13170. {
  13171. if (night_flag != 0) map_day_timer(day_timer_tid, 0, 0, 1);
  13172. return SCRIPT_CMD_SUCCESS;
  13173. }
  13174. /**
  13175. * unequip <equipment slot>{,<char_id>};
  13176. * @author [Spectre]
  13177. **/
  13178. BUILDIN_FUNC(unequip) {
  13179. int pos;
  13180. TBL_PC *sd;
  13181. if (!script_charid2sd(3,sd))
  13182. return SCRIPT_CMD_FAILURE;
  13183. pos = script_getnum(st,2);
  13184. if (equip_index_check(pos)) {
  13185. short i = pc_checkequip(sd,equip_bitmask[pos]);
  13186. if (i >= 0) {
  13187. pc_unequipitem(sd,i,1|2);
  13188. script_pushint(st, 1);
  13189. return SCRIPT_CMD_SUCCESS;
  13190. }
  13191. }
  13192. ShowError("buildin_unequip: No item equipped at pos %d (CID=%d/AID=%d).\n", pos, sd->status.char_id, sd->status.account_id);
  13193. script_pushint(st, 0);
  13194. return SCRIPT_CMD_FAILURE;
  13195. }
  13196. /**
  13197. * equip <item id>{,<char_id>};
  13198. **/
  13199. BUILDIN_FUNC(equip) {
  13200. unsigned short nameid = 0;
  13201. TBL_PC *sd;
  13202. struct item_data *item_data;
  13203. if (!script_charid2sd(3,sd))
  13204. return SCRIPT_CMD_FAILURE;
  13205. nameid = script_getnum(st,2);
  13206. if ((item_data = itemdb_exists(nameid))) {
  13207. int i;
  13208. ARR_FIND( 0, MAX_INVENTORY, i, sd->inventory.u.items_inventory[i].nameid == nameid );
  13209. if (i < MAX_INVENTORY) {
  13210. pc_equipitem(sd,i,item_data->equip);
  13211. script_pushint(st,1);
  13212. return SCRIPT_CMD_SUCCESS;
  13213. }
  13214. }
  13215. ShowError("buildin_equip: Item %hu cannot be equipped\n",nameid);
  13216. script_pushint(st,0);
  13217. return SCRIPT_CMD_FAILURE;
  13218. }
  13219. BUILDIN_FUNC(autoequip)
  13220. {
  13221. unsigned short nameid;
  13222. int flag;
  13223. struct item_data *item_data;
  13224. nameid=script_getnum(st,2);
  13225. flag=script_getnum(st,3);
  13226. if( ( item_data = itemdb_exists(nameid) ) == NULL )
  13227. {
  13228. ShowError("buildin_autoequip: Invalid item '%hu'.\n", nameid);
  13229. return SCRIPT_CMD_FAILURE;
  13230. }
  13231. if( !itemdb_isequip2(item_data) )
  13232. {
  13233. ShowError("buildin_autoequip: Item '%hu' cannot be equipped.\n", nameid);
  13234. return SCRIPT_CMD_FAILURE;
  13235. }
  13236. item_data->flag.autoequip = flag>0?1:0;
  13237. return SCRIPT_CMD_SUCCESS;
  13238. }
  13239. BUILDIN_FUNC(setbattleflag)
  13240. {
  13241. const char *flag, *value;
  13242. flag = script_getstr(st,2);
  13243. value = script_getstr(st,3); // HACK: Retrieve number as string (auto-converted) for battle_set_value
  13244. if (battle_set_value(flag, value) == 0)
  13245. ShowWarning("buildin_setbattleflag: unknown battle_config flag '%s'\n",flag);
  13246. else
  13247. ShowInfo("buildin_setbattleflag: battle_config flag '%s' is now set to '%s'.\n",flag,value);
  13248. return SCRIPT_CMD_SUCCESS;
  13249. }
  13250. BUILDIN_FUNC(getbattleflag)
  13251. {
  13252. const char *flag;
  13253. flag = script_getstr(st,2);
  13254. script_pushint(st,battle_get_value(flag));
  13255. return SCRIPT_CMD_SUCCESS;
  13256. }
  13257. //=======================================================
  13258. // strlen [Valaris]
  13259. //-------------------------------------------------------
  13260. BUILDIN_FUNC(getstrlen)
  13261. {
  13262. const char *str = script_getstr(st,2);
  13263. int len = (str) ? (int)strlen(str) : 0;
  13264. script_pushint(st,len);
  13265. return SCRIPT_CMD_SUCCESS;
  13266. }
  13267. //=======================================================
  13268. // isalpha [Valaris]
  13269. //-------------------------------------------------------
  13270. BUILDIN_FUNC(charisalpha)
  13271. {
  13272. const char *str=script_getstr(st,2);
  13273. int pos=script_getnum(st,3);
  13274. int val = ( str && pos >= 0 && (unsigned int)pos < strlen(str) ) ? ISALPHA( str[pos] ) != 0 : 0;
  13275. script_pushint(st,val);
  13276. return SCRIPT_CMD_SUCCESS;
  13277. }
  13278. //=======================================================
  13279. // charisupper <str>, <index>
  13280. //-------------------------------------------------------
  13281. BUILDIN_FUNC(charisupper)
  13282. {
  13283. const char *str = script_getstr(st,2);
  13284. int pos = script_getnum(st,3);
  13285. int val = ( str && pos >= 0 && (unsigned int)pos < strlen(str) ) ? ISUPPER( str[pos] ) : 0;
  13286. script_pushint(st,val);
  13287. return SCRIPT_CMD_SUCCESS;
  13288. }
  13289. //=======================================================
  13290. // charislower <str>, <index>
  13291. //-------------------------------------------------------
  13292. BUILDIN_FUNC(charislower)
  13293. {
  13294. const char *str = script_getstr(st,2);
  13295. int pos = script_getnum(st,3);
  13296. int val = ( str && pos >= 0 && (unsigned int)pos < strlen(str) ) ? ISLOWER( str[pos] ) : 0;
  13297. script_pushint(st,val);
  13298. return SCRIPT_CMD_SUCCESS;
  13299. }
  13300. //=======================================================
  13301. // charat <str>, <index>
  13302. //-------------------------------------------------------
  13303. BUILDIN_FUNC(charat) {
  13304. const char *str = script_getstr(st,2);
  13305. int pos = script_getnum(st,3);
  13306. if( pos >= 0 && (unsigned int)pos < strlen(str) ) {
  13307. char output[2];
  13308. output[0] = str[pos];
  13309. output[1] = '\0';
  13310. script_pushstrcopy(st, output);
  13311. } else
  13312. script_pushconststr(st, "");
  13313. return SCRIPT_CMD_SUCCESS;
  13314. }
  13315. //=======================================================
  13316. // setchar <string>, <char>, <index>
  13317. //-------------------------------------------------------
  13318. BUILDIN_FUNC(setchar)
  13319. {
  13320. const char *str = script_getstr(st,2);
  13321. const char *c = script_getstr(st,3);
  13322. int index = script_getnum(st,4);
  13323. char *output = aStrdup(str);
  13324. if(index >= 0 && index < strlen(output))
  13325. output[index] = *c;
  13326. script_pushstr(st, output);
  13327. return SCRIPT_CMD_SUCCESS;
  13328. }
  13329. //=======================================================
  13330. // insertchar <string>, <char>, <index>
  13331. //-------------------------------------------------------
  13332. BUILDIN_FUNC(insertchar)
  13333. {
  13334. const char *str = script_getstr(st,2);
  13335. const char *c = script_getstr(st,3);
  13336. int index = script_getnum(st,4);
  13337. char *output;
  13338. size_t len = strlen(str);
  13339. if(index < 0)
  13340. index = 0;
  13341. else if(index > len)
  13342. index = len;
  13343. output = (char*)aMalloc(len + 2);
  13344. memcpy(output, str, index);
  13345. output[index] = c[0];
  13346. memcpy(&output[index+1], &str[index], len - index);
  13347. output[len+1] = '\0';
  13348. script_pushstr(st, output);
  13349. return SCRIPT_CMD_SUCCESS;
  13350. }
  13351. //=======================================================
  13352. // delchar <string>, <index>
  13353. //-------------------------------------------------------
  13354. BUILDIN_FUNC(delchar)
  13355. {
  13356. const char *str = script_getstr(st,2);
  13357. int index = script_getnum(st,3);
  13358. char *output;
  13359. size_t len = strlen(str);
  13360. if(index < 0 || index > len) {
  13361. //return original
  13362. output = aStrdup(str);
  13363. script_pushstr(st, output);
  13364. return SCRIPT_CMD_SUCCESS;
  13365. }
  13366. output = (char*)aMalloc(len);
  13367. memcpy(output, str, index);
  13368. memcpy(&output[index], &str[index+1], len - index);
  13369. script_pushstr(st, output);
  13370. return SCRIPT_CMD_SUCCESS;
  13371. }
  13372. //=======================================================
  13373. // strtoupper <str>
  13374. //-------------------------------------------------------
  13375. BUILDIN_FUNC(strtoupper)
  13376. {
  13377. const char *str = script_getstr(st,2);
  13378. char *output = aStrdup(str);
  13379. char *cursor = output;
  13380. while (*cursor != '\0') {
  13381. *cursor = TOUPPER(*cursor);
  13382. cursor++;
  13383. }
  13384. script_pushstr(st, output);
  13385. return SCRIPT_CMD_SUCCESS;
  13386. }
  13387. //=======================================================
  13388. // strtolower <str>
  13389. //-------------------------------------------------------
  13390. BUILDIN_FUNC(strtolower)
  13391. {
  13392. const char *str = script_getstr(st,2);
  13393. char *output = aStrdup(str);
  13394. char *cursor = output;
  13395. while (*cursor != '\0') {
  13396. *cursor = TOLOWER(*cursor);
  13397. cursor++;
  13398. }
  13399. script_pushstr(st, output);
  13400. return SCRIPT_CMD_SUCCESS;
  13401. }
  13402. //=======================================================
  13403. // substr <str>, <start>, <end>
  13404. //-------------------------------------------------------
  13405. BUILDIN_FUNC(substr)
  13406. {
  13407. const char *str = script_getstr(st,2);
  13408. char *output;
  13409. int start = script_getnum(st,3);
  13410. int end = script_getnum(st,4);
  13411. int len = 0;
  13412. if(start >= 0 && end < strlen(str) && start <= end) {
  13413. len = end - start + 1;
  13414. output = (char*)aMalloc(len + 1);
  13415. memcpy(output, &str[start], len);
  13416. } else
  13417. output = (char*)aMalloc(1);
  13418. output[len] = '\0';
  13419. script_pushstr(st, output);
  13420. return SCRIPT_CMD_SUCCESS;
  13421. }
  13422. //=======================================================
  13423. // explode <dest_string_array>, <str>, <delimiter>
  13424. // Note: delimiter is limited to 1 char
  13425. //-------------------------------------------------------
  13426. BUILDIN_FUNC(explode)
  13427. {
  13428. struct script_data* data = script_getdata(st, 2);
  13429. const char *str = script_getstr(st,3);
  13430. const char delimiter = script_getstr(st, 4)[0];
  13431. int32 id;
  13432. size_t len = strlen(str);
  13433. int i = 0, j = 0;
  13434. int start;
  13435. char *temp;
  13436. const char* name;
  13437. TBL_PC* sd = NULL;
  13438. temp = (char*)aMalloc(len + 1);
  13439. if( !data_isreference(data) ) {
  13440. ShowError("script:explode: not a variable\n");
  13441. script_reportdata(data);
  13442. st->state = END;
  13443. return SCRIPT_CMD_FAILURE;// not a variable
  13444. }
  13445. id = reference_getid(data);
  13446. start = reference_getindex(data);
  13447. name = reference_getname(data);
  13448. if( !is_string_variable(name) ) {
  13449. ShowError("script:explode: not string array\n");
  13450. script_reportdata(data);
  13451. st->state = END;
  13452. return SCRIPT_CMD_FAILURE;// data type mismatch
  13453. }
  13454. if( not_server_variable(*name) ) {
  13455. sd = script_rid2sd(st);
  13456. if( sd == NULL )
  13457. return SCRIPT_CMD_SUCCESS;// no player attached
  13458. }
  13459. while(str[i] != '\0') {
  13460. if(str[i] == delimiter && start < SCRIPT_MAX_ARRAYSIZE-1) { //break at delimiter but ignore after reaching last array index
  13461. temp[j] = '\0';
  13462. set_reg(st, sd, reference_uid(id, start++), name, (void*)temp, reference_getref(data));
  13463. j = 0;
  13464. ++i;
  13465. } else {
  13466. temp[j++] = str[i++];
  13467. }
  13468. }
  13469. //set last string
  13470. temp[j] = '\0';
  13471. set_reg(st, sd, reference_uid(id, start), name, (void*)temp, reference_getref(data));
  13472. aFree(temp);
  13473. return SCRIPT_CMD_SUCCESS;
  13474. }
  13475. //=======================================================
  13476. // implode <string_array>
  13477. // implode <string_array>, <glue>
  13478. //-------------------------------------------------------
  13479. BUILDIN_FUNC(implode)
  13480. {
  13481. struct script_data* data = script_getdata(st, 2);
  13482. const char *name;
  13483. uint32 glue_len = 0, array_size, id;
  13484. char *output;
  13485. TBL_PC* sd = NULL;
  13486. if( !data_isreference(data) ) {
  13487. ShowError("script:implode: not a variable\n");
  13488. script_reportdata(data);
  13489. st->state = END;
  13490. return SCRIPT_CMD_FAILURE;// not a variable
  13491. }
  13492. id = reference_getid(data);
  13493. name = reference_getname(data);
  13494. if( !is_string_variable(name) ) {
  13495. ShowError("script:implode: not string array\n");
  13496. script_reportdata(data);
  13497. st->state = END;
  13498. return SCRIPT_CMD_FAILURE;// data type mismatch
  13499. }
  13500. if( not_server_variable(*name) ) {
  13501. sd = script_rid2sd(st);
  13502. if( sd == NULL )
  13503. return SCRIPT_CMD_SUCCESS;// no player attached
  13504. }
  13505. //count chars
  13506. array_size = script_array_highest_key(st, sd, name, reference_getref(data)) - 1;
  13507. if(array_size == -1) { //empty array check (AmsTaff)
  13508. ShowWarning("script:implode: array length = 0\n");
  13509. output = (char*)aMalloc(sizeof(char)*5);
  13510. sprintf(output,"%s","NULL");
  13511. } else {
  13512. const char *glue = NULL, *temp;
  13513. size_t len = 0;
  13514. int i, k = 0;
  13515. for(i = 0; i <= array_size; ++i) {
  13516. temp = (char*) get_val2(st, reference_uid(id, i), reference_getref(data));
  13517. len += strlen(temp);
  13518. script_removetop(st, -1, 0);
  13519. }
  13520. //allocate mem
  13521. if( script_hasdata(st,3) ) {
  13522. glue = script_getstr(st,3);
  13523. glue_len = strlen(glue);
  13524. len += glue_len * (array_size);
  13525. }
  13526. output = (char*)aMalloc(len + 1);
  13527. //build output
  13528. for(i = 0; i < array_size; ++i) {
  13529. temp = (char*) get_val2(st, reference_uid(id, i), reference_getref(data));
  13530. len = strlen(temp);
  13531. memcpy(&output[k], temp, len);
  13532. k += len;
  13533. if(glue_len != 0) {
  13534. memcpy(&output[k], glue, glue_len);
  13535. k += glue_len;
  13536. }
  13537. script_removetop(st, -1, 0);
  13538. }
  13539. temp = (char*) get_val2(st, reference_uid(id, array_size), reference_getref(data));
  13540. len = strlen(temp);
  13541. memcpy(&output[k], temp, len);
  13542. k += len;
  13543. script_removetop(st, -1, 0);
  13544. output[k] = '\0';
  13545. }
  13546. script_pushstr(st, output);
  13547. return SCRIPT_CMD_SUCCESS;
  13548. }
  13549. //=======================================================
  13550. // sprintf(<format>, ...);
  13551. // Implements C sprintf, except format %n. The resulting string is
  13552. // returned, instead of being saved in variable by reference.
  13553. //-------------------------------------------------------
  13554. BUILDIN_FUNC(sprintf)
  13555. {
  13556. unsigned int len, argc = 0, arg = 0, buf2_len = 0;
  13557. const char* format;
  13558. char* p;
  13559. char* q;
  13560. char* buf = NULL;
  13561. char* buf2 = NULL;
  13562. struct script_data* data;
  13563. StringBuf final_buf;
  13564. // Fetch init data
  13565. format = script_getstr(st, 2);
  13566. argc = script_lastdata(st)-2;
  13567. len = strlen(format);
  13568. // Skip parsing, where no parsing is required.
  13569. if(len == 0) {
  13570. script_pushconststr(st,"");
  13571. return SCRIPT_CMD_SUCCESS;
  13572. }
  13573. // Pessimistic alloc
  13574. CREATE(buf, char, len+1);
  13575. // Need not be parsed, just solve stuff like %%.
  13576. if(argc == 0) {
  13577. memcpy(buf,format,len+1);
  13578. script_pushstrcopy(st, buf);
  13579. aFree(buf);
  13580. return SCRIPT_CMD_SUCCESS;
  13581. }
  13582. safestrncpy(buf, format, len+1);
  13583. // Issue sprintf for each parameter
  13584. StringBuf_Init(&final_buf);
  13585. q = buf;
  13586. while((p = strchr(q, '%')) != NULL) {
  13587. if(p != q) {
  13588. len = p - q + 1;
  13589. if(buf2_len < len) {
  13590. RECREATE(buf2, char, len);
  13591. buf2_len = len;
  13592. }
  13593. safestrncpy(buf2, q, len);
  13594. StringBuf_AppendStr(&final_buf, buf2);
  13595. q = p;
  13596. }
  13597. p = q + 1;
  13598. if(*p == '%') { // %%
  13599. StringBuf_AppendStr(&final_buf, "%");
  13600. q+=2;
  13601. continue;
  13602. }
  13603. if(*p == 'n') { // %n
  13604. ShowWarning("buildin_sprintf: Format %%n not supported! Skipping...\n");
  13605. script_reportsrc(st);
  13606. q+=2;
  13607. continue;
  13608. }
  13609. if(arg >= argc) {
  13610. ShowError("buildin_sprintf: Not enough arguments passed!\n");
  13611. if(buf) aFree(buf);
  13612. if(buf2) aFree(buf2);
  13613. StringBuf_Destroy(&final_buf);
  13614. script_pushconststr(st,"");
  13615. return SCRIPT_CMD_FAILURE;
  13616. }
  13617. if((p = strchr(q+1, '%')) == NULL)
  13618. p = strchr(q, 0); // EOS
  13619. len = p - q + 1;
  13620. if(buf2_len < len) {
  13621. RECREATE(buf2, char, len);
  13622. buf2_len = len;
  13623. }
  13624. safestrncpy(buf2, q, len);
  13625. q = p;
  13626. // Note: This assumes the passed value being the correct
  13627. // type to the current format specifier. If not, the server
  13628. // probably crashes or returns anything else, than expected,
  13629. // but it would behave in normal code the same way so it's
  13630. // the scripter's responsibility.
  13631. data = script_getdata(st, arg+3);
  13632. if(data_isstring(data)) // String
  13633. StringBuf_Printf(&final_buf, buf2, script_getstr(st, arg+3));
  13634. else if(data_isint(data)) // Number
  13635. StringBuf_Printf(&final_buf, buf2, script_getnum(st, arg+3));
  13636. else if(data_isreference(data)) { // Variable
  13637. char* name = reference_getname(data);
  13638. if(name[strlen(name)-1]=='$') // var Str
  13639. StringBuf_Printf(&final_buf, buf2, script_getstr(st, arg+3));
  13640. else // var Int
  13641. StringBuf_Printf(&final_buf, buf2, script_getnum(st, arg+3));
  13642. } else { // Unsupported type
  13643. ShowError("buildin_sprintf: Unknown argument type!\n");
  13644. if(buf) aFree(buf);
  13645. if(buf2) aFree(buf2);
  13646. StringBuf_Destroy(&final_buf);
  13647. script_pushconststr(st,"");
  13648. return SCRIPT_CMD_FAILURE;
  13649. }
  13650. arg++;
  13651. }
  13652. // Append anything left
  13653. if(*q)
  13654. StringBuf_AppendStr(&final_buf, q);
  13655. // Passed more, than needed
  13656. if(arg < argc) {
  13657. ShowWarning("buildin_sprintf: Unused arguments passed.\n");
  13658. script_reportsrc(st);
  13659. }
  13660. script_pushstrcopy(st, StringBuf_Value(&final_buf));
  13661. if(buf) aFree(buf);
  13662. if(buf2) aFree(buf2);
  13663. StringBuf_Destroy(&final_buf);
  13664. return SCRIPT_CMD_SUCCESS;
  13665. }
  13666. //=======================================================
  13667. // sscanf(<str>, <format>, ...);
  13668. // Implements C sscanf.
  13669. //-------------------------------------------------------
  13670. BUILDIN_FUNC(sscanf){
  13671. unsigned int argc, arg = 0, len;
  13672. struct script_data* data;
  13673. struct map_session_data* sd = NULL;
  13674. const char* str;
  13675. const char* format;
  13676. const char* p;
  13677. const char* q;
  13678. char* buf = NULL;
  13679. char* buf_p;
  13680. char* ref_str = NULL;
  13681. int ref_int;
  13682. // Get data
  13683. str = script_getstr(st, 2);
  13684. format = script_getstr(st, 3);
  13685. argc = script_lastdata(st)-3;
  13686. len = strlen(format);
  13687. if (len != 0 && strlen(str) == 0) {
  13688. // If the source string is empty but the format string is not, we return -1
  13689. // according to the C specs. (if the format string is also empty, we shall
  13690. // continue and return 0: 0 conversions took place out of the 0 attempted.)
  13691. script_pushint(st, -1);
  13692. return SCRIPT_CMD_SUCCESS;
  13693. }
  13694. CREATE(buf, char, len*2+1);
  13695. // Issue sscanf for each parameter
  13696. *buf = 0;
  13697. q = format;
  13698. while((p = strchr(q, '%'))){
  13699. if(p!=q){
  13700. strncat(buf, q, (size_t)(p-q));
  13701. q = p;
  13702. }
  13703. p = q+1;
  13704. if(*p=='*' || *p=='%'){ // Skip
  13705. strncat(buf, q, 2);
  13706. q+=2;
  13707. continue;
  13708. }
  13709. if(arg>=argc){
  13710. ShowError("buildin_sscanf: Not enough arguments passed!\n");
  13711. script_pushint(st, -1);
  13712. if(buf) aFree(buf);
  13713. if(ref_str) aFree(ref_str);
  13714. return SCRIPT_CMD_FAILURE;
  13715. }
  13716. if((p = strchr(q+1, '%'))==NULL){
  13717. p = strchr(q, 0); // EOS
  13718. }
  13719. len = p-q;
  13720. strncat(buf, q, len);
  13721. q = p;
  13722. // Validate output
  13723. data = script_getdata(st, arg+4);
  13724. if(!data_isreference(data) || !reference_tovariable(data)){
  13725. ShowError("buildin_sscanf: Target argument is not a variable!\n");
  13726. script_pushint(st, -1);
  13727. if(buf) aFree(buf);
  13728. if(ref_str) aFree(ref_str);
  13729. return SCRIPT_CMD_FAILURE;
  13730. }
  13731. buf_p = reference_getname(data);
  13732. if(not_server_variable(*buf_p) && (sd = script_rid2sd(st))==NULL){
  13733. script_pushint(st, -1);
  13734. if(buf) aFree(buf);
  13735. if(ref_str) aFree(ref_str);
  13736. return SCRIPT_CMD_SUCCESS;
  13737. }
  13738. // Save value if any
  13739. if(buf_p[strlen(buf_p)-1]=='$'){ // String
  13740. if(ref_str==NULL){
  13741. CREATE(ref_str, char, strlen(str)+1);
  13742. }
  13743. if(sscanf(str, buf, ref_str)==0){
  13744. break;
  13745. }
  13746. set_reg(st, sd, reference_uid( reference_getid(data), reference_getindex(data) ), buf_p, (void *)(ref_str), reference_getref(data));
  13747. } else { // Number
  13748. if(sscanf(str, buf, &ref_int)==0){
  13749. break;
  13750. }
  13751. set_reg(st, sd, reference_uid( reference_getid(data), reference_getindex(data) ), buf_p, (void *)__64BPRTSIZE(ref_int), reference_getref(data));
  13752. }
  13753. arg++;
  13754. // Disable used format (%... -> %*...)
  13755. buf_p = strchr(buf, 0);
  13756. memmove(buf_p-len+2, buf_p-len+1, len);
  13757. *(buf_p-len+1) = '*';
  13758. }
  13759. script_pushint(st, arg);
  13760. if(buf) aFree(buf);
  13761. if(ref_str) aFree(ref_str);
  13762. return SCRIPT_CMD_SUCCESS;
  13763. }
  13764. //=======================================================
  13765. // strpos(<haystack>, <needle>)
  13766. // strpos(<haystack>, <needle>, <offset>)
  13767. //
  13768. // Implements PHP style strpos. Adapted from code from
  13769. // http://www.daniweb.com/code/snippet313.html, Dave Sinkula
  13770. //-------------------------------------------------------
  13771. BUILDIN_FUNC(strpos) {
  13772. const char *haystack = script_getstr(st,2);
  13773. const char *needle = script_getstr(st,3);
  13774. int i;
  13775. size_t len;
  13776. if( script_hasdata(st,4) )
  13777. i = script_getnum(st,4);
  13778. else
  13779. i = 0;
  13780. if (needle[0] == '\0') {
  13781. script_pushint(st, -1);
  13782. return SCRIPT_CMD_SUCCESS;
  13783. }
  13784. len = strlen(haystack);
  13785. for ( ; i < len; ++i ) {
  13786. if ( haystack[i] == *needle ) {
  13787. // matched starting char -- loop through remaining chars
  13788. const char *h, *n;
  13789. for ( h = &haystack[i], n = needle; *h && *n; ++h, ++n ) {
  13790. if ( *h != *n ) {
  13791. break;
  13792. }
  13793. }
  13794. if ( !*n ) { // matched all of 'needle' to null termination
  13795. script_pushint(st, i);
  13796. return SCRIPT_CMD_SUCCESS;
  13797. }
  13798. }
  13799. }
  13800. script_pushint(st, -1);
  13801. return SCRIPT_CMD_SUCCESS;
  13802. }
  13803. //===============================================================
  13804. // replacestr <input>, <search>, <replace>{, <usecase>{, <count>}}
  13805. //
  13806. // Note: Finds all instances of <search> in <input> and replaces
  13807. // with <replace>. If specified will only replace as many
  13808. // instances as specified in <count>. By default will be case
  13809. // sensitive.
  13810. //---------------------------------------------------------------
  13811. BUILDIN_FUNC(replacestr)
  13812. {
  13813. const char *input = script_getstr(st, 2);
  13814. const char *find = script_getstr(st, 3);
  13815. const char *replace = script_getstr(st, 4);
  13816. size_t inputlen = strlen(input);
  13817. size_t findlen = strlen(find);
  13818. struct StringBuf output;
  13819. bool usecase = true;
  13820. int count = 0;
  13821. int numFinds = 0;
  13822. int i = 0, f = 0;
  13823. if(findlen == 0) {
  13824. ShowError("script:replacestr: Invalid search length.\n");
  13825. st->state = END;
  13826. return SCRIPT_CMD_FAILURE;
  13827. }
  13828. if(script_hasdata(st, 5)) {
  13829. struct script_data *data = script_getdata(st, 5);
  13830. get_val(st, data); // Convert into value in case of a variable
  13831. if( !data_isstring(data) )
  13832. usecase = script_getnum(st, 5) != 0;
  13833. else {
  13834. ShowError("script:replacestr: Invalid usecase value. Expected int got string\n");
  13835. st->state = END;
  13836. return SCRIPT_CMD_FAILURE;
  13837. }
  13838. }
  13839. if(script_hasdata(st, 6)) {
  13840. count = script_getnum(st, 6);
  13841. if(count == 0) {
  13842. ShowError("script:replacestr: Invalid count value. Expected int got string\n");
  13843. st->state = END;
  13844. return SCRIPT_CMD_FAILURE;
  13845. }
  13846. }
  13847. StringBuf_Init(&output);
  13848. for(; i < inputlen; i++) {
  13849. if(count && count == numFinds) { //found enough, stop looking
  13850. break;
  13851. }
  13852. for(f = 0; f <= findlen; f++) {
  13853. if(f == findlen) { //complete match
  13854. numFinds++;
  13855. StringBuf_AppendStr(&output, replace);
  13856. i += findlen - 1;
  13857. break;
  13858. } else {
  13859. if(usecase) {
  13860. if((i + f) > inputlen || input[i + f] != find[f]) {
  13861. StringBuf_Printf(&output, "%c", input[i]);
  13862. break;
  13863. }
  13864. } else {
  13865. if(((i + f) > inputlen || input[i + f] != find[f]) && TOUPPER(input[i+f]) != TOUPPER(find[f])) {
  13866. StringBuf_Printf(&output, "%c", input[i]);
  13867. break;
  13868. }
  13869. }
  13870. }
  13871. }
  13872. }
  13873. //append excess after enough found
  13874. if(i < inputlen)
  13875. StringBuf_AppendStr(&output, &(input[i]));
  13876. script_pushstrcopy(st, StringBuf_Value(&output));
  13877. StringBuf_Destroy(&output);
  13878. return SCRIPT_CMD_SUCCESS;
  13879. }
  13880. //========================================================
  13881. // countstr <input>, <search>{, <usecase>}
  13882. //
  13883. // Note: Counts the number of times <search> occurs in
  13884. // <input>. By default will be case sensitive.
  13885. //--------------------------------------------------------
  13886. BUILDIN_FUNC(countstr)
  13887. {
  13888. const char *input = script_getstr(st, 2);
  13889. const char *find = script_getstr(st, 3);
  13890. size_t inputlen = strlen(input);
  13891. size_t findlen = strlen(find);
  13892. bool usecase = true;
  13893. int numFinds = 0;
  13894. int i = 0, f = 0;
  13895. if(findlen == 0) {
  13896. ShowError("script:countstr: Invalid search length.\n");
  13897. st->state = END;
  13898. return SCRIPT_CMD_FAILURE;
  13899. }
  13900. if(script_hasdata(st, 4)) {
  13901. struct script_data *data;
  13902. data = script_getdata(st, 4);
  13903. get_val(st, data); // Convert into value in case of a variable
  13904. if( !data_isstring(data) )
  13905. usecase = script_getnum(st, 4) != 0;
  13906. else {
  13907. ShowError("script:countstr: Invalid usecase value. Expected int got string\n");
  13908. st->state = END;
  13909. return SCRIPT_CMD_FAILURE;
  13910. }
  13911. }
  13912. for(; i < inputlen; i++) {
  13913. for(f = 0; f <= findlen; f++) {
  13914. if(f == findlen) { //complete match
  13915. numFinds++;
  13916. i += findlen - 1;
  13917. break;
  13918. } else {
  13919. if(usecase) {
  13920. if((i + f) > inputlen || input[i + f] != find[f]) {
  13921. break;
  13922. }
  13923. } else {
  13924. if(((i + f) > inputlen || input[i + f] != find[f]) && TOUPPER(input[i+f]) != TOUPPER(find[f])) {
  13925. break;
  13926. }
  13927. }
  13928. }
  13929. }
  13930. }
  13931. script_pushint(st, numFinds);
  13932. return SCRIPT_CMD_SUCCESS;
  13933. }
  13934. /// Changes the display name and/or display class of the npc.
  13935. /// Returns 0 is successful, 1 if the npc does not exist.
  13936. ///
  13937. /// setnpcdisplay("<npc name>", "<new display name>", <new class id>, <new size>) -> <int>
  13938. /// setnpcdisplay("<npc name>", "<new display name>", <new class id>) -> <int>
  13939. /// setnpcdisplay("<npc name>", "<new display name>") -> <int>
  13940. /// setnpcdisplay("<npc name>", <new class id>) -> <int>
  13941. BUILDIN_FUNC(setnpcdisplay)
  13942. {
  13943. const char* name;
  13944. const char* newname = NULL;
  13945. int class_ = -1, size = -1;
  13946. struct script_data* data;
  13947. struct npc_data* nd;
  13948. name = script_getstr(st,2);
  13949. data = script_getdata(st,3);
  13950. if( script_hasdata(st,4) )
  13951. class_ = script_getnum(st,4);
  13952. if( script_hasdata(st,5) )
  13953. size = script_getnum(st,5);
  13954. get_val(st, data);
  13955. if( data_isstring(data) )
  13956. newname = conv_str(st,data);
  13957. else if( data_isint(data) )
  13958. class_ = conv_num(st,data);
  13959. else
  13960. {
  13961. ShowError("script:setnpcdisplay: expected string or number\n");
  13962. script_reportdata(data);
  13963. return SCRIPT_CMD_FAILURE;
  13964. }
  13965. nd = npc_name2id(name);
  13966. if( nd == NULL )
  13967. {// not found
  13968. script_pushint(st,1);
  13969. return SCRIPT_CMD_SUCCESS;
  13970. }
  13971. // update npc
  13972. if( newname )
  13973. npc_setdisplayname(nd, newname);
  13974. if( size != -1 && size != (int)nd->size )
  13975. nd->size = size;
  13976. else
  13977. size = -1;
  13978. if( class_ != -1 && nd->class_ != class_ )
  13979. npc_setclass(nd, class_);
  13980. else if( size != -1 )
  13981. { // Required to update the visual size
  13982. clif_clearunit_area(&nd->bl, CLR_OUTSIGHT);
  13983. clif_spawn(&nd->bl);
  13984. }
  13985. script_pushint(st,0);
  13986. return SCRIPT_CMD_SUCCESS;
  13987. }
  13988. BUILDIN_FUNC(atoi)
  13989. {
  13990. const char *value;
  13991. value = script_getstr(st,2);
  13992. script_pushint(st,atoi(value));
  13993. return SCRIPT_CMD_SUCCESS;
  13994. }
  13995. BUILDIN_FUNC(axtoi)
  13996. {
  13997. const char *hex = script_getstr(st,2);
  13998. long value = strtol(hex, NULL, 16);
  13999. #if LONG_MAX > INT_MAX || LONG_MIN < INT_MIN
  14000. value = cap_value(value, INT_MIN, INT_MAX);
  14001. #endif
  14002. script_pushint(st, (int)value);
  14003. return SCRIPT_CMD_SUCCESS;
  14004. }
  14005. BUILDIN_FUNC(strtol)
  14006. {
  14007. const char *string = script_getstr(st, 2);
  14008. int base = script_getnum(st, 3);
  14009. long value = strtol(string, NULL, base);
  14010. #if LONG_MAX > INT_MAX || LONG_MIN < INT_MIN
  14011. value = cap_value(value, INT_MIN, INT_MAX);
  14012. #endif
  14013. script_pushint(st, (int)value);
  14014. return SCRIPT_CMD_SUCCESS;
  14015. }
  14016. // case-insensitive substring search [lordalfa]
  14017. BUILDIN_FUNC(compare)
  14018. {
  14019. const char *message;
  14020. const char *cmpstring;
  14021. message = script_getstr(st,2);
  14022. cmpstring = script_getstr(st,3);
  14023. script_pushint(st,(stristr(message,cmpstring) != NULL));
  14024. return SCRIPT_CMD_SUCCESS;
  14025. }
  14026. BUILDIN_FUNC(strcmp)
  14027. {
  14028. const char *str1;
  14029. const char *str2;
  14030. str1 = script_getstr(st,2);
  14031. str2 = script_getstr(st,3);
  14032. script_pushint(st,strcmp(str1, str2));
  14033. return SCRIPT_CMD_SUCCESS;
  14034. }
  14035. // [zBuffer] List of mathematics commands --->
  14036. BUILDIN_FUNC(sqrt)
  14037. {
  14038. double i, a;
  14039. i = script_getnum(st,2);
  14040. a = sqrt(i);
  14041. script_pushint(st,(int)a);
  14042. return SCRIPT_CMD_SUCCESS;
  14043. }
  14044. BUILDIN_FUNC(pow)
  14045. {
  14046. double i, a, b;
  14047. a = script_getnum(st,2);
  14048. b = script_getnum(st,3);
  14049. i = pow(a,b);
  14050. script_pushint(st,(int)i);
  14051. return SCRIPT_CMD_SUCCESS;
  14052. }
  14053. BUILDIN_FUNC(distance)
  14054. {
  14055. int x0, y0, x1, y1;
  14056. x0 = script_getnum(st,2);
  14057. y0 = script_getnum(st,3);
  14058. x1 = script_getnum(st,4);
  14059. y1 = script_getnum(st,5);
  14060. script_pushint(st,distance_xy(x0,y0,x1,y1));
  14061. return SCRIPT_CMD_SUCCESS;
  14062. }
  14063. // <--- [zBuffer] List of mathematics commands
  14064. BUILDIN_FUNC(md5)
  14065. {
  14066. const char *tmpstr;
  14067. char *md5str;
  14068. tmpstr = script_getstr(st,2);
  14069. md5str = (char *)aMalloc((32+1)*sizeof(char));
  14070. MD5_String(tmpstr, md5str);
  14071. script_pushstr(st, md5str);
  14072. return SCRIPT_CMD_SUCCESS;
  14073. }
  14074. // [zBuffer] List of dynamic var commands --->
  14075. /**
  14076. * setd "<variable name>",<value>{,<char_id>};
  14077. **/
  14078. BUILDIN_FUNC(setd)
  14079. {
  14080. TBL_PC *sd = NULL;
  14081. char varname[100];
  14082. const char *buffer;
  14083. int elem;
  14084. buffer = script_getstr(st, 2);
  14085. if(sscanf(buffer, "%99[^[][%11d]", varname, &elem) < 2)
  14086. elem = 0;
  14087. if( not_server_variable(*varname) ) {
  14088. if (!script_charid2sd(4,sd))
  14089. return SCRIPT_CMD_FAILURE;
  14090. }
  14091. if( is_string_variable(varname) ) {
  14092. setd_sub(st, sd, varname, elem, (void *)script_getstr(st, 3), NULL);
  14093. } else {
  14094. setd_sub(st, sd, varname, elem, (void *)__64BPRTSIZE(script_getnum(st, 3)), NULL);
  14095. }
  14096. return SCRIPT_CMD_SUCCESS;
  14097. }
  14098. int buildin_query_sql_sub(struct script_state* st, Sql* handle)
  14099. {
  14100. int i, j;
  14101. TBL_PC* sd = NULL;
  14102. const char* query;
  14103. struct script_data* data;
  14104. const char* name;
  14105. unsigned int max_rows = SCRIPT_MAX_ARRAYSIZE; // maximum number of rows
  14106. int num_vars;
  14107. int num_cols;
  14108. // check target variables
  14109. for( i = 3; script_hasdata(st,i); ++i ) {
  14110. data = script_getdata(st, i);
  14111. if( data_isreference(data) ) { // it's a variable
  14112. name = reference_getname(data);
  14113. if( not_server_variable(*name) && sd == NULL ) { // requires a player
  14114. sd = script_rid2sd(st);
  14115. if( sd == NULL ) { // no player attached
  14116. script_reportdata(data);
  14117. st->state = END;
  14118. return SCRIPT_CMD_FAILURE;
  14119. }
  14120. }
  14121. } else {
  14122. ShowError("script:query_sql: not a variable\n");
  14123. script_reportdata(data);
  14124. st->state = END;
  14125. return SCRIPT_CMD_FAILURE;
  14126. }
  14127. }
  14128. num_vars = i - 3;
  14129. // Execute the query
  14130. query = script_getstr(st,2);
  14131. if( SQL_ERROR == Sql_QueryStr(handle, query) ) {
  14132. Sql_ShowDebug(handle);
  14133. script_pushint(st, -1);
  14134. return SCRIPT_CMD_FAILURE;
  14135. }
  14136. if( Sql_NumRows(handle) == 0 ) { // No data received
  14137. Sql_FreeResult(handle);
  14138. script_pushint(st, 0);
  14139. return SCRIPT_CMD_SUCCESS;
  14140. }
  14141. // Count the number of columns to store
  14142. num_cols = Sql_NumColumns(handle);
  14143. if( num_vars < num_cols ) {
  14144. ShowWarning("script:query_sql: Too many columns, discarding last %u columns.\n", (unsigned int)(num_cols-num_vars));
  14145. script_reportsrc(st);
  14146. } else if( num_vars > num_cols ) {
  14147. ShowWarning("script:query_sql: Too many variables (%u extra).\n", (unsigned int)(num_vars-num_cols));
  14148. script_reportsrc(st);
  14149. }
  14150. // Store data
  14151. for( i = 0; i < max_rows && SQL_SUCCESS == Sql_NextRow(handle); ++i ) {
  14152. for( j = 0; j < num_vars; ++j ) {
  14153. char* str = NULL;
  14154. if( j < num_cols )
  14155. Sql_GetData(handle, j, &str, NULL);
  14156. data = script_getdata(st, j+3);
  14157. name = reference_getname(data);
  14158. if( is_string_variable(name) )
  14159. setd_sub(st, sd, name, i, (void *)(str?str:""), reference_getref(data));
  14160. else
  14161. setd_sub(st, sd, name, i, (void *)__64BPRTSIZE((str?atoi(str):0)), reference_getref(data));
  14162. }
  14163. }
  14164. if( i == max_rows && max_rows < Sql_NumRows(handle) ) {
  14165. ShowWarning("script:query_sql: Only %d/%u rows have been stored.\n", max_rows, (unsigned int)Sql_NumRows(handle));
  14166. script_reportsrc(st);
  14167. }
  14168. // Free data
  14169. Sql_FreeResult(handle);
  14170. script_pushint(st, i);
  14171. return SCRIPT_CMD_SUCCESS;
  14172. }
  14173. BUILDIN_FUNC(query_sql) {
  14174. #ifdef BETA_THREAD_TEST
  14175. if( st->state != RERUNLINE ) {
  14176. queryThread_add(st,false);
  14177. st->state = RERUNLINE;/* will continue when the query is finished running. */
  14178. } else
  14179. st->state = RUN;
  14180. return SCRIPT_CMD_SUCCESS;
  14181. #else
  14182. return buildin_query_sql_sub(st, qsmysql_handle);
  14183. #endif
  14184. }
  14185. BUILDIN_FUNC(query_logsql) {
  14186. if( !log_config.sql_logs ) {// logmysql_handle == NULL
  14187. ShowWarning("buildin_query_logsql: SQL logs are disabled, query '%s' will not be executed.\n", script_getstr(st,2));
  14188. script_pushint(st,-1);
  14189. return SCRIPT_CMD_FAILURE;
  14190. }
  14191. #ifdef BETA_THREAD_TEST
  14192. if( st->state != RERUNLINE ) {
  14193. queryThread_add(st,true);
  14194. st->state = RERUNLINE;/* will continue when the query is finished running. */
  14195. } else
  14196. st->state = RUN;
  14197. return SCRIPT_CMD_SUCCESS;
  14198. #else
  14199. return buildin_query_sql_sub(st, logmysql_handle);
  14200. #endif
  14201. }
  14202. //Allows escaping of a given string.
  14203. BUILDIN_FUNC(escape_sql)
  14204. {
  14205. const char *str;
  14206. char *esc_str;
  14207. size_t len;
  14208. str = script_getstr(st,2);
  14209. len = strlen(str);
  14210. esc_str = (char*)aMalloc(len*2+1);
  14211. Sql_EscapeStringLen(mmysql_handle, esc_str, str, len);
  14212. script_pushstr(st, esc_str);
  14213. return SCRIPT_CMD_SUCCESS;
  14214. }
  14215. BUILDIN_FUNC(getd)
  14216. {
  14217. char varname[100];
  14218. const char *buffer;
  14219. int elem;
  14220. buffer = script_getstr(st, 2);
  14221. if(sscanf(buffer, "%99[^[][%11d]", varname, &elem) < 2)
  14222. elem = 0;
  14223. // Push the 'pointer' so it's more flexible [Lance]
  14224. push_val(st->stack, C_NAME, reference_uid(add_str(varname), elem));
  14225. return SCRIPT_CMD_SUCCESS;
  14226. }
  14227. BUILDIN_FUNC(callshop)
  14228. {
  14229. TBL_PC *sd = NULL;
  14230. struct npc_data *nd;
  14231. const char *shopname;
  14232. int flag = 0;
  14233. sd = script_rid2sd(st);
  14234. if (!sd) {
  14235. script_pushint(st,0);
  14236. return SCRIPT_CMD_SUCCESS;
  14237. }
  14238. shopname = script_getstr(st, 2);
  14239. if (script_hasdata(st,3))
  14240. flag = script_getnum(st,3);
  14241. nd = npc_name2id(shopname);
  14242. 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) ) {
  14243. ShowError("buildin_callshop: Shop [%s] not found (or NPC is not shop type)\n", shopname);
  14244. script_pushint(st,0);
  14245. return SCRIPT_CMD_FAILURE;
  14246. }
  14247. if (nd->subtype == NPCTYPE_SHOP) {
  14248. // flag the user as using a valid script call for opening the shop (for floating NPCs)
  14249. sd->state.callshop = 1;
  14250. switch (flag) {
  14251. case 1: npc_buysellsel(sd,nd->bl.id,0); break; //Buy window
  14252. case 2: npc_buysellsel(sd,nd->bl.id,1); break; //Sell window
  14253. default: clif_npcbuysell(sd,nd->bl.id); break; //Show menu
  14254. }
  14255. }
  14256. #if PACKETVER >= 20131223
  14257. else if (nd->subtype == NPCTYPE_MARKETSHOP) {
  14258. unsigned short i;
  14259. for (i = 0; i < nd->u.shop.count; i++) {
  14260. if (nd->u.shop.shop_item[i].qty)
  14261. break;
  14262. }
  14263. if (i == nd->u.shop.count) {
  14264. clif_colormes(sd->fd, color_table[COLOR_RED], msg_txt(sd, 534));
  14265. return SCRIPT_CMD_FAILURE;
  14266. }
  14267. sd->npc_shopid = nd->bl.id;
  14268. clif_npc_market_open(sd, nd);
  14269. script_pushint(st,1);
  14270. return SCRIPT_CMD_SUCCESS;
  14271. }
  14272. #endif
  14273. else
  14274. clif_cashshop_show(sd, nd);
  14275. sd->npc_shopid = nd->bl.id;
  14276. script_pushint(st,1);
  14277. return SCRIPT_CMD_SUCCESS;
  14278. }
  14279. BUILDIN_FUNC(npcshopitem)
  14280. {
  14281. const char* npcname = script_getstr(st, 2);
  14282. struct npc_data* nd = npc_name2id(npcname);
  14283. int n, i;
  14284. int amount;
  14285. uint16 offs = 2;
  14286. if( !nd || ( nd->subtype != NPCTYPE_SHOP && nd->subtype != NPCTYPE_CASHSHOP && nd->subtype != NPCTYPE_ITEMSHOP && nd->subtype != NPCTYPE_POINTSHOP && nd->subtype != NPCTYPE_MARKETSHOP ) ) { // Not found.
  14287. script_pushint(st,0);
  14288. return SCRIPT_CMD_SUCCESS;
  14289. }
  14290. #if PACKETVER >= 20131223
  14291. if (nd->subtype == NPCTYPE_MARKETSHOP) {
  14292. offs = 3;
  14293. npc_market_delfromsql_(nd->exname, 0, true);
  14294. }
  14295. #endif
  14296. // get the count of new entries
  14297. amount = (script_lastdata(st)-2)/offs;
  14298. // generate new shop item list
  14299. RECREATE(nd->u.shop.shop_item, struct npc_item_list, amount);
  14300. for (n = 0, i = 3; n < amount; n++, i+=offs) {
  14301. nd->u.shop.shop_item[n].nameid = script_getnum(st,i);
  14302. nd->u.shop.shop_item[n].value = script_getnum(st,i+1);
  14303. #if PACKETVER >= 20131223
  14304. if (nd->subtype == NPCTYPE_MARKETSHOP) {
  14305. nd->u.shop.shop_item[n].qty = script_getnum(st,i+2);
  14306. nd->u.shop.shop_item[n].flag = 1;
  14307. npc_market_tosql(nd->exname, &nd->u.shop.shop_item[n]);
  14308. }
  14309. #endif
  14310. }
  14311. nd->u.shop.count = n;
  14312. script_pushint(st,1);
  14313. return SCRIPT_CMD_SUCCESS;
  14314. }
  14315. BUILDIN_FUNC(npcshopadditem)
  14316. {
  14317. const char* npcname = script_getstr(st,2);
  14318. struct npc_data* nd = npc_name2id(npcname);
  14319. int n, i;
  14320. uint16 offs = 2, amount;
  14321. if (!nd || ( nd->subtype != NPCTYPE_SHOP && nd->subtype != NPCTYPE_CASHSHOP && nd->subtype != NPCTYPE_ITEMSHOP && nd->subtype != NPCTYPE_POINTSHOP && nd->subtype != NPCTYPE_MARKETSHOP)) { // Not found.
  14322. script_pushint(st,0);
  14323. return SCRIPT_CMD_SUCCESS;
  14324. }
  14325. if (nd->subtype == NPCTYPE_MARKETSHOP)
  14326. offs = 3;
  14327. // get the count of new entries
  14328. amount = (script_lastdata(st)-2)/offs;
  14329. #if PACKETVER >= 20131223
  14330. if (nd->subtype == NPCTYPE_MARKETSHOP) {
  14331. for (n = 0, i = 3; n < amount; n++, i += offs) {
  14332. uint16 nameid = script_getnum(st,i), j;
  14333. // Check existing entries
  14334. ARR_FIND(0, nd->u.shop.count, j, nd->u.shop.shop_item[j].nameid == nameid);
  14335. if (j == nd->u.shop.count) {
  14336. RECREATE(nd->u.shop.shop_item, struct npc_item_list, nd->u.shop.count+1);
  14337. j = nd->u.shop.count;
  14338. nd->u.shop.shop_item[j].flag = 1;
  14339. nd->u.shop.count++;
  14340. }
  14341. nd->u.shop.shop_item[j].nameid = nameid;
  14342. nd->u.shop.shop_item[j].value = script_getnum(st,i+1);
  14343. nd->u.shop.shop_item[j].qty = script_getnum(st,i+2);
  14344. npc_market_tosql(nd->exname, &nd->u.shop.shop_item[j]);
  14345. }
  14346. script_pushint(st,1);
  14347. return SCRIPT_CMD_SUCCESS;
  14348. }
  14349. #endif
  14350. // append new items to existing shop item list
  14351. RECREATE(nd->u.shop.shop_item, struct npc_item_list, nd->u.shop.count+amount);
  14352. for (n = nd->u.shop.count, i = 3; n < nd->u.shop.count+amount; n++, i+=offs)
  14353. {
  14354. nd->u.shop.shop_item[n].nameid = script_getnum(st,i);
  14355. nd->u.shop.shop_item[n].value = script_getnum(st,i+1);
  14356. }
  14357. nd->u.shop.count = n;
  14358. script_pushint(st,1);
  14359. return SCRIPT_CMD_SUCCESS;
  14360. }
  14361. BUILDIN_FUNC(npcshopdelitem)
  14362. {
  14363. const char* npcname = script_getstr(st,2);
  14364. struct npc_data* nd = npc_name2id(npcname);
  14365. int n, i, size;
  14366. unsigned short amount;
  14367. if (!nd || ( nd->subtype != NPCTYPE_SHOP && nd->subtype != NPCTYPE_CASHSHOP && nd->subtype != NPCTYPE_ITEMSHOP && nd->subtype != NPCTYPE_POINTSHOP && nd->subtype != NPCTYPE_MARKETSHOP)) { // Not found.
  14368. script_pushint(st,0);
  14369. return SCRIPT_CMD_SUCCESS;
  14370. }
  14371. amount = script_lastdata(st)-2;
  14372. size = nd->u.shop.count;
  14373. // remove specified items from the shop item list
  14374. for( i = 3; i < 3 + amount; i++ ) {
  14375. unsigned short nameid = script_getnum(st,i);
  14376. ARR_FIND( 0, size, n, nd->u.shop.shop_item[n].nameid == nameid );
  14377. if( n < size ) {
  14378. if (n+1 != size)
  14379. memmove(&nd->u.shop.shop_item[n], &nd->u.shop.shop_item[n+1], sizeof(nd->u.shop.shop_item[0])*(size-n));
  14380. #if PACKETVER >= 20131223
  14381. if (nd->subtype == NPCTYPE_MARKETSHOP)
  14382. npc_market_delfromsql_(nd->exname, nameid, false);
  14383. #endif
  14384. size--;
  14385. }
  14386. }
  14387. RECREATE(nd->u.shop.shop_item, struct npc_item_list, size);
  14388. nd->u.shop.count = size;
  14389. script_pushint(st,1);
  14390. return SCRIPT_CMD_SUCCESS;
  14391. }
  14392. /**
  14393. * Sets a script to attach to a shop npc.
  14394. * npcshopattach "<npc_name>";
  14395. **/
  14396. BUILDIN_FUNC(npcshopattach)
  14397. {
  14398. const char* npcname = script_getstr(st,2);
  14399. struct npc_data* nd = npc_name2id(npcname);
  14400. int flag = 1;
  14401. if( script_hasdata(st,3) )
  14402. flag = script_getnum(st,3);
  14403. if (!nd || ( nd->subtype != NPCTYPE_SHOP && nd->subtype != NPCTYPE_CASHSHOP && nd->subtype != NPCTYPE_ITEMSHOP && nd->subtype != NPCTYPE_POINTSHOP && nd->subtype != NPCTYPE_MARKETSHOP)) { // Not found.
  14404. script_pushint(st,0);
  14405. return SCRIPT_CMD_SUCCESS;
  14406. }
  14407. if (flag)
  14408. nd->master_nd = ((struct npc_data *)map_id2bl(st->oid));
  14409. else
  14410. nd->master_nd = NULL;
  14411. script_pushint(st,1);
  14412. return SCRIPT_CMD_SUCCESS;
  14413. }
  14414. /*==========================================
  14415. * Returns some values of an item [Lupus]
  14416. * Price, Weight, etc...
  14417. setitemscript(itemID,"{new item bonus script}",[n]);
  14418. Where n:
  14419. 0 - script
  14420. 1 - Equip script
  14421. 2 - Unequip script
  14422. *------------------------------------------*/
  14423. BUILDIN_FUNC(setitemscript)
  14424. {
  14425. unsigned short item_id;
  14426. int n = 0;
  14427. const char *script;
  14428. struct item_data *i_data;
  14429. struct script_code **dstscript;
  14430. item_id = script_getnum(st,2);
  14431. script = script_getstr(st,3);
  14432. if( script_hasdata(st,4) )
  14433. n=script_getnum(st,4);
  14434. i_data = itemdb_exists(item_id);
  14435. if (!i_data || script==NULL || ( script[0] && script[0]!='{' )) {
  14436. script_pushint(st,0);
  14437. return SCRIPT_CMD_SUCCESS;
  14438. }
  14439. switch (n) {
  14440. case 2:
  14441. dstscript = &i_data->unequip_script;
  14442. break;
  14443. case 1:
  14444. dstscript = &i_data->equip_script;
  14445. break;
  14446. default:
  14447. dstscript = &i_data->script;
  14448. break;
  14449. }
  14450. if(*dstscript)
  14451. script_free_code(*dstscript);
  14452. *dstscript = script[0] ? parse_script(script, "script_setitemscript", 0, 0) : NULL;
  14453. script_pushint(st,1);
  14454. return SCRIPT_CMD_SUCCESS;
  14455. }
  14456. /*=======================================================
  14457. * Add or Update a mob drop temporarily [Akinari]
  14458. * Original Idea By: [Lupus]
  14459. *
  14460. * addmonsterdrop <mob_id or name>,<item_id>,<rate>;
  14461. *
  14462. * If given an item the mob already drops, the rate
  14463. * is updated to the new rate. Rate cannot exceed 10000
  14464. * Returns 1 if succeeded (added/updated a mob drop)
  14465. *-------------------------------------------------------*/
  14466. BUILDIN_FUNC(addmonsterdrop)
  14467. {
  14468. struct mob_db *mob;
  14469. struct script_data *data;
  14470. unsigned short item_id;
  14471. int rate;
  14472. data = script_getdata(st, 2);
  14473. get_val(st, data); // Convert into value in case of a variable
  14474. if(data_isstring(data))
  14475. mob = mob_db(mobdb_searchname(script_getstr(st,2)));
  14476. else
  14477. mob = mob_db(script_getnum(st,2));
  14478. item_id=script_getnum(st,3);
  14479. rate=script_getnum(st,4);
  14480. if(!itemdb_exists(item_id)){
  14481. ShowError("addmonsterdrop: Nonexistant item %hu requested.\n", item_id );
  14482. return SCRIPT_CMD_FAILURE;
  14483. }
  14484. if(mob) { //We got a valid monster, check for available drop slot
  14485. unsigned char i, c = 0;
  14486. for(i = 0; i < MAX_MOB_DROP; i++) {
  14487. if(mob->dropitem[i].nameid) {
  14488. if(mob->dropitem[i].nameid == item_id) { //If it equals item_id we update that drop
  14489. c = i;
  14490. break;
  14491. }
  14492. continue;
  14493. }
  14494. if(!c) //Accept first available slot only
  14495. c = i;
  14496. }
  14497. if(c) { //Fill in the slot with the item and rate
  14498. mob->dropitem[c].nameid = item_id;
  14499. mob->dropitem[c].p = (rate > 10000)?10000:rate;
  14500. script_pushint(st,1);
  14501. } else //No place to put the new drop
  14502. script_pushint(st,0);
  14503. } else {
  14504. ShowWarning("addmonsterdrop: bad mob id given %d\n",script_getnum(st,2));
  14505. return SCRIPT_CMD_FAILURE;
  14506. }
  14507. return SCRIPT_CMD_SUCCESS;
  14508. }
  14509. /*=======================================================
  14510. * Delete a mob drop temporarily [Akinari]
  14511. * Original Idea By: [Lupus]
  14512. *
  14513. * delmonsterdrop <mob_id or name>,<item_id>;
  14514. *
  14515. * Returns 1 if succeeded (deleted a mob drop)
  14516. *-------------------------------------------------------*/
  14517. BUILDIN_FUNC(delmonsterdrop)
  14518. {
  14519. struct mob_db *mob;
  14520. struct script_data *data;
  14521. unsigned short item_id;
  14522. data = script_getdata(st, 2);
  14523. get_val(st, data); // Convert into value in case of a variable
  14524. if(data_isstring(data))
  14525. mob = mob_db(mobdb_searchname(script_getstr(st,2)));
  14526. else
  14527. mob = mob_db(script_getnum(st,2));
  14528. item_id=script_getnum(st,3);
  14529. if(!itemdb_exists(item_id)){
  14530. ShowError("delmonsterdrop: Nonexistant item %hu requested.\n", item_id );
  14531. return SCRIPT_CMD_FAILURE;
  14532. }
  14533. if(mob) { //We got a valid monster, check for item drop on monster
  14534. unsigned char i;
  14535. for(i = 0; i < MAX_MOB_DROP; i++) {
  14536. if(mob->dropitem[i].nameid == item_id) {
  14537. mob->dropitem[i].nameid = 0;
  14538. mob->dropitem[i].p = 0;
  14539. script_pushint(st,1);
  14540. return SCRIPT_CMD_SUCCESS;
  14541. }
  14542. }
  14543. //No drop on that monster
  14544. script_pushint(st,0);
  14545. } else {
  14546. ShowWarning("delmonsterdrop: bad mob id given %d\n",script_getnum(st,2));
  14547. return SCRIPT_CMD_FAILURE;
  14548. }
  14549. return SCRIPT_CMD_SUCCESS;
  14550. }
  14551. /*==========================================
  14552. * Returns some values of a monster [Lupus]
  14553. * Name, Level, race, size, etc...
  14554. getmonsterinfo(monsterID,queryIndex);
  14555. *------------------------------------------*/
  14556. BUILDIN_FUNC(getmonsterinfo)
  14557. {
  14558. struct mob_db *mob;
  14559. int mob_id;
  14560. mob_id = script_getnum(st,2);
  14561. if (!mobdb_checkid(mob_id)) {
  14562. //ShowError("buildin_getmonsterinfo: Wrong Monster ID: %i\n", mob_id);
  14563. if ( script_getnum(st,3) == MOB_NAME ) // requested the name
  14564. script_pushconststr(st,"null");
  14565. else
  14566. script_pushint(st,-1);
  14567. return SCRIPT_CMD_SUCCESS;
  14568. }
  14569. mob = mob_db(mob_id);
  14570. switch ( script_getnum(st,3) ) {
  14571. case MOB_NAME: script_pushstrcopy(st,mob->jname); break;
  14572. case MOB_LV: script_pushint(st,mob->lv); break;
  14573. case MOB_MAXHP: script_pushint(st,mob->status.max_hp); break;
  14574. case MOB_BASEEXP: script_pushint(st,mob->base_exp); break;
  14575. case MOB_JOBEXP: script_pushint(st,mob->job_exp); break;
  14576. case MOB_ATK1: script_pushint(st,mob->status.rhw.atk); break;
  14577. case MOB_ATK2: script_pushint(st,mob->status.rhw.atk2); break;
  14578. case MOB_DEF: script_pushint(st,mob->status.def); break;
  14579. case MOB_MDEF: script_pushint(st,mob->status.mdef); break;
  14580. case MOB_STR: script_pushint(st,mob->status.str); break;
  14581. case MOB_AGI: script_pushint(st,mob->status.agi); break;
  14582. case MOB_VIT: script_pushint(st,mob->status.vit); break;
  14583. case MOB_INT: script_pushint(st,mob->status.int_); break;
  14584. case MOB_DEX: script_pushint(st,mob->status.dex); break;
  14585. case MOB_LUK: script_pushint(st,mob->status.luk); break;
  14586. case MOB_RANGE: script_pushint(st,mob->status.rhw.range); break;
  14587. case MOB_RANGE2: script_pushint(st,mob->range2); break;
  14588. case MOB_RANGE3: script_pushint(st,mob->range3); break;
  14589. case MOB_SIZE: script_pushint(st,mob->status.size); break;
  14590. case MOB_RACE: script_pushint(st,mob->status.race); break;
  14591. case MOB_ELEMENT: script_pushint(st,mob->status.def_ele); break;
  14592. case MOB_MODE: script_pushint(st,mob->status.mode); break;
  14593. case MOB_MVPEXP: script_pushint(st,mob->mexp); break;
  14594. default: script_pushint(st,-1); //wrong Index
  14595. }
  14596. return SCRIPT_CMD_SUCCESS;
  14597. }
  14598. /**
  14599. * Check player's vending/buyingstore/autotrade state
  14600. * checkvending({"<Player Name>"})
  14601. * @author [Nab4]
  14602. */
  14603. BUILDIN_FUNC(checkvending) {
  14604. TBL_PC *sd = NULL;
  14605. if (script_hasdata(st,2)) {
  14606. if (!(sd = map_nick2sd(script_getstr(st,2)))) {
  14607. ShowError("buildin_checkvending: Player '%s' is not online!\n", script_getstr(st,2));
  14608. return SCRIPT_CMD_FAILURE;
  14609. }
  14610. }
  14611. else
  14612. sd = script_rid2sd(st);
  14613. if (!sd) {
  14614. script_pushint(st,0);
  14615. return SCRIPT_CMD_SUCCESS;
  14616. }
  14617. else {
  14618. int8 ret = 0;
  14619. if (sd->state.vending)
  14620. ret = 1;
  14621. else if (sd->state.buyingstore)
  14622. ret = 4;
  14623. if (sd->state.autotrade)
  14624. ret |= 2;
  14625. script_pushint(st, ret);
  14626. }
  14627. return SCRIPT_CMD_SUCCESS;
  14628. }
  14629. BUILDIN_FUNC(checkchatting) // check chatting [Marka]
  14630. {
  14631. TBL_PC *sd = NULL;
  14632. if(script_hasdata(st,2))
  14633. sd = map_nick2sd(script_getstr(st,2));
  14634. else
  14635. sd = script_rid2sd(st);
  14636. if(sd)
  14637. script_pushint(st,(sd->chatID != 0));
  14638. else
  14639. script_pushint(st,0);
  14640. return SCRIPT_CMD_SUCCESS;
  14641. }
  14642. BUILDIN_FUNC(checkidle)
  14643. {
  14644. TBL_PC *sd = NULL;
  14645. if (script_hasdata(st, 2))
  14646. sd = map_nick2sd(script_getstr(st, 2));
  14647. else
  14648. sd = script_rid2sd(st);
  14649. if (sd)
  14650. script_pushint(st, DIFF_TICK(last_tick, sd->idletime));
  14651. else
  14652. script_pushint(st, 0);
  14653. return SCRIPT_CMD_SUCCESS;
  14654. }
  14655. BUILDIN_FUNC(searchitem)
  14656. {
  14657. struct script_data* data = script_getdata(st, 2);
  14658. const char *itemname = script_getstr(st,3);
  14659. struct item_data *items[MAX_SEARCH];
  14660. int count;
  14661. char* name;
  14662. int32 start;
  14663. int32 id;
  14664. int32 i;
  14665. TBL_PC* sd = NULL;
  14666. if ((items[0] = itemdb_exists(atoi(itemname))))
  14667. count = 1;
  14668. else
  14669. count = itemdb_searchname_array(items, ARRAYLENGTH(items), itemname);
  14670. if (!count) {
  14671. script_pushint(st, 0);
  14672. return SCRIPT_CMD_SUCCESS;
  14673. }
  14674. if( !data_isreference(data) )
  14675. {
  14676. ShowError("script:searchitem: not a variable\n");
  14677. script_reportdata(data);
  14678. st->state = END;
  14679. return SCRIPT_CMD_FAILURE;// not a variable
  14680. }
  14681. id = reference_getid(data);
  14682. start = reference_getindex(data);
  14683. name = reference_getname(data);
  14684. if( not_server_variable(*name) )
  14685. {
  14686. sd = script_rid2sd(st);
  14687. if( sd == NULL )
  14688. return SCRIPT_CMD_SUCCESS;// no player attached
  14689. }
  14690. if( is_string_variable(name) )
  14691. {// string array
  14692. ShowError("script:searchitem: not an integer array reference\n");
  14693. script_reportdata(data);
  14694. st->state = END;
  14695. return SCRIPT_CMD_FAILURE;// not supported
  14696. }
  14697. for( i = 0; i < count; ++start, ++i )
  14698. {// Set array
  14699. void* v = (void*)__64BPRTSIZE((int)items[i]->nameid);
  14700. set_reg(st, sd, reference_uid(id, start), name, v, reference_getref(data));
  14701. }
  14702. script_pushint(st, count);
  14703. return SCRIPT_CMD_SUCCESS;
  14704. }
  14705. // [zBuffer] List of player cont commands --->
  14706. BUILDIN_FUNC(rid2name)
  14707. {
  14708. struct block_list *bl = NULL;
  14709. int rid = script_getnum(st,2);
  14710. if((bl = map_id2bl(rid)))
  14711. {
  14712. switch(bl->type) {
  14713. case BL_MOB: script_pushstrcopy(st,((TBL_MOB*)bl)->name); break;
  14714. case BL_PC: script_pushstrcopy(st,((TBL_PC*)bl)->status.name); break;
  14715. case BL_NPC: script_pushstrcopy(st,((TBL_NPC*)bl)->exname); break;
  14716. case BL_PET: script_pushstrcopy(st,((TBL_PET*)bl)->pet.name); break;
  14717. case BL_HOM: script_pushstrcopy(st,((TBL_HOM*)bl)->homunculus.name); break;
  14718. case BL_MER: script_pushstrcopy(st,((TBL_MER*)bl)->db->name); break;
  14719. default:
  14720. ShowError("buildin_rid2name: BL type unknown.\n");
  14721. script_pushconststr(st,"");
  14722. break;
  14723. }
  14724. } else {
  14725. ShowError("buildin_rid2name: invalid RID\n");
  14726. script_pushconststr(st,"(null)");
  14727. }
  14728. return SCRIPT_CMD_SUCCESS;
  14729. }
  14730. BUILDIN_FUNC(pcblockmove)
  14731. {
  14732. struct block_list *bl = NULL;
  14733. if (script_getnum(st, 2))
  14734. bl = map_id2bl(script_getnum(st,2));
  14735. else
  14736. bl = map_id2bl(st->rid);
  14737. if (bl) {
  14738. struct unit_data *ud = unit_bl2ud(bl);
  14739. if (ud)
  14740. ud->state.blockedmove = script_getnum(st,3) > 0;
  14741. }
  14742. return SCRIPT_CMD_SUCCESS;
  14743. }
  14744. BUILDIN_FUNC(pcfollow)
  14745. {
  14746. int id, targetid;
  14747. TBL_PC *sd = NULL;
  14748. id = script_getnum(st,2);
  14749. targetid = script_getnum(st,3);
  14750. if(id)
  14751. sd = map_id2sd(id);
  14752. else
  14753. sd = script_rid2sd(st);
  14754. if(sd)
  14755. pc_follow(sd, targetid);
  14756. return SCRIPT_CMD_SUCCESS;
  14757. }
  14758. BUILDIN_FUNC(pcstopfollow)
  14759. {
  14760. int id;
  14761. TBL_PC *sd = NULL;
  14762. id = script_getnum(st,2);
  14763. if(id)
  14764. sd = map_id2sd(id);
  14765. else
  14766. sd = script_rid2sd(st);
  14767. if(sd)
  14768. pc_stop_following(sd);
  14769. return SCRIPT_CMD_SUCCESS;
  14770. }
  14771. // <--- [zBuffer] List of player cont commands
  14772. // [zBuffer] List of unit control commands --->
  14773. /// Checks to see if the unit exists.
  14774. ///
  14775. /// unitexists <unit id>;
  14776. BUILDIN_FUNC(unitexists)
  14777. {
  14778. struct block_list* bl;
  14779. bl = map_id2bl(script_getnum(st, 2));
  14780. if (!bl)
  14781. script_pushint(st, false);
  14782. else
  14783. script_pushint(st, true);
  14784. return SCRIPT_CMD_SUCCESS;
  14785. }
  14786. /// Gets the type of the given Game ID.
  14787. ///
  14788. /// getunittype <unit id>;
  14789. BUILDIN_FUNC(getunittype)
  14790. {
  14791. struct block_list* bl;
  14792. uint8 value = 0;
  14793. if(!script_rid2bl(2,bl))
  14794. {
  14795. script_pushint(st, -1);
  14796. return SCRIPT_CMD_FAILURE;
  14797. }
  14798. switch (bl->type) {
  14799. case BL_PC: value = UNITTYPE_PC; break;
  14800. case BL_NPC: value = UNITTYPE_NPC; break;
  14801. case BL_PET: value = UNITTYPE_PET; break;
  14802. case BL_MOB: value = UNITTYPE_MOB; break;
  14803. case BL_HOM: value = UNITTYPE_HOM; break;
  14804. case BL_MER: value = UNITTYPE_MER; break;
  14805. case BL_ELEM: value = UNITTYPE_ELEM; break;
  14806. default: value = -1; break;
  14807. }
  14808. script_pushint(st, value);
  14809. return SCRIPT_CMD_SUCCESS;
  14810. }
  14811. /// Gets specific live information of a bl.
  14812. ///
  14813. /// getunitdata <unit id>,<arrayname>;
  14814. BUILDIN_FUNC(getunitdata)
  14815. {
  14816. TBL_PC *sd = st->rid ? map_id2sd(st->rid) : NULL;
  14817. struct block_list* bl;
  14818. TBL_MOB* md = NULL;
  14819. TBL_HOM* hd = NULL;
  14820. TBL_MER* mc = NULL;
  14821. TBL_PET* pd = NULL;
  14822. TBL_ELEM* ed = NULL;
  14823. TBL_NPC* nd = NULL;
  14824. char* name;
  14825. struct script_data *data = script_getdata(st, 3);
  14826. if (!data_isreference(data)) {
  14827. ShowWarning("buildin_getunitdata: Error in argument! Please give a variable to store values in.\n");
  14828. return SCRIPT_CMD_FAILURE;
  14829. }
  14830. if(!script_rid2bl(2,bl))
  14831. {
  14832. script_pushint(st, -1);
  14833. return SCRIPT_CMD_FAILURE;
  14834. }
  14835. switch (bl->type) {
  14836. case BL_MOB: md = map_id2md(bl->id); break;
  14837. case BL_HOM: hd = map_id2hd(bl->id); break;
  14838. case BL_PET: pd = map_id2pd(bl->id); break;
  14839. case BL_MER: mc = map_id2mc(bl->id); break;
  14840. case BL_ELEM: ed = map_id2ed(bl->id); break;
  14841. case BL_NPC: nd = map_id2nd(bl->id); break;
  14842. }
  14843. name = reference_getname(data);
  14844. #define getunitdata_sub(idx__,var__) setd_sub(st,sd,name,(idx__),(void *)__64BPRTSIZE((int)(var__)),data->ref)
  14845. switch(bl->type) {
  14846. case BL_MOB:
  14847. if (!md) {
  14848. ShowWarning("buildin_getunitdata: Error in finding object BL_MOB!\n");
  14849. return SCRIPT_CMD_FAILURE;
  14850. }
  14851. getunitdata_sub(UMOB_SIZE, md->status.size);
  14852. getunitdata_sub(UMOB_LEVEL, md->level);
  14853. getunitdata_sub(UMOB_HP, md->status.hp);
  14854. getunitdata_sub(UMOB_MAXHP, md->status.max_hp);
  14855. getunitdata_sub(UMOB_MASTERAID, md->master_id);
  14856. getunitdata_sub(UMOB_MAPID, md->bl.m);
  14857. getunitdata_sub(UMOB_X, md->bl.x);
  14858. getunitdata_sub(UMOB_Y, md->bl.y);
  14859. getunitdata_sub(UMOB_SPEED, md->status.speed);
  14860. getunitdata_sub(UMOB_MODE, md->status.mode);
  14861. getunitdata_sub(UMOB_AI, md->special_state.ai);
  14862. getunitdata_sub(UMOB_SCOPTION, md->sc.option);
  14863. getunitdata_sub(UMOB_SEX, md->vd->sex);
  14864. getunitdata_sub(UMOB_CLASS, md->vd->class_);
  14865. getunitdata_sub(UMOB_HAIRSTYLE, md->vd->hair_style);
  14866. getunitdata_sub(UMOB_HAIRCOLOR, md->vd->hair_color);
  14867. getunitdata_sub(UMOB_HEADBOTTOM, md->vd->head_bottom);
  14868. getunitdata_sub(UMOB_HEADMIDDLE, md->vd->head_mid);
  14869. getunitdata_sub(UMOB_HEADTOP, md->vd->head_top);
  14870. getunitdata_sub(UMOB_CLOTHCOLOR, md->vd->cloth_color);
  14871. getunitdata_sub(UMOB_SHIELD, md->vd->shield);
  14872. getunitdata_sub(UMOB_WEAPON, md->vd->weapon);
  14873. getunitdata_sub(UMOB_LOOKDIR, md->ud.dir);
  14874. getunitdata_sub(UMOB_CANMOVETICK, md->ud.canmove_tick);
  14875. getunitdata_sub(UMOB_STR, md->status.str);
  14876. getunitdata_sub(UMOB_AGI, md->status.agi);
  14877. getunitdata_sub(UMOB_VIT, md->status.vit);
  14878. getunitdata_sub(UMOB_INT, md->status.int_);
  14879. getunitdata_sub(UMOB_DEX, md->status.dex);
  14880. getunitdata_sub(UMOB_LUK, md->status.luk);
  14881. getunitdata_sub(UMOB_SLAVECPYMSTRMD, md->state.copy_master_mode);
  14882. getunitdata_sub(UMOB_DMGIMMUNE, md->ud.immune_attack);
  14883. getunitdata_sub(UMOB_ATKRANGE, md->status.rhw.range);
  14884. getunitdata_sub(UMOB_ATKMIN, md->status.rhw.atk);
  14885. getunitdata_sub(UMOB_ATKMAX, md->status.rhw.atk2);
  14886. getunitdata_sub(UMOB_MATKMIN, md->status.matk_min);
  14887. getunitdata_sub(UMOB_MATKMAX, md->status.matk_max);
  14888. getunitdata_sub(UMOB_DEF, md->status.def);
  14889. getunitdata_sub(UMOB_MDEF, md->status.mdef);
  14890. getunitdata_sub(UMOB_HIT, md->status.hit);
  14891. getunitdata_sub(UMOB_FLEE, md->status.flee);
  14892. getunitdata_sub(UMOB_PDODGE, md->status.flee2);
  14893. getunitdata_sub(UMOB_CRIT, md->status.cri);
  14894. getunitdata_sub(UMOB_RACE, md->status.race);
  14895. getunitdata_sub(UMOB_ELETYPE, md->status.def_ele);
  14896. getunitdata_sub(UMOB_ELELEVEL, md->status.ele_lv);
  14897. getunitdata_sub(UMOB_AMOTION, md->status.amotion);
  14898. getunitdata_sub(UMOB_ADELAY, md->status.adelay);
  14899. getunitdata_sub(UMOB_DMOTION, md->status.dmotion);
  14900. break;
  14901. case BL_HOM:
  14902. if (!hd) {
  14903. ShowWarning("buildin_getunitdata: Error in finding object BL_HOM!\n");
  14904. return SCRIPT_CMD_FAILURE;
  14905. }
  14906. getunitdata_sub(UHOM_SIZE, hd->base_status.size);
  14907. getunitdata_sub(UHOM_LEVEL, hd->homunculus.level);
  14908. getunitdata_sub(UHOM_HP, hd->homunculus.hp);
  14909. getunitdata_sub(UHOM_MAXHP, hd->homunculus.max_hp);
  14910. getunitdata_sub(UHOM_SP, hd->homunculus.sp);
  14911. getunitdata_sub(UHOM_MAXSP, hd->homunculus.max_sp);
  14912. getunitdata_sub(UHOM_MASTERCID, hd->homunculus.char_id);
  14913. getunitdata_sub(UHOM_MAPID, hd->bl.m);
  14914. getunitdata_sub(UHOM_X, hd->bl.x);
  14915. getunitdata_sub(UHOM_Y, hd->bl.y);
  14916. getunitdata_sub(UHOM_HUNGER, hd->homunculus.hunger);
  14917. getunitdata_sub(UHOM_INTIMACY, hd->homunculus.intimacy);
  14918. getunitdata_sub(UHOM_SPEED, hd->base_status.speed);
  14919. getunitdata_sub(UHOM_LOOKDIR, hd->ud.dir);
  14920. getunitdata_sub(UHOM_CANMOVETICK, hd->ud.canmove_tick);
  14921. getunitdata_sub(UHOM_STR, hd->base_status.str);
  14922. getunitdata_sub(UHOM_AGI, hd->base_status.agi);
  14923. getunitdata_sub(UHOM_VIT, hd->base_status.vit);
  14924. getunitdata_sub(UHOM_INT, hd->base_status.int_);
  14925. getunitdata_sub(UHOM_DEX, hd->base_status.dex);
  14926. getunitdata_sub(UHOM_LUK, hd->base_status.luk);
  14927. getunitdata_sub(UHOM_DMGIMMUNE, hd->ud.immune_attack);
  14928. getunitdata_sub(UHOM_ATKRANGE, hd->battle_status.rhw.range);
  14929. getunitdata_sub(UHOM_ATKMIN, hd->base_status.rhw.atk);
  14930. getunitdata_sub(UHOM_ATKMAX, hd->base_status.rhw.atk2);
  14931. getunitdata_sub(UHOM_MATKMIN, hd->base_status.matk_min);
  14932. getunitdata_sub(UHOM_MATKMAX, hd->base_status.matk_max);
  14933. getunitdata_sub(UHOM_DEF, hd->battle_status.def);
  14934. getunitdata_sub(UHOM_MDEF, hd->battle_status.mdef);
  14935. getunitdata_sub(UHOM_HIT, hd->battle_status.hit);
  14936. getunitdata_sub(UHOM_FLEE, hd->battle_status.flee);
  14937. getunitdata_sub(UHOM_PDODGE, hd->battle_status.flee2);
  14938. getunitdata_sub(UHOM_CRIT, hd->battle_status.cri);
  14939. getunitdata_sub(UHOM_RACE, hd->battle_status.race);
  14940. getunitdata_sub(UHOM_ELETYPE, hd->battle_status.def_ele);
  14941. getunitdata_sub(UHOM_ELELEVEL, hd->battle_status.ele_lv);
  14942. getunitdata_sub(UHOM_AMOTION, hd->battle_status.amotion);
  14943. getunitdata_sub(UHOM_ADELAY, hd->battle_status.adelay);
  14944. getunitdata_sub(UHOM_DMOTION, hd->battle_status.dmotion);
  14945. break;
  14946. case BL_PET:
  14947. if (!pd) {
  14948. ShowWarning("buildin_getunitdata: Error in finding object BL_PET!\n");
  14949. return SCRIPT_CMD_FAILURE;
  14950. }
  14951. getunitdata_sub(UPET_SIZE, pd->status.size);
  14952. getunitdata_sub(UPET_LEVEL, pd->pet.level);
  14953. getunitdata_sub(UPET_HP, pd->status.hp);
  14954. getunitdata_sub(UPET_MAXHP, pd->status.max_hp);
  14955. getunitdata_sub(UPET_MASTERAID, pd->pet.account_id);
  14956. getunitdata_sub(UPET_MAPID, pd->bl.m);
  14957. getunitdata_sub(UPET_X, pd->bl.x);
  14958. getunitdata_sub(UPET_Y, pd->bl.y);
  14959. getunitdata_sub(UPET_HUNGER, pd->pet.hungry);
  14960. getunitdata_sub(UPET_INTIMACY, pd->pet.intimate);
  14961. getunitdata_sub(UPET_SPEED, pd->status.speed);
  14962. getunitdata_sub(UPET_LOOKDIR, pd->ud.dir);
  14963. getunitdata_sub(UPET_CANMOVETICK, pd->ud.canmove_tick);
  14964. getunitdata_sub(UPET_STR, pd->status.str);
  14965. getunitdata_sub(UPET_AGI, pd->status.agi);
  14966. getunitdata_sub(UPET_VIT, pd->status.vit);
  14967. getunitdata_sub(UPET_INT, pd->status.int_);
  14968. getunitdata_sub(UPET_DEX, pd->status.dex);
  14969. getunitdata_sub(UPET_LUK, pd->status.luk);
  14970. getunitdata_sub(UPET_DMGIMMUNE, pd->ud.immune_attack);
  14971. getunitdata_sub(UPET_ATKRANGE, pd->status.rhw.range);
  14972. getunitdata_sub(UPET_ATKMIN, pd->status.rhw.atk);
  14973. getunitdata_sub(UPET_ATKMAX, pd->status.rhw.atk2);
  14974. getunitdata_sub(UPET_MATKMIN, pd->status.matk_min);
  14975. getunitdata_sub(UPET_MATKMAX, pd->status.matk_max);
  14976. getunitdata_sub(UPET_DEF, pd->status.def);
  14977. getunitdata_sub(UPET_MDEF, pd->status.mdef);
  14978. getunitdata_sub(UPET_HIT, pd->status.hit);
  14979. getunitdata_sub(UPET_FLEE, pd->status.flee);
  14980. getunitdata_sub(UPET_PDODGE, pd->status.flee2);
  14981. getunitdata_sub(UPET_CRIT, pd->status.cri);
  14982. getunitdata_sub(UPET_RACE, pd->status.race);
  14983. getunitdata_sub(UPET_ELETYPE, pd->status.def_ele);
  14984. getunitdata_sub(UPET_ELELEVEL, pd->status.ele_lv);
  14985. getunitdata_sub(UPET_AMOTION, pd->status.amotion);
  14986. getunitdata_sub(UPET_ADELAY, pd->status.adelay);
  14987. getunitdata_sub(UPET_DMOTION, pd->status.dmotion);
  14988. break;
  14989. case BL_MER:
  14990. if (!mc) {
  14991. ShowWarning("buildin_getunitdata: Error in finding object BL_MER!\n");
  14992. return SCRIPT_CMD_FAILURE;
  14993. }
  14994. getunitdata_sub(UMER_SIZE, mc->base_status.size);
  14995. getunitdata_sub(UMER_HP, mc->base_status.hp);
  14996. getunitdata_sub(UMER_MAXHP, mc->base_status.max_hp);
  14997. getunitdata_sub(UMER_MASTERCID, mc->mercenary.char_id);
  14998. getunitdata_sub(UMER_MAPID, mc->bl.m);
  14999. getunitdata_sub(UMER_X, mc->bl.x);
  15000. getunitdata_sub(UMER_Y, mc->bl.y);
  15001. getunitdata_sub(UMER_KILLCOUNT, mc->mercenary.kill_count);
  15002. getunitdata_sub(UMER_LIFETIME, mc->mercenary.life_time);
  15003. getunitdata_sub(UMER_SPEED, mc->base_status.speed);
  15004. getunitdata_sub(UMER_LOOKDIR, mc->ud.dir);
  15005. getunitdata_sub(UMER_CANMOVETICK, mc->ud.canmove_tick);
  15006. getunitdata_sub(UMER_STR, mc->base_status.str);
  15007. getunitdata_sub(UMER_AGI, mc->base_status.agi);
  15008. getunitdata_sub(UMER_VIT, mc->base_status.vit);
  15009. getunitdata_sub(UMER_INT, mc->base_status.int_);
  15010. getunitdata_sub(UMER_DEX, mc->base_status.dex);
  15011. getunitdata_sub(UMER_LUK, mc->base_status.luk);
  15012. getunitdata_sub(UMER_DMGIMMUNE, mc->ud.immune_attack);
  15013. getunitdata_sub(UMER_ATKRANGE, mc->base_status.rhw.range);
  15014. getunitdata_sub(UMER_ATKMIN, mc->base_status.rhw.atk);
  15015. getunitdata_sub(UMER_ATKMAX, mc->base_status.rhw.atk2);
  15016. getunitdata_sub(UMER_MATKMIN, mc->base_status.matk_min);
  15017. getunitdata_sub(UMER_MATKMAX, mc->base_status.matk_max);
  15018. getunitdata_sub(UMER_DEF, mc->base_status.def);
  15019. getunitdata_sub(UMER_MDEF, mc->base_status.mdef);
  15020. getunitdata_sub(UMER_HIT, mc->base_status.hit);
  15021. getunitdata_sub(UMER_FLEE, mc->base_status.flee);
  15022. getunitdata_sub(UMER_PDODGE, mc->base_status.flee2);
  15023. getunitdata_sub(UMER_CRIT, mc->base_status.cri);
  15024. getunitdata_sub(UMER_RACE, mc->base_status.race);
  15025. getunitdata_sub(UMER_ELETYPE, mc->base_status.def_ele);
  15026. getunitdata_sub(UMER_ELELEVEL, mc->base_status.ele_lv);
  15027. getunitdata_sub(UMER_AMOTION, mc->base_status.amotion);
  15028. getunitdata_sub(UMER_ADELAY, mc->base_status.adelay);
  15029. getunitdata_sub(UMER_DMOTION, mc->base_status.dmotion);
  15030. break;
  15031. case BL_ELEM:
  15032. if (!ed) {
  15033. ShowWarning("buildin_getunitdata: Error in finding object BL_ELEM!\n");
  15034. return SCRIPT_CMD_FAILURE;
  15035. }
  15036. getunitdata_sub(UELE_SIZE, ed->base_status.size);
  15037. getunitdata_sub(UELE_HP, ed->elemental.hp);
  15038. getunitdata_sub(UELE_MAXHP, ed->elemental.max_hp);
  15039. getunitdata_sub(UELE_SP, ed->elemental.sp);
  15040. getunitdata_sub(UELE_MAXSP, ed->elemental.max_sp);
  15041. getunitdata_sub(UELE_MASTERCID, ed->elemental.char_id);
  15042. getunitdata_sub(UELE_MAPID, ed->bl.m);
  15043. getunitdata_sub(UELE_X, ed->bl.x);
  15044. getunitdata_sub(UELE_Y, ed->bl.y);
  15045. getunitdata_sub(UELE_LIFETIME, ed->elemental.life_time);
  15046. getunitdata_sub(UELE_MODE, ed->elemental.mode);
  15047. getunitdata_sub(UELE_SP, ed->base_status.speed);
  15048. getunitdata_sub(UELE_LOOKDIR, ed->ud.dir);
  15049. getunitdata_sub(UELE_CANMOVETICK, ed->ud.canmove_tick);
  15050. getunitdata_sub(UELE_STR, ed->base_status.str);
  15051. getunitdata_sub(UELE_AGI, ed->base_status.agi);
  15052. getunitdata_sub(UELE_VIT, ed->base_status.vit);
  15053. getunitdata_sub(UELE_INT, ed->base_status.int_);
  15054. getunitdata_sub(UELE_DEX, ed->base_status.dex);
  15055. getunitdata_sub(UELE_LUK, ed->base_status.luk);
  15056. getunitdata_sub(UELE_DMGIMMUNE, ed->ud.immune_attack);
  15057. getunitdata_sub(UELE_ATKRANGE, ed->base_status.rhw.range);
  15058. getunitdata_sub(UELE_ATKMIN, ed->base_status.rhw.atk);
  15059. getunitdata_sub(UELE_ATKMAX, ed->base_status.rhw.atk2);
  15060. getunitdata_sub(UELE_MATKMIN, ed->base_status.matk_min);
  15061. getunitdata_sub(UELE_MATKMAX, ed->base_status.matk_max);
  15062. getunitdata_sub(UELE_DEF, ed->base_status.def);
  15063. getunitdata_sub(UELE_MDEF, ed->base_status.mdef);
  15064. getunitdata_sub(UELE_HIT, ed->base_status.hit);
  15065. getunitdata_sub(UELE_FLEE, ed->base_status.flee);
  15066. getunitdata_sub(UELE_PDODGE, ed->base_status.flee2);
  15067. getunitdata_sub(UELE_CRIT, ed->base_status.cri);
  15068. getunitdata_sub(UELE_RACE, ed->base_status.race);
  15069. getunitdata_sub(UELE_ELETYPE, ed->base_status.def_ele);
  15070. getunitdata_sub(UELE_ELELEVEL, ed->base_status.ele_lv);
  15071. getunitdata_sub(UELE_AMOTION, ed->base_status.amotion);
  15072. getunitdata_sub(UELE_ADELAY, ed->base_status.adelay);
  15073. getunitdata_sub(UELE_DMOTION, ed->base_status.dmotion);
  15074. break;
  15075. case BL_NPC:
  15076. if (!nd) {
  15077. ShowWarning("buildin_getunitdata: Error in finding object BL_NPC!\n");
  15078. return SCRIPT_CMD_FAILURE;
  15079. }
  15080. getunitdata_sub(UNPC_DISPLAY, nd->class_);
  15081. getunitdata_sub(UNPC_LEVEL, nd->level);
  15082. getunitdata_sub(UNPC_HP, nd->status.hp);
  15083. getunitdata_sub(UNPC_MAXHP, nd->status.max_hp);
  15084. getunitdata_sub(UNPC_MAPID, nd->bl.m);
  15085. getunitdata_sub(UNPC_X, nd->bl.x);
  15086. getunitdata_sub(UNPC_Y, nd->bl.y);
  15087. getunitdata_sub(UNPC_LOOKDIR, nd->ud.dir);
  15088. getunitdata_sub(UNPC_STR, nd->status.str);
  15089. getunitdata_sub(UNPC_AGI, nd->status.agi);
  15090. getunitdata_sub(UNPC_VIT, nd->status.vit);
  15091. getunitdata_sub(UNPC_INT, nd->status.int_);
  15092. getunitdata_sub(UNPC_DEX, nd->status.dex);
  15093. getunitdata_sub(UNPC_LUK, nd->status.luk);
  15094. getunitdata_sub(UNPC_PLUSALLSTAT, nd->stat_point);
  15095. getunitdata_sub(UNPC_DMGIMMUNE, nd->ud.immune_attack);
  15096. getunitdata_sub(UNPC_ATKRANGE, nd->status.rhw.range);
  15097. getunitdata_sub(UNPC_ATKMIN, nd->status.rhw.atk);
  15098. getunitdata_sub(UNPC_ATKMAX, nd->status.rhw.atk2);
  15099. getunitdata_sub(UNPC_MATKMIN, nd->status.matk_min);
  15100. getunitdata_sub(UNPC_MATKMAX, nd->status.matk_max);
  15101. getunitdata_sub(UNPC_DEF, nd->status.def);
  15102. getunitdata_sub(UNPC_MDEF, nd->status.mdef);
  15103. getunitdata_sub(UNPC_HIT, nd->status.hit);
  15104. getunitdata_sub(UNPC_FLEE, nd->status.flee);
  15105. getunitdata_sub(UNPC_PDODGE, nd->status.flee2);
  15106. getunitdata_sub(UNPC_CRIT, nd->status.cri);
  15107. getunitdata_sub(UNPC_RACE, nd->status.race);
  15108. getunitdata_sub(UNPC_ELETYPE, nd->status.def_ele);
  15109. getunitdata_sub(UNPC_ELELEVEL, nd->status.ele_lv);
  15110. getunitdata_sub(UNPC_AMOTION, nd->status.amotion);
  15111. getunitdata_sub(UNPC_ADELAY, nd->status.adelay);
  15112. getunitdata_sub(UNPC_DMOTION, nd->status.dmotion);
  15113. break;
  15114. default:
  15115. ShowWarning("buildin_getunitdata: Unknown object type!\n");
  15116. return SCRIPT_CMD_FAILURE;
  15117. }
  15118. #undef getunitdata_sub
  15119. return SCRIPT_CMD_SUCCESS;
  15120. }
  15121. /// Changes the live data of a bl.
  15122. ///
  15123. /// setunitdata <unit id>,<type>,<value>;
  15124. BUILDIN_FUNC(setunitdata)
  15125. {
  15126. struct block_list* bl = NULL;
  15127. struct script_data* data;
  15128. const char *mapname = NULL;
  15129. TBL_MOB* md = NULL;
  15130. TBL_HOM* hd = NULL;
  15131. TBL_MER* mc = NULL;
  15132. TBL_PET* pd = NULL;
  15133. TBL_ELEM* ed = NULL;
  15134. TBL_NPC* nd = NULL;
  15135. int type, value = 0;
  15136. if(!script_rid2bl(2,bl))
  15137. {
  15138. script_pushint(st, -1);
  15139. return SCRIPT_CMD_FAILURE;
  15140. }
  15141. switch (bl->type) {
  15142. case BL_MOB: md = map_id2md(bl->id); break;
  15143. case BL_HOM: hd = map_id2hd(bl->id); break;
  15144. case BL_PET: pd = map_id2pd(bl->id); break;
  15145. case BL_MER: mc = map_id2mc(bl->id); break;
  15146. case BL_ELEM: ed = map_id2ed(bl->id); break;
  15147. case BL_NPC:
  15148. nd = map_id2nd(bl->id);
  15149. if (!nd->status.hp)
  15150. status_calc_npc(nd, SCO_FIRST);
  15151. else
  15152. status_calc_npc(nd, SCO_NONE);
  15153. break;
  15154. default:
  15155. ShowError("buildin_setunitdata: Invalid object!\n");
  15156. return SCRIPT_CMD_FAILURE;
  15157. }
  15158. type = script_getnum(st, 3);
  15159. data = script_getdata(st, 4);
  15160. get_val(st, data);
  15161. if (type == 5 && data_isstring(data))
  15162. mapname = conv_str(st, data);
  15163. else if (data_isint(data))
  15164. value = conv_num(st, data);
  15165. else {
  15166. ShowError("buildin_setunitdata: Invalid data type for argument #3 (%d).\n", data->type);
  15167. return SCRIPT_CMD_FAILURE;
  15168. }
  15169. switch (bl->type) {
  15170. case BL_MOB:
  15171. if (!md) {
  15172. ShowWarning("buildin_setunitdata: Error in finding object BL_MOB!\n");
  15173. return SCRIPT_CMD_FAILURE;
  15174. }
  15175. switch (type) {
  15176. case UMOB_SIZE: md->status.size = (unsigned char)value; break;
  15177. case UMOB_LEVEL: md->level = (unsigned short)value; break;
  15178. case UMOB_HP: status_set_hp(bl, (unsigned int)value, 0); clif_name_area(&md->bl); break;
  15179. case UMOB_MAXHP: status_set_maxhp(bl, (unsigned int)value, 0); clif_name_area(&md->bl); break;
  15180. case UMOB_MASTERAID: md->master_id = value; break;
  15181. case UMOB_MAPID: if (mapname) value = map_mapname2mapid(mapname); unit_warp(bl, (short)value, 0, 0, CLR_TELEPORT); break;
  15182. case UMOB_X: if (!unit_walktoxy(bl, (short)value, md->bl.y, 2)) unit_movepos(bl, (short)value, md->bl.y, 0, 0); break;
  15183. case UMOB_Y: if (!unit_walktoxy(bl, md->bl.x, (short)value, 2)) unit_movepos(bl, md->bl.x, (short)value, 0, 0); break;
  15184. case UMOB_SPEED: md->status.speed = (unsigned short)value; status_calc_misc(bl, &md->status, md->level); break;
  15185. case UMOB_MODE: md->status.mode = (enum e_mode)value; break;
  15186. case UMOB_AI: md->special_state.ai = (enum mob_ai)value; break;
  15187. case UMOB_SCOPTION: md->sc.option = (unsigned short)value; break;
  15188. case UMOB_SEX: md->vd->sex = (char)value; break;
  15189. case UMOB_CLASS: status_set_viewdata(bl, (unsigned short)value); break;
  15190. case UMOB_HAIRSTYLE: clif_changelook(bl, LOOK_HAIR, (unsigned short)value); break;
  15191. case UMOB_HAIRCOLOR: clif_changelook(bl, LOOK_HAIR_COLOR, (unsigned short)value); break;
  15192. case UMOB_HEADBOTTOM: clif_changelook(bl, LOOK_HEAD_BOTTOM, (unsigned short)value); break;
  15193. case UMOB_HEADMIDDLE: clif_changelook(bl, LOOK_HEAD_MID, (unsigned short)value); break;
  15194. case UMOB_HEADTOP: clif_changelook(bl, LOOK_HEAD_TOP, (unsigned short)value); break;
  15195. case UMOB_CLOTHCOLOR: clif_changelook(bl, LOOK_CLOTHES_COLOR, (unsigned short)value); break;
  15196. case UMOB_SHIELD: clif_changelook(bl, LOOK_SHIELD, (unsigned short)value); break;
  15197. case UMOB_WEAPON: clif_changelook(bl, LOOK_WEAPON, (unsigned short)value); break;
  15198. case UMOB_LOOKDIR: unit_setdir(bl, (uint8)value); break;
  15199. case UMOB_CANMOVETICK: md->ud.canmove_tick = value > 0 ? (unsigned int)value : 0; break;
  15200. case UMOB_STR: md->status.str = (unsigned short)value; status_calc_misc(bl, &md->status, md->level); break;
  15201. case UMOB_AGI: md->status.agi = (unsigned short)value; status_calc_misc(bl, &md->status, md->level); break;
  15202. case UMOB_VIT: md->status.vit = (unsigned short)value; status_calc_misc(bl, &md->status, md->level); break;
  15203. case UMOB_INT: md->status.int_ = (unsigned short)value; status_calc_misc(bl, &md->status, md->level); break;
  15204. case UMOB_DEX: md->status.dex = (unsigned short)value; status_calc_misc(bl, &md->status, md->level); break;
  15205. case UMOB_LUK: md->status.luk = (unsigned short)value; status_calc_misc(bl, &md->status, md->level); break;
  15206. case UMOB_SLAVECPYMSTRMD:
  15207. if (value > 0) {
  15208. TBL_MOB *md2 = NULL;
  15209. if (!md->master_id || !(md2 = map_id2md(md->master_id))) {
  15210. ShowWarning("buildin_setunitdata: Trying to set UMOB_SLAVECPYMSTRMD on mob without master!\n");
  15211. break;
  15212. }
  15213. md->status.mode = md2->status.mode;
  15214. md->state.copy_master_mode = 1;
  15215. } else
  15216. md->state.copy_master_mode = 0;
  15217. break;
  15218. case UMOB_DMGIMMUNE: md->ud.immune_attack = (bool)value > 0 ? 1 : 0; break;
  15219. case UMOB_ATKRANGE: md->status.rhw.range = (unsigned short)value; break;
  15220. case UMOB_ATKMIN: md->status.rhw.atk = (unsigned short)value; break;
  15221. case UMOB_ATKMAX: md->status.rhw.atk2 = (unsigned short)value; break;
  15222. case UMOB_MATKMIN: md->status.matk_min = (unsigned short)value; break;
  15223. case UMOB_MATKMAX: md->status.matk_max = (unsigned short)value; break;
  15224. case UMOB_DEF: md->status.def = (defType)value; break;
  15225. case UMOB_MDEF: md->status.mdef = (defType)value; break;
  15226. case UMOB_HIT: md->status.hit = (short)value; break;
  15227. case UMOB_FLEE: md->status.flee = (short)value; break;
  15228. case UMOB_PDODGE: md->status.flee2 = (short)value; break;
  15229. case UMOB_CRIT: md->status.cri = (short)value; break;
  15230. case UMOB_RACE: md->status.race = (unsigned char)value; break;
  15231. case UMOB_ELETYPE: md->status.def_ele = (unsigned char)value; break;
  15232. case UMOB_ELELEVEL: md->status.ele_lv = (unsigned char)value; break;
  15233. case UMOB_AMOTION: md->status.amotion = (short)value; break;
  15234. case UMOB_ADELAY: md->status.adelay = (short)value; break;
  15235. case UMOB_DMOTION: md->status.dmotion = (short)value; break;
  15236. default:
  15237. ShowError("buildin_setunitdata: Unknown data identifier %d for BL_MOB.\n", type);
  15238. return SCRIPT_CMD_FAILURE;
  15239. }
  15240. break;
  15241. case BL_HOM:
  15242. if (!hd) {
  15243. ShowWarning("buildin_setunitdata: Error in finding object BL_HOM!\n");
  15244. return SCRIPT_CMD_FAILURE;
  15245. }
  15246. switch (type) {
  15247. case UHOM_SIZE: hd->base_status.size = (unsigned char)value; break;
  15248. case UHOM_LEVEL: hd->homunculus.level = (unsigned short)value; break;
  15249. case UHOM_HP: status_set_hp(bl, (unsigned int)value, 0); break;
  15250. case UHOM_MAXHP: status_set_maxhp(bl, (unsigned int)value, 0); break;
  15251. case UHOM_SP: status_set_sp(bl, (unsigned int)value, 0); break;
  15252. case UHOM_MAXSP: status_set_maxsp(bl, (unsigned int)value, 0); break;
  15253. case UHOM_MASTERCID: hd->homunculus.char_id = (uint32)value; break;
  15254. case UHOM_MAPID: if (mapname) value = map_mapname2mapid(mapname); unit_warp(bl, (short)value, 0, 0, CLR_TELEPORT); break;
  15255. case UHOM_X: if (!unit_walktoxy(bl, (short)value, hd->bl.y, 2)) unit_movepos(bl, (short)value, hd->bl.y, 0, 0); break;
  15256. case UHOM_Y: if (!unit_walktoxy(bl, hd->bl.x, (short)value, 2)) unit_movepos(bl, hd->bl.x, (short)value, 0, 0); break;
  15257. case UHOM_HUNGER: hd->homunculus.hunger = (short)value; clif_send_homdata(map_charid2sd(hd->homunculus.char_id), SP_HUNGRY, hd->homunculus.hunger); break;
  15258. case UHOM_INTIMACY: hom_increase_intimacy(hd, (unsigned int)value); clif_send_homdata(map_charid2sd(hd->homunculus.char_id), SP_INTIMATE, hd->homunculus.intimacy / 100); break;
  15259. case UHOM_SPEED: hd->base_status.speed = (unsigned short)value; status_calc_misc(bl, &hd->base_status, hd->homunculus.level); break;
  15260. case UHOM_LOOKDIR: unit_setdir(bl, (uint8)value); break;
  15261. case UHOM_CANMOVETICK: hd->ud.canmove_tick = value > 0 ? (unsigned int)value : 0; break;
  15262. case UHOM_STR: hd->base_status.str = (unsigned short)value; status_calc_misc(bl, &hd->base_status, hd->homunculus.level); break;
  15263. case UHOM_AGI: hd->base_status.agi = (unsigned short)value; status_calc_misc(bl, &hd->base_status, hd->homunculus.level); break;
  15264. case UHOM_VIT: hd->base_status.vit = (unsigned short)value; status_calc_misc(bl, &hd->base_status, hd->homunculus.level); break;
  15265. case UHOM_INT: hd->base_status.int_ = (unsigned short)value; status_calc_misc(bl, &hd->base_status, hd->homunculus.level); break;
  15266. case UHOM_DEX: hd->base_status.dex = (unsigned short)value; status_calc_misc(bl, &hd->base_status, hd->homunculus.level); break;
  15267. case UHOM_LUK: hd->base_status.luk = (unsigned short)value; status_calc_misc(bl, &hd->base_status, hd->homunculus.level); break;
  15268. case UHOM_DMGIMMUNE: hd->ud.immune_attack = (bool)value > 0 ? 1 : 0; break;
  15269. case UHOM_ATKRANGE: hd->base_status.rhw.range = (unsigned short)value; break;
  15270. case UHOM_ATKMIN: hd->base_status.rhw.atk = (unsigned short)value; break;
  15271. case UHOM_ATKMAX: hd->base_status.rhw.atk2 = (unsigned short)value; break;
  15272. case UHOM_MATKMIN: hd->base_status.matk_min = (unsigned short)value; break;
  15273. case UHOM_MATKMAX: hd->base_status.matk_max = (unsigned short)value; break;
  15274. case UHOM_DEF: hd->base_status.def = (defType)value; break;
  15275. case UHOM_MDEF: hd->base_status.mdef = (defType)value; break;
  15276. case UHOM_HIT: hd->base_status.hit = (short)value; break;
  15277. case UHOM_FLEE: hd->base_status.flee = (short)value; break;
  15278. case UHOM_PDODGE: hd->base_status.flee2 = (short)value; break;
  15279. case UHOM_CRIT: hd->base_status.cri = (short)value; break;
  15280. case UHOM_RACE: hd->base_status.race = (unsigned char)value; break;
  15281. case UHOM_ELETYPE: hd->base_status.def_ele = (unsigned char)value; break;
  15282. case UHOM_ELELEVEL: hd->base_status.ele_lv = (unsigned char)value; break;
  15283. case UHOM_AMOTION: hd->base_status.amotion = (short)value; break;
  15284. case UHOM_ADELAY: hd->base_status.adelay = (short)value; break;
  15285. case UHOM_DMOTION: hd->base_status.dmotion = (short)value; break;
  15286. default:
  15287. ShowError("buildin_setunitdata: Unknown data identifier %d for BL_HOM.\n", type);
  15288. return SCRIPT_CMD_FAILURE;
  15289. }
  15290. break;
  15291. case BL_PET:
  15292. if (!pd) {
  15293. ShowWarning("buildin_setunitdata: Error in finding object BL_PET!\n");
  15294. return SCRIPT_CMD_FAILURE;
  15295. }
  15296. switch (type) {
  15297. case UPET_SIZE: pd->status.size = (unsigned char)value; break;
  15298. case UPET_LEVEL: pd->pet.level = (unsigned short)value; break;
  15299. case UPET_HP: status_set_hp(bl, (unsigned int)value, 0); break;
  15300. case UPET_MAXHP: status_set_maxhp(bl, (unsigned int)value, 0); break;
  15301. case UPET_MASTERAID: pd->pet.account_id = (unsigned int)value; break;
  15302. case UPET_MAPID: if (mapname) value = map_mapname2mapid(mapname); unit_warp(bl, (short)value, 0, 0, CLR_TELEPORT); break;
  15303. case UPET_X: if (!unit_walktoxy(bl, (short)value, pd->bl.y, 2)) unit_movepos(bl, (short)value, md->bl.y, 0, 0); break;
  15304. case UPET_Y: if (!unit_walktoxy(bl, pd->bl.x, (short)value, 2)) unit_movepos(bl, pd->bl.x, (short)value, 0, 0); break;
  15305. case UPET_HUNGER: pd->pet.hungry = (short)value; clif_send_petdata(map_id2sd(pd->pet.account_id), pd, 2, pd->pet.hungry); break;
  15306. case UPET_INTIMACY: pet_set_intimate(pd, (unsigned int)value); clif_send_petdata(map_id2sd(pd->pet.account_id), pd, 1, pd->pet.intimate); break;
  15307. case UPET_SPEED: pd->status.speed = (unsigned short)value; status_calc_misc(bl, &pd->status, pd->pet.level); break;
  15308. case UPET_LOOKDIR: unit_setdir(bl, (uint8)value); break;
  15309. case UPET_CANMOVETICK: pd->ud.canmove_tick = value > 0 ? (unsigned int)value : 0; break;
  15310. case UPET_STR: pd->status.str = (unsigned short)value; status_calc_misc(bl, &pd->status, pd->pet.level); break;
  15311. case UPET_AGI: pd->status.agi = (unsigned short)value; status_calc_misc(bl, &pd->status, pd->pet.level); break;
  15312. case UPET_VIT: pd->status.vit = (unsigned short)value; status_calc_misc(bl, &pd->status, pd->pet.level); break;
  15313. case UPET_INT: pd->status.int_ = (unsigned short)value; status_calc_misc(bl, &pd->status, pd->pet.level); break;
  15314. case UPET_DEX: pd->status.dex = (unsigned short)value; status_calc_misc(bl, &pd->status, pd->pet.level); break;
  15315. case UPET_LUK: pd->status.luk = (unsigned short)value; status_calc_misc(bl, &pd->status, pd->pet.level); break;
  15316. case UPET_DMGIMMUNE: pd->ud.immune_attack = (bool)value > 0 ? 1 : 0; break;
  15317. case UPET_ATKRANGE: pd->status.rhw.range = (unsigned short)value; break;
  15318. case UPET_ATKMIN: pd->status.rhw.atk = (unsigned short)value; break;
  15319. case UPET_ATKMAX: pd->status.rhw.atk2 = (unsigned short)value; break;
  15320. case UPET_MATKMIN: pd->status.matk_min = (unsigned short)value; break;
  15321. case UPET_MATKMAX: pd->status.matk_max = (unsigned short)value; break;
  15322. case UPET_DEF: pd->status.def = (defType)value; break;
  15323. case UPET_MDEF: pd->status.mdef = (defType)value; break;
  15324. case UPET_HIT: pd->status.hit = (short)value; break;
  15325. case UPET_FLEE: pd->status.flee = (short)value; break;
  15326. case UPET_PDODGE: pd->status.flee2 = (short)value; break;
  15327. case UPET_CRIT: pd->status.cri = (short)value; break;
  15328. case UPET_RACE: pd->status.race = (unsigned char)value; break;
  15329. case UPET_ELETYPE: pd->status.def_ele = (unsigned char)value; break;
  15330. case UPET_ELELEVEL: pd->status.ele_lv = (unsigned char)value; break;
  15331. case UPET_AMOTION: pd->status.amotion = (short)value; break;
  15332. case UPET_ADELAY: pd->status.adelay = (short)value; break;
  15333. case UPET_DMOTION: pd->status.dmotion = (short)value; break;
  15334. default:
  15335. ShowError("buildin_setunitdata: Unknown data identifier %d for BL_PET.\n", type);
  15336. return SCRIPT_CMD_FAILURE;
  15337. }
  15338. break;
  15339. case BL_MER:
  15340. if (!mc) {
  15341. ShowWarning("buildin_setunitdata: Error in finding object BL_MER!\n");
  15342. return SCRIPT_CMD_FAILURE;
  15343. }
  15344. switch (type) {
  15345. case UMER_SIZE: mc->base_status.size = (unsigned char)value; break;
  15346. case UMER_HP: status_set_hp(bl, (unsigned int)value, 0); break;
  15347. case UMER_MAXHP: status_set_maxhp(bl, (unsigned int)value, 0); break;
  15348. case UMER_MASTERCID: mc->mercenary.char_id = (uint32)value; break;
  15349. case UMER_MAPID: if (mapname) value = map_mapname2mapid(mapname); unit_warp(bl, (short)value, 0, 0, CLR_TELEPORT); break;
  15350. case UMER_X: if (!unit_walktoxy(bl, (short)value, mc->bl.y, 2)) unit_movepos(bl, (short)value, mc->bl.y, 0, 0); break;
  15351. case UMER_Y: if (!unit_walktoxy(bl, mc->bl.x, (short)value, 2)) unit_movepos(bl, mc->bl.x, (short)value, 0, 0); break;
  15352. case UMER_KILLCOUNT: mc->mercenary.kill_count = (unsigned int)value; break;
  15353. case UMER_LIFETIME: mc->mercenary.life_time = (unsigned int)value; break;
  15354. case UMER_SPEED: mc->base_status.speed = (unsigned short)value; status_calc_misc(bl, &mc->base_status, mc->db->lv); break;
  15355. case UMER_LOOKDIR: unit_setdir(bl, (uint8)value); break;
  15356. case UMER_CANMOVETICK: mc->ud.canmove_tick = value > 0 ? (unsigned int)value : 0; break;
  15357. case UMER_STR: mc->base_status.str = (unsigned short)value; status_calc_misc(bl, &mc->base_status, mc->db->lv); break;
  15358. case UMER_AGI: mc->base_status.agi = (unsigned short)value; status_calc_misc(bl, &mc->base_status, mc->db->lv); break;
  15359. case UMER_VIT: mc->base_status.vit = (unsigned short)value; status_calc_misc(bl, &mc->base_status, mc->db->lv); break;
  15360. case UMER_INT: mc->base_status.int_ = (unsigned short)value; status_calc_misc(bl, &mc->base_status, mc->db->lv); break;
  15361. case UMER_DEX: mc->base_status.dex = (unsigned short)value; status_calc_misc(bl, &mc->base_status, mc->db->lv); break;
  15362. case UMER_LUK: mc->base_status.luk = (unsigned short)value; status_calc_misc(bl, &mc->base_status, mc->db->lv); break;
  15363. case UMER_DMGIMMUNE: mc->ud.immune_attack = (bool)value > 0 ? 1 : 0; break;
  15364. case UMER_ATKRANGE: mc->base_status.rhw.range = (unsigned short)value; break;
  15365. case UMER_ATKMIN: mc->base_status.rhw.atk = (unsigned short)value; break;
  15366. case UMER_ATKMAX: mc->base_status.rhw.atk2 = (unsigned short)value; break;
  15367. case UMER_MATKMIN: mc->base_status.matk_min = (unsigned short)value; break;
  15368. case UMER_MATKMAX: mc->base_status.matk_max = (unsigned short)value; break;
  15369. case UMER_DEF: mc->base_status.def = (defType)value; break;
  15370. case UMER_MDEF: mc->base_status.mdef = (defType)value; break;
  15371. case UMER_HIT: mc->base_status.hit = (short)value; break;
  15372. case UMER_FLEE: mc->base_status.flee = (short)value; break;
  15373. case UMER_PDODGE: mc->base_status.flee2 = (short)value; break;
  15374. case UMER_CRIT: mc->base_status.cri = (short)value; break;
  15375. case UMER_RACE: mc->base_status.race = (unsigned char)value; break;
  15376. case UMER_ELETYPE: mc->base_status.def_ele = (unsigned char)value; break;
  15377. case UMER_ELELEVEL: mc->base_status.ele_lv = (unsigned char)value; break;
  15378. case UMER_AMOTION: mc->base_status.amotion = (short)value; break;
  15379. case UMER_ADELAY: mc->base_status.adelay = (short)value; break;
  15380. case UMER_DMOTION: mc->base_status.dmotion = (short)value; break;
  15381. default:
  15382. ShowError("buildin_setunitdata: Unknown data identifier %d for BL_MER.\n", type);
  15383. return SCRIPT_CMD_FAILURE;
  15384. }
  15385. break;
  15386. case BL_ELEM:
  15387. if (!ed) {
  15388. ShowWarning("buildin_setunitdata: Error in finding object BL_ELEM!\n");
  15389. return SCRIPT_CMD_FAILURE;
  15390. }
  15391. switch (type) {
  15392. case UELE_SIZE: ed->base_status.size = (unsigned char)value; break;
  15393. case UELE_HP: status_set_hp(bl, (unsigned int)value, 0); break;
  15394. case UELE_MAXHP: status_set_maxhp(bl, (unsigned int)value, 0); break;
  15395. case UELE_SP: status_set_sp(bl, (unsigned int)value, 0); break;
  15396. case UELE_MAXSP: status_set_maxsp(bl, (unsigned int)value, 0); break;
  15397. case UELE_MASTERCID: ed->elemental.char_id = (uint32)value; break;
  15398. case UELE_MAPID: if (mapname) value = map_mapname2mapid(mapname); unit_warp(bl, (short)value, 0, 0, CLR_TELEPORT); break;
  15399. case UELE_X: if (!unit_walktoxy(bl, (short)value, ed->bl.y, 2)) unit_movepos(bl, (short)value, ed->bl.y, 0, 0); break;
  15400. case UELE_Y: if (!unit_walktoxy(bl, ed->bl.x, (short)value, 2)) unit_movepos(bl, ed->bl.x, (short)value, 0, 0); break;
  15401. case UELE_LIFETIME: ed->elemental.life_time = (unsigned int)value; break;
  15402. case UELE_MODE: ed->elemental.mode = (enum e_mode)value; break;
  15403. case UELE_SPEED: ed->base_status.speed = (unsigned short)value; status_calc_misc(bl, &ed->base_status, ed->db->lv); break;
  15404. case UELE_LOOKDIR: unit_setdir(bl, (uint8)value); break;
  15405. case UELE_CANMOVETICK: ed->ud.canmove_tick = value > 0 ? (unsigned int)value : 0; break;
  15406. case UELE_STR: ed->base_status.str = (unsigned short)value; status_calc_misc(bl, &ed->base_status, ed->db->lv); break;
  15407. case UELE_AGI: ed->base_status.agi = (unsigned short)value; status_calc_misc(bl, &ed->base_status, ed->db->lv); break;
  15408. case UELE_VIT: ed->base_status.vit = (unsigned short)value; status_calc_misc(bl, &ed->base_status, ed->db->lv); break;
  15409. case UELE_INT: ed->base_status.int_ = (unsigned short)value; status_calc_misc(bl, &ed->base_status, ed->db->lv); break;
  15410. case UELE_DEX: ed->base_status.dex = (unsigned short)value; status_calc_misc(bl, &ed->base_status, ed->db->lv); break;
  15411. case UELE_LUK: ed->base_status.luk = (unsigned short)value; status_calc_misc(bl, &ed->base_status, ed->db->lv); break;
  15412. case UELE_DMGIMMUNE: ed->ud.immune_attack = (bool)value > 0 ? 1 : 0; break;
  15413. case UELE_ATKRANGE: ed->base_status.rhw.range = (unsigned short)value; break;
  15414. case UELE_ATKMIN: ed->base_status.rhw.atk = (unsigned short)value; break;
  15415. case UELE_ATKMAX: ed->base_status.rhw.atk2 = (unsigned short)value; break;
  15416. case UELE_MATKMIN: ed->base_status.matk_min = (unsigned short)value; break;
  15417. case UELE_MATKMAX: ed->base_status.matk_max = (unsigned short)value; break;
  15418. case UELE_DEF: ed->base_status.def = (defType)value; break;
  15419. case UELE_MDEF: ed->base_status.mdef = (defType)value; break;
  15420. case UELE_HIT: ed->base_status.hit = (short)value; break;
  15421. case UELE_FLEE: ed->base_status.flee = (short)value; break;
  15422. case UELE_PDODGE: ed->base_status.flee2 = (short)value; break;
  15423. case UELE_CRIT: ed->base_status.cri = (short)value; break;
  15424. case UELE_RACE: ed->base_status.race = (unsigned char)value; break;
  15425. case UELE_ELETYPE: ed->base_status.def_ele = (unsigned char)value; break;
  15426. case UELE_ELELEVEL: ed->base_status.ele_lv = (unsigned char)value; break;
  15427. case UELE_AMOTION: ed->base_status.amotion = (short)value; break;
  15428. case UELE_ADELAY: ed->base_status.adelay = (short)value; break;
  15429. case UELE_DMOTION: ed->base_status.dmotion = (short)value; break;
  15430. default:
  15431. ShowError("buildin_setunitdata: Unknown data identifier %d for BL_ELEM.\n", type);
  15432. return SCRIPT_CMD_FAILURE;
  15433. }
  15434. break;
  15435. case BL_NPC:
  15436. if (!nd) {
  15437. ShowWarning("buildin_setunitdata: Error in finding object BL_NPC!\n");
  15438. return SCRIPT_CMD_FAILURE;
  15439. }
  15440. switch (type) {
  15441. case UNPC_DISPLAY: status_set_viewdata(bl, (unsigned short)value); break;
  15442. case UNPC_LEVEL: nd->level = (unsigned int)value; break;
  15443. case UNPC_HP: status_set_hp(bl, (unsigned int)value, 0); break;
  15444. case UNPC_MAXHP: status_set_maxhp(bl, (unsigned int)value, 0); break;
  15445. case UNPC_MAPID: if (mapname) value = map_mapname2mapid(mapname); unit_warp(bl, (short)value, 0, 0, CLR_TELEPORT); break;
  15446. case UNPC_X: if (!unit_walktoxy(bl, (short)value, nd->bl.y, 2)) unit_movepos(bl, (short)value, nd->bl.x, 0, 0); break;
  15447. case UNPC_Y: if (!unit_walktoxy(bl, nd->bl.x, (short)value, 2)) unit_movepos(bl, nd->bl.x, (short)value, 0, 0); break;
  15448. case UNPC_LOOKDIR: unit_setdir(bl, (uint8)value); break;
  15449. case UNPC_STR: nd->params.str = (unsigned short)value; status_calc_misc(bl, &nd->status, nd->level); break;
  15450. case UNPC_AGI: nd->params.agi = (unsigned short)value; status_calc_misc(bl, &nd->status, nd->level); break;
  15451. case UNPC_VIT: nd->params.vit = (unsigned short)value; status_calc_misc(bl, &nd->status, nd->level); break;
  15452. case UNPC_INT: nd->params.int_ = (unsigned short)value; status_calc_misc(bl, &nd->status, nd->level); break;
  15453. case UNPC_DEX: nd->params.dex = (unsigned short)value; status_calc_misc(bl, &nd->status, nd->level); break;
  15454. case UNPC_LUK: nd->params.luk = (unsigned short)value; status_calc_misc(bl, &nd->status, nd->level); break;
  15455. case UNPC_PLUSALLSTAT: nd->stat_point = (unsigned int)value; break;
  15456. case UNPC_ATKRANGE: nd->status.rhw.range = (unsigned short)value; break;
  15457. case UNPC_ATKMIN: nd->status.rhw.atk = (unsigned short)value; break;
  15458. case UNPC_ATKMAX: nd->status.rhw.atk2 = (unsigned short)value; break;
  15459. case UNPC_MATKMIN: nd->status.matk_min = (unsigned short)value; break;
  15460. case UNPC_MATKMAX: nd->status.matk_max = (unsigned short)value; break;
  15461. case UNPC_DEF: nd->status.def = (defType)value; break;
  15462. case UNPC_MDEF: nd->status.mdef = (defType)value; break;
  15463. case UNPC_HIT: nd->status.hit = (short)value; break;
  15464. case UNPC_FLEE: nd->status.flee = (short)value; break;
  15465. case UNPC_PDODGE: nd->status.flee2 = (short)value; break;
  15466. case UNPC_CRIT: nd->status.cri = (short)value; break;
  15467. case UNPC_RACE: nd->status.race = (unsigned char)value; break;
  15468. case UNPC_ELETYPE: nd->status.def_ele = (unsigned char)value; break;
  15469. case UNPC_ELELEVEL: nd->status.ele_lv = (unsigned char)value; break;
  15470. case UNPC_AMOTION: nd->status.amotion = (short)value; break;
  15471. case UNPC_ADELAY: nd->status.adelay = (short)value; break;
  15472. case UNPC_DMOTION: nd->status.dmotion = (short)value; break;
  15473. default:
  15474. ShowError("buildin_setunitdata: Unknown data identifier %d for BL_NPC.\n", type);
  15475. return SCRIPT_CMD_FAILURE;
  15476. }
  15477. break;
  15478. default:
  15479. ShowWarning("buildin_setunitdata: Unknown object type!\n");
  15480. return SCRIPT_CMD_FAILURE;
  15481. }
  15482. // Client information updates
  15483. switch (bl->type) {
  15484. case BL_HOM:
  15485. clif_send_homdata(hd->master, SP_ACK, 0);
  15486. break;
  15487. case BL_PET:
  15488. clif_send_petstatus(pd->master);
  15489. break;
  15490. case BL_MER:
  15491. clif_mercenary_info(map_charid2sd(mc->mercenary.char_id));
  15492. clif_mercenary_skillblock(map_charid2sd(mc->mercenary.char_id));
  15493. break;
  15494. case BL_ELEM:
  15495. clif_elemental_info(ed->master);
  15496. break;
  15497. }
  15498. return SCRIPT_CMD_SUCCESS;
  15499. }
  15500. /// Gets the name of a bl.
  15501. /// Supported types are [MOB|HOM|PET|NPC].
  15502. /// MER and ELEM don't support custom names.
  15503. ///
  15504. /// getunitname <unit id>;
  15505. BUILDIN_FUNC(getunitname)
  15506. {
  15507. struct block_list* bl = NULL;
  15508. if(!script_rid2bl(2,bl)){
  15509. script_pushconststr(st, "Unknown");
  15510. return SCRIPT_CMD_FAILURE;
  15511. }
  15512. script_pushstrcopy(st, status_get_name(bl));
  15513. return SCRIPT_CMD_SUCCESS;
  15514. }
  15515. /// Changes the name of a bl.
  15516. /// Supported types are [MOB|HOM|PET].
  15517. /// For NPC see 'setnpcdisplay', MER and ELEM don't support custom names.
  15518. ///
  15519. /// setunitname <unit id>,<name>;
  15520. BUILDIN_FUNC(setunitname)
  15521. {
  15522. struct block_list* bl = NULL;
  15523. TBL_MOB* md = NULL;
  15524. TBL_HOM* hd = NULL;
  15525. TBL_PET* pd = NULL;
  15526. if(!script_rid2bl(2,bl))
  15527. {
  15528. script_pushconststr(st, "Unknown");
  15529. return SCRIPT_CMD_FAILURE;
  15530. }
  15531. switch (bl->type) {
  15532. case BL_MOB: md = map_id2md(bl->id); break;
  15533. case BL_HOM: hd = map_id2hd(bl->id); break;
  15534. case BL_PET: pd = map_id2pd(bl->id); break;
  15535. }
  15536. switch (bl->type) {
  15537. case BL_MOB:
  15538. if (!md) {
  15539. ShowWarning("buildin_setunitname: Error in finding object BL_MOB!\n");
  15540. return SCRIPT_CMD_FAILURE;
  15541. }
  15542. safestrncpy(md->name, script_getstr(st, 3), NAME_LENGTH);
  15543. break;
  15544. case BL_HOM:
  15545. if (!hd) {
  15546. ShowWarning("buildin_setunitname: Error in finding object BL_HOM!\n");
  15547. return SCRIPT_CMD_FAILURE;
  15548. }
  15549. safestrncpy(hd->homunculus.name, script_getstr(st, 3), NAME_LENGTH);
  15550. break;
  15551. case BL_PET:
  15552. if (!pd) {
  15553. ShowWarning("buildin_setunitname: Error in finding object BL_PET!\n");
  15554. return SCRIPT_CMD_FAILURE;
  15555. }
  15556. safestrncpy(pd->pet.name, script_getstr(st, 3), NAME_LENGTH);
  15557. break;
  15558. default:
  15559. ShowWarning("buildin_setunitname: Unknown object type!\n");
  15560. return SCRIPT_CMD_FAILURE;
  15561. }
  15562. clif_name_area(bl); // Send update to client.
  15563. return SCRIPT_CMD_SUCCESS;
  15564. }
  15565. /// Makes the unit walk to target position or map.
  15566. /// Returns if it was successful.
  15567. ///
  15568. /// unitwalk(<unit_id>,<x>,<y>{,<event_label>}) -> <bool>
  15569. /// unitwalkto(<unit_id>,<target_id>{,<event_label>}) -> <bool>
  15570. BUILDIN_FUNC(unitwalk)
  15571. {
  15572. struct block_list* bl;
  15573. struct unit_data *ud = NULL;
  15574. const char *cmd = script_getfuncname(st), *done_label = "";
  15575. uint8 off = 5;
  15576. if(!script_rid2bl(2,bl))
  15577. {
  15578. script_pushint(st, 0);
  15579. return SCRIPT_CMD_FAILURE;
  15580. }
  15581. ud = unit_bl2ud(bl);
  15582. if (!strcmp(cmd,"unitwalk")) {
  15583. int x = script_getnum(st,3);
  15584. int y = script_getnum(st,4);
  15585. if (script_pushint(st, unit_can_reach_pos(bl,x,y,0)))
  15586. add_timer(gettick()+50, unit_delay_walktoxy_timer, bl->id, (x<<16)|(y&0xFFFF)); // Need timer to avoid mismatches
  15587. } else {
  15588. struct block_list* tbl = map_id2bl(script_getnum(st,3));
  15589. if (!tbl) {
  15590. ShowError("buildin_unitwalk: Bad target destination.\n");
  15591. script_pushint(st, 0);
  15592. return SCRIPT_CMD_FAILURE;
  15593. } else if (script_pushint(st, unit_can_reach_bl(bl, tbl, distance_bl(bl, tbl)+1, 0, NULL, NULL)))
  15594. add_timer(gettick()+50, unit_delay_walktobl_timer, bl->id, tbl->id); // Need timer to avoid mismatches
  15595. off = 4;
  15596. }
  15597. if (ud && script_hasdata(st, off)) {
  15598. done_label = script_getstr(st, off);
  15599. check_event(st, done_label);
  15600. safestrncpy(ud->walk_done_event, done_label, sizeof(ud->walk_done_event));
  15601. }
  15602. return SCRIPT_CMD_SUCCESS;
  15603. }
  15604. /// Kills the unit.
  15605. ///
  15606. /// unitkill <unit_id>;
  15607. BUILDIN_FUNC(unitkill)
  15608. {
  15609. struct block_list* bl;
  15610. if(script_rid2bl(2,bl))
  15611. status_kill(bl);
  15612. return SCRIPT_CMD_SUCCESS;
  15613. }
  15614. /// Warps the unit to the target position in the target map.
  15615. /// Returns if it was successful.
  15616. ///
  15617. /// unitwarp(<unit_id>,"<map name>",<x>,<y>) -> <bool>
  15618. BUILDIN_FUNC(unitwarp)
  15619. {
  15620. int map_idx;
  15621. short x;
  15622. short y;
  15623. struct block_list* bl;
  15624. const char *mapname;
  15625. mapname = script_getstr(st, 3);
  15626. x = (short)script_getnum(st,4);
  15627. y = (short)script_getnum(st,5);
  15628. if(!script_rid2bl(2,bl))
  15629. {
  15630. script_pushint(st, 0);
  15631. return SCRIPT_CMD_SUCCESS;
  15632. }
  15633. if (!strcmp(mapname,"this"))
  15634. map_idx = bl?bl->m:-1;
  15635. else
  15636. map_idx = map_mapname2mapid(mapname);
  15637. if (map_idx >= 0 && bl != NULL)
  15638. script_pushint(st, unit_warp(bl,map_idx,x,y,CLR_OUTSIGHT));
  15639. else
  15640. script_pushint(st, 0);
  15641. return SCRIPT_CMD_SUCCESS;
  15642. }
  15643. /// Makes the unit attack the target.
  15644. /// If the unit is a player and <action type> is not 0, it does a continuous
  15645. /// attack instead of a single attack.
  15646. /// Returns if the request was successful.
  15647. ///
  15648. /// unitattack(<unit_id>,"<target name>"{,<action type>}) -> <bool>
  15649. /// unitattack(<unit_id>,<target_id>{,<action type>}) -> <bool>
  15650. BUILDIN_FUNC(unitattack)
  15651. {
  15652. struct block_list* unit_bl;
  15653. struct block_list* target_bl = NULL;
  15654. struct script_data* data;
  15655. int actiontype = 0;
  15656. if (!script_rid2bl(2,unit_bl)) {
  15657. script_pushint(st, 0);
  15658. return SCRIPT_CMD_FAILURE;
  15659. }
  15660. data = script_getdata(st, 3);
  15661. get_val(st, data);
  15662. if (data_isstring(data)) {
  15663. TBL_PC* sd = map_nick2sd(conv_str(st, data));
  15664. if( sd != NULL )
  15665. target_bl = &sd->bl;
  15666. } else
  15667. target_bl = map_id2bl(conv_num(st, data));
  15668. if (!target_bl) {
  15669. script_pushint(st, 0);
  15670. return SCRIPT_CMD_FAILURE;
  15671. }
  15672. if (script_hasdata(st,4))
  15673. actiontype = script_getnum(st,4);
  15674. switch(unit_bl->type) {
  15675. case BL_PC:
  15676. clif_parse_ActionRequest_sub(((TBL_PC *)unit_bl), actiontype > 0 ? 0x07 : 0x00, target_bl->id, gettick());
  15677. script_pushint(st, 1);
  15678. return SCRIPT_CMD_SUCCESS;
  15679. case BL_MOB:
  15680. ((TBL_MOB *)unit_bl)->target_id = target_bl->id;
  15681. break;
  15682. case BL_PET:
  15683. ((TBL_PET *)unit_bl)->target_id = target_bl->id;
  15684. break;
  15685. default:
  15686. ShowError("buildin_unitattack: Unsupported source unit type %d.\n", unit_bl->type);
  15687. script_pushint(st, 0);
  15688. return SCRIPT_CMD_FAILURE;
  15689. }
  15690. script_pushint(st, unit_walktobl(unit_bl, target_bl, 65025, 2));
  15691. return SCRIPT_CMD_SUCCESS;
  15692. }
  15693. /// Makes the unit stop attacking.
  15694. ///
  15695. /// unitstopattack <unit_id>;
  15696. BUILDIN_FUNC(unitstopattack)
  15697. {
  15698. struct block_list* bl;
  15699. if(script_rid2bl(2,bl))
  15700. {
  15701. unit_stop_attack(bl);
  15702. if (bl->type == BL_MOB)
  15703. ((TBL_MOB*)bl)->target_id = 0;
  15704. }
  15705. return SCRIPT_CMD_SUCCESS;
  15706. }
  15707. /// Makes the unit stop walking.
  15708. ///
  15709. /// unitstopwalk <unit_id>;
  15710. BUILDIN_FUNC(unitstopwalk)
  15711. {
  15712. struct block_list* bl;
  15713. if(script_rid2bl(2,bl))
  15714. unit_stop_walking(bl, 0);
  15715. return SCRIPT_CMD_SUCCESS;
  15716. }
  15717. /// Makes the unit say the given message.
  15718. ///
  15719. /// unittalk <unit_id>,"<message>";
  15720. BUILDIN_FUNC(unittalk)
  15721. {
  15722. const char* message;
  15723. struct block_list* bl;
  15724. message = script_getstr(st, 3);
  15725. if(script_rid2bl(2,bl))
  15726. {
  15727. struct StringBuf sbuf;
  15728. StringBuf_Init(&sbuf);
  15729. StringBuf_Printf(&sbuf, "%s", message);
  15730. clif_disp_overhead(bl, StringBuf_Value(&sbuf));
  15731. StringBuf_Destroy(&sbuf);
  15732. }
  15733. return SCRIPT_CMD_SUCCESS;
  15734. }
  15735. /// Makes the unit do an emotion.
  15736. ///
  15737. /// unitemote <unit_id>,<emotion>;
  15738. ///
  15739. /// @see e_* in db/const.txt
  15740. BUILDIN_FUNC(unitemote)
  15741. {
  15742. int emotion;
  15743. struct block_list* bl;
  15744. emotion = script_getnum(st,3);
  15745. if(script_rid2bl(2,bl))
  15746. clif_emotion(bl, emotion);
  15747. return SCRIPT_CMD_SUCCESS;
  15748. }
  15749. /// Makes the unit cast the skill on the target or self if no target is specified.
  15750. ///
  15751. /// unitskilluseid <unit_id>,<skill_id>,<skill_lv>{,<target_id>,<casttime>};
  15752. /// unitskilluseid <unit_id>,"<skill name>",<skill_lv>{,<target_id>,<casttime>};
  15753. BUILDIN_FUNC(unitskilluseid)
  15754. {
  15755. int unit_id, target_id, casttime;
  15756. uint16 skill_id, skill_lv;
  15757. struct block_list* bl;
  15758. struct script_data *data;
  15759. unit_id = script_getnum(st,2);
  15760. data = script_getdata(st, 3);
  15761. get_val(st, data); // Convert into value in case of a variable
  15762. skill_id = ( data_isstring(data) ? skill_name2id(script_getstr(st,3)) : script_getnum(st,3) );
  15763. skill_lv = script_getnum(st,4);
  15764. target_id = ( script_hasdata(st,5) ? script_getnum(st,5) : unit_id );
  15765. casttime = ( script_hasdata(st,6) ? script_getnum(st,6) : 0 );
  15766. if(script_rid2bl(2,bl)){
  15767. if (bl->type == BL_NPC) {
  15768. if (!((TBL_NPC*)bl)->status.hp)
  15769. status_calc_npc(((TBL_NPC*)bl), SCO_FIRST);
  15770. else
  15771. status_calc_npc(((TBL_NPC*)bl), SCO_NONE);
  15772. }
  15773. unit_skilluse_id2(bl, target_id, skill_id, skill_lv, (casttime * 1000) + skill_castfix(bl, skill_id, skill_lv), skill_get_castcancel(skill_id));
  15774. }
  15775. return SCRIPT_CMD_SUCCESS;
  15776. }
  15777. /// Makes the unit cast the skill on the target position.
  15778. ///
  15779. /// unitskillusepos <unit_id>,<skill_id>,<skill_lv>,<target_x>,<target_y>{,<casttime>};
  15780. /// unitskillusepos <unit_id>,"<skill name>",<skill_lv>,<target_x>,<target_y>{,<casttime>};
  15781. BUILDIN_FUNC(unitskillusepos)
  15782. {
  15783. int skill_x, skill_y, casttime;
  15784. uint16 skill_id, skill_lv;
  15785. struct block_list* bl;
  15786. struct script_data *data;
  15787. data = script_getdata(st, 3);
  15788. get_val(st, data); // Convert into value in case of a variable
  15789. skill_id = ( data_isstring(data) ? skill_name2id(script_getstr(st,3)) : script_getnum(st,3) );
  15790. skill_lv = script_getnum(st,4);
  15791. skill_x = script_getnum(st,5);
  15792. skill_y = script_getnum(st,6);
  15793. casttime = ( script_hasdata(st,7) ? script_getnum(st,7) : 0 );
  15794. if(script_rid2bl(2,bl)){
  15795. if (bl->type == BL_NPC) {
  15796. if (!((TBL_NPC*)bl)->status.hp)
  15797. status_calc_npc(((TBL_NPC*)bl), SCO_FIRST);
  15798. else
  15799. status_calc_npc(((TBL_NPC*)bl), SCO_NONE);
  15800. }
  15801. 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));
  15802. }
  15803. return SCRIPT_CMD_SUCCESS;
  15804. }
  15805. // <--- [zBuffer] List of unit control commands
  15806. /// Pauses the execution of the script, detaching the player
  15807. ///
  15808. /// sleep <mili seconds>;
  15809. BUILDIN_FUNC(sleep)
  15810. {
  15811. int ticks;
  15812. ticks = script_getnum(st,2);
  15813. // detach the player
  15814. script_detach_rid(st);
  15815. if( ticks <= 0 )
  15816. {// do nothing
  15817. }
  15818. else if( st->sleep.tick == 0 )
  15819. {// sleep for the target amount of time
  15820. st->state = RERUNLINE;
  15821. st->sleep.tick = ticks;
  15822. }
  15823. else
  15824. {// sleep time is over
  15825. st->state = RUN;
  15826. st->sleep.tick = 0;
  15827. }
  15828. return SCRIPT_CMD_SUCCESS;
  15829. }
  15830. /// Pauses the execution of the script, keeping the unit attached
  15831. /// Returns if the unit is still attached
  15832. ///
  15833. /// sleep2(<mili secconds>) -> <bool>
  15834. BUILDIN_FUNC(sleep2)
  15835. {
  15836. int ticks;
  15837. ticks = script_getnum(st,2);
  15838. if( ticks <= 0 )
  15839. {// do nothing
  15840. script_pushint(st, (map_id2bl(st->rid)!=NULL));
  15841. }
  15842. else if( !st->sleep.tick )
  15843. {// sleep for the target amount of time
  15844. st->state = RERUNLINE;
  15845. st->sleep.tick = ticks;
  15846. }
  15847. else
  15848. {// sleep time is over
  15849. st->state = RUN;
  15850. st->sleep.tick = 0;
  15851. script_pushint(st, (map_id2bl(st->rid)!=NULL));
  15852. }
  15853. return SCRIPT_CMD_SUCCESS;
  15854. }
  15855. /// Awakes all the sleep timers of the target npc
  15856. ///
  15857. /// awake "<npc name>";
  15858. BUILDIN_FUNC(awake)
  15859. {
  15860. DBIterator *iter;
  15861. struct script_state *tst;
  15862. struct npc_data* nd;
  15863. if ((nd = npc_name2id(script_getstr(st, 2))) == NULL) {
  15864. ShowError("buildin_awake: NPC \"%s\" not found\n", script_getstr(st, 2));
  15865. return SCRIPT_CMD_FAILURE;
  15866. }
  15867. iter = db_iterator(st_db);
  15868. for (tst = dbi_first(iter); dbi_exists(iter); tst = dbi_next(iter)) {
  15869. if (tst->oid == nd->bl.id) {
  15870. if (tst->sleep.timer == INVALID_TIMER) { // already awake ???
  15871. continue;
  15872. }
  15873. delete_timer(tst->sleep.timer, run_script_timer);
  15874. // Trigger the timer function
  15875. run_script_timer(INVALID_TIMER, gettick(), tst->sleep.charid, (intptr_t)tst);
  15876. }
  15877. }
  15878. dbi_destroy(iter);
  15879. return SCRIPT_CMD_SUCCESS;
  15880. }
  15881. /// Returns a reference to a variable of the target NPC.
  15882. /// Returns 0 if an error occurs.
  15883. ///
  15884. /// getvariableofnpc(<variable>, "<npc name>") -> <reference>
  15885. BUILDIN_FUNC(getvariableofnpc)
  15886. {
  15887. struct script_data* data;
  15888. const char* name;
  15889. struct npc_data* nd;
  15890. data = script_getdata(st,2);
  15891. if( !data_isreference(data) )
  15892. {// Not a reference (aka varaible name)
  15893. ShowError("buildin_getvariableofnpc: not a variable\n");
  15894. script_reportdata(data);
  15895. script_pushnil(st);
  15896. st->state = END;
  15897. return SCRIPT_CMD_FAILURE;
  15898. }
  15899. name = reference_getname(data);
  15900. if( *name != '.' || name[1] == '@' )
  15901. {// not a npc variable
  15902. ShowError("buildin_getvariableofnpc: invalid scope (not npc variable)\n");
  15903. script_reportdata(data);
  15904. script_pushnil(st);
  15905. st->state = END;
  15906. return SCRIPT_CMD_FAILURE;
  15907. }
  15908. nd = npc_name2id(script_getstr(st,3));
  15909. if( nd == NULL || nd->subtype != NPCTYPE_SCRIPT || nd->u.scr.script == NULL )
  15910. {// NPC not found or has no script
  15911. ShowError("buildin_getvariableofnpc: can't find npc %s\n", script_getstr(st,3));
  15912. script_pushnil(st);
  15913. st->state = END;
  15914. return SCRIPT_CMD_FAILURE;
  15915. }
  15916. if (!nd->u.scr.script->local.vars)
  15917. nd->u.scr.script->local.vars = i64db_alloc(DB_OPT_RELEASE_DATA);
  15918. push_val2(st->stack, C_NAME, reference_getuid(data), &nd->u.scr.script->local);
  15919. return SCRIPT_CMD_SUCCESS;
  15920. }
  15921. /// Opens a warp portal.
  15922. /// Has no "portal opening" effect/sound, it opens the portal immediately.
  15923. ///
  15924. /// warpportal <source x>,<source y>,"<target map>",<target x>,<target y>;
  15925. ///
  15926. /// @author blackhole89
  15927. BUILDIN_FUNC(warpportal)
  15928. {
  15929. int spx;
  15930. int spy;
  15931. unsigned short mapindex;
  15932. int tpx;
  15933. int tpy;
  15934. struct skill_unit_group* group;
  15935. struct block_list* bl;
  15936. bl = map_id2bl(st->oid);
  15937. if( bl == NULL ) {
  15938. ShowError("buildin_warpportal: NPC is needed\n");
  15939. return SCRIPT_CMD_FAILURE;
  15940. }
  15941. spx = script_getnum(st,2);
  15942. spy = script_getnum(st,3);
  15943. mapindex = mapindex_name2id(script_getstr(st, 4));
  15944. tpx = script_getnum(st,5);
  15945. tpy = script_getnum(st,6);
  15946. if( mapindex == 0 ) {
  15947. ShowError("buildin_warpportal: Target map not found %s.\n", script_getstr(st, 4));
  15948. return SCRIPT_CMD_FAILURE;
  15949. }
  15950. group = skill_unitsetting(bl, AL_WARP, 4, spx, spy, 0);
  15951. if( group == NULL )
  15952. return SCRIPT_CMD_FAILURE;// failed
  15953. group->val1 = (group->val1<<16)|(short)0;
  15954. group->val2 = (tpx<<16) | tpy;
  15955. group->val3 = mapindex;
  15956. return SCRIPT_CMD_SUCCESS;
  15957. }
  15958. /**
  15959. * openmail({<char_id>});
  15960. **/
  15961. BUILDIN_FUNC(openmail)
  15962. {
  15963. TBL_PC* sd;
  15964. if (!script_charid2sd(2,sd))
  15965. return SCRIPT_CMD_FAILURE;
  15966. mail_openmail(sd);
  15967. return SCRIPT_CMD_SUCCESS;
  15968. }
  15969. /**
  15970. * openauction({<char_id>});
  15971. **/
  15972. BUILDIN_FUNC(openauction)
  15973. {
  15974. TBL_PC* sd;
  15975. if (!script_charid2sd(2,sd))
  15976. return SCRIPT_CMD_FAILURE;
  15977. if( !battle_config.feature_auction ) {
  15978. clif_colormes(sd->fd, color_table[COLOR_RED], msg_txt(sd, 517));
  15979. return SCRIPT_CMD_SUCCESS;
  15980. }
  15981. clif_Auction_openwindow(sd);
  15982. return SCRIPT_CMD_SUCCESS;
  15983. }
  15984. /// Retrieves the value of the specified flag of the specified cell.
  15985. ///
  15986. /// checkcell("<map name>",<x>,<y>,<type>) -> <bool>
  15987. ///
  15988. /// @see cell_chk* constants in const.txt for the types
  15989. BUILDIN_FUNC(checkcell)
  15990. {
  15991. int16 m = map_mapname2mapid(script_getstr(st,2));
  15992. int16 x = script_getnum(st,3);
  15993. int16 y = script_getnum(st,4);
  15994. cell_chk type = (cell_chk)script_getnum(st,5);
  15995. script_pushint(st, map_getcell(m, x, y, type));
  15996. return SCRIPT_CMD_SUCCESS;
  15997. }
  15998. /// Modifies flags of cells in the specified area.
  15999. ///
  16000. /// setcell "<map name>",<x1>,<y1>,<x2>,<y2>,<type>,<flag>;
  16001. ///
  16002. /// @see cell_* constants in const.txt for the types
  16003. BUILDIN_FUNC(setcell)
  16004. {
  16005. int16 m = map_mapname2mapid(script_getstr(st,2));
  16006. int16 x1 = script_getnum(st,3);
  16007. int16 y1 = script_getnum(st,4);
  16008. int16 x2 = script_getnum(st,5);
  16009. int16 y2 = script_getnum(st,6);
  16010. cell_t type = (cell_t)script_getnum(st,7);
  16011. bool flag = (bool)script_getnum(st,8);
  16012. int x,y;
  16013. if( x1 > x2 ) swap(x1,x2);
  16014. if( y1 > y2 ) swap(y1,y2);
  16015. for( y = y1; y <= y2; ++y )
  16016. for( x = x1; x <= x2; ++x )
  16017. map_setcell(m, x, y, type, flag);
  16018. return SCRIPT_CMD_SUCCESS;
  16019. }
  16020. /*==========================================
  16021. * Mercenary Commands
  16022. *------------------------------------------*/
  16023. BUILDIN_FUNC(mercenary_create)
  16024. {
  16025. struct map_session_data *sd;
  16026. int class_, contract_time;
  16027. if( (sd = script_rid2sd(st)) == NULL || sd->md || sd->status.mer_id != 0 )
  16028. return SCRIPT_CMD_SUCCESS;
  16029. class_ = script_getnum(st,2);
  16030. if( !mercenary_class(class_) )
  16031. return SCRIPT_CMD_SUCCESS;
  16032. contract_time = script_getnum(st,3);
  16033. mercenary_create(sd, class_, contract_time);
  16034. return SCRIPT_CMD_SUCCESS;
  16035. }
  16036. BUILDIN_FUNC(mercenary_heal)
  16037. {
  16038. struct map_session_data *sd = script_rid2sd(st);
  16039. int hp, sp;
  16040. if( sd == NULL || sd->md == NULL )
  16041. return SCRIPT_CMD_SUCCESS;
  16042. hp = script_getnum(st,2);
  16043. sp = script_getnum(st,3);
  16044. status_heal(&sd->md->bl, hp, sp, 0);
  16045. return SCRIPT_CMD_SUCCESS;
  16046. }
  16047. BUILDIN_FUNC(mercenary_sc_start)
  16048. {
  16049. struct map_session_data *sd = script_rid2sd(st);
  16050. enum sc_type type;
  16051. int tick, val1;
  16052. if( sd == NULL || sd->md == NULL )
  16053. return SCRIPT_CMD_SUCCESS;
  16054. type = (sc_type)script_getnum(st,2);
  16055. tick = script_getnum(st,3);
  16056. val1 = script_getnum(st,4);
  16057. status_change_start(NULL, &sd->md->bl, type, 10000, val1, 0, 0, 0, tick, SCSTART_NOTICKDEF);
  16058. return SCRIPT_CMD_SUCCESS;
  16059. }
  16060. BUILDIN_FUNC(mercenary_get_calls)
  16061. {
  16062. struct map_session_data *sd = script_rid2sd(st);
  16063. int guild;
  16064. if( sd == NULL )
  16065. return SCRIPT_CMD_SUCCESS;
  16066. guild = script_getnum(st,2);
  16067. switch( guild )
  16068. {
  16069. case ARCH_MERC_GUILD:
  16070. script_pushint(st,sd->status.arch_calls);
  16071. break;
  16072. case SPEAR_MERC_GUILD:
  16073. script_pushint(st,sd->status.spear_calls);
  16074. break;
  16075. case SWORD_MERC_GUILD:
  16076. script_pushint(st,sd->status.sword_calls);
  16077. break;
  16078. default:
  16079. script_pushint(st,0);
  16080. break;
  16081. }
  16082. return SCRIPT_CMD_SUCCESS;
  16083. }
  16084. BUILDIN_FUNC(mercenary_set_calls)
  16085. {
  16086. struct map_session_data *sd = script_rid2sd(st);
  16087. int guild, value, *calls;
  16088. if( sd == NULL )
  16089. return SCRIPT_CMD_SUCCESS;
  16090. guild = script_getnum(st,2);
  16091. value = script_getnum(st,3);
  16092. switch( guild )
  16093. {
  16094. case ARCH_MERC_GUILD:
  16095. calls = &sd->status.arch_calls;
  16096. break;
  16097. case SPEAR_MERC_GUILD:
  16098. calls = &sd->status.spear_calls;
  16099. break;
  16100. case SWORD_MERC_GUILD:
  16101. calls = &sd->status.sword_calls;
  16102. break;
  16103. default:
  16104. ShowError("buildin_mercenary_set_calls: Invalid guild '%d'.\n", guild);
  16105. return SCRIPT_CMD_SUCCESS; // Invalid Guild
  16106. }
  16107. *calls += value;
  16108. *calls = cap_value(*calls, 0, INT_MAX);
  16109. return SCRIPT_CMD_SUCCESS;
  16110. }
  16111. BUILDIN_FUNC(mercenary_get_faith)
  16112. {
  16113. struct map_session_data *sd = script_rid2sd(st);
  16114. int guild;
  16115. if( sd == NULL )
  16116. return SCRIPT_CMD_SUCCESS;
  16117. guild = script_getnum(st,2);
  16118. switch( guild )
  16119. {
  16120. case ARCH_MERC_GUILD:
  16121. script_pushint(st,sd->status.arch_faith);
  16122. break;
  16123. case SPEAR_MERC_GUILD:
  16124. script_pushint(st,sd->status.spear_faith);
  16125. break;
  16126. case SWORD_MERC_GUILD:
  16127. script_pushint(st,sd->status.sword_faith);
  16128. break;
  16129. default:
  16130. script_pushint(st,0);
  16131. break;
  16132. }
  16133. return SCRIPT_CMD_SUCCESS;
  16134. }
  16135. BUILDIN_FUNC(mercenary_set_faith)
  16136. {
  16137. struct map_session_data *sd = script_rid2sd(st);
  16138. int guild, value, *calls;
  16139. if( sd == NULL )
  16140. return SCRIPT_CMD_SUCCESS;
  16141. guild = script_getnum(st,2);
  16142. value = script_getnum(st,3);
  16143. switch( guild )
  16144. {
  16145. case ARCH_MERC_GUILD:
  16146. calls = &sd->status.arch_faith;
  16147. break;
  16148. case SPEAR_MERC_GUILD:
  16149. calls = &sd->status.spear_faith;
  16150. break;
  16151. case SWORD_MERC_GUILD:
  16152. calls = &sd->status.sword_faith;
  16153. break;
  16154. default:
  16155. ShowError("buildin_mercenary_set_faith: Invalid guild '%d'.\n", guild);
  16156. return SCRIPT_CMD_SUCCESS; // Invalid Guild
  16157. }
  16158. *calls += value;
  16159. *calls = cap_value(*calls, 0, INT_MAX);
  16160. if( mercenary_get_guild(sd->md) == guild )
  16161. clif_mercenary_updatestatus(sd,SP_MERCFAITH);
  16162. return SCRIPT_CMD_SUCCESS;
  16163. }
  16164. /*------------------------------------------
  16165. * Book Reading
  16166. *------------------------------------------*/
  16167. BUILDIN_FUNC(readbook)
  16168. {
  16169. struct map_session_data *sd;
  16170. int book_id, page;
  16171. if( (sd = script_rid2sd(st)) == NULL )
  16172. return SCRIPT_CMD_SUCCESS;
  16173. book_id = script_getnum(st,2);
  16174. page = script_getnum(st,3);
  16175. clif_readbook(sd->fd, book_id, page);
  16176. return SCRIPT_CMD_SUCCESS;
  16177. }
  16178. /******************
  16179. Questlog script commands
  16180. *******************/
  16181. /**
  16182. * Add job criteria to questinfo
  16183. * @param qi Quest Info
  16184. * @param job
  16185. * @author [Cydh]
  16186. **/
  16187. static void buildin_questinfo_setjob(struct questinfo *qi, int job) {
  16188. RECREATE(qi->jobid, unsigned short, qi->jobid_count+1);
  16189. qi->jobid[qi->jobid_count++] = job;
  16190. }
  16191. /**
  16192. * questinfo <Quest ID>,<Icon>{,<Map Mark Color>{,<Job Class>}};
  16193. **/
  16194. BUILDIN_FUNC(questinfo)
  16195. {
  16196. TBL_NPC* nd = map_id2nd(st->oid);
  16197. int quest_id, icon;
  16198. struct questinfo qi, *q2;
  16199. if( nd == NULL || nd->bl.m == -1 ) {
  16200. ShowError("buildin_questinfo: No NPC attached.\n");
  16201. return SCRIPT_CMD_FAILURE;
  16202. }
  16203. quest_id = script_getnum(st, 2);
  16204. icon = script_getnum(st, 3);
  16205. #if PACKETVER >= 20120410
  16206. switch(icon){
  16207. case QTYPE_QUEST:
  16208. case QTYPE_QUEST2:
  16209. case QTYPE_JOB:
  16210. case QTYPE_JOB2:
  16211. case QTYPE_EVENT:
  16212. case QTYPE_EVENT2:
  16213. case QTYPE_WARG:
  16214. case QTYPE_WARG2:
  16215. // Leave everything as it is
  16216. break;
  16217. case QTYPE_NONE:
  16218. default:
  16219. // Default to nothing if icon id is invalid.
  16220. icon = QTYPE_NONE;
  16221. break;
  16222. }
  16223. #else
  16224. if(icon < QTYPE_QUEST || icon > 7) // TODO: check why 7 and not QTYPE_WARG, might be related to icon + 1 below
  16225. icon = QTYPE_QUEST;
  16226. else
  16227. icon = icon + 1;
  16228. #endif
  16229. qi.quest_id = quest_id;
  16230. qi.icon = (unsigned char)icon;
  16231. qi.nd = nd;
  16232. if( script_hasdata(st, 4) ) {
  16233. int color = script_getnum(st, 4);
  16234. if( color < 0 || color > 3 ) {
  16235. ShowWarning("buildin_questinfo: invalid color '%d', changing to 0\n",color);
  16236. script_reportfunc(st);
  16237. color = 0;
  16238. }
  16239. qi.color = (unsigned char)color;
  16240. }
  16241. qi.min_level = 1;
  16242. qi.max_level = MAX_LEVEL;
  16243. q2 = map_add_questinfo(nd->bl.m, &qi);
  16244. q2->req = NULL;
  16245. q2->req_count = 0;
  16246. q2->jobid = NULL;
  16247. q2->jobid_count = 0;
  16248. if(script_hasdata(st, 5)) {
  16249. int job = script_getnum(st, 5);
  16250. if (!pcdb_checkid(job))
  16251. ShowError("buildin_questinfo: Nonexistant Job Class.\n");
  16252. else {
  16253. buildin_questinfo_setjob(q2, job);
  16254. }
  16255. }
  16256. return SCRIPT_CMD_SUCCESS;
  16257. }
  16258. /**
  16259. * setquest <ID>{,<char_id>};
  16260. **/
  16261. BUILDIN_FUNC(setquest)
  16262. {
  16263. struct map_session_data *sd;
  16264. int quest_id;
  16265. quest_id = script_getnum(st, 2);
  16266. if (!script_charid2sd(3,sd))
  16267. return SCRIPT_CMD_FAILURE;
  16268. if( quest_add(sd, quest_id) == -1 ){
  16269. script_reportsrc(st);
  16270. script_reportfunc(st);
  16271. }
  16272. //20120410 or 20090218 ? no reason that shouldn't work for 2009
  16273. pc_show_questinfo(sd);
  16274. return SCRIPT_CMD_SUCCESS;
  16275. }
  16276. /**
  16277. * erasequest <ID>{,<char_id>};
  16278. **/
  16279. BUILDIN_FUNC(erasequest)
  16280. {
  16281. struct map_session_data *sd;
  16282. if (!script_charid2sd(3,sd))
  16283. return SCRIPT_CMD_FAILURE;
  16284. if( quest_delete(sd, script_getnum(st, 2)) == -1 ){
  16285. script_reportsrc(st);
  16286. script_reportfunc(st);
  16287. }
  16288. return SCRIPT_CMD_SUCCESS;
  16289. }
  16290. /**
  16291. * completequest <ID>{,<char_id>};
  16292. **/
  16293. BUILDIN_FUNC(completequest)
  16294. {
  16295. struct map_session_data *sd;
  16296. if (!script_charid2sd(3,sd))
  16297. return SCRIPT_CMD_FAILURE;
  16298. quest_update_status(sd, script_getnum(st, 2), Q_COMPLETE);
  16299. //20120410 or 20090218
  16300. pc_show_questinfo(sd);
  16301. return SCRIPT_CMD_SUCCESS;
  16302. }
  16303. /**
  16304. * changequest <ID>,<ID2>{,<char_id>};
  16305. **/
  16306. BUILDIN_FUNC(changequest)
  16307. {
  16308. struct map_session_data *sd;
  16309. if (!script_charid2sd(4,sd))
  16310. return SCRIPT_CMD_FAILURE;
  16311. if( quest_change(sd, script_getnum(st, 2),script_getnum(st, 3)) == -1 ){
  16312. script_reportsrc(st);
  16313. script_reportfunc(st);
  16314. }
  16315. //20120410 or 20090218
  16316. pc_show_questinfo(sd);
  16317. return SCRIPT_CMD_SUCCESS;
  16318. }
  16319. /**
  16320. * checkquest(<ID>{,PLAYTIME|HUNTING{,<char_id>}})
  16321. **/
  16322. BUILDIN_FUNC(checkquest)
  16323. {
  16324. struct map_session_data *sd;
  16325. enum quest_check_type type = HAVEQUEST;
  16326. if( script_hasdata(st, 3) )
  16327. type = (enum quest_check_type)script_getnum(st, 3);
  16328. if (!script_charid2sd(4,sd))
  16329. return SCRIPT_CMD_FAILURE;
  16330. script_pushint(st, quest_check(sd, script_getnum(st, 2), type));
  16331. return SCRIPT_CMD_SUCCESS;
  16332. }
  16333. /**
  16334. * isbegin_quest(<ID>{,<char_id>})
  16335. **/
  16336. BUILDIN_FUNC(isbegin_quest)
  16337. {
  16338. struct map_session_data *sd;
  16339. int i;
  16340. if (!script_charid2sd(3,sd))
  16341. return SCRIPT_CMD_FAILURE;
  16342. i = quest_check(sd, script_getnum(st, 2), (enum quest_check_type) HAVEQUEST);
  16343. script_pushint(st, i + (i < 1));
  16344. return SCRIPT_CMD_SUCCESS;
  16345. }
  16346. /**
  16347. * showevent <icon>{,<mark color>{,<char_id>}}
  16348. **/
  16349. BUILDIN_FUNC(showevent)
  16350. {
  16351. TBL_PC *sd;
  16352. struct npc_data *nd = map_id2nd(st->oid);
  16353. int icon, color = 0;
  16354. if (!script_charid2sd(4,sd))
  16355. return SCRIPT_CMD_FAILURE;
  16356. if( sd == NULL || nd == NULL )
  16357. return SCRIPT_CMD_SUCCESS;
  16358. icon = script_getnum(st, 2);
  16359. if( script_hasdata(st, 3) ) {
  16360. color = script_getnum(st, 3);
  16361. if( color < 0 || color > 3 ) {
  16362. ShowWarning("buildin_showevent: invalid color '%d', changing to 0\n",color);
  16363. script_reportfunc(st);
  16364. color = 0;
  16365. }
  16366. }
  16367. #if PACKETVER >= 20120410
  16368. if(icon < 0 || (icon > 8 && icon != 9999) || icon == 7)
  16369. icon = 9999; // Default to nothing if icon id is invalid.
  16370. #else
  16371. if(icon < 0 || icon > 7)
  16372. icon = 0;
  16373. else
  16374. icon = icon + 1;
  16375. #endif
  16376. clif_quest_show_event(sd, &nd->bl, icon, color);
  16377. return SCRIPT_CMD_SUCCESS;
  16378. }
  16379. /*==========================================
  16380. * BattleGround System
  16381. *------------------------------------------*/
  16382. BUILDIN_FUNC(waitingroom2bg)
  16383. {
  16384. struct npc_data *nd;
  16385. struct chat_data *cd;
  16386. const char *map_name, *ev = "", *dev = "";
  16387. int x, y, mapindex = 0, bg_id;
  16388. unsigned char i,c=0;
  16389. if( script_hasdata(st,7) )
  16390. nd = npc_name2id(script_getstr(st,7));
  16391. else
  16392. nd = (struct npc_data *)map_id2bl(st->oid);
  16393. if( nd == NULL || (cd = (struct chat_data *)map_id2bl(nd->chat_id)) == NULL )
  16394. {
  16395. script_pushint(st,0);
  16396. return SCRIPT_CMD_SUCCESS;
  16397. }
  16398. map_name = script_getstr(st,2);
  16399. if (strcmp(map_name, "-") != 0 && (mapindex = mapindex_name2id(map_name)) == 0)
  16400. { // Invalid Map
  16401. script_pushint(st, 0);
  16402. return SCRIPT_CMD_SUCCESS;
  16403. }
  16404. x = script_getnum(st,3);
  16405. y = script_getnum(st,4);
  16406. if(script_hasdata(st,5))
  16407. ev = script_getstr(st,5); // Logout Event
  16408. if(script_hasdata(st,6))
  16409. dev = script_getstr(st,6); // Die Event
  16410. check_event(st, ev);
  16411. check_event(st, dev);
  16412. if( (bg_id = bg_create(mapindex, x, y, ev, dev)) == 0 )
  16413. { // Creation failed
  16414. script_pushint(st,0);
  16415. return SCRIPT_CMD_SUCCESS;
  16416. }
  16417. for (i = 0; i < cd->users; i++) { // Only add those who are in the chat room
  16418. struct map_session_data *sd;
  16419. if( (sd = cd->usersd[i]) != NULL && bg_team_join(bg_id, sd) ){
  16420. mapreg_setreg(reference_uid(add_str("$@arenamembers"), c), sd->bl.id);
  16421. ++c;
  16422. }
  16423. }
  16424. mapreg_setreg(add_str("$@arenamembersnum"), c);
  16425. script_pushint(st,bg_id);
  16426. return SCRIPT_CMD_SUCCESS;
  16427. }
  16428. BUILDIN_FUNC(waitingroom2bg_single)
  16429. {
  16430. const char* map_name;
  16431. struct npc_data *nd;
  16432. struct chat_data *cd;
  16433. struct map_session_data *sd;
  16434. struct battleground_data *bg;
  16435. int x, y, mapindex, bg_id;
  16436. bg_id = script_getnum(st,2);
  16437. if ((bg = bg_team_search(bg_id)) == NULL) {
  16438. script_pushint(st, false);
  16439. return SCRIPT_CMD_SUCCESS;
  16440. }
  16441. if (script_hasdata(st, 3)) {
  16442. map_name = script_getstr(st, 3);
  16443. if ((mapindex = mapindex_name2id(map_name)) == 0) {
  16444. script_pushint(st, false);
  16445. return SCRIPT_CMD_SUCCESS; // Invalid Map
  16446. }
  16447. x = script_getnum(st, 4);
  16448. y = script_getnum(st, 5);
  16449. }
  16450. else {
  16451. mapindex = bg->mapindex;
  16452. x = bg->x;
  16453. y = bg->y;
  16454. }
  16455. nd = npc_name2id(script_getstr(st,6));
  16456. if( nd == NULL || (cd = (struct chat_data *)map_id2bl(nd->chat_id)) == NULL || cd->users <= 0 )
  16457. return SCRIPT_CMD_SUCCESS;
  16458. if( (sd = cd->usersd[0]) == NULL )
  16459. return SCRIPT_CMD_SUCCESS;
  16460. if( bg_team_join(bg_id, sd) && pc_setpos(sd, mapindex, x, y, CLR_TELEPORT) == SETPOS_OK)
  16461. {
  16462. script_pushint(st, true);
  16463. }
  16464. else
  16465. script_pushint(st, false);
  16466. return SCRIPT_CMD_SUCCESS;
  16467. }
  16468. /// Creates an instance of battleground battle group.
  16469. /// *bg_create("<map name>",<x>,<y>{,"<On Quit Event>","<On Death Event>"});
  16470. /// @author [secretdataz]
  16471. BUILDIN_FUNC(bg_create) {
  16472. const char *map_name, *ev = "", *dev = "";
  16473. int x, y, mapindex = 0;
  16474. map_name = script_getstr(st, 2);
  16475. if (strcmp(map_name, "-") != 0 && (mapindex = mapindex_name2id(map_name)) == 0)
  16476. { // Invalid Map
  16477. script_pushint(st, 0);
  16478. return SCRIPT_CMD_SUCCESS;
  16479. }
  16480. x = script_getnum(st, 3);
  16481. y = script_getnum(st, 4);
  16482. if(script_hasdata(st, 5))
  16483. ev = script_getstr(st, 5); // Logout Event
  16484. if(script_hasdata(st, 6))
  16485. dev = script_getstr(st, 6); // Die Event
  16486. check_event(st, ev);
  16487. check_event(st, dev);
  16488. script_pushint(st, bg_create(mapindex, x, y, ev, dev));
  16489. return SCRIPT_CMD_SUCCESS;
  16490. }
  16491. /// Adds attached player or <char id> (if specified) to an existing
  16492. /// battleground group and warps it to the specified coordinates on
  16493. /// the given map.
  16494. /// bg_join(<battle group>,{"<map name>",<x>,<y>{,<char id>}});
  16495. /// @author [secretdataz]
  16496. BUILDIN_FUNC(bg_join) {
  16497. const char* map_name;
  16498. struct map_session_data *sd;
  16499. struct battleground_data *bg;
  16500. int x, y, bg_id, mapindex;
  16501. bg_id = script_getnum(st, 2);
  16502. if ((bg = bg_team_search(bg_id)) == NULL) {
  16503. script_pushint(st, false);
  16504. return SCRIPT_CMD_SUCCESS;
  16505. }
  16506. if (script_hasdata(st, 3)) {
  16507. map_name = script_getstr(st, 3);
  16508. if ((mapindex = mapindex_name2id(map_name)) == 0) {
  16509. script_pushint(st, false);
  16510. return SCRIPT_CMD_SUCCESS; // Invalid Map
  16511. }
  16512. x = script_getnum(st, 4);
  16513. y = script_getnum(st, 5);
  16514. } else {
  16515. mapindex = bg->mapindex;
  16516. x = bg->x;
  16517. y = bg->y;
  16518. }
  16519. if (!script_charid2sd(6, sd)) {
  16520. script_pushint(st, false);
  16521. return SCRIPT_CMD_FAILURE;
  16522. }
  16523. if (bg_team_join(bg_id, sd) && pc_setpos(sd, mapindex, x, y, CLR_TELEPORT) == SETPOS_OK)
  16524. {
  16525. script_pushint(st, true);
  16526. }
  16527. else
  16528. script_pushint(st, false);
  16529. return SCRIPT_CMD_SUCCESS;
  16530. }
  16531. BUILDIN_FUNC(bg_team_setxy)
  16532. {
  16533. struct battleground_data *bg;
  16534. int bg_id;
  16535. bg_id = script_getnum(st,2);
  16536. if( (bg = bg_team_search(bg_id)) == NULL )
  16537. return SCRIPT_CMD_SUCCESS;
  16538. bg->x = script_getnum(st,3);
  16539. bg->y = script_getnum(st,4);
  16540. return SCRIPT_CMD_SUCCESS;
  16541. }
  16542. BUILDIN_FUNC(bg_warp)
  16543. {
  16544. int x, y, mapindex, bg_id;
  16545. const char* map_name;
  16546. bg_id = script_getnum(st,2);
  16547. map_name = script_getstr(st,3);
  16548. if( (mapindex = mapindex_name2id(map_name)) == 0 )
  16549. return SCRIPT_CMD_SUCCESS; // Invalid Map
  16550. x = script_getnum(st,4);
  16551. y = script_getnum(st,5);
  16552. bg_team_warp(bg_id, mapindex, x, y);
  16553. return SCRIPT_CMD_SUCCESS;
  16554. }
  16555. BUILDIN_FUNC(bg_monster)
  16556. {
  16557. int class_ = 0, x = 0, y = 0, bg_id = 0;
  16558. const char *str,*mapname, *evt="";
  16559. bg_id = script_getnum(st,2);
  16560. mapname = script_getstr(st,3);
  16561. x = script_getnum(st,4);
  16562. y = script_getnum(st,5);
  16563. str = script_getstr(st,6);
  16564. class_ = script_getnum(st,7);
  16565. if( script_hasdata(st,8) ) evt = script_getstr(st,8);
  16566. check_event(st, evt);
  16567. script_pushint(st, mob_spawn_bg(mapname,x,y,str,class_,evt,bg_id));
  16568. return SCRIPT_CMD_SUCCESS;
  16569. }
  16570. BUILDIN_FUNC(bg_monster_set_team)
  16571. {
  16572. struct mob_data *md;
  16573. struct block_list *mbl;
  16574. int id = script_getnum(st,2),
  16575. bg_id = script_getnum(st,3);
  16576. if( id == 0 || (mbl = map_id2bl(id)) == NULL || mbl->type != BL_MOB )
  16577. return SCRIPT_CMD_SUCCESS;
  16578. md = (TBL_MOB *)mbl;
  16579. md->bg_id = bg_id;
  16580. mob_stop_attack(md);
  16581. mob_stop_walking(md, 0);
  16582. md->target_id = md->attacked_id = 0;
  16583. clif_name_area(&md->bl);
  16584. return SCRIPT_CMD_SUCCESS;
  16585. }
  16586. BUILDIN_FUNC(bg_leave)
  16587. {
  16588. struct map_session_data *sd = NULL;
  16589. if( !script_charid2sd(2,sd) || !sd->bg_id )
  16590. return SCRIPT_CMD_SUCCESS;
  16591. bg_team_leave(sd,0);
  16592. return SCRIPT_CMD_SUCCESS;
  16593. }
  16594. BUILDIN_FUNC(bg_destroy)
  16595. {
  16596. int bg_id = script_getnum(st,2);
  16597. bg_team_delete(bg_id);
  16598. return SCRIPT_CMD_SUCCESS;
  16599. }
  16600. BUILDIN_FUNC(bg_getareausers)
  16601. {
  16602. const char *str;
  16603. int16 m, x0, y0, x1, y1;
  16604. int bg_id;
  16605. int i = 0, c = 0;
  16606. struct battleground_data *bg = NULL;
  16607. bg_id = script_getnum(st,2);
  16608. str = script_getstr(st,3);
  16609. if( (bg = bg_team_search(bg_id)) == NULL || (m = map_mapname2mapid(str)) < 0 )
  16610. {
  16611. script_pushint(st,0);
  16612. return SCRIPT_CMD_SUCCESS;
  16613. }
  16614. x0 = script_getnum(st,4);
  16615. y0 = script_getnum(st,5);
  16616. x1 = script_getnum(st,6);
  16617. y1 = script_getnum(st,7);
  16618. for( i = 0; i < MAX_BG_MEMBERS; i++ )
  16619. {
  16620. struct map_session_data *sd;
  16621. if( (sd = bg->members[i].sd) == NULL )
  16622. continue;
  16623. if( sd->bl.m != m || sd->bl.x < x0 || sd->bl.y < y0 || sd->bl.x > x1 || sd->bl.y > y1 )
  16624. continue;
  16625. c++;
  16626. }
  16627. script_pushint(st,c);
  16628. return SCRIPT_CMD_SUCCESS;
  16629. }
  16630. BUILDIN_FUNC(bg_updatescore)
  16631. {
  16632. const char *str;
  16633. int16 m;
  16634. str = script_getstr(st,2);
  16635. if( (m = map_mapname2mapid(str)) < 0 )
  16636. return SCRIPT_CMD_SUCCESS;
  16637. map[m].bgscore_lion = script_getnum(st,3);
  16638. map[m].bgscore_eagle = script_getnum(st,4);
  16639. clif_bg_updatescore(m);
  16640. return SCRIPT_CMD_SUCCESS;
  16641. }
  16642. BUILDIN_FUNC(bg_get_data)
  16643. {
  16644. struct battleground_data *bg;
  16645. int bg_id = script_getnum(st,2),
  16646. type = script_getnum(st,3);
  16647. if( (bg = bg_team_search(bg_id)) == NULL )
  16648. {
  16649. script_pushint(st,0);
  16650. return SCRIPT_CMD_SUCCESS;
  16651. }
  16652. switch( type )
  16653. {
  16654. case 0: script_pushint(st, bg->count); break;
  16655. default:
  16656. ShowError("script:bg_get_data: unknown data identifier %d\n", type);
  16657. break;
  16658. }
  16659. return SCRIPT_CMD_SUCCESS;
  16660. }
  16661. /*==========================================
  16662. * Instancing System
  16663. *------------------------------------------*/
  16664. //Returns an Instance ID
  16665. //Checks NPC first, then if player is attached we check
  16666. unsigned short script_instancegetid(struct script_state* st)
  16667. {
  16668. unsigned short instance_id = 0;
  16669. struct npc_data *nd;
  16670. if( (nd = map_id2nd(st->oid)) && nd->instance_id > 0 )
  16671. instance_id = nd->instance_id;
  16672. else {
  16673. struct map_session_data *sd = NULL;
  16674. struct party_data *p = NULL;
  16675. struct guild *g = NULL;
  16676. if ((sd = script_rid2sd(st)) != NULL) {
  16677. if (sd->instance_id)
  16678. instance_id = sd->instance_id;
  16679. if (instance_id == 0 && sd->status.party_id && (p = party_search(sd->status.party_id)) != NULL && p->instance_id)
  16680. instance_id = p->instance_id;
  16681. if (instance_id == 0 && sd->status.guild_id && (g = guild_search(sd->status.guild_id)) != NULL && g->instance_id)
  16682. instance_id = g->instance_id;
  16683. }
  16684. }
  16685. return instance_id;
  16686. }
  16687. /*==========================================
  16688. * Creates the instance
  16689. * Returns Instance ID if created successfully
  16690. *------------------------------------------*/
  16691. BUILDIN_FUNC(instance_create)
  16692. {
  16693. enum instance_mode mode = IM_PARTY;
  16694. int owner_id = 0;
  16695. if (script_hasdata(st, 3)) {
  16696. mode = script_getnum(st, 3);
  16697. if (mode < IM_NONE || mode >= IM_MAX) {
  16698. ShowError("buildin_instance_create: Unknown instance mode %d for '%s'\n", mode, script_getstr(st, 2));
  16699. return SCRIPT_CMD_FAILURE;
  16700. }
  16701. }
  16702. if (script_hasdata(st, 4))
  16703. owner_id = script_getnum(st, 4);
  16704. else {
  16705. struct map_session_data *sd = NULL;
  16706. sd = script_rid2sd(st); // If sd is NULL, instance_create will return -2.
  16707. switch(mode) {
  16708. case IM_NONE:
  16709. owner_id = st->oid;
  16710. break;
  16711. case IM_CHAR:
  16712. if (sd)
  16713. owner_id = sd->status.char_id;
  16714. break;
  16715. case IM_PARTY:
  16716. if (sd)
  16717. owner_id = sd->status.party_id;
  16718. break;
  16719. case IM_GUILD:
  16720. if (sd)
  16721. owner_id = sd->status.guild_id;
  16722. break;
  16723. default:
  16724. ShowError("buildin_instance_create: Invalid instance mode (instance name: %s)\n", script_getstr(st, 2));
  16725. return SCRIPT_CMD_FAILURE;
  16726. }
  16727. }
  16728. script_pushint(st, instance_create(owner_id, script_getstr(st, 2), mode));
  16729. return SCRIPT_CMD_SUCCESS;
  16730. }
  16731. /*==========================================
  16732. * Destroys an instance (unofficial)
  16733. * Officially instances are only destroyed by timeout
  16734. *
  16735. * instance_destroy {<instance_id>};
  16736. *------------------------------------------*/
  16737. BUILDIN_FUNC(instance_destroy)
  16738. {
  16739. unsigned short instance_id;
  16740. if( script_hasdata(st,2) )
  16741. instance_id = script_getnum(st,2);
  16742. else
  16743. instance_id = script_instancegetid(st);
  16744. if( instance_id == 0 || instance_id >= MAX_MAP_PER_SERVER ) {
  16745. ShowError("buildin_instance_destroy: Trying to destroy invalid instance %hu.\n", instance_id);
  16746. return SCRIPT_CMD_FAILURE;
  16747. }
  16748. instance_destroy(instance_id);
  16749. return SCRIPT_CMD_SUCCESS;
  16750. }
  16751. /*==========================================
  16752. * Warps player to instance
  16753. * Results:
  16754. * IE_OK: Success
  16755. * IE_NOMEMBER: Character not in party/guild (for party/guild type instances)
  16756. * IE_NOINSTANCE: Character/Party/Guild doesn't have instance
  16757. * IE_OTHER: Other errors (instance not in DB, instance doesn't match with character/party/guild, etc.)
  16758. *------------------------------------------*/
  16759. BUILDIN_FUNC(instance_enter)
  16760. {
  16761. struct map_session_data *sd = NULL;
  16762. int x = script_hasdata(st,3) ? script_getnum(st, 3) : -1;
  16763. int y = script_hasdata(st,4) ? script_getnum(st, 4) : -1;
  16764. unsigned short instance_id;
  16765. if (script_hasdata(st, 6))
  16766. instance_id = script_getnum(st, 6);
  16767. else
  16768. instance_id = script_instancegetid(st);
  16769. if (!script_charid2sd(5,sd))
  16770. return SCRIPT_CMD_FAILURE;
  16771. script_pushint(st, instance_enter(sd, instance_id, script_getstr(st, 2), x, y));
  16772. return SCRIPT_CMD_SUCCESS;
  16773. }
  16774. /*==========================================
  16775. * Returns the name of a duplicated NPC
  16776. *
  16777. * instance_npcname <npc_name>{,<instance_id>};
  16778. * <npc_name> is the full name of an NPC.
  16779. *------------------------------------------*/
  16780. BUILDIN_FUNC(instance_npcname)
  16781. {
  16782. const char *str;
  16783. unsigned short instance_id = 0;
  16784. struct npc_data *nd;
  16785. str = script_getstr(st,2);
  16786. if( script_hasdata(st,3) )
  16787. instance_id = script_getnum(st,3);
  16788. else
  16789. instance_id = script_instancegetid(st);
  16790. if( instance_id && (nd = npc_name2id(str)) != NULL ) {
  16791. static char npcname[NAME_LENGTH];
  16792. snprintf(npcname, sizeof(npcname), "dup_%hu_%d", instance_id, nd->bl.id);
  16793. script_pushconststr(st,npcname);
  16794. } else {
  16795. ShowError("buildin_instance_npcname: Invalid instance NPC (instance_id: %hu, NPC name: \"%s\".)\n", instance_id, str);
  16796. st->state = END;
  16797. return SCRIPT_CMD_FAILURE;
  16798. }
  16799. return SCRIPT_CMD_SUCCESS;
  16800. }
  16801. /*==========================================
  16802. * Returns the name of a duplicated map
  16803. *
  16804. * instance_mapname <map_name>{,<instance_id>};
  16805. *------------------------------------------*/
  16806. BUILDIN_FUNC(instance_mapname)
  16807. {
  16808. const char *str;
  16809. int16 m;
  16810. unsigned short instance_id = 0;
  16811. str = script_getstr(st,2);
  16812. if( script_hasdata(st,3) )
  16813. instance_id = script_getnum(st,3);
  16814. else
  16815. instance_id = script_instancegetid(st);
  16816. // Check that instance mapname is a valid map
  16817. if(!instance_id || (m = instance_mapname2mapid(str,instance_id)) < 0)
  16818. script_pushconststr(st, "");
  16819. else
  16820. script_pushconststr(st, map[m].name);
  16821. return SCRIPT_CMD_SUCCESS;
  16822. }
  16823. /*==========================================
  16824. * Returns an Instance ID
  16825. *------------------------------------------*/
  16826. BUILDIN_FUNC(instance_id)
  16827. {
  16828. script_pushint(st, script_instancegetid(st));
  16829. return SCRIPT_CMD_SUCCESS;
  16830. }
  16831. /*==========================================
  16832. * Warps all players inside an instance
  16833. *
  16834. * instance_warpall <map_name>,<x>,<y>{,<instance_id>};
  16835. *------------------------------------------*/
  16836. static int buildin_instance_warpall_sub(struct block_list *bl, va_list ap)
  16837. {
  16838. unsigned int m = va_arg(ap,unsigned int);
  16839. int x = va_arg(ap,int);
  16840. int y = va_arg(ap,int);
  16841. unsigned short instance_id = va_arg(ap,unsigned int);
  16842. struct map_session_data *sd = NULL;
  16843. int owner_id = 0;
  16844. nullpo_retr(0, bl);
  16845. if (bl->type != BL_PC)
  16846. return 0;
  16847. sd = (TBL_PC *)bl;
  16848. owner_id = instance_data[instance_id].owner_id;
  16849. switch(instance_data[instance_id].mode) {
  16850. case IM_NONE:
  16851. break;
  16852. case IM_CHAR:
  16853. if (sd->status.char_id != owner_id)
  16854. return 0;
  16855. break;
  16856. case IM_PARTY:
  16857. if (sd->status.party_id != owner_id)
  16858. return 0;
  16859. break;
  16860. case IM_GUILD:
  16861. if (sd->status.guild_id != owner_id)
  16862. return 0;
  16863. }
  16864. pc_setpos(sd, m, x, y, CLR_TELEPORT);
  16865. return 1;
  16866. }
  16867. BUILDIN_FUNC(instance_warpall)
  16868. {
  16869. int16 m, i;
  16870. unsigned short instance_id;
  16871. const char *mapn;
  16872. int x, y;
  16873. mapn = script_getstr(st,2);
  16874. x = script_getnum(st,3);
  16875. y = script_getnum(st,4);
  16876. if( script_hasdata(st,5) )
  16877. instance_id = script_getnum(st,5);
  16878. else
  16879. instance_id = script_instancegetid(st);
  16880. if( !instance_id || (m = map_mapname2mapid(mapn)) < 0 || (m = instance_mapname2mapid(map[m].name,instance_id)) < 0)
  16881. return SCRIPT_CMD_FAILURE;
  16882. for(i = 0; i < instance_data[instance_id].cnt_map; i++)
  16883. map_foreachinmap(buildin_instance_warpall_sub, instance_data[instance_id].map[i]->m, BL_PC, map_id2index(m), x, y, instance_id);
  16884. return SCRIPT_CMD_SUCCESS;
  16885. }
  16886. /*==========================================
  16887. * Broadcasts to all maps inside an instance
  16888. *
  16889. * instance_announce <instance id>,"<text>",<flag>{,<fontColor>{,<fontType>{,<fontSize>{,<fontAlign>{,<fontY>}}}}};
  16890. * Using 0 for <instance id> will auto-detect the id.
  16891. *------------------------------------------*/
  16892. BUILDIN_FUNC(instance_announce) {
  16893. unsigned short instance_id = script_getnum(st,2);
  16894. const char *mes = script_getstr(st,3);
  16895. int flag = script_getnum(st,4);
  16896. const char *fontColor = script_hasdata(st,5) ? script_getstr(st,5) : NULL;
  16897. int fontType = script_hasdata(st,6) ? script_getnum(st,6) : FW_NORMAL; // default fontType
  16898. int fontSize = script_hasdata(st,7) ? script_getnum(st,7) : 12; // default fontSize
  16899. int fontAlign = script_hasdata(st,8) ? script_getnum(st,8) : 0; // default fontAlign
  16900. int fontY = script_hasdata(st,9) ? script_getnum(st,9) : 0; // default fontY
  16901. int i;
  16902. if( instance_id == 0 ) {
  16903. instance_id = script_instancegetid(st);
  16904. }
  16905. if( !instance_id && &instance_data[instance_id] != NULL) {
  16906. ShowError("buildin_instance_announce: Intance is not found.\n");
  16907. return SCRIPT_CMD_FAILURE;
  16908. }
  16909. for( i = 0; i < instance_data[instance_id].cnt_map; i++ )
  16910. map_foreachinmap(buildin_announce_sub, instance_data[instance_id].map[i]->m, BL_PC,
  16911. mes, strlen(mes)+1, flag&BC_COLOR_MASK, fontColor, fontType, fontSize, fontAlign, fontY);
  16912. return SCRIPT_CMD_SUCCESS;
  16913. }
  16914. /*==========================================
  16915. * instance_check_party [malufett]
  16916. * Values:
  16917. * party_id : Party ID of the invoking character. [Required Parameter]
  16918. * amount : Amount of needed Partymembers for the Instance. [Optional Parameter]
  16919. * min : Minimum Level needed to join the Instance. [Optional Parameter]
  16920. * max : Maxium Level allowed to join the Instance. [Optional Parameter]
  16921. * Example: instance_check_party (getcharid(1){,amount}{,min}{,max});
  16922. * Example 2: instance_check_party (getcharid(1),1,1,99);
  16923. *------------------------------------------*/
  16924. BUILDIN_FUNC(instance_check_party)
  16925. {
  16926. int amount, min, max, i, party_id, c = 0;
  16927. struct party_data *p = NULL;
  16928. amount = script_hasdata(st,3) ? script_getnum(st,3) : 1; // Amount of needed Partymembers for the Instance.
  16929. min = script_hasdata(st,4) ? script_getnum(st,4) : 1; // Minimum Level needed to join the Instance.
  16930. max = script_hasdata(st,5) ? script_getnum(st,5) : MAX_LEVEL; // Maxium Level allowed to join the Instance.
  16931. if( min < 1 || min > MAX_LEVEL) {
  16932. ShowError("buildin_instance_check_party: Invalid min level, %d\n", min);
  16933. return SCRIPT_CMD_FAILURE;
  16934. } else if( max < 1 || max > MAX_LEVEL) {
  16935. ShowError("buildin_instance_check_party: Invalid max level, %d\n", max);
  16936. return SCRIPT_CMD_FAILURE;
  16937. }
  16938. if( script_hasdata(st,2) )
  16939. party_id = script_getnum(st,2);
  16940. else return SCRIPT_CMD_FAILURE;
  16941. if( !(p = party_search(party_id)) ) {
  16942. script_pushint(st, 0); // Returns false if party does not exist.
  16943. return SCRIPT_CMD_FAILURE;
  16944. }
  16945. for( i = 0; i < MAX_PARTY; i++ ) {
  16946. struct map_session_data *pl_sd;
  16947. if( (pl_sd = p->data[i].sd) )
  16948. if(map_id2bl(pl_sd->bl.id)) {
  16949. if(pl_sd->status.base_level < min) {
  16950. script_pushint(st, 0);
  16951. return SCRIPT_CMD_SUCCESS;
  16952. } else if(pl_sd->status.base_level > max) {
  16953. script_pushint(st, 0);
  16954. return SCRIPT_CMD_SUCCESS;
  16955. }
  16956. c++;
  16957. }
  16958. }
  16959. if(c < amount)
  16960. script_pushint(st, 0); // Not enough Members in the Party to join Instance.
  16961. else
  16962. script_pushint(st, 1);
  16963. return SCRIPT_CMD_SUCCESS;
  16964. }
  16965. /*==========================================
  16966. * instance_check_guild
  16967. * Values:
  16968. * guild_id : Guild ID of the invoking character. [Required Parameter]
  16969. * amount : Amount of needed Guild members for the Instance. [Optional Parameter]
  16970. * min : Minimum Level needed to join the Instance. [Optional Parameter]
  16971. * max : Maxium Level allowed to join the Instance. [Optional Parameter]
  16972. * Example: instance_check_guild (getcharid(2){,amount}{,min}{,max});
  16973. * Example 2: instance_check_guild (getcharid(2),1,1,99);
  16974. *------------------------------------------*/
  16975. BUILDIN_FUNC(instance_check_guild)
  16976. {
  16977. int amount, min, max, i, guild_id = 0, c = 0;
  16978. struct guild *g = NULL;
  16979. amount = script_hasdata(st,3) ? script_getnum(st,3) : 1; // Amount of needed Guild members for the Instance.
  16980. min = script_hasdata(st,4) ? script_getnum(st,4) : 1; // Minimum Level needed to join the Instance.
  16981. max = script_hasdata(st,5) ? script_getnum(st,5) : MAX_LEVEL; // Maxium Level allowed to join the Instance.
  16982. if (min < 1 || min > MAX_LEVEL) {
  16983. ShowError("buildin_instance_check_guild: Invalid min level, %d\n", min);
  16984. return SCRIPT_CMD_FAILURE;
  16985. } else if (max < 1 || max > MAX_LEVEL) {
  16986. ShowError("buildin_instance_check_guild: Invalid max level, %d\n", max);
  16987. return SCRIPT_CMD_FAILURE;
  16988. }
  16989. if (script_hasdata(st,2))
  16990. guild_id = script_getnum(st,2);
  16991. else
  16992. return SCRIPT_CMD_FAILURE;
  16993. if (!(g = guild_search(guild_id))) {
  16994. script_pushint(st, 0); // Returns false if guild does not exist.
  16995. return SCRIPT_CMD_FAILURE;
  16996. }
  16997. for(i = 0; i < MAX_GUILD; i++) {
  16998. struct map_session_data *pl_sd;
  16999. if ((pl_sd = g->member[i].sd)) {
  17000. if (map_id2bl(pl_sd->bl.id)) {
  17001. if (pl_sd->status.base_level < min) {
  17002. script_pushint(st, 0);
  17003. return SCRIPT_CMD_SUCCESS;
  17004. } else if (pl_sd->status.base_level > max) {
  17005. script_pushint(st, 0);
  17006. return SCRIPT_CMD_SUCCESS;
  17007. }
  17008. c++;
  17009. }
  17010. }
  17011. }
  17012. if (c < amount)
  17013. script_pushint(st, 0); // Not enough Members in the Guild to join Instance.
  17014. else
  17015. script_pushint(st, 1);
  17016. return SCRIPT_CMD_SUCCESS;
  17017. }
  17018. /*==========================================
  17019. * instance_info
  17020. * Values:
  17021. * name : name of the instance you want to look up. [Required Parameter]
  17022. * type : type of information you want to look up for the specified instance. [Required Parameter]
  17023. * index : index of the map in the instance. [Optional Parameter]
  17024. *------------------------------------------*/
  17025. BUILDIN_FUNC(instance_info)
  17026. {
  17027. const char* name = script_getstr(st, 2);
  17028. int type = script_getnum(st, 3);
  17029. int index = 0;
  17030. struct instance_db *db = instance_searchname_db(name);
  17031. if( !db ){
  17032. ShowError( "buildin_instance_info: Unknown instance name \"%s\".\n", name );
  17033. script_pushint(st, -1);
  17034. return SCRIPT_CMD_FAILURE;
  17035. }
  17036. switch( type ){
  17037. case IIT_ID:
  17038. script_pushint(st, db->id);
  17039. break;
  17040. case IIT_TIME_LIMIT:
  17041. script_pushint(st, db->limit);
  17042. break;
  17043. case IIT_IDLE_TIMEOUT:
  17044. script_pushint(st, db->timeout);
  17045. break;
  17046. case IIT_ENTER_MAP:
  17047. script_pushstrcopy(st, StringBuf_Value(db->enter.mapname));
  17048. break;
  17049. case IIT_ENTER_X:
  17050. script_pushint(st, db->enter.x);
  17051. break;
  17052. case IIT_ENTER_Y:
  17053. script_pushint(st, db->enter.y);
  17054. break;
  17055. case IIT_MAPCOUNT:
  17056. script_pushint(st, db->maplist_count);
  17057. break;
  17058. case IIT_MAP:
  17059. if( !script_hasdata(st, 4) || script_isstring(st, 4) ){
  17060. ShowError( "buildin_instance_info: Type IIT_MAP requires a numeric index argument.\n" );
  17061. script_pushstr(st, "");
  17062. return SCRIPT_CMD_FAILURE;
  17063. }
  17064. index = script_getnum(st, 4);
  17065. if( index < 0 ){
  17066. ShowError( "buildin_instance_info: Type IIT_MAP does not support a negative index argument.\n" );
  17067. script_pushstr(st, "");
  17068. return SCRIPT_CMD_FAILURE;
  17069. }
  17070. if( index > UINT8_MAX ){
  17071. ShowError( "buildin_instance_info: Type IIT_MAP does only support up to index %hu.\n", UINT8_MAX );
  17072. script_pushstr(st, "");
  17073. return SCRIPT_CMD_FAILURE;
  17074. }
  17075. script_pushstrcopy(st, StringBuf_Value(db->maplist[index]));
  17076. break;
  17077. default:
  17078. ShowError("buildin_instance_info: Unknown instance information type \"%d\".\n", type );
  17079. script_pushint(st, -1);
  17080. return SCRIPT_CMD_FAILURE;
  17081. }
  17082. return SCRIPT_CMD_SUCCESS;
  17083. }
  17084. /*==========================================
  17085. * Custom Fonts
  17086. *------------------------------------------*/
  17087. BUILDIN_FUNC(setfont)
  17088. {
  17089. struct map_session_data *sd = script_rid2sd(st);
  17090. int font = script_getnum(st,2);
  17091. if( sd == NULL )
  17092. return SCRIPT_CMD_SUCCESS;
  17093. if( sd->status.font != font )
  17094. sd->status.font = font;
  17095. else
  17096. sd->status.font = 0;
  17097. clif_font(sd);
  17098. return SCRIPT_CMD_SUCCESS;
  17099. }
  17100. static int buildin_mobuseskill_sub(struct block_list *bl,va_list ap)
  17101. {
  17102. TBL_MOB* md = (TBL_MOB*)bl;
  17103. struct block_list *tbl;
  17104. int mobid = va_arg(ap,int);
  17105. uint16 skill_id = va_arg(ap,int);
  17106. uint16 skill_lv = va_arg(ap,int);
  17107. int casttime = va_arg(ap,int);
  17108. int cancel = va_arg(ap,int);
  17109. int emotion = va_arg(ap,int);
  17110. int target = va_arg(ap,int);
  17111. if( md->mob_id != mobid )
  17112. return 0;
  17113. // 0:self, 1:target, 2:master, default:random
  17114. switch( target ) {
  17115. case 0: tbl = map_id2bl(md->bl.id); break;
  17116. case 1: tbl = map_id2bl(md->target_id); break;
  17117. case 2: tbl = map_id2bl(md->master_id); break;
  17118. default:tbl = battle_getenemy(&md->bl, DEFAULT_ENEMY_TYPE(md), skill_get_range2(&md->bl, skill_id, skill_lv, true)); break;
  17119. }
  17120. if( !tbl )
  17121. return 0;
  17122. if( md->ud.skilltimer != INVALID_TIMER ) // Cancel the casting skill.
  17123. unit_skillcastcancel(bl,0);
  17124. if( skill_get_casttype(skill_id) == CAST_GROUND )
  17125. unit_skilluse_pos2(&md->bl, tbl->x, tbl->y, skill_id, skill_lv, casttime, cancel);
  17126. else
  17127. unit_skilluse_id2(&md->bl, tbl->id, skill_id, skill_lv, casttime, cancel);
  17128. clif_emotion(&md->bl, emotion);
  17129. return 1;
  17130. }
  17131. /*==========================================
  17132. * areamobuseskill "Map Name",<x>,<y>,<range>,<Mob ID>,"Skill Name"/<Skill ID>,<Skill Lv>,<Cast Time>,<Cancelable>,<Emotion>,<Target Type>;
  17133. *------------------------------------------*/
  17134. BUILDIN_FUNC(areamobuseskill)
  17135. {
  17136. struct block_list center;
  17137. struct script_data *data;
  17138. int16 m;
  17139. int range,mobid,skill_id,skill_lv,casttime,emotion,target,cancel;
  17140. if( (m = map_mapname2mapid(script_getstr(st,2))) < 0 ) {
  17141. ShowError("areamobuseskill: invalid map name.\n");
  17142. return SCRIPT_CMD_SUCCESS;
  17143. }
  17144. center.m = m;
  17145. center.x = script_getnum(st,3);
  17146. center.y = script_getnum(st,4);
  17147. range = script_getnum(st,5);
  17148. mobid = script_getnum(st,6);
  17149. data = script_getdata(st, 7);
  17150. get_val(st, data); // Convert into value in case of a variable
  17151. skill_id = ( data_isstring(data) ? skill_name2id(script_getstr(st,7)) : script_getnum(st,7) );
  17152. if( (skill_lv = script_getnum(st,8)) > battle_config.mob_max_skilllvl )
  17153. skill_lv = battle_config.mob_max_skilllvl;
  17154. casttime = script_getnum(st,9);
  17155. cancel = script_getnum(st,10);
  17156. emotion = script_getnum(st,11);
  17157. target = script_getnum(st,12);
  17158. map_foreachinrange(buildin_mobuseskill_sub, &center, range, BL_MOB, mobid, skill_id, skill_lv, casttime, cancel, emotion, target);
  17159. return SCRIPT_CMD_SUCCESS;
  17160. }
  17161. BUILDIN_FUNC(progressbar)
  17162. {
  17163. struct map_session_data * sd = script_rid2sd(st);
  17164. const char * color;
  17165. unsigned int second;
  17166. if( !st || !sd )
  17167. return SCRIPT_CMD_SUCCESS;
  17168. st->state = STOP;
  17169. color = script_getstr(st,2);
  17170. second = script_getnum(st,3);
  17171. sd->progressbar.npc_id = st->oid;
  17172. sd->progressbar.timeout = gettick() + second*1000;
  17173. sd->state.workinprogress = WIP_DISABLE_ALL;
  17174. clif_progressbar(sd, strtol(color, (char **)NULL, 0), second);
  17175. return SCRIPT_CMD_SUCCESS;
  17176. }
  17177. BUILDIN_FUNC(pushpc)
  17178. {
  17179. uint8 dir;
  17180. int cells, dx, dy;
  17181. struct map_session_data* sd;
  17182. if((sd = script_rid2sd(st))==NULL)
  17183. {
  17184. return SCRIPT_CMD_SUCCESS;
  17185. }
  17186. dir = script_getnum(st,2);
  17187. cells = script_getnum(st,3);
  17188. if(dir >= DIR_MAX)
  17189. {
  17190. ShowWarning("buildin_pushpc: Invalid direction %d specified.\n", dir);
  17191. script_reportsrc(st);
  17192. dir%= DIR_MAX; // trim spin-over
  17193. }
  17194. if(!cells)
  17195. {// zero distance
  17196. return SCRIPT_CMD_SUCCESS;
  17197. }
  17198. else if(cells<0)
  17199. {// pushing backwards
  17200. dir = (dir+DIR_MAX/2)%DIR_MAX; // turn around
  17201. cells = -cells;
  17202. }
  17203. dx = dirx[dir];
  17204. dy = diry[dir];
  17205. unit_blown(&sd->bl, dx, dy, cells, 0);
  17206. return SCRIPT_CMD_SUCCESS;
  17207. }
  17208. /// Invokes buying store preparation window
  17209. /// buyingstore <slots>;
  17210. BUILDIN_FUNC(buyingstore)
  17211. {
  17212. struct map_session_data* sd;
  17213. if( ( sd = script_rid2sd(st) ) == NULL )
  17214. {
  17215. return SCRIPT_CMD_SUCCESS;
  17216. }
  17217. if( npc_isnear(&sd->bl) ) {
  17218. char output[150];
  17219. sprintf(output, msg_txt(sd,662), battle_config.min_npc_vendchat_distance);
  17220. clif_displaymessage(sd->fd, output);
  17221. return SCRIPT_CMD_SUCCESS;
  17222. }
  17223. buyingstore_setup(sd, script_getnum(st,2));
  17224. return SCRIPT_CMD_SUCCESS;
  17225. }
  17226. /// Invokes search store info window
  17227. /// searchstores <uses>,<effect>;
  17228. BUILDIN_FUNC(searchstores)
  17229. {
  17230. unsigned short effect;
  17231. unsigned int uses;
  17232. struct map_session_data* sd;
  17233. if( ( sd = script_rid2sd(st) ) == NULL )
  17234. {
  17235. return SCRIPT_CMD_SUCCESS;
  17236. }
  17237. uses = script_getnum(st,2);
  17238. effect = script_getnum(st,3);
  17239. if( !uses )
  17240. {
  17241. ShowError("buildin_searchstores: Amount of uses cannot be zero.\n");
  17242. return SCRIPT_CMD_FAILURE;
  17243. }
  17244. if( effect > 1 )
  17245. {
  17246. ShowError("buildin_searchstores: Invalid effect id %hu, specified.\n", effect);
  17247. return SCRIPT_CMD_FAILURE;
  17248. }
  17249. searchstore_open(sd, uses, effect);
  17250. return SCRIPT_CMD_SUCCESS;
  17251. }
  17252. /// Displays a number as large digital clock.
  17253. /// showdigit <value>[,<type>];
  17254. BUILDIN_FUNC(showdigit)
  17255. {
  17256. unsigned int type = 0;
  17257. int value;
  17258. struct map_session_data* sd;
  17259. if( ( sd = script_rid2sd(st) ) == NULL )
  17260. {
  17261. return SCRIPT_CMD_SUCCESS;
  17262. }
  17263. value = script_getnum(st,2);
  17264. if( script_hasdata(st,3) ){
  17265. type = script_getnum(st,3);
  17266. }
  17267. switch( type ){
  17268. case 0:
  17269. break;
  17270. case 1:
  17271. case 2:
  17272. // Use absolute value and then the negative value of it as starting value
  17273. // This is what gravity's client does for these counters
  17274. value = -abs(value);
  17275. break;
  17276. case 3:
  17277. value = abs(value);
  17278. if( value > 99 ){
  17279. ShowWarning("buildin_showdigit: type 3 can display 2 digits at max. Capping value %d to 99...\n", value);
  17280. script_reportsrc(st);
  17281. value = 99;
  17282. }
  17283. break;
  17284. default:
  17285. ShowError("buildin_showdigit: Invalid type %u.\n", type);
  17286. return SCRIPT_CMD_FAILURE;
  17287. }
  17288. clif_showdigit(sd, (unsigned char)type, value);
  17289. return SCRIPT_CMD_SUCCESS;
  17290. }
  17291. /**
  17292. * Rune Knight
  17293. * makerune <% success bonus>{,<char_id>};
  17294. **/
  17295. BUILDIN_FUNC(makerune) {
  17296. TBL_PC* sd;
  17297. if (!script_charid2sd(3,sd))
  17298. return SCRIPT_CMD_FAILURE;
  17299. clif_skill_produce_mix_list(sd,RK_RUNEMASTERY,24);
  17300. sd->itemid = script_getnum(st,2);
  17301. return SCRIPT_CMD_SUCCESS;
  17302. }
  17303. /**
  17304. * checkdragon({<char_id>}) returns 1 if mounting a dragon or 0 otherwise.
  17305. **/
  17306. BUILDIN_FUNC(checkdragon) {
  17307. TBL_PC* sd;
  17308. if (!script_charid2sd(2,sd))
  17309. return SCRIPT_CMD_FAILURE;
  17310. if( pc_isridingdragon(sd) )
  17311. script_pushint(st,1);
  17312. else
  17313. script_pushint(st,0);
  17314. return SCRIPT_CMD_SUCCESS;
  17315. }
  17316. /**
  17317. * setdragon({optional Color{,<char_id>}}) returns 1 on success or 0 otherwise
  17318. * - Toggles the dragon on a RK if he can mount;
  17319. * @param Color - when not provided uses the green dragon;
  17320. * - 1 : Green Dragon
  17321. * - 2 : Brown Dragon
  17322. * - 3 : Gray Dragon
  17323. * - 4 : Blue Dragon
  17324. * - 5 : Red Dragon
  17325. **/
  17326. BUILDIN_FUNC(setdragon) {
  17327. TBL_PC* sd;
  17328. int color = script_hasdata(st,2) ? script_getnum(st,2) : 0;
  17329. if (!script_charid2sd(3,sd))
  17330. return SCRIPT_CMD_FAILURE;
  17331. if( !pc_checkskill(sd,RK_DRAGONTRAINING) || (sd->class_&MAPID_THIRDMASK) != MAPID_RUNE_KNIGHT )
  17332. script_pushint(st,0);//Doesn't have the skill or it's not a Rune Knight
  17333. else if ( pc_isridingdragon(sd) ) {//Is mounted; release
  17334. pc_setoption(sd, sd->sc.option&~OPTION_DRAGON);
  17335. script_pushint(st,1);
  17336. } else {//Not mounted; Mount now.
  17337. unsigned int option = OPTION_DRAGON1;
  17338. if( color ) {
  17339. option = ( color == 1 ? OPTION_DRAGON1 :
  17340. color == 2 ? OPTION_DRAGON2 :
  17341. color == 3 ? OPTION_DRAGON3 :
  17342. color == 4 ? OPTION_DRAGON4 :
  17343. color == 5 ? OPTION_DRAGON5 : 0);
  17344. if( !option ) {
  17345. ShowWarning("script_setdragon: Unknown Color %d used; changing to green (1)\n",color);
  17346. option = OPTION_DRAGON1;
  17347. }
  17348. }
  17349. pc_setoption(sd, sd->sc.option|option);
  17350. script_pushint(st,1);
  17351. }
  17352. return SCRIPT_CMD_SUCCESS;
  17353. }
  17354. /**
  17355. * ismounting({<char_id>}) returns 1 if mounting a new mount or 0 otherwise
  17356. **/
  17357. BUILDIN_FUNC(ismounting) {
  17358. TBL_PC* sd;
  17359. if (!script_charid2sd(2,sd))
  17360. return SCRIPT_CMD_FAILURE;
  17361. if( &sd->sc && sd->sc.data[SC_ALL_RIDING] )
  17362. script_pushint(st,1);
  17363. else
  17364. script_pushint(st,0);
  17365. return SCRIPT_CMD_SUCCESS;
  17366. }
  17367. /**
  17368. * setmounting({<char_id>}) returns 1 on success or 0 otherwise
  17369. * - Toggles new mounts on a player when he can mount
  17370. * - Will fail if the player is mounting a non-new mount, e.g. dragon, peco, wug, etc.
  17371. * - Will unmount the player is he is already mounting
  17372. **/
  17373. BUILDIN_FUNC(setmounting) {
  17374. TBL_PC* sd;
  17375. if (!script_charid2sd(2,sd))
  17376. return SCRIPT_CMD_FAILURE;
  17377. if( &sd->sc && sd->sc.option&(OPTION_WUGRIDER|OPTION_RIDING|OPTION_DRAGON|OPTION_MADOGEAR) ) {
  17378. clif_msg(sd, NEED_REINS_OF_MOUNT);
  17379. script_pushint(st,0); //can't mount with one of these
  17380. } else {
  17381. if( &sd->sc && sd->sc.data[SC_ALL_RIDING] )
  17382. status_change_end(&sd->bl, SC_ALL_RIDING, INVALID_TIMER); //release mount
  17383. else
  17384. sc_start(NULL, &sd->bl, SC_ALL_RIDING, 10000, 1, INVALID_TIMER); //mount
  17385. script_pushint(st,1);//in both cases, return 1.
  17386. }
  17387. return SCRIPT_CMD_SUCCESS;
  17388. }
  17389. /**
  17390. * Retrieves quantity of arguments provided to callfunc/callsub.
  17391. * getargcount() -> amount of arguments received in a function
  17392. **/
  17393. BUILDIN_FUNC(getargcount) {
  17394. struct script_retinfo* ri;
  17395. if( st->stack->defsp < 1 || st->stack->stack_data[st->stack->defsp - 1].type != C_RETINFO ) {
  17396. ShowError("script:getargcount: used out of function or callsub label!\n");
  17397. st->state = END;
  17398. return SCRIPT_CMD_FAILURE;
  17399. }
  17400. ri = st->stack->stack_data[st->stack->defsp - 1].u.ri;
  17401. script_pushint(st, ri->nargs);
  17402. return SCRIPT_CMD_SUCCESS;
  17403. }
  17404. /**
  17405. * getcharip(<account ID>/<character ID>/<character name>)
  17406. **/
  17407. BUILDIN_FUNC(getcharip)
  17408. {
  17409. struct map_session_data* sd = NULL;
  17410. /* check if a character name is specified */
  17411. if( script_hasdata(st, 2) )
  17412. {
  17413. struct script_data *data;
  17414. data = script_getdata(st, 2);
  17415. get_val(st, data); // Convert into value in case of a variable
  17416. if (data_isstring(data))
  17417. sd = map_nick2sd(script_getstr(st, 2));
  17418. else if (data_isint(data) || script_getnum(st, 2))
  17419. {
  17420. int id = 0;
  17421. id = script_getnum(st, 2);
  17422. sd = (map_id2sd(id) ? map_id2sd(id) : map_charid2sd(id));
  17423. }
  17424. }
  17425. else
  17426. sd = script_rid2sd(st);
  17427. /* check for sd and IP */
  17428. if (!sd || !session[sd->fd]->client_addr)
  17429. {
  17430. script_pushconststr(st, "");
  17431. return SCRIPT_CMD_SUCCESS;
  17432. }
  17433. /* return the client ip_addr converted for output */
  17434. if (sd && sd->fd && session[sd->fd])
  17435. {
  17436. /* initiliaze */
  17437. const char *ip_addr = NULL;
  17438. uint32 ip;
  17439. /* set ip, ip_addr and convert to ip and push str */
  17440. ip = session[sd->fd]->client_addr;
  17441. ip_addr = ip2str(ip, NULL);
  17442. script_pushstrcopy(st, ip_addr);
  17443. }
  17444. return SCRIPT_CMD_SUCCESS;
  17445. }
  17446. /**
  17447. * is_function(<function name>) -> 1 if function exists, 0 otherwise
  17448. **/
  17449. BUILDIN_FUNC(is_function) {
  17450. const char* str = script_getstr(st,2);
  17451. if( strdb_exists(userfunc_db, str) )
  17452. script_pushint(st,1);
  17453. else
  17454. script_pushint(st,0);
  17455. return SCRIPT_CMD_SUCCESS;
  17456. }
  17457. /**
  17458. * get_revision() -> retrieves the current svn revision (if available)
  17459. **/
  17460. BUILDIN_FUNC(get_revision) {
  17461. const char *svn = get_svn_revision();
  17462. if ( svn[0] != UNKNOWN_VERSION )
  17463. script_pushint(st,atoi(svn));
  17464. else
  17465. script_pushint(st,-1); //unknown
  17466. return SCRIPT_CMD_SUCCESS;
  17467. }
  17468. /* get_hash() -> retrieves the current git hash (if available)*/
  17469. BUILDIN_FUNC(get_githash) {
  17470. const char* git = get_git_hash();
  17471. char buf[CHAT_SIZE_MAX];
  17472. safestrncpy(buf,git,strlen(git)+1);
  17473. if ( git[0] != UNKNOWN_VERSION )
  17474. script_pushstr(st,buf);
  17475. else
  17476. script_pushstr(st,"Unknown"); //unknown
  17477. return SCRIPT_CMD_SUCCESS;
  17478. }
  17479. /**
  17480. * freeloop(<toggle>) -> toggles this script instance's looping-check ability
  17481. **/
  17482. BUILDIN_FUNC(freeloop) {
  17483. if( script_hasdata(st,2) ) {
  17484. if( script_getnum(st,2) )
  17485. st->freeloop = 1;
  17486. else
  17487. st->freeloop = 0;
  17488. }
  17489. script_pushint(st, st->freeloop);
  17490. return SCRIPT_CMD_SUCCESS;
  17491. }
  17492. /**
  17493. * @commands (script based)
  17494. **/
  17495. BUILDIN_FUNC(bindatcmd) {
  17496. const char* atcmd;
  17497. const char* eventName;
  17498. int i, level = 0, level2 = 0;
  17499. bool create = false;
  17500. atcmd = script_getstr(st,2);
  17501. eventName = script_getstr(st,3);
  17502. if( *atcmd == atcommand_symbol || *atcmd == charcommand_symbol )
  17503. atcmd++;
  17504. if( script_hasdata(st,4) ) level = script_getnum(st,4);
  17505. if( script_hasdata(st,5) ) level2 = script_getnum(st,5);
  17506. if( atcmd_binding_count == 0 ) {
  17507. CREATE(atcmd_binding,struct atcmd_binding_data*,1);
  17508. create = true;
  17509. } else {
  17510. ARR_FIND(0, atcmd_binding_count, i, strcmp(atcmd_binding[i]->command,atcmd) == 0);
  17511. if( i < atcmd_binding_count ) {/* update existent entry */
  17512. safestrncpy(atcmd_binding[i]->npc_event, eventName, 50);
  17513. atcmd_binding[i]->level = level;
  17514. atcmd_binding[i]->level2 = level2;
  17515. } else
  17516. create = true;
  17517. }
  17518. if( create ) {
  17519. i = atcmd_binding_count;
  17520. if( atcmd_binding_count++ != 0 )
  17521. RECREATE(atcmd_binding,struct atcmd_binding_data*,atcmd_binding_count);
  17522. CREATE(atcmd_binding[i],struct atcmd_binding_data,1);
  17523. safestrncpy(atcmd_binding[i]->command, atcmd, 50);
  17524. safestrncpy(atcmd_binding[i]->npc_event, eventName, 50);
  17525. atcmd_binding[i]->level = level;
  17526. atcmd_binding[i]->level2 = level2;
  17527. }
  17528. return SCRIPT_CMD_SUCCESS;
  17529. }
  17530. BUILDIN_FUNC(unbindatcmd) {
  17531. const char* atcmd;
  17532. int i = 0;
  17533. atcmd = script_getstr(st, 2);
  17534. if( *atcmd == atcommand_symbol || *atcmd == charcommand_symbol )
  17535. atcmd++;
  17536. if( atcmd_binding_count == 0 ) {
  17537. script_pushint(st, 0);
  17538. return SCRIPT_CMD_SUCCESS;
  17539. }
  17540. ARR_FIND(0, atcmd_binding_count, i, strcmp(atcmd_binding[i]->command, atcmd) == 0);
  17541. if( i < atcmd_binding_count ) {
  17542. int cursor = 0;
  17543. aFree(atcmd_binding[i]);
  17544. atcmd_binding[i] = NULL;
  17545. /* compact the list now that we freed a slot somewhere */
  17546. for( i = 0, cursor = 0; i < atcmd_binding_count; i++ ) {
  17547. if( atcmd_binding[i] == NULL )
  17548. continue;
  17549. if( cursor != i ) {
  17550. memmove(&atcmd_binding[cursor], &atcmd_binding[i], sizeof(struct atcmd_binding_data*));
  17551. }
  17552. cursor++;
  17553. }
  17554. if( (atcmd_binding_count = cursor) == 0 )
  17555. aFree(atcmd_binding);
  17556. script_pushint(st, 1);
  17557. } else
  17558. script_pushint(st, 0);/* not found */
  17559. return SCRIPT_CMD_SUCCESS;
  17560. }
  17561. BUILDIN_FUNC(useatcmd) {
  17562. return atcommand_sub(st,3);
  17563. }
  17564. BUILDIN_FUNC(checkre)
  17565. {
  17566. int num;
  17567. num=script_getnum(st,2);
  17568. switch(num){
  17569. case 0:
  17570. #ifdef RENEWAL
  17571. script_pushint(st, 1);
  17572. #else
  17573. script_pushint(st, 0);
  17574. #endif
  17575. break;
  17576. case 1:
  17577. #ifdef RENEWAL_CAST
  17578. script_pushint(st, 1);
  17579. #else
  17580. script_pushint(st, 0);
  17581. #endif
  17582. break;
  17583. case 2:
  17584. #ifdef RENEWAL_DROP
  17585. script_pushint(st, 1);
  17586. #else
  17587. script_pushint(st, 0);
  17588. #endif
  17589. break;
  17590. case 3:
  17591. #ifdef RENEWAL_EXP
  17592. script_pushint(st, 1);
  17593. #else
  17594. script_pushint(st, 0);
  17595. #endif
  17596. break;
  17597. case 4:
  17598. #ifdef RENEWAL_LVDMG
  17599. script_pushint(st, 1);
  17600. #else
  17601. script_pushint(st, 0);
  17602. #endif
  17603. break;
  17604. case 5:
  17605. #ifdef RENEWAL_ASPD
  17606. script_pushint(st, 1);
  17607. #else
  17608. script_pushint(st, 0);
  17609. #endif
  17610. break;
  17611. default:
  17612. ShowWarning("buildin_checkre: unknown parameter.\n");
  17613. break;
  17614. }
  17615. return SCRIPT_CMD_SUCCESS;
  17616. }
  17617. /* getrandgroupitem <group_id>{,<quantity>{,<sub_group>}} */
  17618. BUILDIN_FUNC(getrandgroupitem) {
  17619. TBL_PC* sd;
  17620. int i, get_count = 0;
  17621. uint16 group = script_getnum(st,2), qty = 0;
  17622. uint8 sub_group = 1;
  17623. struct item item_tmp;
  17624. struct s_item_group_entry *entry = NULL;
  17625. if (!(sd = script_rid2sd(st)))
  17626. return SCRIPT_CMD_SUCCESS;
  17627. if (!group) {
  17628. ShowError("buildin_getrandgroupitem: Invalid group id (%d)!\n",script_getnum(st,2));
  17629. return SCRIPT_CMD_FAILURE;
  17630. }
  17631. FETCH(3, qty);
  17632. FETCH(4, sub_group);
  17633. entry = itemdb_get_randgroupitem(group,sub_group);
  17634. if (!entry)
  17635. return SCRIPT_CMD_FAILURE; //ensure valid itemid
  17636. memset(&item_tmp,0,sizeof(item_tmp));
  17637. item_tmp.nameid = entry->nameid;
  17638. item_tmp.identify = itemdb_isidentified(entry->nameid);
  17639. if (!qty)
  17640. qty = entry->amount;
  17641. //Check if it's stackable.
  17642. if (!itemdb_isstackable(entry->nameid)) {
  17643. item_tmp.amount = 1;
  17644. get_count = qty;
  17645. }
  17646. else {
  17647. item_tmp.amount = qty;
  17648. get_count = 1;
  17649. }
  17650. for (i = 0; i < get_count; i++) {
  17651. // if not pet egg
  17652. if (!pet_create_egg(sd, entry->nameid)) {
  17653. unsigned char flag = 0;
  17654. if ((flag = pc_additem(sd,&item_tmp,item_tmp.amount,LOG_TYPE_SCRIPT))) {
  17655. clif_additem(sd,0,0,flag);
  17656. if (pc_candrop(sd,&item_tmp))
  17657. map_addflooritem(&item_tmp,item_tmp.amount,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0,0);
  17658. }
  17659. }
  17660. }
  17661. return SCRIPT_CMD_SUCCESS;
  17662. }
  17663. /* getgroupitem <group_id>{,<char_id>};
  17664. * Gives item(s) to the attached player based on item group contents
  17665. */
  17666. BUILDIN_FUNC(getgroupitem) {
  17667. TBL_PC *sd;
  17668. int group_id = script_getnum(st,2);
  17669. if (!script_charid2sd(3,sd))
  17670. return SCRIPT_CMD_SUCCESS;
  17671. if (itemdb_pc_get_itemgroup(group_id,sd)) {
  17672. ShowError("buildin_getgroupitem: Invalid group id '%d' specified.\n",group_id);
  17673. return SCRIPT_CMD_FAILURE;
  17674. }
  17675. return SCRIPT_CMD_SUCCESS;
  17676. }
  17677. /* cleanmap <map_name>;
  17678. * cleanarea <map_name>, <x0>, <y0>, <x1>, <y1>; */
  17679. static int atcommand_cleanfloor_sub(struct block_list *bl, va_list ap)
  17680. {
  17681. nullpo_ret(bl);
  17682. map_clearflooritem(bl);
  17683. return SCRIPT_CMD_SUCCESS;
  17684. }
  17685. BUILDIN_FUNC(cleanmap)
  17686. {
  17687. const char *mapname;
  17688. int16 m;
  17689. mapname = script_getstr(st, 2);
  17690. m = map_mapname2mapid(mapname);
  17691. if (!m)
  17692. return SCRIPT_CMD_FAILURE;
  17693. if ((script_lastdata(st) - 2) < 4) {
  17694. map_foreachinmap(atcommand_cleanfloor_sub, m, BL_ITEM);
  17695. } else {
  17696. int16 x0 = script_getnum(st, 3);
  17697. int16 y0 = script_getnum(st, 4);
  17698. int16 x1 = script_getnum(st, 5);
  17699. int16 y1 = script_getnum(st, 6);
  17700. if (x0 > 0 && y0 > 0 && x1 > 0 && y1 > 0) {
  17701. map_foreachinarea(atcommand_cleanfloor_sub, m, x0, y0, x1, y1, BL_ITEM);
  17702. } else {
  17703. ShowError("cleanarea: invalid coordinate defined!\n");
  17704. return SCRIPT_CMD_FAILURE;
  17705. }
  17706. }
  17707. return SCRIPT_CMD_SUCCESS;
  17708. }
  17709. /* Cast a skill on the attached player.
  17710. * npcskill <skill id>, <skill lvl>, <stat point>, <NPC level>;
  17711. * npcskill "<skill name>", <skill lvl>, <stat point>, <NPC level>; */
  17712. BUILDIN_FUNC(npcskill)
  17713. {
  17714. uint16 skill_id;
  17715. unsigned short skill_level;
  17716. unsigned int stat_point;
  17717. unsigned int npc_level;
  17718. struct npc_data *nd;
  17719. struct map_session_data *sd;
  17720. struct script_data *data;
  17721. data = script_getdata(st, 2);
  17722. get_val(st, data); // Convert into value in case of a variable
  17723. skill_id = data_isstring(data) ? skill_name2id(script_getstr(st, 2)) : script_getnum(st, 2);
  17724. skill_level = script_getnum(st, 3);
  17725. stat_point = script_getnum(st, 4);
  17726. npc_level = script_getnum(st, 5);
  17727. sd = script_rid2sd(st);
  17728. nd = (struct npc_data *)map_id2bl(sd->npc_id);
  17729. if (stat_point > battle_config.max_third_parameter) {
  17730. ShowError("npcskill: stat point exceeded maximum of %d.\n",battle_config.max_third_parameter );
  17731. return SCRIPT_CMD_FAILURE;
  17732. }
  17733. if (npc_level > MAX_LEVEL) {
  17734. ShowError("npcskill: level exceeded maximum of %d.\n", MAX_LEVEL);
  17735. return SCRIPT_CMD_FAILURE;
  17736. }
  17737. if (sd == NULL || nd == NULL) { //ain't possible, but I don't trust people.
  17738. return SCRIPT_CMD_FAILURE;
  17739. }
  17740. nd->level = npc_level;
  17741. nd->stat_point = stat_point;
  17742. if (!nd->status.hp)
  17743. status_calc_npc(nd, SCO_FIRST);
  17744. else
  17745. status_calc_npc(nd, SCO_NONE);
  17746. if (skill_get_inf(skill_id)&INF_GROUND_SKILL)
  17747. unit_skilluse_pos(&nd->bl, sd->bl.x, sd->bl.y, skill_id, skill_level);
  17748. else
  17749. unit_skilluse_id(&nd->bl, sd->bl.id, skill_id, skill_level);
  17750. return SCRIPT_CMD_SUCCESS;
  17751. }
  17752. /* Consumes an item.
  17753. * consumeitem <item id>{,<char_id>};
  17754. * consumeitem "<item name>"{,<char_id>};
  17755. * @param item: Item ID or name
  17756. */
  17757. BUILDIN_FUNC(consumeitem)
  17758. {
  17759. TBL_PC *sd;
  17760. struct script_data *data;
  17761. struct item_data *item_data;
  17762. if (!script_charid2sd(3, sd))
  17763. return SCRIPT_CMD_FAILURE;
  17764. data = script_getdata( st, 2 );
  17765. get_val( st, data );
  17766. if( data_isstring( data ) ){
  17767. const char *name = conv_str( st, data );
  17768. if( ( item_data = itemdb_searchname( name ) ) == NULL ){
  17769. ShowError( "buildin_consumeitem: Nonexistant item %s requested.\n", name );
  17770. return SCRIPT_CMD_FAILURE;
  17771. }
  17772. }else if( data_isint( data ) ){
  17773. unsigned short nameid = conv_num( st, data );
  17774. if( ( item_data = itemdb_exists( nameid ) ) == NULL ){
  17775. ShowError("buildin_consumeitem: Nonexistant item %hu requested.\n", nameid );
  17776. return SCRIPT_CMD_FAILURE;
  17777. }
  17778. }else{
  17779. ShowError("buildin_consumeitem: invalid data type for argument #1 (%d).\n", data->type );
  17780. return SCRIPT_CMD_FAILURE;
  17781. }
  17782. run_script( item_data->script, 0, sd->bl.id, 0 );
  17783. return SCRIPT_CMD_SUCCESS;
  17784. }
  17785. /** Makes a player sit/stand.
  17786. * sit {"<character name>"};
  17787. * stand {"<character name>"};
  17788. * Note: Use readparam(Sitting) which returns 1 or 0 (sitting or standing).
  17789. * @param name: Player name that will be invoked
  17790. */
  17791. BUILDIN_FUNC(sit)
  17792. {
  17793. TBL_PC *sd;
  17794. if( script_hasdata(st, 2) )
  17795. sd = map_nick2sd(script_getstr(st, 2));
  17796. else
  17797. sd = script_rid2sd(st);
  17798. if( sd == NULL)
  17799. return SCRIPT_CMD_FAILURE;
  17800. if( !pc_issit(sd) ) {
  17801. pc_setsit(sd);
  17802. skill_sit(sd, 1);
  17803. clif_sitting(&sd->bl);
  17804. }
  17805. return SCRIPT_CMD_SUCCESS;
  17806. }
  17807. /** Makes player to stand
  17808. * @param name: Player name that will be set
  17809. */
  17810. BUILDIN_FUNC(stand)
  17811. {
  17812. TBL_PC *sd;
  17813. if( script_hasdata(st, 2) )
  17814. sd = map_nick2sd(script_getstr(st, 2));
  17815. else
  17816. sd = script_rid2sd(st);
  17817. if( sd == NULL)
  17818. return SCRIPT_CMD_FAILURE;
  17819. if( pc_issit(sd) && pc_setstand(sd, false)) {
  17820. skill_sit(sd, 0);
  17821. clif_standing(&sd->bl);
  17822. }
  17823. return SCRIPT_CMD_SUCCESS;
  17824. }
  17825. /** Creates an array of bounded item IDs
  17826. * countbound {<type>{,<char_id>}};
  17827. * @param type: 0 - All bound items; 1 - Account Bound; 2 - Guild Bound; 3 - Party Bound
  17828. * @return amt: Amount of items found
  17829. */
  17830. BUILDIN_FUNC(countbound)
  17831. {
  17832. int i, type, j = 0, k = 0;
  17833. TBL_PC *sd;
  17834. if (!script_charid2sd(3,sd))
  17835. return SCRIPT_CMD_FAILURE;
  17836. type = script_getnum(st,2);
  17837. for( i = 0; i < MAX_INVENTORY; i ++ ) {
  17838. if( sd->inventory.u.items_inventory[i].nameid > 0 && (
  17839. (!type && sd->inventory.u.items_inventory[i].bound) || (type && sd->inventory.u.items_inventory[i].bound == type)
  17840. ))
  17841. {
  17842. pc_setreg(sd,reference_uid(add_str("@bound_items"), k),sd->inventory.u.items_inventory[i].nameid);
  17843. k++;
  17844. j += sd->inventory.u.items_inventory[i].amount;
  17845. }
  17846. }
  17847. script_pushint(st,j);
  17848. return SCRIPT_CMD_SUCCESS;
  17849. }
  17850. /** Creates new party
  17851. * party_create "<party name>"{,<char id>{,<item share>{,<item share type>}}};
  17852. * @param party_name: String of party name that will be created
  17853. * @param char_id: Chara that will be leader of this new party. If no char_id specified, the invoker will be party leader
  17854. * @param item_share: 0-Each Take. 1-Party Share
  17855. * @param item_share_type: 0-Each Take. 1-Even Share
  17856. * @return val: Result value
  17857. * -3 - party name is exist
  17858. * -2 - player is in party already
  17859. * -1 - player is not found
  17860. * 0 - unknown error
  17861. * 1 - success, will return party id $@party_create_id
  17862. */
  17863. BUILDIN_FUNC(party_create)
  17864. {
  17865. char party_name[NAME_LENGTH];
  17866. int item1 = 0, item2 = 0;
  17867. TBL_PC *sd = NULL;
  17868. if (!script_charid2sd(3, sd)) {
  17869. script_pushint(st,-1);
  17870. return SCRIPT_CMD_FAILURE;
  17871. }
  17872. if( sd->status.party_id ) {
  17873. script_pushint(st,-2);
  17874. return SCRIPT_CMD_FAILURE;
  17875. }
  17876. safestrncpy(party_name,script_getstr(st,2),NAME_LENGTH);
  17877. trim(party_name);
  17878. if( party_searchname(party_name) ) {
  17879. script_pushint(st,-3);
  17880. return SCRIPT_CMD_FAILURE;
  17881. }
  17882. if( script_getnum(st,4) )
  17883. item1 = 1;
  17884. if( script_getnum(st,5) )
  17885. item2 = 1;
  17886. party_create_byscript = 1;
  17887. script_pushint(st,party_create(sd,party_name,item1,item2));
  17888. return SCRIPT_CMD_SUCCESS;
  17889. }
  17890. /** Adds player to specified party
  17891. * party_addmember <party id>,<char id>;
  17892. * @param party_id: The party that will be entered by player
  17893. * @param char_id: Char id of player that will be joined to the party
  17894. * @return val: Result value
  17895. * -5 - another character of the same account is in the party
  17896. * -4 - party is full
  17897. * -3 - party is not found
  17898. * -2 - player is in party already
  17899. * -1 - player is not found
  17900. * 0 - unknown error
  17901. * 1 - success
  17902. */
  17903. BUILDIN_FUNC(party_addmember)
  17904. {
  17905. int party_id = script_getnum(st,2);
  17906. TBL_PC *sd;
  17907. struct party_data *party;
  17908. if( !(sd = map_charid2sd(script_getnum(st,3))) ) {
  17909. script_pushint(st,-1);
  17910. return SCRIPT_CMD_FAILURE;
  17911. }
  17912. if( sd->status.party_id ) {
  17913. script_pushint(st,-2);
  17914. return SCRIPT_CMD_FAILURE;
  17915. }
  17916. if( !(party = party_search(party_id)) ) {
  17917. script_pushint(st,-3);
  17918. return SCRIPT_CMD_FAILURE;
  17919. }
  17920. if (battle_config.block_account_in_same_party) {
  17921. int i;
  17922. ARR_FIND(0, MAX_PARTY, i, party->party.member[i].account_id == sd->status.account_id);
  17923. if (i < MAX_PARTY) {
  17924. script_pushint(st,-5);
  17925. return SCRIPT_CMD_FAILURE;
  17926. }
  17927. }
  17928. if( party->party.count >= MAX_PARTY ) {
  17929. script_pushint(st,-4);
  17930. return SCRIPT_CMD_FAILURE;
  17931. }
  17932. sd->party_invite = party_id;
  17933. script_pushint(st,party_add_member(party_id,sd));
  17934. return SCRIPT_CMD_SUCCESS;
  17935. }
  17936. /** Removes player from his/her party. If party_id and char_id is empty remove the invoker from his/her party
  17937. * party_delmember {<char id>,<party_id>};
  17938. * @param: char_id
  17939. * @param: party_id
  17940. * @return val: Result value
  17941. * -3 - player is not in party
  17942. * -2 - party is not found
  17943. * -1 - player is not found
  17944. * 0 - unknown error
  17945. * 1 - success
  17946. */
  17947. BUILDIN_FUNC(party_delmember)
  17948. {
  17949. TBL_PC *sd = NULL;
  17950. if( !script_hasdata(st,2) && !script_hasdata(st,3) && !(sd = script_rid2sd(st)) ) {
  17951. script_pushint(st,-1);
  17952. return SCRIPT_CMD_FAILURE;
  17953. }
  17954. if( sd || (script_getnum(st,2) && (sd = map_charid2sd(script_getnum(st,2)))) )
  17955. script_pushint(st,party_removemember2(sd,0,0));
  17956. else
  17957. script_pushint(st,party_removemember2(NULL,script_getnum(st,2),script_getnum(st,3)));
  17958. return SCRIPT_CMD_SUCCESS;
  17959. }
  17960. /** Changes party leader of specified party (even the leader is offline)
  17961. * party_changeleader <party id>,<char id>;
  17962. * @param party_id: ID of party
  17963. * @param char_id: Char ID of new leader
  17964. * @return val: Result value
  17965. * -4 - player is party leader already
  17966. * -3 - player is not in this party
  17967. * -2 - player is not found
  17968. * -1 - party is not found
  17969. * 0 - unknown error
  17970. * 1 - success */
  17971. BUILDIN_FUNC(party_changeleader)
  17972. {
  17973. int i, party_id = script_getnum(st,2);
  17974. TBL_PC *sd = NULL;
  17975. TBL_PC *tsd = NULL;
  17976. struct party_data *party = NULL;
  17977. if( !(party = party_search(party_id)) ) {
  17978. script_pushint(st,-1);
  17979. return SCRIPT_CMD_FAILURE;
  17980. }
  17981. if( !(tsd = map_charid2sd(script_getnum(st,3))) ) {
  17982. script_pushint(st,-2);
  17983. return SCRIPT_CMD_FAILURE;
  17984. }
  17985. if( tsd->status.party_id != party_id ) {
  17986. script_pushint(st,-3);
  17987. return SCRIPT_CMD_FAILURE;
  17988. }
  17989. ARR_FIND(0,MAX_PARTY,i,party->party.member[i].leader);
  17990. if( i >= MAX_PARTY ) { //this is should impossible!
  17991. script_pushint(st,0);
  17992. return SCRIPT_CMD_FAILURE;
  17993. }
  17994. if( party->data[i].sd == tsd ) {
  17995. script_pushint(st,-4);
  17996. return SCRIPT_CMD_FAILURE;
  17997. }
  17998. script_pushint(st,party_changeleader(sd,tsd,party));
  17999. return SCRIPT_CMD_SUCCESS;
  18000. }
  18001. /** Changes party option
  18002. * party_changeoption <party id>,<option>,<flag>;
  18003. * @param party_id: ID of party that will be changed
  18004. * @param option: Type of option
  18005. * @return val: -1 - party is not found, 0 - invalid option, 1 - success
  18006. */
  18007. BUILDIN_FUNC(party_changeoption)
  18008. {
  18009. struct party_data *party;
  18010. if( !(party = party_search(script_getnum(st,2))) ) {
  18011. script_pushint(st,-1);
  18012. return SCRIPT_CMD_FAILURE;
  18013. }
  18014. script_pushint(st,party_setoption(party,script_getnum(st,3),script_getnum(st,4)));
  18015. return SCRIPT_CMD_SUCCESS;
  18016. }
  18017. /** Destroys party with party id.
  18018. * party_destroy <party id>;
  18019. * @param party_id: ID of party that will be destroyed
  18020. */
  18021. BUILDIN_FUNC(party_destroy)
  18022. {
  18023. int i;
  18024. struct party_data *party;
  18025. if( !(party = party_search(script_getnum(st,2))) ) {
  18026. script_pushint(st,0);
  18027. return SCRIPT_CMD_FAILURE;
  18028. }
  18029. ARR_FIND(0,MAX_PARTY,i,party->party.member[i].leader);
  18030. if( i >= MAX_PARTY || !party->data[i].sd ) { //leader not online
  18031. int j;
  18032. for( j = 0; j < MAX_PARTY; j++ ) {
  18033. TBL_PC *sd = party->data[j].sd;
  18034. if(sd)
  18035. party_member_withdraw(party->party.party_id,sd->status.account_id,sd->status.char_id,sd->status.name,PARTY_MEMBER_WITHDRAW_LEAVE);
  18036. else if( party->party.member[j].char_id )
  18037. intif_party_leave(party->party.party_id,party->party.member[j].account_id,party->party.member[j].char_id,party->party.member[j].name,PARTY_MEMBER_WITHDRAW_LEAVE);
  18038. }
  18039. party_broken(party->party.party_id);
  18040. script_pushint(st,1);
  18041. }
  18042. else //leader leave = party broken
  18043. script_pushint(st,party_leave(party->data[i].sd));
  18044. return SCRIPT_CMD_SUCCESS;
  18045. }
  18046. /** Checks if a player's client version meets a required version or date.
  18047. * @param type: 0 - check by version number; 1 - check by date
  18048. * @param data: Input
  18049. */
  18050. BUILDIN_FUNC(is_clientver) {
  18051. TBL_PC *sd = NULL;
  18052. int type = script_getnum(st,2);
  18053. int data = script_getnum(st,3);
  18054. int ret = 0;
  18055. if (script_hasdata(st,4))
  18056. sd = map_charid2sd(script_getnum(st,4));
  18057. else
  18058. sd = script_rid2sd(st);
  18059. if (sd == NULL) {
  18060. script_pushint(st,0);
  18061. return SCRIPT_CMD_FAILURE;
  18062. }
  18063. switch(type){
  18064. case 0:
  18065. ret = (sd->packet_ver >= data)?1:0;
  18066. break;
  18067. case 1:
  18068. ret = (sd->packet_ver >= date2version(data))?1:0;
  18069. break;
  18070. }
  18071. script_pushint(st,ret);
  18072. return SCRIPT_CMD_SUCCESS;
  18073. }
  18074. /** Returns various information about a player's VIP status. Need to enable VIP system
  18075. * vip_status <type>,{"<character name>"};
  18076. * @param type: Info type, 1: VIP status, 2: Expired date, 3: Remaining time
  18077. * @param name: Character name (Optional)
  18078. */
  18079. BUILDIN_FUNC(vip_status) {
  18080. #ifdef VIP_ENABLE
  18081. TBL_PC *sd;
  18082. char vip_str[26];
  18083. time_t now = time(NULL);
  18084. int type = script_getnum(st, 2);
  18085. if (script_hasdata(st, 3))
  18086. sd = map_nick2sd(script_getstr(st, 3));
  18087. else
  18088. sd = script_rid2sd(st);
  18089. if (sd == NULL)
  18090. return SCRIPT_CMD_FAILURE;
  18091. switch(type) {
  18092. case 1: // Get VIP status.
  18093. script_pushint(st, pc_isvip(sd));
  18094. break;
  18095. case 2: // Get VIP expire date.
  18096. if (pc_isvip(sd)) {
  18097. time_t viptime = sd->vip.time;
  18098. strftime(vip_str, 24, "%Y-%m-%d %H:%M", localtime(&viptime));
  18099. vip_str[25] = '\0';
  18100. script_pushstrcopy(st, vip_str);
  18101. } else
  18102. script_pushint(st, 0);
  18103. break;
  18104. case 3: // Get remaining time.
  18105. if (pc_isvip(sd)) {
  18106. time_t viptime_remain = sd->vip.time - now;
  18107. int year=0,month=0,day=0,hour=0,min=0,sec=0;
  18108. split_time((int)viptime_remain,&year,&month,&day,&hour,&min,&sec);
  18109. safesnprintf(vip_str,sizeof(vip_str),"%d-%d-%d %d:%d",year,month,day,hour,min);
  18110. script_pushstrcopy(st, vip_str);
  18111. } else
  18112. script_pushint(st, 0);
  18113. break;
  18114. }
  18115. #else
  18116. script_pushint(st, 0);
  18117. #endif
  18118. return SCRIPT_CMD_SUCCESS;
  18119. }
  18120. /** Adds or removes VIP time in minutes. Need to enable VIP system
  18121. * vip_time <time in mn>,{"<character name>"};
  18122. * @param time: VIP duration in minutes. If time < 0 remove time, else add time.
  18123. * @param name: Character name (optional)
  18124. */
  18125. BUILDIN_FUNC(vip_time) {
  18126. #ifdef VIP_ENABLE //would be a pain for scripting npc otherwise
  18127. TBL_PC *sd;
  18128. int viptime = script_getnum(st, 2) * 60; // Convert since it's given in minutes.
  18129. if (script_hasdata(st, 3))
  18130. sd = map_nick2sd(script_getstr(st, 3));
  18131. else
  18132. sd = script_rid2sd(st);
  18133. if (sd == NULL)
  18134. return SCRIPT_CMD_FAILURE;
  18135. chrif_req_login_operation(sd->status.account_id, sd->status.name, CHRIF_OP_LOGIN_VIP, viptime, 7, 0);
  18136. #endif
  18137. return SCRIPT_CMD_SUCCESS;
  18138. }
  18139. /**
  18140. * Turns a player into a monster and optionally can grant a SC attribute effect.
  18141. * montransform <monster name/ID>, <duration>, <sc type>, <val1>, <val2>, <val3>, <val4>;
  18142. * active_transform <monster name/ID>, <duration>, <sc type>, <val1>, <val2>, <val3>, <val4>;
  18143. * @param monster: Monster ID or name
  18144. * @param duration: Transform duration in millisecond (ms)
  18145. * @param sc_type: Type of SC that will be affected during the transformation
  18146. * @param val1: Value for SC
  18147. * @param val2: Value for SC
  18148. * @param val3: Value for SC
  18149. * @param val4: Value for SC
  18150. * @author: malufett
  18151. */
  18152. BUILDIN_FUNC(montransform) {
  18153. TBL_PC *sd;
  18154. enum sc_type type;
  18155. int tick, mob_id, val1, val2, val3, val4;
  18156. struct script_data *data;
  18157. val1 = val2 = val3 = val4 = 0;
  18158. if( (sd = script_rid2sd(st)) == NULL )
  18159. return SCRIPT_CMD_FAILURE;
  18160. data = script_getdata(st, 2);
  18161. get_val(st, data); // Convert into value in case of a variable
  18162. if( data_isstring(data) )
  18163. mob_id = mobdb_searchname(script_getstr(st, 2));
  18164. else
  18165. mob_id = mobdb_checkid(script_getnum(st, 2));
  18166. tick = script_getnum(st, 3);
  18167. if (script_hasdata(st, 4))
  18168. type = (sc_type)script_getnum(st, 4);
  18169. else
  18170. type = SC_NONE;
  18171. if (mob_id == 0) {
  18172. if( data_isstring(data) )
  18173. ShowWarning("buildin_montransform: Attempted to use non-existing monster '%s'.\n", script_getstr(st, 2));
  18174. else
  18175. ShowWarning("buildin_montransform: Attempted to use non-existing monster of ID '%d'.\n", script_getnum(st, 2));
  18176. return SCRIPT_CMD_FAILURE;
  18177. }
  18178. if (mob_id == MOBID_EMPERIUM) {
  18179. ShowWarning("buildin_montransform: Monster 'Emperium' cannot be used.\n");
  18180. return SCRIPT_CMD_FAILURE;
  18181. }
  18182. if (!(type >= SC_NONE && type < SC_MAX)) {
  18183. ShowWarning("buildin_montransform: Unsupported status change id %d\n", type);
  18184. return SCRIPT_CMD_FAILURE;
  18185. }
  18186. if (script_hasdata(st, 5))
  18187. val1 = script_getnum(st, 5);
  18188. if (script_hasdata(st, 6))
  18189. val2 = script_getnum(st, 6);
  18190. if (script_hasdata(st, 7))
  18191. val3 = script_getnum(st, 7);
  18192. if (script_hasdata(st, 8))
  18193. val4 = script_getnum(st, 8);
  18194. if (tick != 0) {
  18195. if (battle_config.mon_trans_disable_in_gvg && map_flag_gvg2(sd->bl.m)) {
  18196. clif_displaymessage(sd->fd, msg_txt(sd,731)); // Transforming into monster is not allowed in Guild Wars.
  18197. return SCRIPT_CMD_FAILURE;
  18198. }
  18199. if (sd->disguise){
  18200. clif_displaymessage(sd->fd, msg_txt(sd,729)); // Cannot transform into monster while in disguise.
  18201. return SCRIPT_CMD_FAILURE;
  18202. }
  18203. if (!strcmp(script_getfuncname(st), "active_transform")) {
  18204. status_change_end(&sd->bl, SC_ACTIVE_MONSTER_TRANSFORM, INVALID_TIMER); // Clear previous
  18205. sc_start2(NULL, &sd->bl, SC_ACTIVE_MONSTER_TRANSFORM, 100, mob_id, type, tick);
  18206. } else {
  18207. status_change_end(&sd->bl, SC_MONSTER_TRANSFORM, INVALID_TIMER); // Clear previous
  18208. sc_start2(NULL, &sd->bl, SC_MONSTER_TRANSFORM, 100, mob_id, type, tick);
  18209. }
  18210. if (type != SC_NONE)
  18211. sc_start4(NULL, &sd->bl, type, 100, val1, val2, val3, val4, tick);
  18212. }
  18213. return SCRIPT_CMD_SUCCESS;
  18214. }
  18215. /**
  18216. * Attach script to player for certain duration
  18217. * bonus_script "<script code>",<duration>{,<flag>{,<type>{,<status_icon>{,<char_id>}}}};
  18218. * @param "script code"
  18219. * @param duration
  18220. * @param flag
  18221. * @param icon
  18222. * @param char_id
  18223. * @author [Cydh]
  18224. **/
  18225. BUILDIN_FUNC(bonus_script) {
  18226. uint16 flag = 0;
  18227. int16 icon = SI_BLANK;
  18228. uint32 dur;
  18229. uint8 type = 0;
  18230. TBL_PC* sd;
  18231. const char *script_str = NULL;
  18232. struct s_bonus_script_entry *entry = NULL;
  18233. if (script_hasdata(st,7)) {
  18234. if (!(sd = map_charid2sd(script_getnum(st,7)))) {
  18235. ShowError("buildin_bonus_script: Player CID=%d is not found.\n", script_getnum(st,7));
  18236. return SCRIPT_CMD_FAILURE;
  18237. }
  18238. }
  18239. else
  18240. sd = script_rid2sd(st);
  18241. if (sd == NULL)
  18242. return SCRIPT_CMD_FAILURE;
  18243. script_str = script_getstr(st,2);
  18244. dur = 1000 * abs(script_getnum(st,3));
  18245. FETCH(4, flag);
  18246. FETCH(5, type);
  18247. FETCH(6, icon);
  18248. // No Script string, No Duration!
  18249. if (script_str[0] == '\0' || !dur) {
  18250. ShowError("buildin_bonus_script: Invalid! Script: \"%s\". Duration: %d\n", script_str, dur);
  18251. return SCRIPT_CMD_FAILURE;
  18252. }
  18253. if (strlen(script_str) >= MAX_BONUS_SCRIPT_LENGTH) {
  18254. ShowError("buildin_bonus_script: Script string to long: \"%s\".\n", script_str);
  18255. return SCRIPT_CMD_FAILURE;
  18256. }
  18257. if (icon <= SI_BLANK || icon >= SI_MAX)
  18258. icon = SI_BLANK;
  18259. if ((entry = pc_bonus_script_add(sd, script_str, dur, (enum si_type)icon, flag, type))) {
  18260. linkdb_insert(&sd->bonus_script.head, (void *)((intptr_t)entry), entry);
  18261. status_calc_pc(sd,SCO_NONE);
  18262. }
  18263. return SCRIPT_CMD_SUCCESS;
  18264. }
  18265. /**
  18266. * Removes all bonus script from player
  18267. * bonus_script_clear {<flag>,{<char_id>}};
  18268. * @param flag 0 - Except permanent bonus, 1 - With permanent bonus
  18269. * @param char_id Clear script from this character
  18270. * @author [Cydh]
  18271. */
  18272. BUILDIN_FUNC(bonus_script_clear) {
  18273. TBL_PC* sd;
  18274. bool flag = false;
  18275. if (script_hasdata(st,2))
  18276. flag = script_getnum(st,2);
  18277. if (script_hasdata(st,3))
  18278. sd = map_charid2sd(script_getnum(st,3));
  18279. else
  18280. sd = script_rid2sd(st);
  18281. if (sd == NULL)
  18282. return SCRIPT_CMD_FAILURE;
  18283. pc_bonus_script_clear(sd,(flag ? BSF_PERMANENT : BSF_REM_ALL));
  18284. return SCRIPT_CMD_SUCCESS;
  18285. }
  18286. /** Allows player to use atcommand while talking with NPC
  18287. * enable_command;
  18288. * @author [Cydh], [Kichi]
  18289. */
  18290. BUILDIN_FUNC(enable_command) {
  18291. TBL_PC* sd = script_rid2sd(st);
  18292. if (!sd)
  18293. return SCRIPT_CMD_FAILURE;
  18294. sd->state.disable_atcommand_on_npc = 0;
  18295. return SCRIPT_CMD_SUCCESS;
  18296. }
  18297. /** Prevents player to use atcommand while talking with NPC
  18298. * disable_command;
  18299. * @author [Cydh], [Kichi]
  18300. */
  18301. BUILDIN_FUNC(disable_command) {
  18302. TBL_PC* sd = script_rid2sd(st);
  18303. if (!sd)
  18304. return SCRIPT_CMD_FAILURE;
  18305. sd->state.disable_atcommand_on_npc = 1;
  18306. return SCRIPT_CMD_SUCCESS;
  18307. }
  18308. /** Get the information of the members of a guild by type.
  18309. * getguildmember <guild_id>{,<type>};
  18310. * @param guild_id: ID of guild
  18311. * @param type: Type of option (optional)
  18312. */
  18313. BUILDIN_FUNC(getguildmember)
  18314. {
  18315. struct guild *g = NULL;
  18316. uint8 j = 0;
  18317. g = guild_search(script_getnum(st,2));
  18318. if (g) {
  18319. uint8 i, type = 0;
  18320. struct script_data *data = NULL;
  18321. char *varname = NULL;
  18322. if (script_hasdata(st,3))
  18323. type = script_getnum(st,3);
  18324. if (script_hasdata(st,4)) {
  18325. data = script_getdata(st, 4);
  18326. if (!data_isreference(data)) {
  18327. ShowError("buildin_getguildmember: Error in argument! Please give a variable to store values in.\n");
  18328. return SCRIPT_CMD_FAILURE;
  18329. }
  18330. varname = reference_getname(data);
  18331. if (type <= 0 && varname[strlen(varname)-1] != '$') {
  18332. ShowError("buildin_getguildmember: The array %s is not string type.\n", varname);
  18333. return SCRIPT_CMD_FAILURE;
  18334. }
  18335. }
  18336. for (i = 0; i < MAX_GUILD; i++) {
  18337. if (g->member[i].account_id) {
  18338. switch (type) {
  18339. case 2:
  18340. if (data)
  18341. setd_sub(st, NULL, varname, j, (void *)__64BPRTSIZE(g->member[i].account_id), data->ref);
  18342. else
  18343. mapreg_setreg(reference_uid(add_str("$@guildmemberaid"), j),g->member[i].account_id);
  18344. break;
  18345. case 1:
  18346. if (data)
  18347. setd_sub(st, NULL, varname, j, (void *)__64BPRTSIZE(g->member[i].char_id), data->ref);
  18348. else
  18349. mapreg_setreg(reference_uid(add_str("$@guildmembercid"), j), g->member[i].char_id);
  18350. break;
  18351. default:
  18352. if (data)
  18353. setd_sub(st, NULL, varname, j, (void *)__64BPRTSIZE(g->member[i].name), data->ref);
  18354. else
  18355. mapreg_setregstr(reference_uid(add_str("$@guildmembername$"), j), g->member[i].name);
  18356. break;
  18357. }
  18358. j++;
  18359. }
  18360. }
  18361. }
  18362. mapreg_setreg(add_str("$@guildmembercount"), j);
  18363. script_pushint(st, j);
  18364. return SCRIPT_CMD_SUCCESS;
  18365. }
  18366. /** Adds spirit ball to player for 'duration' in milisecond
  18367. * addspiritball <count>,<duration>{,<char_id>};
  18368. * @param count How many spirit ball will be added
  18369. * @param duration How long spiritball is active until it disappears
  18370. * @param char_id Target player (Optional)
  18371. * @author [Cydh]
  18372. */
  18373. BUILDIN_FUNC(addspiritball) {
  18374. uint8 i, count = script_getnum(st,2);
  18375. uint16 duration = script_getnum(st,3);
  18376. struct map_session_data *sd = NULL;
  18377. if (count == 0)
  18378. return SCRIPT_CMD_SUCCESS;
  18379. if (script_hasdata(st,4)) {
  18380. if (!script_isstring(st,4))
  18381. sd = map_charid2sd(script_getnum(st,4));
  18382. else
  18383. sd = map_nick2sd(script_getstr(st,4));
  18384. }
  18385. else
  18386. sd = script_rid2sd(st);
  18387. if (!sd)
  18388. return SCRIPT_CMD_FAILURE;
  18389. for (i = 0; i < count; i++)
  18390. pc_addspiritball(sd,duration,10);
  18391. return SCRIPT_CMD_SUCCESS;
  18392. }
  18393. /** Deletes the spirit ball(s) from player
  18394. * delspiritball <count>{,<char_id>};
  18395. * @param count How many spirit ball will be deleted
  18396. * @param char_id Target player (Optional)
  18397. * @author [Cydh]
  18398. */
  18399. BUILDIN_FUNC(delspiritball) {
  18400. uint8 count = script_getnum(st,2);
  18401. struct map_session_data *sd = NULL;
  18402. if (count == 0)
  18403. count = 1;
  18404. if (script_hasdata(st,3)) {
  18405. if (!script_isstring(st,3))
  18406. sd = map_charid2sd(script_getnum(st,3));
  18407. else
  18408. sd = map_nick2sd(script_getstr(st,3));
  18409. }
  18410. else
  18411. sd = script_rid2sd(st);
  18412. if (!sd)
  18413. return SCRIPT_CMD_FAILURE;
  18414. pc_delspiritball(sd,count,0);
  18415. return SCRIPT_CMD_SUCCESS;
  18416. }
  18417. /** Counts the spirit ball that player has
  18418. * countspiritball {<char_id>};
  18419. * @param char_id Target player (Optional)
  18420. * @author [Cydh]
  18421. */
  18422. BUILDIN_FUNC(countspiritball) {
  18423. struct map_session_data *sd;
  18424. if (script_hasdata(st,2)) {
  18425. if (!script_isstring(st,2))
  18426. sd = map_charid2sd(script_getnum(st,2));
  18427. else
  18428. sd = map_nick2sd(script_getstr(st,2));
  18429. }
  18430. else
  18431. sd = script_rid2sd(st);
  18432. if (!sd)
  18433. return SCRIPT_CMD_FAILURE;
  18434. script_pushint(st,sd->spiritball);
  18435. return SCRIPT_CMD_SUCCESS;
  18436. }
  18437. /** Merges separated stackable items because of guid
  18438. * mergeitem {<char_id>};
  18439. * @param char_id Char ID (Optional)
  18440. * @author [Cydh]
  18441. */
  18442. BUILDIN_FUNC(mergeitem) {
  18443. struct map_session_data *sd;
  18444. if (!script_charid2sd(2, sd))
  18445. return SCRIPT_CMD_FAILURE;
  18446. clif_merge_item_open(sd);
  18447. return SCRIPT_CMD_SUCCESS;
  18448. }
  18449. /** Merges separated stackable items because of guid (Unofficial)
  18450. * mergeitem2 {<item_id>,{<char_id>}};
  18451. * mergeitem2 {"<item name>",{<char_id>}};
  18452. * @param item Item ID/Name for merging specific item (Optional)
  18453. * @author [Cydh]
  18454. */
  18455. BUILDIN_FUNC(mergeitem2) {
  18456. struct map_session_data *sd;
  18457. struct item *items = NULL;
  18458. uint16 i, count = 0, nameid = 0;
  18459. if (!script_charid2sd(3, sd))
  18460. return SCRIPT_CMD_FAILURE;
  18461. if (script_hasdata(st, 2)) {
  18462. struct script_data *data = script_getdata(st, 2);
  18463. get_val(st, data);
  18464. if (data_isstring(data)) {// "<item name>"
  18465. const char *name = conv_str(st,data);
  18466. struct item_data *id;
  18467. if (!(id = itemdb_searchname(name))) {
  18468. ShowError("buildin_mergeitem2: Nonexistant item %s requested.\n", name);
  18469. script_pushint(st, count);
  18470. return SCRIPT_CMD_FAILURE;
  18471. }
  18472. nameid = id->nameid;
  18473. }
  18474. else if (data_isint(data)) {// <item id>
  18475. nameid = conv_num(st,data);
  18476. if (!itemdb_exists(nameid)) {
  18477. ShowError("buildin_mergeitem: Nonexistant item %d requested.\n", nameid);
  18478. script_pushint(st, count);
  18479. return SCRIPT_CMD_FAILURE;
  18480. }
  18481. }
  18482. }
  18483. for (i = 0; i < MAX_INVENTORY; i++) {
  18484. struct item *it = &sd->inventory.u.items_inventory[i];
  18485. if (!it || !it->unique_id || it->expire_time || !itemdb_isstackable(it->nameid))
  18486. continue;
  18487. if ((!nameid || (nameid == it->nameid))) {
  18488. uint8 k;
  18489. if (!count) {
  18490. CREATE(items, struct item, 1);
  18491. memcpy(&items[count++], it, sizeof(struct item));
  18492. pc_delitem(sd, i, it->amount, 0, 0, LOG_TYPE_NPC);
  18493. continue;
  18494. }
  18495. for (k = 0; k < count; k++) {
  18496. // Find Match
  18497. if (&items[k] && items[k].nameid == it->nameid && items[k].bound == it->bound && memcmp(items[k].card, it->card, sizeof(it->card)) == 0) {
  18498. items[k].amount += it->amount;
  18499. pc_delitem(sd, i, it->amount, 0, 0, LOG_TYPE_NPC);
  18500. break;
  18501. }
  18502. }
  18503. if (k >= count) {
  18504. // New entry
  18505. RECREATE(items, struct item, count+1);
  18506. memcpy(&items[count++], it, sizeof(struct item));
  18507. pc_delitem(sd, i, it->amount, 0, 0, LOG_TYPE_NPC);
  18508. }
  18509. }
  18510. }
  18511. if (!items) // Nothing todo here
  18512. return SCRIPT_CMD_SUCCESS;
  18513. // Retrieve the items
  18514. for (i = 0; i < count; i++) {
  18515. uint8 flag = 0;
  18516. if (!&items[i])
  18517. continue;
  18518. items[i].id = 0;
  18519. items[i].unique_id = 0;
  18520. if ((flag = pc_additem(sd, &items[i], items[i].amount, LOG_TYPE_NPC)))
  18521. clif_additem(sd, i, items[i].amount, flag);
  18522. }
  18523. aFree(items);
  18524. script_pushint(st, count);
  18525. return SCRIPT_CMD_SUCCESS;
  18526. }
  18527. /**
  18528. * Update an entry from NPC shop.
  18529. * npcshopupdate "<name>",<item_id>,<price>{,<stock>}
  18530. **/
  18531. BUILDIN_FUNC(npcshopupdate) {
  18532. const char* npcname = script_getstr(st, 2);
  18533. struct npc_data* nd = npc_name2id(npcname);
  18534. uint16 nameid = script_getnum(st, 3);
  18535. int price = script_getnum(st, 4);
  18536. #if PACKETVER >= 20131223
  18537. uint16 stock = script_hasdata(st,5) ? script_getnum(st,5) : 0;
  18538. #endif
  18539. int i;
  18540. if( !nd || ( nd->subtype != NPCTYPE_SHOP && nd->subtype != NPCTYPE_CASHSHOP && nd->subtype != NPCTYPE_ITEMSHOP && nd->subtype != NPCTYPE_POINTSHOP && nd->subtype != NPCTYPE_MARKETSHOP ) ) { // Not found.
  18541. script_pushint(st,0);
  18542. return SCRIPT_CMD_FAILURE;
  18543. }
  18544. if (!nd->u.shop.count) {
  18545. ShowError("buildin_npcshopupdate: Attempt to update empty shop from '%s'.\n", nd->exname);
  18546. script_pushint(st,0);
  18547. return SCRIPT_CMD_FAILURE;
  18548. }
  18549. for (i = 0; i < nd->u.shop.count; i++) {
  18550. if (nd->u.shop.shop_item[i].nameid == nameid) {
  18551. if (price != 0)
  18552. nd->u.shop.shop_item[i].value = price;
  18553. #if PACKETVER >= 20131223
  18554. if (nd->subtype == NPCTYPE_MARKETSHOP) {
  18555. nd->u.shop.shop_item[i].qty = stock;
  18556. npc_market_tosql(nd->exname, &nd->u.shop.shop_item[i]);
  18557. }
  18558. #endif
  18559. }
  18560. }
  18561. script_pushint(st,1);
  18562. return SCRIPT_CMD_SUCCESS;
  18563. }
  18564. // Clan System
  18565. BUILDIN_FUNC(clan_join){
  18566. struct map_session_data *sd;
  18567. int clan_id = script_getnum(st,2);
  18568. if( !script_charid2sd( 3, sd ) ){
  18569. script_pushint(st, false);
  18570. return SCRIPT_CMD_FAILURE;
  18571. }
  18572. if( clan_member_join( sd, clan_id, sd->status.account_id, sd->status.char_id ) )
  18573. script_pushint(st, true);
  18574. else
  18575. script_pushint(st, false);
  18576. return SCRIPT_CMD_SUCCESS;
  18577. }
  18578. BUILDIN_FUNC(clan_leave){
  18579. struct map_session_data *sd;
  18580. if( !script_charid2sd( 2, sd ) ){
  18581. script_pushint(st, false);
  18582. return SCRIPT_CMD_FAILURE;
  18583. }
  18584. if( clan_member_leave( sd, sd->status.clan_id, sd->status.account_id, sd->status.char_id ) )
  18585. script_pushint(st, true);
  18586. else
  18587. script_pushint(st, false);
  18588. return SCRIPT_CMD_SUCCESS;
  18589. }
  18590. /**
  18591. * Get rid from running script.
  18592. * getattachedrid();
  18593. **/
  18594. BUILDIN_FUNC(getattachedrid) {
  18595. script_pushint(st, st->rid);
  18596. return SCRIPT_CMD_SUCCESS;
  18597. }
  18598. /**
  18599. * Get variable from a Player
  18600. * getvar <variable>,<char_id>;
  18601. */
  18602. BUILDIN_FUNC(getvar) {
  18603. int char_id = script_getnum(st, 3);
  18604. struct map_session_data *sd = map_charid2sd(char_id);
  18605. struct script_data *data = NULL;
  18606. const char *name = NULL;
  18607. if (!sd) {
  18608. ShowError("buildin_getvar: No player found with char id '%d'.\n", char_id);
  18609. return SCRIPT_CMD_FAILURE;
  18610. }
  18611. data = script_getdata(st, 2);
  18612. if (!data_isreference(data)) {
  18613. ShowError("buildin_getvar: Not a variable\n");
  18614. script_reportdata(data);
  18615. script_pushnil(st);
  18616. st->state = END;
  18617. return SCRIPT_CMD_FAILURE;
  18618. }
  18619. name = reference_getname(data);
  18620. if (reference_toparam(data)) {
  18621. ShowError("buildin_getvar: '%s' is a parameter - please use readparam instead\n", name);
  18622. script_reportdata(data);
  18623. script_pushnil(st);
  18624. st->state = END;
  18625. return SCRIPT_CMD_FAILURE;
  18626. }
  18627. if (name[0] == '.' || name[0] == '$' || name[0] == '\'') { // Not a PC variable
  18628. ShowError("buildin_getvar: Invalid scope (not PC variable)\n");
  18629. script_reportdata(data);
  18630. script_pushnil(st);
  18631. st->state = END;
  18632. return SCRIPT_CMD_FAILURE;
  18633. }
  18634. get_val_(st, data, sd);
  18635. if (data_isint(data))
  18636. script_pushint(st, conv_num_(st, data, sd));
  18637. else
  18638. script_pushstrcopy(st, conv_str_(st, data, sd));
  18639. push_val2(st->stack, C_NAME, reference_getuid(data), reference_getref(data));
  18640. return SCRIPT_CMD_SUCCESS;
  18641. }
  18642. /**
  18643. * Display script message
  18644. * showscript "<message>"{,<GID>};
  18645. **/
  18646. BUILDIN_FUNC(showscript) {
  18647. struct block_list *bl = NULL;
  18648. const char *msg = script_getstr(st,2);
  18649. int id = 0;
  18650. if (script_hasdata(st,3)) {
  18651. id = script_getnum(st,3);
  18652. bl = map_id2bl(id);
  18653. }
  18654. else {
  18655. bl = st->rid ? map_id2bl(st->rid) : map_id2bl(st->oid);
  18656. }
  18657. if (!bl) {
  18658. ShowError("buildin_showscript: Script not attached. (id=%d, rid=%d, oid=%d)\n", id, st->rid, st->oid);
  18659. script_pushint(st,0);
  18660. return SCRIPT_CMD_FAILURE;
  18661. }
  18662. clif_showscript(bl, msg);
  18663. script_pushint(st,1);
  18664. return SCRIPT_CMD_SUCCESS;
  18665. }
  18666. /**
  18667. * Ignore the SECURE_NPCTIMEOUT function.
  18668. * ignoretimeout <flag>{,<char_id>};
  18669. */
  18670. BUILDIN_FUNC(ignoretimeout)
  18671. {
  18672. #ifdef SECURE_NPCTIMEOUT
  18673. struct map_session_data *sd = NULL;
  18674. if (script_hasdata(st,3)) {
  18675. if (!script_isstring(st,3))
  18676. sd = map_charid2sd(script_getnum(st,3));
  18677. else
  18678. sd = map_nick2sd(script_getstr(st,3));
  18679. } else
  18680. sd = script_rid2sd(st);
  18681. if (!sd)
  18682. return SCRIPT_CMD_FAILURE;
  18683. sd->state.ignoretimeout = script_getnum(st,2) > 0;
  18684. #endif
  18685. return SCRIPT_CMD_SUCCESS;
  18686. }
  18687. /**
  18688. * geteleminfo <type>{,<char_id>};
  18689. **/
  18690. BUILDIN_FUNC(geteleminfo) {
  18691. TBL_ELEM *ed = NULL;
  18692. TBL_PC *sd = NULL;
  18693. int type = script_getnum(st,2);
  18694. if (!script_charid2sd(3, sd)) {
  18695. script_pushint(st, 0);
  18696. return SCRIPT_CMD_SUCCESS;
  18697. }
  18698. if (!(ed = sd->ed)) {
  18699. //ShowDebug("buildin_geteleminfo: Player doesn't have Elemental.\n");
  18700. script_pushint(st, 0);
  18701. return SCRIPT_CMD_SUCCESS;
  18702. }
  18703. switch (type) {
  18704. case 0: script_pushint(st, ed->elemental.elemental_id); break;
  18705. case 1: script_pushint(st, ed->bl.id); break;
  18706. default:
  18707. ShowError("buildin_geteleminfo: Invalid type '%d'.\n", type);
  18708. script_pushint(st, 0);
  18709. return SCRIPT_CMD_FAILURE;
  18710. }
  18711. return SCRIPT_CMD_SUCCESS;
  18712. }
  18713. /**
  18714. * Set the quest info of quest_id only showed on player in level range.
  18715. * setquestinfo_level <quest_id>,<min_level>,<max_level>
  18716. * @author [Cydh]
  18717. **/
  18718. BUILDIN_FUNC(setquestinfo_level) {
  18719. TBL_NPC* nd = map_id2nd(st->oid);
  18720. int quest_id = script_getnum(st, 2);
  18721. struct questinfo *qi = map_has_questinfo(nd->bl.m, nd, quest_id);
  18722. if (!qi) {
  18723. ShowError("buildin_setquestinfo_level: Quest with ID '%d' is not defined yet.\n", quest_id);
  18724. return SCRIPT_CMD_FAILURE;
  18725. }
  18726. qi->min_level = script_getnum(st, 3);
  18727. qi->max_level = script_getnum(st, 4);
  18728. if (!qi->max_level)
  18729. qi->max_level = MAX_LEVEL;
  18730. return SCRIPT_CMD_SUCCESS;
  18731. }
  18732. /**
  18733. * Set the quest info of quest_id only showed for player that has quest criteria
  18734. * setquestinfo_req <quest_id>,<quest_req_id>,<state>{,<quest_req_id>,<state>,...};
  18735. * @author [Cydh]
  18736. **/
  18737. BUILDIN_FUNC(setquestinfo_req) {
  18738. TBL_NPC* nd = map_id2nd(st->oid);
  18739. int quest_id = script_getnum(st, 2);
  18740. struct questinfo *qi = map_has_questinfo(nd->bl.m, nd, quest_id);
  18741. uint8 i = 0;
  18742. uint8 num = script_lastdata(st);
  18743. if (!qi) {
  18744. ShowError("buildin_setquestinfo_req: Quest with ID '%d' is not defined yet.\n", quest_id);
  18745. return SCRIPT_CMD_FAILURE;
  18746. }
  18747. if (quest_search(quest_id) == &quest_dummy) {
  18748. ShowError("buildin_setquestinfo_req: Quest with ID '%d' is not found in Quest DB.\n", quest_id);
  18749. return SCRIPT_CMD_FAILURE;
  18750. }
  18751. if (num%2) {
  18752. ShowError("buildin_setquestinfo_req: Odd number of parameters(%d) - pairs of requirements are expected.\n", num-2);
  18753. return SCRIPT_CMD_FAILURE;
  18754. }
  18755. for (i = 3; i <= num; i += 2) {
  18756. RECREATE(qi->req, struct questinfo_req, qi->req_count+1);
  18757. qi->req[qi->req_count].quest_id = script_getnum(st, i);
  18758. qi->req[qi->req_count].state = (script_getnum(st, i+1) >= 2) ? 2 : (script_getnum(st, i+1) <= 0) ? 0 : 1;
  18759. qi->req_count++;
  18760. }
  18761. return SCRIPT_CMD_SUCCESS;
  18762. }
  18763. /**
  18764. * Set the quest info of quest_id only showed for player that has specified Job
  18765. * setquestinfo_job <quest_id>,<job>{,<job>...};
  18766. * @author [Cydh]
  18767. **/
  18768. BUILDIN_FUNC(setquestinfo_job) {
  18769. TBL_NPC* nd = map_id2nd(st->oid);
  18770. int quest_id = script_getnum(st, 2);
  18771. struct questinfo *qi = map_has_questinfo(nd->bl.m, nd, quest_id);
  18772. int job_id = 0;
  18773. uint8 i = 0;
  18774. uint8 num = script_lastdata(st)+1;
  18775. if (!qi) {
  18776. ShowError("buildin_setquestinfo_job: Quest with ID '%d' is not defined yet.\n", quest_id);
  18777. return SCRIPT_CMD_FAILURE;
  18778. }
  18779. for (i = 3; i < num; i++) {
  18780. job_id = script_getnum(st, i);
  18781. if (!pcdb_checkid(job_id)) {
  18782. ShowError("buildin_setquestinfo_job: Invalid job id '%d' in Quest with ID %d.\n", job_id, quest_id);
  18783. continue;
  18784. }
  18785. buildin_questinfo_setjob(qi, job_id);
  18786. }
  18787. return SCRIPT_CMD_SUCCESS;
  18788. }
  18789. /**
  18790. * opendressroom(<flag>{,<char_id>});
  18791. */
  18792. BUILDIN_FUNC(opendressroom)
  18793. {
  18794. #if PACKETVER >= 20150513
  18795. int flag = 1;
  18796. TBL_PC* sd;
  18797. if( script_hasdata(st,2) )
  18798. flag = script_getnum(st,2);
  18799. if (!script_charid2sd(3, sd))
  18800. return SCRIPT_CMD_FAILURE;
  18801. clif_dressing_room(sd, flag);
  18802. return SCRIPT_CMD_SUCCESS;
  18803. #else
  18804. return SCRIPT_CMD_FAILURE;
  18805. #endif
  18806. }
  18807. /**
  18808. * navigateto("<map>"{,<x>,<y>,<flag>,<hide_window>,<monster_id>,<char_id>});
  18809. */
  18810. BUILDIN_FUNC(navigateto){
  18811. #if PACKETVER >= 20111010
  18812. TBL_PC* sd;
  18813. const char *mapname;
  18814. uint16 x = 0, y = 0, monster_id = 0;
  18815. uint8 flag = NAV_KAFRA_AND_AIRSHIP;
  18816. bool hideWindow = true;
  18817. mapname = script_getstr(st,2);
  18818. if( script_hasdata(st,3) )
  18819. x = script_getnum(st,3);
  18820. if( script_hasdata(st,4) )
  18821. y = script_getnum(st,4);
  18822. if( script_hasdata(st,5) )
  18823. flag = (uint8)script_getnum(st,5);
  18824. if( script_hasdata(st,6) )
  18825. hideWindow = script_getnum(st,6) ? true : false;
  18826. if( script_hasdata(st,7) )
  18827. monster_id = script_getnum(st,7);
  18828. if (!script_charid2sd(8, sd))
  18829. return SCRIPT_CMD_FAILURE;
  18830. clif_navigateTo(sd,mapname,x,y,flag,hideWindow,monster_id);
  18831. return SCRIPT_CMD_SUCCESS;
  18832. #else
  18833. return SCRIPT_CMD_FAILURE;
  18834. #endif
  18835. }
  18836. /**
  18837. * adopt("<parent_name>","<baby_name>");
  18838. * adopt(<parent_id>,<baby_id>);
  18839. * https://rathena.org/board/topic/104014-suggestion-add-adopt-or-etc/
  18840. */
  18841. BUILDIN_FUNC(adopt)
  18842. {
  18843. TBL_PC *sd, *b_sd;
  18844. struct script_data *data;
  18845. enum adopt_responses response;
  18846. data = script_getdata(st, 2);
  18847. get_val(st, data);
  18848. if (data_isstring(data)) {
  18849. const char *name = conv_str(st, data);
  18850. sd = map_nick2sd(name);
  18851. if (sd == NULL) {
  18852. ShowError("buildin_adopt: Non-existant parent character %s requested.\n", name);
  18853. return SCRIPT_CMD_FAILURE;
  18854. }
  18855. } else if (data_isint(data)) {
  18856. uint32 char_id = conv_num(st, data);
  18857. sd = map_charid2sd(char_id);
  18858. if (sd == NULL) {
  18859. ShowError("buildin_adopt: Non-existant parent character %d requested.\n", char_id);
  18860. return SCRIPT_CMD_FAILURE;
  18861. }
  18862. } else {
  18863. ShowError("buildin_adopt: Invalid data type for argument #1 (%d).", data->type);
  18864. return SCRIPT_CMD_FAILURE;
  18865. }
  18866. data = script_getdata(st, 3);
  18867. get_val(st, data);
  18868. if (data_isstring(data)) {
  18869. const char *name = conv_str(st, data);
  18870. b_sd = map_nick2sd(name);
  18871. if (b_sd == NULL) {
  18872. ShowError("buildin_adopt: Non-existant baby character %s requested.\n", name);
  18873. return SCRIPT_CMD_FAILURE;
  18874. }
  18875. } else if (data_isint(data)) {
  18876. uint32 char_id = conv_num(st, data);
  18877. b_sd = map_charid2sd(char_id);
  18878. if (b_sd == NULL) {
  18879. ShowError("buildin_adopt: Non-existant baby character %d requested.\n", char_id);
  18880. return SCRIPT_CMD_FAILURE;
  18881. }
  18882. } else {
  18883. ShowError("buildin_adopt: Invalid data type for argument #2 (%d).", data->type);
  18884. return SCRIPT_CMD_FAILURE;
  18885. }
  18886. response = pc_try_adopt(sd, map_charid2sd(sd->status.partner_id), b_sd);
  18887. if (response == ADOPT_ALLOWED) {
  18888. TBL_PC *p_sd = map_charid2sd(sd->status.partner_id);
  18889. b_sd->adopt_invite = sd->status.account_id;
  18890. clif_Adopt_request(b_sd, sd, p_sd->status.account_id);
  18891. script_pushint(st, ADOPT_ALLOWED);
  18892. return SCRIPT_CMD_SUCCESS;
  18893. }
  18894. script_pushint(st, response);
  18895. return SCRIPT_CMD_FAILURE;
  18896. }
  18897. /**
  18898. * Returns the minimum or maximum of all the given parameters for integer variables.
  18899. *
  18900. * min( <value or array>{,value or array 2,...} );
  18901. * minimum( <value or array>{,value or array 2,...} );
  18902. * max( <value or array>{,value or array 2,...} );
  18903. * maximum( <value or array>{,value or array 2,...} );
  18904. */
  18905. BUILDIN_FUNC(minmax){
  18906. char *functionname;
  18907. unsigned int i;
  18908. int value;
  18909. // Function pointer for our comparison function (either min or max at the moment)
  18910. int32 (*func)(int32, int32);
  18911. // Get the real function name
  18912. functionname = script_getfuncname(st);
  18913. // Our data should start at offset 2
  18914. i = 2;
  18915. if( !script_hasdata( st, i ) ){
  18916. ShowError( "buildin_%s: no arguments given!\n", functionname );
  18917. st->state = END;
  18918. return SCRIPT_CMD_FAILURE;
  18919. }
  18920. if( strnicmp( functionname, "min", strlen( "min" ) ) == 0 ){
  18921. value = INT_MAX;
  18922. func = i32min;
  18923. }else if( strnicmp( functionname, "max", strlen( "max" ) ) == 0 ){
  18924. value = INT_MIN;
  18925. func = i32max;
  18926. }else{
  18927. ShowError( "buildin_%s: Unknown call case for min/max!\n", functionname );
  18928. st->state = END;
  18929. return SCRIPT_CMD_FAILURE;
  18930. }
  18931. // As long as we have data on our script stack
  18932. while( script_hasdata(st,i) ){
  18933. struct script_data *data;
  18934. // Get the next piece of data from the script stack
  18935. data = script_getdata( st, i );
  18936. // Is the current parameter a single integer?
  18937. if( data_isint( data ) ){
  18938. value = func( value, script_getnum( st, i ) );
  18939. // Is the current parameter an array variable?
  18940. }else if( data_isreference( data ) ){
  18941. const char *name;
  18942. struct map_session_data* sd;
  18943. unsigned int start, end;
  18944. // Get the name of the variable
  18945. name = reference_getname(data);
  18946. // Check if it's a string variable
  18947. if( is_string_variable( name ) ){
  18948. ShowError( "buildin_%s: illegal type, need integer!\n", functionname );
  18949. script_reportdata( data );
  18950. st->state = END;
  18951. return SCRIPT_CMD_FAILURE;
  18952. }
  18953. // Get the session data, if a player is attached
  18954. sd = st->rid ? map_id2sd(st->rid) : NULL;
  18955. // Try to find the array's source pointer
  18956. if( !script_array_src( st, sd, name, reference_getref( data ) ) ){
  18957. ShowError( "buildin_%s: not a array!\n", functionname );
  18958. script_reportdata( data );
  18959. st->state = END;
  18960. return SCRIPT_CMD_FAILURE;
  18961. }
  18962. // Get the start and end indices of the array
  18963. start = reference_getindex( data );
  18964. end = script_array_highest_key( st, sd, name, reference_getref( data ) );
  18965. // Skip empty arrays
  18966. if( start < end ){
  18967. int id;
  18968. // For getting the values we need the id of the array
  18969. id = reference_getid( data );
  18970. // Loop through each value stored in the array
  18971. for( ; start < end; start++ ){
  18972. value = func( value, (int32)__64BPRTSIZE( get_val2( st, reference_uid( id, start ), reference_getref( data ) ) ) );
  18973. script_removetop( st, -1, 0 );
  18974. }
  18975. }
  18976. }else{
  18977. ShowError( "buildin_%s: not a supported data type!\n", functionname );
  18978. script_reportdata( data );
  18979. st->state = END;
  18980. return SCRIPT_CMD_FAILURE;
  18981. }
  18982. // Continue with the next stack entry
  18983. i++;
  18984. }
  18985. script_pushint( st, value );
  18986. return SCRIPT_CMD_SUCCESS;
  18987. }
  18988. /**
  18989. * Safety Base/Job EXP addition than using `set BaseExp,n;` or `set JobExp,n;`
  18990. * Unlike `getexp` that affected by some adjustments.
  18991. * getexp2 <base_exp>,<job_exp>{,<char_id>};
  18992. * @author [Cydh]
  18993. **/
  18994. BUILDIN_FUNC(getexp2) {
  18995. TBL_PC *sd = NULL;
  18996. int base_exp = script_getnum(st, 2);
  18997. int job_exp = script_getnum(st, 3);
  18998. if (!script_charid2sd(4, sd))
  18999. return SCRIPT_CMD_FAILURE;
  19000. if (base_exp == 0 && job_exp == 0)
  19001. return SCRIPT_CMD_SUCCESS;
  19002. if (base_exp > 0)
  19003. pc_gainexp(sd, NULL, base_exp, 0, 2);
  19004. else if (base_exp < 0)
  19005. pc_lostexp(sd, base_exp * -1, 0);
  19006. if (job_exp > 0)
  19007. pc_gainexp(sd, NULL, 0, job_exp, 2);
  19008. else if (job_exp < 0)
  19009. pc_lostexp(sd, 0, job_exp * -1);
  19010. return SCRIPT_CMD_SUCCESS;
  19011. }
  19012. /**
  19013. * Force stat recalculation of sd
  19014. * recalculatestat;
  19015. * @author [secretdataz]
  19016. **/
  19017. BUILDIN_FUNC(recalculatestat) {
  19018. TBL_PC* sd = script_rid2sd(st);
  19019. if (sd == NULL)
  19020. return SCRIPT_CMD_FAILURE;
  19021. status_calc_pc(sd, SCO_FORCE);
  19022. return SCRIPT_CMD_SUCCESS;
  19023. }
  19024. BUILDIN_FUNC(hateffect){
  19025. #if PACKETVER >= 20150513
  19026. struct map_session_data* sd = script_rid2sd(st);
  19027. bool enable;
  19028. int i, effectID;
  19029. if( sd == NULL )
  19030. return SCRIPT_CMD_FAILURE;
  19031. effectID = script_getnum(st,2);
  19032. enable = script_getnum(st,3) ? true : false;
  19033. ARR_FIND( 0, sd->hatEffectCount, i, sd->hatEffectIDs[i] == effectID );
  19034. if( enable ){
  19035. if( i < sd->hatEffectCount ){
  19036. return SCRIPT_CMD_SUCCESS;
  19037. }
  19038. RECREATE(sd->hatEffectIDs,uint32,sd->hatEffectCount+1);
  19039. sd->hatEffectIDs[sd->hatEffectCount] = effectID;
  19040. sd->hatEffectCount++;
  19041. }else{
  19042. if( i == sd->hatEffectCount ){
  19043. return SCRIPT_CMD_SUCCESS;
  19044. }
  19045. for( ; i < sd->hatEffectCount - 1; i++ ){
  19046. sd->hatEffectIDs[i] = sd->hatEffectIDs[i+1];
  19047. }
  19048. sd->hatEffectCount--;
  19049. if( !sd->hatEffectCount ){
  19050. aFree(sd->hatEffectIDs);
  19051. sd->hatEffectIDs = NULL;
  19052. }
  19053. }
  19054. if( !sd->state.connect_new ){
  19055. clif_hat_effect_single( sd, effectID, enable );
  19056. }
  19057. #endif
  19058. return SCRIPT_CMD_SUCCESS;
  19059. }
  19060. /**
  19061. * Retrieves param of current random option. Intended for random option script only.
  19062. * getrandomoptinfo(<type>);
  19063. * @author [secretdataz]
  19064. **/
  19065. BUILDIN_FUNC(getrandomoptinfo) {
  19066. struct map_session_data *sd;
  19067. int val;
  19068. int param = script_getnum(st, 2);
  19069. if ((sd = script_rid2sd(st)) != NULL && current_equip_item_index != -1 && current_equip_opt_index != -1 && sd->inventory.u.items_inventory[current_equip_item_index].option[current_equip_opt_index].id) {
  19070. switch (param) {
  19071. case ROA_ID:
  19072. val = sd->inventory.u.items_inventory[current_equip_item_index].option[current_equip_opt_index].id;
  19073. break;
  19074. case ROA_VALUE:
  19075. val = sd->inventory.u.items_inventory[current_equip_item_index].option[current_equip_opt_index].value;
  19076. break;
  19077. case ROA_PARAM:
  19078. val = sd->inventory.u.items_inventory[current_equip_item_index].option[current_equip_opt_index].param;
  19079. break;
  19080. default:
  19081. ShowWarning("buildin_getrandomoptinfo: Invalid attribute type %d (Max %d).\n", param, MAX_ITEM_RDM_OPT);
  19082. val = 0;
  19083. break;
  19084. }
  19085. script_pushint(st, val);
  19086. }
  19087. else {
  19088. script_pushint(st, 0);
  19089. }
  19090. return SCRIPT_CMD_SUCCESS;
  19091. }
  19092. /**
  19093. * Retrieves a random option on an equipped item.
  19094. * getequiprandomoption(<equipment slot>,<index>,<type>{,<char id>});
  19095. * @author [secretdataz]
  19096. */
  19097. BUILDIN_FUNC(getequiprandomoption) {
  19098. struct map_session_data *sd;
  19099. int val;
  19100. short i = -1;
  19101. int pos = script_getnum(st, 2);
  19102. int index = script_getnum(st, 3);
  19103. int type = script_getnum(st, 4);
  19104. if (!script_charid2sd(5, sd)) {
  19105. script_pushint(st, -1);
  19106. return SCRIPT_CMD_FAILURE;
  19107. }
  19108. if (index < 0 || index >= MAX_ITEM_RDM_OPT) {
  19109. ShowError("buildin_getequiprandomoption: Invalid random option index %d.\n", index);
  19110. script_pushint(st, -1);
  19111. return SCRIPT_CMD_FAILURE;
  19112. }
  19113. if (equip_index_check(pos))
  19114. i = pc_checkequip(sd, equip_bitmask[pos]);
  19115. if (i < 0) {
  19116. ShowError("buildin_getequiprandomoption: No item equipped at pos %d (CID=%d/AID=%d).\n", pos, sd->status.char_id, sd->status.account_id);
  19117. script_pushint(st, -1);
  19118. return SCRIPT_CMD_FAILURE;
  19119. }
  19120. switch (type) {
  19121. case ROA_ID:
  19122. val = sd->inventory.u.items_inventory[i].option[index].id;
  19123. break;
  19124. case ROA_VALUE:
  19125. val = sd->inventory.u.items_inventory[i].option[index].value;
  19126. break;
  19127. case ROA_PARAM:
  19128. val = sd->inventory.u.items_inventory[i].option[index].param;
  19129. break;
  19130. default:
  19131. ShowWarning("buildin_getequiprandomoption: Invalid attribute type %d (Max %d).\n", type, MAX_ITEM_RDM_OPT);
  19132. val = 0;
  19133. break;
  19134. }
  19135. script_pushint(st, val);
  19136. return SCRIPT_CMD_SUCCESS;
  19137. }
  19138. /**
  19139. * Adds a random option on a random option slot on an equipped item and overwrites
  19140. * existing random option in the process.
  19141. * setrandomoption(<equipment slot>,<index>,<id>,<value>,<param>{,<char id>});
  19142. * @author [secretdataz]
  19143. */
  19144. BUILDIN_FUNC(setrandomoption) {
  19145. struct map_session_data *sd;
  19146. struct s_random_opt_data *opt;
  19147. int pos, index, id, value, param, ep;
  19148. int i = -1;
  19149. if (!script_charid2sd(7, sd))
  19150. return SCRIPT_CMD_FAILURE;
  19151. pos = script_getnum(st, 2);
  19152. index = script_getnum(st, 3);
  19153. id = script_getnum(st, 4);
  19154. value = script_getnum(st, 5);
  19155. param = script_getnum(st, 6);
  19156. if ((opt = itemdb_randomopt_exists((short)id)) == NULL) {
  19157. ShowError("buildin_setrandomoption: Random option ID %d does not exists.\n", id);
  19158. script_pushint(st, 0);
  19159. return SCRIPT_CMD_FAILURE;
  19160. }
  19161. if (index < 0 || index >= MAX_ITEM_RDM_OPT) {
  19162. ShowError("buildin_setrandomoption: Invalid random option index %d.\n", index);
  19163. script_pushint(st, 0);
  19164. return SCRIPT_CMD_FAILURE;
  19165. }
  19166. if (equip_index_check(pos))
  19167. i = pc_checkequip(sd, equip_bitmask[pos]);
  19168. if (i >= 0) {
  19169. ep = sd->inventory.u.items_inventory[i].equip;
  19170. log_pick_pc(sd, LOG_TYPE_SCRIPT, -1, &sd->inventory.u.items_inventory[i]);
  19171. pc_unequipitem(sd, i, 2);
  19172. sd->inventory.u.items_inventory[i].option[index].id = id;
  19173. sd->inventory.u.items_inventory[i].option[index].value = value;
  19174. sd->inventory.u.items_inventory[i].option[index].param = param;
  19175. clif_delitem(sd, i, 1, 3);
  19176. log_pick_pc(sd, LOG_TYPE_SCRIPT, -1, &sd->inventory.u.items_inventory[i]);
  19177. clif_additem(sd, i, 1, 0);
  19178. pc_equipitem(sd, i, ep);
  19179. script_pushint(st, 1);
  19180. return SCRIPT_CMD_SUCCESS;
  19181. }
  19182. ShowError("buildin_setrandomoption: No item equipped at pos %d (CID=%d/AID=%d).\n", pos, sd->status.char_id, sd->status.account_id);
  19183. script_pushint(st, 0);
  19184. return SCRIPT_CMD_FAILURE;
  19185. }
  19186. /// Returns the number of stat points needed to change the specified stat by val.
  19187. /// If val is negative, returns the number of stat points that would be needed to
  19188. /// raise the specified stat from (current value - val) to current value.
  19189. /// *needed_status_point(<type>,<val>{,<char id>});
  19190. /// @author [secretdataz]
  19191. BUILDIN_FUNC(needed_status_point) {
  19192. struct map_session_data *sd;
  19193. int type, val;
  19194. if (!script_charid2sd(4, sd))
  19195. return SCRIPT_CMD_FAILURE;
  19196. type = script_getnum(st, 2);
  19197. val = script_getnum(st, 3);
  19198. script_pushint(st, pc_need_status_point(sd, type, val));
  19199. return SCRIPT_CMD_SUCCESS;
  19200. }
  19201. /**
  19202. * jobcanentermap("<mapname>"{,<JobID>});
  19203. * Check if (player with) JobID can enter the map.
  19204. * @param mapname Map name
  19205. * @param JobID Player's JobID (optional)
  19206. **/
  19207. BUILDIN_FUNC(jobcanentermap) {
  19208. const char *mapname = script_getstr(st, 2);
  19209. int mapidx = mapindex_name2id(mapname), m = -1;
  19210. int jobid = 0;
  19211. TBL_PC *sd = NULL;
  19212. if (!mapidx) {// Invalid map
  19213. script_pushint(st, false);
  19214. return SCRIPT_CMD_FAILURE;
  19215. }
  19216. m = map_mapindex2mapid(mapidx);
  19217. if (m == -1) { // Map is on different map server
  19218. ShowError("buildin_jobcanentermap: Map '%s' is not found in this server.\n", mapname);
  19219. script_pushint(st, false);
  19220. return SCRIPT_CMD_FAILURE;
  19221. }
  19222. if (script_hasdata(st, 3)) {
  19223. jobid = script_getnum(st, 3);
  19224. } else {
  19225. if (!(sd = script_rid2sd(st))) {
  19226. script_pushint(st, false);
  19227. return SCRIPT_CMD_FAILURE;
  19228. }
  19229. jobid = sd->status.class_;
  19230. }
  19231. script_pushint(st, pc_job_can_entermap((enum e_job)jobid, m, sd ? sd->group_level : 0));
  19232. return SCRIPT_CMD_SUCCESS;
  19233. }
  19234. /**
  19235. * Return alliance information between the two guilds.
  19236. * getguildalliance(<guild id1>,<guild id2>);
  19237. * Return values:
  19238. * -2 - Guild ID1 does not exist
  19239. * -1 - Guild ID2 does not exist
  19240. * 0 - Both guilds have no relation OR guild ID aren't given
  19241. * 1 - Both guilds are allies
  19242. * 2 - Both guilds are antagonists
  19243. */
  19244. BUILDIN_FUNC(getguildalliance)
  19245. {
  19246. struct guild *guild_data1, *guild_data2;
  19247. int guild_id1, guild_id2, i = 0;
  19248. guild_id1 = script_getnum(st,2);
  19249. guild_id2 = script_getnum(st,3);
  19250. if (guild_id1 < 1 || guild_id2 < 1) {
  19251. script_pushint(st, 0);
  19252. return SCRIPT_CMD_SUCCESS;
  19253. }
  19254. if (guild_id1 == guild_id2) {
  19255. script_pushint(st, 1);
  19256. return SCRIPT_CMD_SUCCESS;
  19257. }
  19258. guild_data1 = guild_search(guild_id1);
  19259. guild_data2 = guild_search(guild_id2);
  19260. if (guild_data1 == NULL) {
  19261. ShowWarning("buildin_getguildalliance: Requesting non-existent GuildID1 '%d'.\n", guild_id1);
  19262. script_pushint(st, -2);
  19263. return SCRIPT_CMD_FAILURE;
  19264. }
  19265. if (guild_data2 == NULL) {
  19266. ShowWarning("buildin_getguildalliance: Requesting non-existent GuildID2 '%d'.\n", guild_id2);
  19267. script_pushint(st, -1);
  19268. return SCRIPT_CMD_FAILURE;
  19269. }
  19270. ARR_FIND(0, MAX_GUILDALLIANCE, i, guild_data1->alliance[i].guild_id == guild_id2);
  19271. if (i == MAX_GUILDALLIANCE) {
  19272. script_pushint(st, 0);
  19273. return SCRIPT_CMD_SUCCESS;
  19274. }
  19275. if (guild_data1->alliance[i].opposition)
  19276. script_pushint(st, 2);
  19277. else
  19278. script_pushint(st, 1);
  19279. return SCRIPT_CMD_SUCCESS;
  19280. }
  19281. /*
  19282. * openstorage2 <storage_id>,<mode>{,<account_id>}
  19283. * mode @see enum e_storage_mode
  19284. **/
  19285. BUILDIN_FUNC(openstorage2) {
  19286. int stor_id = script_getnum(st, 2);
  19287. TBL_PC *sd = NULL;
  19288. if (!script_accid2sd(4, sd)) {
  19289. script_pushint(st, 0);
  19290. return SCRIPT_CMD_FAILURE;
  19291. }
  19292. if (!storage_exists(stor_id)) {
  19293. ShowError("buildin_openstorage2: Invalid storage_id '%d'!\n", stor_id);
  19294. return SCRIPT_CMD_FAILURE;
  19295. }
  19296. script_pushint(st, storage_premiumStorage_load(sd, stor_id, script_getnum(st, 3)));
  19297. return SCRIPT_CMD_SUCCESS;
  19298. }
  19299. #include "../custom/script.inc"
  19300. // declarations that were supposed to be exported from npc_chat.c
  19301. #ifdef PCRE_SUPPORT
  19302. BUILDIN_FUNC(defpattern);
  19303. BUILDIN_FUNC(activatepset);
  19304. BUILDIN_FUNC(deactivatepset);
  19305. BUILDIN_FUNC(deletepset);
  19306. #endif
  19307. /** Regular expression matching
  19308. * preg_match(<pattern>,<string>{,<offset>})
  19309. */
  19310. BUILDIN_FUNC(preg_match) {
  19311. #ifdef PCRE_SUPPORT
  19312. pcre *re;
  19313. pcre_extra *pcreExtra;
  19314. const char *error;
  19315. int erroffset, r, offset = 0;
  19316. int subStrVec[30];
  19317. const char* pattern = script_getstr(st,2);
  19318. const char* subject = script_getstr(st,3);
  19319. if (script_hasdata(st,4))
  19320. offset = script_getnum(st,4);
  19321. re = pcre_compile(pattern, 0, &error, &erroffset, NULL);
  19322. pcreExtra = pcre_study(re, 0, &error);
  19323. r = pcre_exec(re, pcreExtra, subject, (int)strlen(subject), offset, 0, subStrVec, 30);
  19324. pcre_free(re);
  19325. if (pcreExtra != NULL)
  19326. pcre_free(pcreExtra);
  19327. if (r < 0)
  19328. script_pushint(st,0);
  19329. else
  19330. script_pushint(st,(r > 0) ? r : 30 / 3);
  19331. return SCRIPT_CMD_SUCCESS;
  19332. #else
  19333. ShowDebug("script:preg_match: cannot run without PCRE library enabled.\n");
  19334. script_pushint(st,0);
  19335. return SCRIPT_CMD_SUCCESS;
  19336. #endif
  19337. }
  19338. /// script command definitions
  19339. /// for an explanation on args, see add_buildin_func
  19340. struct script_function buildin_func[] = {
  19341. // NPC interaction
  19342. BUILDIN_DEF(mes,"s*"),
  19343. BUILDIN_DEF(next,""),
  19344. BUILDIN_DEF(close,""),
  19345. BUILDIN_DEF(close2,""),
  19346. BUILDIN_DEF(menu,"sl*"),
  19347. BUILDIN_DEF(select,"s*"), //for future jA script compatibility
  19348. BUILDIN_DEF(prompt,"s*"),
  19349. //
  19350. BUILDIN_DEF(goto,"l"),
  19351. BUILDIN_DEF(callsub,"l*"),
  19352. BUILDIN_DEF(callfunc,"s*"),
  19353. BUILDIN_DEF(return,"?"),
  19354. BUILDIN_DEF(getarg,"i?"),
  19355. BUILDIN_DEF(jobchange,"i??"),
  19356. BUILDIN_DEF(jobname,"i"),
  19357. BUILDIN_DEF(input,"r??"),
  19358. BUILDIN_DEF(warp,"sii?"),
  19359. BUILDIN_DEF2(warp, "warpchar", "sii?"),
  19360. BUILDIN_DEF(areawarp,"siiiisii??"),
  19361. BUILDIN_DEF(warpparty,"siii?"), // [Fredzilla] [Paradox924X]
  19362. BUILDIN_DEF(warpguild,"siii"), // [Fredzilla]
  19363. BUILDIN_DEF(setlook,"ii?"),
  19364. BUILDIN_DEF(changelook,"ii?"), // Simulates but don't Store it
  19365. BUILDIN_DEF2(setr,"set","rv?"),
  19366. BUILDIN_DEF(setr,"rv??"), // Not meant to be used directly, required for var++/var--
  19367. BUILDIN_DEF(setarray,"rv*"),
  19368. BUILDIN_DEF(cleararray,"rvi"),
  19369. BUILDIN_DEF(copyarray,"rri"),
  19370. BUILDIN_DEF(getarraysize,"r"),
  19371. BUILDIN_DEF(deletearray,"r?"),
  19372. BUILDIN_DEF(getelementofarray,"ri"),
  19373. BUILDIN_DEF(getitem,"vi?"),
  19374. BUILDIN_DEF(rentitem,"vi?"),
  19375. BUILDIN_DEF(rentitem2,"viiiiiiii?"),
  19376. BUILDIN_DEF(getitem2,"viiiiiiii?"),
  19377. BUILDIN_DEF(getnameditem,"vv"),
  19378. BUILDIN_DEF2(grouprandomitem,"groupranditem","i?"),
  19379. BUILDIN_DEF(makeitem,"visii"),
  19380. BUILDIN_DEF(makeitem2,"visiiiiiiiii"),
  19381. BUILDIN_DEF(delitem,"vi?"),
  19382. BUILDIN_DEF2(delitem,"storagedelitem","vi?"),
  19383. BUILDIN_DEF2(delitem,"guildstoragedelitem","vi?"),
  19384. BUILDIN_DEF2(delitem,"cartdelitem","vi?"),
  19385. BUILDIN_DEF(delitem2,"viiiiiiii?"),
  19386. BUILDIN_DEF2(delitem2,"storagedelitem2","viiiiiiii?"),
  19387. BUILDIN_DEF2(delitem2,"guildstoragedelitem2","viiiiiiii?"),
  19388. BUILDIN_DEF2(delitem2,"cartdelitem2","viiiiiiii?"),
  19389. BUILDIN_DEF2(enableitemuse,"enable_items",""),
  19390. BUILDIN_DEF2(disableitemuse,"disable_items",""),
  19391. BUILDIN_DEF(cutin,"si"),
  19392. BUILDIN_DEF(viewpoint,"iiiii"),
  19393. BUILDIN_DEF(heal,"ii?"),
  19394. BUILDIN_DEF(itemheal,"ii?"),
  19395. BUILDIN_DEF(percentheal,"ii?"),
  19396. BUILDIN_DEF(rand,"i?"),
  19397. BUILDIN_DEF(countitem,"v?"),
  19398. BUILDIN_DEF2(countitem,"storagecountitem","v?"),
  19399. BUILDIN_DEF2(countitem,"guildstoragecountitem","v?"),
  19400. BUILDIN_DEF2(countitem,"cartcountitem","v?"),
  19401. BUILDIN_DEF2(countitem,"countitem2","viiiiiii?"),
  19402. BUILDIN_DEF2(countitem,"storagecountitem2","viiiiiii?"),
  19403. BUILDIN_DEF2(countitem,"guildstoragecountitem2","viiiiiii?"),
  19404. BUILDIN_DEF2(countitem,"cartcountitem2","viiiiiii?"),
  19405. BUILDIN_DEF(checkweight,"vi*"),
  19406. BUILDIN_DEF(checkweight2,"rr"),
  19407. BUILDIN_DEF(readparam,"i?"),
  19408. BUILDIN_DEF(getcharid,"i?"),
  19409. BUILDIN_DEF(getnpcid,"i?"),
  19410. BUILDIN_DEF(getpartyname,"i"),
  19411. BUILDIN_DEF(getpartymember,"i??"),
  19412. BUILDIN_DEF(getpartyleader,"i?"),
  19413. BUILDIN_DEF(getguildname,"i"),
  19414. BUILDIN_DEF(getguildmaster,"i"),
  19415. BUILDIN_DEF(getguildmasterid,"i"),
  19416. BUILDIN_DEF(strcharinfo,"i?"),
  19417. BUILDIN_DEF(strnpcinfo,"i"),
  19418. BUILDIN_DEF(getequipid,"i?"),
  19419. BUILDIN_DEF(getequipuniqueid,"i?"),
  19420. BUILDIN_DEF(getequipname,"i?"),
  19421. BUILDIN_DEF(getbrokenid,"i?"), // [Valaris]
  19422. BUILDIN_DEF(repair,"i?"), // [Valaris]
  19423. BUILDIN_DEF(repairall,"?"),
  19424. BUILDIN_DEF(getequipisequiped,"i?"),
  19425. BUILDIN_DEF(getequipisenableref,"i?"),
  19426. BUILDIN_DEF(getequiprefinerycnt,"i?"),
  19427. BUILDIN_DEF(getequipweaponlv,"i?"),
  19428. BUILDIN_DEF(getequippercentrefinery,"i?"),
  19429. BUILDIN_DEF(successrefitem,"i??"),
  19430. BUILDIN_DEF(failedrefitem,"i?"),
  19431. BUILDIN_DEF(downrefitem,"i??"),
  19432. BUILDIN_DEF(statusup,"i?"),
  19433. BUILDIN_DEF(statusup2,"ii?"),
  19434. BUILDIN_DEF(bonus,"iv"),
  19435. BUILDIN_DEF2(bonus,"bonus2","ivi"),
  19436. BUILDIN_DEF2(bonus,"bonus3","ivii"),
  19437. BUILDIN_DEF2(bonus,"bonus4","ivvii"),
  19438. BUILDIN_DEF2(bonus,"bonus5","ivviii"),
  19439. BUILDIN_DEF(autobonus,"sii??"),
  19440. BUILDIN_DEF(autobonus2,"sii??"),
  19441. BUILDIN_DEF(autobonus3,"siiv?"),
  19442. BUILDIN_DEF(skill,"vi?"),
  19443. BUILDIN_DEF2(skill,"addtoskill","vi?"), // [Valaris]
  19444. BUILDIN_DEF(guildskill,"vi"),
  19445. BUILDIN_DEF(getskilllv,"v"),
  19446. BUILDIN_DEF(getgdskilllv,"iv"),
  19447. BUILDIN_DEF(basicskillcheck,""),
  19448. BUILDIN_DEF(getgmlevel,"?"),
  19449. BUILDIN_DEF(getgroupid,"?"),
  19450. BUILDIN_DEF(end,""),
  19451. BUILDIN_DEF(checkoption,"i?"),
  19452. BUILDIN_DEF(setoption,"i??"),
  19453. BUILDIN_DEF(setcart,"??"),
  19454. BUILDIN_DEF(checkcart,"?"),
  19455. BUILDIN_DEF(setfalcon,"??"),
  19456. BUILDIN_DEF(checkfalcon,"?"),
  19457. BUILDIN_DEF(setriding,"??"),
  19458. BUILDIN_DEF(checkriding,"?"),
  19459. BUILDIN_DEF(checkwug,"?"),
  19460. BUILDIN_DEF(checkmadogear,"?"),
  19461. BUILDIN_DEF(setmadogear,"??"),
  19462. BUILDIN_DEF2(savepoint,"save","sii???"),
  19463. BUILDIN_DEF(savepoint,"sii???"),
  19464. BUILDIN_DEF(gettimetick,"i"),
  19465. BUILDIN_DEF(gettime,"i"),
  19466. BUILDIN_DEF(gettimestr,"si"),
  19467. BUILDIN_DEF(openstorage,""),
  19468. BUILDIN_DEF(guildopenstorage,""),
  19469. BUILDIN_DEF(itemskill,"vi?"),
  19470. BUILDIN_DEF(produce,"i"),
  19471. BUILDIN_DEF(cooking,"i"),
  19472. BUILDIN_DEF(monster,"siisii???"),
  19473. BUILDIN_DEF(getmobdrops,"i"),
  19474. BUILDIN_DEF(areamonster,"siiiisii???"),
  19475. BUILDIN_DEF(killmonster,"ss?"),
  19476. BUILDIN_DEF(killmonsterall,"s?"),
  19477. BUILDIN_DEF(clone,"siisi????"),
  19478. BUILDIN_DEF(doevent,"s"),
  19479. BUILDIN_DEF(donpcevent,"s"),
  19480. BUILDIN_DEF(cmdothernpc,"ss"),
  19481. BUILDIN_DEF(addtimer,"is"),
  19482. BUILDIN_DEF(deltimer,"s"),
  19483. BUILDIN_DEF(addtimercount,"is"),
  19484. BUILDIN_DEF(initnpctimer,"??"),
  19485. BUILDIN_DEF(stopnpctimer,"??"),
  19486. BUILDIN_DEF(startnpctimer,"??"),
  19487. BUILDIN_DEF(setnpctimer,"i?"),
  19488. BUILDIN_DEF(getnpctimer,"i?"),
  19489. BUILDIN_DEF(attachnpctimer,"?"), // attached the player id to the npc timer [Celest]
  19490. BUILDIN_DEF(detachnpctimer,"?"), // detached the player id from the npc timer [Celest]
  19491. BUILDIN_DEF(playerattached,""), // returns id of the current attached player. [Skotlex]
  19492. BUILDIN_DEF(announce,"si?????"),
  19493. BUILDIN_DEF(mapannounce,"ssi?????"),
  19494. BUILDIN_DEF(areaannounce,"siiiisi?????"),
  19495. BUILDIN_DEF(getusers,"i"),
  19496. BUILDIN_DEF(getmapguildusers,"si"),
  19497. BUILDIN_DEF(getmapusers,"s"),
  19498. BUILDIN_DEF(getareausers,"siiii"),
  19499. BUILDIN_DEF(getareadropitem,"siiiiv"),
  19500. BUILDIN_DEF(enablenpc,"s"),
  19501. BUILDIN_DEF(disablenpc,"s"),
  19502. BUILDIN_DEF(hideoffnpc,"s"),
  19503. BUILDIN_DEF(hideonnpc,"s"),
  19504. BUILDIN_DEF(sc_start,"iii???"),
  19505. BUILDIN_DEF2(sc_start,"sc_start2","iiii???"),
  19506. BUILDIN_DEF2(sc_start,"sc_start4","iiiiii???"),
  19507. BUILDIN_DEF(sc_end,"i?"),
  19508. BUILDIN_DEF(getstatus, "i??"),
  19509. BUILDIN_DEF(getscrate,"ii?"),
  19510. BUILDIN_DEF(debugmes,"s"),
  19511. BUILDIN_DEF2(catchpet,"pet","i"),
  19512. BUILDIN_DEF2(birthpet,"bpet",""),
  19513. BUILDIN_DEF(resetlvl,"i?"),
  19514. BUILDIN_DEF(resetstatus,"?"),
  19515. BUILDIN_DEF(resetskill,"?"),
  19516. BUILDIN_DEF(skillpointcount,"?"),
  19517. BUILDIN_DEF(changebase,"i?"),
  19518. BUILDIN_DEF(changesex,"?"),
  19519. BUILDIN_DEF(changecharsex,"?"),
  19520. BUILDIN_DEF(waitingroom,"si?????"),
  19521. BUILDIN_DEF(delwaitingroom,"?"),
  19522. BUILDIN_DEF(waitingroomkick,"ss"),
  19523. BUILDIN_DEF(getwaitingroomusers, "?"),
  19524. BUILDIN_DEF2(waitingroomkickall,"kickwaitingroomall","?"),
  19525. BUILDIN_DEF(enablewaitingroomevent,"?"),
  19526. BUILDIN_DEF(disablewaitingroomevent,"?"),
  19527. BUILDIN_DEF2(enablewaitingroomevent,"enablearena",""), // Added by RoVeRT
  19528. BUILDIN_DEF2(disablewaitingroomevent,"disablearena",""), // Added by RoVeRT
  19529. BUILDIN_DEF(getwaitingroomstate,"i?"),
  19530. BUILDIN_DEF(warpwaitingpc,"sii?"),
  19531. BUILDIN_DEF(attachrid,"i"),
  19532. BUILDIN_DEF(addrid,"i?????"),
  19533. BUILDIN_DEF(detachrid,""),
  19534. BUILDIN_DEF(isloggedin,"i?"),
  19535. BUILDIN_DEF(setmapflagnosave,"ssii"),
  19536. BUILDIN_DEF(getmapflag,"si?"),
  19537. BUILDIN_DEF(setmapflag,"si??"),
  19538. BUILDIN_DEF(removemapflag,"si?"),
  19539. BUILDIN_DEF(pvpon,"s"),
  19540. BUILDIN_DEF(pvpoff,"s"),
  19541. BUILDIN_DEF(gvgon,"s"),
  19542. BUILDIN_DEF(gvgoff,"s"),
  19543. BUILDIN_DEF(emotion,"i??"),
  19544. BUILDIN_DEF(maprespawnguildid,"sii"),
  19545. BUILDIN_DEF(agitstart,""), // <Agit>
  19546. BUILDIN_DEF(agitend,""),
  19547. BUILDIN_DEF(agitcheck,""), // <Agitcheck>
  19548. BUILDIN_DEF(flagemblem,"i"), // Flag Emblem
  19549. BUILDIN_DEF(getcastlename,"s"),
  19550. BUILDIN_DEF(getcastledata,"si"),
  19551. BUILDIN_DEF(setcastledata,"sii"),
  19552. BUILDIN_DEF(requestguildinfo,"i?"),
  19553. BUILDIN_DEF(getequipcardcnt,"i"),
  19554. BUILDIN_DEF(successremovecards,"i"),
  19555. BUILDIN_DEF(failedremovecards,"ii"),
  19556. BUILDIN_DEF(marriage,"s"),
  19557. BUILDIN_DEF2(wedding_effect,"wedding",""),
  19558. BUILDIN_DEF(divorce,"?"),
  19559. BUILDIN_DEF(ispartneron,"?"),
  19560. BUILDIN_DEF(getpartnerid,"?"),
  19561. BUILDIN_DEF(getchildid,"?"),
  19562. BUILDIN_DEF(getmotherid,"?"),
  19563. BUILDIN_DEF(getfatherid,"?"),
  19564. BUILDIN_DEF(warppartner,"sii"),
  19565. BUILDIN_DEF(getitemname,"v"),
  19566. BUILDIN_DEF(getitemslots,"i"),
  19567. BUILDIN_DEF(makepet,"i"),
  19568. BUILDIN_DEF(getexp,"ii?"),
  19569. BUILDIN_DEF(getinventorylist,"?"),
  19570. BUILDIN_DEF(getskilllist,"?"),
  19571. BUILDIN_DEF(clearitem,"?"),
  19572. BUILDIN_DEF(classchange,"ii"),
  19573. BUILDIN_DEF(misceffect,"i"),
  19574. BUILDIN_DEF(playBGM,"s"),
  19575. BUILDIN_DEF(playBGMall,"s?????"),
  19576. BUILDIN_DEF(soundeffect,"si"),
  19577. BUILDIN_DEF(soundeffectall,"si?????"), // SoundEffectAll [Codemaster]
  19578. BUILDIN_DEF(strmobinfo,"ii"), // display mob data [Valaris]
  19579. BUILDIN_DEF(guardian,"siisi??"), // summon guardians
  19580. BUILDIN_DEF(guardianinfo,"sii"), // display guardian data [Valaris]
  19581. BUILDIN_DEF(petskillbonus,"iiii"), // [Valaris]
  19582. BUILDIN_DEF(petrecovery,"ii"), // [Valaris]
  19583. BUILDIN_DEF(petloot,"i"), // [Valaris]
  19584. BUILDIN_DEF(petskillattack,"viii"), // [Skotlex]
  19585. BUILDIN_DEF(petskillattack2,"viiii"), // [Valaris]
  19586. BUILDIN_DEF(petskillsupport,"viiii"), // [Skotlex]
  19587. BUILDIN_DEF(skilleffect,"vi"), // skill effect [Celest]
  19588. BUILDIN_DEF(npcskilleffect,"viii"), // npc skill effect [Valaris]
  19589. BUILDIN_DEF(specialeffect,"i??"), // npc skill effect [Valaris]
  19590. BUILDIN_DEF(specialeffect2,"i??"), // skill effect on players[Valaris]
  19591. BUILDIN_DEF(nude,"?"), // nude command [Valaris]
  19592. BUILDIN_DEF(mapwarp,"ssii??"), // Added by RoVeRT
  19593. BUILDIN_DEF(atcommand,"s"), // [MouseJstr]
  19594. BUILDIN_DEF2(atcommand,"charcommand","s"), // [MouseJstr]
  19595. BUILDIN_DEF(movenpc,"sii?"), // [MouseJstr]
  19596. BUILDIN_DEF(message,"ss"), // [MouseJstr]
  19597. BUILDIN_DEF(npctalk,"s?"), // [Valaris]
  19598. BUILDIN_DEF(mobcount,"ss"),
  19599. BUILDIN_DEF(getlook,"i?"),
  19600. BUILDIN_DEF(getsavepoint,"i?"),
  19601. BUILDIN_DEF(npcspeed,"i"), // [Valaris]
  19602. BUILDIN_DEF(npcwalkto,"ii"), // [Valaris]
  19603. BUILDIN_DEF(npcstop,""), // [Valaris]
  19604. BUILDIN_DEF(getmapxy,"rrri?"), //by Lorky [Lupus]
  19605. BUILDIN_DEF(mapid2name,"i"),
  19606. BUILDIN_DEF(checkoption1,"i?"),
  19607. BUILDIN_DEF(checkoption2,"i?"),
  19608. BUILDIN_DEF(guildgetexp,"i"),
  19609. BUILDIN_DEF(guildchangegm,"is"),
  19610. BUILDIN_DEF(logmes,"s"), //this command actls as MES but rints info into LOG file either SQL/TXT [Lupus]
  19611. BUILDIN_DEF(summon,"si??"), // summons a slave monster [Celest]
  19612. BUILDIN_DEF(isnight,""), // check whether it is night time [Celest]
  19613. BUILDIN_DEF(isday,""), // check whether it is day time [Celest]
  19614. BUILDIN_DEF(isequipped,"i*"), // check whether another item/card has been equipped [Celest]
  19615. BUILDIN_DEF(isequippedcnt,"i*"), // check how many items/cards are being equipped [Celest]
  19616. BUILDIN_DEF(cardscnt,"i*"), // check how many items/cards are being equipped in the same arm [Lupus]
  19617. BUILDIN_DEF(getrefine,""), // returns the refined number of the current item, or an item with index specified [celest]
  19618. BUILDIN_DEF(night,""), // sets the server to night time
  19619. BUILDIN_DEF(day,""), // sets the server to day time
  19620. #ifdef PCRE_SUPPORT
  19621. BUILDIN_DEF(defpattern,"iss"), // Define pattern to listen for [MouseJstr]
  19622. BUILDIN_DEF(activatepset,"i"), // Activate a pattern set [MouseJstr]
  19623. BUILDIN_DEF(deactivatepset,"i"), // Deactive a pattern set [MouseJstr]
  19624. BUILDIN_DEF(deletepset,"i"), // Delete a pattern set [MouseJstr]
  19625. #endif
  19626. BUILDIN_DEF(preg_match,"ss?"),
  19627. BUILDIN_DEF(dispbottom,"s??"), //added from jA [Lupus]
  19628. BUILDIN_DEF(recovery,"i???"),
  19629. BUILDIN_DEF(getpetinfo,"i?"),
  19630. BUILDIN_DEF(gethominfo,"i?"),
  19631. BUILDIN_DEF(getmercinfo,"i?"),
  19632. BUILDIN_DEF(checkequipedcard,"i"),
  19633. BUILDIN_DEF(jump_zero,"il"), //for future jA script compatibility
  19634. BUILDIN_DEF(globalmes,"s?"), //end jA addition
  19635. BUILDIN_DEF(unequip,"i?"), // unequip command [Spectre]
  19636. BUILDIN_DEF(getstrlen,"s"), //strlen [Valaris]
  19637. BUILDIN_DEF(charisalpha,"si"), //isalpha [Valaris]
  19638. BUILDIN_DEF(charat,"si"),
  19639. BUILDIN_DEF(setchar,"ssi"),
  19640. BUILDIN_DEF(insertchar,"ssi"),
  19641. BUILDIN_DEF(delchar,"si"),
  19642. BUILDIN_DEF(strtoupper,"s"),
  19643. BUILDIN_DEF(strtolower,"s"),
  19644. BUILDIN_DEF(charisupper, "si"),
  19645. BUILDIN_DEF(charislower, "si"),
  19646. BUILDIN_DEF(substr,"sii"),
  19647. BUILDIN_DEF(explode, "rss"),
  19648. BUILDIN_DEF(implode, "r?"),
  19649. BUILDIN_DEF(sprintf,"s*"), // [Mirei]
  19650. BUILDIN_DEF(sscanf,"ss*"), // [Mirei]
  19651. BUILDIN_DEF(strpos,"ss?"),
  19652. BUILDIN_DEF(replacestr,"sss??"),
  19653. BUILDIN_DEF(countstr,"ss?"),
  19654. BUILDIN_DEF(setnpcdisplay,"sv??"),
  19655. BUILDIN_DEF(compare,"ss"), // Lordalfa - To bring strstr to scripting Engine.
  19656. BUILDIN_DEF(strcmp,"ss"),
  19657. BUILDIN_DEF(getiteminfo,"ii"), //[Lupus] returns Items Buy / sell Price, etc info
  19658. BUILDIN_DEF(setiteminfo,"iii"), //[Lupus] set Items Buy / sell Price, etc info
  19659. BUILDIN_DEF(getequipcardid,"ii"), //[Lupus] returns CARD ID or other info from CARD slot N of equipped item
  19660. // [zBuffer] List of mathematics commands --->
  19661. BUILDIN_DEF(sqrt,"i"),
  19662. BUILDIN_DEF(pow,"ii"),
  19663. BUILDIN_DEF(distance,"iiii"),
  19664. BUILDIN_DEF2(minmax,"min", "*"),
  19665. BUILDIN_DEF2(minmax,"minimum", "*"),
  19666. BUILDIN_DEF2(minmax,"max", "*"),
  19667. BUILDIN_DEF2(minmax,"maximum", "*"),
  19668. // <--- [zBuffer] List of mathematics commands
  19669. BUILDIN_DEF(md5,"s"),
  19670. // [zBuffer] List of dynamic var commands --->
  19671. BUILDIN_DEF(getd,"s"),
  19672. BUILDIN_DEF(setd,"sv?"),
  19673. BUILDIN_DEF(callshop,"s?"), // [Skotlex]
  19674. BUILDIN_DEF(npcshopitem,"sii*"), // [Lance]
  19675. BUILDIN_DEF(npcshopadditem,"sii*"),
  19676. BUILDIN_DEF(npcshopdelitem,"si*"),
  19677. BUILDIN_DEF(npcshopattach,"s?"),
  19678. BUILDIN_DEF(equip,"i?"),
  19679. BUILDIN_DEF(autoequip,"ii"),
  19680. BUILDIN_DEF(setbattleflag,"si"),
  19681. BUILDIN_DEF(getbattleflag,"s"),
  19682. BUILDIN_DEF(setitemscript,"is?"), //Set NEW item bonus script. Lupus
  19683. BUILDIN_DEF(disguise,"i?"), //disguise player. Lupus
  19684. BUILDIN_DEF(undisguise,"?"), //undisguise player. Lupus
  19685. BUILDIN_DEF(getmonsterinfo,"ii"), //Lupus
  19686. BUILDIN_DEF(addmonsterdrop,"vii"), //Akinari [Lupus]
  19687. BUILDIN_DEF(delmonsterdrop,"vi"), //Akinari [Lupus]
  19688. BUILDIN_DEF(axtoi,"s"),
  19689. BUILDIN_DEF(query_sql,"s*"),
  19690. BUILDIN_DEF(query_logsql,"s*"),
  19691. BUILDIN_DEF(escape_sql,"v"),
  19692. BUILDIN_DEF(atoi,"s"),
  19693. BUILDIN_DEF(strtol,"si"),
  19694. // [zBuffer] List of player cont commands --->
  19695. BUILDIN_DEF(rid2name,"i"),
  19696. BUILDIN_DEF(pcfollow,"ii"),
  19697. BUILDIN_DEF(pcstopfollow,"i"),
  19698. BUILDIN_DEF(pcblockmove,"ii"),
  19699. // <--- [zBuffer] List of player cont commands
  19700. // [zBuffer] List of unit control commands --->
  19701. BUILDIN_DEF(unitexists,"i"),
  19702. BUILDIN_DEF(getunittype,"i"),
  19703. BUILDIN_DEF(getunitname,"i"),
  19704. BUILDIN_DEF(setunitname,"is"),
  19705. BUILDIN_DEF(getunitdata,"i*"),
  19706. BUILDIN_DEF(setunitdata,"iii"),
  19707. BUILDIN_DEF(unitwalk,"iii?"),
  19708. BUILDIN_DEF2(unitwalk,"unitwalkto","ii?"),
  19709. BUILDIN_DEF(unitkill,"i"),
  19710. BUILDIN_DEF(unitwarp,"isii"),
  19711. BUILDIN_DEF(unitattack,"iv?"),
  19712. BUILDIN_DEF(unitstopattack,"i"),
  19713. BUILDIN_DEF(unitstopwalk,"i"),
  19714. BUILDIN_DEF2(pcblockmove,"unitblockmove","ii"),
  19715. BUILDIN_DEF(unittalk,"is"),
  19716. BUILDIN_DEF(unitemote,"ii"),
  19717. BUILDIN_DEF(unitskilluseid,"ivi??"), // originally by Qamera [Celest]
  19718. BUILDIN_DEF(unitskillusepos,"iviii?"), // [Celest]
  19719. // <--- [zBuffer] List of unit control commands
  19720. BUILDIN_DEF(sleep,"i"),
  19721. BUILDIN_DEF(sleep2,"i"),
  19722. BUILDIN_DEF(awake,"s"),
  19723. BUILDIN_DEF(getvariableofnpc,"rs"),
  19724. BUILDIN_DEF(warpportal,"iisii"),
  19725. BUILDIN_DEF2(homunculus_evolution,"homevolution",""), //[orn]
  19726. BUILDIN_DEF2(homunculus_mutate,"hommutate","?"),
  19727. BUILDIN_DEF(morphembryo,""),
  19728. BUILDIN_DEF2(homunculus_shuffle,"homshuffle",""), //[Zephyrus]
  19729. BUILDIN_DEF(checkhomcall,""),
  19730. BUILDIN_DEF(eaclass,"??"), //[Skotlex]
  19731. BUILDIN_DEF(roclass,"i?"), //[Skotlex]
  19732. BUILDIN_DEF(checkvending,"?"),
  19733. BUILDIN_DEF(checkchatting,"?"),
  19734. BUILDIN_DEF(checkidle,"?"),
  19735. BUILDIN_DEF(openmail,"?"),
  19736. BUILDIN_DEF(openauction,"?"),
  19737. BUILDIN_DEF(checkcell,"siii"),
  19738. BUILDIN_DEF(setcell,"siiiiii"),
  19739. BUILDIN_DEF(setwall,"siiiiis"),
  19740. BUILDIN_DEF(delwall,"s"),
  19741. BUILDIN_DEF(searchitem,"rs"),
  19742. BUILDIN_DEF(mercenary_create,"ii"),
  19743. BUILDIN_DEF(mercenary_heal,"ii"),
  19744. BUILDIN_DEF(mercenary_sc_start,"iii"),
  19745. BUILDIN_DEF(mercenary_get_calls,"i"),
  19746. BUILDIN_DEF(mercenary_get_faith,"i"),
  19747. BUILDIN_DEF(mercenary_set_calls,"ii"),
  19748. BUILDIN_DEF(mercenary_set_faith,"ii"),
  19749. BUILDIN_DEF(readbook,"ii"),
  19750. BUILDIN_DEF(setfont,"i"),
  19751. BUILDIN_DEF(areamobuseskill,"siiiiviiiii"),
  19752. BUILDIN_DEF(progressbar,"si"),
  19753. BUILDIN_DEF(pushpc,"ii"),
  19754. BUILDIN_DEF(buyingstore,"i"),
  19755. BUILDIN_DEF(searchstores,"ii"),
  19756. BUILDIN_DEF(showdigit,"i?"),
  19757. // WoE SE
  19758. BUILDIN_DEF(agitstart2,""),
  19759. BUILDIN_DEF(agitend2,""),
  19760. BUILDIN_DEF(agitcheck2,""),
  19761. // BattleGround
  19762. BUILDIN_DEF(waitingroom2bg,"sii???"),
  19763. BUILDIN_DEF(waitingroom2bg_single,"i????"),
  19764. BUILDIN_DEF(bg_team_setxy,"iii"),
  19765. BUILDIN_DEF(bg_warp,"isii"),
  19766. BUILDIN_DEF(bg_monster,"isiisi?"),
  19767. BUILDIN_DEF(bg_monster_set_team,"ii"),
  19768. BUILDIN_DEF(bg_leave,"?"),
  19769. BUILDIN_DEF(bg_destroy,"i"),
  19770. BUILDIN_DEF(areapercentheal,"siiiiii"),
  19771. BUILDIN_DEF(bg_get_data,"ii"),
  19772. BUILDIN_DEF(bg_getareausers,"isiiii"),
  19773. BUILDIN_DEF(bg_updatescore,"sii"),
  19774. BUILDIN_DEF(bg_join,"i????"),
  19775. BUILDIN_DEF(bg_create,"sii??"),
  19776. // Instancing
  19777. BUILDIN_DEF(instance_create,"s??"),
  19778. BUILDIN_DEF(instance_destroy,"?"),
  19779. BUILDIN_DEF(instance_id,""),
  19780. BUILDIN_DEF(instance_enter,"s????"),
  19781. BUILDIN_DEF(instance_npcname,"s?"),
  19782. BUILDIN_DEF(instance_mapname,"s?"),
  19783. BUILDIN_DEF(instance_warpall,"sii?"),
  19784. BUILDIN_DEF(instance_announce,"isi?????"),
  19785. BUILDIN_DEF(instance_check_party,"i???"),
  19786. BUILDIN_DEF(instance_check_guild,"i???"),
  19787. BUILDIN_DEF(instance_info,"si?"),
  19788. /**
  19789. * 3rd-related
  19790. **/
  19791. BUILDIN_DEF(makerune,"i?"),
  19792. BUILDIN_DEF(checkdragon,"?"),//[Ind]
  19793. BUILDIN_DEF(setdragon,"??"),//[Ind]
  19794. BUILDIN_DEF(ismounting,"?"),//[Ind]
  19795. BUILDIN_DEF(setmounting,"?"),//[Ind]
  19796. BUILDIN_DEF(checkre,"i"),
  19797. /**
  19798. * rAthena and beyond!
  19799. **/
  19800. BUILDIN_DEF(getargcount,""),
  19801. BUILDIN_DEF(getcharip,"?"),
  19802. BUILDIN_DEF(is_function,"s"),
  19803. BUILDIN_DEF(get_revision,""),
  19804. BUILDIN_DEF(get_githash,""),
  19805. BUILDIN_DEF(freeloop,"?"),
  19806. BUILDIN_DEF(getrandgroupitem,"i??"),
  19807. BUILDIN_DEF(cleanmap,"s"),
  19808. BUILDIN_DEF2(cleanmap,"cleanarea","siiii"),
  19809. BUILDIN_DEF(npcskill,"viii"),
  19810. BUILDIN_DEF(consumeitem,"v?"),
  19811. BUILDIN_DEF(delequip,"i?"),
  19812. BUILDIN_DEF(breakequip,"i?"),
  19813. BUILDIN_DEF(sit,"?"),
  19814. BUILDIN_DEF(stand,"?"),
  19815. //@commands (script based)
  19816. BUILDIN_DEF(bindatcmd, "ss??"),
  19817. BUILDIN_DEF(unbindatcmd, "s"),
  19818. BUILDIN_DEF(useatcmd, "s"),
  19819. //Quest Log System [Inkfish]
  19820. BUILDIN_DEF(questinfo, "ii??"),
  19821. BUILDIN_DEF(setquest, "i?"),
  19822. BUILDIN_DEF(erasequest, "i?"),
  19823. BUILDIN_DEF(completequest, "i?"),
  19824. BUILDIN_DEF(checkquest, "i??"),
  19825. BUILDIN_DEF(isbegin_quest,"i?"),
  19826. BUILDIN_DEF(changequest, "ii?"),
  19827. BUILDIN_DEF(showevent, "i??"),
  19828. //Bound items [Xantara] & [Akinari]
  19829. BUILDIN_DEF2(getitem,"getitembound","vii?"),
  19830. BUILDIN_DEF2(getitem2,"getitembound2","viiiiiiiii?"),
  19831. BUILDIN_DEF(countbound, "??"),
  19832. // Party related
  19833. BUILDIN_DEF(party_create,"s???"),
  19834. BUILDIN_DEF(party_addmember,"ii"),
  19835. BUILDIN_DEF(party_delmember,"??"),
  19836. BUILDIN_DEF(party_changeleader,"ii"),
  19837. BUILDIN_DEF(party_changeoption,"iii"),
  19838. BUILDIN_DEF(party_destroy,"i"),
  19839. // Clan system
  19840. BUILDIN_DEF(clan_join,"i?"),
  19841. BUILDIN_DEF(clan_leave,"?"),
  19842. BUILDIN_DEF(is_clientver,"ii?"),
  19843. BUILDIN_DEF2(montransform, "transform", "vi?????"), // Monster Transform [malufett/Hercules]
  19844. BUILDIN_DEF2(montransform, "active_transform", "vi?????"),
  19845. BUILDIN_DEF(vip_status,"i?"),
  19846. BUILDIN_DEF(vip_time,"i?"),
  19847. BUILDIN_DEF(bonus_script,"si????"),
  19848. BUILDIN_DEF(bonus_script_clear,"??"),
  19849. BUILDIN_DEF(getgroupitem,"i?"),
  19850. BUILDIN_DEF(enable_command,""),
  19851. BUILDIN_DEF(disable_command,""),
  19852. BUILDIN_DEF(getguildmember,"i??"),
  19853. BUILDIN_DEF(addspiritball,"ii?"),
  19854. BUILDIN_DEF(delspiritball,"i?"),
  19855. BUILDIN_DEF(countspiritball,"?"),
  19856. BUILDIN_DEF(mergeitem,"?"),
  19857. BUILDIN_DEF(mergeitem2,"??"),
  19858. BUILDIN_DEF(npcshopupdate,"sii?"),
  19859. BUILDIN_DEF(getattachedrid,""),
  19860. BUILDIN_DEF(getvar,"vi"),
  19861. BUILDIN_DEF(showscript,"s?"),
  19862. BUILDIN_DEF(ignoretimeout,"i?"),
  19863. BUILDIN_DEF(geteleminfo,"i?"),
  19864. BUILDIN_DEF(setquestinfo_level,"iii"),
  19865. BUILDIN_DEF(setquestinfo_req,"iii*"),
  19866. BUILDIN_DEF(setquestinfo_job,"ii*"),
  19867. BUILDIN_DEF(opendressroom,"i?"),
  19868. BUILDIN_DEF(navigateto,"s???????"),
  19869. BUILDIN_DEF(getguildalliance,"ii"),
  19870. BUILDIN_DEF(adopt,"vv"),
  19871. BUILDIN_DEF(getexp2,"ii?"),
  19872. BUILDIN_DEF(recalculatestat,""),
  19873. BUILDIN_DEF(hateffect,"ii"),
  19874. BUILDIN_DEF(getrandomoptinfo, "i"),
  19875. BUILDIN_DEF(getequiprandomoption, "iii?"),
  19876. BUILDIN_DEF(setrandomoption,"iiiii?"),
  19877. BUILDIN_DEF(needed_status_point,"ii?"),
  19878. BUILDIN_DEF(jobcanentermap,"s?"),
  19879. BUILDIN_DEF(openstorage2,"ii?"),
  19880. // WoE TE
  19881. BUILDIN_DEF(agitstart3,""),
  19882. BUILDIN_DEF(agitend3,""),
  19883. BUILDIN_DEF(agitcheck3,""),
  19884. BUILDIN_DEF(gvgon3,"s"),
  19885. BUILDIN_DEF(gvgoff3,"s"),
  19886. #include "../custom/script_def.inc"
  19887. {NULL,NULL,NULL},
  19888. };