script.c 609 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327133281332913330133311333213333133341333513336133371333813339133401334113342133431334413345133461334713348133491335013351133521335313354133551335613357133581335913360133611336213363133641336513366133671336813369133701337113372133731337413375133761337713378133791338013381133821338313384133851338613387133881338913390133911339213393133941339513396133971339813399134001340113402134031340413405134061340713408134091341013411134121341313414134151341613417134181341913420134211342213423134241342513426134271342813429134301343113432134331343413435134361343713438134391344013441134421344313444134451344613447134481344913450134511345213453134541345513456134571345813459134601346113462134631346413465134661346713468134691347013471134721347313474134751347613477134781347913480134811348213483134841348513486134871348813489134901349113492134931349413495134961349713498134991350013501135021350313504135051350613507135081350913510135111351213513135141351513516135171351813519135201352113522135231352413525135261352713528135291353013531135321353313534135351353613537135381353913540135411354213543135441354513546135471354813549135501355113552135531355413555135561355713558135591356013561135621356313564135651356613567135681356913570135711357213573135741357513576135771357813579135801358113582135831358413585135861358713588135891359013591135921359313594135951359613597135981359913600136011360213603136041360513606136071360813609136101361113612136131361413615136161361713618136191362013621136221362313624136251362613627136281362913630136311363213633136341363513636136371363813639136401364113642136431364413645136461364713648136491365013651136521365313654136551365613657136581365913660136611366213663136641366513666136671366813669136701367113672136731367413675136761367713678136791368013681136821368313684136851368613687136881368913690136911369213693136941369513696136971369813699137001370113702137031370413705137061370713708137091371013711137121371313714137151371613717137181371913720137211372213723137241372513726137271372813729137301373113732137331373413735137361373713738137391374013741137421374313744137451374613747137481374913750137511375213753137541375513756137571375813759137601376113762137631376413765137661376713768137691377013771137721377313774137751377613777137781377913780137811378213783137841378513786137871378813789137901379113792137931379413795137961379713798137991380013801138021380313804138051380613807138081380913810138111381213813138141381513816138171381813819138201382113822138231382413825138261382713828138291383013831138321383313834138351383613837138381383913840138411384213843138441384513846138471384813849138501385113852138531385413855138561385713858138591386013861138621386313864138651386613867138681386913870138711387213873138741387513876138771387813879138801388113882138831388413885138861388713888138891389013891138921389313894138951389613897138981389913900139011390213903139041390513906139071390813909139101391113912139131391413915139161391713918139191392013921139221392313924139251392613927139281392913930139311393213933139341393513936139371393813939139401394113942139431394413945139461394713948139491395013951139521395313954139551395613957139581395913960139611396213963139641396513966139671396813969139701397113972139731397413975139761397713978139791398013981139821398313984139851398613987139881398913990139911399213993139941399513996139971399813999140001400114002140031400414005140061400714008140091401014011140121401314014140151401614017140181401914020140211402214023140241402514026140271402814029140301403114032140331403414035140361403714038140391404014041140421404314044140451404614047140481404914050140511405214053140541405514056140571405814059140601406114062140631406414065140661406714068140691407014071140721407314074140751407614077140781407914080140811408214083140841408514086140871408814089140901409114092140931409414095140961409714098140991410014101141021410314104141051410614107141081410914110141111411214113141141411514116141171411814119141201412114122141231412414125141261412714128141291413014131141321413314134141351413614137141381413914140141411414214143141441414514146141471414814149141501415114152141531415414155141561415714158141591416014161141621416314164141651416614167141681416914170141711417214173141741417514176141771417814179141801418114182141831418414185141861418714188141891419014191141921419314194141951419614197141981419914200142011420214203142041420514206142071420814209142101421114212142131421414215142161421714218142191422014221142221422314224142251422614227142281422914230142311423214233142341423514236142371423814239142401424114242142431424414245142461424714248142491425014251142521425314254142551425614257142581425914260142611426214263142641426514266142671426814269142701427114272142731427414275142761427714278142791428014281142821428314284142851428614287142881428914290142911429214293142941429514296142971429814299143001430114302143031430414305143061430714308143091431014311143121431314314143151431614317143181431914320143211432214323143241432514326143271432814329143301433114332143331433414335143361433714338143391434014341143421434314344143451434614347143481434914350143511435214353143541435514356143571435814359143601436114362143631436414365143661436714368143691437014371143721437314374143751437614377143781437914380143811438214383143841438514386143871438814389143901439114392143931439414395143961439714398143991440014401144021440314404144051440614407144081440914410144111441214413144141441514416144171441814419144201442114422144231442414425144261442714428144291443014431144321443314434144351443614437144381443914440144411444214443144441444514446144471444814449144501445114452144531445414455144561445714458144591446014461144621446314464144651446614467144681446914470144711447214473144741447514476144771447814479144801448114482144831448414485144861448714488144891449014491144921449314494144951449614497144981449914500145011450214503145041450514506145071450814509145101451114512145131451414515145161451714518145191452014521145221452314524145251452614527145281452914530145311453214533145341453514536145371453814539145401454114542145431454414545145461454714548145491455014551145521455314554145551455614557145581455914560145611456214563145641456514566145671456814569145701457114572145731457414575145761457714578145791458014581145821458314584145851458614587145881458914590145911459214593145941459514596145971459814599146001460114602146031460414605146061460714608146091461014611146121461314614146151461614617146181461914620146211462214623146241462514626146271462814629146301463114632146331463414635146361463714638146391464014641146421464314644146451464614647146481464914650146511465214653146541465514656146571465814659146601466114662146631466414665146661466714668146691467014671146721467314674146751467614677146781467914680146811468214683146841468514686146871468814689146901469114692146931469414695146961469714698146991470014701147021470314704147051470614707147081470914710147111471214713147141471514716147171471814719147201472114722147231472414725147261472714728147291473014731147321473314734147351473614737147381473914740147411474214743147441474514746147471474814749147501475114752147531475414755147561475714758147591476014761147621476314764147651476614767147681476914770147711477214773147741477514776147771477814779147801478114782147831478414785147861478714788147891479014791147921479314794147951479614797147981479914800148011480214803148041480514806148071480814809148101481114812148131481414815148161481714818148191482014821148221482314824148251482614827148281482914830148311483214833148341483514836148371483814839148401484114842148431484414845148461484714848148491485014851148521485314854148551485614857148581485914860148611486214863148641486514866148671486814869148701487114872148731487414875148761487714878148791488014881148821488314884148851488614887148881488914890148911489214893148941489514896148971489814899149001490114902149031490414905149061490714908149091491014911149121491314914149151491614917149181491914920149211492214923149241492514926149271492814929149301493114932149331493414935149361493714938149391494014941149421494314944149451494614947149481494914950149511495214953149541495514956149571495814959149601496114962149631496414965149661496714968149691497014971149721497314974149751497614977149781497914980149811498214983149841498514986149871498814989149901499114992149931499414995149961499714998149991500015001150021500315004150051500615007150081500915010150111501215013150141501515016150171501815019150201502115022150231502415025150261502715028150291503015031150321503315034150351503615037150381503915040150411504215043150441504515046150471504815049150501505115052150531505415055150561505715058150591506015061150621506315064150651506615067150681506915070150711507215073150741507515076150771507815079150801508115082150831508415085150861508715088150891509015091150921509315094150951509615097150981509915100151011510215103151041510515106151071510815109151101511115112151131511415115151161511715118151191512015121151221512315124151251512615127151281512915130151311513215133151341513515136151371513815139151401514115142151431514415145151461514715148151491515015151151521515315154151551515615157151581515915160151611516215163151641516515166151671516815169151701517115172151731517415175151761517715178151791518015181151821518315184151851518615187151881518915190151911519215193151941519515196151971519815199152001520115202152031520415205152061520715208152091521015211152121521315214152151521615217152181521915220152211522215223152241522515226152271522815229152301523115232152331523415235152361523715238152391524015241152421524315244152451524615247152481524915250152511525215253152541525515256152571525815259152601526115262152631526415265152661526715268152691527015271152721527315274152751527615277152781527915280152811528215283152841528515286152871528815289152901529115292152931529415295152961529715298152991530015301153021530315304153051530615307153081530915310153111531215313153141531515316153171531815319153201532115322153231532415325153261532715328153291533015331153321533315334153351533615337153381533915340153411534215343153441534515346153471534815349153501535115352153531535415355153561535715358153591536015361153621536315364153651536615367153681536915370153711537215373153741537515376153771537815379153801538115382153831538415385153861538715388153891539015391153921539315394153951539615397153981539915400154011540215403154041540515406154071540815409154101541115412154131541415415154161541715418154191542015421154221542315424154251542615427154281542915430154311543215433154341543515436154371543815439154401544115442154431544415445154461544715448154491545015451154521545315454154551545615457154581545915460154611546215463154641546515466154671546815469154701547115472154731547415475154761547715478154791548015481154821548315484154851548615487154881548915490154911549215493154941549515496154971549815499155001550115502155031550415505155061550715508155091551015511155121551315514155151551615517155181551915520155211552215523155241552515526155271552815529155301553115532155331553415535155361553715538155391554015541155421554315544155451554615547155481554915550155511555215553155541555515556155571555815559155601556115562155631556415565155661556715568155691557015571155721557315574155751557615577155781557915580155811558215583155841558515586155871558815589155901559115592155931559415595155961559715598155991560015601156021560315604156051560615607156081560915610156111561215613156141561515616156171561815619156201562115622156231562415625156261562715628156291563015631156321563315634156351563615637156381563915640156411564215643156441564515646156471564815649156501565115652156531565415655156561565715658156591566015661156621566315664156651566615667156681566915670156711567215673156741567515676156771567815679156801568115682156831568415685156861568715688156891569015691156921569315694156951569615697156981569915700157011570215703157041570515706157071570815709157101571115712157131571415715157161571715718157191572015721157221572315724157251572615727157281572915730157311573215733157341573515736157371573815739157401574115742157431574415745157461574715748157491575015751157521575315754157551575615757157581575915760157611576215763157641576515766157671576815769157701577115772157731577415775157761577715778157791578015781157821578315784157851578615787157881578915790157911579215793157941579515796157971579815799158001580115802158031580415805158061580715808158091581015811158121581315814158151581615817158181581915820158211582215823158241582515826158271582815829158301583115832158331583415835158361583715838158391584015841158421584315844158451584615847158481584915850158511585215853158541585515856158571585815859158601586115862158631586415865158661586715868158691587015871158721587315874158751587615877158781587915880158811588215883158841588515886158871588815889158901589115892158931589415895158961589715898158991590015901159021590315904159051590615907159081590915910159111591215913159141591515916159171591815919159201592115922159231592415925159261592715928159291593015931159321593315934159351593615937159381593915940159411594215943159441594515946159471594815949159501595115952159531595415955159561595715958159591596015961159621596315964159651596615967159681596915970159711597215973159741597515976159771597815979159801598115982159831598415985159861598715988159891599015991159921599315994159951599615997159981599916000160011600216003160041600516006160071600816009160101601116012160131601416015160161601716018160191602016021160221602316024160251602616027160281602916030160311603216033160341603516036160371603816039160401604116042160431604416045160461604716048160491605016051160521605316054160551605616057160581605916060160611606216063160641606516066160671606816069160701607116072160731607416075160761607716078160791608016081160821608316084160851608616087160881608916090160911609216093160941609516096160971609816099161001610116102161031610416105161061610716108161091611016111161121611316114161151611616117161181611916120161211612216123161241612516126161271612816129161301613116132161331613416135161361613716138161391614016141161421614316144161451614616147161481614916150161511615216153161541615516156161571615816159161601616116162161631616416165161661616716168161691617016171161721617316174161751617616177161781617916180161811618216183161841618516186161871618816189161901619116192161931619416195161961619716198161991620016201162021620316204162051620616207162081620916210162111621216213162141621516216162171621816219162201622116222162231622416225162261622716228162291623016231162321623316234162351623616237162381623916240162411624216243162441624516246162471624816249162501625116252162531625416255162561625716258162591626016261162621626316264162651626616267162681626916270162711627216273162741627516276162771627816279162801628116282162831628416285162861628716288162891629016291162921629316294162951629616297162981629916300163011630216303163041630516306163071630816309163101631116312163131631416315163161631716318163191632016321163221632316324163251632616327163281632916330163311633216333163341633516336163371633816339163401634116342163431634416345163461634716348163491635016351163521635316354163551635616357163581635916360163611636216363163641636516366163671636816369163701637116372163731637416375163761637716378163791638016381163821638316384163851638616387163881638916390163911639216393163941639516396163971639816399164001640116402164031640416405164061640716408164091641016411164121641316414164151641616417164181641916420164211642216423164241642516426164271642816429164301643116432164331643416435164361643716438164391644016441164421644316444164451644616447164481644916450164511645216453164541645516456164571645816459164601646116462164631646416465164661646716468164691647016471164721647316474164751647616477164781647916480164811648216483164841648516486164871648816489164901649116492164931649416495164961649716498164991650016501165021650316504165051650616507165081650916510165111651216513165141651516516165171651816519165201652116522165231652416525165261652716528165291653016531165321653316534165351653616537165381653916540165411654216543165441654516546165471654816549165501655116552165531655416555165561655716558165591656016561165621656316564165651656616567165681656916570165711657216573165741657516576165771657816579165801658116582165831658416585165861658716588165891659016591165921659316594165951659616597165981659916600166011660216603166041660516606166071660816609166101661116612166131661416615166161661716618166191662016621166221662316624166251662616627166281662916630166311663216633166341663516636166371663816639166401664116642166431664416645166461664716648166491665016651166521665316654166551665616657166581665916660166611666216663166641666516666166671666816669166701667116672166731667416675166761667716678166791668016681166821668316684166851668616687166881668916690166911669216693166941669516696166971669816699167001670116702167031670416705167061670716708167091671016711167121671316714167151671616717167181671916720167211672216723167241672516726167271672816729167301673116732167331673416735167361673716738167391674016741167421674316744167451674616747167481674916750167511675216753167541675516756167571675816759167601676116762167631676416765167661676716768167691677016771167721677316774167751677616777167781677916780167811678216783167841678516786167871678816789167901679116792167931679416795167961679716798167991680016801168021680316804168051680616807168081680916810168111681216813168141681516816168171681816819168201682116822168231682416825168261682716828168291683016831168321683316834168351683616837168381683916840168411684216843168441684516846168471684816849168501685116852168531685416855168561685716858168591686016861168621686316864168651686616867168681686916870168711687216873168741687516876168771687816879168801688116882168831688416885168861688716888168891689016891168921689316894168951689616897168981689916900169011690216903169041690516906169071690816909169101691116912169131691416915169161691716918169191692016921169221692316924169251692616927169281692916930169311693216933169341693516936169371693816939169401694116942169431694416945169461694716948169491695016951169521695316954169551695616957169581695916960169611696216963169641696516966169671696816969169701697116972169731697416975169761697716978169791698016981169821698316984169851698616987169881698916990169911699216993169941699516996169971699816999170001700117002170031700417005170061700717008170091701017011170121701317014170151701617017170181701917020170211702217023170241702517026170271702817029170301703117032170331703417035170361703717038170391704017041170421704317044170451704617047170481704917050170511705217053170541705517056170571705817059170601706117062170631706417065170661706717068170691707017071170721707317074170751707617077170781707917080170811708217083170841708517086170871708817089170901709117092170931709417095170961709717098170991710017101171021710317104171051710617107171081710917110171111711217113171141711517116171171711817119171201712117122171231712417125171261712717128171291713017131171321713317134171351713617137171381713917140171411714217143171441714517146171471714817149171501715117152171531715417155171561715717158171591716017161171621716317164171651716617167171681716917170171711717217173171741717517176171771717817179171801718117182171831718417185171861718717188171891719017191171921719317194171951719617197171981719917200172011720217203172041720517206172071720817209172101721117212172131721417215172161721717218172191722017221172221722317224172251722617227172281722917230172311723217233172341723517236172371723817239172401724117242172431724417245172461724717248172491725017251172521725317254172551725617257172581725917260172611726217263172641726517266172671726817269172701727117272172731727417275172761727717278172791728017281172821728317284172851728617287172881728917290172911729217293172941729517296172971729817299173001730117302173031730417305173061730717308173091731017311173121731317314173151731617317173181731917320173211732217323173241732517326173271732817329173301733117332173331733417335173361733717338173391734017341173421734317344173451734617347173481734917350173511735217353173541735517356173571735817359173601736117362173631736417365173661736717368173691737017371173721737317374173751737617377173781737917380173811738217383173841738517386173871738817389173901739117392173931739417395173961739717398173991740017401174021740317404174051740617407174081740917410174111741217413174141741517416174171741817419174201742117422174231742417425174261742717428174291743017431174321743317434174351743617437174381743917440174411744217443174441744517446174471744817449174501745117452174531745417455174561745717458174591746017461174621746317464174651746617467174681746917470174711747217473174741747517476174771747817479174801748117482174831748417485174861748717488174891749017491174921749317494174951749617497174981749917500175011750217503175041750517506175071750817509175101751117512175131751417515175161751717518175191752017521175221752317524175251752617527175281752917530175311753217533175341753517536175371753817539175401754117542175431754417545175461754717548175491755017551175521755317554175551755617557175581755917560175611756217563175641756517566175671756817569175701757117572175731757417575175761757717578175791758017581175821758317584175851758617587175881758917590175911759217593175941759517596175971759817599176001760117602176031760417605176061760717608176091761017611176121761317614176151761617617176181761917620176211762217623176241762517626176271762817629176301763117632176331763417635176361763717638176391764017641176421764317644176451764617647176481764917650176511765217653176541765517656176571765817659176601766117662176631766417665176661766717668176691767017671176721767317674176751767617677176781767917680176811768217683176841768517686176871768817689176901769117692176931769417695176961769717698176991770017701177021770317704177051770617707177081770917710177111771217713177141771517716177171771817719177201772117722177231772417725177261772717728177291773017731177321773317734177351773617737177381773917740177411774217743177441774517746177471774817749177501775117752177531775417755177561775717758177591776017761177621776317764177651776617767177681776917770177711777217773177741777517776177771777817779177801778117782177831778417785177861778717788177891779017791177921779317794177951779617797177981779917800178011780217803178041780517806178071780817809178101781117812178131781417815178161781717818178191782017821178221782317824178251782617827178281782917830178311783217833178341783517836178371783817839178401784117842178431784417845178461784717848178491785017851178521785317854178551785617857178581785917860178611786217863178641786517866178671786817869178701787117872178731787417875178761787717878178791788017881178821788317884178851788617887178881788917890178911789217893178941789517896178971789817899179001790117902179031790417905179061790717908179091791017911179121791317914179151791617917179181791917920179211792217923179241792517926179271792817929179301793117932179331793417935179361793717938179391794017941179421794317944179451794617947179481794917950179511795217953179541795517956179571795817959179601796117962179631796417965179661796717968179691797017971179721797317974179751797617977179781797917980179811798217983179841798517986179871798817989179901799117992179931799417995179961799717998179991800018001180021800318004180051800618007180081800918010180111801218013180141801518016180171801818019180201802118022180231802418025180261802718028180291803018031180321803318034180351803618037180381803918040180411804218043180441804518046180471804818049180501805118052180531805418055180561805718058180591806018061180621806318064180651806618067180681806918070180711807218073180741807518076180771807818079180801808118082180831808418085180861808718088180891809018091180921809318094180951809618097180981809918100181011810218103181041810518106181071810818109181101811118112181131811418115181161811718118181191812018121181221812318124181251812618127181281812918130181311813218133181341813518136181371813818139181401814118142181431814418145181461814718148181491815018151181521815318154181551815618157181581815918160181611816218163181641816518166181671816818169181701817118172181731817418175181761817718178181791818018181181821818318184181851818618187181881818918190181911819218193181941819518196181971819818199182001820118202182031820418205182061820718208182091821018211182121821318214182151821618217182181821918220182211822218223182241822518226182271822818229182301823118232182331823418235182361823718238182391824018241182421824318244182451824618247182481824918250182511825218253182541825518256182571825818259182601826118262182631826418265182661826718268182691827018271182721827318274182751827618277182781827918280182811828218283182841828518286182871828818289182901829118292182931829418295182961829718298182991830018301183021830318304183051830618307183081830918310183111831218313183141831518316183171831818319183201832118322183231832418325183261832718328183291833018331183321833318334183351833618337183381833918340183411834218343183441834518346183471834818349183501835118352183531835418355183561835718358183591836018361183621836318364183651836618367183681836918370183711837218373183741837518376183771837818379183801838118382183831838418385183861838718388183891839018391183921839318394183951839618397183981839918400184011840218403184041840518406184071840818409184101841118412184131841418415184161841718418184191842018421184221842318424184251842618427184281842918430184311843218433184341843518436184371843818439184401844118442184431844418445184461844718448184491845018451184521845318454184551845618457184581845918460184611846218463184641846518466184671846818469184701847118472184731847418475184761847718478184791848018481184821848318484184851848618487184881848918490184911849218493184941849518496184971849818499185001850118502185031850418505185061850718508185091851018511185121851318514185151851618517185181851918520185211852218523185241852518526185271852818529185301853118532185331853418535185361853718538185391854018541185421854318544185451854618547185481854918550185511855218553185541855518556185571855818559185601856118562185631856418565185661856718568185691857018571185721857318574185751857618577185781857918580185811858218583185841858518586185871858818589185901859118592185931859418595185961859718598185991860018601186021860318604186051860618607186081860918610186111861218613186141861518616186171861818619186201862118622186231862418625186261862718628186291863018631186321863318634186351863618637186381863918640186411864218643186441864518646186471864818649186501865118652186531865418655186561865718658186591866018661186621866318664186651866618667186681866918670186711867218673186741867518676186771867818679186801868118682186831868418685186861868718688186891869018691186921869318694186951869618697186981869918700187011870218703187041870518706187071870818709187101871118712187131871418715187161871718718187191872018721187221872318724187251872618727187281872918730187311873218733187341873518736187371873818739187401874118742187431874418745187461874718748187491875018751187521875318754187551875618757187581875918760187611876218763187641876518766187671876818769187701877118772187731877418775187761877718778187791878018781187821878318784187851878618787187881878918790187911879218793187941879518796187971879818799188001880118802188031880418805188061880718808188091881018811188121881318814188151881618817188181881918820188211882218823188241882518826188271882818829188301883118832188331883418835188361883718838188391884018841188421884318844188451884618847188481884918850188511885218853188541885518856188571885818859188601886118862188631886418865188661886718868188691887018871188721887318874188751887618877188781887918880188811888218883188841888518886188871888818889188901889118892188931889418895188961889718898188991890018901189021890318904189051890618907189081890918910189111891218913189141891518916189171891818919189201892118922189231892418925189261892718928189291893018931189321893318934189351893618937189381893918940189411894218943189441894518946189471894818949189501895118952189531895418955189561895718958189591896018961189621896318964189651896618967189681896918970189711897218973189741897518976189771897818979189801898118982189831898418985189861898718988189891899018991189921899318994189951899618997189981899919000190011900219003190041900519006190071900819009190101901119012190131901419015190161901719018190191902019021190221902319024190251902619027190281902919030190311903219033190341903519036190371903819039190401904119042190431904419045190461904719048190491905019051190521905319054190551905619057190581905919060190611906219063190641906519066190671906819069190701907119072190731907419075190761907719078190791908019081190821908319084190851908619087190881908919090190911909219093190941909519096190971909819099191001910119102191031910419105191061910719108191091911019111191121911319114191151911619117191181911919120191211912219123191241912519126191271912819129191301913119132191331913419135191361913719138191391914019141191421914319144191451914619147191481914919150191511915219153191541915519156191571915819159191601916119162191631916419165191661916719168191691917019171191721917319174191751917619177191781917919180191811918219183191841918519186191871918819189191901919119192191931919419195191961919719198191991920019201192021920319204192051920619207192081920919210192111921219213192141921519216192171921819219192201922119222192231922419225192261922719228192291923019231192321923319234192351923619237192381923919240192411924219243192441924519246192471924819249192501925119252192531925419255192561925719258192591926019261192621926319264192651926619267192681926919270192711927219273192741927519276192771927819279192801928119282192831928419285192861928719288192891929019291192921929319294192951929619297192981929919300193011930219303193041930519306193071930819309193101931119312193131931419315193161931719318193191932019321193221932319324193251932619327193281932919330193311933219333193341933519336193371933819339193401934119342193431934419345193461934719348193491935019351193521935319354193551935619357193581935919360193611936219363193641936519366193671936819369193701937119372193731937419375193761937719378193791938019381193821938319384193851938619387193881938919390193911939219393193941939519396193971939819399194001940119402194031940419405194061940719408194091941019411194121941319414194151941619417194181941919420194211942219423194241942519426194271942819429194301943119432194331943419435194361943719438194391944019441194421944319444194451944619447194481944919450194511945219453194541945519456194571945819459194601946119462194631946419465194661946719468194691947019471194721947319474194751947619477194781947919480194811948219483194841948519486194871948819489194901949119492194931949419495194961949719498194991950019501195021950319504195051950619507195081950919510195111951219513195141951519516195171951819519195201952119522195231952419525195261952719528195291953019531195321953319534195351953619537195381953919540195411954219543195441954519546195471954819549195501955119552195531955419555195561955719558195591956019561195621956319564195651956619567195681956919570195711957219573195741957519576195771957819579195801958119582195831958419585195861958719588195891959019591195921959319594195951959619597195981959919600196011960219603196041960519606196071960819609196101961119612196131961419615196161961719618196191962019621196221962319624196251962619627196281962919630196311963219633196341963519636196371963819639196401964119642196431964419645196461964719648196491965019651196521965319654196551965619657196581965919660196611966219663196641966519666196671966819669196701967119672196731967419675196761967719678196791968019681196821968319684196851968619687196881968919690196911969219693196941969519696196971969819699197001970119702197031970419705197061970719708197091971019711197121971319714197151971619717197181971919720197211972219723197241972519726197271972819729197301973119732197331973419735197361973719738197391974019741197421974319744197451974619747197481974919750197511975219753197541975519756197571975819759197601976119762197631976419765197661976719768197691977019771197721977319774197751977619777197781977919780197811978219783197841978519786197871978819789197901979119792197931979419795197961979719798197991980019801198021980319804198051980619807198081980919810198111981219813198141981519816198171981819819198201982119822198231982419825198261982719828198291983019831198321983319834198351983619837198381983919840198411984219843198441984519846198471984819849198501985119852198531985419855198561985719858198591986019861198621986319864198651986619867198681986919870198711987219873198741987519876198771987819879198801988119882198831988419885198861988719888198891989019891198921989319894198951989619897198981989919900199011990219903199041990519906199071990819909199101991119912199131991419915199161991719918199191992019921199221992319924199251992619927199281992919930199311993219933199341993519936199371993819939199401994119942199431994419945199461994719948199491995019951199521995319954199551995619957199581995919960199611996219963199641996519966199671996819969199701997119972199731997419975199761997719978199791998019981199821998319984199851998619987199881998919990199911999219993199941999519996199971999819999200002000120002200032000420005200062000720008200092001020011200122001320014200152001620017200182001920020200212002220023200242002520026200272002820029200302003120032200332003420035200362003720038200392004020041200422004320044200452004620047200482004920050200512005220053200542005520056200572005820059200602006120062200632006420065200662006720068200692007020071200722007320074200752007620077200782007920080200812008220083200842008520086200872008820089200902009120092200932009420095200962009720098200992010020101201022010320104201052010620107201082010920110201112011220113201142011520116201172011820119201202012120122201232012420125201262012720128201292013020131201322013320134201352013620137201382013920140201412014220143201442014520146201472014820149201502015120152201532015420155201562015720158201592016020161201622016320164201652016620167201682016920170201712017220173201742017520176201772017820179201802018120182201832018420185201862018720188201892019020191201922019320194201952019620197201982019920200202012020220203202042020520206202072020820209202102021120212202132021420215202162021720218202192022020221202222022320224202252022620227202282022920230202312023220233202342023520236202372023820239202402024120242202432024420245202462024720248202492025020251202522025320254202552025620257202582025920260202612026220263202642026520266202672026820269202702027120272202732027420275202762027720278202792028020281202822028320284202852028620287202882028920290202912029220293202942029520296202972029820299203002030120302203032030420305203062030720308203092031020311203122031320314203152031620317203182031920320203212032220323203242032520326203272032820329203302033120332203332033420335203362033720338203392034020341203422034320344203452034620347203482034920350203512035220353203542035520356203572035820359203602036120362203632036420365203662036720368203692037020371203722037320374203752037620377203782037920380203812038220383203842038520386203872038820389203902039120392203932039420395203962039720398203992040020401204022040320404204052040620407204082040920410204112041220413204142041520416204172041820419204202042120422204232042420425204262042720428204292043020431204322043320434204352043620437204382043920440204412044220443204442044520446204472044820449204502045120452204532045420455204562045720458204592046020461204622046320464204652046620467204682046920470204712047220473204742047520476204772047820479204802048120482204832048420485204862048720488204892049020491204922049320494204952049620497204982049920500205012050220503205042050520506205072050820509205102051120512205132051420515205162051720518205192052020521205222052320524205252052620527205282052920530205312053220533205342053520536205372053820539205402054120542205432054420545205462054720548205492055020551205522055320554205552055620557205582055920560205612056220563205642056520566205672056820569205702057120572205732057420575205762057720578205792058020581205822058320584205852058620587205882058920590205912059220593205942059520596205972059820599206002060120602206032060420605206062060720608206092061020611206122061320614206152061620617206182061920620206212062220623206242062520626206272062820629206302063120632206332063420635206362063720638206392064020641206422064320644206452064620647206482064920650206512065220653206542065520656206572065820659206602066120662206632066420665206662066720668206692067020671206722067320674206752067620677206782067920680206812068220683206842068520686206872068820689206902069120692206932069420695206962069720698206992070020701207022070320704207052070620707207082070920710207112071220713207142071520716207172071820719207202072120722207232072420725207262072720728207292073020731207322073320734207352073620737207382073920740207412074220743207442074520746207472074820749207502075120752207532075420755207562075720758207592076020761207622076320764207652076620767207682076920770207712077220773207742077520776207772077820779207802078120782207832078420785207862078720788207892079020791207922079320794207952079620797207982079920800208012080220803208042080520806208072080820809208102081120812208132081420815208162081720818208192082020821208222082320824208252082620827208282082920830208312083220833208342083520836208372083820839208402084120842208432084420845208462084720848208492085020851208522085320854208552085620857208582085920860208612086220863208642086520866208672086820869208702087120872208732087420875208762087720878208792088020881208822088320884208852088620887208882088920890208912089220893208942089520896208972089820899209002090120902209032090420905209062090720908209092091020911209122091320914209152091620917209182091920920209212092220923209242092520926209272092820929209302093120932209332093420935209362093720938209392094020941209422094320944209452094620947209482094920950209512095220953209542095520956209572095820959209602096120962209632096420965209662096720968209692097020971209722097320974209752097620977209782097920980209812098220983209842098520986209872098820989209902099120992209932099420995209962099720998209992100021001210022100321004210052100621007210082100921010210112101221013210142101521016210172101821019210202102121022210232102421025210262102721028210292103021031210322103321034210352103621037210382103921040210412104221043210442104521046210472104821049210502105121052210532105421055210562105721058210592106021061210622106321064210652106621067210682106921070210712107221073210742107521076210772107821079210802108121082210832108421085210862108721088210892109021091210922109321094210952109621097210982109921100211012110221103211042110521106211072110821109211102111121112211132111421115211162111721118211192112021121211222112321124211252112621127211282112921130211312113221133211342113521136211372113821139211402114121142211432114421145211462114721148211492115021151211522115321154211552115621157211582115921160211612116221163211642116521166211672116821169211702117121172211732117421175211762117721178211792118021181211822118321184211852118621187211882118921190211912119221193211942119521196211972119821199212002120121202212032120421205212062120721208212092121021211212122121321214212152121621217212182121921220212212122221223212242122521226212272122821229212302123121232212332123421235212362123721238212392124021241212422124321244212452124621247212482124921250212512125221253212542125521256212572125821259212602126121262212632126421265212662126721268212692127021271212722127321274212752127621277212782127921280212812128221283212842128521286212872128821289212902129121292212932129421295212962129721298212992130021301213022130321304213052130621307213082130921310213112131221313213142131521316213172131821319213202132121322213232132421325213262132721328213292133021331213322133321334213352133621337213382133921340213412134221343213442134521346213472134821349213502135121352213532135421355213562135721358213592136021361213622136321364213652136621367213682136921370213712137221373213742137521376213772137821379213802138121382213832138421385213862138721388213892139021391213922139321394213952139621397213982139921400214012140221403214042140521406214072140821409214102141121412214132141421415214162141721418214192142021421214222142321424214252142621427214282142921430214312143221433214342143521436214372143821439214402144121442214432144421445214462144721448214492145021451214522145321454214552145621457214582145921460214612146221463214642146521466214672146821469214702147121472214732147421475214762147721478214792148021481214822148321484214852148621487214882148921490214912149221493214942149521496214972149821499215002150121502215032150421505215062150721508215092151021511215122151321514215152151621517215182151921520215212152221523215242152521526215272152821529215302153121532215332153421535215362153721538215392154021541215422154321544215452154621547215482154921550215512155221553215542155521556215572155821559215602156121562215632156421565215662156721568215692157021571215722157321574215752157621577215782157921580215812158221583215842158521586215872158821589215902159121592215932159421595215962159721598215992160021601216022160321604216052160621607216082160921610216112161221613216142161521616216172161821619216202162121622216232162421625216262162721628216292163021631216322163321634216352163621637216382163921640216412164221643216442164521646216472164821649216502165121652216532165421655216562165721658216592166021661216622166321664216652166621667216682166921670216712167221673216742167521676216772167821679216802168121682216832168421685216862168721688216892169021691216922169321694216952169621697216982169921700217012170221703217042170521706217072170821709217102171121712217132171421715217162171721718217192172021721217222172321724217252172621727217282172921730217312173221733217342173521736217372173821739217402174121742217432174421745217462174721748217492175021751217522175321754217552175621757217582175921760217612176221763217642176521766217672176821769217702177121772217732177421775217762177721778217792178021781217822178321784217852178621787217882178921790217912179221793217942179521796217972179821799218002180121802218032180421805218062180721808218092181021811218122181321814218152181621817218182181921820218212182221823218242182521826218272182821829218302183121832218332183421835218362183721838218392184021841218422184321844218452184621847218482184921850218512185221853218542185521856218572185821859218602186121862218632186421865218662186721868218692187021871218722187321874218752187621877218782187921880218812188221883218842188521886218872188821889218902189121892218932189421895218962189721898218992190021901219022190321904219052190621907219082190921910219112191221913219142191521916219172191821919219202192121922219232192421925219262192721928219292193021931219322193321934219352193621937219382193921940219412194221943219442194521946219472194821949219502195121952219532195421955219562195721958219592196021961219622196321964219652196621967219682196921970219712197221973219742197521976219772197821979219802198121982219832198421985219862198721988219892199021991219922199321994219952199621997219982199922000220012200222003220042200522006220072200822009220102201122012220132201422015220162201722018220192202022021220222202322024220252202622027220282202922030220312203222033220342203522036220372203822039220402204122042220432204422045220462204722048220492205022051220522205322054220552205622057220582205922060220612206222063220642206522066220672206822069220702207122072220732207422075220762207722078220792208022081220822208322084220852208622087220882208922090220912209222093220942209522096220972209822099221002210122102221032210422105221062210722108221092211022111221122211322114221152211622117221182211922120221212212222123221242212522126221272212822129221302213122132221332213422135221362213722138221392214022141221422214322144221452214622147221482214922150221512215222153221542215522156221572215822159221602216122162221632216422165221662216722168221692217022171221722217322174221752217622177221782217922180221812218222183221842218522186221872218822189221902219122192221932219422195221962219722198221992220022201222022220322204222052220622207222082220922210222112221222213222142221522216222172221822219222202222122222222232222422225222262222722228222292223022231222322223322234222352223622237222382223922240222412224222243222442224522246222472224822249222502225122252222532225422255222562225722258222592226022261222622226322264222652226622267222682226922270222712227222273222742227522276222772227822279222802228122282222832228422285222862228722288222892229022291222922229322294222952229622297222982229922300223012230222303223042230522306223072230822309223102231122312223132231422315223162231722318223192232022321223222232322324223252232622327223282232922330223312233222333223342233522336223372233822339223402234122342223432234422345223462234722348223492235022351223522235322354223552235622357223582235922360223612236222363223642236522366223672236822369223702237122372223732237422375223762237722378223792238022381223822238322384223852238622387223882238922390223912239222393223942239522396223972239822399224002240122402224032240422405224062240722408224092241022411224122241322414224152241622417224182241922420224212242222423224242242522426224272242822429224302243122432224332243422435224362243722438224392244022441224422244322444224452244622447224482244922450224512245222453224542245522456224572245822459224602246122462224632246422465224662246722468224692247022471224722247322474224752247622477224782247922480224812248222483224842248522486224872248822489224902249122492224932249422495224962249722498224992250022501225022250322504225052250622507225082250922510225112251222513225142251522516225172251822519225202252122522225232252422525225262252722528225292253022531225322253322534225352253622537225382253922540225412254222543225442254522546225472254822549225502255122552225532255422555225562255722558225592256022561225622256322564225652256622567225682256922570225712257222573225742257522576225772257822579225802258122582225832258422585225862258722588225892259022591225922259322594225952259622597225982259922600226012260222603226042260522606226072260822609226102261122612226132261422615226162261722618226192262022621226222262322624226252262622627226282262922630226312263222633226342263522636226372263822639226402264122642226432264422645226462264722648226492265022651226522265322654226552265622657226582265922660226612266222663226642266522666226672266822669226702267122672226732267422675226762267722678226792268022681226822268322684226852268622687226882268922690226912269222693226942269522696226972269822699227002270122702227032270422705227062270722708227092271022711227122271322714227152271622717227182271922720227212272222723227242272522726227272272822729227302273122732227332273422735227362273722738227392274022741227422274322744227452274622747227482274922750227512275222753227542275522756227572275822759227602276122762
  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 "date.h" // date type enum, date_get()
  33. #include "itemdb.h"
  34. #include "pc.h"
  35. #include "storage.h"
  36. #include "pet.h"
  37. #include "mapreg.h"
  38. #include "homunculus.h"
  39. #include "instance.h"
  40. #include "mercenary.h"
  41. #include "intif.h"
  42. #include "chat.h"
  43. #include "battleground.h"
  44. #include "party.h"
  45. #include "mail.h"
  46. #include "quest.h"
  47. #include "elemental.h"
  48. #include <math.h>
  49. #include <stdlib.h> // atoi, strtol, strtoll, exit
  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_,false)))
  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 <EVENT_NAME_LENGTH> characters
  183. // PC related
  184. "OnPCDieEvent", //die_event_name
  185. "OnPCKillEvent", //kill_pc_event_name
  186. "OnNPCKillEvent", //kill_mob_event_name
  187. "OnPCLoginEvent", //login_event_name
  188. "OnPCLogoutEvent", //logout_event_name
  189. "OnPCLoadMapEvent", //loadmap_event_name
  190. "OnPCBaseLvUpEvent", //baselvup_event_name
  191. "OnPCJobLvUpEvent", //joblvup_event_name
  192. "OnPCStatCalcEvent", //stat_calc_event_name
  193. // NPC related
  194. "OnTouch_", //ontouch_event_name (runs on first visible char to enter area, picks another char if the first char leaves)
  195. "OnTouch", //ontouch2_event_name (run whenever a char walks into the OnTouch area)
  196. "OnTouchNPC", //ontouchnpc_event_name (run whenever a monster walks into the OnTouch area)
  197. "OnWhisperGlobal", //onwhisper_event_name (is executed when a player sends a whisper message to the NPC)
  198. "OnCommand", //oncommand_event_name (is executed by script command cmdothernpc)
  199. // Init related
  200. "OnInit", //init_event_name (is executed on all npcs when all npcs were loaded)
  201. "OnInterIfInit", //inter_init_event_name (is executed on inter server connection)
  202. "OnInterIfInitOnce", //inter_init_once_event_name (is only executed on the first inter server connection)
  203. // Guild related
  204. "OnGuildBreak", //guild_break_event_name (is executed on all castles of the guild that is broken)
  205. "OnAgitStart", //agit_start_event_name (is executed when WoE FE is started)
  206. "OnAgitInit", //agit_init_event_name (is executed after all castle owning guilds have been loaded)
  207. "OnAgitEnd", //agit_end_event_name (is executed when WoE FE has ended)
  208. "OnAgitStart2", //agit_start2_event_name (is executed when WoE SE is started)
  209. "OnAgitInit2", //agit_init2_event_name (is executed after all castle owning guilds have been loaded)
  210. "OnAgitEnd2", //agit_end2_event_name (is executed when WoE SE has ended)
  211. "OnAgitStart3", //agit_start3_event_name (is executed when WoE TE is started)
  212. "OnAgitInit3", //agit_init3_event_name (is executed after all castle owning guilds have been loaded)
  213. "OnAgitEnd3", //agit_end3_event_name (is executed when WoE TE has ended)
  214. // Timer related
  215. "OnTimer", //timer_event_name (is executed by a timer at the specific second)
  216. "OnMinute", //timer_minute_event_name (is executed by a timer at the specific minute)
  217. "OnHour", //timer_hour_event_name (is executed by a timer at the specific hour)
  218. "OnClock", //timer_clock_event_name (is executed by a timer at the specific hour and minute)
  219. "OnDay", //timer_day_event_name (is executed by a timer at the specific month and day)
  220. "OnSun", //timer_sunday_event_name (is executed by a timer on sunday at the specific hour and minute)
  221. "OnMon", //timer_monday_event_name (is executed by a timer on monday at the specific hour and minute)
  222. "OnTue", //timer_tuesday_event_name (is executed by a timer on tuesday at the specific hour and minute)
  223. "OnWed", //timer_wednesday_event_name (is executed by a timer on wednesday at the specific hour and minute)
  224. "OnThu", //timer_thursday_event_name (is executed by a timer on thursday at the specific hour and minute)
  225. "OnFri", //timer_friday_event_name (is executed by a timer on friday at the specific hour and minute)
  226. "OnSat", //timer_saturday_event_name (is executed by a timer on saturday at the specific hour and minute)
  227. // Instance related
  228. "OnInstanceInit", //instance_init_event_name (is executed right after instance creation)
  229. "OnInstanceDestroy", //instance_destroy_event_name (is executed right before instance destruction)
  230. };
  231. static jmp_buf error_jump;
  232. static char* error_msg;
  233. static const char* error_pos;
  234. static int error_report; // if the error should produce output
  235. // Used by disp_warning_message
  236. static const char* parser_current_src;
  237. static const char* parser_current_file;
  238. static int parser_current_line;
  239. // for advanced scripting support ( nested if, switch, while, for, do-while, function, etc )
  240. // [Eoe / jA 1080, 1081, 1094, 1164]
  241. enum curly_type {
  242. TYPE_NULL = 0,
  243. TYPE_IF,
  244. TYPE_SWITCH,
  245. TYPE_WHILE,
  246. TYPE_FOR,
  247. TYPE_DO,
  248. TYPE_USERFUNC,
  249. TYPE_ARGLIST // function argument list
  250. };
  251. enum e_arglist
  252. {
  253. ARGLIST_UNDEFINED = 0,
  254. ARGLIST_NO_PAREN = 1,
  255. ARGLIST_PAREN = 2,
  256. };
  257. static struct {
  258. struct {
  259. enum curly_type type;
  260. int index;
  261. int count;
  262. int flag;
  263. struct linkdb_node *case_label;
  264. } curly[256]; // Information right parenthesis
  265. int curly_count; // The number of right brackets
  266. int index; // Number of the syntax used in the script
  267. } syntax;
  268. const char* parse_curly_close(const char* p);
  269. const char* parse_syntax_close(const char* p);
  270. const char* parse_syntax_close_sub(const char* p,int* flag);
  271. const char* parse_syntax(const char* p);
  272. static int parse_syntax_for_flag = 0;
  273. extern short current_equip_item_index; //for New CARDS Scripts. It contains Inventory Index of the EQUIP_SCRIPT caller item. [Lupus]
  274. extern unsigned int current_equip_combo_pos;
  275. int potion_flag=0; //For use on Alchemist improved potions/Potion Pitcher. [Skotlex]
  276. int potion_hp=0, potion_per_hp=0, potion_sp=0, potion_per_sp=0;
  277. int potion_target = 0;
  278. unsigned int *generic_ui_array = NULL;
  279. unsigned int generic_ui_array_size = 0;
  280. c_op get_com(unsigned char *script,int *pos);
  281. int get_num(unsigned char *script,int *pos);
  282. typedef struct script_function {
  283. int (*func)(struct script_state *st);
  284. const char *name;
  285. const char *arg;
  286. } script_function;
  287. extern script_function buildin_func[];
  288. #ifdef BETA_THREAD_TEST
  289. /**
  290. * MySQL Query Slave
  291. **/
  292. static SPIN_LOCK queryThreadLock;
  293. static rAthread queryThread = NULL;
  294. static ramutex queryThreadMutex = NULL;
  295. static racond queryThreadCond = NULL;
  296. static volatile int32 queryThreadTerminate = 0;
  297. struct queryThreadEntry {
  298. bool ok;
  299. bool type; /* main db or log db? */
  300. struct script_state *st;
  301. };
  302. /* Ladies and Gentleman the Manager! */
  303. struct {
  304. struct queryThreadEntry **entry;/* array of structs */
  305. int count;
  306. int timer;/* used to receive processed entries */
  307. } queryThreadData;
  308. #endif
  309. /*==========================================
  310. * (Only those needed) local declaration prototype
  311. *------------------------------------------*/
  312. const char* parse_subexpr(const char* p,int limit);
  313. int run_func(struct script_state *st);
  314. unsigned short script_instancegetid(struct script_state *st);
  315. enum {
  316. MF_NOMEMO, //0
  317. MF_NOTELEPORT,
  318. MF_NOSAVE,
  319. MF_NOBRANCH,
  320. MF_NOPENALTY,
  321. MF_NOZENYPENALTY,
  322. MF_PVP,
  323. MF_PVP_NOPARTY,
  324. MF_PVP_NOGUILD,
  325. MF_GVG,
  326. MF_GVG_NOPARTY, //10
  327. MF_NOTRADE,
  328. MF_NOSKILL,
  329. MF_NOWARP,
  330. MF_PARTYLOCK,
  331. MF_NOICEWALL,
  332. MF_SNOW,
  333. MF_FOG,
  334. MF_SAKURA,
  335. MF_LEAVES,
  336. /**
  337. * No longer available, keeping here just in case it's back someday. [Ind]
  338. **/
  339. //MF_RAIN, //20
  340. // 21 free
  341. MF_NOGO = 22,
  342. MF_CLOUDS,
  343. MF_CLOUDS2,
  344. MF_FIREWORKS,
  345. MF_GVG_CASTLE,
  346. MF_GVG_DUNGEON,
  347. MF_NIGHTENABLED,
  348. MF_NOBASEEXP,
  349. MF_NOJOBEXP, //30
  350. MF_NOMOBLOOT,
  351. MF_NOMVPLOOT,
  352. MF_NORETURN,
  353. MF_NOWARPTO,
  354. MF_NIGHTMAREDROP,
  355. MF_RESTRICTED,
  356. MF_NOCOMMAND,
  357. MF_NODROP,
  358. MF_JEXP,
  359. MF_BEXP, //40
  360. MF_NOVENDING,
  361. MF_LOADEVENT,
  362. MF_NOCHAT,
  363. MF_NOEXPPENALTY,
  364. MF_GUILDLOCK,
  365. MF_TOWN,
  366. MF_AUTOTRADE,
  367. MF_ALLOWKS,
  368. MF_MONSTER_NOTELEPORT,
  369. MF_PVP_NOCALCRANK, //50
  370. MF_BATTLEGROUND,
  371. MF_RESET,
  372. MF_CHANNELAUTOJOIN,
  373. MF_NOUSECART,
  374. MF_NOITEMCONSUMPTION,
  375. MF_SUMSTARTMIRACLE,
  376. MF_NOMINEEFFECT,
  377. MF_NOLOCKON,
  378. MF_NOTOMB,
  379. MF_SKILL_DAMAGE, //60
  380. MF_NOCOSTUME,
  381. MF_GVG_TE_CASTLE,
  382. MF_GVG_TE,
  383. MF_HIDEMOBHPBAR,
  384. };
  385. const char* script_op2name(int op)
  386. {
  387. #define RETURN_OP_NAME(type) case type: return #type
  388. switch( op )
  389. {
  390. RETURN_OP_NAME(C_NOP);
  391. RETURN_OP_NAME(C_POS);
  392. RETURN_OP_NAME(C_INT);
  393. RETURN_OP_NAME(C_PARAM);
  394. RETURN_OP_NAME(C_FUNC);
  395. RETURN_OP_NAME(C_STR);
  396. RETURN_OP_NAME(C_CONSTSTR);
  397. RETURN_OP_NAME(C_ARG);
  398. RETURN_OP_NAME(C_NAME);
  399. RETURN_OP_NAME(C_EOL);
  400. RETURN_OP_NAME(C_RETINFO);
  401. RETURN_OP_NAME(C_USERFUNC);
  402. RETURN_OP_NAME(C_USERFUNC_POS);
  403. RETURN_OP_NAME(C_REF);
  404. // operators
  405. RETURN_OP_NAME(C_OP3);
  406. RETURN_OP_NAME(C_LOR);
  407. RETURN_OP_NAME(C_LAND);
  408. RETURN_OP_NAME(C_LE);
  409. RETURN_OP_NAME(C_LT);
  410. RETURN_OP_NAME(C_GE);
  411. RETURN_OP_NAME(C_GT);
  412. RETURN_OP_NAME(C_EQ);
  413. RETURN_OP_NAME(C_NE);
  414. RETURN_OP_NAME(C_XOR);
  415. RETURN_OP_NAME(C_OR);
  416. RETURN_OP_NAME(C_AND);
  417. RETURN_OP_NAME(C_ADD);
  418. RETURN_OP_NAME(C_SUB);
  419. RETURN_OP_NAME(C_MUL);
  420. RETURN_OP_NAME(C_DIV);
  421. RETURN_OP_NAME(C_MOD);
  422. RETURN_OP_NAME(C_NEG);
  423. RETURN_OP_NAME(C_LNOT);
  424. RETURN_OP_NAME(C_NOT);
  425. RETURN_OP_NAME(C_R_SHIFT);
  426. RETURN_OP_NAME(C_L_SHIFT);
  427. RETURN_OP_NAME(C_ADD_POST);
  428. RETURN_OP_NAME(C_SUB_POST);
  429. RETURN_OP_NAME(C_ADD_PRE);
  430. RETURN_OP_NAME(C_SUB_PRE);
  431. default:
  432. ShowDebug("script_op2name: unexpected op=%d\n", op);
  433. return "???";
  434. }
  435. #undef RETURN_OP_NAME
  436. }
  437. #ifdef DEBUG_DUMP_STACK
  438. static void script_dump_stack(struct script_state* st)
  439. {
  440. int i;
  441. ShowMessage("\tstart = %d\n", st->start);
  442. ShowMessage("\tend = %d\n", st->end);
  443. ShowMessage("\tdefsp = %d\n", st->stack->defsp);
  444. ShowMessage("\tsp = %d\n", st->stack->sp);
  445. for( i = 0; i < st->stack->sp; ++i )
  446. {
  447. struct script_data* data = &st->stack->stack_data[i];
  448. ShowMessage("\t[%d] %s", i, script_op2name(data->type));
  449. switch( data->type )
  450. {
  451. case C_INT:
  452. case C_POS:
  453. ShowMessage(" %d\n", data->u.num);
  454. break;
  455. case C_STR:
  456. case C_CONSTSTR:
  457. ShowMessage(" \"%s\"\n", data->u.str);
  458. break;
  459. case C_NAME:
  460. 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));
  461. break;
  462. case C_RETINFO:
  463. {
  464. struct script_retinfo* ri = data->u.ri;
  465. 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);
  466. }
  467. break;
  468. default:
  469. ShowMessage("\n");
  470. break;
  471. }
  472. }
  473. }
  474. #endif
  475. /// Reports on the console the src of a script error.
  476. static void script_reportsrc(struct script_state *st)
  477. {
  478. struct block_list* bl;
  479. if( st->oid == 0 )
  480. return; //Can't report source.
  481. bl = map_id2bl(st->oid);
  482. if( bl == NULL )
  483. return;
  484. switch( bl->type ) {
  485. case BL_NPC:
  486. if( bl->m >= 0 )
  487. ShowDebug("Source (NPC): %s at %s (%d,%d)\n", ((struct npc_data *)bl)->name, map[bl->m].name, bl->x, bl->y);
  488. else
  489. ShowDebug("Source (NPC): %s (invisible/not on a map)\n", ((struct npc_data *)bl)->name);
  490. break;
  491. default:
  492. if( bl->m >= 0 )
  493. 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);
  494. else
  495. ShowDebug("Source (Non-NPC type %d): name %s (invisible/not on a map)\n", bl->type, status_get_name(bl));
  496. break;
  497. }
  498. }
  499. /// Reports on the console information about the script data.
  500. static void script_reportdata(struct script_data* data)
  501. {
  502. if( data == NULL )
  503. return;
  504. switch( data->type ) {
  505. case C_NOP:// no value
  506. ShowDebug("Data: nothing (nil)\n");
  507. break;
  508. case C_INT:// number
  509. ShowDebug("Data: number value=%"PRId64"\n", data->u.num);
  510. break;
  511. case C_STR:
  512. case C_CONSTSTR:// string
  513. if( data->u.str ) {
  514. ShowDebug("Data: string value=\"%s\"\n", data->u.str);
  515. } else {
  516. ShowDebug("Data: string value=NULL\n");
  517. }
  518. break;
  519. case C_NAME:// reference
  520. if( reference_tovariable(data) ) {// variable
  521. const char* name = reference_getname(data);
  522. ShowDebug("Data: variable name='%s' index=%d\n", name, reference_getindex(data));
  523. } else if( reference_toconstant(data) ) {// constant
  524. ShowDebug("Data: constant name='%s' value=%d\n", reference_getname(data), reference_getconstant(data));
  525. } else if( reference_toparam(data) ) {// param
  526. ShowDebug("Data: param name='%s' type=%d\n", reference_getname(data), reference_getparamtype(data));
  527. } else {// ???
  528. ShowDebug("Data: reference name='%s' type=%s\n", reference_getname(data), script_op2name(data->type));
  529. ShowDebug("Please report this!!! - script->str_data.type=%s\n", script_op2name(str_data[reference_getid(data)].type));
  530. }
  531. break;
  532. case C_POS:// label
  533. ShowDebug("Data: label pos=%"PRId64"\n", data->u.num);
  534. break;
  535. default:
  536. ShowDebug("Data: %s\n", script_op2name(data->type));
  537. break;
  538. }
  539. }
  540. /// Reports on the console information about the current built-in function.
  541. static void script_reportfunc(struct script_state* st)
  542. {
  543. int params, id;
  544. struct script_data* data;
  545. if( !script_hasdata(st,0) )
  546. {// no stack
  547. return;
  548. }
  549. data = script_getdata(st,0);
  550. if( !data_isreference(data) || str_data[reference_getid(data)].type != C_FUNC )
  551. {// script currently not executing a built-in function or corrupt stack
  552. return;
  553. }
  554. id = reference_getid(data);
  555. params = script_lastdata(st)-1;
  556. if( params > 0 )
  557. {
  558. int i;
  559. ShowDebug("Function: %s (%d parameter%s):\n", get_str(id), params, ( params == 1 ) ? "" : "s");
  560. for( i = 2; i <= script_lastdata(st); i++ )
  561. {
  562. script_reportdata(script_getdata(st,i));
  563. }
  564. }
  565. else
  566. {
  567. ShowDebug("Function: %s (no parameters)\n", get_str(id));
  568. }
  569. }
  570. /*==========================================
  571. * Output error message
  572. *------------------------------------------*/
  573. static void disp_error_message2(const char *mes,const char *pos,int report)
  574. {
  575. error_msg = aStrdup(mes);
  576. error_pos = pos;
  577. error_report = report;
  578. longjmp( error_jump, 1 );
  579. }
  580. #define disp_error_message(mes,pos) disp_error_message2(mes,pos,1)
  581. static void disp_warning_message(const char *mes, const char *pos) {
  582. script_warning(parser_current_src,parser_current_file,parser_current_line,mes,pos);
  583. }
  584. /// Checks event parameter validity
  585. static void check_event(struct script_state *st, const char *evt)
  586. {
  587. if( evt && evt[0] && !stristr(evt, "::On") )
  588. {
  589. ShowWarning("NPC event parameter deprecated! Please use 'NPCNAME::OnEVENT' instead of '%s'.\n", evt);
  590. script_reportsrc(st);
  591. }
  592. }
  593. /*==========================================
  594. * Hashes the input string
  595. *------------------------------------------*/
  596. static unsigned int calc_hash(const char* p)
  597. {
  598. unsigned int h;
  599. #if defined(SCRIPT_HASH_DJB2)
  600. h = 5381;
  601. while( *p ) // hash*33 + c
  602. h = ( h << 5 ) + h + ((unsigned char)TOLOWER(*p++));
  603. #elif defined(SCRIPT_HASH_SDBM)
  604. h = 0;
  605. while( *p ) // hash*65599 + c
  606. h = ( h << 6 ) + ( h << 16 ) - h + ((unsigned char)TOLOWER(*p++));
  607. #elif defined(SCRIPT_HASH_ELF) // UNIX ELF hash
  608. h = 0;
  609. while( *p ){
  610. unsigned int g;
  611. h = ( h << 4 ) + ((unsigned char)TOLOWER(*p++));
  612. g = h & 0xF0000000;
  613. if( g )
  614. {
  615. h ^= g >> 24;
  616. h &= ~g;
  617. }
  618. }
  619. #else // athena hash
  620. h = 0;
  621. while( *p )
  622. h = ( h << 1 ) + ( h >> 3 ) + ( h >> 5 ) + ( h >> 8 ) + (unsigned char)TOLOWER(*p++);
  623. #endif
  624. return h % SCRIPT_HASH_SIZE;
  625. }
  626. /*==========================================
  627. * str_data manipulation functions
  628. *------------------------------------------*/
  629. /// Looks up string using the provided id.
  630. const char* get_str(int id)
  631. {
  632. Assert( id >= LABEL_START && id < str_size );
  633. return str_buf+str_data[id].str;
  634. }
  635. /// Returns the uid of the string, or -1.
  636. static int search_str(const char* p)
  637. {
  638. int i;
  639. for( i = str_hash[calc_hash(p)]; i != 0; i = str_data[i].next )
  640. if( strcasecmp(get_str(i),p) == 0 )
  641. return i;
  642. return -1;
  643. }
  644. /// Stores a copy of the string and returns its id.
  645. /// If an identical string is already present, returns its id instead.
  646. int add_str(const char* p)
  647. {
  648. int h;
  649. int len;
  650. h = calc_hash(p);
  651. if( str_hash[h] == 0 )
  652. {// empty bucket, add new node here
  653. str_hash[h] = str_num;
  654. }
  655. else
  656. {// scan for end of list, or occurence of identical string
  657. int i;
  658. for( i = str_hash[h]; ; i = str_data[i].next )
  659. {
  660. if( strcasecmp(get_str(i),p) == 0 )
  661. return i; // string already in list
  662. if( str_data[i].next == 0 )
  663. break; // reached the end
  664. }
  665. // append node to end of list
  666. str_data[i].next = str_num;
  667. }
  668. // grow list if neccessary
  669. if( str_num >= str_data_size )
  670. {
  671. str_data_size += 128;
  672. RECREATE(str_data,struct str_data_struct,str_data_size);
  673. memset(str_data + (str_data_size - 128), '\0', 128);
  674. }
  675. len=(int)strlen(p);
  676. // grow string buffer if neccessary
  677. while( str_pos+len+1 >= str_size )
  678. {
  679. str_size += 256;
  680. RECREATE(str_buf,char,str_size);
  681. memset(str_buf + (str_size - 256), '\0', 256);
  682. }
  683. safestrncpy(str_buf+str_pos, p, len+1);
  684. str_data[str_num].type = C_NOP;
  685. str_data[str_num].str = str_pos;
  686. str_data[str_num].next = 0;
  687. str_data[str_num].func = NULL;
  688. str_data[str_num].backpatch = -1;
  689. str_data[str_num].label = -1;
  690. str_pos += len+1;
  691. return str_num++;
  692. }
  693. /// Appends 1 byte to the script buffer.
  694. static void add_scriptb(int a)
  695. {
  696. if( script_pos+1 >= script_size )
  697. {
  698. script_size += SCRIPT_BLOCK_SIZE;
  699. RECREATE(script_buf,unsigned char,script_size);
  700. }
  701. script_buf[script_pos++] = (uint8)(a);
  702. }
  703. /// Appends a c_op value to the script buffer.
  704. /// The value is variable-length encoded into 8-bit blocks.
  705. /// The encoding scheme is ( 01?????? )* 00??????, LSB first.
  706. /// All blocks but the last hold 7 bits of data, topmost bit is always 1 (carries).
  707. static void add_scriptc(int a)
  708. {
  709. while( a >= 0x40 )
  710. {
  711. add_scriptb((a&0x3f)|0x40);
  712. a = (a - 0x40) >> 6;
  713. }
  714. add_scriptb(a);
  715. }
  716. /// Appends an integer value to the script buffer.
  717. /// The value is variable-length encoded into 8-bit blocks.
  718. /// The encoding scheme is ( 11?????? )* 10??????, LSB first.
  719. /// All blocks but the last hold 7 bits of data, topmost bit is always 1 (carries).
  720. static void add_scripti(int a)
  721. {
  722. while( a >= 0x40 )
  723. {
  724. add_scriptb((a&0x3f)|0xc0);
  725. a = (a - 0x40) >> 6;
  726. }
  727. add_scriptb(a|0x80);
  728. }
  729. /// Appends a str_data object (label/function/variable/integer) to the script buffer.
  730. ///
  731. /// @param l The id of the str_data entry
  732. // Maximum up to 16M
  733. static void add_scriptl(int l)
  734. {
  735. int backpatch = str_data[l].backpatch;
  736. switch(str_data[l].type){
  737. case C_POS:
  738. case C_USERFUNC_POS:
  739. add_scriptc(C_POS);
  740. add_scriptb(str_data[l].label);
  741. add_scriptb(str_data[l].label>>8);
  742. add_scriptb(str_data[l].label>>16);
  743. break;
  744. case C_NOP:
  745. case C_USERFUNC:
  746. // Embedded data backpatch there is a possibility of label
  747. add_scriptc(C_NAME);
  748. str_data[l].backpatch = script_pos;
  749. add_scriptb(backpatch);
  750. add_scriptb(backpatch>>8);
  751. add_scriptb(backpatch>>16);
  752. break;
  753. case C_INT:
  754. add_scripti(abs(str_data[l].val));
  755. if( str_data[l].val < 0 ) //Notice that this is negative, from jA (Rayce)
  756. add_scriptc(C_NEG);
  757. break;
  758. default: // assume C_NAME
  759. add_scriptc(C_NAME);
  760. add_scriptb(l);
  761. add_scriptb(l>>8);
  762. add_scriptb(l>>16);
  763. break;
  764. }
  765. }
  766. /*==========================================
  767. * Resolve the label
  768. *------------------------------------------*/
  769. void set_label(int l,int pos, const char* script_pos_cur)
  770. {
  771. int i;
  772. if(str_data[l].type==C_INT || str_data[l].type==C_PARAM || str_data[l].type==C_FUNC)
  773. { //Prevent overwriting constants values, parameters and built-in functions [Skotlex]
  774. disp_error_message("set_label: invalid label name",script_pos_cur);
  775. return;
  776. }
  777. if(str_data[l].label!=-1){
  778. disp_error_message("set_label: dup label ",script_pos_cur);
  779. return;
  780. }
  781. str_data[l].type=(str_data[l].type == C_USERFUNC ? C_USERFUNC_POS : C_POS);
  782. str_data[l].label=pos;
  783. for(i=str_data[l].backpatch;i>=0 && i!=0x00ffffff;){
  784. int next=GETVALUE(script_buf,i);
  785. script_buf[i-1]=(str_data[l].type == C_USERFUNC ? C_USERFUNC_POS : C_POS);
  786. SETVALUE(script_buf,i,pos);
  787. i=next;
  788. }
  789. }
  790. /// Skips spaces and/or comments.
  791. const char* skip_space(const char* p)
  792. {
  793. if( p == NULL )
  794. return NULL;
  795. for(;;)
  796. {
  797. while( ISSPACE(*p) )
  798. ++p;
  799. if( *p == '/' && p[1] == '/' )
  800. {// line comment
  801. while(*p && *p!='\n')
  802. ++p;
  803. }
  804. else if( *p == '/' && p[1] == '*' )
  805. {// block comment
  806. p += 2;
  807. for(;;)
  808. {
  809. if( *p == '\0' ) {
  810. disp_warning_message("script:script->skip_space: end of file while parsing block comment. expected "CL_BOLD"*/"CL_NORM, p);
  811. return p;
  812. }
  813. if( *p == '*' && p[1] == '/' )
  814. {// end of block comment
  815. p += 2;
  816. break;
  817. }
  818. ++p;
  819. }
  820. }
  821. else
  822. break;
  823. }
  824. return p;
  825. }
  826. /// Skips a word.
  827. /// A word consists of undercores and/or alphanumeric characters,
  828. /// and valid variable prefixes/postfixes.
  829. static const char* skip_word(const char* p)
  830. {
  831. // prefix
  832. switch( *p ) {
  833. case '@':// temporary char variable
  834. ++p; break;
  835. case '#':// account variable
  836. p += ( p[1] == '#' ? 2 : 1 ); break;
  837. case '\'':// instance variable
  838. ++p; break;
  839. case '.':// npc variable
  840. p += ( p[1] == '@' ? 2 : 1 ); break;
  841. case '$':// global variable
  842. p += ( p[1] == '@' ? 2 : 1 ); break;
  843. }
  844. while( ISALNUM(*p) || *p == '_' )
  845. ++p;
  846. // postfix
  847. if( *p == '$' )// string
  848. p++;
  849. return p;
  850. }
  851. /// Adds a word to str_data.
  852. /// @see skip_word
  853. /// @see add_str
  854. static int add_word(const char* p)
  855. {
  856. char* word;
  857. int len;
  858. int i;
  859. // Check for a word
  860. len = skip_word(p) - p;
  861. if( len == 0 )
  862. disp_error_message("script:add_word: invalid word. A word consists of undercores and/or alphanumeric characters, and valid variable prefixes/postfixes.", p);
  863. // Duplicate the word
  864. word = (char*)aMalloc(len+1);
  865. memcpy(word, p, len);
  866. word[len] = 0;
  867. // add the word
  868. i = add_str(word);
  869. aFree(word);
  870. return i;
  871. }
  872. /// Parses a function call.
  873. /// The argument list can have parenthesis or not.
  874. /// The number of arguments is checked.
  875. static
  876. const char* parse_callfunc(const char* p, int require_paren, int is_custom)
  877. {
  878. const char* p2;
  879. const char* arg=NULL;
  880. int func;
  881. func = add_word(p);
  882. if( str_data[func].type == C_FUNC ){
  883. // buildin function
  884. add_scriptl(func);
  885. add_scriptc(C_ARG);
  886. arg = buildin_func[str_data[func].val].arg;
  887. } else if( str_data[func].type == C_USERFUNC || str_data[func].type == C_USERFUNC_POS ){
  888. // script defined function
  889. add_scriptl(buildin_callsub_ref);
  890. add_scriptc(C_ARG);
  891. add_scriptl(func);
  892. arg = buildin_func[str_data[buildin_callsub_ref].val].arg;
  893. if( *arg == 0 )
  894. disp_error_message("parse_callfunc: callsub has no arguments, please review its definition",p);
  895. if( *arg != '*' )
  896. ++arg; // count func as argument
  897. } else {
  898. #ifdef SCRIPT_CALLFUNC_CHECK
  899. const char* name = get_str(func);
  900. if( !is_custom && strdb_get(userfunc_db, name) == NULL ) {
  901. #endif
  902. disp_error_message("parse_line: expect command, missing function name or calling undeclared function",p);
  903. #ifdef SCRIPT_CALLFUNC_CHECK
  904. } else {;
  905. add_scriptl(buildin_callfunc_ref);
  906. add_scriptc(C_ARG);
  907. add_scriptc(C_STR);
  908. while( *name ) add_scriptb(*name ++);
  909. add_scriptb(0);
  910. arg = buildin_func[str_data[buildin_callfunc_ref].val].arg;
  911. if( *arg != '*' ) ++ arg;
  912. }
  913. #endif
  914. }
  915. p = skip_word(p);
  916. p = skip_space(p);
  917. syntax.curly[syntax.curly_count].type = TYPE_ARGLIST;
  918. syntax.curly[syntax.curly_count].count = 0;
  919. if( *p == ';' )
  920. {// <func name> ';'
  921. syntax.curly[syntax.curly_count].flag = ARGLIST_NO_PAREN;
  922. } else if( *p == '(' && *(p2=skip_space(p+1)) == ')' )
  923. {// <func name> '(' ')'
  924. syntax.curly[syntax.curly_count].flag = ARGLIST_PAREN;
  925. p = p2;
  926. /*
  927. } else if( 0 && require_paren && *p != '(' )
  928. {// <func name>
  929. syntax.curly[syntax.curly_count].flag = ARGLIST_NO_PAREN;
  930. */
  931. } else
  932. {// <func name> <arg list>
  933. if( require_paren ){
  934. if( *p != '(' )
  935. disp_error_message("need '('",p);
  936. ++p; // skip '('
  937. syntax.curly[syntax.curly_count].flag = ARGLIST_PAREN;
  938. } else if( *p == '(' ){
  939. syntax.curly[syntax.curly_count].flag = ARGLIST_UNDEFINED;
  940. } else {
  941. syntax.curly[syntax.curly_count].flag = ARGLIST_NO_PAREN;
  942. }
  943. ++syntax.curly_count;
  944. while( *arg ) {
  945. p2=parse_subexpr(p,-1);
  946. if( p == p2 )
  947. break; // not an argument
  948. if( *arg != '*' )
  949. ++arg; // next argument
  950. p=skip_space(p2);
  951. if( *arg == 0 || *p != ',' )
  952. break; // no more arguments
  953. ++p; // skip comma
  954. }
  955. --syntax.curly_count;
  956. }
  957. if( *arg && *arg != '?' && *arg != '*' )
  958. disp_error_message2("parse_callfunc: not enough arguments, expected ','", p, script_config.warn_func_mismatch_paramnum);
  959. if( syntax.curly[syntax.curly_count].type != TYPE_ARGLIST )
  960. disp_error_message("parse_callfunc: DEBUG last curly is not an argument list",p);
  961. if( syntax.curly[syntax.curly_count].flag == ARGLIST_PAREN ){
  962. if( *p != ')' )
  963. disp_error_message("parse_callfunc: expected ')' to close argument list",p);
  964. ++p;
  965. }
  966. add_scriptc(C_FUNC);
  967. return p;
  968. }
  969. /// Processes end of logical script line.
  970. /// @param first When true, only fix up scheduling data is initialized
  971. /// @param p Script position for error reporting in set_label
  972. static void parse_nextline(bool first, const char* p)
  973. {
  974. if( !first )
  975. {
  976. add_scriptc(C_EOL); // mark end of line for stack cleanup
  977. set_label(LABEL_NEXTLINE, script_pos, p); // fix up '-' labels
  978. }
  979. // initialize data for new '-' label fix up scheduling
  980. str_data[LABEL_NEXTLINE].type = C_NOP;
  981. str_data[LABEL_NEXTLINE].backpatch = -1;
  982. str_data[LABEL_NEXTLINE].label = -1;
  983. }
  984. /**
  985. * Pushes a variable into stack, processing its array index if needed.
  986. * @see parse_variable
  987. */
  988. void parse_variable_sub_push(int word, const char *p2)
  989. {
  990. if( p2 ) { // Process the variable index
  991. const char *p3 = NULL;
  992. // Push the getelementofarray method into the stack
  993. add_scriptl(buildin_getelementofarray_ref);
  994. add_scriptc(C_ARG);
  995. add_scriptl(word);
  996. // Process the sub-expression for this assignment
  997. p3 = parse_subexpr(p2 + 1, 1);
  998. p3 = skip_space(p3);
  999. if( *p3 != ']' ) // Closing parenthesis is required for this script
  1000. disp_error_message("Missing closing ']' parenthesis for the variable assignment.", p3);
  1001. // Push the closing function stack operator onto the stack
  1002. add_scriptc(C_FUNC);
  1003. p3++;
  1004. } else // No array index, simply push the variable or value onto the stack
  1005. add_scriptl(word);
  1006. }
  1007. /// Parse a variable assignment using the direct equals operator
  1008. /// @param p script position where the function should run from
  1009. /// @return NULL if not a variable assignment, the new position otherwise
  1010. const char* parse_variable(const char* p) {
  1011. int word;
  1012. c_op type = C_NOP;
  1013. const char *p2 = NULL;
  1014. const char *var = p;
  1015. if ((p[0] == '+' && p[1] == '+' && (type = C_ADD_PRE)) // pre ++
  1016. || (p[0] == '-' && p[1] == '-' && (type = C_SUB_PRE))) // pre --
  1017. var = p = skip_space(&p[2]);
  1018. // skip the variable where applicable
  1019. p = skip_word(p);
  1020. p = skip_space(p);
  1021. if( p == NULL ) {// end of the line or invalid buffer
  1022. return NULL;
  1023. }
  1024. if( *p == '[' ) {// array variable so process the array as appropriate
  1025. int i,j;
  1026. for( p2 = p, i = 0, j = 1; p; ++ i ) {
  1027. if( *p ++ == ']' && --(j) == 0 ) break;
  1028. if( *p == '[' ) ++ j;
  1029. }
  1030. if( !(p = skip_space(p)) ) {// end of line or invalid characters remaining
  1031. disp_error_message("Missing right expression or closing bracket for variable.", p);
  1032. }
  1033. }
  1034. if( type == C_NOP &&
  1035. !( ( p[0] == '=' && p[1] != '=' && (type = C_EQ) ) // =
  1036. || ( p[0] == '+' && p[1] == '=' && (type = C_ADD) ) // +=
  1037. || ( p[0] == '-' && p[1] == '=' && (type = C_SUB) ) // -=
  1038. || ( p[0] == '^' && p[1] == '=' && (type = C_XOR) ) // ^=
  1039. || ( p[0] == '|' && p[1] == '=' && (type = C_OR ) ) // |=
  1040. || ( p[0] == '&' && p[1] == '=' && (type = C_AND) ) // &=
  1041. || ( p[0] == '*' && p[1] == '=' && (type = C_MUL) ) // *=
  1042. || ( p[0] == '/' && p[1] == '=' && (type = C_DIV) ) // /=
  1043. || ( p[0] == '%' && p[1] == '=' && (type = C_MOD) ) // %=
  1044. || ( p[0] == '~' && p[1] == '=' && (type = C_NOT) ) // ~=
  1045. || ( p[0] == '+' && p[1] == '+' && (type = C_ADD_POST) ) // post ++
  1046. || ( p[0] == '-' && p[1] == '-' && (type = C_SUB_POST) ) // post --
  1047. || ( p[0] == '<' && p[1] == '<' && p[2] == '=' && (type = C_L_SHIFT) ) // <<=
  1048. || ( p[0] == '>' && p[1] == '>' && p[2] == '=' && (type = C_R_SHIFT) ) // >>=
  1049. ) )
  1050. {// failed to find a matching operator combination so invalid
  1051. return NULL;
  1052. }
  1053. switch( type ) {
  1054. case C_ADD_PRE: // pre ++
  1055. case C_SUB_PRE: // pre --
  1056. // Nothing more to skip
  1057. break;
  1058. case C_EQ: {// incremental modifier
  1059. p = skip_space( &p[1] );
  1060. }
  1061. break;
  1062. case C_L_SHIFT:
  1063. case C_R_SHIFT: {// left or right shift modifier
  1064. p = skip_space( &p[3] );
  1065. }
  1066. break;
  1067. default: {// normal incremental command
  1068. p = skip_space( &p[2] );
  1069. }
  1070. }
  1071. if( p == NULL ) {// end of line or invalid buffer
  1072. return NULL;
  1073. }
  1074. // push the set function onto the stack
  1075. add_scriptl(buildin_set_ref);
  1076. add_scriptc(C_ARG);
  1077. // always append parenthesis to avoid errors
  1078. syntax.curly[syntax.curly_count].type = TYPE_ARGLIST;
  1079. syntax.curly[syntax.curly_count].count = 0;
  1080. syntax.curly[syntax.curly_count].flag = ARGLIST_PAREN;
  1081. // increment the total curly count for the position in the script
  1082. ++ syntax.curly_count;
  1083. // parse the variable currently being modified
  1084. word = add_word(var);
  1085. 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
  1086. disp_error_message("Cannot modify a variable which has the same name as a function or label.", p);
  1087. parse_variable_sub_push(word, p2); // Push variable onto the stack
  1088. if( type != C_EQ )
  1089. parse_variable_sub_push(word, p2); // Push variable onto the stack once again (first argument of setr)
  1090. if( type == C_ADD_POST || type == C_SUB_POST ) { // post ++ / --
  1091. add_scripti(1);
  1092. add_scriptc(type == C_ADD_POST ? C_ADD : C_SUB);
  1093. parse_variable_sub_push(word, p2); // Push variable onto the stack (third argument of setr)
  1094. } else if( type == C_ADD_PRE || type == C_SUB_PRE ) { // pre ++ / --
  1095. add_scripti(1);
  1096. add_scriptc(type == C_ADD_PRE ? C_ADD : C_SUB);
  1097. } else { // Process the value as an expression
  1098. p = parse_subexpr(p, -1);
  1099. if( type != C_EQ )
  1100. {// push the type of modifier onto the stack
  1101. add_scriptc(type);
  1102. }
  1103. }
  1104. // decrement the curly count for the position within the script
  1105. -- syntax.curly_count;
  1106. // close the script by appending the function operator
  1107. add_scriptc(C_FUNC);
  1108. // push the buffer from the method
  1109. return p;
  1110. }
  1111. /*
  1112. * Checks whether the gives string is a number literal
  1113. *
  1114. * Mainly necessary to differentiate between number literals and NPC name
  1115. * constants, since several of those start with a digit.
  1116. *
  1117. * All this does is to check if the string begins with an optional + or - sign,
  1118. * followed by a hexadecimal or decimal number literal literal and is NOT
  1119. * followed by a underscore or letter.
  1120. *
  1121. * @author : Hercules.ws
  1122. * @param p Pointer to the string to check
  1123. * @return Whether the string is a number literal
  1124. */
  1125. bool is_number(const char *p) {
  1126. const char *np;
  1127. if (!p)
  1128. return false;
  1129. if (*p == '-' || *p == '+')
  1130. p++;
  1131. np = p;
  1132. if (*p == '0' && p[1] == 'x') {
  1133. p+=2;
  1134. np = p;
  1135. // Hexadecimal
  1136. while (ISXDIGIT(*np))
  1137. np++;
  1138. } else {
  1139. // Decimal
  1140. while (ISDIGIT(*np))
  1141. np++;
  1142. }
  1143. if (p != np && *np != '_' && !ISALPHA(*np)) // At least one digit, and next isn't a letter or _
  1144. return true;
  1145. return false;
  1146. }
  1147. /*==========================================
  1148. * Analysis section
  1149. *------------------------------------------*/
  1150. const char* parse_simpleexpr(const char *p)
  1151. {
  1152. long long i;
  1153. p=skip_space(p);
  1154. if(*p==';' || *p==',')
  1155. disp_error_message("parse_simpleexpr: unexpected end of expression",p);
  1156. if(*p=='('){
  1157. if( (i=syntax.curly_count-1) >= 0 && syntax.curly[i].type == TYPE_ARGLIST )
  1158. ++syntax.curly[i].count;
  1159. p=parse_subexpr(p+1,-1);
  1160. p=skip_space(p);
  1161. if( (i=syntax.curly_count-1) >= 0 && syntax.curly[i].type == TYPE_ARGLIST &&
  1162. syntax.curly[i].flag == ARGLIST_UNDEFINED && --syntax.curly[i].count == 0
  1163. ){
  1164. if( *p == ',' ){
  1165. syntax.curly[i].flag = ARGLIST_PAREN;
  1166. return p;
  1167. } else
  1168. syntax.curly[i].flag = ARGLIST_NO_PAREN;
  1169. }
  1170. if( *p != ')' )
  1171. disp_error_message("parse_simpleexpr: unmatched ')'",p);
  1172. ++p;
  1173. } else if(is_number(p)) {
  1174. char *np;
  1175. while(*p == '0' && ISDIGIT(p[1])) p++;
  1176. i=strtoll(p,&np,0);
  1177. if( i < INT_MIN ) {
  1178. i = INT_MIN;
  1179. disp_warning_message("parse_simpleexpr: underflow detected, capping value to INT_MIN",p);
  1180. } else if( i > INT_MAX ) {
  1181. i = INT_MAX;
  1182. disp_warning_message("parse_simpleexpr: overflow detected, capping value to INT_MAX",p);
  1183. }
  1184. add_scripti((int)i);
  1185. p=np;
  1186. } else if(*p=='"'){
  1187. add_scriptc(C_STR);
  1188. p++;
  1189. while( *p && *p != '"' ){
  1190. if( (unsigned char)p[-1] <= 0x7e && *p == '\\' )
  1191. {
  1192. char buf[8];
  1193. size_t len = skip_escaped_c(p) - p;
  1194. size_t n = sv_unescape_c(buf, p, len);
  1195. if( n != 1 )
  1196. ShowDebug("parse_simpleexpr: unexpected length %d after unescape (\"%.*s\" -> %.*s)\n", (int)n, (int)len, p, (int)n, buf);
  1197. p += len;
  1198. add_scriptb(*buf);
  1199. continue;
  1200. }
  1201. else if( *p == '\n' )
  1202. disp_error_message("parse_simpleexpr: unexpected newline @ string",p);
  1203. add_scriptb(*p++);
  1204. }
  1205. if(!*p)
  1206. disp_error_message("parse_simpleexpr: unexpected eof @ string",p);
  1207. add_scriptb(0);
  1208. p++; //'"'
  1209. } else {
  1210. int l;
  1211. const char* pv;
  1212. // label , register , function etc
  1213. if(skip_word(p)==p)
  1214. disp_error_message("parse_simpleexpr: unexpected character",p);
  1215. l=add_word(p);
  1216. if( str_data[l].type == C_FUNC || str_data[l].type == C_USERFUNC || str_data[l].type == C_USERFUNC_POS)
  1217. return parse_callfunc(p,1,0);
  1218. #ifdef SCRIPT_CALLFUNC_CHECK
  1219. else {
  1220. const char* name = get_str(l);
  1221. if( strdb_get(userfunc_db,name) != NULL ) {
  1222. return parse_callfunc(p,1,1);
  1223. }
  1224. }
  1225. #endif
  1226. if( (pv = parse_variable(p)) )
  1227. {// successfully processed a variable assignment
  1228. return pv;
  1229. }
  1230. p=skip_word(p);
  1231. if( *p == '[' ){
  1232. // array(name[i] => getelementofarray(name,i) )
  1233. add_scriptl(buildin_getelementofarray_ref);
  1234. add_scriptc(C_ARG);
  1235. add_scriptl(l);
  1236. p=parse_subexpr(p+1,-1);
  1237. p=skip_space(p);
  1238. if( *p != ']' )
  1239. disp_error_message("parse_simpleexpr: unmatched ']'",p);
  1240. ++p;
  1241. add_scriptc(C_FUNC);
  1242. }else
  1243. add_scriptl(l);
  1244. }
  1245. return p;
  1246. }
  1247. /*==========================================
  1248. * Analysis of the expression
  1249. *------------------------------------------*/
  1250. const char* parse_subexpr(const char* p,int limit)
  1251. {
  1252. int op,opl,len;
  1253. p=skip_space(p);
  1254. if( *p == '-' ){
  1255. const char* tmpp = skip_space(p+1);
  1256. if( *tmpp == ';' || *tmpp == ',' ){
  1257. add_scriptl(LABEL_NEXTLINE);
  1258. p++;
  1259. return p;
  1260. }
  1261. }
  1262. if( (op = C_ADD_PRE, p[0] == '+' && p[1] == '+') || (op = C_SUB_PRE, p[0] == '-' && p[1] == '-') ) // Pre ++ -- operators
  1263. p = parse_variable(p);
  1264. else if( (op = C_NEG, *p == '-') || (op = C_LNOT, *p == '!') || (op = C_NOT, *p == '~') ) { // Unary - ! ~ operators
  1265. p = parse_subexpr(p + 1, 11);
  1266. add_scriptc(op);
  1267. } else
  1268. p = parse_simpleexpr(p);
  1269. p = skip_space(p);
  1270. while((
  1271. (op=C_OP3,opl=0,len=1,*p=='?') ||
  1272. (op=C_ADD,opl=9,len=1,*p=='+') ||
  1273. (op=C_SUB,opl=9,len=1,*p=='-') ||
  1274. (op=C_MUL,opl=10,len=1,*p=='*') ||
  1275. (op=C_DIV,opl=10,len=1,*p=='/') ||
  1276. (op=C_MOD,opl=10,len=1,*p=='%') ||
  1277. (op=C_LAND,opl=2,len=2,*p=='&' && p[1]=='&') ||
  1278. (op=C_AND,opl=5,len=1,*p=='&') ||
  1279. (op=C_LOR,opl=1,len=2,*p=='|' && p[1]=='|') ||
  1280. (op=C_OR,opl=3,len=1,*p=='|') ||
  1281. (op=C_XOR,opl=4,len=1,*p=='^') ||
  1282. (op=C_EQ,opl=6,len=2,*p=='=' && p[1]=='=') ||
  1283. (op=C_NE,opl=6,len=2,*p=='!' && p[1]=='=') ||
  1284. (op=C_R_SHIFT,opl=8,len=2,*p=='>' && p[1]=='>') ||
  1285. (op=C_GE,opl=7,len=2,*p=='>' && p[1]=='=') ||
  1286. (op=C_GT,opl=7,len=1,*p=='>') ||
  1287. (op=C_L_SHIFT,opl=8,len=2,*p=='<' && p[1]=='<') ||
  1288. (op=C_LE,opl=7,len=2,*p=='<' && p[1]=='=') ||
  1289. (op=C_LT,opl=7,len=1,*p=='<')) && opl>limit){
  1290. p+=len;
  1291. if(op == C_OP3) {
  1292. p=parse_subexpr(p,-1);
  1293. p=skip_space(p);
  1294. if( *(p++) != ':')
  1295. disp_error_message("parse_subexpr: expected ':'", p-1);
  1296. p=parse_subexpr(p,-1);
  1297. } else {
  1298. p=parse_subexpr(p,opl);
  1299. }
  1300. add_scriptc(op);
  1301. p=skip_space(p);
  1302. }
  1303. return p; /* return first untreated operator */
  1304. }
  1305. /*==========================================
  1306. * Evaluation of the expression
  1307. *------------------------------------------*/
  1308. const char* parse_expr(const char *p)
  1309. {
  1310. switch(*p){
  1311. case ')': case ';': case ':': case '[': case ']':
  1312. case '}':
  1313. disp_error_message("parse_expr: unexpected character",p);
  1314. }
  1315. p=parse_subexpr(p,-1);
  1316. return p;
  1317. }
  1318. /*==========================================
  1319. * Analysis of the line
  1320. *------------------------------------------*/
  1321. const char* parse_line(const char* p)
  1322. {
  1323. const char* p2;
  1324. p=skip_space(p);
  1325. if(*p==';') {
  1326. //Close decision for if(); for(); while();
  1327. p = parse_syntax_close(p + 1);
  1328. return p;
  1329. }
  1330. if(*p==')' && parse_syntax_for_flag)
  1331. return p+1;
  1332. p = skip_space(p);
  1333. if(p[0] == '{') {
  1334. syntax.curly[syntax.curly_count].type = TYPE_NULL;
  1335. syntax.curly[syntax.curly_count].count = -1;
  1336. syntax.curly[syntax.curly_count].index = -1;
  1337. syntax.curly_count++;
  1338. return p + 1;
  1339. } else if(p[0] == '}') {
  1340. return parse_curly_close(p);
  1341. }
  1342. // Syntax-related processing
  1343. p2 = parse_syntax(p);
  1344. if(p2 != NULL)
  1345. return p2;
  1346. // attempt to process a variable assignment
  1347. p2 = parse_variable(p);
  1348. if( p2 != NULL )
  1349. {// variable assignment processed so leave the method
  1350. return parse_syntax_close(p2 + 1);
  1351. }
  1352. p = parse_callfunc(p,0,0);
  1353. p = skip_space(p);
  1354. if(parse_syntax_for_flag) {
  1355. if( *p != ')' )
  1356. disp_error_message("parse_line: expected ')'",p);
  1357. } else {
  1358. if( *p != ';' )
  1359. disp_error_message("parse_line: expected ';'",p);
  1360. }
  1361. //Binding decision for if(), for(), while()
  1362. p = parse_syntax_close(p+1);
  1363. return p;
  1364. }
  1365. // { ... } Closing process
  1366. const char* parse_curly_close(const char* p)
  1367. {
  1368. if(syntax.curly_count <= 0) {
  1369. disp_error_message("parse_curly_close: unexpected string",p);
  1370. return p + 1;
  1371. } else if(syntax.curly[syntax.curly_count-1].type == TYPE_NULL) {
  1372. syntax.curly_count--;
  1373. //Close decision if, for , while
  1374. p = parse_syntax_close(p + 1);
  1375. return p;
  1376. } else if(syntax.curly[syntax.curly_count-1].type == TYPE_SWITCH) {
  1377. //Closing switch()
  1378. int pos = syntax.curly_count-1;
  1379. char label[256];
  1380. int l;
  1381. // Remove temporary variables
  1382. sprintf(label,"set $@__SW%x_VAL,0;",syntax.curly[pos].index);
  1383. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1384. parse_line(label);
  1385. syntax.curly_count--;
  1386. // Go to the end pointer unconditionally
  1387. sprintf(label,"goto __SW%x_FIN;",syntax.curly[pos].index);
  1388. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1389. parse_line(label);
  1390. syntax.curly_count--;
  1391. // You are here labeled
  1392. sprintf(label,"__SW%x_%x",syntax.curly[pos].index,syntax.curly[pos].count);
  1393. l=add_str(label);
  1394. set_label(l,script_pos, p);
  1395. if(syntax.curly[pos].flag) {
  1396. //Exists default
  1397. sprintf(label,"goto __SW%x_DEF;",syntax.curly[pos].index);
  1398. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1399. parse_line(label);
  1400. syntax.curly_count--;
  1401. }
  1402. // Label end
  1403. sprintf(label,"__SW%x_FIN",syntax.curly[pos].index);
  1404. l=add_str(label);
  1405. set_label(l,script_pos, p);
  1406. linkdb_final(&syntax.curly[pos].case_label); // free the list of case label
  1407. syntax.curly_count--;
  1408. //Closing decision if, for , while
  1409. p = parse_syntax_close(p + 1);
  1410. return p;
  1411. } else {
  1412. disp_error_message("parse_curly_close: unexpected string",p);
  1413. return p + 1;
  1414. }
  1415. }
  1416. // Syntax-related processing
  1417. // break, case, continue, default, do, for, function,
  1418. // if, switch, while ? will handle this internally.
  1419. const char* parse_syntax(const char* p)
  1420. {
  1421. const char *p2 = skip_word(p);
  1422. switch(*p) {
  1423. case 'B':
  1424. case 'b':
  1425. if(p2 - p == 5 && !strncasecmp(p,"break",5)) {
  1426. // break Processing
  1427. char label[256];
  1428. int pos = syntax.curly_count - 1;
  1429. while(pos >= 0) {
  1430. if(syntax.curly[pos].type == TYPE_DO) {
  1431. sprintf(label,"goto __DO%x_FIN;",syntax.curly[pos].index);
  1432. break;
  1433. } else if(syntax.curly[pos].type == TYPE_FOR) {
  1434. sprintf(label,"goto __FR%x_FIN;",syntax.curly[pos].index);
  1435. break;
  1436. } else if(syntax.curly[pos].type == TYPE_WHILE) {
  1437. sprintf(label,"goto __WL%x_FIN;",syntax.curly[pos].index);
  1438. break;
  1439. } else if(syntax.curly[pos].type == TYPE_SWITCH) {
  1440. sprintf(label,"goto __SW%x_FIN;",syntax.curly[pos].index);
  1441. break;
  1442. }
  1443. pos--;
  1444. }
  1445. if(pos < 0) {
  1446. disp_error_message("parse_syntax: unexpected 'break'",p);
  1447. } else {
  1448. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1449. parse_line(label);
  1450. syntax.curly_count--;
  1451. }
  1452. p = skip_space(p2);
  1453. if(*p != ';')
  1454. disp_error_message("parse_syntax: expected ';'",p);
  1455. // Closing decision if, for , while
  1456. p = parse_syntax_close(p + 1);
  1457. return p;
  1458. }
  1459. break;
  1460. case 'c':
  1461. case 'C':
  1462. if(p2 - p == 4 && !strncasecmp(p,"case",4)) {
  1463. //Processing case
  1464. int pos = syntax.curly_count-1;
  1465. if(pos < 0 || syntax.curly[pos].type != TYPE_SWITCH) {
  1466. disp_error_message("parse_syntax: unexpected 'case' ",p);
  1467. return p+1;
  1468. } else {
  1469. char label[256];
  1470. int l,v;
  1471. char *np;
  1472. if(syntax.curly[pos].count != 1) {
  1473. //Jump for FALLTHRU
  1474. sprintf(label,"goto __SW%x_%xJ;",syntax.curly[pos].index,syntax.curly[pos].count);
  1475. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1476. parse_line(label);
  1477. syntax.curly_count--;
  1478. // You are here labeled
  1479. sprintf(label,"__SW%x_%x",syntax.curly[pos].index,syntax.curly[pos].count);
  1480. l=add_str(label);
  1481. set_label(l,script_pos, p);
  1482. }
  1483. //Decision statement switch
  1484. p = skip_space(p2);
  1485. if(p == p2) {
  1486. disp_error_message("parse_syntax: expected a space ' '",p);
  1487. }
  1488. // check whether case label is integer or not
  1489. if(is_number(p)) {
  1490. //Numeric value
  1491. v = (int)strtol(p,&np,0);
  1492. if((*p == '-' || *p == '+') && ISDIGIT(p[1])) // pre-skip because '-' can not skip_word
  1493. p++;
  1494. p = skip_word(p);
  1495. if(np != p)
  1496. disp_error_message("parse_syntax: 'case' label is not an integer",np);
  1497. } else {
  1498. //Check for constants
  1499. p2 = skip_word(p);
  1500. v = (int)(size_t) (p2-p); // length of word at p2
  1501. memcpy(label,p,v);
  1502. label[v]='\0';
  1503. if( !script_get_constant(label, &v) )
  1504. disp_error_message("parse_syntax: 'case' label is not an integer",p);
  1505. p = skip_word(p);
  1506. }
  1507. p = skip_space(p);
  1508. if(*p != ':')
  1509. disp_error_message("parse_syntax: expect ':'",p);
  1510. sprintf(label,"if(%d != $@__SW%x_VAL) goto __SW%x_%x;",
  1511. v,syntax.curly[pos].index,syntax.curly[pos].index,syntax.curly[pos].count+1);
  1512. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1513. // Bad I do not parse twice
  1514. p2 = parse_line(label);
  1515. parse_line(p2);
  1516. syntax.curly_count--;
  1517. if(syntax.curly[pos].count != 1) {
  1518. // Label after the completion of FALLTHRU
  1519. sprintf(label,"__SW%x_%xJ",syntax.curly[pos].index,syntax.curly[pos].count);
  1520. l=add_str(label);
  1521. set_label(l,script_pos,p);
  1522. }
  1523. // check duplication of case label [Rayce]
  1524. if(linkdb_search(&syntax.curly[pos].case_label, (void*)__64BPRTSIZE(v)) != NULL)
  1525. disp_error_message("parse_syntax: dup 'case'",p);
  1526. linkdb_insert(&syntax.curly[pos].case_label, (void*)__64BPRTSIZE(v), (void*)1);
  1527. sprintf(label,"set $@__SW%x_VAL,0;",syntax.curly[pos].index);
  1528. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1529. parse_line(label);
  1530. syntax.curly_count--;
  1531. syntax.curly[pos].count++;
  1532. }
  1533. return p + 1;
  1534. } else if(p2 - p == 8 && !strncasecmp(p,"continue",8)) {
  1535. // Processing continue
  1536. char label[256];
  1537. int pos = syntax.curly_count - 1;
  1538. while(pos >= 0) {
  1539. if(syntax.curly[pos].type == TYPE_DO) {
  1540. sprintf(label,"goto __DO%x_NXT;",syntax.curly[pos].index);
  1541. syntax.curly[pos].flag = 1; //Flag put the link for continue
  1542. break;
  1543. } else if(syntax.curly[pos].type == TYPE_FOR) {
  1544. sprintf(label,"goto __FR%x_NXT;",syntax.curly[pos].index);
  1545. break;
  1546. } else if(syntax.curly[pos].type == TYPE_WHILE) {
  1547. sprintf(label,"goto __WL%x_NXT;",syntax.curly[pos].index);
  1548. break;
  1549. }
  1550. pos--;
  1551. }
  1552. if(pos < 0) {
  1553. disp_error_message("parse_syntax: unexpected 'continue'",p);
  1554. } else {
  1555. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1556. parse_line(label);
  1557. syntax.curly_count--;
  1558. }
  1559. p = skip_space(p2);
  1560. if(*p != ';')
  1561. disp_error_message("parse_syntax: expected ';'",p);
  1562. //Closing decision if, for , while
  1563. p = parse_syntax_close(p + 1);
  1564. return p;
  1565. }
  1566. break;
  1567. case 'd':
  1568. case 'D':
  1569. if(p2 - p == 7 && !strncasecmp(p,"default",7)) {
  1570. // Switch - default processing
  1571. int pos = syntax.curly_count-1;
  1572. if(pos < 0 || syntax.curly[pos].type != TYPE_SWITCH) {
  1573. disp_error_message("parse_syntax: unexpected 'default'",p);
  1574. } else if(syntax.curly[pos].flag) {
  1575. disp_error_message("parse_syntax: dup 'default'",p);
  1576. } else {
  1577. char label[256];
  1578. int l;
  1579. // Put the label location
  1580. p = skip_space(p2);
  1581. if(*p != ':') {
  1582. disp_error_message("parse_syntax: expected ':'",p);
  1583. }
  1584. sprintf(label,"__SW%x_%x",syntax.curly[pos].index,syntax.curly[pos].count);
  1585. l=add_str(label);
  1586. set_label(l,script_pos,p);
  1587. // Skip to the next link w/o condition
  1588. sprintf(label,"goto __SW%x_%x;",syntax.curly[pos].index,syntax.curly[pos].count+1);
  1589. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1590. parse_line(label);
  1591. syntax.curly_count--;
  1592. // The default label
  1593. sprintf(label,"__SW%x_DEF",syntax.curly[pos].index);
  1594. l=add_str(label);
  1595. set_label(l,script_pos,p);
  1596. syntax.curly[syntax.curly_count - 1].flag = 1;
  1597. syntax.curly[pos].count++;
  1598. }
  1599. return p + 1;
  1600. } else if(p2 - p == 2 && !strncasecmp(p,"do",2)) {
  1601. int l;
  1602. char label[256];
  1603. p=skip_space(p2);
  1604. syntax.curly[syntax.curly_count].type = TYPE_DO;
  1605. syntax.curly[syntax.curly_count].count = 1;
  1606. syntax.curly[syntax.curly_count].index = syntax.index++;
  1607. syntax.curly[syntax.curly_count].flag = 0;
  1608. // Label of the (do) form here
  1609. sprintf(label,"__DO%x_BGN",syntax.curly[syntax.curly_count].index);
  1610. l=add_str(label);
  1611. set_label(l,script_pos,p);
  1612. syntax.curly_count++;
  1613. return p;
  1614. }
  1615. break;
  1616. case 'f':
  1617. case 'F':
  1618. if(p2 - p == 3 && !strncasecmp(p,"for",3)) {
  1619. int l;
  1620. char label[256];
  1621. int pos = syntax.curly_count;
  1622. syntax.curly[syntax.curly_count].type = TYPE_FOR;
  1623. syntax.curly[syntax.curly_count].count = 1;
  1624. syntax.curly[syntax.curly_count].index = syntax.index++;
  1625. syntax.curly[syntax.curly_count].flag = 0;
  1626. syntax.curly_count++;
  1627. p=skip_space(p2);
  1628. if(*p != '(')
  1629. disp_error_message("parse_syntax: expected '('",p);
  1630. p++;
  1631. // Execute the initialization statement
  1632. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1633. p=parse_line(p);
  1634. syntax.curly_count--;
  1635. // Form the start of label decision
  1636. sprintf(label,"__FR%x_J",syntax.curly[pos].index);
  1637. l=add_str(label);
  1638. set_label(l,script_pos,p);
  1639. p=skip_space(p);
  1640. if(*p == ';') {
  1641. // For (; Because the pattern of always true ;)
  1642. ;
  1643. } else {
  1644. // Skip to the end point if the condition is false
  1645. sprintf(label,"__FR%x_FIN",syntax.curly[pos].index);
  1646. add_scriptl(add_str("jump_zero"));
  1647. add_scriptc(C_ARG);
  1648. p=parse_expr(p);
  1649. p=skip_space(p);
  1650. add_scriptl(add_str(label));
  1651. add_scriptc(C_FUNC);
  1652. }
  1653. if(*p != ';')
  1654. disp_error_message("parse_syntax: expected ';'",p);
  1655. p++;
  1656. // Skip to the beginning of the loop
  1657. sprintf(label,"goto __FR%x_BGN;",syntax.curly[pos].index);
  1658. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1659. parse_line(label);
  1660. syntax.curly_count--;
  1661. // Labels to form the next loop
  1662. sprintf(label,"__FR%x_NXT",syntax.curly[pos].index);
  1663. l=add_str(label);
  1664. set_label(l,script_pos,p);
  1665. // Process the next time you enter the loop
  1666. // A ')' last for; flag to be treated as'
  1667. parse_syntax_for_flag = 1;
  1668. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1669. p=parse_line(p);
  1670. syntax.curly_count--;
  1671. parse_syntax_for_flag = 0;
  1672. // Skip to the determination process conditions
  1673. sprintf(label,"goto __FR%x_J;",syntax.curly[pos].index);
  1674. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1675. parse_line(label);
  1676. syntax.curly_count--;
  1677. // Loop start labeling
  1678. sprintf(label,"__FR%x_BGN",syntax.curly[pos].index);
  1679. l=add_str(label);
  1680. set_label(l,script_pos,p);
  1681. return p;
  1682. }
  1683. else if( p2 - p == 8 && strncasecmp(p,"function",8) == 0 )
  1684. {// internal script function
  1685. const char *func_name;
  1686. func_name = skip_space(p2);
  1687. p = skip_word(func_name);
  1688. if( p == func_name )
  1689. disp_error_message("parse_syntax:function: function name is missing or invalid", p);
  1690. p2 = skip_space(p);
  1691. if( *p2 == ';' )
  1692. {// function <name> ;
  1693. // function declaration - just register the name
  1694. int l;
  1695. l = add_word(func_name);
  1696. if( str_data[l].type == C_NOP )// register only, if the name was not used by something else
  1697. str_data[l].type = C_USERFUNC;
  1698. else if( str_data[l].type == C_USERFUNC )
  1699. ; // already registered
  1700. else
  1701. disp_error_message("parse_syntax:function: function name is invalid", func_name);
  1702. // Close condition of if, for, while
  1703. p = parse_syntax_close(p2 + 1);
  1704. return p;
  1705. }
  1706. else if(*p2 == '{')
  1707. {// function <name> <line/block of code>
  1708. char label[256];
  1709. int l;
  1710. syntax.curly[syntax.curly_count].type = TYPE_USERFUNC;
  1711. syntax.curly[syntax.curly_count].count = 1;
  1712. syntax.curly[syntax.curly_count].index = syntax.index++;
  1713. syntax.curly[syntax.curly_count].flag = 0;
  1714. ++syntax.curly_count;
  1715. // Jump over the function code
  1716. sprintf(label, "goto __FN%x_FIN;", syntax.curly[syntax.curly_count-1].index);
  1717. syntax.curly[syntax.curly_count].type = TYPE_NULL;
  1718. ++syntax.curly_count;
  1719. parse_line(label);
  1720. --syntax.curly_count;
  1721. // Set the position of the function (label)
  1722. l=add_word(func_name);
  1723. if( str_data[l].type == C_NOP || str_data[l].type == C_USERFUNC )// register only, if the name was not used by something else
  1724. {
  1725. str_data[l].type = C_USERFUNC;
  1726. set_label(l, script_pos, p);
  1727. if( parse_options&SCRIPT_USE_LABEL_DB )
  1728. strdb_iput(scriptlabel_db, get_str(l), script_pos);
  1729. }
  1730. else
  1731. disp_error_message("parse_syntax:function: function name is invalid", func_name);
  1732. return skip_space(p);
  1733. }
  1734. else
  1735. {
  1736. disp_error_message("expect ';' or '{' at function syntax",p);
  1737. }
  1738. }
  1739. break;
  1740. case 'i':
  1741. case 'I':
  1742. if(p2 - p == 2 && !strncasecmp(p,"if",2)) {
  1743. // If process
  1744. char label[256];
  1745. p=skip_space(p2);
  1746. if(*p != '(') { //Prevent if this {} non-c syntax. from Rayce (jA)
  1747. disp_error_message("need '('",p);
  1748. }
  1749. syntax.curly[syntax.curly_count].type = TYPE_IF;
  1750. syntax.curly[syntax.curly_count].count = 1;
  1751. syntax.curly[syntax.curly_count].index = syntax.index++;
  1752. syntax.curly[syntax.curly_count].flag = 0;
  1753. sprintf(label,"__IF%x_%x",syntax.curly[syntax.curly_count].index,syntax.curly[syntax.curly_count].count);
  1754. syntax.curly_count++;
  1755. add_scriptl(add_str("jump_zero"));
  1756. add_scriptc(C_ARG);
  1757. p=parse_expr(p);
  1758. p=skip_space(p);
  1759. add_scriptl(add_str(label));
  1760. add_scriptc(C_FUNC);
  1761. return p;
  1762. }
  1763. break;
  1764. case 's':
  1765. case 'S':
  1766. if(p2 - p == 6 && !strncasecmp(p,"switch",6)) {
  1767. // Processing of switch ()
  1768. char label[256];
  1769. p=skip_space(p2);
  1770. if(*p != '(') {
  1771. disp_error_message("need '('",p);
  1772. }
  1773. syntax.curly[syntax.curly_count].type = TYPE_SWITCH;
  1774. syntax.curly[syntax.curly_count].count = 1;
  1775. syntax.curly[syntax.curly_count].index = syntax.index++;
  1776. syntax.curly[syntax.curly_count].flag = 0;
  1777. sprintf(label,"$@__SW%x_VAL",syntax.curly[syntax.curly_count].index);
  1778. syntax.curly_count++;
  1779. add_scriptl(add_str("set"));
  1780. add_scriptc(C_ARG);
  1781. add_scriptl(add_str(label));
  1782. p=parse_expr(p);
  1783. p=skip_space(p);
  1784. if(*p != '{') {
  1785. disp_error_message("parse_syntax: expected '{'",p);
  1786. }
  1787. add_scriptc(C_FUNC);
  1788. return p + 1;
  1789. }
  1790. break;
  1791. case 'w':
  1792. case 'W':
  1793. if(p2 - p == 5 && !strncasecmp(p,"while",5)) {
  1794. int l;
  1795. char label[256];
  1796. p=skip_space(p2);
  1797. if(*p != '(') {
  1798. disp_error_message("need '('",p);
  1799. }
  1800. syntax.curly[syntax.curly_count].type = TYPE_WHILE;
  1801. syntax.curly[syntax.curly_count].count = 1;
  1802. syntax.curly[syntax.curly_count].index = syntax.index++;
  1803. syntax.curly[syntax.curly_count].flag = 0;
  1804. // Form the start of label decision
  1805. sprintf(label,"__WL%x_NXT",syntax.curly[syntax.curly_count].index);
  1806. l=add_str(label);
  1807. set_label(l,script_pos,p);
  1808. // Skip to the end point if the condition is false
  1809. sprintf(label,"__WL%x_FIN",syntax.curly[syntax.curly_count].index);
  1810. syntax.curly_count++;
  1811. add_scriptl(add_str("jump_zero"));
  1812. add_scriptc(C_ARG);
  1813. p=parse_expr(p);
  1814. p=skip_space(p);
  1815. add_scriptl(add_str(label));
  1816. add_scriptc(C_FUNC);
  1817. return p;
  1818. }
  1819. break;
  1820. }
  1821. return NULL;
  1822. }
  1823. const char* parse_syntax_close(const char *p) {
  1824. // If (...) for (...) hoge (); as to make sure closed closed once again
  1825. int flag;
  1826. do {
  1827. p = parse_syntax_close_sub(p,&flag);
  1828. } while(flag);
  1829. return p;
  1830. }
  1831. // Close judgment if, for, while, of do
  1832. // flag == 1 : closed
  1833. // flag == 0 : not closed
  1834. const char* parse_syntax_close_sub(const char* p,int* flag)
  1835. {
  1836. char label[256];
  1837. int pos = syntax.curly_count - 1;
  1838. int l;
  1839. *flag = 1;
  1840. if(syntax.curly_count <= 0) {
  1841. *flag = 0;
  1842. return p;
  1843. } else if(syntax.curly[pos].type == TYPE_IF) {
  1844. const char *bp = p;
  1845. const char *p2;
  1846. // if-block and else-block end is a new line
  1847. parse_nextline(false, p);
  1848. // Skip to the last location if
  1849. sprintf(label,"goto __IF%x_FIN;",syntax.curly[pos].index);
  1850. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1851. parse_line(label);
  1852. syntax.curly_count--;
  1853. // Put the label of the location
  1854. sprintf(label,"__IF%x_%x",syntax.curly[pos].index,syntax.curly[pos].count);
  1855. l=add_str(label);
  1856. set_label(l,script_pos,p);
  1857. syntax.curly[pos].count++;
  1858. p = skip_space(p);
  1859. p2 = skip_word(p);
  1860. if(!syntax.curly[pos].flag && p2 - p == 4 && !strncasecmp(p,"else",4)) {
  1861. // else or else - if
  1862. p = skip_space(p2);
  1863. p2 = skip_word(p);
  1864. if(p2 - p == 2 && !strncasecmp(p,"if",2)) {
  1865. // else - if
  1866. p=skip_space(p2);
  1867. if(*p != '(') {
  1868. disp_error_message("need '('",p);
  1869. }
  1870. sprintf(label,"__IF%x_%x",syntax.curly[pos].index,syntax.curly[pos].count);
  1871. add_scriptl(add_str("jump_zero"));
  1872. add_scriptc(C_ARG);
  1873. p=parse_expr(p);
  1874. p=skip_space(p);
  1875. add_scriptl(add_str(label));
  1876. add_scriptc(C_FUNC);
  1877. *flag = 0;
  1878. return p;
  1879. } else {
  1880. // else
  1881. if(!syntax.curly[pos].flag) {
  1882. syntax.curly[pos].flag = 1;
  1883. *flag = 0;
  1884. return p;
  1885. }
  1886. }
  1887. }
  1888. // Close if
  1889. syntax.curly_count--;
  1890. // Put the label of the final location
  1891. sprintf(label,"__IF%x_FIN",syntax.curly[pos].index);
  1892. l=add_str(label);
  1893. set_label(l,script_pos,p);
  1894. if(syntax.curly[pos].flag == 1) {
  1895. // Because the position of the pointer is the same if not else for this
  1896. return bp;
  1897. }
  1898. return p;
  1899. } else if(syntax.curly[pos].type == TYPE_DO) {
  1900. int l2;
  1901. char label2[256];
  1902. const char *p2;
  1903. if(syntax.curly[pos].flag) {
  1904. // (Come here continue) to form the label here
  1905. sprintf(label2,"__DO%x_NXT",syntax.curly[pos].index);
  1906. l2=add_str(label2);
  1907. set_label(l2,script_pos,p);
  1908. }
  1909. // Skip to the end point if the condition is false
  1910. p = skip_space(p);
  1911. p2 = skip_word(p);
  1912. if(p2 - p != 5 || strncasecmp(p,"while",5))
  1913. disp_error_message("parse_syntax: expected 'while'",p);
  1914. p = skip_space(p2);
  1915. if(*p != '(') {
  1916. disp_error_message("need '('",p);
  1917. }
  1918. // do-block end is a new line
  1919. parse_nextline(false, p);
  1920. sprintf(label2,"__DO%x_FIN",syntax.curly[pos].index);
  1921. add_scriptl(add_str("jump_zero"));
  1922. add_scriptc(C_ARG);
  1923. p=parse_expr(p);
  1924. p=skip_space(p);
  1925. add_scriptl(add_str(label2));
  1926. add_scriptc(C_FUNC);
  1927. // Skip to the starting point
  1928. sprintf(label2,"goto __DO%x_BGN;",syntax.curly[pos].index);
  1929. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1930. parse_line(label2);
  1931. syntax.curly_count--;
  1932. // Form label of the end point conditions
  1933. sprintf(label2,"__DO%x_FIN",syntax.curly[pos].index);
  1934. l2=add_str(label2);
  1935. set_label(l2,script_pos,p);
  1936. p = skip_space(p);
  1937. if(*p != ';') {
  1938. disp_error_message("parse_syntax: expected ';'",p);
  1939. return p+1;
  1940. }
  1941. p++;
  1942. syntax.curly_count--;
  1943. return p;
  1944. } else if(syntax.curly[pos].type == TYPE_FOR) {
  1945. // for-block end is a new line
  1946. parse_nextline(false, p);
  1947. // Skip to the next loop
  1948. sprintf(label,"goto __FR%x_NXT;",syntax.curly[pos].index);
  1949. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1950. parse_line(label);
  1951. syntax.curly_count--;
  1952. // End for labeling
  1953. sprintf(label,"__FR%x_FIN",syntax.curly[pos].index);
  1954. l=add_str(label);
  1955. set_label(l,script_pos,p);
  1956. syntax.curly_count--;
  1957. return p;
  1958. } else if(syntax.curly[pos].type == TYPE_WHILE) {
  1959. // while-block end is a new line
  1960. parse_nextline(false, p);
  1961. // Skip to the decision while
  1962. sprintf(label,"goto __WL%x_NXT;",syntax.curly[pos].index);
  1963. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1964. parse_line(label);
  1965. syntax.curly_count--;
  1966. // End while labeling
  1967. sprintf(label,"__WL%x_FIN",syntax.curly[pos].index);
  1968. l=add_str(label);
  1969. set_label(l,script_pos,p);
  1970. syntax.curly_count--;
  1971. return p;
  1972. } else if(syntax.curly[syntax.curly_count-1].type == TYPE_USERFUNC) {
  1973. int pos2 = syntax.curly_count-1;
  1974. char label2[256];
  1975. int l2;
  1976. // Back
  1977. sprintf(label2,"return;");
  1978. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1979. parse_line(label2);
  1980. syntax.curly_count--;
  1981. // Put the label of the location
  1982. sprintf(label2,"__FN%x_FIN",syntax.curly[pos2].index);
  1983. l2=add_str(label2);
  1984. set_label(l2,script_pos,p);
  1985. syntax.curly_count--;
  1986. return p;
  1987. } else {
  1988. *flag = 0;
  1989. return p;
  1990. }
  1991. }
  1992. /*==========================================
  1993. * Added built-in functions
  1994. *------------------------------------------*/
  1995. static void add_buildin_func(void)
  1996. {
  1997. int i;
  1998. for( i = 0; buildin_func[i].func; i++ )
  1999. {
  2000. // arg must follow the pattern: (v|s|i|r|l)*\?*\*?
  2001. // 'v' - value (either string or int or reference)
  2002. // 's' - string
  2003. // 'i' - int
  2004. // 'r' - reference (of a variable)
  2005. // 'l' - label
  2006. // '?' - one optional parameter
  2007. // '*' - unknown number of optional parameters
  2008. const char* p = buildin_func[i].arg;
  2009. while( *p == 'v' || *p == 's' || *p == 'i' || *p == 'r' || *p == 'l' ) ++p;
  2010. while( *p == '?' ) ++p;
  2011. if( *p == '*' ) ++p;
  2012. if( *p != 0){
  2013. ShowWarning("add_buildin_func: ignoring function \"%s\" with invalid arg \"%s\".\n", buildin_func[i].name, buildin_func[i].arg);
  2014. } else if( *skip_word(buildin_func[i].name) != 0 ){
  2015. ShowWarning("add_buildin_func: ignoring function with invalid name \"%s\" (must be a word).\n", buildin_func[i].name);
  2016. } else {
  2017. int n = add_str(buildin_func[i].name);
  2018. str_data[n].type = C_FUNC;
  2019. str_data[n].val = i;
  2020. str_data[n].func = buildin_func[i].func;
  2021. if (!strcmp(buildin_func[i].name, "setr")) buildin_set_ref = n;
  2022. else if (!strcmp(buildin_func[i].name, "callsub")) buildin_callsub_ref = n;
  2023. else if (!strcmp(buildin_func[i].name, "callfunc")) buildin_callfunc_ref = n;
  2024. else if( !strcmp(buildin_func[i].name, "getelementofarray") ) buildin_getelementofarray_ref = n;
  2025. }
  2026. }
  2027. }
  2028. /// Retrieves the value of a constant.
  2029. bool script_get_constant(const char* name, int* value)
  2030. {
  2031. int n = search_str(name);
  2032. if( n == -1 || str_data[n].type != C_INT )
  2033. {// not found or not a constant
  2034. return false;
  2035. }
  2036. value[0] = str_data[n].val;
  2037. return true;
  2038. }
  2039. /// Creates new constant or parameter with given value.
  2040. void script_set_constant(const char* name, int value, bool isparameter)
  2041. {
  2042. int n = add_str(name);
  2043. if( str_data[n].type == C_NOP )
  2044. {// new
  2045. str_data[n].type = isparameter ? C_PARAM : C_INT;
  2046. str_data[n].val = value;
  2047. }
  2048. else if( str_data[n].type == C_PARAM || str_data[n].type == C_INT )
  2049. {// existing parameter or constant
  2050. 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);
  2051. }
  2052. else
  2053. {// existing name
  2054. ShowError("script_set_constant: Invalid name for %s '%s' (already defined as %s).\n", isparameter ? "parameter" : "constant", name, script_op2name(str_data[n].type));
  2055. }
  2056. }
  2057. /*==========================================
  2058. * Reading constant databases
  2059. * const.txt
  2060. *------------------------------------------*/
  2061. static void read_constdb(void)
  2062. {
  2063. FILE *fp;
  2064. char line[1024],name[1024],val[1024];
  2065. int type;
  2066. int entries=0, skipped=0, linenum=0;
  2067. sprintf(line, "%s/const.txt", db_path);
  2068. fp=fopen(line, "r");
  2069. if(fp==NULL){
  2070. ShowError("can't read %s\n", line);
  2071. return ;
  2072. }
  2073. while(fgets(line, sizeof(line), fp))
  2074. {
  2075. linenum++;
  2076. if( line[0] == '\0' || line[0] == '\n' || line[0] == '\r') //ignore empty line
  2077. continue;
  2078. if(line[0]=='/' && line[1]=='/') //ignore commented line
  2079. continue;
  2080. type=0;
  2081. if(sscanf(line,"%1023[A-Za-z0-9/_],%1023[A-Za-z0-9/_-],%11d",name,val,&type)>=2 ||
  2082. sscanf(line,"%1023[A-Za-z0-9/_] %1023[A-Za-z0-9/_-] %11d",name,val,&type)>=2){
  2083. entries++;
  2084. script_set_constant(name, (int)strtol(val, NULL, 0), (bool)type);
  2085. }
  2086. else {
  2087. skipped++;
  2088. ShowWarning("Skipping line '"CL_WHITE"%d"CL_RESET"', invalide constant definition\n",linenum);
  2089. }
  2090. }
  2091. fclose(fp);
  2092. ShowStatus("Done reading '"CL_WHITE"%d"CL_RESET"' entries in '"CL_WHITE"%s/const.txt"CL_RESET"'.\n", entries, db_path);
  2093. if(skipped){
  2094. ShowWarning("Skipped '"CL_WHITE"%d"CL_RESET"', entries\n",skipped);
  2095. }
  2096. }
  2097. /**
  2098. * Sets source-end constants for NPC scripts to access.
  2099. **/
  2100. void script_hardcoded_constants(void) {
  2101. #include "script_constants.h"
  2102. }
  2103. /*==========================================
  2104. * Display emplacement line of script
  2105. *------------------------------------------*/
  2106. static const char* script_print_line(StringBuf* buf, const char* p, const char* mark, int line)
  2107. {
  2108. int i;
  2109. if( p == NULL || !p[0] ) return NULL;
  2110. if( line < 0 )
  2111. StringBuf_Printf(buf, "*% 5d : ", -line);
  2112. else
  2113. StringBuf_Printf(buf, " % 5d : ", line);
  2114. for(i=0;p[i] && p[i] != '\n';i++){
  2115. if(p + i != mark)
  2116. StringBuf_Printf(buf, "%c", p[i]);
  2117. else
  2118. StringBuf_Printf(buf, "\'%c\'", p[i]);
  2119. }
  2120. StringBuf_AppendStr(buf, "\n");
  2121. return p+i+(p[i] == '\n' ? 1 : 0);
  2122. }
  2123. 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) {
  2124. // Find the line where the error occurred
  2125. int j;
  2126. int line = start_line;
  2127. const char *p;
  2128. const char *linestart[5] = { NULL, NULL, NULL, NULL, NULL };
  2129. for(p=src;p && *p;line++){
  2130. const char *lineend=strchr(p,'\n');
  2131. if(lineend==NULL || error_pos_cur<lineend){
  2132. break;
  2133. }
  2134. for( j = 0; j < 4; j++ ) {
  2135. linestart[j] = linestart[j+1];
  2136. }
  2137. linestart[4] = p;
  2138. p=lineend+1;
  2139. }
  2140. StringBuf_Printf(buf, "script error on %s line %d\n", file, line);
  2141. StringBuf_Printf(buf, " %s\n", error_msg_cur);
  2142. for(j = 0; j < 5; j++ ) {
  2143. script_print_line(buf, linestart[j], NULL, line + j - 5);
  2144. }
  2145. p = script_print_line(buf, p, error_pos_cur, -line);
  2146. for(j = 0; j < 5; j++) {
  2147. p = script_print_line(buf, p, NULL, line + j + 1 );
  2148. }
  2149. }
  2150. void script_error(const char* src, const char* file, int start_line, const char* error_msg_cur, const char* error_pos_cur) {
  2151. StringBuf buf;
  2152. StringBuf_Init(&buf);
  2153. StringBuf_AppendStr(&buf, "\a\n");
  2154. script_errorwarning_sub(&buf, src, file, start_line, error_msg_cur, error_pos_cur);
  2155. ShowError("%s", StringBuf_Value(&buf));
  2156. StringBuf_Destroy(&buf);
  2157. }
  2158. void script_warning(const char* src, const char* file, int start_line, const char* error_msg_cur, const char* error_pos_cur) {
  2159. StringBuf buf;
  2160. StringBuf_Init(&buf);
  2161. script_errorwarning_sub(&buf, src, file, start_line, error_msg_cur, error_pos_cur);
  2162. ShowWarning("%s", StringBuf_Value(&buf));
  2163. StringBuf_Destroy(&buf);
  2164. }
  2165. /*==========================================
  2166. * Analysis of the script
  2167. *------------------------------------------*/
  2168. struct script_code* parse_script(const char *src,const char *file,int line,int options)
  2169. {
  2170. const char *p,*tmpp;
  2171. int i;
  2172. struct script_code* code = NULL;
  2173. static int first=1;
  2174. char end;
  2175. bool unresolved_names = false;
  2176. parser_current_src = src;
  2177. parser_current_file = file;
  2178. parser_current_line = line;
  2179. if( src == NULL )
  2180. return NULL;// empty script
  2181. memset(&syntax,0,sizeof(syntax));
  2182. if(first){
  2183. add_buildin_func();
  2184. read_constdb();
  2185. script_hardcoded_constants();
  2186. first=0;
  2187. }
  2188. script_buf=(unsigned char *)aMalloc(SCRIPT_BLOCK_SIZE*sizeof(unsigned char));
  2189. script_pos=0;
  2190. script_size=SCRIPT_BLOCK_SIZE;
  2191. parse_nextline(true, NULL);
  2192. // who called parse_script is responsible for clearing the database after using it, but just in case... lets clear it here
  2193. if( options&SCRIPT_USE_LABEL_DB )
  2194. db_clear(scriptlabel_db);
  2195. parse_options = options;
  2196. if( setjmp( error_jump ) != 0 ) {
  2197. //Restore program state when script has problems. [from jA]
  2198. int j;
  2199. const int size = ARRAYLENGTH(syntax.curly);
  2200. if( error_report )
  2201. script_error(src,file,line,error_msg,error_pos);
  2202. aFree( error_msg );
  2203. aFree( script_buf );
  2204. script_pos = 0;
  2205. script_size = 0;
  2206. script_buf = NULL;
  2207. for(j=LABEL_START;j<str_num;j++)
  2208. if(str_data[j].type == C_NOP) str_data[j].type = C_NAME;
  2209. for(j=0; j<size; j++)
  2210. linkdb_final(&syntax.curly[j].case_label);
  2211. return NULL;
  2212. }
  2213. parse_syntax_for_flag=0;
  2214. p=src;
  2215. p=skip_space(p);
  2216. if( options&SCRIPT_IGNORE_EXTERNAL_BRACKETS )
  2217. {// does not require brackets around the script
  2218. if( *p == '\0' && !(options&SCRIPT_RETURN_EMPTY_SCRIPT) )
  2219. {// empty script and can return NULL
  2220. aFree( script_buf );
  2221. script_pos = 0;
  2222. script_size = 0;
  2223. script_buf = NULL;
  2224. return NULL;
  2225. }
  2226. end = '\0';
  2227. }
  2228. else
  2229. {// requires brackets around the script
  2230. if( *p != '{' )
  2231. disp_error_message("not found '{'",p);
  2232. p = skip_space(p+1);
  2233. if( *p == '}' && !(options&SCRIPT_RETURN_EMPTY_SCRIPT) )
  2234. {// empty script and can return NULL
  2235. aFree( script_buf );
  2236. script_pos = 0;
  2237. script_size = 0;
  2238. script_buf = NULL;
  2239. return NULL;
  2240. }
  2241. end = '}';
  2242. }
  2243. // clear references of labels, variables and internal functions
  2244. for(i=LABEL_START;i<str_num;i++){
  2245. if(
  2246. str_data[i].type==C_POS || str_data[i].type==C_NAME ||
  2247. str_data[i].type==C_USERFUNC || str_data[i].type == C_USERFUNC_POS
  2248. ){
  2249. str_data[i].type=C_NOP;
  2250. str_data[i].backpatch=-1;
  2251. str_data[i].label=-1;
  2252. }
  2253. }
  2254. while( syntax.curly_count != 0 || *p != end )
  2255. {
  2256. if( *p == '\0' )
  2257. disp_error_message("unexpected end of script",p);
  2258. // Special handling only label
  2259. tmpp=skip_space(skip_word(p));
  2260. if(*tmpp==':' && !(!strncasecmp(p,"default:",8) && p + 7 == tmpp)){
  2261. i=add_word(p);
  2262. set_label(i,script_pos,p);
  2263. if( parse_options&SCRIPT_USE_LABEL_DB )
  2264. strdb_iput(scriptlabel_db, get_str(i), script_pos);
  2265. p=tmpp+1;
  2266. p=skip_space(p);
  2267. continue;
  2268. }
  2269. // All other lumped
  2270. p=parse_line(p);
  2271. p=skip_space(p);
  2272. parse_nextline(false, p);
  2273. }
  2274. add_scriptc(C_NOP);
  2275. // trim code to size
  2276. script_size = script_pos;
  2277. RECREATE(script_buf,unsigned char,script_pos);
  2278. // default unknown references to variables
  2279. for(i=LABEL_START;i<str_num;i++){
  2280. if(str_data[i].type==C_NOP){
  2281. int j;
  2282. str_data[i].type=C_NAME;
  2283. str_data[i].label=i;
  2284. for(j=str_data[i].backpatch;j>=0 && j!=0x00ffffff;){
  2285. int next=GETVALUE(script_buf,j);
  2286. SETVALUE(script_buf,j,i);
  2287. j=next;
  2288. }
  2289. }
  2290. else if( str_data[i].type == C_USERFUNC )
  2291. {// 'function name;' without follow-up code
  2292. ShowError("parse_script: function '%s' declared but not defined.\n", str_buf+str_data[i].str);
  2293. unresolved_names = true;
  2294. }
  2295. }
  2296. if( unresolved_names )
  2297. {
  2298. disp_error_message("parse_script: unresolved function references", p);
  2299. }
  2300. #ifdef DEBUG_DISP
  2301. for(i=0;i<script_pos;i++){
  2302. if((i&15)==0) ShowMessage("%04x : ",i);
  2303. ShowMessage("%02x ",script_buf[i]);
  2304. if((i&15)==15) ShowMessage("\n");
  2305. }
  2306. ShowMessage("\n");
  2307. #endif
  2308. #ifdef DEBUG_DISASM
  2309. {
  2310. int i = 0,j;
  2311. while(i < script_pos) {
  2312. c_op op = get_com(script_buf,&i);
  2313. ShowMessage("%06x %s", i, script_op2name(op));
  2314. j = i;
  2315. switch(op) {
  2316. case C_INT:
  2317. ShowMessage(" %d", get_num(script_buf,&i));
  2318. break;
  2319. case C_POS:
  2320. ShowMessage(" 0x%06x", *(int*)(script_buf+i)&0xffffff);
  2321. i += 3;
  2322. break;
  2323. case C_NAME:
  2324. j = (*(int*)(script_buf+i)&0xffffff);
  2325. ShowMessage(" %s", ( j == 0xffffff ) ? "?? unknown ??" : get_str(j));
  2326. i += 3;
  2327. break;
  2328. case C_STR:
  2329. j = strlen(script_buf + i);
  2330. ShowMessage(" %s", script_buf + i);
  2331. i += j+1;
  2332. break;
  2333. }
  2334. ShowMessage(CL_CLL"\n");
  2335. }
  2336. }
  2337. #endif
  2338. CREATE(code,struct script_code,1);
  2339. code->script_buf = script_buf;
  2340. code->script_size = script_size;
  2341. code->local.vars = NULL;
  2342. code->local.arrays = NULL;
  2343. return code;
  2344. }
  2345. /// Returns the player attached to this script, identified by the rid.
  2346. /// If there is no player attached, the script is terminated.
  2347. TBL_PC *script_rid2sd(struct script_state *st)
  2348. {
  2349. TBL_PC *sd=map_id2sd(st->rid);
  2350. if(!sd){
  2351. ShowError("script_rid2sd: fatal error ! player not attached!\n");
  2352. script_reportfunc(st);
  2353. script_reportsrc(st);
  2354. st->state = END;
  2355. }
  2356. return sd;
  2357. }
  2358. /**
  2359. * Dereferences a variable/constant, replacing it with a copy of the value.
  2360. * @param st Script state
  2361. * @param data Variable/constant
  2362. * @param sd If NULL, will try to use sd from st->rid (for player's variables)
  2363. */
  2364. void get_val_(struct script_state* st, struct script_data* data, struct map_session_data *sd)
  2365. {
  2366. const char* name;
  2367. char prefix;
  2368. char postfix;
  2369. if( !data_isreference(data) )
  2370. return;// not a variable/constant
  2371. name = reference_getname(data);
  2372. prefix = name[0];
  2373. postfix = name[strlen(name) - 1];
  2374. //##TODO use reference_tovariable(data) when it's confirmed that it works [FlavioJS]
  2375. if( !reference_toconstant(data) && not_server_variable(prefix) ) {
  2376. if (sd == NULL)
  2377. sd = script_rid2sd(st);
  2378. if( sd == NULL ) {// needs player attached
  2379. if( postfix == '$' ) {// string variable
  2380. ShowWarning("script:get_val: cannot access player variable '%s', defaulting to \"\"\n", name);
  2381. data->type = C_CONSTSTR;
  2382. data->u.str = "";
  2383. } else {// integer variable
  2384. ShowWarning("script:get_val: cannot access player variable '%s', defaulting to 0\n", name);
  2385. data->type = C_INT;
  2386. data->u.num = 0;
  2387. }
  2388. return;
  2389. }
  2390. }
  2391. if( postfix == '$' ) {// string variable
  2392. switch( prefix ) {
  2393. case '@':
  2394. data->u.str = pc_readregstr(sd, data->u.num);
  2395. break;
  2396. case '$':
  2397. data->u.str = mapreg_readregstr(data->u.num);
  2398. break;
  2399. case '#':
  2400. if( name[1] == '#' )
  2401. data->u.str = pc_readaccountreg2str(sd, data->u.num);// global
  2402. else
  2403. data->u.str = pc_readaccountregstr(sd, data->u.num);// local
  2404. break;
  2405. case '.':
  2406. {
  2407. struct DBMap* n = data->ref ?
  2408. data->ref->vars : name[1] == '@' ?
  2409. st->stack->scope.vars : // instance/scope variable
  2410. st->script->local.vars; // npc variable
  2411. if( n )
  2412. data->u.str = (char*)i64db_get(n,reference_getuid(data));
  2413. else
  2414. data->u.str = NULL;
  2415. }
  2416. break;
  2417. case '\'':
  2418. {
  2419. unsigned short instance_id = script_instancegetid(st);
  2420. if( instance_id )
  2421. data->u.str = (char*)i64db_get(instance_data[instance_id].regs.vars,reference_getuid(data));
  2422. else {
  2423. ShowWarning("script:get_val: cannot access instance variable '%s', defaulting to \"\"\n", name);
  2424. data->u.str = NULL;
  2425. }
  2426. }
  2427. break;
  2428. default:
  2429. data->u.str = pc_readglobalreg_str(sd, data->u.num);
  2430. break;
  2431. }
  2432. if( data->u.str == NULL || data->u.str[0] == '\0' ) {// empty string
  2433. data->type = C_CONSTSTR;
  2434. data->u.str = "";
  2435. } else {// duplicate string
  2436. data->type = C_STR;
  2437. data->u.str = aStrdup(data->u.str);
  2438. }
  2439. } else {// integer variable
  2440. data->type = C_INT;
  2441. if( reference_toconstant(data) ) {
  2442. data->u.num = reference_getconstant(data);
  2443. } else if( reference_toparam(data) ) {
  2444. data->u.num = pc_readparam(sd, reference_getparamtype(data));
  2445. } else
  2446. switch( prefix ) {
  2447. case '@':
  2448. data->u.num = pc_readreg(sd, data->u.num);
  2449. break;
  2450. case '$':
  2451. data->u.num = mapreg_readreg(data->u.num);
  2452. break;
  2453. case '#':
  2454. if( name[1] == '#' )
  2455. data->u.num = pc_readaccountreg2(sd, data->u.num);// global
  2456. else
  2457. data->u.num = pc_readaccountreg(sd, data->u.num);// local
  2458. break;
  2459. case '.':
  2460. {
  2461. struct DBMap* n = data->ref ?
  2462. data->ref->vars : name[1] == '@' ?
  2463. st->stack->scope.vars : // instance/scope variable
  2464. st->script->local.vars; // npc variable
  2465. if( n )
  2466. data->u.num = (int)i64db_iget(n,reference_getuid(data));
  2467. else
  2468. data->u.num = 0;
  2469. }
  2470. break;
  2471. case '\'':
  2472. {
  2473. unsigned short instance_id = script_instancegetid(st);
  2474. if( instance_id )
  2475. data->u.num = (int)i64db_iget(instance_data[instance_id].regs.vars,reference_getuid(data));
  2476. else {
  2477. ShowWarning("script:get_val: cannot access instance variable '%s', defaulting to 0\n", name);
  2478. data->u.num = 0;
  2479. }
  2480. }
  2481. break;
  2482. default:
  2483. data->u.num = pc_readglobalreg(sd, data->u.num);
  2484. break;
  2485. }
  2486. }
  2487. data->ref = NULL;
  2488. return;
  2489. }
  2490. void get_val(struct script_state* st, struct script_data* data)
  2491. {
  2492. get_val_(st,data,NULL);
  2493. }
  2494. struct script_data* push_val2(struct script_stack* stack, enum c_op type, int64 val, struct reg_db* ref);
  2495. /// Retrieves the value of a reference identified by uid (variable, constant, param)
  2496. /// The value is left in the top of the stack and needs to be removed manually.
  2497. /// @param st[in]: script state.
  2498. /// @param uid[in]: reference identifier.
  2499. /// @param ref[in]: the container to look up the reference into.
  2500. /// @return: the retrieved value of the reference.
  2501. void* get_val2(struct script_state* st, int64 uid, struct reg_db *ref)
  2502. {
  2503. struct script_data* data;
  2504. push_val2(st->stack, C_NAME, uid, ref);
  2505. data = script_getdatatop(st, -1);
  2506. get_val(st, data);
  2507. //! TODO: Support data->u.num as int64 instead cast it to int? (in get_val, it was casted to int).
  2508. return (data->type == C_INT ? (void*)__64BPRTSIZE((int)data->u.num) : (void*)__64BPRTSIZE(data->u.str));
  2509. }
  2510. /**
  2511. * Because, currently, array members with key 0 are indifferenciable from normal variables, we should ensure its actually in
  2512. * Will be gone as soon as undefined var feature is implemented
  2513. **/
  2514. void script_array_ensure_zero(struct script_state *st, struct map_session_data *sd, int64 uid, struct reg_db *ref)
  2515. {
  2516. const char *name = get_str(script_getvarid(uid));
  2517. // is here st can be null pointer and st->rid is wrong?
  2518. struct reg_db *src = script_array_src(st, sd ? sd : st->rid ? map_id2sd(st->rid) : NULL, name, ref);
  2519. bool insert = false;
  2520. if (sd && !st) {
  2521. // when sd comes, st isn't available
  2522. insert = true;
  2523. } else {
  2524. if( is_string_variable(name) ) {
  2525. char* str = (char*)get_val2(st, uid, ref);
  2526. if( str && *str )
  2527. insert = true;
  2528. script_removetop(st, -1, 0);
  2529. } else {
  2530. int32 num = (int32)__64BPRTSIZE(get_val2(st, uid, ref));
  2531. if( num )
  2532. insert = true;
  2533. script_removetop(st, -1, 0);
  2534. }
  2535. }
  2536. if (src && src->arrays) {
  2537. struct script_array *sa = idb_get(src->arrays, script_getvarid(uid));
  2538. if (sa) {
  2539. unsigned int i;
  2540. ARR_FIND(0, sa->size, i, sa->members[i] == 0);
  2541. if( i != sa->size ) {
  2542. if( !insert )
  2543. script_array_remove_member(src,sa,i);
  2544. return;
  2545. }
  2546. script_array_add_member(sa,0);
  2547. } else if (insert) {
  2548. script_array_update(src,reference_uid(script_getvarid(uid), 0),false);
  2549. }
  2550. }
  2551. }
  2552. /**
  2553. * Returns array size by ID
  2554. **/
  2555. unsigned int script_array_size(struct script_state *st, struct map_session_data *sd, const char *name, struct reg_db *ref)
  2556. {
  2557. struct script_array *sa = NULL;
  2558. struct reg_db *src = script_array_src(st, sd, name, ref);
  2559. if (src && src->arrays)
  2560. sa = idb_get(src->arrays, search_str(name));
  2561. return sa ? sa->size : 0;
  2562. }
  2563. /**
  2564. * Returns array's highest key (for that awful getarraysize implementation that doesn't really gets the array size)
  2565. **/
  2566. unsigned int script_array_highest_key(struct script_state *st, struct map_session_data *sd, const char *name, struct reg_db *ref)
  2567. {
  2568. struct script_array *sa = NULL;
  2569. struct reg_db *src = script_array_src(st, sd, name, ref);
  2570. if (src && src->arrays) {
  2571. int key = add_word(name);
  2572. script_array_ensure_zero(st,sd,reference_uid(key, 0), ref);
  2573. if( ( sa = idb_get(src->arrays, key) ) ) {
  2574. unsigned int i, highest_key = 0;
  2575. for(i = 0; i < sa->size; i++) {
  2576. if( sa->members[i] > highest_key )
  2577. highest_key = sa->members[i];
  2578. }
  2579. return sa->size ? highest_key + 1 : 0;
  2580. }
  2581. }
  2582. return SCRIPT_CMD_SUCCESS;
  2583. }
  2584. int script_free_array_db(DBKey key, DBData *data, va_list ap)
  2585. {
  2586. struct script_array *sa = db_data2ptr(data);
  2587. aFree(sa->members);
  2588. ers_free(array_ers, sa);
  2589. return SCRIPT_CMD_SUCCESS;
  2590. }
  2591. /**
  2592. * Clears script_array and removes it from script->array_db
  2593. **/
  2594. void script_array_delete(struct reg_db *src, struct script_array *sa)
  2595. {
  2596. aFree(sa->members);
  2597. idb_remove(src->arrays, sa->id);
  2598. ers_free(array_ers, sa);
  2599. }
  2600. /**
  2601. * Removes a member from a script_array list
  2602. *
  2603. * @param idx the index of the member in script_array struct list, not of the actual array member
  2604. **/
  2605. void script_array_remove_member(struct reg_db *src, struct script_array *sa, unsigned int idx)
  2606. {
  2607. unsigned int i, cursor;
  2608. // it's the only member left, no need to do anything other than delete the array data
  2609. if( sa->size == 1 ) {
  2610. script_array_delete(src,sa);
  2611. return;
  2612. }
  2613. sa->members[idx] = UINT_MAX;
  2614. for(i = 0, cursor = 0; i < sa->size; i++) {
  2615. if( sa->members[i] == UINT_MAX )
  2616. continue;
  2617. if( i != cursor )
  2618. sa->members[cursor] = sa->members[i];
  2619. cursor++;
  2620. }
  2621. sa->size = cursor;
  2622. }
  2623. /**
  2624. * Appends a new array index to the list in script_array
  2625. *
  2626. * @param idx the index of the array member being inserted
  2627. **/
  2628. void script_array_add_member(struct script_array *sa, unsigned int idx)
  2629. {
  2630. RECREATE(sa->members, unsigned int, ++sa->size);
  2631. sa->members[sa->size - 1] = idx;
  2632. }
  2633. /**
  2634. * Obtains the source of the array database for this type and scenario
  2635. * Initializes such database when not yet initialized.
  2636. **/
  2637. struct reg_db *script_array_src(struct script_state *st, struct map_session_data *sd, const char *name, struct reg_db *ref)
  2638. {
  2639. struct reg_db *src = NULL;
  2640. switch( name[0] ) {
  2641. // from player
  2642. default: // char reg
  2643. case '@': // temp char reg
  2644. case '#': // account reg
  2645. src = &sd->regs;
  2646. break;
  2647. case '$': // map reg
  2648. src = &regs;
  2649. break;
  2650. case '.': // npc/script
  2651. if (ref)
  2652. src = ref;
  2653. else
  2654. src = (name[1] == '@') ? &st->stack->scope : &st->script->local;
  2655. break;
  2656. case '\'': // instance
  2657. {
  2658. unsigned short instance_id = script_instancegetid(st);
  2659. if( instance_id ) {
  2660. src = &instance_data[instance_id].regs;
  2661. }
  2662. break;
  2663. }
  2664. }
  2665. if( src ) {
  2666. if( !src->arrays )
  2667. src->arrays = idb_alloc(DB_OPT_BASE);
  2668. return src;
  2669. }
  2670. return NULL;
  2671. }
  2672. /**
  2673. * Processes a array member modification, and update data accordingly
  2674. *
  2675. * @param src[in,out] Variable source database. If the array database doesn't exist, it is created.
  2676. * @param num[in] Variable ID
  2677. * @param empty[in] Whether the modified member is empty (needs to be removed)
  2678. **/
  2679. void script_array_update(struct reg_db *src, int64 num, bool empty)
  2680. {
  2681. struct script_array *sa = NULL;
  2682. int id = script_getvarid(num);
  2683. unsigned int index = script_getvaridx(num);
  2684. if (!src->arrays) {
  2685. src->arrays = idb_alloc(DB_OPT_BASE);
  2686. } else {
  2687. sa = idb_get(src->arrays, id);
  2688. }
  2689. if( sa ) {
  2690. unsigned int i;
  2691. // search
  2692. for(i = 0; i < sa->size; i++) {
  2693. if( sa->members[i] == index )
  2694. break;
  2695. }
  2696. // if existent
  2697. if( i != sa->size ) {
  2698. // if empty, we gotta remove it
  2699. if( empty ) {
  2700. script_array_remove_member(src, sa, i);
  2701. }
  2702. } else if( !empty ) { /* new entry */
  2703. script_array_add_member(sa,index);
  2704. // we do nothing if its empty, no point in modifying array data for a new empty member
  2705. }
  2706. } else if ( !empty ) { // we only move to create if not empty
  2707. sa = ers_alloc(array_ers, struct script_array);
  2708. sa->id = id;
  2709. sa->members = NULL;
  2710. sa->size = 0;
  2711. script_array_add_member(sa,index);
  2712. idb_put(src->arrays, id, sa);
  2713. }
  2714. }
  2715. /**
  2716. * Stores the value of a script variable
  2717. *
  2718. * @param st: current script state.
  2719. * @param sd: current character data.
  2720. * @param num: variable identifier.
  2721. * @param name: variable name.
  2722. * @param value: new value.
  2723. * @param ref: variable container, in case of a npc/scope variable reference outside the current scope.
  2724. * @return: 0 failure, 1 success.
  2725. *
  2726. * TODO: return values are screwed up, have been for some time (reaad: years), e.g. some functions return 1 failure and success.
  2727. *------------------------------------------*/
  2728. int set_reg(struct script_state* st, TBL_PC* sd, int64 num, const char* name, const void* value, struct reg_db *ref)
  2729. {
  2730. char prefix = name[0];
  2731. if( is_string_variable(name) ) {// string variable
  2732. const char *str = (const char*)value;
  2733. switch (prefix) {
  2734. case '@':
  2735. pc_setregstr(sd, num, str);
  2736. return 1;
  2737. case '$':
  2738. return mapreg_setregstr(num, str);
  2739. case '#':
  2740. return (name[1] == '#') ?
  2741. pc_setaccountreg2str(sd, num, str) :
  2742. pc_setaccountregstr(sd, num, str);
  2743. case '.':
  2744. {
  2745. struct reg_db *n = (ref) ? ref : (name[1] == '@') ? &st->stack->scope : &st->script->local;
  2746. if( n ) {
  2747. if (str[0]) {
  2748. i64db_put(n->vars, num, aStrdup(str));
  2749. if( script_getvaridx(num) )
  2750. script_array_update(n, num, false);
  2751. } else {
  2752. i64db_remove(n->vars, num);
  2753. if( script_getvaridx(num) )
  2754. script_array_update(n, num, true);
  2755. }
  2756. }
  2757. }
  2758. return 1;
  2759. case '\'':
  2760. {
  2761. unsigned short instance_id = script_instancegetid(st);
  2762. if( instance_id ) {
  2763. if( str[0] ) {
  2764. i64db_put(instance_data[instance_id].regs.vars, num, aStrdup(str));
  2765. if( script_getvaridx(num) )
  2766. script_array_update(&instance_data[instance_id].regs, num, false);
  2767. } else {
  2768. i64db_remove(instance_data[instance_id].regs.vars, num);
  2769. if (script_getvaridx(num))
  2770. script_array_update(&instance_data[instance_id].regs, num, true);
  2771. }
  2772. } else {
  2773. ShowError("script_set_reg: cannot write instance variable '%s', NPC not in a instance!\n", name);
  2774. script_reportsrc(st);
  2775. }
  2776. return 1;
  2777. }
  2778. default:
  2779. return pc_setglobalreg_str(sd, num, str);
  2780. }
  2781. } else {// integer variable
  2782. int val = (int)__64BPRTSIZE(value);
  2783. if(str_data[script_getvarid(num)].type == C_PARAM) {
  2784. if( pc_setparam(sd, str_data[script_getvarid(num)].val, val) == 0 ) {
  2785. if( st != NULL ) {
  2786. ShowError("script_set_reg: failed to set param '%s' to %d.\n", name, val);
  2787. script_reportsrc(st);
  2788. st->state = END;
  2789. }
  2790. return 0;
  2791. }
  2792. return 1;
  2793. }
  2794. switch (prefix) {
  2795. case '@':
  2796. pc_setreg(sd, num, val);
  2797. return 1;
  2798. case '$':
  2799. return mapreg_setreg(num, val);
  2800. case '#':
  2801. return (name[1] == '#') ?
  2802. pc_setaccountreg2(sd, num, val) :
  2803. pc_setaccountreg(sd, num, val);
  2804. case '.':
  2805. {
  2806. struct reg_db *n = (ref) ? ref : (name[1] == '@') ? &st->stack->scope : &st->script->local;
  2807. if( n ) {
  2808. if( val != 0 ) {
  2809. i64db_iput(n->vars, num, val);
  2810. if( script_getvaridx(num) )
  2811. script_array_update(n, num, false);
  2812. } else {
  2813. i64db_remove(n->vars, num);
  2814. if( script_getvaridx(num) )
  2815. script_array_update(n, num, true);
  2816. }
  2817. }
  2818. }
  2819. return 1;
  2820. case '\'':
  2821. {
  2822. unsigned short instance_id = script_instancegetid(st);
  2823. if( instance_id ) {
  2824. if( val != 0 ) {
  2825. i64db_iput(instance_data[instance_id].regs.vars, num, val);
  2826. if( script_getvaridx(num) )
  2827. script_array_update(&instance_data[instance_id].regs, num, false);
  2828. } else {
  2829. i64db_remove(instance_data[instance_id].regs.vars, num);
  2830. if (script_getvaridx(num))
  2831. script_array_update(&instance_data[instance_id].regs, num, true);
  2832. }
  2833. } else {
  2834. ShowError("script_set_reg: cannot write instance variable '%s', NPC not in a instance!\n", name);
  2835. script_reportsrc(st);
  2836. }
  2837. return 1;
  2838. }
  2839. default:
  2840. return pc_setglobalreg(sd, num, val);
  2841. }
  2842. }
  2843. }
  2844. int set_var(struct map_session_data* sd, char* name, void* val)
  2845. {
  2846. return set_reg(NULL, sd, reference_uid(add_str(name),0), name, val, NULL);
  2847. }
  2848. void setd_sub(struct script_state *st, TBL_PC *sd, const char *varname, int elem, void *value, struct reg_db *ref)
  2849. {
  2850. set_reg(st, sd, reference_uid(add_str(varname),elem), varname, value, ref);
  2851. }
  2852. /**
  2853. * Converts the data to a string
  2854. * @param st
  2855. * @param data
  2856. * @param sd
  2857. */
  2858. const char* conv_str_(struct script_state* st, struct script_data* data, struct map_session_data *sd)
  2859. {
  2860. char* p;
  2861. get_val_(st, data, sd);
  2862. if( data_isstring(data) )
  2863. {// nothing to convert
  2864. }
  2865. else if( data_isint(data) )
  2866. {// int -> string
  2867. CREATE(p, char, ITEM_NAME_LENGTH);
  2868. snprintf(p, ITEM_NAME_LENGTH, "%"PRId64"", data->u.num);
  2869. p[ITEM_NAME_LENGTH-1] = '\0';
  2870. data->type = C_STR;
  2871. data->u.str = p;
  2872. }
  2873. else if( data_isreference(data) )
  2874. {// reference -> string
  2875. //##TODO when does this happen (check get_val) [FlavioJS]
  2876. data->type = C_CONSTSTR;
  2877. data->u.str = reference_getname(data);
  2878. }
  2879. else
  2880. {// unsupported data type
  2881. ShowError("script:conv_str: cannot convert to string, defaulting to \"\"\n");
  2882. script_reportdata(data);
  2883. script_reportsrc(st);
  2884. data->type = C_CONSTSTR;
  2885. data->u.str = "";
  2886. }
  2887. return data->u.str;
  2888. }
  2889. const char* conv_str(struct script_state* st, struct script_data* data)
  2890. {
  2891. return conv_str_(st, data, NULL);
  2892. }
  2893. /**
  2894. * Converts the data to an int
  2895. * @param st
  2896. * @param data
  2897. * @param sd
  2898. */
  2899. int conv_num_(struct script_state* st, struct script_data* data, struct map_session_data *sd)
  2900. {
  2901. get_val_(st, data, sd);
  2902. if( data_isint(data) )
  2903. {// nothing to convert
  2904. }
  2905. else if( data_isstring(data) )
  2906. {// string -> int
  2907. // the result does not overflow or underflow, it is capped instead
  2908. // ex: 999999999999 is capped to INT_MAX (2147483647)
  2909. char* p = data->u.str;
  2910. long num;
  2911. errno = 0;
  2912. num = strtol(data->u.str, NULL, 10);// change radix to 0 to support octal numbers "o377" and hex numbers "0xFF"
  2913. if( errno == ERANGE
  2914. #if LONG_MAX > INT_MAX
  2915. || num < INT_MIN || num > INT_MAX
  2916. #endif
  2917. )
  2918. {
  2919. if( num <= INT_MIN )
  2920. {
  2921. num = INT_MIN;
  2922. ShowError("script:conv_num: underflow detected, capping to %ld\n", num);
  2923. }
  2924. else//if( num >= INT_MAX )
  2925. {
  2926. num = INT_MAX;
  2927. ShowError("script:conv_num: overflow detected, capping to %ld\n", num);
  2928. }
  2929. script_reportdata(data);
  2930. script_reportsrc(st);
  2931. }
  2932. if( data->type == C_STR )
  2933. aFree(p);
  2934. data->type = C_INT;
  2935. data->u.num = (int)num;
  2936. }
  2937. #if 0
  2938. // FIXME this function is being used to retrieve the position of labels and
  2939. // probably other stuff [FlavioJS]
  2940. else
  2941. {// unsupported data type
  2942. ShowError("script:conv_num: cannot convert to number, defaulting to 0\n");
  2943. script_reportdata(data);
  2944. script_reportsrc(st);
  2945. data->type = C_INT;
  2946. data->u.num = 0;
  2947. }
  2948. #endif
  2949. return (int)data->u.num;
  2950. }
  2951. int conv_num(struct script_state* st, struct script_data* data)
  2952. {
  2953. return conv_num_(st, data, NULL);
  2954. }
  2955. //
  2956. // Stack operations
  2957. //
  2958. /// Increases the size of the stack
  2959. void stack_expand(struct script_stack* stack)
  2960. {
  2961. stack->sp_max += 64;
  2962. stack->stack_data = (struct script_data*)aRealloc(stack->stack_data,
  2963. stack->sp_max * sizeof(stack->stack_data[0]) );
  2964. memset(stack->stack_data + (stack->sp_max - 64), 0,
  2965. 64 * sizeof(stack->stack_data[0]) );
  2966. }
  2967. /// Pushes a value into the stack
  2968. #define push_val(stack,type,val) push_val2(stack, type, val, NULL)
  2969. /// Pushes a value into the stack (with reference)
  2970. struct script_data* push_val2(struct script_stack* stack, enum c_op type, int64 val, struct reg_db *ref)
  2971. {
  2972. if( stack->sp >= stack->sp_max )
  2973. stack_expand(stack);
  2974. stack->stack_data[stack->sp].type = type;
  2975. stack->stack_data[stack->sp].u.num = val;
  2976. stack->stack_data[stack->sp].ref = ref;
  2977. stack->sp++;
  2978. return &stack->stack_data[stack->sp-1];
  2979. }
  2980. /// Pushes a string into the stack
  2981. struct script_data* push_str(struct script_stack* stack, enum c_op type, char* str)
  2982. {
  2983. if( stack->sp >= stack->sp_max )
  2984. stack_expand(stack);
  2985. stack->stack_data[stack->sp].type = type;
  2986. stack->stack_data[stack->sp].u.str = str;
  2987. stack->stack_data[stack->sp].ref = NULL;
  2988. stack->sp++;
  2989. return &stack->stack_data[stack->sp-1];
  2990. }
  2991. /// Pushes a retinfo into the stack
  2992. struct script_data* push_retinfo(struct script_stack* stack, struct script_retinfo* ri, struct reg_db *ref)
  2993. {
  2994. if( stack->sp >= stack->sp_max )
  2995. stack_expand(stack);
  2996. stack->stack_data[stack->sp].type = C_RETINFO;
  2997. stack->stack_data[stack->sp].u.ri = ri;
  2998. stack->stack_data[stack->sp].ref = ref;
  2999. stack->sp++;
  3000. return &stack->stack_data[stack->sp-1];
  3001. }
  3002. /// Pushes a copy of the target position into the stack
  3003. struct script_data* push_copy(struct script_stack* stack, int pos)
  3004. {
  3005. switch( stack->stack_data[pos].type ) {
  3006. case C_CONSTSTR:
  3007. return push_str(stack, C_CONSTSTR, stack->stack_data[pos].u.str);
  3008. break;
  3009. case C_STR:
  3010. return push_str(stack, C_STR, aStrdup(stack->stack_data[pos].u.str));
  3011. break;
  3012. case C_RETINFO:
  3013. ShowFatalError("script:push_copy: can't create copies of C_RETINFO. Exiting...\n");
  3014. exit(1);
  3015. break;
  3016. default:
  3017. return push_val2(
  3018. stack,stack->stack_data[pos].type,
  3019. stack->stack_data[pos].u.num,
  3020. stack->stack_data[pos].ref
  3021. );
  3022. break;
  3023. }
  3024. }
  3025. /// Removes the values in indexes [start,end] from the stack.
  3026. /// Adjusts all stack pointers.
  3027. void pop_stack(struct script_state* st, int start, int end)
  3028. {
  3029. struct script_stack* stack = st->stack;
  3030. struct script_data* data;
  3031. int i;
  3032. if( start < 0 )
  3033. start = 0;
  3034. if( end > stack->sp )
  3035. end = stack->sp;
  3036. if( start >= end )
  3037. return;// nothing to pop
  3038. // free stack elements
  3039. for( i = start; i < end; i++ )
  3040. {
  3041. data = &stack->stack_data[i];
  3042. if( data->type == C_STR )
  3043. aFree(data->u.str);
  3044. if( data->type == C_RETINFO ) {
  3045. struct script_retinfo* ri = data->u.ri;
  3046. if (ri->scope.vars) {
  3047. script_free_vars(ri->scope.vars);
  3048. ri->scope.vars = NULL;
  3049. }
  3050. if (ri->scope.arrays) {
  3051. ri->scope.arrays->destroy(ri->scope.arrays, script_free_array_db);
  3052. ri->scope.arrays = NULL;
  3053. }
  3054. if( data->ref )
  3055. aFree(data->ref);
  3056. aFree(ri);
  3057. }
  3058. data->type = C_NOP;
  3059. }
  3060. // move the rest of the elements
  3061. if( stack->sp > end )
  3062. {
  3063. memmove(&stack->stack_data[start], &stack->stack_data[end], sizeof(stack->stack_data[0])*(stack->sp - end));
  3064. for( i = start + stack->sp - end; i < stack->sp; ++i )
  3065. stack->stack_data[i].type = C_NOP;
  3066. }
  3067. // adjust stack pointers
  3068. if( st->start > end ) st->start -= end - start;
  3069. else if( st->start > start ) st->start = start;
  3070. if( st->end > end ) st->end -= end - start;
  3071. else if( st->end > start ) st->end = start;
  3072. if( stack->defsp > end ) stack->defsp -= end - start;
  3073. else if( stack->defsp > start ) stack->defsp = start;
  3074. stack->sp -= end - start;
  3075. }
  3076. ///
  3077. ///
  3078. ///
  3079. /*==========================================
  3080. * Release script dependent variable, dependent variable of function
  3081. *------------------------------------------*/
  3082. void script_free_vars(struct DBMap* storage)
  3083. {
  3084. if( storage ) {
  3085. // destroy the storage construct containing the variables
  3086. db_destroy(storage);
  3087. }
  3088. }
  3089. void script_free_code(struct script_code* code)
  3090. {
  3091. nullpo_retv(code);
  3092. if (code->instances)
  3093. script_stop_instances(code);
  3094. script_free_vars(code->local.vars);
  3095. if (code->local.arrays)
  3096. code->local.arrays->destroy(code->local.arrays, script_free_array_db);
  3097. aFree(code->script_buf);
  3098. aFree(code);
  3099. }
  3100. /// Creates a new script state.
  3101. ///
  3102. /// @param script Script code
  3103. /// @param pos Position in the code
  3104. /// @param rid Who is running the script (attached player)
  3105. /// @param oid Where the code is being run (npc 'object')
  3106. /// @return Script state
  3107. struct script_state* script_alloc_state(struct script_code* rootscript, int pos, int rid, int oid)
  3108. {
  3109. struct script_state* st;
  3110. st = ers_alloc(st_ers, struct script_state);
  3111. st->stack = ers_alloc(stack_ers, struct script_stack);
  3112. st->stack->sp = 0;
  3113. st->stack->sp_max = 64;
  3114. CREATE(st->stack->stack_data, struct script_data, st->stack->sp_max);
  3115. st->stack->defsp = st->stack->sp;
  3116. st->stack->scope.vars = i64db_alloc(DB_OPT_RELEASE_DATA);
  3117. st->stack->scope.arrays = NULL;
  3118. st->state = RUN;
  3119. st->script = rootscript;
  3120. st->pos = pos;
  3121. st->rid = rid;
  3122. st->oid = oid;
  3123. st->sleep.timer = INVALID_TIMER;
  3124. st->npc_item_flag = battle_config.item_enabled_npc;
  3125. if( st->script->instances != USHRT_MAX )
  3126. st->script->instances++;
  3127. else {
  3128. struct npc_data *nd = map_id2nd(oid);
  3129. ShowError("Over 65k instances of '%s' script are being run!\n",nd ? nd->name : "unknown");
  3130. }
  3131. if (!st->script->local.vars)
  3132. st->script->local.vars = i64db_alloc(DB_OPT_RELEASE_DATA);
  3133. st->id = next_id++;
  3134. active_scripts++;
  3135. idb_put(st_db, st->id, st);
  3136. return st;
  3137. }
  3138. /// Frees a script state.
  3139. ///
  3140. /// @param st Script state
  3141. void script_free_state(struct script_state* st)
  3142. {
  3143. if (idb_exists(st_db, st->id)) {
  3144. struct map_session_data *sd = st->rid ? map_id2sd(st->rid) : NULL;
  3145. if (st->bk_st) // backup was not restored
  3146. 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);
  3147. if (sd && sd->st == st) { // Current script is aborted.
  3148. if(sd->state.using_fake_npc) {
  3149. clif_clearunit_single(sd->npc_id, CLR_OUTSIGHT, sd->fd);
  3150. sd->state.using_fake_npc = 0;
  3151. }
  3152. sd->st = NULL;
  3153. sd->npc_id = 0;
  3154. }
  3155. if (st->sleep.timer != INVALID_TIMER)
  3156. delete_timer(st->sleep.timer, run_script_timer);
  3157. if (st->stack) {
  3158. script_free_vars(st->stack->scope.vars);
  3159. if (st->stack->scope.arrays)
  3160. st->stack->scope.arrays->destroy(st->stack->scope.arrays, script_free_array_db);
  3161. pop_stack(st, 0, st->stack->sp);
  3162. aFree(st->stack->stack_data);
  3163. ers_free(stack_ers, st->stack);
  3164. st->stack = NULL;
  3165. }
  3166. if (st->script && st->script->instances != USHRT_MAX && --st->script->instances == 0) {
  3167. if (st->script->local.vars && !db_size(st->script->local.vars)) {
  3168. script_free_vars(st->script->local.vars);
  3169. st->script->local.vars = NULL;
  3170. }
  3171. if (st->script->local.arrays && !db_size(st->script->local.arrays)) {
  3172. st->script->local.arrays->destroy(st->script->local.arrays, script_free_array_db);
  3173. st->script->local.arrays = NULL;
  3174. }
  3175. }
  3176. st->pos = -1;
  3177. idb_remove(st_db, st->id);
  3178. ers_free(st_ers, st);
  3179. if (--active_scripts == 0)
  3180. next_id = 0;
  3181. }
  3182. }
  3183. //
  3184. // Main execution unit
  3185. //
  3186. /*==========================================
  3187. * Read command
  3188. *------------------------------------------*/
  3189. c_op get_com(unsigned char *script,int *pos)
  3190. {
  3191. int i = 0, j = 0;
  3192. if(script[*pos]>=0x80){
  3193. return C_INT;
  3194. }
  3195. while(script[*pos]>=0x40){
  3196. i=script[(*pos)++]<<j;
  3197. j+=6;
  3198. }
  3199. return (c_op)(i+(script[(*pos)++]<<j));
  3200. }
  3201. /*==========================================
  3202. * Income figures
  3203. *------------------------------------------*/
  3204. int get_num(unsigned char *script,int *pos)
  3205. {
  3206. int i,j;
  3207. i=0; j=0;
  3208. while(script[*pos]>=0xc0){
  3209. i+=(script[(*pos)++]&0x7f)<<j;
  3210. j+=6;
  3211. }
  3212. return i+((script[(*pos)++]&0x7f)<<j);
  3213. }
  3214. /// Ternary operators
  3215. /// test ? if_true : if_false
  3216. void op_3(struct script_state* st, int op)
  3217. {
  3218. struct script_data* data;
  3219. int flag = 0;
  3220. data = script_getdatatop(st, -3);
  3221. get_val(st, data);
  3222. if( data_isstring(data) )
  3223. flag = data->u.str[0];// "" -> false
  3224. else if( data_isint(data) )
  3225. flag = data->u.num == 0 ? 0 : 1;// 0 -> false
  3226. else
  3227. {
  3228. ShowError("script:op_3: invalid data for the ternary operator test\n");
  3229. script_reportdata(data);
  3230. script_reportsrc(st);
  3231. script_removetop(st, -3, 0);
  3232. script_pushnil(st);
  3233. return;
  3234. }
  3235. if( flag )
  3236. script_pushcopytop(st, -2);
  3237. else
  3238. script_pushcopytop(st, -1);
  3239. script_removetop(st, -4, -1);
  3240. }
  3241. /// Binary string operators
  3242. /// s1 EQ s2 -> i
  3243. /// s1 NE s2 -> i
  3244. /// s1 GT s2 -> i
  3245. /// s1 GE s2 -> i
  3246. /// s1 LT s2 -> i
  3247. /// s1 LE s2 -> i
  3248. /// s1 ADD s2 -> s
  3249. void op_2str(struct script_state* st, int op, const char* s1, const char* s2)
  3250. {
  3251. int a = 0;
  3252. switch(op){
  3253. case C_EQ: a = (strcmp(s1,s2) == 0); break;
  3254. case C_NE: a = (strcmp(s1,s2) != 0); break;
  3255. case C_GT: a = (strcmp(s1,s2) > 0); break;
  3256. case C_GE: a = (strcmp(s1,s2) >= 0); break;
  3257. case C_LT: a = (strcmp(s1,s2) < 0); break;
  3258. case C_LE: a = (strcmp(s1,s2) <= 0); break;
  3259. case C_ADD:
  3260. {
  3261. char* buf = (char *)aMalloc((strlen(s1)+strlen(s2)+1)*sizeof(char));
  3262. strcpy(buf, s1);
  3263. strcat(buf, s2);
  3264. script_pushstr(st, buf);
  3265. return;
  3266. }
  3267. default:
  3268. ShowError("script:op2_str: unexpected string operator %s\n", script_op2name(op));
  3269. script_reportsrc(st);
  3270. script_pushnil(st);
  3271. st->state = END;
  3272. return;
  3273. }
  3274. script_pushint(st,a);
  3275. }
  3276. /// Binary number operators
  3277. /// i OP i -> i
  3278. void op_2num(struct script_state* st, int op, int i1, int i2)
  3279. {
  3280. int ret;
  3281. double ret_double;
  3282. switch( op ) {
  3283. case C_AND: ret = i1 & i2; break;
  3284. case C_OR: ret = i1 | i2; break;
  3285. case C_XOR: ret = i1 ^ i2; break;
  3286. case C_LAND: ret = (i1 && i2); break;
  3287. case C_LOR: ret = (i1 || i2); break;
  3288. case C_EQ: ret = (i1 == i2); break;
  3289. case C_NE: ret = (i1 != i2); break;
  3290. case C_GT: ret = (i1 > i2); break;
  3291. case C_GE: ret = (i1 >= i2); break;
  3292. case C_LT: ret = (i1 < i2); break;
  3293. case C_LE: ret = (i1 <= i2); break;
  3294. case C_R_SHIFT: ret = i1>>i2; break;
  3295. case C_L_SHIFT: ret = i1<<i2; break;
  3296. case C_DIV:
  3297. case C_MOD:
  3298. if( i2 == 0 )
  3299. {
  3300. ShowError("script:op_2num: division by zero detected op=%s i1=%d i2=%d\n", script_op2name(op), i1, i2);
  3301. script_reportsrc(st);
  3302. script_pushnil(st);
  3303. st->state = END;
  3304. return;
  3305. }
  3306. else if( op == C_DIV )
  3307. ret = i1 / i2;
  3308. else//if( op == C_MOD )
  3309. ret = i1 % i2;
  3310. break;
  3311. default:
  3312. switch( op ) {// operators that can overflow/underflow
  3313. case C_ADD: ret = i1 + i2; ret_double = (double)i1 + (double)i2; break;
  3314. case C_SUB: ret = i1 - i2; ret_double = (double)i1 - (double)i2; break;
  3315. case C_MUL: ret = i1 * i2; ret_double = (double)i1 * (double)i2; break;
  3316. default:
  3317. ShowError("script:op_2num: unexpected number operator %s i1=%d i2=%d\n", script_op2name(op), i1, i2);
  3318. script_reportsrc(st);
  3319. script_pushnil(st);
  3320. return;
  3321. }
  3322. if( ret_double < (double)INT_MIN ) {
  3323. ShowWarning("script:op_2num: underflow detected op=%s i1=%d i2=%d\n", script_op2name(op), i1, i2);
  3324. script_reportsrc(st);
  3325. ret = INT_MIN;
  3326. }
  3327. else if( ret_double > (double)INT_MAX ) {
  3328. ShowWarning("script:op_2num: overflow detected op=%s i1=%d i2=%d\n", script_op2name(op), i1, i2);
  3329. script_reportsrc(st);
  3330. ret = INT_MAX;
  3331. }
  3332. }
  3333. script_pushint(st, ret);
  3334. }
  3335. /// Binary operators
  3336. void op_2(struct script_state *st, int op)
  3337. {
  3338. struct script_data* left, leftref;
  3339. struct script_data* right;
  3340. leftref.type = C_NOP;
  3341. left = script_getdatatop(st, -2);
  3342. right = script_getdatatop(st, -1);
  3343. if (st->op2ref) {
  3344. if (data_isreference(left)) {
  3345. leftref = *left;
  3346. }
  3347. st->op2ref = 0;
  3348. }
  3349. get_val(st, left);
  3350. get_val(st, right);
  3351. // automatic conversions
  3352. switch( op )
  3353. {
  3354. case C_ADD:
  3355. if( data_isint(left) && data_isstring(right) )
  3356. {// convert int-string to string-string
  3357. conv_str(st, left);
  3358. }
  3359. else if( data_isstring(left) && data_isint(right) )
  3360. {// convert string-int to string-string
  3361. conv_str(st, right);
  3362. }
  3363. break;
  3364. }
  3365. if( data_isstring(left) && data_isstring(right) )
  3366. {// ss => op_2str
  3367. op_2str(st, op, left->u.str, right->u.str);
  3368. script_removetop(st, leftref.type == C_NOP ? -3 : -2, -1);// pop the two values before the top one
  3369. if (leftref.type != C_NOP)
  3370. {
  3371. if (left->type == C_STR) // don't free C_CONSTSTR
  3372. aFree(left->u.str);
  3373. *left = leftref;
  3374. }
  3375. }
  3376. else if( data_isint(left) && data_isint(right) )
  3377. {// ii => op_2num
  3378. int i1 = (int)left->u.num;
  3379. int i2 = (int)right->u.num;
  3380. script_removetop(st, leftref.type == C_NOP ? -2 : -1, 0);
  3381. op_2num(st, op, i1, i2);
  3382. if (leftref.type != C_NOP)
  3383. *left = leftref;
  3384. }
  3385. else
  3386. {// invalid argument
  3387. ShowError("script:op_2: invalid data for operator %s\n", script_op2name(op));
  3388. script_reportdata(left);
  3389. script_reportdata(right);
  3390. script_reportsrc(st);
  3391. script_removetop(st, -2, 0);
  3392. script_pushnil(st);
  3393. st->state = END;
  3394. }
  3395. }
  3396. /// Unary operators
  3397. /// NEG i -> i
  3398. /// NOT i -> i
  3399. /// LNOT i -> i
  3400. void op_1(struct script_state* st, int op)
  3401. {
  3402. struct script_data* data;
  3403. int i1;
  3404. data = script_getdatatop(st, -1);
  3405. get_val(st, data);
  3406. if( !data_isint(data) )
  3407. {// not a number
  3408. ShowError("script:op_1: argument is not a number (op=%s)\n", script_op2name(op));
  3409. script_reportdata(data);
  3410. script_reportsrc(st);
  3411. script_pushnil(st);
  3412. st->state = END;
  3413. return;
  3414. }
  3415. i1 = (int)data->u.num;
  3416. script_removetop(st, -1, 0);
  3417. switch( op )
  3418. {
  3419. case C_NEG: i1 = -i1; break;
  3420. case C_NOT: i1 = ~i1; break;
  3421. case C_LNOT: i1 = !i1; break;
  3422. default:
  3423. ShowError("script:op_1: unexpected operator %s i1=%d\n", script_op2name(op), i1);
  3424. script_reportsrc(st);
  3425. script_pushnil(st);
  3426. st->state = END;
  3427. return;
  3428. }
  3429. script_pushint(st, i1);
  3430. }
  3431. /// Checks the type of all arguments passed to a built-in function.
  3432. ///
  3433. /// @param st Script state whose stack arguments should be inspected.
  3434. /// @param func Built-in function for which the arguments are intended.
  3435. static void script_check_buildin_argtype(struct script_state* st, int func)
  3436. {
  3437. int idx, invalid = 0;
  3438. for( idx = 2; script_hasdata(st, idx); idx++ )
  3439. {
  3440. struct script_data* data = script_getdata(st, idx);
  3441. script_function* sf = &buildin_func[str_data[func].val];
  3442. char type = sf->arg[idx-2];
  3443. if( type == '?' || type == '*' )
  3444. {// optional argument or unknown number of optional parameters ( no types are after this )
  3445. break;
  3446. }
  3447. else if( type == 0 )
  3448. {// more arguments than necessary ( should not happen, as it is checked before )
  3449. ShowWarning("Found more arguments than necessary. unexpected arg type %s\n",script_op2name(data->type));
  3450. invalid++;
  3451. break;
  3452. }
  3453. else
  3454. {
  3455. const char* name = NULL;
  3456. if( data_isreference(data) )
  3457. {// get name for variables to determine the type they refer to
  3458. name = reference_getname(data);
  3459. }
  3460. switch( type )
  3461. {
  3462. case 'v':
  3463. if( !data_isstring(data) && !data_isint(data) && !data_isreference(data) )
  3464. {// variant
  3465. ShowWarning("Unexpected type for argument %d. Expected string, number or variable.\n", idx-1);
  3466. script_reportdata(data);
  3467. invalid++;
  3468. }
  3469. break;
  3470. case 's':
  3471. if( !data_isstring(data) && !( data_isreference(data) && is_string_variable(name) ) )
  3472. {// string
  3473. ShowWarning("Unexpected type for argument %d. Expected string.\n", idx-1);
  3474. script_reportdata(data);
  3475. invalid++;
  3476. }
  3477. break;
  3478. case 'i':
  3479. if( !data_isint(data) && !( data_isreference(data) && ( reference_toparam(data) || reference_toconstant(data) || !is_string_variable(name) ) ) )
  3480. {// int ( params and constants are always int )
  3481. ShowWarning("Unexpected type for argument %d. Expected number.\n", idx-1);
  3482. script_reportdata(data);
  3483. invalid++;
  3484. }
  3485. break;
  3486. case 'r':
  3487. if( !data_isreference(data) )
  3488. {// variables
  3489. ShowWarning("Unexpected type for argument %d. Expected variable, got %s.\n", idx-1,script_op2name(data->type));
  3490. script_reportdata(data);
  3491. invalid++;
  3492. }
  3493. break;
  3494. case 'l':
  3495. if( !data_islabel(data) && !data_isfunclabel(data) )
  3496. {// label
  3497. ShowWarning("Unexpected type for argument %d. Expected label, got %s\n", idx-1,script_op2name(data->type));
  3498. script_reportdata(data);
  3499. invalid++;
  3500. }
  3501. break;
  3502. }
  3503. }
  3504. }
  3505. if(invalid)
  3506. {
  3507. ShowDebug("Function: %s\n", get_str(func));
  3508. script_reportsrc(st);
  3509. }
  3510. }
  3511. /// Executes a buildin command.
  3512. /// Stack: C_NAME(<command>) C_ARG <arg0> <arg1> ... <argN>
  3513. int run_func(struct script_state *st)
  3514. {
  3515. struct script_data* data;
  3516. int i,start_sp,end_sp,func;
  3517. end_sp = st->stack->sp;// position after the last argument
  3518. for( i = end_sp-1; i > 0 ; --i )
  3519. if( st->stack->stack_data[i].type == C_ARG )
  3520. break;
  3521. if( i == 0 ) {
  3522. ShowError("script:run_func: C_ARG not found. please report this!!!\n");
  3523. st->state = END;
  3524. script_reportsrc(st);
  3525. return 1;
  3526. }
  3527. start_sp = i-1;// C_NAME of the command
  3528. st->start = start_sp;
  3529. st->end = end_sp;
  3530. data = &st->stack->stack_data[st->start];
  3531. if( data->type == C_NAME && str_data[data->u.num].type == C_FUNC ) {
  3532. func = (int)data->u.num;
  3533. st->funcname = reference_getname(data);
  3534. } else {
  3535. ShowError("script:run_func: not a buildin command.\n");
  3536. script_reportdata(data);
  3537. script_reportsrc(st);
  3538. st->state = END;
  3539. return 1;
  3540. }
  3541. if( script_config.warn_func_mismatch_argtypes ) {
  3542. script_check_buildin_argtype(st, func);
  3543. }
  3544. if(str_data[func].func) {
  3545. if (str_data[func].func(st) == SCRIPT_CMD_FAILURE) //Report error
  3546. script_reportsrc(st);
  3547. } else {
  3548. 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));
  3549. script_reportsrc(st);
  3550. st->state = END;
  3551. }
  3552. // Stack's datum are used when re-running functions [Eoe]
  3553. if( st->state == RERUNLINE )
  3554. return 0;
  3555. pop_stack(st, st->start, st->end);
  3556. if( st->state == RETFUNC ) {// return from a user-defined function
  3557. struct script_retinfo* ri;
  3558. int olddefsp = st->stack->defsp;
  3559. int nargs;
  3560. pop_stack(st, st->stack->defsp, st->start);// pop distractions from the stack
  3561. if( st->stack->defsp < 1 || st->stack->stack_data[st->stack->defsp-1].type != C_RETINFO )
  3562. {
  3563. ShowWarning("script:run_func: return without callfunc or callsub!\n");
  3564. script_reportsrc(st);
  3565. st->state = END;
  3566. return 1;
  3567. }
  3568. script_free_vars(st->stack->scope.vars);
  3569. st->stack->scope.arrays->destroy(st->stack->scope.arrays, script_free_array_db);
  3570. ri = st->stack->stack_data[st->stack->defsp-1].u.ri;
  3571. nargs = ri->nargs;
  3572. st->pos = ri->pos;
  3573. st->script = ri->script;
  3574. st->stack->scope.vars = ri->scope.vars;
  3575. st->stack->scope.arrays = ri->scope.arrays;
  3576. st->stack->defsp = ri->defsp;
  3577. memset(ri, 0, sizeof(struct script_retinfo));
  3578. pop_stack(st, olddefsp-nargs-1, olddefsp);// pop arguments and retinfo
  3579. st->state = GOTO;
  3580. }
  3581. return 0;
  3582. }
  3583. /*==========================================
  3584. * script execution
  3585. *------------------------------------------*/
  3586. void run_script(struct script_code *rootscript, int pos, int rid, int oid)
  3587. {
  3588. struct script_state *st;
  3589. if( rootscript == NULL || pos < 0 )
  3590. return;
  3591. // TODO In jAthena, this function can take over the pending script in the player. [FlavioJS]
  3592. // It is unclear how that can be triggered, so it needs the be traced/checked in more detail.
  3593. // NOTE At the time of this change, this function wasn't capable of taking over the script state because st->scriptroot was never set.
  3594. st = script_alloc_state(rootscript, pos, rid, oid);
  3595. run_script_main(st);
  3596. }
  3597. void script_stop_instances(struct script_code *code) {
  3598. DBIterator *iter;
  3599. struct script_state* st;
  3600. if( !active_scripts )
  3601. return; // Don't even bother.
  3602. iter = db_iterator(st_db);
  3603. for( st = dbi_first(iter); dbi_exists(iter); st = dbi_next(iter) ) {
  3604. if( st->script == code )
  3605. script_free_state(st);
  3606. }
  3607. dbi_destroy(iter);
  3608. }
  3609. /*==========================================
  3610. * Timer function for sleep
  3611. *------------------------------------------*/
  3612. int run_script_timer(int tid, unsigned int tick, int id, intptr_t data)
  3613. {
  3614. struct script_state *st = (struct script_state *)data;
  3615. // If it was a player before going to sleep and there is still a unit attached to the script
  3616. if( id != 0 && st->rid ){
  3617. struct map_session_data *sd = map_id2sd(st->rid);
  3618. // Attached player is offline or another unit type - should not happen
  3619. if( !sd ){
  3620. ShowWarning( "Script sleep timer called by an offline character or non player unit.\n" );
  3621. script_reportsrc(st);
  3622. st->rid = 0;
  3623. st->state = END;
  3624. // Character mismatch. Cancel execution.
  3625. }else if( sd->status.char_id != id ){
  3626. ShowWarning( "Script sleep timer detected a character mismatch CID %d != %d\n", sd->status.char_id, id );
  3627. script_reportsrc(st);
  3628. st->rid = 0;
  3629. st->state = END;
  3630. }
  3631. }
  3632. st->sleep.timer = INVALID_TIMER;
  3633. if(st->state != RERUNLINE)
  3634. st->sleep.tick = 0;
  3635. run_script_main(st);
  3636. return 0;
  3637. }
  3638. /// Detaches script state from possibly attached character and restores it's previous script if any.
  3639. ///
  3640. /// @param st Script state to detach.
  3641. /// @param dequeue_event Whether to schedule any queued events, when there was no previous script.
  3642. static void script_detach_state(struct script_state* st, bool dequeue_event)
  3643. {
  3644. struct map_session_data* sd;
  3645. if(st->rid && (sd = map_id2sd(st->rid))!=NULL) {
  3646. sd->st = st->bk_st;
  3647. sd->npc_id = st->bk_npcid;
  3648. sd->state.disable_atcommand_on_npc = 0;
  3649. if(st->bk_st) {
  3650. //Remove tag for removal.
  3651. st->bk_st = NULL;
  3652. st->bk_npcid = 0;
  3653. } else if(dequeue_event) {
  3654. #ifdef SECURE_NPCTIMEOUT
  3655. /**
  3656. * We're done with this NPC session, so we cancel the timer (if existent) and move on
  3657. **/
  3658. if( sd->npc_idle_timer != INVALID_TIMER ) {
  3659. delete_timer(sd->npc_idle_timer,npc_rr_secure_timeout_timer);
  3660. sd->npc_idle_timer = INVALID_TIMER;
  3661. }
  3662. #endif
  3663. npc_event_dequeue(sd);
  3664. }
  3665. }
  3666. else if(st->bk_st)
  3667. {// rid was set to 0, before detaching the script state
  3668. 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);
  3669. script_reportsrc(st->bk_st);
  3670. script_free_state(st->bk_st);
  3671. st->bk_st = NULL;
  3672. }
  3673. }
  3674. /// Attaches script state to possibly attached character and backups it's previous script, if any.
  3675. ///
  3676. /// @param st Script state to attach.
  3677. static void script_attach_state(struct script_state* st)
  3678. {
  3679. struct map_session_data* sd;
  3680. if(st->rid && (sd = map_id2sd(st->rid))!=NULL)
  3681. {
  3682. if(st!=sd->st)
  3683. {
  3684. if(st->bk_st)
  3685. {// there is already a backup
  3686. 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);
  3687. }
  3688. st->bk_st = sd->st;
  3689. st->bk_npcid = sd->npc_id;
  3690. }
  3691. sd->st = st;
  3692. sd->npc_id = st->oid;
  3693. sd->npc_item_flag = st->npc_item_flag; // load default.
  3694. sd->state.disable_atcommand_on_npc = (!pc_has_permission(sd, PC_PERM_ENABLE_COMMAND));
  3695. #ifdef SECURE_NPCTIMEOUT
  3696. if( sd->npc_idle_timer == INVALID_TIMER )
  3697. sd->npc_idle_timer = add_timer(gettick() + (SECURE_NPCTIMEOUT_INTERVAL*1000),npc_rr_secure_timeout_timer,sd->bl.id,0);
  3698. sd->npc_idle_tick = gettick();
  3699. #endif
  3700. }
  3701. }
  3702. /*==========================================
  3703. * The main part of the script execution
  3704. *------------------------------------------*/
  3705. void run_script_main(struct script_state *st)
  3706. {
  3707. int cmdcount = script_config.check_cmdcount;
  3708. int gotocount = script_config.check_gotocount;
  3709. TBL_PC *sd;
  3710. struct script_stack *stack = st->stack;
  3711. script_attach_state(st);
  3712. if(st->state == RERUNLINE) {
  3713. run_func(st);
  3714. if(st->state == GOTO)
  3715. st->state = RUN;
  3716. } else if(st->state != END)
  3717. st->state = RUN;
  3718. while(st->state == RUN) {
  3719. enum c_op c = get_com(st->script->script_buf,&st->pos);
  3720. switch(c){
  3721. case C_EOL:
  3722. if( stack->defsp > stack->sp )
  3723. ShowError("script:run_script_main: unexpected stack position (defsp=%d sp=%d). please report this!!!\n", stack->defsp, stack->sp);
  3724. else
  3725. pop_stack(st, stack->defsp, stack->sp);// pop unused stack data. (unused return value)
  3726. break;
  3727. case C_INT:
  3728. push_val(stack,C_INT,get_num(st->script->script_buf,&st->pos));
  3729. break;
  3730. case C_POS:
  3731. case C_NAME:
  3732. push_val(stack,c,GETVALUE(st->script->script_buf,st->pos));
  3733. st->pos+=3;
  3734. break;
  3735. case C_ARG:
  3736. push_val(stack,c,0);
  3737. break;
  3738. case C_STR:
  3739. push_str(stack,C_CONSTSTR,(char*)(st->script->script_buf+st->pos));
  3740. while(st->script->script_buf[st->pos++]);
  3741. break;
  3742. case C_FUNC:
  3743. run_func(st);
  3744. if(st->state==GOTO){
  3745. st->state = RUN;
  3746. if( !st->freeloop && gotocount>0 && (--gotocount)<=0 ){
  3747. ShowError("script:run_script_main: infinity loop !\n");
  3748. script_reportsrc(st);
  3749. st->state=END;
  3750. }
  3751. }
  3752. break;
  3753. case C_REF:
  3754. st->op2ref = 1;
  3755. break;
  3756. case C_NEG:
  3757. case C_NOT:
  3758. case C_LNOT:
  3759. op_1(st ,c);
  3760. break;
  3761. case C_ADD:
  3762. case C_SUB:
  3763. case C_MUL:
  3764. case C_DIV:
  3765. case C_MOD:
  3766. case C_EQ:
  3767. case C_NE:
  3768. case C_GT:
  3769. case C_GE:
  3770. case C_LT:
  3771. case C_LE:
  3772. case C_AND:
  3773. case C_OR:
  3774. case C_XOR:
  3775. case C_LAND:
  3776. case C_LOR:
  3777. case C_R_SHIFT:
  3778. case C_L_SHIFT:
  3779. op_2(st, c);
  3780. break;
  3781. case C_OP3:
  3782. op_3(st, c);
  3783. break;
  3784. case C_NOP:
  3785. st->state=END;
  3786. break;
  3787. default:
  3788. ShowError("script:run_script_main:unknown command : %d @ %d\n",c,st->pos);
  3789. st->state=END;
  3790. break;
  3791. }
  3792. if( !st->freeloop && cmdcount>0 && (--cmdcount)<=0 ){
  3793. ShowError("script:run_script_main: infinity loop !\n");
  3794. script_reportsrc(st);
  3795. st->state=END;
  3796. }
  3797. }
  3798. if(st->sleep.tick > 0) {
  3799. //Restore previous script
  3800. script_detach_state(st, false);
  3801. //Delay execution
  3802. sd = map_id2sd(st->rid); // Get sd since script might have attached someone while running. [Inkfish]
  3803. st->sleep.charid = sd?sd->status.char_id:0;
  3804. st->sleep.timer = add_timer(gettick()+st->sleep.tick,
  3805. run_script_timer, st->sleep.charid, (intptr_t)st);
  3806. } else if(st->state != END && st->rid) {
  3807. //Resume later (st is already attached to player).
  3808. if(st->bk_st) {
  3809. ShowWarning("Unable to restore stack! Double continuation!\n");
  3810. //Report BOTH scripts to see if that can help somehow.
  3811. ShowDebug("Previous script (lost):\n");
  3812. script_reportsrc(st->bk_st);
  3813. ShowDebug("Current script:\n");
  3814. script_reportsrc(st);
  3815. script_free_state(st->bk_st);
  3816. st->bk_st = NULL;
  3817. }
  3818. } else {
  3819. //Dispose of script.
  3820. if ((sd = map_id2sd(st->rid))!=NULL)
  3821. { //Restore previous stack and save char.
  3822. if(sd->state.using_fake_npc){
  3823. clif_clearunit_single(sd->npc_id, CLR_OUTSIGHT, sd->fd);
  3824. sd->state.using_fake_npc = 0;
  3825. }
  3826. //Restore previous script if any.
  3827. script_detach_state(st, true);
  3828. if (sd->vars_dirty)
  3829. intif_saveregistry(sd);
  3830. }
  3831. script_free_state(st);
  3832. }
  3833. }
  3834. int script_config_read(char *cfgName)
  3835. {
  3836. int i;
  3837. char line[1024],w1[1024],w2[1024];
  3838. FILE *fp;
  3839. fp=fopen(cfgName,"r");
  3840. if(fp==NULL){
  3841. ShowError("File not found: %s\n", cfgName);
  3842. return 1;
  3843. }
  3844. while(fgets(line, sizeof(line), fp))
  3845. {
  3846. if(line[0] == '/' && line[1] == '/')
  3847. continue;
  3848. i=sscanf(line,"%1023[^:]: %1023[^\r\n]",w1,w2);
  3849. if(i!=2)
  3850. continue;
  3851. if(strcmpi(w1,"warn_func_mismatch_paramnum")==0) {
  3852. script_config.warn_func_mismatch_paramnum = config_switch(w2);
  3853. }
  3854. else if(strcmpi(w1,"check_cmdcount")==0) {
  3855. script_config.check_cmdcount = config_switch(w2);
  3856. }
  3857. else if(strcmpi(w1,"check_gotocount")==0) {
  3858. script_config.check_gotocount = config_switch(w2);
  3859. }
  3860. else if(strcmpi(w1,"input_min_value")==0) {
  3861. script_config.input_min_value = config_switch(w2);
  3862. }
  3863. else if(strcmpi(w1,"input_max_value")==0) {
  3864. script_config.input_max_value = config_switch(w2);
  3865. }
  3866. else if(strcmpi(w1,"warn_func_mismatch_argtypes")==0) {
  3867. script_config.warn_func_mismatch_argtypes = config_switch(w2);
  3868. }
  3869. else if(strcmpi(w1,"import")==0){
  3870. script_config_read(w2);
  3871. }
  3872. else {
  3873. ShowWarning("Unknown setting '%s' in file %s\n", w1, cfgName);
  3874. }
  3875. }
  3876. fclose(fp);
  3877. return 0;
  3878. }
  3879. /**
  3880. * @see DBApply
  3881. */
  3882. static int db_script_free_code_sub(DBKey key, DBData *data, va_list ap)
  3883. {
  3884. struct script_code *code = db_data2ptr(data);
  3885. if (code)
  3886. script_free_code(code);
  3887. return 0;
  3888. }
  3889. void script_run_autobonus(const char *autobonus, struct map_session_data *sd, unsigned int pos)
  3890. {
  3891. struct script_code *script = (struct script_code *)strdb_get(autobonus_db, autobonus);
  3892. if( script )
  3893. {
  3894. int j;
  3895. ARR_FIND( 0, EQI_MAX, j, sd->equip_index[j] >= 0 && sd->inventory.u.items_inventory[sd->equip_index[j]].equip == pos );
  3896. if( j < EQI_MAX ) {
  3897. //Single item autobonus
  3898. current_equip_item_index = sd->equip_index[j];
  3899. current_equip_combo_pos = 0;
  3900. } else {
  3901. //Combo autobonus
  3902. current_equip_item_index = -1;
  3903. current_equip_combo_pos = pos;
  3904. }
  3905. run_script(script,0,sd->bl.id,0);
  3906. }
  3907. }
  3908. void script_add_autobonus(const char *autobonus)
  3909. {
  3910. if( strdb_get(autobonus_db, autobonus) == NULL )
  3911. {
  3912. struct script_code *script = parse_script(autobonus, "autobonus", 0, 0);
  3913. if( script )
  3914. strdb_put(autobonus_db, autobonus, script);
  3915. }
  3916. }
  3917. /// resets a temporary character array variable to given value
  3918. void script_cleararray_pc(struct map_session_data* sd, const char* varname, void* value)
  3919. {
  3920. struct script_array *sa = NULL;
  3921. struct reg_db *src = NULL;
  3922. unsigned int i, *list = NULL, size = 0;
  3923. int key;
  3924. key = add_str(varname);
  3925. if( !(src = script_array_src(NULL,sd,varname,NULL) ) )
  3926. return;
  3927. if( value )
  3928. script_array_ensure_zero(NULL,sd,reference_uid(key,0), NULL);
  3929. if( !(sa = idb_get(src->arrays, key)) ) /* non-existent array, nothing to empty */
  3930. return;
  3931. size = sa->size;
  3932. list = script_array_cpy_list(sa);
  3933. for(i = 0; i < size; i++) {
  3934. set_reg(NULL,sd,reference_uid(key, list[i]),varname,value,NULL);
  3935. }
  3936. }
  3937. /// sets a temporary character array variable element idx to given value
  3938. /// @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.
  3939. void script_setarray_pc(struct map_session_data* sd, const char* varname, uint32 idx, void* value, int* refcache)
  3940. {
  3941. int key;
  3942. if( idx >= SCRIPT_MAX_ARRAYSIZE ) {
  3943. ShowError("script_setarray_pc: Variable '%s' has invalid index '%u' (char_id=%d).\n", varname, idx, sd->status.char_id);
  3944. return;
  3945. }
  3946. key = ( refcache && refcache[0] ) ? refcache[0] : add_str(varname);
  3947. set_reg(NULL,sd,reference_uid(key, idx),varname,value,NULL);
  3948. if( refcache ) {
  3949. // save to avoid repeated script->add_str calls
  3950. refcache[0] = key;
  3951. }
  3952. }
  3953. /**
  3954. * Clears persistent variables from memory
  3955. **/
  3956. int script_reg_destroy(DBKey key, DBData *data, va_list ap)
  3957. {
  3958. struct script_reg_state *src;
  3959. if( data->type != DB_DATA_PTR ) // got no need for those!
  3960. return 0;
  3961. src = db_data2ptr(data);
  3962. if( src->type ) {
  3963. struct script_reg_str *p = (struct script_reg_str *)src;
  3964. if( p->value )
  3965. aFree(p->value);
  3966. ers_free(str_reg_ers,p);
  3967. } else {
  3968. ers_free(num_reg_ers,(struct script_reg_num*)src);
  3969. }
  3970. return 0;
  3971. }
  3972. /**
  3973. * Clears a single persistent variable
  3974. **/
  3975. void script_reg_destroy_single(struct map_session_data *sd, int64 reg, struct script_reg_state *data)
  3976. {
  3977. i64db_remove(sd->regs.vars, reg);
  3978. if( data->type ) {
  3979. struct script_reg_str *p = (struct script_reg_str*)data;
  3980. if( p->value )
  3981. aFree(p->value);
  3982. ers_free(str_reg_ers,p);
  3983. } else {
  3984. ers_free(num_reg_ers,(struct script_reg_num*)data);
  3985. }
  3986. }
  3987. unsigned int *script_array_cpy_list(struct script_array *sa)
  3988. {
  3989. if( sa->size > generic_ui_array_size )
  3990. script_generic_ui_array_expand(sa->size);
  3991. memcpy(generic_ui_array, sa->members, sizeof(unsigned int)*sa->size);
  3992. return generic_ui_array;
  3993. }
  3994. void script_generic_ui_array_expand (unsigned int plus)
  3995. {
  3996. generic_ui_array_size += plus + 100;
  3997. RECREATE(generic_ui_array, unsigned int, generic_ui_array_size);
  3998. }
  3999. int buildin_query_sql_sub(struct script_state *st, Sql *handle);
  4000. #ifdef BETA_THREAD_TEST
  4001. /* used to receive items the queryThread has already processed */
  4002. int queryThread_timer(int tid, unsigned int tick, int id, intptr_t data) {
  4003. int i, cursor = 0;
  4004. bool allOk = true;
  4005. EnterSpinLock(&queryThreadLock);
  4006. for( i = 0; i < queryThreadData.count; i++ ) {
  4007. struct queryThreadEntry *entry = queryThreadData.entry[i];
  4008. if( !entry->ok ) {
  4009. allOk = false;
  4010. continue;
  4011. }
  4012. run_script_main(entry->st);
  4013. entry->st = NULL;/* empty entries */
  4014. aFree(entry);
  4015. queryThreadData.entry[i] = NULL;
  4016. }
  4017. if( allOk ) {
  4018. /* cancel the repeating timer -- it'll re-create itself when necessary, dont need to remain looping */
  4019. delete_timer(queryThreadData.timer, queryThread_timer);
  4020. queryThreadData.timer = INVALID_TIMER;
  4021. }
  4022. /* now lets clear the mess. */
  4023. for( i = 0; i < queryThreadData.count; i++ ) {
  4024. struct queryThreadEntry *entry = queryThreadData.entry[i];
  4025. if( entry == NULL )
  4026. continue;/* entry on hold */
  4027. /* move */
  4028. memmove(&queryThreadData.entry[cursor], &queryThreadData.entry[i], sizeof(struct queryThreadEntry*));
  4029. cursor++;
  4030. }
  4031. queryThreadData.count = cursor;
  4032. LeaveSpinLock(&queryThreadLock);
  4033. return 0;
  4034. }
  4035. void queryThread_add(struct script_state *st, bool type) {
  4036. int idx = 0;
  4037. struct queryThreadEntry* entry = NULL;
  4038. EnterSpinLock(&queryThreadLock);
  4039. if( queryThreadData.count++ != 0 )
  4040. RECREATE(queryThreadData.entry, struct queryThreadEntry* , queryThreadData.count);
  4041. idx = queryThreadData.count-1;
  4042. CREATE(queryThreadData.entry[idx],struct queryThreadEntry,1);
  4043. entry = queryThreadData.entry[idx];
  4044. entry->st = st;
  4045. entry->ok = false;
  4046. entry->type = type;
  4047. if( queryThreadData.timer == INVALID_TIMER ) { /* start the receiver timer */
  4048. queryThreadData.timer = add_timer_interval(gettick() + 100, queryThread_timer, 0, 0, 100);
  4049. }
  4050. LeaveSpinLock(&queryThreadLock);
  4051. /* unlock the queryThread */
  4052. racond_signal(queryThreadCond);
  4053. }
  4054. /* adds a new log to the queue */
  4055. void queryThread_log(char * entry, int length) {
  4056. int idx = logThreadData.count;
  4057. EnterSpinLock(&queryThreadLock);
  4058. if( logThreadData.count++ != 0 )
  4059. RECREATE(logThreadData.entry, char* , logThreadData.count);
  4060. CREATE(logThreadData.entry[idx], char, length + 1 );
  4061. safestrncpy(logThreadData.entry[idx], entry, length + 1 );
  4062. LeaveSpinLock(&queryThreadLock);
  4063. /* unlock the queryThread */
  4064. racond_signal(queryThreadCond);
  4065. }
  4066. /* queryThread_main */
  4067. static void *queryThread_main(void *x) {
  4068. Sql *queryThread_handle = Sql_Malloc();
  4069. int i;
  4070. if ( SQL_ERROR == Sql_Connect(queryThread_handle, map_server_id, map_server_pw, map_server_ip, map_server_port, map_server_db) ){
  4071. ShowError("Couldn't connect with uname='%s',passwd='%s',host='%s',port='%d',database='%s'\n",
  4072. map_server_id, map_server_pw, map_server_ip, map_server_port, map_server_db);
  4073. Sql_ShowDebug(queryThread_handle);
  4074. Sql_Free(queryThread_handle);
  4075. exit(EXIT_FAILURE);
  4076. }
  4077. if( strlen(default_codepage) > 0 )
  4078. if ( SQL_ERROR == Sql_SetEncoding(queryThread_handle, default_codepage) )
  4079. Sql_ShowDebug(queryThread_handle);
  4080. if( log_config.sql_logs ) {
  4081. logmysql_handle = Sql_Malloc();
  4082. if ( SQL_ERROR == Sql_Connect(logmysql_handle, log_db_id, log_db_pw, log_db_ip, log_db_port, log_db_db) ){
  4083. ShowError("Couldn't connect with uname='%s',passwd='%s',host='%s',port='%d',database='%s'\n",
  4084. log_db_id, log_db_pw, log_db_ip, log_db_port, log_db_db);
  4085. Sql_ShowDebug(logmysql_handle);
  4086. Sql_Free(logmysql_handle);
  4087. exit(EXIT_FAILURE);
  4088. }
  4089. if( strlen(default_codepage) > 0 )
  4090. if ( SQL_ERROR == Sql_SetEncoding(logmysql_handle, default_codepage) )
  4091. Sql_ShowDebug(logmysql_handle);
  4092. }
  4093. while( 1 ) {
  4094. if(queryThreadTerminate > 0)
  4095. break;
  4096. EnterSpinLock(&queryThreadLock);
  4097. /* mess with queryThreadData within the lock */
  4098. for( i = 0; i < queryThreadData.count; i++ ) {
  4099. struct queryThreadEntry *entry = queryThreadData.entry[i];
  4100. if( entry->ok )
  4101. continue;
  4102. else if ( !entry->st || !entry->st->stack ) {
  4103. entry->ok = true;/* dispose */
  4104. continue;
  4105. }
  4106. buildin_query_sql_sub(entry->st, entry->type ? logmysql_handle : queryThread_handle);
  4107. entry->ok = true;/* we're done with this */
  4108. }
  4109. /* also check for any logs in need to be sent */
  4110. if( log_config.sql_logs ) {
  4111. for( i = 0; i < logThreadData.count; i++ ) {
  4112. if( SQL_ERROR == Sql_Query(logmysql_handle, logThreadData.entry[i]) )
  4113. Sql_ShowDebug(logmysql_handle);
  4114. aFree(logThreadData.entry[i]);
  4115. }
  4116. logThreadData.count = 0;
  4117. }
  4118. LeaveSpinLock(&queryThreadLock);
  4119. ramutex_lock( queryThreadMutex );
  4120. racond_wait( queryThreadCond, queryThreadMutex, -1 );
  4121. ramutex_unlock( queryThreadMutex );
  4122. }
  4123. Sql_Free(queryThread_handle);
  4124. if( log_config.sql_logs ) {
  4125. Sql_Free(logmysql_handle);
  4126. }
  4127. return NULL;
  4128. }
  4129. #endif
  4130. /*==========================================
  4131. * Destructor
  4132. *------------------------------------------*/
  4133. void do_final_script() {
  4134. int i;
  4135. DBIterator *iter;
  4136. struct script_state *st;
  4137. #ifdef DEBUG_HASH
  4138. if (battle_config.etc_log)
  4139. {
  4140. FILE *fp = fopen("hash_dump.txt","wt");
  4141. if(fp) {
  4142. int count[SCRIPT_HASH_SIZE];
  4143. int count2[SCRIPT_HASH_SIZE]; // number of buckets with a certain number of items
  4144. int n=0;
  4145. int min=INT_MAX,max=0,zero=0;
  4146. double mean=0.0f;
  4147. double median=0.0f;
  4148. ShowNotice("Dumping script str hash information to hash_dump.txt\n");
  4149. memset(count, 0, sizeof(count));
  4150. fprintf(fp,"num : hash : data_name\n");
  4151. fprintf(fp,"---------------------------------------------------------------\n");
  4152. for(i=LABEL_START; i<str_num; i++) {
  4153. unsigned int h = calc_hash(get_str(i));
  4154. fprintf(fp,"%04d : %4u : %s\n",i,h, get_str(i));
  4155. ++count[h];
  4156. }
  4157. fprintf(fp,"--------------------\n\n");
  4158. memset(count2, 0, sizeof(count2));
  4159. for(i=0; i<SCRIPT_HASH_SIZE; i++) {
  4160. fprintf(fp," hash %3d = %d\n",i,count[i]);
  4161. if(min > count[i])
  4162. min = count[i]; // minimun count of collision
  4163. if(max < count[i])
  4164. max = count[i]; // maximun count of collision
  4165. if(count[i] == 0)
  4166. zero++;
  4167. ++count2[count[i]];
  4168. }
  4169. fprintf(fp,"\n--------------------\n items : buckets\n--------------------\n");
  4170. for( i=min; i <= max; ++i ){
  4171. fprintf(fp," %5d : %7d\n",i,count2[i]);
  4172. mean += 1.0f*i*count2[i]/SCRIPT_HASH_SIZE; // Note: this will always result in <nr labels>/<nr buckets>
  4173. }
  4174. for( i=min; i <= max; ++i ){
  4175. n += count2[i];
  4176. if( n*2 >= SCRIPT_HASH_SIZE )
  4177. {
  4178. if( SCRIPT_HASH_SIZE%2 == 0 && SCRIPT_HASH_SIZE/2 == n )
  4179. median = (i+i+1)/2.0f;
  4180. else
  4181. median = i;
  4182. break;
  4183. }
  4184. }
  4185. fprintf(fp,"--------------------\n min = %d, max = %d, zero = %d\n mean = %lf, median = %lf\n",min,max,zero,mean,median);
  4186. fclose(fp);
  4187. }
  4188. }
  4189. #endif
  4190. mapreg_final();
  4191. db_destroy(scriptlabel_db);
  4192. userfunc_db->destroy(userfunc_db, db_script_free_code_sub);
  4193. autobonus_db->destroy(autobonus_db, db_script_free_code_sub);
  4194. ers_destroy(array_ers);
  4195. if (generic_ui_array)
  4196. aFree(generic_ui_array);
  4197. iter = db_iterator(st_db);
  4198. for(st = dbi_first(iter); dbi_exists(iter); st = dbi_next(iter))
  4199. script_free_state(st);
  4200. dbi_destroy(iter);
  4201. if (str_data)
  4202. aFree(str_data);
  4203. if (str_buf)
  4204. aFree(str_buf);
  4205. for( i = 0; i < atcmd_binding_count; i++ ) {
  4206. aFree(atcmd_binding[i]);
  4207. }
  4208. if( atcmd_binding_count != 0 )
  4209. aFree(atcmd_binding);
  4210. ers_destroy(st_ers);
  4211. ers_destroy(stack_ers);
  4212. db_destroy(st_db);
  4213. #ifdef BETA_THREAD_TEST
  4214. /* QueryThread */
  4215. InterlockedIncrement(&queryThreadTerminate);
  4216. racond_signal(queryThreadCond);
  4217. rathread_wait(queryThread, NULL);
  4218. // Destroy cond var and mutex.
  4219. racond_destroy( queryThreadCond );
  4220. ramutex_destroy( queryThreadMutex );
  4221. /* Clear missing vars */
  4222. for( i = 0; i < queryThreadData.count; i++ ) {
  4223. aFree(queryThreadData.entry[i]);
  4224. }
  4225. aFree(queryThreadData.entry);
  4226. for( i = 0; i < logThreadData.count; i++ ) {
  4227. aFree(logThreadData.entry[i]);
  4228. }
  4229. aFree(logThreadData.entry);
  4230. #endif
  4231. }
  4232. /*==========================================
  4233. * Initialization
  4234. *------------------------------------------*/
  4235. void do_init_script(void) {
  4236. st_db = idb_alloc(DB_OPT_BASE);
  4237. userfunc_db = strdb_alloc(DB_OPT_DUP_KEY,0);
  4238. scriptlabel_db = strdb_alloc(DB_OPT_DUP_KEY,50);
  4239. autobonus_db = strdb_alloc(DB_OPT_DUP_KEY,0);
  4240. st_ers = ers_new(sizeof(struct script_state), "script.c::st_ers", ERS_CACHE_OPTIONS);
  4241. stack_ers = ers_new(sizeof(struct script_stack), "script.c::script_stack", ERS_OPT_FLEX_CHUNK);
  4242. 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
  4243. ers_chunk_size(st_ers, 10);
  4244. ers_chunk_size(stack_ers, 10);
  4245. active_scripts = 0;
  4246. next_id = 0;
  4247. mapreg_init();
  4248. #ifdef BETA_THREAD_TEST
  4249. CREATE(queryThreadData.entry, struct queryThreadEntry*, 1);
  4250. queryThreadData.count = 0;
  4251. CREATE(logThreadData.entry, char *, 1);
  4252. logThreadData.count = 0;
  4253. /* QueryThread Start */
  4254. InitializeSpinLock(&queryThreadLock);
  4255. queryThreadData.timer = INVALID_TIMER;
  4256. queryThreadTerminate = 0;
  4257. queryThreadMutex = ramutex_create();
  4258. queryThreadCond = racond_create();
  4259. queryThread = rathread_create(queryThread_main, NULL);
  4260. if(queryThread == NULL){
  4261. ShowFatalError("do_init_script: cannot spawn Query Thread.\n");
  4262. exit(EXIT_FAILURE);
  4263. }
  4264. add_timer_func_list(queryThread_timer, "queryThread_timer");
  4265. #endif
  4266. }
  4267. void script_reload(void) {
  4268. int i;
  4269. DBIterator *iter;
  4270. struct script_state *st;
  4271. #ifdef BETA_THREAD_TEST
  4272. /* we're reloading so any queries undergoing should be...exterminated. */
  4273. EnterSpinLock(&queryThreadLock);
  4274. for( i = 0; i < queryThreadData.count; i++ ) {
  4275. aFree(queryThreadData.entry[i]);
  4276. }
  4277. queryThreadData.count = 0;
  4278. if( queryThreadData.timer != INVALID_TIMER ) {
  4279. delete_timer(queryThreadData.timer, queryThread_timer);
  4280. queryThreadData.timer = INVALID_TIMER;
  4281. }
  4282. LeaveSpinLock(&queryThreadLock);
  4283. #endif
  4284. userfunc_db->clear(userfunc_db, db_script_free_code_sub);
  4285. db_clear(scriptlabel_db);
  4286. // @commands (script based)
  4287. // Clear bindings
  4288. for( i = 0; i < atcmd_binding_count; i++ ) {
  4289. aFree(atcmd_binding[i]);
  4290. }
  4291. if( atcmd_binding_count != 0 )
  4292. aFree(atcmd_binding);
  4293. atcmd_binding_count = 0;
  4294. iter = db_iterator(st_db);
  4295. for(st = dbi_first(iter); dbi_exists(iter); st = dbi_next(iter))
  4296. script_free_state(st);
  4297. dbi_destroy(iter);
  4298. db_clear(st_db);
  4299. mapreg_reload();
  4300. }
  4301. //-----------------------------------------------------------------------------
  4302. // buildin functions
  4303. //
  4304. #define BUILDIN_DEF(x,args) { buildin_ ## x , #x , args }
  4305. #define BUILDIN_DEF2(x,x2,args) { buildin_ ## x , x2 , args }
  4306. #define BUILDIN_FUNC(x) int buildin_ ## x (struct script_state* st)
  4307. /////////////////////////////////////////////////////////////////////
  4308. // NPC interaction
  4309. //
  4310. /// Appends a message to the npc dialog.
  4311. /// If a dialog doesn't exist yet, one is created.
  4312. ///
  4313. /// mes "<message>";
  4314. BUILDIN_FUNC(mes)
  4315. {
  4316. TBL_PC* sd = script_rid2sd(st);
  4317. if( sd == NULL )
  4318. return SCRIPT_CMD_SUCCESS;
  4319. if( !script_hasdata(st, 3) )
  4320. {// only a single line detected in the script
  4321. clif_scriptmes(sd, st->oid, script_getstr(st, 2));
  4322. }
  4323. else
  4324. {// parse multiple lines as they exist
  4325. int i;
  4326. for( i = 2; script_hasdata(st, i); i++ )
  4327. {
  4328. // send the message to the client
  4329. clif_scriptmes(sd, st->oid, script_getstr(st, i));
  4330. }
  4331. }
  4332. st->mes_active = 1; // Invoking character has a NPC dialog box open.
  4333. return SCRIPT_CMD_SUCCESS;
  4334. }
  4335. /// Displays the button 'next' in the npc dialog.
  4336. /// The dialog text is cleared and the script continues when the button is pressed.
  4337. ///
  4338. /// next;
  4339. BUILDIN_FUNC(next)
  4340. {
  4341. TBL_PC* sd;
  4342. sd = script_rid2sd(st);
  4343. if( sd == NULL )
  4344. return SCRIPT_CMD_SUCCESS;
  4345. #ifdef SECURE_NPCTIMEOUT
  4346. sd->npc_idle_type = NPCT_WAIT;
  4347. #endif
  4348. st->state = STOP;
  4349. clif_scriptnext(sd, st->oid);
  4350. return SCRIPT_CMD_SUCCESS;
  4351. }
  4352. /// Ends the script and displays the button 'close' on the npc dialog.
  4353. /// The dialog is closed when the button is pressed.
  4354. ///
  4355. /// close;
  4356. BUILDIN_FUNC(close)
  4357. {
  4358. TBL_PC* sd;
  4359. sd = script_rid2sd(st);
  4360. if( sd == NULL )
  4361. return SCRIPT_CMD_SUCCESS;
  4362. if( !st->mes_active ) {
  4363. TBL_NPC* nd = map_id2nd(st->oid);
  4364. st->state = END; // Keep backwards compatibility.
  4365. ShowWarning("Incorrect use of 'close' command! (source:%s / path:%s)\n",nd?nd->name:"Unknown",nd?nd->path:"Unknown");
  4366. } else {
  4367. st->state = CLOSE;
  4368. st->mes_active = 0;
  4369. }
  4370. clif_scriptclose(sd, st->oid);
  4371. return SCRIPT_CMD_SUCCESS;
  4372. }
  4373. /// Displays the button 'close' on the npc dialog.
  4374. /// The dialog is closed and the script continues when the button is pressed.
  4375. ///
  4376. /// close2;
  4377. BUILDIN_FUNC(close2)
  4378. {
  4379. TBL_PC* sd;
  4380. sd = script_rid2sd(st);
  4381. if( sd == NULL )
  4382. return SCRIPT_CMD_SUCCESS;
  4383. st->state = STOP;
  4384. if( st->mes_active )
  4385. st->mes_active = 0;
  4386. clif_scriptclose(sd, st->oid);
  4387. return SCRIPT_CMD_SUCCESS;
  4388. }
  4389. /// Counts the number of valid and total number of options in 'str'
  4390. /// If max_count > 0 the counting stops when that valid option is reached
  4391. /// total is incremented for each option (NULL is supported)
  4392. static int menu_countoptions(const char* str, int max_count, int* total)
  4393. {
  4394. int count = 0;
  4395. int bogus_total;
  4396. if( total == NULL )
  4397. total = &bogus_total;
  4398. ++(*total);
  4399. // initial empty options
  4400. while( *str == ':' )
  4401. {
  4402. ++str;
  4403. ++(*total);
  4404. }
  4405. // count menu options
  4406. while( *str != '\0' )
  4407. {
  4408. ++count;
  4409. --max_count;
  4410. if( max_count == 0 )
  4411. break;
  4412. while( *str != ':' && *str != '\0' )
  4413. ++str;
  4414. while( *str == ':' )
  4415. {
  4416. ++str;
  4417. ++(*total);
  4418. }
  4419. }
  4420. return count;
  4421. }
  4422. /// Displays a menu with options and goes to the target label.
  4423. /// The script is stopped if cancel is pressed.
  4424. /// Options with no text are not displayed in the client.
  4425. ///
  4426. /// Options can be grouped together, separated by the character ':' in the text:
  4427. /// ex: menu "A:B:C",L_target;
  4428. /// All these options go to the specified target label.
  4429. ///
  4430. /// The index of the selected option is put in the variable @menu.
  4431. /// Indexes start with 1 and are consistent with grouped and empty options.
  4432. /// ex: menu "A::B",-,"",L_Impossible,"C",-;
  4433. /// // displays "A", "B" and "C", corresponding to indexes 1, 3 and 5
  4434. ///
  4435. /// NOTE: the client closes the npc dialog when cancel is pressed
  4436. ///
  4437. /// menu "<option_text>",<target_label>{,"<option_text>",<target_label>,...};
  4438. BUILDIN_FUNC(menu)
  4439. {
  4440. int i;
  4441. const char* text;
  4442. TBL_PC* sd;
  4443. sd = script_rid2sd(st);
  4444. if( sd == NULL )
  4445. return SCRIPT_CMD_SUCCESS;
  4446. #ifdef SECURE_NPCTIMEOUT
  4447. sd->npc_idle_type = NPCT_MENU;
  4448. #endif
  4449. // TODO detect multiple scripts waiting for input at the same time, and what to do when that happens
  4450. if( sd->state.menu_or_input == 0 )
  4451. {
  4452. struct StringBuf buf;
  4453. if( script_lastdata(st) % 2 == 0 )
  4454. {// argument count is not even (1st argument is at index 2)
  4455. ShowError("buildin_menu: Illegal number of arguments (%d).\n", (script_lastdata(st) - 1));
  4456. st->state = END;
  4457. return SCRIPT_CMD_FAILURE;
  4458. }
  4459. StringBuf_Init(&buf);
  4460. sd->npc_menu = 0;
  4461. for( i = 2; i < script_lastdata(st); i += 2 )
  4462. {
  4463. struct script_data* data;
  4464. // menu options
  4465. text = script_getstr(st, i);
  4466. // target label
  4467. data = script_getdata(st, i+1);
  4468. if( !data_islabel(data) )
  4469. {// not a label
  4470. StringBuf_Destroy(&buf);
  4471. ShowError("buildin_menu: Argument #%d (from 1) is not a label or label not found.\n", i);
  4472. script_reportdata(data);
  4473. st->state = END;
  4474. return SCRIPT_CMD_FAILURE;
  4475. }
  4476. // append option(s)
  4477. if( text[0] == '\0' )
  4478. continue;// empty string, ignore
  4479. if( sd->npc_menu > 0 )
  4480. StringBuf_AppendStr(&buf, ":");
  4481. StringBuf_AppendStr(&buf, text);
  4482. sd->npc_menu += menu_countoptions(text, 0, NULL);
  4483. }
  4484. st->state = RERUNLINE;
  4485. sd->state.menu_or_input = 1;
  4486. /**
  4487. * menus beyond this length crash the client (see bugreport:6402)
  4488. **/
  4489. if( StringBuf_Length(&buf) >= 2047 ) {
  4490. struct npc_data * nd = map_id2nd(st->oid);
  4491. char* menu;
  4492. CREATE(menu, char, 2048);
  4493. safestrncpy(menu, StringBuf_Value(&buf), 2047);
  4494. ShowWarning("buildin_menu: NPC Menu too long! (source:%s / length:%d)\n",nd?nd->name:"Unknown",StringBuf_Length(&buf));
  4495. clif_scriptmenu(sd, st->oid, menu);
  4496. aFree(menu);
  4497. } else
  4498. clif_scriptmenu(sd, st->oid, StringBuf_Value(&buf));
  4499. StringBuf_Destroy(&buf);
  4500. if( sd->npc_menu >= 0xff )
  4501. {// 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
  4502. ShowWarning("buildin_menu: Too many options specified (current=%d, max=254).\n", sd->npc_menu);
  4503. script_reportsrc(st);
  4504. }
  4505. }
  4506. else if( sd->npc_menu == 0xff )
  4507. {// Cancel was pressed
  4508. sd->state.menu_or_input = 0;
  4509. st->state = END;
  4510. }
  4511. else
  4512. {// goto target label
  4513. int menu = 0;
  4514. sd->state.menu_or_input = 0;
  4515. if( sd->npc_menu <= 0 )
  4516. {
  4517. ShowDebug("buildin_menu: Unexpected selection (%d)\n", sd->npc_menu);
  4518. st->state = END;
  4519. return SCRIPT_CMD_FAILURE;
  4520. }
  4521. // get target label
  4522. for( i = 2; i < script_lastdata(st); i += 2 )
  4523. {
  4524. text = script_getstr(st, i);
  4525. sd->npc_menu -= menu_countoptions(text, sd->npc_menu, &menu);
  4526. if( sd->npc_menu <= 0 )
  4527. break;// entry found
  4528. }
  4529. if( sd->npc_menu > 0 )
  4530. {// Invalid selection
  4531. ShowDebug("buildin_menu: Selection is out of range (%d pairs are missing?) - please report this\n", sd->npc_menu);
  4532. st->state = END;
  4533. return SCRIPT_CMD_FAILURE;
  4534. }
  4535. if( !data_islabel(script_getdata(st, i + 1)) )
  4536. {// TODO remove this temporary crash-prevention code (fallback for multiple scripts requesting user input)
  4537. ShowError("buildin_menu: Unexpected data in label argument\n");
  4538. script_reportdata(script_getdata(st, i + 1));
  4539. st->state = END;
  4540. return SCRIPT_CMD_FAILURE;
  4541. }
  4542. pc_setreg(sd, add_str("@menu"), menu);
  4543. st->pos = script_getnum(st, i + 1);
  4544. st->state = GOTO;
  4545. }
  4546. return SCRIPT_CMD_SUCCESS;
  4547. }
  4548. /// Displays a menu with options and returns the selected option.
  4549. /// Behaves like 'menu' without the target labels.
  4550. ///
  4551. /// select(<option_text>{,<option_text>,...}) -> <selected_option>
  4552. ///
  4553. /// @see menu
  4554. BUILDIN_FUNC(select)
  4555. {
  4556. int i;
  4557. const char* text;
  4558. TBL_PC* sd;
  4559. sd = script_rid2sd(st);
  4560. if( sd == NULL )
  4561. return SCRIPT_CMD_SUCCESS;
  4562. #ifdef SECURE_NPCTIMEOUT
  4563. sd->npc_idle_type = NPCT_MENU;
  4564. #endif
  4565. if( sd->state.menu_or_input == 0 ) {
  4566. struct StringBuf buf;
  4567. StringBuf_Init(&buf);
  4568. sd->npc_menu = 0;
  4569. for( i = 2; i <= script_lastdata(st); ++i ) {
  4570. text = script_getstr(st, i);
  4571. if( sd->npc_menu > 0 )
  4572. StringBuf_AppendStr(&buf, ":");
  4573. StringBuf_AppendStr(&buf, text);
  4574. sd->npc_menu += menu_countoptions(text, 0, NULL);
  4575. }
  4576. st->state = RERUNLINE;
  4577. sd->state.menu_or_input = 1;
  4578. /**
  4579. * menus beyond this length crash the client (see bugreport:6402)
  4580. **/
  4581. if( StringBuf_Length(&buf) >= 2047 ) {
  4582. struct npc_data * nd = map_id2nd(st->oid);
  4583. char* menu;
  4584. CREATE(menu, char, 2048);
  4585. safestrncpy(menu, StringBuf_Value(&buf), 2047);
  4586. ShowWarning("buildin_select: NPC Menu too long! (source:%s / length:%d)\n",nd?nd->name:"Unknown",StringBuf_Length(&buf));
  4587. clif_scriptmenu(sd, st->oid, menu);
  4588. aFree(menu);
  4589. } else
  4590. clif_scriptmenu(sd, st->oid, StringBuf_Value(&buf));
  4591. StringBuf_Destroy(&buf);
  4592. if( sd->npc_menu >= 0xff ) {
  4593. ShowWarning("buildin_select: Too many options specified (current=%d, max=254).\n", sd->npc_menu);
  4594. script_reportsrc(st);
  4595. }
  4596. } else if( sd->npc_menu == 0xff ) {// Cancel was pressed
  4597. sd->state.menu_or_input = 0;
  4598. st->state = END;
  4599. } else {// return selected option
  4600. int menu = 0;
  4601. sd->state.menu_or_input = 0;
  4602. for( i = 2; i <= script_lastdata(st); ++i ) {
  4603. text = script_getstr(st, i);
  4604. sd->npc_menu -= menu_countoptions(text, sd->npc_menu, &menu);
  4605. if( sd->npc_menu <= 0 )
  4606. break;// entry found
  4607. }
  4608. pc_setreg(sd, add_str("@menu"), menu);
  4609. script_pushint(st, menu);
  4610. st->state = RUN;
  4611. }
  4612. return SCRIPT_CMD_SUCCESS;
  4613. }
  4614. /// Displays a menu with options and returns the selected option.
  4615. /// Behaves like 'menu' without the target labels, except when cancel is
  4616. /// pressed.
  4617. /// When cancel is pressed, the script continues and 255 is returned.
  4618. ///
  4619. /// prompt(<option_text>{,<option_text>,...}) -> <selected_option>
  4620. ///
  4621. /// @see menu
  4622. BUILDIN_FUNC(prompt)
  4623. {
  4624. int i;
  4625. const char *text;
  4626. TBL_PC* sd;
  4627. sd = script_rid2sd(st);
  4628. if( sd == NULL )
  4629. return SCRIPT_CMD_SUCCESS;
  4630. #ifdef SECURE_NPCTIMEOUT
  4631. sd->npc_idle_type = NPCT_MENU;
  4632. #endif
  4633. if( sd->state.menu_or_input == 0 )
  4634. {
  4635. struct StringBuf buf;
  4636. StringBuf_Init(&buf);
  4637. sd->npc_menu = 0;
  4638. for( i = 2; i <= script_lastdata(st); ++i )
  4639. {
  4640. text = script_getstr(st, i);
  4641. if( sd->npc_menu > 0 )
  4642. StringBuf_AppendStr(&buf, ":");
  4643. StringBuf_AppendStr(&buf, text);
  4644. sd->npc_menu += menu_countoptions(text, 0, NULL);
  4645. }
  4646. st->state = RERUNLINE;
  4647. sd->state.menu_or_input = 1;
  4648. /**
  4649. * menus beyond this length crash the client (see bugreport:6402)
  4650. **/
  4651. if( StringBuf_Length(&buf) >= 2047 ) {
  4652. struct npc_data * nd = map_id2nd(st->oid);
  4653. char* menu;
  4654. CREATE(menu, char, 2048);
  4655. safestrncpy(menu, StringBuf_Value(&buf), 2047);
  4656. ShowWarning("buildin_prompt: NPC Menu too long! (source:%s / length:%d)\n",nd?nd->name:"Unknown",StringBuf_Length(&buf));
  4657. clif_scriptmenu(sd, st->oid, menu);
  4658. aFree(menu);
  4659. } else
  4660. clif_scriptmenu(sd, st->oid, StringBuf_Value(&buf));
  4661. StringBuf_Destroy(&buf);
  4662. if( sd->npc_menu >= 0xff )
  4663. {
  4664. ShowWarning("buildin_prompt: Too many options specified (current=%d, max=254).\n", sd->npc_menu);
  4665. script_reportsrc(st);
  4666. }
  4667. }
  4668. else if( sd->npc_menu == 0xff )
  4669. {// Cancel was pressed
  4670. sd->state.menu_or_input = 0;
  4671. pc_setreg(sd, add_str("@menu"), 0xff);
  4672. script_pushint(st, 0xff);
  4673. st->state = RUN;
  4674. }
  4675. else
  4676. {// return selected option
  4677. int menu = 0;
  4678. sd->state.menu_or_input = 0;
  4679. for( i = 2; i <= script_lastdata(st); ++i )
  4680. {
  4681. text = script_getstr(st, i);
  4682. sd->npc_menu -= menu_countoptions(text, sd->npc_menu, &menu);
  4683. if( sd->npc_menu <= 0 )
  4684. break;// entry found
  4685. }
  4686. pc_setreg(sd, add_str("@menu"), menu);
  4687. script_pushint(st, menu);
  4688. st->state = RUN;
  4689. }
  4690. return SCRIPT_CMD_SUCCESS;
  4691. }
  4692. /////////////////////////////////////////////////////////////////////
  4693. // ...
  4694. //
  4695. /// Jumps to the target script label.
  4696. ///
  4697. /// goto <label>;
  4698. BUILDIN_FUNC(goto)
  4699. {
  4700. if( !data_islabel(script_getdata(st,2)) )
  4701. {
  4702. ShowError("buildin_goto: Not a label\n");
  4703. script_reportdata(script_getdata(st,2));
  4704. st->state = END;
  4705. return SCRIPT_CMD_FAILURE;
  4706. }
  4707. st->pos = script_getnum(st,2);
  4708. st->state = GOTO;
  4709. return SCRIPT_CMD_SUCCESS;
  4710. }
  4711. /*==========================================
  4712. * user-defined function call
  4713. *------------------------------------------*/
  4714. BUILDIN_FUNC(callfunc)
  4715. {
  4716. int i, j;
  4717. struct script_retinfo* ri;
  4718. struct script_code* scr;
  4719. const char* str = script_getstr(st,2);
  4720. struct reg_db *ref = NULL;
  4721. scr = (struct script_code*)strdb_get(userfunc_db, str);
  4722. if(!scr) {
  4723. ShowError("buildin_callfunc: Function not found! [%s]\n", str);
  4724. st->state = END;
  4725. return SCRIPT_CMD_FAILURE;
  4726. }
  4727. ref = (struct reg_db *)aCalloc(sizeof(struct reg_db), 2);
  4728. ref[0].vars = st->stack->scope.vars;
  4729. if (!st->stack->scope.arrays)
  4730. st->stack->scope.arrays = idb_alloc(DB_OPT_BASE); // TODO: Can this happen? when?
  4731. ref[0].arrays = st->stack->scope.arrays;
  4732. ref[1].vars = st->script->local.vars;
  4733. if (!st->script->local.arrays)
  4734. st->script->local.arrays = idb_alloc(DB_OPT_BASE); // TODO: Can this happen? when?
  4735. ref[1].arrays = st->script->local.arrays;
  4736. for(i = st->start+3, j = 0; i < st->end; i++, j++) {
  4737. struct script_data* data = push_copy(st->stack,i);
  4738. if (data_isreference(data) && !data->ref) {
  4739. const char* name = reference_getname(data);
  4740. if (name[0] == '.')
  4741. data->ref = (name[1] == '@' ? &ref[0] : &ref[1]);
  4742. }
  4743. }
  4744. CREATE(ri, struct script_retinfo, 1);
  4745. ri->script = st->script; // script code
  4746. ri->scope.vars = st->stack->scope.vars; // scope variables
  4747. ri->scope.arrays = st->stack->scope.arrays; // scope arrays
  4748. ri->pos = st->pos; // script location
  4749. ri->nargs = j; // argument count
  4750. ri->defsp = st->stack->defsp; // default stack pointer
  4751. push_retinfo(st->stack, ri, ref);
  4752. st->pos = 0;
  4753. st->script = scr;
  4754. st->stack->defsp = st->stack->sp;
  4755. st->state = GOTO;
  4756. st->stack->scope.vars = i64db_alloc(DB_OPT_RELEASE_DATA);
  4757. st->stack->scope.arrays = idb_alloc(DB_OPT_BASE);
  4758. if (!st->script->local.vars)
  4759. st->script->local.vars = i64db_alloc(DB_OPT_RELEASE_DATA);
  4760. return SCRIPT_CMD_SUCCESS;
  4761. }
  4762. /*==========================================
  4763. * subroutine call
  4764. *------------------------------------------*/
  4765. BUILDIN_FUNC(callsub)
  4766. {
  4767. int i,j;
  4768. struct script_retinfo* ri;
  4769. int pos = script_getnum(st,2);
  4770. struct reg_db *ref = NULL;
  4771. if( !data_islabel(script_getdata(st,2)) && !data_isfunclabel(script_getdata(st,2)) ) {
  4772. ShowError("buildin_callsub: Argument is not a label\n");
  4773. script_reportdata(script_getdata(st,2));
  4774. st->state = END;
  4775. return SCRIPT_CMD_FAILURE;
  4776. }
  4777. ref = (struct reg_db *)aCalloc(sizeof(struct reg_db), 1);
  4778. ref[0].vars = st->stack->scope.vars;
  4779. if (!st->stack->scope.arrays)
  4780. st->stack->scope.arrays = idb_alloc(DB_OPT_BASE); // TODO: Can this happen? when?
  4781. ref[0].arrays = st->stack->scope.arrays;
  4782. for(i = st->start+3, j = 0; i < st->end; i++, j++) {
  4783. struct script_data* data = push_copy(st->stack,i);
  4784. if (data_isreference(data) && !data->ref) {
  4785. const char* name = reference_getname(data);
  4786. if (name[0] == '.' && name[1] == '@')
  4787. data->ref = &ref[0];
  4788. }
  4789. }
  4790. CREATE(ri, struct script_retinfo, 1);
  4791. ri->script = st->script; // script code
  4792. ri->scope.vars = st->stack->scope.vars; // scope variables
  4793. ri->scope.arrays = st->stack->scope.arrays; // scope arrays
  4794. ri->pos = st->pos; // script location
  4795. ri->nargs = j; // argument count
  4796. ri->defsp = st->stack->defsp; // default stack pointer
  4797. push_retinfo(st->stack, ri, ref);
  4798. st->pos = pos;
  4799. st->stack->defsp = st->stack->sp;
  4800. st->state = GOTO;
  4801. st->stack->scope.vars = i64db_alloc(DB_OPT_RELEASE_DATA);
  4802. st->stack->scope.arrays = idb_alloc(DB_OPT_BASE);
  4803. return SCRIPT_CMD_SUCCESS;
  4804. }
  4805. /// Retrieves an argument provided to callfunc/callsub.
  4806. /// If the argument doesn't exist
  4807. ///
  4808. /// getarg(<index>{,<default_value>}) -> <value>
  4809. BUILDIN_FUNC(getarg)
  4810. {
  4811. struct script_retinfo* ri;
  4812. int idx;
  4813. if( st->stack->defsp < 1 || st->stack->stack_data[st->stack->defsp - 1].type != C_RETINFO )
  4814. {
  4815. ShowError("buildin_getarg: No callfunc or callsub!\n");
  4816. st->state = END;
  4817. return SCRIPT_CMD_FAILURE;
  4818. }
  4819. ri = st->stack->stack_data[st->stack->defsp - 1].u.ri;
  4820. idx = script_getnum(st,2);
  4821. if( idx >= 0 && idx < ri->nargs )
  4822. push_copy(st->stack, st->stack->defsp - 1 - ri->nargs + idx);
  4823. else if( script_hasdata(st,3) )
  4824. script_pushcopy(st, 3);
  4825. else
  4826. {
  4827. ShowError("buildin_getarg: Index (idx=%d) out of range (nargs=%d) and no default value found\n", idx, ri->nargs);
  4828. st->state = END;
  4829. return SCRIPT_CMD_FAILURE;
  4830. }
  4831. return SCRIPT_CMD_SUCCESS;
  4832. }
  4833. /// Returns from the current function, optionaly returning a value from the functions.
  4834. /// Don't use outside script functions.
  4835. ///
  4836. /// return;
  4837. /// return <value>;
  4838. BUILDIN_FUNC(return)
  4839. {
  4840. if( script_hasdata(st,2) )
  4841. {// return value
  4842. struct script_data* data;
  4843. script_pushcopy(st, 2);
  4844. data = script_getdatatop(st, -1);
  4845. if( data_isreference(data) ) {
  4846. const char* name = reference_getname(data);
  4847. if( name[0] == '.' && name[1] == '@' ) { // scope variable
  4848. if( !data->ref || data->ref->vars == st->stack->scope.vars )
  4849. get_val(st, data); // current scope, convert to value
  4850. if( data->ref && data->ref->vars == st->stack->stack_data[st->stack->defsp-1].u.ri->scope.vars )
  4851. data->ref = NULL; // Reference to the parent scope, remove reference pointer
  4852. } else if( name[0] == '.' && !data->ref ) { // script variable, link to current script
  4853. data->ref = (struct reg_db *)aCalloc(sizeof(struct reg_db), 1);
  4854. data->ref->vars = st->script->local.vars;
  4855. if (!st->script->local.arrays)
  4856. st->script->local.arrays = idb_alloc(DB_OPT_BASE);
  4857. data->ref->arrays = st->script->local.arrays;
  4858. }
  4859. }
  4860. }
  4861. else
  4862. {// no return value
  4863. script_pushnil(st);
  4864. }
  4865. st->state = RETFUNC;
  4866. return SCRIPT_CMD_SUCCESS;
  4867. }
  4868. /// Returns a random number from 0 to <range>-1.
  4869. /// Or returns a random number from <min> to <max>.
  4870. /// If <min> is greater than <max>, their numbers are switched.
  4871. /// rand(<range>) -> <int>
  4872. /// rand(<min>,<max>) -> <int>
  4873. BUILDIN_FUNC(rand)
  4874. {
  4875. int range;
  4876. int min;
  4877. if( script_hasdata(st,3) )
  4878. {// min,max
  4879. int max = script_getnum(st,3);
  4880. min = script_getnum(st,2);
  4881. if( max < min )
  4882. swap(min, max);
  4883. range = max - min + 1;
  4884. }
  4885. else
  4886. {// range
  4887. min = 0;
  4888. range = script_getnum(st,2);
  4889. }
  4890. if( range <= 1 )
  4891. script_pushint(st, min);
  4892. else
  4893. script_pushint(st, rnd()%range + min);
  4894. return SCRIPT_CMD_SUCCESS;
  4895. }
  4896. /*==========================================
  4897. * Warp sd to str,x,y or Random or SavePoint/Save
  4898. *------------------------------------------*/
  4899. BUILDIN_FUNC(warp)
  4900. {
  4901. int ret;
  4902. int x,y;
  4903. const char* str;
  4904. struct map_session_data* sd;
  4905. if(!script_charid2sd(5, sd))
  4906. return SCRIPT_CMD_SUCCESS;
  4907. str = script_getstr(st,2);
  4908. x = script_getnum(st,3);
  4909. y = script_getnum(st,4);
  4910. if(strcmp(str,"Random")==0)
  4911. ret = pc_randomwarp(sd,CLR_TELEPORT);
  4912. else if(strcmp(str,"SavePoint")==0 || strcmp(str,"Save")==0)
  4913. ret = pc_setpos(sd,sd->status.save_point.map,sd->status.save_point.x,sd->status.save_point.y,CLR_TELEPORT);
  4914. else
  4915. ret = pc_setpos(sd,mapindex_name2id(str),x,y,CLR_OUTSIGHT);
  4916. if( ret ) {
  4917. ShowError("buildin_warp: moving player '%s' to \"%s\",%d,%d failed.\n", sd->status.name, str, x, y);
  4918. return SCRIPT_CMD_FAILURE;
  4919. }
  4920. return SCRIPT_CMD_SUCCESS;
  4921. }
  4922. /*==========================================
  4923. * Warp a specified area
  4924. *------------------------------------------*/
  4925. static int buildin_areawarp_sub(struct block_list *bl,va_list ap)
  4926. {
  4927. int x2,y2,x3,y3;
  4928. unsigned int index;
  4929. index = va_arg(ap,unsigned int);
  4930. x2 = va_arg(ap,int);
  4931. y2 = va_arg(ap,int);
  4932. x3 = va_arg(ap,int);
  4933. y3 = va_arg(ap,int);
  4934. if(index == 0)
  4935. pc_randomwarp((TBL_PC *)bl,CLR_TELEPORT);
  4936. else if(x3 && y3) {
  4937. int max, tx, ty, j = 0;
  4938. int16 m;
  4939. m = map_mapindex2mapid(index);
  4940. // choose a suitable max number of attempts
  4941. if( (max = (y3-y2+1)*(x3-x2+1)*3) > 1000 )
  4942. max = 1000;
  4943. // find a suitable map cell
  4944. do {
  4945. tx = rnd()%(x3-x2+1)+x2;
  4946. ty = rnd()%(y3-y2+1)+y2;
  4947. j++;
  4948. } while( map_getcell(m,tx,ty,CELL_CHKNOPASS) && j < max );
  4949. pc_setpos((TBL_PC *)bl,index,tx,ty,CLR_OUTSIGHT);
  4950. }
  4951. else
  4952. pc_setpos((TBL_PC *)bl,index,x2,y2,CLR_OUTSIGHT);
  4953. return 0;
  4954. }
  4955. BUILDIN_FUNC(areawarp)
  4956. {
  4957. int16 m, x0,y0,x1,y1, x2,y2,x3=0,y3=0;
  4958. unsigned int index;
  4959. const char *str;
  4960. const char *mapname;
  4961. mapname = script_getstr(st,2);
  4962. x0 = script_getnum(st,3);
  4963. y0 = script_getnum(st,4);
  4964. x1 = script_getnum(st,5);
  4965. y1 = script_getnum(st,6);
  4966. str = script_getstr(st,7);
  4967. x2 = script_getnum(st,8);
  4968. y2 = script_getnum(st,9);
  4969. if( script_hasdata(st,10) && script_hasdata(st,11) ) { // Warp area to area
  4970. if( (x3 = script_getnum(st,10)) < 0 || (y3 = script_getnum(st,11)) < 0 ){
  4971. x3 = 0;
  4972. y3 = 0;
  4973. } else if( x3 && y3 ) {
  4974. // normalize x3/y3 coordinates
  4975. if( x3 < x2 ) swap(x3,x2);
  4976. if( y3 < y2 ) swap(y3,y2);
  4977. }
  4978. }
  4979. if( (m = map_mapname2mapid(mapname)) < 0 )
  4980. return SCRIPT_CMD_FAILURE;
  4981. if( strcmp(str,"Random") == 0 )
  4982. index = 0;
  4983. else if( !(index=mapindex_name2id(str)) )
  4984. return SCRIPT_CMD_FAILURE;
  4985. map_foreachinarea(buildin_areawarp_sub, m,x0,y0,x1,y1, BL_PC, index,x2,y2,x3,y3);
  4986. return SCRIPT_CMD_SUCCESS;
  4987. }
  4988. /*==========================================
  4989. * areapercentheal <map>,<x1>,<y1>,<x2>,<y2>,<hp>,<sp>
  4990. *------------------------------------------*/
  4991. static int buildin_areapercentheal_sub(struct block_list *bl,va_list ap)
  4992. {
  4993. int hp, sp;
  4994. hp = va_arg(ap, int);
  4995. sp = va_arg(ap, int);
  4996. pc_percentheal((TBL_PC *)bl,hp,sp);
  4997. return 0;
  4998. }
  4999. BUILDIN_FUNC(areapercentheal)
  5000. {
  5001. int hp,sp,m;
  5002. const char *mapname;
  5003. int x0,y0,x1,y1;
  5004. mapname=script_getstr(st,2);
  5005. x0=script_getnum(st,3);
  5006. y0=script_getnum(st,4);
  5007. x1=script_getnum(st,5);
  5008. y1=script_getnum(st,6);
  5009. hp=script_getnum(st,7);
  5010. sp=script_getnum(st,8);
  5011. if( (m=map_mapname2mapid(mapname))< 0)
  5012. return SCRIPT_CMD_FAILURE;
  5013. map_foreachinarea(buildin_areapercentheal_sub,m,x0,y0,x1,y1,BL_PC,hp,sp);
  5014. return SCRIPT_CMD_SUCCESS;
  5015. }
  5016. /*==========================================
  5017. * Warpparty - [Fredzilla] [Paradox924X]
  5018. * Syntax: warpparty "to_mapname",x,y,Party_ID,{"from_mapname"};
  5019. * If 'from_mapname' is specified, only the party members on that map will be warped
  5020. *------------------------------------------*/
  5021. BUILDIN_FUNC(warpparty)
  5022. {
  5023. TBL_PC *sd = NULL;
  5024. TBL_PC *pl_sd;
  5025. struct party_data* p;
  5026. int type;
  5027. int mapindex = 0, m = -1;
  5028. int i;
  5029. const char* str = script_getstr(st,2);
  5030. int x = script_getnum(st,3);
  5031. int y = script_getnum(st,4);
  5032. int p_id = script_getnum(st,5);
  5033. const char* str2 = NULL;
  5034. if ( script_hasdata(st,6) )
  5035. str2 = script_getstr(st,6);
  5036. p = party_search(p_id);
  5037. if(!p)
  5038. return SCRIPT_CMD_SUCCESS;
  5039. type = ( strcmp(str,"Random")==0 ) ? 0
  5040. : ( strcmp(str,"SavePointAll")==0 ) ? 1
  5041. : ( strcmp(str,"SavePoint")==0 ) ? 2
  5042. : ( strcmp(str,"Leader")==0 ) ? 3
  5043. : 4;
  5044. switch (type)
  5045. {
  5046. case 3:
  5047. for(i = 0; i < MAX_PARTY && !p->party.member[i].leader; i++);
  5048. if (i == MAX_PARTY || !p->data[i].sd) //Leader not found / not online
  5049. return SCRIPT_CMD_FAILURE;
  5050. pl_sd = p->data[i].sd;
  5051. mapindex = pl_sd->mapindex;
  5052. m = map_mapindex2mapid(mapindex);
  5053. x = pl_sd->bl.x;
  5054. y = pl_sd->bl.y;
  5055. break;
  5056. case 4:
  5057. mapindex = mapindex_name2id(str);
  5058. if (!mapindex) {// Invalid map
  5059. return SCRIPT_CMD_FAILURE;
  5060. }
  5061. m = map_mapindex2mapid(mapindex);
  5062. break;
  5063. case 2:
  5064. //"SavePoint" uses save point of the currently attached player
  5065. if (( sd = script_rid2sd(st) ) == NULL )
  5066. return SCRIPT_CMD_SUCCESS;
  5067. break;
  5068. }
  5069. for (i = 0; i < MAX_PARTY; i++)
  5070. {
  5071. if( !(pl_sd = p->data[i].sd) || pl_sd->status.party_id != p_id )
  5072. continue;
  5073. if( str2 && strcmp(str2, map[pl_sd->bl.m].name) != 0 )
  5074. continue;
  5075. if( pc_isdead(pl_sd) )
  5076. continue;
  5077. switch( type )
  5078. {
  5079. case 0: // Random
  5080. if(!map[pl_sd->bl.m].flag.nowarp)
  5081. pc_randomwarp(pl_sd,CLR_TELEPORT);
  5082. break;
  5083. case 1: // SavePointAll
  5084. if(!map[pl_sd->bl.m].flag.noreturn)
  5085. pc_setpos(pl_sd,pl_sd->status.save_point.map,pl_sd->status.save_point.x,pl_sd->status.save_point.y,CLR_TELEPORT);
  5086. break;
  5087. case 2: // SavePoint
  5088. if(!map[pl_sd->bl.m].flag.noreturn)
  5089. pc_setpos(pl_sd,sd->status.save_point.map,sd->status.save_point.x,sd->status.save_point.y,CLR_TELEPORT);
  5090. break;
  5091. case 3: // Leader
  5092. case 4: // m,x,y
  5093. 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))
  5094. pc_setpos(pl_sd,mapindex,x,y,CLR_TELEPORT);
  5095. break;
  5096. }
  5097. }
  5098. return SCRIPT_CMD_SUCCESS;
  5099. }
  5100. /*==========================================
  5101. * Warpguild - [Fredzilla]
  5102. * Syntax: warpguild "mapname",x,y,Guild_ID;
  5103. *------------------------------------------*/
  5104. BUILDIN_FUNC(warpguild)
  5105. {
  5106. TBL_PC *sd = NULL;
  5107. TBL_PC *pl_sd;
  5108. struct guild* g;
  5109. struct s_mapiterator* iter;
  5110. int type, mapindex = 0, m = -1;
  5111. const char* str = script_getstr(st,2);
  5112. int x = script_getnum(st,3);
  5113. int y = script_getnum(st,4);
  5114. int gid = script_getnum(st,5);
  5115. g = guild_search(gid);
  5116. if( g == NULL )
  5117. return SCRIPT_CMD_SUCCESS;
  5118. type = ( strcmp(str,"Random")==0 ) ? 0
  5119. : ( strcmp(str,"SavePointAll")==0 ) ? 1
  5120. : ( strcmp(str,"SavePoint")==0 ) ? 2
  5121. : 3;
  5122. if( type == 2 && ( sd = script_rid2sd(st) ) == NULL )
  5123. {// "SavePoint" uses save point of the currently attached player
  5124. return SCRIPT_CMD_SUCCESS;
  5125. }
  5126. switch (type) {
  5127. case 3:
  5128. mapindex = mapindex_name2id(str);
  5129. if (!mapindex)
  5130. return SCRIPT_CMD_FAILURE;
  5131. m = map_mapindex2mapid(mapindex);
  5132. break;
  5133. }
  5134. iter = mapit_getallusers();
  5135. for( pl_sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); pl_sd = (TBL_PC*)mapit_next(iter) )
  5136. {
  5137. if( pl_sd->status.guild_id != gid )
  5138. continue;
  5139. switch( type )
  5140. {
  5141. case 0: // Random
  5142. if(!map[pl_sd->bl.m].flag.nowarp)
  5143. pc_randomwarp(pl_sd,CLR_TELEPORT);
  5144. break;
  5145. case 1: // SavePointAll
  5146. if(!map[pl_sd->bl.m].flag.noreturn)
  5147. pc_setpos(pl_sd,pl_sd->status.save_point.map,pl_sd->status.save_point.x,pl_sd->status.save_point.y,CLR_TELEPORT);
  5148. break;
  5149. case 2: // SavePoint
  5150. if(!map[pl_sd->bl.m].flag.noreturn)
  5151. pc_setpos(pl_sd,sd->status.save_point.map,sd->status.save_point.x,sd->status.save_point.y,CLR_TELEPORT);
  5152. break;
  5153. case 3: // m,x,y
  5154. 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))
  5155. pc_setpos(pl_sd,mapindex,x,y,CLR_TELEPORT);
  5156. break;
  5157. }
  5158. }
  5159. mapit_free(iter);
  5160. return SCRIPT_CMD_SUCCESS;
  5161. }
  5162. /*==========================================
  5163. * Force Heal a player (hp and sp)
  5164. *------------------------------------------*/
  5165. BUILDIN_FUNC(heal)
  5166. {
  5167. TBL_PC *sd;
  5168. int hp,sp;
  5169. if (!script_charid2sd(4,sd))
  5170. return SCRIPT_CMD_SUCCESS;
  5171. hp=script_getnum(st,2);
  5172. sp=script_getnum(st,3);
  5173. status_heal(&sd->bl, hp, sp, 1);
  5174. return SCRIPT_CMD_SUCCESS;
  5175. }
  5176. /*==========================================
  5177. * Heal a player by item (get vit bonus etc)
  5178. *------------------------------------------*/
  5179. BUILDIN_FUNC(itemheal)
  5180. {
  5181. TBL_PC *sd;
  5182. int hp,sp;
  5183. hp=script_getnum(st,2);
  5184. sp=script_getnum(st,3);
  5185. if(potion_flag==1) {
  5186. potion_hp = hp;
  5187. potion_sp = sp;
  5188. return SCRIPT_CMD_SUCCESS;
  5189. }
  5190. if (!script_charid2sd(4,sd))
  5191. return SCRIPT_CMD_SUCCESS;
  5192. pc_itemheal(sd,sd->itemid,hp,sp);
  5193. return SCRIPT_CMD_SUCCESS;
  5194. }
  5195. /*==========================================
  5196. *
  5197. *------------------------------------------*/
  5198. BUILDIN_FUNC(percentheal)
  5199. {
  5200. int hp,sp;
  5201. TBL_PC* sd;
  5202. hp=script_getnum(st,2);
  5203. sp=script_getnum(st,3);
  5204. if(potion_flag==1) {
  5205. potion_per_hp = hp;
  5206. potion_per_sp = sp;
  5207. return SCRIPT_CMD_SUCCESS;
  5208. }
  5209. if (!script_charid2sd(4,sd))
  5210. return SCRIPT_CMD_SUCCESS;
  5211. #ifdef RENEWAL
  5212. if( sd->sc.data[SC_EXTREMITYFIST2] )
  5213. sp = 0;
  5214. #endif
  5215. if (sd->sc.data[SC_NORECOVER_STATE]) {
  5216. hp = 0;
  5217. sp = 0;
  5218. }
  5219. if (sd->sc.data[SC_BITESCAR])
  5220. hp = 0;
  5221. pc_percentheal(sd,hp,sp);
  5222. return SCRIPT_CMD_SUCCESS;
  5223. }
  5224. /*==========================================
  5225. *
  5226. *------------------------------------------*/
  5227. BUILDIN_FUNC(jobchange)
  5228. {
  5229. int job, upper=-1;
  5230. job=script_getnum(st,2);
  5231. if( script_hasdata(st,3) )
  5232. upper=script_getnum(st,3);
  5233. if (pcdb_checkid(job))
  5234. {
  5235. TBL_PC* sd;
  5236. if (!script_charid2sd(4,sd))
  5237. return SCRIPT_CMD_SUCCESS;
  5238. pc_jobchange(sd, job, upper);
  5239. }
  5240. return SCRIPT_CMD_SUCCESS;
  5241. }
  5242. /*==========================================
  5243. *
  5244. *------------------------------------------*/
  5245. BUILDIN_FUNC(jobname)
  5246. {
  5247. int class_=script_getnum(st,2);
  5248. script_pushconststr(st, (char*)job_name(class_));
  5249. return SCRIPT_CMD_SUCCESS;
  5250. }
  5251. /// Get input from the player.
  5252. /// For numeric inputs the value is capped to the range [min,max]. Returns 1 if
  5253. /// the value was higher than 'max', -1 if lower than 'min' and 0 otherwise.
  5254. /// For string inputs it returns 1 if the string was longer than 'max', -1 is
  5255. /// shorter than 'min' and 0 otherwise.
  5256. ///
  5257. /// input(<var>{,<min>{,<max>}}) -> <int>
  5258. BUILDIN_FUNC(input)
  5259. {
  5260. TBL_PC* sd;
  5261. struct script_data* data;
  5262. int64 uid;
  5263. const char* name;
  5264. int min;
  5265. int max;
  5266. sd = script_rid2sd(st);
  5267. if( sd == NULL )
  5268. return SCRIPT_CMD_SUCCESS;
  5269. data = script_getdata(st,2);
  5270. if( !data_isreference(data) ){
  5271. ShowError("script:input: not a variable\n");
  5272. script_reportdata(data);
  5273. st->state = END;
  5274. return SCRIPT_CMD_FAILURE;
  5275. }
  5276. uid = reference_getuid(data);
  5277. name = reference_getname(data);
  5278. min = (script_hasdata(st,3) ? script_getnum(st,3) : script_config.input_min_value);
  5279. max = (script_hasdata(st,4) ? script_getnum(st,4) : script_config.input_max_value);
  5280. #ifdef SECURE_NPCTIMEOUT
  5281. sd->npc_idle_type = NPCT_WAIT;
  5282. #endif
  5283. if( !sd->state.menu_or_input )
  5284. { // first invocation, display npc input box
  5285. sd->state.menu_or_input = 1;
  5286. st->state = RERUNLINE;
  5287. if( is_string_variable(name) )
  5288. clif_scriptinputstr(sd,st->oid);
  5289. else
  5290. clif_scriptinput(sd,st->oid);
  5291. }
  5292. else
  5293. { // take received text/value and store it in the designated variable
  5294. sd->state.menu_or_input = 0;
  5295. if( is_string_variable(name) )
  5296. {
  5297. int len = (int)strlen(sd->npc_str);
  5298. set_reg(st, sd, uid, name, (void*)sd->npc_str, script_getref(st,2));
  5299. script_pushint(st, (len > max ? 1 : len < min ? -1 : 0));
  5300. }
  5301. else
  5302. {
  5303. int amount = sd->npc_amount;
  5304. set_reg(st, sd, uid, name, (void*)__64BPRTSIZE(cap_value(amount,min,max)), script_getref(st,2));
  5305. script_pushint(st, (amount > max ? 1 : amount < min ? -1 : 0));
  5306. }
  5307. st->state = RUN;
  5308. }
  5309. return SCRIPT_CMD_SUCCESS;
  5310. }
  5311. // declare the copyarray method here for future reference
  5312. BUILDIN_FUNC(copyarray);
  5313. /// Sets the value of a variable.
  5314. /// The value is converted to the type of the variable.
  5315. ///
  5316. /// set(<variable>,<value>{,<charid>})
  5317. BUILDIN_FUNC(setr)
  5318. {
  5319. TBL_PC* sd = NULL;
  5320. struct script_data* data;
  5321. //struct script_data* datavalue;
  5322. int64 num;
  5323. uint8 pos = 4;
  5324. const char* name, *command = script_getfuncname(st);
  5325. char prefix;
  5326. data = script_getdata(st,2);
  5327. //datavalue = script_getdata(st,3);
  5328. if( !data_isreference(data) )
  5329. {
  5330. ShowError("script:set: not a variable\n");
  5331. script_reportdata(script_getdata(st,2));
  5332. st->state = END;
  5333. return SCRIPT_CMD_FAILURE;
  5334. }
  5335. num = reference_getuid(data);
  5336. name = reference_getname(data);
  5337. prefix = *name;
  5338. if (!strcmp(command, "setr"))
  5339. pos = 5;
  5340. if (not_server_variable(prefix) && !script_charid2sd(pos,sd)) {
  5341. ShowError("buildin_set: No player attached for player variable '%s'\n", name);
  5342. return SCRIPT_CMD_FAILURE;
  5343. }
  5344. #if 0
  5345. if( data_isreference(datavalue) )
  5346. {// the value being referenced is a variable
  5347. const char* namevalue = reference_getname(datavalue);
  5348. if( !not_array_variable(*namevalue) )
  5349. {// array variable being copied into another array variable
  5350. if( sd == NULL && not_server_variable(*namevalue) && !(sd = script_rid2sd(st)) )
  5351. {// player must be attached in order to copy a player variable
  5352. ShowError("script:set: no player attached for player variable '%s'\n", namevalue);
  5353. return SCRIPT_CMD_SUCCESS;
  5354. }
  5355. if( is_string_variable(namevalue) != is_string_variable(name) )
  5356. {// non-matching array value types
  5357. ShowWarning("script:set: two array variables do not match in type.\n");
  5358. return SCRIPT_CMD_SUCCESS;
  5359. }
  5360. // push the maximum number of array values to the stack
  5361. push_val(st->stack, C_INT, SCRIPT_MAX_ARRAYSIZE);
  5362. // call the copy array method directly
  5363. return buildin_copyarray(st);
  5364. }
  5365. }
  5366. #endif
  5367. if( !strcmp(command, "setr") && script_hasdata(st, 4) ) { // Optional argument used by post-increment/post-decrement constructs to return the previous value
  5368. if( is_string_variable(name) )
  5369. script_pushstrcopy(st,script_getstr(st, 4));
  5370. else
  5371. script_pushint(st,script_getnum(st, 4));
  5372. } else // Return a copy of the variable reference
  5373. script_pushcopy(st, 2);
  5374. if( is_string_variable(name) )
  5375. set_reg(st,sd,num,name,(void*)script_getstr(st,3),script_getref(st,2));
  5376. else
  5377. set_reg(st,sd,num,name,(void*)__64BPRTSIZE(script_getnum(st,3)),script_getref(st,2));
  5378. return SCRIPT_CMD_SUCCESS;
  5379. }
  5380. /////////////////////////////////////////////////////////////////////
  5381. /// Array variables
  5382. ///
  5383. /// Sets values of an array, from the starting index.
  5384. /// ex: setarray arr[1],1,2,3;
  5385. ///
  5386. /// setarray <array variable>,<value1>{,<value2>...};
  5387. BUILDIN_FUNC(setarray)
  5388. {
  5389. struct script_data* data;
  5390. const char* name;
  5391. uint32 start;
  5392. uint32 end;
  5393. int32 id;
  5394. int32 i;
  5395. TBL_PC* sd = NULL;
  5396. data = script_getdata(st, 2);
  5397. if( !data_isreference(data) )
  5398. {
  5399. ShowError("script:setarray: not a variable\n");
  5400. script_reportdata(data);
  5401. st->state = END;
  5402. return SCRIPT_CMD_FAILURE;// not a variable
  5403. }
  5404. id = reference_getid(data);
  5405. start = reference_getindex(data);
  5406. name = reference_getname(data);
  5407. if( not_server_variable(*name) )
  5408. {
  5409. sd = script_rid2sd(st);
  5410. if( sd == NULL )
  5411. return SCRIPT_CMD_SUCCESS;// no player attached
  5412. }
  5413. end = start + script_lastdata(st) - 2;
  5414. if( end > SCRIPT_MAX_ARRAYSIZE )
  5415. end = SCRIPT_MAX_ARRAYSIZE;
  5416. if( is_string_variable(name) )
  5417. {// string array
  5418. for( i = 3; start < end; ++start, ++i )
  5419. set_reg(st, sd, reference_uid(id, start), name, (void*)script_getstr(st,i), reference_getref(data));
  5420. }
  5421. else
  5422. {// int array
  5423. for( i = 3; start < end; ++start, ++i )
  5424. set_reg(st, sd, reference_uid(id, start), name, (void*)__64BPRTSIZE(script_getnum(st,i)), reference_getref(data));
  5425. }
  5426. return SCRIPT_CMD_SUCCESS;
  5427. }
  5428. /// Sets count values of an array, from the starting index.
  5429. /// ex: cleararray arr[0],0,1;
  5430. ///
  5431. /// cleararray <array variable>,<value>,<count>;
  5432. BUILDIN_FUNC(cleararray)
  5433. {
  5434. struct script_data* data;
  5435. const char* name;
  5436. uint32 start;
  5437. uint32 end;
  5438. int32 id;
  5439. void* v;
  5440. TBL_PC* sd = NULL;
  5441. data = script_getdata(st, 2);
  5442. if( !data_isreference(data) )
  5443. {
  5444. ShowError("script:cleararray: not a variable\n");
  5445. script_reportdata(data);
  5446. st->state = END;
  5447. return SCRIPT_CMD_FAILURE;// not a variable
  5448. }
  5449. id = reference_getid(data);
  5450. start = reference_getindex(data);
  5451. name = reference_getname(data);
  5452. if( not_server_variable(*name) )
  5453. {
  5454. sd = script_rid2sd(st);
  5455. if( sd == NULL )
  5456. return SCRIPT_CMD_SUCCESS;// no player attached
  5457. }
  5458. if( is_string_variable(name) )
  5459. v = (void*)script_getstr(st, 3);
  5460. else
  5461. v = (void*)__64BPRTSIZE(script_getnum(st, 3));
  5462. end = start + script_getnum(st, 4);
  5463. if( end > SCRIPT_MAX_ARRAYSIZE )
  5464. end = SCRIPT_MAX_ARRAYSIZE;
  5465. for( ; start < end; ++start )
  5466. set_reg(st, sd, reference_uid(id, start), name, v, script_getref(st,2));
  5467. return SCRIPT_CMD_SUCCESS;
  5468. }
  5469. /// Copies data from one array to another.
  5470. /// ex: copyarray arr[0],arr[2],2;
  5471. ///
  5472. /// copyarray <destination array variable>,<source array variable>,<count>;
  5473. BUILDIN_FUNC(copyarray)
  5474. {
  5475. struct script_data* data1;
  5476. struct script_data* data2;
  5477. const char* name1;
  5478. const char* name2;
  5479. int32 idx1;
  5480. int32 idx2;
  5481. int32 id1;
  5482. int32 id2;
  5483. void* v;
  5484. int32 i;
  5485. uint32 count;
  5486. TBL_PC* sd = NULL;
  5487. data1 = script_getdata(st, 2);
  5488. data2 = script_getdata(st, 3);
  5489. if( !data_isreference(data1) || !data_isreference(data2) )
  5490. {
  5491. ShowError("script:copyarray: not a variable\n");
  5492. script_reportdata(data1);
  5493. script_reportdata(data2);
  5494. st->state = END;
  5495. return SCRIPT_CMD_FAILURE;// not a variable
  5496. }
  5497. id1 = reference_getid(data1);
  5498. id2 = reference_getid(data2);
  5499. idx1 = reference_getindex(data1);
  5500. idx2 = reference_getindex(data2);
  5501. name1 = reference_getname(data1);
  5502. name2 = reference_getname(data2);
  5503. if( is_string_variable(name1) != is_string_variable(name2) )
  5504. {
  5505. ShowError("script:copyarray: type mismatch\n");
  5506. script_reportdata(data1);
  5507. script_reportdata(data2);
  5508. st->state = END;
  5509. return SCRIPT_CMD_FAILURE;// data type mismatch
  5510. }
  5511. if( not_server_variable(*name1) || not_server_variable(*name2) )
  5512. {
  5513. sd = script_rid2sd(st);
  5514. if( sd == NULL )
  5515. return SCRIPT_CMD_SUCCESS;// no player attached
  5516. }
  5517. count = script_getnum(st, 4);
  5518. if( count > SCRIPT_MAX_ARRAYSIZE - idx1 )
  5519. count = SCRIPT_MAX_ARRAYSIZE - idx1;
  5520. if( count <= 0 || (id1 == id2 && idx1 == idx2) )
  5521. return SCRIPT_CMD_SUCCESS;// nothing to copy
  5522. if( id1 == id2 && idx1 > idx2 )
  5523. {// destination might be overlapping the source - copy in reverse order
  5524. for( i = count - 1; i >= 0; --i )
  5525. {
  5526. v = get_val2(st, reference_uid(id2, idx2 + i), reference_getref(data2));
  5527. set_reg(st, sd, reference_uid(id1, idx1 + i), name1, v, reference_getref(data1));
  5528. script_removetop(st, -1, 0);
  5529. }
  5530. }
  5531. else
  5532. {// normal copy
  5533. for( i = 0; i < count; ++i )
  5534. {
  5535. if( idx2 + i < SCRIPT_MAX_ARRAYSIZE )
  5536. {
  5537. v = get_val2(st, reference_uid(id2, idx2 + i), reference_getref(data2));
  5538. set_reg(st, sd, reference_uid(id1, idx1 + i), name1, v, reference_getref(data1));
  5539. script_removetop(st, -1, 0);
  5540. }
  5541. else// out of range - assume ""/0
  5542. set_reg(st, sd, reference_uid(id1, idx1 + i), name1, (is_string_variable(name1)?(void*)"":(void*)0), reference_getref(data1));
  5543. }
  5544. }
  5545. return SCRIPT_CMD_SUCCESS;
  5546. }
  5547. /// Returns the size of the array.
  5548. /// Assumes that everything before the starting index exists.
  5549. /// ex: getarraysize(arr[3])
  5550. ///
  5551. /// getarraysize(<array variable>) -> <int>
  5552. BUILDIN_FUNC(getarraysize)
  5553. {
  5554. struct script_data* data;
  5555. const char* name;
  5556. struct map_session_data* sd = NULL;
  5557. data = script_getdata(st, 2);
  5558. if( !data_isreference(data) )
  5559. {
  5560. ShowError("script:getarraysize: not a variable\n");
  5561. script_reportdata(data);
  5562. script_pushnil(st);
  5563. st->state = END;
  5564. return SCRIPT_CMD_FAILURE;// not a variable
  5565. }
  5566. name = reference_getname(data);
  5567. if( not_server_variable(*name) ){
  5568. sd = script_rid2sd(st);
  5569. if (sd == NULL)
  5570. return SCRIPT_CMD_SUCCESS;// no player attached
  5571. }
  5572. script_pushint(st, script_array_highest_key(st, sd, reference_getname(data), reference_getref(data)));
  5573. return SCRIPT_CMD_SUCCESS;
  5574. }
  5575. int script_array_index_cmp(const void *a, const void *b) {
  5576. return ( *(unsigned int*)a - *(unsigned int*)b );
  5577. }
  5578. /// Deletes count or all the elements in an array, from the starting index.
  5579. /// ex: deletearray arr[4],2;
  5580. ///
  5581. /// deletearray <array variable>;
  5582. /// deletearray <array variable>,<count>;
  5583. BUILDIN_FUNC(deletearray)
  5584. {
  5585. struct script_data* data;
  5586. const char* name;
  5587. unsigned int start, end, i;
  5588. int id;
  5589. TBL_PC *sd = NULL;
  5590. struct script_array *sa = NULL;
  5591. struct reg_db *src = NULL;
  5592. void *value;
  5593. data = script_getdata(st, 2);
  5594. if( !data_isreference(data) ) {
  5595. ShowError("script:deletearray: not a variable\n");
  5596. script_reportdata(data);
  5597. st->state = END;
  5598. return SCRIPT_CMD_FAILURE;// not a variable
  5599. }
  5600. id = reference_getid(data);
  5601. start = reference_getindex(data);
  5602. name = reference_getname(data);
  5603. if( not_server_variable(*name) ) {
  5604. sd = script_rid2sd(st);
  5605. if( sd == NULL )
  5606. return SCRIPT_CMD_SUCCESS;// no player attached
  5607. }
  5608. if (!(src = script_array_src(st, sd, name, reference_getref(data)))) {
  5609. ShowError("script:deletearray: not a array\n");
  5610. script_reportdata(data);
  5611. st->state = END;
  5612. return SCRIPT_CMD_FAILURE;// not a variable
  5613. }
  5614. script_array_ensure_zero(st, NULL, data->u.num, reference_getref(data));
  5615. if ( !(sa = idb_get(src->arrays, id)) ) { // non-existent array, nothing to empty
  5616. return SCRIPT_CMD_SUCCESS;// not a variable
  5617. }
  5618. end = script_array_highest_key(st, sd, name, reference_getref(data));
  5619. if( start >= end )
  5620. return SCRIPT_CMD_SUCCESS;// nothing to free
  5621. if( is_string_variable(name) )
  5622. value = (void *)"";
  5623. else
  5624. value = (void *)0;
  5625. if( script_hasdata(st,3) ) {
  5626. unsigned int count = script_getnum(st, 3);
  5627. if( count > end - start )
  5628. count = end - start;
  5629. if( count <= 0 )
  5630. return SCRIPT_CMD_SUCCESS;// nothing to free
  5631. if( end - start < sa->size ) {
  5632. // Better to iterate directly on the array, no speed-up from using sa
  5633. for( ; start + count < end; ++start ) {
  5634. // Compact and overwrite
  5635. void* v = get_val2(st, reference_uid(id, start + count), reference_getref(data));
  5636. set_reg(st, sd, reference_uid(id, start), name, v, reference_getref(data));
  5637. script_removetop(st, -1, 0);
  5638. }
  5639. for( ; start < end; start++ ) {
  5640. // Clean up any leftovers that weren't overwritten
  5641. set_reg(st, sd, reference_uid(id, start), name, value, reference_getref(data));
  5642. }
  5643. } else {
  5644. // using sa to speed up
  5645. unsigned int *list = NULL, size = 0;
  5646. list = script_array_cpy_list(sa);
  5647. size = sa->size;
  5648. qsort(list, size, sizeof(unsigned int), script_array_index_cmp);
  5649. ARR_FIND(0, size, i, list[i] >= start);
  5650. for( ; i < size && list[i] < start + count; i++ ) {
  5651. // Clear any entries between start and start+count, if they exist
  5652. set_reg(st, sd, reference_uid(id, list[i]), name, value, reference_getref(data));
  5653. }
  5654. for( ; i < size && list[i] < end; i++ ) {
  5655. // Move back count positions any entries between start+count to fill the gaps
  5656. void* v = get_val2(st, reference_uid(id, list[i]), reference_getref(data));
  5657. set_reg(st, sd, reference_uid(id, list[i]-count), name, v, reference_getref(data));
  5658. script_removetop(st, -1, 0);
  5659. // Clear their originals
  5660. set_reg(st, sd, reference_uid(id, list[i]), name, value, reference_getref(data));
  5661. }
  5662. }
  5663. } else {
  5664. unsigned int *list = NULL, size = 0;
  5665. list = script_array_cpy_list(sa);
  5666. size = sa->size;
  5667. for(i = 0; i < size; i++) {
  5668. if( list[i] >= start ) // Less expensive than sorting it, most likely
  5669. set_reg(st, sd, reference_uid(id, list[i]), name, value, reference_getref(data));
  5670. }
  5671. }
  5672. return SCRIPT_CMD_SUCCESS;
  5673. }
  5674. /// Returns a reference to the target index of the array variable.
  5675. /// Equivalent to var[index].
  5676. ///
  5677. /// getelementofarray(<array variable>,<index>) -> <variable reference>
  5678. BUILDIN_FUNC(getelementofarray)
  5679. {
  5680. struct script_data* data;
  5681. int32 id;
  5682. int64 i;
  5683. data = script_getdata(st, 2);
  5684. if( !data_isreference(data) )
  5685. {
  5686. ShowError("script:getelementofarray: not a variable\n");
  5687. script_reportdata(data);
  5688. script_pushnil(st);
  5689. st->state = END;
  5690. return SCRIPT_CMD_SUCCESS;// not a variable
  5691. }
  5692. id = reference_getid(data);
  5693. i = script_getnum(st, 3);
  5694. if (i < 0 || i >= SCRIPT_MAX_ARRAYSIZE) {
  5695. ShowWarning("script:getelementofarray: index out of range (%"PRId64")\n", i);
  5696. script_reportdata(data);
  5697. script_pushnil(st);
  5698. st->state = END;
  5699. return SCRIPT_CMD_FAILURE;// out of range
  5700. }
  5701. push_val2(st->stack, C_NAME, reference_uid(id, i), reference_getref(data));
  5702. return SCRIPT_CMD_SUCCESS;
  5703. }
  5704. /////////////////////////////////////////////////////////////////////
  5705. /// ...
  5706. ///
  5707. /*==========================================
  5708. *
  5709. *------------------------------------------*/
  5710. BUILDIN_FUNC(setlook)
  5711. {
  5712. int type,val;
  5713. TBL_PC* sd;
  5714. type = script_getnum(st,2);
  5715. val = script_getnum(st,3);
  5716. if (!script_charid2sd(4,sd))
  5717. return SCRIPT_CMD_SUCCESS;
  5718. pc_changelook(sd,type,val);
  5719. return SCRIPT_CMD_SUCCESS;
  5720. }
  5721. BUILDIN_FUNC(changelook)
  5722. { // As setlook but only client side
  5723. int type,val;
  5724. TBL_PC* sd;
  5725. type = script_getnum(st,2);
  5726. val = script_getnum(st,3);
  5727. if (!script_charid2sd(4,sd))
  5728. return SCRIPT_CMD_SUCCESS;
  5729. clif_changelook(&sd->bl,type,val);
  5730. return SCRIPT_CMD_SUCCESS;
  5731. }
  5732. /*==========================================
  5733. *
  5734. *------------------------------------------*/
  5735. BUILDIN_FUNC(cutin)
  5736. {
  5737. TBL_PC* sd;
  5738. sd = script_rid2sd(st);
  5739. if( sd == NULL )
  5740. return SCRIPT_CMD_SUCCESS;
  5741. clif_cutin(sd,script_getstr(st,2),script_getnum(st,3));
  5742. return SCRIPT_CMD_SUCCESS;
  5743. }
  5744. /*==========================================
  5745. *
  5746. *------------------------------------------*/
  5747. BUILDIN_FUNC(viewpoint)
  5748. {
  5749. int type,x,y,id,color;
  5750. TBL_PC* sd;
  5751. type=script_getnum(st,2);
  5752. x=script_getnum(st,3);
  5753. y=script_getnum(st,4);
  5754. id=script_getnum(st,5);
  5755. color=script_getnum(st,6);
  5756. sd = script_rid2sd(st);
  5757. if( sd == NULL )
  5758. return SCRIPT_CMD_SUCCESS;
  5759. clif_viewpoint(sd,st->oid,type,x,y,id,color);
  5760. return SCRIPT_CMD_SUCCESS;
  5761. }
  5762. /// Returns number of items in inventory/cart/storage
  5763. /// countitem <nameID>{,<accountID>});
  5764. /// countitem2 <nameID>,<Identified>,<Refine>,<Attribute>,<Card0>,<Card1>,<Card2>,<Card3>{,<accountID>}) [Lupus]
  5765. /// cartcountitem <nameID>{,<accountID>});
  5766. /// cartcountitem2 <nameID>,<Identified>,<Refine>,<Attribute>,<Card0>,<Card1>,<Card2>,<Card3>{,<accountID>})
  5767. /// storagecountitem <nameID>{,<accountID>});
  5768. /// storagecountitem2 <nameID>,<Identified>,<Refine>,<Attribute>,<Card0>,<Card1>,<Card2>,<Card3>{,<accountID>})
  5769. /// guildstoragecountitem <nameID>{,<accountID>});
  5770. /// guildstoragecountitem2 <nameID>,<Identified>,<Refine>,<Attribute>,<Card0>,<Card1>,<Card2>,<Card3>{,<accountID>})
  5771. BUILDIN_FUNC(countitem)
  5772. {
  5773. int i = 0, aid = 3;
  5774. struct item_data* id = NULL;
  5775. struct script_data* data;
  5776. char *command = (char *)script_getfuncname(st);
  5777. uint8 loc = 0;
  5778. uint16 size, count = 0;
  5779. struct item *items;
  5780. TBL_PC *sd = NULL;
  5781. struct s_storage *gstor = NULL;
  5782. if( command[strlen(command)-1] == '2' ) {
  5783. i = 1;
  5784. aid = 10;
  5785. }
  5786. if( script_hasdata(st,aid) ) {
  5787. if( !(sd = map_id2sd( (aid = script_getnum(st, aid)) )) ) {
  5788. ShowError("buildin_%s: player not found (AID=%d).\n", command, aid);
  5789. st->state = END;
  5790. return SCRIPT_CMD_FAILURE;
  5791. }
  5792. }
  5793. else {
  5794. if( !(sd = script_rid2sd(st)) )
  5795. return SCRIPT_CMD_SUCCESS;
  5796. }
  5797. if( !strncmp(command, "cart", 4) ) {
  5798. loc = TABLE_CART;
  5799. size = MAX_CART;
  5800. items = sd->cart.u.items_cart;
  5801. }
  5802. else if( !strncmp(command, "storage", 7) ) {
  5803. loc = TABLE_STORAGE;
  5804. size = MAX_STORAGE;
  5805. items = sd->storage.u.items_storage;
  5806. }
  5807. else if( !strncmp(command, "guildstorage", 12) ) {
  5808. gstor = guild2storage2(sd->status.guild_id);
  5809. if (gstor && !sd->state.storage_flag) {
  5810. loc = TABLE_GUILD_STORAGE;
  5811. size = MAX_GUILD_STORAGE;
  5812. items = gstor->u.items_guild;
  5813. } else {
  5814. script_pushint(st, -1);
  5815. return SCRIPT_CMD_SUCCESS;
  5816. }
  5817. }
  5818. else {
  5819. size = MAX_INVENTORY;
  5820. items = sd->inventory.u.items_inventory;
  5821. }
  5822. if( loc == TABLE_CART && !pc_iscarton(sd) ) {
  5823. ShowError("buildin_%s: Player doesn't have cart (CID:%d).\n", command, sd->status.char_id);
  5824. script_pushint(st,-1);
  5825. return SCRIPT_CMD_SUCCESS;
  5826. }
  5827. data = script_getdata(st, 2);
  5828. get_val(st, data); // Convert into value in case of a variable
  5829. if( data_isstring(data) ) // item name
  5830. id = itemdb_searchname(conv_str(st, data));
  5831. else // item id
  5832. id = itemdb_exists(conv_num(st, data));
  5833. if( id == NULL ) {
  5834. ShowError("buildin_%s: Invalid item '%s'.\n", command, script_getstr(st,2)); // returns string, regardless of what it was
  5835. script_pushint(st,0);
  5836. return SCRIPT_CMD_FAILURE;
  5837. }
  5838. if (loc == TABLE_GUILD_STORAGE)
  5839. gstor->lock = true;
  5840. if( !i ) { // For count/cart/storagecountitem function
  5841. unsigned short nameid = id->nameid;
  5842. for( i = 0; i < size; i++ )
  5843. if( &items[i] && items[i].nameid == nameid )
  5844. count += items[i].amount;
  5845. }
  5846. else { // For count/cart/storagecountitem2 function
  5847. unsigned short nameid;
  5848. int iden, ref, attr, c1, c2, c3, c4;
  5849. nameid = id->nameid;
  5850. iden = script_getnum(st,3);
  5851. ref = script_getnum(st,4);
  5852. attr = script_getnum(st,5);
  5853. c1 = script_getnum(st,6);
  5854. c2 = script_getnum(st,7);
  5855. c3 = script_getnum(st,8);
  5856. c4 = script_getnum(st,9);
  5857. for( i = 0; i < size; i++ )
  5858. if( &items[i] && items[i].nameid > 0 && items[i].nameid == nameid &&
  5859. items[i].amount > 0 && items[i].identify == iden &&
  5860. items[i].refine == ref && items[i].attribute == attr &&
  5861. items[i].card[0] == c1 && items[i].card[1] == c2 &&
  5862. items[i].card[2] == c3 && items[i].card[3] == c4 )
  5863. {
  5864. count += items[i].amount;
  5865. }
  5866. }
  5867. if (loc == TABLE_GUILD_STORAGE) {
  5868. storage_guild_storageclose(sd);
  5869. gstor->lock = false;
  5870. }
  5871. script_pushint(st, count);
  5872. return SCRIPT_CMD_SUCCESS;
  5873. }
  5874. /*==========================================
  5875. * Check if item with this amount can fit in inventory
  5876. * Checking : weight, stack amount >(MAX_AMOUNT), slots amount >(MAX_INVENTORY)
  5877. * Return
  5878. * 0 : fail
  5879. * 1 : success (npc side only)
  5880. *------------------------------------------*/
  5881. BUILDIN_FUNC(checkweight)
  5882. {
  5883. int slots = 0;
  5884. unsigned short amount2 = 0;
  5885. unsigned int weight = 0, i, nbargs;
  5886. struct item_data* id = NULL;
  5887. struct map_session_data* sd;
  5888. if( ( sd = script_rid2sd(st) ) == NULL )
  5889. return SCRIPT_CMD_SUCCESS;
  5890. nbargs = script_lastdata(st)+1;
  5891. if(nbargs%2) {
  5892. ShowError("buildin_checkweight: Invalid nb of args should be a multiple of 2.\n");
  5893. script_pushint(st,0);
  5894. return SCRIPT_CMD_FAILURE;
  5895. }
  5896. slots = pc_inventoryblank(sd); //nb of empty slot
  5897. for (i = 2; i < nbargs; i += 2) {
  5898. unsigned short nameid, amount;
  5899. struct script_data* data = script_getdata(st,i);
  5900. get_val(st, data); // Convert into value in case of a variable
  5901. if( data_isstring(data) ) // item name
  5902. id = itemdb_searchname(conv_str(st, data));
  5903. else // item id
  5904. id = itemdb_exists(conv_num(st, data));
  5905. if( id == NULL ) {
  5906. ShowError("buildin_checkweight: Invalid item '%s'.\n", script_getstr(st,i)); // returns string, regardless of what it was
  5907. script_pushint(st,0);
  5908. return SCRIPT_CMD_FAILURE;
  5909. }
  5910. nameid = id->nameid;
  5911. amount = script_getnum(st,i+1);
  5912. if( amount < 1 ) {
  5913. ShowError("buildin_checkweight: Invalid amount '%d'.\n", amount);
  5914. script_pushint(st,0);
  5915. return SCRIPT_CMD_FAILURE;
  5916. }
  5917. weight += itemdb_weight(nameid)*amount; //total weight for all chk
  5918. if( weight + sd->weight > sd->max_weight )
  5919. {// too heavy
  5920. script_pushint(st,0);
  5921. return SCRIPT_CMD_SUCCESS;
  5922. }
  5923. switch( pc_checkadditem(sd, nameid, amount) ) {
  5924. case CHKADDITEM_EXIST:
  5925. // item is already in inventory, but there is still space for the requested amount
  5926. break;
  5927. case CHKADDITEM_NEW:
  5928. if( itemdb_isstackable(nameid) ) {
  5929. // stackable
  5930. amount2++;
  5931. if( slots < amount2 ) {
  5932. script_pushint(st,0);
  5933. return SCRIPT_CMD_SUCCESS;
  5934. }
  5935. } else {
  5936. // non-stackable
  5937. amount2 += amount;
  5938. if( slots < amount2) {
  5939. script_pushint(st,0);
  5940. return SCRIPT_CMD_SUCCESS;
  5941. }
  5942. }
  5943. break;
  5944. case CHKADDITEM_OVERAMOUNT:
  5945. script_pushint(st,0);
  5946. return SCRIPT_CMD_SUCCESS;
  5947. }
  5948. }
  5949. script_pushint(st,1);
  5950. return SCRIPT_CMD_SUCCESS;
  5951. }
  5952. BUILDIN_FUNC(checkweight2)
  5953. {
  5954. //variable sub checkweight
  5955. int i = 0, slots = 0, weight = 0;
  5956. short fail = 0;
  5957. unsigned short amount2 = 0;
  5958. //variable for array parsing
  5959. struct script_data* data_it;
  5960. struct script_data* data_nb;
  5961. const char* name_it;
  5962. const char* name_nb;
  5963. int32 id_it, id_nb;
  5964. int32 idx_it, idx_nb;
  5965. int nb_it, nb_nb; //array size
  5966. TBL_PC *sd = script_rid2sd(st);
  5967. nullpo_retr(1,sd);
  5968. data_it = script_getdata(st, 2);
  5969. data_nb = script_getdata(st, 3);
  5970. if( !data_isreference(data_it) || !data_isreference(data_nb)) {
  5971. ShowError("buildin_checkweight2: parameter not a variable\n");
  5972. script_pushint(st,0);
  5973. return SCRIPT_CMD_FAILURE;// not a variable
  5974. }
  5975. id_it = reference_getid(data_it);
  5976. id_nb = reference_getid(data_nb);
  5977. idx_it = reference_getindex(data_it);
  5978. idx_nb = reference_getindex(data_nb);
  5979. name_it = reference_getname(data_it);
  5980. name_nb = reference_getname(data_nb);
  5981. if(is_string_variable(name_it) || is_string_variable(name_nb)) {
  5982. ShowError("buildin_checkweight2: illegal type, need int\n");
  5983. script_pushint(st,0);
  5984. return SCRIPT_CMD_FAILURE;// not supported
  5985. }
  5986. nb_it = script_array_highest_key(st, sd, reference_getname(data_it), reference_getref(data_it));
  5987. nb_nb = script_array_highest_key(st, sd, reference_getname(data_nb), reference_getref(data_nb));
  5988. if(nb_it != nb_nb) {
  5989. ShowError("buildin_checkweight2: Size mistmatch: nb_it=%d, nb_nb=%d\n",nb_it,nb_nb);
  5990. fail = 1;
  5991. }
  5992. slots = pc_inventoryblank(sd);
  5993. for(i=0; i<nb_it; i++) {
  5994. unsigned short amount, nameid = (int32)__64BPRTSIZE(get_val2(st,reference_uid(id_it,idx_it+i),reference_getref(data_it)));
  5995. script_removetop(st, -1, 0);
  5996. amount = (int32)__64BPRTSIZE(get_val2(st,reference_uid(id_nb,idx_nb+i),reference_getref(data_nb)));
  5997. script_removetop(st, -1, 0);
  5998. if(fail)
  5999. continue; //cpntonie to depop rest
  6000. if(itemdb_exists(nameid) == NULL ) {
  6001. ShowError("buildin_checkweight2: Invalid item '%d'.\n", nameid);
  6002. fail=1;
  6003. continue;
  6004. }
  6005. if(amount < 0 ) {
  6006. ShowError("buildin_checkweight2: Invalid amount '%d'.\n", amount);
  6007. fail = 1;
  6008. continue;
  6009. }
  6010. weight += itemdb_weight(nameid)*amount;
  6011. if( weight + sd->weight > sd->max_weight ) {
  6012. fail = 1;
  6013. continue;
  6014. }
  6015. switch( pc_checkadditem(sd, nameid, amount) ) {
  6016. case CHKADDITEM_EXIST:
  6017. // item is already in inventory, but there is still space for the requested amount
  6018. break;
  6019. case CHKADDITEM_NEW:
  6020. if( itemdb_isstackable(nameid) ) {// stackable
  6021. amount2++;
  6022. if( slots < amount2 )
  6023. fail = 1;
  6024. } else {// non-stackable
  6025. amount2 += amount;
  6026. if( slots < amount2 ) {
  6027. fail = 1;
  6028. }
  6029. }
  6030. break;
  6031. case CHKADDITEM_OVERAMOUNT:
  6032. fail = 1;
  6033. } //end switch
  6034. } //end loop DO NOT break it prematurly we need to depop all stack
  6035. fail ? script_pushint(st,0) : script_pushint(st,1);
  6036. return SCRIPT_CMD_SUCCESS;
  6037. }
  6038. /*==========================================
  6039. * getitem <item id>,<amount>{,<account ID>};
  6040. * getitem "<item name>",<amount>{,<account ID>};
  6041. *
  6042. * getitembound <item id>,<amount>,<type>{,<account ID>};
  6043. * getitembound "<item id>",<amount>,<type>{,<account ID>};
  6044. * Type:
  6045. * 0 - No bound
  6046. * 1 - Account Bound
  6047. * 2 - Guild Bound
  6048. * 3 - Party Bound
  6049. * 4 - Character Bound
  6050. *------------------------------------------*/
  6051. BUILDIN_FUNC(getitem)
  6052. {
  6053. int get_count, i;
  6054. unsigned short nameid, amount;
  6055. struct item it;
  6056. TBL_PC *sd;
  6057. struct script_data *data;
  6058. unsigned char flag = 0;
  6059. const char* command = script_getfuncname(st);
  6060. struct item_data *id = NULL;
  6061. data = script_getdata(st,2);
  6062. get_val(st,data);
  6063. if( data_isstring(data) ) {// "<item name>"
  6064. const char *name = conv_str(st,data);
  6065. id = itemdb_searchname(name);
  6066. if( id == NULL ){
  6067. ShowError("buildin_getitem: Nonexistant item %s requested.\n", name);
  6068. return SCRIPT_CMD_FAILURE; //No item created.
  6069. }
  6070. nameid = id->nameid;
  6071. } else if( data_isint(data) ) {// <item id>
  6072. nameid = conv_num(st,data);
  6073. if( !(id = itemdb_exists(nameid)) ){
  6074. ShowError("buildin_getitem: Nonexistant item %d requested.\n", nameid);
  6075. return SCRIPT_CMD_FAILURE; //No item created.
  6076. }
  6077. } else {
  6078. ShowError("buildin_getitem: invalid data type for argument #1 (%d).", data->type);
  6079. return SCRIPT_CMD_FAILURE;
  6080. }
  6081. // <amount>
  6082. if( (amount = script_getnum(st,3)) <= 0)
  6083. return SCRIPT_CMD_SUCCESS; //return if amount <=0, skip the useles iteration
  6084. memset(&it,0,sizeof(it));
  6085. it.nameid = nameid;
  6086. it.identify = 1;
  6087. it.bound = BOUND_NONE;
  6088. if( !strcmp(command,"getitembound") ) {
  6089. char bound = script_getnum(st,4);
  6090. if( bound < BOUND_NONE || bound >= BOUND_MAX ) {
  6091. ShowError("script_getitembound: Not a correct bound type! Type=%d\n",bound);
  6092. return SCRIPT_CMD_FAILURE;
  6093. }
  6094. if( script_hasdata(st,5) )
  6095. sd = map_id2sd(script_getnum(st,5));
  6096. else
  6097. sd = script_rid2sd(st); // Attached player
  6098. it.bound = bound;
  6099. } else if( script_hasdata(st,4) )
  6100. sd = map_id2sd(script_getnum(st,4)); // <Account ID>
  6101. else
  6102. sd = script_rid2sd(st); // Attached player
  6103. if( sd == NULL ) // no target
  6104. return SCRIPT_CMD_SUCCESS;
  6105. //Check if it's stackable.
  6106. if (!itemdb_isstackable2(id))
  6107. get_count = 1;
  6108. else
  6109. get_count = amount;
  6110. for (i = 0; i < amount; i += get_count)
  6111. {
  6112. // if not pet egg
  6113. if (!pet_create_egg(sd, nameid))
  6114. {
  6115. if ((flag = pc_additem(sd, &it, get_count, LOG_TYPE_SCRIPT)))
  6116. {
  6117. clif_additem(sd, 0, 0, flag);
  6118. if( pc_candrop(sd,&it) )
  6119. map_addflooritem(&it,get_count,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0,0);
  6120. }
  6121. }
  6122. }
  6123. return SCRIPT_CMD_SUCCESS;
  6124. }
  6125. /*==========================================
  6126. * getitem2 <item id>,<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>{,<account ID>};
  6127. * getitem2 "<item name>",<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>{,<account ID>};
  6128. *
  6129. * getitembound2 <item id>,<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>,<bound type>{,<account ID>};
  6130. * getitembound2 "<item name>",<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>,<bound type>{,<account ID>};
  6131. * Type:
  6132. * 0 - No bound
  6133. * 1 - Account Bound
  6134. * 2 - Guild Bound
  6135. * 3 - Party Bound
  6136. * 4 - Character Bound
  6137. *------------------------------------------*/
  6138. BUILDIN_FUNC(getitem2)
  6139. {
  6140. unsigned short nameid, amount;
  6141. int iden, ref, attr;
  6142. unsigned short c1, c2, c3, c4;
  6143. char bound = BOUND_NONE;
  6144. struct item_data *item_data = NULL;
  6145. struct item item_tmp;
  6146. TBL_PC *sd;
  6147. struct script_data *data;
  6148. const char* command = script_getfuncname(st);
  6149. if( !strcmp(command,"getitembound2") ) {
  6150. bound = script_getnum(st,11);
  6151. if( bound < BOUND_NONE || bound >= BOUND_MAX ) {
  6152. ShowError("script_getitembound2: Not a correct bound type! Type=%d\n",bound);
  6153. return SCRIPT_CMD_FAILURE;
  6154. }
  6155. if( script_hasdata(st,12) )
  6156. sd = map_id2sd(script_getnum(st,12));
  6157. else
  6158. sd = script_rid2sd(st); // Attached player
  6159. } else if( script_hasdata(st,11) )
  6160. sd = map_id2sd(script_getnum(st,11)); // <Account ID>
  6161. else
  6162. sd = script_rid2sd(st); // Attached player
  6163. if( sd == NULL ) // no target
  6164. return SCRIPT_CMD_SUCCESS;
  6165. data = script_getdata(st,2);
  6166. get_val(st,data);
  6167. if( data_isstring(data) ) {
  6168. const char *name = conv_str(st,data);
  6169. if( (item_data = itemdb_searchname(name)) == NULL ){
  6170. ShowError("buildin_getitem2: Nonexistant item %s requested (by conv_str).\n", name);
  6171. return SCRIPT_CMD_FAILURE; //No item created.
  6172. }
  6173. nameid = item_data->nameid;
  6174. } else {
  6175. nameid = conv_num(st,data);
  6176. if( (item_data = itemdb_exists(nameid)) == NULL ){
  6177. ShowError("buildin_getitem2: Nonexistant item %d requested (by conv_num).\n", nameid);
  6178. return SCRIPT_CMD_FAILURE; //No item created.
  6179. }
  6180. }
  6181. amount = script_getnum(st,3);
  6182. iden = script_getnum(st,4);
  6183. ref = script_getnum(st,5);
  6184. attr = script_getnum(st,6);
  6185. c1 = (unsigned short)script_getnum(st,7);
  6186. c2 = (unsigned short)script_getnum(st,8);
  6187. c3 = (unsigned short)script_getnum(st,9);
  6188. c4 = (unsigned short)script_getnum(st,10);
  6189. if( item_data ) {
  6190. int get_count = 0, i;
  6191. memset(&item_tmp,0,sizeof(item_tmp));
  6192. if( item_data->type == IT_WEAPON || item_data->type == IT_ARMOR || item_data->type == IT_SHADOWGEAR ) {
  6193. if(ref > MAX_REFINE)
  6194. ref = MAX_REFINE;
  6195. }
  6196. else if( item_data->type == IT_PETEGG ) {
  6197. iden = 1;
  6198. ref = 0;
  6199. }
  6200. else {
  6201. iden = 1;
  6202. ref = attr = 0;
  6203. }
  6204. item_tmp.nameid = nameid;
  6205. item_tmp.identify = iden;
  6206. item_tmp.refine = ref;
  6207. item_tmp.attribute = attr;
  6208. item_tmp.card[0] = c1;
  6209. item_tmp.card[1] = c2;
  6210. item_tmp.card[2] = c3;
  6211. item_tmp.card[3] = c4;
  6212. item_tmp.bound = bound;
  6213. //Check if it's stackable.
  6214. if (!itemdb_isstackable2(item_data))
  6215. get_count = 1;
  6216. else
  6217. get_count = amount;
  6218. for (i = 0; i < amount; i += get_count)
  6219. {
  6220. // if not pet egg
  6221. if (!pet_create_egg(sd, nameid))
  6222. {
  6223. unsigned char flag = 0;
  6224. if ((flag = pc_additem(sd, &item_tmp, get_count, LOG_TYPE_SCRIPT)))
  6225. {
  6226. clif_additem(sd, 0, 0, flag);
  6227. if( pc_candrop(sd,&item_tmp) )
  6228. map_addflooritem(&item_tmp,get_count,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0,0);
  6229. }
  6230. }
  6231. }
  6232. }
  6233. return SCRIPT_CMD_SUCCESS;
  6234. }
  6235. /** Gives rental item to player
  6236. * rentitem <item id>,<seconds>{,<account_id>}
  6237. * rentitem "<item name>",<seconds>{,<account_id>}
  6238. */
  6239. BUILDIN_FUNC(rentitem) {
  6240. struct map_session_data *sd;
  6241. struct script_data *data;
  6242. struct item it;
  6243. int seconds;
  6244. unsigned short nameid = 0;
  6245. unsigned char flag = 0;
  6246. data = script_getdata(st,2);
  6247. get_val(st,data);
  6248. if (!script_accid2sd(4,sd))
  6249. return SCRIPT_CMD_FAILURE;
  6250. if( data_isstring(data) )
  6251. {
  6252. const char *name = conv_str(st,data);
  6253. struct item_data *itd = itemdb_searchname(name);
  6254. if( itd == NULL )
  6255. {
  6256. ShowError("buildin_rentitem: Nonexistant item %s requested.\n", name);
  6257. return SCRIPT_CMD_FAILURE;
  6258. }
  6259. nameid = itd->nameid;
  6260. }
  6261. else if( data_isint(data) )
  6262. {
  6263. nameid = conv_num(st,data);
  6264. if( nameid == 0 || !itemdb_exists(nameid) )
  6265. {
  6266. ShowError("buildin_rentitem: Nonexistant item %hu requested.\n", nameid);
  6267. return SCRIPT_CMD_FAILURE;
  6268. }
  6269. }
  6270. else
  6271. {
  6272. ShowError("buildin_rentitem: invalid data type for argument #1 (%d).\n", data->type);
  6273. return SCRIPT_CMD_FAILURE;
  6274. }
  6275. seconds = script_getnum(st,3);
  6276. memset(&it, 0, sizeof(it));
  6277. it.nameid = nameid;
  6278. it.identify = 1;
  6279. it.expire_time = (unsigned int)(time(NULL) + seconds);
  6280. it.bound = BOUND_NONE;
  6281. if( (flag = pc_additem(sd, &it, 1, LOG_TYPE_SCRIPT)) )
  6282. {
  6283. clif_additem(sd, 0, 0, flag);
  6284. return SCRIPT_CMD_FAILURE;
  6285. }
  6286. return SCRIPT_CMD_SUCCESS;
  6287. }
  6288. /** Gives rental item to player with advanced option
  6289. * rentitem2 <item id>,<time>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>{,<account_id>};
  6290. * rentitem2 "<item name>",<time>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>{,<account_id>};
  6291. */
  6292. BUILDIN_FUNC(rentitem2) {
  6293. struct map_session_data *sd;
  6294. struct script_data *data;
  6295. struct item it;
  6296. struct item_data *id;
  6297. int seconds;
  6298. unsigned short nameid = 0;
  6299. unsigned char flag = 0;
  6300. int iden,ref,attr,c1,c2,c3,c4;
  6301. data = script_getdata(st,2);
  6302. get_val(st,data);
  6303. if (!script_accid2sd(11,sd))
  6304. return SCRIPT_CMD_FAILURE;
  6305. if( data_isstring(data) ) {
  6306. const char *name = conv_str(st,data);
  6307. id = itemdb_searchname(name);
  6308. if( id == NULL ) {
  6309. ShowError("buildin_rentitem2: Nonexistant item %s requested.\n", name);
  6310. return SCRIPT_CMD_FAILURE;
  6311. }
  6312. nameid = id->nameid;
  6313. }
  6314. else if( data_isint(data) ) {
  6315. nameid = conv_num(st,data);
  6316. if( !(id = itemdb_search(nameid))) {
  6317. ShowError("buildin_rentitem2: Nonexistant item %hu requested.\n", nameid);
  6318. return SCRIPT_CMD_FAILURE;
  6319. }
  6320. }
  6321. else {
  6322. ShowError("buildin_rentitem2: invalid data type for argument #1 (%d).\n", data->type);
  6323. return SCRIPT_CMD_FAILURE;
  6324. }
  6325. seconds = script_getnum(st,3);
  6326. iden = script_getnum(st,4);
  6327. ref = script_getnum(st,5);
  6328. attr = script_getnum(st,6);
  6329. if (id->type==IT_WEAPON || id->type==IT_ARMOR || id->type==IT_SHADOWGEAR) {
  6330. if(ref > MAX_REFINE) ref = MAX_REFINE;
  6331. }
  6332. else if (id->type==IT_PETEGG) {
  6333. iden = 1;
  6334. ref = 0;
  6335. }
  6336. else {
  6337. iden = 1;
  6338. ref = attr = 0;
  6339. }
  6340. c1 = (short)script_getnum(st,7);
  6341. c2 = (short)script_getnum(st,8);
  6342. c3 = (short)script_getnum(st,9);
  6343. c4 = (short)script_getnum(st,10);
  6344. memset(&it, 0, sizeof(it));
  6345. it.nameid = nameid;
  6346. it.identify = iden;
  6347. it.refine = ref;
  6348. it.attribute = attr;
  6349. it.card[0] = (short)c1;
  6350. it.card[1] = (short)c2;
  6351. it.card[2] = (short)c3;
  6352. it.card[3] = (short)c4;
  6353. it.expire_time = (unsigned int)(time(NULL) + seconds);
  6354. if( (flag = pc_additem(sd, &it, 1, LOG_TYPE_SCRIPT)) ) {
  6355. clif_additem(sd, 0, 0, flag);
  6356. return SCRIPT_CMD_FAILURE;
  6357. }
  6358. return SCRIPT_CMD_SUCCESS;
  6359. }
  6360. /*==========================================
  6361. * gets an item with someone's name inscribed [Skotlex]
  6362. * getinscribeditem item_num, character_name
  6363. * Returned Qty is always 1, only works on equip-able
  6364. * equipment
  6365. *------------------------------------------*/
  6366. BUILDIN_FUNC(getnameditem)
  6367. {
  6368. unsigned short nameid;
  6369. struct item item_tmp;
  6370. TBL_PC *sd, *tsd;
  6371. struct script_data *data;
  6372. sd = script_rid2sd(st);
  6373. if (sd == NULL)
  6374. { //Player not attached!
  6375. script_pushint(st,0);
  6376. return SCRIPT_CMD_SUCCESS;
  6377. }
  6378. data=script_getdata(st,2);
  6379. get_val(st,data);
  6380. if( data_isstring(data) ){
  6381. const char *name=conv_str(st,data);
  6382. struct item_data *item_data = itemdb_searchname(name);
  6383. if( item_data == NULL)
  6384. { //Failed
  6385. script_pushint(st,0);
  6386. return SCRIPT_CMD_SUCCESS;
  6387. }
  6388. nameid = item_data->nameid;
  6389. }else
  6390. nameid = conv_num(st,data);
  6391. if(!itemdb_exists(nameid)/* || itemdb_isstackable(nameid)*/)
  6392. { //Even though named stackable items "could" be risky, they are required for certain quests.
  6393. script_pushint(st,0);
  6394. return SCRIPT_CMD_SUCCESS;
  6395. }
  6396. data=script_getdata(st,3);
  6397. get_val(st,data);
  6398. if( data_isstring(data) ) //Char Name
  6399. tsd=map_nick2sd(conv_str(st,data),false);
  6400. else //Char Id was given
  6401. tsd=map_charid2sd(conv_num(st,data));
  6402. if( tsd == NULL )
  6403. { //Failed
  6404. script_pushint(st,0);
  6405. return SCRIPT_CMD_SUCCESS;
  6406. }
  6407. memset(&item_tmp,0,sizeof(item_tmp));
  6408. item_tmp.nameid=nameid;
  6409. item_tmp.amount=1;
  6410. item_tmp.identify=1;
  6411. item_tmp.card[0]=CARD0_CREATE; //we don't use 255! because for example SIGNED WEAPON shouldn't get TOP10 BS Fame bonus [Lupus]
  6412. item_tmp.card[2]=tsd->status.char_id;
  6413. item_tmp.card[3]=tsd->status.char_id >> 16;
  6414. if(pc_additem(sd,&item_tmp,1,LOG_TYPE_SCRIPT)) {
  6415. script_pushint(st,0);
  6416. return SCRIPT_CMD_SUCCESS; //Failed to add item, we will not drop if they don't fit
  6417. }
  6418. script_pushint(st,1);
  6419. return SCRIPT_CMD_SUCCESS;
  6420. }
  6421. /*==========================================
  6422. * gets a random item ID from an item group [Skotlex]
  6423. * groupranditem <group_num>{,<sub_group>};
  6424. *------------------------------------------*/
  6425. BUILDIN_FUNC(grouprandomitem) {
  6426. struct s_item_group_entry *entry = NULL;
  6427. int sub_group = 1;
  6428. FETCH(3, sub_group);
  6429. entry = itemdb_get_randgroupitem(script_getnum(st,2),sub_group);
  6430. if (!entry) {
  6431. ShowError("buildin_grouprandomitem: Invalid item group with group_id '%d', sub_group '%d'.\n", script_getnum(st,2), sub_group);
  6432. script_pushint(st,UNKNOWN_ITEM_ID);
  6433. return SCRIPT_CMD_FAILURE;
  6434. }
  6435. script_pushint(st,entry->nameid);
  6436. return SCRIPT_CMD_SUCCESS;
  6437. }
  6438. /**
  6439. * makeitem <item id>,<amount>,"<map name>",<X>,<Y>;
  6440. * makeitem "<item name>",<amount>,"<map name>",<X>,<Y>;
  6441. */
  6442. BUILDIN_FUNC(makeitem) {
  6443. uint16 nameid, amount, flag = 0, x, y;
  6444. const char *mapname;
  6445. int m;
  6446. struct item item_tmp;
  6447. struct script_data *data;
  6448. data = script_getdata(st,2);
  6449. get_val(st,data);
  6450. if( data_isstring(data) ){
  6451. const char *name = conv_str(st,data);
  6452. struct item_data *item_data = itemdb_searchname(name);
  6453. if( item_data )
  6454. nameid = item_data->nameid;
  6455. else
  6456. nameid = UNKNOWN_ITEM_ID;
  6457. }
  6458. else
  6459. nameid = conv_num(st,data);
  6460. amount = script_getnum(st,3);
  6461. mapname = script_getstr(st,4);
  6462. x = script_getnum(st,5);
  6463. y = script_getnum(st,6);
  6464. if(strcmp(mapname,"this")==0) {
  6465. TBL_PC *sd;
  6466. sd = script_rid2sd(st);
  6467. if (!sd)
  6468. return SCRIPT_CMD_SUCCESS; //Failed...
  6469. m = sd->bl.m;
  6470. } else
  6471. m = map_mapname2mapid(mapname);
  6472. if(nameid<0) {
  6473. nameid = -nameid;
  6474. flag = 1;
  6475. }
  6476. if(nameid > 0) {
  6477. memset(&item_tmp,0,sizeof(item_tmp));
  6478. item_tmp.nameid = nameid;
  6479. if(!flag)
  6480. item_tmp.identify = 1;
  6481. else
  6482. item_tmp.identify = itemdb_isidentified(nameid);
  6483. map_addflooritem(&item_tmp,amount,m,x,y,0,0,0,4,0);
  6484. }
  6485. return SCRIPT_CMD_SUCCESS;
  6486. }
  6487. /**
  6488. * makeitem2 <item id>,<amount>,"<map name>",<X>,<Y>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>;
  6489. * makeitem2 "<item name>",<amount>,"<map name>",<X>,<Y>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>;
  6490. */
  6491. BUILDIN_FUNC(makeitem2) {
  6492. uint16 nameid, amount, x, y;
  6493. const char *mapname;
  6494. int m;
  6495. struct item item_tmp;
  6496. struct script_data *data;
  6497. struct item_data *id;
  6498. data = script_getdata(st,2);
  6499. get_val(st,data);
  6500. if( data_isstring(data) ){
  6501. const char *name = conv_str(st,data);
  6502. struct item_data *item_data = itemdb_searchname(name);
  6503. if( item_data )
  6504. nameid = item_data->nameid;
  6505. else
  6506. nameid = UNKNOWN_ITEM_ID;
  6507. }
  6508. else
  6509. nameid = conv_num(st,data);
  6510. amount = script_getnum(st,3);
  6511. mapname = script_getstr(st,4);
  6512. x = script_getnum(st,5);
  6513. y = script_getnum(st,6);
  6514. if (strcmp(mapname,"this")==0) {
  6515. TBL_PC *sd;
  6516. sd = script_rid2sd(st);
  6517. if (!sd)
  6518. return SCRIPT_CMD_SUCCESS; //Failed...
  6519. m = sd->bl.m;
  6520. }
  6521. else
  6522. m = map_mapname2mapid(mapname);
  6523. if ((id = itemdb_search(nameid))) {
  6524. char iden, ref, attr;
  6525. memset(&item_tmp,0,sizeof(item_tmp));
  6526. item_tmp.nameid = nameid;
  6527. iden = (char)script_getnum(st,7);
  6528. ref = (char)script_getnum(st,8);
  6529. attr = (char)script_getnum(st,9);
  6530. if (id->type==IT_WEAPON || id->type==IT_ARMOR || id->type==IT_SHADOWGEAR) {
  6531. if(ref > MAX_REFINE) ref = MAX_REFINE;
  6532. }
  6533. else if (id->type==IT_PETEGG) {
  6534. iden = 1;
  6535. ref = 0;
  6536. }
  6537. else {
  6538. iden = 1;
  6539. ref = attr = 0;
  6540. }
  6541. item_tmp.identify = iden;
  6542. item_tmp.refine = ref;
  6543. item_tmp.attribute = attr;
  6544. item_tmp.card[0] = script_getnum(st,10);
  6545. item_tmp.card[1] = script_getnum(st,11);
  6546. item_tmp.card[2] = script_getnum(st,12);
  6547. item_tmp.card[3] = script_getnum(st,13);
  6548. map_addflooritem(&item_tmp,amount,m,x,y,0,0,0,4,0);
  6549. }
  6550. else
  6551. return SCRIPT_CMD_FAILURE;
  6552. return SCRIPT_CMD_SUCCESS;
  6553. }
  6554. /// Counts / deletes the current item given by idx.
  6555. /// Used by buildin_delitem_search
  6556. /// Relies on all input data being already fully valid.
  6557. static void buildin_delitem_delete(struct map_session_data* sd, int idx, int* amount, uint8 loc, bool delete_items)
  6558. {
  6559. int delamount;
  6560. struct item *itm = NULL;
  6561. struct s_storage *gstor = NULL;
  6562. switch(loc) {
  6563. case TABLE_CART:
  6564. itm = &sd->cart.u.items_cart[idx];
  6565. break;
  6566. case TABLE_STORAGE:
  6567. itm = &sd->storage.u.items_storage[idx];
  6568. break;
  6569. case TABLE_GUILD_STORAGE:
  6570. {
  6571. gstor = guild2storage2(sd->status.guild_id);
  6572. itm = &gstor->u.items_guild[idx];
  6573. }
  6574. break;
  6575. default: // TABLE_INVENTORY
  6576. itm = &sd->inventory.u.items_inventory[idx];
  6577. break;
  6578. }
  6579. delamount = ( amount[0] < itm->amount ) ? amount[0] : itm->amount;
  6580. if( delete_items )
  6581. {
  6582. if( itemdb_type(itm->nameid) == IT_PETEGG && itm->card[0] == CARD0_PET )
  6583. {// delete associated pet
  6584. intif_delete_petdata(MakeDWord(itm->card[1], itm->card[2]));
  6585. }
  6586. switch(loc) {
  6587. case TABLE_CART:
  6588. pc_cart_delitem(sd,idx,delamount,0,LOG_TYPE_SCRIPT);
  6589. break;
  6590. case TABLE_STORAGE:
  6591. storage_delitem(sd,&sd->storage,idx,delamount);
  6592. log_pick_pc(sd,LOG_TYPE_SCRIPT,-delamount,itm);
  6593. break;
  6594. case TABLE_GUILD_STORAGE:
  6595. gstor->lock = true;
  6596. storage_guild_delitem(sd, gstor, idx, delamount);
  6597. log_pick_pc(sd, LOG_TYPE_SCRIPT, -delamount, itm);
  6598. storage_guild_storageclose(sd);
  6599. gstor->lock = false;
  6600. break;
  6601. default: // TABLE_INVENTORY
  6602. pc_delitem(sd, idx, delamount, 0, 0, LOG_TYPE_SCRIPT);
  6603. break;
  6604. }
  6605. }
  6606. amount[0]-= delamount;
  6607. }
  6608. /// Searches for item(s) and checks, if there is enough of them.
  6609. /// Used by delitem and delitem2
  6610. /// Relies on all input data being already fully valid.
  6611. /// @param exact_match will also match item attributes and cards, not just name id
  6612. /// @return true when all items could be deleted, false when there were not enough items to delete
  6613. static bool buildin_delitem_search(struct map_session_data* sd, struct item* it, bool exact_match, uint8 loc)
  6614. {
  6615. bool delete_items = false;
  6616. int i, amount, size;
  6617. struct item *items;
  6618. // prefer always non-equipped items
  6619. it->equip = 0;
  6620. // when searching for nameid only, prefer additionally
  6621. if( !exact_match )
  6622. {
  6623. // non-refined items
  6624. it->refine = 0;
  6625. // card-less items
  6626. memset(it->card, 0, sizeof(it->card));
  6627. }
  6628. switch(loc) {
  6629. case TABLE_CART:
  6630. size = MAX_CART;
  6631. items = sd->cart.u.items_cart;
  6632. break;
  6633. case TABLE_STORAGE:
  6634. size = MAX_STORAGE;
  6635. items = sd->storage.u.items_storage;
  6636. break;
  6637. case TABLE_GUILD_STORAGE:
  6638. {
  6639. struct s_storage *gstor = guild2storage2(sd->status.guild_id);
  6640. size = MAX_GUILD_STORAGE;
  6641. items = gstor->u.items_guild;
  6642. }
  6643. break;
  6644. default: // TABLE_INVENTORY
  6645. size = MAX_INVENTORY;
  6646. items = sd->inventory.u.items_inventory;
  6647. break;
  6648. }
  6649. for(;;)
  6650. {
  6651. unsigned short important = 0;
  6652. amount = it->amount;
  6653. // 1st pass -- less important items / exact match
  6654. for( i = 0; amount && i < size; i++ )
  6655. {
  6656. struct item *itm = NULL;
  6657. if( !&items[i] || !(itm = &items[i])->nameid || itm->nameid != it->nameid )
  6658. {// wrong/invalid item
  6659. continue;
  6660. }
  6661. if( itm->equip != it->equip || itm->refine != it->refine )
  6662. {// not matching attributes
  6663. important++;
  6664. continue;
  6665. }
  6666. if( exact_match )
  6667. {
  6668. if( itm->identify != it->identify || itm->attribute != it->attribute || memcmp(itm->card, it->card, sizeof(itm->card)) )
  6669. {// not matching exact attributes
  6670. continue;
  6671. }
  6672. }
  6673. else
  6674. {
  6675. if( itemdb_type(itm->nameid) == IT_PETEGG )
  6676. {
  6677. if( itm->card[0] == CARD0_PET && CheckForCharServer() )
  6678. {// pet which cannot be deleted
  6679. continue;
  6680. }
  6681. }
  6682. else if( memcmp(itm->card, it->card, sizeof(itm->card)) )
  6683. {// named/carded item
  6684. important++;
  6685. continue;
  6686. }
  6687. }
  6688. // count / delete item
  6689. buildin_delitem_delete(sd, i, &amount, loc, delete_items);
  6690. }
  6691. // 2nd pass -- any matching item
  6692. if( amount == 0 || important == 0 )
  6693. {// either everything was already consumed or no items were skipped
  6694. ;
  6695. }
  6696. else for( i = 0; amount && i < size; i++ )
  6697. {
  6698. struct item *itm = NULL;
  6699. if( !&items[i] || !(itm = &items[i])->nameid || itm->nameid != it->nameid )
  6700. {// wrong/invalid item
  6701. continue;
  6702. }
  6703. if( itemdb_type(itm->nameid) == IT_PETEGG && itm->card[0] == CARD0_PET && CheckForCharServer() )
  6704. {// pet which cannot be deleted
  6705. continue;
  6706. }
  6707. if( exact_match )
  6708. {
  6709. if( itm->refine != it->refine || itm->identify != it->identify || itm->attribute != it->attribute || memcmp(itm->card, it->card, sizeof(itm->card)) )
  6710. {// not matching attributes
  6711. continue;
  6712. }
  6713. }
  6714. // count / delete item
  6715. buildin_delitem_delete(sd, i, &amount, loc, delete_items);
  6716. }
  6717. if( amount )
  6718. {// not enough items
  6719. return false;
  6720. }
  6721. else if( delete_items )
  6722. {// we are done with the work
  6723. return true;
  6724. }
  6725. else
  6726. {// get rid of the items now
  6727. delete_items = true;
  6728. }
  6729. }
  6730. }
  6731. /// Deletes items from the target/attached player.
  6732. /// Prioritizes ordinary items.
  6733. ///
  6734. /// delitem <item id>,<amount>{,<account id>}
  6735. /// delitem "<item name>",<amount>{,<account id>}
  6736. /// cartdelitem <item id>,<amount>{,<account id>}
  6737. /// cartdelitem "<item name>",<amount>{,<account id>}
  6738. /// storagedelitem <item id>,<amount>{,<account id>}
  6739. /// storagedelitem "<item name>",<amount>{,<account id>}
  6740. /// guildstoragedelitem <item id>,<amount>{,<account id>}
  6741. /// guildstoragedelitem "<item name>",<amount>{,<account id>}
  6742. BUILDIN_FUNC(delitem)
  6743. {
  6744. TBL_PC *sd;
  6745. struct item it;
  6746. struct script_data *data;
  6747. uint8 loc = 0;
  6748. char* command = (char*)script_getfuncname(st);
  6749. if(!strncmp(command, "cart", 4))
  6750. loc = TABLE_CART;
  6751. else if(!strncmp(command, "storage", 7))
  6752. loc = TABLE_STORAGE;
  6753. else if(!strncmp(command, "guildstorage", 12))
  6754. loc = TABLE_GUILD_STORAGE;
  6755. if( script_hasdata(st,4) )
  6756. {
  6757. uint32 account_id = script_getnum(st,4);
  6758. sd = map_id2sd(account_id); // <account id>
  6759. if( sd == NULL )
  6760. {
  6761. ShowError("buildin_%s: player not found (AID=%d).\n", command, account_id);
  6762. st->state = END;
  6763. return SCRIPT_CMD_FAILURE;
  6764. }
  6765. }
  6766. else
  6767. {
  6768. sd = script_rid2sd(st);// attached player
  6769. if( sd == NULL )
  6770. return SCRIPT_CMD_SUCCESS;
  6771. }
  6772. if (loc == TABLE_CART && !pc_iscarton(sd)) {
  6773. ShowError("buildin_cartdelitem: player doesn't have cart (CID=%d).\n", sd->status.char_id);
  6774. script_pushint(st, -1);
  6775. return SCRIPT_CMD_FAILURE;
  6776. }
  6777. if (loc == TABLE_GUILD_STORAGE) {
  6778. struct s_storage *gstor = guild2storage2(sd->status.guild_id);
  6779. if (gstor == NULL || sd->state.storage_flag) {
  6780. script_pushint(st, -1);
  6781. return SCRIPT_CMD_FAILURE;
  6782. }
  6783. }
  6784. data = script_getdata(st,2);
  6785. get_val(st,data);
  6786. if( data_isstring(data) )
  6787. {
  6788. const char* item_name = conv_str(st,data);
  6789. struct item_data* id = itemdb_searchname(item_name);
  6790. if( id == NULL )
  6791. {
  6792. ShowError("buildin_%s: unknown item \"%s\".\n", command, item_name);
  6793. st->state = END;
  6794. return SCRIPT_CMD_FAILURE;
  6795. }
  6796. it.nameid = id->nameid;// "<item name>"
  6797. }
  6798. else
  6799. {
  6800. it.nameid = conv_num(st,data);// <item id>
  6801. if( !itemdb_exists( it.nameid ) )
  6802. {
  6803. ShowError("buildin_%s: unknown item \"%hu\".\n", command, it.nameid);
  6804. st->state = END;
  6805. return SCRIPT_CMD_FAILURE;
  6806. }
  6807. }
  6808. it.amount=script_getnum(st,3);
  6809. if( it.amount <= 0 )
  6810. return SCRIPT_CMD_SUCCESS;// nothing to do
  6811. if( buildin_delitem_search(sd, &it, false, loc) )
  6812. {// success
  6813. return SCRIPT_CMD_SUCCESS;
  6814. }
  6815. ShowError("buildin_%s: failed to delete %d items (AID=%d item_id=%hu).\n", command, it.amount, sd->status.account_id, it.nameid);
  6816. st->state = END;
  6817. st->mes_active = 0;
  6818. clif_scriptclose(sd, st->oid);
  6819. return SCRIPT_CMD_FAILURE;
  6820. }
  6821. /// Deletes items from the target/attached player.
  6822. ///
  6823. /// delitem2 <item id>,<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>{,<account ID>}
  6824. /// delitem2 "<Item name>",<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>{,<account ID>}
  6825. /// cartdelitem2 <item id>,<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>{,<account ID>}
  6826. /// cartdelitem2 "<Item name>",<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>{,<account ID>}
  6827. /// storagedelitem2 <item id>,<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>{,<account ID>}
  6828. /// storagedelitem2 "<Item name>",<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>{,<account ID>}
  6829. /// guildstoragedelitem2 <item id>,<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>{,<account ID>}
  6830. /// guildstoragedelitem2 "<Item name>",<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>{,<account ID>}
  6831. BUILDIN_FUNC(delitem2)
  6832. {
  6833. TBL_PC *sd;
  6834. struct item it;
  6835. struct script_data *data;
  6836. uint8 loc = 0;
  6837. char* command = (char*)script_getfuncname(st);
  6838. if(!strncmp(command, "cart", 4))
  6839. loc = TABLE_CART;
  6840. else if(!strncmp(command, "storage", 7))
  6841. loc = TABLE_STORAGE;
  6842. else if(!strncmp(command, "guildstorage", 12))
  6843. loc = TABLE_GUILD_STORAGE;
  6844. if( script_hasdata(st,11) )
  6845. {
  6846. uint32 account_id = script_getnum(st,11);
  6847. sd = map_id2sd(account_id); // <account id>
  6848. if( sd == NULL )
  6849. {
  6850. ShowError("buildin_%s: player not found (AID=%d).\n", command, account_id);
  6851. st->state = END;
  6852. return SCRIPT_CMD_FAILURE;
  6853. }
  6854. }
  6855. else
  6856. {
  6857. sd = script_rid2sd(st);// attached player
  6858. if( sd == NULL )
  6859. return SCRIPT_CMD_SUCCESS;
  6860. }
  6861. if (loc == TABLE_CART && !pc_iscarton(sd)) {
  6862. ShowError("buildin_cartdelitem: player doesn't have cart (CID=%d).\n", sd->status.char_id);
  6863. script_pushint(st,-1);
  6864. return SCRIPT_CMD_FAILURE;
  6865. }
  6866. if (loc == TABLE_GUILD_STORAGE) {
  6867. struct s_storage *gstor = guild2storage2(sd->status.guild_id);
  6868. if (gstor == NULL || sd->state.storage_flag) {
  6869. script_pushint(st, -1);
  6870. return SCRIPT_CMD_FAILURE;
  6871. }
  6872. }
  6873. data = script_getdata(st,2);
  6874. get_val(st,data);
  6875. if( data_isstring(data) )
  6876. {
  6877. const char* item_name = conv_str(st,data);
  6878. struct item_data* id = itemdb_searchname(item_name);
  6879. if( id == NULL )
  6880. {
  6881. ShowError("buildin_%s: unknown item \"%s\".\n", command, item_name);
  6882. st->state = END;
  6883. return SCRIPT_CMD_FAILURE;
  6884. }
  6885. it.nameid = id->nameid;// "<item name>"
  6886. }
  6887. else
  6888. {
  6889. it.nameid = conv_num(st,data);// <item id>
  6890. if( !itemdb_exists( it.nameid ) )
  6891. {
  6892. ShowError("buildin_%s: unknown item \"%hu\".\n", command, it.nameid);
  6893. st->state = END;
  6894. return SCRIPT_CMD_FAILURE;
  6895. }
  6896. }
  6897. it.amount=script_getnum(st,3);
  6898. it.identify=script_getnum(st,4);
  6899. it.refine=script_getnum(st,5);
  6900. it.attribute=script_getnum(st,6);
  6901. it.card[0]=(short)script_getnum(st,7);
  6902. it.card[1]=(short)script_getnum(st,8);
  6903. it.card[2]=(short)script_getnum(st,9);
  6904. it.card[3]=(short)script_getnum(st,10);
  6905. if( it.amount <= 0 )
  6906. return SCRIPT_CMD_SUCCESS;// nothing to do
  6907. if( buildin_delitem_search(sd, &it, true, loc) )
  6908. {// success
  6909. return SCRIPT_CMD_SUCCESS;
  6910. }
  6911. ShowError("buildin_%s: failed to delete %d items (AID=%d item_id=%hu).\n", command, it.amount, sd->status.account_id, it.nameid);
  6912. st->state = END;
  6913. st->mes_active = 0;
  6914. clif_scriptclose(sd, st->oid);
  6915. return SCRIPT_CMD_FAILURE;
  6916. }
  6917. /*==========================================
  6918. * Enables/Disables use of items while in an NPC [Skotlex]
  6919. *------------------------------------------*/
  6920. BUILDIN_FUNC(enableitemuse)
  6921. {
  6922. TBL_PC *sd;
  6923. sd=script_rid2sd(st);
  6924. if (sd)
  6925. st->npc_item_flag = sd->npc_item_flag = 1;
  6926. return SCRIPT_CMD_SUCCESS;
  6927. }
  6928. BUILDIN_FUNC(disableitemuse)
  6929. {
  6930. TBL_PC *sd;
  6931. sd=script_rid2sd(st);
  6932. if (sd)
  6933. st->npc_item_flag = sd->npc_item_flag = 0;
  6934. return SCRIPT_CMD_SUCCESS;
  6935. }
  6936. /*==========================================
  6937. * Returns a character's specified stat.
  6938. * Check pc_readparam for available options.
  6939. * readparam <param>{,"<nick>"}
  6940. * readparam <param>{,<char_id>}
  6941. *------------------------------------------*/
  6942. BUILDIN_FUNC(readparam)
  6943. {
  6944. int value;
  6945. struct script_data *data = script_getdata(st, 2);
  6946. TBL_PC *sd;
  6947. if( script_hasdata(st, 3) ){
  6948. if( script_isint(st, 3) ){
  6949. script_charid2sd(3, sd);
  6950. }else{
  6951. script_nick2sd(3, sd);
  6952. }
  6953. }else{
  6954. sd = script_rid2sd(st);
  6955. }
  6956. if( !sd ){
  6957. script_pushint(st, -1);
  6958. return SCRIPT_CMD_FAILURE;
  6959. }
  6960. // If you use a parameter, return the value behind it
  6961. if( reference_toparam(data) ){
  6962. get_val_(st, data, sd);
  6963. value = (int)data->u.num;
  6964. }else{
  6965. value = pc_readparam(sd,script_getnum(st, 2));
  6966. }
  6967. script_pushint(st,value);
  6968. return SCRIPT_CMD_SUCCESS;
  6969. }
  6970. /*==========================================
  6971. * Return charid identification
  6972. * return by @num :
  6973. * 0 : char_id
  6974. * 1 : party_id
  6975. * 2 : guild_id
  6976. * 3 : account_id
  6977. * 4 : bg_id
  6978. * 5 : clan_id
  6979. *------------------------------------------*/
  6980. BUILDIN_FUNC(getcharid)
  6981. {
  6982. int num;
  6983. TBL_PC *sd;
  6984. num = script_getnum(st,2);
  6985. if( !script_nick2sd(3,sd) ){
  6986. script_pushint(st,0); //return 0, according docs
  6987. return SCRIPT_CMD_SUCCESS;
  6988. }
  6989. switch( num ) {
  6990. case 0: script_pushint(st,sd->status.char_id); break;
  6991. case 1: script_pushint(st,sd->status.party_id); break;
  6992. case 2: script_pushint(st,sd->status.guild_id); break;
  6993. case 3: script_pushint(st,sd->status.account_id); break;
  6994. case 4: script_pushint(st,sd->bg_id); break;
  6995. case 5: script_pushint(st,sd->status.clan_id); break;
  6996. default:
  6997. ShowError("buildin_getcharid: invalid parameter (%d).\n", num);
  6998. script_pushint(st,0);
  6999. break;
  7000. }
  7001. return SCRIPT_CMD_SUCCESS;
  7002. }
  7003. /*==========================================
  7004. * returns the GID of an NPC
  7005. *------------------------------------------*/
  7006. BUILDIN_FUNC(getnpcid)
  7007. {
  7008. int num = script_getnum(st,2);
  7009. struct npc_data* nd = NULL;
  7010. if( script_hasdata(st,3) )
  7011. {// unique npc name
  7012. if( ( nd = npc_name2id(script_getstr(st,3)) ) == NULL )
  7013. {
  7014. ShowError("buildin_getnpcid: No such NPC '%s'.\n", script_getstr(st,3));
  7015. script_pushint(st,0);
  7016. return SCRIPT_CMD_FAILURE;
  7017. }
  7018. }
  7019. switch (num) {
  7020. case 0:
  7021. script_pushint(st,nd ? nd->bl.id : st->oid);
  7022. break;
  7023. default:
  7024. ShowError("buildin_getnpcid: invalid parameter (%d).\n", num);
  7025. script_pushint(st,0);
  7026. return SCRIPT_CMD_FAILURE;
  7027. }
  7028. return SCRIPT_CMD_SUCCESS;
  7029. }
  7030. /*==========================================
  7031. * Return the name of the party_id
  7032. * null if not found
  7033. *------------------------------------------*/
  7034. BUILDIN_FUNC(getpartyname)
  7035. {
  7036. int party_id;
  7037. struct party_data* p;
  7038. party_id = script_getnum(st,2);
  7039. if( ( p = party_search(party_id) ) != NULL )
  7040. {
  7041. script_pushstrcopy(st,p->party.name);
  7042. }
  7043. else
  7044. {
  7045. script_pushconststr(st,"null");
  7046. }
  7047. return SCRIPT_CMD_SUCCESS;
  7048. }
  7049. /*==========================================
  7050. * Get the information of the members of a party by type
  7051. * @party_id, @type
  7052. * return by @type :
  7053. * - : nom des membres
  7054. * 1 : char_id des membres
  7055. * 2 : account_id des membres
  7056. *------------------------------------------*/
  7057. BUILDIN_FUNC(getpartymember)
  7058. {
  7059. struct party_data *p;
  7060. uint8 j = 0;
  7061. p = party_search(script_getnum(st,2));
  7062. if (p != NULL) {
  7063. uint8 i, type = 0;
  7064. struct script_data *data = NULL;
  7065. char *varname = NULL;
  7066. if (script_hasdata(st,3))
  7067. type = script_getnum(st,3);
  7068. if (script_hasdata(st,4)) {
  7069. data = script_getdata(st, 4);
  7070. if (!data_isreference(data)) {
  7071. ShowError("buildin_getpartymember: Error in argument! Please give a variable to store values in.\n");
  7072. return SCRIPT_CMD_FAILURE;
  7073. }
  7074. varname = reference_getname(data);
  7075. if (type <= 0 && varname[strlen(varname)-1] != '$') {
  7076. ShowError("buildin_getpartymember: The array %s is not string type.\n", varname);
  7077. return SCRIPT_CMD_FAILURE;
  7078. }
  7079. }
  7080. for (i = 0; i < MAX_PARTY; i++) {
  7081. if (p->party.member[i].account_id) {
  7082. switch (type) {
  7083. case 2:
  7084. if (data)
  7085. setd_sub(st, NULL, varname, j, (void *)__64BPRTSIZE(p->party.member[i].account_id), data->ref);
  7086. else
  7087. mapreg_setreg(reference_uid(add_str("$@partymemberaid"), j),p->party.member[i].account_id);
  7088. break;
  7089. case 1:
  7090. if (data)
  7091. setd_sub(st, NULL, varname, j, (void *)__64BPRTSIZE(p->party.member[i].char_id), data->ref);
  7092. else
  7093. mapreg_setreg(reference_uid(add_str("$@partymembercid"), j),p->party.member[i].char_id);
  7094. break;
  7095. default:
  7096. if (data)
  7097. setd_sub(st, NULL, varname, j, (void *)__64BPRTSIZE(p->party.member[i].name), data->ref);
  7098. else
  7099. mapreg_setregstr(reference_uid(add_str("$@partymembername$"), j),p->party.member[i].name);
  7100. break;
  7101. }
  7102. j++;
  7103. }
  7104. }
  7105. }
  7106. mapreg_setreg(add_str("$@partymembercount"),j);
  7107. script_pushint(st, j);
  7108. return SCRIPT_CMD_SUCCESS;
  7109. }
  7110. /*==========================================
  7111. * Retrieves party leader. if flag is specified,
  7112. * return some of the leader data. Otherwise, return name.
  7113. *------------------------------------------*/
  7114. BUILDIN_FUNC(getpartyleader)
  7115. {
  7116. int party_id, type = 0, i=0;
  7117. struct party_data *p;
  7118. party_id=script_getnum(st,2);
  7119. if( script_hasdata(st,3) )
  7120. type=script_getnum(st,3);
  7121. p=party_search(party_id);
  7122. if (p) //Search leader
  7123. for(i = 0; i < MAX_PARTY && !p->party.member[i].leader; i++);
  7124. if (!p || i == MAX_PARTY) { //leader not found
  7125. if (type)
  7126. script_pushint(st,-1);
  7127. else
  7128. script_pushconststr(st,"null");
  7129. return SCRIPT_CMD_SUCCESS;
  7130. }
  7131. switch (type) {
  7132. case 1: script_pushint(st,p->party.member[i].account_id); break;
  7133. case 2: script_pushint(st,p->party.member[i].char_id); break;
  7134. case 3: script_pushint(st,p->party.member[i].class_); break;
  7135. case 4: script_pushstrcopy(st,mapindex_id2name(p->party.member[i].map)); break;
  7136. case 5: script_pushint(st,p->party.member[i].lv); break;
  7137. default: script_pushstrcopy(st,p->party.member[i].name); break;
  7138. }
  7139. return SCRIPT_CMD_SUCCESS;
  7140. }
  7141. /*==========================================
  7142. * Return the name of the @guild_id
  7143. * null if not found
  7144. *------------------------------------------*/
  7145. BUILDIN_FUNC(getguildname)
  7146. {
  7147. int guild_id;
  7148. struct guild* g;
  7149. guild_id = script_getnum(st,2);
  7150. if( ( g = guild_search(guild_id) ) != NULL )
  7151. script_pushstrcopy(st,g->name);
  7152. else
  7153. script_pushconststr(st,"null");
  7154. return SCRIPT_CMD_SUCCESS;
  7155. }
  7156. /*==========================================
  7157. * Return the name of the guild master of @guild_id
  7158. * null if not found
  7159. *------------------------------------------*/
  7160. BUILDIN_FUNC(getguildmaster)
  7161. {
  7162. int guild_id;
  7163. struct guild* g;
  7164. guild_id = script_getnum(st,2);
  7165. if( ( g = guild_search(guild_id) ) != NULL )
  7166. script_pushstrcopy(st,g->member[0].name);
  7167. else
  7168. script_pushconststr(st,"null");
  7169. return SCRIPT_CMD_SUCCESS;
  7170. }
  7171. BUILDIN_FUNC(getguildmasterid)
  7172. {
  7173. int guild_id;
  7174. struct guild* g;
  7175. guild_id = script_getnum(st,2);
  7176. if( ( g = guild_search(guild_id) ) != NULL )
  7177. script_pushint(st,g->member[0].char_id);
  7178. else
  7179. script_pushint(st,0);
  7180. return SCRIPT_CMD_SUCCESS;
  7181. }
  7182. /*==========================================
  7183. * Get char string information by type :
  7184. * Return by @type :
  7185. * 0 : char_name
  7186. * 1 : party_name or ""
  7187. * 2 : guild_name or ""
  7188. * 3 : map_name
  7189. * - : ""
  7190. * strcharinfo(<type>{,<char_id>})
  7191. *------------------------------------------*/
  7192. BUILDIN_FUNC(strcharinfo)
  7193. {
  7194. TBL_PC *sd;
  7195. int num;
  7196. struct guild* g;
  7197. struct party_data* p;
  7198. if (!script_charid2sd(3,sd)) {
  7199. script_pushconststr(st,"");
  7200. return SCRIPT_CMD_FAILURE;
  7201. }
  7202. num=script_getnum(st,2);
  7203. switch(num){
  7204. case 0:
  7205. script_pushstrcopy(st,sd->status.name);
  7206. break;
  7207. case 1:
  7208. if( ( p = party_search(sd->status.party_id) ) != NULL ) {
  7209. script_pushstrcopy(st,p->party.name);
  7210. } else {
  7211. script_pushconststr(st,"");
  7212. }
  7213. break;
  7214. case 2:
  7215. if( ( g = sd->guild ) != NULL ) {
  7216. script_pushstrcopy(st,g->name);
  7217. } else {
  7218. script_pushconststr(st,"");
  7219. }
  7220. break;
  7221. case 3:
  7222. script_pushconststr(st,map[sd->bl.m].name);
  7223. break;
  7224. default:
  7225. ShowWarning("buildin_strcharinfo: unknown parameter.\n");
  7226. script_pushconststr(st,"");
  7227. break;
  7228. }
  7229. return SCRIPT_CMD_SUCCESS;
  7230. }
  7231. /*==========================================
  7232. * Get npc string information by type
  7233. * return by @type:
  7234. * 0 : name
  7235. * 1 : str#
  7236. * 2 : #str
  7237. * 3 : ::str
  7238. * 4 : map name
  7239. *------------------------------------------*/
  7240. BUILDIN_FUNC(strnpcinfo)
  7241. {
  7242. TBL_NPC* nd;
  7243. int num;
  7244. char *buf,*name=NULL;
  7245. nd = map_id2nd(st->oid);
  7246. if (!nd) {
  7247. script_pushconststr(st, "");
  7248. return SCRIPT_CMD_SUCCESS;
  7249. }
  7250. num = script_getnum(st,2);
  7251. switch(num){
  7252. case 0: // display name
  7253. name = aStrdup(nd->name);
  7254. break;
  7255. case 1: // visible part of display name
  7256. if((buf = strchr(nd->name,'#')) != NULL)
  7257. {
  7258. name = aStrdup(nd->name);
  7259. name[buf - nd->name] = 0;
  7260. } else // Return the name, there is no '#' present
  7261. name = aStrdup(nd->name);
  7262. break;
  7263. case 2: // # fragment
  7264. if((buf = strchr(nd->name,'#')) != NULL)
  7265. name = aStrdup(buf+1);
  7266. break;
  7267. case 3: // unique name
  7268. name = aStrdup(nd->exname);
  7269. break;
  7270. case 4: // map name
  7271. name = aStrdup(map[nd->bl.m].name);
  7272. break;
  7273. }
  7274. if(name)
  7275. script_pushstr(st, name);
  7276. else
  7277. script_pushconststr(st, "");
  7278. return SCRIPT_CMD_SUCCESS;
  7279. }
  7280. /**
  7281. * getequipid(<equipment slot>{,<char_id>})
  7282. **/
  7283. BUILDIN_FUNC(getequipid)
  7284. {
  7285. int i, num;
  7286. TBL_PC* sd;
  7287. struct item_data* item;
  7288. if (!script_charid2sd(3, sd)) {
  7289. script_pushint(st,-1);
  7290. return SCRIPT_CMD_FAILURE;
  7291. }
  7292. num = script_getnum(st,2);
  7293. if( !equip_index_check(num) )
  7294. {
  7295. script_pushint(st,-1);
  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. {
  7302. script_pushint(st,-1);
  7303. return SCRIPT_CMD_SUCCESS;
  7304. }
  7305. item = sd->inventory_data[i];
  7306. if( item != 0 )
  7307. script_pushint(st,item->nameid);
  7308. else
  7309. script_pushint(st,0);
  7310. return SCRIPT_CMD_SUCCESS;
  7311. }
  7312. /*==========================================
  7313. * getequipuniqueid(<equipment slot>{,<char_id>})
  7314. *------------------------------------------*/
  7315. BUILDIN_FUNC(getequipuniqueid)
  7316. {
  7317. int i, num;
  7318. TBL_PC* sd;
  7319. struct item* item;
  7320. if (!script_charid2sd(3, sd)) {
  7321. script_pushconststr(st, "");
  7322. return SCRIPT_CMD_FAILURE;
  7323. }
  7324. num = script_getnum(st,2);
  7325. if ( !equip_index_check(num) ) {
  7326. script_pushconststr(st, "");
  7327. return SCRIPT_CMD_FAILURE;
  7328. }
  7329. // get inventory position of item
  7330. i = pc_checkequip(sd,equip_bitmask[num]);
  7331. if (i < 0) {
  7332. script_pushconststr(st, "");
  7333. return SCRIPT_CMD_FAILURE;
  7334. }
  7335. item = &sd->inventory.u.items_inventory[i];
  7336. if (item != 0) {
  7337. int maxlen = 256;
  7338. char *buf = (char *)aMalloc(maxlen*sizeof(char));
  7339. memset(buf, 0, maxlen);
  7340. snprintf(buf, maxlen-1, "%llu", (unsigned long long)item->unique_id);
  7341. script_pushstr(st, buf);
  7342. } else
  7343. script_pushconststr(st, "");
  7344. return SCRIPT_CMD_SUCCESS;
  7345. }
  7346. /*==========================================
  7347. * Get the equipement name at pos
  7348. * return item jname or ""
  7349. * getequipname(<equipment slot>{,<char_id>})
  7350. *------------------------------------------*/
  7351. BUILDIN_FUNC(getequipname)
  7352. {
  7353. int i, num;
  7354. TBL_PC* sd;
  7355. struct item_data* item;
  7356. if (!script_charid2sd(3, sd)) {
  7357. script_pushconststr(st, "");
  7358. return SCRIPT_CMD_FAILURE;
  7359. }
  7360. num = script_getnum(st,2);
  7361. if( !equip_index_check(num) )
  7362. {
  7363. script_pushconststr(st,"");
  7364. return SCRIPT_CMD_SUCCESS;
  7365. }
  7366. // get inventory position of item
  7367. i = pc_checkequip(sd,equip_bitmask[num]);
  7368. if( i < 0 )
  7369. {
  7370. script_pushconststr(st,"");
  7371. return SCRIPT_CMD_SUCCESS;
  7372. }
  7373. item = sd->inventory_data[i];
  7374. if( item != 0 )
  7375. script_pushstrcopy(st,item->jname);
  7376. else
  7377. script_pushconststr(st,"");
  7378. return SCRIPT_CMD_SUCCESS;
  7379. }
  7380. /*==========================================
  7381. * getbrokenid [Valaris]
  7382. * getbrokenid(<number>{,<char_id>})
  7383. *------------------------------------------*/
  7384. BUILDIN_FUNC(getbrokenid)
  7385. {
  7386. int i,num,id = 0,brokencounter = 0;
  7387. TBL_PC *sd;
  7388. if (!script_charid2sd(3, sd)) {
  7389. script_pushint(st,0);
  7390. return SCRIPT_CMD_FAILURE;
  7391. }
  7392. num = script_getnum(st,2);
  7393. for(i = 0; i < MAX_INVENTORY; i++) {
  7394. if(sd->inventory.u.items_inventory[i].attribute) {
  7395. brokencounter++;
  7396. if(num == brokencounter){
  7397. id = sd->inventory.u.items_inventory[i].nameid;
  7398. break;
  7399. }
  7400. }
  7401. }
  7402. script_pushint(st,id);
  7403. return SCRIPT_CMD_SUCCESS;
  7404. }
  7405. /*==========================================
  7406. * repair [Valaris]
  7407. * repair <num>{,<char_id>};
  7408. *------------------------------------------*/
  7409. BUILDIN_FUNC(repair)
  7410. {
  7411. int i,num;
  7412. int repaircounter = 0;
  7413. TBL_PC *sd;
  7414. if (!script_charid2sd(3,sd))
  7415. return SCRIPT_CMD_FAILURE;
  7416. num = script_getnum(st,2);
  7417. for(i = 0; i < MAX_INVENTORY; i++) {
  7418. if(sd->inventory.u.items_inventory[i].attribute) {
  7419. repaircounter++;
  7420. if(num == repaircounter) {
  7421. sd->inventory.u.items_inventory[i].attribute = 0;
  7422. clif_equiplist(sd);
  7423. clif_produceeffect(sd, 0, sd->inventory.u.items_inventory[i].nameid);
  7424. clif_misceffect(&sd->bl, 3);
  7425. break;
  7426. }
  7427. }
  7428. }
  7429. return SCRIPT_CMD_SUCCESS;
  7430. }
  7431. /*==========================================
  7432. * repairall {<char_id>}
  7433. *------------------------------------------*/
  7434. BUILDIN_FUNC(repairall)
  7435. {
  7436. int i, repaircounter = 0;
  7437. TBL_PC *sd;
  7438. if (!script_charid2sd(2,sd))
  7439. return SCRIPT_CMD_FAILURE;
  7440. for(i = 0; i < MAX_INVENTORY; i++)
  7441. {
  7442. if(sd->inventory.u.items_inventory[i].nameid && sd->inventory.u.items_inventory[i].attribute)
  7443. {
  7444. sd->inventory.u.items_inventory[i].attribute = 0;
  7445. clif_produceeffect(sd,0,sd->inventory.u.items_inventory[i].nameid);
  7446. repaircounter++;
  7447. }
  7448. }
  7449. if(repaircounter)
  7450. {
  7451. clif_misceffect(&sd->bl, 3);
  7452. clif_equiplist(sd);
  7453. }
  7454. return SCRIPT_CMD_SUCCESS;
  7455. }
  7456. /*==========================================
  7457. * Chk if player have something equiped at pos
  7458. * getequipisequiped <pos>{,<char_id>}
  7459. *------------------------------------------*/
  7460. BUILDIN_FUNC(getequipisequiped)
  7461. {
  7462. int i = -1,num;
  7463. TBL_PC *sd;
  7464. num = script_getnum(st,2);
  7465. if (!script_charid2sd(3,sd))
  7466. return SCRIPT_CMD_FAILURE;
  7467. if ( equip_index_check(num) )
  7468. i=pc_checkequip(sd,equip_bitmask[num]);
  7469. if(i >= 0)
  7470. script_pushint(st,1);
  7471. else
  7472. script_pushint(st,0);
  7473. return SCRIPT_CMD_SUCCESS;
  7474. }
  7475. /*==========================================
  7476. * Chk if the player have something equiped at pos
  7477. * if so chk if this item ain't marked not refinable or rental
  7478. * return (npc)
  7479. * 1 : true
  7480. * 0 : false
  7481. * getequipisenableref(<equipment slot>{,<char_id>})
  7482. *------------------------------------------*/
  7483. BUILDIN_FUNC(getequipisenableref)
  7484. {
  7485. int i = -1,num;
  7486. TBL_PC *sd;
  7487. num = script_getnum(st,2);
  7488. if (!script_charid2sd(3, sd)) {
  7489. script_pushint(st,0);
  7490. return SCRIPT_CMD_FAILURE;
  7491. }
  7492. if( equip_index_check(num) )
  7493. i = pc_checkequip(sd,equip_bitmask[num]);
  7494. if( i >= 0 && sd->inventory_data[i] && !sd->inventory_data[i]->flag.no_refine && !sd->inventory.u.items_inventory[i].expire_time )
  7495. script_pushint(st,1);
  7496. else
  7497. script_pushint(st,0);
  7498. return SCRIPT_CMD_SUCCESS;
  7499. }
  7500. /*==========================================
  7501. * Get the item refined value at pos
  7502. * return (npc)
  7503. * x : refine amount
  7504. * 0 : false (not refined)
  7505. * getequiprefinerycnt(<equipment slot>{,<char_id>})
  7506. *------------------------------------------*/
  7507. BUILDIN_FUNC(getequiprefinerycnt)
  7508. {
  7509. int i = -1,num;
  7510. TBL_PC *sd;
  7511. num = script_getnum(st,2);
  7512. if (!script_charid2sd(3, sd)) {
  7513. script_pushint(st,0);
  7514. return SCRIPT_CMD_FAILURE;
  7515. }
  7516. if (equip_index_check(num))
  7517. i=pc_checkequip(sd,equip_bitmask[num]);
  7518. if(i >= 0)
  7519. script_pushint(st,sd->inventory.u.items_inventory[i].refine);
  7520. else
  7521. script_pushint(st,0);
  7522. return SCRIPT_CMD_SUCCESS;
  7523. }
  7524. /*==========================================
  7525. * Get the weapon level value at pos
  7526. * (pos should normally only be EQI_HAND_L or EQI_HAND_R)
  7527. * return (npc)
  7528. * x : weapon level
  7529. * 0 : false
  7530. * getequipweaponlv(<equipment slot>{,<char_id>})
  7531. *------------------------------------------*/
  7532. BUILDIN_FUNC(getequipweaponlv)
  7533. {
  7534. int i = -1, num;
  7535. TBL_PC *sd;
  7536. num = script_getnum(st, 2);
  7537. if (!script_charid2sd(3, sd)) {
  7538. script_pushint(st, 0);
  7539. return SCRIPT_CMD_FAILURE;
  7540. }
  7541. if (num == -1)
  7542. i = current_equip_item_index;
  7543. else if (equip_index_check(num))
  7544. i = pc_checkequip(sd, equip_bitmask[num]);
  7545. if (i >= 0 && sd->inventory_data[i])
  7546. script_pushint(st, sd->inventory_data[i]->wlv);
  7547. else
  7548. script_pushint(st, 0);
  7549. return SCRIPT_CMD_SUCCESS;
  7550. }
  7551. /*==========================================
  7552. * Get the item refine chance (from refine.txt) for item at pos
  7553. * return (npc)
  7554. * x : refine chance
  7555. * 0 : false (max refine level or unequip..)
  7556. * getequippercentrefinery(<equipment slot>{,<char_id>})
  7557. *------------------------------------------*/
  7558. BUILDIN_FUNC(getequippercentrefinery)
  7559. {
  7560. int i = -1,num;
  7561. TBL_PC *sd;
  7562. num = script_getnum(st,2);
  7563. if (!script_charid2sd(3, sd)) {
  7564. script_pushint(st,0);
  7565. return SCRIPT_CMD_FAILURE;
  7566. }
  7567. if (equip_index_check(num))
  7568. i = pc_checkequip(sd,equip_bitmask[num]);
  7569. if(i >= 0 && sd->inventory.u.items_inventory[i].nameid && sd->inventory.u.items_inventory[i].refine < MAX_REFINE)
  7570. 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));
  7571. else
  7572. script_pushint(st,0);
  7573. return SCRIPT_CMD_SUCCESS;
  7574. }
  7575. /*==========================================
  7576. * Refine +1 item at pos and log and display refine
  7577. * successrefitem <equipment slot>{,<count>{,<char_id>}}
  7578. *------------------------------------------*/
  7579. BUILDIN_FUNC(successrefitem) {
  7580. short i = -1, up = 1;
  7581. int pos;
  7582. TBL_PC *sd;
  7583. pos = script_getnum(st,2);
  7584. if (!script_charid2sd(4, sd)) {
  7585. script_pushint(st, -1);
  7586. return SCRIPT_CMD_FAILURE;
  7587. }
  7588. if (script_hasdata(st, 3))
  7589. up = script_getnum(st, 3);
  7590. if (equip_index_check(pos))
  7591. i = pc_checkequip(sd,equip_bitmask[pos]);
  7592. if (i >= 0) {
  7593. unsigned int ep = sd->inventory.u.items_inventory[i].equip;
  7594. //Logs items, got from (N)PC scripts [Lupus]
  7595. log_pick_pc(sd, LOG_TYPE_SCRIPT, -1, &sd->inventory.u.items_inventory[i]);
  7596. if (sd->inventory.u.items_inventory[i].refine >= MAX_REFINE) {
  7597. script_pushint(st, MAX_REFINE);
  7598. return SCRIPT_CMD_SUCCESS;
  7599. }
  7600. sd->inventory.u.items_inventory[i].refine += up;
  7601. sd->inventory.u.items_inventory[i].refine = cap_value( sd->inventory.u.items_inventory[i].refine, 0, MAX_REFINE);
  7602. pc_unequipitem(sd,i,2); // status calc will happen in pc_equipitem() below
  7603. clif_refine(sd->fd,0,i,sd->inventory.u.items_inventory[i].refine);
  7604. clif_delitem(sd,i,1,3);
  7605. //Logs items, got from (N)PC scripts [Lupus]
  7606. log_pick_pc(sd, LOG_TYPE_SCRIPT, 1, &sd->inventory.u.items_inventory[i]);
  7607. clif_additem(sd,i,1,0);
  7608. pc_equipitem(sd,i,ep);
  7609. clif_misceffect(&sd->bl,3);
  7610. if (sd->inventory.u.items_inventory[i].refine == MAX_REFINE &&
  7611. sd->inventory.u.items_inventory[i].card[0] == CARD0_FORGE &&
  7612. sd->status.char_id == (int)MakeDWord(sd->inventory.u.items_inventory[i].card[2],sd->inventory.u.items_inventory[i].card[3]))
  7613. { // Fame point system [DracoRPG]
  7614. switch (sd->inventory_data[i]->wlv){
  7615. case 1:
  7616. pc_addfame(sd, battle_config.fame_refine_lv1); // Success to refine to +10 a lv1 weapon you forged = +1 fame point
  7617. break;
  7618. case 2:
  7619. pc_addfame(sd, battle_config.fame_refine_lv2); // Success to refine to +10 a lv2 weapon you forged = +25 fame point
  7620. break;
  7621. case 3:
  7622. pc_addfame(sd, battle_config.fame_refine_lv3); // Success to refine to +10 a lv3 weapon you forged = +1000 fame point
  7623. break;
  7624. }
  7625. }
  7626. script_pushint(st, sd->inventory.u.items_inventory[i].refine);
  7627. return SCRIPT_CMD_SUCCESS;
  7628. }
  7629. ShowError("buildin_successrefitem: No item equipped at pos %d (CID=%d/AID=%d).\n", pos, sd->status.char_id, sd->status.account_id);
  7630. script_pushint(st, -1);
  7631. return SCRIPT_CMD_FAILURE;
  7632. }
  7633. /*==========================================
  7634. * Show a failed Refine +1 attempt
  7635. * failedrefitem <equipment slot>{,<char_id>}
  7636. *------------------------------------------*/
  7637. BUILDIN_FUNC(failedrefitem) {
  7638. short i = -1;
  7639. int pos;
  7640. TBL_PC *sd;
  7641. pos = script_getnum(st,2);
  7642. if (!script_charid2sd(3, sd)) {
  7643. script_pushint(st, 0);
  7644. return SCRIPT_CMD_FAILURE;
  7645. }
  7646. if (equip_index_check(pos))
  7647. i = pc_checkequip(sd,equip_bitmask[pos]);
  7648. if (i >= 0) {
  7649. sd->inventory.u.items_inventory[i].refine = 0;
  7650. pc_unequipitem(sd,i,3); //recalculate bonus
  7651. clif_refine(sd->fd,1,i,sd->inventory.u.items_inventory[i].refine); //notify client of failure
  7652. pc_delitem(sd,i,1,0,2,LOG_TYPE_SCRIPT);
  7653. clif_misceffect(&sd->bl,2); // display failure effect
  7654. script_pushint(st, 1);
  7655. return SCRIPT_CMD_SUCCESS;
  7656. }
  7657. ShowError("buildin_failedrefitem: No item equipped at pos %d (CID=%d/AID=%d).\n", pos, sd->status.char_id, sd->status.account_id);
  7658. script_pushint(st, 0);
  7659. return SCRIPT_CMD_FAILURE;
  7660. }
  7661. /*==========================================
  7662. * Downgrades an Equipment Part by -1 . [Masao]
  7663. * downrefitem <equipment slot>{,<count>{,<char_id>}}
  7664. *------------------------------------------*/
  7665. BUILDIN_FUNC(downrefitem) {
  7666. short i = -1, down = 1;
  7667. int pos;
  7668. TBL_PC *sd;
  7669. if (!script_charid2sd(4, sd)) {
  7670. script_pushint(st, -1);
  7671. return SCRIPT_CMD_FAILURE;
  7672. }
  7673. pos = script_getnum(st,2);
  7674. if (script_hasdata(st, 3))
  7675. down = script_getnum(st, 3);
  7676. if (equip_index_check(pos))
  7677. i = pc_checkequip(sd,equip_bitmask[pos]);
  7678. if (i >= 0) {
  7679. unsigned int ep = sd->inventory.u.items_inventory[i].equip;
  7680. //Logs items, got from (N)PC scripts [Lupus]
  7681. log_pick_pc(sd, LOG_TYPE_SCRIPT, -1, &sd->inventory.u.items_inventory[i]);
  7682. pc_unequipitem(sd,i,2); // status calc will happen in pc_equipitem() below
  7683. sd->inventory.u.items_inventory[i].refine -= down;
  7684. sd->inventory.u.items_inventory[i].refine = cap_value( sd->inventory.u.items_inventory[i].refine, 0, MAX_REFINE);
  7685. clif_refine(sd->fd,2,i,sd->inventory.u.items_inventory[i].refine);
  7686. clif_delitem(sd,i,1,3);
  7687. //Logs items, got from (N)PC scripts [Lupus]
  7688. log_pick_pc(sd, LOG_TYPE_SCRIPT, 1, &sd->inventory.u.items_inventory[i]);
  7689. clif_additem(sd,i,1,0);
  7690. pc_equipitem(sd,i,ep);
  7691. clif_misceffect(&sd->bl,2);
  7692. script_pushint(st, sd->inventory.u.items_inventory[i].refine);
  7693. return SCRIPT_CMD_SUCCESS;
  7694. }
  7695. ShowError("buildin_downrefitem: No item equipped at pos %d (CID=%d/AID=%d).\n", pos, sd->status.char_id, sd->status.account_id);
  7696. script_pushint(st, -1);
  7697. return SCRIPT_CMD_FAILURE;
  7698. }
  7699. /**
  7700. * Delete the item equipped at pos.
  7701. * delequip <equipment slot>{,<char_id>};
  7702. **/
  7703. BUILDIN_FUNC(delequip) {
  7704. short i = -1;
  7705. int pos;
  7706. int8 ret;
  7707. TBL_PC *sd;
  7708. pos = script_getnum(st,2);
  7709. if (!script_charid2sd(3,sd)) {
  7710. st->state = END;
  7711. return SCRIPT_CMD_FAILURE;
  7712. }
  7713. if (equip_index_check(pos))
  7714. i = pc_checkequip(sd,equip_bitmask[pos]);
  7715. if (i >= 0) {
  7716. pc_unequipitem(sd,i,3); //recalculate bonus
  7717. ret = !(pc_delitem(sd,i,1,0,2,LOG_TYPE_SCRIPT));
  7718. }
  7719. else {
  7720. ShowError("buildin_delequip: No item equipped at pos %d (CID=%d/AID=%d).\n", pos, sd->status.char_id, sd->status.account_id);
  7721. st->state = END;
  7722. return SCRIPT_CMD_FAILURE;
  7723. }
  7724. script_pushint(st,ret);
  7725. return SCRIPT_CMD_SUCCESS;
  7726. }
  7727. /**
  7728. * Break the item equipped at pos.
  7729. * breakequip <equipment slot>{,<char_id>};
  7730. **/
  7731. BUILDIN_FUNC(breakequip) {
  7732. short i = -1;
  7733. int pos;
  7734. TBL_PC *sd;
  7735. pos = script_getnum(st,2);
  7736. if (!script_charid2sd(3,sd))
  7737. return SCRIPT_CMD_FAILURE;
  7738. if (equip_index_check(pos))
  7739. i = pc_checkequip(sd,equip_bitmask[pos]);
  7740. if (i >= 0) {
  7741. sd->inventory.u.items_inventory[i].attribute = 1;
  7742. pc_unequipitem(sd,i,3);
  7743. clif_equiplist(sd);
  7744. script_pushint(st,1);
  7745. return SCRIPT_CMD_SUCCESS;
  7746. }
  7747. ShowError("buildin_breakequip: No item equipped at pos %d (CID=%d/AID=%d).\n", pos, sd->status.char_id, sd->status.account_id);
  7748. script_pushint(st,0);
  7749. return SCRIPT_CMD_FAILURE;
  7750. }
  7751. /**
  7752. * statusup <stat>{,<char_id>};
  7753. **/
  7754. BUILDIN_FUNC(statusup)
  7755. {
  7756. int type;
  7757. TBL_PC *sd;
  7758. type = script_getnum(st,2);
  7759. if (!script_charid2sd(3, sd))
  7760. return SCRIPT_CMD_FAILURE;
  7761. pc_statusup(sd, type, 1);
  7762. return SCRIPT_CMD_SUCCESS;
  7763. }
  7764. /**
  7765. * statusup2 <stat>,<amount>{,<char_id>};
  7766. **/
  7767. BUILDIN_FUNC(statusup2)
  7768. {
  7769. int type,val;
  7770. TBL_PC *sd;
  7771. type=script_getnum(st,2);
  7772. val=script_getnum(st,3);
  7773. if (!script_charid2sd(4, sd))
  7774. return SCRIPT_CMD_FAILURE;
  7775. pc_statusup2(sd,type,val);
  7776. return SCRIPT_CMD_SUCCESS;
  7777. }
  7778. /// See 'doc/item_bonus.txt'
  7779. ///
  7780. /// bonus <bonus type>,<val1>;
  7781. /// bonus2 <bonus type>,<val1>,<val2>;
  7782. /// bonus3 <bonus type>,<val1>,<val2>,<val3>;
  7783. /// bonus4 <bonus type>,<val1>,<val2>,<val3>,<val4>;
  7784. /// bonus5 <bonus type>,<val1>,<val2>,<val3>,<val4>,<val5>;
  7785. BUILDIN_FUNC(bonus)
  7786. {
  7787. int type;
  7788. int val1;
  7789. int val2 = 0;
  7790. int val3 = 0;
  7791. int val4 = 0;
  7792. int val5 = 0;
  7793. TBL_PC* sd;
  7794. struct script_data *data;
  7795. sd = script_rid2sd(st);
  7796. if( sd == NULL )
  7797. return SCRIPT_CMD_SUCCESS; // no player attached
  7798. type = script_getnum(st,2);
  7799. switch( type ) {
  7800. case SP_AUTOSPELL:
  7801. case SP_AUTOSPELL_WHENHIT:
  7802. case SP_AUTOSPELL_ONSKILL:
  7803. case SP_SKILL_ATK:
  7804. case SP_SKILL_HEAL:
  7805. case SP_SKILL_HEAL2:
  7806. case SP_ADD_SKILL_BLOW:
  7807. case SP_CASTRATE:
  7808. case SP_ADDEFF_ONSKILL:
  7809. case SP_SKILL_USE_SP_RATE:
  7810. case SP_SKILL_COOLDOWN:
  7811. case SP_SKILL_FIXEDCAST:
  7812. case SP_SKILL_VARIABLECAST:
  7813. case SP_VARCASTRATE:
  7814. case SP_FIXCASTRATE:
  7815. case SP_SKILL_USE_SP:
  7816. case SP_SUB_SKILL:
  7817. // these bonuses support skill names
  7818. data = script_getdata(st, 3);
  7819. get_val(st, data); // Convert into value in case of a variable
  7820. val1 = ( data_isstring(data) ? skill_name2id(script_getstr(st,3)) : script_getnum(st,3) );
  7821. break;
  7822. default:
  7823. val1 = script_getnum(st,3);
  7824. break;
  7825. }
  7826. switch( script_lastdata(st)-2 ) {
  7827. case 1:
  7828. pc_bonus(sd, type, val1);
  7829. break;
  7830. case 2:
  7831. val2 = script_getnum(st,4);
  7832. pc_bonus2(sd, type, val1, val2);
  7833. break;
  7834. case 3:
  7835. val2 = script_getnum(st,4);
  7836. val3 = script_getnum(st,5);
  7837. pc_bonus3(sd, type, val1, val2, val3);
  7838. break;
  7839. case 4:
  7840. data = script_getdata(st, 4);
  7841. get_val(st, data); // Convert into value in case of a variable
  7842. if( type == SP_AUTOSPELL_ONSKILL && data_isstring(data) )
  7843. val2 = skill_name2id(script_getstr(st,4)); // 2nd value can be skill name
  7844. else
  7845. val2 = script_getnum(st,4);
  7846. val3 = script_getnum(st,5);
  7847. val4 = script_getnum(st,6);
  7848. pc_bonus4(sd, type, val1, val2, val3, val4);
  7849. break;
  7850. case 5:
  7851. data = script_getdata(st, 4);
  7852. get_val(st, data); // Convert into value in case of a variable
  7853. if( type == SP_AUTOSPELL_ONSKILL && data_isstring(data) )
  7854. val2 = skill_name2id(script_getstr(st,4)); // 2nd value can be skill name
  7855. else
  7856. val2 = script_getnum(st,4);
  7857. val3 = script_getnum(st,5);
  7858. val4 = script_getnum(st,6);
  7859. val5 = script_getnum(st,7);
  7860. pc_bonus5(sd, type, val1, val2, val3, val4, val5);
  7861. break;
  7862. default:
  7863. ShowDebug("buildin_bonus: unexpected number of arguments (%d)\n", (script_lastdata(st) - 1));
  7864. break;
  7865. }
  7866. return SCRIPT_CMD_SUCCESS;
  7867. }
  7868. BUILDIN_FUNC(autobonus)
  7869. {
  7870. unsigned int dur, pos;
  7871. short rate;
  7872. short atk_type = 0;
  7873. TBL_PC* sd;
  7874. const char *bonus_script, *other_script = NULL;
  7875. sd = script_rid2sd(st);
  7876. if( sd == NULL )
  7877. return SCRIPT_CMD_SUCCESS; // no player attached
  7878. if (current_equip_combo_pos)
  7879. pos = current_equip_combo_pos;
  7880. else
  7881. pos = sd->inventory.u.items_inventory[current_equip_item_index].equip;
  7882. if((sd->state.autobonus&pos) == pos)
  7883. return SCRIPT_CMD_SUCCESS;
  7884. rate = script_getnum(st,3);
  7885. dur = script_getnum(st,4);
  7886. bonus_script = script_getstr(st,2);
  7887. if( !rate || !dur || !pos || !bonus_script )
  7888. return SCRIPT_CMD_SUCCESS;
  7889. if( script_hasdata(st,5) )
  7890. atk_type = script_getnum(st,5);
  7891. if( script_hasdata(st,6) )
  7892. other_script = script_getstr(st,6);
  7893. if( pc_addautobonus(sd->autobonus,ARRAYLENGTH(sd->autobonus),
  7894. bonus_script,rate,dur,atk_type,other_script,pos,false) )
  7895. {
  7896. script_add_autobonus(bonus_script);
  7897. if( other_script )
  7898. script_add_autobonus(other_script);
  7899. }
  7900. return SCRIPT_CMD_SUCCESS;
  7901. }
  7902. BUILDIN_FUNC(autobonus2)
  7903. {
  7904. unsigned int dur, pos;
  7905. short rate;
  7906. short atk_type = 0;
  7907. TBL_PC* sd;
  7908. const char *bonus_script, *other_script = NULL;
  7909. sd = script_rid2sd(st);
  7910. if( sd == NULL )
  7911. return SCRIPT_CMD_SUCCESS; // no player attached
  7912. if (current_equip_combo_pos)
  7913. pos = current_equip_combo_pos;
  7914. else
  7915. pos = sd->inventory.u.items_inventory[current_equip_item_index].equip;
  7916. if((sd->state.autobonus&pos) == pos)
  7917. return SCRIPT_CMD_SUCCESS;
  7918. rate = script_getnum(st,3);
  7919. dur = script_getnum(st,4);
  7920. bonus_script = script_getstr(st,2);
  7921. if( !rate || !dur || !pos || !bonus_script )
  7922. return SCRIPT_CMD_SUCCESS;
  7923. if( script_hasdata(st,5) )
  7924. atk_type = script_getnum(st,5);
  7925. if( script_hasdata(st,6) )
  7926. other_script = script_getstr(st,6);
  7927. if( pc_addautobonus(sd->autobonus2,ARRAYLENGTH(sd->autobonus2),
  7928. bonus_script,rate,dur,atk_type,other_script,pos,false) )
  7929. {
  7930. script_add_autobonus(bonus_script);
  7931. if( other_script )
  7932. script_add_autobonus(other_script);
  7933. }
  7934. return SCRIPT_CMD_SUCCESS;
  7935. }
  7936. BUILDIN_FUNC(autobonus3)
  7937. {
  7938. unsigned int dur, pos;
  7939. short rate,atk_type;
  7940. TBL_PC* sd;
  7941. const char *bonus_script, *other_script = NULL;
  7942. struct script_data *data;
  7943. sd = script_rid2sd(st);
  7944. if( sd == NULL )
  7945. return SCRIPT_CMD_SUCCESS; // no player attached
  7946. if (current_equip_combo_pos)
  7947. pos = current_equip_combo_pos;
  7948. else
  7949. pos = sd->inventory.u.items_inventory[current_equip_item_index].equip;
  7950. if((sd->state.autobonus&pos) == pos)
  7951. return SCRIPT_CMD_SUCCESS;
  7952. rate = script_getnum(st,3);
  7953. dur = script_getnum(st,4);
  7954. data = script_getdata(st, 5);
  7955. get_val(st, data); // Convert into value in case of a variable
  7956. atk_type = ( data_isstring(data) ? skill_name2id(script_getstr(st,5)) : script_getnum(st,5) );
  7957. bonus_script = script_getstr(st,2);
  7958. if( !rate || !dur || !pos || !atk_type || !bonus_script )
  7959. return SCRIPT_CMD_SUCCESS;
  7960. if( script_hasdata(st,6) )
  7961. other_script = script_getstr(st,6);
  7962. if( pc_addautobonus(sd->autobonus3,ARRAYLENGTH(sd->autobonus3),
  7963. bonus_script,rate,dur,atk_type,other_script,pos,true) )
  7964. {
  7965. script_add_autobonus(bonus_script);
  7966. if( other_script )
  7967. script_add_autobonus(other_script);
  7968. }
  7969. return SCRIPT_CMD_SUCCESS;
  7970. }
  7971. /// Changes the level of a player skill.
  7972. /// <flag> defaults to 1
  7973. /// <flag>=0 : set the level of the skill
  7974. /// <flag>=1 : set the temporary level of the skill
  7975. /// <flag>=2 : add to the level of the skill
  7976. ///
  7977. /// skill <skill id>,<level>,<flag>
  7978. /// skill <skill id>,<level>
  7979. /// skill "<skill name>",<level>,<flag>
  7980. /// skill "<skill name>",<level>
  7981. BUILDIN_FUNC(skill)
  7982. {
  7983. int id;
  7984. int level;
  7985. int flag = ADDSKILL_TEMP;
  7986. TBL_PC* sd;
  7987. struct script_data *data;
  7988. const char* command = script_getfuncname(st);
  7989. sd = script_rid2sd(st);
  7990. if( sd == NULL )
  7991. return SCRIPT_CMD_SUCCESS;// no player attached, report source
  7992. if (strcmpi(command, "addtoskill") == 0)
  7993. flag = ADDSKILL_TEMP_ADDLEVEL;
  7994. data = script_getdata(st, 2);
  7995. get_val(st, data); // Convert into value in case of a variable
  7996. id = ( data_isstring(data) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  7997. level = script_getnum(st,3);
  7998. if( script_hasdata(st,4) )
  7999. flag = script_getnum(st,4);
  8000. pc_skill(sd, id, level, (enum e_addskill_type)flag);
  8001. return SCRIPT_CMD_SUCCESS;
  8002. }
  8003. /// Increases the level of a guild skill.
  8004. ///
  8005. /// guildskill <skill id>,<amount>;
  8006. /// guildskill "<skill name>",<amount>;
  8007. BUILDIN_FUNC(guildskill)
  8008. {
  8009. int id;
  8010. int level;
  8011. TBL_PC* sd;
  8012. int i;
  8013. struct script_data *data;
  8014. sd = script_rid2sd(st);
  8015. if( sd == NULL )
  8016. return SCRIPT_CMD_SUCCESS;// no player attached, report source
  8017. data = script_getdata(st, 2);
  8018. get_val(st, data); // Convert into value in case of a variable
  8019. id = ( data_isstring(data) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  8020. level = script_getnum(st,3);
  8021. for( i=0; i < level; i++ )
  8022. guild_skillup(sd, id);
  8023. return SCRIPT_CMD_SUCCESS;
  8024. }
  8025. /// Returns the level of the player skill.
  8026. ///
  8027. /// getskilllv(<skill id>) -> <level>
  8028. /// getskilllv("<skill name>") -> <level>
  8029. BUILDIN_FUNC(getskilllv)
  8030. {
  8031. int id;
  8032. TBL_PC* sd;
  8033. struct script_data *data;
  8034. sd = script_rid2sd(st);
  8035. if( sd == NULL )
  8036. return SCRIPT_CMD_SUCCESS;// no player attached, report source
  8037. data = script_getdata(st, 2);
  8038. get_val(st, data); // Convert into value in case of a variable
  8039. id = ( data_isstring(data) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  8040. script_pushint(st, pc_checkskill(sd,id));
  8041. return SCRIPT_CMD_SUCCESS;
  8042. }
  8043. /// Returns the level of the guild skill.
  8044. ///
  8045. /// getgdskilllv(<guild id>,<skill id>) -> <level>
  8046. /// getgdskilllv(<guild id>,"<skill name>") -> <level>
  8047. BUILDIN_FUNC(getgdskilllv)
  8048. {
  8049. int guild_id;
  8050. uint16 skill_id;
  8051. struct guild* g;
  8052. struct script_data *data;
  8053. guild_id = script_getnum(st,2);
  8054. data = script_getdata(st, 3);
  8055. get_val(st, data); // Convert into value in case of a variable
  8056. skill_id = ( data_isstring(data) ? skill_name2id(script_getstr(st,3)) : script_getnum(st,3) );
  8057. g = guild_search(guild_id);
  8058. if( g == NULL )
  8059. script_pushint(st, -1);
  8060. else
  8061. script_pushint(st, guild_checkskill(g,skill_id));
  8062. return SCRIPT_CMD_SUCCESS;
  8063. }
  8064. /// Returns the 'basic_skill_check' setting.
  8065. /// This config determines if the server checks the skill level of NV_BASIC
  8066. /// before allowing the basic actions.
  8067. ///
  8068. /// basicskillcheck() -> <bool>
  8069. BUILDIN_FUNC(basicskillcheck)
  8070. {
  8071. script_pushint(st, battle_config.basic_skill_check);
  8072. return SCRIPT_CMD_SUCCESS;
  8073. }
  8074. /// Returns the GM level of the player.
  8075. ///
  8076. /// getgmlevel({<char_id>}) -> <level>
  8077. BUILDIN_FUNC(getgmlevel)
  8078. {
  8079. TBL_PC* sd;
  8080. if (!script_charid2sd(2,sd))
  8081. return SCRIPT_CMD_FAILURE;
  8082. script_pushint(st, pc_get_group_level(sd));
  8083. return SCRIPT_CMD_SUCCESS;
  8084. }
  8085. /// Returns the group ID of the player.
  8086. ///
  8087. /// getgroupid({<char_id>}) -> <int>
  8088. BUILDIN_FUNC(getgroupid)
  8089. {
  8090. TBL_PC* sd;
  8091. if (!script_charid2sd(2,sd))
  8092. return SCRIPT_CMD_FAILURE;
  8093. script_pushint(st, pc_get_group_id(sd));
  8094. return SCRIPT_CMD_SUCCESS;
  8095. }
  8096. /// Terminates the execution of this script instance.
  8097. ///
  8098. /// end
  8099. BUILDIN_FUNC(end)
  8100. {
  8101. TBL_PC* sd;
  8102. sd = map_id2sd(st->rid);
  8103. st->state = END;
  8104. if (st->stack->defsp >= 1 && st->stack->stack_data[st->stack->defsp-1].type == C_RETINFO) {
  8105. int i;
  8106. for(i = 0; i < st->stack->sp; i++) {
  8107. if (st->stack->stack_data[i].type == C_RETINFO) { // Grab the first, aka the original
  8108. struct script_retinfo *ri = st->stack->stack_data[i].u.ri;
  8109. st->script = ri->script;
  8110. break;
  8111. }
  8112. }
  8113. }
  8114. if( st->mes_active )
  8115. st->mes_active = 0;
  8116. if (sd){
  8117. if (sd->state.callshop == 0)
  8118. clif_scriptclose(sd, st->oid); // If a menu/select/prompt is active, close it.
  8119. else
  8120. sd->state.callshop = 0;
  8121. }
  8122. return SCRIPT_CMD_SUCCESS;
  8123. }
  8124. /// Checks if the player has that effect state (option).
  8125. ///
  8126. /// checkoption(<option>{,<char_id>}) -> <bool>
  8127. BUILDIN_FUNC(checkoption)
  8128. {
  8129. int option;
  8130. TBL_PC* sd;
  8131. if (!script_charid2sd(3,sd))
  8132. return SCRIPT_CMD_FAILURE;
  8133. option = script_getnum(st,2);
  8134. if( sd->sc.option&option )
  8135. script_pushint(st, 1);
  8136. else
  8137. script_pushint(st, 0);
  8138. return SCRIPT_CMD_SUCCESS;
  8139. }
  8140. /// Checks if the player is in that body state (opt1).
  8141. ///
  8142. /// checkoption1(<opt1>{,<char_id>}) -> <bool>
  8143. BUILDIN_FUNC(checkoption1)
  8144. {
  8145. int opt1;
  8146. TBL_PC* sd;
  8147. if (!script_charid2sd(3,sd))
  8148. return SCRIPT_CMD_FAILURE;
  8149. opt1 = script_getnum(st,2);
  8150. if( sd->sc.opt1 == opt1 )
  8151. script_pushint(st, 1);
  8152. else
  8153. script_pushint(st, 0);
  8154. return SCRIPT_CMD_SUCCESS;
  8155. }
  8156. /// Checks if the player has that health state (opt2).
  8157. ///
  8158. /// checkoption2(<opt2>{,<char_id>}) -> <bool>
  8159. BUILDIN_FUNC(checkoption2)
  8160. {
  8161. int opt2;
  8162. TBL_PC* sd;
  8163. if (!script_charid2sd(3,sd))
  8164. return SCRIPT_CMD_FAILURE;
  8165. opt2 = script_getnum(st,2);
  8166. if( sd->sc.opt2&opt2 )
  8167. script_pushint(st, 1);
  8168. else
  8169. script_pushint(st, 0);
  8170. return SCRIPT_CMD_SUCCESS;
  8171. }
  8172. /// Changes the effect state (option) of the player.
  8173. /// <flag> defaults to 1
  8174. /// <flag>=0 : removes the option
  8175. /// <flag>=other : adds the option
  8176. ///
  8177. /// setoption <option>{,<flag>{,<char_id>}};
  8178. BUILDIN_FUNC(setoption)
  8179. {
  8180. int option;
  8181. int flag = 1;
  8182. TBL_PC* sd;
  8183. if (!script_charid2sd(4,sd))
  8184. return SCRIPT_CMD_FAILURE;
  8185. option = script_getnum(st,2);
  8186. if( script_hasdata(st,3) )
  8187. flag = script_getnum(st,3);
  8188. else if( !option ){// Request to remove everything.
  8189. flag = 0;
  8190. option = OPTION_FALCON|OPTION_RIDING;
  8191. #ifndef NEW_CARTS
  8192. option |= OPTION_CART;
  8193. #endif
  8194. }
  8195. if( flag ){// Add option
  8196. if( option&OPTION_WEDDING && !battle_config.wedding_modifydisplay )
  8197. option &= ~OPTION_WEDDING;// Do not show the wedding sprites
  8198. pc_setoption(sd, sd->sc.option|option);
  8199. } else// Remove option
  8200. pc_setoption(sd, sd->sc.option&~option);
  8201. return SCRIPT_CMD_SUCCESS;
  8202. }
  8203. /// Returns if the player has a cart.
  8204. ///
  8205. /// checkcart({char_id}) -> <bool>
  8206. ///
  8207. /// @author Valaris
  8208. BUILDIN_FUNC(checkcart)
  8209. {
  8210. TBL_PC* sd;
  8211. if (!script_charid2sd(2,sd))
  8212. return SCRIPT_CMD_FAILURE;
  8213. if( pc_iscarton(sd) )
  8214. script_pushint(st, 1);
  8215. else
  8216. script_pushint(st, 0);
  8217. return SCRIPT_CMD_SUCCESS;
  8218. }
  8219. /// Sets the cart of the player.
  8220. /// <type> defaults to 1
  8221. /// <type>=0 : removes the cart
  8222. /// <type>=1 : Normal cart
  8223. /// <type>=2 : Wooden cart
  8224. /// <type>=3 : Covered cart with flowers and ferns
  8225. /// <type>=4 : Wooden cart with a Panda doll on the back
  8226. /// <type>=5 : Normal cart with bigger wheels, a roof and a banner on the back
  8227. ///
  8228. /// setcart {<type>{,<char_id>}};
  8229. BUILDIN_FUNC(setcart)
  8230. {
  8231. int type = 1;
  8232. TBL_PC* sd;
  8233. if (!script_charid2sd(3,sd))
  8234. return SCRIPT_CMD_FAILURE;
  8235. if( script_hasdata(st,2) )
  8236. type = script_getnum(st,2);
  8237. pc_setcart(sd, type);
  8238. return SCRIPT_CMD_SUCCESS;
  8239. }
  8240. /// Returns if the player has a falcon.
  8241. ///
  8242. /// checkfalcon({char_id}) -> <bool>
  8243. ///
  8244. /// @author Valaris
  8245. BUILDIN_FUNC(checkfalcon)
  8246. {
  8247. TBL_PC* sd;
  8248. if (!script_charid2sd(2,sd))
  8249. return SCRIPT_CMD_FAILURE;
  8250. if( pc_isfalcon(sd) )
  8251. script_pushint(st, 1);
  8252. else
  8253. script_pushint(st, 0);
  8254. return SCRIPT_CMD_SUCCESS;
  8255. }
  8256. /// Sets if the player has a falcon or not.
  8257. /// <flag> defaults to 1
  8258. ///
  8259. /// setfalcon {<flag>{,<char_id>}};
  8260. BUILDIN_FUNC(setfalcon)
  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_setfalcon(sd, flag);
  8269. return SCRIPT_CMD_SUCCESS;
  8270. }
  8271. /// Returns if the player is riding.
  8272. ///
  8273. /// checkriding({char_id}) -> <bool>
  8274. ///
  8275. /// @author Valaris
  8276. BUILDIN_FUNC(checkriding)
  8277. {
  8278. TBL_PC* sd;
  8279. if (!script_charid2sd(2,sd))
  8280. return SCRIPT_CMD_FAILURE;
  8281. if( pc_isriding(sd) )
  8282. script_pushint(st, 1);
  8283. else
  8284. script_pushint(st, 0);
  8285. return SCRIPT_CMD_SUCCESS;
  8286. }
  8287. /// Sets if the player is riding.
  8288. /// <flag> defaults to 1
  8289. ///
  8290. /// setriding {<flag>{,<char_id>}};
  8291. BUILDIN_FUNC(setriding)
  8292. {
  8293. int flag = 1;
  8294. TBL_PC* sd;
  8295. if (!script_charid2sd(3,sd))
  8296. return SCRIPT_CMD_FAILURE;
  8297. if( script_hasdata(st,2) )
  8298. flag = script_getnum(st,2);
  8299. pc_setriding(sd, flag);
  8300. return SCRIPT_CMD_SUCCESS;
  8301. }
  8302. /// Returns if the player has a warg.
  8303. ///
  8304. /// checkwug({char_id}) -> <bool>
  8305. ///
  8306. BUILDIN_FUNC(checkwug)
  8307. {
  8308. TBL_PC* sd;
  8309. if (!script_charid2sd(2,sd))
  8310. return SCRIPT_CMD_FAILURE;
  8311. if( pc_iswug(sd) || pc_isridingwug(sd) )
  8312. script_pushint(st, 1);
  8313. else
  8314. script_pushint(st, 0);
  8315. return SCRIPT_CMD_SUCCESS;
  8316. }
  8317. /// Returns if the player is wearing MADO Gear.
  8318. ///
  8319. /// checkmadogear({<char_id>}) -> <bool>
  8320. ///
  8321. BUILDIN_FUNC(checkmadogear)
  8322. {
  8323. TBL_PC* sd;
  8324. if (!script_charid2sd(2,sd))
  8325. return SCRIPT_CMD_FAILURE;
  8326. if( pc_ismadogear(sd) )
  8327. script_pushint(st, 1);
  8328. else
  8329. script_pushint(st, 0);
  8330. return SCRIPT_CMD_SUCCESS;
  8331. }
  8332. /// Sets if the player is riding MADO Gear.
  8333. /// <flag> defaults to 1
  8334. ///
  8335. /// setmadogear {<flag>{,<char_id>}};
  8336. BUILDIN_FUNC(setmadogear)
  8337. {
  8338. int flag = 1;
  8339. TBL_PC* sd;
  8340. if (!script_charid2sd(3,sd))
  8341. return SCRIPT_CMD_FAILURE;
  8342. if( script_hasdata(st,2) )
  8343. flag = script_getnum(st,2);
  8344. pc_setmadogear(sd, flag);
  8345. return SCRIPT_CMD_SUCCESS;
  8346. }
  8347. /// Sets the save point of the player.
  8348. ///
  8349. /// save "<map name>",<x>,<y>{,{<range x>,<range y>,}<char_id>}
  8350. /// savepoint "<map name>",<x>,<y>{,{<range x>,<range y>,}<char_id>}
  8351. BUILDIN_FUNC(savepoint)
  8352. {
  8353. int x, y, m, cid_pos = 5;
  8354. unsigned short map_idx;
  8355. const char* str;
  8356. TBL_PC* sd;
  8357. if (script_lastdata(st) > 5)
  8358. cid_pos = 7;
  8359. if (!script_charid2sd(cid_pos,sd))
  8360. return SCRIPT_CMD_FAILURE;// no player attached, report source
  8361. str = script_getstr(st, 2);
  8362. map_idx = mapindex_name2id(str);
  8363. if( !map_idx )
  8364. return SCRIPT_CMD_FAILURE;
  8365. x = script_getnum(st,3);
  8366. y = script_getnum(st,4);
  8367. m = map_mapindex2mapid(map_idx);
  8368. if (cid_pos == 7) {
  8369. int dx = script_getnum(st,5), dy = script_getnum(st,6),
  8370. x1 = x + dx, y1 = y + dy,
  8371. x0 = x - dx, y0 = y - dy;
  8372. uint8 n = 10;
  8373. do {
  8374. x = x0 + rnd()%(x1-x0+1);
  8375. y = y0 + rnd()%(y1-y0+1);
  8376. } while (m != -1 && (--n) > 0 && !map_getcell(m, x, y, CELL_CHKPASS));
  8377. }
  8378. // Check for valid coordinates if map in local map-server
  8379. if (m != -1 && !map_getcell(m, x, y, CELL_CHKPASS)) {
  8380. ShowError("buildin_savepoint: Invalid coordinates %d,%d at map %s.\n", x, y, str);
  8381. return SCRIPT_CMD_FAILURE;
  8382. }
  8383. pc_setsavepoint(sd, map_idx, x, y);
  8384. return SCRIPT_CMD_SUCCESS;
  8385. }
  8386. /*==========================================
  8387. * GetTimeTick(0: System Tick, 1: Time Second Tick, 2: Unix epoch)
  8388. *------------------------------------------*/
  8389. BUILDIN_FUNC(gettimetick) /* Asgard Version */
  8390. {
  8391. int type;
  8392. time_t timer;
  8393. struct tm *t;
  8394. type=script_getnum(st,2);
  8395. switch(type){
  8396. case 2:
  8397. //type 2:(Get the number of seconds elapsed since 00:00 hours, Jan 1, 1970 UTC
  8398. // from the system clock.)
  8399. script_pushint(st,(int)time(NULL));
  8400. break;
  8401. case 1:
  8402. //type 1:(Second Ticks: 0-86399, 00:00:00-23:59:59)
  8403. time(&timer);
  8404. t=localtime(&timer);
  8405. script_pushint(st,((t->tm_hour)*3600+(t->tm_min)*60+t->tm_sec));
  8406. break;
  8407. case 0:
  8408. default:
  8409. //type 0:(System Ticks)
  8410. script_pushint(st,gettick());
  8411. break;
  8412. }
  8413. return SCRIPT_CMD_SUCCESS;
  8414. }
  8415. /*==========================================
  8416. * GetTime(Type)
  8417. *
  8418. * Returns the current value of a certain date type.
  8419. * Possible types are:
  8420. * - DT_SECOND Current seconds
  8421. * - DT_MINUTE Current minute
  8422. * - DT_HOUR Current hour
  8423. * - DT_DAYOFWEEK Day of current week
  8424. * - DT_DAYOFMONTH Day of current month
  8425. * - DT_MONTH Current month
  8426. * - DT_YEAR Current year
  8427. * - DT_DAYOFYEAR Day of current year
  8428. *
  8429. * If none of the above types is supplied -1 will be returned to the script
  8430. * and the script source will be reported into the mapserver console.
  8431. *------------------------------------------*/
  8432. BUILDIN_FUNC(gettime)
  8433. {
  8434. int type;
  8435. type = script_getnum(st,2);
  8436. if( type <= DT_MIN || type >= DT_MAX ){
  8437. ShowError( "buildin_gettime: Invalid date type %d\n", type );
  8438. script_reportsrc(st);
  8439. script_pushint(st,-1);
  8440. }else{
  8441. script_pushint(st,date_get((enum e_date_type)type));
  8442. }
  8443. return SCRIPT_CMD_SUCCESS;
  8444. }
  8445. /*==========================================
  8446. * GetTimeStr("TimeFMT", Length);
  8447. *------------------------------------------*/
  8448. BUILDIN_FUNC(gettimestr)
  8449. {
  8450. char *tmpstr;
  8451. const char *fmtstr;
  8452. int maxlen;
  8453. time_t now = time(NULL);
  8454. fmtstr=script_getstr(st,2);
  8455. maxlen=script_getnum(st,3);
  8456. tmpstr=(char *)aMalloc((maxlen+1)*sizeof(char));
  8457. strftime(tmpstr,maxlen,fmtstr,localtime(&now));
  8458. tmpstr[maxlen]='\0';
  8459. script_pushstr(st,tmpstr);
  8460. return SCRIPT_CMD_SUCCESS;
  8461. }
  8462. /*==========================================
  8463. * Open player storage
  8464. *------------------------------------------*/
  8465. BUILDIN_FUNC(openstorage)
  8466. {
  8467. TBL_PC* sd;
  8468. sd = script_rid2sd(st);
  8469. if( sd == NULL )
  8470. return SCRIPT_CMD_SUCCESS;
  8471. storage_storageopen(sd);
  8472. return SCRIPT_CMD_SUCCESS;
  8473. }
  8474. BUILDIN_FUNC(guildopenstorage)
  8475. {
  8476. TBL_PC* sd;
  8477. int ret;
  8478. sd = script_rid2sd(st);
  8479. if( sd == NULL )
  8480. return SCRIPT_CMD_SUCCESS;
  8481. ret = storage_guild_storageopen(sd);
  8482. script_pushint(st,ret);
  8483. return SCRIPT_CMD_SUCCESS;
  8484. }
  8485. /*==========================================
  8486. * Make player use a skill trought item usage
  8487. *------------------------------------------*/
  8488. /// itemskill <skill id>,<level>
  8489. /// itemskill "<skill name>",<level>
  8490. BUILDIN_FUNC(itemskill)
  8491. {
  8492. int id;
  8493. int lv;
  8494. bool keep_requirement;
  8495. TBL_PC* sd;
  8496. struct script_data *data;
  8497. sd = script_rid2sd(st);
  8498. if( sd == NULL || sd->ud.skilltimer != INVALID_TIMER )
  8499. return SCRIPT_CMD_SUCCESS;
  8500. data = script_getdata(st, 2);
  8501. get_val(st, data); // Convert into value in case of a variable
  8502. id = ( data_isstring(data) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  8503. lv = script_getnum(st,3);
  8504. if (script_hasdata(st, 4)) {
  8505. keep_requirement = (script_getnum(st, 4) != 0);
  8506. } else {
  8507. keep_requirement = false;
  8508. }
  8509. sd->skillitem=id;
  8510. sd->skillitemlv=lv;
  8511. sd->skillitem_keep_requirement = keep_requirement;
  8512. clif_item_skill(sd,id,lv);
  8513. return SCRIPT_CMD_SUCCESS;
  8514. }
  8515. /*==========================================
  8516. * Attempt to create an item
  8517. *------------------------------------------*/
  8518. BUILDIN_FUNC(produce)
  8519. {
  8520. int trigger;
  8521. TBL_PC* sd;
  8522. sd = script_rid2sd(st);
  8523. if( sd == NULL )
  8524. return SCRIPT_CMD_SUCCESS;
  8525. trigger=script_getnum(st,2);
  8526. clif_skill_produce_mix_list(sd, -1, trigger);
  8527. return SCRIPT_CMD_SUCCESS;
  8528. }
  8529. /*==========================================
  8530. *
  8531. *------------------------------------------*/
  8532. BUILDIN_FUNC(cooking)
  8533. {
  8534. int trigger;
  8535. TBL_PC* sd;
  8536. sd = script_rid2sd(st);
  8537. if( sd == NULL )
  8538. return SCRIPT_CMD_SUCCESS;
  8539. trigger=script_getnum(st,2);
  8540. clif_cooking_list(sd, trigger, AM_PHARMACY, 1, 1);
  8541. return SCRIPT_CMD_SUCCESS;
  8542. }
  8543. /*==========================================
  8544. * Create a pet
  8545. *------------------------------------------*/
  8546. BUILDIN_FUNC(makepet)
  8547. {
  8548. TBL_PC* sd;
  8549. int id,pet_id;
  8550. id=script_getnum(st,2);
  8551. sd = script_rid2sd(st);
  8552. if( sd == NULL )
  8553. return SCRIPT_CMD_SUCCESS;
  8554. pet_id = search_petDB_index(id, PET_CLASS);
  8555. if (pet_id < 0)
  8556. pet_id = search_petDB_index(id, PET_EGG);
  8557. if (pet_id >= 0 && sd) {
  8558. sd->catch_target_class = pet_db[pet_id].class_;
  8559. intif_create_pet(
  8560. sd->status.account_id, sd->status.char_id,
  8561. (short)pet_db[pet_id].class_, (short)mob_db(pet_db[pet_id].class_)->lv,
  8562. (short)pet_db[pet_id].EggID, 0, (short)pet_db[pet_id].intimate,
  8563. 100, 0, 1, pet_db[pet_id].jname);
  8564. }
  8565. return SCRIPT_CMD_SUCCESS;
  8566. }
  8567. /**
  8568. * Give player exp base,job * quest_exp_rate/100
  8569. * getexp <base xp>,<job xp>{,<char_id>};
  8570. **/
  8571. BUILDIN_FUNC(getexp)
  8572. {
  8573. TBL_PC* sd;
  8574. int base=0,job=0;
  8575. double bonus;
  8576. if (!script_charid2sd(4,sd))
  8577. return SCRIPT_CMD_FAILURE;
  8578. base=script_getnum(st,2);
  8579. job =script_getnum(st,3);
  8580. if(base<0 || job<0)
  8581. return SCRIPT_CMD_SUCCESS;
  8582. // bonus for npc-given exp
  8583. bonus = battle_config.quest_exp_rate / 100.;
  8584. if (base)
  8585. base = (int) cap_value(base * bonus, 0, INT_MAX);
  8586. if (job)
  8587. job = (int) cap_value(job * bonus, 0, INT_MAX);
  8588. pc_gainexp(sd, NULL, base, job, 1);
  8589. return SCRIPT_CMD_SUCCESS;
  8590. }
  8591. /*==========================================
  8592. * Gain guild exp [Celest]
  8593. *------------------------------------------*/
  8594. BUILDIN_FUNC(guildgetexp)
  8595. {
  8596. TBL_PC* sd;
  8597. int exp;
  8598. sd = script_rid2sd(st);
  8599. if( sd == NULL )
  8600. return SCRIPT_CMD_SUCCESS;
  8601. exp = script_getnum(st,2);
  8602. if(exp < 0)
  8603. return SCRIPT_CMD_SUCCESS;
  8604. if(sd && sd->status.guild_id > 0)
  8605. guild_getexp (sd, exp);
  8606. return SCRIPT_CMD_SUCCESS;
  8607. }
  8608. /*==========================================
  8609. * Changes the guild master of a guild [Skotlex]
  8610. *------------------------------------------*/
  8611. BUILDIN_FUNC(guildchangegm)
  8612. {
  8613. TBL_PC *sd;
  8614. int guild_id;
  8615. const char *name;
  8616. guild_id = script_getnum(st,2);
  8617. name = script_getstr(st,3);
  8618. sd=map_nick2sd(name,false);
  8619. if (!sd)
  8620. script_pushint(st,0);
  8621. else
  8622. script_pushint(st,guild_gm_change(guild_id, sd));
  8623. return SCRIPT_CMD_SUCCESS;
  8624. }
  8625. /*==========================================
  8626. * Spawn a monster:
  8627. * *monster "<map name>",<x>,<y>,"<name to show>",<mob id>,<amount>{,"<event label>",<size>,<ai>};
  8628. *------------------------------------------*/
  8629. BUILDIN_FUNC(monster)
  8630. {
  8631. const char* mapn = script_getstr(st,2);
  8632. int x = script_getnum(st,3);
  8633. int y = script_getnum(st,4);
  8634. const char* str = script_getstr(st,5);
  8635. int class_ = script_getnum(st,6);
  8636. int amount = script_getnum(st,7);
  8637. const char* event = "";
  8638. unsigned int size = SZ_SMALL;
  8639. unsigned int ai = AI_NONE;
  8640. struct map_session_data* sd;
  8641. int16 m;
  8642. int i;
  8643. if (script_hasdata(st, 8)) {
  8644. event = script_getstr(st, 8);
  8645. check_event(st, event);
  8646. }
  8647. if (script_hasdata(st, 9)) {
  8648. size = script_getnum(st, 9);
  8649. if (size > 3) {
  8650. ShowWarning("buildin_monster: Attempted to spawn non-existing size %d for monster class %d\n", size, class_);
  8651. return SCRIPT_CMD_FAILURE;
  8652. }
  8653. }
  8654. if (script_hasdata(st, 10)) {
  8655. ai = script_getnum(st, 10);
  8656. if (ai >= AI_MAX) {
  8657. ShowWarning("buildin_monster: Attempted to spawn non-existing ai %d for monster class %d\n", ai, class_);
  8658. return SCRIPT_CMD_FAILURE;
  8659. }
  8660. }
  8661. if (class_ >= 0 && !mobdb_checkid(class_)) {
  8662. ShowWarning("buildin_monster: Attempted to spawn non-existing monster class %d\n", class_);
  8663. return SCRIPT_CMD_FAILURE;
  8664. }
  8665. sd = map_id2sd(st->rid);
  8666. if (sd && strcmp(mapn, "this") == 0)
  8667. m = sd->bl.m;
  8668. else
  8669. m = map_mapname2mapid(mapn);
  8670. for(i = 0; i < amount; i++) { //not optimised
  8671. int mobid = mob_once_spawn(sd, m, x, y, str, class_, 1, event, size, ai);
  8672. if (mobid)
  8673. mapreg_setreg(reference_uid(add_str("$@mobid"), i), mobid);
  8674. }
  8675. return SCRIPT_CMD_SUCCESS;
  8676. }
  8677. /*==========================================
  8678. * Request List of Monster Drops
  8679. *------------------------------------------*/
  8680. BUILDIN_FUNC(getmobdrops)
  8681. {
  8682. int class_ = script_getnum(st,2);
  8683. int i, j = 0;
  8684. struct mob_db *mob;
  8685. if( !mobdb_checkid(class_) )
  8686. {
  8687. script_pushint(st, 0);
  8688. return SCRIPT_CMD_SUCCESS;
  8689. }
  8690. mob = mob_db(class_);
  8691. for( i = 0; i < MAX_MOB_DROP; i++ )
  8692. {
  8693. if( mob->dropitem[i].nameid < 1 )
  8694. continue;
  8695. if( itemdb_exists(mob->dropitem[i].nameid) == NULL )
  8696. continue;
  8697. mapreg_setreg(reference_uid(add_str("$@MobDrop_item"), j), mob->dropitem[i].nameid);
  8698. mapreg_setreg(reference_uid(add_str("$@MobDrop_rate"), j), mob->dropitem[i].p);
  8699. j++;
  8700. }
  8701. mapreg_setreg(add_str("$@MobDrop_count"), j);
  8702. script_pushint(st, 1);
  8703. return SCRIPT_CMD_SUCCESS;
  8704. }
  8705. /*==========================================
  8706. * Spawn a monster in a random location
  8707. * in x0,x1,y0,y1 area.
  8708. *------------------------------------------*/
  8709. BUILDIN_FUNC(areamonster)
  8710. {
  8711. const char* mapn = script_getstr(st,2);
  8712. int x0 = script_getnum(st,3);
  8713. int y0 = script_getnum(st,4);
  8714. int x1 = script_getnum(st,5);
  8715. int y1 = script_getnum(st,6);
  8716. const char* str = script_getstr(st,7);
  8717. int class_ = script_getnum(st,8);
  8718. int amount = script_getnum(st,9);
  8719. const char* event = "";
  8720. unsigned int size = SZ_SMALL;
  8721. unsigned int ai = AI_NONE;
  8722. struct map_session_data* sd;
  8723. int16 m;
  8724. int i;
  8725. if (script_hasdata(st,10)) {
  8726. event = script_getstr(st, 10);
  8727. check_event(st, event);
  8728. }
  8729. if (script_hasdata(st, 11)) {
  8730. size = script_getnum(st, 11);
  8731. if (size > 3) {
  8732. ShowWarning("buildin_monster: Attempted to spawn non-existing size %d for monster class %d\n", size, class_);
  8733. return SCRIPT_CMD_FAILURE;
  8734. }
  8735. }
  8736. if (script_hasdata(st, 12)) {
  8737. ai = script_getnum(st, 12);
  8738. if (ai >= AI_MAX) {
  8739. ShowWarning("buildin_monster: Attempted to spawn non-existing ai %d for monster class %d\n", ai, class_);
  8740. return SCRIPT_CMD_FAILURE;
  8741. }
  8742. }
  8743. sd = map_id2sd(st->rid);
  8744. if (sd && strcmp(mapn, "this") == 0)
  8745. m = sd->bl.m;
  8746. else
  8747. m = map_mapname2mapid(mapn);
  8748. for(i = 0; i < amount; i++) { //not optimised
  8749. int mobid = mob_once_spawn_area(sd, m, x0, y0, x1, y1, str, class_, 1, event, size, ai);
  8750. if (mobid)
  8751. mapreg_setreg(reference_uid(add_str("$@mobid"), i), mobid);
  8752. }
  8753. return SCRIPT_CMD_SUCCESS;
  8754. }
  8755. /*==========================================
  8756. * KillMonster subcheck, verify if mob to kill ain't got an even to handle, could be force kill by allflag
  8757. *------------------------------------------*/
  8758. static int buildin_killmonster_sub_strip(struct block_list *bl,va_list ap)
  8759. { //same fix but with killmonster instead - stripping events from mobs.
  8760. TBL_MOB* md = (TBL_MOB*)bl;
  8761. char *event=va_arg(ap,char *);
  8762. int allflag=va_arg(ap,int);
  8763. md->state.npc_killmonster = 1;
  8764. if(!allflag){
  8765. if(strcmp(event,md->npc_event)==0)
  8766. status_kill(bl);
  8767. }else{
  8768. if(!md->spawn)
  8769. status_kill(bl);
  8770. }
  8771. md->state.npc_killmonster = 0;
  8772. return SCRIPT_CMD_SUCCESS;
  8773. }
  8774. static int buildin_killmonster_sub(struct block_list *bl,va_list ap)
  8775. {
  8776. TBL_MOB* md = (TBL_MOB*)bl;
  8777. char *event=va_arg(ap,char *);
  8778. int allflag=va_arg(ap,int);
  8779. if(!allflag){
  8780. if(strcmp(event,md->npc_event)==0)
  8781. status_kill(bl);
  8782. }else{
  8783. if(!md->spawn)
  8784. status_kill(bl);
  8785. }
  8786. return SCRIPT_CMD_SUCCESS;
  8787. }
  8788. BUILDIN_FUNC(killmonster)
  8789. {
  8790. const char *mapname,*event;
  8791. int16 m,allflag=0;
  8792. mapname=script_getstr(st,2);
  8793. event=script_getstr(st,3);
  8794. if(strcmp(event,"All")==0)
  8795. allflag = 1;
  8796. else
  8797. check_event(st, event);
  8798. if( (m=map_mapname2mapid(mapname))<0 )
  8799. return SCRIPT_CMD_SUCCESS;
  8800. if( script_hasdata(st,4) ) {
  8801. if ( script_getnum(st,4) == 1 ) {
  8802. map_foreachinmap(buildin_killmonster_sub, m, BL_MOB, event ,allflag);
  8803. return SCRIPT_CMD_SUCCESS;
  8804. }
  8805. }
  8806. map_freeblock_lock();
  8807. map_foreachinmap(buildin_killmonster_sub_strip, m, BL_MOB, event ,allflag);
  8808. map_freeblock_unlock();
  8809. return SCRIPT_CMD_SUCCESS;
  8810. }
  8811. static int buildin_killmonsterall_sub_strip(struct block_list *bl,va_list ap)
  8812. { //Strips the event from the mob if it's killed the old method.
  8813. struct mob_data *md;
  8814. md = BL_CAST(BL_MOB, bl);
  8815. if (md->npc_event[0])
  8816. md->npc_event[0] = 0;
  8817. status_kill(bl);
  8818. return 0;
  8819. }
  8820. static int buildin_killmonsterall_sub(struct block_list *bl,va_list ap)
  8821. {
  8822. status_kill(bl);
  8823. return 0;
  8824. }
  8825. BUILDIN_FUNC(killmonsterall)
  8826. {
  8827. const char *mapname;
  8828. int16 m;
  8829. mapname=script_getstr(st,2);
  8830. if( (m = map_mapname2mapid(mapname))<0 )
  8831. return SCRIPT_CMD_SUCCESS;
  8832. if( script_hasdata(st,3) ) {
  8833. if ( script_getnum(st,3) == 1 ) {
  8834. map_foreachinmap(buildin_killmonsterall_sub,m,BL_MOB);
  8835. return SCRIPT_CMD_SUCCESS;
  8836. }
  8837. }
  8838. map_foreachinmap(buildin_killmonsterall_sub_strip,m,BL_MOB);
  8839. return SCRIPT_CMD_SUCCESS;
  8840. }
  8841. /*==========================================
  8842. * Creates a clone of a player.
  8843. * clone map, x, y, event, char_id, master_id, mode, flag, duration
  8844. *------------------------------------------*/
  8845. BUILDIN_FUNC(clone)
  8846. {
  8847. TBL_PC *sd, *msd=NULL;
  8848. uint32 char_id, master_id = 0, x, y, flag = 0, m;
  8849. enum e_mode mode = 0;
  8850. unsigned int duration = 0;
  8851. const char *mapname,*event;
  8852. mapname=script_getstr(st,2);
  8853. x=script_getnum(st,3);
  8854. y=script_getnum(st,4);
  8855. event=script_getstr(st,5);
  8856. char_id=script_getnum(st,6);
  8857. if( script_hasdata(st,7) )
  8858. master_id=script_getnum(st,7);
  8859. if( script_hasdata(st,8) )
  8860. mode=script_getnum(st,8);
  8861. if( script_hasdata(st,9) )
  8862. flag=script_getnum(st,9);
  8863. if( script_hasdata(st,10) )
  8864. duration=script_getnum(st,10);
  8865. check_event(st, event);
  8866. m = map_mapname2mapid(mapname);
  8867. if (m < 0)
  8868. return SCRIPT_CMD_SUCCESS;
  8869. sd = map_charid2sd(char_id);
  8870. if (master_id) {
  8871. msd = map_charid2sd(master_id);
  8872. if (msd)
  8873. master_id = msd->bl.id;
  8874. else
  8875. master_id = 0;
  8876. }
  8877. if (sd) //Return ID of newly crafted clone.
  8878. script_pushint(st,mob_clone_spawn(sd, m, x, y, event, master_id, mode, flag, 1000*duration));
  8879. else //Failed to create clone.
  8880. script_pushint(st,0);
  8881. return SCRIPT_CMD_SUCCESS;
  8882. }
  8883. /*==========================================
  8884. *------------------------------------------*/
  8885. BUILDIN_FUNC(doevent)
  8886. {
  8887. const char* event = script_getstr(st,2);
  8888. struct map_session_data* sd;
  8889. if( ( sd = script_rid2sd(st) ) == NULL )
  8890. {
  8891. return SCRIPT_CMD_SUCCESS;
  8892. }
  8893. check_event(st, event);
  8894. npc_event(sd, event, 0);
  8895. return SCRIPT_CMD_SUCCESS;
  8896. }
  8897. /*==========================================
  8898. *------------------------------------------*/
  8899. BUILDIN_FUNC(donpcevent)
  8900. {
  8901. const char* event = script_getstr(st,2);
  8902. check_event(st, event);
  8903. if( !npc_event_do(event) ) {
  8904. struct npc_data * nd = map_id2nd(st->oid);
  8905. ShowDebug("NPCEvent '%s' not found! (source: %s)\n",event,nd?nd->name:"Unknown");
  8906. script_pushint(st, 0);
  8907. } else
  8908. script_pushint(st, 1);
  8909. return SCRIPT_CMD_SUCCESS;
  8910. }
  8911. /// for Aegis compatibility
  8912. /// basically a specialized 'donpcevent', with the event specified as two arguments instead of one
  8913. BUILDIN_FUNC(cmdothernpc) // Added by RoVeRT
  8914. {
  8915. const char* npc = script_getstr(st,2);
  8916. const char* command = script_getstr(st,3);
  8917. char event[EVENT_NAME_LENGTH];
  8918. safesnprintf(event,EVENT_NAME_LENGTH, "%s::%s%s",npc,script_config.oncommand_event_name,command);
  8919. check_event(st, event);
  8920. if( npc_event_do(event) ){
  8921. script_pushint(st, true);
  8922. }else{
  8923. struct npc_data * nd = map_id2nd(st->oid);
  8924. ShowDebug("NPCEvent '%s' not found! (source: %s)\n", event, nd ? nd->name : "Unknown");
  8925. script_pushint(st, false);
  8926. }
  8927. return SCRIPT_CMD_SUCCESS;
  8928. }
  8929. /*==========================================
  8930. *------------------------------------------*/
  8931. BUILDIN_FUNC(addtimer)
  8932. {
  8933. int tick = script_getnum(st,2);
  8934. const char* event = script_getstr(st, 3);
  8935. TBL_PC* sd;
  8936. check_event(st, event);
  8937. sd = script_rid2sd(st);
  8938. if( sd == NULL )
  8939. return SCRIPT_CMD_SUCCESS;
  8940. if (!pc_addeventtimer(sd,tick,event)) {
  8941. ShowWarning("buildin_addtimer: Event timer is full, can't add new event timer. (cid:%d timer:%s)\n",sd->status.char_id,event);
  8942. return SCRIPT_CMD_FAILURE;
  8943. }
  8944. return SCRIPT_CMD_SUCCESS;
  8945. }
  8946. /*==========================================
  8947. *------------------------------------------*/
  8948. BUILDIN_FUNC(deltimer)
  8949. {
  8950. const char *event;
  8951. TBL_PC* sd;
  8952. event=script_getstr(st, 2);
  8953. sd = script_rid2sd(st);
  8954. if( sd == NULL )
  8955. return SCRIPT_CMD_SUCCESS;
  8956. check_event(st, event);
  8957. pc_deleventtimer(sd,event);
  8958. return SCRIPT_CMD_SUCCESS;
  8959. }
  8960. /*==========================================
  8961. *------------------------------------------*/
  8962. BUILDIN_FUNC(addtimercount)
  8963. {
  8964. const char *event;
  8965. int tick;
  8966. TBL_PC* sd;
  8967. tick=script_getnum(st,2);
  8968. event=script_getstr(st,3);
  8969. sd = script_rid2sd(st);
  8970. if( sd == NULL )
  8971. return SCRIPT_CMD_SUCCESS;
  8972. check_event(st, event);
  8973. pc_addeventtimercount(sd,event,tick);
  8974. return SCRIPT_CMD_SUCCESS;
  8975. }
  8976. /*==========================================
  8977. *------------------------------------------*/
  8978. BUILDIN_FUNC(initnpctimer)
  8979. {
  8980. struct npc_data *nd;
  8981. int flag = 0;
  8982. if( script_hasdata(st,3) )
  8983. { //Two arguments: NPC name and attach flag.
  8984. nd = npc_name2id(script_getstr(st, 2));
  8985. flag = script_getnum(st,3);
  8986. }
  8987. else if( script_hasdata(st,2) )
  8988. { //Check if argument is numeric (flag) or string (npc name)
  8989. struct script_data *data;
  8990. data = script_getdata(st,2);
  8991. get_val(st,data);
  8992. if( data_isstring(data) ) //NPC name
  8993. nd = npc_name2id(conv_str(st, data));
  8994. else if( data_isint(data) ) //Flag
  8995. {
  8996. nd = (struct npc_data *)map_id2bl(st->oid);
  8997. flag = conv_num(st,data);
  8998. }
  8999. else
  9000. {
  9001. ShowError("initnpctimer: invalid argument type #1 (needs be int or string)).\n");
  9002. return SCRIPT_CMD_FAILURE;
  9003. }
  9004. }
  9005. else
  9006. nd = (struct npc_data *)map_id2bl(st->oid);
  9007. if( !nd )
  9008. return SCRIPT_CMD_SUCCESS;
  9009. if( flag ) //Attach
  9010. {
  9011. TBL_PC* sd = script_rid2sd(st);
  9012. if( sd == NULL )
  9013. return SCRIPT_CMD_SUCCESS;
  9014. nd->u.scr.rid = sd->bl.id;
  9015. }
  9016. nd->u.scr.timertick = 0;
  9017. npc_settimerevent_tick(nd,0);
  9018. npc_timerevent_start(nd, st->rid);
  9019. return SCRIPT_CMD_SUCCESS;
  9020. }
  9021. /*==========================================
  9022. *------------------------------------------*/
  9023. BUILDIN_FUNC(startnpctimer)
  9024. {
  9025. struct npc_data *nd;
  9026. int flag = 0;
  9027. if( script_hasdata(st,3) )
  9028. { //Two arguments: NPC name and attach flag.
  9029. nd = npc_name2id(script_getstr(st, 2));
  9030. flag = script_getnum(st,3);
  9031. }
  9032. else if( script_hasdata(st,2) )
  9033. { //Check if argument is numeric (flag) or string (npc name)
  9034. struct script_data *data;
  9035. data = script_getdata(st,2);
  9036. get_val(st,data);
  9037. if( data_isstring(data) ) //NPC name
  9038. nd = npc_name2id(conv_str(st, data));
  9039. else if( data_isint(data) ) //Flag
  9040. {
  9041. nd = (struct npc_data *)map_id2bl(st->oid);
  9042. flag = conv_num(st,data);
  9043. }
  9044. else
  9045. {
  9046. ShowError("initnpctimer: invalid argument type #1 (needs be int or string)).\n");
  9047. return SCRIPT_CMD_FAILURE;
  9048. }
  9049. }
  9050. else
  9051. nd=(struct npc_data *)map_id2bl(st->oid);
  9052. if( !nd )
  9053. return SCRIPT_CMD_SUCCESS;
  9054. if( flag ) //Attach
  9055. {
  9056. TBL_PC* sd = script_rid2sd(st);
  9057. if( sd == NULL )
  9058. return SCRIPT_CMD_SUCCESS;
  9059. nd->u.scr.rid = sd->bl.id;
  9060. }
  9061. npc_timerevent_start(nd, st->rid);
  9062. return SCRIPT_CMD_SUCCESS;
  9063. }
  9064. /*==========================================
  9065. *------------------------------------------*/
  9066. BUILDIN_FUNC(stopnpctimer)
  9067. {
  9068. struct npc_data *nd;
  9069. int flag = 0;
  9070. if( script_hasdata(st,3) )
  9071. { //Two arguments: NPC name and attach flag.
  9072. nd = npc_name2id(script_getstr(st, 2));
  9073. flag = script_getnum(st,3);
  9074. }
  9075. else if( script_hasdata(st,2) )
  9076. { //Check if argument is numeric (flag) or string (npc name)
  9077. struct script_data *data;
  9078. data = script_getdata(st,2);
  9079. get_val(st,data);
  9080. if( data_isstring(data) ) //NPC name
  9081. nd = npc_name2id(conv_str(st, data));
  9082. else if( data_isint(data) ) //Flag
  9083. {
  9084. nd = (struct npc_data *)map_id2bl(st->oid);
  9085. flag = conv_num(st,data);
  9086. }
  9087. else
  9088. {
  9089. ShowError("initnpctimer: invalid argument type #1 (needs be int or string)).\n");
  9090. return SCRIPT_CMD_FAILURE;
  9091. }
  9092. }
  9093. else
  9094. nd=(struct npc_data *)map_id2bl(st->oid);
  9095. if( !nd )
  9096. return SCRIPT_CMD_SUCCESS;
  9097. if( flag ) //Detach
  9098. nd->u.scr.rid = 0;
  9099. npc_timerevent_stop(nd);
  9100. return SCRIPT_CMD_SUCCESS;
  9101. }
  9102. /*==========================================
  9103. *------------------------------------------*/
  9104. BUILDIN_FUNC(getnpctimer)
  9105. {
  9106. struct npc_data *nd;
  9107. TBL_PC *sd;
  9108. int type = script_getnum(st,2);
  9109. int val = 0;
  9110. if( script_hasdata(st,3) )
  9111. nd = npc_name2id(script_getstr(st,3));
  9112. else
  9113. nd = (struct npc_data *)map_id2bl(st->oid);
  9114. if( !nd || nd->bl.type != BL_NPC )
  9115. {
  9116. script_pushint(st,0);
  9117. ShowError("getnpctimer: Invalid NPC.\n");
  9118. return SCRIPT_CMD_FAILURE;
  9119. }
  9120. switch( type )
  9121. {
  9122. case 0: val = npc_gettimerevent_tick(nd); break;
  9123. case 1:
  9124. if( nd->u.scr.rid )
  9125. {
  9126. sd = map_id2sd(nd->u.scr.rid);
  9127. if( !sd )
  9128. {
  9129. ShowError("buildin_getnpctimer: Attached player not found!\n");
  9130. break;
  9131. }
  9132. val = (sd->npc_timer_id != INVALID_TIMER);
  9133. }
  9134. else
  9135. val = (nd->u.scr.timerid != INVALID_TIMER);
  9136. break;
  9137. case 2: val = nd->u.scr.timeramount; break;
  9138. }
  9139. script_pushint(st,val);
  9140. return SCRIPT_CMD_SUCCESS;
  9141. }
  9142. /*==========================================
  9143. *------------------------------------------*/
  9144. BUILDIN_FUNC(setnpctimer)
  9145. {
  9146. int tick;
  9147. struct npc_data *nd;
  9148. tick = script_getnum(st,2);
  9149. if( script_hasdata(st,3) )
  9150. nd = npc_name2id(script_getstr(st,3));
  9151. else
  9152. nd = (struct npc_data *)map_id2bl(st->oid);
  9153. if( !nd || nd->bl.type != BL_NPC )
  9154. {
  9155. script_pushint(st,1);
  9156. ShowError("setnpctimer: Invalid NPC.\n");
  9157. return SCRIPT_CMD_FAILURE;
  9158. }
  9159. npc_settimerevent_tick(nd,tick);
  9160. script_pushint(st,0);
  9161. return SCRIPT_CMD_SUCCESS;
  9162. }
  9163. /*==========================================
  9164. * attaches the player rid to the timer [Celest]
  9165. *------------------------------------------*/
  9166. BUILDIN_FUNC(attachnpctimer)
  9167. {
  9168. TBL_PC *sd;
  9169. struct npc_data *nd = (struct npc_data *)map_id2bl(st->oid);
  9170. if( !nd || nd->bl.type != BL_NPC )
  9171. {
  9172. script_pushint(st,1);
  9173. ShowError("setnpctimer: Invalid NPC.\n");
  9174. return SCRIPT_CMD_FAILURE;
  9175. }
  9176. if( !script_nick2sd(2,sd) ){
  9177. script_pushint(st,1);
  9178. ShowWarning("attachnpctimer: Invalid player.\n");
  9179. return SCRIPT_CMD_FAILURE;
  9180. }
  9181. nd->u.scr.rid = sd->bl.id;
  9182. script_pushint(st,0);
  9183. return SCRIPT_CMD_SUCCESS;
  9184. }
  9185. /*==========================================
  9186. * detaches a player rid from the timer [Celest]
  9187. *------------------------------------------*/
  9188. BUILDIN_FUNC(detachnpctimer)
  9189. {
  9190. struct npc_data *nd;
  9191. if( script_hasdata(st,2) )
  9192. nd = npc_name2id(script_getstr(st,2));
  9193. else
  9194. nd = (struct npc_data *)map_id2bl(st->oid);
  9195. if( !nd || nd->bl.type != BL_NPC )
  9196. {
  9197. script_pushint(st,1);
  9198. ShowError("detachnpctimer: Invalid NPC.\n");
  9199. return SCRIPT_CMD_FAILURE;
  9200. }
  9201. nd->u.scr.rid = 0;
  9202. script_pushint(st,0);
  9203. return SCRIPT_CMD_SUCCESS;
  9204. }
  9205. /*==========================================
  9206. * To avoid "player not attached" script errors, this function is provided,
  9207. * it checks if there is a player attached to the current script. [Skotlex]
  9208. * If no, returns 0, if yes, returns the account_id of the attached player.
  9209. *------------------------------------------*/
  9210. BUILDIN_FUNC(playerattached)
  9211. {
  9212. if(st->rid == 0 || map_id2sd(st->rid) == NULL)
  9213. script_pushint(st,0);
  9214. else
  9215. script_pushint(st,st->rid);
  9216. return SCRIPT_CMD_SUCCESS;
  9217. }
  9218. /*==========================================
  9219. *------------------------------------------*/
  9220. BUILDIN_FUNC(announce)
  9221. {
  9222. const char *mes = script_getstr(st,2);
  9223. int flag = script_getnum(st,3);
  9224. const char *fontColor = script_hasdata(st,4) ? script_getstr(st,4) : NULL;
  9225. int fontType = script_hasdata(st,5) ? script_getnum(st,5) : FW_NORMAL; // default fontType
  9226. int fontSize = script_hasdata(st,6) ? script_getnum(st,6) : 12; // default fontSize
  9227. int fontAlign = script_hasdata(st,7) ? script_getnum(st,7) : 0; // default fontAlign
  9228. int fontY = script_hasdata(st,8) ? script_getnum(st,8) : 0; // default fontY
  9229. if (flag&(BC_TARGET_MASK|BC_SOURCE_MASK)) // Broadcast source or broadcast region defined
  9230. {
  9231. send_target target;
  9232. 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
  9233. if (bl == NULL)
  9234. return SCRIPT_CMD_SUCCESS;
  9235. switch (flag&BC_TARGET_MASK) {
  9236. case BC_MAP: target = ALL_SAMEMAP; break;
  9237. case BC_AREA: target = AREA; break;
  9238. case BC_SELF: target = SELF; break;
  9239. default: target = ALL_CLIENT; break; // BC_ALL
  9240. }
  9241. if (fontColor)
  9242. clif_broadcast2(bl, mes, (int)strlen(mes)+1, strtol(fontColor, (char **)NULL, 0), fontType, fontSize, fontAlign, fontY, target);
  9243. else
  9244. clif_broadcast(bl, mes, (int)strlen(mes)+1, flag&BC_COLOR_MASK, target);
  9245. }
  9246. else
  9247. {
  9248. if (fontColor)
  9249. intif_broadcast2(mes, (int)strlen(mes)+1, strtol(fontColor, (char **)NULL, 0), fontType, fontSize, fontAlign, fontY);
  9250. else
  9251. intif_broadcast(mes, (int)strlen(mes)+1, flag&BC_COLOR_MASK);
  9252. }
  9253. return SCRIPT_CMD_SUCCESS;
  9254. }
  9255. /*==========================================
  9256. *------------------------------------------*/
  9257. static int buildin_announce_sub(struct block_list *bl, va_list ap)
  9258. {
  9259. char *mes = va_arg(ap, char *);
  9260. int len = va_arg(ap, int);
  9261. int type = va_arg(ap, int);
  9262. char *fontColor = va_arg(ap, char *);
  9263. short fontType = (short)va_arg(ap, int);
  9264. short fontSize = (short)va_arg(ap, int);
  9265. short fontAlign = (short)va_arg(ap, int);
  9266. short fontY = (short)va_arg(ap, int);
  9267. if (fontColor)
  9268. clif_broadcast2(bl, mes, len, strtol(fontColor, (char **)NULL, 0), fontType, fontSize, fontAlign, fontY, SELF);
  9269. else
  9270. clif_broadcast(bl, mes, len, type, SELF);
  9271. return SCRIPT_CMD_SUCCESS;
  9272. }
  9273. BUILDIN_FUNC(mapannounce)
  9274. {
  9275. const char *mapname = script_getstr(st,2);
  9276. const char *mes = script_getstr(st,3);
  9277. int flag = script_getnum(st,4);
  9278. const char *fontColor = script_hasdata(st,5) ? script_getstr(st,5) : NULL;
  9279. int fontType = script_hasdata(st,6) ? script_getnum(st,6) : FW_NORMAL; // default fontType
  9280. int fontSize = script_hasdata(st,7) ? script_getnum(st,7) : 12; // default fontSize
  9281. int fontAlign = script_hasdata(st,8) ? script_getnum(st,8) : 0; // default fontAlign
  9282. int fontY = script_hasdata(st,9) ? script_getnum(st,9) : 0; // default fontY
  9283. int16 m;
  9284. if ((m = map_mapname2mapid(mapname)) < 0)
  9285. return SCRIPT_CMD_SUCCESS;
  9286. map_foreachinmap(buildin_announce_sub, m, BL_PC,
  9287. mes, strlen(mes)+1, flag&BC_COLOR_MASK, fontColor, fontType, fontSize, fontAlign, fontY);
  9288. return SCRIPT_CMD_SUCCESS;
  9289. }
  9290. /*==========================================
  9291. *------------------------------------------*/
  9292. BUILDIN_FUNC(areaannounce)
  9293. {
  9294. const char *mapname = script_getstr(st,2);
  9295. int x0 = script_getnum(st,3);
  9296. int y0 = script_getnum(st,4);
  9297. int x1 = script_getnum(st,5);
  9298. int y1 = script_getnum(st,6);
  9299. const char *mes = script_getstr(st,7);
  9300. int flag = script_getnum(st,8);
  9301. const char *fontColor = script_hasdata(st,9) ? script_getstr(st,9) : NULL;
  9302. int fontType = script_hasdata(st,10) ? script_getnum(st,10) : FW_NORMAL; // default fontType
  9303. int fontSize = script_hasdata(st,11) ? script_getnum(st,11) : 12; // default fontSize
  9304. int fontAlign = script_hasdata(st,12) ? script_getnum(st,12) : 0; // default fontAlign
  9305. int fontY = script_hasdata(st,13) ? script_getnum(st,13) : 0; // default fontY
  9306. int16 m;
  9307. if ((m = map_mapname2mapid(mapname)) < 0)
  9308. return SCRIPT_CMD_SUCCESS;
  9309. map_foreachinarea(buildin_announce_sub, m, x0, y0, x1, y1, BL_PC,
  9310. mes, strlen(mes)+1, flag&BC_COLOR_MASK, fontColor, fontType, fontSize, fontAlign, fontY);
  9311. return SCRIPT_CMD_SUCCESS;
  9312. }
  9313. /*==========================================
  9314. *------------------------------------------*/
  9315. BUILDIN_FUNC(getusers)
  9316. {
  9317. int flag, val = 0;
  9318. struct map_session_data* sd;
  9319. struct block_list* bl = NULL;
  9320. flag = script_getnum(st,2);
  9321. switch(flag&0x07)
  9322. {
  9323. case 0:
  9324. if(flag&0x8)
  9325. {// npc
  9326. bl = map_id2bl(st->oid);
  9327. }
  9328. else if((sd = script_rid2sd(st))!=NULL)
  9329. {// pc
  9330. bl = &sd->bl;
  9331. }
  9332. if(bl)
  9333. {
  9334. val = map[bl->m].users;
  9335. }
  9336. break;
  9337. case 1:
  9338. val = map_getusers();
  9339. break;
  9340. default:
  9341. ShowWarning("buildin_getusers: Unknown type %d.\n", flag);
  9342. script_pushint(st,0);
  9343. return SCRIPT_CMD_FAILURE;
  9344. }
  9345. script_pushint(st,val);
  9346. return SCRIPT_CMD_SUCCESS;
  9347. }
  9348. /*==========================================
  9349. * getmapguildusers("mapname",guild ID) Returns the number guild members present on a map [Reddozen]
  9350. *------------------------------------------*/
  9351. BUILDIN_FUNC(getmapguildusers)
  9352. {
  9353. const char *str;
  9354. int16 m;
  9355. int gid;
  9356. int c=0;
  9357. struct guild *g = NULL;
  9358. str=script_getstr(st,2);
  9359. gid=script_getnum(st,3);
  9360. if ((m = map_mapname2mapid(str)) < 0) { // map id on this server (m == -1 if not in actual map-server)
  9361. script_pushint(st,-1);
  9362. return SCRIPT_CMD_SUCCESS;
  9363. }
  9364. g = guild_search(gid);
  9365. if (g){
  9366. unsigned short i;
  9367. for(i = 0; i < g->max_member; i++)
  9368. {
  9369. if (g->member[i].sd && g->member[i].sd->bl.m == m)
  9370. c++;
  9371. }
  9372. }
  9373. script_pushint(st,c);
  9374. return SCRIPT_CMD_SUCCESS;
  9375. }
  9376. /*==========================================
  9377. *------------------------------------------*/
  9378. BUILDIN_FUNC(getmapusers)
  9379. {
  9380. const char *str;
  9381. int16 m;
  9382. str=script_getstr(st,2);
  9383. if( (m=map_mapname2mapid(str))< 0){
  9384. script_pushint(st,-1);
  9385. return SCRIPT_CMD_SUCCESS;
  9386. }
  9387. script_pushint(st,map[m].users);
  9388. return SCRIPT_CMD_SUCCESS;
  9389. }
  9390. /*==========================================
  9391. *------------------------------------------*/
  9392. static int buildin_getareausers_sub(struct block_list *bl,va_list ap)
  9393. {
  9394. int *users=va_arg(ap,int *);
  9395. (*users)++;
  9396. return SCRIPT_CMD_SUCCESS;
  9397. }
  9398. BUILDIN_FUNC(getareausers)
  9399. {
  9400. const char *str;
  9401. int16 m,x0,y0,x1,y1,users=0; //doubt we can have more then 32k users on
  9402. str=script_getstr(st,2);
  9403. x0=script_getnum(st,3);
  9404. y0=script_getnum(st,4);
  9405. x1=script_getnum(st,5);
  9406. y1=script_getnum(st,6);
  9407. if( (m=map_mapname2mapid(str))< 0){
  9408. script_pushint(st,-1);
  9409. return SCRIPT_CMD_SUCCESS;
  9410. }
  9411. map_foreachinarea(buildin_getareausers_sub,
  9412. m,x0,y0,x1,y1,BL_PC,&users);
  9413. script_pushint(st,users);
  9414. return SCRIPT_CMD_SUCCESS;
  9415. }
  9416. /*==========================================
  9417. *------------------------------------------*/
  9418. static int buildin_getareadropitem_sub(struct block_list *bl,va_list ap)
  9419. {
  9420. unsigned short nameid = (unsigned short)va_arg(ap, int);
  9421. unsigned short *amount = (unsigned short *)va_arg(ap, int *);
  9422. struct flooritem_data *drop=(struct flooritem_data *)bl;
  9423. if(drop->item.nameid==nameid)
  9424. (*amount)+=drop->item.amount;
  9425. return SCRIPT_CMD_SUCCESS;
  9426. }
  9427. BUILDIN_FUNC(getareadropitem)
  9428. {
  9429. const char *str;
  9430. int16 m,x0,y0,x1,y1;
  9431. unsigned short nameid, amount = 0;
  9432. struct script_data *data;
  9433. str=script_getstr(st,2);
  9434. x0=script_getnum(st,3);
  9435. y0=script_getnum(st,4);
  9436. x1=script_getnum(st,5);
  9437. y1=script_getnum(st,6);
  9438. data=script_getdata(st,7);
  9439. get_val(st,data);
  9440. if( data_isstring(data) ){
  9441. const char *name=conv_str(st,data);
  9442. struct item_data *item_data = itemdb_searchname(name);
  9443. nameid=UNKNOWN_ITEM_ID;
  9444. if( item_data )
  9445. nameid=item_data->nameid;
  9446. }else
  9447. nameid=conv_num(st,data);
  9448. if( (m=map_mapname2mapid(str))< 0){
  9449. script_pushint(st,-1);
  9450. return SCRIPT_CMD_SUCCESS;
  9451. }
  9452. map_foreachinarea(buildin_getareadropitem_sub,
  9453. m,x0,y0,x1,y1,BL_ITEM,nameid,&amount);
  9454. script_pushint(st,amount);
  9455. return SCRIPT_CMD_SUCCESS;
  9456. }
  9457. /*==========================================
  9458. *------------------------------------------*/
  9459. BUILDIN_FUNC(enablenpc)
  9460. {
  9461. const char *str;
  9462. str=script_getstr(st,2);
  9463. npc_enable(str,1);
  9464. return SCRIPT_CMD_SUCCESS;
  9465. }
  9466. /*==========================================
  9467. *------------------------------------------*/
  9468. BUILDIN_FUNC(disablenpc)
  9469. {
  9470. const char *str;
  9471. str=script_getstr(st,2);
  9472. npc_enable(str,0);
  9473. return SCRIPT_CMD_SUCCESS;
  9474. }
  9475. /*==========================================
  9476. *------------------------------------------*/
  9477. BUILDIN_FUNC(hideoffnpc)
  9478. {
  9479. const char *str;
  9480. str=script_getstr(st,2);
  9481. npc_enable(str,2);
  9482. return SCRIPT_CMD_SUCCESS;
  9483. }
  9484. /*==========================================
  9485. *------------------------------------------*/
  9486. BUILDIN_FUNC(hideonnpc)
  9487. {
  9488. const char *str;
  9489. str=script_getstr(st,2);
  9490. npc_enable(str,4);
  9491. return SCRIPT_CMD_SUCCESS;
  9492. }
  9493. /* Starts a status effect on the target unit or on the attached player.
  9494. *
  9495. * sc_start <effect_id>,<duration>,<val1>{,<rate>,<flag>,{<unit_id>}};
  9496. * sc_start2 <effect_id>,<duration>,<val1>,<val2>{,<rate,<flag>,{<unit_id>}};
  9497. * sc_start4 <effect_id>,<duration>,<val1>,<val2>,<val3>,<val4>{,<rate,<flag>,{<unit_id>}};
  9498. * <flag>: enum e_status_change_start_flags
  9499. */
  9500. BUILDIN_FUNC(sc_start)
  9501. {
  9502. TBL_NPC * nd = map_id2nd(st->oid);
  9503. struct block_list* bl;
  9504. enum sc_type type;
  9505. int tick, val1, val2, val3, val4=0, rate, flag;
  9506. char start_type;
  9507. const char* command = script_getfuncname(st);
  9508. if(strstr(command, "4"))
  9509. start_type = 4;
  9510. else if(strstr(command, "2"))
  9511. start_type = 2;
  9512. else
  9513. start_type = 1;
  9514. type = (sc_type)script_getnum(st,2);
  9515. tick = script_getnum(st,3);
  9516. val1 = script_getnum(st,4);
  9517. //If from NPC we make default flag 1 to be unavoidable
  9518. if(nd && nd->bl.id == fake_nd->bl.id)
  9519. flag = script_hasdata(st,5+start_type)?script_getnum(st,5+start_type):SCSTART_NOTICKDEF;
  9520. else
  9521. flag = script_hasdata(st,5+start_type)?script_getnum(st,5+start_type):SCSTART_NOAVOID;
  9522. rate = script_hasdata(st,4+start_type)?min(script_getnum(st,4+start_type),10000):10000;
  9523. if(script_hasdata(st,(6+start_type)))
  9524. bl = map_id2bl(script_getnum(st,(6+start_type)));
  9525. else
  9526. bl = map_id2bl(st->rid);
  9527. if(tick == 0 && val1 > 0 && type > SC_NONE && type < SC_MAX && status_sc2skill(type) != 0)
  9528. {// When there isn't a duration specified, try to get it from the skill_db
  9529. tick = skill_get_time(status_sc2skill(type), val1);
  9530. }
  9531. if(potion_flag == 1 && potion_target) { //skill.c set the flags before running the script, this is a potion-pitched effect.
  9532. bl = map_id2bl(potion_target);
  9533. tick /= 2;// Thrown potions only last half.
  9534. val4 = 1;// Mark that this was a thrown sc_effect
  9535. }
  9536. if(!bl)
  9537. return SCRIPT_CMD_SUCCESS;
  9538. switch(start_type) {
  9539. case 1:
  9540. status_change_start(bl, bl, type, rate, val1, 0, 0, val4, tick, flag);
  9541. break;
  9542. case 2:
  9543. val2 = script_getnum(st,5);
  9544. status_change_start(bl, bl, type, rate, val1, val2, 0, val4, tick, flag);
  9545. break;
  9546. case 4:
  9547. val2 = script_getnum(st,5);
  9548. val3 = script_getnum(st,6);
  9549. val4 = script_getnum(st,7);
  9550. status_change_start(bl, bl, type, rate, val1, val2, val3, val4, tick, flag);
  9551. break;
  9552. }
  9553. return SCRIPT_CMD_SUCCESS;
  9554. }
  9555. /// Ends one or all status effects on the target unit or on the attached player.
  9556. ///
  9557. /// sc_end <effect_id>{,<unit_id>};
  9558. BUILDIN_FUNC(sc_end)
  9559. {
  9560. struct block_list* bl;
  9561. int type;
  9562. type = script_getnum(st, 2);
  9563. if (script_hasdata(st, 3))
  9564. bl = map_id2bl(script_getnum(st, 3));
  9565. else
  9566. bl = map_id2bl(st->rid);
  9567. if (potion_flag == 1 && potion_target) //##TODO how does this work [FlavioJS]
  9568. bl = map_id2bl(potion_target);
  9569. if (!bl)
  9570. return SCRIPT_CMD_SUCCESS;
  9571. if (type >= 0 && type < SC_MAX) {
  9572. struct status_change *sc = status_get_sc(bl);
  9573. struct status_change_entry *sce = sc ? sc->data[type] : NULL;
  9574. if (!sce)
  9575. return SCRIPT_CMD_SUCCESS;
  9576. switch (type) {
  9577. case SC_WEIGHT50:
  9578. case SC_WEIGHT90:
  9579. case SC_NOCHAT:
  9580. case SC_PUSH_CART:
  9581. case SC_ALL_RIDING:
  9582. case SC_STYLE_CHANGE:
  9583. case SC_MONSTER_TRANSFORM:
  9584. case SC_ACTIVE_MONSTER_TRANSFORM:
  9585. case SC_MTF_ASPD:
  9586. case SC_MTF_RANGEATK:
  9587. case SC_MTF_MATK:
  9588. case SC_MTF_MLEATKED:
  9589. case SC_MTF_CRIDAMAGE:
  9590. case SC_MTF_ASPD2:
  9591. case SC_MTF_RANGEATK2:
  9592. case SC_MTF_MATK2:
  9593. case SC_MTF_MHP:
  9594. case SC_MTF_MSP:
  9595. case SC_MTF_PUMPKIN:
  9596. case SC_MTF_HITFLEE:
  9597. case SC_ATTHASTE_CASH:
  9598. case SC_REUSE_LIMIT_A: case SC_REUSE_LIMIT_B: case SC_REUSE_LIMIT_C:
  9599. case SC_REUSE_LIMIT_D: case SC_REUSE_LIMIT_E: case SC_REUSE_LIMIT_F:
  9600. case SC_REUSE_LIMIT_G: case SC_REUSE_LIMIT_H: case SC_REUSE_LIMIT_MTF:
  9601. case SC_REUSE_LIMIT_ASPD_POTION: case SC_REUSE_MILLENNIUMSHIELD: case SC_REUSE_CRUSHSTRIKE:
  9602. case SC_REUSE_STORMBLAST: case SC_ALL_RIDING_REUSE_LIMIT: case SC_REUSE_REFRESH:
  9603. case SC_REUSE_LIMIT_ECL: case SC_REUSE_LIMIT_RECALL:
  9604. return SCRIPT_CMD_SUCCESS;
  9605. default:
  9606. break;
  9607. }
  9608. //This should help status_change_end force disabling the SC in case it has no limit.
  9609. sce->val1 = sce->val2 = sce->val3 = sce->val4 = 0;
  9610. status_change_end(bl, (sc_type)type, INVALID_TIMER);
  9611. } else
  9612. status_change_clear(bl, 3); // remove all effects
  9613. return SCRIPT_CMD_SUCCESS;
  9614. }
  9615. /*==========================================
  9616. * @FIXME atm will return reduced tick, 0 immune, 1 no tick
  9617. *------------------------------------------*/
  9618. BUILDIN_FUNC(getscrate)
  9619. {
  9620. struct block_list *bl;
  9621. int type,rate;
  9622. type=script_getnum(st,2);
  9623. rate=script_getnum(st,3);
  9624. if( script_hasdata(st,4) ) //get for the bl assigned
  9625. bl = map_id2bl(script_getnum(st,4));
  9626. else
  9627. bl = map_id2bl(st->rid);
  9628. if (bl)
  9629. rate = status_get_sc_def(NULL,bl, (sc_type)type, 10000, 10000, SCSTART_NONE);
  9630. script_pushint(st,rate);
  9631. return SCRIPT_CMD_SUCCESS;
  9632. }
  9633. /**
  9634. * getstatus(<effect type>{,<type>{,<char_id>}});
  9635. **/
  9636. BUILDIN_FUNC(getstatus)
  9637. {
  9638. int id, type;
  9639. struct map_session_data* sd;
  9640. if (!script_charid2sd(4,sd))
  9641. return SCRIPT_CMD_FAILURE;
  9642. id = script_getnum(st, 2);
  9643. type = script_hasdata(st, 3) ? script_getnum(st, 3) : 0;
  9644. if( id <= SC_NONE || id >= SC_MAX )
  9645. {// invalid status type given
  9646. ShowWarning("script.c:getstatus: Invalid status type given (%d).\n", id);
  9647. return SCRIPT_CMD_SUCCESS;
  9648. }
  9649. if( sd->sc.count == 0 || !sd->sc.data[id] )
  9650. {// no status is active
  9651. script_pushint(st, 0);
  9652. return SCRIPT_CMD_SUCCESS;
  9653. }
  9654. switch( type )
  9655. {
  9656. case 1: script_pushint(st, sd->sc.data[id]->val1); break;
  9657. case 2: script_pushint(st, sd->sc.data[id]->val2); break;
  9658. case 3: script_pushint(st, sd->sc.data[id]->val3); break;
  9659. case 4: script_pushint(st, sd->sc.data[id]->val4); break;
  9660. case 5:
  9661. {
  9662. struct TimerData* timer = (struct TimerData*)get_timer(sd->sc.data[id]->timer);
  9663. if( timer )
  9664. {// return the amount of time remaining
  9665. script_pushint(st, timer->tick - gettick());
  9666. } else {
  9667. script_pushint(st, -1);
  9668. }
  9669. }
  9670. break;
  9671. default: script_pushint(st, 1); break;
  9672. }
  9673. return SCRIPT_CMD_SUCCESS;
  9674. }
  9675. /*==========================================
  9676. *
  9677. *------------------------------------------*/
  9678. BUILDIN_FUNC(debugmes)
  9679. {
  9680. const char *str;
  9681. str=script_getstr(st,2);
  9682. ShowDebug("script debug : %d %d : %s\n",st->rid,st->oid,str);
  9683. return SCRIPT_CMD_SUCCESS;
  9684. }
  9685. /*==========================================
  9686. *------------------------------------------*/
  9687. BUILDIN_FUNC(catchpet)
  9688. {
  9689. int pet_id;
  9690. TBL_PC *sd;
  9691. pet_id= script_getnum(st,2);
  9692. sd=script_rid2sd(st);
  9693. if( sd == NULL )
  9694. return SCRIPT_CMD_SUCCESS;
  9695. pet_catch_process1(sd,pet_id);
  9696. return SCRIPT_CMD_SUCCESS;
  9697. }
  9698. /*==========================================
  9699. * [orn]
  9700. *------------------------------------------*/
  9701. BUILDIN_FUNC(homunculus_evolution)
  9702. {
  9703. TBL_PC *sd;
  9704. sd=script_rid2sd(st);
  9705. if( sd == NULL )
  9706. return SCRIPT_CMD_SUCCESS;
  9707. if(hom_is_active(sd->hd))
  9708. {
  9709. if (sd->hd->homunculus.intimacy >= battle_config.homunculus_evo_intimacy_need)
  9710. hom_evolution(sd->hd);
  9711. else
  9712. clif_emotion(&sd->hd->bl, E_SWT);
  9713. }
  9714. return SCRIPT_CMD_SUCCESS;
  9715. }
  9716. /*==========================================
  9717. * Checks for vaporized morph state
  9718. * and deletes ITEMID_STRANGE_EMBRYO.
  9719. *------------------------------------------*/
  9720. BUILDIN_FUNC(homunculus_mutate)
  9721. {
  9722. int homun_id;
  9723. TBL_PC *sd;
  9724. sd = script_rid2sd(st);
  9725. if( sd == NULL || sd->hd == NULL )
  9726. return SCRIPT_CMD_SUCCESS;
  9727. if(script_hasdata(st,2))
  9728. homun_id = script_getnum(st,2);
  9729. else
  9730. homun_id = 6048 + (rnd() % 4);
  9731. if( sd->hd->homunculus.vaporize == HOM_ST_MORPH ) {
  9732. int m_class = hom_class2mapid(sd->hd->homunculus.class_);
  9733. int m_id = hom_class2mapid(homun_id);
  9734. short i = pc_search_inventory(sd, ITEMID_STRANGE_EMBRYO);
  9735. if ( m_class != -1 && m_id != -1 && m_class&HOM_EVO && m_id&HOM_S && sd->hd->homunculus.level >= 99 && i >= 0 ) {
  9736. sd->hd->homunculus.vaporize = HOM_ST_REST; // Remove morph state.
  9737. hom_call(sd); // Respawn homunculus.
  9738. hom_mutate(sd->hd, homun_id);
  9739. pc_delitem(sd, i, 1, 0, 0, LOG_TYPE_SCRIPT);
  9740. script_pushint(st, 1);
  9741. return SCRIPT_CMD_SUCCESS;
  9742. } else
  9743. clif_emotion(&sd->bl, E_SWT);
  9744. } else
  9745. clif_emotion(&sd->bl, E_SWT);
  9746. script_pushint(st, 0);
  9747. return SCRIPT_CMD_SUCCESS;
  9748. }
  9749. /*==========================================
  9750. * Puts homunculus into morph state
  9751. * and gives ITEMID_STRANGE_EMBRYO.
  9752. *------------------------------------------*/
  9753. BUILDIN_FUNC(morphembryo)
  9754. {
  9755. struct item item_tmp;
  9756. TBL_PC *sd;
  9757. sd = script_rid2sd(st);
  9758. if( sd == NULL || sd->hd == NULL )
  9759. return SCRIPT_CMD_SUCCESS;
  9760. if( hom_is_active(sd->hd) ) {
  9761. int m_class = hom_class2mapid(sd->hd->homunculus.class_);
  9762. if ( m_class != -1 && m_class&HOM_EVO && sd->hd->homunculus.level >= 99 ) {
  9763. char i;
  9764. memset(&item_tmp, 0, sizeof(item_tmp));
  9765. item_tmp.nameid = ITEMID_STRANGE_EMBRYO;
  9766. item_tmp.identify = 1;
  9767. if( (i = pc_additem(sd, &item_tmp, 1, LOG_TYPE_SCRIPT)) ) {
  9768. clif_additem(sd, 0, 0, i);
  9769. clif_emotion(&sd->bl, E_SWT); // Fail to avoid item drop exploit.
  9770. } else {
  9771. hom_vaporize(sd, HOM_ST_MORPH);
  9772. script_pushint(st, 1);
  9773. return SCRIPT_CMD_SUCCESS;
  9774. }
  9775. } else
  9776. clif_emotion(&sd->hd->bl, E_SWT);
  9777. } else
  9778. clif_emotion(&sd->bl, E_SWT);
  9779. script_pushint(st, 0);
  9780. return SCRIPT_CMD_SUCCESS;
  9781. }
  9782. // [Zephyrus]
  9783. BUILDIN_FUNC(homunculus_shuffle)
  9784. {
  9785. TBL_PC *sd;
  9786. sd=script_rid2sd(st);
  9787. if( sd == NULL )
  9788. return SCRIPT_CMD_SUCCESS;
  9789. if(hom_is_active(sd->hd))
  9790. hom_shuffle(sd->hd);
  9791. return SCRIPT_CMD_SUCCESS;
  9792. }
  9793. /*==========================================
  9794. * Check for homunculus state.
  9795. * Return: -1 = No homunculus
  9796. * 0 = Homunculus is active
  9797. * 1 = Homunculus is vaporized (rest)
  9798. * 2 = Homunculus is in morph state
  9799. *------------------------------------------*/
  9800. BUILDIN_FUNC(checkhomcall)
  9801. {
  9802. TBL_PC *sd = script_rid2sd(st);
  9803. TBL_HOM *hd;
  9804. if( sd == NULL )
  9805. return SCRIPT_CMD_SUCCESS;
  9806. hd = sd->hd;
  9807. if( !hd )
  9808. script_pushint(st, -1);
  9809. else
  9810. script_pushint(st, hd->homunculus.vaporize);
  9811. return SCRIPT_CMD_SUCCESS;
  9812. }
  9813. //These two functions bring the eA MAPID_* class functionality to scripts.
  9814. BUILDIN_FUNC(eaclass)
  9815. {
  9816. int class_;
  9817. if( script_hasdata(st,2) )
  9818. class_ = script_getnum(st,2);
  9819. else {
  9820. TBL_PC *sd;
  9821. if (!script_charid2sd(3,sd)) {
  9822. script_pushint(st,-1);
  9823. return SCRIPT_CMD_SUCCESS;
  9824. }
  9825. class_ = sd->status.class_;
  9826. }
  9827. script_pushint(st,pc_jobid2mapid(class_));
  9828. return SCRIPT_CMD_SUCCESS;
  9829. }
  9830. BUILDIN_FUNC(roclass)
  9831. {
  9832. int class_ =script_getnum(st,2);
  9833. int sex;
  9834. if( script_hasdata(st,3) )
  9835. sex = script_getnum(st,3);
  9836. else {
  9837. TBL_PC *sd;
  9838. if (st->rid && (sd=script_rid2sd(st)))
  9839. sex = sd->status.sex;
  9840. else
  9841. sex = 1; //Just use male when not found.
  9842. }
  9843. script_pushint(st,pc_mapid2jobid(class_, sex));
  9844. return SCRIPT_CMD_SUCCESS;
  9845. }
  9846. /*==========================================
  9847. * Tells client to open a hatching window, used for pet incubator
  9848. *------------------------------------------*/
  9849. BUILDIN_FUNC(birthpet)
  9850. {
  9851. TBL_PC *sd;
  9852. sd=script_rid2sd(st);
  9853. if( sd == NULL )
  9854. return SCRIPT_CMD_SUCCESS;
  9855. if( sd->status.pet_id )
  9856. {// do not send egg list, when you already have a pet
  9857. return SCRIPT_CMD_SUCCESS;
  9858. }
  9859. clif_sendegg(sd);
  9860. return SCRIPT_CMD_SUCCESS;
  9861. }
  9862. /**
  9863. * resetlvl <action type>{,<char_id>};
  9864. * @param action_type:
  9865. * 1 : make like after rebirth
  9866. * 2 : blvl,jlvl=1, skillpoint=0
  9867. * 3 : don't reset skill, blvl=1
  9868. * 4 : jlvl=0
  9869. * @author AppleGirl
  9870. **/
  9871. BUILDIN_FUNC(resetlvl)
  9872. {
  9873. TBL_PC *sd;
  9874. int type=script_getnum(st,2);
  9875. if (!script_charid2sd(3,sd))
  9876. return SCRIPT_CMD_FAILURE;
  9877. pc_resetlvl(sd,type);
  9878. return SCRIPT_CMD_SUCCESS;
  9879. }
  9880. /**
  9881. * Reset a player status point
  9882. * resetstatus({<char_id>});
  9883. **/
  9884. BUILDIN_FUNC(resetstatus)
  9885. {
  9886. TBL_PC *sd;
  9887. if (!script_charid2sd(2,sd))
  9888. return SCRIPT_CMD_FAILURE;
  9889. pc_resetstate(sd);
  9890. return SCRIPT_CMD_SUCCESS;
  9891. }
  9892. /**
  9893. * Reset player's skill
  9894. * resetskill({<char_id>});
  9895. **/
  9896. BUILDIN_FUNC(resetskill)
  9897. {
  9898. TBL_PC *sd;
  9899. if (!script_charid2sd(2,sd))
  9900. return SCRIPT_CMD_FAILURE;
  9901. pc_resetskill(sd,1);
  9902. return SCRIPT_CMD_SUCCESS;
  9903. }
  9904. /**
  9905. * Counts total amount of skill points.
  9906. * skillpointcount({<char_id>})
  9907. **/
  9908. BUILDIN_FUNC(skillpointcount)
  9909. {
  9910. TBL_PC *sd;
  9911. if (!script_charid2sd(2,sd))
  9912. return SCRIPT_CMD_FAILURE;
  9913. script_pushint(st,sd->status.skill_point + pc_resetskill(sd,2));
  9914. return SCRIPT_CMD_SUCCESS;
  9915. }
  9916. /*==========================================
  9917. *
  9918. *------------------------------------------*/
  9919. BUILDIN_FUNC(changebase)
  9920. {
  9921. TBL_PC *sd=NULL;
  9922. int vclass;
  9923. if( script_hasdata(st,3) )
  9924. sd=map_id2sd(script_getnum(st,3));
  9925. else
  9926. sd=script_rid2sd(st);
  9927. if(sd == NULL)
  9928. return SCRIPT_CMD_SUCCESS;
  9929. vclass = script_getnum(st,2);
  9930. if(vclass == JOB_WEDDING)
  9931. {
  9932. if (!battle_config.wedding_modifydisplay || //Do not show the wedding sprites
  9933. sd->class_&JOBL_BABY //Baby classes screw up when showing wedding sprites. [Skotlex] They don't seem to anymore.
  9934. )
  9935. return SCRIPT_CMD_SUCCESS;
  9936. }
  9937. if(!sd->disguise && vclass != sd->vd.class_) {
  9938. status_set_viewdata(&sd->bl, vclass);
  9939. //Updated client view. Base, Weapon and Cloth Colors.
  9940. clif_changelook(&sd->bl,LOOK_BASE,sd->vd.class_);
  9941. clif_changelook(&sd->bl,LOOK_WEAPON,sd->status.weapon);
  9942. if (sd->vd.cloth_color)
  9943. clif_changelook(&sd->bl,LOOK_CLOTHES_COLOR,sd->vd.cloth_color);
  9944. if (sd->vd.body_style)
  9945. clif_changelook(&sd->bl,LOOK_BODY2,sd->vd.body_style);
  9946. clif_skillinfoblock(sd);
  9947. }
  9948. return SCRIPT_CMD_SUCCESS;
  9949. }
  9950. /**
  9951. * Change account sex and unequip all item and request for a changesex to char-serv
  9952. * changesex({<char_id>});
  9953. */
  9954. BUILDIN_FUNC(changesex)
  9955. {
  9956. int i;
  9957. TBL_PC *sd = NULL;
  9958. if (!script_charid2sd(2,sd))
  9959. return SCRIPT_CMD_FAILURE;
  9960. pc_resetskill(sd,4);
  9961. // to avoid any problem with equipment and invalid sex, equipment is unequiped.
  9962. for(i = 0; i < EQI_MAX; i++) {
  9963. if (sd->equip_index[i] >= 0)
  9964. pc_unequipitem(sd, sd->equip_index[i], 3);
  9965. }
  9966. chrif_changesex(sd, true);
  9967. return SCRIPT_CMD_SUCCESS;
  9968. }
  9969. /**
  9970. * Change character's sex and unequip all item and request for a changesex to char-serv
  9971. * changecharsex({<char_id>});
  9972. */
  9973. BUILDIN_FUNC(changecharsex)
  9974. {
  9975. #if PACKETVER >= 20141016
  9976. int i;
  9977. TBL_PC *sd = NULL;
  9978. if (!script_charid2sd(2,sd))
  9979. return SCRIPT_CMD_FAILURE;
  9980. pc_resetskill(sd,4);
  9981. // to avoid any problem with equipment and invalid sex, equipment is unequiped.
  9982. for (i = 0; i < EQI_MAX; i++) {
  9983. if (sd->equip_index[i] >= 0)
  9984. pc_unequipitem(sd, sd->equip_index[i], 3);
  9985. }
  9986. chrif_changesex(sd, false);
  9987. return SCRIPT_CMD_SUCCESS;
  9988. #else
  9989. return SCRIPT_CMD_FAILURE;
  9990. #endif
  9991. }
  9992. /*==========================================
  9993. * Works like 'announce' but outputs in the common chat window
  9994. *------------------------------------------*/
  9995. BUILDIN_FUNC(globalmes)
  9996. {
  9997. struct block_list *bl = map_id2bl(st->oid);
  9998. struct npc_data *nd = (struct npc_data *)bl;
  9999. const char *name=NULL,*mes;
  10000. mes=script_getstr(st,2);
  10001. if(mes==NULL)
  10002. return SCRIPT_CMD_SUCCESS;
  10003. if(script_hasdata(st,3)){ // npc name to display
  10004. name=script_getstr(st,3);
  10005. } else {
  10006. name=nd->name; //use current npc name
  10007. }
  10008. npc_globalmessage(name,mes); // broadcast to all players connected
  10009. return SCRIPT_CMD_SUCCESS;
  10010. }
  10011. /////////////////////////////////////////////////////////////////////
  10012. // NPC waiting room (chat room)
  10013. //
  10014. /// Creates a waiting room (chat room) for this npc.
  10015. ///
  10016. /// waitingroom "<title>",<limit>{,"<event>"{,<trigger>{,<zeny>{,<minlvl>{,<maxlvl>}}}}};
  10017. BUILDIN_FUNC(waitingroom)
  10018. {
  10019. struct npc_data* nd;
  10020. const char* title = script_getstr(st, 2);
  10021. int limit = script_getnum(st, 3);
  10022. const char* ev = script_hasdata(st,4) ? script_getstr(st,4) : "";
  10023. int trigger = script_hasdata(st,5) ? script_getnum(st,5) : limit;
  10024. int zeny = script_hasdata(st,6) ? script_getnum(st,6) : 0;
  10025. int minLvl = script_hasdata(st,7) ? script_getnum(st,7) : 1;
  10026. int maxLvl = script_hasdata(st,8) ? script_getnum(st,8) : MAX_LEVEL;
  10027. nd = (struct npc_data *)map_id2bl(st->oid);
  10028. if( nd != NULL )
  10029. chat_createnpcchat(nd, title, limit, 1, trigger, ev, zeny, minLvl, maxLvl);
  10030. return SCRIPT_CMD_SUCCESS;
  10031. }
  10032. /// Removes the waiting room of the current or target npc.
  10033. ///
  10034. /// delwaitingroom "<npc_name>";
  10035. /// delwaitingroom;
  10036. BUILDIN_FUNC(delwaitingroom)
  10037. {
  10038. struct npc_data* nd;
  10039. if( script_hasdata(st,2) )
  10040. nd = npc_name2id(script_getstr(st, 2));
  10041. else
  10042. nd = (struct npc_data *)map_id2bl(st->oid);
  10043. if( nd != NULL )
  10044. chat_deletenpcchat(nd);
  10045. return SCRIPT_CMD_SUCCESS;
  10046. }
  10047. /// Kick the specified player from the waiting room of the target npc.
  10048. ///
  10049. /// waitingroomkick "<npc_name>", <kickusername>;
  10050. BUILDIN_FUNC(waitingroomkick)
  10051. {
  10052. struct npc_data* nd;
  10053. struct chat_data* cd;
  10054. const char* kickusername;
  10055. nd = npc_name2id(script_getstr(st,2));
  10056. kickusername = script_getstr(st,3);
  10057. if( nd != NULL && (cd=(struct chat_data *)map_id2bl(nd->chat_id)) != NULL )
  10058. chat_npckickchat(cd, kickusername);
  10059. return SCRIPT_CMD_SUCCESS;
  10060. }
  10061. /// Get Users in waiting room and stores gids in .@waitingroom_users[]
  10062. /// Num users stored in .@waitingroom_usercount
  10063. ///
  10064. /// getwaitingroomusers "<npc_name>";
  10065. BUILDIN_FUNC(getwaitingroomusers)
  10066. {
  10067. struct npc_data* nd;
  10068. struct chat_data* cd;
  10069. int i, j=0;
  10070. if( script_hasdata(st,2) )
  10071. nd = npc_name2id(script_getstr(st, 2));
  10072. else
  10073. nd = (struct npc_data *)map_id2bl(st->oid);
  10074. if( nd != NULL && (cd=(struct chat_data *)map_id2bl(nd->chat_id)) != NULL ) {
  10075. for(i = 0; i < cd->users; ++i) {
  10076. setd_sub(st, NULL, ".@waitingroom_users", j, (void *)__64BPRTSIZE(cd->usersd[i]->status.account_id), NULL);
  10077. j++;
  10078. }
  10079. setd_sub(st, NULL, ".@waitingroom_usercount", 0, (void *)__64BPRTSIZE(j), NULL);
  10080. }
  10081. return SCRIPT_CMD_SUCCESS;
  10082. }
  10083. /// Kicks all the players from the waiting room of the current or target npc.
  10084. ///
  10085. /// kickwaitingroomall "<npc_name>";
  10086. /// kickwaitingroomall;
  10087. BUILDIN_FUNC(waitingroomkickall)
  10088. {
  10089. struct npc_data* nd;
  10090. struct chat_data* cd;
  10091. if( script_hasdata(st,2) )
  10092. nd = npc_name2id(script_getstr(st,2));
  10093. else
  10094. nd = (struct npc_data *)map_id2bl(st->oid);
  10095. if( nd != NULL && (cd=(struct chat_data *)map_id2bl(nd->chat_id)) != NULL )
  10096. chat_npckickall(cd);
  10097. return SCRIPT_CMD_SUCCESS;
  10098. }
  10099. /// Enables the waiting room event of the current or target npc.
  10100. ///
  10101. /// enablewaitingroomevent "<npc_name>";
  10102. /// enablewaitingroomevent;
  10103. BUILDIN_FUNC(enablewaitingroomevent)
  10104. {
  10105. struct npc_data* nd;
  10106. struct chat_data* cd;
  10107. if( script_hasdata(st,2) )
  10108. nd = npc_name2id(script_getstr(st, 2));
  10109. else
  10110. nd = (struct npc_data *)map_id2bl(st->oid);
  10111. if( nd != NULL && (cd=(struct chat_data *)map_id2bl(nd->chat_id)) != NULL )
  10112. chat_enableevent(cd);
  10113. return SCRIPT_CMD_SUCCESS;
  10114. }
  10115. /// Disables the waiting room event of the current or target npc.
  10116. ///
  10117. /// disablewaitingroomevent "<npc_name>";
  10118. /// disablewaitingroomevent;
  10119. BUILDIN_FUNC(disablewaitingroomevent)
  10120. {
  10121. struct npc_data *nd;
  10122. struct chat_data *cd;
  10123. if( script_hasdata(st,2) )
  10124. nd = npc_name2id(script_getstr(st, 2));
  10125. else
  10126. nd = (struct npc_data *)map_id2bl(st->oid);
  10127. if( nd != NULL && (cd=(struct chat_data *)map_id2bl(nd->chat_id)) != NULL )
  10128. chat_disableevent(cd);
  10129. return SCRIPT_CMD_SUCCESS;
  10130. }
  10131. /// Returns info on the waiting room of the current or target npc.
  10132. /// Returns -1 if the type unknown
  10133. /// <type>=0 : current number of users
  10134. /// <type>=1 : maximum number of users allowed
  10135. /// <type>=2 : the number of users that trigger the event
  10136. /// <type>=3 : if the trigger is disabled
  10137. /// <type>=4 : the title of the waiting room
  10138. /// <type>=5 : the password of the waiting room
  10139. /// <type>=16 : the name of the waiting room event
  10140. /// <type>=32 : if the waiting room is full
  10141. /// <type>=33 : if there are enough users to trigger the event
  10142. ///
  10143. /// getwaitingroomstate(<type>,"<npc_name>") -> <info>
  10144. /// getwaitingroomstate(<type>) -> <info>
  10145. BUILDIN_FUNC(getwaitingroomstate)
  10146. {
  10147. struct npc_data *nd;
  10148. struct chat_data *cd;
  10149. int type;
  10150. type = script_getnum(st,2);
  10151. if( script_hasdata(st,3) )
  10152. nd = npc_name2id(script_getstr(st, 3));
  10153. else
  10154. nd = (struct npc_data *)map_id2bl(st->oid);
  10155. if( nd == NULL || (cd=(struct chat_data *)map_id2bl(nd->chat_id)) == NULL )
  10156. {
  10157. script_pushint(st, -1);
  10158. return SCRIPT_CMD_SUCCESS;
  10159. }
  10160. switch(type)
  10161. {
  10162. case 0: script_pushint(st, cd->users); break;
  10163. case 1: script_pushint(st, cd->limit); break;
  10164. case 2: script_pushint(st, cd->trigger&0x7f); break;
  10165. case 3: script_pushint(st, ((cd->trigger&0x80)!=0)); break;
  10166. case 4: script_pushstrcopy(st, cd->title); break;
  10167. case 5: script_pushstrcopy(st, cd->pass); break;
  10168. case 16: script_pushstrcopy(st, cd->npc_event);break;
  10169. case 32: script_pushint(st, (cd->users >= cd->limit)); break;
  10170. case 33: script_pushint(st, (cd->users >= cd->trigger)); break;
  10171. default: script_pushint(st, -1); break;
  10172. }
  10173. return SCRIPT_CMD_SUCCESS;
  10174. }
  10175. /// Warps the trigger or target amount of players to the target map and position.
  10176. /// Players are automatically removed from the waiting room.
  10177. /// Those waiting the longest will get warped first.
  10178. /// The target map can be "Random" for a random position in the current map,
  10179. /// and "SavePoint" for the savepoint map+position.
  10180. /// The map flag noteleport of the current map is only considered when teleporting to the savepoint.
  10181. ///
  10182. /// The id's of the teleported players are put into the array $@warpwaitingpc[]
  10183. /// The total number of teleported players is put into $@warpwaitingpcnum
  10184. ///
  10185. /// warpwaitingpc "<map name>",<x>,<y>,<number of players>;
  10186. /// warpwaitingpc "<map name>",<x>,<y>;
  10187. BUILDIN_FUNC(warpwaitingpc)
  10188. {
  10189. int x;
  10190. int y;
  10191. int i;
  10192. int n;
  10193. const char* map_name;
  10194. struct npc_data* nd;
  10195. struct chat_data* cd;
  10196. nd = (struct npc_data *)map_id2bl(st->oid);
  10197. if( nd == NULL || (cd=(struct chat_data *)map_id2bl(nd->chat_id)) == NULL )
  10198. return SCRIPT_CMD_SUCCESS;
  10199. map_name = script_getstr(st,2);
  10200. x = script_getnum(st,3);
  10201. y = script_getnum(st,4);
  10202. n = cd->trigger&0x7f;
  10203. if( script_hasdata(st,5) )
  10204. n = script_getnum(st,5);
  10205. for( i = 0; i < n && cd->users > 0; i++ )
  10206. {
  10207. TBL_PC* sd = cd->usersd[0];
  10208. if( strcmp(map_name,"SavePoint") == 0 && map[sd->bl.m].flag.noteleport )
  10209. {// can't teleport on this map
  10210. break;
  10211. }
  10212. if( cd->zeny )
  10213. {// fee set
  10214. if( (uint32)sd->status.zeny < cd->zeny )
  10215. {// no zeny to cover set fee
  10216. break;
  10217. }
  10218. pc_payzeny(sd, cd->zeny, LOG_TYPE_NPC, NULL);
  10219. }
  10220. mapreg_setreg(reference_uid(add_str("$@warpwaitingpc"), i), sd->bl.id);
  10221. if( strcmp(map_name,"Random") == 0 )
  10222. pc_randomwarp(sd,CLR_TELEPORT);
  10223. else if( strcmp(map_name,"SavePoint") == 0 )
  10224. pc_setpos(sd, sd->status.save_point.map, sd->status.save_point.x, sd->status.save_point.y, CLR_TELEPORT);
  10225. else
  10226. pc_setpos(sd, mapindex_name2id(map_name), x, y, CLR_OUTSIGHT);
  10227. }
  10228. mapreg_setreg(add_str("$@warpwaitingpcnum"), i);
  10229. return SCRIPT_CMD_SUCCESS;
  10230. }
  10231. /////////////////////////////////////////////////////////////////////
  10232. // ...
  10233. //
  10234. /// Detaches a character from a script.
  10235. ///
  10236. /// @param st Script state to detach the character from.
  10237. static void script_detach_rid(struct script_state* st)
  10238. {
  10239. if(st->rid)
  10240. {
  10241. script_detach_state(st, false);
  10242. st->rid = 0;
  10243. }
  10244. }
  10245. /*=========================================================================
  10246. * Attaches a set of RIDs to the current script. [digitalhamster]
  10247. * addrid(<type>{,<flag>{,<parameters>}});
  10248. * <type>:
  10249. * 0 : All players in the server.
  10250. * 1 : All players in the map of the invoking player, or the invoking NPC if no player is attached.
  10251. * 2 : Party members of a specified party ID.
  10252. * [ Parameters: <party id> ]
  10253. * 3 : Guild members of a specified guild ID.
  10254. * [ Parameters: <guild id> ]
  10255. * 4 : All players in a specified area of the map of the invoking player (or NPC).
  10256. * [ Parameters: <x0>,<y0>,<x1>,<y1> ]
  10257. * 5 : All players in the map.
  10258. * [ Parameters: "<map name>" ]
  10259. * Account ID: The specified account ID.
  10260. * <flag>:
  10261. * 0 : Players are always attached. (default)
  10262. * 1 : Players currently running another script will not be attached.
  10263. *-------------------------------------------------------------------------*/
  10264. static int buildin_addrid_sub(struct block_list *bl,va_list ap)
  10265. {
  10266. int forceflag;
  10267. struct map_session_data *sd = (TBL_PC *)bl;
  10268. struct script_state* st;
  10269. st = va_arg(ap,struct script_state*);
  10270. forceflag = va_arg(ap,int);
  10271. if(!forceflag || !sd->st)
  10272. if(sd->status.account_id != st->rid)
  10273. run_script(st->script,st->pos,sd->status.account_id,st->oid);
  10274. return 0;
  10275. }
  10276. BUILDIN_FUNC(addrid)
  10277. {
  10278. struct s_mapiterator* iter;
  10279. struct block_list *bl;
  10280. TBL_PC *sd;
  10281. if(st->rid < 1) {
  10282. st->state = END;
  10283. bl = map_id2bl(st->oid);
  10284. } else
  10285. bl = map_id2bl(st->rid); //if run without rid it'd error,also oid if npc, else rid for map
  10286. iter = mapit_getallusers();
  10287. switch(script_getnum(st,2)) {
  10288. case 0:
  10289. for( sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); sd = (TBL_PC*)mapit_next(iter)) {
  10290. if(!script_getnum(st,3) || !sd->st)
  10291. if(sd->status.account_id != st->rid) //attached player already runs.
  10292. run_script(st->script,st->pos,sd->status.account_id,st->oid);
  10293. }
  10294. break;
  10295. case 1:
  10296. for( sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); sd = (TBL_PC*)mapit_next(iter)) {
  10297. if(!script_getnum(st,3) || !sd->st)
  10298. if((sd->bl.m == bl->m) && (sd->status.account_id != st->rid))
  10299. run_script(st->script,st->pos,sd->status.account_id,st->oid);
  10300. }
  10301. break;
  10302. case 2:
  10303. if(script_getnum(st,4) == 0) {
  10304. script_pushint(st,0);
  10305. return SCRIPT_CMD_SUCCESS;
  10306. }
  10307. for( sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); sd = (TBL_PC*)mapit_next(iter)) {
  10308. if(!script_getnum(st,3) || !sd->st)
  10309. if((sd->status.account_id != st->rid) && (sd->status.party_id == script_getnum(st,4))) //attached player already runs.
  10310. run_script(st->script,st->pos,sd->status.account_id,st->oid);
  10311. }
  10312. break;
  10313. case 3:
  10314. if(script_getnum(st,4) == 0) {
  10315. script_pushint(st,0);
  10316. return SCRIPT_CMD_SUCCESS;
  10317. }
  10318. for( sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); sd = (TBL_PC*)mapit_next(iter)) {
  10319. if(!script_getnum(st,3) || !sd->st)
  10320. if((sd->status.account_id != st->rid) && (sd->status.guild_id == script_getnum(st,4))) //attached player already runs.
  10321. run_script(st->script,st->pos,sd->status.account_id,st->oid);
  10322. }
  10323. break;
  10324. case 4:
  10325. map_foreachinarea(buildin_addrid_sub,
  10326. bl->m,script_getnum(st,4),script_getnum(st,5),script_getnum(st,6),script_getnum(st,7),BL_PC,
  10327. st,script_getnum(st,3));//4-x0 , 5-y0 , 6-x1, 7-y1
  10328. break;
  10329. case 5:
  10330. if (script_getstr(st, 4) == NULL) {
  10331. script_pushint(st, 0);
  10332. return SCRIPT_CMD_FAILURE;
  10333. }
  10334. if (map_mapname2mapid(script_getstr(st, 4)) < 0) {
  10335. script_pushint(st, 0);
  10336. return SCRIPT_CMD_FAILURE;
  10337. }
  10338. map_foreachinmap(buildin_addrid_sub, map_mapname2mapid(script_getstr(st, 4)), BL_PC, st, script_getnum(st, 3));
  10339. break;
  10340. default:
  10341. if((map_id2sd(script_getnum(st,2))) == NULL) { // Player not found.
  10342. script_pushint(st,0);
  10343. return SCRIPT_CMD_SUCCESS;
  10344. }
  10345. if(!script_getnum(st,3) || !map_id2sd(script_getnum(st,2))->st) {
  10346. run_script(st->script,st->pos,script_getnum(st,2),st->oid);
  10347. script_pushint(st,1);
  10348. }
  10349. return SCRIPT_CMD_SUCCESS;
  10350. }
  10351. mapit_free(iter);
  10352. script_pushint(st,1);
  10353. return SCRIPT_CMD_SUCCESS;
  10354. }
  10355. /*==========================================
  10356. * Attach sd char id to script and detach current one if any
  10357. *------------------------------------------*/
  10358. BUILDIN_FUNC(attachrid)
  10359. {
  10360. int rid = script_getnum(st,2);
  10361. if (map_id2sd(rid) != NULL) {
  10362. script_detach_rid(st);
  10363. st->rid = rid;
  10364. script_attach_state(st);
  10365. script_pushint(st,1);
  10366. } else
  10367. script_pushint(st,0);
  10368. return SCRIPT_CMD_SUCCESS;
  10369. }
  10370. /*==========================================
  10371. * Detach script to rid
  10372. *------------------------------------------*/
  10373. BUILDIN_FUNC(detachrid)
  10374. {
  10375. script_detach_rid(st);
  10376. return SCRIPT_CMD_SUCCESS;
  10377. }
  10378. /*==========================================
  10379. * Chk if account connected, (and charid from account if specified)
  10380. *------------------------------------------*/
  10381. BUILDIN_FUNC(isloggedin)
  10382. {
  10383. TBL_PC* sd = map_id2sd(script_getnum(st,2));
  10384. if (script_hasdata(st,3) && sd &&
  10385. sd->status.char_id != script_getnum(st,3))
  10386. sd = NULL;
  10387. push_val(st->stack,C_INT,sd!=NULL);
  10388. return SCRIPT_CMD_SUCCESS;
  10389. }
  10390. /*==========================================
  10391. *
  10392. *------------------------------------------*/
  10393. BUILDIN_FUNC(setmapflagnosave)
  10394. {
  10395. int16 m,x,y;
  10396. unsigned short mapindex;
  10397. const char *str,*str2;
  10398. str=script_getstr(st,2);
  10399. str2=script_getstr(st,3);
  10400. x=script_getnum(st,4);
  10401. y=script_getnum(st,5);
  10402. m = map_mapname2mapid(str);
  10403. mapindex = mapindex_name2id(str2);
  10404. if(m >= 0 && mapindex) {
  10405. map[m].flag.nosave=1;
  10406. map[m].save.map=mapindex;
  10407. map[m].save.x=x;
  10408. map[m].save.y=y;
  10409. }
  10410. return SCRIPT_CMD_SUCCESS;
  10411. }
  10412. BUILDIN_FUNC(getmapflag)
  10413. {
  10414. int16 m,i;
  10415. const char *str;
  10416. str=script_getstr(st,2);
  10417. i=script_getnum(st,3);
  10418. m = map_mapname2mapid(str);
  10419. if(m >= 0) {
  10420. switch(i) {
  10421. case MF_NOMEMO: script_pushint(st,map[m].flag.nomemo); break;
  10422. case MF_NOTELEPORT: script_pushint(st,map[m].flag.noteleport); break;
  10423. case MF_NOSAVE: script_pushint(st,map[m].flag.nosave); break;
  10424. case MF_NOBRANCH: script_pushint(st,map[m].flag.nobranch); break;
  10425. case MF_NOPENALTY: script_pushint(st,map[m].flag.noexppenalty); break;
  10426. case MF_NOZENYPENALTY: script_pushint(st,map[m].flag.nozenypenalty); break;
  10427. case MF_PVP: script_pushint(st,map[m].flag.pvp); break;
  10428. case MF_PVP_NOPARTY: script_pushint(st,map[m].flag.pvp_noparty); break;
  10429. case MF_PVP_NOGUILD: script_pushint(st,map[m].flag.pvp_noguild); break;
  10430. case MF_GVG: script_pushint(st,map[m].flag.gvg); break;
  10431. case MF_GVG_NOPARTY: script_pushint(st,map[m].flag.gvg_noparty); break;
  10432. case MF_NOTRADE: script_pushint(st,map[m].flag.notrade); break;
  10433. case MF_NOSKILL: script_pushint(st,map[m].flag.noskill); break;
  10434. case MF_NOWARP: script_pushint(st,map[m].flag.nowarp); break;
  10435. case MF_PARTYLOCK: script_pushint(st,map[m].flag.partylock); break;
  10436. case MF_NOICEWALL: script_pushint(st,map[m].flag.noicewall); break;
  10437. case MF_SNOW: script_pushint(st,map[m].flag.snow); break;
  10438. case MF_FOG: script_pushint(st,map[m].flag.fog); break;
  10439. case MF_SAKURA: script_pushint(st,map[m].flag.sakura); break;
  10440. case MF_LEAVES: script_pushint(st,map[m].flag.leaves); break;
  10441. case MF_NOGO: script_pushint(st,map[m].flag.nogo); break;
  10442. case MF_CLOUDS: script_pushint(st,map[m].flag.clouds); break;
  10443. case MF_CLOUDS2: script_pushint(st,map[m].flag.clouds2); break;
  10444. case MF_FIREWORKS: script_pushint(st,map[m].flag.fireworks); break;
  10445. case MF_GVG_CASTLE: script_pushint(st,map[m].flag.gvg_castle); break;
  10446. case MF_GVG_DUNGEON: script_pushint(st,map[m].flag.gvg_dungeon); break;
  10447. case MF_NIGHTENABLED: script_pushint(st,map[m].flag.nightenabled); break;
  10448. case MF_NOBASEEXP: script_pushint(st,map[m].flag.nobaseexp); break;
  10449. case MF_NOJOBEXP: script_pushint(st,map[m].flag.nojobexp); break;
  10450. case MF_NOMOBLOOT: script_pushint(st,map[m].flag.nomobloot); break;
  10451. case MF_NOMVPLOOT: script_pushint(st,map[m].flag.nomvploot); break;
  10452. case MF_NORETURN: script_pushint(st,map[m].flag.noreturn); break;
  10453. case MF_NOWARPTO: script_pushint(st,map[m].flag.nowarpto); break;
  10454. case MF_NIGHTMAREDROP: script_pushint(st,map[m].flag.pvp_nightmaredrop); break;
  10455. case MF_RESTRICTED: script_pushint(st,map[m].flag.restricted); break;
  10456. case MF_NOCOMMAND: script_pushint(st,map[m].nocommand); break;
  10457. case MF_NODROP: script_pushint(st,map[m].flag.nodrop); break;
  10458. case MF_JEXP: script_pushint(st,map[m].adjust.jexp); break;
  10459. case MF_BEXP: script_pushint(st,map[m].adjust.bexp); break;
  10460. case MF_NOVENDING: script_pushint(st,map[m].flag.novending); break;
  10461. case MF_LOADEVENT: script_pushint(st,map[m].flag.loadevent); break;
  10462. case MF_NOCHAT: script_pushint(st,map[m].flag.nochat); break;
  10463. case MF_NOEXPPENALTY: script_pushint(st,map[m].flag.noexppenalty ); break;
  10464. case MF_GUILDLOCK: script_pushint(st,map[m].flag.guildlock); break;
  10465. case MF_TOWN: script_pushint(st,map[m].flag.town); break;
  10466. case MF_AUTOTRADE: script_pushint(st,map[m].flag.autotrade); break;
  10467. case MF_ALLOWKS: script_pushint(st,map[m].flag.allowks); break;
  10468. case MF_MONSTER_NOTELEPORT: script_pushint(st,map[m].flag.monster_noteleport); break;
  10469. case MF_PVP_NOCALCRANK: script_pushint(st,map[m].flag.pvp_nocalcrank); break;
  10470. case MF_BATTLEGROUND: script_pushint(st,map[m].flag.battleground); break;
  10471. case MF_RESET: script_pushint(st,map[m].flag.reset); break;
  10472. case MF_CHANNELAUTOJOIN: script_pushint(st,map[m].flag.chmautojoin); break;
  10473. case MF_NOUSECART: script_pushint(st,map[m].flag.nousecart); break;
  10474. case MF_NOITEMCONSUMPTION: script_pushint(st,map[m].flag.noitemconsumption); break;
  10475. case MF_SUMSTARTMIRACLE: script_pushint(st,map[m].flag.nosumstarmiracle); break;
  10476. case MF_NOMINEEFFECT: script_pushint(st,map[m].flag.nomineeffect); break;
  10477. case MF_NOLOCKON: script_pushint(st,map[m].flag.nolockon); break;
  10478. case MF_NOTOMB: script_pushint(st,map[m].flag.notomb); break;
  10479. case MF_NOCOSTUME: script_pushint(st,map[m].flag.nocostume); break;
  10480. case MF_GVG_TE_CASTLE: script_pushint(st,map[m].flag.gvg_te_castle); break;
  10481. case MF_GVG_TE: script_pushint(st,map[m].flag.gvg_te); break;
  10482. case MF_HIDEMOBHPBAR: script_pushint(st,map[m].flag.hidemobhpbar); break;
  10483. #ifdef ADJUST_SKILL_DAMAGE
  10484. case MF_SKILL_DAMAGE:
  10485. {
  10486. int ret_val=0, type=0;
  10487. FETCH(4,type);
  10488. switch (type) {
  10489. case 1: ret_val = map[m].adjust.damage.pc; break;
  10490. case 2: ret_val = map[m].adjust.damage.mob; break;
  10491. case 3: ret_val = map[m].adjust.damage.boss; break;
  10492. case 4: ret_val = map[m].adjust.damage.other; break;
  10493. case 5: ret_val = map[m].adjust.damage.caster; break;
  10494. default: ret_val = map[m].flag.skill_damage; break;
  10495. }
  10496. script_pushint(st,ret_val); break;
  10497. } break;
  10498. #endif
  10499. }
  10500. }
  10501. return SCRIPT_CMD_SUCCESS;
  10502. }
  10503. /* pvp timer handling */
  10504. static int script_mapflag_pvp_sub(struct block_list *bl,va_list ap) {
  10505. TBL_PC* sd = (TBL_PC*)bl;
  10506. if (sd->pvp_timer == INVALID_TIMER) {
  10507. sd->pvp_timer = add_timer(gettick() + 200, pc_calc_pvprank_timer, sd->bl.id, 0);
  10508. sd->pvp_rank = 0;
  10509. sd->pvp_lastusers = 0;
  10510. sd->pvp_point = 5;
  10511. sd->pvp_won = 0;
  10512. sd->pvp_lost = 0;
  10513. }
  10514. clif_map_property(&sd->bl, MAPPROPERTY_FREEPVPZONE,SELF);
  10515. return 0;
  10516. }
  10517. BUILDIN_FUNC(setmapflag)
  10518. {
  10519. int16 m,i;
  10520. const char *str;
  10521. int val=0;
  10522. str=script_getstr(st,2);
  10523. i=script_getnum(st,3);
  10524. FETCH(4,val);
  10525. m = map_mapname2mapid(str);
  10526. if(m >= 0) {
  10527. switch(i) {
  10528. case MF_NOMEMO: map[m].flag.nomemo = 1; break;
  10529. case MF_NOTELEPORT: map[m].flag.noteleport = 1; break;
  10530. case MF_NOSAVE: map[m].flag.nosave = 1; break;
  10531. case MF_NOBRANCH: map[m].flag.nobranch = 1; break;
  10532. case MF_NOPENALTY: map[m].flag.noexppenalty = 1; map[m].flag.nozenypenalty = 1; break;
  10533. case MF_NOZENYPENALTY: map[m].flag.nozenypenalty = 1; break;
  10534. case MF_PVP:
  10535. map[m].flag.pvp = 1;
  10536. if( !battle_config.pk_mode ) {
  10537. map_foreachinmap(script_mapflag_pvp_sub,m,BL_PC);
  10538. }
  10539. break;
  10540. case MF_PVP_NOPARTY: map[m].flag.pvp_noparty = 1; break;
  10541. case MF_PVP_NOGUILD: map[m].flag.pvp_noguild = 1; break;
  10542. case MF_GVG:
  10543. map[m].flag.gvg = 1;
  10544. clif_map_property_mapall(m, MAPPROPERTY_AGITZONE);
  10545. break;
  10546. case MF_GVG_NOPARTY: map[m].flag.gvg_noparty = 1; break;
  10547. case MF_NOTRADE: map[m].flag.notrade = 1; break;
  10548. case MF_NOSKILL: map[m].flag.noskill = 1; break;
  10549. case MF_NOWARP: map[m].flag.nowarp = 1; break;
  10550. case MF_PARTYLOCK: map[m].flag.partylock = 1; break;
  10551. case MF_NOICEWALL: map[m].flag.noicewall = 1; break;
  10552. case MF_SNOW: map[m].flag.snow = 1; break;
  10553. case MF_FOG: map[m].flag.fog = 1; break;
  10554. case MF_SAKURA: map[m].flag.sakura = 1; break;
  10555. case MF_LEAVES: map[m].flag.leaves = 1; break;
  10556. case MF_NOGO: map[m].flag.nogo = 1; break;
  10557. case MF_CLOUDS: map[m].flag.clouds = 1; break;
  10558. case MF_CLOUDS2: map[m].flag.clouds2 = 1; break;
  10559. case MF_FIREWORKS: map[m].flag.fireworks = 1; break;
  10560. case MF_GVG_CASTLE: map[m].flag.gvg_castle = 1; break;
  10561. case MF_GVG_DUNGEON: map[m].flag.gvg_dungeon = 1; break;
  10562. case MF_NIGHTENABLED: map[m].flag.nightenabled = 1; break;
  10563. case MF_NOBASEEXP: map[m].flag.nobaseexp = 1; break;
  10564. case MF_NOJOBEXP: map[m].flag.nojobexp = 1; break;
  10565. case MF_NOMOBLOOT: map[m].flag.nomobloot = 1; break;
  10566. case MF_NOMVPLOOT: map[m].flag.nomvploot = 1; break;
  10567. case MF_NORETURN: map[m].flag.noreturn = 1; break;
  10568. case MF_NOWARPTO: map[m].flag.nowarpto = 1; break;
  10569. case MF_NIGHTMAREDROP: map[m].flag.pvp_nightmaredrop = 1; break;
  10570. case MF_RESTRICTED:
  10571. map[m].zone |= 1<<(val+1);
  10572. map[m].flag.restricted=1;
  10573. break;
  10574. case MF_NOCOMMAND: map[m].nocommand = (val <= 0) ? 100 : val; break;
  10575. case MF_NODROP: map[m].flag.nodrop = 1; break;
  10576. case MF_JEXP: map[m].adjust.jexp = (val <= 0) ? 100 : val; break;
  10577. case MF_BEXP: map[m].adjust.bexp = (val <= 0) ? 100 : val; break;
  10578. case MF_NOVENDING: map[m].flag.novending = 1; break;
  10579. case MF_LOADEVENT: map[m].flag.loadevent = 1; break;
  10580. case MF_NOCHAT: map[m].flag.nochat = 1; break;
  10581. case MF_NOEXPPENALTY: map[m].flag.noexppenalty = 1; break;
  10582. case MF_GUILDLOCK: map[m].flag.guildlock = 1; break;
  10583. case MF_TOWN: map[m].flag.town = 1; break;
  10584. case MF_AUTOTRADE: map[m].flag.autotrade = 1; break;
  10585. case MF_ALLOWKS: map[m].flag.allowks = 1; break;
  10586. case MF_MONSTER_NOTELEPORT: map[m].flag.monster_noteleport = 1; break;
  10587. case MF_PVP_NOCALCRANK: map[m].flag.pvp_nocalcrank = 1; break;
  10588. case MF_BATTLEGROUND: map[m].flag.battleground = (val <= 0 || val > 2) ? 1 : val; break;
  10589. case MF_RESET: map[m].flag.reset = 1; break;
  10590. case MF_CHANNELAUTOJOIN: map[m].flag.chmautojoin = 1 ; break;
  10591. case MF_NOUSECART: map[m].flag.nousecart = 1 ; break;
  10592. case MF_NOITEMCONSUMPTION: map[m].flag.noitemconsumption = 1 ; break;
  10593. case MF_SUMSTARTMIRACLE: map[m].flag.nosumstarmiracle = 1 ; break;
  10594. case MF_NOMINEEFFECT: map[m].flag.nomineeffect = 1 ; break;
  10595. case MF_NOLOCKON: map[m].flag.nolockon = 1 ; break;
  10596. case MF_NOTOMB: map[m].flag.notomb = 1; break;
  10597. case MF_NOCOSTUME: map[m].flag.nocostume = 1; break;
  10598. case MF_GVG_TE_CASTLE: map[m].flag.gvg_te_castle = 1; break;
  10599. case MF_GVG_TE:
  10600. map[m].flag.gvg_te = 1;
  10601. clif_map_property_mapall(m, MAPPROPERTY_AGITZONE);
  10602. break;
  10603. case MF_HIDEMOBHPBAR: map[m].flag.hidemobhpbar = 1; break;
  10604. #ifdef ADJUST_SKILL_DAMAGE
  10605. case MF_SKILL_DAMAGE:
  10606. {
  10607. int type=0;
  10608. FETCH(5,type);
  10609. switch (type) {
  10610. case 1: map[m].adjust.damage.pc = val; break;
  10611. case 2: map[m].adjust.damage.mob = val; break;
  10612. case 3: map[m].adjust.damage.boss = val; break;
  10613. case 4: map[m].adjust.damage.other = val; break;
  10614. case 5: map[m].adjust.damage.caster = val; break;
  10615. }
  10616. map[m].flag.skill_damage = 1;
  10617. } break;
  10618. #endif
  10619. }
  10620. }
  10621. return SCRIPT_CMD_SUCCESS;
  10622. }
  10623. BUILDIN_FUNC(removemapflag)
  10624. {
  10625. int16 m,i;
  10626. const char *str;
  10627. int val=0;
  10628. str=script_getstr(st,2);
  10629. i=script_getnum(st,3);
  10630. FETCH(4,val);
  10631. m = map_mapname2mapid(str);
  10632. if(m >= 0) {
  10633. switch(i) {
  10634. case MF_NOMEMO: map[m].flag.nomemo = 0; break;
  10635. case MF_NOTELEPORT: map[m].flag.noteleport = 0; break;
  10636. case MF_NOSAVE: map[m].flag.nosave = 0; break;
  10637. case MF_NOBRANCH: map[m].flag.nobranch = 0; break;
  10638. case MF_NOPENALTY: map[m].flag.noexppenalty = 0; map[m].flag.nozenypenalty = 0; break;
  10639. case MF_NOZENYPENALTY: map[m].flag.nozenypenalty = 0; break;
  10640. case MF_PVP:
  10641. map[m].flag.pvp = 0;
  10642. clif_map_property_mapall(m, MAPPROPERTY_NOTHING);
  10643. break;
  10644. case MF_PVP_NOPARTY: map[m].flag.pvp_noparty = 0; break;
  10645. case MF_PVP_NOGUILD: map[m].flag.pvp_noguild = 0; break;
  10646. case MF_GVG:
  10647. map[m].flag.gvg = 0;
  10648. clif_map_property_mapall(m, MAPPROPERTY_NOTHING);
  10649. break;
  10650. case MF_GVG_NOPARTY: map[m].flag.gvg_noparty = 0; break;
  10651. case MF_NOTRADE: map[m].flag.notrade = 0; break;
  10652. case MF_NOSKILL: map[m].flag.noskill = 0; break;
  10653. case MF_NOWARP: map[m].flag.nowarp = 0; break;
  10654. case MF_PARTYLOCK: map[m].flag.partylock = 0; break;
  10655. case MF_NOICEWALL: map[m].flag.noicewall = 0; break;
  10656. case MF_SNOW: map[m].flag.snow = 0; break;
  10657. case MF_FOG: map[m].flag.fog = 0; break;
  10658. case MF_SAKURA: map[m].flag.sakura = 0; break;
  10659. case MF_LEAVES: map[m].flag.leaves = 0; break;
  10660. case MF_NOGO: map[m].flag.nogo = 0; break;
  10661. case MF_CLOUDS: map[m].flag.clouds = 0; break;
  10662. case MF_CLOUDS2: map[m].flag.clouds2 = 0; break;
  10663. case MF_FIREWORKS: map[m].flag.fireworks = 0; break;
  10664. case MF_GVG_CASTLE: map[m].flag.gvg_castle = 0; break;
  10665. case MF_GVG_DUNGEON: map[m].flag.gvg_dungeon = 0; break;
  10666. case MF_NIGHTENABLED: map[m].flag.nightenabled = 0; break;
  10667. case MF_NOBASEEXP: map[m].flag.nobaseexp = 0; break;
  10668. case MF_NOJOBEXP: map[m].flag.nojobexp = 0; break;
  10669. case MF_NOMOBLOOT: map[m].flag.nomobloot = 0; break;
  10670. case MF_NOMVPLOOT: map[m].flag.nomvploot = 0; break;
  10671. case MF_NORETURN: map[m].flag.noreturn = 0; break;
  10672. case MF_NOWARPTO: map[m].flag.nowarpto = 0; break;
  10673. case MF_NIGHTMAREDROP: map[m].flag.pvp_nightmaredrop = 0; break;
  10674. case MF_RESTRICTED:
  10675. map[m].zone ^= 1<<(val+1);
  10676. if (map[m].zone == 0){
  10677. map[m].flag.restricted=0;
  10678. }
  10679. break;
  10680. case MF_NOCOMMAND: map[m].nocommand = 0; break;
  10681. case MF_NODROP: map[m].flag.nodrop = 0; break;
  10682. case MF_JEXP: map[m].adjust.jexp = 0; break;
  10683. case MF_BEXP: map[m].adjust.bexp = 0; break;
  10684. case MF_NOVENDING: map[m].flag.novending = 0; break;
  10685. case MF_LOADEVENT: map[m].flag.loadevent = 0; break;
  10686. case MF_NOCHAT: map[m].flag.nochat = 0; break;
  10687. case MF_NOEXPPENALTY: map[m].flag.noexppenalty = 0; break;
  10688. case MF_GUILDLOCK: map[m].flag.guildlock = 0; break;
  10689. case MF_TOWN: map[m].flag.town = 0; break;
  10690. case MF_AUTOTRADE: map[m].flag.autotrade = 0; break;
  10691. case MF_ALLOWKS: map[m].flag.allowks = 0; break;
  10692. case MF_MONSTER_NOTELEPORT: map[m].flag.monster_noteleport = 0; break;
  10693. case MF_PVP_NOCALCRANK: map[m].flag.pvp_nocalcrank = 0; break;
  10694. case MF_BATTLEGROUND: map[m].flag.battleground = 0; break;
  10695. case MF_RESET: map[m].flag.reset = 0; break;
  10696. case MF_CHANNELAUTOJOIN: map[m].flag.chmautojoin = 0 ; break;
  10697. case MF_NOUSECART: map[m].flag.nousecart = 0 ; break;
  10698. case MF_NOITEMCONSUMPTION: map[m].flag.noitemconsumption = 0 ; break;
  10699. case MF_SUMSTARTMIRACLE: map[m].flag.nosumstarmiracle = 0 ; break;
  10700. case MF_NOMINEEFFECT: map[m].flag.nomineeffect = 0 ; break;
  10701. case MF_NOLOCKON: map[m].flag.nolockon = 0 ; break;
  10702. case MF_NOTOMB: map[m].flag.notomb = 0; break;
  10703. case MF_NOCOSTUME: map[m].flag.nocostume = 0; break;
  10704. case MF_GVG_TE_CASTLE: map[m].flag.gvg_te_castle = 0; break;
  10705. case MF_GVG_TE:
  10706. map[m].flag.gvg_te = 0;
  10707. clif_map_property_mapall(m, MAPPROPERTY_NOTHING);
  10708. break;
  10709. case MF_HIDEMOBHPBAR: map[m].flag.hidemobhpbar = 0; break;
  10710. #ifdef ADJUST_SKILL_DAMAGE
  10711. case MF_SKILL_DAMAGE:
  10712. {
  10713. map[m].flag.skill_damage = 0;
  10714. memset(&map[m].adjust.damage, 0, sizeof(map[m].adjust.damage));
  10715. if (map[m].skill_damage.count)
  10716. map_skill_damage_free(&map[m]);
  10717. } break;
  10718. #endif
  10719. }
  10720. }
  10721. return SCRIPT_CMD_SUCCESS;
  10722. }
  10723. BUILDIN_FUNC(pvpon)
  10724. {
  10725. int16 m;
  10726. const char *str;
  10727. TBL_PC* sd = NULL;
  10728. struct s_mapiterator* iter;
  10729. str = script_getstr(st,2);
  10730. m = map_mapname2mapid(str);
  10731. if( m < 0 || map[m].flag.pvp )
  10732. return SCRIPT_CMD_SUCCESS; // nothing to do
  10733. map[m].flag.pvp = 1;
  10734. clif_map_property_mapall(m, MAPPROPERTY_FREEPVPZONE);
  10735. if(battle_config.pk_mode) // disable ranking functions if pk_mode is on [Valaris]
  10736. return SCRIPT_CMD_SUCCESS;
  10737. iter = mapit_getallusers();
  10738. for( sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); sd = (TBL_PC*)mapit_next(iter) )
  10739. {
  10740. if( sd->bl.m != m || sd->pvp_timer != INVALID_TIMER )
  10741. continue; // not applicable
  10742. sd->pvp_timer = add_timer(gettick()+200,pc_calc_pvprank_timer,sd->bl.id,0);
  10743. sd->pvp_rank = 0;
  10744. sd->pvp_lastusers = 0;
  10745. sd->pvp_point = 5;
  10746. sd->pvp_won = 0;
  10747. sd->pvp_lost = 0;
  10748. }
  10749. mapit_free(iter);
  10750. return SCRIPT_CMD_SUCCESS;
  10751. }
  10752. static int buildin_pvpoff_sub(struct block_list *bl,va_list ap)
  10753. {
  10754. TBL_PC* sd = (TBL_PC*)bl;
  10755. clif_pvpset(sd, 0, 0, 2);
  10756. if (sd->pvp_timer != INVALID_TIMER) {
  10757. delete_timer(sd->pvp_timer, pc_calc_pvprank_timer);
  10758. sd->pvp_timer = INVALID_TIMER;
  10759. }
  10760. return 0;
  10761. }
  10762. BUILDIN_FUNC(pvpoff)
  10763. {
  10764. int16 m;
  10765. const char *str;
  10766. str=script_getstr(st,2);
  10767. m = map_mapname2mapid(str);
  10768. if(m < 0 || !map[m].flag.pvp)
  10769. return SCRIPT_CMD_SUCCESS; //fixed Lupus
  10770. map[m].flag.pvp = 0;
  10771. clif_map_property_mapall(m, MAPPROPERTY_NOTHING);
  10772. if(battle_config.pk_mode) // disable ranking options if pk_mode is on [Valaris]
  10773. return SCRIPT_CMD_SUCCESS;
  10774. map_foreachinmap(buildin_pvpoff_sub, m, BL_PC);
  10775. return SCRIPT_CMD_SUCCESS;
  10776. }
  10777. BUILDIN_FUNC(gvgon)
  10778. {
  10779. int16 m;
  10780. const char *str;
  10781. str=script_getstr(st,2);
  10782. m = map_mapname2mapid(str);
  10783. if(m >= 0 && !map[m].flag.gvg) {
  10784. map[m].flag.gvg = 1;
  10785. clif_map_property_mapall(m, MAPPROPERTY_AGITZONE);
  10786. }
  10787. return SCRIPT_CMD_SUCCESS;
  10788. }
  10789. BUILDIN_FUNC(gvgoff)
  10790. {
  10791. int16 m;
  10792. const char *str;
  10793. str=script_getstr(st,2);
  10794. m = map_mapname2mapid(str);
  10795. if(m >= 0 && map[m].flag.gvg) {
  10796. map[m].flag.gvg = 0;
  10797. clif_map_property_mapall(m, MAPPROPERTY_NOTHING);
  10798. }
  10799. return SCRIPT_CMD_SUCCESS;
  10800. }
  10801. BUILDIN_FUNC(gvgon3)
  10802. {
  10803. int16 m;
  10804. const char *str;
  10805. str = script_getstr(st,2);
  10806. m = map_mapname2mapid(str);
  10807. if (m >= 0 && !map[m].flag.gvg_te) {
  10808. map[m].flag.gvg_te = 1;
  10809. clif_map_property_mapall(m, MAPPROPERTY_AGITZONE);
  10810. }
  10811. return SCRIPT_CMD_SUCCESS;
  10812. }
  10813. BUILDIN_FUNC(gvgoff3)
  10814. {
  10815. int16 m;
  10816. const char *str;
  10817. str = script_getstr(st,2);
  10818. m = map_mapname2mapid(str);
  10819. if (m >= 0 && map[m].flag.gvg_te) {
  10820. map[m].flag.gvg_te = 0;
  10821. clif_map_property_mapall(m, MAPPROPERTY_NOTHING);
  10822. }
  10823. return SCRIPT_CMD_SUCCESS;
  10824. }
  10825. /*==========================================
  10826. * Shows an emoticon on top of the player/npc
  10827. * emotion emotion#, <target: 0 - NPC, 1 - PC>, <NPC/PC name>
  10828. *------------------------------------------*/
  10829. //Optional second parameter added by [Skotlex]
  10830. BUILDIN_FUNC(emotion)
  10831. {
  10832. int type;
  10833. int player=0;
  10834. type=script_getnum(st,2);
  10835. if(type < 0 || type > 100)
  10836. return SCRIPT_CMD_SUCCESS;
  10837. if( script_hasdata(st,3) )
  10838. player=script_getnum(st,3);
  10839. if (player) {
  10840. TBL_PC *sd = NULL;
  10841. if( script_nick2sd(4,sd) ){
  10842. clif_emotion(&sd->bl, type);
  10843. }
  10844. } else
  10845. if( script_hasdata(st,4) )
  10846. {
  10847. TBL_NPC *nd = npc_name2id(script_getstr(st,4));
  10848. if(nd)
  10849. clif_emotion(&nd->bl,type);
  10850. }
  10851. else
  10852. clif_emotion(map_id2bl(st->oid),type);
  10853. return SCRIPT_CMD_SUCCESS;
  10854. }
  10855. static int buildin_maprespawnguildid_sub_pc(struct map_session_data* sd, va_list ap)
  10856. {
  10857. int16 m=va_arg(ap,int);
  10858. int g_id=va_arg(ap,int);
  10859. int flag=va_arg(ap,int);
  10860. if(!sd || sd->bl.m != m)
  10861. return 0;
  10862. if(
  10863. (sd->status.guild_id == g_id && flag&1) || //Warp out owners
  10864. (sd->status.guild_id != g_id && flag&2) || //Warp out outsiders
  10865. (sd->status.guild_id == 0 && flag&2) // Warp out players not in guild
  10866. )
  10867. pc_setpos(sd,sd->status.save_point.map,sd->status.save_point.x,sd->status.save_point.y,CLR_TELEPORT);
  10868. return 1;
  10869. }
  10870. static int buildin_maprespawnguildid_sub_mob(struct block_list *bl,va_list ap)
  10871. {
  10872. struct mob_data *md=(struct mob_data *)bl;
  10873. if(!md->guardian_data && md->mob_id != MOBID_EMPERIUM && ( !mob_is_clone(md->mob_id) || battle_config.guild_maprespawn_clones ))
  10874. status_kill(bl);
  10875. return 1;
  10876. }
  10877. /*
  10878. * Function to kick guild members out of a map and to their save points.
  10879. * m : mapid
  10880. * g_id : owner guild id
  10881. * flag & 1 : Warp out owners
  10882. * flag & 2 : Warp out outsiders
  10883. * flag & 4 : reserved for mobs
  10884. * */
  10885. BUILDIN_FUNC(maprespawnguildid)
  10886. {
  10887. const char *mapname=script_getstr(st,2);
  10888. int g_id=script_getnum(st,3);
  10889. int flag=script_getnum(st,4);
  10890. int16 m=map_mapname2mapid(mapname);
  10891. if(m == -1)
  10892. return SCRIPT_CMD_SUCCESS;
  10893. //Catch ALL players (in case some are 'between maps' on execution time)
  10894. map_foreachpc(buildin_maprespawnguildid_sub_pc,m,g_id,flag);
  10895. if (flag&4) //Remove script mobs.
  10896. map_foreachinmap(buildin_maprespawnguildid_sub_mob,m,BL_MOB);
  10897. return SCRIPT_CMD_SUCCESS;
  10898. }
  10899. /// Siege commands
  10900. /**
  10901. * Start WoE:FE
  10902. * agitstart();
  10903. */
  10904. BUILDIN_FUNC(agitstart)
  10905. {
  10906. guild_agit_start();
  10907. return SCRIPT_CMD_SUCCESS;
  10908. }
  10909. /**
  10910. * End WoE:FE
  10911. * agitend();
  10912. */
  10913. BUILDIN_FUNC(agitend)
  10914. {
  10915. guild_agit_end();
  10916. return SCRIPT_CMD_SUCCESS;
  10917. }
  10918. /**
  10919. * Start WoE:SE
  10920. * agitstart2();
  10921. */
  10922. BUILDIN_FUNC(agitstart2)
  10923. {
  10924. guild_agit2_start();
  10925. return SCRIPT_CMD_SUCCESS;
  10926. }
  10927. /**
  10928. * End WoE:SE
  10929. * agitend();
  10930. */
  10931. BUILDIN_FUNC(agitend2)
  10932. {
  10933. if (!agit2_flag)
  10934. return SCRIPT_CMD_SUCCESS;// Agit2 already Ended.
  10935. agit2_flag = false;
  10936. guild_agit2_end();
  10937. return SCRIPT_CMD_SUCCESS;
  10938. }
  10939. /**
  10940. * Start WoE:TE
  10941. * agitstart3();
  10942. */
  10943. BUILDIN_FUNC(agitstart3)
  10944. {
  10945. guild_agit3_start();
  10946. return SCRIPT_CMD_SUCCESS;
  10947. }
  10948. /**
  10949. * End WoE:TE
  10950. * agitend3();
  10951. */
  10952. BUILDIN_FUNC(agitend3)
  10953. {
  10954. guild_agit3_end();
  10955. return SCRIPT_CMD_SUCCESS;
  10956. }
  10957. /**
  10958. * Returns whether WoE:FE is on or off.
  10959. * agitcheck();
  10960. */
  10961. BUILDIN_FUNC(agitcheck)
  10962. {
  10963. script_pushint(st, agit_flag);
  10964. return SCRIPT_CMD_SUCCESS;
  10965. }
  10966. /**
  10967. * Returns whether WoE:SE is on or off.
  10968. * agitcheck2();
  10969. */
  10970. BUILDIN_FUNC(agitcheck2)
  10971. {
  10972. script_pushint(st, agit2_flag);
  10973. return SCRIPT_CMD_SUCCESS;
  10974. }
  10975. /**
  10976. * Returns whether WoE:TE is on or off.
  10977. * agitcheck3();
  10978. */
  10979. BUILDIN_FUNC(agitcheck3)
  10980. {
  10981. script_pushint(st, agit3_flag);
  10982. return SCRIPT_CMD_SUCCESS;
  10983. }
  10984. /// Sets the guild_id of this npc.
  10985. ///
  10986. /// flagemblem <guild_id>;
  10987. BUILDIN_FUNC(flagemblem)
  10988. {
  10989. TBL_NPC* nd;
  10990. int g_id = script_getnum(st,2);
  10991. if(g_id < 0)
  10992. return SCRIPT_CMD_SUCCESS;
  10993. nd = (TBL_NPC*)map_id2nd(st->oid);
  10994. if( nd == NULL ) {
  10995. ShowError("script:flagemblem: npc %d not found\n", st->oid);
  10996. } else if( nd->subtype != NPCTYPE_SCRIPT ) {
  10997. ShowError("script:flagemblem: unexpected subtype %d for npc %d '%s'\n", nd->subtype, st->oid, nd->exname);
  10998. } else {
  10999. bool changed = ( nd->u.scr.guild_id != g_id )?true:false;
  11000. nd->u.scr.guild_id = g_id;
  11001. clif_guild_emblem_area(&nd->bl);
  11002. /* guild flag caching */
  11003. if( g_id ) /* adding a id */
  11004. guild_flag_add(nd);
  11005. else if( changed ) /* removing a flag */
  11006. guild_flag_remove(nd);
  11007. }
  11008. return SCRIPT_CMD_SUCCESS;
  11009. }
  11010. BUILDIN_FUNC(getcastlename)
  11011. {
  11012. const char* mapname = mapindex_getmapname(script_getstr(st,2),NULL);
  11013. struct guild_castle* gc = guild_mapname2gc(mapname);
  11014. const char* name = (gc) ? gc->castle_name : "";
  11015. script_pushstrcopy(st,name);
  11016. return SCRIPT_CMD_SUCCESS;
  11017. }
  11018. BUILDIN_FUNC(getcastledata)
  11019. {
  11020. const char *mapname = mapindex_getmapname(script_getstr(st,2),NULL);
  11021. int index = script_getnum(st,3);
  11022. struct guild_castle *gc = guild_mapname2gc(mapname);
  11023. if (gc == NULL) {
  11024. script_pushint(st,0);
  11025. ShowWarning("buildin_getcastledata: guild castle for map '%s' not found\n", mapname);
  11026. return SCRIPT_CMD_FAILURE;
  11027. }
  11028. switch (index) {
  11029. case 1:
  11030. script_pushint(st,gc->guild_id); break;
  11031. case 2:
  11032. script_pushint(st,gc->economy); break;
  11033. case 3:
  11034. script_pushint(st,gc->defense); break;
  11035. case 4:
  11036. script_pushint(st,gc->triggerE); break;
  11037. case 5:
  11038. script_pushint(st,gc->triggerD); break;
  11039. case 6:
  11040. script_pushint(st,gc->nextTime); break;
  11041. case 7:
  11042. script_pushint(st,gc->payTime); break;
  11043. case 8:
  11044. script_pushint(st,gc->createTime); break;
  11045. case 9:
  11046. script_pushint(st,gc->visibleC); break;
  11047. default:
  11048. if (index > 9 && index <= 9+MAX_GUARDIANS) {
  11049. script_pushint(st,gc->guardian[index-10].visible);
  11050. break;
  11051. }
  11052. script_pushint(st,0);
  11053. ShowWarning("buildin_getcastledata: index = '%d' is out of allowed range\n", index);
  11054. return SCRIPT_CMD_FAILURE;
  11055. }
  11056. return SCRIPT_CMD_SUCCESS;
  11057. }
  11058. BUILDIN_FUNC(setcastledata)
  11059. {
  11060. const char *mapname = mapindex_getmapname(script_getstr(st,2),NULL);
  11061. int index = script_getnum(st,3);
  11062. int value = script_getnum(st,4);
  11063. struct guild_castle *gc = guild_mapname2gc(mapname);
  11064. if (gc == NULL) {
  11065. ShowWarning("buildin_setcastledata: guild castle for map '%s' not found\n", mapname);
  11066. return SCRIPT_CMD_FAILURE;
  11067. }
  11068. if (index <= 0 || index > 9+MAX_GUARDIANS) {
  11069. ShowWarning("buildin_setcastledata: index = '%d' is out of allowed range\n", index);
  11070. return SCRIPT_CMD_FAILURE;
  11071. }
  11072. guild_castledatasave(gc->castle_id, index, value);
  11073. return SCRIPT_CMD_SUCCESS;
  11074. }
  11075. /* =====================================================================
  11076. * ---------------------------------------------------------------------*/
  11077. BUILDIN_FUNC(requestguildinfo)
  11078. {
  11079. int guild_id=script_getnum(st,2);
  11080. const char *event=NULL;
  11081. if( script_hasdata(st,3) ){
  11082. event=script_getstr(st,3);
  11083. check_event(st, event);
  11084. }
  11085. if(guild_id>0)
  11086. guild_npc_request_info(guild_id,event);
  11087. return SCRIPT_CMD_SUCCESS;
  11088. }
  11089. /// Returns the number of cards that have been compounded onto the specified equipped item.
  11090. /// getequipcardcnt(<equipment slot>);
  11091. BUILDIN_FUNC(getequipcardcnt)
  11092. {
  11093. int i=-1,j,num;
  11094. TBL_PC *sd;
  11095. int count;
  11096. num=script_getnum(st,2);
  11097. sd=script_rid2sd(st);
  11098. if (equip_index_check(num))
  11099. i=pc_checkequip(sd,equip_bitmask[num]);
  11100. if (i < 0 || !sd->inventory_data[i]) {
  11101. script_pushint(st,0);
  11102. return SCRIPT_CMD_SUCCESS;
  11103. }
  11104. if(itemdb_isspecial(sd->inventory.u.items_inventory[i].card[0]))
  11105. {
  11106. script_pushint(st,0);
  11107. return SCRIPT_CMD_SUCCESS;
  11108. }
  11109. count = 0;
  11110. for( j = 0; j < sd->inventory_data[i]->slot; j++ )
  11111. if( sd->inventory.u.items_inventory[i].card[j] && itemdb_type(sd->inventory.u.items_inventory[i].card[j]) == IT_CARD )
  11112. count++;
  11113. script_pushint(st,count);
  11114. return SCRIPT_CMD_SUCCESS;
  11115. }
  11116. /// Removes all cards from the item found in the specified equipment slot of the invoking character,
  11117. /// and give them to the character. If any cards were removed in this manner, it will also show a success effect.
  11118. /// successremovecards <slot>;
  11119. BUILDIN_FUNC(successremovecards) {
  11120. int i=-1,c,cardflag=0;
  11121. TBL_PC* sd = script_rid2sd(st);
  11122. int num = script_getnum(st,2);
  11123. if (equip_index_check(num))
  11124. i=pc_checkequip(sd,equip_bitmask[num]);
  11125. if (i < 0 || !sd->inventory_data[i]) {
  11126. return SCRIPT_CMD_SUCCESS;
  11127. }
  11128. if(itemdb_isspecial(sd->inventory.u.items_inventory[i].card[0]))
  11129. return SCRIPT_CMD_SUCCESS;
  11130. for( c = sd->inventory_data[i]->slot - 1; c >= 0; --c ) {
  11131. 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
  11132. unsigned char flag = 0;
  11133. struct item item_tmp;
  11134. memset(&item_tmp,0,sizeof(item_tmp));
  11135. cardflag = 1;
  11136. item_tmp.nameid = sd->inventory.u.items_inventory[i].card[c];
  11137. item_tmp.identify = 1;
  11138. if((flag=pc_additem(sd,&item_tmp,1,LOG_TYPE_SCRIPT))){ // get back the cart in inventory
  11139. clif_additem(sd,0,0,flag);
  11140. map_addflooritem(&item_tmp,1,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0,0);
  11141. }
  11142. }
  11143. }
  11144. if(cardflag == 1) {//if card was remove remplace item with no card
  11145. unsigned char flag = 0, j;
  11146. struct item item_tmp;
  11147. memset(&item_tmp,0,sizeof(item_tmp));
  11148. item_tmp.nameid = sd->inventory.u.items_inventory[i].nameid;
  11149. item_tmp.identify = 1;
  11150. item_tmp.refine = sd->inventory.u.items_inventory[i].refine;
  11151. item_tmp.attribute = sd->inventory.u.items_inventory[i].attribute;
  11152. item_tmp.expire_time = sd->inventory.u.items_inventory[i].expire_time;
  11153. item_tmp.bound = sd->inventory.u.items_inventory[i].bound;
  11154. for (j = sd->inventory_data[i]->slot; j < MAX_SLOTS; j++)
  11155. item_tmp.card[j]=sd->inventory.u.items_inventory[i].card[j];
  11156. for (j = 0; j < MAX_ITEM_RDM_OPT; j++){
  11157. item_tmp.option[j].id=sd->inventory.u.items_inventory[i].option[j].id;
  11158. item_tmp.option[j].value=sd->inventory.u.items_inventory[i].option[j].value;
  11159. item_tmp.option[j].param=sd->inventory.u.items_inventory[i].option[j].param;
  11160. }
  11161. pc_delitem(sd,i,1,0,3,LOG_TYPE_SCRIPT);
  11162. if((flag=pc_additem(sd,&item_tmp,1,LOG_TYPE_SCRIPT))){ //chk if can be spawn in inventory otherwise put on floor
  11163. clif_additem(sd,0,0,flag);
  11164. map_addflooritem(&item_tmp,1,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0,0);
  11165. }
  11166. clif_misceffect(&sd->bl,3);
  11167. }
  11168. return SCRIPT_CMD_SUCCESS;
  11169. }
  11170. /// Removes all cards from the item found in the specified equipment slot of the invoking character.
  11171. /// failedremovecards <slot>, <type>;
  11172. /// <type>=0 : will destroy both the item and the cards.
  11173. /// <type>=1 : will keep the item, but destroy the cards.
  11174. /// <type>=2 : will keep the cards, but destroy the item.
  11175. /// <type>=? : will just display the failure effect.
  11176. BUILDIN_FUNC(failedremovecards) {
  11177. int i=-1,c,cardflag=0;
  11178. TBL_PC* sd = script_rid2sd(st);
  11179. int num = script_getnum(st,2);
  11180. int typefail = script_getnum(st,3);
  11181. if (equip_index_check(num))
  11182. i=pc_checkequip(sd,equip_bitmask[num]);
  11183. if (i < 0 || !sd->inventory_data[i])
  11184. return SCRIPT_CMD_SUCCESS;
  11185. if(itemdb_isspecial(sd->inventory.u.items_inventory[i].card[0]))
  11186. return SCRIPT_CMD_SUCCESS;
  11187. for( c = sd->inventory_data[i]->slot - 1; c >= 0; --c ) {
  11188. if( sd->inventory.u.items_inventory[i].card[c] && itemdb_type(sd->inventory.u.items_inventory[i].card[c]) == IT_CARD ) {
  11189. cardflag = 1;
  11190. if(typefail == 2) {// add cards to inventory, clear
  11191. unsigned char flag = 0;
  11192. struct item item_tmp;
  11193. memset(&item_tmp,0,sizeof(item_tmp));
  11194. item_tmp.nameid = sd->inventory.u.items_inventory[i].card[c];
  11195. item_tmp.identify = 1;
  11196. if((flag=pc_additem(sd,&item_tmp,1,LOG_TYPE_SCRIPT))){
  11197. clif_additem(sd,0,0,flag);
  11198. map_addflooritem(&item_tmp,1,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0,0);
  11199. }
  11200. }
  11201. }
  11202. }
  11203. if(cardflag == 1) {
  11204. if(typefail == 0 || typefail == 2){ // destroy the item
  11205. pc_delitem(sd,i,1,0,2,LOG_TYPE_SCRIPT);
  11206. }else if(typefail == 1){ // destroy the card
  11207. unsigned char flag = 0, j;
  11208. struct item item_tmp;
  11209. memset(&item_tmp,0,sizeof(item_tmp));
  11210. item_tmp.nameid = sd->inventory.u.items_inventory[i].nameid;
  11211. item_tmp.identify = 1;
  11212. item_tmp.refine = sd->inventory.u.items_inventory[i].refine;
  11213. item_tmp.attribute = sd->inventory.u.items_inventory[i].attribute;
  11214. item_tmp.expire_time = sd->inventory.u.items_inventory[i].expire_time;
  11215. item_tmp.bound = sd->inventory.u.items_inventory[i].bound;
  11216. for (j = sd->inventory_data[i]->slot; j < MAX_SLOTS; j++)
  11217. item_tmp.card[j]=sd->inventory.u.items_inventory[i].card[j];
  11218. for (j = 0; j < MAX_ITEM_RDM_OPT; j++){
  11219. item_tmp.option[j].id=sd->inventory.u.items_inventory[i].option[j].id;
  11220. item_tmp.option[j].value=sd->inventory.u.items_inventory[i].option[j].value;
  11221. item_tmp.option[j].param=sd->inventory.u.items_inventory[i].option[j].param;
  11222. }
  11223. pc_delitem(sd,i,1,0,2,LOG_TYPE_SCRIPT);
  11224. if((flag=pc_additem(sd,&item_tmp,1,LOG_TYPE_SCRIPT))){
  11225. clif_additem(sd,0,0,flag);
  11226. map_addflooritem(&item_tmp,1,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0,0);
  11227. }
  11228. }
  11229. clif_misceffect(&sd->bl,2);
  11230. }
  11231. return SCRIPT_CMD_SUCCESS;
  11232. }
  11233. /* ================================================================
  11234. * mapwarp "<from map>","<to map>",<x>,<y>,<type>,<ID for Type>;
  11235. * type: 0=everyone, 1=guild, 2=party; [Reddozen]
  11236. * improved by [Lance]
  11237. * ================================================================*/
  11238. BUILDIN_FUNC(mapwarp) // Added by RoVeRT
  11239. {
  11240. int x,y,m,check_val=0,check_ID=0,i=0;
  11241. struct guild *g = NULL;
  11242. struct party_data *p = NULL;
  11243. const char *str;
  11244. const char *mapname;
  11245. unsigned int index;
  11246. mapname=script_getstr(st,2);
  11247. str=script_getstr(st,3);
  11248. x=script_getnum(st,4);
  11249. y=script_getnum(st,5);
  11250. if(script_hasdata(st,7)){
  11251. check_val=script_getnum(st,6);
  11252. check_ID=script_getnum(st,7);
  11253. }
  11254. if((m=map_mapname2mapid(mapname))< 0)
  11255. return SCRIPT_CMD_SUCCESS;
  11256. if(!(index=mapindex_name2id(str)))
  11257. return SCRIPT_CMD_SUCCESS;
  11258. switch(check_val){
  11259. case 1:
  11260. g = guild_search(check_ID);
  11261. if (g){
  11262. for( i=0; i < g->max_member; i++)
  11263. {
  11264. if(g->member[i].sd && g->member[i].sd->bl.m==m){
  11265. pc_setpos(g->member[i].sd,index,x,y,CLR_TELEPORT);
  11266. }
  11267. }
  11268. }
  11269. break;
  11270. case 2:
  11271. p = party_search(check_ID);
  11272. if(p){
  11273. for(i=0;i<MAX_PARTY; i++){
  11274. if(p->data[i].sd && p->data[i].sd->bl.m == m){
  11275. pc_setpos(p->data[i].sd,index,x,y,CLR_TELEPORT);
  11276. }
  11277. }
  11278. }
  11279. break;
  11280. default:
  11281. map_foreachinmap(buildin_areawarp_sub,m,BL_PC,index,x,y,0,0);
  11282. break;
  11283. }
  11284. return SCRIPT_CMD_SUCCESS;
  11285. }
  11286. static int buildin_mobcount_sub(struct block_list *bl,va_list ap) // Added by RoVeRT
  11287. {
  11288. char *event=va_arg(ap,char *);
  11289. struct mob_data *md = ((struct mob_data *)bl);
  11290. if( md->status.hp > 0 && (!event || strcmp(event,md->npc_event) == 0) )
  11291. return 1;
  11292. return SCRIPT_CMD_SUCCESS;
  11293. }
  11294. BUILDIN_FUNC(mobcount) // Added by RoVeRT
  11295. {
  11296. const char *mapname,*event;
  11297. int16 m;
  11298. mapname=script_getstr(st,2);
  11299. event=script_getstr(st,3);
  11300. if( strcmp(event, "all") == 0 )
  11301. event = NULL;
  11302. else
  11303. check_event(st, event);
  11304. if( strcmp(mapname, "this") == 0 ) {
  11305. struct map_session_data *sd = script_rid2sd(st);
  11306. if( sd )
  11307. m = sd->bl.m;
  11308. else {
  11309. script_pushint(st,-1);
  11310. return SCRIPT_CMD_SUCCESS;
  11311. }
  11312. }
  11313. else if( (m = map_mapname2mapid(mapname)) < 0 ) {
  11314. script_pushint(st,-1);
  11315. return SCRIPT_CMD_SUCCESS;
  11316. }
  11317. script_pushint(st,map_foreachinmap(buildin_mobcount_sub, m, BL_MOB, event));
  11318. return SCRIPT_CMD_SUCCESS;
  11319. }
  11320. BUILDIN_FUNC(marriage)
  11321. {
  11322. const char *partner=script_getstr(st,2);
  11323. TBL_PC *sd=script_rid2sd(st);
  11324. TBL_PC *p_sd=map_nick2sd(partner,false);
  11325. if(!sd || !p_sd || !pc_marriage(sd,p_sd)){
  11326. script_pushint(st,0);
  11327. return SCRIPT_CMD_SUCCESS;
  11328. }
  11329. script_pushint(st,1);
  11330. return SCRIPT_CMD_SUCCESS;
  11331. }
  11332. BUILDIN_FUNC(wedding_effect)
  11333. {
  11334. TBL_PC *sd=script_rid2sd(st);
  11335. struct block_list *bl;
  11336. if(sd==NULL) {
  11337. bl=map_id2bl(st->oid);
  11338. } else
  11339. bl=&sd->bl;
  11340. clif_wedding_effect(bl);
  11341. return SCRIPT_CMD_SUCCESS;
  11342. }
  11343. /**
  11344. * divorce({<char_id>})
  11345. **/
  11346. BUILDIN_FUNC(divorce)
  11347. {
  11348. TBL_PC *sd;
  11349. if (!script_charid2sd(2,sd) || !pc_divorce(sd)) {
  11350. script_pushint(st,0);
  11351. return SCRIPT_CMD_FAILURE;
  11352. }
  11353. script_pushint(st,1);
  11354. return SCRIPT_CMD_SUCCESS;
  11355. }
  11356. /**
  11357. * ispartneron({<char_id>})
  11358. **/
  11359. BUILDIN_FUNC(ispartneron)
  11360. {
  11361. TBL_PC *sd;
  11362. if (!script_charid2sd(2,sd) || !pc_ismarried(sd) ||
  11363. map_charid2sd(sd->status.partner_id) == NULL)
  11364. {
  11365. script_pushint(st,0);
  11366. return SCRIPT_CMD_FAILURE;
  11367. }
  11368. script_pushint(st,1);
  11369. return SCRIPT_CMD_SUCCESS;
  11370. }
  11371. /**
  11372. * getpartnerid({<char_id>})
  11373. **/
  11374. BUILDIN_FUNC(getpartnerid)
  11375. {
  11376. TBL_PC *sd;
  11377. if (!script_charid2sd(2,sd)) {
  11378. script_pushint(st,0);
  11379. return SCRIPT_CMD_FAILURE;
  11380. }
  11381. script_pushint(st,sd->status.partner_id);
  11382. return SCRIPT_CMD_SUCCESS;
  11383. }
  11384. /**
  11385. * getchildid({<char_id>})
  11386. **/
  11387. BUILDIN_FUNC(getchildid)
  11388. {
  11389. TBL_PC *sd;
  11390. if (!script_charid2sd(2,sd)) {
  11391. script_pushint(st,0);
  11392. return SCRIPT_CMD_FAILURE;
  11393. }
  11394. script_pushint(st,sd->status.child);
  11395. return SCRIPT_CMD_SUCCESS;
  11396. }
  11397. /**
  11398. * getmotherid({<char_id>})
  11399. **/
  11400. BUILDIN_FUNC(getmotherid)
  11401. {
  11402. TBL_PC *sd;
  11403. if (!script_charid2sd(2,sd)) {
  11404. script_pushint(st,0);
  11405. return SCRIPT_CMD_FAILURE;
  11406. }
  11407. script_pushint(st,sd->status.mother);
  11408. return SCRIPT_CMD_SUCCESS;
  11409. }
  11410. /**
  11411. * getfatherid({<char_id>})
  11412. **/
  11413. BUILDIN_FUNC(getfatherid)
  11414. {
  11415. TBL_PC *sd;
  11416. if (!script_charid2sd(2,sd)) {
  11417. script_pushint(st,0);
  11418. return SCRIPT_CMD_FAILURE;
  11419. }
  11420. script_pushint(st,sd->status.father);
  11421. return SCRIPT_CMD_SUCCESS;
  11422. }
  11423. BUILDIN_FUNC(warppartner)
  11424. {
  11425. int x,y;
  11426. unsigned short mapindex;
  11427. const char *str;
  11428. TBL_PC *sd=script_rid2sd(st);
  11429. TBL_PC *p_sd=NULL;
  11430. if(sd==NULL || !pc_ismarried(sd) ||
  11431. (p_sd=map_charid2sd(sd->status.partner_id)) == NULL) {
  11432. script_pushint(st,0);
  11433. return SCRIPT_CMD_SUCCESS;
  11434. }
  11435. str=script_getstr(st,2);
  11436. x=script_getnum(st,3);
  11437. y=script_getnum(st,4);
  11438. mapindex = mapindex_name2id(str);
  11439. if (mapindex) {
  11440. pc_setpos(p_sd,mapindex,x,y,CLR_OUTSIGHT);
  11441. script_pushint(st,1);
  11442. } else
  11443. script_pushint(st,0);
  11444. return SCRIPT_CMD_SUCCESS;
  11445. }
  11446. /*================================================
  11447. * Script for Displaying MOB Information [Valaris]
  11448. *------------------------------------------------*/
  11449. BUILDIN_FUNC(strmobinfo)
  11450. {
  11451. int num=script_getnum(st,2);
  11452. int class_=script_getnum(st,3);
  11453. if(!mobdb_checkid(class_))
  11454. {
  11455. if (num < 3) //requested a string
  11456. script_pushconststr(st,"");
  11457. else
  11458. script_pushint(st,0);
  11459. return SCRIPT_CMD_SUCCESS;
  11460. }
  11461. switch (num) {
  11462. case 1: script_pushstrcopy(st,mob_db(class_)->name); break;
  11463. case 2: script_pushstrcopy(st,mob_db(class_)->jname); break;
  11464. case 3: script_pushint(st,mob_db(class_)->lv); break;
  11465. case 4: script_pushint(st,mob_db(class_)->status.max_hp); break;
  11466. case 5: script_pushint(st,mob_db(class_)->status.max_sp); break;
  11467. case 6: script_pushint(st,mob_db(class_)->base_exp); break;
  11468. case 7: script_pushint(st,mob_db(class_)->job_exp); break;
  11469. default:
  11470. script_pushint(st,0);
  11471. break;
  11472. }
  11473. return SCRIPT_CMD_SUCCESS;
  11474. }
  11475. /*==========================================
  11476. * Summon guardians [Valaris]
  11477. * guardian("<map name>",<x>,<y>,"<name to show>",<mob id>{,"<event label>"}{,<guardian index>}) -> <id>
  11478. *------------------------------------------*/
  11479. BUILDIN_FUNC(guardian)
  11480. {
  11481. int class_=0,x=0,y=0,guardian=0;
  11482. const char *str,*mapname,*evt="";
  11483. bool has_index = false;
  11484. mapname =script_getstr(st,2);
  11485. x =script_getnum(st,3);
  11486. y =script_getnum(st,4);
  11487. str =script_getstr(st,5);
  11488. class_=script_getnum(st,6);
  11489. if( script_hasdata(st,8) )
  11490. {// "<event label>",<guardian index>
  11491. evt=script_getstr(st,7);
  11492. guardian=script_getnum(st,8);
  11493. has_index = true;
  11494. } else if( script_hasdata(st,7) ){
  11495. struct script_data *data = script_getdata(st,7);
  11496. get_val(st,data);
  11497. if( data_isstring(data) )
  11498. {// "<event label>"
  11499. evt=script_getstr(st,7);
  11500. } else if( data_isint(data) )
  11501. {// <guardian index>
  11502. guardian=script_getnum(st,7);
  11503. has_index = true;
  11504. } else {
  11505. ShowError("script:guardian: invalid data type for argument #6 (from 1)\n");
  11506. script_reportdata(data);
  11507. return SCRIPT_CMD_FAILURE;
  11508. }
  11509. }
  11510. check_event(st, evt);
  11511. script_pushint(st, mob_spawn_guardian(mapname,x,y,str,class_,evt,guardian,has_index));
  11512. return SCRIPT_CMD_SUCCESS;
  11513. }
  11514. /*==========================================
  11515. * Invisible Walls [Zephyrus]
  11516. *------------------------------------------*/
  11517. BUILDIN_FUNC(setwall)
  11518. {
  11519. const char *mapname, *name;
  11520. int x, y, m, size, dir;
  11521. bool shootable;
  11522. mapname = script_getstr(st,2);
  11523. x = script_getnum(st,3);
  11524. y = script_getnum(st,4);
  11525. size = script_getnum(st,5);
  11526. dir = script_getnum(st,6);
  11527. shootable = script_getnum(st,7);
  11528. name = script_getstr(st,8);
  11529. if( (m = map_mapname2mapid(mapname)) < 0 )
  11530. return SCRIPT_CMD_SUCCESS; // Invalid Map
  11531. map_iwall_set(m, x, y, size, dir, shootable, name);
  11532. return SCRIPT_CMD_SUCCESS;
  11533. }
  11534. BUILDIN_FUNC(delwall)
  11535. {
  11536. const char *name = script_getstr(st,2);
  11537. map_iwall_remove(name);
  11538. return SCRIPT_CMD_SUCCESS;
  11539. }
  11540. /// Retrieves various information about the specified guardian.
  11541. ///
  11542. /// guardianinfo("<map_name>", <index>, <type>) -> <value>
  11543. /// type: 0 - whether it is deployed or not
  11544. /// 1 - maximum hp
  11545. /// 2 - current hp
  11546. ///
  11547. BUILDIN_FUNC(guardianinfo)
  11548. {
  11549. const char* mapname = mapindex_getmapname(script_getstr(st,2),NULL);
  11550. int id = script_getnum(st,3);
  11551. int type = script_getnum(st,4);
  11552. struct guild_castle* gc = guild_mapname2gc(mapname);
  11553. struct mob_data* gd;
  11554. if( gc == NULL || id < 0 || id >= MAX_GUARDIANS )
  11555. {
  11556. script_pushint(st,-1);
  11557. return SCRIPT_CMD_SUCCESS;
  11558. }
  11559. if( type == 0 )
  11560. script_pushint(st, gc->guardian[id].visible);
  11561. else
  11562. if( !gc->guardian[id].visible )
  11563. script_pushint(st,-1);
  11564. else
  11565. if( (gd = map_id2md(gc->guardian[id].id)) == NULL )
  11566. script_pushint(st,-1);
  11567. else
  11568. {
  11569. if ( type == 1 ) script_pushint(st,gd->status.max_hp);
  11570. else if( type == 2 ) script_pushint(st,gd->status.hp);
  11571. else
  11572. script_pushint(st,-1);
  11573. }
  11574. return SCRIPT_CMD_SUCCESS;
  11575. }
  11576. /*==========================================
  11577. * Get the item name by item_id or null
  11578. *------------------------------------------*/
  11579. BUILDIN_FUNC(getitemname)
  11580. {
  11581. unsigned short item_id = 0;
  11582. struct item_data *i_data;
  11583. char *item_name;
  11584. struct script_data *data;
  11585. data=script_getdata(st,2);
  11586. get_val(st,data);
  11587. if( data_isstring(data) ){
  11588. const char *name=conv_str(st,data);
  11589. struct item_data *item_data = itemdb_searchname(name);
  11590. if( item_data )
  11591. item_id=item_data->nameid;
  11592. }else
  11593. item_id=conv_num(st,data);
  11594. i_data = itemdb_exists(item_id);
  11595. if (i_data == NULL)
  11596. {
  11597. script_pushconststr(st,"null");
  11598. return SCRIPT_CMD_SUCCESS;
  11599. }
  11600. item_name=(char *)aMalloc(ITEM_NAME_LENGTH*sizeof(char));
  11601. memcpy(item_name, i_data->jname, ITEM_NAME_LENGTH);
  11602. script_pushstr(st,item_name);
  11603. return SCRIPT_CMD_SUCCESS;
  11604. }
  11605. /*==========================================
  11606. * Returns number of slots an item has. [Skotlex]
  11607. *------------------------------------------*/
  11608. BUILDIN_FUNC(getitemslots)
  11609. {
  11610. unsigned short item_id;
  11611. struct item_data *i_data;
  11612. item_id=script_getnum(st,2);
  11613. i_data = itemdb_exists(item_id);
  11614. if (i_data)
  11615. script_pushint(st,i_data->slot);
  11616. else
  11617. script_pushint(st,-1);
  11618. return SCRIPT_CMD_SUCCESS;
  11619. }
  11620. // TODO: add matk here if needed/once we get rid of RENEWAL
  11621. /*==========================================
  11622. * Returns some values of an item [Lupus]
  11623. * Price, Weight, etc...
  11624. getiteminfo(itemID,n), where n
  11625. 0 value_buy;
  11626. 1 value_sell;
  11627. 2 type;
  11628. 3 maxchance = Max drop chance of this item e.g. 1 = 0.01% , etc..
  11629. if = 0, then monsters don't drop it at all (rare or a quest item)
  11630. if = -1, then this item is sold in NPC shops only
  11631. 4 sex;
  11632. 5 equip;
  11633. 6 weight;
  11634. 7 atk;
  11635. 8 def;
  11636. 9 range;
  11637. 10 slot;
  11638. 11 look;
  11639. 12 elv;
  11640. 13 wlv;
  11641. 14 view id
  11642. 15 eLvmax
  11643. 16 matk (renewal)
  11644. *------------------------------------------*/
  11645. BUILDIN_FUNC(getiteminfo)
  11646. {
  11647. unsigned short item_id,n;
  11648. struct item_data *i_data;
  11649. item_id = script_getnum(st,2);
  11650. n = script_getnum(st,3);
  11651. i_data = itemdb_exists(item_id);
  11652. if (i_data && n <= 16) {
  11653. int *item_arr = (int*)&i_data->value_buy;
  11654. #ifndef RENEWAL
  11655. if (n == 16)
  11656. script_pushint(st,0);
  11657. else
  11658. #endif
  11659. script_pushint(st,item_arr[n]);
  11660. } else
  11661. script_pushint(st,-1);
  11662. return SCRIPT_CMD_SUCCESS;
  11663. }
  11664. /*==========================================
  11665. * Set some values of an item [Lupus]
  11666. * Price, Weight, etc...
  11667. setiteminfo(itemID,n,Value), where n
  11668. 0 value_buy;
  11669. 1 value_sell;
  11670. 2 type;
  11671. 3 maxchance = Max drop chance of this item e.g. 1 = 0.01% , etc..
  11672. if = 0, then monsters don't drop it at all (rare or a quest item)
  11673. if = -1, then this item is sold in NPC shops only
  11674. 4 sex;
  11675. 5 equip;
  11676. 6 weight;
  11677. 7 atk;
  11678. 8 def;
  11679. 9 range;
  11680. 10 slot;
  11681. 11 look;
  11682. 12 elv;
  11683. 13 wlv;
  11684. 14 view id
  11685. * Returns Value or -1 if the wrong field's been set
  11686. *------------------------------------------*/
  11687. BUILDIN_FUNC(setiteminfo)
  11688. {
  11689. unsigned short item_id;
  11690. int n,value;
  11691. struct item_data *i_data;
  11692. item_id = script_getnum(st,2);
  11693. n = script_getnum(st,3);
  11694. value = script_getnum(st,4);
  11695. i_data = itemdb_exists(item_id);
  11696. if (i_data && n>=0 && n<=14) {
  11697. int *item_arr = (int*)&i_data->value_buy;
  11698. item_arr[n] = value;
  11699. script_pushint(st,value);
  11700. } else
  11701. script_pushint(st,-1);
  11702. return SCRIPT_CMD_SUCCESS;
  11703. }
  11704. /*==========================================
  11705. * Returns value from equipped item slot n [Lupus]
  11706. getequipcardid(num,slot)
  11707. where
  11708. num = eqip position slot
  11709. slot = 0,1,2,3 (Card Slot N)
  11710. This func returns CARD ID, 255,254,-255 (for card 0, if the item is produced)
  11711. it's useful when you want to check item cards or if it's signed
  11712. Useful for such quests as "Sign this refined item with players name" etc
  11713. Hat[0] +4 -> Player's Hat[0] +4
  11714. *------------------------------------------*/
  11715. BUILDIN_FUNC(getequipcardid)
  11716. {
  11717. int i=-1,num,slot;
  11718. TBL_PC *sd;
  11719. num=script_getnum(st,2);
  11720. slot=script_getnum(st,3);
  11721. sd=script_rid2sd(st);
  11722. if (equip_index_check(num))
  11723. i=pc_checkequip(sd,equip_bitmask[num]);
  11724. if(i >= 0 && slot>=0 && slot<4)
  11725. script_pushint(st,sd->inventory.u.items_inventory[i].card[slot]);
  11726. else
  11727. script_pushint(st,0);
  11728. return SCRIPT_CMD_SUCCESS;
  11729. }
  11730. /*==========================================
  11731. * petskillbonus [Valaris] //Rewritten by [Skotlex]
  11732. *------------------------------------------*/
  11733. BUILDIN_FUNC(petskillbonus)
  11734. {
  11735. struct pet_data *pd;
  11736. TBL_PC *sd = script_rid2sd(st);
  11737. if(sd == NULL || sd->pd == NULL)
  11738. return SCRIPT_CMD_FAILURE;
  11739. pd = sd->pd;
  11740. if (pd->bonus)
  11741. { //Clear previous bonus
  11742. if (pd->bonus->timer != INVALID_TIMER)
  11743. delete_timer(pd->bonus->timer, pet_skill_bonus_timer);
  11744. } else //init
  11745. pd->bonus = (struct pet_bonus *) aMalloc(sizeof(struct pet_bonus));
  11746. pd->bonus->type = script_getnum(st,2);
  11747. pd->bonus->val = script_getnum(st,3);
  11748. pd->bonus->duration = script_getnum(st,4);
  11749. pd->bonus->delay = script_getnum(st,5);
  11750. if (pd->state.skillbonus == 1)
  11751. pd->state.skillbonus = 0; // waiting state
  11752. // wait for timer to start
  11753. if (battle_config.pet_equip_required && pd->pet.equip == 0)
  11754. pd->bonus->timer = INVALID_TIMER;
  11755. else
  11756. pd->bonus->timer = add_timer(gettick()+pd->bonus->delay*1000, pet_skill_bonus_timer, sd->bl.id, 0);
  11757. return SCRIPT_CMD_SUCCESS;
  11758. }
  11759. /*==========================================
  11760. * pet looting [Valaris] //Rewritten by [Skotlex]
  11761. *------------------------------------------*/
  11762. BUILDIN_FUNC(petloot)
  11763. {
  11764. int max;
  11765. struct pet_data *pd;
  11766. TBL_PC *sd=script_rid2sd(st);
  11767. if(sd==NULL || sd->pd==NULL)
  11768. return SCRIPT_CMD_SUCCESS;
  11769. max=script_getnum(st,2);
  11770. if(max < 1)
  11771. max = 1; //Let'em loot at least 1 item.
  11772. else if (max > MAX_PETLOOT_SIZE)
  11773. max = MAX_PETLOOT_SIZE;
  11774. pd = sd->pd;
  11775. if (pd->loot != NULL)
  11776. { //Release whatever was there already and reallocate memory
  11777. pet_lootitem_drop(pd, pd->master);
  11778. aFree(pd->loot->item);
  11779. }
  11780. else
  11781. pd->loot = (struct pet_loot *)aMalloc(sizeof(struct pet_loot));
  11782. pd->loot->item = (struct item *)aCalloc(max,sizeof(struct item));
  11783. pd->loot->max=max;
  11784. pd->loot->count = 0;
  11785. pd->loot->weight = 0;
  11786. return SCRIPT_CMD_SUCCESS;
  11787. }
  11788. /*==========================================
  11789. * Set arrays with info of all sd inventory :
  11790. * @inventorylist_id, @inventorylist_amount, @inventorylist_equip,
  11791. * @inventorylist_refine, @inventorylist_identify, @inventorylist_attribute,
  11792. * @inventorylist_card(0..3), @inventorylist_expire
  11793. * @inventorylist_count = scalar
  11794. *------------------------------------------*/
  11795. BUILDIN_FUNC(getinventorylist)
  11796. {
  11797. TBL_PC *sd;
  11798. char card_var[NAME_LENGTH];
  11799. int i,j=0,k;
  11800. if (!script_charid2sd(2,sd))
  11801. return SCRIPT_CMD_FAILURE;
  11802. for(i=0;i<MAX_INVENTORY;i++){
  11803. if(sd->inventory.u.items_inventory[i].nameid > 0 && sd->inventory.u.items_inventory[i].amount > 0){
  11804. pc_setreg(sd,reference_uid(add_str("@inventorylist_id"), j),sd->inventory.u.items_inventory[i].nameid);
  11805. pc_setreg(sd,reference_uid(add_str("@inventorylist_amount"), j),sd->inventory.u.items_inventory[i].amount);
  11806. pc_setreg(sd,reference_uid(add_str("@inventorylist_equip"), j),sd->inventory.u.items_inventory[i].equip);
  11807. pc_setreg(sd,reference_uid(add_str("@inventorylist_refine"), j),sd->inventory.u.items_inventory[i].refine);
  11808. pc_setreg(sd,reference_uid(add_str("@inventorylist_identify"), j),sd->inventory.u.items_inventory[i].identify);
  11809. pc_setreg(sd,reference_uid(add_str("@inventorylist_attribute"), j),sd->inventory.u.items_inventory[i].attribute);
  11810. for (k = 0; k < MAX_SLOTS; k++)
  11811. {
  11812. sprintf(card_var, "@inventorylist_card%d",k+1);
  11813. pc_setreg(sd,reference_uid(add_str(card_var), j),sd->inventory.u.items_inventory[i].card[k]);
  11814. }
  11815. pc_setreg(sd,reference_uid(add_str("@inventorylist_expire"), j),sd->inventory.u.items_inventory[i].expire_time);
  11816. pc_setreg(sd,reference_uid(add_str("@inventorylist_bound"), j),sd->inventory.u.items_inventory[i].bound);
  11817. j++;
  11818. }
  11819. }
  11820. pc_setreg(sd,add_str("@inventorylist_count"),j);
  11821. return SCRIPT_CMD_SUCCESS;
  11822. }
  11823. /**
  11824. * getskilllist ({<char_id>});
  11825. **/
  11826. BUILDIN_FUNC(getskilllist)
  11827. {
  11828. TBL_PC *sd;
  11829. int i,j=0;
  11830. if (!script_charid2sd(2,sd))
  11831. return SCRIPT_CMD_FAILURE;
  11832. for(i=0;i<MAX_SKILL;i++){
  11833. if(sd->status.skill[i].id > 0 && sd->status.skill[i].lv > 0){
  11834. pc_setreg(sd,reference_uid(add_str("@skilllist_id"), j),sd->status.skill[i].id);
  11835. pc_setreg(sd,reference_uid(add_str("@skilllist_lv"), j),sd->status.skill[i].lv);
  11836. pc_setreg(sd,reference_uid(add_str("@skilllist_flag"), j),sd->status.skill[i].flag);
  11837. j++;
  11838. }
  11839. }
  11840. pc_setreg(sd,add_str("@skilllist_count"),j);
  11841. return SCRIPT_CMD_SUCCESS;
  11842. }
  11843. /**
  11844. * clearitem ({<char_id>});
  11845. **/
  11846. BUILDIN_FUNC(clearitem)
  11847. {
  11848. TBL_PC *sd;
  11849. int i;
  11850. if (!script_charid2sd(2,sd))
  11851. return SCRIPT_CMD_FAILURE;
  11852. for (i=0; i<MAX_INVENTORY; i++) {
  11853. if (sd->inventory.u.items_inventory[i].amount) {
  11854. pc_delitem(sd, i, sd->inventory.u.items_inventory[i].amount, 0, 0, LOG_TYPE_SCRIPT);
  11855. }
  11856. }
  11857. return SCRIPT_CMD_SUCCESS;
  11858. }
  11859. /**
  11860. * Disguise Player (returns Mob/NPC ID if success, 0 on fail)
  11861. * disguise <Monster ID>{,<char_id>};
  11862. **/
  11863. BUILDIN_FUNC(disguise)
  11864. {
  11865. int id;
  11866. TBL_PC* sd;
  11867. if (!script_charid2sd(3,sd))
  11868. return SCRIPT_CMD_FAILURE;
  11869. id = script_getnum(st,2);
  11870. if (mobdb_checkid(id) || npcdb_checkid(id)) {
  11871. pc_disguise(sd, id);
  11872. script_pushint(st,id);
  11873. } else
  11874. script_pushint(st,0);
  11875. return SCRIPT_CMD_SUCCESS;
  11876. }
  11877. /**
  11878. * Undisguise Player (returns 1 if success, 0 on fail)
  11879. * undisguise {<char_id>};
  11880. **/
  11881. BUILDIN_FUNC(undisguise)
  11882. {
  11883. TBL_PC* sd;
  11884. if (!script_charid2sd(2,sd))
  11885. return SCRIPT_CMD_FAILURE;
  11886. if (sd->disguise) {
  11887. pc_disguise(sd, 0);
  11888. script_pushint(st,0);
  11889. } else {
  11890. script_pushint(st,1);
  11891. }
  11892. return SCRIPT_CMD_SUCCESS;
  11893. }
  11894. /*==========================================
  11895. * Transform a bl to another _class,
  11896. * @type unused
  11897. *------------------------------------------*/
  11898. BUILDIN_FUNC(classchange)
  11899. {
  11900. int _class,type;
  11901. struct block_list *bl=map_id2bl(st->oid);
  11902. if (bl==NULL)
  11903. return SCRIPT_CMD_FAILURE;
  11904. _class=script_getnum(st,2);
  11905. type=script_getnum(st,3);
  11906. clif_class_change(bl,_class,type);
  11907. return SCRIPT_CMD_SUCCESS;
  11908. }
  11909. /*==========================================
  11910. * Display an effect
  11911. *------------------------------------------*/
  11912. BUILDIN_FUNC(misceffect)
  11913. {
  11914. int type;
  11915. type=script_getnum(st,2);
  11916. if(st->oid && st->oid != fake_nd->bl.id) {
  11917. struct block_list *bl = map_id2bl(st->oid);
  11918. if (bl)
  11919. clif_specialeffect(bl,type,AREA);
  11920. } else{
  11921. TBL_PC *sd=script_rid2sd(st);
  11922. if(sd)
  11923. clif_specialeffect(&sd->bl,type,AREA);
  11924. }
  11925. return SCRIPT_CMD_SUCCESS;
  11926. }
  11927. /*==========================================
  11928. * Play a BGM on a single client [Rikter/Yommy]
  11929. *------------------------------------------*/
  11930. BUILDIN_FUNC(playBGM)
  11931. {
  11932. struct map_session_data* sd;
  11933. if( ( sd = script_rid2sd(st) ) != NULL ) {
  11934. clif_playBGM(sd, script_getstr(st,2));
  11935. }
  11936. return SCRIPT_CMD_SUCCESS;
  11937. }
  11938. static int playBGM_sub(struct block_list* bl,va_list ap)
  11939. {
  11940. const char* name = va_arg(ap,const char*);
  11941. clif_playBGM(BL_CAST(BL_PC, bl), name);
  11942. return 0;
  11943. }
  11944. static int playBGM_foreachpc_sub(struct map_session_data* sd, va_list args)
  11945. {
  11946. const char* name = va_arg(args, const char*);
  11947. clif_playBGM(sd, name);
  11948. return 0;
  11949. }
  11950. /*==========================================
  11951. * Play a BGM on multiple client [Rikter/Yommy]
  11952. *------------------------------------------*/
  11953. BUILDIN_FUNC(playBGMall)
  11954. {
  11955. const char* name;
  11956. name = script_getstr(st,2);
  11957. if( script_hasdata(st,7) ) {// specified part of map
  11958. const char* mapname = script_getstr(st,3);
  11959. int x0 = script_getnum(st,4);
  11960. int y0 = script_getnum(st,5);
  11961. int x1 = script_getnum(st,6);
  11962. int y1 = script_getnum(st,7);
  11963. map_foreachinarea(playBGM_sub, map_mapname2mapid(mapname), x0, y0, x1, y1, BL_PC, name);
  11964. }
  11965. else if( script_hasdata(st,3) ) {// entire map
  11966. const char* mapname = script_getstr(st,3);
  11967. map_foreachinmap(playBGM_sub, map_mapname2mapid(mapname), BL_PC, name);
  11968. }
  11969. else {// entire server
  11970. map_foreachpc(&playBGM_foreachpc_sub, name);
  11971. }
  11972. return SCRIPT_CMD_SUCCESS;
  11973. }
  11974. /*==========================================
  11975. * Play a .wav sound for sd
  11976. *------------------------------------------*/
  11977. BUILDIN_FUNC(soundeffect)
  11978. {
  11979. TBL_PC* sd = script_rid2sd(st);
  11980. const char* name = script_getstr(st,2);
  11981. int type = script_getnum(st,3);
  11982. if(sd) {
  11983. clif_soundeffect(sd,&sd->bl,name,type);
  11984. }
  11985. return SCRIPT_CMD_SUCCESS;
  11986. }
  11987. int soundeffect_sub(struct block_list* bl,va_list ap)
  11988. {
  11989. char* name = va_arg(ap,char*);
  11990. int type = va_arg(ap,int);
  11991. clif_soundeffect((TBL_PC *)bl, bl, name, type);
  11992. return 0;
  11993. }
  11994. /*==========================================
  11995. * Play a sound effect (.wav) on multiple clients
  11996. * soundeffectall "<filepath>",<type>{,"<map name>"}{,<x0>,<y0>,<x1>,<y1>};
  11997. *------------------------------------------*/
  11998. BUILDIN_FUNC(soundeffectall)
  11999. {
  12000. struct block_list* bl;
  12001. const char* name;
  12002. int type;
  12003. bl = (st->rid) ? &(script_rid2sd(st)->bl) : map_id2bl(st->oid);
  12004. if (!bl)
  12005. return SCRIPT_CMD_SUCCESS;
  12006. name = script_getstr(st,2);
  12007. type = script_getnum(st,3);
  12008. //FIXME: enumerating map squares (map_foreach) is slower than enumerating the list of online players (map_foreachpc?) [ultramage]
  12009. if(!script_hasdata(st,4))
  12010. { // area around
  12011. clif_soundeffectall(bl, name, type, AREA);
  12012. }
  12013. else
  12014. if(!script_hasdata(st,5))
  12015. { // entire map
  12016. const char* mapname = script_getstr(st,4);
  12017. map_foreachinmap(soundeffect_sub, map_mapname2mapid(mapname), BL_PC, name, type);
  12018. }
  12019. else
  12020. if(script_hasdata(st,8))
  12021. { // specified part of map
  12022. const char* mapname = script_getstr(st,4);
  12023. int x0 = script_getnum(st,5);
  12024. int y0 = script_getnum(st,6);
  12025. int x1 = script_getnum(st,7);
  12026. int y1 = script_getnum(st,8);
  12027. map_foreachinarea(soundeffect_sub, map_mapname2mapid(mapname), x0, y0, x1, y1, BL_PC, name, type);
  12028. }
  12029. else
  12030. {
  12031. ShowError("buildin_soundeffectall: insufficient arguments for specific area broadcast.\n");
  12032. }
  12033. return SCRIPT_CMD_SUCCESS;
  12034. }
  12035. /*==========================================
  12036. * pet status recovery [Valaris] / Rewritten by [Skotlex]
  12037. *------------------------------------------*/
  12038. BUILDIN_FUNC(petrecovery)
  12039. {
  12040. struct pet_data *pd;
  12041. TBL_PC *sd=script_rid2sd(st);
  12042. int sc;
  12043. if(sd == NULL || sd->pd == NULL)
  12044. return SCRIPT_CMD_FAILURE;
  12045. sc = script_getnum(st,2);
  12046. if (sc <= SC_NONE || sc >= SC_MAX) {
  12047. ShowError("buildin_petrecovery: Invalid SC type: %d\n", sc);
  12048. return SCRIPT_CMD_FAILURE;
  12049. }
  12050. pd = sd->pd;
  12051. if (pd->recovery)
  12052. { //Halt previous bonus
  12053. if (pd->recovery->timer != INVALID_TIMER)
  12054. delete_timer(pd->recovery->timer, pet_recovery_timer);
  12055. } else //Init
  12056. pd->recovery = (struct pet_recovery *)aMalloc(sizeof(struct pet_recovery));
  12057. pd->recovery->type = (sc_type)sc;
  12058. pd->recovery->delay = script_getnum(st,3);
  12059. pd->recovery->timer = INVALID_TIMER;
  12060. return SCRIPT_CMD_SUCCESS;
  12061. }
  12062. /*==========================================
  12063. * pet attack skills [Valaris] //Rewritten by [Skotlex]
  12064. *------------------------------------------*/
  12065. /// petskillattack <skill id>,<level>,<rate>,<bonusrate>
  12066. /// petskillattack "<skill name>",<level>,<rate>,<bonusrate>
  12067. BUILDIN_FUNC(petskillattack)
  12068. {
  12069. struct pet_data *pd;
  12070. struct script_data *data;
  12071. TBL_PC *sd = script_rid2sd(st);
  12072. int id = 0;
  12073. if(sd == NULL || sd->pd == NULL)
  12074. return SCRIPT_CMD_FAILURE;
  12075. data = script_getdata(st, 2);
  12076. get_val(st, data);
  12077. id = (data_isstring(data) ? skill_name2id(script_getstr(st,2)) : skill_get_index(script_getnum(st,2)));
  12078. if (!id) {
  12079. ShowError("buildin_petskillattack: Invalid skill defined!\n");
  12080. return SCRIPT_CMD_FAILURE;
  12081. }
  12082. pd = sd->pd;
  12083. if (pd->a_skill == NULL)
  12084. pd->a_skill = (struct pet_skill_attack *)aMalloc(sizeof(struct pet_skill_attack));
  12085. pd->a_skill->id = id;
  12086. pd->a_skill->damage = 0;
  12087. pd->a_skill->lv = (unsigned short)min(script_getnum(st,3), skill_get_max(pd->a_skill->id));
  12088. pd->a_skill->div_ = 0;
  12089. pd->a_skill->rate = script_getnum(st,4);
  12090. pd->a_skill->bonusrate = script_getnum(st,5);
  12091. return SCRIPT_CMD_SUCCESS;
  12092. }
  12093. /*==========================================
  12094. * pet attack skills [Valaris]
  12095. *------------------------------------------*/
  12096. /// petskillattack2 <skill id>,<damage>,<div>,<rate>,<bonusrate>
  12097. /// petskillattack2 "<skill name>",<damage>,<div>,<rate>,<bonusrate>
  12098. BUILDIN_FUNC(petskillattack2)
  12099. {
  12100. struct pet_data *pd;
  12101. struct script_data *data;
  12102. TBL_PC *sd = script_rid2sd(st);
  12103. int id = 0;
  12104. if(sd == NULL || sd->pd == NULL)
  12105. return SCRIPT_CMD_FAILURE;
  12106. data = script_getdata(st, 2);
  12107. get_val(st, data);
  12108. id = (data_isstring(data) ? skill_name2id(script_getstr(st,2)) : skill_get_index(script_getnum(st,2)));
  12109. if (!id) {
  12110. ShowError("buildin_petskillattack2: Invalid skill defined!\n");
  12111. return SCRIPT_CMD_FAILURE;
  12112. }
  12113. pd = sd->pd;
  12114. if (pd->a_skill == NULL)
  12115. pd->a_skill = (struct pet_skill_attack *)aMalloc(sizeof(struct pet_skill_attack));
  12116. pd->a_skill->id = id;
  12117. pd->a_skill->damage = script_getnum(st,3); // Fixed damage
  12118. pd->a_skill->lv = (unsigned short)skill_get_max(pd->a_skill->id); // Adjust to max skill level
  12119. pd->a_skill->div_ = script_getnum(st,4);
  12120. pd->a_skill->rate = script_getnum(st,5);
  12121. pd->a_skill->bonusrate = script_getnum(st,6);
  12122. return SCRIPT_CMD_SUCCESS;
  12123. }
  12124. /*==========================================
  12125. * pet support skills [Skotlex]
  12126. *------------------------------------------*/
  12127. /// petskillsupport <skill id>,<level>,<delay>,<hp>,<sp>
  12128. /// petskillsupport "<skill name>",<level>,<delay>,<hp>,<sp>
  12129. BUILDIN_FUNC(petskillsupport)
  12130. {
  12131. struct pet_data *pd;
  12132. struct script_data *data;
  12133. TBL_PC *sd = script_rid2sd(st);
  12134. int id = 0;
  12135. if(sd == NULL || sd->pd == NULL)
  12136. return SCRIPT_CMD_FAILURE;
  12137. data = script_getdata(st, 2);
  12138. get_val(st, data);
  12139. id = (data_isstring(data) ? skill_name2id(script_getstr(st,2)) : skill_get_index(script_getnum(st,2)));
  12140. if (!id) {
  12141. ShowError("buildin_petskillsupport: Invalid skill defined!\n");
  12142. return SCRIPT_CMD_FAILURE;
  12143. }
  12144. pd = sd->pd;
  12145. if (pd->s_skill)
  12146. { //Clear previous skill
  12147. if (pd->s_skill->timer != INVALID_TIMER)
  12148. {
  12149. if (pd->s_skill->id)
  12150. delete_timer(pd->s_skill->timer, pet_skill_support_timer);
  12151. else
  12152. delete_timer(pd->s_skill->timer, pet_heal_timer);
  12153. }
  12154. } else //init memory
  12155. pd->s_skill = (struct pet_skill_support *) aMalloc(sizeof(struct pet_skill_support));
  12156. pd->s_skill->id = id;
  12157. pd->s_skill->lv = script_getnum(st,3);
  12158. pd->s_skill->delay = script_getnum(st,4);
  12159. pd->s_skill->hp = script_getnum(st,5);
  12160. pd->s_skill->sp = script_getnum(st,6);
  12161. //Use delay as initial offset to avoid skill/heal exploits
  12162. if (battle_config.pet_equip_required && pd->pet.equip == 0)
  12163. pd->s_skill->timer = INVALID_TIMER;
  12164. else
  12165. pd->s_skill->timer = add_timer(gettick()+pd->s_skill->delay*1000,pet_skill_support_timer,sd->bl.id,0);
  12166. return SCRIPT_CMD_SUCCESS;
  12167. }
  12168. /*==========================================
  12169. * Scripted skill effects [Celest]
  12170. *------------------------------------------*/
  12171. /// skilleffect <skill id>,<level>
  12172. /// skilleffect "<skill name>",<level>
  12173. BUILDIN_FUNC(skilleffect)
  12174. {
  12175. TBL_PC *sd;
  12176. uint16 skill_id, skill_lv;
  12177. struct script_data *data = script_getdata(st, 2);
  12178. get_val(st, data); // Convert into value in case of a variable
  12179. skill_id = ( data_isstring(data) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  12180. skill_lv = script_getnum(st,3);
  12181. sd = script_rid2sd(st);
  12182. /* Ensure we're standing because the following packet causes the client to virtually set the char to stand,
  12183. * which leaves the server thinking it still is sitting. */
  12184. if( pc_issit(sd) && pc_setstand(sd, false) ) {
  12185. skill_sit(sd, 0);
  12186. clif_standing(&sd->bl);
  12187. }
  12188. clif_skill_nodamage(&sd->bl,&sd->bl,skill_id,skill_lv,1);
  12189. return SCRIPT_CMD_SUCCESS;
  12190. }
  12191. /*==========================================
  12192. * NPC skill effects [Valaris]
  12193. *------------------------------------------*/
  12194. /// npcskilleffect <skill id>,<level>,<x>,<y>
  12195. /// npcskilleffect "<skill name>",<level>,<x>,<y>
  12196. BUILDIN_FUNC(npcskilleffect)
  12197. {
  12198. struct block_list *bl= map_id2bl(st->oid);
  12199. uint16 skill_id, skill_lv;
  12200. int x, y;
  12201. struct script_data *data = script_getdata(st, 2);
  12202. get_val(st, data); // Convert into value in case of a variable
  12203. skill_id=( data_isstring(data) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  12204. skill_lv=script_getnum(st,3);
  12205. x=script_getnum(st,4);
  12206. y=script_getnum(st,5);
  12207. if (bl)
  12208. clif_skill_poseffect(bl,skill_id,skill_lv,x,y,gettick());
  12209. return SCRIPT_CMD_SUCCESS;
  12210. }
  12211. /*==========================================
  12212. * Special effects [Valaris]
  12213. *------------------------------------------*/
  12214. BUILDIN_FUNC(specialeffect)
  12215. {
  12216. struct block_list *bl=map_id2bl(st->oid);
  12217. int type = script_getnum(st,2);
  12218. enum send_target target = script_hasdata(st,3) ? (send_target)script_getnum(st,3) : AREA;
  12219. if(bl==NULL)
  12220. return SCRIPT_CMD_SUCCESS;
  12221. if( script_hasdata(st,4) )
  12222. {
  12223. TBL_NPC *nd = npc_name2id(script_getstr(st,4));
  12224. if(nd)
  12225. clif_specialeffect(&nd->bl, type, target);
  12226. }
  12227. else
  12228. {
  12229. if (target == SELF) {
  12230. TBL_PC *sd=script_rid2sd(st);
  12231. if (sd)
  12232. clif_specialeffect_single(bl,type,sd->fd);
  12233. } else {
  12234. clif_specialeffect(bl, type, target);
  12235. }
  12236. }
  12237. return SCRIPT_CMD_SUCCESS;
  12238. }
  12239. BUILDIN_FUNC(specialeffect2)
  12240. {
  12241. TBL_PC *sd=script_rid2sd(st);
  12242. int type = script_getnum(st,2);
  12243. enum send_target target = script_hasdata(st,3) ? (send_target)script_getnum(st,3) : AREA;
  12244. if( script_nick2sd(4,sd) )
  12245. clif_specialeffect(&sd->bl, type, target);
  12246. return SCRIPT_CMD_SUCCESS;
  12247. }
  12248. /**
  12249. * nude({<char_id>});
  12250. * @author [Valaris]
  12251. **/
  12252. BUILDIN_FUNC(nude)
  12253. {
  12254. TBL_PC *sd;
  12255. int i, calcflag = 0;
  12256. if (!script_charid2sd(2,sd))
  12257. return SCRIPT_CMD_FAILURE;
  12258. for( i = 0 ; i < EQI_MAX; i++ ) {
  12259. if( sd->equip_index[ i ] >= 0 ) {
  12260. if( !calcflag )
  12261. calcflag = 1;
  12262. pc_unequipitem( sd , sd->equip_index[ i ] , 2);
  12263. }
  12264. }
  12265. if( calcflag )
  12266. status_calc_pc(sd,SCO_NONE);
  12267. return SCRIPT_CMD_SUCCESS;
  12268. }
  12269. int atcommand_sub(struct script_state* st,int type) {
  12270. TBL_PC *sd, dummy_sd;
  12271. int fd;
  12272. const char *cmd;
  12273. cmd = script_getstr(st,2);
  12274. if (st->rid) {
  12275. sd = script_rid2sd(st);
  12276. fd = sd->fd;
  12277. } else { //Use a dummy character.
  12278. sd = &dummy_sd;
  12279. fd = 0;
  12280. memset(&dummy_sd, 0, sizeof(TBL_PC));
  12281. if (st->oid) {
  12282. struct block_list* bl = map_id2bl(st->oid);
  12283. memcpy(&dummy_sd.bl, bl, sizeof(struct block_list));
  12284. if (bl->type == BL_NPC)
  12285. safestrncpy(dummy_sd.status.name, ((TBL_NPC*)bl)->name, NAME_LENGTH);
  12286. sd->mapindex = (bl->m > 0) ? map_id2index(bl->m) : mapindex_name2id(map_default.mapname);
  12287. }
  12288. // Init Group ID, Level, & permissions
  12289. sd->group_id = sd->group_level = 99;
  12290. sd->permissions |= PC_PERM_ALLPERMISSION;
  12291. }
  12292. if (!is_atcommand(fd, sd, cmd, type)) {
  12293. ShowWarning("buildin_atcommand: failed to execute command '%s'\n", cmd);
  12294. script_reportsrc(st);
  12295. return SCRIPT_CMD_FAILURE;
  12296. }
  12297. return SCRIPT_CMD_SUCCESS;
  12298. }
  12299. /*==========================================
  12300. * gmcommand [MouseJstr]
  12301. *------------------------------------------*/
  12302. BUILDIN_FUNC(atcommand) {
  12303. return atcommand_sub(st,0);
  12304. }
  12305. /** Displays a message for the player only (like system messages like "you got an apple" )
  12306. * dispbottom("<message>"{,<color>{,<char_id>}})
  12307. * @param message
  12308. * @param color Hex color default (Green)
  12309. */
  12310. BUILDIN_FUNC(dispbottom)
  12311. {
  12312. TBL_PC *sd;
  12313. int color = 0;
  12314. const char *message;
  12315. if (!script_charid2sd(4,sd))
  12316. return SCRIPT_CMD_FAILURE;
  12317. message = script_getstr(st,2);
  12318. if (script_hasdata(st,3))
  12319. color = script_getnum(st,3); // <color>
  12320. if(sd) {
  12321. if (script_hasdata(st,3))
  12322. clif_messagecolor(&sd->bl, color, message, true, SELF); // [Napster]
  12323. else
  12324. clif_messagecolor(&sd->bl, color_table[COLOR_LIGHT_GREEN], message, false, SELF);
  12325. }
  12326. return SCRIPT_CMD_SUCCESS;
  12327. }
  12328. /*===================================
  12329. * Heal portion of recovery command
  12330. *-----------------------------------*/
  12331. int recovery_sub(struct map_session_data* sd, int revive)
  12332. {
  12333. if(revive&(1|4) && pc_isdead(sd)) {
  12334. status_revive(&sd->bl, 100, 100);
  12335. clif_displaymessage(sd->fd,msg_txt(sd,16)); // You've been revived!
  12336. clif_specialeffect(&sd->bl, 77, AREA);
  12337. } else if(revive&(1|2) && !pc_isdead(sd)) {
  12338. status_percent_heal(&sd->bl, 100, 100);
  12339. clif_displaymessage(sd->fd,msg_txt(sd,680)); // You have been recovered!
  12340. }
  12341. return SCRIPT_CMD_SUCCESS;
  12342. }
  12343. /*=========================================================================
  12344. * Fully Recover a Character's HP/SP - [Capuche] & [Akinari]
  12345. * recovery <type>{,<option>,<revive_flag>{,<map name>}};
  12346. * <type> determines <option>:
  12347. * 0 : char_id
  12348. * 1 : party_id
  12349. * 2 : guild_id
  12350. * 3 : map_name
  12351. * 4 : all characters
  12352. * <revive_flag>:
  12353. * 1 : Revive and heal all players (default)
  12354. * 2 : Heal living players only
  12355. * 4 : Revive dead players only
  12356. * <map name>:
  12357. * for types 1-2 : map_name (null = all maps)
  12358. *-------------------------------------------------------------------------*/
  12359. BUILDIN_FUNC(recovery)
  12360. {
  12361. TBL_PC *sd;
  12362. int map_idx = 0, type = 0, revive = 1;
  12363. type = script_getnum(st,2);
  12364. if(script_hasdata(st,4))
  12365. revive = script_getnum(st,4);
  12366. switch(type) {
  12367. case 0:
  12368. if(script_hasdata(st,3))
  12369. sd=map_charid2sd(script_getnum(st,3));
  12370. else if((sd = script_rid2sd(st)) == NULL)
  12371. return SCRIPT_CMD_SUCCESS; //If we don't have sd by now, bail out
  12372. recovery_sub(sd, revive);
  12373. break;
  12374. case 1:
  12375. {
  12376. struct party_data* p;
  12377. //When no party given, we use invoker party
  12378. int p_id = 0, i;
  12379. if(script_hasdata(st,5)) {//Bad maps shouldn't cause issues
  12380. map_idx = map_mapname2mapid(script_getstr(st,5));
  12381. if(map_idx < 1) { //But we'll check anyways
  12382. ShowDebug("recovery: bad map name given (%s)\n", script_getstr(st,5));
  12383. return SCRIPT_CMD_FAILURE;
  12384. }
  12385. }
  12386. if(script_hasdata(st,3))
  12387. p_id = script_getnum(st,3);
  12388. else if((sd = script_rid2sd(st)))
  12389. p_id = sd->status.party_id;
  12390. p = party_search(p_id);
  12391. if(p == NULL)
  12392. return SCRIPT_CMD_SUCCESS;
  12393. for (i = 0; i < MAX_PARTY; i++) {
  12394. struct map_session_data* pl_sd;
  12395. if((!(pl_sd = p->data[i].sd) || pl_sd->status.party_id != p_id)
  12396. || (map_idx && pl_sd->bl.m != map_idx))
  12397. continue;
  12398. recovery_sub(pl_sd, revive);
  12399. }
  12400. break;
  12401. }
  12402. case 2:
  12403. {
  12404. struct guild* g;
  12405. //When no guild given, we use invoker guild
  12406. int g_id = 0, i;
  12407. if(script_hasdata(st,5)) {//Bad maps shouldn't cause issues
  12408. map_idx = map_mapname2mapid(script_getstr(st,5));
  12409. if(map_idx < 1) { //But we'll check anyways
  12410. ShowDebug("recovery: bad map name given (%s)\n", script_getstr(st,5));
  12411. return SCRIPT_CMD_FAILURE;
  12412. }
  12413. }
  12414. if(script_hasdata(st,3))
  12415. g_id = script_getnum(st,3);
  12416. else if((sd = script_rid2sd(st)))
  12417. g_id = sd->status.guild_id;
  12418. g = guild_search(g_id);
  12419. if(g == NULL)
  12420. return SCRIPT_CMD_SUCCESS;
  12421. for (i = 0; i < MAX_GUILD; i++) {
  12422. struct map_session_data* pl_sd;
  12423. if((!(pl_sd = g->member[i].sd) || pl_sd->status.guild_id != g_id)
  12424. || (map_idx && pl_sd->bl.m != map_idx))
  12425. continue;
  12426. recovery_sub(pl_sd, revive);
  12427. }
  12428. break;
  12429. }
  12430. case 3:
  12431. if(script_hasdata(st,3))
  12432. map_idx = map_mapname2mapid(script_getstr(st,3));
  12433. else if((sd = script_rid2sd(st)))
  12434. map_idx = sd->bl.m;
  12435. if(map_idx < 1)
  12436. return SCRIPT_CMD_FAILURE; //No sd and no map given - return
  12437. case 4:
  12438. {
  12439. struct s_mapiterator *iter;
  12440. struct script_data *data = script_getdata(st, 3);
  12441. get_val(st, data); // Convert into value in case of a variable
  12442. if(script_hasdata(st,3) && !data_isstring(data))
  12443. revive = script_getnum(st,3); // recovery 4,<revive_flag>;
  12444. iter = mapit_getallusers();
  12445. for (sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); sd = (TBL_PC*)mapit_next(iter)) {
  12446. if(type == 3 && sd->bl.m != map_idx)
  12447. continue;
  12448. recovery_sub(sd, revive);
  12449. }
  12450. mapit_free(iter);
  12451. break;
  12452. }
  12453. default:
  12454. ShowWarning("script: buildin_recovery: Invalid type %d\n", type);
  12455. script_pushint(st,-1);
  12456. return SCRIPT_CMD_FAILURE;
  12457. }
  12458. script_pushint(st,1); //Successfully executed without errors
  12459. return SCRIPT_CMD_SUCCESS;
  12460. }
  12461. /*==========================================
  12462. * Get your pet info: getpetinfo <type>{,<char_id>}
  12463. * n -> 0:pet_id 1:pet_class 2:pet_name
  12464. * 3:friendly 4:hungry, 5: rename flag.6:level,
  12465. * 7:game id
  12466. *------------------------------------------*/
  12467. BUILDIN_FUNC(getpetinfo)
  12468. {
  12469. TBL_PC *sd=script_rid2sd(st);
  12470. TBL_PET *pd;
  12471. int type = script_getnum(st,2);
  12472. if (!script_charid2sd(3, sd) || !(pd = sd->pd)) {
  12473. if (type == 2)
  12474. script_pushconststr(st,"null");
  12475. else
  12476. script_pushint(st,0);
  12477. return SCRIPT_CMD_SUCCESS;
  12478. }
  12479. switch(type){
  12480. case PETINFO_ID: script_pushint(st,pd->pet.pet_id); break;
  12481. case PETINFO_CLASS: script_pushint(st,pd->pet.class_); break;
  12482. case PETINFO_NAME: script_pushstrcopy(st,pd->pet.name); break;
  12483. case PETINFO_INTIMATE: script_pushint(st,pd->pet.intimate); break;
  12484. case PETINFO_HUNGRY: script_pushint(st,pd->pet.hungry); break;
  12485. case PETINFO_RENAMED: script_pushint(st,pd->pet.rename_flag); break;
  12486. case PETINFO_LEVEL: script_pushint(st,(int)pd->pet.level); break;
  12487. case PETINFO_BLOCKID: script_pushint(st,pd->bl.id); break;
  12488. default:
  12489. script_pushint(st,0);
  12490. break;
  12491. }
  12492. return SCRIPT_CMD_SUCCESS;
  12493. }
  12494. /*==========================================
  12495. * Get your homunculus info: gethominfo <type>{,<char_id>};
  12496. * type -> 0:hom_id 1:class 2:name
  12497. * 3:friendly 4:hungry, 5: rename flag.
  12498. * 6: level, 7: game id
  12499. *------------------------------------------*/
  12500. BUILDIN_FUNC(gethominfo)
  12501. {
  12502. TBL_PC *sd=script_rid2sd(st);
  12503. TBL_HOM *hd;
  12504. int type=script_getnum(st,2);
  12505. if (!script_charid2sd(3, sd) || !(hd = sd->hd)) {
  12506. if (type == 2)
  12507. script_pushconststr(st,"null");
  12508. else
  12509. script_pushint(st,0);
  12510. return SCRIPT_CMD_SUCCESS;
  12511. }
  12512. switch(type){
  12513. case 0: script_pushint(st,hd->homunculus.hom_id); break;
  12514. case 1: script_pushint(st,hd->homunculus.class_); break;
  12515. case 2: script_pushstrcopy(st,hd->homunculus.name); break;
  12516. case 3: script_pushint(st,hd->homunculus.intimacy); break;
  12517. case 4: script_pushint(st,hd->homunculus.hunger); break;
  12518. case 5: script_pushint(st,hd->homunculus.rename_flag); break;
  12519. case 6: script_pushint(st,hd->homunculus.level); break;
  12520. case 7: script_pushint(st,hd->bl.id); break;
  12521. default:
  12522. script_pushint(st,0);
  12523. break;
  12524. }
  12525. return SCRIPT_CMD_SUCCESS;
  12526. }
  12527. /// Retrieves information about character's mercenary
  12528. /// getmercinfo <type>[,<char id>];
  12529. BUILDIN_FUNC(getmercinfo)
  12530. {
  12531. int type;
  12532. struct map_session_data* sd;
  12533. struct mercenary_data* md;
  12534. type = script_getnum(st,2);
  12535. if( script_hasdata(st,3) )
  12536. {
  12537. uint32 char_id = script_getnum(st,3);
  12538. if( ( sd = map_charid2sd(char_id) ) == NULL )
  12539. {
  12540. ShowError("buildin_getmercinfo: No such character (char_id=%d).\n", char_id);
  12541. script_pushnil(st);
  12542. return SCRIPT_CMD_FAILURE;
  12543. }
  12544. }
  12545. else
  12546. {
  12547. if( ( sd = script_rid2sd(st) ) == NULL )
  12548. {
  12549. script_pushnil(st);
  12550. return SCRIPT_CMD_SUCCESS;
  12551. }
  12552. }
  12553. md = ( sd->status.mer_id && sd->md ) ? sd->md : NULL;
  12554. switch( type )
  12555. {
  12556. case 0: script_pushint(st,md ? md->mercenary.mercenary_id : 0); break;
  12557. case 1: script_pushint(st,md ? md->mercenary.class_ : 0); break;
  12558. case 2:
  12559. if( md )
  12560. script_pushstrcopy(st,md->db->name);
  12561. else
  12562. script_pushconststr(st,"");
  12563. break;
  12564. case 3: script_pushint(st,md ? mercenary_get_faith(md) : 0); break;
  12565. case 4: script_pushint(st,md ? mercenary_get_calls(md) : 0); break;
  12566. case 5: script_pushint(st,md ? md->mercenary.kill_count : 0); break;
  12567. case 6: script_pushint(st,md ? mercenary_get_lifetime(md) : 0); break;
  12568. case 7: script_pushint(st,md ? md->db->lv : 0); break;
  12569. case 8: script_pushint(st,md ? md->bl.id : 0); break;
  12570. default:
  12571. ShowError("buildin_getmercinfo: Invalid type %d (char_id=%d).\n", type, sd->status.char_id);
  12572. script_pushnil(st);
  12573. return SCRIPT_CMD_FAILURE;
  12574. }
  12575. return SCRIPT_CMD_SUCCESS;
  12576. }
  12577. /*==========================================
  12578. * Shows wether your inventory(and equips) contain
  12579. selected card or not.
  12580. checkequipedcard(4001);
  12581. *------------------------------------------*/
  12582. BUILDIN_FUNC(checkequipedcard)
  12583. {
  12584. TBL_PC *sd=script_rid2sd(st);
  12585. if(sd){
  12586. int n,i,c=0;
  12587. c=script_getnum(st,2);
  12588. for(i=0;i<MAX_INVENTORY;i++){
  12589. if(sd->inventory.u.items_inventory[i].nameid > 0 && sd->inventory.u.items_inventory[i].amount && sd->inventory_data[i]){
  12590. if (itemdb_isspecial(sd->inventory.u.items_inventory[i].card[0]))
  12591. continue;
  12592. for(n=0;n<sd->inventory_data[i]->slot;n++){
  12593. if(sd->inventory.u.items_inventory[i].card[n] == c) {
  12594. script_pushint(st,1);
  12595. return SCRIPT_CMD_SUCCESS;
  12596. }
  12597. }
  12598. }
  12599. }
  12600. }
  12601. script_pushint(st,0);
  12602. return SCRIPT_CMD_SUCCESS;
  12603. }
  12604. BUILDIN_FUNC(jump_zero)
  12605. {
  12606. int sel;
  12607. sel=script_getnum(st,2);
  12608. if(!sel) {
  12609. int pos;
  12610. if( !data_islabel(script_getdata(st,3)) ){
  12611. ShowError("script: jump_zero: not label !\n");
  12612. st->state=END;
  12613. return SCRIPT_CMD_FAILURE;
  12614. }
  12615. pos=script_getnum(st,3);
  12616. st->pos=pos;
  12617. st->state=GOTO;
  12618. }
  12619. return SCRIPT_CMD_SUCCESS;
  12620. }
  12621. /*==========================================
  12622. * movenpc [MouseJstr]
  12623. *------------------------------------------*/
  12624. BUILDIN_FUNC(movenpc)
  12625. {
  12626. TBL_NPC *nd = NULL;
  12627. const char *npc;
  12628. int x,y;
  12629. npc = script_getstr(st,2);
  12630. x = script_getnum(st,3);
  12631. y = script_getnum(st,4);
  12632. if ((nd = npc_name2id(npc)) == NULL){
  12633. ShowError("script: movenpc: NPC with ID '%s' was not found!\n", npc );
  12634. return -1;
  12635. }
  12636. if (script_hasdata(st,5))
  12637. nd->ud.dir = script_getnum(st,5) % 8;
  12638. npc_movenpc(nd, x, y);
  12639. return SCRIPT_CMD_SUCCESS;
  12640. }
  12641. /*==========================================
  12642. * message [MouseJstr]
  12643. *------------------------------------------*/
  12644. BUILDIN_FUNC(message)
  12645. {
  12646. const char *msg,*player;
  12647. TBL_PC *pl_sd = NULL;
  12648. player = script_getstr(st,2);
  12649. msg = script_getstr(st,3);
  12650. if((pl_sd=map_nick2sd((char *) player,false)) == NULL)
  12651. return SCRIPT_CMD_SUCCESS;
  12652. clif_displaymessage(pl_sd->fd, msg);
  12653. return SCRIPT_CMD_SUCCESS;
  12654. }
  12655. /*==========================================
  12656. * npctalk (sends message to surrounding area)
  12657. *------------------------------------------*/
  12658. BUILDIN_FUNC(npctalk)
  12659. {
  12660. struct npc_data* nd = NULL;
  12661. const char* str = script_getstr(st,2);
  12662. if (script_hasdata(st, 3))
  12663. nd = npc_name2id(script_getstr(st, 3));
  12664. else
  12665. nd = (struct npc_data *)map_id2bl(st->oid);
  12666. if (nd != NULL) {
  12667. char message[256];
  12668. safesnprintf(message, sizeof(message), "%s", str);
  12669. clif_disp_overhead(&nd->bl, message);
  12670. }
  12671. return SCRIPT_CMD_SUCCESS;
  12672. }
  12673. // change npc walkspeed [Valaris]
  12674. BUILDIN_FUNC(npcspeed)
  12675. {
  12676. struct npc_data* nd;
  12677. int speed;
  12678. speed = script_getnum(st,2);
  12679. nd =(struct npc_data *)map_id2bl(st->oid);
  12680. if( nd ) {
  12681. nd->speed = speed;
  12682. nd->ud.state.speed_changed = 1;
  12683. }
  12684. return SCRIPT_CMD_SUCCESS;
  12685. }
  12686. // make an npc walk to a position [Valaris]
  12687. BUILDIN_FUNC(npcwalkto)
  12688. {
  12689. struct npc_data *nd=(struct npc_data *)map_id2bl(st->oid);
  12690. int x=0,y=0;
  12691. x=script_getnum(st,2);
  12692. y=script_getnum(st,3);
  12693. if(nd) {
  12694. if (!nd->status.hp)
  12695. status_calc_npc(nd, SCO_FIRST);
  12696. else
  12697. status_calc_npc(nd, SCO_NONE);
  12698. unit_walktoxy(&nd->bl,x,y,0);
  12699. }
  12700. return SCRIPT_CMD_SUCCESS;
  12701. }
  12702. // stop an npc's movement [Valaris]
  12703. BUILDIN_FUNC(npcstop)
  12704. {
  12705. struct npc_data *nd=(struct npc_data *)map_id2bl(st->oid);
  12706. if(nd) {
  12707. unit_stop_walking(&nd->bl,1|4);
  12708. }
  12709. return SCRIPT_CMD_SUCCESS;
  12710. }
  12711. /**
  12712. * getlook(<type>{,<char_id>})
  12713. **/
  12714. BUILDIN_FUNC(getlook)
  12715. {
  12716. int type,val;
  12717. TBL_PC *sd;
  12718. if (!script_charid2sd(3,sd)) {
  12719. script_pushint(st,-1);
  12720. return SCRIPT_CMD_FAILURE;
  12721. }
  12722. type=script_getnum(st,2);
  12723. val=-1;
  12724. switch(type) {
  12725. // TODO: implement LOOK_BASE as stated in script doc
  12726. case LOOK_HAIR: val=sd->status.hair; break; //1
  12727. case LOOK_WEAPON: val=sd->status.weapon; break; //2
  12728. case LOOK_HEAD_BOTTOM: val=sd->status.head_bottom; break; //3
  12729. case LOOK_HEAD_TOP: val=sd->status.head_top; break; //4
  12730. case LOOK_HEAD_MID: val=sd->status.head_mid; break; //5
  12731. case LOOK_HAIR_COLOR: val=sd->status.hair_color; break; //6
  12732. case LOOK_CLOTHES_COLOR: val=sd->status.clothes_color; break; //7
  12733. case LOOK_SHIELD: val=sd->status.shield; break; //8
  12734. case LOOK_SHOES: break; //9
  12735. case LOOK_ROBE: val=sd->status.robe; break; //12
  12736. case LOOK_BODY2: val=sd->status.body; break; //13
  12737. }
  12738. script_pushint(st,val);
  12739. return SCRIPT_CMD_SUCCESS;
  12740. }
  12741. /**
  12742. * getsavepoint(<information type>{,<char_id>})
  12743. * @param type 0- map name, 1- x, 2- y
  12744. **/
  12745. BUILDIN_FUNC(getsavepoint)
  12746. {
  12747. TBL_PC* sd;
  12748. int type;
  12749. if (!script_charid2sd(3,sd)) {
  12750. script_pushint(st,0);
  12751. return SCRIPT_CMD_SUCCESS;
  12752. }
  12753. type = script_getnum(st,2);
  12754. switch(type) {
  12755. case 0: script_pushstrcopy(st,mapindex_id2name(sd->status.save_point.map)); break;
  12756. case 1: script_pushint(st,sd->status.save_point.x); break;
  12757. case 2: script_pushint(st,sd->status.save_point.y); break;
  12758. default:
  12759. script_pushint(st,0);
  12760. break;
  12761. }
  12762. return SCRIPT_CMD_SUCCESS;
  12763. }
  12764. /**
  12765. * Get position for BL objects.
  12766. * getmapxy(<map name>,<x>,<y>,<type>{,<char name>});
  12767. * @param mapname: String variable for output map name
  12768. * @param x: Integer variable for output coord X
  12769. * @param y: Integer variable for output coord Y
  12770. * @param type: Type of object
  12771. * UNITTYPE_PC - Character coord
  12772. * UNITTYPE_NPC - NPC coord
  12773. * UNITTYPE_PET - Pet coord
  12774. * UNITTYPE_HOM - Homun coord
  12775. * UNITTYPE_MER - Mercenary coord
  12776. * UNITTYPE_ELEM - Elemental coord
  12777. * @param charname: Name object. If empty or "this" use the current object
  12778. * @return 0 - success; -1 - some error, MapName$,MapX,MapY contains unknown value.
  12779. */
  12780. BUILDIN_FUNC(getmapxy)
  12781. {
  12782. struct block_list *bl = NULL;
  12783. TBL_PC *sd=NULL;
  12784. int64 num;
  12785. const char *name;
  12786. char prefix;
  12787. int x,y,type;
  12788. char mapname[MAP_NAME_LENGTH];
  12789. if( !data_isreference(script_getdata(st,2)) ) {
  12790. ShowWarning("script: buildin_getmapxy: mapname value is not a variable.\n");
  12791. script_pushint(st,-1);
  12792. return SCRIPT_CMD_FAILURE;
  12793. }
  12794. if( !data_isreference(script_getdata(st,3)) ) {
  12795. ShowWarning("script: buildin_getmapxy: mapx value is not a variable.\n");
  12796. script_pushint(st,-1);
  12797. return SCRIPT_CMD_FAILURE;
  12798. }
  12799. if( !data_isreference(script_getdata(st,4)) ) {
  12800. ShowWarning("script: buildin_getmapxy: mapy value is not a variable.\n");
  12801. script_pushint(st,-1);
  12802. return SCRIPT_CMD_FAILURE;
  12803. }
  12804. if( !is_string_variable(reference_getname(script_getdata(st, 2))) ) {
  12805. ShowWarning("script: buildin_getmapxy: %s is not a string variable.\n",reference_getname(script_getdata(st, 2)));
  12806. script_pushint(st,-1);
  12807. return SCRIPT_CMD_FAILURE;
  12808. }
  12809. if( is_string_variable(reference_getname(script_getdata(st, 3))) ) {
  12810. ShowWarning("script: buildin_getmapxy: %s is a string variable, should be an INT.\n",reference_getname(script_getdata(st, 3)));
  12811. script_pushint(st,-1);
  12812. return SCRIPT_CMD_FAILURE;
  12813. }
  12814. if( is_string_variable(reference_getname(script_getdata(st, 4))) ) {
  12815. ShowWarning("script: buildin_getmapxy: %s is a string variable, should be an INT.\n",reference_getname(script_getdata(st, 4)));
  12816. script_pushint(st,-1);
  12817. return SCRIPT_CMD_FAILURE;
  12818. }
  12819. // Possible needly check function parameters on C_STR,C_INT,C_INT
  12820. type=script_getnum(st,5);
  12821. switch (type) {
  12822. case UNITTYPE_PC: //Get Character Position
  12823. if( script_nick2sd(6,sd) )
  12824. bl = &sd->bl;
  12825. break;
  12826. case UNITTYPE_NPC: //Get NPC Position
  12827. if( script_hasdata(st,6) )
  12828. {
  12829. struct npc_data *nd;
  12830. nd=npc_name2id(script_getstr(st,6));
  12831. if (nd)
  12832. bl = &nd->bl;
  12833. } else //In case the origin is not an npc?
  12834. bl=map_id2bl(st->oid);
  12835. break;
  12836. case UNITTYPE_PET: //Get Pet Position
  12837. if( script_nick2sd(6, sd) && sd->pd )
  12838. bl = &sd->pd->bl;
  12839. break;
  12840. case UNITTYPE_HOM: //Get Homun Position
  12841. if( script_nick2sd(6, sd) && sd->hd )
  12842. bl = &sd->hd->bl;
  12843. break;
  12844. case UNITTYPE_MER: //Get Mercenary Position
  12845. if( script_nick2sd(6, sd) && sd->md )
  12846. bl = &sd->md->bl;
  12847. break;
  12848. case UNITTYPE_ELEM: //Get Elemental Position
  12849. if( script_nick2sd(6, sd) && sd->ed )
  12850. bl = &sd->ed->bl;
  12851. break;
  12852. default:
  12853. ShowWarning("script: buildin_getmapxy: Invalid type %d.\n", type);
  12854. script_pushint(st,-1);
  12855. return SCRIPT_CMD_FAILURE;
  12856. }
  12857. if (!bl) { //No object found.
  12858. script_pushint(st,-1);
  12859. return SCRIPT_CMD_SUCCESS;
  12860. }
  12861. x= bl->x;
  12862. y= bl->y;
  12863. safestrncpy(mapname, map[bl->m].name, MAP_NAME_LENGTH);
  12864. //Set MapName$
  12865. num=st->stack->stack_data[st->start+2].u.num;
  12866. name=get_str(script_getvarid(num));
  12867. prefix=*name;
  12868. if(not_server_variable(prefix))
  12869. sd=script_rid2sd(st);
  12870. else
  12871. sd=NULL;
  12872. set_reg(st,sd,num,name,(void*)mapname,script_getref(st,2));
  12873. //Set MapX
  12874. num=st->stack->stack_data[st->start+3].u.num;
  12875. name=get_str(script_getvarid(num));
  12876. prefix=*name;
  12877. if(not_server_variable(prefix))
  12878. sd=script_rid2sd(st);
  12879. else
  12880. sd=NULL;
  12881. set_reg(st,sd,num,name,(void*)__64BPRTSIZE(x),script_getref(st,3));
  12882. //Set MapY
  12883. num=st->stack->stack_data[st->start+4].u.num;
  12884. name=get_str(script_getvarid(num));
  12885. prefix=*name;
  12886. if(not_server_variable(prefix))
  12887. sd=script_rid2sd(st);
  12888. else
  12889. sd=NULL;
  12890. set_reg(st,sd,num,name,(void*)__64BPRTSIZE(y),script_getref(st,4));
  12891. //Return Success value
  12892. script_pushint(st,0);
  12893. return SCRIPT_CMD_SUCCESS;
  12894. }
  12895. /// Returns the map name of given map ID.
  12896. ///
  12897. /// mapid2name <map ID>;
  12898. BUILDIN_FUNC(mapid2name)
  12899. {
  12900. uint16 m = script_getnum(st, 2);
  12901. if (m < 0 || m >= MAX_MAP_PER_SERVER) {
  12902. script_pushstr(st, "");
  12903. return SCRIPT_CMD_FAILURE;
  12904. }
  12905. script_pushstrcopy(st, map_mapid2mapname(m));
  12906. return SCRIPT_CMD_SUCCESS;
  12907. }
  12908. /*==========================================
  12909. * Allows player to write NPC logs (i.e. Bank NPC, etc) [Lupus]
  12910. *------------------------------------------*/
  12911. BUILDIN_FUNC(logmes)
  12912. {
  12913. const char *str;
  12914. TBL_PC* sd;
  12915. sd = script_rid2sd(st);
  12916. if( sd == NULL )
  12917. return SCRIPT_CMD_FAILURE;
  12918. str = script_getstr(st,2);
  12919. log_npc(sd,str);
  12920. return SCRIPT_CMD_SUCCESS;
  12921. }
  12922. BUILDIN_FUNC(summon)
  12923. {
  12924. int _class, timeout=0;
  12925. const char *str,*event="";
  12926. TBL_PC *sd;
  12927. struct mob_data *md;
  12928. int tick = gettick();
  12929. sd=script_rid2sd(st);
  12930. if (!sd)
  12931. return SCRIPT_CMD_SUCCESS;
  12932. str =script_getstr(st,2);
  12933. _class=script_getnum(st,3);
  12934. if( script_hasdata(st,4) )
  12935. timeout=script_getnum(st,4);
  12936. if( script_hasdata(st,5) ){
  12937. event=script_getstr(st,5);
  12938. check_event(st, event);
  12939. }
  12940. clif_skill_poseffect(&sd->bl,AM_CALLHOMUN,1,sd->bl.x,sd->bl.y,tick);
  12941. md = mob_once_spawn_sub(&sd->bl, sd->bl.m, sd->bl.x, sd->bl.y, str, _class, event, SZ_SMALL, AI_NONE);
  12942. if (md) {
  12943. md->master_id=sd->bl.id;
  12944. md->special_state.ai = AI_ATTACK;
  12945. if( md->deletetimer != INVALID_TIMER )
  12946. delete_timer(md->deletetimer, mob_timer_delete);
  12947. md->deletetimer = add_timer(tick+(timeout>0?timeout:60000),mob_timer_delete,md->bl.id,0);
  12948. mob_spawn (md); //Now it is ready for spawning.
  12949. clif_specialeffect(&md->bl,344,AREA);
  12950. sc_start4(NULL,&md->bl, SC_MODECHANGE, 100, 1, 0, MD_AGGRESSIVE, 0, 60000);
  12951. }
  12952. script_pushint(st, md->bl.id);
  12953. return SCRIPT_CMD_SUCCESS;
  12954. }
  12955. /*==========================================
  12956. * Checks whether it is daytime/nighttime
  12957. *------------------------------------------*/
  12958. BUILDIN_FUNC(isnight)
  12959. {
  12960. script_pushint(st,(night_flag == 1));
  12961. return SCRIPT_CMD_SUCCESS;
  12962. }
  12963. BUILDIN_FUNC(isday)
  12964. {
  12965. script_pushint(st,(night_flag == 0));
  12966. return SCRIPT_CMD_SUCCESS;
  12967. }
  12968. /*================================================
  12969. * Check how many items/cards in the list are
  12970. * equipped - used for 2/15's cards patch [celest]
  12971. *------------------------------------------------*/
  12972. BUILDIN_FUNC(isequippedcnt)
  12973. {
  12974. TBL_PC *sd;
  12975. int i, id = 1;
  12976. int ret = 0;
  12977. sd = script_rid2sd(st);
  12978. if (!sd) { //If the player is not attached it is a script error anyway... but better prevent the map server from crashing...
  12979. script_pushint(st,0);
  12980. return SCRIPT_CMD_SUCCESS;
  12981. }
  12982. for (i=0; id!=0; i++) {
  12983. short j;
  12984. FETCH (i+2, id) else id = 0;
  12985. if (id <= 0)
  12986. continue;
  12987. for (j=0; j<EQI_MAX; j++) {
  12988. short index = sd->equip_index[j];
  12989. if(index < 0)
  12990. continue;
  12991. if (pc_is_same_equip_index((enum equip_index)j, sd->equip_index, index))
  12992. continue;
  12993. if(!sd->inventory_data[index])
  12994. continue;
  12995. if (itemdb_type(id) != IT_CARD) { //No card. Count amount in inventory.
  12996. if (sd->inventory_data[index]->nameid == id)
  12997. ret+= sd->inventory.u.items_inventory[index].amount;
  12998. } else { //Count cards.
  12999. short k;
  13000. if (itemdb_isspecial(sd->inventory.u.items_inventory[index].card[0]))
  13001. continue; //No cards
  13002. for(k=0; k<sd->inventory_data[index]->slot; k++) {
  13003. if (sd->inventory.u.items_inventory[index].card[k] == id)
  13004. ret++; //[Lupus]
  13005. }
  13006. }
  13007. }
  13008. }
  13009. script_pushint(st,ret);
  13010. return SCRIPT_CMD_SUCCESS;
  13011. }
  13012. /*================================================
  13013. * Check whether another card has been
  13014. * equipped - used for 2/15's cards patch [celest]
  13015. * -- Items checked cannot be reused in another
  13016. * card set to prevent exploits
  13017. *------------------------------------------------*/
  13018. BUILDIN_FUNC(isequipped)
  13019. {
  13020. TBL_PC *sd;
  13021. int i, id = 1;
  13022. int ret = -1;
  13023. //Original hash to reverse it when full check fails.
  13024. unsigned int setitem_hash = 0, setitem_hash2 = 0;
  13025. sd = script_rid2sd(st);
  13026. if (!sd) { //If the player is not attached it is a script error anyway... but better prevent the map server from crashing...
  13027. script_pushint(st,0);
  13028. return SCRIPT_CMD_SUCCESS;
  13029. }
  13030. setitem_hash = sd->bonus.setitem_hash;
  13031. setitem_hash2 = sd->bonus.setitem_hash2;
  13032. for (i=0; id!=0; i++) {
  13033. int flag = 0;
  13034. short j;
  13035. FETCH (i+2, id) else id = 0;
  13036. if (id <= 0)
  13037. continue;
  13038. for (j=0; j<EQI_MAX; j++) {
  13039. short index = sd->equip_index[j];
  13040. if(index < 0)
  13041. continue;
  13042. if (pc_is_same_equip_index((enum equip_index)i, sd->equip_index, index))
  13043. continue;
  13044. if(!sd->inventory_data[index])
  13045. continue;
  13046. if (itemdb_type(id) != IT_CARD) {
  13047. if (sd->inventory_data[index]->nameid != id)
  13048. continue;
  13049. flag = 1;
  13050. break;
  13051. } else { //Cards
  13052. short k;
  13053. if (sd->inventory_data[index]->slot == 0 ||
  13054. itemdb_isspecial(sd->inventory.u.items_inventory[index].card[0]))
  13055. continue;
  13056. for (k = 0; k < sd->inventory_data[index]->slot; k++)
  13057. { //New hash system which should support up to 4 slots on any equipment. [Skotlex]
  13058. unsigned int hash = 0;
  13059. if (sd->inventory.u.items_inventory[index].card[k] != id)
  13060. continue;
  13061. hash = 1<<((j<5?j:j-5)*4 + k);
  13062. // check if card is already used by another set
  13063. if ( ( j < 5 ? sd->bonus.setitem_hash : sd->bonus.setitem_hash2 ) & hash)
  13064. continue;
  13065. // We have found a match
  13066. flag = 1;
  13067. // Set hash so this card cannot be used by another
  13068. if (j<5)
  13069. sd->bonus.setitem_hash |= hash;
  13070. else
  13071. sd->bonus.setitem_hash2 |= hash;
  13072. break;
  13073. }
  13074. }
  13075. if (flag) break; //Card found
  13076. }
  13077. if (ret == -1)
  13078. ret = flag;
  13079. else
  13080. ret &= flag;
  13081. if (!ret) break;
  13082. }
  13083. if (!ret) {//When check fails, restore original hash values. [Skotlex]
  13084. sd->bonus.setitem_hash = setitem_hash;
  13085. sd->bonus.setitem_hash2 = setitem_hash2;
  13086. }
  13087. script_pushint(st,ret);
  13088. return SCRIPT_CMD_SUCCESS;
  13089. }
  13090. /*================================================
  13091. * Check how many given inserted cards in the CURRENT
  13092. * weapon - used for 2/15's cards patch [Lupus]
  13093. *------------------------------------------------*/
  13094. BUILDIN_FUNC(cardscnt)
  13095. {
  13096. TBL_PC *sd;
  13097. int i, k, id = 1;
  13098. int ret = 0;
  13099. int index;
  13100. sd = script_rid2sd(st);
  13101. for (i=0; id!=0; i++) {
  13102. FETCH (i+2, id) else id = 0;
  13103. if (id <= 0)
  13104. continue;
  13105. index = current_equip_item_index; //we get CURRENT WEAPON inventory index from status.c [Lupus]
  13106. if(index < 0) continue;
  13107. if(!sd->inventory_data[index])
  13108. continue;
  13109. if(itemdb_type(id) != IT_CARD) {
  13110. if (sd->inventory_data[index]->nameid == id)
  13111. ret+= sd->inventory.u.items_inventory[index].amount;
  13112. } else {
  13113. if (itemdb_isspecial(sd->inventory.u.items_inventory[index].card[0]))
  13114. continue;
  13115. for(k=0; k<sd->inventory_data[index]->slot; k++) {
  13116. if (sd->inventory.u.items_inventory[index].card[k] == id)
  13117. ret++;
  13118. }
  13119. }
  13120. }
  13121. script_pushint(st,ret);
  13122. // script_pushint(st,current_equip_item_index);
  13123. return SCRIPT_CMD_SUCCESS;
  13124. }
  13125. /*=======================================================
  13126. * Returns the refined number of the current item, or an
  13127. * item with inventory index specified
  13128. *-------------------------------------------------------*/
  13129. BUILDIN_FUNC(getrefine)
  13130. {
  13131. TBL_PC *sd;
  13132. if ((sd = script_rid2sd(st))!= NULL)
  13133. script_pushint(st,sd->inventory.u.items_inventory[current_equip_item_index].refine);
  13134. else
  13135. script_pushint(st,0);
  13136. return SCRIPT_CMD_SUCCESS;
  13137. }
  13138. /*=======================================================
  13139. * Day/Night controls
  13140. *-------------------------------------------------------*/
  13141. BUILDIN_FUNC(night)
  13142. {
  13143. if (night_flag != 1) map_night_timer(night_timer_tid, 0, 0, 1);
  13144. return SCRIPT_CMD_SUCCESS;
  13145. }
  13146. BUILDIN_FUNC(day)
  13147. {
  13148. if (night_flag != 0) map_day_timer(day_timer_tid, 0, 0, 1);
  13149. return SCRIPT_CMD_SUCCESS;
  13150. }
  13151. /**
  13152. * unequip <equipment slot>{,<char_id>};
  13153. * @author [Spectre]
  13154. **/
  13155. BUILDIN_FUNC(unequip) {
  13156. int pos;
  13157. TBL_PC *sd;
  13158. if (!script_charid2sd(3,sd))
  13159. return SCRIPT_CMD_FAILURE;
  13160. pos = script_getnum(st,2);
  13161. if (equip_index_check(pos)) {
  13162. short i = pc_checkequip(sd,equip_bitmask[pos]);
  13163. if (i >= 0) {
  13164. pc_unequipitem(sd,i,1|2);
  13165. script_pushint(st, 1);
  13166. return SCRIPT_CMD_SUCCESS;
  13167. }
  13168. }
  13169. ShowError("buildin_unequip: No item equipped at pos %d (CID=%d/AID=%d).\n", pos, sd->status.char_id, sd->status.account_id);
  13170. script_pushint(st, 0);
  13171. return SCRIPT_CMD_FAILURE;
  13172. }
  13173. /**
  13174. * equip <item id>{,<char_id>};
  13175. **/
  13176. BUILDIN_FUNC(equip) {
  13177. unsigned short nameid = 0;
  13178. TBL_PC *sd;
  13179. struct item_data *item_data;
  13180. if (!script_charid2sd(3,sd))
  13181. return SCRIPT_CMD_FAILURE;
  13182. nameid = script_getnum(st,2);
  13183. if ((item_data = itemdb_exists(nameid))) {
  13184. int i;
  13185. ARR_FIND( 0, MAX_INVENTORY, i, sd->inventory.u.items_inventory[i].nameid == nameid );
  13186. if (i < MAX_INVENTORY) {
  13187. pc_equipitem(sd,i,item_data->equip);
  13188. script_pushint(st,1);
  13189. return SCRIPT_CMD_SUCCESS;
  13190. }
  13191. }
  13192. ShowError("buildin_equip: Item %hu cannot be equipped\n",nameid);
  13193. script_pushint(st,0);
  13194. return SCRIPT_CMD_FAILURE;
  13195. }
  13196. BUILDIN_FUNC(autoequip)
  13197. {
  13198. unsigned short nameid;
  13199. int flag;
  13200. struct item_data *item_data;
  13201. nameid=script_getnum(st,2);
  13202. flag=script_getnum(st,3);
  13203. if( ( item_data = itemdb_exists(nameid) ) == NULL )
  13204. {
  13205. ShowError("buildin_autoequip: Invalid item '%hu'.\n", nameid);
  13206. return SCRIPT_CMD_FAILURE;
  13207. }
  13208. if( !itemdb_isequip2(item_data) )
  13209. {
  13210. ShowError("buildin_autoequip: Item '%hu' cannot be equipped.\n", nameid);
  13211. return SCRIPT_CMD_FAILURE;
  13212. }
  13213. item_data->flag.autoequip = flag>0?1:0;
  13214. return SCRIPT_CMD_SUCCESS;
  13215. }
  13216. BUILDIN_FUNC(setbattleflag)
  13217. {
  13218. const char *flag, *value;
  13219. flag = script_getstr(st,2);
  13220. value = script_getstr(st,3); // HACK: Retrieve number as string (auto-converted) for battle_set_value
  13221. if (battle_set_value(flag, value) == 0)
  13222. ShowWarning("buildin_setbattleflag: unknown battle_config flag '%s'\n",flag);
  13223. else
  13224. ShowInfo("buildin_setbattleflag: battle_config flag '%s' is now set to '%s'.\n",flag,value);
  13225. return SCRIPT_CMD_SUCCESS;
  13226. }
  13227. BUILDIN_FUNC(getbattleflag)
  13228. {
  13229. const char *flag;
  13230. flag = script_getstr(st,2);
  13231. script_pushint(st,battle_get_value(flag));
  13232. return SCRIPT_CMD_SUCCESS;
  13233. }
  13234. //=======================================================
  13235. // strlen [Valaris]
  13236. //-------------------------------------------------------
  13237. BUILDIN_FUNC(getstrlen)
  13238. {
  13239. const char *str = script_getstr(st,2);
  13240. int len = (str) ? (int)strlen(str) : 0;
  13241. script_pushint(st,len);
  13242. return SCRIPT_CMD_SUCCESS;
  13243. }
  13244. //=======================================================
  13245. // isalpha [Valaris]
  13246. //-------------------------------------------------------
  13247. BUILDIN_FUNC(charisalpha)
  13248. {
  13249. const char *str=script_getstr(st,2);
  13250. int pos=script_getnum(st,3);
  13251. int val = ( str && pos >= 0 && (unsigned int)pos < strlen(str) ) ? ISALPHA( str[pos] ) != 0 : 0;
  13252. script_pushint(st,val);
  13253. return SCRIPT_CMD_SUCCESS;
  13254. }
  13255. //=======================================================
  13256. // charisupper <str>, <index>
  13257. //-------------------------------------------------------
  13258. BUILDIN_FUNC(charisupper)
  13259. {
  13260. const char *str = script_getstr(st,2);
  13261. int pos = script_getnum(st,3);
  13262. int val = ( str && pos >= 0 && (unsigned int)pos < strlen(str) ) ? ISUPPER( str[pos] ) : 0;
  13263. script_pushint(st,val);
  13264. return SCRIPT_CMD_SUCCESS;
  13265. }
  13266. //=======================================================
  13267. // charislower <str>, <index>
  13268. //-------------------------------------------------------
  13269. BUILDIN_FUNC(charislower)
  13270. {
  13271. const char *str = script_getstr(st,2);
  13272. int pos = script_getnum(st,3);
  13273. int val = ( str && pos >= 0 && (unsigned int)pos < strlen(str) ) ? ISLOWER( str[pos] ) : 0;
  13274. script_pushint(st,val);
  13275. return SCRIPT_CMD_SUCCESS;
  13276. }
  13277. //=======================================================
  13278. // charat <str>, <index>
  13279. //-------------------------------------------------------
  13280. BUILDIN_FUNC(charat) {
  13281. const char *str = script_getstr(st,2);
  13282. int pos = script_getnum(st,3);
  13283. if( pos >= 0 && (unsigned int)pos < strlen(str) ) {
  13284. char output[2];
  13285. output[0] = str[pos];
  13286. output[1] = '\0';
  13287. script_pushstrcopy(st, output);
  13288. } else
  13289. script_pushconststr(st, "");
  13290. return SCRIPT_CMD_SUCCESS;
  13291. }
  13292. //=======================================================
  13293. // setchar <string>, <char>, <index>
  13294. //-------------------------------------------------------
  13295. BUILDIN_FUNC(setchar)
  13296. {
  13297. const char *str = script_getstr(st,2);
  13298. const char *c = script_getstr(st,3);
  13299. int index = script_getnum(st,4);
  13300. char *output = aStrdup(str);
  13301. if(index >= 0 && index < strlen(output))
  13302. output[index] = *c;
  13303. script_pushstr(st, output);
  13304. return SCRIPT_CMD_SUCCESS;
  13305. }
  13306. //=======================================================
  13307. // insertchar <string>, <char>, <index>
  13308. //-------------------------------------------------------
  13309. BUILDIN_FUNC(insertchar)
  13310. {
  13311. const char *str = script_getstr(st,2);
  13312. const char *c = script_getstr(st,3);
  13313. int index = script_getnum(st,4);
  13314. char *output;
  13315. size_t len = strlen(str);
  13316. if(index < 0)
  13317. index = 0;
  13318. else if(index > len)
  13319. index = len;
  13320. output = (char*)aMalloc(len + 2);
  13321. memcpy(output, str, index);
  13322. output[index] = c[0];
  13323. memcpy(&output[index+1], &str[index], len - index);
  13324. output[len+1] = '\0';
  13325. script_pushstr(st, output);
  13326. return SCRIPT_CMD_SUCCESS;
  13327. }
  13328. //=======================================================
  13329. // delchar <string>, <index>
  13330. //-------------------------------------------------------
  13331. BUILDIN_FUNC(delchar)
  13332. {
  13333. const char *str = script_getstr(st,2);
  13334. int index = script_getnum(st,3);
  13335. char *output;
  13336. size_t len = strlen(str);
  13337. if(index < 0 || index > len) {
  13338. //return original
  13339. output = aStrdup(str);
  13340. script_pushstr(st, output);
  13341. return SCRIPT_CMD_SUCCESS;
  13342. }
  13343. output = (char*)aMalloc(len);
  13344. memcpy(output, str, index);
  13345. memcpy(&output[index], &str[index+1], len - index);
  13346. script_pushstr(st, output);
  13347. return SCRIPT_CMD_SUCCESS;
  13348. }
  13349. //=======================================================
  13350. // strtoupper <str>
  13351. //-------------------------------------------------------
  13352. BUILDIN_FUNC(strtoupper)
  13353. {
  13354. const char *str = script_getstr(st,2);
  13355. char *output = aStrdup(str);
  13356. char *cursor = output;
  13357. while (*cursor != '\0') {
  13358. *cursor = TOUPPER(*cursor);
  13359. cursor++;
  13360. }
  13361. script_pushstr(st, output);
  13362. return SCRIPT_CMD_SUCCESS;
  13363. }
  13364. //=======================================================
  13365. // strtolower <str>
  13366. //-------------------------------------------------------
  13367. BUILDIN_FUNC(strtolower)
  13368. {
  13369. const char *str = script_getstr(st,2);
  13370. char *output = aStrdup(str);
  13371. char *cursor = output;
  13372. while (*cursor != '\0') {
  13373. *cursor = TOLOWER(*cursor);
  13374. cursor++;
  13375. }
  13376. script_pushstr(st, output);
  13377. return SCRIPT_CMD_SUCCESS;
  13378. }
  13379. //=======================================================
  13380. // substr <str>, <start>, <end>
  13381. //-------------------------------------------------------
  13382. BUILDIN_FUNC(substr)
  13383. {
  13384. const char *str = script_getstr(st,2);
  13385. char *output;
  13386. int start = script_getnum(st,3);
  13387. int end = script_getnum(st,4);
  13388. int len = 0;
  13389. if(start >= 0 && end < strlen(str) && start <= end) {
  13390. len = end - start + 1;
  13391. output = (char*)aMalloc(len + 1);
  13392. memcpy(output, &str[start], len);
  13393. } else
  13394. output = (char*)aMalloc(1);
  13395. output[len] = '\0';
  13396. script_pushstr(st, output);
  13397. return SCRIPT_CMD_SUCCESS;
  13398. }
  13399. //=======================================================
  13400. // explode <dest_string_array>, <str>, <delimiter>
  13401. // Note: delimiter is limited to 1 char
  13402. //-------------------------------------------------------
  13403. BUILDIN_FUNC(explode)
  13404. {
  13405. struct script_data* data = script_getdata(st, 2);
  13406. const char *str = script_getstr(st,3);
  13407. const char delimiter = script_getstr(st, 4)[0];
  13408. int32 id;
  13409. size_t len = strlen(str);
  13410. int i = 0, j = 0;
  13411. int start;
  13412. char *temp;
  13413. const char* name;
  13414. TBL_PC* sd = NULL;
  13415. temp = (char*)aMalloc(len + 1);
  13416. if( !data_isreference(data) ) {
  13417. ShowError("script:explode: not a variable\n");
  13418. script_reportdata(data);
  13419. st->state = END;
  13420. return SCRIPT_CMD_FAILURE;// not a variable
  13421. }
  13422. id = reference_getid(data);
  13423. start = reference_getindex(data);
  13424. name = reference_getname(data);
  13425. if( !is_string_variable(name) ) {
  13426. ShowError("script:explode: not string array\n");
  13427. script_reportdata(data);
  13428. st->state = END;
  13429. return SCRIPT_CMD_FAILURE;// data type mismatch
  13430. }
  13431. if( not_server_variable(*name) ) {
  13432. sd = script_rid2sd(st);
  13433. if( sd == NULL )
  13434. return SCRIPT_CMD_SUCCESS;// no player attached
  13435. }
  13436. while(str[i] != '\0') {
  13437. if(str[i] == delimiter && start < SCRIPT_MAX_ARRAYSIZE-1) { //break at delimiter but ignore after reaching last array index
  13438. temp[j] = '\0';
  13439. set_reg(st, sd, reference_uid(id, start++), name, (void*)temp, reference_getref(data));
  13440. j = 0;
  13441. ++i;
  13442. } else {
  13443. temp[j++] = str[i++];
  13444. }
  13445. }
  13446. //set last string
  13447. temp[j] = '\0';
  13448. set_reg(st, sd, reference_uid(id, start), name, (void*)temp, reference_getref(data));
  13449. aFree(temp);
  13450. return SCRIPT_CMD_SUCCESS;
  13451. }
  13452. //=======================================================
  13453. // implode <string_array>
  13454. // implode <string_array>, <glue>
  13455. //-------------------------------------------------------
  13456. BUILDIN_FUNC(implode)
  13457. {
  13458. struct script_data* data = script_getdata(st, 2);
  13459. const char *name;
  13460. uint32 glue_len = 0, array_size, id;
  13461. char *output;
  13462. TBL_PC* sd = NULL;
  13463. if( !data_isreference(data) ) {
  13464. ShowError("script:implode: not a variable\n");
  13465. script_reportdata(data);
  13466. st->state = END;
  13467. return SCRIPT_CMD_FAILURE;// not a variable
  13468. }
  13469. id = reference_getid(data);
  13470. name = reference_getname(data);
  13471. if( !is_string_variable(name) ) {
  13472. ShowError("script:implode: not string array\n");
  13473. script_reportdata(data);
  13474. st->state = END;
  13475. return SCRIPT_CMD_FAILURE;// data type mismatch
  13476. }
  13477. if( not_server_variable(*name) ) {
  13478. sd = script_rid2sd(st);
  13479. if( sd == NULL )
  13480. return SCRIPT_CMD_SUCCESS;// no player attached
  13481. }
  13482. //count chars
  13483. array_size = script_array_highest_key(st, sd, name, reference_getref(data)) - 1;
  13484. if(array_size == -1) { //empty array check (AmsTaff)
  13485. ShowWarning("script:implode: array length = 0\n");
  13486. output = (char*)aMalloc(sizeof(char)*5);
  13487. sprintf(output,"%s","NULL");
  13488. } else {
  13489. const char *glue = NULL, *temp;
  13490. size_t len = 0;
  13491. int i, k = 0;
  13492. for(i = 0; i <= array_size; ++i) {
  13493. temp = (char*) get_val2(st, reference_uid(id, i), reference_getref(data));
  13494. len += strlen(temp);
  13495. script_removetop(st, -1, 0);
  13496. }
  13497. //allocate mem
  13498. if( script_hasdata(st,3) ) {
  13499. glue = script_getstr(st,3);
  13500. glue_len = strlen(glue);
  13501. len += glue_len * (array_size);
  13502. }
  13503. output = (char*)aMalloc(len + 1);
  13504. //build output
  13505. for(i = 0; i < array_size; ++i) {
  13506. temp = (char*) get_val2(st, reference_uid(id, i), reference_getref(data));
  13507. len = strlen(temp);
  13508. memcpy(&output[k], temp, len);
  13509. k += len;
  13510. if(glue_len != 0) {
  13511. memcpy(&output[k], glue, glue_len);
  13512. k += glue_len;
  13513. }
  13514. script_removetop(st, -1, 0);
  13515. }
  13516. temp = (char*) get_val2(st, reference_uid(id, array_size), reference_getref(data));
  13517. len = strlen(temp);
  13518. memcpy(&output[k], temp, len);
  13519. k += len;
  13520. script_removetop(st, -1, 0);
  13521. output[k] = '\0';
  13522. }
  13523. script_pushstr(st, output);
  13524. return SCRIPT_CMD_SUCCESS;
  13525. }
  13526. //=======================================================
  13527. // sprintf(<format>, ...);
  13528. // Implements C sprintf, except format %n. The resulting string is
  13529. // returned, instead of being saved in variable by reference.
  13530. //-------------------------------------------------------
  13531. BUILDIN_FUNC(sprintf)
  13532. {
  13533. unsigned int len, argc = 0, arg = 0, buf2_len = 0;
  13534. const char* format;
  13535. char* p;
  13536. char* q;
  13537. char* buf = NULL;
  13538. char* buf2 = NULL;
  13539. struct script_data* data;
  13540. StringBuf final_buf;
  13541. // Fetch init data
  13542. format = script_getstr(st, 2);
  13543. argc = script_lastdata(st)-2;
  13544. len = strlen(format);
  13545. // Skip parsing, where no parsing is required.
  13546. if(len == 0) {
  13547. script_pushconststr(st,"");
  13548. return SCRIPT_CMD_SUCCESS;
  13549. }
  13550. // Pessimistic alloc
  13551. CREATE(buf, char, len+1);
  13552. // Need not be parsed, just solve stuff like %%.
  13553. if(argc == 0) {
  13554. memcpy(buf,format,len+1);
  13555. script_pushstrcopy(st, buf);
  13556. aFree(buf);
  13557. return SCRIPT_CMD_SUCCESS;
  13558. }
  13559. safestrncpy(buf, format, len+1);
  13560. // Issue sprintf for each parameter
  13561. StringBuf_Init(&final_buf);
  13562. q = buf;
  13563. while((p = strchr(q, '%')) != NULL) {
  13564. if(p != q) {
  13565. len = p - q + 1;
  13566. if(buf2_len < len) {
  13567. RECREATE(buf2, char, len);
  13568. buf2_len = len;
  13569. }
  13570. safestrncpy(buf2, q, len);
  13571. StringBuf_AppendStr(&final_buf, buf2);
  13572. q = p;
  13573. }
  13574. p = q + 1;
  13575. if(*p == '%') { // %%
  13576. StringBuf_AppendStr(&final_buf, "%");
  13577. q+=2;
  13578. continue;
  13579. }
  13580. if(*p == 'n') { // %n
  13581. ShowWarning("buildin_sprintf: Format %%n not supported! Skipping...\n");
  13582. script_reportsrc(st);
  13583. q+=2;
  13584. continue;
  13585. }
  13586. if(arg >= argc) {
  13587. ShowError("buildin_sprintf: Not enough arguments passed!\n");
  13588. if(buf) aFree(buf);
  13589. if(buf2) aFree(buf2);
  13590. StringBuf_Destroy(&final_buf);
  13591. script_pushconststr(st,"");
  13592. return SCRIPT_CMD_FAILURE;
  13593. }
  13594. if((p = strchr(q+1, '%')) == NULL)
  13595. p = strchr(q, 0); // EOS
  13596. len = p - q + 1;
  13597. if(buf2_len < len) {
  13598. RECREATE(buf2, char, len);
  13599. buf2_len = len;
  13600. }
  13601. safestrncpy(buf2, q, len);
  13602. q = p;
  13603. // Note: This assumes the passed value being the correct
  13604. // type to the current format specifier. If not, the server
  13605. // probably crashes or returns anything else, than expected,
  13606. // but it would behave in normal code the same way so it's
  13607. // the scripter's responsibility.
  13608. data = script_getdata(st, arg+3);
  13609. if(data_isstring(data)) // String
  13610. StringBuf_Printf(&final_buf, buf2, script_getstr(st, arg+3));
  13611. else if(data_isint(data)) // Number
  13612. StringBuf_Printf(&final_buf, buf2, script_getnum(st, arg+3));
  13613. else if(data_isreference(data)) { // Variable
  13614. char* name = reference_getname(data);
  13615. if(name[strlen(name)-1]=='$') // var Str
  13616. StringBuf_Printf(&final_buf, buf2, script_getstr(st, arg+3));
  13617. else // var Int
  13618. StringBuf_Printf(&final_buf, buf2, script_getnum(st, arg+3));
  13619. } else { // Unsupported type
  13620. ShowError("buildin_sprintf: Unknown argument type!\n");
  13621. if(buf) aFree(buf);
  13622. if(buf2) aFree(buf2);
  13623. StringBuf_Destroy(&final_buf);
  13624. script_pushconststr(st,"");
  13625. return SCRIPT_CMD_FAILURE;
  13626. }
  13627. arg++;
  13628. }
  13629. // Append anything left
  13630. if(*q)
  13631. StringBuf_AppendStr(&final_buf, q);
  13632. // Passed more, than needed
  13633. if(arg < argc) {
  13634. ShowWarning("buildin_sprintf: Unused arguments passed.\n");
  13635. script_reportsrc(st);
  13636. }
  13637. script_pushstrcopy(st, StringBuf_Value(&final_buf));
  13638. if(buf) aFree(buf);
  13639. if(buf2) aFree(buf2);
  13640. StringBuf_Destroy(&final_buf);
  13641. return SCRIPT_CMD_SUCCESS;
  13642. }
  13643. //=======================================================
  13644. // sscanf(<str>, <format>, ...);
  13645. // Implements C sscanf.
  13646. //-------------------------------------------------------
  13647. BUILDIN_FUNC(sscanf){
  13648. unsigned int argc, arg = 0, len;
  13649. struct script_data* data;
  13650. struct map_session_data* sd = NULL;
  13651. const char* str;
  13652. const char* format;
  13653. const char* p;
  13654. const char* q;
  13655. char* buf = NULL;
  13656. char* buf_p;
  13657. char* ref_str = NULL;
  13658. int ref_int;
  13659. // Get data
  13660. str = script_getstr(st, 2);
  13661. format = script_getstr(st, 3);
  13662. argc = script_lastdata(st)-3;
  13663. len = strlen(format);
  13664. if (len != 0 && strlen(str) == 0) {
  13665. // If the source string is empty but the format string is not, we return -1
  13666. // according to the C specs. (if the format string is also empty, we shall
  13667. // continue and return 0: 0 conversions took place out of the 0 attempted.)
  13668. script_pushint(st, -1);
  13669. return SCRIPT_CMD_SUCCESS;
  13670. }
  13671. CREATE(buf, char, len*2+1);
  13672. // Issue sscanf for each parameter
  13673. *buf = 0;
  13674. q = format;
  13675. while((p = strchr(q, '%'))){
  13676. if(p!=q){
  13677. strncat(buf, q, (size_t)(p-q));
  13678. q = p;
  13679. }
  13680. p = q+1;
  13681. if(*p=='*' || *p=='%'){ // Skip
  13682. strncat(buf, q, 2);
  13683. q+=2;
  13684. continue;
  13685. }
  13686. if(arg>=argc){
  13687. ShowError("buildin_sscanf: Not enough arguments passed!\n");
  13688. script_pushint(st, -1);
  13689. if(buf) aFree(buf);
  13690. if(ref_str) aFree(ref_str);
  13691. return SCRIPT_CMD_FAILURE;
  13692. }
  13693. if((p = strchr(q+1, '%'))==NULL){
  13694. p = strchr(q, 0); // EOS
  13695. }
  13696. len = p-q;
  13697. strncat(buf, q, len);
  13698. q = p;
  13699. // Validate output
  13700. data = script_getdata(st, arg+4);
  13701. if(!data_isreference(data) || !reference_tovariable(data)){
  13702. ShowError("buildin_sscanf: Target argument is not a variable!\n");
  13703. script_pushint(st, -1);
  13704. if(buf) aFree(buf);
  13705. if(ref_str) aFree(ref_str);
  13706. return SCRIPT_CMD_FAILURE;
  13707. }
  13708. buf_p = reference_getname(data);
  13709. if(not_server_variable(*buf_p) && (sd = script_rid2sd(st))==NULL){
  13710. script_pushint(st, -1);
  13711. if(buf) aFree(buf);
  13712. if(ref_str) aFree(ref_str);
  13713. return SCRIPT_CMD_SUCCESS;
  13714. }
  13715. // Save value if any
  13716. if(buf_p[strlen(buf_p)-1]=='$'){ // String
  13717. if(ref_str==NULL){
  13718. CREATE(ref_str, char, strlen(str)+1);
  13719. }
  13720. if(sscanf(str, buf, ref_str)==0){
  13721. break;
  13722. }
  13723. set_reg(st, sd, reference_uid( reference_getid(data), reference_getindex(data) ), buf_p, (void *)(ref_str), reference_getref(data));
  13724. } else { // Number
  13725. if(sscanf(str, buf, &ref_int)==0){
  13726. break;
  13727. }
  13728. set_reg(st, sd, reference_uid( reference_getid(data), reference_getindex(data) ), buf_p, (void *)__64BPRTSIZE(ref_int), reference_getref(data));
  13729. }
  13730. arg++;
  13731. // Disable used format (%... -> %*...)
  13732. buf_p = strchr(buf, 0);
  13733. memmove(buf_p-len+2, buf_p-len+1, len);
  13734. *(buf_p-len+1) = '*';
  13735. }
  13736. script_pushint(st, arg);
  13737. if(buf) aFree(buf);
  13738. if(ref_str) aFree(ref_str);
  13739. return SCRIPT_CMD_SUCCESS;
  13740. }
  13741. //=======================================================
  13742. // strpos(<haystack>, <needle>)
  13743. // strpos(<haystack>, <needle>, <offset>)
  13744. //
  13745. // Implements PHP style strpos. Adapted from code from
  13746. // http://www.daniweb.com/code/snippet313.html, Dave Sinkula
  13747. //-------------------------------------------------------
  13748. BUILDIN_FUNC(strpos) {
  13749. const char *haystack = script_getstr(st,2);
  13750. const char *needle = script_getstr(st,3);
  13751. int i;
  13752. size_t len;
  13753. if( script_hasdata(st,4) )
  13754. i = script_getnum(st,4);
  13755. else
  13756. i = 0;
  13757. if (needle[0] == '\0') {
  13758. script_pushint(st, -1);
  13759. return SCRIPT_CMD_SUCCESS;
  13760. }
  13761. len = strlen(haystack);
  13762. for ( ; i < len; ++i ) {
  13763. if ( haystack[i] == *needle ) {
  13764. // matched starting char -- loop through remaining chars
  13765. const char *h, *n;
  13766. for ( h = &haystack[i], n = needle; *h && *n; ++h, ++n ) {
  13767. if ( *h != *n ) {
  13768. break;
  13769. }
  13770. }
  13771. if ( !*n ) { // matched all of 'needle' to null termination
  13772. script_pushint(st, i);
  13773. return SCRIPT_CMD_SUCCESS;
  13774. }
  13775. }
  13776. }
  13777. script_pushint(st, -1);
  13778. return SCRIPT_CMD_SUCCESS;
  13779. }
  13780. //===============================================================
  13781. // replacestr <input>, <search>, <replace>{, <usecase>{, <count>}}
  13782. //
  13783. // Note: Finds all instances of <search> in <input> and replaces
  13784. // with <replace>. If specified will only replace as many
  13785. // instances as specified in <count>. By default will be case
  13786. // sensitive.
  13787. //---------------------------------------------------------------
  13788. BUILDIN_FUNC(replacestr)
  13789. {
  13790. const char *input = script_getstr(st, 2);
  13791. const char *find = script_getstr(st, 3);
  13792. const char *replace = script_getstr(st, 4);
  13793. size_t inputlen = strlen(input);
  13794. size_t findlen = strlen(find);
  13795. struct StringBuf output;
  13796. bool usecase = true;
  13797. int count = 0;
  13798. int numFinds = 0;
  13799. int i = 0, f = 0;
  13800. if(findlen == 0) {
  13801. ShowError("script:replacestr: Invalid search length.\n");
  13802. st->state = END;
  13803. return SCRIPT_CMD_FAILURE;
  13804. }
  13805. if(script_hasdata(st, 5)) {
  13806. struct script_data *data = script_getdata(st, 5);
  13807. get_val(st, data); // Convert into value in case of a variable
  13808. if( !data_isstring(data) )
  13809. usecase = script_getnum(st, 5) != 0;
  13810. else {
  13811. ShowError("script:replacestr: Invalid usecase value. Expected int got string\n");
  13812. st->state = END;
  13813. return SCRIPT_CMD_FAILURE;
  13814. }
  13815. }
  13816. if(script_hasdata(st, 6)) {
  13817. count = script_getnum(st, 6);
  13818. if(count == 0) {
  13819. ShowError("script:replacestr: Invalid count value. Expected int got string\n");
  13820. st->state = END;
  13821. return SCRIPT_CMD_FAILURE;
  13822. }
  13823. }
  13824. StringBuf_Init(&output);
  13825. for(; i < inputlen; i++) {
  13826. if(count && count == numFinds) { //found enough, stop looking
  13827. break;
  13828. }
  13829. for(f = 0; f <= findlen; f++) {
  13830. if(f == findlen) { //complete match
  13831. numFinds++;
  13832. StringBuf_AppendStr(&output, replace);
  13833. i += findlen - 1;
  13834. break;
  13835. } else {
  13836. if(usecase) {
  13837. if((i + f) > inputlen || input[i + f] != find[f]) {
  13838. StringBuf_Printf(&output, "%c", input[i]);
  13839. break;
  13840. }
  13841. } else {
  13842. if(((i + f) > inputlen || input[i + f] != find[f]) && TOUPPER(input[i+f]) != TOUPPER(find[f])) {
  13843. StringBuf_Printf(&output, "%c", input[i]);
  13844. break;
  13845. }
  13846. }
  13847. }
  13848. }
  13849. }
  13850. //append excess after enough found
  13851. if(i < inputlen)
  13852. StringBuf_AppendStr(&output, &(input[i]));
  13853. script_pushstrcopy(st, StringBuf_Value(&output));
  13854. StringBuf_Destroy(&output);
  13855. return SCRIPT_CMD_SUCCESS;
  13856. }
  13857. //========================================================
  13858. // countstr <input>, <search>{, <usecase>}
  13859. //
  13860. // Note: Counts the number of times <search> occurs in
  13861. // <input>. By default will be case sensitive.
  13862. //--------------------------------------------------------
  13863. BUILDIN_FUNC(countstr)
  13864. {
  13865. const char *input = script_getstr(st, 2);
  13866. const char *find = script_getstr(st, 3);
  13867. size_t inputlen = strlen(input);
  13868. size_t findlen = strlen(find);
  13869. bool usecase = true;
  13870. int numFinds = 0;
  13871. int i = 0, f = 0;
  13872. if(findlen == 0) {
  13873. ShowError("script:countstr: Invalid search length.\n");
  13874. st->state = END;
  13875. return SCRIPT_CMD_FAILURE;
  13876. }
  13877. if(script_hasdata(st, 4)) {
  13878. struct script_data *data;
  13879. data = script_getdata(st, 4);
  13880. get_val(st, data); // Convert into value in case of a variable
  13881. if( !data_isstring(data) )
  13882. usecase = script_getnum(st, 4) != 0;
  13883. else {
  13884. ShowError("script:countstr: Invalid usecase value. Expected int got string\n");
  13885. st->state = END;
  13886. return SCRIPT_CMD_FAILURE;
  13887. }
  13888. }
  13889. for(; i < inputlen; i++) {
  13890. for(f = 0; f <= findlen; f++) {
  13891. if(f == findlen) { //complete match
  13892. numFinds++;
  13893. i += findlen - 1;
  13894. break;
  13895. } else {
  13896. if(usecase) {
  13897. if((i + f) > inputlen || input[i + f] != find[f]) {
  13898. break;
  13899. }
  13900. } else {
  13901. if(((i + f) > inputlen || input[i + f] != find[f]) && TOUPPER(input[i+f]) != TOUPPER(find[f])) {
  13902. break;
  13903. }
  13904. }
  13905. }
  13906. }
  13907. }
  13908. script_pushint(st, numFinds);
  13909. return SCRIPT_CMD_SUCCESS;
  13910. }
  13911. /// Changes the display name and/or display class of the npc.
  13912. /// Returns 0 is successful, 1 if the npc does not exist.
  13913. ///
  13914. /// setnpcdisplay("<npc name>", "<new display name>", <new class id>, <new size>) -> <int>
  13915. /// setnpcdisplay("<npc name>", "<new display name>", <new class id>) -> <int>
  13916. /// setnpcdisplay("<npc name>", "<new display name>") -> <int>
  13917. /// setnpcdisplay("<npc name>", <new class id>) -> <int>
  13918. BUILDIN_FUNC(setnpcdisplay)
  13919. {
  13920. const char* name;
  13921. const char* newname = NULL;
  13922. int class_ = -1, size = -1;
  13923. struct script_data* data;
  13924. struct npc_data* nd;
  13925. name = script_getstr(st,2);
  13926. data = script_getdata(st,3);
  13927. if( script_hasdata(st,4) )
  13928. class_ = script_getnum(st,4);
  13929. if( script_hasdata(st,5) )
  13930. size = script_getnum(st,5);
  13931. get_val(st, data);
  13932. if( data_isstring(data) )
  13933. newname = conv_str(st,data);
  13934. else if( data_isint(data) )
  13935. class_ = conv_num(st,data);
  13936. else
  13937. {
  13938. ShowError("script:setnpcdisplay: expected string or number\n");
  13939. script_reportdata(data);
  13940. return SCRIPT_CMD_FAILURE;
  13941. }
  13942. nd = npc_name2id(name);
  13943. if( nd == NULL )
  13944. {// not found
  13945. script_pushint(st,1);
  13946. return SCRIPT_CMD_SUCCESS;
  13947. }
  13948. // update npc
  13949. if( newname )
  13950. npc_setdisplayname(nd, newname);
  13951. if( size != -1 && size != (int)nd->size )
  13952. nd->size = size;
  13953. else
  13954. size = -1;
  13955. if( class_ != -1 && nd->class_ != class_ )
  13956. npc_setclass(nd, class_);
  13957. else if( size != -1 )
  13958. { // Required to update the visual size
  13959. clif_clearunit_area(&nd->bl, CLR_OUTSIGHT);
  13960. clif_spawn(&nd->bl);
  13961. }
  13962. script_pushint(st,0);
  13963. return SCRIPT_CMD_SUCCESS;
  13964. }
  13965. BUILDIN_FUNC(atoi)
  13966. {
  13967. const char *value;
  13968. value = script_getstr(st,2);
  13969. script_pushint(st,atoi(value));
  13970. return SCRIPT_CMD_SUCCESS;
  13971. }
  13972. BUILDIN_FUNC(axtoi)
  13973. {
  13974. const char *hex = script_getstr(st,2);
  13975. long value = strtol(hex, NULL, 16);
  13976. #if LONG_MAX > INT_MAX || LONG_MIN < INT_MIN
  13977. value = cap_value(value, INT_MIN, INT_MAX);
  13978. #endif
  13979. script_pushint(st, (int)value);
  13980. return SCRIPT_CMD_SUCCESS;
  13981. }
  13982. BUILDIN_FUNC(strtol)
  13983. {
  13984. const char *string = script_getstr(st, 2);
  13985. int base = script_getnum(st, 3);
  13986. long value = strtol(string, NULL, base);
  13987. #if LONG_MAX > INT_MAX || LONG_MIN < INT_MIN
  13988. value = cap_value(value, INT_MIN, INT_MAX);
  13989. #endif
  13990. script_pushint(st, (int)value);
  13991. return SCRIPT_CMD_SUCCESS;
  13992. }
  13993. // case-insensitive substring search [lordalfa]
  13994. BUILDIN_FUNC(compare)
  13995. {
  13996. const char *message;
  13997. const char *cmpstring;
  13998. message = script_getstr(st,2);
  13999. cmpstring = script_getstr(st,3);
  14000. script_pushint(st,(stristr(message,cmpstring) != NULL));
  14001. return SCRIPT_CMD_SUCCESS;
  14002. }
  14003. BUILDIN_FUNC(strcmp)
  14004. {
  14005. const char *str1;
  14006. const char *str2;
  14007. str1 = script_getstr(st,2);
  14008. str2 = script_getstr(st,3);
  14009. script_pushint(st,strcmp(str1, str2));
  14010. return SCRIPT_CMD_SUCCESS;
  14011. }
  14012. // [zBuffer] List of mathematics commands --->
  14013. BUILDIN_FUNC(sqrt)
  14014. {
  14015. double i, a;
  14016. i = script_getnum(st,2);
  14017. a = sqrt(i);
  14018. script_pushint(st,(int)a);
  14019. return SCRIPT_CMD_SUCCESS;
  14020. }
  14021. BUILDIN_FUNC(pow)
  14022. {
  14023. double i, a, b;
  14024. a = script_getnum(st,2);
  14025. b = script_getnum(st,3);
  14026. i = pow(a,b);
  14027. script_pushint(st,(int)i);
  14028. return SCRIPT_CMD_SUCCESS;
  14029. }
  14030. BUILDIN_FUNC(distance)
  14031. {
  14032. int x0, y0, x1, y1;
  14033. x0 = script_getnum(st,2);
  14034. y0 = script_getnum(st,3);
  14035. x1 = script_getnum(st,4);
  14036. y1 = script_getnum(st,5);
  14037. script_pushint(st,distance_xy(x0,y0,x1,y1));
  14038. return SCRIPT_CMD_SUCCESS;
  14039. }
  14040. // <--- [zBuffer] List of mathematics commands
  14041. BUILDIN_FUNC(md5)
  14042. {
  14043. const char *tmpstr;
  14044. char *md5str;
  14045. tmpstr = script_getstr(st,2);
  14046. md5str = (char *)aMalloc((32+1)*sizeof(char));
  14047. MD5_String(tmpstr, md5str);
  14048. script_pushstr(st, md5str);
  14049. return SCRIPT_CMD_SUCCESS;
  14050. }
  14051. // [zBuffer] List of dynamic var commands --->
  14052. /**
  14053. * setd "<variable name>",<value>{,<char_id>};
  14054. **/
  14055. BUILDIN_FUNC(setd)
  14056. {
  14057. TBL_PC *sd = NULL;
  14058. char varname[100];
  14059. const char *buffer;
  14060. int elem;
  14061. buffer = script_getstr(st, 2);
  14062. if(sscanf(buffer, "%99[^[][%11d]", varname, &elem) < 2)
  14063. elem = 0;
  14064. if( not_server_variable(*varname) ) {
  14065. if (!script_charid2sd(4,sd))
  14066. return SCRIPT_CMD_FAILURE;
  14067. }
  14068. if( is_string_variable(varname) ) {
  14069. setd_sub(st, sd, varname, elem, (void *)script_getstr(st, 3), NULL);
  14070. } else {
  14071. setd_sub(st, sd, varname, elem, (void *)__64BPRTSIZE(script_getnum(st, 3)), NULL);
  14072. }
  14073. return SCRIPT_CMD_SUCCESS;
  14074. }
  14075. int buildin_query_sql_sub(struct script_state* st, Sql* handle)
  14076. {
  14077. int i, j;
  14078. TBL_PC* sd = NULL;
  14079. const char* query;
  14080. struct script_data* data;
  14081. const char* name;
  14082. unsigned int max_rows = SCRIPT_MAX_ARRAYSIZE; // maximum number of rows
  14083. int num_vars;
  14084. int num_cols;
  14085. // check target variables
  14086. for( i = 3; script_hasdata(st,i); ++i ) {
  14087. data = script_getdata(st, i);
  14088. if( data_isreference(data) ) { // it's a variable
  14089. name = reference_getname(data);
  14090. if( not_server_variable(*name) && sd == NULL ) { // requires a player
  14091. sd = script_rid2sd(st);
  14092. if( sd == NULL ) { // no player attached
  14093. script_reportdata(data);
  14094. st->state = END;
  14095. return SCRIPT_CMD_FAILURE;
  14096. }
  14097. }
  14098. } else {
  14099. ShowError("script:query_sql: not a variable\n");
  14100. script_reportdata(data);
  14101. st->state = END;
  14102. return SCRIPT_CMD_FAILURE;
  14103. }
  14104. }
  14105. num_vars = i - 3;
  14106. // Execute the query
  14107. query = script_getstr(st,2);
  14108. if( SQL_ERROR == Sql_QueryStr(handle, query) ) {
  14109. Sql_ShowDebug(handle);
  14110. script_pushint(st, -1);
  14111. return SCRIPT_CMD_FAILURE;
  14112. }
  14113. if( Sql_NumRows(handle) == 0 ) { // No data received
  14114. Sql_FreeResult(handle);
  14115. script_pushint(st, 0);
  14116. return SCRIPT_CMD_SUCCESS;
  14117. }
  14118. // Count the number of columns to store
  14119. num_cols = Sql_NumColumns(handle);
  14120. if( num_vars < num_cols ) {
  14121. ShowWarning("script:query_sql: Too many columns, discarding last %u columns.\n", (unsigned int)(num_cols-num_vars));
  14122. script_reportsrc(st);
  14123. } else if( num_vars > num_cols ) {
  14124. ShowWarning("script:query_sql: Too many variables (%u extra).\n", (unsigned int)(num_vars-num_cols));
  14125. script_reportsrc(st);
  14126. }
  14127. // Store data
  14128. for( i = 0; i < max_rows && SQL_SUCCESS == Sql_NextRow(handle); ++i ) {
  14129. for( j = 0; j < num_vars; ++j ) {
  14130. char* str = NULL;
  14131. if( j < num_cols )
  14132. Sql_GetData(handle, j, &str, NULL);
  14133. data = script_getdata(st, j+3);
  14134. name = reference_getname(data);
  14135. if( is_string_variable(name) )
  14136. setd_sub(st, sd, name, i, (void *)(str?str:""), reference_getref(data));
  14137. else
  14138. setd_sub(st, sd, name, i, (void *)__64BPRTSIZE((str?atoi(str):0)), reference_getref(data));
  14139. }
  14140. }
  14141. if( i == max_rows && max_rows < Sql_NumRows(handle) ) {
  14142. ShowWarning("script:query_sql: Only %d/%u rows have been stored.\n", max_rows, (unsigned int)Sql_NumRows(handle));
  14143. script_reportsrc(st);
  14144. }
  14145. // Free data
  14146. Sql_FreeResult(handle);
  14147. script_pushint(st, i);
  14148. return SCRIPT_CMD_SUCCESS;
  14149. }
  14150. BUILDIN_FUNC(query_sql) {
  14151. #ifdef BETA_THREAD_TEST
  14152. if( st->state != RERUNLINE ) {
  14153. queryThread_add(st,false);
  14154. st->state = RERUNLINE;/* will continue when the query is finished running. */
  14155. } else
  14156. st->state = RUN;
  14157. return SCRIPT_CMD_SUCCESS;
  14158. #else
  14159. return buildin_query_sql_sub(st, qsmysql_handle);
  14160. #endif
  14161. }
  14162. BUILDIN_FUNC(query_logsql) {
  14163. if( !log_config.sql_logs ) {// logmysql_handle == NULL
  14164. ShowWarning("buildin_query_logsql: SQL logs are disabled, query '%s' will not be executed.\n", script_getstr(st,2));
  14165. script_pushint(st,-1);
  14166. return SCRIPT_CMD_FAILURE;
  14167. }
  14168. #ifdef BETA_THREAD_TEST
  14169. if( st->state != RERUNLINE ) {
  14170. queryThread_add(st,true);
  14171. st->state = RERUNLINE;/* will continue when the query is finished running. */
  14172. } else
  14173. st->state = RUN;
  14174. return SCRIPT_CMD_SUCCESS;
  14175. #else
  14176. return buildin_query_sql_sub(st, logmysql_handle);
  14177. #endif
  14178. }
  14179. //Allows escaping of a given string.
  14180. BUILDIN_FUNC(escape_sql)
  14181. {
  14182. const char *str;
  14183. char *esc_str;
  14184. size_t len;
  14185. str = script_getstr(st,2);
  14186. len = strlen(str);
  14187. esc_str = (char*)aMalloc(len*2+1);
  14188. Sql_EscapeStringLen(mmysql_handle, esc_str, str, len);
  14189. script_pushstr(st, esc_str);
  14190. return SCRIPT_CMD_SUCCESS;
  14191. }
  14192. BUILDIN_FUNC(getd)
  14193. {
  14194. char varname[100];
  14195. const char *buffer;
  14196. int elem;
  14197. buffer = script_getstr(st, 2);
  14198. if(sscanf(buffer, "%99[^[][%11d]", varname, &elem) < 2)
  14199. elem = 0;
  14200. // Push the 'pointer' so it's more flexible [Lance]
  14201. push_val(st->stack, C_NAME, reference_uid(add_str(varname), elem));
  14202. return SCRIPT_CMD_SUCCESS;
  14203. }
  14204. BUILDIN_FUNC(callshop)
  14205. {
  14206. TBL_PC *sd = NULL;
  14207. struct npc_data *nd;
  14208. const char *shopname;
  14209. int flag = 0;
  14210. sd = script_rid2sd(st);
  14211. if (!sd) {
  14212. script_pushint(st,0);
  14213. return SCRIPT_CMD_SUCCESS;
  14214. }
  14215. shopname = script_getstr(st, 2);
  14216. if (script_hasdata(st,3))
  14217. flag = script_getnum(st,3);
  14218. nd = npc_name2id(shopname);
  14219. 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) ) {
  14220. ShowError("buildin_callshop: Shop [%s] not found (or NPC is not shop type)\n", shopname);
  14221. script_pushint(st,0);
  14222. return SCRIPT_CMD_FAILURE;
  14223. }
  14224. if (nd->subtype == NPCTYPE_SHOP) {
  14225. // flag the user as using a valid script call for opening the shop (for floating NPCs)
  14226. sd->state.callshop = 1;
  14227. switch (flag) {
  14228. case 1: npc_buysellsel(sd,nd->bl.id,0); break; //Buy window
  14229. case 2: npc_buysellsel(sd,nd->bl.id,1); break; //Sell window
  14230. default: clif_npcbuysell(sd,nd->bl.id); break; //Show menu
  14231. }
  14232. }
  14233. #if PACKETVER >= 20131223
  14234. else if (nd->subtype == NPCTYPE_MARKETSHOP) {
  14235. unsigned short i;
  14236. for (i = 0; i < nd->u.shop.count; i++) {
  14237. if (nd->u.shop.shop_item[i].qty)
  14238. break;
  14239. }
  14240. if (i == nd->u.shop.count) {
  14241. clif_messagecolor(&sd->bl, color_table[COLOR_RED], msg_txt(sd, 534), false, SELF);
  14242. return SCRIPT_CMD_FAILURE;
  14243. }
  14244. sd->npc_shopid = nd->bl.id;
  14245. clif_npc_market_open(sd, nd);
  14246. script_pushint(st,1);
  14247. return SCRIPT_CMD_SUCCESS;
  14248. }
  14249. #endif
  14250. else
  14251. clif_cashshop_show(sd, nd);
  14252. sd->npc_shopid = nd->bl.id;
  14253. script_pushint(st,1);
  14254. return SCRIPT_CMD_SUCCESS;
  14255. }
  14256. BUILDIN_FUNC(npcshopitem)
  14257. {
  14258. const char* npcname = script_getstr(st, 2);
  14259. struct npc_data* nd = npc_name2id(npcname);
  14260. int n, i;
  14261. int amount;
  14262. uint16 offs = 2;
  14263. if( !nd || ( nd->subtype != NPCTYPE_SHOP && nd->subtype != NPCTYPE_CASHSHOP && nd->subtype != NPCTYPE_ITEMSHOP && nd->subtype != NPCTYPE_POINTSHOP && nd->subtype != NPCTYPE_MARKETSHOP ) ) { // Not found.
  14264. script_pushint(st,0);
  14265. return SCRIPT_CMD_SUCCESS;
  14266. }
  14267. #if PACKETVER >= 20131223
  14268. if (nd->subtype == NPCTYPE_MARKETSHOP) {
  14269. offs = 3;
  14270. npc_market_delfromsql_(nd->exname, 0, true);
  14271. }
  14272. #endif
  14273. // get the count of new entries
  14274. amount = (script_lastdata(st)-2)/offs;
  14275. // generate new shop item list
  14276. RECREATE(nd->u.shop.shop_item, struct npc_item_list, amount);
  14277. for (n = 0, i = 3; n < amount; n++, i+=offs) {
  14278. nd->u.shop.shop_item[n].nameid = script_getnum(st,i);
  14279. nd->u.shop.shop_item[n].value = script_getnum(st,i+1);
  14280. #if PACKETVER >= 20131223
  14281. if (nd->subtype == NPCTYPE_MARKETSHOP) {
  14282. nd->u.shop.shop_item[n].qty = script_getnum(st,i+2);
  14283. nd->u.shop.shop_item[n].flag = 1;
  14284. npc_market_tosql(nd->exname, &nd->u.shop.shop_item[n]);
  14285. }
  14286. #endif
  14287. }
  14288. nd->u.shop.count = n;
  14289. script_pushint(st,1);
  14290. return SCRIPT_CMD_SUCCESS;
  14291. }
  14292. BUILDIN_FUNC(npcshopadditem)
  14293. {
  14294. const char* npcname = script_getstr(st,2);
  14295. struct npc_data* nd = npc_name2id(npcname);
  14296. int n, i;
  14297. uint16 offs = 2, amount;
  14298. if (!nd || ( nd->subtype != NPCTYPE_SHOP && nd->subtype != NPCTYPE_CASHSHOP && nd->subtype != NPCTYPE_ITEMSHOP && nd->subtype != NPCTYPE_POINTSHOP && nd->subtype != NPCTYPE_MARKETSHOP)) { // Not found.
  14299. script_pushint(st,0);
  14300. return SCRIPT_CMD_SUCCESS;
  14301. }
  14302. if (nd->subtype == NPCTYPE_MARKETSHOP)
  14303. offs = 3;
  14304. // get the count of new entries
  14305. amount = (script_lastdata(st)-2)/offs;
  14306. #if PACKETVER >= 20131223
  14307. if (nd->subtype == NPCTYPE_MARKETSHOP) {
  14308. for (n = 0, i = 3; n < amount; n++, i += offs) {
  14309. uint16 nameid = script_getnum(st,i), j;
  14310. // Check existing entries
  14311. ARR_FIND(0, nd->u.shop.count, j, nd->u.shop.shop_item[j].nameid == nameid);
  14312. if (j == nd->u.shop.count) {
  14313. RECREATE(nd->u.shop.shop_item, struct npc_item_list, nd->u.shop.count+1);
  14314. j = nd->u.shop.count;
  14315. nd->u.shop.shop_item[j].flag = 1;
  14316. nd->u.shop.count++;
  14317. }
  14318. nd->u.shop.shop_item[j].nameid = nameid;
  14319. nd->u.shop.shop_item[j].value = script_getnum(st,i+1);
  14320. nd->u.shop.shop_item[j].qty = script_getnum(st,i+2);
  14321. npc_market_tosql(nd->exname, &nd->u.shop.shop_item[j]);
  14322. }
  14323. script_pushint(st,1);
  14324. return SCRIPT_CMD_SUCCESS;
  14325. }
  14326. #endif
  14327. // append new items to existing shop item list
  14328. RECREATE(nd->u.shop.shop_item, struct npc_item_list, nd->u.shop.count+amount);
  14329. for (n = nd->u.shop.count, i = 3; n < nd->u.shop.count+amount; n++, i+=offs)
  14330. {
  14331. nd->u.shop.shop_item[n].nameid = script_getnum(st,i);
  14332. nd->u.shop.shop_item[n].value = script_getnum(st,i+1);
  14333. }
  14334. nd->u.shop.count = n;
  14335. script_pushint(st,1);
  14336. return SCRIPT_CMD_SUCCESS;
  14337. }
  14338. BUILDIN_FUNC(npcshopdelitem)
  14339. {
  14340. const char* npcname = script_getstr(st,2);
  14341. struct npc_data* nd = npc_name2id(npcname);
  14342. int n, i, size;
  14343. unsigned short amount;
  14344. if (!nd || ( nd->subtype != NPCTYPE_SHOP && nd->subtype != NPCTYPE_CASHSHOP && nd->subtype != NPCTYPE_ITEMSHOP && nd->subtype != NPCTYPE_POINTSHOP && nd->subtype != NPCTYPE_MARKETSHOP)) { // Not found.
  14345. script_pushint(st,0);
  14346. return SCRIPT_CMD_SUCCESS;
  14347. }
  14348. amount = script_lastdata(st)-2;
  14349. size = nd->u.shop.count;
  14350. // remove specified items from the shop item list
  14351. for( i = 3; i < 3 + amount; i++ ) {
  14352. unsigned short nameid = script_getnum(st,i);
  14353. ARR_FIND( 0, size, n, nd->u.shop.shop_item[n].nameid == nameid );
  14354. if( n < size ) {
  14355. if (n+1 != size)
  14356. memmove(&nd->u.shop.shop_item[n], &nd->u.shop.shop_item[n+1], sizeof(nd->u.shop.shop_item[0])*(size-n));
  14357. #if PACKETVER >= 20131223
  14358. if (nd->subtype == NPCTYPE_MARKETSHOP)
  14359. npc_market_delfromsql_(nd->exname, nameid, false);
  14360. #endif
  14361. size--;
  14362. }
  14363. }
  14364. RECREATE(nd->u.shop.shop_item, struct npc_item_list, size);
  14365. nd->u.shop.count = size;
  14366. script_pushint(st,1);
  14367. return SCRIPT_CMD_SUCCESS;
  14368. }
  14369. /**
  14370. * Sets a script to attach to a shop npc.
  14371. * npcshopattach "<npc_name>";
  14372. **/
  14373. BUILDIN_FUNC(npcshopattach)
  14374. {
  14375. const char* npcname = script_getstr(st,2);
  14376. struct npc_data* nd = npc_name2id(npcname);
  14377. int flag = 1;
  14378. if( script_hasdata(st,3) )
  14379. flag = script_getnum(st,3);
  14380. if (!nd || ( nd->subtype != NPCTYPE_SHOP && nd->subtype != NPCTYPE_CASHSHOP && nd->subtype != NPCTYPE_ITEMSHOP && nd->subtype != NPCTYPE_POINTSHOP && nd->subtype != NPCTYPE_MARKETSHOP)) { // Not found.
  14381. script_pushint(st,0);
  14382. return SCRIPT_CMD_SUCCESS;
  14383. }
  14384. if (flag)
  14385. nd->master_nd = ((struct npc_data *)map_id2bl(st->oid));
  14386. else
  14387. nd->master_nd = NULL;
  14388. script_pushint(st,1);
  14389. return SCRIPT_CMD_SUCCESS;
  14390. }
  14391. /*==========================================
  14392. * Returns some values of an item [Lupus]
  14393. * Price, Weight, etc...
  14394. setitemscript(itemID,"{new item bonus script}",[n]);
  14395. Where n:
  14396. 0 - script
  14397. 1 - Equip script
  14398. 2 - Unequip script
  14399. *------------------------------------------*/
  14400. BUILDIN_FUNC(setitemscript)
  14401. {
  14402. unsigned short item_id;
  14403. int n = 0;
  14404. const char *script;
  14405. struct item_data *i_data;
  14406. struct script_code **dstscript;
  14407. item_id = script_getnum(st,2);
  14408. script = script_getstr(st,3);
  14409. if( script_hasdata(st,4) )
  14410. n=script_getnum(st,4);
  14411. i_data = itemdb_exists(item_id);
  14412. if (!i_data || script==NULL || ( script[0] && script[0]!='{' )) {
  14413. script_pushint(st,0);
  14414. return SCRIPT_CMD_SUCCESS;
  14415. }
  14416. switch (n) {
  14417. case 2:
  14418. dstscript = &i_data->unequip_script;
  14419. break;
  14420. case 1:
  14421. dstscript = &i_data->equip_script;
  14422. break;
  14423. default:
  14424. dstscript = &i_data->script;
  14425. break;
  14426. }
  14427. if(*dstscript)
  14428. script_free_code(*dstscript);
  14429. *dstscript = script[0] ? parse_script(script, "script_setitemscript", 0, 0) : NULL;
  14430. script_pushint(st,1);
  14431. return SCRIPT_CMD_SUCCESS;
  14432. }
  14433. /*=======================================================
  14434. * Add or Update a mob drop temporarily [Akinari]
  14435. * Original Idea By: [Lupus]
  14436. *
  14437. * addmonsterdrop <mob_id or name>,<item_id>,<rate>;
  14438. *
  14439. * If given an item the mob already drops, the rate
  14440. * is updated to the new rate. Rate cannot exceed 10000
  14441. * Returns 1 if succeeded (added/updated a mob drop)
  14442. *-------------------------------------------------------*/
  14443. BUILDIN_FUNC(addmonsterdrop)
  14444. {
  14445. struct mob_db *mob;
  14446. struct script_data *data;
  14447. unsigned short item_id;
  14448. int rate;
  14449. data = script_getdata(st, 2);
  14450. get_val(st, data); // Convert into value in case of a variable
  14451. if(data_isstring(data))
  14452. mob = mob_db(mobdb_searchname(script_getstr(st,2)));
  14453. else
  14454. mob = mob_db(script_getnum(st,2));
  14455. item_id=script_getnum(st,3);
  14456. rate=script_getnum(st,4);
  14457. if(!itemdb_exists(item_id)){
  14458. ShowError("addmonsterdrop: Nonexistant item %hu requested.\n", item_id );
  14459. return SCRIPT_CMD_FAILURE;
  14460. }
  14461. if(mob) { //We got a valid monster, check for available drop slot
  14462. unsigned char i, c = 0;
  14463. for(i = 0; i < MAX_MOB_DROP; i++) {
  14464. if(mob->dropitem[i].nameid) {
  14465. if(mob->dropitem[i].nameid == item_id) { //If it equals item_id we update that drop
  14466. c = i;
  14467. break;
  14468. }
  14469. continue;
  14470. }
  14471. if(!c) //Accept first available slot only
  14472. c = i;
  14473. }
  14474. if(c) { //Fill in the slot with the item and rate
  14475. mob->dropitem[c].nameid = item_id;
  14476. mob->dropitem[c].p = (rate > 10000)?10000:rate;
  14477. script_pushint(st,1);
  14478. } else //No place to put the new drop
  14479. script_pushint(st,0);
  14480. } else {
  14481. ShowWarning("addmonsterdrop: bad mob id given %d\n",script_getnum(st,2));
  14482. return SCRIPT_CMD_FAILURE;
  14483. }
  14484. return SCRIPT_CMD_SUCCESS;
  14485. }
  14486. /*=======================================================
  14487. * Delete a mob drop temporarily [Akinari]
  14488. * Original Idea By: [Lupus]
  14489. *
  14490. * delmonsterdrop <mob_id or name>,<item_id>;
  14491. *
  14492. * Returns 1 if succeeded (deleted a mob drop)
  14493. *-------------------------------------------------------*/
  14494. BUILDIN_FUNC(delmonsterdrop)
  14495. {
  14496. struct mob_db *mob;
  14497. struct script_data *data;
  14498. unsigned short item_id;
  14499. data = script_getdata(st, 2);
  14500. get_val(st, data); // Convert into value in case of a variable
  14501. if(data_isstring(data))
  14502. mob = mob_db(mobdb_searchname(script_getstr(st,2)));
  14503. else
  14504. mob = mob_db(script_getnum(st,2));
  14505. item_id=script_getnum(st,3);
  14506. if(!itemdb_exists(item_id)){
  14507. ShowError("delmonsterdrop: Nonexistant item %hu requested.\n", item_id );
  14508. return SCRIPT_CMD_FAILURE;
  14509. }
  14510. if(mob) { //We got a valid monster, check for item drop on monster
  14511. unsigned char i;
  14512. for(i = 0; i < MAX_MOB_DROP; i++) {
  14513. if(mob->dropitem[i].nameid == item_id) {
  14514. mob->dropitem[i].nameid = 0;
  14515. mob->dropitem[i].p = 0;
  14516. script_pushint(st,1);
  14517. return SCRIPT_CMD_SUCCESS;
  14518. }
  14519. }
  14520. //No drop on that monster
  14521. script_pushint(st,0);
  14522. } else {
  14523. ShowWarning("delmonsterdrop: bad mob id given %d\n",script_getnum(st,2));
  14524. return SCRIPT_CMD_FAILURE;
  14525. }
  14526. return SCRIPT_CMD_SUCCESS;
  14527. }
  14528. /*==========================================
  14529. * Returns some values of a monster [Lupus]
  14530. * Name, Level, race, size, etc...
  14531. getmonsterinfo(monsterID,queryIndex);
  14532. *------------------------------------------*/
  14533. BUILDIN_FUNC(getmonsterinfo)
  14534. {
  14535. struct mob_db *mob;
  14536. int mob_id;
  14537. mob_id = script_getnum(st,2);
  14538. if (!mobdb_checkid(mob_id)) {
  14539. //ShowError("buildin_getmonsterinfo: Wrong Monster ID: %i\n", mob_id);
  14540. if ( script_getnum(st,3) == MOB_NAME ) // requested the name
  14541. script_pushconststr(st,"null");
  14542. else
  14543. script_pushint(st,-1);
  14544. return SCRIPT_CMD_SUCCESS;
  14545. }
  14546. mob = mob_db(mob_id);
  14547. switch ( script_getnum(st,3) ) {
  14548. case MOB_NAME: script_pushstrcopy(st,mob->jname); break;
  14549. case MOB_LV: script_pushint(st,mob->lv); break;
  14550. case MOB_MAXHP: script_pushint(st,mob->status.max_hp); break;
  14551. case MOB_BASEEXP: script_pushint(st,mob->base_exp); break;
  14552. case MOB_JOBEXP: script_pushint(st,mob->job_exp); break;
  14553. case MOB_ATK1: script_pushint(st,mob->status.rhw.atk); break;
  14554. case MOB_ATK2: script_pushint(st,mob->status.rhw.atk2); break;
  14555. case MOB_DEF: script_pushint(st,mob->status.def); break;
  14556. case MOB_MDEF: script_pushint(st,mob->status.mdef); break;
  14557. case MOB_STR: script_pushint(st,mob->status.str); break;
  14558. case MOB_AGI: script_pushint(st,mob->status.agi); break;
  14559. case MOB_VIT: script_pushint(st,mob->status.vit); break;
  14560. case MOB_INT: script_pushint(st,mob->status.int_); break;
  14561. case MOB_DEX: script_pushint(st,mob->status.dex); break;
  14562. case MOB_LUK: script_pushint(st,mob->status.luk); break;
  14563. case MOB_RANGE: script_pushint(st,mob->status.rhw.range); break;
  14564. case MOB_RANGE2: script_pushint(st,mob->range2); break;
  14565. case MOB_RANGE3: script_pushint(st,mob->range3); break;
  14566. case MOB_SIZE: script_pushint(st,mob->status.size); break;
  14567. case MOB_RACE: script_pushint(st,mob->status.race); break;
  14568. case MOB_ELEMENT: script_pushint(st,mob->status.def_ele); break;
  14569. case MOB_MODE: script_pushint(st,mob->status.mode); break;
  14570. case MOB_MVPEXP: script_pushint(st,mob->mexp); break;
  14571. default: script_pushint(st,-1); //wrong Index
  14572. }
  14573. return SCRIPT_CMD_SUCCESS;
  14574. }
  14575. /**
  14576. * Check player's vending/buyingstore/autotrade state
  14577. * checkvending({"<Player Name>"})
  14578. * @author [Nab4]
  14579. */
  14580. BUILDIN_FUNC(checkvending) {
  14581. TBL_PC *sd = NULL;
  14582. if (!script_nick2sd(2,sd) ) {
  14583. script_pushint(st,0);
  14584. return SCRIPT_CMD_SUCCESS;
  14585. }
  14586. else {
  14587. int8 ret = 0;
  14588. if (sd->state.vending)
  14589. ret = 1;
  14590. else if (sd->state.buyingstore)
  14591. ret = 4;
  14592. if (sd->state.autotrade)
  14593. ret |= 2;
  14594. script_pushint(st, ret);
  14595. }
  14596. return SCRIPT_CMD_SUCCESS;
  14597. }
  14598. BUILDIN_FUNC(checkchatting) // check chatting [Marka]
  14599. {
  14600. TBL_PC *sd = NULL;
  14601. if( script_nick2sd(2,sd) )
  14602. script_pushint(st,(sd->chatID != 0));
  14603. else
  14604. script_pushint(st,0);
  14605. return SCRIPT_CMD_SUCCESS;
  14606. }
  14607. BUILDIN_FUNC(checkidle)
  14608. {
  14609. TBL_PC *sd = NULL;
  14610. if( script_nick2sd(2,sd) )
  14611. script_pushint(st, DIFF_TICK(last_tick, sd->idletime));
  14612. else
  14613. script_pushint(st, 0);
  14614. return SCRIPT_CMD_SUCCESS;
  14615. }
  14616. BUILDIN_FUNC(searchitem)
  14617. {
  14618. struct script_data* data = script_getdata(st, 2);
  14619. const char *itemname = script_getstr(st,3);
  14620. struct item_data *items[MAX_SEARCH];
  14621. int count;
  14622. char* name;
  14623. int32 start;
  14624. int32 id;
  14625. int32 i;
  14626. TBL_PC* sd = NULL;
  14627. if ((items[0] = itemdb_exists(atoi(itemname))))
  14628. count = 1;
  14629. else
  14630. count = itemdb_searchname_array(items, ARRAYLENGTH(items), itemname);
  14631. if (!count) {
  14632. script_pushint(st, 0);
  14633. return SCRIPT_CMD_SUCCESS;
  14634. }
  14635. if( !data_isreference(data) )
  14636. {
  14637. ShowError("script:searchitem: not a variable\n");
  14638. script_reportdata(data);
  14639. st->state = END;
  14640. return SCRIPT_CMD_FAILURE;// not a variable
  14641. }
  14642. id = reference_getid(data);
  14643. start = reference_getindex(data);
  14644. name = reference_getname(data);
  14645. if( not_server_variable(*name) )
  14646. {
  14647. sd = script_rid2sd(st);
  14648. if( sd == NULL )
  14649. return SCRIPT_CMD_SUCCESS;// no player attached
  14650. }
  14651. if( is_string_variable(name) )
  14652. {// string array
  14653. ShowError("script:searchitem: not an integer array reference\n");
  14654. script_reportdata(data);
  14655. st->state = END;
  14656. return SCRIPT_CMD_FAILURE;// not supported
  14657. }
  14658. for( i = 0; i < count; ++start, ++i )
  14659. {// Set array
  14660. void* v = (void*)__64BPRTSIZE((int)items[i]->nameid);
  14661. set_reg(st, sd, reference_uid(id, start), name, v, reference_getref(data));
  14662. }
  14663. script_pushint(st, count);
  14664. return SCRIPT_CMD_SUCCESS;
  14665. }
  14666. // [zBuffer] List of player cont commands --->
  14667. BUILDIN_FUNC(rid2name)
  14668. {
  14669. struct block_list *bl = NULL;
  14670. int rid = script_getnum(st,2);
  14671. if((bl = map_id2bl(rid)))
  14672. {
  14673. switch(bl->type) {
  14674. case BL_MOB: script_pushstrcopy(st,((TBL_MOB*)bl)->name); break;
  14675. case BL_PC: script_pushstrcopy(st,((TBL_PC*)bl)->status.name); break;
  14676. case BL_NPC: script_pushstrcopy(st,((TBL_NPC*)bl)->exname); break;
  14677. case BL_PET: script_pushstrcopy(st,((TBL_PET*)bl)->pet.name); break;
  14678. case BL_HOM: script_pushstrcopy(st,((TBL_HOM*)bl)->homunculus.name); break;
  14679. case BL_MER: script_pushstrcopy(st,((TBL_MER*)bl)->db->name); break;
  14680. default:
  14681. ShowError("buildin_rid2name: BL type unknown.\n");
  14682. script_pushconststr(st,"");
  14683. break;
  14684. }
  14685. } else {
  14686. ShowError("buildin_rid2name: invalid RID\n");
  14687. script_pushconststr(st,"(null)");
  14688. }
  14689. return SCRIPT_CMD_SUCCESS;
  14690. }
  14691. /**
  14692. * Toggle a unit from moving.
  14693. * pcblockmove(<unit_id>,<option>);
  14694. */
  14695. BUILDIN_FUNC(pcblockmove)
  14696. {
  14697. struct block_list *bl = NULL;
  14698. if (script_getnum(st, 2))
  14699. bl = map_id2bl(script_getnum(st,2));
  14700. else
  14701. bl = map_id2bl(st->rid);
  14702. if (bl) {
  14703. struct unit_data *ud = unit_bl2ud(bl);
  14704. if (ud)
  14705. ud->state.blockedmove = script_getnum(st,3) > 0;
  14706. }
  14707. return SCRIPT_CMD_SUCCESS;
  14708. }
  14709. /**
  14710. * Toggle a unit from casting skills.
  14711. * pcblockskill(<unit_id>,<option>);
  14712. */
  14713. BUILDIN_FUNC(pcblockskill)
  14714. {
  14715. struct block_list *bl = NULL;
  14716. if (script_getnum(st, 2))
  14717. bl = map_id2bl(script_getnum(st,2));
  14718. else
  14719. bl = map_id2bl(st->rid);
  14720. if (bl) {
  14721. struct unit_data *ud = unit_bl2ud(bl);
  14722. if (ud)
  14723. ud->state.blockedskill = script_getnum(st, 3) > 0;
  14724. }
  14725. return SCRIPT_CMD_SUCCESS;
  14726. }
  14727. BUILDIN_FUNC(pcfollow)
  14728. {
  14729. int id, targetid;
  14730. TBL_PC *sd = NULL;
  14731. id = script_getnum(st,2);
  14732. targetid = script_getnum(st,3);
  14733. if(id)
  14734. sd = map_id2sd(id);
  14735. else
  14736. sd = script_rid2sd(st);
  14737. if(sd)
  14738. pc_follow(sd, targetid);
  14739. return SCRIPT_CMD_SUCCESS;
  14740. }
  14741. BUILDIN_FUNC(pcstopfollow)
  14742. {
  14743. int id;
  14744. TBL_PC *sd = NULL;
  14745. id = script_getnum(st,2);
  14746. if(id)
  14747. sd = map_id2sd(id);
  14748. else
  14749. sd = script_rid2sd(st);
  14750. if(sd)
  14751. pc_stop_following(sd);
  14752. return SCRIPT_CMD_SUCCESS;
  14753. }
  14754. // <--- [zBuffer] List of player cont commands
  14755. // [zBuffer] List of unit control commands --->
  14756. /// Checks to see if the unit exists.
  14757. ///
  14758. /// unitexists <unit id>;
  14759. BUILDIN_FUNC(unitexists)
  14760. {
  14761. struct block_list* bl;
  14762. bl = map_id2bl(script_getnum(st, 2));
  14763. if (!bl)
  14764. script_pushint(st, false);
  14765. else
  14766. script_pushint(st, true);
  14767. return SCRIPT_CMD_SUCCESS;
  14768. }
  14769. /// Gets the type of the given Game ID.
  14770. ///
  14771. /// getunittype <unit id>;
  14772. BUILDIN_FUNC(getunittype)
  14773. {
  14774. struct block_list* bl;
  14775. uint8 value = 0;
  14776. if(!script_rid2bl(2,bl))
  14777. {
  14778. script_pushint(st, -1);
  14779. return SCRIPT_CMD_FAILURE;
  14780. }
  14781. switch (bl->type) {
  14782. case BL_PC: value = UNITTYPE_PC; break;
  14783. case BL_NPC: value = UNITTYPE_NPC; break;
  14784. case BL_PET: value = UNITTYPE_PET; break;
  14785. case BL_MOB: value = UNITTYPE_MOB; break;
  14786. case BL_HOM: value = UNITTYPE_HOM; break;
  14787. case BL_MER: value = UNITTYPE_MER; break;
  14788. case BL_ELEM: value = UNITTYPE_ELEM; break;
  14789. default: value = -1; break;
  14790. }
  14791. script_pushint(st, value);
  14792. return SCRIPT_CMD_SUCCESS;
  14793. }
  14794. /// Gets specific live information of a bl.
  14795. ///
  14796. /// getunitdata <unit id>,<arrayname>;
  14797. BUILDIN_FUNC(getunitdata)
  14798. {
  14799. TBL_PC *sd = st->rid ? map_id2sd(st->rid) : NULL;
  14800. struct block_list* bl;
  14801. TBL_MOB* md = NULL;
  14802. TBL_HOM* hd = NULL;
  14803. TBL_MER* mc = NULL;
  14804. TBL_PET* pd = NULL;
  14805. TBL_ELEM* ed = NULL;
  14806. TBL_NPC* nd = NULL;
  14807. char* name;
  14808. struct script_data *data = script_getdata(st, 3);
  14809. if (!data_isreference(data)) {
  14810. ShowWarning("buildin_getunitdata: Error in argument! Please give a variable to store values in.\n");
  14811. return SCRIPT_CMD_FAILURE;
  14812. }
  14813. if(!script_rid2bl(2,bl))
  14814. {
  14815. script_pushint(st, -1);
  14816. return SCRIPT_CMD_FAILURE;
  14817. }
  14818. switch (bl->type) {
  14819. case BL_MOB: md = map_id2md(bl->id); break;
  14820. case BL_HOM: hd = map_id2hd(bl->id); break;
  14821. case BL_PET: pd = map_id2pd(bl->id); break;
  14822. case BL_MER: mc = map_id2mc(bl->id); break;
  14823. case BL_ELEM: ed = map_id2ed(bl->id); break;
  14824. case BL_NPC: nd = map_id2nd(bl->id); break;
  14825. }
  14826. name = reference_getname(data);
  14827. #define getunitdata_sub(idx__,var__) setd_sub(st,sd,name,(idx__),(void *)__64BPRTSIZE((int)(var__)),data->ref)
  14828. switch(bl->type) {
  14829. case BL_MOB:
  14830. if (!md) {
  14831. ShowWarning("buildin_getunitdata: Error in finding object BL_MOB!\n");
  14832. return SCRIPT_CMD_FAILURE;
  14833. }
  14834. getunitdata_sub(UMOB_SIZE, md->status.size);
  14835. getunitdata_sub(UMOB_LEVEL, md->level);
  14836. getunitdata_sub(UMOB_HP, md->status.hp);
  14837. getunitdata_sub(UMOB_MAXHP, md->status.max_hp);
  14838. getunitdata_sub(UMOB_MASTERAID, md->master_id);
  14839. getunitdata_sub(UMOB_MAPID, md->bl.m);
  14840. getunitdata_sub(UMOB_X, md->bl.x);
  14841. getunitdata_sub(UMOB_Y, md->bl.y);
  14842. getunitdata_sub(UMOB_SPEED, md->status.speed);
  14843. getunitdata_sub(UMOB_MODE, md->status.mode);
  14844. getunitdata_sub(UMOB_AI, md->special_state.ai);
  14845. getunitdata_sub(UMOB_SCOPTION, md->sc.option);
  14846. getunitdata_sub(UMOB_SEX, md->vd->sex);
  14847. getunitdata_sub(UMOB_CLASS, md->vd->class_);
  14848. getunitdata_sub(UMOB_HAIRSTYLE, md->vd->hair_style);
  14849. getunitdata_sub(UMOB_HAIRCOLOR, md->vd->hair_color);
  14850. getunitdata_sub(UMOB_HEADBOTTOM, md->vd->head_bottom);
  14851. getunitdata_sub(UMOB_HEADMIDDLE, md->vd->head_mid);
  14852. getunitdata_sub(UMOB_HEADTOP, md->vd->head_top);
  14853. getunitdata_sub(UMOB_CLOTHCOLOR, md->vd->cloth_color);
  14854. getunitdata_sub(UMOB_SHIELD, md->vd->shield);
  14855. getunitdata_sub(UMOB_WEAPON, md->vd->weapon);
  14856. getunitdata_sub(UMOB_LOOKDIR, md->ud.dir);
  14857. getunitdata_sub(UMOB_CANMOVETICK, md->ud.canmove_tick);
  14858. getunitdata_sub(UMOB_STR, md->status.str);
  14859. getunitdata_sub(UMOB_AGI, md->status.agi);
  14860. getunitdata_sub(UMOB_VIT, md->status.vit);
  14861. getunitdata_sub(UMOB_INT, md->status.int_);
  14862. getunitdata_sub(UMOB_DEX, md->status.dex);
  14863. getunitdata_sub(UMOB_LUK, md->status.luk);
  14864. getunitdata_sub(UMOB_SLAVECPYMSTRMD, md->state.copy_master_mode);
  14865. getunitdata_sub(UMOB_DMGIMMUNE, md->ud.immune_attack);
  14866. getunitdata_sub(UMOB_ATKRANGE, md->status.rhw.range);
  14867. getunitdata_sub(UMOB_ATKMIN, md->status.rhw.atk);
  14868. getunitdata_sub(UMOB_ATKMAX, md->status.rhw.atk2);
  14869. getunitdata_sub(UMOB_MATKMIN, md->status.matk_min);
  14870. getunitdata_sub(UMOB_MATKMAX, md->status.matk_max);
  14871. getunitdata_sub(UMOB_DEF, md->status.def);
  14872. getunitdata_sub(UMOB_MDEF, md->status.mdef);
  14873. getunitdata_sub(UMOB_HIT, md->status.hit);
  14874. getunitdata_sub(UMOB_FLEE, md->status.flee);
  14875. getunitdata_sub(UMOB_PDODGE, md->status.flee2);
  14876. getunitdata_sub(UMOB_CRIT, md->status.cri);
  14877. getunitdata_sub(UMOB_RACE, md->status.race);
  14878. getunitdata_sub(UMOB_ELETYPE, md->status.def_ele);
  14879. getunitdata_sub(UMOB_ELELEVEL, md->status.ele_lv);
  14880. getunitdata_sub(UMOB_AMOTION, md->status.amotion);
  14881. getunitdata_sub(UMOB_ADELAY, md->status.adelay);
  14882. getunitdata_sub(UMOB_DMOTION, md->status.dmotion);
  14883. break;
  14884. case BL_HOM:
  14885. if (!hd) {
  14886. ShowWarning("buildin_getunitdata: Error in finding object BL_HOM!\n");
  14887. return SCRIPT_CMD_FAILURE;
  14888. }
  14889. getunitdata_sub(UHOM_SIZE, hd->base_status.size);
  14890. getunitdata_sub(UHOM_LEVEL, hd->homunculus.level);
  14891. getunitdata_sub(UHOM_HP, hd->homunculus.hp);
  14892. getunitdata_sub(UHOM_MAXHP, hd->homunculus.max_hp);
  14893. getunitdata_sub(UHOM_SP, hd->homunculus.sp);
  14894. getunitdata_sub(UHOM_MAXSP, hd->homunculus.max_sp);
  14895. getunitdata_sub(UHOM_MASTERCID, hd->homunculus.char_id);
  14896. getunitdata_sub(UHOM_MAPID, hd->bl.m);
  14897. getunitdata_sub(UHOM_X, hd->bl.x);
  14898. getunitdata_sub(UHOM_Y, hd->bl.y);
  14899. getunitdata_sub(UHOM_HUNGER, hd->homunculus.hunger);
  14900. getunitdata_sub(UHOM_INTIMACY, hd->homunculus.intimacy);
  14901. getunitdata_sub(UHOM_SPEED, hd->base_status.speed);
  14902. getunitdata_sub(UHOM_LOOKDIR, hd->ud.dir);
  14903. getunitdata_sub(UHOM_CANMOVETICK, hd->ud.canmove_tick);
  14904. getunitdata_sub(UHOM_STR, hd->base_status.str);
  14905. getunitdata_sub(UHOM_AGI, hd->base_status.agi);
  14906. getunitdata_sub(UHOM_VIT, hd->base_status.vit);
  14907. getunitdata_sub(UHOM_INT, hd->base_status.int_);
  14908. getunitdata_sub(UHOM_DEX, hd->base_status.dex);
  14909. getunitdata_sub(UHOM_LUK, hd->base_status.luk);
  14910. getunitdata_sub(UHOM_DMGIMMUNE, hd->ud.immune_attack);
  14911. getunitdata_sub(UHOM_ATKRANGE, hd->battle_status.rhw.range);
  14912. getunitdata_sub(UHOM_ATKMIN, hd->base_status.rhw.atk);
  14913. getunitdata_sub(UHOM_ATKMAX, hd->base_status.rhw.atk2);
  14914. getunitdata_sub(UHOM_MATKMIN, hd->base_status.matk_min);
  14915. getunitdata_sub(UHOM_MATKMAX, hd->base_status.matk_max);
  14916. getunitdata_sub(UHOM_DEF, hd->battle_status.def);
  14917. getunitdata_sub(UHOM_MDEF, hd->battle_status.mdef);
  14918. getunitdata_sub(UHOM_HIT, hd->battle_status.hit);
  14919. getunitdata_sub(UHOM_FLEE, hd->battle_status.flee);
  14920. getunitdata_sub(UHOM_PDODGE, hd->battle_status.flee2);
  14921. getunitdata_sub(UHOM_CRIT, hd->battle_status.cri);
  14922. getunitdata_sub(UHOM_RACE, hd->battle_status.race);
  14923. getunitdata_sub(UHOM_ELETYPE, hd->battle_status.def_ele);
  14924. getunitdata_sub(UHOM_ELELEVEL, hd->battle_status.ele_lv);
  14925. getunitdata_sub(UHOM_AMOTION, hd->battle_status.amotion);
  14926. getunitdata_sub(UHOM_ADELAY, hd->battle_status.adelay);
  14927. getunitdata_sub(UHOM_DMOTION, hd->battle_status.dmotion);
  14928. break;
  14929. case BL_PET:
  14930. if (!pd) {
  14931. ShowWarning("buildin_getunitdata: Error in finding object BL_PET!\n");
  14932. return SCRIPT_CMD_FAILURE;
  14933. }
  14934. getunitdata_sub(UPET_SIZE, pd->status.size);
  14935. getunitdata_sub(UPET_LEVEL, pd->pet.level);
  14936. getunitdata_sub(UPET_HP, pd->status.hp);
  14937. getunitdata_sub(UPET_MAXHP, pd->status.max_hp);
  14938. getunitdata_sub(UPET_MASTERAID, pd->pet.account_id);
  14939. getunitdata_sub(UPET_MAPID, pd->bl.m);
  14940. getunitdata_sub(UPET_X, pd->bl.x);
  14941. getunitdata_sub(UPET_Y, pd->bl.y);
  14942. getunitdata_sub(UPET_HUNGER, pd->pet.hungry);
  14943. getunitdata_sub(UPET_INTIMACY, pd->pet.intimate);
  14944. getunitdata_sub(UPET_SPEED, pd->status.speed);
  14945. getunitdata_sub(UPET_LOOKDIR, pd->ud.dir);
  14946. getunitdata_sub(UPET_CANMOVETICK, pd->ud.canmove_tick);
  14947. getunitdata_sub(UPET_STR, pd->status.str);
  14948. getunitdata_sub(UPET_AGI, pd->status.agi);
  14949. getunitdata_sub(UPET_VIT, pd->status.vit);
  14950. getunitdata_sub(UPET_INT, pd->status.int_);
  14951. getunitdata_sub(UPET_DEX, pd->status.dex);
  14952. getunitdata_sub(UPET_LUK, pd->status.luk);
  14953. getunitdata_sub(UPET_DMGIMMUNE, pd->ud.immune_attack);
  14954. getunitdata_sub(UPET_ATKRANGE, pd->status.rhw.range);
  14955. getunitdata_sub(UPET_ATKMIN, pd->status.rhw.atk);
  14956. getunitdata_sub(UPET_ATKMAX, pd->status.rhw.atk2);
  14957. getunitdata_sub(UPET_MATKMIN, pd->status.matk_min);
  14958. getunitdata_sub(UPET_MATKMAX, pd->status.matk_max);
  14959. getunitdata_sub(UPET_DEF, pd->status.def);
  14960. getunitdata_sub(UPET_MDEF, pd->status.mdef);
  14961. getunitdata_sub(UPET_HIT, pd->status.hit);
  14962. getunitdata_sub(UPET_FLEE, pd->status.flee);
  14963. getunitdata_sub(UPET_PDODGE, pd->status.flee2);
  14964. getunitdata_sub(UPET_CRIT, pd->status.cri);
  14965. getunitdata_sub(UPET_RACE, pd->status.race);
  14966. getunitdata_sub(UPET_ELETYPE, pd->status.def_ele);
  14967. getunitdata_sub(UPET_ELELEVEL, pd->status.ele_lv);
  14968. getunitdata_sub(UPET_AMOTION, pd->status.amotion);
  14969. getunitdata_sub(UPET_ADELAY, pd->status.adelay);
  14970. getunitdata_sub(UPET_DMOTION, pd->status.dmotion);
  14971. break;
  14972. case BL_MER:
  14973. if (!mc) {
  14974. ShowWarning("buildin_getunitdata: Error in finding object BL_MER!\n");
  14975. return SCRIPT_CMD_FAILURE;
  14976. }
  14977. getunitdata_sub(UMER_SIZE, mc->base_status.size);
  14978. getunitdata_sub(UMER_HP, mc->base_status.hp);
  14979. getunitdata_sub(UMER_MAXHP, mc->base_status.max_hp);
  14980. getunitdata_sub(UMER_MASTERCID, mc->mercenary.char_id);
  14981. getunitdata_sub(UMER_MAPID, mc->bl.m);
  14982. getunitdata_sub(UMER_X, mc->bl.x);
  14983. getunitdata_sub(UMER_Y, mc->bl.y);
  14984. getunitdata_sub(UMER_KILLCOUNT, mc->mercenary.kill_count);
  14985. getunitdata_sub(UMER_LIFETIME, mc->mercenary.life_time);
  14986. getunitdata_sub(UMER_SPEED, mc->base_status.speed);
  14987. getunitdata_sub(UMER_LOOKDIR, mc->ud.dir);
  14988. getunitdata_sub(UMER_CANMOVETICK, mc->ud.canmove_tick);
  14989. getunitdata_sub(UMER_STR, mc->base_status.str);
  14990. getunitdata_sub(UMER_AGI, mc->base_status.agi);
  14991. getunitdata_sub(UMER_VIT, mc->base_status.vit);
  14992. getunitdata_sub(UMER_INT, mc->base_status.int_);
  14993. getunitdata_sub(UMER_DEX, mc->base_status.dex);
  14994. getunitdata_sub(UMER_LUK, mc->base_status.luk);
  14995. getunitdata_sub(UMER_DMGIMMUNE, mc->ud.immune_attack);
  14996. getunitdata_sub(UMER_ATKRANGE, mc->base_status.rhw.range);
  14997. getunitdata_sub(UMER_ATKMIN, mc->base_status.rhw.atk);
  14998. getunitdata_sub(UMER_ATKMAX, mc->base_status.rhw.atk2);
  14999. getunitdata_sub(UMER_MATKMIN, mc->base_status.matk_min);
  15000. getunitdata_sub(UMER_MATKMAX, mc->base_status.matk_max);
  15001. getunitdata_sub(UMER_DEF, mc->base_status.def);
  15002. getunitdata_sub(UMER_MDEF, mc->base_status.mdef);
  15003. getunitdata_sub(UMER_HIT, mc->base_status.hit);
  15004. getunitdata_sub(UMER_FLEE, mc->base_status.flee);
  15005. getunitdata_sub(UMER_PDODGE, mc->base_status.flee2);
  15006. getunitdata_sub(UMER_CRIT, mc->base_status.cri);
  15007. getunitdata_sub(UMER_RACE, mc->base_status.race);
  15008. getunitdata_sub(UMER_ELETYPE, mc->base_status.def_ele);
  15009. getunitdata_sub(UMER_ELELEVEL, mc->base_status.ele_lv);
  15010. getunitdata_sub(UMER_AMOTION, mc->base_status.amotion);
  15011. getunitdata_sub(UMER_ADELAY, mc->base_status.adelay);
  15012. getunitdata_sub(UMER_DMOTION, mc->base_status.dmotion);
  15013. break;
  15014. case BL_ELEM:
  15015. if (!ed) {
  15016. ShowWarning("buildin_getunitdata: Error in finding object BL_ELEM!\n");
  15017. return SCRIPT_CMD_FAILURE;
  15018. }
  15019. getunitdata_sub(UELE_SIZE, ed->base_status.size);
  15020. getunitdata_sub(UELE_HP, ed->elemental.hp);
  15021. getunitdata_sub(UELE_MAXHP, ed->elemental.max_hp);
  15022. getunitdata_sub(UELE_SP, ed->elemental.sp);
  15023. getunitdata_sub(UELE_MAXSP, ed->elemental.max_sp);
  15024. getunitdata_sub(UELE_MASTERCID, ed->elemental.char_id);
  15025. getunitdata_sub(UELE_MAPID, ed->bl.m);
  15026. getunitdata_sub(UELE_X, ed->bl.x);
  15027. getunitdata_sub(UELE_Y, ed->bl.y);
  15028. getunitdata_sub(UELE_LIFETIME, ed->elemental.life_time);
  15029. getunitdata_sub(UELE_MODE, ed->elemental.mode);
  15030. getunitdata_sub(UELE_SP, ed->base_status.speed);
  15031. getunitdata_sub(UELE_LOOKDIR, ed->ud.dir);
  15032. getunitdata_sub(UELE_CANMOVETICK, ed->ud.canmove_tick);
  15033. getunitdata_sub(UELE_STR, ed->base_status.str);
  15034. getunitdata_sub(UELE_AGI, ed->base_status.agi);
  15035. getunitdata_sub(UELE_VIT, ed->base_status.vit);
  15036. getunitdata_sub(UELE_INT, ed->base_status.int_);
  15037. getunitdata_sub(UELE_DEX, ed->base_status.dex);
  15038. getunitdata_sub(UELE_LUK, ed->base_status.luk);
  15039. getunitdata_sub(UELE_DMGIMMUNE, ed->ud.immune_attack);
  15040. getunitdata_sub(UELE_ATKRANGE, ed->base_status.rhw.range);
  15041. getunitdata_sub(UELE_ATKMIN, ed->base_status.rhw.atk);
  15042. getunitdata_sub(UELE_ATKMAX, ed->base_status.rhw.atk2);
  15043. getunitdata_sub(UELE_MATKMIN, ed->base_status.matk_min);
  15044. getunitdata_sub(UELE_MATKMAX, ed->base_status.matk_max);
  15045. getunitdata_sub(UELE_DEF, ed->base_status.def);
  15046. getunitdata_sub(UELE_MDEF, ed->base_status.mdef);
  15047. getunitdata_sub(UELE_HIT, ed->base_status.hit);
  15048. getunitdata_sub(UELE_FLEE, ed->base_status.flee);
  15049. getunitdata_sub(UELE_PDODGE, ed->base_status.flee2);
  15050. getunitdata_sub(UELE_CRIT, ed->base_status.cri);
  15051. getunitdata_sub(UELE_RACE, ed->base_status.race);
  15052. getunitdata_sub(UELE_ELETYPE, ed->base_status.def_ele);
  15053. getunitdata_sub(UELE_ELELEVEL, ed->base_status.ele_lv);
  15054. getunitdata_sub(UELE_AMOTION, ed->base_status.amotion);
  15055. getunitdata_sub(UELE_ADELAY, ed->base_status.adelay);
  15056. getunitdata_sub(UELE_DMOTION, ed->base_status.dmotion);
  15057. break;
  15058. case BL_NPC:
  15059. if (!nd) {
  15060. ShowWarning("buildin_getunitdata: Error in finding object BL_NPC!\n");
  15061. return SCRIPT_CMD_FAILURE;
  15062. }
  15063. getunitdata_sub(UNPC_DISPLAY, nd->class_);
  15064. getunitdata_sub(UNPC_LEVEL, nd->level);
  15065. getunitdata_sub(UNPC_HP, nd->status.hp);
  15066. getunitdata_sub(UNPC_MAXHP, nd->status.max_hp);
  15067. getunitdata_sub(UNPC_MAPID, nd->bl.m);
  15068. getunitdata_sub(UNPC_X, nd->bl.x);
  15069. getunitdata_sub(UNPC_Y, nd->bl.y);
  15070. getunitdata_sub(UNPC_LOOKDIR, nd->ud.dir);
  15071. getunitdata_sub(UNPC_STR, nd->status.str);
  15072. getunitdata_sub(UNPC_AGI, nd->status.agi);
  15073. getunitdata_sub(UNPC_VIT, nd->status.vit);
  15074. getunitdata_sub(UNPC_INT, nd->status.int_);
  15075. getunitdata_sub(UNPC_DEX, nd->status.dex);
  15076. getunitdata_sub(UNPC_LUK, nd->status.luk);
  15077. getunitdata_sub(UNPC_PLUSALLSTAT, nd->stat_point);
  15078. getunitdata_sub(UNPC_DMGIMMUNE, nd->ud.immune_attack);
  15079. getunitdata_sub(UNPC_ATKRANGE, nd->status.rhw.range);
  15080. getunitdata_sub(UNPC_ATKMIN, nd->status.rhw.atk);
  15081. getunitdata_sub(UNPC_ATKMAX, nd->status.rhw.atk2);
  15082. getunitdata_sub(UNPC_MATKMIN, nd->status.matk_min);
  15083. getunitdata_sub(UNPC_MATKMAX, nd->status.matk_max);
  15084. getunitdata_sub(UNPC_DEF, nd->status.def);
  15085. getunitdata_sub(UNPC_MDEF, nd->status.mdef);
  15086. getunitdata_sub(UNPC_HIT, nd->status.hit);
  15087. getunitdata_sub(UNPC_FLEE, nd->status.flee);
  15088. getunitdata_sub(UNPC_PDODGE, nd->status.flee2);
  15089. getunitdata_sub(UNPC_CRIT, nd->status.cri);
  15090. getunitdata_sub(UNPC_RACE, nd->status.race);
  15091. getunitdata_sub(UNPC_ELETYPE, nd->status.def_ele);
  15092. getunitdata_sub(UNPC_ELELEVEL, nd->status.ele_lv);
  15093. getunitdata_sub(UNPC_AMOTION, nd->status.amotion);
  15094. getunitdata_sub(UNPC_ADELAY, nd->status.adelay);
  15095. getunitdata_sub(UNPC_DMOTION, nd->status.dmotion);
  15096. break;
  15097. default:
  15098. ShowWarning("buildin_getunitdata: Unknown object type!\n");
  15099. return SCRIPT_CMD_FAILURE;
  15100. }
  15101. #undef getunitdata_sub
  15102. return SCRIPT_CMD_SUCCESS;
  15103. }
  15104. /// Changes the live data of a bl.
  15105. ///
  15106. /// setunitdata <unit id>,<type>,<value>;
  15107. BUILDIN_FUNC(setunitdata)
  15108. {
  15109. struct block_list* bl = NULL;
  15110. struct script_data* data;
  15111. const char *mapname = NULL;
  15112. TBL_MOB* md = NULL;
  15113. TBL_HOM* hd = NULL;
  15114. TBL_MER* mc = NULL;
  15115. TBL_PET* pd = NULL;
  15116. TBL_ELEM* ed = NULL;
  15117. TBL_NPC* nd = NULL;
  15118. int type, value = 0;
  15119. if(!script_rid2bl(2,bl))
  15120. {
  15121. script_pushint(st, -1);
  15122. return SCRIPT_CMD_FAILURE;
  15123. }
  15124. switch (bl->type) {
  15125. case BL_MOB: md = map_id2md(bl->id); break;
  15126. case BL_HOM: hd = map_id2hd(bl->id); break;
  15127. case BL_PET: pd = map_id2pd(bl->id); break;
  15128. case BL_MER: mc = map_id2mc(bl->id); break;
  15129. case BL_ELEM: ed = map_id2ed(bl->id); break;
  15130. case BL_NPC:
  15131. nd = map_id2nd(bl->id);
  15132. if (!nd->status.hp)
  15133. status_calc_npc(nd, SCO_FIRST);
  15134. else
  15135. status_calc_npc(nd, SCO_NONE);
  15136. break;
  15137. default:
  15138. ShowError("buildin_setunitdata: Invalid object!\n");
  15139. return SCRIPT_CMD_FAILURE;
  15140. }
  15141. type = script_getnum(st, 3);
  15142. data = script_getdata(st, 4);
  15143. get_val(st, data);
  15144. if (type == 5 && data_isstring(data))
  15145. mapname = conv_str(st, data);
  15146. else if (data_isint(data))
  15147. value = conv_num(st, data);
  15148. else {
  15149. ShowError("buildin_setunitdata: Invalid data type for argument #3 (%d).\n", data->type);
  15150. return SCRIPT_CMD_FAILURE;
  15151. }
  15152. switch (bl->type) {
  15153. case BL_MOB:
  15154. if (!md) {
  15155. ShowWarning("buildin_setunitdata: Error in finding object BL_MOB!\n");
  15156. return SCRIPT_CMD_FAILURE;
  15157. }
  15158. switch (type) {
  15159. case UMOB_SIZE: md->status.size = (unsigned char)value; break;
  15160. case UMOB_LEVEL: md->level = (unsigned short)value; break;
  15161. case UMOB_HP: status_set_hp(bl, (unsigned int)value, 0); clif_name_area(&md->bl); break;
  15162. case UMOB_MAXHP: status_set_maxhp(bl, (unsigned int)value, 0); clif_name_area(&md->bl); break;
  15163. case UMOB_MASTERAID: md->master_id = value; break;
  15164. case UMOB_MAPID: if (mapname) value = map_mapname2mapid(mapname); unit_warp(bl, (short)value, 0, 0, CLR_TELEPORT); break;
  15165. case UMOB_X: if (!unit_walktoxy(bl, (short)value, md->bl.y, 2)) unit_movepos(bl, (short)value, md->bl.y, 0, 0); break;
  15166. case UMOB_Y: if (!unit_walktoxy(bl, md->bl.x, (short)value, 2)) unit_movepos(bl, md->bl.x, (short)value, 0, 0); break;
  15167. case UMOB_SPEED: md->status.speed = (unsigned short)value; status_calc_misc(bl, &md->status, md->level); break;
  15168. case UMOB_MODE: md->status.mode = (enum e_mode)value; break;
  15169. case UMOB_AI: md->special_state.ai = (enum mob_ai)value; break;
  15170. case UMOB_SCOPTION: md->sc.option = (unsigned short)value; break;
  15171. case UMOB_SEX: md->vd->sex = (char)value; break;
  15172. case UMOB_CLASS: status_set_viewdata(bl, (unsigned short)value); break;
  15173. case UMOB_HAIRSTYLE: clif_changelook(bl, LOOK_HAIR, (unsigned short)value); break;
  15174. case UMOB_HAIRCOLOR: clif_changelook(bl, LOOK_HAIR_COLOR, (unsigned short)value); break;
  15175. case UMOB_HEADBOTTOM: clif_changelook(bl, LOOK_HEAD_BOTTOM, (unsigned short)value); break;
  15176. case UMOB_HEADMIDDLE: clif_changelook(bl, LOOK_HEAD_MID, (unsigned short)value); break;
  15177. case UMOB_HEADTOP: clif_changelook(bl, LOOK_HEAD_TOP, (unsigned short)value); break;
  15178. case UMOB_CLOTHCOLOR: clif_changelook(bl, LOOK_CLOTHES_COLOR, (unsigned short)value); break;
  15179. case UMOB_SHIELD: clif_changelook(bl, LOOK_SHIELD, (unsigned short)value); break;
  15180. case UMOB_WEAPON: clif_changelook(bl, LOOK_WEAPON, (unsigned short)value); break;
  15181. case UMOB_LOOKDIR: unit_setdir(bl, (uint8)value); break;
  15182. case UMOB_CANMOVETICK: md->ud.canmove_tick = value > 0 ? (unsigned int)value : 0; break;
  15183. case UMOB_STR: md->status.str = (unsigned short)value; status_calc_misc(bl, &md->status, md->level); break;
  15184. case UMOB_AGI: md->status.agi = (unsigned short)value; status_calc_misc(bl, &md->status, md->level); break;
  15185. case UMOB_VIT: md->status.vit = (unsigned short)value; status_calc_misc(bl, &md->status, md->level); break;
  15186. case UMOB_INT: md->status.int_ = (unsigned short)value; status_calc_misc(bl, &md->status, md->level); break;
  15187. case UMOB_DEX: md->status.dex = (unsigned short)value; status_calc_misc(bl, &md->status, md->level); break;
  15188. case UMOB_LUK: md->status.luk = (unsigned short)value; status_calc_misc(bl, &md->status, md->level); break;
  15189. case UMOB_SLAVECPYMSTRMD:
  15190. if (value > 0) {
  15191. TBL_MOB *md2 = NULL;
  15192. if (!md->master_id || !(md2 = map_id2md(md->master_id))) {
  15193. ShowWarning("buildin_setunitdata: Trying to set UMOB_SLAVECPYMSTRMD on mob without master!\n");
  15194. break;
  15195. }
  15196. md->status.mode = md2->status.mode;
  15197. md->state.copy_master_mode = 1;
  15198. } else
  15199. md->state.copy_master_mode = 0;
  15200. break;
  15201. case UMOB_DMGIMMUNE: md->ud.immune_attack = (bool)value > 0 ? 1 : 0; break;
  15202. case UMOB_ATKRANGE: md->status.rhw.range = (unsigned short)value; break;
  15203. case UMOB_ATKMIN: md->status.rhw.atk = (unsigned short)value; break;
  15204. case UMOB_ATKMAX: md->status.rhw.atk2 = (unsigned short)value; break;
  15205. case UMOB_MATKMIN: md->status.matk_min = (unsigned short)value; break;
  15206. case UMOB_MATKMAX: md->status.matk_max = (unsigned short)value; break;
  15207. case UMOB_DEF: md->status.def = (defType)value; break;
  15208. case UMOB_MDEF: md->status.mdef = (defType)value; break;
  15209. case UMOB_HIT: md->status.hit = (short)value; break;
  15210. case UMOB_FLEE: md->status.flee = (short)value; break;
  15211. case UMOB_PDODGE: md->status.flee2 = (short)value; break;
  15212. case UMOB_CRIT: md->status.cri = (short)value; break;
  15213. case UMOB_RACE: md->status.race = (unsigned char)value; break;
  15214. case UMOB_ELETYPE: md->status.def_ele = (unsigned char)value; break;
  15215. case UMOB_ELELEVEL: md->status.ele_lv = (unsigned char)value; break;
  15216. case UMOB_AMOTION: md->status.amotion = (short)value; break;
  15217. case UMOB_ADELAY: md->status.adelay = (short)value; break;
  15218. case UMOB_DMOTION: md->status.dmotion = (short)value; break;
  15219. default:
  15220. ShowError("buildin_setunitdata: Unknown data identifier %d for BL_MOB.\n", type);
  15221. return SCRIPT_CMD_FAILURE;
  15222. }
  15223. break;
  15224. case BL_HOM:
  15225. if (!hd) {
  15226. ShowWarning("buildin_setunitdata: Error in finding object BL_HOM!\n");
  15227. return SCRIPT_CMD_FAILURE;
  15228. }
  15229. switch (type) {
  15230. case UHOM_SIZE: hd->base_status.size = (unsigned char)value; break;
  15231. case UHOM_LEVEL: hd->homunculus.level = (unsigned short)value; break;
  15232. case UHOM_HP: status_set_hp(bl, (unsigned int)value, 0); break;
  15233. case UHOM_MAXHP: status_set_maxhp(bl, (unsigned int)value, 0); break;
  15234. case UHOM_SP: status_set_sp(bl, (unsigned int)value, 0); break;
  15235. case UHOM_MAXSP: status_set_maxsp(bl, (unsigned int)value, 0); break;
  15236. case UHOM_MASTERCID: hd->homunculus.char_id = (uint32)value; break;
  15237. case UHOM_MAPID: if (mapname) value = map_mapname2mapid(mapname); unit_warp(bl, (short)value, 0, 0, CLR_TELEPORT); break;
  15238. case UHOM_X: if (!unit_walktoxy(bl, (short)value, hd->bl.y, 2)) unit_movepos(bl, (short)value, hd->bl.y, 0, 0); break;
  15239. case UHOM_Y: if (!unit_walktoxy(bl, hd->bl.x, (short)value, 2)) unit_movepos(bl, hd->bl.x, (short)value, 0, 0); break;
  15240. case UHOM_HUNGER: hd->homunculus.hunger = (short)value; clif_send_homdata(map_charid2sd(hd->homunculus.char_id), SP_HUNGRY, hd->homunculus.hunger); break;
  15241. 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;
  15242. case UHOM_SPEED: hd->base_status.speed = (unsigned short)value; status_calc_misc(bl, &hd->base_status, hd->homunculus.level); break;
  15243. case UHOM_LOOKDIR: unit_setdir(bl, (uint8)value); break;
  15244. case UHOM_CANMOVETICK: hd->ud.canmove_tick = value > 0 ? (unsigned int)value : 0; break;
  15245. case UHOM_STR: hd->base_status.str = (unsigned short)value; status_calc_misc(bl, &hd->base_status, hd->homunculus.level); break;
  15246. case UHOM_AGI: hd->base_status.agi = (unsigned short)value; status_calc_misc(bl, &hd->base_status, hd->homunculus.level); break;
  15247. case UHOM_VIT: hd->base_status.vit = (unsigned short)value; status_calc_misc(bl, &hd->base_status, hd->homunculus.level); break;
  15248. case UHOM_INT: hd->base_status.int_ = (unsigned short)value; status_calc_misc(bl, &hd->base_status, hd->homunculus.level); break;
  15249. case UHOM_DEX: hd->base_status.dex = (unsigned short)value; status_calc_misc(bl, &hd->base_status, hd->homunculus.level); break;
  15250. case UHOM_LUK: hd->base_status.luk = (unsigned short)value; status_calc_misc(bl, &hd->base_status, hd->homunculus.level); break;
  15251. case UHOM_DMGIMMUNE: hd->ud.immune_attack = (bool)value > 0 ? 1 : 0; break;
  15252. case UHOM_ATKRANGE: hd->base_status.rhw.range = (unsigned short)value; break;
  15253. case UHOM_ATKMIN: hd->base_status.rhw.atk = (unsigned short)value; break;
  15254. case UHOM_ATKMAX: hd->base_status.rhw.atk2 = (unsigned short)value; break;
  15255. case UHOM_MATKMIN: hd->base_status.matk_min = (unsigned short)value; break;
  15256. case UHOM_MATKMAX: hd->base_status.matk_max = (unsigned short)value; break;
  15257. case UHOM_DEF: hd->base_status.def = (defType)value; break;
  15258. case UHOM_MDEF: hd->base_status.mdef = (defType)value; break;
  15259. case UHOM_HIT: hd->base_status.hit = (short)value; break;
  15260. case UHOM_FLEE: hd->base_status.flee = (short)value; break;
  15261. case UHOM_PDODGE: hd->base_status.flee2 = (short)value; break;
  15262. case UHOM_CRIT: hd->base_status.cri = (short)value; break;
  15263. case UHOM_RACE: hd->base_status.race = (unsigned char)value; break;
  15264. case UHOM_ELETYPE: hd->base_status.def_ele = (unsigned char)value; break;
  15265. case UHOM_ELELEVEL: hd->base_status.ele_lv = (unsigned char)value; break;
  15266. case UHOM_AMOTION: hd->base_status.amotion = (short)value; break;
  15267. case UHOM_ADELAY: hd->base_status.adelay = (short)value; break;
  15268. case UHOM_DMOTION: hd->base_status.dmotion = (short)value; break;
  15269. default:
  15270. ShowError("buildin_setunitdata: Unknown data identifier %d for BL_HOM.\n", type);
  15271. return SCRIPT_CMD_FAILURE;
  15272. }
  15273. break;
  15274. case BL_PET:
  15275. if (!pd) {
  15276. ShowWarning("buildin_setunitdata: Error in finding object BL_PET!\n");
  15277. return SCRIPT_CMD_FAILURE;
  15278. }
  15279. switch (type) {
  15280. case UPET_SIZE: pd->status.size = (unsigned char)value; break;
  15281. case UPET_LEVEL: pd->pet.level = (unsigned short)value; break;
  15282. case UPET_HP: status_set_hp(bl, (unsigned int)value, 0); break;
  15283. case UPET_MAXHP: status_set_maxhp(bl, (unsigned int)value, 0); break;
  15284. case UPET_MASTERAID: pd->pet.account_id = (unsigned int)value; break;
  15285. case UPET_MAPID: if (mapname) value = map_mapname2mapid(mapname); unit_warp(bl, (short)value, 0, 0, CLR_TELEPORT); break;
  15286. case UPET_X: if (!unit_walktoxy(bl, (short)value, pd->bl.y, 2)) unit_movepos(bl, (short)value, md->bl.y, 0, 0); break;
  15287. case UPET_Y: if (!unit_walktoxy(bl, pd->bl.x, (short)value, 2)) unit_movepos(bl, pd->bl.x, (short)value, 0, 0); break;
  15288. case UPET_HUNGER: pd->pet.hungry = (short)value; clif_send_petdata(map_id2sd(pd->pet.account_id), pd, 2, pd->pet.hungry); break;
  15289. 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;
  15290. case UPET_SPEED: pd->status.speed = (unsigned short)value; status_calc_misc(bl, &pd->status, pd->pet.level); break;
  15291. case UPET_LOOKDIR: unit_setdir(bl, (uint8)value); break;
  15292. case UPET_CANMOVETICK: pd->ud.canmove_tick = value > 0 ? (unsigned int)value : 0; break;
  15293. case UPET_STR: pd->status.str = (unsigned short)value; status_calc_misc(bl, &pd->status, pd->pet.level); break;
  15294. case UPET_AGI: pd->status.agi = (unsigned short)value; status_calc_misc(bl, &pd->status, pd->pet.level); break;
  15295. case UPET_VIT: pd->status.vit = (unsigned short)value; status_calc_misc(bl, &pd->status, pd->pet.level); break;
  15296. case UPET_INT: pd->status.int_ = (unsigned short)value; status_calc_misc(bl, &pd->status, pd->pet.level); break;
  15297. case UPET_DEX: pd->status.dex = (unsigned short)value; status_calc_misc(bl, &pd->status, pd->pet.level); break;
  15298. case UPET_LUK: pd->status.luk = (unsigned short)value; status_calc_misc(bl, &pd->status, pd->pet.level); break;
  15299. case UPET_DMGIMMUNE: pd->ud.immune_attack = (bool)value > 0 ? 1 : 0; break;
  15300. case UPET_ATKRANGE: pd->status.rhw.range = (unsigned short)value; break;
  15301. case UPET_ATKMIN: pd->status.rhw.atk = (unsigned short)value; break;
  15302. case UPET_ATKMAX: pd->status.rhw.atk2 = (unsigned short)value; break;
  15303. case UPET_MATKMIN: pd->status.matk_min = (unsigned short)value; break;
  15304. case UPET_MATKMAX: pd->status.matk_max = (unsigned short)value; break;
  15305. case UPET_DEF: pd->status.def = (defType)value; break;
  15306. case UPET_MDEF: pd->status.mdef = (defType)value; break;
  15307. case UPET_HIT: pd->status.hit = (short)value; break;
  15308. case UPET_FLEE: pd->status.flee = (short)value; break;
  15309. case UPET_PDODGE: pd->status.flee2 = (short)value; break;
  15310. case UPET_CRIT: pd->status.cri = (short)value; break;
  15311. case UPET_RACE: pd->status.race = (unsigned char)value; break;
  15312. case UPET_ELETYPE: pd->status.def_ele = (unsigned char)value; break;
  15313. case UPET_ELELEVEL: pd->status.ele_lv = (unsigned char)value; break;
  15314. case UPET_AMOTION: pd->status.amotion = (short)value; break;
  15315. case UPET_ADELAY: pd->status.adelay = (short)value; break;
  15316. case UPET_DMOTION: pd->status.dmotion = (short)value; break;
  15317. default:
  15318. ShowError("buildin_setunitdata: Unknown data identifier %d for BL_PET.\n", type);
  15319. return SCRIPT_CMD_FAILURE;
  15320. }
  15321. break;
  15322. case BL_MER:
  15323. if (!mc) {
  15324. ShowWarning("buildin_setunitdata: Error in finding object BL_MER!\n");
  15325. return SCRIPT_CMD_FAILURE;
  15326. }
  15327. switch (type) {
  15328. case UMER_SIZE: mc->base_status.size = (unsigned char)value; break;
  15329. case UMER_HP: status_set_hp(bl, (unsigned int)value, 0); break;
  15330. case UMER_MAXHP: status_set_maxhp(bl, (unsigned int)value, 0); break;
  15331. case UMER_MASTERCID: mc->mercenary.char_id = (uint32)value; break;
  15332. case UMER_MAPID: if (mapname) value = map_mapname2mapid(mapname); unit_warp(bl, (short)value, 0, 0, CLR_TELEPORT); break;
  15333. case UMER_X: if (!unit_walktoxy(bl, (short)value, mc->bl.y, 2)) unit_movepos(bl, (short)value, mc->bl.y, 0, 0); break;
  15334. case UMER_Y: if (!unit_walktoxy(bl, mc->bl.x, (short)value, 2)) unit_movepos(bl, mc->bl.x, (short)value, 0, 0); break;
  15335. case UMER_KILLCOUNT: mc->mercenary.kill_count = (unsigned int)value; break;
  15336. case UMER_LIFETIME: mc->mercenary.life_time = (unsigned int)value; break;
  15337. case UMER_SPEED: mc->base_status.speed = (unsigned short)value; status_calc_misc(bl, &mc->base_status, mc->db->lv); break;
  15338. case UMER_LOOKDIR: unit_setdir(bl, (uint8)value); break;
  15339. case UMER_CANMOVETICK: mc->ud.canmove_tick = value > 0 ? (unsigned int)value : 0; break;
  15340. case UMER_STR: mc->base_status.str = (unsigned short)value; status_calc_misc(bl, &mc->base_status, mc->db->lv); break;
  15341. case UMER_AGI: mc->base_status.agi = (unsigned short)value; status_calc_misc(bl, &mc->base_status, mc->db->lv); break;
  15342. case UMER_VIT: mc->base_status.vit = (unsigned short)value; status_calc_misc(bl, &mc->base_status, mc->db->lv); break;
  15343. case UMER_INT: mc->base_status.int_ = (unsigned short)value; status_calc_misc(bl, &mc->base_status, mc->db->lv); break;
  15344. case UMER_DEX: mc->base_status.dex = (unsigned short)value; status_calc_misc(bl, &mc->base_status, mc->db->lv); break;
  15345. case UMER_LUK: mc->base_status.luk = (unsigned short)value; status_calc_misc(bl, &mc->base_status, mc->db->lv); break;
  15346. case UMER_DMGIMMUNE: mc->ud.immune_attack = (bool)value > 0 ? 1 : 0; break;
  15347. case UMER_ATKRANGE: mc->base_status.rhw.range = (unsigned short)value; break;
  15348. case UMER_ATKMIN: mc->base_status.rhw.atk = (unsigned short)value; break;
  15349. case UMER_ATKMAX: mc->base_status.rhw.atk2 = (unsigned short)value; break;
  15350. case UMER_MATKMIN: mc->base_status.matk_min = (unsigned short)value; break;
  15351. case UMER_MATKMAX: mc->base_status.matk_max = (unsigned short)value; break;
  15352. case UMER_DEF: mc->base_status.def = (defType)value; break;
  15353. case UMER_MDEF: mc->base_status.mdef = (defType)value; break;
  15354. case UMER_HIT: mc->base_status.hit = (short)value; break;
  15355. case UMER_FLEE: mc->base_status.flee = (short)value; break;
  15356. case UMER_PDODGE: mc->base_status.flee2 = (short)value; break;
  15357. case UMER_CRIT: mc->base_status.cri = (short)value; break;
  15358. case UMER_RACE: mc->base_status.race = (unsigned char)value; break;
  15359. case UMER_ELETYPE: mc->base_status.def_ele = (unsigned char)value; break;
  15360. case UMER_ELELEVEL: mc->base_status.ele_lv = (unsigned char)value; break;
  15361. case UMER_AMOTION: mc->base_status.amotion = (short)value; break;
  15362. case UMER_ADELAY: mc->base_status.adelay = (short)value; break;
  15363. case UMER_DMOTION: mc->base_status.dmotion = (short)value; break;
  15364. default:
  15365. ShowError("buildin_setunitdata: Unknown data identifier %d for BL_MER.\n", type);
  15366. return SCRIPT_CMD_FAILURE;
  15367. }
  15368. break;
  15369. case BL_ELEM:
  15370. if (!ed) {
  15371. ShowWarning("buildin_setunitdata: Error in finding object BL_ELEM!\n");
  15372. return SCRIPT_CMD_FAILURE;
  15373. }
  15374. switch (type) {
  15375. case UELE_SIZE: ed->base_status.size = (unsigned char)value; break;
  15376. case UELE_HP: status_set_hp(bl, (unsigned int)value, 0); break;
  15377. case UELE_MAXHP: status_set_maxhp(bl, (unsigned int)value, 0); break;
  15378. case UELE_SP: status_set_sp(bl, (unsigned int)value, 0); break;
  15379. case UELE_MAXSP: status_set_maxsp(bl, (unsigned int)value, 0); break;
  15380. case UELE_MASTERCID: ed->elemental.char_id = (uint32)value; break;
  15381. case UELE_MAPID: if (mapname) value = map_mapname2mapid(mapname); unit_warp(bl, (short)value, 0, 0, CLR_TELEPORT); break;
  15382. case UELE_X: if (!unit_walktoxy(bl, (short)value, ed->bl.y, 2)) unit_movepos(bl, (short)value, ed->bl.y, 0, 0); break;
  15383. case UELE_Y: if (!unit_walktoxy(bl, ed->bl.x, (short)value, 2)) unit_movepos(bl, ed->bl.x, (short)value, 0, 0); break;
  15384. case UELE_LIFETIME: ed->elemental.life_time = (unsigned int)value; break;
  15385. case UELE_MODE: ed->elemental.mode = (enum e_mode)value; break;
  15386. case UELE_SPEED: ed->base_status.speed = (unsigned short)value; status_calc_misc(bl, &ed->base_status, ed->db->lv); break;
  15387. case UELE_LOOKDIR: unit_setdir(bl, (uint8)value); break;
  15388. case UELE_CANMOVETICK: ed->ud.canmove_tick = value > 0 ? (unsigned int)value : 0; break;
  15389. case UELE_STR: ed->base_status.str = (unsigned short)value; status_calc_misc(bl, &ed->base_status, ed->db->lv); break;
  15390. case UELE_AGI: ed->base_status.agi = (unsigned short)value; status_calc_misc(bl, &ed->base_status, ed->db->lv); break;
  15391. case UELE_VIT: ed->base_status.vit = (unsigned short)value; status_calc_misc(bl, &ed->base_status, ed->db->lv); break;
  15392. case UELE_INT: ed->base_status.int_ = (unsigned short)value; status_calc_misc(bl, &ed->base_status, ed->db->lv); break;
  15393. case UELE_DEX: ed->base_status.dex = (unsigned short)value; status_calc_misc(bl, &ed->base_status, ed->db->lv); break;
  15394. case UELE_LUK: ed->base_status.luk = (unsigned short)value; status_calc_misc(bl, &ed->base_status, ed->db->lv); break;
  15395. case UELE_DMGIMMUNE: ed->ud.immune_attack = (bool)value > 0 ? 1 : 0; break;
  15396. case UELE_ATKRANGE: ed->base_status.rhw.range = (unsigned short)value; break;
  15397. case UELE_ATKMIN: ed->base_status.rhw.atk = (unsigned short)value; break;
  15398. case UELE_ATKMAX: ed->base_status.rhw.atk2 = (unsigned short)value; break;
  15399. case UELE_MATKMIN: ed->base_status.matk_min = (unsigned short)value; break;
  15400. case UELE_MATKMAX: ed->base_status.matk_max = (unsigned short)value; break;
  15401. case UELE_DEF: ed->base_status.def = (defType)value; break;
  15402. case UELE_MDEF: ed->base_status.mdef = (defType)value; break;
  15403. case UELE_HIT: ed->base_status.hit = (short)value; break;
  15404. case UELE_FLEE: ed->base_status.flee = (short)value; break;
  15405. case UELE_PDODGE: ed->base_status.flee2 = (short)value; break;
  15406. case UELE_CRIT: ed->base_status.cri = (short)value; break;
  15407. case UELE_RACE: ed->base_status.race = (unsigned char)value; break;
  15408. case UELE_ELETYPE: ed->base_status.def_ele = (unsigned char)value; break;
  15409. case UELE_ELELEVEL: ed->base_status.ele_lv = (unsigned char)value; break;
  15410. case UELE_AMOTION: ed->base_status.amotion = (short)value; break;
  15411. case UELE_ADELAY: ed->base_status.adelay = (short)value; break;
  15412. case UELE_DMOTION: ed->base_status.dmotion = (short)value; break;
  15413. default:
  15414. ShowError("buildin_setunitdata: Unknown data identifier %d for BL_ELEM.\n", type);
  15415. return SCRIPT_CMD_FAILURE;
  15416. }
  15417. break;
  15418. case BL_NPC:
  15419. if (!nd) {
  15420. ShowWarning("buildin_setunitdata: Error in finding object BL_NPC!\n");
  15421. return SCRIPT_CMD_FAILURE;
  15422. }
  15423. switch (type) {
  15424. case UNPC_DISPLAY: status_set_viewdata(bl, (unsigned short)value); break;
  15425. case UNPC_LEVEL: nd->level = (unsigned int)value; break;
  15426. case UNPC_HP: status_set_hp(bl, (unsigned int)value, 0); break;
  15427. case UNPC_MAXHP: status_set_maxhp(bl, (unsigned int)value, 0); break;
  15428. case UNPC_MAPID: if (mapname) value = map_mapname2mapid(mapname); unit_warp(bl, (short)value, 0, 0, CLR_TELEPORT); break;
  15429. case UNPC_X: if (!unit_walktoxy(bl, (short)value, nd->bl.y, 2)) unit_movepos(bl, (short)value, nd->bl.x, 0, 0); break;
  15430. case UNPC_Y: if (!unit_walktoxy(bl, nd->bl.x, (short)value, 2)) unit_movepos(bl, nd->bl.x, (short)value, 0, 0); break;
  15431. case UNPC_LOOKDIR: unit_setdir(bl, (uint8)value); break;
  15432. case UNPC_STR: nd->params.str = (unsigned short)value; status_calc_misc(bl, &nd->status, nd->level); break;
  15433. case UNPC_AGI: nd->params.agi = (unsigned short)value; status_calc_misc(bl, &nd->status, nd->level); break;
  15434. case UNPC_VIT: nd->params.vit = (unsigned short)value; status_calc_misc(bl, &nd->status, nd->level); break;
  15435. case UNPC_INT: nd->params.int_ = (unsigned short)value; status_calc_misc(bl, &nd->status, nd->level); break;
  15436. case UNPC_DEX: nd->params.dex = (unsigned short)value; status_calc_misc(bl, &nd->status, nd->level); break;
  15437. case UNPC_LUK: nd->params.luk = (unsigned short)value; status_calc_misc(bl, &nd->status, nd->level); break;
  15438. case UNPC_PLUSALLSTAT: nd->stat_point = (unsigned int)value; break;
  15439. case UNPC_ATKRANGE: nd->status.rhw.range = (unsigned short)value; break;
  15440. case UNPC_ATKMIN: nd->status.rhw.atk = (unsigned short)value; break;
  15441. case UNPC_ATKMAX: nd->status.rhw.atk2 = (unsigned short)value; break;
  15442. case UNPC_MATKMIN: nd->status.matk_min = (unsigned short)value; break;
  15443. case UNPC_MATKMAX: nd->status.matk_max = (unsigned short)value; break;
  15444. case UNPC_DEF: nd->status.def = (defType)value; break;
  15445. case UNPC_MDEF: nd->status.mdef = (defType)value; break;
  15446. case UNPC_HIT: nd->status.hit = (short)value; break;
  15447. case UNPC_FLEE: nd->status.flee = (short)value; break;
  15448. case UNPC_PDODGE: nd->status.flee2 = (short)value; break;
  15449. case UNPC_CRIT: nd->status.cri = (short)value; break;
  15450. case UNPC_RACE: nd->status.race = (unsigned char)value; break;
  15451. case UNPC_ELETYPE: nd->status.def_ele = (unsigned char)value; break;
  15452. case UNPC_ELELEVEL: nd->status.ele_lv = (unsigned char)value; break;
  15453. case UNPC_AMOTION: nd->status.amotion = (short)value; break;
  15454. case UNPC_ADELAY: nd->status.adelay = (short)value; break;
  15455. case UNPC_DMOTION: nd->status.dmotion = (short)value; break;
  15456. default:
  15457. ShowError("buildin_setunitdata: Unknown data identifier %d for BL_NPC.\n", type);
  15458. return SCRIPT_CMD_FAILURE;
  15459. }
  15460. break;
  15461. default:
  15462. ShowWarning("buildin_setunitdata: Unknown object type!\n");
  15463. return SCRIPT_CMD_FAILURE;
  15464. }
  15465. // Client information updates
  15466. switch (bl->type) {
  15467. case BL_HOM:
  15468. clif_send_homdata(hd->master, SP_ACK, 0);
  15469. break;
  15470. case BL_PET:
  15471. clif_send_petstatus(pd->master);
  15472. break;
  15473. case BL_MER:
  15474. clif_mercenary_info(map_charid2sd(mc->mercenary.char_id));
  15475. clif_mercenary_skillblock(map_charid2sd(mc->mercenary.char_id));
  15476. break;
  15477. case BL_ELEM:
  15478. clif_elemental_info(ed->master);
  15479. break;
  15480. }
  15481. return SCRIPT_CMD_SUCCESS;
  15482. }
  15483. /// Gets the name of a bl.
  15484. /// Supported types are [MOB|HOM|PET|NPC].
  15485. /// MER and ELEM don't support custom names.
  15486. ///
  15487. /// getunitname <unit id>;
  15488. BUILDIN_FUNC(getunitname)
  15489. {
  15490. struct block_list* bl = NULL;
  15491. if(!script_rid2bl(2,bl)){
  15492. script_pushconststr(st, "Unknown");
  15493. return SCRIPT_CMD_FAILURE;
  15494. }
  15495. script_pushstrcopy(st, status_get_name(bl));
  15496. return SCRIPT_CMD_SUCCESS;
  15497. }
  15498. /// Changes the name of a bl.
  15499. /// Supported types are [MOB|HOM|PET].
  15500. /// For NPC see 'setnpcdisplay', MER and ELEM don't support custom names.
  15501. ///
  15502. /// setunitname <unit id>,<name>;
  15503. BUILDIN_FUNC(setunitname)
  15504. {
  15505. struct block_list* bl = NULL;
  15506. TBL_MOB* md = NULL;
  15507. TBL_HOM* hd = NULL;
  15508. TBL_PET* pd = NULL;
  15509. if(!script_rid2bl(2,bl))
  15510. {
  15511. script_pushconststr(st, "Unknown");
  15512. return SCRIPT_CMD_FAILURE;
  15513. }
  15514. switch (bl->type) {
  15515. case BL_MOB: md = map_id2md(bl->id); break;
  15516. case BL_HOM: hd = map_id2hd(bl->id); break;
  15517. case BL_PET: pd = map_id2pd(bl->id); break;
  15518. }
  15519. switch (bl->type) {
  15520. case BL_MOB:
  15521. if (!md) {
  15522. ShowWarning("buildin_setunitname: Error in finding object BL_MOB!\n");
  15523. return SCRIPT_CMD_FAILURE;
  15524. }
  15525. safestrncpy(md->name, script_getstr(st, 3), NAME_LENGTH);
  15526. break;
  15527. case BL_HOM:
  15528. if (!hd) {
  15529. ShowWarning("buildin_setunitname: Error in finding object BL_HOM!\n");
  15530. return SCRIPT_CMD_FAILURE;
  15531. }
  15532. safestrncpy(hd->homunculus.name, script_getstr(st, 3), NAME_LENGTH);
  15533. break;
  15534. case BL_PET:
  15535. if (!pd) {
  15536. ShowWarning("buildin_setunitname: Error in finding object BL_PET!\n");
  15537. return SCRIPT_CMD_FAILURE;
  15538. }
  15539. safestrncpy(pd->pet.name, script_getstr(st, 3), NAME_LENGTH);
  15540. break;
  15541. default:
  15542. ShowWarning("buildin_setunitname: Unknown object type!\n");
  15543. return SCRIPT_CMD_FAILURE;
  15544. }
  15545. clif_name_area(bl); // Send update to client.
  15546. return SCRIPT_CMD_SUCCESS;
  15547. }
  15548. /// Makes the unit walk to target position or map.
  15549. /// Returns if it was successful.
  15550. ///
  15551. /// unitwalk(<unit_id>,<x>,<y>{,<event_label>}) -> <bool>
  15552. /// unitwalkto(<unit_id>,<target_id>{,<event_label>}) -> <bool>
  15553. BUILDIN_FUNC(unitwalk)
  15554. {
  15555. struct block_list* bl;
  15556. struct unit_data *ud = NULL;
  15557. const char *cmd = script_getfuncname(st), *done_label = "";
  15558. uint8 off = 5;
  15559. if(!script_rid2bl(2,bl))
  15560. {
  15561. script_pushint(st, 0);
  15562. return SCRIPT_CMD_FAILURE;
  15563. }
  15564. ud = unit_bl2ud(bl);
  15565. if (!strcmp(cmd,"unitwalk")) {
  15566. int x = script_getnum(st,3);
  15567. int y = script_getnum(st,4);
  15568. if (script_pushint(st, unit_can_reach_pos(bl,x,y,0)))
  15569. add_timer(gettick()+50, unit_delay_walktoxy_timer, bl->id, (x<<16)|(y&0xFFFF)); // Need timer to avoid mismatches
  15570. } else {
  15571. struct block_list* tbl = map_id2bl(script_getnum(st,3));
  15572. if (!tbl) {
  15573. ShowError("buildin_unitwalk: Bad target destination.\n");
  15574. script_pushint(st, 0);
  15575. return SCRIPT_CMD_FAILURE;
  15576. } else if (script_pushint(st, unit_can_reach_bl(bl, tbl, distance_bl(bl, tbl)+1, 0, NULL, NULL)))
  15577. add_timer(gettick()+50, unit_delay_walktobl_timer, bl->id, tbl->id); // Need timer to avoid mismatches
  15578. off = 4;
  15579. }
  15580. if (ud && script_hasdata(st, off)) {
  15581. done_label = script_getstr(st, off);
  15582. check_event(st, done_label);
  15583. safestrncpy(ud->walk_done_event, done_label, sizeof(ud->walk_done_event));
  15584. }
  15585. return SCRIPT_CMD_SUCCESS;
  15586. }
  15587. /// Kills the unit.
  15588. ///
  15589. /// unitkill <unit_id>;
  15590. BUILDIN_FUNC(unitkill)
  15591. {
  15592. struct block_list* bl;
  15593. if(script_rid2bl(2,bl))
  15594. status_kill(bl);
  15595. return SCRIPT_CMD_SUCCESS;
  15596. }
  15597. /// Warps the unit to the target position in the target map.
  15598. /// Returns if it was successful.
  15599. ///
  15600. /// unitwarp(<unit_id>,"<map name>",<x>,<y>) -> <bool>
  15601. BUILDIN_FUNC(unitwarp)
  15602. {
  15603. int map_idx;
  15604. short x;
  15605. short y;
  15606. struct block_list* bl;
  15607. const char *mapname;
  15608. mapname = script_getstr(st, 3);
  15609. x = (short)script_getnum(st,4);
  15610. y = (short)script_getnum(st,5);
  15611. if(!script_rid2bl(2,bl))
  15612. {
  15613. script_pushint(st, 0);
  15614. return SCRIPT_CMD_SUCCESS;
  15615. }
  15616. if (!strcmp(mapname,"this"))
  15617. map_idx = bl?bl->m:-1;
  15618. else
  15619. map_idx = map_mapname2mapid(mapname);
  15620. if (map_idx >= 0 && bl != NULL)
  15621. script_pushint(st, unit_warp(bl,map_idx,x,y,CLR_OUTSIGHT));
  15622. else
  15623. script_pushint(st, 0);
  15624. return SCRIPT_CMD_SUCCESS;
  15625. }
  15626. /// Makes the unit attack the target.
  15627. /// If the unit is a player and <action type> is not 0, it does a continuous
  15628. /// attack instead of a single attack.
  15629. /// Returns if the request was successful.
  15630. ///
  15631. /// unitattack(<unit_id>,"<target name>"{,<action type>}) -> <bool>
  15632. /// unitattack(<unit_id>,<target_id>{,<action type>}) -> <bool>
  15633. BUILDIN_FUNC(unitattack)
  15634. {
  15635. struct block_list* unit_bl;
  15636. struct block_list* target_bl = NULL;
  15637. struct script_data* data;
  15638. int actiontype = 0;
  15639. if (!script_rid2bl(2,unit_bl)) {
  15640. script_pushint(st, 0);
  15641. return SCRIPT_CMD_FAILURE;
  15642. }
  15643. data = script_getdata(st, 3);
  15644. get_val(st, data);
  15645. if (data_isstring(data)) {
  15646. TBL_PC* sd = map_nick2sd(conv_str(st, data),false);
  15647. if( sd != NULL )
  15648. target_bl = &sd->bl;
  15649. } else
  15650. target_bl = map_id2bl(conv_num(st, data));
  15651. if (!target_bl) {
  15652. script_pushint(st, 0);
  15653. return SCRIPT_CMD_FAILURE;
  15654. }
  15655. if (script_hasdata(st,4))
  15656. actiontype = script_getnum(st,4);
  15657. switch(unit_bl->type) {
  15658. case BL_PC:
  15659. clif_parse_ActionRequest_sub(((TBL_PC *)unit_bl), actiontype > 0 ? 0x07 : 0x00, target_bl->id, gettick());
  15660. script_pushint(st, 1);
  15661. return SCRIPT_CMD_SUCCESS;
  15662. case BL_MOB:
  15663. ((TBL_MOB *)unit_bl)->target_id = target_bl->id;
  15664. break;
  15665. case BL_PET:
  15666. ((TBL_PET *)unit_bl)->target_id = target_bl->id;
  15667. break;
  15668. default:
  15669. ShowError("buildin_unitattack: Unsupported source unit type %d.\n", unit_bl->type);
  15670. script_pushint(st, 0);
  15671. return SCRIPT_CMD_FAILURE;
  15672. }
  15673. script_pushint(st, unit_walktobl(unit_bl, target_bl, 65025, 2));
  15674. return SCRIPT_CMD_SUCCESS;
  15675. }
  15676. /// Makes the unit stop attacking.
  15677. ///
  15678. /// unitstopattack <unit_id>;
  15679. BUILDIN_FUNC(unitstopattack)
  15680. {
  15681. struct block_list* bl;
  15682. if(script_rid2bl(2,bl))
  15683. {
  15684. unit_stop_attack(bl);
  15685. if (bl->type == BL_MOB)
  15686. ((TBL_MOB*)bl)->target_id = 0;
  15687. }
  15688. return SCRIPT_CMD_SUCCESS;
  15689. }
  15690. /// Makes the unit stop walking.
  15691. ///
  15692. /// unitstopwalk <unit_id>;
  15693. BUILDIN_FUNC(unitstopwalk)
  15694. {
  15695. struct block_list* bl;
  15696. if(script_rid2bl(2,bl))
  15697. unit_stop_walking(bl, 0);
  15698. return SCRIPT_CMD_SUCCESS;
  15699. }
  15700. /// Makes the unit say the given message.
  15701. ///
  15702. /// unittalk <unit_id>,"<message>";
  15703. BUILDIN_FUNC(unittalk)
  15704. {
  15705. const char* message;
  15706. struct block_list* bl;
  15707. message = script_getstr(st, 3);
  15708. if(script_rid2bl(2,bl))
  15709. {
  15710. struct StringBuf sbuf;
  15711. StringBuf_Init(&sbuf);
  15712. StringBuf_Printf(&sbuf, "%s", message);
  15713. clif_disp_overhead(bl, StringBuf_Value(&sbuf));
  15714. StringBuf_Destroy(&sbuf);
  15715. }
  15716. return SCRIPT_CMD_SUCCESS;
  15717. }
  15718. /// Makes the unit do an emotion.
  15719. ///
  15720. /// unitemote <unit_id>,<emotion>;
  15721. ///
  15722. /// @see e_* in db/const.txt
  15723. BUILDIN_FUNC(unitemote)
  15724. {
  15725. int emotion;
  15726. struct block_list* bl;
  15727. emotion = script_getnum(st,3);
  15728. if(script_rid2bl(2,bl))
  15729. clif_emotion(bl, emotion);
  15730. return SCRIPT_CMD_SUCCESS;
  15731. }
  15732. /// Makes the unit cast the skill on the target or self if no target is specified.
  15733. ///
  15734. /// unitskilluseid <unit_id>,<skill_id>,<skill_lv>{,<target_id>,<casttime>};
  15735. /// unitskilluseid <unit_id>,"<skill name>",<skill_lv>{,<target_id>,<casttime>};
  15736. BUILDIN_FUNC(unitskilluseid)
  15737. {
  15738. int unit_id, target_id, casttime;
  15739. uint16 skill_id, skill_lv;
  15740. struct block_list* bl;
  15741. struct script_data *data;
  15742. unit_id = script_getnum(st,2);
  15743. data = script_getdata(st, 3);
  15744. get_val(st, data); // Convert into value in case of a variable
  15745. skill_id = ( data_isstring(data) ? skill_name2id(script_getstr(st,3)) : script_getnum(st,3) );
  15746. skill_lv = script_getnum(st,4);
  15747. target_id = ( script_hasdata(st,5) ? script_getnum(st,5) : unit_id );
  15748. casttime = ( script_hasdata(st,6) ? script_getnum(st,6) : 0 );
  15749. if(script_rid2bl(2,bl)){
  15750. if (bl->type == BL_NPC) {
  15751. if (!((TBL_NPC*)bl)->status.hp)
  15752. status_calc_npc(((TBL_NPC*)bl), SCO_FIRST);
  15753. else
  15754. status_calc_npc(((TBL_NPC*)bl), SCO_NONE);
  15755. }
  15756. unit_skilluse_id2(bl, target_id, skill_id, skill_lv, (casttime * 1000) + skill_castfix(bl, skill_id, skill_lv), skill_get_castcancel(skill_id));
  15757. }
  15758. return SCRIPT_CMD_SUCCESS;
  15759. }
  15760. /// Makes the unit cast the skill on the target position.
  15761. ///
  15762. /// unitskillusepos <unit_id>,<skill_id>,<skill_lv>,<target_x>,<target_y>{,<casttime>};
  15763. /// unitskillusepos <unit_id>,"<skill name>",<skill_lv>,<target_x>,<target_y>{,<casttime>};
  15764. BUILDIN_FUNC(unitskillusepos)
  15765. {
  15766. int skill_x, skill_y, casttime;
  15767. uint16 skill_id, skill_lv;
  15768. struct block_list* bl;
  15769. struct script_data *data;
  15770. data = script_getdata(st, 3);
  15771. get_val(st, data); // Convert into value in case of a variable
  15772. skill_id = ( data_isstring(data) ? skill_name2id(script_getstr(st,3)) : script_getnum(st,3) );
  15773. skill_lv = script_getnum(st,4);
  15774. skill_x = script_getnum(st,5);
  15775. skill_y = script_getnum(st,6);
  15776. casttime = ( script_hasdata(st,7) ? script_getnum(st,7) : 0 );
  15777. if(script_rid2bl(2,bl)){
  15778. if (bl->type == BL_NPC) {
  15779. if (!((TBL_NPC*)bl)->status.hp)
  15780. status_calc_npc(((TBL_NPC*)bl), SCO_FIRST);
  15781. else
  15782. status_calc_npc(((TBL_NPC*)bl), SCO_NONE);
  15783. }
  15784. 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));
  15785. }
  15786. return SCRIPT_CMD_SUCCESS;
  15787. }
  15788. // <--- [zBuffer] List of unit control commands
  15789. /// Pauses the execution of the script, detaching the player
  15790. ///
  15791. /// sleep <mili seconds>;
  15792. BUILDIN_FUNC(sleep)
  15793. {
  15794. int ticks;
  15795. ticks = script_getnum(st,2);
  15796. // detach the player
  15797. script_detach_rid(st);
  15798. if( ticks <= 0 )
  15799. {// do nothing
  15800. }
  15801. else if( st->sleep.tick == 0 )
  15802. {// sleep for the target amount of time
  15803. st->state = RERUNLINE;
  15804. st->sleep.tick = ticks;
  15805. }
  15806. else
  15807. {// sleep time is over
  15808. st->state = RUN;
  15809. st->sleep.tick = 0;
  15810. }
  15811. return SCRIPT_CMD_SUCCESS;
  15812. }
  15813. /// Pauses the execution of the script, keeping the unit attached
  15814. /// Returns if the unit is still attached
  15815. ///
  15816. /// sleep2(<mili secconds>) -> <bool>
  15817. BUILDIN_FUNC(sleep2)
  15818. {
  15819. int ticks;
  15820. ticks = script_getnum(st,2);
  15821. if( ticks <= 0 )
  15822. {// do nothing
  15823. script_pushint(st, (map_id2bl(st->rid)!=NULL));
  15824. }
  15825. else if( !st->sleep.tick )
  15826. {// sleep for the target amount of time
  15827. st->state = RERUNLINE;
  15828. st->sleep.tick = ticks;
  15829. }
  15830. else
  15831. {// sleep time is over
  15832. st->state = RUN;
  15833. st->sleep.tick = 0;
  15834. script_pushint(st, (map_id2bl(st->rid)!=NULL));
  15835. }
  15836. return SCRIPT_CMD_SUCCESS;
  15837. }
  15838. /// Awakes all the sleep timers of the target npc
  15839. ///
  15840. /// awake "<npc name>";
  15841. BUILDIN_FUNC(awake)
  15842. {
  15843. DBIterator *iter;
  15844. struct script_state *tst;
  15845. struct npc_data* nd;
  15846. if ((nd = npc_name2id(script_getstr(st, 2))) == NULL) {
  15847. ShowError("buildin_awake: NPC \"%s\" not found\n", script_getstr(st, 2));
  15848. return SCRIPT_CMD_FAILURE;
  15849. }
  15850. iter = db_iterator(st_db);
  15851. for (tst = dbi_first(iter); dbi_exists(iter); tst = dbi_next(iter)) {
  15852. if (tst->oid == nd->bl.id) {
  15853. if (tst->sleep.timer == INVALID_TIMER) { // already awake ???
  15854. continue;
  15855. }
  15856. delete_timer(tst->sleep.timer, run_script_timer);
  15857. // Trigger the timer function
  15858. run_script_timer(INVALID_TIMER, gettick(), tst->sleep.charid, (intptr_t)tst);
  15859. }
  15860. }
  15861. dbi_destroy(iter);
  15862. return SCRIPT_CMD_SUCCESS;
  15863. }
  15864. /// Returns a reference to a variable of the target NPC.
  15865. /// Returns 0 if an error occurs.
  15866. ///
  15867. /// getvariableofnpc(<variable>, "<npc name>") -> <reference>
  15868. BUILDIN_FUNC(getvariableofnpc)
  15869. {
  15870. struct script_data* data;
  15871. const char* name;
  15872. struct npc_data* nd;
  15873. data = script_getdata(st,2);
  15874. if( !data_isreference(data) )
  15875. {// Not a reference (aka varaible name)
  15876. ShowError("buildin_getvariableofnpc: not a variable\n");
  15877. script_reportdata(data);
  15878. script_pushnil(st);
  15879. st->state = END;
  15880. return SCRIPT_CMD_FAILURE;
  15881. }
  15882. name = reference_getname(data);
  15883. if( *name != '.' || name[1] == '@' )
  15884. {// not a npc variable
  15885. ShowError("buildin_getvariableofnpc: invalid scope (not npc variable)\n");
  15886. script_reportdata(data);
  15887. script_pushnil(st);
  15888. st->state = END;
  15889. return SCRIPT_CMD_FAILURE;
  15890. }
  15891. nd = npc_name2id(script_getstr(st,3));
  15892. if( nd == NULL || nd->subtype != NPCTYPE_SCRIPT || nd->u.scr.script == NULL )
  15893. {// NPC not found or has no script
  15894. ShowError("buildin_getvariableofnpc: can't find npc %s\n", script_getstr(st,3));
  15895. script_pushnil(st);
  15896. st->state = END;
  15897. return SCRIPT_CMD_FAILURE;
  15898. }
  15899. if (!nd->u.scr.script->local.vars)
  15900. nd->u.scr.script->local.vars = i64db_alloc(DB_OPT_RELEASE_DATA);
  15901. push_val2(st->stack, C_NAME, reference_getuid(data), &nd->u.scr.script->local);
  15902. return SCRIPT_CMD_SUCCESS;
  15903. }
  15904. /// Opens a warp portal.
  15905. /// Has no "portal opening" effect/sound, it opens the portal immediately.
  15906. ///
  15907. /// warpportal <source x>,<source y>,"<target map>",<target x>,<target y>;
  15908. ///
  15909. /// @author blackhole89
  15910. BUILDIN_FUNC(warpportal)
  15911. {
  15912. int spx;
  15913. int spy;
  15914. unsigned short mapindex;
  15915. int tpx;
  15916. int tpy;
  15917. struct skill_unit_group* group;
  15918. struct block_list* bl;
  15919. bl = map_id2bl(st->oid);
  15920. if( bl == NULL ) {
  15921. ShowError("buildin_warpportal: NPC is needed\n");
  15922. return SCRIPT_CMD_FAILURE;
  15923. }
  15924. spx = script_getnum(st,2);
  15925. spy = script_getnum(st,3);
  15926. mapindex = mapindex_name2id(script_getstr(st, 4));
  15927. tpx = script_getnum(st,5);
  15928. tpy = script_getnum(st,6);
  15929. if( mapindex == 0 ) {
  15930. ShowError("buildin_warpportal: Target map not found %s.\n", script_getstr(st, 4));
  15931. return SCRIPT_CMD_FAILURE;
  15932. }
  15933. group = skill_unitsetting(bl, AL_WARP, 4, spx, spy, 0);
  15934. if( group == NULL )
  15935. return SCRIPT_CMD_FAILURE;// failed
  15936. group->val1 = (group->val1<<16)|(short)0;
  15937. group->val2 = (tpx<<16) | tpy;
  15938. group->val3 = mapindex;
  15939. return SCRIPT_CMD_SUCCESS;
  15940. }
  15941. /**
  15942. * openmail({<char_id>});
  15943. **/
  15944. BUILDIN_FUNC(openmail)
  15945. {
  15946. TBL_PC* sd;
  15947. if (!script_charid2sd(2,sd))
  15948. return SCRIPT_CMD_FAILURE;
  15949. mail_openmail(sd);
  15950. return SCRIPT_CMD_SUCCESS;
  15951. }
  15952. /**
  15953. * openauction({<char_id>});
  15954. **/
  15955. BUILDIN_FUNC(openauction)
  15956. {
  15957. TBL_PC* sd;
  15958. if (!script_charid2sd(2,sd))
  15959. return SCRIPT_CMD_FAILURE;
  15960. if( !battle_config.feature_auction ) {
  15961. clif_messagecolor(&sd->bl, color_table[COLOR_RED], msg_txt(sd, 517), false, SELF);
  15962. return SCRIPT_CMD_SUCCESS;
  15963. }
  15964. clif_Auction_openwindow(sd);
  15965. return SCRIPT_CMD_SUCCESS;
  15966. }
  15967. /// Retrieves the value of the specified flag of the specified cell.
  15968. ///
  15969. /// checkcell("<map name>",<x>,<y>,<type>) -> <bool>
  15970. ///
  15971. /// @see cell_chk* constants in const.txt for the types
  15972. BUILDIN_FUNC(checkcell)
  15973. {
  15974. int16 m = map_mapname2mapid(script_getstr(st,2));
  15975. int16 x = script_getnum(st,3);
  15976. int16 y = script_getnum(st,4);
  15977. cell_chk type = (cell_chk)script_getnum(st,5);
  15978. script_pushint(st, map_getcell(m, x, y, type));
  15979. return SCRIPT_CMD_SUCCESS;
  15980. }
  15981. /// Modifies flags of cells in the specified area.
  15982. ///
  15983. /// setcell "<map name>",<x1>,<y1>,<x2>,<y2>,<type>,<flag>;
  15984. ///
  15985. /// @see cell_* constants in const.txt for the types
  15986. BUILDIN_FUNC(setcell)
  15987. {
  15988. int16 m = map_mapname2mapid(script_getstr(st,2));
  15989. int16 x1 = script_getnum(st,3);
  15990. int16 y1 = script_getnum(st,4);
  15991. int16 x2 = script_getnum(st,5);
  15992. int16 y2 = script_getnum(st,6);
  15993. cell_t type = (cell_t)script_getnum(st,7);
  15994. bool flag = (bool)script_getnum(st,8);
  15995. int x,y;
  15996. if( x1 > x2 ) swap(x1,x2);
  15997. if( y1 > y2 ) swap(y1,y2);
  15998. for( y = y1; y <= y2; ++y )
  15999. for( x = x1; x <= x2; ++x )
  16000. map_setcell(m, x, y, type, flag);
  16001. return SCRIPT_CMD_SUCCESS;
  16002. }
  16003. /*==========================================
  16004. * Mercenary Commands
  16005. *------------------------------------------*/
  16006. BUILDIN_FUNC(mercenary_create)
  16007. {
  16008. struct map_session_data *sd;
  16009. int class_, contract_time;
  16010. if( (sd = script_rid2sd(st)) == NULL || sd->md || sd->status.mer_id != 0 )
  16011. return SCRIPT_CMD_SUCCESS;
  16012. class_ = script_getnum(st,2);
  16013. if( !mercenary_class(class_) )
  16014. return SCRIPT_CMD_SUCCESS;
  16015. contract_time = script_getnum(st,3);
  16016. mercenary_create(sd, class_, contract_time);
  16017. return SCRIPT_CMD_SUCCESS;
  16018. }
  16019. BUILDIN_FUNC(mercenary_heal)
  16020. {
  16021. struct map_session_data *sd = script_rid2sd(st);
  16022. int hp, sp;
  16023. if( sd == NULL || sd->md == NULL )
  16024. return SCRIPT_CMD_SUCCESS;
  16025. hp = script_getnum(st,2);
  16026. sp = script_getnum(st,3);
  16027. status_heal(&sd->md->bl, hp, sp, 0);
  16028. return SCRIPT_CMD_SUCCESS;
  16029. }
  16030. BUILDIN_FUNC(mercenary_sc_start)
  16031. {
  16032. struct map_session_data *sd = script_rid2sd(st);
  16033. enum sc_type type;
  16034. int tick, val1;
  16035. if( sd == NULL || sd->md == NULL )
  16036. return SCRIPT_CMD_SUCCESS;
  16037. type = (sc_type)script_getnum(st,2);
  16038. tick = script_getnum(st,3);
  16039. val1 = script_getnum(st,4);
  16040. status_change_start(NULL, &sd->md->bl, type, 10000, val1, 0, 0, 0, tick, SCSTART_NOTICKDEF);
  16041. return SCRIPT_CMD_SUCCESS;
  16042. }
  16043. BUILDIN_FUNC(mercenary_get_calls)
  16044. {
  16045. struct map_session_data *sd = script_rid2sd(st);
  16046. int guild;
  16047. if( sd == NULL )
  16048. return SCRIPT_CMD_SUCCESS;
  16049. guild = script_getnum(st,2);
  16050. switch( guild )
  16051. {
  16052. case ARCH_MERC_GUILD:
  16053. script_pushint(st,sd->status.arch_calls);
  16054. break;
  16055. case SPEAR_MERC_GUILD:
  16056. script_pushint(st,sd->status.spear_calls);
  16057. break;
  16058. case SWORD_MERC_GUILD:
  16059. script_pushint(st,sd->status.sword_calls);
  16060. break;
  16061. default:
  16062. script_pushint(st,0);
  16063. break;
  16064. }
  16065. return SCRIPT_CMD_SUCCESS;
  16066. }
  16067. BUILDIN_FUNC(mercenary_set_calls)
  16068. {
  16069. struct map_session_data *sd = script_rid2sd(st);
  16070. int guild, value, *calls;
  16071. if( sd == NULL )
  16072. return SCRIPT_CMD_SUCCESS;
  16073. guild = script_getnum(st,2);
  16074. value = script_getnum(st,3);
  16075. switch( guild )
  16076. {
  16077. case ARCH_MERC_GUILD:
  16078. calls = &sd->status.arch_calls;
  16079. break;
  16080. case SPEAR_MERC_GUILD:
  16081. calls = &sd->status.spear_calls;
  16082. break;
  16083. case SWORD_MERC_GUILD:
  16084. calls = &sd->status.sword_calls;
  16085. break;
  16086. default:
  16087. ShowError("buildin_mercenary_set_calls: Invalid guild '%d'.\n", guild);
  16088. return SCRIPT_CMD_SUCCESS; // Invalid Guild
  16089. }
  16090. *calls += value;
  16091. *calls = cap_value(*calls, 0, INT_MAX);
  16092. return SCRIPT_CMD_SUCCESS;
  16093. }
  16094. BUILDIN_FUNC(mercenary_get_faith)
  16095. {
  16096. struct map_session_data *sd = script_rid2sd(st);
  16097. int guild;
  16098. if( sd == NULL )
  16099. return SCRIPT_CMD_SUCCESS;
  16100. guild = script_getnum(st,2);
  16101. switch( guild )
  16102. {
  16103. case ARCH_MERC_GUILD:
  16104. script_pushint(st,sd->status.arch_faith);
  16105. break;
  16106. case SPEAR_MERC_GUILD:
  16107. script_pushint(st,sd->status.spear_faith);
  16108. break;
  16109. case SWORD_MERC_GUILD:
  16110. script_pushint(st,sd->status.sword_faith);
  16111. break;
  16112. default:
  16113. script_pushint(st,0);
  16114. break;
  16115. }
  16116. return SCRIPT_CMD_SUCCESS;
  16117. }
  16118. BUILDIN_FUNC(mercenary_set_faith)
  16119. {
  16120. struct map_session_data *sd = script_rid2sd(st);
  16121. int guild, value, *calls;
  16122. if( sd == NULL )
  16123. return SCRIPT_CMD_SUCCESS;
  16124. guild = script_getnum(st,2);
  16125. value = script_getnum(st,3);
  16126. switch( guild )
  16127. {
  16128. case ARCH_MERC_GUILD:
  16129. calls = &sd->status.arch_faith;
  16130. break;
  16131. case SPEAR_MERC_GUILD:
  16132. calls = &sd->status.spear_faith;
  16133. break;
  16134. case SWORD_MERC_GUILD:
  16135. calls = &sd->status.sword_faith;
  16136. break;
  16137. default:
  16138. ShowError("buildin_mercenary_set_faith: Invalid guild '%d'.\n", guild);
  16139. return SCRIPT_CMD_SUCCESS; // Invalid Guild
  16140. }
  16141. *calls += value;
  16142. *calls = cap_value(*calls, 0, INT_MAX);
  16143. if( mercenary_get_guild(sd->md) == guild )
  16144. clif_mercenary_updatestatus(sd,SP_MERCFAITH);
  16145. return SCRIPT_CMD_SUCCESS;
  16146. }
  16147. /*------------------------------------------
  16148. * Book Reading
  16149. *------------------------------------------*/
  16150. BUILDIN_FUNC(readbook)
  16151. {
  16152. struct map_session_data *sd;
  16153. int book_id, page;
  16154. if( (sd = script_rid2sd(st)) == NULL )
  16155. return SCRIPT_CMD_SUCCESS;
  16156. book_id = script_getnum(st,2);
  16157. page = script_getnum(st,3);
  16158. clif_readbook(sd->fd, book_id, page);
  16159. return SCRIPT_CMD_SUCCESS;
  16160. }
  16161. /******************
  16162. Questlog script commands
  16163. *******************/
  16164. /**
  16165. * Add job criteria to questinfo
  16166. * @param qi Quest Info
  16167. * @param job
  16168. * @author [Cydh]
  16169. **/
  16170. static void buildin_questinfo_setjob(struct questinfo *qi, int job) {
  16171. RECREATE(qi->jobid, unsigned short, qi->jobid_count+1);
  16172. qi->jobid[qi->jobid_count++] = job;
  16173. }
  16174. /**
  16175. * questinfo <Quest ID>,<Icon>{,<Map Mark Color>{,<Job Class>}};
  16176. **/
  16177. BUILDIN_FUNC(questinfo)
  16178. {
  16179. TBL_NPC* nd = map_id2nd(st->oid);
  16180. int quest_id, icon;
  16181. struct questinfo qi, *q2;
  16182. if( nd == NULL || nd->bl.m == -1 ) {
  16183. ShowError("buildin_questinfo: No NPC attached.\n");
  16184. return SCRIPT_CMD_FAILURE;
  16185. }
  16186. quest_id = script_getnum(st, 2);
  16187. icon = script_getnum(st, 3);
  16188. #if PACKETVER >= 20120410
  16189. switch(icon){
  16190. case QTYPE_QUEST:
  16191. case QTYPE_QUEST2:
  16192. case QTYPE_JOB:
  16193. case QTYPE_JOB2:
  16194. case QTYPE_EVENT:
  16195. case QTYPE_EVENT2:
  16196. case QTYPE_WARG:
  16197. case QTYPE_WARG2:
  16198. // Leave everything as it is
  16199. break;
  16200. case QTYPE_NONE:
  16201. default:
  16202. // Default to nothing if icon id is invalid.
  16203. icon = QTYPE_NONE;
  16204. break;
  16205. }
  16206. #else
  16207. if(icon < QTYPE_QUEST || icon > 7) // TODO: check why 7 and not QTYPE_WARG, might be related to icon + 1 below
  16208. icon = QTYPE_QUEST;
  16209. else
  16210. icon = icon + 1;
  16211. #endif
  16212. qi.quest_id = quest_id;
  16213. qi.icon = (unsigned char)icon;
  16214. qi.nd = nd;
  16215. if( script_hasdata(st, 4) ) {
  16216. int color = script_getnum(st, 4);
  16217. if( color < 0 || color > 3 ) {
  16218. ShowWarning("buildin_questinfo: invalid color '%d', changing to 0\n",color);
  16219. script_reportfunc(st);
  16220. color = 0;
  16221. }
  16222. qi.color = (unsigned char)color;
  16223. }
  16224. qi.min_level = 1;
  16225. qi.max_level = MAX_LEVEL;
  16226. q2 = map_add_questinfo(nd->bl.m, &qi);
  16227. q2->req = NULL;
  16228. q2->req_count = 0;
  16229. q2->jobid = NULL;
  16230. q2->jobid_count = 0;
  16231. if(script_hasdata(st, 5)) {
  16232. int job = script_getnum(st, 5);
  16233. if (!pcdb_checkid(job))
  16234. ShowError("buildin_questinfo: Nonexistant Job Class.\n");
  16235. else {
  16236. buildin_questinfo_setjob(q2, job);
  16237. }
  16238. }
  16239. return SCRIPT_CMD_SUCCESS;
  16240. }
  16241. /**
  16242. * setquest <ID>{,<char_id>};
  16243. **/
  16244. BUILDIN_FUNC(setquest)
  16245. {
  16246. struct map_session_data *sd;
  16247. int quest_id;
  16248. quest_id = script_getnum(st, 2);
  16249. if (!script_charid2sd(3,sd))
  16250. return SCRIPT_CMD_FAILURE;
  16251. if( quest_add(sd, quest_id) == -1 ){
  16252. script_reportsrc(st);
  16253. script_reportfunc(st);
  16254. }
  16255. //20120410 or 20090218 ? no reason that shouldn't work for 2009
  16256. pc_show_questinfo(sd);
  16257. return SCRIPT_CMD_SUCCESS;
  16258. }
  16259. /**
  16260. * erasequest <ID>{,<char_id>};
  16261. **/
  16262. BUILDIN_FUNC(erasequest)
  16263. {
  16264. struct map_session_data *sd;
  16265. if (!script_charid2sd(3,sd))
  16266. return SCRIPT_CMD_FAILURE;
  16267. if( quest_delete(sd, script_getnum(st, 2)) == -1 ){
  16268. script_reportsrc(st);
  16269. script_reportfunc(st);
  16270. }
  16271. return SCRIPT_CMD_SUCCESS;
  16272. }
  16273. /**
  16274. * completequest <ID>{,<char_id>};
  16275. **/
  16276. BUILDIN_FUNC(completequest)
  16277. {
  16278. struct map_session_data *sd;
  16279. if (!script_charid2sd(3,sd))
  16280. return SCRIPT_CMD_FAILURE;
  16281. quest_update_status(sd, script_getnum(st, 2), Q_COMPLETE);
  16282. //20120410 or 20090218
  16283. pc_show_questinfo(sd);
  16284. return SCRIPT_CMD_SUCCESS;
  16285. }
  16286. /**
  16287. * changequest <ID>,<ID2>{,<char_id>};
  16288. **/
  16289. BUILDIN_FUNC(changequest)
  16290. {
  16291. struct map_session_data *sd;
  16292. if (!script_charid2sd(4,sd))
  16293. return SCRIPT_CMD_FAILURE;
  16294. if( quest_change(sd, script_getnum(st, 2),script_getnum(st, 3)) == -1 ){
  16295. script_reportsrc(st);
  16296. script_reportfunc(st);
  16297. }
  16298. //20120410 or 20090218
  16299. pc_show_questinfo(sd);
  16300. return SCRIPT_CMD_SUCCESS;
  16301. }
  16302. /**
  16303. * checkquest(<ID>{,PLAYTIME|HUNTING{,<char_id>}})
  16304. **/
  16305. BUILDIN_FUNC(checkquest)
  16306. {
  16307. struct map_session_data *sd;
  16308. enum quest_check_type type = HAVEQUEST;
  16309. if( script_hasdata(st, 3) )
  16310. type = (enum quest_check_type)script_getnum(st, 3);
  16311. if (!script_charid2sd(4,sd))
  16312. return SCRIPT_CMD_FAILURE;
  16313. script_pushint(st, quest_check(sd, script_getnum(st, 2), type));
  16314. return SCRIPT_CMD_SUCCESS;
  16315. }
  16316. /**
  16317. * isbegin_quest(<ID>{,<char_id>})
  16318. **/
  16319. BUILDIN_FUNC(isbegin_quest)
  16320. {
  16321. struct map_session_data *sd;
  16322. int i;
  16323. if (!script_charid2sd(3,sd))
  16324. return SCRIPT_CMD_FAILURE;
  16325. i = quest_check(sd, script_getnum(st, 2), (enum quest_check_type) HAVEQUEST);
  16326. script_pushint(st, i + (i < 1));
  16327. return SCRIPT_CMD_SUCCESS;
  16328. }
  16329. /**
  16330. * showevent <icon>{,<mark color>{,<char_id>}}
  16331. **/
  16332. BUILDIN_FUNC(showevent)
  16333. {
  16334. TBL_PC *sd;
  16335. struct npc_data *nd = map_id2nd(st->oid);
  16336. int icon, color = 0;
  16337. if (!script_charid2sd(4,sd))
  16338. return SCRIPT_CMD_FAILURE;
  16339. if( sd == NULL || nd == NULL )
  16340. return SCRIPT_CMD_SUCCESS;
  16341. icon = script_getnum(st, 2);
  16342. if( script_hasdata(st, 3) ) {
  16343. color = script_getnum(st, 3);
  16344. if( color < 0 || color > 3 ) {
  16345. ShowWarning("buildin_showevent: invalid color '%d', changing to 0\n",color);
  16346. script_reportfunc(st);
  16347. color = 0;
  16348. }
  16349. }
  16350. #if PACKETVER >= 20120410
  16351. if(icon < 0 || (icon > 8 && icon != 9999) || icon == 7)
  16352. icon = 9999; // Default to nothing if icon id is invalid.
  16353. #else
  16354. if(icon < 0 || icon > 7)
  16355. icon = 0;
  16356. else
  16357. icon = icon + 1;
  16358. #endif
  16359. clif_quest_show_event(sd, &nd->bl, icon, color);
  16360. return SCRIPT_CMD_SUCCESS;
  16361. }
  16362. /*==========================================
  16363. * BattleGround System
  16364. *------------------------------------------*/
  16365. BUILDIN_FUNC(waitingroom2bg)
  16366. {
  16367. struct npc_data *nd;
  16368. struct chat_data *cd;
  16369. const char *map_name, *ev = "", *dev = "";
  16370. int x, y, mapindex = 0, bg_id;
  16371. unsigned char i,c=0;
  16372. if( script_hasdata(st,7) )
  16373. nd = npc_name2id(script_getstr(st,7));
  16374. else
  16375. nd = (struct npc_data *)map_id2bl(st->oid);
  16376. if( nd == NULL || (cd = (struct chat_data *)map_id2bl(nd->chat_id)) == NULL )
  16377. {
  16378. script_pushint(st,0);
  16379. return SCRIPT_CMD_SUCCESS;
  16380. }
  16381. map_name = script_getstr(st,2);
  16382. if (strcmp(map_name, "-") != 0 && (mapindex = mapindex_name2id(map_name)) == 0)
  16383. { // Invalid Map
  16384. script_pushint(st, 0);
  16385. return SCRIPT_CMD_SUCCESS;
  16386. }
  16387. x = script_getnum(st,3);
  16388. y = script_getnum(st,4);
  16389. if(script_hasdata(st,5))
  16390. ev = script_getstr(st,5); // Logout Event
  16391. if(script_hasdata(st,6))
  16392. dev = script_getstr(st,6); // Die Event
  16393. check_event(st, ev);
  16394. check_event(st, dev);
  16395. if( (bg_id = bg_create(mapindex, x, y, ev, dev)) == 0 )
  16396. { // Creation failed
  16397. script_pushint(st,0);
  16398. return SCRIPT_CMD_SUCCESS;
  16399. }
  16400. for (i = 0; i < cd->users; i++) { // Only add those who are in the chat room
  16401. struct map_session_data *sd;
  16402. if( (sd = cd->usersd[i]) != NULL && bg_team_join(bg_id, sd) ){
  16403. mapreg_setreg(reference_uid(add_str("$@arenamembers"), c), sd->bl.id);
  16404. ++c;
  16405. }
  16406. }
  16407. mapreg_setreg(add_str("$@arenamembersnum"), c);
  16408. script_pushint(st,bg_id);
  16409. return SCRIPT_CMD_SUCCESS;
  16410. }
  16411. BUILDIN_FUNC(waitingroom2bg_single)
  16412. {
  16413. const char* map_name;
  16414. struct npc_data *nd;
  16415. struct chat_data *cd;
  16416. struct map_session_data *sd;
  16417. struct battleground_data *bg;
  16418. int x, y, mapindex, bg_id;
  16419. bg_id = script_getnum(st,2);
  16420. if ((bg = bg_team_search(bg_id)) == NULL) {
  16421. script_pushint(st, false);
  16422. return SCRIPT_CMD_SUCCESS;
  16423. }
  16424. if (script_hasdata(st, 3)) {
  16425. map_name = script_getstr(st, 3);
  16426. if ((mapindex = mapindex_name2id(map_name)) == 0) {
  16427. script_pushint(st, false);
  16428. return SCRIPT_CMD_SUCCESS; // Invalid Map
  16429. }
  16430. x = script_getnum(st, 4);
  16431. y = script_getnum(st, 5);
  16432. }
  16433. else {
  16434. mapindex = bg->mapindex;
  16435. x = bg->x;
  16436. y = bg->y;
  16437. }
  16438. nd = npc_name2id(script_getstr(st,6));
  16439. if( nd == NULL || (cd = (struct chat_data *)map_id2bl(nd->chat_id)) == NULL || cd->users <= 0 )
  16440. return SCRIPT_CMD_SUCCESS;
  16441. if( (sd = cd->usersd[0]) == NULL )
  16442. return SCRIPT_CMD_SUCCESS;
  16443. if( bg_team_join(bg_id, sd) && pc_setpos(sd, mapindex, x, y, CLR_TELEPORT) == SETPOS_OK)
  16444. {
  16445. script_pushint(st, true);
  16446. }
  16447. else
  16448. script_pushint(st, false);
  16449. return SCRIPT_CMD_SUCCESS;
  16450. }
  16451. /// Creates an instance of battleground battle group.
  16452. /// *bg_create("<map name>",<x>,<y>{,"<On Quit Event>","<On Death Event>"});
  16453. /// @author [secretdataz]
  16454. BUILDIN_FUNC(bg_create) {
  16455. const char *map_name, *ev = "", *dev = "";
  16456. int x, y, mapindex = 0;
  16457. map_name = script_getstr(st, 2);
  16458. if (strcmp(map_name, "-") != 0 && (mapindex = mapindex_name2id(map_name)) == 0)
  16459. { // Invalid Map
  16460. script_pushint(st, 0);
  16461. return SCRIPT_CMD_SUCCESS;
  16462. }
  16463. x = script_getnum(st, 3);
  16464. y = script_getnum(st, 4);
  16465. if(script_hasdata(st, 5))
  16466. ev = script_getstr(st, 5); // Logout Event
  16467. if(script_hasdata(st, 6))
  16468. dev = script_getstr(st, 6); // Die Event
  16469. check_event(st, ev);
  16470. check_event(st, dev);
  16471. script_pushint(st, bg_create(mapindex, x, y, ev, dev));
  16472. return SCRIPT_CMD_SUCCESS;
  16473. }
  16474. /// Adds attached player or <char id> (if specified) to an existing
  16475. /// battleground group and warps it to the specified coordinates on
  16476. /// the given map.
  16477. /// bg_join(<battle group>,{"<map name>",<x>,<y>{,<char id>}});
  16478. /// @author [secretdataz]
  16479. BUILDIN_FUNC(bg_join) {
  16480. const char* map_name;
  16481. struct map_session_data *sd;
  16482. struct battleground_data *bg;
  16483. int x, y, bg_id, mapindex;
  16484. bg_id = script_getnum(st, 2);
  16485. if ((bg = bg_team_search(bg_id)) == NULL) {
  16486. script_pushint(st, false);
  16487. return SCRIPT_CMD_SUCCESS;
  16488. }
  16489. if (script_hasdata(st, 3)) {
  16490. map_name = script_getstr(st, 3);
  16491. if ((mapindex = mapindex_name2id(map_name)) == 0) {
  16492. script_pushint(st, false);
  16493. return SCRIPT_CMD_SUCCESS; // Invalid Map
  16494. }
  16495. x = script_getnum(st, 4);
  16496. y = script_getnum(st, 5);
  16497. } else {
  16498. mapindex = bg->mapindex;
  16499. x = bg->x;
  16500. y = bg->y;
  16501. }
  16502. if (!script_charid2sd(6, sd)) {
  16503. script_pushint(st, false);
  16504. return SCRIPT_CMD_FAILURE;
  16505. }
  16506. if (bg_team_join(bg_id, sd) && pc_setpos(sd, mapindex, x, y, CLR_TELEPORT) == SETPOS_OK)
  16507. {
  16508. script_pushint(st, true);
  16509. }
  16510. else
  16511. script_pushint(st, false);
  16512. return SCRIPT_CMD_SUCCESS;
  16513. }
  16514. BUILDIN_FUNC(bg_team_setxy)
  16515. {
  16516. struct battleground_data *bg;
  16517. int bg_id;
  16518. bg_id = script_getnum(st,2);
  16519. if( (bg = bg_team_search(bg_id)) == NULL )
  16520. return SCRIPT_CMD_SUCCESS;
  16521. bg->x = script_getnum(st,3);
  16522. bg->y = script_getnum(st,4);
  16523. return SCRIPT_CMD_SUCCESS;
  16524. }
  16525. BUILDIN_FUNC(bg_warp)
  16526. {
  16527. int x, y, mapindex, bg_id;
  16528. const char* map_name;
  16529. bg_id = script_getnum(st,2);
  16530. map_name = script_getstr(st,3);
  16531. if( (mapindex = mapindex_name2id(map_name)) == 0 )
  16532. return SCRIPT_CMD_SUCCESS; // Invalid Map
  16533. x = script_getnum(st,4);
  16534. y = script_getnum(st,5);
  16535. bg_team_warp(bg_id, mapindex, x, y);
  16536. return SCRIPT_CMD_SUCCESS;
  16537. }
  16538. BUILDIN_FUNC(bg_monster)
  16539. {
  16540. int class_ = 0, x = 0, y = 0, bg_id = 0;
  16541. const char *str,*mapname, *evt="";
  16542. bg_id = script_getnum(st,2);
  16543. mapname = script_getstr(st,3);
  16544. x = script_getnum(st,4);
  16545. y = script_getnum(st,5);
  16546. str = script_getstr(st,6);
  16547. class_ = script_getnum(st,7);
  16548. if( script_hasdata(st,8) ) evt = script_getstr(st,8);
  16549. check_event(st, evt);
  16550. script_pushint(st, mob_spawn_bg(mapname,x,y,str,class_,evt,bg_id));
  16551. return SCRIPT_CMD_SUCCESS;
  16552. }
  16553. BUILDIN_FUNC(bg_monster_set_team)
  16554. {
  16555. struct mob_data *md;
  16556. struct block_list *mbl;
  16557. int id = script_getnum(st,2),
  16558. bg_id = script_getnum(st,3);
  16559. if( id == 0 || (mbl = map_id2bl(id)) == NULL || mbl->type != BL_MOB )
  16560. return SCRIPT_CMD_SUCCESS;
  16561. md = (TBL_MOB *)mbl;
  16562. md->bg_id = bg_id;
  16563. mob_stop_attack(md);
  16564. mob_stop_walking(md, 0);
  16565. md->target_id = md->attacked_id = 0;
  16566. clif_name_area(&md->bl);
  16567. return SCRIPT_CMD_SUCCESS;
  16568. }
  16569. BUILDIN_FUNC(bg_leave)
  16570. {
  16571. struct map_session_data *sd = NULL;
  16572. if( !script_charid2sd(2,sd) || !sd->bg_id )
  16573. return SCRIPT_CMD_SUCCESS;
  16574. bg_team_leave(sd,0);
  16575. return SCRIPT_CMD_SUCCESS;
  16576. }
  16577. BUILDIN_FUNC(bg_destroy)
  16578. {
  16579. int bg_id = script_getnum(st,2);
  16580. bg_team_delete(bg_id);
  16581. return SCRIPT_CMD_SUCCESS;
  16582. }
  16583. BUILDIN_FUNC(bg_getareausers)
  16584. {
  16585. const char *str;
  16586. int16 m, x0, y0, x1, y1;
  16587. int bg_id;
  16588. int i = 0, c = 0;
  16589. struct battleground_data *bg = NULL;
  16590. bg_id = script_getnum(st,2);
  16591. str = script_getstr(st,3);
  16592. if( (bg = bg_team_search(bg_id)) == NULL || (m = map_mapname2mapid(str)) < 0 )
  16593. {
  16594. script_pushint(st,0);
  16595. return SCRIPT_CMD_SUCCESS;
  16596. }
  16597. x0 = script_getnum(st,4);
  16598. y0 = script_getnum(st,5);
  16599. x1 = script_getnum(st,6);
  16600. y1 = script_getnum(st,7);
  16601. for( i = 0; i < MAX_BG_MEMBERS; i++ )
  16602. {
  16603. struct map_session_data *sd;
  16604. if( (sd = bg->members[i].sd) == NULL )
  16605. continue;
  16606. if( sd->bl.m != m || sd->bl.x < x0 || sd->bl.y < y0 || sd->bl.x > x1 || sd->bl.y > y1 )
  16607. continue;
  16608. c++;
  16609. }
  16610. script_pushint(st,c);
  16611. return SCRIPT_CMD_SUCCESS;
  16612. }
  16613. BUILDIN_FUNC(bg_updatescore)
  16614. {
  16615. const char *str;
  16616. int16 m;
  16617. str = script_getstr(st,2);
  16618. if( (m = map_mapname2mapid(str)) < 0 )
  16619. return SCRIPT_CMD_SUCCESS;
  16620. map[m].bgscore_lion = script_getnum(st,3);
  16621. map[m].bgscore_eagle = script_getnum(st,4);
  16622. clif_bg_updatescore(m);
  16623. return SCRIPT_CMD_SUCCESS;
  16624. }
  16625. BUILDIN_FUNC(bg_get_data)
  16626. {
  16627. struct battleground_data *bg;
  16628. int bg_id = script_getnum(st,2),
  16629. type = script_getnum(st,3);
  16630. if( (bg = bg_team_search(bg_id)) == NULL )
  16631. {
  16632. script_pushint(st,0);
  16633. return SCRIPT_CMD_SUCCESS;
  16634. }
  16635. switch( type )
  16636. {
  16637. case 0: script_pushint(st, bg->count); break;
  16638. default:
  16639. ShowError("script:bg_get_data: unknown data identifier %d\n", type);
  16640. break;
  16641. }
  16642. return SCRIPT_CMD_SUCCESS;
  16643. }
  16644. /*==========================================
  16645. * Instancing System
  16646. *------------------------------------------*/
  16647. //Returns an Instance ID
  16648. //Checks NPC first, then if player is attached we check
  16649. unsigned short script_instancegetid(struct script_state* st)
  16650. {
  16651. unsigned short instance_id = 0;
  16652. struct npc_data *nd;
  16653. if( (nd = map_id2nd(st->oid)) && nd->instance_id > 0 )
  16654. instance_id = nd->instance_id;
  16655. else {
  16656. struct map_session_data *sd = NULL;
  16657. struct party_data *p = NULL;
  16658. struct guild *g = NULL;
  16659. if ((sd = script_rid2sd(st)) != NULL) {
  16660. if (sd->instance_id)
  16661. instance_id = sd->instance_id;
  16662. if (instance_id == 0 && sd->status.party_id && (p = party_search(sd->status.party_id)) != NULL && p->instance_id)
  16663. instance_id = p->instance_id;
  16664. if (instance_id == 0 && sd->status.guild_id && (g = guild_search(sd->status.guild_id)) != NULL && g->instance_id)
  16665. instance_id = g->instance_id;
  16666. }
  16667. }
  16668. return instance_id;
  16669. }
  16670. /*==========================================
  16671. * Creates the instance
  16672. * Returns Instance ID if created successfully
  16673. *------------------------------------------*/
  16674. BUILDIN_FUNC(instance_create)
  16675. {
  16676. enum instance_mode mode = IM_PARTY;
  16677. int owner_id = 0;
  16678. if (script_hasdata(st, 3)) {
  16679. mode = script_getnum(st, 3);
  16680. if (mode < IM_NONE || mode >= IM_MAX) {
  16681. ShowError("buildin_instance_create: Unknown instance mode %d for '%s'\n", mode, script_getstr(st, 2));
  16682. return SCRIPT_CMD_FAILURE;
  16683. }
  16684. }
  16685. if (script_hasdata(st, 4))
  16686. owner_id = script_getnum(st, 4);
  16687. else {
  16688. struct map_session_data *sd = NULL;
  16689. sd = script_rid2sd(st); // If sd is NULL, instance_create will return -2.
  16690. switch(mode) {
  16691. case IM_NONE:
  16692. owner_id = st->oid;
  16693. break;
  16694. case IM_CHAR:
  16695. if (sd)
  16696. owner_id = sd->status.char_id;
  16697. break;
  16698. case IM_PARTY:
  16699. if (sd)
  16700. owner_id = sd->status.party_id;
  16701. break;
  16702. case IM_GUILD:
  16703. if (sd)
  16704. owner_id = sd->status.guild_id;
  16705. break;
  16706. default:
  16707. ShowError("buildin_instance_create: Invalid instance mode (instance name: %s)\n", script_getstr(st, 2));
  16708. return SCRIPT_CMD_FAILURE;
  16709. }
  16710. }
  16711. script_pushint(st, instance_create(owner_id, script_getstr(st, 2), mode));
  16712. return SCRIPT_CMD_SUCCESS;
  16713. }
  16714. /*==========================================
  16715. * Destroys an instance (unofficial)
  16716. * Officially instances are only destroyed by timeout
  16717. *
  16718. * instance_destroy {<instance_id>};
  16719. *------------------------------------------*/
  16720. BUILDIN_FUNC(instance_destroy)
  16721. {
  16722. unsigned short instance_id;
  16723. if( script_hasdata(st,2) )
  16724. instance_id = script_getnum(st,2);
  16725. else
  16726. instance_id = script_instancegetid(st);
  16727. if( instance_id == 0 || instance_id >= MAX_MAP_PER_SERVER ) {
  16728. ShowError("buildin_instance_destroy: Trying to destroy invalid instance %hu.\n", instance_id);
  16729. return SCRIPT_CMD_FAILURE;
  16730. }
  16731. instance_destroy(instance_id);
  16732. return SCRIPT_CMD_SUCCESS;
  16733. }
  16734. /*==========================================
  16735. * Warps player to instance
  16736. * Results:
  16737. * IE_OK: Success
  16738. * IE_NOMEMBER: Character not in party/guild (for party/guild type instances)
  16739. * IE_NOINSTANCE: Character/Party/Guild doesn't have instance
  16740. * IE_OTHER: Other errors (instance not in DB, instance doesn't match with character/party/guild, etc.)
  16741. *------------------------------------------*/
  16742. BUILDIN_FUNC(instance_enter)
  16743. {
  16744. struct map_session_data *sd = NULL;
  16745. int x = script_hasdata(st,3) ? script_getnum(st, 3) : -1;
  16746. int y = script_hasdata(st,4) ? script_getnum(st, 4) : -1;
  16747. unsigned short instance_id;
  16748. if (script_hasdata(st, 6))
  16749. instance_id = script_getnum(st, 6);
  16750. else
  16751. instance_id = script_instancegetid(st);
  16752. if (!script_charid2sd(5,sd))
  16753. return SCRIPT_CMD_FAILURE;
  16754. script_pushint(st, instance_enter(sd, instance_id, script_getstr(st, 2), x, y));
  16755. return SCRIPT_CMD_SUCCESS;
  16756. }
  16757. /*==========================================
  16758. * Returns the name of a duplicated NPC
  16759. *
  16760. * instance_npcname <npc_name>{,<instance_id>};
  16761. * <npc_name> is the full name of an NPC.
  16762. *------------------------------------------*/
  16763. BUILDIN_FUNC(instance_npcname)
  16764. {
  16765. const char *str;
  16766. unsigned short instance_id = 0;
  16767. struct npc_data *nd;
  16768. str = script_getstr(st,2);
  16769. if( script_hasdata(st,3) )
  16770. instance_id = script_getnum(st,3);
  16771. else
  16772. instance_id = script_instancegetid(st);
  16773. if( instance_id && (nd = npc_name2id(str)) != NULL ) {
  16774. static char npcname[NAME_LENGTH];
  16775. snprintf(npcname, sizeof(npcname), "dup_%hu_%d", instance_id, nd->bl.id);
  16776. script_pushconststr(st,npcname);
  16777. } else {
  16778. ShowError("buildin_instance_npcname: Invalid instance NPC (instance_id: %hu, NPC name: \"%s\".)\n", instance_id, str);
  16779. st->state = END;
  16780. return SCRIPT_CMD_FAILURE;
  16781. }
  16782. return SCRIPT_CMD_SUCCESS;
  16783. }
  16784. /*==========================================
  16785. * Returns the name of a duplicated map
  16786. *
  16787. * instance_mapname <map_name>{,<instance_id>};
  16788. *------------------------------------------*/
  16789. BUILDIN_FUNC(instance_mapname)
  16790. {
  16791. const char *str;
  16792. int16 m;
  16793. unsigned short instance_id = 0;
  16794. str = script_getstr(st,2);
  16795. if( script_hasdata(st,3) )
  16796. instance_id = script_getnum(st,3);
  16797. else
  16798. instance_id = script_instancegetid(st);
  16799. // Check that instance mapname is a valid map
  16800. if(!instance_id || (m = instance_mapname2mapid(str,instance_id)) < 0)
  16801. script_pushconststr(st, "");
  16802. else
  16803. script_pushconststr(st, map[m].name);
  16804. return SCRIPT_CMD_SUCCESS;
  16805. }
  16806. /*==========================================
  16807. * Returns an Instance ID
  16808. *------------------------------------------*/
  16809. BUILDIN_FUNC(instance_id)
  16810. {
  16811. script_pushint(st, script_instancegetid(st));
  16812. return SCRIPT_CMD_SUCCESS;
  16813. }
  16814. /*==========================================
  16815. * Warps all players inside an instance
  16816. *
  16817. * instance_warpall <map_name>,<x>,<y>{,<instance_id>};
  16818. *------------------------------------------*/
  16819. static int buildin_instance_warpall_sub(struct block_list *bl, va_list ap)
  16820. {
  16821. unsigned int m = va_arg(ap,unsigned int);
  16822. int x = va_arg(ap,int);
  16823. int y = va_arg(ap,int);
  16824. unsigned short instance_id = va_arg(ap,unsigned int);
  16825. struct map_session_data *sd = NULL;
  16826. int owner_id = 0;
  16827. nullpo_retr(0, bl);
  16828. if (bl->type != BL_PC)
  16829. return 0;
  16830. sd = (TBL_PC *)bl;
  16831. owner_id = instance_data[instance_id].owner_id;
  16832. switch(instance_data[instance_id].mode) {
  16833. case IM_NONE:
  16834. break;
  16835. case IM_CHAR:
  16836. if (sd->status.char_id != owner_id)
  16837. return 0;
  16838. break;
  16839. case IM_PARTY:
  16840. if (sd->status.party_id != owner_id)
  16841. return 0;
  16842. break;
  16843. case IM_GUILD:
  16844. if (sd->status.guild_id != owner_id)
  16845. return 0;
  16846. }
  16847. pc_setpos(sd, m, x, y, CLR_TELEPORT);
  16848. return 1;
  16849. }
  16850. BUILDIN_FUNC(instance_warpall)
  16851. {
  16852. int16 m, i;
  16853. unsigned short instance_id;
  16854. const char *mapn;
  16855. int x, y;
  16856. mapn = script_getstr(st,2);
  16857. x = script_getnum(st,3);
  16858. y = script_getnum(st,4);
  16859. if( script_hasdata(st,5) )
  16860. instance_id = script_getnum(st,5);
  16861. else
  16862. instance_id = script_instancegetid(st);
  16863. if( !instance_id || (m = map_mapname2mapid(mapn)) < 0 || (m = instance_mapname2mapid(map[m].name,instance_id)) < 0)
  16864. return SCRIPT_CMD_FAILURE;
  16865. for(i = 0; i < instance_data[instance_id].cnt_map; i++)
  16866. map_foreachinmap(buildin_instance_warpall_sub, instance_data[instance_id].map[i]->m, BL_PC, map_id2index(m), x, y, instance_id);
  16867. return SCRIPT_CMD_SUCCESS;
  16868. }
  16869. /*==========================================
  16870. * Broadcasts to all maps inside an instance
  16871. *
  16872. * instance_announce <instance id>,"<text>",<flag>{,<fontColor>{,<fontType>{,<fontSize>{,<fontAlign>{,<fontY>}}}}};
  16873. * Using 0 for <instance id> will auto-detect the id.
  16874. *------------------------------------------*/
  16875. BUILDIN_FUNC(instance_announce) {
  16876. unsigned short instance_id = script_getnum(st,2);
  16877. const char *mes = script_getstr(st,3);
  16878. int flag = script_getnum(st,4);
  16879. const char *fontColor = script_hasdata(st,5) ? script_getstr(st,5) : NULL;
  16880. int fontType = script_hasdata(st,6) ? script_getnum(st,6) : FW_NORMAL; // default fontType
  16881. int fontSize = script_hasdata(st,7) ? script_getnum(st,7) : 12; // default fontSize
  16882. int fontAlign = script_hasdata(st,8) ? script_getnum(st,8) : 0; // default fontAlign
  16883. int fontY = script_hasdata(st,9) ? script_getnum(st,9) : 0; // default fontY
  16884. int i;
  16885. if( instance_id == 0 ) {
  16886. instance_id = script_instancegetid(st);
  16887. }
  16888. if( !instance_id && &instance_data[instance_id] != NULL) {
  16889. ShowError("buildin_instance_announce: Intance is not found.\n");
  16890. return SCRIPT_CMD_FAILURE;
  16891. }
  16892. for( i = 0; i < instance_data[instance_id].cnt_map; i++ )
  16893. map_foreachinmap(buildin_announce_sub, instance_data[instance_id].map[i]->m, BL_PC,
  16894. mes, strlen(mes)+1, flag&BC_COLOR_MASK, fontColor, fontType, fontSize, fontAlign, fontY);
  16895. return SCRIPT_CMD_SUCCESS;
  16896. }
  16897. /*==========================================
  16898. * instance_check_party [malufett]
  16899. * Values:
  16900. * party_id : Party ID of the invoking character. [Required Parameter]
  16901. * amount : Amount of needed Partymembers for the Instance. [Optional Parameter]
  16902. * min : Minimum Level needed to join the Instance. [Optional Parameter]
  16903. * max : Maxium Level allowed to join the Instance. [Optional Parameter]
  16904. * Example: instance_check_party (getcharid(1){,amount}{,min}{,max});
  16905. * Example 2: instance_check_party (getcharid(1),1,1,99);
  16906. *------------------------------------------*/
  16907. BUILDIN_FUNC(instance_check_party)
  16908. {
  16909. int amount, min, max, i, party_id, c = 0;
  16910. struct party_data *p = NULL;
  16911. amount = script_hasdata(st,3) ? script_getnum(st,3) : 1; // Amount of needed Partymembers for the Instance.
  16912. min = script_hasdata(st,4) ? script_getnum(st,4) : 1; // Minimum Level needed to join the Instance.
  16913. max = script_hasdata(st,5) ? script_getnum(st,5) : MAX_LEVEL; // Maxium Level allowed to join the Instance.
  16914. if( min < 1 || min > MAX_LEVEL) {
  16915. ShowError("buildin_instance_check_party: Invalid min level, %d\n", min);
  16916. return SCRIPT_CMD_FAILURE;
  16917. } else if( max < 1 || max > MAX_LEVEL) {
  16918. ShowError("buildin_instance_check_party: Invalid max level, %d\n", max);
  16919. return SCRIPT_CMD_FAILURE;
  16920. }
  16921. if( script_hasdata(st,2) )
  16922. party_id = script_getnum(st,2);
  16923. else return SCRIPT_CMD_FAILURE;
  16924. if( !(p = party_search(party_id)) ) {
  16925. script_pushint(st, 0); // Returns false if party does not exist.
  16926. return SCRIPT_CMD_FAILURE;
  16927. }
  16928. for( i = 0; i < MAX_PARTY; i++ ) {
  16929. struct map_session_data *pl_sd;
  16930. if( (pl_sd = p->data[i].sd) )
  16931. if(map_id2bl(pl_sd->bl.id)) {
  16932. if(pl_sd->status.base_level < min) {
  16933. script_pushint(st, 0);
  16934. return SCRIPT_CMD_SUCCESS;
  16935. } else if(pl_sd->status.base_level > max) {
  16936. script_pushint(st, 0);
  16937. return SCRIPT_CMD_SUCCESS;
  16938. }
  16939. c++;
  16940. }
  16941. }
  16942. if(c < amount)
  16943. script_pushint(st, 0); // Not enough Members in the Party to join Instance.
  16944. else
  16945. script_pushint(st, 1);
  16946. return SCRIPT_CMD_SUCCESS;
  16947. }
  16948. /*==========================================
  16949. * instance_check_guild
  16950. * Values:
  16951. * guild_id : Guild ID of the invoking character. [Required Parameter]
  16952. * amount : Amount of needed Guild members for the Instance. [Optional Parameter]
  16953. * min : Minimum Level needed to join the Instance. [Optional Parameter]
  16954. * max : Maxium Level allowed to join the Instance. [Optional Parameter]
  16955. * Example: instance_check_guild (getcharid(2){,amount}{,min}{,max});
  16956. * Example 2: instance_check_guild (getcharid(2),1,1,99);
  16957. *------------------------------------------*/
  16958. BUILDIN_FUNC(instance_check_guild)
  16959. {
  16960. int amount, min, max, i, guild_id = 0, c = 0;
  16961. struct guild *g = NULL;
  16962. amount = script_hasdata(st,3) ? script_getnum(st,3) : 1; // Amount of needed Guild members for the Instance.
  16963. min = script_hasdata(st,4) ? script_getnum(st,4) : 1; // Minimum Level needed to join the Instance.
  16964. max = script_hasdata(st,5) ? script_getnum(st,5) : MAX_LEVEL; // Maxium Level allowed to join the Instance.
  16965. if (min < 1 || min > MAX_LEVEL) {
  16966. ShowError("buildin_instance_check_guild: Invalid min level, %d\n", min);
  16967. return SCRIPT_CMD_FAILURE;
  16968. } else if (max < 1 || max > MAX_LEVEL) {
  16969. ShowError("buildin_instance_check_guild: Invalid max level, %d\n", max);
  16970. return SCRIPT_CMD_FAILURE;
  16971. }
  16972. if (script_hasdata(st,2))
  16973. guild_id = script_getnum(st,2);
  16974. else
  16975. return SCRIPT_CMD_FAILURE;
  16976. if (!(g = guild_search(guild_id))) {
  16977. script_pushint(st, 0); // Returns false if guild does not exist.
  16978. return SCRIPT_CMD_FAILURE;
  16979. }
  16980. for(i = 0; i < MAX_GUILD; i++) {
  16981. struct map_session_data *pl_sd;
  16982. if ((pl_sd = g->member[i].sd)) {
  16983. if (map_id2bl(pl_sd->bl.id)) {
  16984. if (pl_sd->status.base_level < min) {
  16985. script_pushint(st, 0);
  16986. return SCRIPT_CMD_SUCCESS;
  16987. } else if (pl_sd->status.base_level > max) {
  16988. script_pushint(st, 0);
  16989. return SCRIPT_CMD_SUCCESS;
  16990. }
  16991. c++;
  16992. }
  16993. }
  16994. }
  16995. if (c < amount)
  16996. script_pushint(st, 0); // Not enough Members in the Guild to join Instance.
  16997. else
  16998. script_pushint(st, 1);
  16999. return SCRIPT_CMD_SUCCESS;
  17000. }
  17001. /*==========================================
  17002. * instance_info
  17003. * Values:
  17004. * name : name of the instance you want to look up. [Required Parameter]
  17005. * type : type of information you want to look up for the specified instance. [Required Parameter]
  17006. * index : index of the map in the instance. [Optional Parameter]
  17007. *------------------------------------------*/
  17008. BUILDIN_FUNC(instance_info)
  17009. {
  17010. const char* name = script_getstr(st, 2);
  17011. int type = script_getnum(st, 3);
  17012. int index = 0;
  17013. struct instance_db *db = instance_searchname_db(name);
  17014. if( !db ){
  17015. ShowError( "buildin_instance_info: Unknown instance name \"%s\".\n", name );
  17016. script_pushint(st, -1);
  17017. return SCRIPT_CMD_FAILURE;
  17018. }
  17019. switch( type ){
  17020. case IIT_ID:
  17021. script_pushint(st, db->id);
  17022. break;
  17023. case IIT_TIME_LIMIT:
  17024. script_pushint(st, db->limit);
  17025. break;
  17026. case IIT_IDLE_TIMEOUT:
  17027. script_pushint(st, db->timeout);
  17028. break;
  17029. case IIT_ENTER_MAP:
  17030. script_pushstrcopy(st, StringBuf_Value(db->enter.mapname));
  17031. break;
  17032. case IIT_ENTER_X:
  17033. script_pushint(st, db->enter.x);
  17034. break;
  17035. case IIT_ENTER_Y:
  17036. script_pushint(st, db->enter.y);
  17037. break;
  17038. case IIT_MAPCOUNT:
  17039. script_pushint(st, db->maplist_count);
  17040. break;
  17041. case IIT_MAP:
  17042. if( !script_hasdata(st, 4) || script_isstring(st, 4) ){
  17043. ShowError( "buildin_instance_info: Type IIT_MAP requires a numeric index argument.\n" );
  17044. script_pushstr(st, "");
  17045. return SCRIPT_CMD_FAILURE;
  17046. }
  17047. index = script_getnum(st, 4);
  17048. if( index < 0 ){
  17049. ShowError( "buildin_instance_info: Type IIT_MAP does not support a negative index argument.\n" );
  17050. script_pushstr(st, "");
  17051. return SCRIPT_CMD_FAILURE;
  17052. }
  17053. if( index > UINT8_MAX ){
  17054. ShowError( "buildin_instance_info: Type IIT_MAP does only support up to index %hu.\n", UINT8_MAX );
  17055. script_pushstr(st, "");
  17056. return SCRIPT_CMD_FAILURE;
  17057. }
  17058. script_pushstrcopy(st, StringBuf_Value(db->maplist[index]));
  17059. break;
  17060. default:
  17061. ShowError("buildin_instance_info: Unknown instance information type \"%d\".\n", type );
  17062. script_pushint(st, -1);
  17063. return SCRIPT_CMD_FAILURE;
  17064. }
  17065. return SCRIPT_CMD_SUCCESS;
  17066. }
  17067. /*==========================================
  17068. * Custom Fonts
  17069. *------------------------------------------*/
  17070. BUILDIN_FUNC(setfont)
  17071. {
  17072. struct map_session_data *sd = script_rid2sd(st);
  17073. int font = script_getnum(st,2);
  17074. if( sd == NULL )
  17075. return SCRIPT_CMD_SUCCESS;
  17076. if( sd->status.font != font )
  17077. sd->status.font = font;
  17078. else
  17079. sd->status.font = 0;
  17080. clif_font(sd);
  17081. return SCRIPT_CMD_SUCCESS;
  17082. }
  17083. static int buildin_mobuseskill_sub(struct block_list *bl,va_list ap)
  17084. {
  17085. TBL_MOB* md = (TBL_MOB*)bl;
  17086. struct block_list *tbl;
  17087. int mobid = va_arg(ap,int);
  17088. uint16 skill_id = va_arg(ap,int);
  17089. uint16 skill_lv = va_arg(ap,int);
  17090. int casttime = va_arg(ap,int);
  17091. int cancel = va_arg(ap,int);
  17092. int emotion = va_arg(ap,int);
  17093. int target = va_arg(ap,int);
  17094. if( md->mob_id != mobid )
  17095. return 0;
  17096. // 0:self, 1:target, 2:master, default:random
  17097. switch( target ) {
  17098. case 0: tbl = map_id2bl(md->bl.id); break;
  17099. case 1: tbl = map_id2bl(md->target_id); break;
  17100. case 2: tbl = map_id2bl(md->master_id); break;
  17101. default:tbl = battle_getenemy(&md->bl, DEFAULT_ENEMY_TYPE(md), skill_get_range2(&md->bl, skill_id, skill_lv, true)); break;
  17102. }
  17103. if( !tbl )
  17104. return 0;
  17105. if( md->ud.skilltimer != INVALID_TIMER ) // Cancel the casting skill.
  17106. unit_skillcastcancel(bl,0);
  17107. if( skill_get_casttype(skill_id) == CAST_GROUND )
  17108. unit_skilluse_pos2(&md->bl, tbl->x, tbl->y, skill_id, skill_lv, casttime, cancel);
  17109. else
  17110. unit_skilluse_id2(&md->bl, tbl->id, skill_id, skill_lv, casttime, cancel);
  17111. clif_emotion(&md->bl, emotion);
  17112. return 1;
  17113. }
  17114. /*==========================================
  17115. * areamobuseskill "Map Name",<x>,<y>,<range>,<Mob ID>,"Skill Name"/<Skill ID>,<Skill Lv>,<Cast Time>,<Cancelable>,<Emotion>,<Target Type>;
  17116. *------------------------------------------*/
  17117. BUILDIN_FUNC(areamobuseskill)
  17118. {
  17119. struct block_list center;
  17120. struct script_data *data;
  17121. int16 m;
  17122. int range,mobid,skill_id,skill_lv,casttime,emotion,target,cancel;
  17123. if( (m = map_mapname2mapid(script_getstr(st,2))) < 0 ) {
  17124. ShowError("areamobuseskill: invalid map name.\n");
  17125. return SCRIPT_CMD_SUCCESS;
  17126. }
  17127. center.m = m;
  17128. center.x = script_getnum(st,3);
  17129. center.y = script_getnum(st,4);
  17130. range = script_getnum(st,5);
  17131. mobid = script_getnum(st,6);
  17132. data = script_getdata(st, 7);
  17133. get_val(st, data); // Convert into value in case of a variable
  17134. skill_id = ( data_isstring(data) ? skill_name2id(script_getstr(st,7)) : script_getnum(st,7) );
  17135. if( (skill_lv = script_getnum(st,8)) > battle_config.mob_max_skilllvl )
  17136. skill_lv = battle_config.mob_max_skilllvl;
  17137. casttime = script_getnum(st,9);
  17138. cancel = script_getnum(st,10);
  17139. emotion = script_getnum(st,11);
  17140. target = script_getnum(st,12);
  17141. map_foreachinrange(buildin_mobuseskill_sub, &center, range, BL_MOB, mobid, skill_id, skill_lv, casttime, cancel, emotion, target);
  17142. return SCRIPT_CMD_SUCCESS;
  17143. }
  17144. BUILDIN_FUNC(progressbar)
  17145. {
  17146. struct map_session_data * sd = script_rid2sd(st);
  17147. const char * color;
  17148. unsigned int second;
  17149. if( !st || !sd )
  17150. return SCRIPT_CMD_SUCCESS;
  17151. st->state = STOP;
  17152. color = script_getstr(st,2);
  17153. second = script_getnum(st,3);
  17154. sd->progressbar.npc_id = st->oid;
  17155. sd->progressbar.timeout = gettick() + second*1000;
  17156. sd->state.workinprogress = WIP_DISABLE_ALL;
  17157. clif_progressbar(sd, strtol(color, (char **)NULL, 0), second);
  17158. return SCRIPT_CMD_SUCCESS;
  17159. }
  17160. BUILDIN_FUNC(pushpc)
  17161. {
  17162. uint8 dir;
  17163. int cells, dx, dy;
  17164. struct map_session_data* sd;
  17165. if((sd = script_rid2sd(st))==NULL)
  17166. {
  17167. return SCRIPT_CMD_SUCCESS;
  17168. }
  17169. dir = script_getnum(st,2);
  17170. cells = script_getnum(st,3);
  17171. if(dir >= DIR_MAX)
  17172. {
  17173. ShowWarning("buildin_pushpc: Invalid direction %d specified.\n", dir);
  17174. script_reportsrc(st);
  17175. dir%= DIR_MAX; // trim spin-over
  17176. }
  17177. if(!cells)
  17178. {// zero distance
  17179. return SCRIPT_CMD_SUCCESS;
  17180. }
  17181. else if(cells<0)
  17182. {// pushing backwards
  17183. dir = (dir+DIR_MAX/2)%DIR_MAX; // turn around
  17184. cells = -cells;
  17185. }
  17186. dx = dirx[dir];
  17187. dy = diry[dir];
  17188. unit_blown(&sd->bl, dx, dy, cells, 0);
  17189. return SCRIPT_CMD_SUCCESS;
  17190. }
  17191. /// Invokes buying store preparation window
  17192. /// buyingstore <slots>;
  17193. BUILDIN_FUNC(buyingstore)
  17194. {
  17195. struct map_session_data* sd;
  17196. if( ( sd = script_rid2sd(st) ) == NULL )
  17197. {
  17198. return SCRIPT_CMD_SUCCESS;
  17199. }
  17200. if( npc_isnear(&sd->bl) ) {
  17201. char output[150];
  17202. sprintf(output, msg_txt(sd,662), battle_config.min_npc_vendchat_distance);
  17203. clif_displaymessage(sd->fd, output);
  17204. return SCRIPT_CMD_SUCCESS;
  17205. }
  17206. buyingstore_setup(sd, script_getnum(st,2));
  17207. return SCRIPT_CMD_SUCCESS;
  17208. }
  17209. /// Invokes search store info window
  17210. /// searchstores <uses>,<effect>;
  17211. BUILDIN_FUNC(searchstores)
  17212. {
  17213. unsigned short effect;
  17214. unsigned int uses;
  17215. struct map_session_data* sd;
  17216. if( ( sd = script_rid2sd(st) ) == NULL )
  17217. {
  17218. return SCRIPT_CMD_SUCCESS;
  17219. }
  17220. uses = script_getnum(st,2);
  17221. effect = script_getnum(st,3);
  17222. if( !uses )
  17223. {
  17224. ShowError("buildin_searchstores: Amount of uses cannot be zero.\n");
  17225. return SCRIPT_CMD_FAILURE;
  17226. }
  17227. if( effect > 1 )
  17228. {
  17229. ShowError("buildin_searchstores: Invalid effect id %hu, specified.\n", effect);
  17230. return SCRIPT_CMD_FAILURE;
  17231. }
  17232. searchstore_open(sd, uses, effect);
  17233. return SCRIPT_CMD_SUCCESS;
  17234. }
  17235. /// Displays a number as large digital clock.
  17236. /// showdigit <value>[,<type>];
  17237. BUILDIN_FUNC(showdigit)
  17238. {
  17239. unsigned int type = 0;
  17240. int value;
  17241. struct map_session_data* sd;
  17242. if( ( sd = script_rid2sd(st) ) == NULL )
  17243. {
  17244. return SCRIPT_CMD_SUCCESS;
  17245. }
  17246. value = script_getnum(st,2);
  17247. if( script_hasdata(st,3) ){
  17248. type = script_getnum(st,3);
  17249. }
  17250. switch( type ){
  17251. case 0:
  17252. break;
  17253. case 1:
  17254. case 2:
  17255. // Use absolute value and then the negative value of it as starting value
  17256. // This is what gravity's client does for these counters
  17257. value = -abs(value);
  17258. break;
  17259. case 3:
  17260. value = abs(value);
  17261. if( value > 99 ){
  17262. ShowWarning("buildin_showdigit: type 3 can display 2 digits at max. Capping value %d to 99...\n", value);
  17263. script_reportsrc(st);
  17264. value = 99;
  17265. }
  17266. break;
  17267. default:
  17268. ShowError("buildin_showdigit: Invalid type %u.\n", type);
  17269. return SCRIPT_CMD_FAILURE;
  17270. }
  17271. clif_showdigit(sd, (unsigned char)type, value);
  17272. return SCRIPT_CMD_SUCCESS;
  17273. }
  17274. /**
  17275. * Rune Knight
  17276. * makerune <% success bonus>{,<char_id>};
  17277. **/
  17278. BUILDIN_FUNC(makerune) {
  17279. TBL_PC* sd;
  17280. if (!script_charid2sd(3,sd))
  17281. return SCRIPT_CMD_FAILURE;
  17282. clif_skill_produce_mix_list(sd,RK_RUNEMASTERY,24);
  17283. sd->itemid = script_getnum(st,2);
  17284. return SCRIPT_CMD_SUCCESS;
  17285. }
  17286. /**
  17287. * checkdragon({<char_id>}) returns 1 if mounting a dragon or 0 otherwise.
  17288. **/
  17289. BUILDIN_FUNC(checkdragon) {
  17290. TBL_PC* sd;
  17291. if (!script_charid2sd(2,sd))
  17292. return SCRIPT_CMD_FAILURE;
  17293. if( pc_isridingdragon(sd) )
  17294. script_pushint(st,1);
  17295. else
  17296. script_pushint(st,0);
  17297. return SCRIPT_CMD_SUCCESS;
  17298. }
  17299. /**
  17300. * setdragon({optional Color{,<char_id>}}) returns 1 on success or 0 otherwise
  17301. * - Toggles the dragon on a RK if he can mount;
  17302. * @param Color - when not provided uses the green dragon;
  17303. * - 1 : Green Dragon
  17304. * - 2 : Brown Dragon
  17305. * - 3 : Gray Dragon
  17306. * - 4 : Blue Dragon
  17307. * - 5 : Red Dragon
  17308. **/
  17309. BUILDIN_FUNC(setdragon) {
  17310. TBL_PC* sd;
  17311. int color = script_hasdata(st,2) ? script_getnum(st,2) : 0;
  17312. if (!script_charid2sd(3,sd))
  17313. return SCRIPT_CMD_FAILURE;
  17314. if( !pc_checkskill(sd,RK_DRAGONTRAINING) || (sd->class_&MAPID_THIRDMASK) != MAPID_RUNE_KNIGHT )
  17315. script_pushint(st,0);//Doesn't have the skill or it's not a Rune Knight
  17316. else if ( pc_isridingdragon(sd) ) {//Is mounted; release
  17317. pc_setoption(sd, sd->sc.option&~OPTION_DRAGON);
  17318. script_pushint(st,1);
  17319. } else {//Not mounted; Mount now.
  17320. unsigned int option = OPTION_DRAGON1;
  17321. if( color ) {
  17322. option = ( color == 1 ? OPTION_DRAGON1 :
  17323. color == 2 ? OPTION_DRAGON2 :
  17324. color == 3 ? OPTION_DRAGON3 :
  17325. color == 4 ? OPTION_DRAGON4 :
  17326. color == 5 ? OPTION_DRAGON5 : 0);
  17327. if( !option ) {
  17328. ShowWarning("script_setdragon: Unknown Color %d used; changing to green (1)\n",color);
  17329. option = OPTION_DRAGON1;
  17330. }
  17331. }
  17332. pc_setoption(sd, sd->sc.option|option);
  17333. script_pushint(st,1);
  17334. }
  17335. return SCRIPT_CMD_SUCCESS;
  17336. }
  17337. /**
  17338. * ismounting({<char_id>}) returns 1 if mounting a new mount or 0 otherwise
  17339. **/
  17340. BUILDIN_FUNC(ismounting) {
  17341. TBL_PC* sd;
  17342. if (!script_charid2sd(2,sd))
  17343. return SCRIPT_CMD_FAILURE;
  17344. if( &sd->sc && sd->sc.data[SC_ALL_RIDING] )
  17345. script_pushint(st,1);
  17346. else
  17347. script_pushint(st,0);
  17348. return SCRIPT_CMD_SUCCESS;
  17349. }
  17350. /**
  17351. * setmounting({<char_id>}) returns 1 on success or 0 otherwise
  17352. * - Toggles new mounts on a player when he can mount
  17353. * - Will fail if the player is mounting a non-new mount, e.g. dragon, peco, wug, etc.
  17354. * - Will unmount the player is he is already mounting
  17355. **/
  17356. BUILDIN_FUNC(setmounting) {
  17357. TBL_PC* sd;
  17358. if (!script_charid2sd(2,sd))
  17359. return SCRIPT_CMD_FAILURE;
  17360. if( &sd->sc && sd->sc.option&(OPTION_WUGRIDER|OPTION_RIDING|OPTION_DRAGON|OPTION_MADOGEAR) ) {
  17361. clif_msg(sd, NEED_REINS_OF_MOUNT);
  17362. script_pushint(st,0); //can't mount with one of these
  17363. } else {
  17364. if( &sd->sc && sd->sc.data[SC_ALL_RIDING] )
  17365. status_change_end(&sd->bl, SC_ALL_RIDING, INVALID_TIMER); //release mount
  17366. else
  17367. sc_start(NULL, &sd->bl, SC_ALL_RIDING, 10000, 1, INVALID_TIMER); //mount
  17368. script_pushint(st,1);//in both cases, return 1.
  17369. }
  17370. return SCRIPT_CMD_SUCCESS;
  17371. }
  17372. /**
  17373. * Retrieves quantity of arguments provided to callfunc/callsub.
  17374. * getargcount() -> amount of arguments received in a function
  17375. **/
  17376. BUILDIN_FUNC(getargcount) {
  17377. struct script_retinfo* ri;
  17378. if( st->stack->defsp < 1 || st->stack->stack_data[st->stack->defsp - 1].type != C_RETINFO ) {
  17379. ShowError("script:getargcount: used out of function or callsub label!\n");
  17380. st->state = END;
  17381. return SCRIPT_CMD_FAILURE;
  17382. }
  17383. ri = st->stack->stack_data[st->stack->defsp - 1].u.ri;
  17384. script_pushint(st, ri->nargs);
  17385. return SCRIPT_CMD_SUCCESS;
  17386. }
  17387. /**
  17388. * getcharip(<account ID>/<character ID>/<character name>)
  17389. **/
  17390. BUILDIN_FUNC(getcharip)
  17391. {
  17392. struct map_session_data* sd = NULL;
  17393. /* check if a character name is specified */
  17394. if( script_hasdata(st, 2) )
  17395. {
  17396. struct script_data *data;
  17397. data = script_getdata(st, 2);
  17398. get_val(st, data); // Convert into value in case of a variable
  17399. if (data_isstring(data))
  17400. sd = map_nick2sd(script_getstr(st, 2),false);
  17401. else if (data_isint(data) || script_getnum(st, 2))
  17402. {
  17403. int id = 0;
  17404. id = script_getnum(st, 2);
  17405. sd = (map_id2sd(id) ? map_id2sd(id) : map_charid2sd(id));
  17406. }
  17407. }
  17408. else
  17409. sd = script_rid2sd(st);
  17410. /* check for sd and IP */
  17411. if (!sd || !session[sd->fd]->client_addr)
  17412. {
  17413. script_pushconststr(st, "");
  17414. return SCRIPT_CMD_SUCCESS;
  17415. }
  17416. /* return the client ip_addr converted for output */
  17417. if (sd && sd->fd && session[sd->fd])
  17418. {
  17419. /* initiliaze */
  17420. const char *ip_addr = NULL;
  17421. uint32 ip;
  17422. /* set ip, ip_addr and convert to ip and push str */
  17423. ip = session[sd->fd]->client_addr;
  17424. ip_addr = ip2str(ip, NULL);
  17425. script_pushstrcopy(st, ip_addr);
  17426. }
  17427. return SCRIPT_CMD_SUCCESS;
  17428. }
  17429. /**
  17430. * is_function(<function name>) -> 1 if function exists, 0 otherwise
  17431. **/
  17432. BUILDIN_FUNC(is_function) {
  17433. const char* str = script_getstr(st,2);
  17434. if( strdb_exists(userfunc_db, str) )
  17435. script_pushint(st,1);
  17436. else
  17437. script_pushint(st,0);
  17438. return SCRIPT_CMD_SUCCESS;
  17439. }
  17440. /**
  17441. * get_revision() -> retrieves the current svn revision (if available)
  17442. **/
  17443. BUILDIN_FUNC(get_revision) {
  17444. const char *svn = get_svn_revision();
  17445. if ( svn[0] != UNKNOWN_VERSION )
  17446. script_pushint(st,atoi(svn));
  17447. else
  17448. script_pushint(st,-1); //unknown
  17449. return SCRIPT_CMD_SUCCESS;
  17450. }
  17451. /* get_hash() -> retrieves the current git hash (if available)*/
  17452. BUILDIN_FUNC(get_githash) {
  17453. const char* git = get_git_hash();
  17454. char buf[CHAT_SIZE_MAX];
  17455. safestrncpy(buf,git,strlen(git)+1);
  17456. if ( git[0] != UNKNOWN_VERSION )
  17457. script_pushstr(st,buf);
  17458. else
  17459. script_pushstr(st,"Unknown"); //unknown
  17460. return SCRIPT_CMD_SUCCESS;
  17461. }
  17462. /**
  17463. * freeloop(<toggle>) -> toggles this script instance's looping-check ability
  17464. **/
  17465. BUILDIN_FUNC(freeloop) {
  17466. if( script_hasdata(st,2) ) {
  17467. if( script_getnum(st,2) )
  17468. st->freeloop = 1;
  17469. else
  17470. st->freeloop = 0;
  17471. }
  17472. script_pushint(st, st->freeloop);
  17473. return SCRIPT_CMD_SUCCESS;
  17474. }
  17475. /**
  17476. * @commands (script based)
  17477. **/
  17478. BUILDIN_FUNC(bindatcmd) {
  17479. const char* atcmd;
  17480. const char* eventName;
  17481. int i, level = 0, level2 = 0;
  17482. bool create = false;
  17483. atcmd = script_getstr(st,2);
  17484. eventName = script_getstr(st,3);
  17485. if( *atcmd == atcommand_symbol || *atcmd == charcommand_symbol )
  17486. atcmd++;
  17487. if( script_hasdata(st,4) ) level = script_getnum(st,4);
  17488. if( script_hasdata(st,5) ) level2 = script_getnum(st,5);
  17489. if( atcmd_binding_count == 0 ) {
  17490. CREATE(atcmd_binding,struct atcmd_binding_data*,1);
  17491. create = true;
  17492. } else {
  17493. ARR_FIND(0, atcmd_binding_count, i, strcmp(atcmd_binding[i]->command,atcmd) == 0);
  17494. if( i < atcmd_binding_count ) {/* update existent entry */
  17495. safestrncpy(atcmd_binding[i]->npc_event, eventName, 50);
  17496. atcmd_binding[i]->level = level;
  17497. atcmd_binding[i]->level2 = level2;
  17498. } else
  17499. create = true;
  17500. }
  17501. if( create ) {
  17502. i = atcmd_binding_count;
  17503. if( atcmd_binding_count++ != 0 )
  17504. RECREATE(atcmd_binding,struct atcmd_binding_data*,atcmd_binding_count);
  17505. CREATE(atcmd_binding[i],struct atcmd_binding_data,1);
  17506. safestrncpy(atcmd_binding[i]->command, atcmd, 50);
  17507. safestrncpy(atcmd_binding[i]->npc_event, eventName, 50);
  17508. atcmd_binding[i]->level = level;
  17509. atcmd_binding[i]->level2 = level2;
  17510. }
  17511. return SCRIPT_CMD_SUCCESS;
  17512. }
  17513. BUILDIN_FUNC(unbindatcmd) {
  17514. const char* atcmd;
  17515. int i = 0;
  17516. atcmd = script_getstr(st, 2);
  17517. if( *atcmd == atcommand_symbol || *atcmd == charcommand_symbol )
  17518. atcmd++;
  17519. if( atcmd_binding_count == 0 ) {
  17520. script_pushint(st, 0);
  17521. return SCRIPT_CMD_SUCCESS;
  17522. }
  17523. ARR_FIND(0, atcmd_binding_count, i, strcmp(atcmd_binding[i]->command, atcmd) == 0);
  17524. if( i < atcmd_binding_count ) {
  17525. int cursor = 0;
  17526. aFree(atcmd_binding[i]);
  17527. atcmd_binding[i] = NULL;
  17528. /* compact the list now that we freed a slot somewhere */
  17529. for( i = 0, cursor = 0; i < atcmd_binding_count; i++ ) {
  17530. if( atcmd_binding[i] == NULL )
  17531. continue;
  17532. if( cursor != i ) {
  17533. memmove(&atcmd_binding[cursor], &atcmd_binding[i], sizeof(struct atcmd_binding_data*));
  17534. }
  17535. cursor++;
  17536. }
  17537. if( (atcmd_binding_count = cursor) == 0 )
  17538. aFree(atcmd_binding);
  17539. script_pushint(st, 1);
  17540. } else
  17541. script_pushint(st, 0);/* not found */
  17542. return SCRIPT_CMD_SUCCESS;
  17543. }
  17544. BUILDIN_FUNC(useatcmd) {
  17545. return atcommand_sub(st,3);
  17546. }
  17547. BUILDIN_FUNC(checkre)
  17548. {
  17549. int num;
  17550. num=script_getnum(st,2);
  17551. switch(num){
  17552. case 0:
  17553. #ifdef RENEWAL
  17554. script_pushint(st, 1);
  17555. #else
  17556. script_pushint(st, 0);
  17557. #endif
  17558. break;
  17559. case 1:
  17560. #ifdef RENEWAL_CAST
  17561. script_pushint(st, 1);
  17562. #else
  17563. script_pushint(st, 0);
  17564. #endif
  17565. break;
  17566. case 2:
  17567. #ifdef RENEWAL_DROP
  17568. script_pushint(st, 1);
  17569. #else
  17570. script_pushint(st, 0);
  17571. #endif
  17572. break;
  17573. case 3:
  17574. #ifdef RENEWAL_EXP
  17575. script_pushint(st, 1);
  17576. #else
  17577. script_pushint(st, 0);
  17578. #endif
  17579. break;
  17580. case 4:
  17581. #ifdef RENEWAL_LVDMG
  17582. script_pushint(st, 1);
  17583. #else
  17584. script_pushint(st, 0);
  17585. #endif
  17586. break;
  17587. case 5:
  17588. #ifdef RENEWAL_ASPD
  17589. script_pushint(st, 1);
  17590. #else
  17591. script_pushint(st, 0);
  17592. #endif
  17593. break;
  17594. default:
  17595. ShowWarning("buildin_checkre: unknown parameter.\n");
  17596. break;
  17597. }
  17598. return SCRIPT_CMD_SUCCESS;
  17599. }
  17600. /* getrandgroupitem <group_id>{,<quantity>{,<sub_group>}} */
  17601. BUILDIN_FUNC(getrandgroupitem) {
  17602. TBL_PC* sd;
  17603. int i, get_count = 0;
  17604. uint16 group = script_getnum(st,2), qty = 0;
  17605. uint8 sub_group = 1;
  17606. struct item item_tmp;
  17607. struct s_item_group_entry *entry = NULL;
  17608. if (!(sd = script_rid2sd(st)))
  17609. return SCRIPT_CMD_SUCCESS;
  17610. if (!group) {
  17611. ShowError("buildin_getrandgroupitem: Invalid group id (%d)!\n",script_getnum(st,2));
  17612. return SCRIPT_CMD_FAILURE;
  17613. }
  17614. FETCH(3, qty);
  17615. FETCH(4, sub_group);
  17616. entry = itemdb_get_randgroupitem(group,sub_group);
  17617. if (!entry)
  17618. return SCRIPT_CMD_FAILURE; //ensure valid itemid
  17619. memset(&item_tmp,0,sizeof(item_tmp));
  17620. item_tmp.nameid = entry->nameid;
  17621. item_tmp.identify = itemdb_isidentified(entry->nameid);
  17622. if (!qty)
  17623. qty = entry->amount;
  17624. //Check if it's stackable.
  17625. if (!itemdb_isstackable(entry->nameid)) {
  17626. item_tmp.amount = 1;
  17627. get_count = qty;
  17628. }
  17629. else {
  17630. item_tmp.amount = qty;
  17631. get_count = 1;
  17632. }
  17633. for (i = 0; i < get_count; i++) {
  17634. // if not pet egg
  17635. if (!pet_create_egg(sd, entry->nameid)) {
  17636. unsigned char flag = 0;
  17637. if ((flag = pc_additem(sd,&item_tmp,item_tmp.amount,LOG_TYPE_SCRIPT))) {
  17638. clif_additem(sd,0,0,flag);
  17639. if (pc_candrop(sd,&item_tmp))
  17640. map_addflooritem(&item_tmp,item_tmp.amount,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0,0);
  17641. }
  17642. }
  17643. }
  17644. return SCRIPT_CMD_SUCCESS;
  17645. }
  17646. /* getgroupitem <group_id>{,<char_id>};
  17647. * Gives item(s) to the attached player based on item group contents
  17648. */
  17649. BUILDIN_FUNC(getgroupitem) {
  17650. TBL_PC *sd;
  17651. int group_id = script_getnum(st,2);
  17652. if (!script_charid2sd(3,sd))
  17653. return SCRIPT_CMD_SUCCESS;
  17654. if (itemdb_pc_get_itemgroup(group_id,sd)) {
  17655. ShowError("buildin_getgroupitem: Invalid group id '%d' specified.\n",group_id);
  17656. return SCRIPT_CMD_FAILURE;
  17657. }
  17658. return SCRIPT_CMD_SUCCESS;
  17659. }
  17660. /* cleanmap <map_name>;
  17661. * cleanarea <map_name>, <x0>, <y0>, <x1>, <y1>; */
  17662. static int atcommand_cleanfloor_sub(struct block_list *bl, va_list ap)
  17663. {
  17664. nullpo_ret(bl);
  17665. map_clearflooritem(bl);
  17666. return SCRIPT_CMD_SUCCESS;
  17667. }
  17668. BUILDIN_FUNC(cleanmap)
  17669. {
  17670. const char *mapname;
  17671. int16 m;
  17672. mapname = script_getstr(st, 2);
  17673. m = map_mapname2mapid(mapname);
  17674. if (!m)
  17675. return SCRIPT_CMD_FAILURE;
  17676. if ((script_lastdata(st) - 2) < 4) {
  17677. map_foreachinmap(atcommand_cleanfloor_sub, m, BL_ITEM);
  17678. } else {
  17679. int16 x0 = script_getnum(st, 3);
  17680. int16 y0 = script_getnum(st, 4);
  17681. int16 x1 = script_getnum(st, 5);
  17682. int16 y1 = script_getnum(st, 6);
  17683. if (x0 > 0 && y0 > 0 && x1 > 0 && y1 > 0) {
  17684. map_foreachinarea(atcommand_cleanfloor_sub, m, x0, y0, x1, y1, BL_ITEM);
  17685. } else {
  17686. ShowError("cleanarea: invalid coordinate defined!\n");
  17687. return SCRIPT_CMD_FAILURE;
  17688. }
  17689. }
  17690. return SCRIPT_CMD_SUCCESS;
  17691. }
  17692. /* Cast a skill on the attached player.
  17693. * npcskill <skill id>, <skill lvl>, <stat point>, <NPC level>;
  17694. * npcskill "<skill name>", <skill lvl>, <stat point>, <NPC level>; */
  17695. BUILDIN_FUNC(npcskill)
  17696. {
  17697. uint16 skill_id;
  17698. unsigned short skill_level;
  17699. unsigned int stat_point;
  17700. unsigned int npc_level;
  17701. struct npc_data *nd;
  17702. struct map_session_data *sd;
  17703. struct script_data *data;
  17704. data = script_getdata(st, 2);
  17705. get_val(st, data); // Convert into value in case of a variable
  17706. skill_id = data_isstring(data) ? skill_name2id(script_getstr(st, 2)) : script_getnum(st, 2);
  17707. skill_level = script_getnum(st, 3);
  17708. stat_point = script_getnum(st, 4);
  17709. npc_level = script_getnum(st, 5);
  17710. sd = script_rid2sd(st);
  17711. nd = (struct npc_data *)map_id2bl(sd->npc_id);
  17712. if (stat_point > battle_config.max_third_parameter) {
  17713. ShowError("npcskill: stat point exceeded maximum of %d.\n",battle_config.max_third_parameter );
  17714. return SCRIPT_CMD_FAILURE;
  17715. }
  17716. if (npc_level > MAX_LEVEL) {
  17717. ShowError("npcskill: level exceeded maximum of %d.\n", MAX_LEVEL);
  17718. return SCRIPT_CMD_FAILURE;
  17719. }
  17720. if (sd == NULL || nd == NULL) { //ain't possible, but I don't trust people.
  17721. return SCRIPT_CMD_FAILURE;
  17722. }
  17723. nd->level = npc_level;
  17724. nd->stat_point = stat_point;
  17725. if (!nd->status.hp)
  17726. status_calc_npc(nd, SCO_FIRST);
  17727. else
  17728. status_calc_npc(nd, SCO_NONE);
  17729. if (skill_get_inf(skill_id)&INF_GROUND_SKILL)
  17730. unit_skilluse_pos(&nd->bl, sd->bl.x, sd->bl.y, skill_id, skill_level);
  17731. else
  17732. unit_skilluse_id(&nd->bl, sd->bl.id, skill_id, skill_level);
  17733. return SCRIPT_CMD_SUCCESS;
  17734. }
  17735. /* Consumes an item.
  17736. * consumeitem <item id>{,<char_id>};
  17737. * consumeitem "<item name>"{,<char_id>};
  17738. * @param item: Item ID or name
  17739. */
  17740. BUILDIN_FUNC(consumeitem)
  17741. {
  17742. TBL_PC *sd;
  17743. struct script_data *data;
  17744. struct item_data *item_data;
  17745. if (!script_charid2sd(3, sd))
  17746. return SCRIPT_CMD_FAILURE;
  17747. data = script_getdata( st, 2 );
  17748. get_val( st, data );
  17749. if( data_isstring( data ) ){
  17750. const char *name = conv_str( st, data );
  17751. if( ( item_data = itemdb_searchname( name ) ) == NULL ){
  17752. ShowError( "buildin_consumeitem: Nonexistant item %s requested.\n", name );
  17753. return SCRIPT_CMD_FAILURE;
  17754. }
  17755. }else if( data_isint( data ) ){
  17756. unsigned short nameid = conv_num( st, data );
  17757. if( ( item_data = itemdb_exists( nameid ) ) == NULL ){
  17758. ShowError("buildin_consumeitem: Nonexistant item %hu requested.\n", nameid );
  17759. return SCRIPT_CMD_FAILURE;
  17760. }
  17761. }else{
  17762. ShowError("buildin_consumeitem: invalid data type for argument #1 (%d).\n", data->type );
  17763. return SCRIPT_CMD_FAILURE;
  17764. }
  17765. run_script( item_data->script, 0, sd->bl.id, 0 );
  17766. return SCRIPT_CMD_SUCCESS;
  17767. }
  17768. /** Makes a player sit/stand.
  17769. * sit {"<character name>"};
  17770. * stand {"<character name>"};
  17771. * Note: Use readparam(Sitting) which returns 1 or 0 (sitting or standing).
  17772. * @param name: Player name that will be invoked
  17773. */
  17774. BUILDIN_FUNC(sit)
  17775. {
  17776. TBL_PC *sd;
  17777. if( !script_nick2sd(2,sd) )
  17778. return SCRIPT_CMD_FAILURE;
  17779. if( !pc_issit(sd) ) {
  17780. pc_setsit(sd);
  17781. skill_sit(sd, 1);
  17782. clif_sitting(&sd->bl);
  17783. }
  17784. return SCRIPT_CMD_SUCCESS;
  17785. }
  17786. /** Makes player to stand
  17787. * @param name: Player name that will be set
  17788. */
  17789. BUILDIN_FUNC(stand)
  17790. {
  17791. TBL_PC *sd;
  17792. if( !script_nick2sd(2,sd) )
  17793. return SCRIPT_CMD_FAILURE;
  17794. if( pc_issit(sd) && pc_setstand(sd, false)) {
  17795. skill_sit(sd, 0);
  17796. clif_standing(&sd->bl);
  17797. }
  17798. return SCRIPT_CMD_SUCCESS;
  17799. }
  17800. /** Creates an array of bounded item IDs
  17801. * countbound {<type>{,<char_id>}};
  17802. * @param type: 0 - All bound items; 1 - Account Bound; 2 - Guild Bound; 3 - Party Bound
  17803. * @return amt: Amount of items found
  17804. */
  17805. BUILDIN_FUNC(countbound)
  17806. {
  17807. int i, type, j = 0, k = 0;
  17808. TBL_PC *sd;
  17809. if (!script_charid2sd(3,sd))
  17810. return SCRIPT_CMD_FAILURE;
  17811. type = script_getnum(st,2);
  17812. for( i = 0; i < MAX_INVENTORY; i ++ ) {
  17813. if( sd->inventory.u.items_inventory[i].nameid > 0 && (
  17814. (!type && sd->inventory.u.items_inventory[i].bound) || (type && sd->inventory.u.items_inventory[i].bound == type)
  17815. ))
  17816. {
  17817. pc_setreg(sd,reference_uid(add_str("@bound_items"), k),sd->inventory.u.items_inventory[i].nameid);
  17818. k++;
  17819. j += sd->inventory.u.items_inventory[i].amount;
  17820. }
  17821. }
  17822. script_pushint(st,j);
  17823. return SCRIPT_CMD_SUCCESS;
  17824. }
  17825. /** Creates new party
  17826. * party_create "<party name>"{,<char id>{,<item share>{,<item share type>}}};
  17827. * @param party_name: String of party name that will be created
  17828. * @param char_id: Chara that will be leader of this new party. If no char_id specified, the invoker will be party leader
  17829. * @param item_share: 0-Each Take. 1-Party Share
  17830. * @param item_share_type: 0-Each Take. 1-Even Share
  17831. * @return val: Result value
  17832. * -3 - party name is exist
  17833. * -2 - player is in party already
  17834. * -1 - player is not found
  17835. * 0 - unknown error
  17836. * 1 - success, will return party id $@party_create_id
  17837. */
  17838. BUILDIN_FUNC(party_create)
  17839. {
  17840. char party_name[NAME_LENGTH];
  17841. int item1 = 0, item2 = 0;
  17842. TBL_PC *sd = NULL;
  17843. if (!script_charid2sd(3, sd)) {
  17844. script_pushint(st,-1);
  17845. return SCRIPT_CMD_FAILURE;
  17846. }
  17847. if( sd->status.party_id ) {
  17848. script_pushint(st,-2);
  17849. return SCRIPT_CMD_FAILURE;
  17850. }
  17851. safestrncpy(party_name,script_getstr(st,2),NAME_LENGTH);
  17852. trim(party_name);
  17853. if( party_searchname(party_name) ) {
  17854. script_pushint(st,-3);
  17855. return SCRIPT_CMD_FAILURE;
  17856. }
  17857. if( script_getnum(st,4) )
  17858. item1 = 1;
  17859. if( script_getnum(st,5) )
  17860. item2 = 1;
  17861. party_create_byscript = 1;
  17862. script_pushint(st,party_create(sd,party_name,item1,item2));
  17863. return SCRIPT_CMD_SUCCESS;
  17864. }
  17865. /** Adds player to specified party
  17866. * party_addmember <party id>,<char id>;
  17867. * @param party_id: The party that will be entered by player
  17868. * @param char_id: Char id of player that will be joined to the party
  17869. * @return val: Result value
  17870. * -5 - another character of the same account is in the party
  17871. * -4 - party is full
  17872. * -3 - party is not found
  17873. * -2 - player is in party already
  17874. * -1 - player is not found
  17875. * 0 - unknown error
  17876. * 1 - success
  17877. */
  17878. BUILDIN_FUNC(party_addmember)
  17879. {
  17880. int party_id = script_getnum(st,2);
  17881. TBL_PC *sd;
  17882. struct party_data *party;
  17883. if( !(sd = map_charid2sd(script_getnum(st,3))) ) {
  17884. script_pushint(st,-1);
  17885. return SCRIPT_CMD_FAILURE;
  17886. }
  17887. if( sd->status.party_id ) {
  17888. script_pushint(st,-2);
  17889. return SCRIPT_CMD_FAILURE;
  17890. }
  17891. if( !(party = party_search(party_id)) ) {
  17892. script_pushint(st,-3);
  17893. return SCRIPT_CMD_FAILURE;
  17894. }
  17895. if (battle_config.block_account_in_same_party) {
  17896. int i;
  17897. ARR_FIND(0, MAX_PARTY, i, party->party.member[i].account_id == sd->status.account_id);
  17898. if (i < MAX_PARTY) {
  17899. script_pushint(st,-5);
  17900. return SCRIPT_CMD_FAILURE;
  17901. }
  17902. }
  17903. if( party->party.count >= MAX_PARTY ) {
  17904. script_pushint(st,-4);
  17905. return SCRIPT_CMD_FAILURE;
  17906. }
  17907. sd->party_invite = party_id;
  17908. script_pushint(st,party_add_member(party_id,sd));
  17909. return SCRIPT_CMD_SUCCESS;
  17910. }
  17911. /** Removes player from his/her party. If party_id and char_id is empty remove the invoker from his/her party
  17912. * party_delmember {<char id>,<party_id>};
  17913. * @param: char_id
  17914. * @param: party_id
  17915. * @return val: Result value
  17916. * -3 - player is not in party
  17917. * -2 - party is not found
  17918. * -1 - player is not found
  17919. * 0 - unknown error
  17920. * 1 - success
  17921. */
  17922. BUILDIN_FUNC(party_delmember)
  17923. {
  17924. TBL_PC *sd = NULL;
  17925. if( !script_hasdata(st,2) && !script_hasdata(st,3) && !(sd = script_rid2sd(st)) ) {
  17926. script_pushint(st,-1);
  17927. return SCRIPT_CMD_FAILURE;
  17928. }
  17929. if( sd || (script_getnum(st,2) && (sd = map_charid2sd(script_getnum(st,2)))) )
  17930. script_pushint(st,party_removemember2(sd,0,0));
  17931. else
  17932. script_pushint(st,party_removemember2(NULL,script_getnum(st,2),script_getnum(st,3)));
  17933. return SCRIPT_CMD_SUCCESS;
  17934. }
  17935. /** Changes party leader of specified party (even the leader is offline)
  17936. * party_changeleader <party id>,<char id>;
  17937. * @param party_id: ID of party
  17938. * @param char_id: Char ID of new leader
  17939. * @return val: Result value
  17940. * -4 - player is party leader already
  17941. * -3 - player is not in this party
  17942. * -2 - player is not found
  17943. * -1 - party is not found
  17944. * 0 - unknown error
  17945. * 1 - success */
  17946. BUILDIN_FUNC(party_changeleader)
  17947. {
  17948. int i, party_id = script_getnum(st,2);
  17949. TBL_PC *sd = NULL;
  17950. TBL_PC *tsd = NULL;
  17951. struct party_data *party = NULL;
  17952. if( !(party = party_search(party_id)) ) {
  17953. script_pushint(st,-1);
  17954. return SCRIPT_CMD_FAILURE;
  17955. }
  17956. if( !(tsd = map_charid2sd(script_getnum(st,3))) ) {
  17957. script_pushint(st,-2);
  17958. return SCRIPT_CMD_FAILURE;
  17959. }
  17960. if( tsd->status.party_id != party_id ) {
  17961. script_pushint(st,-3);
  17962. return SCRIPT_CMD_FAILURE;
  17963. }
  17964. ARR_FIND(0,MAX_PARTY,i,party->party.member[i].leader);
  17965. if( i >= MAX_PARTY ) { //this is should impossible!
  17966. script_pushint(st,0);
  17967. return SCRIPT_CMD_FAILURE;
  17968. }
  17969. if( party->data[i].sd == tsd ) {
  17970. script_pushint(st,-4);
  17971. return SCRIPT_CMD_FAILURE;
  17972. }
  17973. script_pushint(st,party_changeleader(sd,tsd,party));
  17974. return SCRIPT_CMD_SUCCESS;
  17975. }
  17976. /** Changes party option
  17977. * party_changeoption <party id>,<option>,<flag>;
  17978. * @param party_id: ID of party that will be changed
  17979. * @param option: Type of option
  17980. * @return val: -1 - party is not found, 0 - invalid option, 1 - success
  17981. */
  17982. BUILDIN_FUNC(party_changeoption)
  17983. {
  17984. struct party_data *party;
  17985. if( !(party = party_search(script_getnum(st,2))) ) {
  17986. script_pushint(st,-1);
  17987. return SCRIPT_CMD_FAILURE;
  17988. }
  17989. script_pushint(st,party_setoption(party,script_getnum(st,3),script_getnum(st,4)));
  17990. return SCRIPT_CMD_SUCCESS;
  17991. }
  17992. /** Destroys party with party id.
  17993. * party_destroy <party id>;
  17994. * @param party_id: ID of party that will be destroyed
  17995. */
  17996. BUILDIN_FUNC(party_destroy)
  17997. {
  17998. int i;
  17999. struct party_data *party;
  18000. if( !(party = party_search(script_getnum(st,2))) ) {
  18001. script_pushint(st,0);
  18002. return SCRIPT_CMD_FAILURE;
  18003. }
  18004. ARR_FIND(0,MAX_PARTY,i,party->party.member[i].leader);
  18005. if( i >= MAX_PARTY || !party->data[i].sd ) { //leader not online
  18006. int j;
  18007. for( j = 0; j < MAX_PARTY; j++ ) {
  18008. TBL_PC *sd = party->data[j].sd;
  18009. if(sd)
  18010. party_member_withdraw(party->party.party_id,sd->status.account_id,sd->status.char_id,sd->status.name,PARTY_MEMBER_WITHDRAW_LEAVE);
  18011. else if( party->party.member[j].char_id )
  18012. 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);
  18013. }
  18014. party_broken(party->party.party_id);
  18015. script_pushint(st,1);
  18016. }
  18017. else //leader leave = party broken
  18018. script_pushint(st,party_leave(party->data[i].sd));
  18019. return SCRIPT_CMD_SUCCESS;
  18020. }
  18021. /** Checks if a player's client version meets a required version or date.
  18022. * @param type: 0 - check by version number; 1 - check by date
  18023. * @param data: Input
  18024. */
  18025. BUILDIN_FUNC(is_clientver) {
  18026. TBL_PC *sd = NULL;
  18027. int type = script_getnum(st,2);
  18028. int data = script_getnum(st,3);
  18029. int ret = 0;
  18030. if (script_hasdata(st,4))
  18031. sd = map_charid2sd(script_getnum(st,4));
  18032. else
  18033. sd = script_rid2sd(st);
  18034. if (sd == NULL) {
  18035. script_pushint(st,0);
  18036. return SCRIPT_CMD_FAILURE;
  18037. }
  18038. switch(type){
  18039. case 0:
  18040. ret = (sd->packet_ver >= data)?1:0;
  18041. break;
  18042. case 1:
  18043. ret = (sd->packet_ver >= date2version(data))?1:0;
  18044. break;
  18045. }
  18046. script_pushint(st,ret);
  18047. return SCRIPT_CMD_SUCCESS;
  18048. }
  18049. /** Returns various information about a player's VIP status. Need to enable VIP system
  18050. * vip_status <type>,{"<character name>"};
  18051. * @param type: Info type, 1: VIP status, 2: Expired date, 3: Remaining time
  18052. * @param name: Character name (Optional)
  18053. */
  18054. BUILDIN_FUNC(vip_status) {
  18055. #ifdef VIP_ENABLE
  18056. TBL_PC *sd;
  18057. char vip_str[26];
  18058. time_t now = time(NULL);
  18059. int type = script_getnum(st, 2);
  18060. if( !script_nick2sd(3,sd) )
  18061. return SCRIPT_CMD_FAILURE;
  18062. switch(type) {
  18063. case 1: // Get VIP status.
  18064. script_pushint(st, pc_isvip(sd));
  18065. break;
  18066. case 2: // Get VIP expire date.
  18067. if (pc_isvip(sd)) {
  18068. time_t viptime = sd->vip.time;
  18069. strftime(vip_str, 24, "%Y-%m-%d %H:%M", localtime(&viptime));
  18070. vip_str[25] = '\0';
  18071. script_pushstrcopy(st, vip_str);
  18072. } else
  18073. script_pushint(st, 0);
  18074. break;
  18075. case 3: // Get remaining time.
  18076. if (pc_isvip(sd)) {
  18077. time_t viptime_remain = sd->vip.time - now;
  18078. int year=0,month=0,day=0,hour=0,min=0,sec=0;
  18079. split_time((int)viptime_remain,&year,&month,&day,&hour,&min,&sec);
  18080. safesnprintf(vip_str,sizeof(vip_str),"%d-%d-%d %d:%d",year,month,day,hour,min);
  18081. script_pushstrcopy(st, vip_str);
  18082. } else
  18083. script_pushint(st, 0);
  18084. break;
  18085. }
  18086. #else
  18087. script_pushint(st, 0);
  18088. #endif
  18089. return SCRIPT_CMD_SUCCESS;
  18090. }
  18091. /** Adds or removes VIP time in minutes. Need to enable VIP system
  18092. * vip_time <time in mn>,{"<character name>"};
  18093. * @param time: VIP duration in minutes. If time < 0 remove time, else add time.
  18094. * @param name: Character name (optional)
  18095. */
  18096. BUILDIN_FUNC(vip_time) {
  18097. #ifdef VIP_ENABLE //would be a pain for scripting npc otherwise
  18098. TBL_PC *sd;
  18099. int viptime = script_getnum(st, 2) * 60; // Convert since it's given in minutes.
  18100. if( !script_nick2sd(3,sd) )
  18101. return SCRIPT_CMD_FAILURE;
  18102. chrif_req_login_operation(sd->status.account_id, sd->status.name, CHRIF_OP_LOGIN_VIP, viptime, 7, 0);
  18103. #endif
  18104. return SCRIPT_CMD_SUCCESS;
  18105. }
  18106. /**
  18107. * Turns a player into a monster and optionally can grant a SC attribute effect.
  18108. * montransform <monster name/ID>, <duration>, <sc type>, <val1>, <val2>, <val3>, <val4>;
  18109. * active_transform <monster name/ID>, <duration>, <sc type>, <val1>, <val2>, <val3>, <val4>;
  18110. * @param monster: Monster ID or name
  18111. * @param duration: Transform duration in millisecond (ms)
  18112. * @param sc_type: Type of SC that will be affected during the transformation
  18113. * @param val1: Value for SC
  18114. * @param val2: Value for SC
  18115. * @param val3: Value for SC
  18116. * @param val4: Value for SC
  18117. * @author: malufett
  18118. */
  18119. BUILDIN_FUNC(montransform) {
  18120. TBL_PC *sd;
  18121. enum sc_type type;
  18122. int tick, mob_id, val1, val2, val3, val4;
  18123. struct script_data *data;
  18124. val1 = val2 = val3 = val4 = 0;
  18125. if( (sd = script_rid2sd(st)) == NULL )
  18126. return SCRIPT_CMD_FAILURE;
  18127. data = script_getdata(st, 2);
  18128. get_val(st, data); // Convert into value in case of a variable
  18129. if( data_isstring(data) )
  18130. mob_id = mobdb_searchname(script_getstr(st, 2));
  18131. else
  18132. mob_id = mobdb_checkid(script_getnum(st, 2));
  18133. tick = script_getnum(st, 3);
  18134. if (script_hasdata(st, 4))
  18135. type = (sc_type)script_getnum(st, 4);
  18136. else
  18137. type = SC_NONE;
  18138. if (mob_id == 0) {
  18139. if( data_isstring(data) )
  18140. ShowWarning("buildin_montransform: Attempted to use non-existing monster '%s'.\n", script_getstr(st, 2));
  18141. else
  18142. ShowWarning("buildin_montransform: Attempted to use non-existing monster of ID '%d'.\n", script_getnum(st, 2));
  18143. return SCRIPT_CMD_FAILURE;
  18144. }
  18145. if (mob_id == MOBID_EMPERIUM) {
  18146. ShowWarning("buildin_montransform: Monster 'Emperium' cannot be used.\n");
  18147. return SCRIPT_CMD_FAILURE;
  18148. }
  18149. if (!(type >= SC_NONE && type < SC_MAX)) {
  18150. ShowWarning("buildin_montransform: Unsupported status change id %d\n", type);
  18151. return SCRIPT_CMD_FAILURE;
  18152. }
  18153. if (script_hasdata(st, 5))
  18154. val1 = script_getnum(st, 5);
  18155. if (script_hasdata(st, 6))
  18156. val2 = script_getnum(st, 6);
  18157. if (script_hasdata(st, 7))
  18158. val3 = script_getnum(st, 7);
  18159. if (script_hasdata(st, 8))
  18160. val4 = script_getnum(st, 8);
  18161. if (tick != 0) {
  18162. if (battle_config.mon_trans_disable_in_gvg && map_flag_gvg2(sd->bl.m)) {
  18163. clif_displaymessage(sd->fd, msg_txt(sd,731)); // Transforming into monster is not allowed in Guild Wars.
  18164. return SCRIPT_CMD_FAILURE;
  18165. }
  18166. if (sd->disguise){
  18167. clif_displaymessage(sd->fd, msg_txt(sd,729)); // Cannot transform into monster while in disguise.
  18168. return SCRIPT_CMD_FAILURE;
  18169. }
  18170. if (!strcmp(script_getfuncname(st), "active_transform")) {
  18171. status_change_end(&sd->bl, SC_ACTIVE_MONSTER_TRANSFORM, INVALID_TIMER); // Clear previous
  18172. sc_start2(NULL, &sd->bl, SC_ACTIVE_MONSTER_TRANSFORM, 100, mob_id, type, tick);
  18173. } else {
  18174. status_change_end(&sd->bl, SC_MONSTER_TRANSFORM, INVALID_TIMER); // Clear previous
  18175. sc_start2(NULL, &sd->bl, SC_MONSTER_TRANSFORM, 100, mob_id, type, tick);
  18176. }
  18177. if (type != SC_NONE)
  18178. sc_start4(NULL, &sd->bl, type, 100, val1, val2, val3, val4, tick);
  18179. }
  18180. return SCRIPT_CMD_SUCCESS;
  18181. }
  18182. /**
  18183. * Attach script to player for certain duration
  18184. * bonus_script "<script code>",<duration>{,<flag>{,<type>{,<status_icon>{,<char_id>}}}};
  18185. * @param "script code"
  18186. * @param duration
  18187. * @param flag
  18188. * @param icon
  18189. * @param char_id
  18190. * @author [Cydh]
  18191. **/
  18192. BUILDIN_FUNC(bonus_script) {
  18193. uint16 flag = 0;
  18194. int16 icon = SI_BLANK;
  18195. uint32 dur;
  18196. uint8 type = 0;
  18197. TBL_PC* sd;
  18198. const char *script_str = NULL;
  18199. struct s_bonus_script_entry *entry = NULL;
  18200. if (script_hasdata(st,7)) {
  18201. if (!(sd = map_charid2sd(script_getnum(st,7)))) {
  18202. ShowError("buildin_bonus_script: Player CID=%d is not found.\n", script_getnum(st,7));
  18203. return SCRIPT_CMD_FAILURE;
  18204. }
  18205. }
  18206. else
  18207. sd = script_rid2sd(st);
  18208. if (sd == NULL)
  18209. return SCRIPT_CMD_FAILURE;
  18210. script_str = script_getstr(st,2);
  18211. dur = 1000 * abs(script_getnum(st,3));
  18212. FETCH(4, flag);
  18213. FETCH(5, type);
  18214. FETCH(6, icon);
  18215. // No Script string, No Duration!
  18216. if (script_str[0] == '\0' || !dur) {
  18217. ShowError("buildin_bonus_script: Invalid! Script: \"%s\". Duration: %d\n", script_str, dur);
  18218. return SCRIPT_CMD_FAILURE;
  18219. }
  18220. if (strlen(script_str) >= MAX_BONUS_SCRIPT_LENGTH) {
  18221. ShowError("buildin_bonus_script: Script string to long: \"%s\".\n", script_str);
  18222. return SCRIPT_CMD_FAILURE;
  18223. }
  18224. if (icon <= SI_BLANK || icon >= SI_MAX)
  18225. icon = SI_BLANK;
  18226. if ((entry = pc_bonus_script_add(sd, script_str, dur, (enum si_type)icon, flag, type))) {
  18227. linkdb_insert(&sd->bonus_script.head, (void *)((intptr_t)entry), entry);
  18228. status_calc_pc(sd,SCO_NONE);
  18229. }
  18230. return SCRIPT_CMD_SUCCESS;
  18231. }
  18232. /**
  18233. * Removes all bonus script from player
  18234. * bonus_script_clear {<flag>,{<char_id>}};
  18235. * @param flag 0 - Except permanent bonus, 1 - With permanent bonus
  18236. * @param char_id Clear script from this character
  18237. * @author [Cydh]
  18238. */
  18239. BUILDIN_FUNC(bonus_script_clear) {
  18240. TBL_PC* sd;
  18241. bool flag = false;
  18242. if (script_hasdata(st,2))
  18243. flag = script_getnum(st,2);
  18244. if (script_hasdata(st,3))
  18245. sd = map_charid2sd(script_getnum(st,3));
  18246. else
  18247. sd = script_rid2sd(st);
  18248. if (sd == NULL)
  18249. return SCRIPT_CMD_FAILURE;
  18250. pc_bonus_script_clear(sd,(flag ? BSF_PERMANENT : BSF_REM_ALL));
  18251. return SCRIPT_CMD_SUCCESS;
  18252. }
  18253. /** Allows player to use atcommand while talking with NPC
  18254. * enable_command;
  18255. * @author [Cydh], [Kichi]
  18256. */
  18257. BUILDIN_FUNC(enable_command) {
  18258. TBL_PC* sd = script_rid2sd(st);
  18259. if (!sd)
  18260. return SCRIPT_CMD_FAILURE;
  18261. sd->state.disable_atcommand_on_npc = 0;
  18262. return SCRIPT_CMD_SUCCESS;
  18263. }
  18264. /** Prevents player to use atcommand while talking with NPC
  18265. * disable_command;
  18266. * @author [Cydh], [Kichi]
  18267. */
  18268. BUILDIN_FUNC(disable_command) {
  18269. TBL_PC* sd = script_rid2sd(st);
  18270. if (!sd)
  18271. return SCRIPT_CMD_FAILURE;
  18272. sd->state.disable_atcommand_on_npc = 1;
  18273. return SCRIPT_CMD_SUCCESS;
  18274. }
  18275. /** Get the information of the members of a guild by type.
  18276. * getguildmember <guild_id>{,<type>};
  18277. * @param guild_id: ID of guild
  18278. * @param type: Type of option (optional)
  18279. */
  18280. BUILDIN_FUNC(getguildmember)
  18281. {
  18282. struct guild *g = NULL;
  18283. uint8 j = 0;
  18284. g = guild_search(script_getnum(st,2));
  18285. if (g) {
  18286. uint8 i, type = 0;
  18287. struct script_data *data = NULL;
  18288. char *varname = NULL;
  18289. if (script_hasdata(st,3))
  18290. type = script_getnum(st,3);
  18291. if (script_hasdata(st,4)) {
  18292. data = script_getdata(st, 4);
  18293. if (!data_isreference(data)) {
  18294. ShowError("buildin_getguildmember: Error in argument! Please give a variable to store values in.\n");
  18295. return SCRIPT_CMD_FAILURE;
  18296. }
  18297. varname = reference_getname(data);
  18298. if (type <= 0 && varname[strlen(varname)-1] != '$') {
  18299. ShowError("buildin_getguildmember: The array %s is not string type.\n", varname);
  18300. return SCRIPT_CMD_FAILURE;
  18301. }
  18302. }
  18303. for (i = 0; i < MAX_GUILD; i++) {
  18304. if (g->member[i].account_id) {
  18305. switch (type) {
  18306. case 2:
  18307. if (data)
  18308. setd_sub(st, NULL, varname, j, (void *)__64BPRTSIZE(g->member[i].account_id), data->ref);
  18309. else
  18310. mapreg_setreg(reference_uid(add_str("$@guildmemberaid"), j),g->member[i].account_id);
  18311. break;
  18312. case 1:
  18313. if (data)
  18314. setd_sub(st, NULL, varname, j, (void *)__64BPRTSIZE(g->member[i].char_id), data->ref);
  18315. else
  18316. mapreg_setreg(reference_uid(add_str("$@guildmembercid"), j), g->member[i].char_id);
  18317. break;
  18318. default:
  18319. if (data)
  18320. setd_sub(st, NULL, varname, j, (void *)__64BPRTSIZE(g->member[i].name), data->ref);
  18321. else
  18322. mapreg_setregstr(reference_uid(add_str("$@guildmembername$"), j), g->member[i].name);
  18323. break;
  18324. }
  18325. j++;
  18326. }
  18327. }
  18328. }
  18329. mapreg_setreg(add_str("$@guildmembercount"), j);
  18330. script_pushint(st, j);
  18331. return SCRIPT_CMD_SUCCESS;
  18332. }
  18333. /** Adds spirit ball to player for 'duration' in milisecond
  18334. * addspiritball <count>,<duration>{,<char_id>};
  18335. * @param count How many spirit ball will be added
  18336. * @param duration How long spiritball is active until it disappears
  18337. * @param char_id Target player (Optional)
  18338. * @author [Cydh]
  18339. */
  18340. BUILDIN_FUNC(addspiritball) {
  18341. uint8 i, count = script_getnum(st,2);
  18342. uint16 duration = script_getnum(st,3);
  18343. struct map_session_data *sd = NULL;
  18344. if (count == 0)
  18345. return SCRIPT_CMD_SUCCESS;
  18346. if (script_hasdata(st,4)) {
  18347. if (!script_isstring(st,4))
  18348. sd = map_charid2sd(script_getnum(st,4));
  18349. else
  18350. sd = map_nick2sd(script_getstr(st,4),false);
  18351. }
  18352. else
  18353. sd = script_rid2sd(st);
  18354. if (!sd)
  18355. return SCRIPT_CMD_FAILURE;
  18356. for (i = 0; i < count; i++)
  18357. pc_addspiritball(sd,duration,10);
  18358. return SCRIPT_CMD_SUCCESS;
  18359. }
  18360. /** Deletes the spirit ball(s) from player
  18361. * delspiritball <count>{,<char_id>};
  18362. * @param count How many spirit ball will be deleted
  18363. * @param char_id Target player (Optional)
  18364. * @author [Cydh]
  18365. */
  18366. BUILDIN_FUNC(delspiritball) {
  18367. uint8 count = script_getnum(st,2);
  18368. struct map_session_data *sd = NULL;
  18369. if (count == 0)
  18370. count = 1;
  18371. if (script_hasdata(st,3)) {
  18372. if (!script_isstring(st,3))
  18373. sd = map_charid2sd(script_getnum(st,3));
  18374. else
  18375. sd = map_nick2sd(script_getstr(st,3),false);
  18376. }
  18377. else
  18378. sd = script_rid2sd(st);
  18379. if (!sd)
  18380. return SCRIPT_CMD_FAILURE;
  18381. pc_delspiritball(sd,count,0);
  18382. return SCRIPT_CMD_SUCCESS;
  18383. }
  18384. /** Counts the spirit ball that player has
  18385. * countspiritball {<char_id>};
  18386. * @param char_id Target player (Optional)
  18387. * @author [Cydh]
  18388. */
  18389. BUILDIN_FUNC(countspiritball) {
  18390. struct map_session_data *sd;
  18391. if (script_hasdata(st,2)) {
  18392. if (!script_isstring(st,2))
  18393. sd = map_charid2sd(script_getnum(st,2));
  18394. else
  18395. sd = map_nick2sd(script_getstr(st,2),false);
  18396. }
  18397. else
  18398. sd = script_rid2sd(st);
  18399. if (!sd)
  18400. return SCRIPT_CMD_FAILURE;
  18401. script_pushint(st,sd->spiritball);
  18402. return SCRIPT_CMD_SUCCESS;
  18403. }
  18404. /** Merges separated stackable items because of guid
  18405. * mergeitem {<char_id>};
  18406. * @param char_id Char ID (Optional)
  18407. * @author [Cydh]
  18408. */
  18409. BUILDIN_FUNC(mergeitem) {
  18410. struct map_session_data *sd;
  18411. if (!script_charid2sd(2, sd))
  18412. return SCRIPT_CMD_FAILURE;
  18413. clif_merge_item_open(sd);
  18414. return SCRIPT_CMD_SUCCESS;
  18415. }
  18416. /** Merges separated stackable items because of guid (Unofficial)
  18417. * mergeitem2 {<item_id>,{<char_id>}};
  18418. * mergeitem2 {"<item name>",{<char_id>}};
  18419. * @param item Item ID/Name for merging specific item (Optional)
  18420. * @author [Cydh]
  18421. */
  18422. BUILDIN_FUNC(mergeitem2) {
  18423. struct map_session_data *sd;
  18424. struct item *items = NULL;
  18425. uint16 i, count = 0, nameid = 0;
  18426. if (!script_charid2sd(3, sd))
  18427. return SCRIPT_CMD_FAILURE;
  18428. if (script_hasdata(st, 2)) {
  18429. struct script_data *data = script_getdata(st, 2);
  18430. get_val(st, data);
  18431. if (data_isstring(data)) {// "<item name>"
  18432. const char *name = conv_str(st,data);
  18433. struct item_data *id;
  18434. if (!(id = itemdb_searchname(name))) {
  18435. ShowError("buildin_mergeitem2: Nonexistant item %s requested.\n", name);
  18436. script_pushint(st, count);
  18437. return SCRIPT_CMD_FAILURE;
  18438. }
  18439. nameid = id->nameid;
  18440. }
  18441. else if (data_isint(data)) {// <item id>
  18442. nameid = conv_num(st,data);
  18443. if (!itemdb_exists(nameid)) {
  18444. ShowError("buildin_mergeitem: Nonexistant item %d requested.\n", nameid);
  18445. script_pushint(st, count);
  18446. return SCRIPT_CMD_FAILURE;
  18447. }
  18448. }
  18449. }
  18450. for (i = 0; i < MAX_INVENTORY; i++) {
  18451. struct item *it = &sd->inventory.u.items_inventory[i];
  18452. if (!it || !it->unique_id || it->expire_time || !itemdb_isstackable(it->nameid))
  18453. continue;
  18454. if ((!nameid || (nameid == it->nameid))) {
  18455. uint8 k;
  18456. if (!count) {
  18457. CREATE(items, struct item, 1);
  18458. memcpy(&items[count++], it, sizeof(struct item));
  18459. pc_delitem(sd, i, it->amount, 0, 0, LOG_TYPE_NPC);
  18460. continue;
  18461. }
  18462. for (k = 0; k < count; k++) {
  18463. // Find Match
  18464. if (&items[k] && items[k].nameid == it->nameid && items[k].bound == it->bound && memcmp(items[k].card, it->card, sizeof(it->card)) == 0) {
  18465. items[k].amount += it->amount;
  18466. pc_delitem(sd, i, it->amount, 0, 0, LOG_TYPE_NPC);
  18467. break;
  18468. }
  18469. }
  18470. if (k >= count) {
  18471. // New entry
  18472. RECREATE(items, struct item, count+1);
  18473. memcpy(&items[count++], it, sizeof(struct item));
  18474. pc_delitem(sd, i, it->amount, 0, 0, LOG_TYPE_NPC);
  18475. }
  18476. }
  18477. }
  18478. if (!items) // Nothing todo here
  18479. return SCRIPT_CMD_SUCCESS;
  18480. // Retrieve the items
  18481. for (i = 0; i < count; i++) {
  18482. uint8 flag = 0;
  18483. if (!&items[i])
  18484. continue;
  18485. items[i].id = 0;
  18486. items[i].unique_id = 0;
  18487. if ((flag = pc_additem(sd, &items[i], items[i].amount, LOG_TYPE_NPC)))
  18488. clif_additem(sd, i, items[i].amount, flag);
  18489. }
  18490. aFree(items);
  18491. script_pushint(st, count);
  18492. return SCRIPT_CMD_SUCCESS;
  18493. }
  18494. /**
  18495. * Update an entry from NPC shop.
  18496. * npcshopupdate "<name>",<item_id>,<price>{,<stock>}
  18497. **/
  18498. BUILDIN_FUNC(npcshopupdate) {
  18499. const char* npcname = script_getstr(st, 2);
  18500. struct npc_data* nd = npc_name2id(npcname);
  18501. uint16 nameid = script_getnum(st, 3);
  18502. int price = script_getnum(st, 4);
  18503. #if PACKETVER >= 20131223
  18504. uint16 stock = script_hasdata(st,5) ? script_getnum(st,5) : 0;
  18505. #endif
  18506. int i;
  18507. if( !nd || ( nd->subtype != NPCTYPE_SHOP && nd->subtype != NPCTYPE_CASHSHOP && nd->subtype != NPCTYPE_ITEMSHOP && nd->subtype != NPCTYPE_POINTSHOP && nd->subtype != NPCTYPE_MARKETSHOP ) ) { // Not found.
  18508. script_pushint(st,0);
  18509. return SCRIPT_CMD_FAILURE;
  18510. }
  18511. if (!nd->u.shop.count) {
  18512. ShowError("buildin_npcshopupdate: Attempt to update empty shop from '%s'.\n", nd->exname);
  18513. script_pushint(st,0);
  18514. return SCRIPT_CMD_FAILURE;
  18515. }
  18516. for (i = 0; i < nd->u.shop.count; i++) {
  18517. if (nd->u.shop.shop_item[i].nameid == nameid) {
  18518. if (price != 0)
  18519. nd->u.shop.shop_item[i].value = price;
  18520. #if PACKETVER >= 20131223
  18521. if (nd->subtype == NPCTYPE_MARKETSHOP) {
  18522. nd->u.shop.shop_item[i].qty = stock;
  18523. npc_market_tosql(nd->exname, &nd->u.shop.shop_item[i]);
  18524. }
  18525. #endif
  18526. }
  18527. }
  18528. script_pushint(st,1);
  18529. return SCRIPT_CMD_SUCCESS;
  18530. }
  18531. // Clan System
  18532. BUILDIN_FUNC(clan_join){
  18533. struct map_session_data *sd;
  18534. int clan_id = script_getnum(st,2);
  18535. if( !script_charid2sd( 3, sd ) ){
  18536. script_pushint(st, false);
  18537. return SCRIPT_CMD_FAILURE;
  18538. }
  18539. if( clan_member_join( sd, clan_id, sd->status.account_id, sd->status.char_id ) )
  18540. script_pushint(st, true);
  18541. else
  18542. script_pushint(st, false);
  18543. return SCRIPT_CMD_SUCCESS;
  18544. }
  18545. BUILDIN_FUNC(clan_leave){
  18546. struct map_session_data *sd;
  18547. if( !script_charid2sd( 2, sd ) ){
  18548. script_pushint(st, false);
  18549. return SCRIPT_CMD_FAILURE;
  18550. }
  18551. if( clan_member_leave( sd, sd->status.clan_id, sd->status.account_id, sd->status.char_id ) )
  18552. script_pushint(st, true);
  18553. else
  18554. script_pushint(st, false);
  18555. return SCRIPT_CMD_SUCCESS;
  18556. }
  18557. /**
  18558. * Get rid from running script.
  18559. * getattachedrid();
  18560. **/
  18561. BUILDIN_FUNC(getattachedrid) {
  18562. script_pushint(st, st->rid);
  18563. return SCRIPT_CMD_SUCCESS;
  18564. }
  18565. /**
  18566. * Get variable from a Player
  18567. * getvar <variable>,<char_id>;
  18568. */
  18569. BUILDIN_FUNC(getvar) {
  18570. int char_id = script_getnum(st, 3);
  18571. struct map_session_data *sd = map_charid2sd(char_id);
  18572. struct script_data *data = NULL;
  18573. const char *name = NULL;
  18574. if (!sd) {
  18575. ShowError("buildin_getvar: No player found with char id '%d'.\n", char_id);
  18576. return SCRIPT_CMD_FAILURE;
  18577. }
  18578. data = script_getdata(st, 2);
  18579. if (!data_isreference(data)) {
  18580. ShowError("buildin_getvar: Not a variable\n");
  18581. script_reportdata(data);
  18582. script_pushnil(st);
  18583. st->state = END;
  18584. return SCRIPT_CMD_FAILURE;
  18585. }
  18586. name = reference_getname(data);
  18587. if (reference_toparam(data)) {
  18588. ShowError("buildin_getvar: '%s' is a parameter - please use readparam instead\n", name);
  18589. script_reportdata(data);
  18590. script_pushnil(st);
  18591. st->state = END;
  18592. return SCRIPT_CMD_FAILURE;
  18593. }
  18594. if (name[0] == '.' || name[0] == '$' || name[0] == '\'') { // Not a PC variable
  18595. ShowError("buildin_getvar: Invalid scope (not PC variable)\n");
  18596. script_reportdata(data);
  18597. script_pushnil(st);
  18598. st->state = END;
  18599. return SCRIPT_CMD_FAILURE;
  18600. }
  18601. get_val_(st, data, sd);
  18602. if (data_isint(data))
  18603. script_pushint(st, conv_num_(st, data, sd));
  18604. else
  18605. script_pushstrcopy(st, conv_str_(st, data, sd));
  18606. push_val2(st->stack, C_NAME, reference_getuid(data), reference_getref(data));
  18607. return SCRIPT_CMD_SUCCESS;
  18608. }
  18609. /**
  18610. * Display script message
  18611. * showscript "<message>"{,<GID>};
  18612. **/
  18613. BUILDIN_FUNC(showscript) {
  18614. struct block_list *bl = NULL;
  18615. const char *msg = script_getstr(st,2);
  18616. int id = 0;
  18617. if (script_hasdata(st,3)) {
  18618. id = script_getnum(st,3);
  18619. bl = map_id2bl(id);
  18620. }
  18621. else {
  18622. bl = st->rid ? map_id2bl(st->rid) : map_id2bl(st->oid);
  18623. }
  18624. if (!bl) {
  18625. ShowError("buildin_showscript: Script not attached. (id=%d, rid=%d, oid=%d)\n", id, st->rid, st->oid);
  18626. script_pushint(st,0);
  18627. return SCRIPT_CMD_FAILURE;
  18628. }
  18629. clif_showscript(bl, msg);
  18630. script_pushint(st,1);
  18631. return SCRIPT_CMD_SUCCESS;
  18632. }
  18633. /**
  18634. * Ignore the SECURE_NPCTIMEOUT function.
  18635. * ignoretimeout <flag>{,<char_id>};
  18636. */
  18637. BUILDIN_FUNC(ignoretimeout)
  18638. {
  18639. #ifdef SECURE_NPCTIMEOUT
  18640. struct map_session_data *sd = NULL;
  18641. if (script_hasdata(st,3)) {
  18642. if (!script_isstring(st,3))
  18643. sd = map_charid2sd(script_getnum(st,3));
  18644. else
  18645. sd = map_nick2sd(script_getstr(st,3),false);
  18646. } else
  18647. sd = script_rid2sd(st);
  18648. if (!sd)
  18649. return SCRIPT_CMD_FAILURE;
  18650. sd->state.ignoretimeout = script_getnum(st,2) > 0;
  18651. #endif
  18652. return SCRIPT_CMD_SUCCESS;
  18653. }
  18654. /**
  18655. * geteleminfo <type>{,<char_id>};
  18656. **/
  18657. BUILDIN_FUNC(geteleminfo) {
  18658. TBL_ELEM *ed = NULL;
  18659. TBL_PC *sd = NULL;
  18660. int type = script_getnum(st,2);
  18661. if (!script_charid2sd(3, sd)) {
  18662. script_pushint(st, 0);
  18663. return SCRIPT_CMD_SUCCESS;
  18664. }
  18665. if (!(ed = sd->ed)) {
  18666. //ShowDebug("buildin_geteleminfo: Player doesn't have Elemental.\n");
  18667. script_pushint(st, 0);
  18668. return SCRIPT_CMD_SUCCESS;
  18669. }
  18670. switch (type) {
  18671. case 0: script_pushint(st, ed->elemental.elemental_id); break;
  18672. case 1: script_pushint(st, ed->bl.id); break;
  18673. default:
  18674. ShowError("buildin_geteleminfo: Invalid type '%d'.\n", type);
  18675. script_pushint(st, 0);
  18676. return SCRIPT_CMD_FAILURE;
  18677. }
  18678. return SCRIPT_CMD_SUCCESS;
  18679. }
  18680. /**
  18681. * Set the quest info of quest_id only showed on player in level range.
  18682. * setquestinfo_level <quest_id>,<min_level>,<max_level>
  18683. * @author [Cydh]
  18684. **/
  18685. BUILDIN_FUNC(setquestinfo_level) {
  18686. TBL_NPC* nd = map_id2nd(st->oid);
  18687. int quest_id = script_getnum(st, 2);
  18688. struct questinfo *qi = map_has_questinfo(nd->bl.m, nd, quest_id);
  18689. if (!qi) {
  18690. ShowError("buildin_setquestinfo_level: Quest with ID '%d' is not defined yet.\n", quest_id);
  18691. return SCRIPT_CMD_FAILURE;
  18692. }
  18693. qi->min_level = script_getnum(st, 3);
  18694. qi->max_level = script_getnum(st, 4);
  18695. if (!qi->max_level)
  18696. qi->max_level = MAX_LEVEL;
  18697. return SCRIPT_CMD_SUCCESS;
  18698. }
  18699. /**
  18700. * Set the quest info of quest_id only showed for player that has quest criteria
  18701. * setquestinfo_req <quest_id>,<quest_req_id>,<state>{,<quest_req_id>,<state>,...};
  18702. * @author [Cydh]
  18703. **/
  18704. BUILDIN_FUNC(setquestinfo_req) {
  18705. TBL_NPC* nd = map_id2nd(st->oid);
  18706. int quest_id = script_getnum(st, 2);
  18707. struct questinfo *qi = map_has_questinfo(nd->bl.m, nd, quest_id);
  18708. uint8 i = 0;
  18709. uint8 num = script_lastdata(st);
  18710. if (!qi) {
  18711. ShowError("buildin_setquestinfo_req: Quest with ID '%d' is not defined yet.\n", quest_id);
  18712. return SCRIPT_CMD_FAILURE;
  18713. }
  18714. if (quest_search(quest_id) == &quest_dummy) {
  18715. ShowError("buildin_setquestinfo_req: Quest with ID '%d' is not found in Quest DB.\n", quest_id);
  18716. return SCRIPT_CMD_FAILURE;
  18717. }
  18718. if (num%2) {
  18719. ShowError("buildin_setquestinfo_req: Odd number of parameters(%d) - pairs of requirements are expected.\n", num-2);
  18720. return SCRIPT_CMD_FAILURE;
  18721. }
  18722. for (i = 3; i <= num; i += 2) {
  18723. RECREATE(qi->req, struct questinfo_req, qi->req_count+1);
  18724. qi->req[qi->req_count].quest_id = script_getnum(st, i);
  18725. qi->req[qi->req_count].state = (script_getnum(st, i+1) >= 2) ? 2 : (script_getnum(st, i+1) <= 0) ? 0 : 1;
  18726. qi->req_count++;
  18727. }
  18728. return SCRIPT_CMD_SUCCESS;
  18729. }
  18730. /**
  18731. * Set the quest info of quest_id only showed for player that has specified Job
  18732. * setquestinfo_job <quest_id>,<job>{,<job>...};
  18733. * @author [Cydh]
  18734. **/
  18735. BUILDIN_FUNC(setquestinfo_job) {
  18736. TBL_NPC* nd = map_id2nd(st->oid);
  18737. int quest_id = script_getnum(st, 2);
  18738. struct questinfo *qi = map_has_questinfo(nd->bl.m, nd, quest_id);
  18739. int job_id = 0;
  18740. uint8 i = 0;
  18741. uint8 num = script_lastdata(st)+1;
  18742. if (!qi) {
  18743. ShowError("buildin_setquestinfo_job: Quest with ID '%d' is not defined yet.\n", quest_id);
  18744. return SCRIPT_CMD_FAILURE;
  18745. }
  18746. for (i = 3; i < num; i++) {
  18747. job_id = script_getnum(st, i);
  18748. if (!pcdb_checkid(job_id)) {
  18749. ShowError("buildin_setquestinfo_job: Invalid job id '%d' in Quest with ID %d.\n", job_id, quest_id);
  18750. continue;
  18751. }
  18752. buildin_questinfo_setjob(qi, job_id);
  18753. }
  18754. return SCRIPT_CMD_SUCCESS;
  18755. }
  18756. /**
  18757. * opendressroom(<flag>{,<char_id>});
  18758. */
  18759. BUILDIN_FUNC(opendressroom)
  18760. {
  18761. #if PACKETVER >= 20150513
  18762. int flag = 1;
  18763. TBL_PC* sd;
  18764. if( script_hasdata(st,2) )
  18765. flag = script_getnum(st,2);
  18766. if (!script_charid2sd(3, sd))
  18767. return SCRIPT_CMD_FAILURE;
  18768. clif_dressing_room(sd, flag);
  18769. return SCRIPT_CMD_SUCCESS;
  18770. #else
  18771. return SCRIPT_CMD_FAILURE;
  18772. #endif
  18773. }
  18774. /**
  18775. * navigateto("<map>"{,<x>,<y>,<flag>,<hide_window>,<monster_id>,<char_id>});
  18776. */
  18777. BUILDIN_FUNC(navigateto){
  18778. #if PACKETVER >= 20111010
  18779. TBL_PC* sd;
  18780. const char *mapname;
  18781. uint16 x = 0, y = 0, monster_id = 0;
  18782. uint8 flag = NAV_KAFRA_AND_AIRSHIP;
  18783. bool hideWindow = true;
  18784. mapname = script_getstr(st,2);
  18785. if( script_hasdata(st,3) )
  18786. x = script_getnum(st,3);
  18787. if( script_hasdata(st,4) )
  18788. y = script_getnum(st,4);
  18789. if( script_hasdata(st,5) )
  18790. flag = (uint8)script_getnum(st,5);
  18791. if( script_hasdata(st,6) )
  18792. hideWindow = script_getnum(st,6) ? true : false;
  18793. if( script_hasdata(st,7) )
  18794. monster_id = script_getnum(st,7);
  18795. if (!script_charid2sd(8, sd))
  18796. return SCRIPT_CMD_FAILURE;
  18797. clif_navigateTo(sd,mapname,x,y,flag,hideWindow,monster_id);
  18798. return SCRIPT_CMD_SUCCESS;
  18799. #else
  18800. return SCRIPT_CMD_FAILURE;
  18801. #endif
  18802. }
  18803. /**
  18804. * adopt("<parent_name>","<baby_name>");
  18805. * adopt(<parent_id>,<baby_id>);
  18806. * https://rathena.org/board/topic/104014-suggestion-add-adopt-or-etc/
  18807. */
  18808. BUILDIN_FUNC(adopt)
  18809. {
  18810. TBL_PC *sd, *b_sd;
  18811. struct script_data *data;
  18812. enum adopt_responses response;
  18813. data = script_getdata(st, 2);
  18814. get_val(st, data);
  18815. if (data_isstring(data)) {
  18816. const char *name = conv_str(st, data);
  18817. sd = map_nick2sd(name,false);
  18818. if (sd == NULL) {
  18819. ShowError("buildin_adopt: Non-existant parent character %s requested.\n", name);
  18820. return SCRIPT_CMD_FAILURE;
  18821. }
  18822. } else if (data_isint(data)) {
  18823. uint32 char_id = conv_num(st, data);
  18824. sd = map_charid2sd(char_id);
  18825. if (sd == NULL) {
  18826. ShowError("buildin_adopt: Non-existant parent character %d requested.\n", char_id);
  18827. return SCRIPT_CMD_FAILURE;
  18828. }
  18829. } else {
  18830. ShowError("buildin_adopt: Invalid data type for argument #1 (%d).", data->type);
  18831. return SCRIPT_CMD_FAILURE;
  18832. }
  18833. data = script_getdata(st, 3);
  18834. get_val(st, data);
  18835. if (data_isstring(data)) {
  18836. const char *name = conv_str(st, data);
  18837. b_sd = map_nick2sd(name,false);
  18838. if (b_sd == NULL) {
  18839. ShowError("buildin_adopt: Non-existant baby character %s requested.\n", name);
  18840. return SCRIPT_CMD_FAILURE;
  18841. }
  18842. } else if (data_isint(data)) {
  18843. uint32 char_id = conv_num(st, data);
  18844. b_sd = map_charid2sd(char_id);
  18845. if (b_sd == NULL) {
  18846. ShowError("buildin_adopt: Non-existant baby character %d requested.\n", char_id);
  18847. return SCRIPT_CMD_FAILURE;
  18848. }
  18849. } else {
  18850. ShowError("buildin_adopt: Invalid data type for argument #2 (%d).", data->type);
  18851. return SCRIPT_CMD_FAILURE;
  18852. }
  18853. response = pc_try_adopt(sd, map_charid2sd(sd->status.partner_id), b_sd);
  18854. if (response == ADOPT_ALLOWED) {
  18855. TBL_PC *p_sd = map_charid2sd(sd->status.partner_id);
  18856. b_sd->adopt_invite = sd->status.account_id;
  18857. clif_Adopt_request(b_sd, sd, p_sd->status.account_id);
  18858. script_pushint(st, ADOPT_ALLOWED);
  18859. return SCRIPT_CMD_SUCCESS;
  18860. }
  18861. script_pushint(st, response);
  18862. return SCRIPT_CMD_FAILURE;
  18863. }
  18864. /**
  18865. * Returns the minimum or maximum of all the given parameters for integer variables.
  18866. *
  18867. * min( <value or array>{,value or array 2,...} );
  18868. * minimum( <value or array>{,value or array 2,...} );
  18869. * max( <value or array>{,value or array 2,...} );
  18870. * maximum( <value or array>{,value or array 2,...} );
  18871. */
  18872. BUILDIN_FUNC(minmax){
  18873. char *functionname;
  18874. unsigned int i;
  18875. int value;
  18876. // Function pointer for our comparison function (either min or max at the moment)
  18877. int32 (*func)(int32, int32);
  18878. // Get the real function name
  18879. functionname = script_getfuncname(st);
  18880. // Our data should start at offset 2
  18881. i = 2;
  18882. if( !script_hasdata( st, i ) ){
  18883. ShowError( "buildin_%s: no arguments given!\n", functionname );
  18884. st->state = END;
  18885. return SCRIPT_CMD_FAILURE;
  18886. }
  18887. if( strnicmp( functionname, "min", strlen( "min" ) ) == 0 ){
  18888. value = INT_MAX;
  18889. func = i32min;
  18890. }else if( strnicmp( functionname, "max", strlen( "max" ) ) == 0 ){
  18891. value = INT_MIN;
  18892. func = i32max;
  18893. }else{
  18894. ShowError( "buildin_%s: Unknown call case for min/max!\n", functionname );
  18895. st->state = END;
  18896. return SCRIPT_CMD_FAILURE;
  18897. }
  18898. // As long as we have data on our script stack
  18899. while( script_hasdata(st,i) ){
  18900. struct script_data *data;
  18901. // Get the next piece of data from the script stack
  18902. data = script_getdata( st, i );
  18903. // Is the current parameter a single integer?
  18904. if( data_isint( data ) ){
  18905. value = func( value, script_getnum( st, i ) );
  18906. // Is the current parameter an array variable?
  18907. }else if( data_isreference( data ) ){
  18908. const char *name;
  18909. struct map_session_data* sd;
  18910. unsigned int start, end;
  18911. // Get the name of the variable
  18912. name = reference_getname(data);
  18913. // Check if it's a string variable
  18914. if( is_string_variable( name ) ){
  18915. ShowError( "buildin_%s: illegal type, need integer!\n", functionname );
  18916. script_reportdata( data );
  18917. st->state = END;
  18918. return SCRIPT_CMD_FAILURE;
  18919. }
  18920. // Get the session data, if a player is attached
  18921. sd = st->rid ? map_id2sd(st->rid) : NULL;
  18922. // Try to find the array's source pointer
  18923. if( !script_array_src( st, sd, name, reference_getref( data ) ) ){
  18924. ShowError( "buildin_%s: not a array!\n", functionname );
  18925. script_reportdata( data );
  18926. st->state = END;
  18927. return SCRIPT_CMD_FAILURE;
  18928. }
  18929. // Get the start and end indices of the array
  18930. start = reference_getindex( data );
  18931. end = script_array_highest_key( st, sd, name, reference_getref( data ) );
  18932. // Skip empty arrays
  18933. if( start < end ){
  18934. int id;
  18935. // For getting the values we need the id of the array
  18936. id = reference_getid( data );
  18937. // Loop through each value stored in the array
  18938. for( ; start < end; start++ ){
  18939. value = func( value, (int32)__64BPRTSIZE( get_val2( st, reference_uid( id, start ), reference_getref( data ) ) ) );
  18940. script_removetop( st, -1, 0 );
  18941. }
  18942. }
  18943. }else{
  18944. ShowError( "buildin_%s: not a supported data type!\n", functionname );
  18945. script_reportdata( data );
  18946. st->state = END;
  18947. return SCRIPT_CMD_FAILURE;
  18948. }
  18949. // Continue with the next stack entry
  18950. i++;
  18951. }
  18952. script_pushint( st, value );
  18953. return SCRIPT_CMD_SUCCESS;
  18954. }
  18955. /**
  18956. * Safety Base/Job EXP addition than using `set BaseExp,n;` or `set JobExp,n;`
  18957. * Unlike `getexp` that affected by some adjustments.
  18958. * getexp2 <base_exp>,<job_exp>{,<char_id>};
  18959. * @author [Cydh]
  18960. **/
  18961. BUILDIN_FUNC(getexp2) {
  18962. TBL_PC *sd = NULL;
  18963. int base_exp = script_getnum(st, 2);
  18964. int job_exp = script_getnum(st, 3);
  18965. if (!script_charid2sd(4, sd))
  18966. return SCRIPT_CMD_FAILURE;
  18967. if (base_exp == 0 && job_exp == 0)
  18968. return SCRIPT_CMD_SUCCESS;
  18969. if (base_exp > 0)
  18970. pc_gainexp(sd, NULL, base_exp, 0, 2);
  18971. else if (base_exp < 0)
  18972. pc_lostexp(sd, base_exp * -1, 0);
  18973. if (job_exp > 0)
  18974. pc_gainexp(sd, NULL, 0, job_exp, 2);
  18975. else if (job_exp < 0)
  18976. pc_lostexp(sd, 0, job_exp * -1);
  18977. return SCRIPT_CMD_SUCCESS;
  18978. }
  18979. /**
  18980. * Force stat recalculation of sd
  18981. * recalculatestat;
  18982. * @author [secretdataz]
  18983. **/
  18984. BUILDIN_FUNC(recalculatestat) {
  18985. TBL_PC* sd = script_rid2sd(st);
  18986. if (sd == NULL)
  18987. return SCRIPT_CMD_FAILURE;
  18988. status_calc_pc(sd, SCO_FORCE);
  18989. return SCRIPT_CMD_SUCCESS;
  18990. }
  18991. BUILDIN_FUNC(hateffect){
  18992. #if PACKETVER >= 20150513
  18993. struct map_session_data* sd = script_rid2sd(st);
  18994. bool enable;
  18995. int i, effectID;
  18996. if( sd == NULL )
  18997. return SCRIPT_CMD_FAILURE;
  18998. effectID = script_getnum(st,2);
  18999. enable = script_getnum(st,3) ? true : false;
  19000. ARR_FIND( 0, sd->hatEffectCount, i, sd->hatEffectIDs[i] == effectID );
  19001. if( enable ){
  19002. if( i < sd->hatEffectCount ){
  19003. return SCRIPT_CMD_SUCCESS;
  19004. }
  19005. RECREATE(sd->hatEffectIDs,uint32,sd->hatEffectCount+1);
  19006. sd->hatEffectIDs[sd->hatEffectCount] = effectID;
  19007. sd->hatEffectCount++;
  19008. }else{
  19009. if( i == sd->hatEffectCount ){
  19010. return SCRIPT_CMD_SUCCESS;
  19011. }
  19012. for( ; i < sd->hatEffectCount - 1; i++ ){
  19013. sd->hatEffectIDs[i] = sd->hatEffectIDs[i+1];
  19014. }
  19015. sd->hatEffectCount--;
  19016. if( !sd->hatEffectCount ){
  19017. aFree(sd->hatEffectIDs);
  19018. sd->hatEffectIDs = NULL;
  19019. }
  19020. }
  19021. if( !sd->state.connect_new ){
  19022. clif_hat_effect_single( sd, effectID, enable );
  19023. }
  19024. #endif
  19025. return SCRIPT_CMD_SUCCESS;
  19026. }
  19027. /**
  19028. * Retrieves param of current random option. Intended for random option script only.
  19029. * getrandomoptinfo(<type>);
  19030. * @author [secretdataz]
  19031. **/
  19032. BUILDIN_FUNC(getrandomoptinfo) {
  19033. struct map_session_data *sd;
  19034. int val;
  19035. int param = script_getnum(st, 2);
  19036. 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) {
  19037. switch (param) {
  19038. case ROA_ID:
  19039. val = sd->inventory.u.items_inventory[current_equip_item_index].option[current_equip_opt_index].id;
  19040. break;
  19041. case ROA_VALUE:
  19042. val = sd->inventory.u.items_inventory[current_equip_item_index].option[current_equip_opt_index].value;
  19043. break;
  19044. case ROA_PARAM:
  19045. val = sd->inventory.u.items_inventory[current_equip_item_index].option[current_equip_opt_index].param;
  19046. break;
  19047. default:
  19048. ShowWarning("buildin_getrandomoptinfo: Invalid attribute type %d (Max %d).\n", param, MAX_ITEM_RDM_OPT);
  19049. val = 0;
  19050. break;
  19051. }
  19052. script_pushint(st, val);
  19053. }
  19054. else {
  19055. script_pushint(st, 0);
  19056. }
  19057. return SCRIPT_CMD_SUCCESS;
  19058. }
  19059. /**
  19060. * Retrieves a random option on an equipped item.
  19061. * getequiprandomoption(<equipment slot>,<index>,<type>{,<char id>});
  19062. * @author [secretdataz]
  19063. */
  19064. BUILDIN_FUNC(getequiprandomoption) {
  19065. struct map_session_data *sd;
  19066. int val;
  19067. short i = -1;
  19068. int pos = script_getnum(st, 2);
  19069. int index = script_getnum(st, 3);
  19070. int type = script_getnum(st, 4);
  19071. if (!script_charid2sd(5, sd)) {
  19072. script_pushint(st, -1);
  19073. return SCRIPT_CMD_FAILURE;
  19074. }
  19075. if (index < 0 || index >= MAX_ITEM_RDM_OPT) {
  19076. ShowError("buildin_getequiprandomoption: Invalid random option index %d.\n", index);
  19077. script_pushint(st, -1);
  19078. return SCRIPT_CMD_FAILURE;
  19079. }
  19080. if (equip_index_check(pos))
  19081. i = pc_checkequip(sd, equip_bitmask[pos]);
  19082. if (i < 0) {
  19083. ShowError("buildin_getequiprandomoption: No item equipped at pos %d (CID=%d/AID=%d).\n", pos, sd->status.char_id, sd->status.account_id);
  19084. script_pushint(st, -1);
  19085. return SCRIPT_CMD_FAILURE;
  19086. }
  19087. switch (type) {
  19088. case ROA_ID:
  19089. val = sd->inventory.u.items_inventory[i].option[index].id;
  19090. break;
  19091. case ROA_VALUE:
  19092. val = sd->inventory.u.items_inventory[i].option[index].value;
  19093. break;
  19094. case ROA_PARAM:
  19095. val = sd->inventory.u.items_inventory[i].option[index].param;
  19096. break;
  19097. default:
  19098. ShowWarning("buildin_getequiprandomoption: Invalid attribute type %d (Max %d).\n", type, MAX_ITEM_RDM_OPT);
  19099. val = 0;
  19100. break;
  19101. }
  19102. script_pushint(st, val);
  19103. return SCRIPT_CMD_SUCCESS;
  19104. }
  19105. /**
  19106. * Adds a random option on a random option slot on an equipped item and overwrites
  19107. * existing random option in the process.
  19108. * setrandomoption(<equipment slot>,<index>,<id>,<value>,<param>{,<char id>});
  19109. * @author [secretdataz]
  19110. */
  19111. BUILDIN_FUNC(setrandomoption) {
  19112. struct map_session_data *sd;
  19113. struct s_random_opt_data *opt;
  19114. int pos, index, id, value, param, ep;
  19115. int i = -1;
  19116. if (!script_charid2sd(7, sd))
  19117. return SCRIPT_CMD_FAILURE;
  19118. pos = script_getnum(st, 2);
  19119. index = script_getnum(st, 3);
  19120. id = script_getnum(st, 4);
  19121. value = script_getnum(st, 5);
  19122. param = script_getnum(st, 6);
  19123. if ((opt = itemdb_randomopt_exists((short)id)) == NULL) {
  19124. ShowError("buildin_setrandomoption: Random option ID %d does not exists.\n", id);
  19125. script_pushint(st, 0);
  19126. return SCRIPT_CMD_FAILURE;
  19127. }
  19128. if (index < 0 || index >= MAX_ITEM_RDM_OPT) {
  19129. ShowError("buildin_setrandomoption: Invalid random option index %d.\n", index);
  19130. script_pushint(st, 0);
  19131. return SCRIPT_CMD_FAILURE;
  19132. }
  19133. if (equip_index_check(pos))
  19134. i = pc_checkequip(sd, equip_bitmask[pos]);
  19135. if (i >= 0) {
  19136. ep = sd->inventory.u.items_inventory[i].equip;
  19137. log_pick_pc(sd, LOG_TYPE_SCRIPT, -1, &sd->inventory.u.items_inventory[i]);
  19138. pc_unequipitem(sd, i, 2);
  19139. sd->inventory.u.items_inventory[i].option[index].id = id;
  19140. sd->inventory.u.items_inventory[i].option[index].value = value;
  19141. sd->inventory.u.items_inventory[i].option[index].param = param;
  19142. clif_delitem(sd, i, 1, 3);
  19143. log_pick_pc(sd, LOG_TYPE_SCRIPT, -1, &sd->inventory.u.items_inventory[i]);
  19144. clif_additem(sd, i, 1, 0);
  19145. pc_equipitem(sd, i, ep);
  19146. script_pushint(st, 1);
  19147. return SCRIPT_CMD_SUCCESS;
  19148. }
  19149. ShowError("buildin_setrandomoption: No item equipped at pos %d (CID=%d/AID=%d).\n", pos, sd->status.char_id, sd->status.account_id);
  19150. script_pushint(st, 0);
  19151. return SCRIPT_CMD_FAILURE;
  19152. }
  19153. /// Returns the number of stat points needed to change the specified stat by val.
  19154. /// If val is negative, returns the number of stat points that would be needed to
  19155. /// raise the specified stat from (current value - val) to current value.
  19156. /// *needed_status_point(<type>,<val>{,<char id>});
  19157. /// @author [secretdataz]
  19158. BUILDIN_FUNC(needed_status_point) {
  19159. struct map_session_data *sd;
  19160. int type, val;
  19161. if (!script_charid2sd(4, sd))
  19162. return SCRIPT_CMD_FAILURE;
  19163. type = script_getnum(st, 2);
  19164. val = script_getnum(st, 3);
  19165. script_pushint(st, pc_need_status_point(sd, type, val));
  19166. return SCRIPT_CMD_SUCCESS;
  19167. }
  19168. /**
  19169. * jobcanentermap("<mapname>"{,<JobID>});
  19170. * Check if (player with) JobID can enter the map.
  19171. * @param mapname Map name
  19172. * @param JobID Player's JobID (optional)
  19173. **/
  19174. BUILDIN_FUNC(jobcanentermap) {
  19175. const char *mapname = script_getstr(st, 2);
  19176. int mapidx = mapindex_name2id(mapname), m = -1;
  19177. int jobid = 0;
  19178. TBL_PC *sd = NULL;
  19179. if (!mapidx) {// Invalid map
  19180. script_pushint(st, false);
  19181. return SCRIPT_CMD_FAILURE;
  19182. }
  19183. m = map_mapindex2mapid(mapidx);
  19184. if (m == -1) { // Map is on different map server
  19185. ShowError("buildin_jobcanentermap: Map '%s' is not found in this server.\n", mapname);
  19186. script_pushint(st, false);
  19187. return SCRIPT_CMD_FAILURE;
  19188. }
  19189. if (script_hasdata(st, 3)) {
  19190. jobid = script_getnum(st, 3);
  19191. } else {
  19192. if (!(sd = script_rid2sd(st))) {
  19193. script_pushint(st, false);
  19194. return SCRIPT_CMD_FAILURE;
  19195. }
  19196. jobid = sd->status.class_;
  19197. }
  19198. script_pushint(st, pc_job_can_entermap((enum e_job)jobid, m, sd ? sd->group_level : 0));
  19199. return SCRIPT_CMD_SUCCESS;
  19200. }
  19201. /**
  19202. * Return alliance information between the two guilds.
  19203. * getguildalliance(<guild id1>,<guild id2>);
  19204. * Return values:
  19205. * -2 - Guild ID1 does not exist
  19206. * -1 - Guild ID2 does not exist
  19207. * 0 - Both guilds have no relation OR guild ID aren't given
  19208. * 1 - Both guilds are allies
  19209. * 2 - Both guilds are antagonists
  19210. */
  19211. BUILDIN_FUNC(getguildalliance)
  19212. {
  19213. struct guild *guild_data1, *guild_data2;
  19214. int guild_id1, guild_id2, i = 0;
  19215. guild_id1 = script_getnum(st,2);
  19216. guild_id2 = script_getnum(st,3);
  19217. if (guild_id1 < 1 || guild_id2 < 1) {
  19218. script_pushint(st, 0);
  19219. return SCRIPT_CMD_SUCCESS;
  19220. }
  19221. if (guild_id1 == guild_id2) {
  19222. script_pushint(st, 1);
  19223. return SCRIPT_CMD_SUCCESS;
  19224. }
  19225. guild_data1 = guild_search(guild_id1);
  19226. guild_data2 = guild_search(guild_id2);
  19227. if (guild_data1 == NULL) {
  19228. ShowWarning("buildin_getguildalliance: Requesting non-existent GuildID1 '%d'.\n", guild_id1);
  19229. script_pushint(st, -2);
  19230. return SCRIPT_CMD_FAILURE;
  19231. }
  19232. if (guild_data2 == NULL) {
  19233. ShowWarning("buildin_getguildalliance: Requesting non-existent GuildID2 '%d'.\n", guild_id2);
  19234. script_pushint(st, -1);
  19235. return SCRIPT_CMD_FAILURE;
  19236. }
  19237. ARR_FIND(0, MAX_GUILDALLIANCE, i, guild_data1->alliance[i].guild_id == guild_id2);
  19238. if (i == MAX_GUILDALLIANCE) {
  19239. script_pushint(st, 0);
  19240. return SCRIPT_CMD_SUCCESS;
  19241. }
  19242. if (guild_data1->alliance[i].opposition)
  19243. script_pushint(st, 2);
  19244. else
  19245. script_pushint(st, 1);
  19246. return SCRIPT_CMD_SUCCESS;
  19247. }
  19248. /*
  19249. * openstorage2 <storage_id>,<mode>{,<account_id>}
  19250. * mode @see enum e_storage_mode
  19251. **/
  19252. BUILDIN_FUNC(openstorage2) {
  19253. int stor_id = script_getnum(st, 2);
  19254. TBL_PC *sd = NULL;
  19255. if (!script_accid2sd(4, sd)) {
  19256. script_pushint(st, 0);
  19257. return SCRIPT_CMD_FAILURE;
  19258. }
  19259. if (!storage_exists(stor_id)) {
  19260. ShowError("buildin_openstorage2: Invalid storage_id '%d'!\n", stor_id);
  19261. return SCRIPT_CMD_FAILURE;
  19262. }
  19263. script_pushint(st, storage_premiumStorage_load(sd, stor_id, script_getnum(st, 3)));
  19264. return SCRIPT_CMD_SUCCESS;
  19265. }
  19266. #include "../custom/script.inc"
  19267. // declarations that were supposed to be exported from npc_chat.c
  19268. #ifdef PCRE_SUPPORT
  19269. BUILDIN_FUNC(defpattern);
  19270. BUILDIN_FUNC(activatepset);
  19271. BUILDIN_FUNC(deactivatepset);
  19272. BUILDIN_FUNC(deletepset);
  19273. #endif
  19274. /** Regular expression matching
  19275. * preg_match(<pattern>,<string>{,<offset>})
  19276. */
  19277. BUILDIN_FUNC(preg_match) {
  19278. #ifdef PCRE_SUPPORT
  19279. pcre *re;
  19280. pcre_extra *pcreExtra;
  19281. const char *error;
  19282. int erroffset, r, offset = 0;
  19283. int subStrVec[30];
  19284. const char* pattern = script_getstr(st,2);
  19285. const char* subject = script_getstr(st,3);
  19286. if (script_hasdata(st,4))
  19287. offset = script_getnum(st,4);
  19288. re = pcre_compile(pattern, 0, &error, &erroffset, NULL);
  19289. pcreExtra = pcre_study(re, 0, &error);
  19290. r = pcre_exec(re, pcreExtra, subject, (int)strlen(subject), offset, 0, subStrVec, 30);
  19291. pcre_free(re);
  19292. if (pcreExtra != NULL)
  19293. pcre_free(pcreExtra);
  19294. if (r < 0)
  19295. script_pushint(st,0);
  19296. else
  19297. script_pushint(st,(r > 0) ? r : 30 / 3);
  19298. return SCRIPT_CMD_SUCCESS;
  19299. #else
  19300. ShowDebug("script:preg_match: cannot run without PCRE library enabled.\n");
  19301. script_pushint(st,0);
  19302. return SCRIPT_CMD_SUCCESS;
  19303. #endif
  19304. }
  19305. /// script command definitions
  19306. /// for an explanation on args, see add_buildin_func
  19307. struct script_function buildin_func[] = {
  19308. // NPC interaction
  19309. BUILDIN_DEF(mes,"s*"),
  19310. BUILDIN_DEF(next,""),
  19311. BUILDIN_DEF(close,""),
  19312. BUILDIN_DEF(close2,""),
  19313. BUILDIN_DEF(menu,"sl*"),
  19314. BUILDIN_DEF(select,"s*"), //for future jA script compatibility
  19315. BUILDIN_DEF(prompt,"s*"),
  19316. //
  19317. BUILDIN_DEF(goto,"l"),
  19318. BUILDIN_DEF(callsub,"l*"),
  19319. BUILDIN_DEF(callfunc,"s*"),
  19320. BUILDIN_DEF(return,"?"),
  19321. BUILDIN_DEF(getarg,"i?"),
  19322. BUILDIN_DEF(jobchange,"i??"),
  19323. BUILDIN_DEF(jobname,"i"),
  19324. BUILDIN_DEF(input,"r??"),
  19325. BUILDIN_DEF(warp,"sii?"),
  19326. BUILDIN_DEF2(warp, "warpchar", "sii?"),
  19327. BUILDIN_DEF(areawarp,"siiiisii??"),
  19328. BUILDIN_DEF(warpparty,"siii?"), // [Fredzilla] [Paradox924X]
  19329. BUILDIN_DEF(warpguild,"siii"), // [Fredzilla]
  19330. BUILDIN_DEF(setlook,"ii?"),
  19331. BUILDIN_DEF(changelook,"ii?"), // Simulates but don't Store it
  19332. BUILDIN_DEF2(setr,"set","rv?"),
  19333. BUILDIN_DEF(setr,"rv??"), // Not meant to be used directly, required for var++/var--
  19334. BUILDIN_DEF(setarray,"rv*"),
  19335. BUILDIN_DEF(cleararray,"rvi"),
  19336. BUILDIN_DEF(copyarray,"rri"),
  19337. BUILDIN_DEF(getarraysize,"r"),
  19338. BUILDIN_DEF(deletearray,"r?"),
  19339. BUILDIN_DEF(getelementofarray,"ri"),
  19340. BUILDIN_DEF(getitem,"vi?"),
  19341. BUILDIN_DEF(rentitem,"vi?"),
  19342. BUILDIN_DEF(rentitem2,"viiiiiiii?"),
  19343. BUILDIN_DEF(getitem2,"viiiiiiii?"),
  19344. BUILDIN_DEF(getnameditem,"vv"),
  19345. BUILDIN_DEF2(grouprandomitem,"groupranditem","i?"),
  19346. BUILDIN_DEF(makeitem,"visii"),
  19347. BUILDIN_DEF(makeitem2,"visiiiiiiiii"),
  19348. BUILDIN_DEF(delitem,"vi?"),
  19349. BUILDIN_DEF2(delitem,"storagedelitem","vi?"),
  19350. BUILDIN_DEF2(delitem,"guildstoragedelitem","vi?"),
  19351. BUILDIN_DEF2(delitem,"cartdelitem","vi?"),
  19352. BUILDIN_DEF(delitem2,"viiiiiiii?"),
  19353. BUILDIN_DEF2(delitem2,"storagedelitem2","viiiiiiii?"),
  19354. BUILDIN_DEF2(delitem2,"guildstoragedelitem2","viiiiiiii?"),
  19355. BUILDIN_DEF2(delitem2,"cartdelitem2","viiiiiiii?"),
  19356. BUILDIN_DEF2(enableitemuse,"enable_items",""),
  19357. BUILDIN_DEF2(disableitemuse,"disable_items",""),
  19358. BUILDIN_DEF(cutin,"si"),
  19359. BUILDIN_DEF(viewpoint,"iiiii"),
  19360. BUILDIN_DEF(heal,"ii?"),
  19361. BUILDIN_DEF(itemheal,"ii?"),
  19362. BUILDIN_DEF(percentheal,"ii?"),
  19363. BUILDIN_DEF(rand,"i?"),
  19364. BUILDIN_DEF(countitem,"v?"),
  19365. BUILDIN_DEF2(countitem,"storagecountitem","v?"),
  19366. BUILDIN_DEF2(countitem,"guildstoragecountitem","v?"),
  19367. BUILDIN_DEF2(countitem,"cartcountitem","v?"),
  19368. BUILDIN_DEF2(countitem,"countitem2","viiiiiii?"),
  19369. BUILDIN_DEF2(countitem,"storagecountitem2","viiiiiii?"),
  19370. BUILDIN_DEF2(countitem,"guildstoragecountitem2","viiiiiii?"),
  19371. BUILDIN_DEF2(countitem,"cartcountitem2","viiiiiii?"),
  19372. BUILDIN_DEF(checkweight,"vi*"),
  19373. BUILDIN_DEF(checkweight2,"rr"),
  19374. BUILDIN_DEF(readparam,"i?"),
  19375. BUILDIN_DEF(getcharid,"i?"),
  19376. BUILDIN_DEF(getnpcid,"i?"),
  19377. BUILDIN_DEF(getpartyname,"i"),
  19378. BUILDIN_DEF(getpartymember,"i??"),
  19379. BUILDIN_DEF(getpartyleader,"i?"),
  19380. BUILDIN_DEF(getguildname,"i"),
  19381. BUILDIN_DEF(getguildmaster,"i"),
  19382. BUILDIN_DEF(getguildmasterid,"i"),
  19383. BUILDIN_DEF(strcharinfo,"i?"),
  19384. BUILDIN_DEF(strnpcinfo,"i"),
  19385. BUILDIN_DEF(getequipid,"i?"),
  19386. BUILDIN_DEF(getequipuniqueid,"i?"),
  19387. BUILDIN_DEF(getequipname,"i?"),
  19388. BUILDIN_DEF(getbrokenid,"i?"), // [Valaris]
  19389. BUILDIN_DEF(repair,"i?"), // [Valaris]
  19390. BUILDIN_DEF(repairall,"?"),
  19391. BUILDIN_DEF(getequipisequiped,"i?"),
  19392. BUILDIN_DEF(getequipisenableref,"i?"),
  19393. BUILDIN_DEF(getequiprefinerycnt,"i?"),
  19394. BUILDIN_DEF(getequipweaponlv,"i?"),
  19395. BUILDIN_DEF(getequippercentrefinery,"i?"),
  19396. BUILDIN_DEF(successrefitem,"i??"),
  19397. BUILDIN_DEF(failedrefitem,"i?"),
  19398. BUILDIN_DEF(downrefitem,"i??"),
  19399. BUILDIN_DEF(statusup,"i?"),
  19400. BUILDIN_DEF(statusup2,"ii?"),
  19401. BUILDIN_DEF(bonus,"iv"),
  19402. BUILDIN_DEF2(bonus,"bonus2","ivi"),
  19403. BUILDIN_DEF2(bonus,"bonus3","ivii"),
  19404. BUILDIN_DEF2(bonus,"bonus4","ivvii"),
  19405. BUILDIN_DEF2(bonus,"bonus5","ivviii"),
  19406. BUILDIN_DEF(autobonus,"sii??"),
  19407. BUILDIN_DEF(autobonus2,"sii??"),
  19408. BUILDIN_DEF(autobonus3,"siiv?"),
  19409. BUILDIN_DEF(skill,"vi?"),
  19410. BUILDIN_DEF2(skill,"addtoskill","vi?"), // [Valaris]
  19411. BUILDIN_DEF(guildskill,"vi"),
  19412. BUILDIN_DEF(getskilllv,"v"),
  19413. BUILDIN_DEF(getgdskilllv,"iv"),
  19414. BUILDIN_DEF(basicskillcheck,""),
  19415. BUILDIN_DEF(getgmlevel,"?"),
  19416. BUILDIN_DEF(getgroupid,"?"),
  19417. BUILDIN_DEF(end,""),
  19418. BUILDIN_DEF(checkoption,"i?"),
  19419. BUILDIN_DEF(setoption,"i??"),
  19420. BUILDIN_DEF(setcart,"??"),
  19421. BUILDIN_DEF(checkcart,"?"),
  19422. BUILDIN_DEF(setfalcon,"??"),
  19423. BUILDIN_DEF(checkfalcon,"?"),
  19424. BUILDIN_DEF(setriding,"??"),
  19425. BUILDIN_DEF(checkriding,"?"),
  19426. BUILDIN_DEF(checkwug,"?"),
  19427. BUILDIN_DEF(checkmadogear,"?"),
  19428. BUILDIN_DEF(setmadogear,"??"),
  19429. BUILDIN_DEF2(savepoint,"save","sii???"),
  19430. BUILDIN_DEF(savepoint,"sii???"),
  19431. BUILDIN_DEF(gettimetick,"i"),
  19432. BUILDIN_DEF(gettime,"i"),
  19433. BUILDIN_DEF(gettimestr,"si"),
  19434. BUILDIN_DEF(openstorage,""),
  19435. BUILDIN_DEF(guildopenstorage,""),
  19436. BUILDIN_DEF(itemskill,"vi?"),
  19437. BUILDIN_DEF(produce,"i"),
  19438. BUILDIN_DEF(cooking,"i"),
  19439. BUILDIN_DEF(monster,"siisii???"),
  19440. BUILDIN_DEF(getmobdrops,"i"),
  19441. BUILDIN_DEF(areamonster,"siiiisii???"),
  19442. BUILDIN_DEF(killmonster,"ss?"),
  19443. BUILDIN_DEF(killmonsterall,"s?"),
  19444. BUILDIN_DEF(clone,"siisi????"),
  19445. BUILDIN_DEF(doevent,"s"),
  19446. BUILDIN_DEF(donpcevent,"s"),
  19447. BUILDIN_DEF(cmdothernpc,"ss"),
  19448. BUILDIN_DEF(addtimer,"is"),
  19449. BUILDIN_DEF(deltimer,"s"),
  19450. BUILDIN_DEF(addtimercount,"is"),
  19451. BUILDIN_DEF(initnpctimer,"??"),
  19452. BUILDIN_DEF(stopnpctimer,"??"),
  19453. BUILDIN_DEF(startnpctimer,"??"),
  19454. BUILDIN_DEF(setnpctimer,"i?"),
  19455. BUILDIN_DEF(getnpctimer,"i?"),
  19456. BUILDIN_DEF(attachnpctimer,"?"), // attached the player id to the npc timer [Celest]
  19457. BUILDIN_DEF(detachnpctimer,"?"), // detached the player id from the npc timer [Celest]
  19458. BUILDIN_DEF(playerattached,""), // returns id of the current attached player. [Skotlex]
  19459. BUILDIN_DEF(announce,"si?????"),
  19460. BUILDIN_DEF(mapannounce,"ssi?????"),
  19461. BUILDIN_DEF(areaannounce,"siiiisi?????"),
  19462. BUILDIN_DEF(getusers,"i"),
  19463. BUILDIN_DEF(getmapguildusers,"si"),
  19464. BUILDIN_DEF(getmapusers,"s"),
  19465. BUILDIN_DEF(getareausers,"siiii"),
  19466. BUILDIN_DEF(getareadropitem,"siiiiv"),
  19467. BUILDIN_DEF(enablenpc,"s"),
  19468. BUILDIN_DEF(disablenpc,"s"),
  19469. BUILDIN_DEF(hideoffnpc,"s"),
  19470. BUILDIN_DEF(hideonnpc,"s"),
  19471. BUILDIN_DEF(sc_start,"iii???"),
  19472. BUILDIN_DEF2(sc_start,"sc_start2","iiii???"),
  19473. BUILDIN_DEF2(sc_start,"sc_start4","iiiiii???"),
  19474. BUILDIN_DEF(sc_end,"i?"),
  19475. BUILDIN_DEF(getstatus, "i??"),
  19476. BUILDIN_DEF(getscrate,"ii?"),
  19477. BUILDIN_DEF(debugmes,"s"),
  19478. BUILDIN_DEF2(catchpet,"pet","i"),
  19479. BUILDIN_DEF2(birthpet,"bpet",""),
  19480. BUILDIN_DEF(resetlvl,"i?"),
  19481. BUILDIN_DEF(resetstatus,"?"),
  19482. BUILDIN_DEF(resetskill,"?"),
  19483. BUILDIN_DEF(skillpointcount,"?"),
  19484. BUILDIN_DEF(changebase,"i?"),
  19485. BUILDIN_DEF(changesex,"?"),
  19486. BUILDIN_DEF(changecharsex,"?"),
  19487. BUILDIN_DEF(waitingroom,"si?????"),
  19488. BUILDIN_DEF(delwaitingroom,"?"),
  19489. BUILDIN_DEF(waitingroomkick,"ss"),
  19490. BUILDIN_DEF(getwaitingroomusers, "?"),
  19491. BUILDIN_DEF2(waitingroomkickall,"kickwaitingroomall","?"),
  19492. BUILDIN_DEF(enablewaitingroomevent,"?"),
  19493. BUILDIN_DEF(disablewaitingroomevent,"?"),
  19494. BUILDIN_DEF2(enablewaitingroomevent,"enablearena",""), // Added by RoVeRT
  19495. BUILDIN_DEF2(disablewaitingroomevent,"disablearena",""), // Added by RoVeRT
  19496. BUILDIN_DEF(getwaitingroomstate,"i?"),
  19497. BUILDIN_DEF(warpwaitingpc,"sii?"),
  19498. BUILDIN_DEF(attachrid,"i"),
  19499. BUILDIN_DEF(addrid,"i?????"),
  19500. BUILDIN_DEF(detachrid,""),
  19501. BUILDIN_DEF(isloggedin,"i?"),
  19502. BUILDIN_DEF(setmapflagnosave,"ssii"),
  19503. BUILDIN_DEF(getmapflag,"si?"),
  19504. BUILDIN_DEF(setmapflag,"si??"),
  19505. BUILDIN_DEF(removemapflag,"si?"),
  19506. BUILDIN_DEF(pvpon,"s"),
  19507. BUILDIN_DEF(pvpoff,"s"),
  19508. BUILDIN_DEF(gvgon,"s"),
  19509. BUILDIN_DEF(gvgoff,"s"),
  19510. BUILDIN_DEF(emotion,"i??"),
  19511. BUILDIN_DEF(maprespawnguildid,"sii"),
  19512. BUILDIN_DEF(agitstart,""), // <Agit>
  19513. BUILDIN_DEF(agitend,""),
  19514. BUILDIN_DEF(agitcheck,""), // <Agitcheck>
  19515. BUILDIN_DEF(flagemblem,"i"), // Flag Emblem
  19516. BUILDIN_DEF(getcastlename,"s"),
  19517. BUILDIN_DEF(getcastledata,"si"),
  19518. BUILDIN_DEF(setcastledata,"sii"),
  19519. BUILDIN_DEF(requestguildinfo,"i?"),
  19520. BUILDIN_DEF(getequipcardcnt,"i"),
  19521. BUILDIN_DEF(successremovecards,"i"),
  19522. BUILDIN_DEF(failedremovecards,"ii"),
  19523. BUILDIN_DEF(marriage,"s"),
  19524. BUILDIN_DEF2(wedding_effect,"wedding",""),
  19525. BUILDIN_DEF(divorce,"?"),
  19526. BUILDIN_DEF(ispartneron,"?"),
  19527. BUILDIN_DEF(getpartnerid,"?"),
  19528. BUILDIN_DEF(getchildid,"?"),
  19529. BUILDIN_DEF(getmotherid,"?"),
  19530. BUILDIN_DEF(getfatherid,"?"),
  19531. BUILDIN_DEF(warppartner,"sii"),
  19532. BUILDIN_DEF(getitemname,"v"),
  19533. BUILDIN_DEF(getitemslots,"i"),
  19534. BUILDIN_DEF(makepet,"i"),
  19535. BUILDIN_DEF(getexp,"ii?"),
  19536. BUILDIN_DEF(getinventorylist,"?"),
  19537. BUILDIN_DEF(getskilllist,"?"),
  19538. BUILDIN_DEF(clearitem,"?"),
  19539. BUILDIN_DEF(classchange,"ii"),
  19540. BUILDIN_DEF(misceffect,"i"),
  19541. BUILDIN_DEF(playBGM,"s"),
  19542. BUILDIN_DEF(playBGMall,"s?????"),
  19543. BUILDIN_DEF(soundeffect,"si"),
  19544. BUILDIN_DEF(soundeffectall,"si?????"), // SoundEffectAll [Codemaster]
  19545. BUILDIN_DEF(strmobinfo,"ii"), // display mob data [Valaris]
  19546. BUILDIN_DEF(guardian,"siisi??"), // summon guardians
  19547. BUILDIN_DEF(guardianinfo,"sii"), // display guardian data [Valaris]
  19548. BUILDIN_DEF(petskillbonus,"iiii"), // [Valaris]
  19549. BUILDIN_DEF(petrecovery,"ii"), // [Valaris]
  19550. BUILDIN_DEF(petloot,"i"), // [Valaris]
  19551. BUILDIN_DEF(petskillattack,"viii"), // [Skotlex]
  19552. BUILDIN_DEF(petskillattack2,"viiii"), // [Valaris]
  19553. BUILDIN_DEF(petskillsupport,"viiii"), // [Skotlex]
  19554. BUILDIN_DEF(skilleffect,"vi"), // skill effect [Celest]
  19555. BUILDIN_DEF(npcskilleffect,"viii"), // npc skill effect [Valaris]
  19556. BUILDIN_DEF(specialeffect,"i??"), // npc skill effect [Valaris]
  19557. BUILDIN_DEF(specialeffect2,"i??"), // skill effect on players[Valaris]
  19558. BUILDIN_DEF(nude,"?"), // nude command [Valaris]
  19559. BUILDIN_DEF(mapwarp,"ssii??"), // Added by RoVeRT
  19560. BUILDIN_DEF(atcommand,"s"), // [MouseJstr]
  19561. BUILDIN_DEF2(atcommand,"charcommand","s"), // [MouseJstr]
  19562. BUILDIN_DEF(movenpc,"sii?"), // [MouseJstr]
  19563. BUILDIN_DEF(message,"ss"), // [MouseJstr]
  19564. BUILDIN_DEF(npctalk,"s?"), // [Valaris]
  19565. BUILDIN_DEF(mobcount,"ss"),
  19566. BUILDIN_DEF(getlook,"i?"),
  19567. BUILDIN_DEF(getsavepoint,"i?"),
  19568. BUILDIN_DEF(npcspeed,"i"), // [Valaris]
  19569. BUILDIN_DEF(npcwalkto,"ii"), // [Valaris]
  19570. BUILDIN_DEF(npcstop,""), // [Valaris]
  19571. BUILDIN_DEF(getmapxy,"rrri?"), //by Lorky [Lupus]
  19572. BUILDIN_DEF(mapid2name,"i"),
  19573. BUILDIN_DEF(checkoption1,"i?"),
  19574. BUILDIN_DEF(checkoption2,"i?"),
  19575. BUILDIN_DEF(guildgetexp,"i"),
  19576. BUILDIN_DEF(guildchangegm,"is"),
  19577. BUILDIN_DEF(logmes,"s"), //this command actls as MES but rints info into LOG file either SQL/TXT [Lupus]
  19578. BUILDIN_DEF(summon,"si??"), // summons a slave monster [Celest]
  19579. BUILDIN_DEF(isnight,""), // check whether it is night time [Celest]
  19580. BUILDIN_DEF(isday,""), // check whether it is day time [Celest]
  19581. BUILDIN_DEF(isequipped,"i*"), // check whether another item/card has been equipped [Celest]
  19582. BUILDIN_DEF(isequippedcnt,"i*"), // check how many items/cards are being equipped [Celest]
  19583. BUILDIN_DEF(cardscnt,"i*"), // check how many items/cards are being equipped in the same arm [Lupus]
  19584. BUILDIN_DEF(getrefine,""), // returns the refined number of the current item, or an item with index specified [celest]
  19585. BUILDIN_DEF(night,""), // sets the server to night time
  19586. BUILDIN_DEF(day,""), // sets the server to day time
  19587. #ifdef PCRE_SUPPORT
  19588. BUILDIN_DEF(defpattern,"iss"), // Define pattern to listen for [MouseJstr]
  19589. BUILDIN_DEF(activatepset,"i"), // Activate a pattern set [MouseJstr]
  19590. BUILDIN_DEF(deactivatepset,"i"), // Deactive a pattern set [MouseJstr]
  19591. BUILDIN_DEF(deletepset,"i"), // Delete a pattern set [MouseJstr]
  19592. #endif
  19593. BUILDIN_DEF(preg_match,"ss?"),
  19594. BUILDIN_DEF(dispbottom,"s??"), //added from jA [Lupus]
  19595. BUILDIN_DEF(recovery,"i???"),
  19596. BUILDIN_DEF(getpetinfo,"i?"),
  19597. BUILDIN_DEF(gethominfo,"i?"),
  19598. BUILDIN_DEF(getmercinfo,"i?"),
  19599. BUILDIN_DEF(checkequipedcard,"i"),
  19600. BUILDIN_DEF(jump_zero,"il"), //for future jA script compatibility
  19601. BUILDIN_DEF(globalmes,"s?"), //end jA addition
  19602. BUILDIN_DEF(unequip,"i?"), // unequip command [Spectre]
  19603. BUILDIN_DEF(getstrlen,"s"), //strlen [Valaris]
  19604. BUILDIN_DEF(charisalpha,"si"), //isalpha [Valaris]
  19605. BUILDIN_DEF(charat,"si"),
  19606. BUILDIN_DEF(setchar,"ssi"),
  19607. BUILDIN_DEF(insertchar,"ssi"),
  19608. BUILDIN_DEF(delchar,"si"),
  19609. BUILDIN_DEF(strtoupper,"s"),
  19610. BUILDIN_DEF(strtolower,"s"),
  19611. BUILDIN_DEF(charisupper, "si"),
  19612. BUILDIN_DEF(charislower, "si"),
  19613. BUILDIN_DEF(substr,"sii"),
  19614. BUILDIN_DEF(explode, "rss"),
  19615. BUILDIN_DEF(implode, "r?"),
  19616. BUILDIN_DEF(sprintf,"s*"), // [Mirei]
  19617. BUILDIN_DEF(sscanf,"ss*"), // [Mirei]
  19618. BUILDIN_DEF(strpos,"ss?"),
  19619. BUILDIN_DEF(replacestr,"sss??"),
  19620. BUILDIN_DEF(countstr,"ss?"),
  19621. BUILDIN_DEF(setnpcdisplay,"sv??"),
  19622. BUILDIN_DEF(compare,"ss"), // Lordalfa - To bring strstr to scripting Engine.
  19623. BUILDIN_DEF(strcmp,"ss"),
  19624. BUILDIN_DEF(getiteminfo,"ii"), //[Lupus] returns Items Buy / sell Price, etc info
  19625. BUILDIN_DEF(setiteminfo,"iii"), //[Lupus] set Items Buy / sell Price, etc info
  19626. BUILDIN_DEF(getequipcardid,"ii"), //[Lupus] returns CARD ID or other info from CARD slot N of equipped item
  19627. // [zBuffer] List of mathematics commands --->
  19628. BUILDIN_DEF(sqrt,"i"),
  19629. BUILDIN_DEF(pow,"ii"),
  19630. BUILDIN_DEF(distance,"iiii"),
  19631. BUILDIN_DEF2(minmax,"min", "*"),
  19632. BUILDIN_DEF2(minmax,"minimum", "*"),
  19633. BUILDIN_DEF2(minmax,"max", "*"),
  19634. BUILDIN_DEF2(minmax,"maximum", "*"),
  19635. // <--- [zBuffer] List of mathematics commands
  19636. BUILDIN_DEF(md5,"s"),
  19637. // [zBuffer] List of dynamic var commands --->
  19638. BUILDIN_DEF(getd,"s"),
  19639. BUILDIN_DEF(setd,"sv?"),
  19640. BUILDIN_DEF(callshop,"s?"), // [Skotlex]
  19641. BUILDIN_DEF(npcshopitem,"sii*"), // [Lance]
  19642. BUILDIN_DEF(npcshopadditem,"sii*"),
  19643. BUILDIN_DEF(npcshopdelitem,"si*"),
  19644. BUILDIN_DEF(npcshopattach,"s?"),
  19645. BUILDIN_DEF(equip,"i?"),
  19646. BUILDIN_DEF(autoequip,"ii"),
  19647. BUILDIN_DEF(setbattleflag,"si"),
  19648. BUILDIN_DEF(getbattleflag,"s"),
  19649. BUILDIN_DEF(setitemscript,"is?"), //Set NEW item bonus script. Lupus
  19650. BUILDIN_DEF(disguise,"i?"), //disguise player. Lupus
  19651. BUILDIN_DEF(undisguise,"?"), //undisguise player. Lupus
  19652. BUILDIN_DEF(getmonsterinfo,"ii"), //Lupus
  19653. BUILDIN_DEF(addmonsterdrop,"vii"), //Akinari [Lupus]
  19654. BUILDIN_DEF(delmonsterdrop,"vi"), //Akinari [Lupus]
  19655. BUILDIN_DEF(axtoi,"s"),
  19656. BUILDIN_DEF(query_sql,"s*"),
  19657. BUILDIN_DEF(query_logsql,"s*"),
  19658. BUILDIN_DEF(escape_sql,"v"),
  19659. BUILDIN_DEF(atoi,"s"),
  19660. BUILDIN_DEF(strtol,"si"),
  19661. // [zBuffer] List of player cont commands --->
  19662. BUILDIN_DEF(rid2name,"i"),
  19663. BUILDIN_DEF(pcfollow,"ii"),
  19664. BUILDIN_DEF(pcstopfollow,"i"),
  19665. BUILDIN_DEF(pcblockmove,"ii"),
  19666. BUILDIN_DEF2(pcblockmove,"unitblockmove","ii"),
  19667. BUILDIN_DEF(pcblockskill,"ii"),
  19668. BUILDIN_DEF2(pcblockskill,"unitblockskill","ii"),
  19669. // <--- [zBuffer] List of player cont commands
  19670. // [zBuffer] List of unit control commands --->
  19671. BUILDIN_DEF(unitexists,"i"),
  19672. BUILDIN_DEF(getunittype,"i"),
  19673. BUILDIN_DEF(getunitname,"i"),
  19674. BUILDIN_DEF(setunitname,"is"),
  19675. BUILDIN_DEF(getunitdata,"i*"),
  19676. BUILDIN_DEF(setunitdata,"iii"),
  19677. BUILDIN_DEF(unitwalk,"iii?"),
  19678. BUILDIN_DEF2(unitwalk,"unitwalkto","ii?"),
  19679. BUILDIN_DEF(unitkill,"i"),
  19680. BUILDIN_DEF(unitwarp,"isii"),
  19681. BUILDIN_DEF(unitattack,"iv?"),
  19682. BUILDIN_DEF(unitstopattack,"i"),
  19683. BUILDIN_DEF(unitstopwalk,"i"),
  19684. BUILDIN_DEF(unittalk,"is"),
  19685. BUILDIN_DEF(unitemote,"ii"),
  19686. BUILDIN_DEF(unitskilluseid,"ivi??"), // originally by Qamera [Celest]
  19687. BUILDIN_DEF(unitskillusepos,"iviii?"), // [Celest]
  19688. // <--- [zBuffer] List of unit control commands
  19689. BUILDIN_DEF(sleep,"i"),
  19690. BUILDIN_DEF(sleep2,"i"),
  19691. BUILDIN_DEF(awake,"s"),
  19692. BUILDIN_DEF(getvariableofnpc,"rs"),
  19693. BUILDIN_DEF(warpportal,"iisii"),
  19694. BUILDIN_DEF2(homunculus_evolution,"homevolution",""), //[orn]
  19695. BUILDIN_DEF2(homunculus_mutate,"hommutate","?"),
  19696. BUILDIN_DEF(morphembryo,""),
  19697. BUILDIN_DEF2(homunculus_shuffle,"homshuffle",""), //[Zephyrus]
  19698. BUILDIN_DEF(checkhomcall,""),
  19699. BUILDIN_DEF(eaclass,"??"), //[Skotlex]
  19700. BUILDIN_DEF(roclass,"i?"), //[Skotlex]
  19701. BUILDIN_DEF(checkvending,"?"),
  19702. BUILDIN_DEF(checkchatting,"?"),
  19703. BUILDIN_DEF(checkidle,"?"),
  19704. BUILDIN_DEF(openmail,"?"),
  19705. BUILDIN_DEF(openauction,"?"),
  19706. BUILDIN_DEF(checkcell,"siii"),
  19707. BUILDIN_DEF(setcell,"siiiiii"),
  19708. BUILDIN_DEF(setwall,"siiiiis"),
  19709. BUILDIN_DEF(delwall,"s"),
  19710. BUILDIN_DEF(searchitem,"rs"),
  19711. BUILDIN_DEF(mercenary_create,"ii"),
  19712. BUILDIN_DEF(mercenary_heal,"ii"),
  19713. BUILDIN_DEF(mercenary_sc_start,"iii"),
  19714. BUILDIN_DEF(mercenary_get_calls,"i"),
  19715. BUILDIN_DEF(mercenary_get_faith,"i"),
  19716. BUILDIN_DEF(mercenary_set_calls,"ii"),
  19717. BUILDIN_DEF(mercenary_set_faith,"ii"),
  19718. BUILDIN_DEF(readbook,"ii"),
  19719. BUILDIN_DEF(setfont,"i"),
  19720. BUILDIN_DEF(areamobuseskill,"siiiiviiiii"),
  19721. BUILDIN_DEF(progressbar,"si"),
  19722. BUILDIN_DEF(pushpc,"ii"),
  19723. BUILDIN_DEF(buyingstore,"i"),
  19724. BUILDIN_DEF(searchstores,"ii"),
  19725. BUILDIN_DEF(showdigit,"i?"),
  19726. // WoE SE
  19727. BUILDIN_DEF(agitstart2,""),
  19728. BUILDIN_DEF(agitend2,""),
  19729. BUILDIN_DEF(agitcheck2,""),
  19730. // BattleGround
  19731. BUILDIN_DEF(waitingroom2bg,"sii???"),
  19732. BUILDIN_DEF(waitingroom2bg_single,"i????"),
  19733. BUILDIN_DEF(bg_team_setxy,"iii"),
  19734. BUILDIN_DEF(bg_warp,"isii"),
  19735. BUILDIN_DEF(bg_monster,"isiisi?"),
  19736. BUILDIN_DEF(bg_monster_set_team,"ii"),
  19737. BUILDIN_DEF(bg_leave,"?"),
  19738. BUILDIN_DEF(bg_destroy,"i"),
  19739. BUILDIN_DEF(areapercentheal,"siiiiii"),
  19740. BUILDIN_DEF(bg_get_data,"ii"),
  19741. BUILDIN_DEF(bg_getareausers,"isiiii"),
  19742. BUILDIN_DEF(bg_updatescore,"sii"),
  19743. BUILDIN_DEF(bg_join,"i????"),
  19744. BUILDIN_DEF(bg_create,"sii??"),
  19745. // Instancing
  19746. BUILDIN_DEF(instance_create,"s??"),
  19747. BUILDIN_DEF(instance_destroy,"?"),
  19748. BUILDIN_DEF(instance_id,""),
  19749. BUILDIN_DEF(instance_enter,"s????"),
  19750. BUILDIN_DEF(instance_npcname,"s?"),
  19751. BUILDIN_DEF(instance_mapname,"s?"),
  19752. BUILDIN_DEF(instance_warpall,"sii?"),
  19753. BUILDIN_DEF(instance_announce,"isi?????"),
  19754. BUILDIN_DEF(instance_check_party,"i???"),
  19755. BUILDIN_DEF(instance_check_guild,"i???"),
  19756. BUILDIN_DEF(instance_info,"si?"),
  19757. /**
  19758. * 3rd-related
  19759. **/
  19760. BUILDIN_DEF(makerune,"i?"),
  19761. BUILDIN_DEF(checkdragon,"?"),//[Ind]
  19762. BUILDIN_DEF(setdragon,"??"),//[Ind]
  19763. BUILDIN_DEF(ismounting,"?"),//[Ind]
  19764. BUILDIN_DEF(setmounting,"?"),//[Ind]
  19765. BUILDIN_DEF(checkre,"i"),
  19766. /**
  19767. * rAthena and beyond!
  19768. **/
  19769. BUILDIN_DEF(getargcount,""),
  19770. BUILDIN_DEF(getcharip,"?"),
  19771. BUILDIN_DEF(is_function,"s"),
  19772. BUILDIN_DEF(get_revision,""),
  19773. BUILDIN_DEF(get_githash,""),
  19774. BUILDIN_DEF(freeloop,"?"),
  19775. BUILDIN_DEF(getrandgroupitem,"i??"),
  19776. BUILDIN_DEF(cleanmap,"s"),
  19777. BUILDIN_DEF2(cleanmap,"cleanarea","siiii"),
  19778. BUILDIN_DEF(npcskill,"viii"),
  19779. BUILDIN_DEF(consumeitem,"v?"),
  19780. BUILDIN_DEF(delequip,"i?"),
  19781. BUILDIN_DEF(breakequip,"i?"),
  19782. BUILDIN_DEF(sit,"?"),
  19783. BUILDIN_DEF(stand,"?"),
  19784. //@commands (script based)
  19785. BUILDIN_DEF(bindatcmd, "ss??"),
  19786. BUILDIN_DEF(unbindatcmd, "s"),
  19787. BUILDIN_DEF(useatcmd, "s"),
  19788. //Quest Log System [Inkfish]
  19789. BUILDIN_DEF(questinfo, "ii??"),
  19790. BUILDIN_DEF(setquest, "i?"),
  19791. BUILDIN_DEF(erasequest, "i?"),
  19792. BUILDIN_DEF(completequest, "i?"),
  19793. BUILDIN_DEF(checkquest, "i??"),
  19794. BUILDIN_DEF(isbegin_quest,"i?"),
  19795. BUILDIN_DEF(changequest, "ii?"),
  19796. BUILDIN_DEF(showevent, "i??"),
  19797. //Bound items [Xantara] & [Akinari]
  19798. BUILDIN_DEF2(getitem,"getitembound","vii?"),
  19799. BUILDIN_DEF2(getitem2,"getitembound2","viiiiiiiii?"),
  19800. BUILDIN_DEF(countbound, "??"),
  19801. // Party related
  19802. BUILDIN_DEF(party_create,"s???"),
  19803. BUILDIN_DEF(party_addmember,"ii"),
  19804. BUILDIN_DEF(party_delmember,"??"),
  19805. BUILDIN_DEF(party_changeleader,"ii"),
  19806. BUILDIN_DEF(party_changeoption,"iii"),
  19807. BUILDIN_DEF(party_destroy,"i"),
  19808. // Clan system
  19809. BUILDIN_DEF(clan_join,"i?"),
  19810. BUILDIN_DEF(clan_leave,"?"),
  19811. BUILDIN_DEF(is_clientver,"ii?"),
  19812. BUILDIN_DEF2(montransform, "transform", "vi?????"), // Monster Transform [malufett/Hercules]
  19813. BUILDIN_DEF2(montransform, "active_transform", "vi?????"),
  19814. BUILDIN_DEF(vip_status,"i?"),
  19815. BUILDIN_DEF(vip_time,"i?"),
  19816. BUILDIN_DEF(bonus_script,"si????"),
  19817. BUILDIN_DEF(bonus_script_clear,"??"),
  19818. BUILDIN_DEF(getgroupitem,"i?"),
  19819. BUILDIN_DEF(enable_command,""),
  19820. BUILDIN_DEF(disable_command,""),
  19821. BUILDIN_DEF(getguildmember,"i??"),
  19822. BUILDIN_DEF(addspiritball,"ii?"),
  19823. BUILDIN_DEF(delspiritball,"i?"),
  19824. BUILDIN_DEF(countspiritball,"?"),
  19825. BUILDIN_DEF(mergeitem,"?"),
  19826. BUILDIN_DEF(mergeitem2,"??"),
  19827. BUILDIN_DEF(npcshopupdate,"sii?"),
  19828. BUILDIN_DEF(getattachedrid,""),
  19829. BUILDIN_DEF(getvar,"vi"),
  19830. BUILDIN_DEF(showscript,"s?"),
  19831. BUILDIN_DEF(ignoretimeout,"i?"),
  19832. BUILDIN_DEF(geteleminfo,"i?"),
  19833. BUILDIN_DEF(setquestinfo_level,"iii"),
  19834. BUILDIN_DEF(setquestinfo_req,"iii*"),
  19835. BUILDIN_DEF(setquestinfo_job,"ii*"),
  19836. BUILDIN_DEF(opendressroom,"i?"),
  19837. BUILDIN_DEF(navigateto,"s???????"),
  19838. BUILDIN_DEF(getguildalliance,"ii"),
  19839. BUILDIN_DEF(adopt,"vv"),
  19840. BUILDIN_DEF(getexp2,"ii?"),
  19841. BUILDIN_DEF(recalculatestat,""),
  19842. BUILDIN_DEF(hateffect,"ii"),
  19843. BUILDIN_DEF(getrandomoptinfo, "i"),
  19844. BUILDIN_DEF(getequiprandomoption, "iii?"),
  19845. BUILDIN_DEF(setrandomoption,"iiiii?"),
  19846. BUILDIN_DEF(needed_status_point,"ii?"),
  19847. BUILDIN_DEF(jobcanentermap,"s?"),
  19848. BUILDIN_DEF(openstorage2,"ii?"),
  19849. // WoE TE
  19850. BUILDIN_DEF(agitstart3,""),
  19851. BUILDIN_DEF(agitend3,""),
  19852. BUILDIN_DEF(agitcheck3,""),
  19853. BUILDIN_DEF(gvgon3,"s"),
  19854. BUILDIN_DEF(gvgoff3,"s"),
  19855. #include "../custom/script_def.inc"
  19856. {NULL,NULL,NULL},
  19857. };